data Term = Var Integer
| Apply Term Term
| Lambda Term
deriving (Eq, Show)
sub :: Term -> Integer -> Term -> Term
sub e v r = case e of
Var x -> if x == v then r else e
Apply m1 m2 -> Apply (sub m1 v r) (sub m2 v r)
Lambda t -> Lambda (sub t (v + 1) r)
beta :: Term -> Term
beta t = case t of
Apply (Lambda e) e' -> sub e 0 e'
otherwise -> t
eta :: Term -> Term
eta t = case t of
Lambda (Apply f (Var 0)) -> f
otherwise -> t
reduce :: Term -> Term
reduce t = if t == t'
then t
else reduce t'
where t' = beta . eta $ t
我试过了:
let zero = Lambda $ Lambda $ Var 0
let succ = Lambda $ Lambda $ Lambda $ Apply (Var 1) $ (Apply (Apply (Var 2) (Var 1)) (Var 0))
reduce (Apply succ zero)
在GHCi中,但它似乎没有给我一个表达式(Lambda(应用(Var 1)(Var 0))我正在寻找。相反它给了我:
Lambda (Lambda (Apply (Var 1) (Apply (Apply (Lambda (Lambda (Var 0))) (Var 1)) (Var 0))))
变量不是按名称编码,而是按需要走多少个lambdas来获取参数。
答案 0 :(得分:3)
与通常评估lambda演算的方式一样,您的reducer不会减少内部Lambda
项 - 它只会删除顶级redex。它产生的结果应该是等效到one
,因为如果你将两者都应用于相同的参数,你将获得相同的结果,但在语法上不相同。