Answers
- A semigroup is an
s
type with a(<>)
binary associative operator. In Haskell, it is modeled by the following type class:class
Semigroup s
where(<>) :: s -> s -> s
This is subject to the following associativity law:
(x <> y) <> z = x <> (y <> z)
- A monoid is a semigroup (that is, type
m
with a(<>)
binary associate operator) that also has a neutral element (aka identity),mempty
. In Haskell, it is modeled by the following subclass ofSemigroup
:class
Semigroup m => Monoid m
wheremempty :: m
This is subject to the two identity laws:
x <> mempty = x
mempty <> x = x
- The following table lists examples from the standard libraries we have covered. The first column gives the names of the types (possibly subject to type class constraints). The second column lists the names of the newtype wrappers, in case there are multiple possibilities for the...