Created
September 4, 2018 03:56
-
-
Save j-walk/d64190bbd52b65d448e3cfafe98086d1 to your computer and use it in GitHub Desktop.
Lambda Calculus reducer
This file contains 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
data Expr = DeBruijn Int | |
| App Expr Expr | |
| Lambda Expr deriving Show | |
eval :: Expr -> Expr | |
eval (App (Lambda body) arg) = (arg `captureIn` body) 0 | |
eval (App (App lhs rhs) arg) = eval $ App (eval (App lhs rhs)) arg | |
eval a = a | |
captureIn :: Expr -> Expr -> Int -> Expr | |
-- | |
captureIn arg body@(DeBruijn i) depth | |
| depth == i = arg | |
| otherwise = body | |
captureIn arg (App lhs rhs) depth = | |
eval $ App ((arg `captureIn` lhs) depth) | |
((arg `captureIn` rhs) depth) | |
captureIn arg (Lambda body) depth = | |
Lambda $ (arg `captureIn` body) (depth + 1) | |
main = print $ eval $ App (Lambda (DeBruijn 0)) (DeBruijn 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment