Type-level web APIs with servant: an exercise in domain-specific generic programming

A Mestanogullari, S Hahn, JK Arni, A Löh - Proceedings of the 11th ACM …, 2015 - dl.acm.org
A Mestanogullari, S Hahn, JK Arni, A Löh
Proceedings of the 11th ACM SIGPLAN Workshop on Generic Programming, 2015dl.acm.org
We describe the design and motivation for Servant, an extensible, type-level DSL for
describing Web APIs. Servant APIs are Haskell types. An API type can be interpreted in
several different ways: as a server that processes requests, interprets them and dispatches
them to appropriate handlers; as a client that can correctly query the endpoints of the API; as
systematic documentation for the API; and more. Servant is fully extensible: the API
language can be augmented with new constructs, and new interpretations can be defined …
We describe the design and motivation for Servant, an extensible, type-level DSL for describing Web APIs. Servant APIs are Haskell types. An API type can be interpreted in several different ways: as a server that processes requests, interprets them and dispatches them to appropriate handlers; as a client that can correctly query the endpoints of the API; as systematic documentation for the API; and more. Servant is fully extensible: the API language can be augmented with new constructs, and new interpretations can be defined. The key Haskell features making all this possible are data kinds, (open) type families and (open) type classes. The techniques we use are reminiscent of general-purpose generic programming. However, where most generic programming libraries are interested in automatically deriving programs for a large class of datatypes from many different domains, we are only interested in a small class of datatypes that is used in the DSL for describing APIs.
ACM Digital Library