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

Computing all infix products for a monoid / semigroup

Introduction: Infix products for a group Suppose I have a group G = (G, *) and a list of elements A = {0, 1, ..., n} ⊂ ℕ x : A -> G If our goal is to implement a function f : A × A -> G such that f(i, j) = x(i) * x(i+1) * ... * x(j) (and we…
Cactus
  • 27,075
  • 9
  • 69
  • 149
0
votes
1 answer

mempty with different definition depending on whether mempty is left or right arg?

I have the following datatype and semigroup instance: data Or a b = Fst a | Snd b deriving (Eq, Show) instance Semigroup (Or a b) where (<>) (Fst a) (Fst b) = Fst b (<>) (Snd a) (Fst b) = Snd a (<>) (Fst a) (Snd b) = Snd b (<>) (Snd a)…
The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156
0
votes
1 answer

Clojure cats append nil behaviour

I am using funcool/cats, append monoid with the following code : (m/mappend (maybe/just [1 2 3]) nil (maybe/just [4 5 6]) (maybe/nothing)) ;;=> # What is the rationale for treating nil as…
nha
  • 17,623
  • 13
  • 87
  • 133
0
votes
1 answer

Generic approach to finding index of element

There have been some major changes in base library, so I'm wondering whether today idiomatic solution would involve fmap, Maybe, Monoid, First, Foldable and maybe other classes. I figured I would need to fold a complex structure of a Monoid with zip…
sevo
  • 4,559
  • 1
  • 15
  • 31
0
votes
1 answer

Add a list to a list of lists in Haskell

How can I add a list to a list of lists? Say I want to add itemz to bagList, which is a list of lists. How can I do that? bagList itemz = mappend bagList itemz
Vlad Balanescu
  • 664
  • 5
  • 27
0
votes
1 answer

Algebird HyperLogLog - Register Index and Register Value Bits

I am new to HyperLogLog and Scala, and am trying to use the Twitter Algebird's HyperLogLog implementation - https://github.com/twitter/algebird/blob/develop/algebird-core/src/main/scala/com/twitter/algebird/HyperLogLog.scala. In other…
DJElbow
  • 3,345
  • 11
  • 41
  • 52
0
votes
1 answer

Folds above functions - monoids

Why is the fold above the length of a list no monoid? length = foldr (\_ n -> 1+n) 0 Isn't it associative and has the neutral element 0 so that it should be a monoid?
fragant
  • 361
  • 4
  • 16
0
votes
0 answers

How to check XML with monoid in Scala?

Suppose I need to validate an input XML, e.g. a1a1a1 a2a2a2 I need to make sure that its root element has label "a" and children with labels "a1", "a2", "a3" and texts "a1a1a1", "a2a2a2" and ""…
Michael
  • 41,026
  • 70
  • 193
  • 341
0
votes
1 answer

What is this signature { zero : α; append : α -> β -> α; } name?

I have Monoid and Builder interfaces: interface IMonoidFor { T Zero { get; } T Append(T a, T b); } interface IBuilderFor { IBuilderFor Append(T value); T Result(); } And want to implement from Monoid to Builder converting…
Valentyn Zakharenko
  • 2,710
  • 1
  • 21
  • 47
0
votes
1 answer

Haskell data type Counts for State Monad

How can I use the following data type for a Monoid instance of it? data Counts = Counts { binds :: Int, returns :: Int, gets :: Int, puts :: Int } deriving (Eq, Show) E.g. I figured something like: mempty = Counts { 0, 0,…
user2999349
  • 859
  • 8
  • 21
0
votes
2 answers

Proof of Paper, Scissor, Rock as Monoid Instance in Coq

So while learning Coq I did a simple example with the game paper, scissor, rock. I defined a data type. Inductive PSR : Set := paper | scissor | rock. And three functions: Definition me (elem: PSR) : PSR := elem. Definition beats (elem: PSR) : PSR…
Cristian Garcia
  • 9,630
  • 6
  • 54
  • 75
0
votes
1 answer

Problems using Nothing bottom type while trying to create generic zeros for parametrized monoids

Here's my code. It permits to create typesafe MongoDB queries using Casbah trait TypesafeQuery[ObjectType, BuildType] { def build: BuildType } trait TypesafeMongoQuery[ObjectType] extends TypesafeQuery[ObjectType, DBObject] case class…
Sebastien Lorber
  • 89,644
  • 67
  • 288
  • 419
0
votes
1 answer

Foldable and Trees

I have the following definition of a Tree data Tree a = Leaf a | Node [Tree a] deriving (Show) And the following instance of foldable: instance Foldable (Tree) where foldMap f (Leaf t) = (f t) foldMap f (Node t) = (foldMap…
Paul K
  • 77
  • 5
-2
votes
1 answer

Why we use a monoid and a functor there?

I'm new in Haskell. I can't figure out why we use a monoid and instance Functor Matrix in the code bellow and how instance Functor Matrix works? instance Functor Matrix where fmap f (M n m v) = M n m $ fmap (fmap f) v instance Num a => Num…
dmitryvodop
  • 167
  • 2
  • 2
  • 6
1 2 3
15
16