Skip to content

Instantly share code, notes, and snippets.

@jonathanlking
Created November 6, 2016 22:20
Show Gist options
  • Save jonathanlking/72fd3e572a436c0d4bfbc0d26f5cf90a to your computer and use it in GitHub Desktop.
Save jonathanlking/72fd3e572a436c0d4bfbc0d26f5cf90a to your computer and use it in GitHub Desktop.
Example code from "Type-level Web APIs with Servant" paper (http://alpmestan.com/servant/servant-wgp.pdf)
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
-- Example code from "Type-level Web APIs with Servant" paper
-- http://alpmestan.com/servant/servant-wgp.pdf
data Proxy a = Proxy
data One
data e1 :+ e2
data Hole
type Two = One :+ One
class HasValue a where
type Value a r :: *
valOf :: Proxy a -> (Int -> r) -> Value a r
instance HasValue One where
type Value One r = r
valOf _ ret = ret 1
instance (HasValue e1, HasValue e2) => HasValue (e1 :+ e2) where
type Value (e1 :+ e2) r = Value e1 (Value e2 r)
valOf _ ret = valOf (Proxy :: Proxy e1) (\v1 ->
valOf (Proxy :: Proxy e2) (\v2 ->
ret (v1 + v2)))
instance HasValue Hole where
type Value Hole r = Int -> r
valOf _ ret n = ret n
valueOf :: HasValue a => Proxy a -> Value a Int
valueOf p = valOf p id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment