summaryrefslogtreecommitdiff
path: root/src/K/Eval.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/K/Eval.hs')
-rw-r--r--src/K/Eval.hs40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/K/Eval.hs b/src/K/Eval.hs
new file mode 100644
index 0000000..a7ebcba
--- /dev/null
+++ b/src/K/Eval.hs
@@ -0,0 +1,40 @@
+module K.Eval where
+
+import K.Ast
+import K.Parsing
+import Data.Maybe
+
+isNum Zero = True
+isNum (Succ t) = isNum t
+isNum _ = False
+
+isVal :: Expr -> Bool
+isVal Tru = True
+isVal Fals = True
+isVal t | isNum t = True
+isVal _ = False
+
+eval' x = case x of
+ IsZero Zero -> Just Tru
+ IsZero (Succ t) | isNum t -> Just Fals
+ IsZero t -> IsZero <$> eval' t
+
+ Succ t -> Succ <$> eval' t
+
+ Pred Zero -> Just Zero
+ Pred (Succ t) | isNum t -> Just t
+ Pred t -> Pred <$> eval' t
+
+ If Tru t _ -> Just t
+ If Fals _ f -> Just f
+ If c t f -> (\c' -> If c' t f) <$> eval' c
+
+ _ -> Nothing
+
+nf x = fromMaybe x (nf <$> eval' x)
+
+eval :: Expr -> Maybe Expr
+eval t = case nf t of
+ nft | isVal nft -> Just nft
+ | otherwise -> Nothing
+