Created
December 14, 2019 16:40
-
-
Save lunalunaa/6324aead9ba6acf42ae1c06f926d2ecc to your computer and use it in GitHub Desktop.
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
| data Term | |
| = TmVar Integer | |
| | TmAbs String Term | |
| | TmApp Term Term | |
| deriving (Show, Eq) | |
| termShift :: Integer -> Term -> Term | |
| termShift d = walk 0 | |
| where | |
| walk :: Integer -> Term -> Term | |
| walk c t = case t of | |
| TmVar x -> if x > c then TmVar (x + d) else t | |
| TmAbs x t1 -> TmAbs x (walk (c+1) t1) | |
| TmApp t1 t2 -> TmApp (walk c t1) (walk c t2) | |
| termSubst :: Integer -> Term -> Term -> Term | |
| termSubst j s = walk 0 | |
| where | |
| walk :: Integer -> Term -> Term | |
| walk c t = case t of | |
| TmVar x -> if x == j + c then termShift c s else t | |
| TmAbs x t1 -> TmAbs x (walk (c+1) t1) | |
| TmApp t1 t2 -> TmApp (walk c t1) (walk c t2) | |
| termSubstTop :: Term -> Term -> Term | |
| termSubstTop s t = termShift (-1) $ termSubst 0 (termShift 1 s) t | |
| isval :: Term -> Bool | |
| isval t = case t of | |
| TmAbs _ _ -> True | |
| _ -> False | |
| eval :: Term -> Term | |
| eval t = case t of | |
| TmApp t1 t2 -> case eval t1 of | |
| TmAbs x t1' -> let v2' = eval t2 in | |
| if isval v2' | |
| then eval (termSubstTop v2' t1') | |
| else TmApp t1' t2 | |
| _ -> t | |
| _ -> t |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment