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

Monoidal folds on fixed points

Given an arbitrary datastructure with a fixed point, can we construct an monoidal algebra without manually specifying all cases? Assume we are given the datatype Expr as below. Using the recursion-schemes library, we can derive a base functor ExprF,…
ThreeFx
  • 7,250
  • 1
  • 27
  • 51
5
votes
2 answers

Applicative implementation of Const Monoid

instance Monoid m => Applicative (Const m) where pure _ = Const mempty Const f <*> Const v = Const (f `mappend` v) I do not understand how can the definition of <*> type-check. On the left side f is constrained by the signature of <*> as in…
libeako
  • 2,324
  • 1
  • 16
  • 20
5
votes
3 answers

Are monads Writer m and Either e categorically dual?

I noticed there is a dual relation between Writer m and Either e monads. If m is a monoid, then unit :: () -> m join :: (m,m) -> m can be used to form a monad: return is composition: a -> ((),a) -> (m,a) join is composition: (m,(m,a)) -> ((m,m),a)…
sdcvvc
  • 25,343
  • 4
  • 66
  • 102
5
votes
2 answers

Haskell: Better way of writing Maybe Endo?

I have just discovered the Endo type thanks to the network-api-support package and I have found the need to be able to throw Maybe values into Endo's. As a result I have written a function called maybeEndo. Here is an example of it being…
Robert Massaioli
  • 13,379
  • 7
  • 57
  • 73
5
votes
1 answer

Use HSpec and QuickCheck to verify Data.Monoid properties

I'm trying to use HSpec and QuickCheck to verify properties of Monoids (associativity and identity element). I am going to verify particular instances, but would like to keep most of the code polymorphic. This is what I came up with after several…
maciekszajna
  • 325
  • 1
  • 4
  • 9
5
votes
2 answers

Can you formulate the insertion sort as a monoid in Clojure?

This is the code for an insertion sort in Clojure: (defn in-sort! [data] (letfn [(insert ([raw x](insert [] raw x)) ([sorted [y & raw] x] (if (nil? y) (conj sorted x) (if (<= x y ) (concat sorted [x,y] raw) …
hawkeye
  • 34,745
  • 30
  • 150
  • 304
5
votes
1 answer

Can you formulate the Bubble sort as a monoid or semigroup?

Given the following pseudocode for the bubble-sort procedure bubbleSort( A : list of sortable items ) repeat swapped = false for i = 1 to length(A) - 1 inclusive do: /* if this pair is out of order */ if A[i-1] > A[i]…
hawkeye
  • 34,745
  • 30
  • 150
  • 304
5
votes
1 answer

Applicative Instance for (Monad m, Monoid o) => m o?

Sorry for the terrible title. I'm trying to make an instance of Applicative for a Monad wrapping a type that is a Monoid. instance (Monad m, Monoid o) => Applicative (m o) where pure x = return mempty xm <*> ym = do x <- xm y…
Callum Rogers
  • 15,630
  • 17
  • 67
  • 90
5
votes
1 answer

Haskell - wrapping and unwrapping newtype wrappers - is there an easier way?

I'm writing a function pad that takes a list and pads it until it is a certain size. I tried 2 implementations: pad :: Monoid a => Int -> [a] -> [a] pad len list = replicate (len - length list) mempty ++ list and pad :: Int -> a -> [a] -> [a] pad…
Drew
  • 12,578
  • 11
  • 58
  • 98
5
votes
1 answer

How/can this type be made into a Monoid instance

I have data type: data Stuff s = Stuff { name :: s, idx :: Int } And want to make this into a monoid w/ the following implementations: tmappend :: Stuff s -> Stuff t -> Stuff (s,t) tmappend s1 s2 = Stuff (name s1, name s2) (idx s1 + idx…
xiaolingxiao
  • 4,793
  • 5
  • 41
  • 88
5
votes
1 answer

Instancing Monoid for a Type

I have a Type in Haskell to make a Map have several values associated to a key. If I compile the following code: type Mapa k v = Map k [v] instance Monoid (Mapa k v) where --mempty :: Mapa k v mempty = DM.empty --mappend :: Mapa k v -> Mapa k…
chamini2
  • 2,820
  • 2
  • 24
  • 37
5
votes
1 answer

Stack overflow in monoidal fold over large list

First some imports, import Control.Applicative import Data.Traversable as T import Data.Foldable as F import Data.Monoid Say I have a functor holding a pair of values, data Fret a = Fret a a deriving (Show) instance Functor Fret where fmap f…
bgamari
  • 5,913
  • 1
  • 18
  • 21
4
votes
1 answer

Is there a class that models "patches"?

I'm looking for a class in like the following in a Haskell library (or at least to know the mathematical name for such a thing): class Monoid patch => MyThing patch t where applyPatch :: t -> patch -> t I can have instances like this: instance…
Clinton
  • 22,361
  • 15
  • 67
  • 163
4
votes
2 answers

What is a cocartesian comonoid, and what is a cocartesian comonoidal functor?

I've been experimenting with monoids and Distributives lately, and I think I've found something of interest (described in my answer) - are these already known structures? (I've been unable to find any reference to them online, and I don't think I've…
janet
  • 221
  • 1
  • 9
4
votes
2 answers

How to use Monoid Scala?

trait Monoid[A] { def op(a1: A, a2: A): A def zero: A } def mapMergeMonoid[K, V](V: Monoid[V]): Monoid[Map[K, V]] = new Monoid[Map[K, V]] { override def op(a1: Map[K, V], a2: Map[K, V]): Map[K, V] = (a1.keySet ++…
andrew17
  • 851
  • 2
  • 10
  • 25