@@ -3,11 +3,14 @@
4module Core.Evaluate(normalise,
4module Core.Evaluate(normalise,
5 Fun(..), Def(..), Context,
5 Fun(..), Def(..), Context,
6− addToCtxt, addConstant, addDatatype,
6+ addToCtxt, addConstant, addDatatype, addCasedef,
7− lookupTy, lookupP, lookupDef, lookupVal, lookupTyEnv) where
7+ lookupTy, lookupP, lookupDef, lookupVal, lookupTyEnv,
9import Debug.Trace
10import Debug.Trace
10import Core.TT
12import Core.TT
12-- VALUES (as HOAS) ---------------------------------------------------------
15-- VALUES (as HOAS) ---------------------------------------------------------
@@ -49,8 +52,10 @@ eval :: Context -> Env -> TT Name -> Value
49eval ctxt genv tm = ev [] tm where
52eval ctxt genv tm = ev [] tm where
50 ev env (P Bound n ty)
53 ev env (P Bound n ty)
51 | Just (Let t v) <- lookup n genv = ev env v
54 | Just (Let t v) <- lookup n genv = ev env v
52− ev env (P Ref n ty)
55+ ev env (P Ref n ty) = case lookupDef n ctxt of
53− | Just v <- lookupVal n ctxt = v
56+ Just (Function (Fun _ _ _ v)) -> v
57+ Just (Constant nt ty hty) -> VP nt n hty
58+ _ -> VP Ref n (ev env ty)
54 ev env (P nt n ty) = VP nt n (ev env ty)
59 ev env (P nt n ty) = VP nt n (ev env ty)
55 ev env (V i) | i < length env = env !! i
60 ev env (V i) | i < length env = env !! i
56 | otherwise = error $ "Internal error: V" ++ show i
61 | otherwise = error $ "Internal error: V" ++ show i
@@ -67,12 +72,57 @@ eval ctxt genv tm = ev [] tm where
67 evApply env args f = apply env (ev env f) args
72 evApply env args f = apply env (ev env f) args
69 apply env (VBind n (Lam t) sc) (a:as) = wknV (-1) $ apply env (sc (ev env a)) as
74 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
70 apply env f (a:as) = unload env f (a:as)
80 apply env f (a:as) = unload env f (a:as)
71 apply env f [] = f
81 apply env f [] = f
73 unload env f [] = f
83 unload env f [] = f
74 unload env f (a:as) = unload env (VApp f (ev env a)) as
84 unload env f (a:as) = unload env (VApp f (ev env a)) as
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
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
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
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
118+ findDefault [] = Nothing
119+ findDefault (DefaultCase sc : xs) = Just sc
120+ findDefault (_ : xs) = findDefault xs
122+ getValArgs tm = getValArgs' tm []
123+ getValArgs' (VApp f a) as = getValArgs' f (a:as)
124+ getValArgs' f as = (f, as)
76quote :: Int -> Value -> TT Name
126quote :: Int -> Value -> TT Name
77quote i (VP nt n v) = P nt n (quote i v)
127quote i (VP nt n v) = P nt n (quote i v)
78quote i (VV x) = V x
128quote i (VV x) = V x
@@ -97,17 +147,19 @@ data Fun = Fun Type Value Term Value
97{- A definition is either a simple function (just an expression with a type),
147{- A definition is either a simple function (just an expression with a type),
98 a constant, which could be a data or type constructor, an axiom or as an
148 a constant, which could be a data or type constructor, an axiom or as an
99 yet undefined function, or an Operator.
149 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 -}
103data Def = Function Fun
153data Def = Function Fun
104 | Constant NameType Type Value
154 | Constant NameType Type Value
105− | Operator Type [(Term, Term)] ([Value] -> Maybe Value)
155+ | Operator Type ([Value] -> Maybe Value)
156+ | CaseOp Type [Name] SC
107instance Show Def where
158instance Show Def where
108 show (Function f) = "Function: " ++ show f
159 show (Function f) = "Function: " ++ show f
109 show (Constant nt ty val) = "Constant: " ++ show nt ++ " " ++ show ty
160 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
@@ -132,11 +184,17 @@ addDatatype (Data n ty cons) ctxt
132 addCons (tag+1) cons (addDef n
184 addCons (tag+1) cons (addDef n
133 (Constant (DCon tag (arity ty')) ty (eval ctxt [] ty)) ctxt)
185 (Constant (DCon tag (arity ty')) ty (eval ctxt [] ty)) ctxt)
187+addCasedef :: Name -> CaseDef -> Type -> Context -> Context
188+addCasedef n (CaseDef args sc) ty ctxt
189+ = addDef n (CaseOp ty args sc) ctxt
135lookupTy :: Name -> Context -> Maybe Type
191lookupTy :: Name -> Context -> Maybe Type
136lookupTy n ctxt = do def <- lookupCtxt n ctxt
192lookupTy n ctxt = do def <- lookupCtxt n ctxt
137 case def of
193 case def of
138 (Function (Fun ty _ _ _)) -> return ty
194 (Function (Fun ty _ _ _)) -> return ty
139 (Constant _ ty _) -> return ty
195 (Constant _ ty _) -> return ty
196+ (Operator ty _) -> return ty
197+ (CaseOp ty _ _) -> return ty
141lookupP :: Name -> Context -> Maybe Term
199lookupP :: Name -> Context -> Maybe Term
142lookupP n ctxt
200lookupP n ctxt
@@ -144,13 +202,11 @@ lookupP n ctxt
144 case def of
202 case def of
145 (Function (Fun ty _ tm _)) -> return (P Ref n ty)
203 (Function (Fun ty _ tm _)) -> return (P Ref n ty)
146 (Constant nt ty hty) -> return (P nt n ty)
204 (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)
148−lookupDef :: Name -> Context -> Maybe Term
208+lookupDef :: Name -> Context -> Maybe Def
149−lookupDef n ctxt
209+lookupDef n ctxt = lookupCtxt n ctxt
150− = do def <- lookupCtxt n ctxt
152− (Function (Fun ty _ tm _)) -> return tm
153− (Constant nt ty hty) -> return (P nt n ty)
155lookupVal :: Name -> Context -> Maybe Value
211lookupVal :: Name -> Context -> Maybe Value
156lookupVal n ctxt
212lookupVal n ctxt