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
9
votes
3 answers

non-lawful Monoid instances for building up AST not considered harmful?

I've seen a data type defined like the following with a corresponding Monoid instance: data Foo where FooEmpty :: String -> Foo FooAppend :: Foo -> Foo -> Foo -- | Create a 'Foo' with a specific 'String'. foo :: String -> Foo foo =…
illabout
  • 3,517
  • 1
  • 18
  • 39
9
votes
3 answers

Haskell: How to write a `Monoid` instance for something that depends on parameters

I am working on a small library for the university that does integer calculations in a cyclic group; Things like: (3 (% 11)) + (10 (% 11)) --> (2 (% 11)) 'Integers (% n)' clearly form a monoid under addition with '0 (% n)' as identity element.…
Qqwy
  • 5,214
  • 5
  • 42
  • 83
9
votes
1 answer

What can we do with Alternative but cannot do with Monoid?

I read Why MonadPlus and not Monad + Monoid? and I understand a theoretical difference, but I cannot figure out a practical difference, because for List it looks the same. mappend [1] [2] == [1] <|> [2] Yes. Maybe has different…
ais
  • 2,514
  • 2
  • 17
  • 24
9
votes
1 answer

Applicative instance for a tuple with monoid and function inside

I was trying to convert a haskell example, I came across earlier, to scalaz. The original example was this: ("Answer to the ", (*)) <*> ("Ultimate Question of ", 6) <*> ("Life, the Universe, and Everything", 7) Which, as far as I am able to…
George
  • 8,368
  • 12
  • 65
  • 106
9
votes
1 answer

How to write monoid protocol in Clojure?

The following does not work, for obvious reasons. (defprotocol Monoid (mappend [a b]) (mzero [])) mzero has zero arguments, and zero argument methods are not allowed (or do not make sense) in protocols. In Haskell or Scala, where the dispatch…
missingfaktor
  • 90,905
  • 62
  • 285
  • 365
8
votes
1 answer

Why does mconcat require a list rather than a Foldable?

While looking at the definition of Monoid I noticed that mconcat has the following definition (source): mconcat :: Monoid a => [a] -> a mconcat = foldr mappend mempty Why would the signature limit this to [a] rather than the more generic Foldable…
fphilipe
  • 9,739
  • 1
  • 40
  • 52
8
votes
1 answer

How can foldMap do the same as foldr when the latter does not have anything to do with monoids?

foldr and foldMap can be used to define each other as I understand. But how is that possible, as the latter uses monoids, while the former does not? Do we have any guarantees that the stuff the foldr works on can have a monoid?
The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156
8
votes
3 answers

Is my understanding of monoid valid?

So, I'm learning Haskell at the moment, and I would like to confirm or debunk my understanding of monoid. What I figured out from reading CIS194 course is that monoid is basically "API" for defining custom binary operation on custom set. Than I…
Reygoch
  • 1,204
  • 1
  • 11
  • 24
8
votes
2 answers

Can you formulate a monoid or semigroup for the radix sort?

This is the pseudocode for the radix sort: Pseudocode for Radix Sort: Radix-Sort(A, d) // Each key in A[1..n] is a d-digit integer. (Digits are // numbered 1 to d from right to left.) 1. for i = 1 to d do Use a stable sorting algorithm to sort A on…
hawkeye
  • 34,745
  • 30
  • 150
  • 304
8
votes
1 answer

Folding without Monoid instance

I have a simple tree structure: data Tree a = Leaf | Node a (Tree a) (Tree a) And a Foldable implementation: import qualified Data.Foldable as F instance F.Foldable Tree where foldMap f Leaf = mempty foldMap f (Node x l r) = F.foldMap…
saidelmark
  • 83
  • 3
8
votes
2 answers

Where do the foldl/foldr implementations of Foldable come from for binary trees in haskell?

I'm working through Learn You a Haskell, and I'm on the section on monoids. In this section, the author defines the foldMap method for a tree as follows: instance F.Foldable Tree where foldMap f Empty = mempty foldMap f (Node x l r) =…
MYV
  • 4,294
  • 6
  • 28
  • 24
8
votes
5 answers

Monoid mempty in pattern matching

I tried to write a generalized maximum function similar to the one in Prelude. My first naiv approach looked like this: maximum' :: (F.Foldable a, Ord b) => a b -> Maybe b maximum' mempty = Nothing maximum' xs = Just $ F.foldl1 max xs However, when…
mmh
  • 275
  • 1
  • 7
7
votes
1 answer

What are those class extensions for the Cartesian class for?

The Cartesian class from the constrained-category project is for categories, products of objects in which are objects in the same category yet again. I wonder about the classes Cartesian extends: class ( Category k …
Zhiltsoff Igor
  • 1,812
  • 8
  • 24
7
votes
3 answers

What is the purpose of the ArgMin and ArgMax type synonyms in Data.Semigroup?

The base library in Haskell has the following type synonyms in Data.Semigroup: type ArgMin a b = Min (Arg a b) type ArgMax a b = Max (Arg a b) Here are links to the haddocks: ArgMin and ArgMax What is the purpose of these two type synonyms? …
illabout
  • 3,517
  • 1
  • 18
  • 39
7
votes
1 answer

Identity element for std::min/float monoid

In this (very interesting) talk the speaker poses a question: What is the e value for the float/std::min monoid. In other words: what is the identity element for the monoid composed of standard C++ floats and std::min operation? The speaker says…
lukeg
  • 4,189
  • 3
  • 19
  • 40
1 2
3
15 16