Skip to content

Instantly share code, notes, and snippets.

@DadgadCafe
Created June 3, 2017 12:36
Show Gist options
  • Save DadgadCafe/e8bfbc5a8055a58ed54fbf0740ba3d66 to your computer and use it in GitHub Desktop.
Save DadgadCafe/e8bfbc5a8055a58ed54fbf0740ba3d66 to your computer and use it in GitHub Desktop.
-- lambda
data Term = Lam Var Term
| Var Var
| App Term Term -- func application
data Var = V String
instance Show Var where
show (V s) = s
instance Show Term where
show (Var v) = show v
show (Lam v t) = "λ" ++ show v ++ "." ++ show t
show (App t1 t2) = "(" ++ show t1 ++ " " ++ show t2 ++ ")"
-- exercises
-- a b c d e f
(App (App (App (App (App (Var (V "a"))
(Var (V "b")))
(Var (V "c")))
(Var (V "d")))
(Var (V "e")))
(Var (V "f")))
-- (g h) (i j)
(App (App (Var (V "g"))
(Var (V "h")))
(App (Var (V "i"))
(Var (V "j"))))
-- λx.f (λy.f y)
(Lam (V "x")
(App (f)
(Lam (y)
(App (f)
(Var (y)))))
where f = Var (V "f")
y = V "y"
-- (λf.f x y) (λx.λy.x)
(App (Lam (f)
(App (App (Val f)
(Val (V "x")))
(Val (V "y"))))
(Lam (x)
(Lam (V "y")
(Val x))))
where f = V "f"
x = V "x"
-- f g λx.y z
(App (App (App (Val (V "f"))
(Val (V "g")))
(Lam (V "x")
(Val (V "y"))))
(Val (V "z")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment