Skip to content

Instantly share code, notes, and snippets.

@MgaMPKAy
Created February 22, 2012 04:34
Show Gist options
  • Save MgaMPKAy/1881430 to your computer and use it in GitHub Desktop.
Save MgaMPKAy/1881430 to your computer and use it in GitHub Desktop.
Types and Programming Languages chapter 3
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