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
16
votes
4 answers

What is the main difference between Free Monoid and Monoid?

Looks like I have a pretty clear understanding what a Monoid is in Haskell, but last time I heard about something called a free monoid. What is a free monoid and how does it relate to a monoid? Can you provide an example in Haskell?
mkUltra
  • 2,828
  • 1
  • 22
  • 47
16
votes
1 answer

Why is there not 'Alternative' instance for 'Control.Applicative.Const'

There is an instance Monoid a => Monoid (Const a b) for the Const functor from Control.Applicative. There is also an instance Monoid m => Applicative (Const m). I would therefore expect that there is also an instance Monoid m => Alternative (Const…
meiersi
  • 308
  • 1
  • 5
15
votes
1 answer

Why does the Alternative typeclass need to be a sub-class of Control.Applicative

Haskell provides a standard typeclass 'Alternative' that effectively provides the <|> operator for any type that is also an Applicative. As I understand it Alternative is considered a Monoid on Applicative's, however the <|> operator seems to make…
James Davies
  • 9,602
  • 5
  • 38
  • 42
15
votes
1 answer

Understanding Data.Functor.Constant constructor and applicative laws

I'm confused about Data.Functor.Constant's type constructor and also how it works with applicative. First the constructor: When I examine the type of Constant :: a -> Constant a b I see it takes an a, but returns a Constant a b Where does the b…
Brian
  • 692
  • 3
  • 14
14
votes
3 answers

Scala PartialFunction can be Monoid?

I thought PartialFunction can be Monoid. Is my thought process correct ? For example, import scalaz._ import scala.{PartialFunction => -->} implicit def partialFunctionSemigroup[A,B]:Semigroup[A-->B] = new Semigroup[A-->B]{ def append(s1: A-->B,…
13
votes
3 answers

Is there a standard abstraction for semirings or monoids in C++?

Does boost, or any other common C++ library, provide semiring or monoid abstractions (such as a template class)? I have some algorithms that I would like to express in terms of these abstract structures, but so far I haven't come across anything. I…
Jason Dagit
  • 13,684
  • 8
  • 33
  • 56
12
votes
1 answer

Write a Maximum Monoid using Maybe in Haskell

I've been going through Haskell monoids and their uses, which has given me a fairly good understanding of the basics of monoids. One of the things introduced in the blog post is the Any monoid, and it's usage like the following: foldMap (Any . (==…
lukerandall
  • 2,201
  • 17
  • 29
12
votes
3 answers

How is "a monoid on applicative functors" different than "a monoid in the category of endofunctors"?

Perhaps neither of these statements are categorically precise, but a monad is often defined as "a monoid in the category of endofunctors"; a Haskell Alternative is defined as "a monoid on applicative functors", where an applicative functor is a…
Keith Pinson
  • 7,835
  • 7
  • 61
  • 104
12
votes
4 answers

Haskell: duplicated functions (+) and (++), mappend

(+) and (++) are just specializations of mappend; am I right? Why are they needed? This is useless duplication since Haskell has these powerful typeclasses and type inference. Let's say we delete (+) and (++) and rename mappend (+) for visual…
L01man
  • 1,521
  • 10
  • 27
11
votes
1 answer

Don't understand notation of morphisms in Monoid definition

I'm trying to understand what Monoid is from a category theory perspective, but I'm a bit confused with the notation used to describe it. Here is Wikipedia: In category theory, a monoid (or monoid object) (M, μ, η) in a monoidal category (C, ⊗, I)…
Bogdan Vakulenko
  • 3,380
  • 1
  • 10
  • 25
11
votes
7 answers

Why "and []" is True and "or []" is False

Why "and" on an empty list returns true, does it imply that an empty list holds True? Sorry but I cannot read and comprehend this correctly, so please correct me. Thanks. Prelude> and [] True Prelude> or [] False
Rohit Sharma
  • 6,136
  • 3
  • 28
  • 47
11
votes
3 answers

How to use the maybe monoid and combine values with a custom operation, easily?

What I'm trying to do is trivial to define by hand, basically maybeCombine :: (a->a->a) -> Maybe a -> Maybe a -> Maybe a maybeCombine _ Nothing Nothing = Nothing maybeCombine _ (Just a) Nothing = Just a maybeCombine _ Nothing (Just a) = Just…
leftaroundabout
  • 117,950
  • 5
  • 174
  • 319
10
votes
1 answer

How can I express foldr in terms of foldMap for type-aligned sequences?

I'm playing around with type-aligned sequences, and in particular I'm messing around with the idea of folding them. A foldable type-aligned sequence looks something like this: class FoldableTA fm where foldMapTA :: Category h => …
dfeuer
  • 48,079
  • 5
  • 63
  • 167
10
votes
2 answers

Why would my datatype need to an instance of Monoid to use this lens?

I'm using the code below on a record that has a field '_scene' of type SceneGraph. I've created lenses for it using makeLenses. inputGame :: Input -> Game -> Game inputGame i g = flip execState g $ do let es = g ^. userInput . events sg…
schellsan
  • 2,164
  • 1
  • 22
  • 32
9
votes
2 answers

If a data structure is foldable, is it a monoid?

Obviously, if a data structure is a monoid, it's foldable, but is it safe to say if a data structure is foldable, it's a monoid? https://en.wikibooks.org/wiki/Haskell/Foldable If a data structure is foldable, is it a monoid?
user10180218
1
2
3
15 16