Atypical data constructors

Compare the kind of Either to the types of its constructors:

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

Notice that the Either kind has two Type parameters, and the Right constructors has two type variables a and b.

Datatypes typically follow this pattern. This series is about the atypical 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 GHC2021 language standard; we need some extensions. There are some different combinations of extensions that we could choose. To keep it simple, we will limit ourselves to two.

{-# LANGUAGE 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.