Created
June 11, 2015 19:22
-
-
Save sdiehl/7d1381fd4dae0e3299d6 to your computer and use it in GitHub Desktop.
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
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