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)

213 questions
787
votes
5 answers

A monad is just a monoid in the category of endofunctors, what's the problem?

Who first said the following? A monad is just a monoid in the category of endofunctors, what's the problem? And on a less important note, is this true and if so could you give an explanation (hopefully one that can be understood by someone who…
Roman A. Taycher
  • 16,401
  • 19
  • 81
  • 129
87
votes
1 answer

Distinction between typeclasses MonadPlus, Alternative, and Monoid?

The standard-library Haskell typeclasses MonadPlus, Alternative, and Monoid each provide two methods with essentially the same semantics: An empty value: mzero, empty, or mempty. An operator a -> a -> a that joins values in the typeclass together:…
00dani
  • 1,448
  • 15
  • 17
67
votes
4 answers

What is monoid homomorphism exactly?

I've read about monoid homomorphism from Monoid Morphisms, Products, and Coproducts and could not understand 100%. The author says (emphasis original): The length function maps from String to Int while preserving the monoid structure. Such a…
softshipper
  • 26,415
  • 36
  • 123
  • 260
50
votes
2 answers

Simple examples to illustrate Category, Monoid and Monad?

I am getting very confused with these three concepts. Is there any simple examples to illustrate the differences between Category, Monoid and Monad ? It would be very helpful if there is a illustration of these abstract concepts.
Znatz
  • 1,512
  • 2
  • 18
  • 28
41
votes
4 answers

Why MonadPlus and not Monad + Monoid?

I'm trying to understand the motivation behind the MonadPlus. Why is it necessary if there are already the typeclasses Monad and Monoid? Granted, instances of Monoid are concrete types, whereas instances of Monad require a single type parameter.…
mxxk
  • 6,943
  • 4
  • 30
  • 41
27
votes
4 answers

Examples of monoids/semigroups in programming

It is well-known that monoids are stunningly ubiquitous in programing. They are so ubiquitous and so useful that I, as a 'hobby project', am working on a system that is completely based on their properties (distributed data aggregation). To make the…
jkff
  • 16,670
  • 3
  • 46
  • 79
26
votes
2 answers

monoid vs monad in Scala

I have recently tried to find a good source on the difference between monads and monoids. Could someone provide a link to a good resource on this or perhaps take one's time to elaborate on the similarities/differences?
Bober02
  • 13,918
  • 28
  • 83
  • 157
24
votes
4 answers

What is practical use of monoids?

I'm reading Learn You a Haskell and I've already covered applicative and now I'm on monoids. I have no problem understanding the both, although I found applicative useful in practice and monoid isn't quite so. So I think I don't understand something…
Mark Karpov
  • 7,164
  • 2
  • 23
  • 54
24
votes
3 answers

Monoid vs MonadPlus

I am very new to both Monads and Monoids and recently also learned about MonadPlus. From what I see, Monoid and MonadPlus both provide a type with a associative binary operation and an identity. (I'd call this a semigroup in mathematical parlance.)…
Code-Apprentice
  • 69,701
  • 17
  • 115
  • 226
23
votes
2 answers

Monoidal parsing -- what is it?

I just stumbled upon the term monoidal parsing from a slide named "Introduction to Monoids" by Edward Kmett. The slide uses haskell throughout. Now when searching for the term I found nothing but a very few mentions of it and the most from the same…
Tarrasch
  • 9,028
  • 5
  • 38
  • 55
21
votes
1 answer

A basic Monoid definition gives "No instance for (Semigroup MyMonoid) arising from the superclasses of an instance declaration"

I am attempting to define Haskell integer sets with the union operation as a Monoid. module MyMonoid where import qualified Data.IntSet as S data MyMonoid = MyMonoid S.IntSet instance Monoid MyMonoid where mempty = MyMonoid S.empty MyMonoid…
W.P. McNeill
  • 13,777
  • 9
  • 63
  • 94
21
votes
1 answer

Why isn't Kleisli an instance of Monoid?

If you wish to append two functions of type (a -> m b) so you get only one function of the same type appending both results, you could use Kleisli to do so: instance (Monad m, Monoid b) => Monoid (Kleisli m a b) where mempty = Kleisli (\_ ->…
leo
  • 455
  • 2
  • 8
20
votes
2 answers

Why can't GHC derive instances for Monoid?

GHC has a few language flags, such as DeriveFunctor, DeriveDataTypeable etc., which enable compiler generation of derived instances for type classes other than those allowed in Haskell 98. This especially makes sense for something like Functor,…
mergeconflict
  • 7,861
  • 31
  • 63
18
votes
3 answers

Monoids and Num in Haskell

I have been learning Haskell over the past few months and I came across an example of Monoids that has me puzzled. Given these definitions: data Tree a = Empty | Node a (Tree a) (Tree a) deriving (Show, Read, Eq) instance F.Foldable Tree where …
Bradley Nowacki
  • 241
  • 2
  • 8
16
votes
5 answers

What's the practical value of all those newtype wrappers in `Data.Monoid`?

When looking at Data.Monoid, I see there are various newtype wrappers, such as All, Sum, or Product, which encode various kinds of monoids. However, when trying to use those wrappers, I can't help but wonder what's the benefit over using their…
danom
  • 525
  • 4
  • 7
1
2 3
14 15