13 More about types

 

This chapter covers

  • Using types to direct processing stages
  • Using types to implement safe interfaces
  • Programming with dependent types in Haskell

We’ve discussed a lot of powerful Haskell features in the previous chapters. To keep the presentation simple, I tried to talk about them in isolation. Their powers become much stronger, however, when we use them in combination. We can achieve a huge boost in the expressiveness of our types together with the provable safety of our programs if we apply types, type classes, type families, GADTs, and Template Haskell at once. In this chapter, I demonstrate how we can do that in two extended examples.

In the first section we’ll explore the usage of type families and type classes to describe and implement RESTful web services. We’ll do that in the vein of the Servant library, the most-used Haskell library for implementing web services.

13.1 Types for specifying a web API

13.1.1 Implementing a web API from scratch

13.1.2 Implementing a web service with servant

13.2 Toward dependent types with singletons

13.2.1 Safety in Haskell programs

13.2.2 Example: Unsafe interface for elevators

13.2.3 Dependent types and substituting them with singletons

13.2.4 Example: Safe interface for elevators

Summary