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

Implicit parameters with 2 instances of the same type

Scala allows us to define implicit parameters. Based on the exact type the correct definition is chosen. In example below, 2 monoid instances are defined for the same Money type, MoneyAdditionMonoid for accumulation and MoneyCompareMonoid for…
Alexandr
  • 9,213
  • 12
  • 62
  • 102
0
votes
1 answer

Scala : EndoMonoids Function composition and Associativity Rules

I was going through the laws that govern Monoids and one of the two laws state that the append operation must be associative. For function composition this means that for all functions X->X given there are 3 functions f,g and h (f∘g)∘h=f∘(g∘h) In…
Jayadeep Jayaraman
  • 2,747
  • 3
  • 15
  • 26
0
votes
1 answer

Simple composition of implicits with Monoids in Scala

I have the following trait: import scalaz.Monoid trait Mapper[M, R] { def map(m: M): R } object Mapper { @inline implicit def listMapper[M, R] (implicit mapper: Mapper[M, R], s: Monoid[R]): Mapper[List[M], R] = (xs: List[M]) =>…
St.Antario
  • 26,175
  • 41
  • 130
  • 318
0
votes
1 answer

Algorithm to multiply edges of a Networkx graph

So my problem is to find the longest path from a node to another node (or the same node) in a graph implemented with Networkx library. I don't want to add the edges' weights but multiply them and take the biggest result. Obviously, passing only once…
pokatore
  • 51
  • 1
  • 7
0
votes
1 answer

Scalaz implementation of Semigroup using advanced Scala features

I was digging through the implementation of Monoids in Scalaz. I came across the |+| operator that is supposed to come out of the box if you define the append operation on Monoid. The definition of this operator is in SemigroupSyntax. That class…
zaxme
  • 1,065
  • 11
  • 29
0
votes
1 answer

Why Float is not instance of the Monoid type class?

My question is why Float is not readily defined to be an instance of the Monoid type class ? I mean what is against it ? Doesn't Floats have the identity element and if no why? Also the operations (*) and (+) are associative in the set of Floats, or…
IPiiro
  • 97
  • 1
  • 9
0
votes
0 answers

Performance or other similar reasons to use foldMap instead of its variants?

I have a list of some ~10.000 observations in a custom data type observations :: [Obs] that I want to aggregate into a statistics object :: Stat . A fold seemed like a sensible choice. I find myself with two options. (1) Define a function…
LudvigH
  • 3,662
  • 5
  • 31
  • 49
0
votes
1 answer

Does the Exclusive-Or form a monoid for Bools?

I made an operator for the xor-function, it looks like that: op :: Integer -> Integer -> Maybe Integer op x y | x == 0 && y == 0 = Just 0 | x == 0 && y == 1 = Just 1 | x == 1 && y == 0 = Just 1 | x == 1 && y == 1 = Just 0 | otherwise =…
user8138142
0
votes
1 answer

What is MonoidAggregator in Algebird

I don't find any documention about MonoidAggregator. What is it for ? An example of its use: MultiAggregator( ..., Aggregator.forall[T](_.use)).andThenPresent(...) ..., ) forAll return a MonoidAggregator. Whould it be roughly the same as…
azerty
  • 698
  • 7
  • 28
0
votes
1 answer

How to make a phantom applicative functor in PureScript?

I'm reading through this paper and it says that Monoids are phantom applicative functors. I tried setting up a phantom type in purescript but I get a type error in the Functor instance. My guess is the compiler doesn't know what the a is in Accy o…
Albtzrly
  • 924
  • 1
  • 6
  • 15
0
votes
2 answers

Why does mempty evaluate the content of the Monoid

Why does (mempty :: String) work, but (mempty :: Maybe Bool) gives me an error about Bool not having a type class of Monoid, but why does it even matter, it's wrapped in Maybe anyway?
hgiesel
  • 5,430
  • 2
  • 29
  • 56
0
votes
2 answers

Simple unit testing with monoids

Suppose I testing a function echo: String => String, which is just repeating the input, with specs2. I can write a few tests like that: class EchoSpec extends SpecificationWithJUnit { "echo should handle ASCII alphanumeric names" in { …
Michael
  • 41,026
  • 70
  • 193
  • 341
0
votes
0 answers

Why scalaz |+| operator only doesn't work with Maps which value are Seq?

I have two maps as follows. import scalaz._, Scalaz._ val map1: Map[String, Seq[String]] = Some value val map2: Map[String, Seq[String]] = Somve Value This compiles fine and everything works as expected. // Compiles map1.mapValues{_.toList} |+|…
user1819676
  • 369
  • 1
  • 12
0
votes
1 answer

Access member of class instance from outside its definition

I have this type: newtype Mem s a = Mem { runMem :: s -> (a,s) } and I have to create an instance of monoid for this type, but to do so I have to use the mempty and the mappend of the monoid a, regardless of what it may be. How would one go about…
Gérard
  • 3
  • 1
0
votes
1 answer

Monoid on any Foldable

I'm trying to make an instance of a Monoid on haskell, a monoid that can be applicable on any Foldable strutucture that contains comparables elements and returns the maximun value stored. So far i have this import Data.List import…
1 2 3
15
16