Questions tagged [monoids]

A monoid is a set that is closed under an associative binary operation and has an identity element I ∈ Such that for all a ∈ S, Ia = aI = a. Note that unlike a group, its elements need not have inverses. It can also be thought of as a semigroup with an identity element.

A monoid is a set that is closed under an associative binary operation and has an identity element I ∈ Such that for all a ∈ S, Ia = aI = a. Note that unlike a group, its elements need not have inverses. It can also be thought of as a semigroup with an identity element. Put simply, a monoid is an algebraic structure with an associative binary operation that has an identity element. Examples include:

  • lists under concatenation
  • numbers under addition or multiplication
  • booleans under conjunction or disjunction
  • sets under union or intersection
  • functions from a type to itself, under composition

Note that in most of these cases the operation is also commutative, but it need not be; concatenation and function composition are not commutative.

Useful links:

Wikipedia - Monoid

Wikipedia - Monoid (category theory)

239 questions
7
votes
5 answers

Is Last a free monoid?

The free monoids are often being regarded as "list monoids". Yet, I am interested in other possible structures which might give us free monoids. Firstly, let us go over the definition of free monoids. I have never quite understood how is it possible…
Zhiltsoff Igor
  • 1,812
  • 8
  • 24
7
votes
3 answers

Why are Monoidal and Applicative laws telling us the same thing?

I have learnt about Monoidal being an alternative way to represent Applicative not so long ago. There is an interesting question on Typeclassopedia: (Tricky) Prove that given your implementations from the first exercise [pure and (<*>) written…
Zhiltsoff Igor
  • 1,812
  • 8
  • 24
7
votes
2 answers

What does Traversable is to Applicative contexts mean?

I am trying to understand Traversable with the help of https://namc.in/2018-02-05-foldables-traversals. Somewhere the author mentioned the following sentence: Traversable is to Applicative contexts what Foldable is to Monoid values. What did…
softshipper
  • 32,463
  • 51
  • 192
  • 400
7
votes
2 answers

Why is Maybe's Semigroup instance biased towards Just and the Monoid uses Nothing as its empty element?

Maybe expresses computations that might not produce a result due to an error. Therefore such computations must be short circuited. Now Maybe's Semigroup/Monoid instances seem to break with this semantics, because the former is biased towards Just…
user6445533
7
votes
1 answer

How do I use a monoid instance of a function?

Today I tried to reduce a list of functions trough monoid typeclass but the resulting function expects its argument to be an instance of Monoid for some reason. GHCI tells me that the type of mconcat [id, id, id, id] is Monoid a => a -> a. Yet I…
Ford O.
  • 1,394
  • 8
  • 25
7
votes
1 answer

Haskell - Implementing Monoid what happens if the operator is not associative

According to the definition or a monoid the binary operator must be associative e.g. A op (B op C) == (A op B) op C. The base mconcat definition in haskell is: mconcat = foldr mappend mempty Since I know the implementation details of the mconcat…
poida
  • 3,403
  • 26
  • 26
7
votes
4 answers

Haskell monoid foldable rose tree

I need to make a foldable instance for a Rose tree data structure: data Rose a = a :> [Rose a] deriving (Eq, Show) With the following monoid and rose-related class/instances: instance Functor Rose where fmap f (a :> bs) = (f a) :> (map…
user2999349
  • 859
  • 8
  • 21
6
votes
3 answers

Using monads, monoids, functors and arrows in practice

I recently ran into this post about useful resources for different aspects of functional programming, such as monads and monoids, etc. But the question is - what use can an average programmer make out of such concepts. I often run into "academic"…
SPIRiT_1984
  • 2,717
  • 3
  • 29
  • 46
6
votes
1 answer

Haskell newtype that flips semigroup operation?

Is there any newtype in base that would basically achieve the following? newtype SemigroupFlip a = SemigroupFlip a instance Semigroup a => Semigroup (SemigroupFlip a) where (SemigroupFlip a) <> (SemigroupFlip b) = SemigroupFlip (b <> a) instance…
Clinton
  • 22,361
  • 15
  • 67
  • 163
6
votes
3 answers

Is there a "unit" class? Would it be useful?

Is there a class for types having a single unit value (not sure of the correct terminology here) i.e. types with some pre-defined value? class Unit a where unit :: a instance Unit () where unit = () instance Unit (Maybe a) where unit =…
jberryman
  • 16,334
  • 5
  • 42
  • 83
6
votes
2 answers

Confused about why all morphisms for a monoid are not the same as the identity morphism

I am busy reading Bartosz Milewski's Category Theory book for programmers and I'm struggling with the depiction of non-identity morphisms when moving between describing a monoid as a set and a monoid as a category. I understand that when viewing a…
sdb
  • 109
  • 5
6
votes
2 answers

Haskell Monoid Instance Question for a newtype

I am trying to define an instance: newtype Join a = Join { getJoin :: a -> Bool } deriving Generic instance Monoid (Join a) where f <> g = ??? mempty = ??? The goal is that the function foldMap Join should return True if all functions in…
6
votes
4 answers

How to compose functions that return Bools to one function

There is a similar question I found here that asks almost the same thing, but not quite. The question I have is how to compose a list of functions of type (a -> Bool) to be one function that is also (a -> Bool). Ex. compose :: [(a -> Bool)] -> (a ->…
BryceTheGrand
  • 643
  • 3
  • 9
6
votes
1 answer

Scala Monoid[Map[A,B]]

I'm reading a book with the following: sealed trait Currency case object USD extends Currency ... other currency types case class Money(m: Map[Currency, BigDecimal]) { ... methods defined } The discussion goes on to recognize certain types of…
melston
  • 2,198
  • 22
  • 39
6
votes
1 answer

How does Monoid assist in parallel training?

The Readme of HLearn states that the Monoid typeclass is used for parallel batch training. I've seen trainMonoid mentioned in several files, but I'm having a difficulty to dissect this huge codebase. Could someone explain in beginner-friendly terms…
dimid
  • 7,285
  • 1
  • 46
  • 85