Created
October 5, 2022 20:00
-
-
Save mjgpy3/be4e96fa8d12e54c29685ba5da665a7d to your computer and use it in GitHub Desktop.
calc v scheme in Haskell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module CalcVScheme where | |
import Prelude hiding (lookup) | |
data Term | |
= Var Var | |
| Lambda Var Term | |
| Apply Term Term | |
| Closure Env Var Term | |
deriving (Eq, Show) | |
type Env = [(Var, Term)] | |
type Var = String | |
eval e (Var v) = lookup e v | |
eval e (Lambda v t) = Closure e v t | |
eval e (Apply t0 t1) = apply (eval e t0) (eval e t1) | |
apply (Closure e v t0) t1 = eval (extend e v t1) t0 | |
lookup ((v0, t) : e) v1 | |
| v0 == v1 = t | |
| otherwise = lookup e v1 | |
extend e v t = (v, t):e | |
empty = [] | |
i n = Lambda n $ Var n | |
k a b = Lambda a $ Lambda b $ Var a | |
s x y z = Lambda x $ Lambda y $ Lambda z $ Apply (Apply (Var x) (Var z)) $ Apply (Var y) (Var z) | |
main = do | |
print $ eval empty (Apply (Lambda "x" (Var "x")) (Lambda "y" (Var "y"))) | |
print $ eval empty (Apply (Apply (k "a" "b") (i "x")) (i "y")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment