Skip to content

Instantly share code, notes, and snippets.

@sdiehl
Created June 11, 2015 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdiehl/7d1381fd4dae0e3299d6 to your computer and use it in GitHub Desktop.
Save sdiehl/7d1381fd4dae0e3299d6 to your computer and use it in GitHub Desktop.
module Eval (
eval
) where
import Syntax
import Data.Functor ((<$>))
isNum :: Expr -> Bool
isNum Zero = True
isNum (Succ x) = isNum x
isNum _ = False
isVal :: Expr -> Bool
isVal Tr = True
isVal Fl = True
isVal t | isNum t = True
isVal _ = False
eval :: Expr -> Maybe Expr
eval x = case step x of
Just a -> eval a -- Case 1: We evaluated a single step go again.
Nothing | isVal x -> Just x -- Case 2: Normal form found, give back normal form.
| otherwise -> Nothing -- Case 3: No normal form, we have an expression so give nothing.
where
step a = case a of
Succ t -> Succ <$> step t
Pred Zero -> Just Zero
Pred (Succ nv) | isNum nv -> Just nv
Pred t -> Pred <$> step t
IsZero Zero -> Just Tr
IsZero (Succ _) -> Just Fl
IsZero t -> IsZero <$> step t
If Tr t _ -> eval t
If Fl _ t -> eval t
If c t f -> (\ec -> If ec t f) <$> (step c)
_ -> Nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment