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

Where is the Monoid instance on Comparison defined?

newtype Comparison a is defined in Data.Functor.Contravariant. In the version of this module defined in contravariant-1.5, the Monoid instance on Contravariant is defined as follows: instance Monoid (Comparison a) where mempty = Comparison (\_ _…
Joe
  • 1,479
  • 13
  • 22
3
votes
1 answer

ZipList Monoid haskell

The default monoid for lists in the GHC Prelude is concatenation. [1,2,3] <> [4,5,6] becomes [1,2,3] ++ [4,5,6] and thus [1,2,3,4,5,6] I want to write a ZipList Monoid instance that behaves like this: [ 1 <> 4 , 2 <> 5 , 3 <> 6 ] The result is…
Kevin Kamau
  • 236
  • 3
  • 11
3
votes
0 answers

Compatible code for Semigroup Monoid Proposal

Semigroup is becoming a superclass of Monoid. I did read the recommendations on that page for writing compatible code, but I neither like to conditionally depend on the semigroups package nor to put my mappend code in a top-level declaration. My…
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
3
votes
1 answer

Apply all filter functions to a value

I have a function that looks like this: def createBuilder(builder: InitialBuilder, name: Option[String], useCache: Boolean, timeout: Option[Long]): Builder = { val filters: List[Builder => Option[Builder]] = List( b =>…
Igal Tabachnik
  • 31,174
  • 15
  • 92
  • 157
3
votes
1 answer

Function Types as Monoid Instances

I have a function that looks like this transition :: State -> ([State], [State]) Given the particular domain of my problem, I know how to chain together two successive transition function calls, something like this: transition `chain` trainsition…
Anton Xue
  • 813
  • 11
  • 25
3
votes
3 answers

What is the identity of the type?

I have following data type: data Bull = Fools | Twoo deriving (Eq, Show) and use Monoid to implement it: instance Monoid Bull where mempty = Fools mappend _ _ = Fools As you can see, mempty is the identity function the identity laws does…
softshipper
  • 32,463
  • 51
  • 192
  • 400
3
votes
1 answer

How to combine arrays of monoid type?

A list or array of monoid type A is a monoid too. Now I would like to combine arrays of integers using cats. scala> 1 |+| 2 res1: Int = 3 scala> Array(1, 2, 3) |+| Array(1, 2, 3) :21: error: value |+| is not a member of Array[Int] …
Michael
  • 41,026
  • 70
  • 193
  • 341
3
votes
1 answer

Relation between the Semigroupoid and Semigroup classes

In the last week I've been trying to grasp some of Haskell's "core" types and type classes (but have been studying Haskell for at most two weeks total), and I've found something that bugs me: a "Semigroupoid" is a generalization of a "Category",…
3
votes
1 answer

What does adjoining mean in practical uses?

I often run into term "adjoin" when trying to understand some concepts. Those things are too abstract for me to understand as I'm neither expert in field nor category theory. The simplest case I found is a Monoid Maybe a instance which often behaves…
sevo
  • 4,559
  • 1
  • 15
  • 31
3
votes
2 answers

Endofunction as Monoid

I'm trying this (for learning purposes): {-# LANGUAGE FlexibleInstances #-} instance Monoid (a -> a) where mempty = id mappend f g = f . g expecting id <> id to be equal to id . id However, with (id <> id) 1 I receive this error: Non…
Ivan Kleshnin
  • 1,667
  • 2
  • 21
  • 24
3
votes
3 answers

Automatic propagation of monoidal mappend

Let's say I have a monoid defined as this: data TotalLine = TotalLine { totalQuantity :: Int, orderTotal :: Float } instance Monoid TotalLine where mempty = zero mappend = add As totalQuantity and orderTotal are both numbers they also form a…
Batou99
  • 869
  • 10
  • 19
3
votes
2 answers

Monad: Why does Identity matter, what's going to happen if there's no such special member in a set?

I'm trying to learn the concept of monad, I'm watching this excellent video Brian Beckend trying to explain what is monad. When he talks about monoid, it's a collection of types, it has a rule of composition, and this composition has to obey 2…
Aaron Shen
  • 8,124
  • 10
  • 44
  • 86
3
votes
1 answer

A Monoid application to subtypes doesn't compile with append operator, but works when explicitly called

I am making a Monoid for combining strategies for Retry Execution, and the RetryExecutor[T] is the based type. I have defined the following base type and a monoid: trait RetryExecutor[C] { def retry[T](f: C => T)(context: C): T def predicate:…
PlexQ
  • 3,154
  • 2
  • 21
  • 26
3
votes
2 answers

What are the implications of the differences between a monoid and a ring?

This is an example of a monoid in Haskell: > import Data.Monoid > Sum 5 <> Sum 6 <> Sum 10 Sum {getSum = 21} > mconcat [Sum 5, Sum 6, Sum 10] Sum {getSum = 21} > getSum $ mconcat $ map Sum [5, 6, 10] 21 > getProduct $ mconcat $ map Product [5, 6,…
hawkeye
  • 34,745
  • 30
  • 150
  • 304
2
votes
0 answers

Is there a semigroup/monoid in the context of a monad?

I'm giving my tensor operations a notion of sharing, using a monadic context Shared (implemented as State Nat), so (+) : Tensor F64 -> Tensor F64 -> Tensor F64 becomes (+) : Tensor F64 -> Tensor F64 -> Shared $ Tensor F64 If I do this, (+) can't…
joel
  • 6,359
  • 2
  • 30
  • 55