@@ -3,9 +3,9 @@
4module Core.Evaluate(normalise,
4module Core.Evaluate(normalise,
5 Fun(..), Def(..), Context,
5 Fun(..), Def(..), Context,
6− addToCtxt, addConstant, addDatatype, addCasedef,
6+ addToCtxt, addTyDecl, addDatatype, addCasedef, addOperator,
7 lookupTy, lookupP, lookupDef, lookupVal, lookupTyEnv,
7 lookupTy, lookupP, lookupDef, lookupVal, lookupTyEnv,
8− Value) where
8+ Value(..)) where
10import Debug.Trace
10import Debug.Trace
@@ -19,6 +19,7 @@ data Value = VP NameType Name Value
19 | VBind Name (Binder Value) (Value -> Value)
19 | VBind Name (Binder Value) (Value -> Value)
20 | VApp Value Value
20 | VApp Value Value
21 | VSet Int
21 | VSet Int
22 | VTmp Int
23 | VTmp Int
24instance Show Value where
25instance Show Value where
@@ -54,7 +55,7 @@ eval ctxt genv tm = ev [] tm where
54 | Just (Let t v) <- lookup n genv = ev env v
55 | Just (Let t v) <- lookup n genv = ev env v
55 ev env (P Ref n ty) = case lookupDef n ctxt of
56 ev env (P Ref n ty) = case lookupDef n ctxt of
56 Just (Function (Fun _ _ _ v)) -> v
57 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
58 Just (CaseOp _ [] tree) ->
59 Just (CaseOp _ [] tree) ->
59 case evCase env [] [] tree of
60 case evCase env [] [] tree of
60 (Nothing, _) -> VP Ref n (ev env ty)
61 (Nothing, _) -> VP Ref n (ev env ty)
@@ -70,6 +71,7 @@ eval ctxt genv tm = ev [] tm where
70 ev env (Bind n b sc) = VBind n (vbind env b) (\x -> ev (x:env) sc)
71 ev env (Bind n b sc) = VBind n (vbind env b) (\x -> ev (x:env) sc)
71 where vbind env t = fmap (ev env) t
72 where vbind env t = fmap (ev env) t
72 ev env (App f a) = evApply env [ev env a] (ev env f)
73 ev env (App f a) = evApply env [ev env a] (ev env f)
74+ ev env (Constant c) = VConstant c
73 ev env (Set i) = VSet i
75 ev env (Set i) = VSet i
75 evApply env args (VApp f a) = evApply env (a:args) f
77 evApply env args (VApp f a) = evApply env (a:args) f
@@ -81,6 +83,12 @@ eval ctxt genv tm = ev [] tm where
81 = case evCase env ns args tree of
83 = case evCase env ns args tree of
82 (Nothing, _) -> unload env (VP Ref n ty) args
84 (Nothing, _) -> unload env (VP Ref n ty) args
83 (Just v, rest) -> evApply env rest v
85 (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
84 apply env f (a:as) = unload env f (a:as)
92 apply env f (a:as) = unload env f (a:as)
85 apply env f [] = f
93 apply env f [] = f
@@ -107,9 +115,10 @@ eval ctxt genv tm = ev [] tm where
107 Maybe ([(Name, Value)], SC)
115 Maybe ([(Name, Value)], SC)
108 chooseAlt _ (VP (DCon i a) _ _, args) alts amap
116 chooseAlt _ (VP (DCon i a) _ _, args) alts amap
109 | Just (ns, sc) <- findTag i alts = Just (updateAmap (zip ns args) amap, sc)
117 | Just (ns, sc) <- findTag i alts = Just (updateAmap (zip ns args) amap, sc)
110− | Just sc <- findDefault alts = Just (amap, sc)
118+ | Just v <- findDefault alts = Just (amap, v)
111−-- chooseAlt v _ alts amap
119+ chooseAlt _ (VConstant c, []) alts amap
112−-- | Just sc <- safeDefault alts = Just (amap, sc)
120+ | Just v <- findConst c alts = Just (amap, v)
121+ | Just v <- findDefault alts = Just (amap, v)
113 chooseAlt _ _ _ _ = Nothing
122 chooseAlt _ _ _ _ = Nothing
115 -- Replace old variable names in the map with new matches
124 -- Replace old variable names in the map with new matches
@@ -125,6 +134,10 @@ eval ctxt genv tm = ev [] tm where
125 findDefault (DefaultCase sc : xs) = Just sc
134 findDefault (DefaultCase sc : xs) = Just sc
126 findDefault (_ : xs) = findDefault xs
135 findDefault (_ : xs) = findDefault xs
137+ findConst c [] = Nothing
138+ findConst c (ConstCase c' v : xs) | c == c' = Just v
139+ findConst c (_ : xs) = findConst c xs
128 getValArgs tm = getValArgs' tm []
141 getValArgs tm = getValArgs' tm []
129 getValArgs' (VApp f a) as = getValArgs' f (a:as)
142 getValArgs' (VApp f a) as = getValArgs' f (a:as)
130 getValArgs' f as = (f, as)
143 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)))
136 where quoteB t = fmap (quote i) t
149 where quoteB t = fmap (quote i) t
137quote i (VApp f a) = App (quote i f) (quote i a)
150quote i (VApp f a) = App (quote i f) (quote i a)
138quote i (VSet u) = Set u
151quote i (VSet u) = Set u
152+quote i (VConstant c) = Constant c
139quote i (VTmp x) = V (i - x - 1)
153quote i (VTmp x) = V (i - x - 1)
@@ -157,14 +171,14 @@ data Fun = Fun Type Value Term Value
157 A CaseOp is a function defined by a simple case tree -}
171 A CaseOp is a function defined by a simple case tree -}
159data Def = Function Fun
173data Def = Function Fun
160− | Constant NameType Type Value
174+ | TyDecl NameType Type Value
161− | Operator Type ([Value] -> Maybe Value)
175+ | Operator Type Int ([Value] -> Maybe Value)
162 | CaseOp Type [Name] SC
176 | CaseOp Type [Name] SC
164instance Show Def where
178instance Show Def where
165 show (Function f) = "Function: " ++ show f
179 show (Function f) = "Function: " ++ show f
166− show (Constant nt ty val) = "Constant: " ++ show nt ++ " " ++ show ty
180+ show (TyDecl nt ty val) = "TyDecl: " ++ show nt ++ " " ++ show ty
167− show (Operator ty _) = "Operator: " ++ show ty
181+ show (Operator ty _ _) = "Operator: " ++ show ty
168 show (CaseOp ty ns sc) = "Case: " ++ show ns ++ " " ++ show sc
182 show (CaseOp ty ns sc) = "Case: " ++ show ns ++ " " ++ show sc
@@ -175,31 +189,35 @@ addToCtxt :: Name -> Term -> Type -> Context -> Context
175addToCtxt n tm ty ctxt = addDef n (Function (Fun ty (eval ctxt [] ty)
189addToCtxt n tm ty ctxt = addDef n (Function (Fun ty (eval ctxt [] ty)
176 tm (eval ctxt [] tm))) ctxt
190 tm (eval ctxt [] tm))) ctxt
178−addConstant :: Name -> Type -> Context -> Context
192+addTyDecl :: Name -> Type -> Context -> Context
179−addConstant n ty ctxt = addDef n (Constant Ref ty (eval ctxt [] ty)) ctxt
193+addTyDecl n ty ctxt = addDef n (TyDecl Ref ty (eval ctxt [] ty)) ctxt
181addDatatype :: Datatype Name -> Context -> Context
195addDatatype :: Datatype Name -> Context -> Context
182addDatatype (Data n ty cons) ctxt
196addDatatype (Data n ty cons) ctxt
183 = let ty' = normalise ctxt [] ty in
197 = let ty' = normalise ctxt [] ty in
184 addCons 0 cons (addDef n
198 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)
187 addCons tag [] ctxt = ctxt
201 addCons tag [] ctxt = ctxt
188 addCons tag ((n, ty) : cons) ctxt
202 addCons tag ((n, ty) : cons) ctxt
189 = let ty' = normalise ctxt [] ty in
203 = let ty' = normalise ctxt [] ty in
190 addCons (tag+1) cons (addDef n
204 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)
193addCasedef :: Name -> CaseDef -> Type -> Context -> Context
207addCasedef :: Name -> CaseDef -> Type -> Context -> Context
194addCasedef n (CaseDef args sc) ty ctxt
208addCasedef n (CaseDef args sc) ty ctxt
195 = addDef n (CaseOp ty args sc) ctxt
209 = addDef n (CaseOp ty args sc) ctxt
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
197lookupTy :: Name -> Context -> Maybe Type
215lookupTy :: Name -> Context -> Maybe Type
198lookupTy n ctxt = do def <- lookupCtxt n ctxt
216lookupTy n ctxt = do def <- lookupCtxt n ctxt
199 case def of
217 case def of
200 (Function (Fun ty _ _ _)) -> return ty
218 (Function (Fun ty _ _ _)) -> return ty
201− (Constant _ ty _) -> return ty
219+ (TyDecl _ ty _) -> return ty
202− (Operator ty _) -> return ty
220+ (Operator ty _ _) -> return ty
203 (CaseOp ty _ _) -> return ty
221 (CaseOp ty _ _) -> return ty
205lookupP :: Name -> Context -> Maybe Term
223lookupP :: Name -> Context -> Maybe Term
@@ -207,9 +225,9 @@ lookupP n ctxt
207 = do def <- lookupCtxt n ctxt
225 = do def <- lookupCtxt n ctxt
208 case def of
226 case def of
209 (Function (Fun ty _ tm _)) -> return (P Ref n ty)
227 (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)
211 (CaseOp ty _ _) -> return (P Ref n ty)
229 (CaseOp ty _ _) -> return (P Ref n ty)
212− (Operator ty _) -> return (P Ref n ty)
230+ (Operator ty _ _) -> return (P Ref n ty)
214lookupDef :: Name -> Context -> Maybe Def
232lookupDef :: Name -> Context -> Maybe Def
215lookupDef n ctxt = lookupCtxt n ctxt
233lookupDef n ctxt = lookupCtxt n ctxt
@@ -219,7 +237,7 @@ lookupVal n ctxt
219 = do def <- lookupCtxt n ctxt
237 = do def <- lookupCtxt n ctxt
220 case def of
238 case def of
221 (Function (Fun _ _ _ htm)) -> return htm
239 (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)
224lookupTyEnv :: Name -> Env -> Maybe (Int, Type)
242lookupTyEnv :: Name -> Env -> Maybe (Int, Type)
225lookupTyEnv n env = li n 0 env where
243lookupTyEnv n env = li n 0 env where