about summary refs log tree commit diff
path: root/presentations/servant-2016/slides.pdfpc
blob: ed46003768c001c3adfe712a76d1e95cd7f87e65 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
[file]
slides.pdf
[font_size]
10897
[notes]
### 1
13### 2
Let's talk about servant, which is several things:
API description DSL, we'll speak about how this DSL works
and why it's at the type level

Interpretations of the types resulting from that DSL, for example in
web servers or API clients

Servant is commonly used or implementing services with APIs, or for accessing
other APIs with a simple, typed client
### 3
Why type-level DSLs?
Type-level DSL:  express *something*, e.g. endpoints of API, on  type level by combining types. Types can be uninhabited

Phil Wadler's: expression problem: things should be extensible both in the cases of a type, and in the functions operating on the type
Normal data types: can't add new constructors easily
Servant lifts thisup to simply allow the declaration of new types that can be included in the DSL, and new interpretations that can be attached to the types through typeclasses

APIs become first-class citizens, can pass them around, combine them etc, they are separate from interpretations such as server implementations. In contrast, in most webframeworks, API declaration is implicit

(Mention previous attemps at type-safe web, Yesod / web-routes + boomerang etc)
### 4
Three extensions are necessary:
TypeOperators lets us use infix operators on the type level as constructors
DataKinds promotes new type declarations to the kind level, makes type-level literals (strings and natural numbers) available, lets us use type-level lists and pairs in combination with typeoperators
TypeFamilies: Type-level functions, map one set of types to another, come in two forms (type families, non-injective; data families, injective), more powerful than associated types
### 5
Here you can see servant's general syntax, we define an API type as a simple alias of some other type combinations
strings are type-level strings, not actually values, represent path elements
endpoints are separated by :<|>, all endpoints end in a method with content types and return types
Capture captures path segments, but there are other combinators, for example for headers
Everything that is used from the request is expressed in types, enforcing checkability, no "escape hatch" inside handlers to get request
Every combinator has associated interpretations through typeclasses
### 6
Explain type alias, point out Capture
Server is a type level function (type family), as mentioned earlier
### 7
If we expand server (in ghci with kind!) we can see the actual type of the
function
### 8
Lets speak about some interpretations of these things
### 9
Servant server is the main interpretation that people are interested in, it's used
for taking a type specification and creating a server from it
Based on WAI, the web application interface, common abstraction for web servers which came out of the Yesod project. Implemented by the web server warp, which Yesod runs on
### 10
Explain snippet, path gets removed from server type (irrelevant for handler),
route extracts string to value level
### 11
Explain echo server quickly
### 12
servant client allows generation of Haskell functions that query the API with the same types
this makes for easy to use RPC for example
### 13
A lot of other interpretations exist for all kinds of things, mock servers for testing, foreign functions in various languages, documentation ...
### 14
Demo!
1. Go quickly through code
2. Run server, query with curl
3. Open javascript function
4. Show JS code in the thing
5. Open the map itself
6. Open GHCi, use client
7. Generate docs
### 15
Conclusion
Servant is pretty good, it's very easy to get started and it's great to raise the level of things that the compiler can tell you about when you do them wrong.
### 16
Drawbacks.