on master help sign in
Fixing evaluator bugs
commit9c6ea4a893db376e4aad8e501e22977e829f1558 authorEdwin Brady <eb@cs.st-andrews.ac.uk> dateSat 24 Sep 2011, 22:08 (15 years ago) parent4c68b68
5 files changed+64−36
src/Core.hs+4−5 src/Evaluate.hs+11−5 src/ProofState.hs+32−12 src/ShellParser.hs+1 src/Typecheck.hs+16−14
src/Core.hs+4−5
@@ -102,7 +102,8 @@ showEnv env t = showEnv' env t False
102102showEnvDbg env t = showEnv' env t True
103103
104104showEnv' env t dbg = se 10 env t where
105− se p env (P _ n _) = show n
105+ se p env (P nt n t) = show n ++
106+ if dbg then "{" ++ show nt ++ " : " ++ se 10 env t ++ "}" else ""
106107 se p env (V i) | i < length env = (show $ fst $ env!!i) ++
107108 if dbg then "{" ++ show i ++ "}" else ""
108109 | otherwise = "!!V " ++ show i ++ "!!"
@@ -112,7 +113,7 @@ showEnv' env t dbg = se 10 env t where
112113
113114 sb env n (Lam t) = showb env "\\ " " => " n t
114115 sb env n (Hole t) = showb env "? " ". " n t
115− sb env n (Pi t) = showb env "forall " " -> " n t
116+ sb env n (Pi t) = showb env "(" ") -> " n t
116117 sb env n (PVar t) = showb env "! " ". " n t
117118 sb env n (Let t v) = showbv env "let " " in " n t v
118119 sb env n (Guess t v) = showbv env "?? " " in " n t v
@@ -143,9 +144,7 @@ weakenTm i t = wk i 0 t
143144 wk i m (App f a) = App (wk i m f) (wk i m a)
144145 wk i m (Bind x b sc) = Bind x (wkb i m b) (wk i (m + 1) sc)
145146 wk i m t = t
146− wkb i m (Let t v) = Let (wk i m t) (wk i m v)
147− wkb i m (Guess t v) = Guess (wk i m t) (wk i m v)
148− wkb i m t = t { binderTy = wk i m (binderTy t) }
147+ wkb i m t = fmap (wk i m) t
149148
150149-- weaken an environment so that all the de Bruijn indices are correct according
151150-- to the latest bound variable
src/Evaluate.hs+11−5
@@ -28,7 +28,7 @@ normalise ctxt env t = quote 0 (eval ctxt (weakenEnv env) t)
2828eval :: Context -> Env -> TT Name -> Value
2929eval ctxt genv tm = ev [] tm where
3030 ev env (P Ref n ty)
31− | Just v <- lookupVal n ctxt = v -- FIXME! Needs evalling
31+ | Just v <- lookupVal n ctxt = v
3232 ev env (P nt n ty) = VP nt n (ev env ty)
3333 ev env (V i) | i < length env = env !! i
3434 | i < length env + length genv
@@ -37,7 +37,7 @@ eval ctxt genv tm = ev [] tm where
3737 _ -> VV i
3838 | otherwise = error $ "Internal error: V" ++ show i
3939 ev env (Bind n (Let t v) sc)
40− = ev (ev env v : env) sc
40+ = wknV (-1) $ ev (ev env v : env) sc
4141 ev env (Bind n b sc) = VBind n (vbind env b) (\x -> ev (x:env) sc)
4242 where vbind env t = fmap (ev env) t
4343 ev env (App f a) = evApply env [a] f
@@ -46,7 +46,7 @@ eval ctxt genv tm = ev [] tm where
4646 evApply env args (App f a) = evApply env (a:args) f
4747 evApply env args f = apply env (ev env f) args
4848
49− apply env (VBind n (Lam t) sc) (a:as) = apply env (sc (ev env a)) as
49+ apply env (VBind n (Lam t) sc) (a:as) = wknV (-1) $ apply env (sc (ev env a)) as
5050 apply env f (a:as) = unload env f (a:as)
5151 apply env f [] = f
5252
@@ -56,13 +56,19 @@ eval ctxt genv tm = ev [] tm where
5656quote :: Int -> Value -> TT Name
5757quote i (VP nt n v) = P nt n (quote i v)
5858quote i (VV x) = V x
59−quote i (VBind n b sc) = Bind n (quoteB b) (quote (i+1) (sc (VTmp (i+1))))
59+quote i (VBind n b sc) = Bind n (quoteB b) (quote (i+1) (sc (VTmp i)))
6060 where quoteB t = fmap (quote i) t
6161quote i (VApp f a) = App (quote i f) (quote i a)
6262quote i (VSet u) = Set u
63−quote i (VTmp x) = V (i - x)
63+quote i (VTmp x) = V (i - x - 1)
6464
6565
66+wknV :: Int -> Value -> Value
67+wknV i (VV x) = VV (x + i)
68+wknV i (VBind n b sc) = VBind n (fmap (wknV i) b) (\x -> (wknV i (sc x)))
69+wknV i (VApp f a) = VApp (wknV i f) (wknV i a)
70+wknV i t = t
71+
6672-- CONTEXTS -----------------------------------------------------------------
6773
6874data Fun = Fun Type Value Term Value
src/ProofState.hs+32−12
@@ -9,6 +9,7 @@ import Core
99import Control.Monad.State
1010import Control.Applicative
1111import Data.List
12+import Debug.Trace
1213
1314data ProofState = PS { thname :: Name,
1415 holes :: [Name], -- holes still to be solved
@@ -36,11 +37,14 @@ data Tactic = Attack
3637 | EvalIn Raw
3738 | CheckIn Raw
3839 | Intro Name
40+ | Focus Name
3941 | ProofState
4042 | QED
4143-- Next: add 'EvalHere' and 'CheckHere' tactics
4244
43−data TacticAction = AddGoal Name
45+data TacticAction = AddGoal Name -- add a new goal, solve immediately
46+ | NextGoal Name -- add a new goal, solve it after current one
47+ | FocusGoal Name -- focus on this goal next
4448 | Solved Name
4549 | Log String
4650
@@ -50,21 +54,23 @@ instance Show ProofState where
5054 show (PS nm [] _ tm _ _ _) = show nm ++ ": no more goals"
5155 show (PS nm (h:hs) _ tm _ ctxt _)
5256 = let OK g = goal (Just h) tm ctxt
53− wkenv = weakenEnv (premises g) in
57+ wkenv = premises g in
5458 showPs wkenv (reverse wkenv) ++ "\n" ++
5559 "-------------------------------- (" ++ show nm ++
5660 ") -------\n" ++
5761 show h ++ " : " ++ showG wkenv (goalType g) ++ "\n"
5862 where showPs env [] = ""
5963 showPs env ((n, b):bs)
60− = " " ++ show n ++ " : " ++ showEnv env (binderTy b) ++
64+ = " " ++ show n ++ " : " ++
65+ showEnv env (normalise ctxt env (binderTy b)) ++
6166 "\n" ++ showPs env bs
62− showG ps (Guess t v) = showEnv ps t ++ " =?= " ++ showEnv ps v
67+ showG ps (Guess t v) = showEnv ps (normalise ctxt ps t) ++
68+ " =?= " ++ showEnv ps v
6369 showG ps b = showEnv ps (binderTy b)
6470
6571same Nothing n = True
6672same (Just x) n = x == n
67−
73+
6874hole (Hole _) = True
6975hole (Guess _ _) = True
7076hole _ = False
@@ -91,7 +97,8 @@ type Hole = Maybe Name -- Nothing = default hole, first in list in proof state
9197goal :: Hole -> Term -> Context -> TC Goal
9298goal h tm ctxt = g [] tm where
9399 g env (Bind n b sc) | hole b && same h n = return $ GD env b
94− | otherwise = gb env b `mplus` g ((n,b):env) sc
100+ | otherwise
101+ = gb env b `mplus` g ((n,fmap (weakenTm 1) b):weakenTmEnv 1 env) sc
95102 g env (App f a) = g env f `mplus` g env a
96103 g env t = Error "Can't find hole"
97104
@@ -105,8 +112,10 @@ tactic h ps ctxt f = do let tm = pterm ps
105112 return (ps { pterm = tm' })
106113 where
107114 atH env binder@(Bind n b sc)
108− | hole b && same h n = f ctxt (weakenEnv env) binder
109− | otherwise = pure Bind <*> pure n <*> atHb env b <*> atH ((n,b):env) sc
115+ | hole b && same h n = f ctxt env binder
116+ | otherwise
117+ = pure Bind <*> pure n <*> atHb env b <*>
118+ atH ((n,fmap (weakenTm 1) b):weakenTmEnv 1 env) sc
110119 atH env (App f a) = pure App <*> atH env f <*> atH env a
111120 atH env t = return t
112121
@@ -125,7 +134,15 @@ attack ctxt env (Bind x (Hole t) sc)
125134attack ctxt env _ = fail "Not an attackable hole"
126135
127136claim :: Name -> Raw -> RunTactic
128−claim = undefined
137+claim n ty ctxt env t =
138+ do (tyv, tyt) <- lift $ check ctxt env ty
139+ lift $ isSet ctxt env tyt
140+ action (NextGoal n)
141+ return $ Bind n (Hole tyv) t
142+
143+focus :: Name -> RunTactic
144+focus n ctxt env t = do action (FocusGoal n)
145+ return t
129146
130147regret :: RunTactic
131148regret = undefined
@@ -182,9 +199,11 @@ processTactic t ps = case holes ps of
182199 logs (_ : xs) = logs xs
183200
184201 goals g [] = g
185− goals g (AddGoal n : xs) = goals (n : g) xs
186− goals g (Solved n : xs) = goals (g \\ [n]) xs
187− goals g (_ : xs) = goals g xs
202+ goals g (AddGoal n : xs) = goals (n : g) xs
203+ goals (g:gs) (NextGoal n : xs) = goals (g : n : gs) xs
204+ goals g (FocusGoal n : xs) = goals (n : (g \\ [n])) xs
205+ goals g (Solved n : xs) = goals (g \\ [n]) xs
206+ goals g (_ : xs) = goals g xs
188207
189208
190209process :: Tactic -> Name -> ProofState -> StateT TState TC ProofState
@@ -197,3 +216,4 @@ process t h ps = tactic (Just h) ps (context ps) (mktac t)
197216 mktac (Intro n) = intro n
198217 mktac (CheckIn r) = check_in r
199218 mktac (EvalIn r) = eval_in r
219+ mktac (Focus n) = focus n
src/ShellParser.hs+1
@@ -52,6 +52,7 @@ pTactic = do reserved "attack"; return Attack
5252 <|> do reserved "intro"; n <- iName; return (Intro n)
5353 <|> do reserved "eval"; t <- pTerm; return (EvalIn t)
5454 <|> do reserved "check"; t <- pTerm; return (CheckIn t)
55+ <|> do reserved "focus"; n <- iName; return (Focus n)
5556 <|> do reserved "state"; return ProofState
5657 <|> do reserved "qed"; return QED
5758
src/Typecheck.hs+16−14
@@ -33,15 +33,17 @@ instance MonadPlus TC where
3333 err `mplus` _ = err
3434
3535converts :: Context -> Env -> Term -> Term -> TC ()
36−converts ctxt env x y = if (normalise ctxt env (bindEnv env x) ==
37− normalise ctxt env (bindEnv env y))
36+converts ctxt env x y = if (normalise ctxt env x == normalise ctxt env y)
3837 then return ()
3938 else fail ("Can't convert between " ++
40− show x ++ " and " ++ show y)
39+ showEnv env (normalise ctxt env x) ++ " and " ++
40+ showEnv env (normalise ctxt env y))
4141
42−isSet :: Term -> TC ()
43−isSet (Set _) = return ()
44−isSet tm = fail (show tm ++ " is not a Set")
42+isSet :: Context -> Env -> Term -> TC ()
43+isSet ctxt env tm = isSet' (normalise ctxt env tm)
44+ where isSet' :: Term -> TC ()
45+ isSet' (Set _) = return ()
46+ isSet' tm = fail (showEnv env tm ++ " is not a Set")
4547
4648check :: Context -> Env -> Raw -> TC (Term, Type)
4749check ctxt env (Var n)
@@ -67,13 +69,13 @@ check ctxt env (RBind n b sc)
6769 = do (tv, tt) <- check ctxt env t
6870 let tv' = normalise ctxt env tv
6971 let tt' = normalise ctxt env tt
70− isSet tt'
72+ isSet ctxt env tt'
7173 return (Lam tv')
7274 checkBinder (Pi t)
7375 = do (tv, tt) <- check ctxt env t
7476 let tv' = normalise ctxt env tv
7577 let tt' = normalise ctxt env tt
76− isSet tt'
78+ isSet ctxt env tt'
7779 return (Pi tv')
7880 checkBinder (Let t v)
7981 = do (tv, tt) <- check ctxt env t
@@ -81,13 +83,13 @@ check ctxt env (RBind n b sc)
8183 let tv' = normalise ctxt env tv
8284 let tt' = normalise ctxt env tt
8385 converts ctxt env tv vt
84− isSet tt'
86+ isSet ctxt env tt'
8587 return (Let tv' vv)
8688 checkBinder (Hole t)
8789 = do (tv, tt) <- check ctxt env t
8890 let tv' = normalise ctxt env tv
8991 let tt' = normalise ctxt env tt
90− isSet tt'
92+ isSet ctxt env tt'
9193 return (Hole tv')
9294 checkBinder (Guess t v)
9395 = do (tv, tt) <- check ctxt env t
@@ -95,13 +97,13 @@ check ctxt env (RBind n b sc)
9597 let tv' = normalise ctxt env tv
9698 let tt' = normalise ctxt env tt
9799 converts ctxt env tv vt
98− isSet tt'
100+ isSet ctxt env tt'
99101 return (Guess tv' vv)
100102 checkBinder (PVar t)
101103 = do (tv, tt) <- check ctxt env t
102104 let tv' = normalise ctxt env tv
103105 let tt' = normalise ctxt env tt
104− isSet tt'
106+ isSet ctxt env tt'
105107 return (PVar tv')
106108
107109 discharge n (Lam t) scv sct
@@ -132,12 +134,12 @@ checkProgram :: Context -> RProgram -> TC Context
132134checkProgram ctxt [] = return ctxt
133135checkProgram ctxt ((n, RConst t):xs)
134136 = do (t', tt') <- trace (show n) $ check ctxt [] t
135− isSet tt'
137+ isSet ctxt [] tt'
136138 checkProgram (addConstant n t' ctxt) xs
137139checkProgram ctxt ((n, RFunction (RawFun ty val)):xs)
138140 = do (ty', tyt') <- trace (show n) $ check ctxt [] ty
139141 (val', valt') <- check ctxt [] val
140− isSet tyt'
142+ isSet ctxt [] tyt'
141143 converts ctxt [] ty' valt'
142144 checkProgram (addToCtxt n val' ty' ctxt) xs
143145