Thanks to some excellent answers here, I generally understand (clearly in a limited way) the purpose of Haskell\'s Maybe
and that its definition is
Maybe a
is designed so to have one more value than the type a
. In type theory, sometimes it is written as 1 + a
(up to iso), which makes that fact even more evident.
As an experiment, consider the type Maybe (Maybe Bool)
. Here we have 1 + 1 + 2
values, namely:
Nothing
Just Nothing
Just (Just False)
Just (Just True)
If we were allowed to define
data Maybe a = Nothing | a
we would lose the distinction between the cases Just Nothing
and Nothing
, since there is no longer Just
to make them apart. Indeed, Maybe (Maybe a)
would collapse into Maybe a
. This would be an inconvenient special case.