Maybe identities

In the monoid lesson, we posed this question: If each of the following four functions is the (<>) operation for a semigroup, what is the identity value that makes it a monoid?

f1

Identity value: Nothing

f2

Identity value: Nothing

f3

Identity value: Just mempty

This one was slightly a trick question, because we have to strengthen the constraint on a from Semigroup to Monoid in order to ensure that this identity value exists.

Note that Nothing is not an identity for this function! Remember the identity laws. For all x:

  • mempty <> x = x
  • x <> mempty = x

Take, for example, x = Just 1. Then f3 Nothing (Just 1) is Nothing, not Just 1, therefore we know that Nothing is not an identity.

f4

Identity value: Nothing

Type Classes offers courses and projects to get you started and make you an expert in FP with Haskell. For $29/month, you get access to the complete archive and all the latest content.