From: Mart Lubbers Date: Wed, 6 Mar 2019 10:33:41 +0000 (+0100) Subject: cleanup X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=9e4fcffea5466c7b06985cd7272dd657e0b457b0;p=minfp.git cleanup --- diff --git a/int.icl b/int.icl index 4eeedf0..eec77c4 100644 --- a/int.icl +++ b/int.icl @@ -1,7 +1,6 @@ implementation module int import StdEnv - import Data.Either import Data.Functor import Data.Maybe @@ -13,27 +12,25 @@ import Control.Monad.Trans import ast +:: Eval a :== StateT EvalState (Either [String]) a +:: EvalState :== [([Char], Value)] + int :: Expression -> Either [String] Value int e = evalStateT (eval e) preamble -:: Eval a :== StateT EvalState (Either [String]) a -:: EvalState :== [([Char], Value)] preamble = - [(['_if'], Func \i->pure (Func \t->pure (Func \e->eval i >>= \(Bool b)-> - if b (eval t) (eval e)))) + [(['_if'], Func \i->pure (Func \t->pure (Func \e->eval i >>= \(Bool b)->if b (eval t) (eval e)))) ,(['_eq'], binop \(Int i) (Int j)->Bool (i==j)) - ,(['_sub'], binop \(Int i) (Int j)->Int (i-j)) - ,(['_add'], binop \(Int i) (Int j)->Int (i+j)) - ,(['_mul'], binop \(Int i) (Int j)->Int (i*j)) - ,(['_div'], binop \(Int i) (Int j)->Int (i/j)) + ,(['_sub'], binop \(Int i) (Int j)->Int (i-j)) + ,(['_add'], binop \(Int i) (Int j)->Int (i+j)) + ,(['_mul'], binop \(Int i) (Int j)->Int (i*j)) + ,(['_div'], binop \(Int i) (Int j)->Int (i/j)) ] where binop op = Func \l->pure (Func \r->op <$> eval l <*> eval r) sub :: [Char] Expression Expression -> Expression -sub ident subst (Let v b rest) - = Let v (sub ident subst b) - (if (v == ident) rest (sub ident subst rest)) +sub ident subst (Let v b rest) = Let v (sub ident subst b) (if (v == ident) rest (sub ident subst rest)) sub ident subst (Var v) | ident == v = subst sub ident subst (App e1 e2) = App (sub ident subst e1) (sub ident subst e2) @@ -42,8 +39,7 @@ sub ident subst (Lambda v b) sub _ _ x = x eval :: Expression -> Eval Value -eval (Let ident body rest) - = eval body >>= \v->modify (\vs->[(ident, v):vs]) >>| eval rest +eval (Let ident body rest) = eval body >>= \v->modify (\vs->[(ident, v):vs]) >>| eval rest eval (Lit v) = pure v eval (Var v) = gets (lookup v) >>= maybe (liftT (Left [toString v +++ " not found"])) pure eval (App e1 e2) = eval e1 >>= \(Func v)->v e2