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

Can we think of non-symmetric product data types in Haskell?

Control.Category.Constrained.Cartesian is a class for monoidal categories with some natural transformations (the product is (,) and the unit defaults to (); the product cannot be changed, unlike the sum in…
1
vote
2 answers

understanding functors and monoids

I have been looking over this code for so long now and I still do not understand what it is trying to do. what does Functor ((,) x) mean? Similarly, what does Applicative ((,) x) mean why does pure a = (mempty ,a), what is mempty? what are the…
henry
  • 61
  • 5
1
vote
1 answer

Problem with understanding an equation used to define a monoid on the natural numbers

Consider following code: import Data.Semigroup newtype Sum a = Sum { getSum :: a } deriving (Eq, Ord, Show) instance Num a => Semigroup (Sum a) where Sum a <> Sum b = Sum (a + b) instance Num a => Monoid (Sum a) where mempty = Sum 0 mappend =…
user65526
  • 685
  • 6
  • 19
1
vote
1 answer

Cats' Monoid instance throws exception when merging Maps of Arrays

I defined a Monoid instance for Map[Int, Array[Int]] and tried to use it to merge a list of such maps: import cats.Monoid import cats.implicits._ implicit val m: Monoid[Map[Int, Array[Int]]] = Monoid[Map[Int, Array[Int]]] List( Map( (0 ->…
Bertrand
  • 1,718
  • 2
  • 13
  • 24
1
vote
2 answers

Merging elements of a list of case classes

I have the following case class: case class GHUser(login:String, contributions:Option[Int]) And a list of such elements: val list = List( List(GHUser("a", Some(10)), GHUser("b", Some(10))), List(GHUser("b", Some(300))) ).flatten And now I…
Alejandro Alcalde
  • 5,990
  • 6
  • 39
  • 79
1
vote
0 answers

How to fix State monoid type in the following code

I am using State monoid to update the state of an object as listed in Functional and Reactive Domain modeling book while working CQRS. As I understand, if you do not want to return any value, you can just have () as below. But from me, I keep…
Srini
  • 79
  • 4
1
vote
2 answers

How to turn a function returning [a] into a function returning a Monoid?

I'm playing with Haskell return-type polymorphism using the following function: f :: [a] -> [a] f [] = mempty f (x:xs) = [x] <> f xs Obviously it does nothing. What I'd like to do is to modify the type so that it takes a list and returns a Monoid…
Listerone
  • 1,381
  • 1
  • 11
  • 25
1
vote
2 answers

Getting identity for type parameter?

I have the following type that i want to be an instance of Monoid typeclass.I do not know how to set a parametrized field to the identity.Is there any way when using parametrized type to get the identity of that type ? data Tree a=Leaf a | Node a…
Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152
1
vote
2 answers

meaning of `<>` in this family `<*>,<$>,<&>`

I'm Trying to expand my understanding about symbols in Haskell : $ : Function Application operator (Allow you to apply arguments over a function) & : flipped version of Function Application Operator? (&) = flip ($) <> : associative operator…
Nicolas Henin
  • 3,244
  • 2
  • 21
  • 42
1
vote
2 answers

Question about the Writer monad as taught in LYAH. (How did the appending to the log take place?)

I'm learning Haskell from the "Learn you a Haskell for Great Good" tutorial and I've got to the part on writer monads. Here's the example that I can't figure out. import Control.Monad.Writer logNumber :: Int -> Writer [String] Int logNumber x =…
Andy
  • 11
  • 2
1
vote
2 answers

What is the binary operator of Function Monoids in example of JavaScript

In this article Function Monoids are introduced with C# code and Haskell Type definition. A function a -> b is a monoid if b is a monoid. This means that you can combine two functions with the same type. In an object-oriented context, it means…
user11239932
1
vote
1 answer

Idris - use same interface instance

I have a data structure record IdentityPreservingMorphism domain codomain where constructor MkMorphismOfMonoids func : domain -> codomain funcRespId : (Monoid domain, Monoid codomain) => func (Algebra.neutral) = Algebra.neutral which just says…
marcosh
  • 8,780
  • 5
  • 44
  • 74
1
vote
1 answer

Can not lift multiple parameters in a monad

Hello i am trying to do the following: module MyMonad where f::(Monad m),=>m (a->b->c)->m a -> m b -> m c f mf ma mb= ma >>= \a -> mb >>= \b -> mf >>= \c -> return (c a b) and use it like this : f (Just 3) (Just 4) And i get…
Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152
1
vote
2 answers

"mayBeMempty" function for a Semigroup

The following function - mayBeMempty :: (Eq a, Semigroup a) => a -> a -> Bool mayBeMempty candidate ref = candidate <> ref == ref Is a (less efficient) generalization of Data.Set.isSubSetOf. It checks if the first argument is "contained" in the…
yairchu
  • 23,680
  • 7
  • 69
  • 109
1
vote
1 answer

Custom monoid with WriterT

I am trying to implement WriterT with custom data type. I have implemented the monoid as the required by runWriterT. But I am unable to compile the code. I get an error Could not deduce (Semigroup (Env a)) arising from the superclasses of…
DBS
  • 794
  • 2
  • 9
  • 21