Atypical data constructors

If we compare the kind of Either

λ> :kind Either
Either :: Type -> Type -> Type

to the types of its constructors

λ> :set -fprint-explicit-foralls
λ> :type Right
Right :: forall {b} {a}. b -> Either a b

we notice that

  • the Either kind has two Type parameters; and
  • the Right constructors has two type variables a and b.

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:

  1. 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.
  2. 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.

{-# LANGUAGE ExplicitForAll, GADTs, TypeFamilies #-}

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.

Join Type Classes for courses and projects to get you started and make you an expert in FP with Haskell.