Skip to content

Instantly share code, notes, and snippets.

@Icelandjack
Created December 15, 2017 23:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Icelandjack/c0576f8eabc3350ef24a7750d454dfc1 to your computer and use it in GitHub Desktop.
Save Icelandjack/c0576f8eabc3350ef24a7750d454dfc1 to your computer and use it in GitHub Desktop.
Embedding System F
@Icelandjack
Copy link
Author

Icelandjack commented Dec 15, 2017

First we witness membership (I follow Richard's lead here, make sure to check out his dissertation for more info):

import Data.Kind

data Elem :: [a] -> a -> Type where
  EZ :: Elem (x:xs) x
  ES :: Elem    xs  x
     -> Elem (y:xs) x

-- Example
ex :: Elem [Int, Bool, (), Double] ()
ex = ES (ES EZ)

And now we can implement standard encoding of a typed lambda calculus

data Exp :: [Type] -> Type -> Type where
  Var :: Elem xs a -> Exp xs a
  Lam :: Exp (x:xs) a -> Exp xs (x -> a)
  App :: Exp xs (a -> b) -> (Exp xs a -> Exp xs b)

so we can capture functions as

expId :: Exp '[] (a -> a)
expId = Lam (Var EZ)

but we lack the polymorphism of System F, we cannot define “Exp '[] (forall x. x -> x).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment