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
6
votes
1 answer

Why Monoid is not a requirement for foldr/foldl?

I am looking at the Foldable class in Haskell. Two of the methods fold, foldMap requires a Monoid instance. But foldr or foldl don't have any such constraint. fold :: Monoid m => t m -> m foldMap :: Monoid m => (a -> m) -> t a -> m foldr :: (a -> b…
zeronone
  • 2,912
  • 25
  • 28
6
votes
2 answers

Monoid Bool in Haskell

Of course the data type is not exact, but is this how (more or less) the Monoid Bool is implemented? import Data.Monoid data Bool' = T | F deriving (Show) instance Monoid (Bool') where mempty = T mappend T _ = T mappend _ T = T …
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
6
votes
1 answer

Debugging infinite Sum in Haskell

Say I have a function (it doesn't have any practical application, just an academic interest, thus weird way to write it, with monoids, applicative functors and fixpoint combinators) f :: Num a => a -> Sum a f = fix ((<>) <$> Sum <*>) It typechecks,…
dmedvinsky
  • 8,046
  • 3
  • 31
  • 25
6
votes
1 answer

How do you implement monoid interface for this tree in haskell?

Please excuse the terminology, my mind is still bending. The tree: data Ftree a = Empty | Leaf a | Branch ( Ftree a ) ( Ftree a ) deriving ( Show ) I have a few questions: If Ftree could not be Empty, would it no longer be a Monoid since there…
xiaolingxiao
  • 4,793
  • 5
  • 41
  • 88
5
votes
1 answer

Group values by a key with any Monoid

I would like to write a method mergeKeys that groups the values in an Iterable[(K, V)] by the keys. For example, I could write: def mergeKeysList[K, V](iter: Iterable[(K, V)]) = { iter.foldLeft(Map[K,…
schmmd
  • 18,650
  • 16
  • 58
  • 102
5
votes
2 answers

How to make my probability density type an instance of Monoid?

I have a type to describe post-calibration radiocarbon date probability distributions. The details and background don't matter for the question: It boils down to one probability value in _calPDFDens for each year in _calPDFCals: data CalPDF = CalPDF…
nevrome
  • 1,471
  • 1
  • 13
  • 28
5
votes
4 answers

Can the composite pattern be used to generate HTML from a tree and handle the indenting as well, or this inherently not possible?

I watched this video on the composite pattern, where the main example is how to use the pattern as a mean to generate HTML code from a tree structure describing a todo list where each item can be in turn a todo list, and it seems a convenient…
Enlico
  • 23,259
  • 6
  • 48
  • 102
5
votes
1 answer

Is this a good monoid action?

I think I might have stumbled across a general, albeit somewhat degenerate, monoid action. Pseudo-Haskell: instance (Monoid m, Monoid n) => Act m n where act mempty x = x -- let's pretend that we can use `mempty` as a pattern act _ _ =…
Benjamin Hodgson
  • 42,952
  • 15
  • 108
  • 157
5
votes
1 answer

How do operator associativity, the associative law and value dependencies of monads fit together?

On the one hand the monadic bind operator >>= is left associative (AFAIK). On the other hand the monad law demands associativity, i.e. evaluation order doesn't matter (like with monoids). Besides, monads encode a value dependency by making the next…
user5536315
5
votes
2 answers

Monoid homomorphism and isomorphism

I am reading the book 'Programming in Scala' (The red book). In the chapter about Monoids, I understand what a Monoid homomorphism is, for example: The String Monoid M with concatenation and length function f preserves the monoid structure, and…
5
votes
1 answer

Understanding curried function passed in to fold

I am having problems understanding this code from the Book FP in Scala. Here is the code: trait Monoid[A] { def op(a1: A, a2: A): A def zero: A } def endoMonoid[A]: Monoid[A => A] = new Monoid[A => A] { def op(f: A => A, g: A => A) = f…
Alejandro Alcalde
  • 5,990
  • 6
  • 39
  • 79
5
votes
1 answer

Does Scala's list form a monoid under the concatenation operator?

First of all, sorry, but I'm not a native English speaker. I will however try to do my best. I'm actually studying some theoretical concepts as a hobby to deepen my understandings of functional programming and have some questions to check that I…
Argurth
  • 625
  • 6
  • 18
5
votes
3 answers

Defining a monoid instance for a record type

Suppose I have a type like data Options = Options { _optionOne :: Maybe Integer , _optionTwo :: Maybe Integer , _optionThree :: Maybe String } deriving Show with many more fields. I would like to define a Monoid instance for this type,…
user4601931
  • 4,982
  • 5
  • 30
  • 42
5
votes
2 answers

Keeping IO lazy under append

I may have been under the false impression that Haskell is lazier than it is, but I wonder if there's a way to get the best of both worlds... Data.Monoid and Data.Semigroup define two variations of First. The monoidal version models the leftmost,…
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
5
votes
3 answers

Difference between associative and commutative

I am trying to understand associative in monoid. From the book, it says: Associativity simply says that you can associate the arguments of your operation differently and the result will be the same. For example: Prelude> (1 + 9001) +…
softshipper
  • 32,463
  • 51
  • 192
  • 400