Skip to main content

option-semigroup-monoid-second

Question

It is possible to define a monoid instance for Option<A> that behaves like that:

xyconcat(x, y)
nonenonenone
some(a1)nonesome(a1)
nonesome(a2)some(a2)
some(a1)some(a2)some(S.concat(a1, a2))
// the implementation is left as an exercise for the reader
declare const getMonoid: <A>(S: Semigroup<A>) => Monoid<Option<A>>

What is the empty member for the monoid?

Answer

none is the empty member for our monoid because with it, all the Monoid laws are true. Let's check the monoid laws on our new monoid:

associative

concat(none, concat(none, concat(none))) === concat(concat(none, none), none)
concat(none, concat(none, concat(some(z)))) === concat(concat(none, none), some(z))
concat(none, concat(some(y), concat(none))) === concat(concat(none, some(y)), none)
concat(none, concat(some(y), concat(some(z)))) === concat(concat(none, some(y)), some(z))
concat(some(x), concat(none, concat(none))) === concat(concat(some(x), none), none)
...
concat(some(x), concat(some(y), concat(some(z)))) === concat(concat(some(x), some(y)), some(z))

right identity

concat(some(x), none) === some(x)

left identity

concat(none, some(x)) === some(x)