Atypical data constructors
If we compare the kind of Either
to the types of its constructors
we notice that
- the
Either
kind has twoType
parameters; and - the
Right
constructors has two type variablesa
andb
.
Most of the datatypes you’re used to follow this pattern. This series is about ways to deviate from the regular path and define slightly more exotic types:
- By placing equality constraints (
~
) on a data constructor, Note that putting constraints on a constructor is different from putting constraints on a type, which is enabled by a deprecated extension called DatatypeContexts which we do not recommend or discuss here. we can reduce the number of parameters it has relative to the kind of the type constructor. - Using the
forall
keyword in a data constructor, we can increase the number of the type parameters it has relative to the kind of the type constructor.
None of this is possible in the Haskell 2010 language standard; we need some extensions. There are some different combinations of extensions that could choose. To keep it simple, we will limit ourselves to three.
For all of the code in this series, these extensions are enabled. The -fprint-explicit-foralls
GHC flag will remain in effect as well.
The first lessons will establish some foundations, and subsequent lessons will apply these concepts to practical demonstrations.