module Lecture10LambdaCalc where {-- simple version of Lambda calculus without alpha convertion, beta reduction uses a small-step flavour (one step at the time) --} -- Syntax type Name = String data Expr = Var Name | App Expr Expr | Lam Name Expr deriving (Eq, Show) -- Substitution -- substitute x by expr in expr' and return expr'' after the substitution subst :: Name -> Expr -> Expr -> Expr subst x s (Var y) | x == y = undefined | otherwise = undefined subst x s (App t1 t2) = undefined subst x s (Lam y t0) | x == y = undefined -- bound variable, stop | otherwise = undefined -- evaluation eval :: Expr -> Expr eval t = case beta t of Just t' -> eval t' -- if something can be reduced then call eval again (continue beta reduction) Nothing -> t -- we cannot reduce further, return the result -- Beta Reduction -- small step semantics beta :: Expr -> Maybe Expr beta (App (Lam x t0) t) = undefined beta (App t1 t2) = case beta t1 of -- try reducing t1 Just t1' -> undefined -- success case, then rebuild the term Nothing -> case beta t2 of -- try reducing t2 Just t2' -> undefined -- success case, then rebuild the term Nothing -> Nothing beta (Lam x t0) = case beta t0 of -- try reducing t0 Just t0' -> undefined -- success case, then rebuild the term Nothing -> Nothing beta _ = Nothing {-- example: (\x.x) y -->* y --} example1 :: Expr example1 = App (Lam "x" (Var "x")) (Var "y") {-- > eval example1 Var "y" --} {-- example: (\y.y y) x -->* x x --} example2 :: Expr example2 = App (Lam "y" (App (Var "y") (Var "y"))) (Var "x") {-- > eval example2 App (Var "x") (Var "x") --}