Last active
December 18, 2015 08:29
-
-
Save kakkun61/5754414 to your computer and use it in GitHub Desktop.
TaPL 3章
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 Term = TTrue | TFalse | Zero | |
| Succ Term | Pred Term | IsZero Term | |
| If Term Term Term | |
deriving (Eq, Show) | |
eval :: Term -> Term | |
eval (IsZero Zero) = TTrue | |
eval (IsZero TTrue) = TFalse | |
eval (IsZero TFalse) = TFalse | |
eval (IsZero t) = isZero $ eval t | |
where | |
isZero Zero = TTrue | |
isZero _ = TFalse | |
eval (If c t f) = if eval c == TTrue then eval t else eval f | |
eval (Succ (Pred t)) = eval t | |
eval (Succ t) = Succ $ eval t | |
eval (Pred (Succ t)) = eval t | |
eval (Pred t) = Pred $ eval t | |
eval t = t | |
pretty :: Term -> String | |
pretty TTrue = "True" | |
pretty TFalse = "False" | |
pretty t = show $ num t | |
num :: Term -> Integer | |
num (Succ t) = (num t) + 1 | |
num (Pred t) = (num t) - 1 | |
num Zero = 0 | |
num _ = error "not evaluated syntax tree" | |
main = do | |
putStrLn $ pretty $ eval $ Succ $ Zero | |
putStrLn $ pretty $ eval $ | |
If (IsZero $ Pred $ Succ $ Zero) | |
(Succ $ Succ $ Succ $ Zero) | |
(Pred $ Zero) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
s/pritty/pretty/