Nearly every example that we’ve worked through in this book has made use of the type system in one way or another, and many of the examples have focused on ways to use the type system to write better programs. Even though we’ve leaned heavily on the type system as we were programming, the types have been something that we decided on ahead of time, or left to the compiler to infer. Our programs have been well typed, but the computations that our programs carried out have all been runtime values.

Type level programming lets us expand beyond computing only with runtime values. With type level programming we can also write computations that work on types . This lets us create more sophisticated interfaces to our applications since we can write APIs that are, in a way, their own small programs. Since our type level code is run at compile time, the results are types that are still applied when the rest of our application is compiled, so we can write somewhat more dynamic code without giving up type safety. The trade-off for being able to retain type safety is that we’re limited in the programs that we can write at the type level. You’ll get to see examples of the limitations we’ll run into as you’re working through this chapter.

Terms and Types

Using type level programming means that we can write computations over types. This presents a bit of vocabulary problem, because we now have two different sorts of computation that will live side by side in our code, but are executed separately and work on different values. To help keep things clear, when we’re dealing with type level programming, we’ll use the word term to refer to expressions and values that are going to be evaluated at runtime, and we will continue to use type to refer to any type values or expressions that evaluate to types at compile time. Similarly, we’ll use phrases like term level to differentiate ordinary Haskell code that will be evaluated at runtime from type level code that will be evaluated at compile time.