[Haskell-cafe] Why does Haskell have both `Maybe a` and `Either a ()`?

Ben Franksen ben.franksen at online.de
Fri May 29 19:51:40 UTC 2020

Am 29.05.20 um 12:24 schrieb Wiebe-Marten Wijnja:
> Recently I was involved in a discussion on the new ML-style language
> 'gleam'.
> Gleam has for quite a while now only had an `Either a b` type,
> with all functions that in Haskell one would use a `Maybe a` for,
> working on an `Either a ()` instead.
> In the discussion(https://github.com/gleam-lang/gleam/issues/591), the
> language designers were asking the community whether it would make sense
> to add `Maybe` to the language as well,
> or keep using only `Either a ()`.

A separate data type makes the intention clearer and (as others have
stated) is a bit more memory efficient (in Haskell, but I think in an
ML-like language, too). The disadvantage is that you cannot easily
re-use existing functionality. So there is a danger that implementations
of functions on Maybe deviate from those for Either () for no good reason.

> My question: Is the difference between `Maybe a` and `Either a ()` only
> semantic and are they functionally equivalent,

I'd say any semantic difference (apart from laziness) between (Maybe a)
and (Either () a) is more accidental than expressly desired.

> or are there differences in functionality as well?

There seem to be some, as was previously observed (e.g. the Ord
instance). Whether this was intended is questionable.

The mentioned differences regarding () vs. bottom are, I think, not of
particular interest to you: if gleam is ML-style then it probably is a
strict language, and thus Maybe and Either () would be fully equivalent
as data types.


More information about the Haskell-Cafe mailing list