@@ -8,22 +8,27 @@ module Core.Evaluate(normalise,
88 Value(..)) where
99
1010import Debug.Trace
11+import Control.Monad.State
1112
1213import Core.TT
1314import Core.CaseTree
1415
1516-- VALUES (as HOAS) ---------------------------------------------------------
1617
18+type EvalState = ()
19+type Eval a = State EvalState a
20+
1721data Value = VP NameType Name Value
1822 | VV Int
19− | VBind Name (Binder Value) (Value -> Value)
23+ | VBind Name (Binder Value) (Value -> Eval Value)
2024 | VApp Value Value
2125 | VSet Int
2226 | VConstant Const
2327 | VTmp Int
2428
2529instance Show Value where
26− show = show . quote 0
30+ show v = "<<Value>>"
31+
2732
2833-- THE EVALUATOR ------------------------------------------------------------
2934
@@ -33,7 +38,8 @@ instance Show Value where
3338-- while building a proof.
3439
3540normalise :: 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)
42+ quote 0 val) ()
3743
3844-- unbindEnv env (quote 0 (eval ctxt (bindEnv env t)))
3945
@@ -52,70 +58,85 @@ unbindEnv (_:bs) (Bind n b sc) = unbindEnv bs sc
5258-- Evaluate in a context of locally named things (i.e. not de Bruijn indexed,
5359-- such as we might have during construction of a proof)
5460
55−eval :: Context -> Env -> TT Name -> Value
61+eval :: Context -> Env -> TT Name -> Eval Value
5662eval ctxt genv tm = ev [] tm where
5763 ev env (P _ n ty)
5864 | Just (Let t v) <- lookup n genv = ev env v
5965 ev env (P Ref n ty) = case lookupDef n ctxt of
60− Just (Function (Fun _ _ _ v)) -> v
61− Just (TyDecl nt ty hty) -> VP nt n hty
66+ Just (Function (Fun _ _ _ v)) -> return v
67+ Just (TyDecl nt ty hty) -> return $ VP nt n hty
6268 Just (CaseOp _ _ [] tree) ->
63− case evCase env [] [] tree of
64− (Nothing, _) -> VP Ref n (ev env ty)
65− (Just v, _) -> v
66− _ -> VP Ref n (ev env ty)
67− ev env (P nt n ty) = VP nt n (ev env ty)
68− ev env (V i) | i < length env = env !! i
69− | otherwise = VV i --error $ "Internal error: V" ++ show i ++ " "
70− -- ++ showEnv genv tm ++ "\n" ++ show genv
69+ do c <- evCase env [] [] tree
70+ case c of
71+ (Nothing, _) -> liftM (VP Ref n) (ev env ty)
72+ (Just v, _) -> return v
73+ _ -> liftM (VP Ref n) (ev env ty)
74+ ev env (P nt n ty) = liftM (VP nt n) (ev env ty)
75+ ev env (V i) | i < length env = return $ env !! i
76+ | otherwise = return $ VV i
7177 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
80+ wknV (-1) sc'
7381 ev env (Bind n (NLet t v) sc)
74− = VBind n (Let (ev env (finalise t)) (ev env (finalise v)))
75− $ (\x -> ev (ev env v : env) sc)
76− ev env (Bind n b sc) = VBind n (vbind env b) (\x -> ev (x:env) sc)
77− where vbind env t = fmap (\tm -> ev env (finalise tm)) t
78− ev env (App f a) = evApply env [ev env a] (ev env f)
79− ev env (Constant c) = VConstant c
80− ev env (Set i) = VSet i
82+ = do t' <- ev env (finalise t)
83+ v' <- ev env (finalise v)
84+ sc' <- ev (v' : env) sc
85+ return $ VBind n (Let t' v') (\x -> return sc')
86+ ev env (Bind n b sc)
87+ = do b' <- vbind env b
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
91+ a' <- ev env a
92+ evApply env [a'] f'
93+ ev env (Constant c) = return $ VConstant c
94+ ev env (Set i) = return $ VSet i
8195
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
8398 evApply env args f = apply env f args
8499
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
102+ wknV (-1) app
86103 apply env (VP Ref n ty) args
87104 | Just (CaseOp _ _ ns tree) <- lookupDef n ctxt
88− = case evCase env ns args tree of
89− (Nothing, _) -> unload env (VP Ref n ty) args
105+ = do c <- evCase env ns args tree
106+ case c of
107+ (Nothing, _) -> return $ unload env (VP Ref n ty) args
90108 (Just v, rest) -> evApply env rest v
91109 | Just (Operator _ i op) <- lookupDef n ctxt
92110 = if (i <= length args)
93− then case op (take i args) of
94− Nothing -> unload env (VP Ref n ty) args
95− Just v -> evApply env (drop i args) v
96− else unload env (VP Ref n ty) args
97− apply env f (a:as) = unload env f (a:as)
98− apply env f [] = f
111+ then case op (take i args) of
112+ Nothing -> return $ unload env (VP Ref n ty) args
113+ Just v -> evApply env (drop i args) v
114+ else return $ unload env (VP Ref n ty) args
115+ apply env f (a:as) = return $ unload env f (a:as)
116+ apply env f [] = return f
99117
100118 unload env f [] = f
101119 unload env f (a:as) = unload env (VApp f a) as
102120
103121 evCase env ns args tree
104122 | length ns <= length args
105− = let args' = take (length ns) args
106− rest = drop (length ns) args in
107− (evTree env (zipWith (\n t -> (n, t)) ns args') tree, rest)
108− | otherwise = (Nothing, args)
109−
110− evTree :: [Value] -> [(Name, Value)] -> SC -> Maybe Value
111− evTree env amap (UnmatchedCase str) = Nothing
123+ = do let args' = take (length ns) args
124+ let rest = drop (length ns) args
125+ t <- evTree env (zipWith (\n t -> (n, t)) ns args') tree
126+ return (t, rest)
127+ | otherwise = return (Nothing, args)
128+
129+ evTree :: [Value] -> [(Name, Value)] -> SC -> Eval (Maybe Value)
130+ evTree env amap (UnmatchedCase str) = return Nothing
112131 evTree env amap (STerm tm)
113− = let etm = pToVs (map fst amap) tm in
114− Just $ ev (map snd amap ++ env) etm
132+ = do let etm = pToVs (map fst amap) tm
133+ etm' <- ev (map snd amap ++ env) etm
134+ return $ Just etm'
115135 evTree env amap (Case n alts)
116− = do v <- lookup n amap
117− (altmap, sc) <- chooseAlt v (getValArgs v) alts amap
118− evTree env altmap sc
136+ = case (do v <- lookup n amap
137+ chooseAlt v (getValArgs v) alts amap) of
138+ Just (altmap, sc) -> evTree env altmap sc
139+ _ -> return Nothing
119140
120141 chooseAlt :: Value -> (Value, [Value]) -> [CaseAlt] -> [(Name, Value)] ->
121142 Maybe ([(Name, Value)], SC)
@@ -156,22 +177,26 @@ eval ctxt genv tm = ev [] tm where
156177 getValArgs' (VApp f a) as = getValArgs' f (a:as)
157178 getValArgs' f as = (f, as)
158179
159−quote :: Int -> Value -> TT Name
160−quote i (VP nt n v) = P nt n (quote i v)
161−quote i (VV x) = V x
162−quote i (VBind n b sc) = Bind n (quoteB b) (quote (i+1) (sc (VTmp i)))
163− where quoteB t = fmap (quote i) t
164−quote i (VApp f a) = App (quote i f) (quote i a)
165−quote i (VSet u) = Set u
166−quote i (VConstant c) = Constant c
167−quote i (VTmp x) = V (i - x - 1)
168−
169−
170−wknV :: Int -> Value -> Value
171−wknV i (VV x) = VV (x + i)
172−wknV i (VBind n b sc) = VBind n (fmap (wknV i) b) (\x -> (wknV i (sc x)))
173−wknV i (VApp f a) = VApp (wknV i f) (wknV i a)
174−wknV i t = t
180+quote :: Int -> Value -> Eval (TT Name)
181+quote i (VP nt n v) = liftM (P nt n) (quote i v)
182+quote i (VV x) = return $ V x
183+quote i (VBind n b sc) = do sc' <- sc (VTmp i)
184+ b' <- quoteB b
185+ liftM (Bind n b') (quote (i+1) sc')
186+ where quoteB t = fmapMB (quote i) t
187+quote i (VApp f a) = liftM2 App (quote i f) (quote i a)
188+quote i (VSet u) = return $ Set u
189+quote i (VConstant c) = return $ Constant c
190+quote i (VTmp x) = return $ V (i - x - 1)
191+
192+
193+wknV :: Int -> Value -> Eval Value
194+wknV i (VV x) = return $ VV (x + i)
195+wknV i (VBind n b sc) = do b' <- fmapMB (wknV i) b
196+ return $ VBind n b' (\x -> do x' <- sc x
197+ wknV i x')
198+wknV i (VApp f a) = liftM2 VApp (wknV i f) (wknV i a)
199+wknV i t = return t
175200
176201-- CONTEXTS -----------------------------------------------------------------
177202
@@ -199,24 +224,26 @@ instance Show Def where
199224
200225type Context = Ctxt Def
201226
227+veval ctxt env t = evalState (eval ctxt env t) ()
228+
202229addToCtxt :: Name -> Term -> Type -> Context -> Context
203−addToCtxt n tm ty ctxt = addDef n (Function (Fun ty (eval ctxt [] ty)
204− tm (eval ctxt [] tm))) ctxt
230+addToCtxt n tm ty ctxt = addDef n (Function (Fun ty (veval ctxt [] ty)
231+ tm (veval ctxt [] tm))) ctxt
205232
206233addTyDecl :: 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
208235
209236addDatatype :: Datatype Name -> Context -> Context
210237addDatatype (Data n tag ty cons) ctxt
211238 = let ty' = normalise ctxt [] ty in
212239 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)
214241 where
215242 addCons tag [] ctxt = ctxt
216243 addCons tag ((n, ty) : cons) ctxt
217244 = let ty' = normalise ctxt [] ty in
218245 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)
220247
221248addCasedef :: Name -> Bool -> [(Term, Term)] -> Type -> Context -> Context
222249addCasedef n tcase ps ty ctxt