@@ -1,10 +1,11 @@
1{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-}
1{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-}
3module Evaluate(normalise,
3module Evaluate(normalise,
4− Fun(..), Def(..), Context,
4+ Fun(..), Def(..), Context, toAlist,
5 emptyContext, addToCtxt, addConstant, addDatatype,
5 emptyContext, addToCtxt, addConstant, addDatatype,
6 lookupTy, lookupP, lookupDef, lookupVal, lookupTyEnv) where
6 lookupTy, lookupP, lookupDef, lookupVal, lookupTyEnv) where
8+import qualified Data.Map as Map
8import Debug.Trace
9import Debug.Trace
9import Core
10import Core
@@ -97,6 +98,7 @@ data Def = Function Fun
97 | Constant NameType Type Value
98 | Constant NameType Type Value
98 deriving Show
99 deriving Show
100type Context = [(Name, Def)]
102type Context = [(Name, Def)]
102emptyContext = []
104emptyContext = []
@@ -104,6 +106,24 @@ emptyContext = []
104addDef :: Name -> Def -> Context -> Context
106addDef :: Name -> Def -> Context -> Context
105addDef n d ctxt = (n, d) : ctxt
107addDef n d ctxt = (n, d) : ctxt
109+lookupCtxt :: Name -> Context -> Maybe Def
110+lookupCtxt n c = lookup n c
113+type Context = Map.Map Name Def
114+emptyContext = Map.empty
116+addDef :: Name -> Def -> Context -> Context
119+lookupCtxt :: Name -> Context -> Maybe Def
120+lookupCtxt = Map.lookup
122+toAlist :: Context -> [(Name, Def)]
107addToCtxt :: Name -> Term -> Type -> Context -> Context
127addToCtxt :: Name -> Term -> Type -> Context -> Context
108addToCtxt n tm ty ctxt = addDef n (Function (Fun ty (eval ctxt [] ty)
128addToCtxt n tm ty ctxt = addDef n (Function (Fun ty (eval ctxt [] ty)
109 tm (eval ctxt [] tm))) ctxt
129 tm (eval ctxt [] tm))) ctxt
@@ -124,28 +144,28 @@ addDatatype (Data n ty cons) ctxt
124 (Constant (DCon tag (arity ty')) ty (eval ctxt [] ty)) ctxt)
144 (Constant (DCon tag (arity ty')) ty (eval ctxt [] ty)) ctxt)
126lookupTy :: Name -> Context -> Maybe Type
146lookupTy :: Name -> Context -> Maybe Type
127−lookupTy n ctxt = do def <- lookup n ctxt
147+lookupTy n ctxt = do def <- lookupCtxt n ctxt
128 case def of
148 case def of
129 (Function (Fun ty _ _ _)) -> return ty
149 (Function (Fun ty _ _ _)) -> return ty
130 (Constant _ ty _) -> return ty
150 (Constant _ ty _) -> return ty
132lookupP :: Name -> Context -> Maybe Term
152lookupP :: Name -> Context -> Maybe Term
133lookupP n ctxt
153lookupP n ctxt
134− = do def <- lookup n ctxt
154+ = do def <- lookupCtxt n ctxt
135 case def of
155 case def of
136 (Function (Fun ty _ tm _)) -> return (P Ref n ty)
156 (Function (Fun ty _ tm _)) -> return (P Ref n ty)
137 (Constant nt ty hty) -> return (P nt n ty)
157 (Constant nt ty hty) -> return (P nt n ty)
139lookupDef :: Name -> Context -> Maybe Term
159lookupDef :: Name -> Context -> Maybe Term
140lookupDef n ctxt
160lookupDef n ctxt
141− = do def <- lookup n ctxt
161+ = do def <- lookupCtxt n ctxt
142 case def of
162 case def of
143 (Function (Fun ty _ tm _)) -> return tm
163 (Function (Fun ty _ tm _)) -> return tm
144 (Constant nt ty hty) -> return (P nt n ty)
164 (Constant nt ty hty) -> return (P nt n ty)
146lookupVal :: Name -> Context -> Maybe Value
166lookupVal :: Name -> Context -> Maybe Value
147lookupVal n ctxt
167lookupVal n ctxt
148− = do def <- lookup n ctxt
168+ = do def <- lookupCtxt n ctxt
149 case def of
169 case def of
150 (Function (Fun _ _ _ htm)) -> return htm
170 (Function (Fun _ _ _ htm)) -> return htm
151 (Constant nt ty hty) -> return (VP nt n hty)
171 (Constant nt ty hty) -> return (VP nt n hty)