Created
February 22, 2012 04:34
-
-
Save MgaMPKAy/1881430 to your computer and use it in GitHub Desktop.
Types and Programming Languages chapter 3
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
module Arith where | |
import Prelude hiding (True, False) | |
data Arith = Zero | |
| True | |
| False | |
| If Arith Arith Arith | |
| Succ Arith | |
| Pred Arith | |
| IsZero Arith | |
deriving (Eq, Show) | |
eval :: Arith -> Arith | |
eval True = True | |
eval False = False | |
eval Zero = Zero | |
eval (IsZero a) = if eval a == Zero then True else False | |
eval (If p q1 q2) = eval $ if (eval p) == True then q1 else q2 | |
eval (Succ a) = case a of | |
Pred a1 -> eval a1 | |
Zero -> Succ Zero | |
a1 -> eval (Succ (eval a1)) | |
eval (Pred a) = case a of | |
Succ a1 -> eval a1 | |
Zero -> Pred Zero | |
a1 -> eval (Pred (eval a1)) | |
{-- | |
test1 :: Arith | |
test1 = IsZero (Pred $ Pred $ Succ $ Pred $ Succ $ Succ Zero) | |
test2 :: Arith | |
test2 = eval $ IsZero $ Pred $ (If (IsZero test1) (Pred Zero) (Succ Zero)) | |
--} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment