@@ -29,7 +29,7 @@ data ProofState = PS { thname :: Name,
29 unified :: (Name, [(Name, Term)]),
29 unified :: (Name, [(Name, Term)]),
30 solved :: Maybe (Name, Term),
30 solved :: Maybe (Name, Term),
31 problems :: Fails,
31 problems :: Fails,
32− injective :: [(Term, Term, Term)],
32+ injective :: [Name],
33 deferred :: [Name], -- names we'll need to define
33 deferred :: [Name], -- names we'll need to define
34 instances :: [Name], -- instance arguments (for type classes)
34 instances :: [Name], -- instance arguments (for type classes)
35 previous :: Maybe ProofState, -- for undo
35 previous :: Maybe ProofState, -- for undo
@@ -68,6 +68,7 @@ data Tactic = Attack
68 | Defer Name
68 | Defer Name
69 | DeferType Name Raw [Name]
69 | DeferType Name Raw [Name]
70 | Instance Name
70 | Instance Name
71 | MoveLast Name
72 | MoveLast Name
72 | ProofState
73 | ProofState
@@ -148,15 +149,28 @@ hole _ = False
149holeName i = MN i "hole"
150holeName i = MN i "hole"
152+qshow :: Fails -> String
153+qshow fs = show (map (\ (x, y, _, _) -> (x, y)) fs)
151unify' :: Context -> Env -> TT Name -> TT Name -> StateT TState TC [(Name, TT Name)]
155unify' :: Context -> Env -> TT Name -> TT Name -> StateT TState TC [(Name, TT Name)]
156+unify' ctxt env topx topy =
152−unify' ctxt env topx topy = do (u, inj, fails) <- lift $ unify ctxt env topx topy
158+ (u, fails) <- lift $ unify ctxt env topx topy (injective ps) (holes ps)
153− addInj inj
159+-- trace ("Unified " ++ show (topx, topy) ++ show (injective ps) ++
160+-- " " ++ show u ++ "\n" ++ qshow fails ++ "\nCurrent problems:\n"
161+-- ++ qshow (problems ps) ++ "\n" ++ show (holes ps)) $
154− case fails of
162+ case fails of
155− [] -> return u
163+-- [] -> return u
157− do ps <- get
165+ do ps <- get
166+ let (h, ns) = unified ps
167+ let (ns', probs') = updateProblems (context ps) (u ++ ns)
168+ (err ++ problems ps)
158− put (ps { problems = err ++ problems ps })
171+ put (ps { problems = probs',
172+ unified = (h, ns') })
159− return []
173+ return u
161getName :: Monad m => String -> StateT TState m Name
175getName :: Monad m => String -> StateT TState m Name
162getName tag = do ps <- get
176getName tag = do ps <- get
@@ -194,6 +208,7 @@ goalAtFocus :: ProofState -> TC (Binder Type)
194goalAtFocus ps
208goalAtFocus ps
195 | not $ null (holes ps) = do g <- goal (Just (head (holes ps))) (pterm ps)
209 | not $ null (holes ps) = do g <- goal (Just (head (holes ps))) (pterm ps)
196 return (goalType g)
210 return (goalType g)
211+ | otherwise = error $ "No goal in " ++ show (holes ps) ++ show (pterm ps)
198goal :: Hole -> Term -> TC Goal
213goal :: Hole -> Term -> TC Goal
199goal h tm = g [] tm where
214goal h tm = g [] tm where
@@ -288,6 +303,12 @@ instanceArg n ctxt env (Bind x (Hole t) sc)
288 instances = x:is })
303 instances = x:is })
289 return (Bind x (Hole t) sc)
304 return (Bind x (Hole t) sc)
306+setinj :: Name -> RunTactic
307+setinj n ctxt env (Bind x (Hole t) sc)
308+ = do action (\ps -> let is = injective ps in
309+ ps { injective = n : is })
310+ return (Bind x (Hole t) sc)
291defer :: Name -> RunTactic
312defer :: Name -> RunTactic
292defer n ctxt env (Bind x (Hole t) (P nt x' ty)) | x == x' =
313defer n ctxt env (Bind x (Hole t) (P nt x' ty)) | x == x' =
293 do action (\ps -> let hs = holes ps in
314 do action (\ps -> let hs = holes ps in
@@ -322,10 +343,6 @@ regret ctxt env (Bind x (Hole t) sc) | noOccurrence x sc =
322 return sc
343 return sc
323regret ctxt env (Bind x (Hole t) _) = fail $ show x ++ " : " ++ show t ++ " is not solved"
344regret ctxt env (Bind x (Hole t) _) = fail $ show x ++ " : " ++ show t ++ " is not solved"
325−addInj :: [(Term, Term, Term)] -> StateT TState TC ()
326−addInj inj = do ps <- get
327− put (ps { injective = inj ++ injective ps })
329exact :: Raw -> RunTactic
346exact :: Raw -> RunTactic
330exact guess ctxt env (Bind x (Hole ty) sc) =
347exact guess ctxt env (Bind x (Hole ty) sc) =
331 do (val, valty) <- lift $ check ctxt env guess
348 do (val, valty) <- lift $ check ctxt env guess
@@ -344,7 +361,7 @@ fill guess ctxt env (Bind x (Hole ty) sc) =
344 ns <- unify' ctxt env valty ty
361 ns <- unify' ctxt env valty ty
345 ps <- get
362 ps <- get
346 let (uh, uns) = unified ps
363 let (uh, uns) = unified ps
347− put (ps { unified = (uh, uns ++ ns) })
364+-- put (ps { unified = (uh, uns ++ ns) })
348-- addLog (show (uh, uns ++ ns))
365-- addLog (show (uh, uns ++ ns))
349 return $ Bind x (Guess ty val) sc
366 return $ Bind x (Guess ty val) sc
350fill _ _ _ _ = fail "Can't fill here."
367fill _ _ _ _ = fail "Can't fill here."
@@ -362,22 +379,25 @@ complete_fill ctxt env (Bind x (Guess ty val) sc) =
362 ns <- unify' ctxt env valty ty
379 ns <- unify' ctxt env valty ty
363 ps <- get
380 ps <- get
364 let (uh, uns) = unified ps
381 let (uh, uns) = unified ps
365− put (ps { unified = (uh, uns ++ ns) })
382+-- put (ps { unified = (uh, uns ++ ns) })
366 return $ Bind x (Guess ty val) sc
383 return $ Bind x (Guess ty val) sc
367complete_fill ctxt env t = fail $ "Can't complete fill at " ++ show t
384complete_fill ctxt env t = fail $ "Can't complete fill at " ++ show t
369solve :: RunTactic
386solve :: RunTactic
370solve ctxt env (Bind x (Guess ty val) sc)
387solve ctxt env (Bind x (Guess ty val) sc)
371− | pureTerm val = do ps <- get
388+ | True = do ps <- get
372 let (uh, uns) = unified ps
389 let (uh, uns) = unified ps
373 action (\ps -> ps { holes = holes ps \\ [x],
390 action (\ps -> ps { holes = holes ps \\ [x],
374 solved = Just (x, val),
391 solved = Just (x, val),
392+-- problems = solveInProblems
393+-- x val (problems ps),
375 -- dontunify = dontunify ps \\ [x],
394 -- dontunify = dontunify ps \\ [x],
376 -- unified = (uh, uns ++ [(x, val)]),
395 -- unified = (uh, uns ++ [(x, val)]),
377 instances = instances ps \\ [x] })
396 instances = instances ps \\ [x] })
378 return $ {- Bind x (Let ty val) sc -}
397 return $ {- Bind x (Let ty val) sc -}
379 instantiate val (pToV x sc)
398 instantiate val (pToV x sc)
399+ | otherwise = do ps <- get
380− | otherwise = lift $ tfail $ IncompleteTerm val
400+ lift $ tfail $ IncompleteTerm val
381solve _ _ h = do ps <- get
401solve _ _ h = do ps <- get
382 fail $ "Not a guess " ++ show h ++ "\n" ++ show (holes ps, pterm ps)
402 fail $ "Not a guess " ++ show h ++ "\n" ++ show (holes ps, pterm ps)
@@ -396,7 +416,7 @@ introTy ty mn ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
396 do ns <- unify' ctxt env s tyv
416 do ns <- unify' ctxt env s tyv
397 ps <- get
417 ps <- get
398 let (uh, uns) = unified ps
418 let (uh, uns) = unified ps
399− put (ps { unified = (uh, uns ++ ns) })
419+-- put (ps { unified = (uh, uns ++ ns) })
400 return $ Bind n (Lam tyv) (Bind x (Hole t') (P Bound x t'))
420 return $ Bind n (Lam tyv) (Bind x (Hole t') (P Bound x t'))
401 _ -> fail "Nothing to introduce"
421 _ -> fail "Nothing to introduce"
402introTy ty n ctxt env _ = fail "Can't introduce here."
422introTy ty n ctxt env _ = fail "Can't introduce here."
@@ -494,7 +514,8 @@ hnf_compute ctxt env (Bind x (Hole ty) sc) =
494 do let ty' = hnf ctxt env ty in
514 do let ty' = hnf ctxt env ty in
495-- trace ("HNF " ++ show (ty, ty')) $
515-- trace ("HNF " ++ show (ty, ty')) $
496 return $ Bind x (Hole ty') sc
516 return $ Bind x (Hole ty') sc
497−
517+hnf_compute ctxt env t = return t
498check_in :: Raw -> RunTactic
519check_in :: Raw -> RunTactic
499check_in t ctxt env tm =
520check_in t ctxt env tm =
500 do (val, valty) <- lift $ check ctxt env t
521 do (val, valty) <- lift $ check ctxt env t
@@ -515,7 +536,7 @@ eval_in t ctxt env tm =
515 return tm
536 return tm
517start_unify :: Name -> RunTactic
538start_unify :: Name -> RunTactic
518−start_unify n ctxt env tm = do action (\ps -> ps { unified = (n, []) })
539+start_unify n ctxt env tm = do -- action (\ps -> ps { unified = (n, []) })
519 return tm
540 return tm
521tmap f (a, b, c) = (f a, b, c)
542tmap f (a, b, c) = (f a, b, c)
@@ -524,22 +545,24 @@ solve_unified :: RunTactic
524solve_unified ctxt env tm =
545solve_unified ctxt env tm =
525 do ps <- get
546 do ps <- get
526 let (_, ns) = unified ps
547 let (_, ns) = unified ps
527− let unify = dropGiven (dontunify ps) ns
548+ let unify = dropGiven (dontunify ps) ns (holes ps)
528 action (\ps -> ps { holes = holes ps \\ map fst unify })
549 action (\ps -> ps { holes = holes ps \\ map fst unify })
529 action (\ps -> ps { pterm = updateSolved unify (pterm ps) })
550 action (\ps -> ps { pterm = updateSolved unify (pterm ps) })
530− action (\ps -> ps { injective = map (tmap (updateSolved unify)) (injective ps) })
531 return (updateSolved unify tm)
551 return (updateSolved unify tm)
534−dropGiven du [] = []
554+dropGiven du [] hs = []
535−dropGiven du ((n, P Bound t ty) : us) | n `elem` du && not (t `elem` du)
555+dropGiven du ((n, P Bound t ty) : us) hs
556+ | n `elem` du && not (t `elem` du)
557+ && n `elem` hs && t `elem` hs
536− = (t, P Bound n ty) : dropGiven du us
558+ = (t, P Bound n ty) : dropGiven du us hs
559+dropGiven du (u@(n, _) : us) hs
537−dropGiven du (u@(n, _) : us) | n `elem` du = dropGiven du us
560+ | n `elem` du = dropGiven du us hs
538-- dropGiven du (u@(_, P a n ty) : us) | n `elem` du = dropGiven du us
561-- dropGiven du (u@(_, P a n ty) : us) | n `elem` du = dropGiven du us
539−dropGiven du (u : us) = u : dropGiven du us
562+dropGiven du (u : us) hs = u : dropGiven du us hs
541updateSolved xs x = -- trace ("Updating " ++ show xs ++ " in " ++ show x) $
564updateSolved xs x = -- trace ("Updating " ++ show xs ++ " in " ++ show x) $
542− updateSolved' xs x
565+ updateSolved' xs x
543updateSolved' xs (Bind n (Hole ty) t)
566updateSolved' xs (Bind n (Hole ty) t)
544 | Just v <- lookup n xs = instantiate v (pToV n (updateSolved' xs t))
567 | Just v <- lookup n xs = instantiate v (pToV n (updateSolved' xs t))
545updateSolved' xs (Bind n b t)
568updateSolved' xs (Bind n b t)
@@ -549,11 +572,30 @@ updateSolved' xs (P _ n _)
549 | Just v <- lookup n xs = v
572 | Just v <- lookup n xs = v
550updateSolved' xs t = t
573updateSolved' xs t = t
576+updateEnv ns ((n, b) : env) = (n, fmap (updateSolved ns) b) : updateEnv ns env
578+updateError ns (CantUnify b l r e xs sc)
579+ = CantUnify b (updateSolved ns l) (updateSolved ns r) (updateError ns e) xs sc
582+solveInProblems x val [] = []
583+solveInProblems x val ((l, r, env, err) : ps)
584+ = ((instantiate val (pToV x l), instantiate val (pToV x r),
585+ updateEnv [(x, val)] env, err) : solveInProblems x val ps)
552−updateProblems ns [] = []
587+updateProblems ctxt ns ps inj holes = up ns ps where
553−updateProblems ns ((x, y, env, err) : ps) =
589+ up ns ((x, y, env, err) : ps) =
554 let x' = updateSolved ns x
590 let x' = updateSolved ns x
555− y' = updateSolved ns y in
591+ y' = updateSolved ns y
592+ err' = updateError ns err
593+ env' = updateEnv ns env in
594+ case unify ctxt env' x' y' inj holes of
595+ OK (v, []) -> -- trace ("Added " ++ show v ++ " from " ++ show (x', y')) $
597+ _ -> let (ns', ps') = up ns ps in
556− (x',y',env,err) : updateProblems ns ps
598+ (ns', (x',y',env',err') : ps')
558processTactic :: Tactic -> ProofState -> TC (ProofState, String)
600processTactic :: Tactic -> ProofState -> TC (ProofState, String)
559processTactic QED ps = case holes ps of
601processTactic QED ps = case holes ps of
@@ -568,18 +610,16 @@ processTactic Undo ps = case previous ps of
568 Just pold -> return (pold, "")
610 Just pold -> return (pold, "")
569processTactic EndUnify ps
611processTactic EndUnify ps
570 = let (h, ns_in) = unified ps
612 = let (h, ns_in) = unified ps
571− ns = dropGiven (dontunify ps) ns_in
613+ ns = dropGiven (dontunify ps) ns_in (holes ps)
572 ns' = map (\ (n, t) -> (n, updateSolved ns t)) ns
614 ns' = map (\ (n, t) -> (n, updateSolved ns t)) ns
615+ (ns'', probs') = updateProblems (context ps) ns' (problems ps)
616+ (injective ps) (holes ps)
573− tm' = -- trace ("Updating " ++ show ns') $ -- ++ " in " ++ show (pterm ps)) $
617+ tm' = -- trace ("Updating " ++ show ns' ++ "\n" ++ show ns'') $ -- ++ " in " ++ show (pterm ps)) $
574− updateSolved ns' (pterm ps)
618+ updateSolved ns'' (pterm ps) in
575− probs' = updateProblems ns' (problems ps) in
577− [] -> return (ps { pterm = tm',
619+ return (ps { pterm = tm',
578− unified = (h, []),
620+ unified = (h, []),
579− injective = map (tmap (updateSolved ns'))
621+ problems = probs',
581− holes = holes ps \\ map fst ns' }, "")
622+ holes = holes ps \\ map fst ns'' }, "")
582− errs@((_,_,_,err):_) -> tfail err
583processTactic (Reorder n) ps
623processTactic (Reorder n) ps
584 = do ps' <- execStateT (tactic (Just n) reorder_claims) ps
624 = do ps' <- execStateT (tactic (Just n) reorder_claims) ps
585 return (ps' { previous = Just ps, plog = "" }, plog ps')
625 return (ps' { previous = Just ps, plog = "" }, plog ps')
@@ -590,9 +630,21 @@ processTactic t ps
590 let pterm' = case solved ps' of
630 let pterm' = case solved ps' of
591 Just s -> updateSolved [s] (pterm ps')
631 Just s -> updateSolved [s] (pterm ps')
592 _ -> pterm ps'
632 _ -> pterm ps'
634+ = case solved ps' of
635+ Just s -> updateProblems (context ps')
639+ _ -> ([], problems ps')
640+ -- rechecking problems may find more solutions, so
642+ let pterm'' = updateSolved ns' pterm'
593− return (ps' { pterm = pterm',
643+ return (ps' { pterm = pterm'',
594 solved = Nothing,
644 solved = Nothing,
595− previous = Just ps, plog = "" }, plog ps')
646+ previous = Just ps, plog = "",
647+ holes = holes ps' \\ (map fst ns')}, plog ps')
597process :: Tactic -> Name -> StateT TState TC ()
649process :: Tactic -> Name -> StateT TState TC ()
598process EndUnify _
650process EndUnify _
@@ -624,5 +676,6 @@ process t h = tactic (Just h) (mktac t)
624 mktac (Defer n) = defer n
676 mktac (Defer n) = defer n
625 mktac (DeferType n t a) = deferType n t a
677 mktac (DeferType n t a) = deferType n t a
626 mktac (Instance n) = instanceArg n
678 mktac (Instance n) = instanceArg n
679+ mktac (SetInjective n) = setinj n
627 mktac (MoveLast n) = movelast n
680 mktac (MoveLast n) = movelast n