@@ -8,22 +8,27 @@ module Core.Evaluate(normalise,
8 Value(..)) where
8 Value(..)) where
10import Debug.Trace
10import Debug.Trace
11+import Control.Monad.State
12import Core.TT
13import Core.TT
13import Core.CaseTree
14import Core.CaseTree
15-- VALUES (as HOAS) ---------------------------------------------------------
16-- VALUES (as HOAS) ---------------------------------------------------------
19+type Eval a = State EvalState a
17data Value = VP NameType Name Value
21data Value = VP NameType Name Value
19− | VBind Name (Binder Value) (Value -> Value)
23+ | VBind Name (Binder Value) (Value -> Eval Value)
20 | VApp Value Value
24 | VApp Value Value
21 | VSet Int
25 | VSet Int
22 | VConstant Const
26 | VConstant Const
23 | VTmp Int
27 | VTmp Int
25instance Show Value where
29instance Show Value where
26− show = show . quote 0
30+ show v = "<<Value>>"
28-- THE EVALUATOR ------------------------------------------------------------
33-- THE EVALUATOR ------------------------------------------------------------
@@ -33,7 +38,8 @@ instance Show Value where
33-- while building a proof.
38-- while building a proof.
35normalise :: Context -> Env -> TT Name -> TT Name
40normalise :: Context -> Env -> TT Name -> TT Name
36−normalise ctxt env t = quote 0 (eval ctxt (map finalEntry env) (finalise t))
41+normalise ctxt env t = evalState (do val <- eval ctxt (map finalEntry env) (finalise t)
38-- unbindEnv env (quote 0 (eval ctxt (bindEnv env t)))
44-- unbindEnv env (quote 0 (eval ctxt (bindEnv env t)))
@@ -52,70 +58,85 @@ unbindEnv (_:bs) (Bind n b sc) = unbindEnv bs sc
52-- Evaluate in a context of locally named things (i.e. not de Bruijn indexed,
58-- Evaluate in a context of locally named things (i.e. not de Bruijn indexed,
53-- such as we might have during construction of a proof)
59-- such as we might have during construction of a proof)
55−eval :: Context -> Env -> TT Name -> Value
61+eval :: Context -> Env -> TT Name -> Eval Value
56eval ctxt genv tm = ev [] tm where
62eval ctxt genv tm = ev [] tm where
57 ev env (P _ n ty)
63 ev env (P _ n ty)
58 | Just (Let t v) <- lookup n genv = ev env v
64 | Just (Let t v) <- lookup n genv = ev env v
59 ev env (P Ref n ty) = case lookupDef n ctxt of
65 ev env (P Ref n ty) = case lookupDef n ctxt of
60− Just (Function (Fun _ _ _ v)) -> v
66+ Just (Function (Fun _ _ _ v)) -> return v
61− Just (TyDecl nt ty hty) -> VP nt n hty
67+ Just (TyDecl nt ty hty) -> return $ VP nt n hty
62 Just (CaseOp _ _ [] tree) ->
68 Just (CaseOp _ _ [] tree) ->
63− case evCase env [] [] tree of
69+ do c <- evCase env [] [] tree
64− (Nothing, _) -> VP Ref n (ev env ty)
70+ case c of
65− (Just v, _) -> v
71+ (Nothing, _) -> liftM (VP Ref n) (ev env ty)
66− _ -> VP Ref n (ev env ty)
72+ (Just v, _) -> return v
67− ev env (P nt n ty) = VP nt n (ev env ty)
73+ _ -> liftM (VP Ref n) (ev env ty)
68− ev env (V i) | i < length env = env !! i
74+ ev env (P nt n ty) = liftM (VP nt n) (ev env ty)
69− | otherwise = VV i --error $ "Internal error: V" ++ show i ++ " "
75+ ev env (V i) | i < length env = return $ env !! i
70− -- ++ showEnv genv tm ++ "\n" ++ show genv
76+ | otherwise = return $ VV i
71 ev env (Bind n (Let t v) sc)
77 ev env (Bind n (Let t v) sc)
72− = wknV (-1) $ ev (ev env (finalise v) : env) sc
78+ = do v' <- ev env (finalise v)
79+ sc' <- ev (v' : env) sc
73 ev env (Bind n (NLet t v) sc)
81 ev env (Bind n (NLet t v) sc)
74− = VBind n (Let (ev env (finalise t)) (ev env (finalise v)))
82+ = do t' <- ev env (finalise t)
75− $ (\x -> ev (ev env v : env) sc)
83+ v' <- ev env (finalise v)
76− ev env (Bind n b sc) = VBind n (vbind env b) (\x -> ev (x:env) sc)
84+ sc' <- ev (v' : env) sc
77− where vbind env t = fmap (\tm -> ev env (finalise tm)) t
85+ return $ VBind n (Let t' v') (\x -> return sc')
78− ev env (App f a) = evApply env [ev env a] (ev env f)
86+ ev env (Bind n b sc)
79− ev env (Constant c) = VConstant c
87+ = do b' <- vbind env b
80− ev env (Set i) = VSet i
88+ return $ VBind n b' (\x -> ev (x:env) sc)
89+ where vbind env t = fmapMB (\tm -> ev env (finalise tm)) t
90+ ev env (App f a) = do f' <- ev env f
93+ ev env (Constant c) = return $ VConstant c
94+ ev env (Set i) = return $ VSet i
82− evApply env args (VApp f a) = evApply env (a:args) f
96+ evApply env args (VApp f a) =
97+ evApply env (a:args) f
83 evApply env args f = apply env f args
98 evApply env args f = apply env f args
85− apply env (VBind n (Lam t) sc) (a:as) = wknV (-1) $ apply env (sc a) as
100+ apply env (VBind n (Lam t) sc) (a:as) = do a' <- sc a
101+ app <- apply env a' as
86 apply env (VP Ref n ty) args
103 apply env (VP Ref n ty) args
87 | Just (CaseOp _ _ ns tree) <- lookupDef n ctxt
104 | Just (CaseOp _ _ ns tree) <- lookupDef n ctxt
88− = case evCase env ns args tree of
105+ = do c <- evCase env ns args tree
89− (Nothing, _) -> unload env (VP Ref n ty) args
106+ case c of
107+ (Nothing, _) -> return $ unload env (VP Ref n ty) args
90 (Just v, rest) -> evApply env rest v
108 (Just v, rest) -> evApply env rest v
91 | Just (Operator _ i op) <- lookupDef n ctxt
109 | Just (Operator _ i op) <- lookupDef n ctxt
92 = if (i <= length args)
110 = if (i <= length args)
93− then case op (take i args) of
111+ then case op (take i args) of
94− Nothing -> unload env (VP Ref n ty) args
112+ Nothing -> return $ unload env (VP Ref n ty) args
95− Just v -> evApply env (drop i args) v
113+ Just v -> evApply env (drop i args) v
96− else unload env (VP Ref n ty) args
114+ else return $ unload env (VP Ref n ty) args
97− apply env f (a:as) = unload env f (a:as)
115+ apply env f (a:as) = return $ unload env f (a:as)
98− apply env f [] = f
116+ apply env f [] = return f
100 unload env f [] = f
118 unload env f [] = f
101 unload env f (a:as) = unload env (VApp f a) as
119 unload env f (a:as) = unload env (VApp f a) as
103 evCase env ns args tree
121 evCase env ns args tree
104 | length ns <= length args
122 | length ns <= length args
105− = let args' = take (length ns) args
123+ = do let args' = take (length ns) args
106− rest = drop (length ns) args in
124+ let rest = drop (length ns) args
107− (evTree env (zipWith (\n t -> (n, t)) ns args') tree, rest)
125+ t <- evTree env (zipWith (\n t -> (n, t)) ns args') tree
108− | otherwise = (Nothing, args)
126+ return (t, rest)
109−
127+ | otherwise = return (Nothing, args)
110− evTree :: [Value] -> [(Name, Value)] -> SC -> Maybe Value
128+
111− evTree env amap (UnmatchedCase str) = Nothing
129+ evTree :: [Value] -> [(Name, Value)] -> SC -> Eval (Maybe Value)
130+ evTree env amap (UnmatchedCase str) = return Nothing
112 evTree env amap (STerm tm)
131 evTree env amap (STerm tm)
113− = let etm = pToVs (map fst amap) tm in
132+ = do let etm = pToVs (map fst amap) tm
114− Just $ ev (map snd amap ++ env) etm
133+ etm' <- ev (map snd amap ++ env) etm
115 evTree env amap (Case n alts)
135 evTree env amap (Case n alts)
116− = do v <- lookup n amap
136+ = case (do v <- lookup n amap
117− (altmap, sc) <- chooseAlt v (getValArgs v) alts amap
137+ chooseAlt v (getValArgs v) alts amap) of
118− evTree env altmap sc
138+ Just (altmap, sc) -> evTree env altmap sc
120 chooseAlt :: Value -> (Value, [Value]) -> [CaseAlt] -> [(Name, Value)] ->
141 chooseAlt :: Value -> (Value, [Value]) -> [CaseAlt] -> [(Name, Value)] ->
121 Maybe ([(Name, Value)], SC)
142 Maybe ([(Name, Value)], SC)
@@ -156,22 +177,26 @@ eval ctxt genv tm = ev [] tm where
156 getValArgs' (VApp f a) as = getValArgs' f (a:as)
177 getValArgs' (VApp f a) as = getValArgs' f (a:as)
157 getValArgs' f as = (f, as)
178 getValArgs' f as = (f, as)
159−quote :: Int -> Value -> TT Name
180+quote :: Int -> Value -> Eval (TT Name)
160−quote i (VP nt n v) = P nt n (quote i v)
181+quote i (VP nt n v) = liftM (P nt n) (quote i v)
161−quote i (VV x) = V x
182+quote i (VV x) = return $ V x
162−quote i (VBind n b sc) = Bind n (quoteB b) (quote (i+1) (sc (VTmp i)))
183+quote i (VBind n b sc) = do sc' <- sc (VTmp i)
163− where quoteB t = fmap (quote i) t
184+ b' <- quoteB b
164−quote i (VApp f a) = App (quote i f) (quote i a)
185+ liftM (Bind n b') (quote (i+1) sc')
165−quote i (VSet u) = Set u
186+ where quoteB t = fmapMB (quote i) t
166−quote i (VConstant c) = Constant c
187+quote i (VApp f a) = liftM2 App (quote i f) (quote i a)
167−quote i (VTmp x) = V (i - x - 1)
188+quote i (VSet u) = return $ Set u
168−
189+quote i (VConstant c) = return $ Constant c
169−
190+quote i (VTmp x) = return $ V (i - x - 1)
170−wknV :: Int -> Value -> Value
191+
171−wknV i (VV x) = VV (x + i)
192+
172−wknV i (VBind n b sc) = VBind n (fmap (wknV i) b) (\x -> (wknV i (sc x)))
193+wknV :: Int -> Value -> Eval Value
173−wknV i (VApp f a) = VApp (wknV i f) (wknV i a)
194+wknV i (VV x) = return $ VV (x + i)
174−wknV i t = t
195+wknV i (VBind n b sc) = do b' <- fmapMB (wknV i) b
196+ return $ VBind n b' (\x -> do x' <- sc x
198+wknV i (VApp f a) = liftM2 VApp (wknV i f) (wknV i a)
176-- CONTEXTS -----------------------------------------------------------------
201-- CONTEXTS -----------------------------------------------------------------
@@ -199,24 +224,26 @@ instance Show Def where
200type Context = Ctxt Def
225type Context = Ctxt Def
227+veval ctxt env t = evalState (eval ctxt env t) ()
202addToCtxt :: Name -> Term -> Type -> Context -> Context
229addToCtxt :: Name -> Term -> Type -> Context -> Context
203−addToCtxt n tm ty ctxt = addDef n (Function (Fun ty (eval ctxt [] ty)
230+addToCtxt n tm ty ctxt = addDef n (Function (Fun ty (veval ctxt [] ty)
204− tm (eval ctxt [] tm))) ctxt
231+ tm (veval ctxt [] tm))) ctxt
206addTyDecl :: Name -> Type -> Context -> Context
233addTyDecl :: Name -> Type -> Context -> Context
207−addTyDecl n ty ctxt = addDef n (TyDecl Ref ty (eval ctxt [] ty)) ctxt
234+addTyDecl n ty ctxt = addDef n (TyDecl Ref ty (veval ctxt [] ty)) ctxt
209addDatatype :: Datatype Name -> Context -> Context
236addDatatype :: Datatype Name -> Context -> Context
210addDatatype (Data n tag ty cons) ctxt
237addDatatype (Data n tag ty cons) ctxt
211 = let ty' = normalise ctxt [] ty in
238 = let ty' = normalise ctxt [] ty in
212 addCons 0 cons (addDef n
239 addCons 0 cons (addDef n
213− (TyDecl (TCon tag (arity ty')) ty (eval ctxt [] ty)) ctxt)
240+ (TyDecl (TCon tag (arity ty')) ty (veval ctxt [] ty)) ctxt)
215 addCons tag [] ctxt = ctxt
242 addCons tag [] ctxt = ctxt
216 addCons tag ((n, ty) : cons) ctxt
243 addCons tag ((n, ty) : cons) ctxt
217 = let ty' = normalise ctxt [] ty in
244 = let ty' = normalise ctxt [] ty in
218 addCons (tag+1) cons (addDef n
245 addCons (tag+1) cons (addDef n
219− (TyDecl (DCon tag (arity ty')) ty (eval ctxt [] ty)) ctxt)
246+ (TyDecl (DCon tag (arity ty')) ty (veval ctxt [] ty)) ctxt)
221addCasedef :: Name -> Bool -> [(Term, Term)] -> Type -> Context -> Context
248addCasedef :: Name -> Bool -> [(Term, Term)] -> Type -> Context -> Context
222addCasedef n tcase ps ty ctxt
249addCasedef n tcase ps ty ctxt