@@ -1,10 +1,11 @@
11{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-}
22
33module Evaluate(normalise,
4− Fun(..), Def(..), Context,
4+ Fun(..), Def(..), Context, toAlist,
55 emptyContext, addToCtxt, addConstant, addDatatype,
66 lookupTy, lookupP, lookupDef, lookupVal, lookupTyEnv) where
77
8+import qualified Data.Map as Map
89import Debug.Trace
910import Core
1011
@@ -97,6 +98,7 @@ data Def = Function Fun
9798 | Constant NameType Type Value
9899 deriving Show
99100
101+{-
100102type Context = [(Name, Def)]
101103
102104emptyContext = []
@@ -104,6 +106,24 @@ emptyContext = []
104106addDef :: Name -> Def -> Context -> Context
105107addDef n d ctxt = (n, d) : ctxt
106108
109+lookupCtxt :: Name -> Context -> Maybe Def
110+lookupCtxt n c = lookup n c
111+-}
112+
113+type Context = Map.Map Name Def
114+emptyContext = Map.empty
115+
116+addDef :: Name -> Def -> Context -> Context
117+addDef = Map.insert
118+
119+lookupCtxt :: Name -> Context -> Maybe Def
120+lookupCtxt = Map.lookup
121+
122+toAlist :: Context -> [(Name, Def)]
123+toAlist = Map.toList
124+
125+-------
126+
107127addToCtxt :: Name -> Term -> Type -> Context -> Context
108128addToCtxt n tm ty ctxt = addDef n (Function (Fun ty (eval ctxt [] ty)
109129 tm (eval ctxt [] tm))) ctxt
@@ -124,28 +144,28 @@ addDatatype (Data n ty cons) ctxt
124144 (Constant (DCon tag (arity ty')) ty (eval ctxt [] ty)) ctxt)
125145
126146lookupTy :: Name -> Context -> Maybe Type
127−lookupTy n ctxt = do def <- lookup n ctxt
147+lookupTy n ctxt = do def <- lookupCtxt n ctxt
128148 case def of
129149 (Function (Fun ty _ _ _)) -> return ty
130150 (Constant _ ty _) -> return ty
131151
132152lookupP :: Name -> Context -> Maybe Term
133153lookupP n ctxt
134− = do def <- lookup n ctxt
154+ = do def <- lookupCtxt n ctxt
135155 case def of
136156 (Function (Fun ty _ tm _)) -> return (P Ref n ty)
137157 (Constant nt ty hty) -> return (P nt n ty)
138158
139159lookupDef :: Name -> Context -> Maybe Term
140160lookupDef n ctxt
141− = do def <- lookup n ctxt
161+ = do def <- lookupCtxt n ctxt
142162 case def of
143163 (Function (Fun ty _ tm _)) -> return tm
144164 (Constant nt ty hty) -> return (P nt n ty)
145165
146166lookupVal :: Name -> Context -> Maybe Value
147167lookupVal n ctxt
148− = do def <- lookup n ctxt
168+ = do def <- lookupCtxt n ctxt
149169 case def of
150170 (Function (Fun _ _ _ htm)) -> return htm
151171 (Constant nt ty hty) -> return (VP nt n hty)