From 5d1d3b4f251bf938648d7d21c6641a1a0cc0768b Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Sat, 26 Dec 2020 18:08:15 +0100 Subject: Initial --- Eval.hs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Eval.hs (limited to 'Eval.hs') diff --git a/Eval.hs b/Eval.hs new file mode 100644 index 0000000..af2cd3c --- /dev/null +++ b/Eval.hs @@ -0,0 +1,28 @@ +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE GADTs #-} +{-# LANGUAGE RankNTypes #-} +module Eval where + +import AST +import ASTfunc + + +eval :: Expr '[] t -> t +eval = eval'Expr VZ + +eval'Expr :: Val env -> Expr env t -> t +eval'Expr v (Expr e) = eval' eval'Expr v e + +eval' :: (forall env' t'. Val env' -> expr env' t' -> t') + -> Val env -> PExpr expr env t -> t +eval' _ _ (Const _ x) = x +eval' f v (Pair x y) = (f v x, f v y) +eval' _ _ Nil = () +eval' f v (Prim op x) = applyOper op (f v x) +eval' _ v (Var (V _ i)) = prjV v i +eval' f v (Let lhs rhs e) = f (vpush lhs (f v rhs) v) e + +applyOper :: Oper (t -> t') -> t -> t' +applyOper (Add t) (x, y) | NumDict <- reifyNum t = x + y +applyOper (Mul t) (x, y) | NumDict <- reifyNum t = x * y +applyOper Round x = round x -- cgit v1.2.3-70-g09d2