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

Hiding typeclass instance declarations while importing in Haskell

I am trying to make a tic-tac-toe game, and I decided to construct types for cells (elements of the board) and the board as follows: data Cell = X | O deriving (Show, Eq) type Board = [[Maybe Cell]] Here, Nothing represents an empty cell, (Just X)…
Jad Issa
  • 65
  • 1
  • 6
4
votes
2 answers

LINQ equivalent of f#'s builder.Zero()?

So, I've become quite addicted to f#'s computation expressions and custom builders. I have to use c# for most of my daily work but still want to use LINQ expressions with my own monads/monoids. Does anybody know if there's a c# analog to f#'s Zero…
Jonathan Wilson
  • 4,138
  • 1
  • 24
  • 36
4
votes
2 answers

Fold a Foldable of Maybe (Monoid) ignoring the missing values in Haskell

I am working with some data that has missing values, which is simply represented as lists of Maybe values. I would like to perform various aggregates/statistical operations, which simply ignore the missing values. This is related to the following…
kloffy
  • 2,928
  • 2
  • 25
  • 34
4
votes
2 answers

How to write a Semigroup Instance?

Problem Given a data type, implement the Semigroup instance. Heres the data type I was given to implement: data Or a b = Fst a | Snd b deriving (Eq, Show, Num). And it should function like so: Prelude> Fst 1 <> Snd 2 Snd 2 Prelude> Fst 1 <> Fst…
Jonathan Portorreal
  • 2,730
  • 4
  • 21
  • 38
4
votes
1 answer

Scalaz `Tag.apply`: How does it work?

Hi I am studying the Advanced Scala book, and I have some trouble understading this piece of code from scalaz source: object Tag { /** `subst` specialized to `Id`. * * @todo According to Miles, @specialized doesn't help here. Maybe…
Filippo De Luca
  • 704
  • 5
  • 21
4
votes
1 answer

Why is there no type class for monoids on functors in Haskell?

I admit that this question is a bit unspecific, but I was wondering why I never stumbled upon a type class for monoids on functors in Haskell. Did I just miss it, is there good reason for this absence or is it entirely due to historic causes? IMHO,…
4
votes
1 answer

Why is there no fold' method?

I noticed that the Foldable class contains fold, foldl, foldr, foldl', and foldr', but there's no fold' (for strict monoidal folds) How can I emulate the behavior of fold' with an IntMap (which is implemented as a tree, but doesn't give direct…
dspyz
  • 5,280
  • 2
  • 25
  • 63
3
votes
1 answer

How to generate a list using monoid concat?

I have been doing functional programming for quite some time now, but I am new to monoids and other pure abstractions. I am wondering if there is a way to generate a list of values by using concat function defined for a monoid? For example, with 0…
tht
  • 31
  • 2
3
votes
2 answers

haskell, is IO Monoid associativity broken?

In haskell IO type has instance of Monoid: instance Monoid a => Monoid (IO a) where mempty = pure empty if I have three actions which share some state, and change behavior of each other via side effect, this can lead to breaking associativity…
Evg
  • 2,978
  • 5
  • 43
  • 58
3
votes
2 answers

Is there a Monad which collects results and `mappend`s them?

I have the following pattern of a Reader with a Semigroup Element: runFunction :: Reader Env Element runFunction = do a <- getA b <- getB c <- getC return $ a <> b <> c Where getA :: Reader Env Element. Is there a way to: runFunction = do …
3
votes
1 answer

What are the practical implications that both applicatives/monads preserve monoidal structure but in a slightly different way?

I've read this Q&A but don't understand the category theory part. Here is my reasoning so far: When I look at the types F (a -> b) -> F a -> F b (a -> M b) -> M a -> M b a -> F a a -> M a the only portion that resembles a monoid at the type level…
user5536315
3
votes
1 answer

Finding the minimal form for an element of the free idempotent monoid in Haskell

The free idempotent monoid is like the free monoid, but quotiented by the equation x² = x; for instance, aa = a, bcbcb = b(cb)(cb) = bcb, However, to find the minimal form of a word in the monoid, expansion (x = x²) as well as contraction (x² = x)…
Zemyla
  • 468
  • 3
  • 7
3
votes
2 answers

What is the relationship between a monoid and a functor?

I'm trying to understand the relationship between a functor and a monoid. They are often mentioned together but I haven't been able to quite connect the dots. I understand that, simply speaking, in programming a monoid can be thought of as a…
automasean
  • 391
  • 2
  • 8
3
votes
1 answer

MonadPlus IO isn't a monoid

Instance MonadPlus IO is unique because mzero throws: Prelude Control.Monad> mzero *** Exception: user error (mzero) So accordingly, MonadPlus IO implies that it is also intended for errors. mzero apparently serves as the identity element if the…
Dannyu NDos
  • 2,458
  • 13
  • 32
3
votes
2 answers

How do I call a monoid that also supports lifting?

What I mean is a device like a list: mempty = [ ] lift x = [x] mappend = (++) Is it merely IsList?
Ignat Insarov
  • 4,660
  • 18
  • 37