haskell-servant / servant

Servant is a Haskell DSL for describing, serving, querying, mocking, documenting web applications and more!
https://docs.servant.dev/
1.83k stars 413 forks source link

How to implement a dependently-typed server with servant-server >0.4 #1095

Open lortabac opened 5 years ago

lortabac commented 5 years ago

I am trying to adapt the ideas in https://www.well-typed.com/blog/2015/12/dependently-typed-servers/ to more recent versions of servant-server.

In servant-server-0.4 in the route method you could access the request, parse the path segment, do dependent pattern-matching on the decoded value and then call route recursively. In recent versions you cannot access the request directly anymore, but have to work with a Delayed, therefore the same technique is not applicable.

For an example of where I am stuck, you can look at this experimental branch of my versioning library: https://github.com/lortabac/versioning/blob/servant-dependent-i-am-stuck/versioning-servant/src/Versioning/Servant/Dependent.hs

phadej commented 5 years ago

Delayed rearranges things (i.e. servant-server checks things in different order than written in the type, for performance reasons, e.g. parsing ReqBody very last); so the dependent server is indeed a puzzle.

I have no idea if you can do dependent servers with Delayed, and whether you need to modify it (and how much).

arianvp commented 5 years ago

I wonder if Delayed could be modeled as some kind of state machine over the routing information using something ala https://github.com/coot/free-category/blob/master/examples/src/LoginStateMachine.hs , that pops off the correct bits of information from the type-level representation...

food for thought indeed

alpmestan commented 5 years ago

This would be a nice thing to investigate once I get around to extracting a servant-server-core package with just the routing machinery and everything relevant that's wai/warp agnostic in general. That'd probably make it easier, abstracting away from a lot of irrelevant details. I've unfortunately been quite busy...