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
1
vote
4 answers

Applicative instance trying to use monoidal functors

I'm learning Haskell and trying to do exercises from book Haskell Programming from first principles and I'm stack trying to write applicative for Pair type data Pair a = Pair a a deriving Show I have seen some other examples on web but I'm trying…
simkestuff
  • 55
  • 1
  • 3
1
vote
1 answer

Idiomatic approach to "composing" monoids using cats?

I would like to "compose" two monoids using cats. If there exists a defined Monoid[(A, A) => Int], then I would like to be able to create a Monoid[Preference[A]] using the combine and empty methods of the Monoid[(A, A) => Int]. I am using the term…
davidrpugh
  • 4,363
  • 5
  • 32
  • 46
1
vote
2 answers

Monoid instance for ParMap?

Is there an existing Monoid instance for scala.collection.parallel.ParMap in Cats? I don't think so, but I am not certain. If not, then how would I go about creating such an instance? Here is what I have so far... import cats._ import…
davidrpugh
  • 4,363
  • 5
  • 32
  • 46
1
vote
2 answers

Adding a Monoid constraint to a hidden variable

I'm in the Haskell Book chapter on Applicative. I am writing the Applicative instance for ZipList and I know I overthought it and am changing my approach. My old approach was: data List a = Nil | Cons a (List a) deriving (Eq, Show) newtype ZipList'…
tesserakt
  • 3,231
  • 4
  • 27
  • 40
1
vote
1 answer

Monoid instance for A => A in Cats

Functions A => A are monoid with identity as empty and composition as combine. Unfortunately I did not find it in cats library. Does the library provide a monoid instance for these functions ? What about A => M[A], where M is a monad or applicative…
Michael
  • 41,026
  • 70
  • 193
  • 341
1
vote
2 answers

Apply a list of Boolean functions to two arguments in Haskell

I have a list of functions of type a -> b -> Bool, and am trying to apply them to two inputs and combine the results with All or Any. I have this working with functions of one variable: mconcat (map (All . ) [(<7),(>7),(==7)]) $ 6 but I cannot…
Jan Hlavacek
  • 207
  • 2
  • 5
1
vote
1 answer

Set of transformations f::a->a form a monoid over function composition - how do I make this an instance of Monoid?

It looks as though transformations should form a Monoid, with the identity function as the empty element and standard function composition as the binary operation. I don't think it's particularly useful, but it should be possible. Something along…
1
vote
1 answer

What is wrong with the method definition

Below are my method signatures and definitions in scala def accumulate[T[_]: Traversable, O: Monoid, A]: (A => O) => T[A] => O = fao => ta => (implicitly[Traversable[T]].traverse[({type f[X] = Acc[O, X]})#f, A, O](ta)(a =>…
Abdul Rahman
  • 1,294
  • 22
  • 41
1
vote
1 answer

simple aggregation with scalaz?

Here is my implementation of a simple aggregation. val mapping = Map("AA" -> "A", "AB" -> "A", "B" -> "B") val input = Map("AA" -> 1, "AB" -> 1, "B" -> 1) val output = input.groupBy { case (k, _) => mapping(k) } …
Yann Moisan
  • 8,161
  • 8
  • 47
  • 91
1
vote
1 answer

Is Monoid[String] really a Monoid in scala

I am currently learning about category theory in scala and the law of associativity says (x + y) + z = x + (y + z) Thats just fine when working with more than two values ("Foo" + "Bar") + "Test" == "Foo" + ("Bar" + "Test") // true In that case…
Jay
  • 1,035
  • 2
  • 11
  • 22
1
vote
0 answers

How to create a generic Monoid from an ADT

Given I have an existing ADT, is it possible to create a generic monoid. From now, I have as many monoid as classes in my ADT, but it's not DRY. Here is the code : import scalaz._ import Scalaz._ object DataRework extends App { sealed trait…
Yann Moisan
  • 8,161
  • 8
  • 47
  • 91
1
vote
1 answer

How to make Monoid instance for my type?

I have the following type: data SomeType = Var String deriving (Eq,Show) newtype TypeA = TypeA [(String, SomeType)] deriving (Eq,Show) Also, I have a function: fun1 :: TypeA -> TypeA -> TypeA I can use this function for mappend. I don't…
David
  • 674
  • 6
  • 19
1
vote
1 answer

How to represent a polymorphic list monoid in Standard ML

I was recently playing with monoids in Standard ML. The signature is easy to write: signature MONOID = sig type monoid val neutral : monoid val combine : monoid -> monoid -> monoid end And so are simple monoids, such as an integer addition…
Ionuț G. Stan
  • 176,118
  • 18
  • 189
  • 202
1
vote
1 answer

Reading lines in haskell until non-empty string

I am trying to read lines from input in Haskell until I find a non-empty line. Actually, I know how to do it simply using the following code: notEmpty [] = return "" notEmpty (l:xs) = do s <- l if s /= "" then return s else notEmpty…
Sparshong
  • 417
  • 4
  • 9
1
vote
1 answer

Is there a good way to deal with Option[Monoid[T]] mappend operations?

If I have a Monoid[T] that has a zero and mappend, it seems to me logically that the result of implicit myMonoid: Monoid[T] = ... val x: T = thing() val y: Option[T] = none[T] val z: Option[T] = Some(value) val a: T = x.mappend(y) val b: T =…
PlexQ
  • 3,154
  • 2
  • 21
  • 26