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