@@ -3,9 +3,9 @@
33
44module Core.Evaluate(normalise,
55 Fun(..), Def(..), Context,
6− addToCtxt, addConstant, addDatatype, addCasedef,
6+ addToCtxt, addTyDecl, addDatatype, addCasedef, addOperator,
77 lookupTy, lookupP, lookupDef, lookupVal, lookupTyEnv,
8− Value) where
8+ Value(..)) where
99
1010import Debug.Trace
1111
@@ -19,6 +19,7 @@ data Value = VP NameType Name Value
1919 | VBind Name (Binder Value) (Value -> Value)
2020 | VApp Value Value
2121 | VSet Int
22+ | VConstant Const
2223 | VTmp Int
2324
2425instance Show Value where
@@ -54,7 +55,7 @@ eval ctxt genv tm = ev [] tm where
5455 | Just (Let t v) <- lookup n genv = ev env v
5556 ev env (P Ref n ty) = case lookupDef n ctxt of
5657 Just (Function (Fun _ _ _ v)) -> v
57− Just (Constant nt ty hty) -> VP nt n hty
58+ Just (TyDecl nt ty hty) -> VP nt n hty
5859 Just (CaseOp _ [] tree) ->
5960 case evCase env [] [] tree of
6061 (Nothing, _) -> VP Ref n (ev env ty)
@@ -70,6 +71,7 @@ eval ctxt genv tm = ev [] tm where
7071 ev env (Bind n b sc) = VBind n (vbind env b) (\x -> ev (x:env) sc)
7172 where vbind env t = fmap (ev env) t
7273 ev env (App f a) = evApply env [ev env a] (ev env f)
74+ ev env (Constant c) = VConstant c
7375 ev env (Set i) = VSet i
7476
7577 evApply env args (VApp f a) = evApply env (a:args) f
@@ -81,6 +83,12 @@ eval ctxt genv tm = ev [] tm where
8183 = case evCase env ns args tree of
8284 (Nothing, _) -> unload env (VP Ref n ty) args
8385 (Just v, rest) -> evApply env rest v
86+ | Just (Operator _ i op) <- lookupDef n ctxt
87+ = if (i <= length args)
88+ then case op (take i args) of
89+ Nothing -> unload env (VP Ref n ty) args
90+ Just v -> evApply env (drop i args) v
91+ else unload env (VP Ref n ty) args
8492 apply env f (a:as) = unload env f (a:as)
8593 apply env f [] = f
8694
@@ -107,9 +115,10 @@ eval ctxt genv tm = ev [] tm where
107115 Maybe ([(Name, Value)], SC)
108116 chooseAlt _ (VP (DCon i a) _ _, args) alts amap
109117 | Just (ns, sc) <- findTag i alts = Just (updateAmap (zip ns args) amap, sc)
110− | Just sc <- findDefault alts = Just (amap, sc)
111−-- chooseAlt v _ alts amap
112−-- | Just sc <- safeDefault alts = Just (amap, sc)
118+ | Just v <- findDefault alts = Just (amap, v)
119+ chooseAlt _ (VConstant c, []) alts amap
120+ | Just v <- findConst c alts = Just (amap, v)
121+ | Just v <- findDefault alts = Just (amap, v)
113122 chooseAlt _ _ _ _ = Nothing
114123
115124 -- Replace old variable names in the map with new matches
@@ -125,6 +134,10 @@ eval ctxt genv tm = ev [] tm where
125134 findDefault (DefaultCase sc : xs) = Just sc
126135 findDefault (_ : xs) = findDefault xs
127136
137+ findConst c [] = Nothing
138+ findConst c (ConstCase c' v : xs) | c == c' = Just v
139+ findConst c (_ : xs) = findConst c xs
140+
128141 getValArgs tm = getValArgs' tm []
129142 getValArgs' (VApp f a) as = getValArgs' f (a:as)
130143 getValArgs' f as = (f, as)
@@ -136,6 +149,7 @@ quote i (VBind n b sc) = Bind n (quoteB b) (quote (i+1) (sc (VTmp i)))
136149 where quoteB t = fmap (quote i) t
137150quote i (VApp f a) = App (quote i f) (quote i a)
138151quote i (VSet u) = Set u
152+quote i (VConstant c) = Constant c
139153quote i (VTmp x) = V (i - x - 1)
140154
141155
@@ -157,14 +171,14 @@ data Fun = Fun Type Value Term Value
157171 A CaseOp is a function defined by a simple case tree -}
158172
159173data Def = Function Fun
160− | Constant NameType Type Value
161− | Operator Type ([Value] -> Maybe Value)
174+ | TyDecl NameType Type Value
175+ | Operator Type Int ([Value] -> Maybe Value)
162176 | CaseOp Type [Name] SC
163177
164178instance Show Def where
165179 show (Function f) = "Function: " ++ show f
166− show (Constant nt ty val) = "Constant: " ++ show nt ++ " " ++ show ty
167− show (Operator ty _) = "Operator: " ++ show ty
180+ show (TyDecl nt ty val) = "TyDecl: " ++ show nt ++ " " ++ show ty
181+ show (Operator ty _ _) = "Operator: " ++ show ty
168182 show (CaseOp ty ns sc) = "Case: " ++ show ns ++ " " ++ show sc
169183
170184-------
@@ -175,31 +189,35 @@ addToCtxt :: Name -> Term -> Type -> Context -> Context
175189addToCtxt n tm ty ctxt = addDef n (Function (Fun ty (eval ctxt [] ty)
176190 tm (eval ctxt [] tm))) ctxt
177191
178−addConstant :: Name -> Type -> Context -> Context
179−addConstant n ty ctxt = addDef n (Constant Ref ty (eval ctxt [] ty)) ctxt
192+addTyDecl :: Name -> Type -> Context -> Context
193+addTyDecl n ty ctxt = addDef n (TyDecl Ref ty (eval ctxt [] ty)) ctxt
180194
181195addDatatype :: Datatype Name -> Context -> Context
182196addDatatype (Data n ty cons) ctxt
183197 = let ty' = normalise ctxt [] ty in
184198 addCons 0 cons (addDef n
185− (Constant (TCon (arity ty')) ty (eval ctxt [] ty)) ctxt)
199+ (TyDecl (TCon (arity ty')) ty (eval ctxt [] ty)) ctxt)
186200 where
187201 addCons tag [] ctxt = ctxt
188202 addCons tag ((n, ty) : cons) ctxt
189203 = let ty' = normalise ctxt [] ty in
190204 addCons (tag+1) cons (addDef n
191− (Constant (DCon tag (arity ty')) ty (eval ctxt [] ty)) ctxt)
205+ (TyDecl (DCon tag (arity ty')) ty (eval ctxt [] ty)) ctxt)
192206
193207addCasedef :: Name -> CaseDef -> Type -> Context -> Context
194208addCasedef n (CaseDef args sc) ty ctxt
195209 = addDef n (CaseOp ty args sc) ctxt
196210
211+addOperator :: Name -> Type -> Int -> ([Value] -> Maybe Value) -> Context -> Context
212+addOperator n ty a op ctxt
213+ = addDef n (Operator ty a op) ctxt
214+
197215lookupTy :: Name -> Context -> Maybe Type
198216lookupTy n ctxt = do def <- lookupCtxt n ctxt
199217 case def of
200218 (Function (Fun ty _ _ _)) -> return ty
201− (Constant _ ty _) -> return ty
202− (Operator ty _) -> return ty
219+ (TyDecl _ ty _) -> return ty
220+ (Operator ty _ _) -> return ty
203221 (CaseOp ty _ _) -> return ty
204222
205223lookupP :: Name -> Context -> Maybe Term
@@ -207,9 +225,9 @@ lookupP n ctxt
207225 = do def <- lookupCtxt n ctxt
208226 case def of
209227 (Function (Fun ty _ tm _)) -> return (P Ref n ty)
210− (Constant nt ty hty) -> return (P nt n ty)
228+ (TyDecl nt ty hty) -> return (P nt n ty)
211229 (CaseOp ty _ _) -> return (P Ref n ty)
212− (Operator ty _) -> return (P Ref n ty)
230+ (Operator ty _ _) -> return (P Ref n ty)
213231
214232lookupDef :: Name -> Context -> Maybe Def
215233lookupDef n ctxt = lookupCtxt n ctxt
@@ -219,7 +237,7 @@ lookupVal n ctxt
219237 = do def <- lookupCtxt n ctxt
220238 case def of
221239 (Function (Fun _ _ _ htm)) -> return htm
222− (Constant nt ty hty) -> return (VP nt n hty)
240+ (TyDecl nt ty hty) -> return (VP nt n hty)
223241
224242lookupTyEnv :: Name -> Env -> Maybe (Int, Type)
225243lookupTyEnv n env = li n 0 env where