@@ -3,11 +3,14 @@
33
44module Core.Evaluate(normalise,
55 Fun(..), Def(..), Context,
6− addToCtxt, addConstant, addDatatype,
7− lookupTy, lookupP, lookupDef, lookupVal, lookupTyEnv) where
6+ addToCtxt, addConstant, addDatatype, addCasedef,
7+ lookupTy, lookupP, lookupDef, lookupVal, lookupTyEnv,
8+ Value) where
89
910import Debug.Trace
11+
1012import Core.TT
13+import Core.CaseTree
1114
1215-- VALUES (as HOAS) ---------------------------------------------------------
1316
@@ -49,8 +52,10 @@ eval :: Context -> Env -> TT Name -> Value
4952eval ctxt genv tm = ev [] tm where
5053 ev env (P Bound n ty)
5154 | Just (Let t v) <- lookup n genv = ev env v
52− ev env (P Ref n ty)
53− | Just v <- lookupVal n ctxt = v
55+ ev env (P Ref n ty) = case lookupDef n ctxt of
56+ Just (Function (Fun _ _ _ v)) -> v
57+ Just (Constant nt ty hty) -> VP nt n hty
58+ _ -> VP Ref n (ev env ty)
5459 ev env (P nt n ty) = VP nt n (ev env ty)
5560 ev env (V i) | i < length env = env !! i
5661 | otherwise = error $ "Internal error: V" ++ show i
@@ -67,12 +72,57 @@ eval ctxt genv tm = ev [] tm where
6772 evApply env args f = apply env (ev env f) args
6873
6974 apply env (VBind n (Lam t) sc) (a:as) = wknV (-1) $ apply env (sc (ev env a)) as
75+ apply env (VP Ref n ty) args
76+ | Just (CaseOp _ ns tree) <- lookupDef n ctxt
77+ = case evCase env ns args tree of
78+ Nothing -> unload env (VP Ref n ty) args
79+ Just v -> v
7080 apply env f (a:as) = unload env f (a:as)
7181 apply env f [] = f
7282
7383 unload env f [] = f
7484 unload env f (a:as) = unload env (VApp f (ev env a)) as
7585
86+ evCase env ns args tree
87+ | length ns == length args
88+ = evTree env (zipWith (\n t -> (n, ev env t)) ns args) tree
89+ | otherwise = Nothing
90+
91+ evTree :: [Value] -> [(Name, Value)] -> SC -> Maybe Value
92+ evTree env amap (UnmatchedCase str) = Nothing
93+ evTree env amap (STerm tm)
94+ = Just $ ev (map snd amap ++ env) (pToVs (map fst amap) tm)
95+ evTree env amap (Case n alts)
96+ = do v <- lookup n amap
97+ (altmap, sc) <- chooseAlt v (getValArgs v) alts amap
98+ evTree env altmap sc
99+
100+ chooseAlt :: Value -> (Value, [Value]) -> [CaseAlt] -> [(Name, Value)] ->
101+ Maybe ([(Name, Value)], SC)
102+ chooseAlt _ (VP (DCon i a) _ _, args) alts amap
103+ | Just (ns, sc) <- findTag i alts = Just (updateAmap (zip ns args) amap, sc)
104+ | Just sc <- findDefault alts = Just (amap, sc)
105+-- chooseAlt v _ alts amap
106+-- | Just sc <- safeDefault alts = Just (amap, sc)
107+ chooseAlt _ _ _ _ = Nothing
108+
109+ -- Replace old variable names in the map with new matches
110+ -- (This is possibly unnecessary since we make unique names and don't
111+ -- allow repeated variables...?)
112+ updateAmap newm amap
113+ = newm ++ amap --filter (\ (x, _) -> not (elem x (map fst newm))) amap
114+ findTag i [] = Nothing
115+ findTag i (ConCase n j ns sc : xs) | i == j = Just (ns, sc)
116+ findTag i (_ : xs) = findTag i xs
117+
118+ findDefault [] = Nothing
119+ findDefault (DefaultCase sc : xs) = Just sc
120+ findDefault (_ : xs) = findDefault xs
121+
122+ getValArgs tm = getValArgs' tm []
123+ getValArgs' (VApp f a) as = getValArgs' f (a:as)
124+ getValArgs' f as = (f, as)
125+
76126quote :: Int -> Value -> TT Name
77127quote i (VP nt n v) = P nt n (quote i v)
78128quote i (VV x) = V x
@@ -97,17 +147,19 @@ data Fun = Fun Type Value Term Value
97147{- A definition is either a simple function (just an expression with a type),
98148 a constant, which could be a data or type constructor, an axiom or as an
99149 yet undefined function, or an Operator.
100− An Operator is a collection of pattern matching definitions, and a function
101− which explains how to reduce. -}
150+ An Operator is a function which explains how to reduce.
151+ A CaseOp is a function defined by a simple case tree -}
102152
103153data Def = Function Fun
104154 | Constant NameType Type Value
105− | Operator Type [(Term, Term)] ([Value] -> Maybe Value)
155+ | Operator Type ([Value] -> Maybe Value)
156+ | CaseOp Type [Name] SC
106157
107158instance Show Def where
108159 show (Function f) = "Function: " ++ show f
109160 show (Constant nt ty val) = "Constant: " ++ show nt ++ " " ++ show ty
110− show (Operator ty ps _) = "Operator: " ++ show ty ++ " " ++ show ps
161+ show (Operator ty _) = "Operator: " ++ show ty
162+ show (CaseOp ty ns sc) = "Case: " ++ show ns ++ " " ++ show sc
111163
112164-------
113165
@@ -132,11 +184,17 @@ addDatatype (Data n ty cons) ctxt
132184 addCons (tag+1) cons (addDef n
133185 (Constant (DCon tag (arity ty')) ty (eval ctxt [] ty)) ctxt)
134186
187+addCasedef :: Name -> CaseDef -> Type -> Context -> Context
188+addCasedef n (CaseDef args sc) ty ctxt
189+ = addDef n (CaseOp ty args sc) ctxt
190+
135191lookupTy :: Name -> Context -> Maybe Type
136192lookupTy n ctxt = do def <- lookupCtxt n ctxt
137193 case def of
138194 (Function (Fun ty _ _ _)) -> return ty
139195 (Constant _ ty _) -> return ty
196+ (Operator ty _) -> return ty
197+ (CaseOp ty _ _) -> return ty
140198
141199lookupP :: Name -> Context -> Maybe Term
142200lookupP n ctxt
@@ -144,13 +202,11 @@ lookupP n ctxt
144202 case def of
145203 (Function (Fun ty _ tm _)) -> return (P Ref n ty)
146204 (Constant nt ty hty) -> return (P nt n ty)
205+ (CaseOp ty _ _) -> return (P Ref n ty)
206+ (Operator ty _) -> return (P Ref n ty)
147207
148−lookupDef :: Name -> Context -> Maybe Term
149−lookupDef n ctxt
150− = do def <- lookupCtxt n ctxt
151− case def of
152− (Function (Fun ty _ tm _)) -> return tm
153− (Constant nt ty hty) -> return (P nt n ty)
208+lookupDef :: Name -> Context -> Maybe Def
209+lookupDef n ctxt = lookupCtxt n ctxt
154210
155211lookupVal :: Name -> Context -> Maybe Value
156212lookupVal n ctxt