Before we can start building out large type level programming examples, we need to spent some time looking at two of the most basic building blocks of type level code: types and kinds. In type level code, the types are the values that we are computing with, and the kinds of those types give us “type” safety in the same way types provide safety to our term level code. Although you’ve worked extensively with types in this book, and you’ve encountered kinds occasionally, the way that we work with them in type level code is frequently different from how we’d work with them in term level code. In this section, we’ll look at how to work with types and kinds for type level code, and see some examples of types and kinds that you wouldn’t encounter when writing term level code.

The Type Kind

So far in this book, every time we’ve needed to work with kinds it’s been so that we can work with higher kinded types that we’ll use in our term level code. Since all of the types we were working with were intended to eventually represent some term level values, the only kinds that we’ve worked with so far are Type , and kinds that accept Type parameters. Let’s look at a couple of examples. In these examples, we’ll turn on the NoStarIsType extension. As you may recall from some earlier examples, we imported Data.Kind to allow us to write Type instead of * when using kind signatures. NoStarIsType goes the other direction, telling GHC to use the word Type instead of * in both error messages and ghci output.

NoStarIsType

| images/aside-icons/info.png | Technically speaking, NoStarIsType isn’t actually a language extension. The StarIsType extension has been available since GHC 8.6.1. StarIsType is enabled by default in Haskell2010 and GHC2021 . The No StarIsType “extension” disables StarIsType . The StarIsType extension can introduce conflicts with the TypeOperators extension, and it generally breaks consistency with other kinds Nat and Symbol using proper names. There are proposals to make NoStarIsType the default in a future version of GHC . |

​  ​ λ ​ :set -​ XNoStarIsType ​
​  ​ λ ​ ​ import ​ ​ Control.Monad.State.Strict ​
​  ​ λ ​ :kind ​ Int ​
​  ​ Int ​ :: ​ Type ​
​  ​ λ ​ :kind ​ Maybe ​
​  ​ Maybe ​ :: ​ Type ​ -> ​ Type ​
​  ​ λ ​ :kind ​ StateT ​
​  ​ StateT ​ :: ​ Type ​ -> (​ Type ​ -> ​ Type ​) -> ​ Type ​ -> ​ Type ​

By now you’ve worked with higher kinded types enough that you have a reasonable intuition for how to read these kinds. The kind Type in all of these examples represents some type that we might want to pass in, like Int or Maybe String . One of the first things we need to do in order to start writing type level code is to move past the intuition here and get a better understanding of what Type is, and start to look at other kinds.

The Type kind appears so often in our earlier examples because it’s the kind of all types of term level values. Whatever the type, if we can have a value of it, its kind must be Type . Although we can’t have values of higher kinded types like Maybe or StateT , the type parameters that we pass to these higher kinded types are also going to be types that will represent runtime values, so even our higher kinded type examples so far have necessarily revolved around the Type kind.

As we start to write code at the type level, types themselves become the values we want to compute with and we’re no longer restricted to working only with types of actual runtime values. This means that in type level code, we’ll start to work with a wide variety of different kinds, including kinds we define ourselves, and even polymorphic kinds.

Type Level Natural Numbers

Nat and Natural

| images/aside-icons/info.png | In earlier versions of GHC, type level natural numbers had the kind Nat . In more recent versions Natural is used instead, and Nat is an alias. We’re using Natural in these examples, so if you are following along using an older version of GHC you can either rename Natural to Nat or define your own alias by adding type Natural = Nat . |

Type level literals give us a way of writing values like numbers and strings and using those as values at the type level. Type level literals are also a great first example of types with a kind other than Type . Let’s open up ghci and take a look at a few examples.

We’ll start by importing the GHC.TypeLits module from base . This module defines the kinds we’ll be working with, and a number of utility functions to help us integrate our type level code with term level code.

​  ​ λ ​ ​ import ​ ​ GHC.TypeLits ​

Next, let’s explore some of the type level literals that you’ll be working with frequently in this chapter. We’ll start by looking at an example of how we can work with numbers when we’re doing type level programming, using type level naturals :

​  ​ λ ​ :kind 0
​  0 :: ​ Natural ​
​  
​  ​ λ ​ :kind 1
​  1 :: ​ Natural ​
​  
​  ​ λ ​ :kind 2
​  2 :: ​ Natural ​

As you can see, we write type level naturals the same way we’d write numeric literals at the term level. Since we’re dealing with natural numbers, we can’t make negative type level naturals. Aside from this restriction, many of the basic mathematical operations that you can do at the term level are also available at the type level. Let’s try to add a couple of numbers at the type level and see what happens:

​  ​ λ ​ :kind 1 + 2
​  1 + 2 :: ​ Natural ​

As it turns out, using :kind to evaluate a type level expression will give us the kind of the expression, but it doesn’t evaluate the expression like we had expected. This can be useful in some circumstances, for example, if you want to find the kind of an expression that is computationally expensive to evaluate, but in most cases we’d like to see the actual result of our type level expression computed. We can ask ghci to evaluate the type level expression using :kind! (note the extra ! at the end). Let’s see a few examples:

​  ​ λ ​ :kind! (+) 1 1
​  (+) 1 1 :: ​ Natural ​
​  = 2
​  
​  ​ λ ​ :kind! 1 + 2 + 3 + 4
​  1 + 2 + 3 + 4 :: ​ Natural ​
​  = 10
​  
​  ​ λ ​ :kind! 2 + 3 * 4
​  2 + 3 * 4 :: ​ Natural ​
​  = 14
​  
​  ​ λ ​ :kind! (2 + 3) * 4
​  (2 + 3) * 4 :: ​ Natural ​
​  = 20
​  
​  ​ λ ​ :kind! 5 <=? 10
​  5 <=? 10 :: ​ Bool ​
​  = ​ 'T ​​ r ​ue

There are a few different things that we can see in these examples. First, you can see how using :kind! shows you both the kind of the entire expression you enter, as well as the simplified type that you get after evaluating the expression. You can also see that these type level expressions are evaluated similarly to term level expressions, including things like order of operations and parentheses. Finally, you’ll notice in the last of these examples that we’ve used a type level expression with two naturals that returns a kind of Bool and a type of True . You’ll learn more about type level booleans later on in this section, but for now this serves as a good example that, just like at the term level, we can have type level expressions that compute a value with a different kind than the values in the expression.

Type Level Strings

In addition to natural numbers, GHC.TypeLits also gives us an easy way to deal with type level strings, which have the kind Symbol . It’s common for them to be referred to as symbols or type level strings interchangeably. You can write type level string literals the same way you write term level string literals:

​  ​ λ ​ :kind! ​ "Hello" ​
​  ​ "Hello" ​ :: ​ Symbol ​
​  = ​ "Hello" ​

There are only a few operations defined for us in symbols. We can combine them using AppendSymbol :

​  ​ λ ​ :kind! ​ AppendSymbol ​ ​ "Hello, " ​ ​ "World" ​
​  ​ AppendSymbol ​ ​ "Hello, " ​ ​ "World" ​ :: ​ Symbol ​
​  = ​ "Hello, World" ​
​  
​  ​ λ ​ :kind! ​ "Hello, " ​ `​ AppendSymbol ​` ​ "World" ​
​  ​ "Hello, " ​ `​ AppendSymbol ​` ​ "World" ​ :: ​ Symbol ​
​  = ​ "Hello, World" ​
​  
​  ​ λ ​ :kind! ​ "a" ​ `​ AppendSymbol ​` ​ "b" ​ `​ AppendSymbol ​` ​ "c" ​
​  ​ "a" ​ `​ AppendSymbol ​` ​ "b" ​ `​ AppendSymbol ​` ​ "c" ​ :: ​ Symbol ​
​  = ​ "abc" ​

You’ll notice in this example that we’re using AppendSymbol as an infix function, just like we would do with a term level function. One key difference between type and term level code is that type level functions like AppendSymbol still start with a capital letter.

In addition to symbols, newer versions of GHC also have a Char kind that we can use to add and remove characters from a type level string:

​  ​ λ ​ :kind! ​ 'a' ​ `​ ConsSymbol ​` ​ "bc" ​
​  ​ 'a' ​ `​ ConsSymbol ​` ​ "bc" ​ :: ​ Symbol ​
​  = ​ "abc" ​
​  
​  ​ λ ​ :kind! ​ UnconsSymbol ​ ​ "abc" ​
​  ​ UnconsSymbol ​ ​ "abc" ​ :: ​ Maybe ​ (​ Char ​, ​ Symbol ​)
​  = ​ 'J ​​ u ​st ​ '(' ​a', ​ "bc" ​)
​  
​  ​ λ ​ :kind! ​ UnconsSymbol ​ ​ "" ​
​  ​ UnconsSymbol ​ ​ "" ​ :: ​ Maybe ​ (​ Char ​, ​ Symbol ​)
​  = ​ 'N ​​ o ​thing

As you can see from all of the examples so far, type level expressions share a lot of similarities with the term level code you’re already familiar with from working through this book. Some of the types you’re used to, like Bool , Maybe , and tuples also have equivalents at the type level. It turns out that it’s no accident that so many types that you’re accustomed to from term level code make an appearance as kinds in type level code. In many cases we can automatically promote types to kinds using the DataKinds extension.

Data Kinds

The DataKinds extension lets us use the same types we’ve been writing at the term level as kinds when we’re working at the type level. With this extension enabled, every type we’ve defined is also a kind, and the constructors for that type are its inhabitants. Let’s look at an example by creating a new module and defining a simple sum type to represent some colors:

DataKinds

| images/aside-icons/info.png | The DataKinds extension has been available since GHC 7.4.1. This extension isn’t enabled by default in either GHC2021 or Haskell2010 so you’ll need to enable it manually. This extension is generally safe, however it may change the way certain errors are displayed. |

​  ​ module ​ ​ ColorDemo ​ ​ where ​
​  
​  ​ data ​ ​ Color ​ = ​ Red ​ | ​ Green ​ | ​ Blue ​

If we load this into ghci we can look at the type of Red , Green , and Blue if we treat them as term level constructors, but if we try to treat them as types and look at their kind we’ll get an error:

​  λ :type Red
​  Red :: Color
​  
​  λ :kind Red
​  
​  <interactive>:1:1: error:
​  Not in scope: type constructor or class ‘Red’
​  Suggested fixes:
​  • Perhaps you intended to use DataKinds
​  to refer to the data constructor of that name?

Helpfully, the compiler is telling us that we need to add the DataKinds extension if we want to do this. Let’s take the compiler’s advice and try again with the extension enabled:

​  ​ λ ​ :set -​ XDataKinds ​
​  
​  ​ λ ​ :​ type ​ ​ Red ​
​  ​ Red ​ :: ​ Color ​
​  ​ λ ​ :kind ​ Red ​
​  ​ Red ​ :: ​ Color ​
​  
​  ​ λ ​ :​ type ​ ​ Blue ​
​  ​ Blue ​ :: ​ Color ​
​  ​ λ ​ :kind ​ Blue ​
​  ​ Blue ​ :: ​ Color ​
​  
​  ​ λ ​ :​ type ​ ​ Green ​
​  ​ Green ​ :: ​ Color ​
​  ​ λ ​ :kind ​ Green ​
​  ​ Green ​ :: ​ Color ​

In this example, you can see that with DataKinds enabled, each of our constructors is now both a value with the type Color as well as a type with the kind Color . This ends up being a fairly common source of confusion when working with type level code. Since the type and term level code share the same names, and are implemented with the same code, it’s easy to get mixed up and start conflating type and term level meanings of things. The typical manifestation of this in your code is that you’ll start seeing errors that the compiler expected the kind Type but instead got a kind like Color . For example, if we try to create a value Red with the type Red , we’ll get an error:

​  λ r = Red :: Red
​  <interactive>:37:12-14: error:
​  • Expected a type, but ‘Red’ has kind ‘Color’
​  • In an expression type signature: Red
​  In the expression: Red :: Red
​  In an equation for ‘r’: r = Red :: Red

One way to differentiate between the term level constructor and the type level value is to use a single quote ( ’ ) to refer to the promoted type:

​  ​ λ ​ :kind ​ 'R ​​ e ​d
​  ​ 'R ​​ e ​d :: ​ Color ​
​  
​  ​ λ ​ :kind ​ 'G ​​ r ​een
​  ​ 'G ​​ r ​een :: ​ Color ​
​  
​  ​ λ ​ :kind ​ 'B ​​ l ​ue
​  ​ 'B ​​ l ​ue :: ​ Color ​

There are some circumstances where the quote is required to remove ambiguity, like when there is both a type and constructor with the same name. For example, imagine that we have a type with a single constructor whose name shadows the name of the type:

​  ​ data ​ ​ Foo ​ = ​ Foo ​

If we try to look at the kind of Foo , the compiler will tell us that its kind is Type :

​  ​ λ ​ :kind ​ Foo ​
​  ​ Foo ​ :: ​ Type ​

This happens because the type name takes precedence over the promoted constructor when the compiler is trying to figure out the kind of a type. In this case, if we want to refer to the promoted constructor, we need to add a single quote:

​  ​ λ ​ :kind ​ 'F ​​ o ​o
​  ​ 'F ​​ o ​o :: ​ Foo ​

This situation will happen any time there’s a constructor and a type in scope with the same name. To avoid this problem, it used to be a common recommendation to always explicitly use a quote to identify promoted constructors, and prior to GHC 9.4 the compiler will generate a warning if you enable -Wall and fail to use the quote. GHC 9.4 plans to remove this warning from -Wall and there is a bit less consensus on style these days. The examples in this chapter will only add the quote if it’s required or greatly enhances the clarity of a particular example.

Although basic sum types like Color are a good introduction to defining your own kinds, there is a lot more that we can do. You’ve already seen an example of using tuples and Maybe at the type level when we called UnconsSymbol . Let’s look at another example: building lists at the type level.

Let’s start by revisiting the list implementation you created much earlier in this book:

​  ​ module ​ ​ TypeLevelList ​ ​ where ​
​  
​  ​ data ​ ​ List ​ a = ​ Empty ​ | ​ Cons ​ a (​ List ​ a)

With the DataKinds extension enabled, we can use this same list definition to create lists at the type level. Let’s try it out in ghci :

​  ​ λ ​ :kind! ​ Cons ​ ​ Int ​ (​ Cons ​ ​ String ​ (​ Cons ​ ​ Bool ​ ​ Empty ​))
​  ​ Cons ​ ​ Int ​ (​ Cons ​ ​ String ​ (​ Cons ​ ​ Bool ​ ​ Empty ​)) :: ​ List ​ ​ Type ​
​  = ​ 'C ​​ o ​ns ​ Int ​ (​ 'C ​​ o ​ns [​ Char ​] (​ 'C ​​ o ​ns ​ Bool ​ ​ 'E ​​ m ​pty))
​  
​  ​ λ ​ ​ data ​ ​ Color ​ = ​ Cyan ​ | ​ Magenta ​ | ​ Yellow ​ | ​ Black ​
​  ​ λ ​ :kind! ​ Cons ​ ​ Cyan ​
​  (​ Cons ​ ​ Magenta ​
​  (​ Cons ​ ​ Yellow ​
​  (​ Cons ​ ​ Black ​ ​ Empty ​)))
​  ​ Cons ​ ​ Cyan ​
​  (​ Cons ​ ​ Magenta ​
​  (​ Cons ​ ​ Yellow ​
​  (​ Cons ​ ​ Black ​ ​ Empty ​))) :: ​ List ​ ​ Color ​
​  = ​ 'C ​​ o ​ns ​ 'C ​​ y ​an
​  (​ 'C ​​ o ​ns ​ 'M ​​ a ​genta
​  (​ 'C ​​ o ​ns ​ 'Y ​​ e ​llow
​  (​ 'C ​​ o ​ns ​ 'B ​​ l ​ack ​ 'E ​​ m ​pty)))

Although it’s nice we can create type level lists, it’s annoying that we have to type out Cons every time we want to add an item to the list. It would be much nicer if we could have an operator similar to (:) that we use at the term level. If we enable the TypeOperators extension, we can define our own operators to use at the type level. One way that we can add type operators is to use a type alias to define the operator. For example, let’s define a new right-associative operator named (:+) as an alias for Cons . You’ll notice in the example that we write the fixity declaration for a type operator exactly the same way that we would write it for a term level operator. One key benefit of this is that operators that are lifted with DataKinds will use the same fixity at both the type and term levels. We’ll also need need to enable the PolyKinds extension to opt into GHC’s more powerful kind system.

PolyKinds

| images/aside-icons/info.png | The PolyKinds extension has been available since GHC 7.4, although we’ll be using newer features of the type system and versions prior to GHC 8.10 have not been tested with this chapter. The PolyKinds extension is enabled by default in GHC2021 , but you’ll need to enable it manually if you are using Haskell2010 . This extension enables syntax and modifies type inference to support the more advanced features of GHC’s kind system. This extension should be safe to enable in most existing code, although it changes type inference which may rarely break some existing code, or cause changes to error messages. |

TypeOperators

| images/aside-icons/info.png | The TypeOperators extension has been available since GHC 6.8.1. This extension is enabled by default in GHC2021 but you’ll need to enable it manually if you are using Haskell2010 . This is a safe extension that shouldn’t introduce problems with any existing code. |

​  ​ {-# LANGUAGE DataKinds #-} ​
​  ​ {-# LANGUAGE TypeOperators #-} ​
​  ​ {-# LANGUAGE PolyKinds #-} ​
​  ​ module ​ ​ TypeLevelList ​ ​ where ​
​  
​  ​ infixr ​ 6 :+
​  ​ type ​ (:+) = ​ Cons ​
​  ​ data ​ ​ List ​ a = ​ Empty ​ | ​ Cons ​ a (​ List ​ a)

With our type operator enabled, we can create type level lists that are much easier to both read and write:

​  ​ λ ​ :kind! 0 :+ 1 :+ 2 :+ ​ Empty ​
​  0 :+ 1 :+ 2 :+ ​ Empty ​ :: ​ List ​ ​ GHC ​.​ Num ​.​ Natural ​.​ Natural ​
​  = ​ 'C ​​ o ​ns 0 (​ 'C ​​ o ​ns 1 (​ 'C ​​ o ​ns 2 ​ 'E ​​ m ​pty))

Since we’re using an alias, the evaluated version of our expression still uses the name Cons . If you don’t want this behavior, then you can define your operator as a constructor and it’ll be lifted like any other constructor when you use DataKinds . As an example, let’s rewrite our list and replace Cons with (:+) directly:

​  ​ infixr ​ 6 :+
​  ​ data ​ ​ List ​ a = ​ Empty ​ | (:+) a (​ List ​ a)

Now when we evaluate your list expression, we’ll get back the more readable operator-based version:

​  ​ λ ​ :kind! 0 :+ 1 :+ 2 :+ ​ Empty ​
​  0 :+ 1 :+ 2 :+ ​ Empty ​ :: ​ List ​ ​ GHC ​.​ Num ​.​ Natural ​.​ Natural ​
​  = 0 ​ ': ​​ + ​ (1 ​ ': ​​ + ​ (2 ​ ': ​​ + ​ ​ 'E ​​ m ​pty))

As you can see in these examples, we can construct type level lists the same way that we were able to create lists at the term level, using a recursively defined kind, with a kind parameter. In the first example, we created a type level list of types with the kind Type , and in the second example we created a list of Color types. Just like we can only have term level lists of a single type, we’re also restricted to type level lists of a single kind. If we try to create a list with Int and Cyan for example, we’ll get an error because we’re trying to mix two different kinds:

​  λ :kind! Int :+ Cyan :+ Empty
​  <interactive>:1:8-20: error:
​  • Couldn't match kind ‘Color’ with ‘Type’
​  Expected kind ‘List Type’,
​  but ‘Cyan :+ Empty’ has kind ‘List Color’
​  • In the second argument of ‘(:+)’, namely ‘Cyan :+ Empty’
​  In the type ‘Int :+ Cyan :+ Empty’

As you might have come to expect by now, although we can define our own type level lists, we don’t actually need to. The normal list type that we use at the term level is also available to us at the type level. We can create literal type level lists the same way we’d create term level lists:

​  ​ λ ​ :kind! [1,2,3]
​  [1,2,3] :: [​ Natural ​]
​  = ​ '[ ​​ 1 ​, 2, 3]
​  
​  ​ λ ​ :kind! [​ "Hello" ​, ​ "World" ​]
​  [​ "Hello" ​, ​ "World" ​] :: [​ Symbol ​]
​  = ​ '[ ​​ " ​​ Hello ​​ ", " ​​ World ​​ "] ​
​  
​  ​ λ :kind! [String, Bool, Int -> (Int, Int)] ​
​  ​ [String, Bool, Int -> (Int, Int)] :: [Type] ​
​  ​ = '[[Char], Bool, Int -> (Int, Int)] ​

We can also cons new values onto type level lists, just like we’d do with term level lists, but we need to explicitly lift our list up to the type level. Otherwise, the compiler will tend to get confused and give us surprising error messages. Let’s look at an example:

​  λ :kind! 1 : []
​  <interactive>:1:5-6: error:
​  • Expecting one more argument to ‘[]’
​  Expected kind ‘[Natural]’, but ‘[]’ has kind ‘Type -> Type’
​  • In the second argument of ‘(:)’, namely ‘[]’
​  In the type ‘1 : []’
​  
​  λ :kind! 1 : [2]
​  <interactive>:1:5-7: error:
​  • Expected kind ‘[Natural]’, but ‘[2]’ has kind ‘Type’
​  • In the second argument of ‘(:)’, namely ‘[2]’
​  In the type ‘1 : [2]’
​  
​  <interactive>:1:6: error:
​  • Expected a type, but ‘2’ has kind ‘Natural’
​  • In the second argument of ‘(:)’, namely ‘[2]’
​  In the type ‘1 : [2]’
​  
​  λ :kind! 1 : [2,3]
​  1 : [2,3] :: [Natural]
​  = '[1, 2, 3]

In the first two examples here, we get an error because the compiler can’t determine that [] should represent an empty type level list of Natural . Instead, the compiler is treating [] as though it’s the term level list type with the kind Type -> Type . In the second example, the compiler is making the same assumption about the term level use of [] , but it thinks we’ve now applied the type 2 . In other words, it’s treating [2] as a type of kind Type . In the final example, the list we’re using has two elements. Since the term level [] only accepts a single parameter, the compiler is able to recognize we must be using the type level version of the list.

Using explicitly lifted lists will help us move past the errors in our first two examples. Although it’s not necessary when we’re consing onto a list of two or more elements, we can include the explicit lift for consistency and avoid introducing bugs if we change the code later to remove some elements. Let’s take another look at the examples:

​  ​ λ ​ :kind! 1 : ​ '[ ​​ ] ​
​  1 : ​ '[ ​​ ] ​ :: [​ Natural ​]
​  = ​ '[ ​​ 1 ​]
​  
​  ​ λ ​ :kind! 1 : ​ '[ ​​ 2 ​]
​  1 : ​ '[ ​​ 2 ​] :: [​ Natural ​]
​  = ​ '[ ​​ 1 ​, 2]
​  
​  ​ λ ​ :kind! 1 : ​ '[ ​​ 2 ​,3]
​  1 : ​ '[ ​​ 2 ​,3] :: [​ Natural ​]
​  = ​ '[ ​​ 1 ​, 2, 3]

We run into a similar situation using type level tuples. If we try to create a type level tuple without explicitly promoting it, the compiler will assume we’re trying to create a term level tuple and give us an error:

​  λ :kind! (1, Cyan)
​  <interactive>:1:2: error:
​  • Expected a type, but ‘1’ has kind ‘Natural’
​  • In the type ‘(1, Cyan)’
​  
​  <interactive>:1:5-8: error:
​  • Expected a type, but ‘Cyan’ has kind ‘Color’
​  • In the type ‘(1, Cyan)’

Explicitly promoting the tuple will work just as we’d hope:

​  ​ λ ​ :kind! ​ '( ​​ 1 ​, ​ Cyan ​)
​  ​ '( ​​ 1 ​, ​ Cyan ​) :: (​ Natural ​, ​ Color ​)
​  = ​ '( ​​ 1 ​, ​ 'C ​​ y ​an)

As you can see, the DataKinds extension gives us a great deal of power to create data structures that exist at the type level. In many cases, we can even re-use code we defined at the term level without any changes. All this expressive power doesn’t do us much good without being able to write computations on this data. In the next section you’ll learn how to write type level functions to build computations on the type level data structures you’ve learned how to define.