on 1b71663 help sign in
Idris-dev/src/Idris/Core/ProofState.hs
1073 lines 48 kB history raw
1{-|
2Module : Idris.Core.ProofState
3Description : Proof state implementation.
5License : BSD3
6Maintainer : The Idris Community.
8Implements a proof state, some primitive tactics for manipulating
9proofs, and some high level commands for introducing new theorems,
10evaluation/checking inside the proof system, etc.
11-}
13{-# LANGUAGE CPP, FlexibleContexts, FlexibleInstances, MultiParamTypeClasses,
14 PatternGuards #-}
15module Idris.Core.ProofState(
16 ProofState(..), newProof, envAtFocus, goalAtFocus
17 , Tactic(..), Goal(..), processTactic, nowElaboratingPS
18 , doneElaboratingAppPS, doneElaboratingArgPS, dropGiven
19 , keepGiven, getProvenance
20 ) where
22import Idris.Core.Evaluate
23import Idris.Core.ProofTerm
24import Idris.Core.TT
25import Idris.Core.Typecheck
26import Idris.Core.Unify
27import Idris.Core.WHNF
29import Util.Pretty hiding (fill)
31import Control.Arrow ((***))
32import Control.Monad.State.Strict
33import Data.List
35#if (MIN_VERSION_base(4,11,0))
36import Prelude hiding ((<>))
37#endif
39data ProofState = PS { thname :: Name,
40 holes :: [Name], -- ^ holes still to be solved
41 usedns :: [Name], -- ^ used names, don't use again
42 nextname :: Int, -- ^ name supply, for locally unique names
43 global_nextname :: Int, -- ^ a mirror of the global name supply,
44 -- for generating things like type tags
45 -- in reflection
46 pterm :: ProofTerm, -- ^ current proof term
47 ptype :: Type, -- ^ original goal
48 dontunify :: [Name], -- ^ explicitly given by programmer, leave it
49 unified :: (Name, [(Name, Term)]),
50 notunified :: [(Name, Term)],
51 dotted :: [(Name, [Name])], -- ^ dot pattern holes + environment
52 -- either hole or something in env must turn up in the 'notunified' list during elaboration
53 solved :: Maybe (Name, Term),
54 problems :: Fails,
55 injective :: [Name],
56 deferred :: [Name], -- ^ names we'll need to define
57 implementations :: [Name], -- ^ implementation arguments (for interfaces)
58 autos :: [(Name, ([FailContext], [Name]))], -- ^ unsolved 'auto' implicits with their holes
59 psnames :: [Name], -- ^ Local names okay to use in proof search
60 previous :: Maybe ProofState, -- ^ for undo
61 context :: Context,
62 datatypes :: Ctxt TypeInfo,
63 plog :: String,
64 unifylog :: Bool,
65 done :: Bool,
66 recents :: [Name],
67 while_elaborating :: [FailContext],
68 constraint_ns :: String
69 }
71data Tactic = Attack
72 | Claim Name Raw
73 | ClaimFn Name Name Raw
74 | Reorder Name
75 | Exact Raw
76 | Fill Raw
77 | MatchFill Raw
78 | PrepFill Name [Name]
79 | CompleteFill
80 | Regret
81 | Solve
82 | StartUnify Name
83 | EndUnify
84 | UnifyAll
85 | Compute
86 | ComputeLet Name
87 | Simplify
88 | WHNF_Compute
89 | WHNF_ComputeArgs
90 | EvalIn Raw
91 | CheckIn Raw
92 | Intro (Maybe Name)
93 | IntroTy Raw (Maybe Name)
94 | Forall Name RigCount (Maybe ImplicitInfo) Raw
95 | LetBind Name RigCount Raw Raw
96 | ExpandLet Name Term
97 | Rewrite Raw
98 | Equiv Raw
99 | PatVar Name
100 | PatBind Name RigCount
101 | Focus Name
102 | Defer [Name] [Name] Name
103 | DeferType Name Raw [Name]
104 | Implementation Name
105 | AutoArg Name
106 | SetInjective Name
107 | MoveLast Name
108 | MatchProblems Bool
109 | UnifyProblems
110 | UnifyGoal Raw
111 | UnifyTerms Raw Raw
112 | ProofState
113 | Undo
114 | QED
115 deriving Show
117-- Some utilites on proof and tactic states
119instance Show ProofState where
120 show ps | [] <- holes ps
121 = show (thname ps) ++ ": no more goals"
122 show ps | (h : hs) <- holes ps
123 = let tm = pterm ps
124 nm = thname ps
125 OK g = goal (Just h) tm
126 wkenv = premises g in
127 "Other goals: " ++ show hs ++ "\n" ++
128 showPs wkenv (reverse wkenv) ++ "\n" ++
129 "-------------------------------- (" ++ show nm ++
130 ") -------\n " ++
131 show h ++ " : " ++ showG wkenv (goalType g) ++ "\n"
132 where showPs env [] = ""
133 showPs env ((n, _, Let _ t v):bs)
134 = " " ++ show n ++ " : " ++
135 showEnv env ({- normalise ctxt env -} t) ++ " = " ++
136 showEnv env ({- normalise ctxt env -} v) ++
137 "\n" ++ showPs env bs
138 showPs env ((n, _, b):bs)
139 = " " ++ show n ++ " : " ++
140 showEnv env ({- normalise ctxt env -} (binderTy b)) ++
141 "\n" ++ showPs env bs
142 showG ps (Guess t v) = showEnv ps ({- normalise ctxt ps -} t) ++
143 " =?= " ++ showEnv ps v
144 showG ps b = showEnv ps (binderTy b)
146instance Pretty ProofState OutputAnnotation where
147 pretty ps | [] <- holes ps =
148 pretty (thname ps) <+> colon <+> text " no more goals."
149 pretty ps | (h : hs) <- holes ps =
150 let tm = pterm ps
151 OK g = goal (Just h) tm
152 nm = thname ps in
153 let wkEnv = premises g in
154 text "Other goals" <+> colon <+> pretty hs <+>
155 prettyPs wkEnv (reverse wkEnv) <+>
156 text "---------- " <+> text "Focussing on" <> colon <+> pretty nm <+> text " ----------" <+>
157 pretty h <+> colon <+> prettyGoal wkEnv (goalType g)
158 where
159 prettyGoal ps (Guess t v) =
160 prettyEnv ps t <+> text "=?=" <+> prettyEnv ps v
161 prettyGoal ps b = prettyEnv ps $ binderTy b
163 prettyPs env [] = empty
164 prettyPs env ((n, _, Let _ t v):bs) =
165 nest nestingSize (pretty n <+> colon <+>
166 prettyEnv env t <+> text "=" <+> prettyEnv env v <+>
167 nest nestingSize (prettyPs env bs))
168 prettyPs env ((n, _, b):bs) =
169 nest nestingSize (pretty n <+> colon <+> prettyEnv env (binderTy b) <+>
170 nest nestingSize (prettyPs env bs))
172holeName i = sMN i "hole"
174qshow :: Fails -> String
175qshow fs = show (map (\ (x, y, hs, env, _, _, t) -> (t, map fstEnv env, x, y, hs)) fs)
177match_unify' :: Context -> Env ->
178 (TT Name, Maybe Provenance) ->
179 (TT Name, Maybe Provenance) ->
180 StateT TState TC [(Name, TT Name)]
181match_unify' ctxt env (topx, xfrom) (topy, yfrom) =
182 do ps <- get
183 let while = while_elaborating ps
184 let inj = injective ps
185 traceWhen (unifylog ps)
186 ("Matching " ++ show (topx, topy) ++
187 " in " ++ show env ++
188 "\nHoles: " ++ show (holes ps)
189 ++ "\n"
190 ++ "\n" ++ show (getProofTerm (pterm ps)) ++ "\n\n"
191 ) $
192 case match_unify ctxt env (topx, xfrom) (topy, yfrom) inj (holes ps) while of
193 OK u -> traceWhen (unifylog ps)
194 ("Matched " ++ show u) $
195 do let (h, ns) = unified ps
196 put (ps { unified = (h, u ++ ns) })
197 return u
198 Error e -> traceWhen (unifylog ps)
199 ("No match " ++ show e) $
200 do put (ps { problems = (topx, topy, True,
201 env, e, while, Match) :
202 problems ps })
203 return []
204-- traceWhen (unifylog ps)
205-- ("Matched " ++ show (topx, topy) ++ " without " ++ show dont ++
206-- "\nSolved: " ++ show u
207-- ++ "\nCurrent problems:\n" ++ qshow (problems ps)
208-- -- ++ show (pterm ps)
209-- ++ "\n----------") $
211mergeSolutions :: Env -> [(Name, TT Name)] -> StateT TState TC [(Name, TT Name)]
212mergeSolutions env ns = merge [] ns
213 where
214 merge acc [] = return (reverse acc)
215 merge acc ((n, t) : ns)
216 | Just t' <- lookup n ns
217 = do ps <- get
218 let probs = problems ps
219 put (ps { problems = probs ++ [(t,t',True,env,
220 CantUnify True (t, Nothing) (t', Nothing) (Msg "") (errEnv env) 0,
221 [], Unify)] })
222 merge acc ns
223 | otherwise = merge ((n, t): acc) ns
225dropSwaps :: [(Name, TT Name)] -> [(Name, TT Name)]
226dropSwaps [] = []
227dropSwaps (p@(x, P _ y _) : xs) | solvedIn y x xs = dropSwaps xs
228 where solvedIn _ _ [] = False
229 solvedIn y x ((y', P _ x' _) : xs) | y == y' && x == x' = True
230 solvedIn y x (_ : xs) = solvedIn y x xs
231dropSwaps (p : xs) = p : dropSwaps xs
233unify' :: Context -> Env ->
234 (TT Name, Maybe Provenance) ->
235 (TT Name, Maybe Provenance) ->
236 StateT TState TC [(Name, TT Name)]
237unify' ctxt env (topx, xfrom) (topy, yfrom) =
238 do ps <- get
239 let while = while_elaborating ps
240 let dont = dontunify ps
241 let inj = injective ps
242 (u, fails) <- traceWhen (unifylog ps)
243 ("Trying " ++ show (topx, topy) ++
244 "\nNormalised " ++ show (normalise ctxt env topx,
245 normalise ctxt env topy) ++
246 " in\n" ++ show env ++
247 "\nHoles: " ++ show (holes ps)
248 ++ "\nInjective: " ++ show (injective ps)
249 ++ "\n") $
250 lift $ unify ctxt env (topx, xfrom) (topy, yfrom) inj (holes ps)
251 (map fst (notunified ps)) while
252 let notu = filter (\ (n, t) -> case t of
253-- P _ _ _ -> False
254 _ -> n `elem` dont) u
255 traceWhen (unifylog ps)
256 ("Unified " ++ show (topx, topy) ++ " without " ++ show dont ++
257 "\nSolved: " ++ show u ++ "\nNew problems: " ++ qshow fails
258 ++ "\nNot unified:\n" ++ show (notunified ps)
259 ++ "\nCurrent problems:\n" ++ qshow (problems ps)
260-- ++ show (pterm ps)
261 ++ "\n----------") $
262 do let (h, ns) = unified ps
263 -- solve in results (maybe unify should do this itself...)
264 let u' = map (\(n, sol) -> (n, updateSolvedTerm u sol)) u
265 -- if a metavar has multiple solutions, make a new unification
266 -- problem for each.
267 uns <- mergeSolutions env (u' ++ ns)
268 ps <- get
269 let (ns_p, probs') = updateProblems ps uns (fails ++ problems ps)
270 let ns' = dropSwaps ns_p
271 let (notu', probs_notu) = mergeNotunified env (holes ps) (notu ++ notunified ps)
272 traceWhen (unifylog ps)
273 ("Now solved: " ++ show ns' ++
274 "\nNow problems: " ++ qshow (probs' ++ probs_notu) ++
275 "\nNow injective: " ++ show (updateInj u (injective ps))) $
276 put (ps { problems = probs' ++ probs_notu,
277 unified = (h, ns'),
278 injective = updateInj u (injective ps),
279 notunified = notu' })
280 return u
281 where updateInj ((n, a) : us) inj
282 | (P _ n' _, _) <- unApply a,
283 n `elem` inj = updateInj us (n':inj)
284 | (P _ n' _, _) <- unApply a,
285 n' `elem` inj = updateInj us (n:inj)
286 updateInj (_ : us) inj = updateInj us inj
287 updateInj [] inj = inj
289nowElaboratingPS :: FC -> Name -> Name -> ProofState -> ProofState
290nowElaboratingPS fc f arg ps = ps { while_elaborating = FailContext fc f arg : while_elaborating ps }
292dropUntil :: (a -> Bool) -> [a] -> [a]
293dropUntil p [] = []
294dropUntil p (x:xs) | p x = xs
295 | otherwise = dropUntil p xs
297doneElaboratingAppPS :: Name -> ProofState -> ProofState
298doneElaboratingAppPS f ps = let while = while_elaborating ps
299 while' = dropUntil (\ (FailContext _ f' _) -> f == f') while
300 in ps { while_elaborating = while' }
302doneElaboratingArgPS :: Name -> Name -> ProofState -> ProofState
303doneElaboratingArgPS f x ps = let while = while_elaborating ps
304 while' = dropUntil (\ (FailContext _ f' x') -> f == f' && x == x') while
305 in ps { while_elaborating = while' }
307getName :: Monad m => String -> StateT TState m Name
308getName tag = do ps <- get
309 let n = nextname ps
310 put (ps { nextname = n+1 })
311 return $ sMN n tag
313action :: Monad m => (ProofState -> ProofState) -> StateT TState m ()
314action a = do ps <- get
315 put (a ps)
317query :: Monad m => (ProofState -> r) -> StateT TState m r
318query q = do ps <- get
319 return $ q ps
321addLog :: Monad m => String -> StateT TState m ()
322addLog str = action (\ps -> ps { plog = plog ps ++ str ++ "\n" })
324newProof :: Name -- ^ the name of what's to be elaborated
325 -> String -- ^ current source file
326 -> Context -- ^ the current global context
327 -> Ctxt TypeInfo -- ^ the value of the idris_datatypes field of IState
328 -> Int -- ^ the value of the idris_name field of IState
329 -> Type -- ^ the goal type
330 -> ProofState
331newProof n tcns ctxt datatypes globalNames ty =
332 let h = holeName 0
333 ty' = vToP ty
334 in PS n [h] [] 1 globalNames (mkProofTerm (Bind h (Hole ty')
335 (P Bound h ty'))) ty [] (h, []) [] []
336 Nothing [] []
337 [] [] [] []
338 Nothing ctxt datatypes "" False False [] [] tcns
340type TState = ProofState -- [TacticAction])
341type RunTactic = RunTactic' TState
343envAtFocus :: ProofState -> TC Env
344envAtFocus ps
345 | not $ null (holes ps) = do g <- goal (Just (head (holes ps))) (pterm ps)
346 return (premises g)
347 | otherwise = fail $ "No holes in " ++ show (getProofTerm (pterm ps))
349goalAtFocus :: ProofState -> TC (Binder Type)
350goalAtFocus ps
351 | not $ null (holes ps) = do g <- goal (Just (head (holes ps))) (pterm ps)
352 return (goalType g)
353 | otherwise = Error . Msg $ "No goal in " ++ show (holes ps) ++ show (getProofTerm (pterm ps))
355tactic :: Hole -> RunTactic -> StateT TState TC ()
356tactic h f = do ps <- get
357 (tm', _) <- atHole h f (context ps) [] (pterm ps)
358 ps <- get -- might have changed while processing
359 put (ps { pterm = tm' })
361computeLet :: Context -> Name -> Term -> Term
362computeLet ctxt n tm = cl [] tm where
363 cl env (Bind n' (Let r t v) sc)
364 | n' == n = let v' = normalise ctxt env v in
365 Bind n' (Let r t v') sc
366 cl env (Bind n' b sc) = Bind n' (fmap (cl env) b) (cl ((n, Rig0, b):env) sc)
367 cl env (App s f a) = App s (cl env f) (cl env a)
368 cl env t = t
370attack :: RunTactic
371attack ctxt env (Bind x (Hole t) sc)
372 = do h <- getName "hole"
373 action (\ps -> ps { holes = h : holes ps })
374 return $ Bind x (Guess t (newtm h)) sc
375 where
376 newtm h = Bind h (Hole t) (P Bound h t)
377attack ctxt env _ = fail "Not an attackable hole"
379claim :: Name -> Raw -> RunTactic
380claim n ty ctxt env t =
381 do (tyv, tyt) <- lift $ check ctxt env ty
382 lift $ isType ctxt env tyt
383 action (\ps -> let (g:gs) = holes ps in
384 ps { holes = g : n : gs } )
385 return $ Bind n (Hole tyv) t
387-- If the current goal is 'retty', make a claim which is a function that
388-- can compute a retty from argty (i.e a claim 'argty -> retty')
389claimFn :: Name -> Name -> Raw -> RunTactic
390claimFn n bn argty ctxt env t@(Bind x (Hole retty) sc) =
391 do (tyv, tyt) <- lift $ check ctxt env argty
392 lift $ isType ctxt env tyt
393 action (\ps -> let (g:gs) = holes ps in
394 ps { holes = g : n : gs } )
395 return $ Bind n (Hole (Bind bn (Pi RigW Nothing tyv tyt) retty)) t
396claimFn _ _ _ ctxt env _ = fail "Can't make function type here"
398reorder_claims :: RunTactic
399reorder_claims ctxt env t
400 = -- trace (showSep "\n" (map show (scvs t))) $
401 let (bs, sc) = scvs t []
402 newbs = reverse (sortB (reverse bs)) in
403 traceWhen (bs /= newbs) (show bs ++ "\n ==> \n" ++ show newbs) $
404 return (bindAll newbs sc)
405 where scvs (Bind n b@(Hole _) sc) acc = scvs sc ((n, b):acc)
406 scvs sc acc = (reverse acc, sc)
408 sortB :: [(Name, Binder (TT Name))] -> [(Name, Binder (TT Name))]
409 sortB [] = []
410 sortB (x:xs) | all (noOcc x) xs = x : sortB xs
411 | otherwise = sortB (insertB x xs)
413 insertB x [] = [x]
414 insertB x (y:ys) | all (noOcc x) (y:ys) = x : y : ys
415 | otherwise = y : insertB x ys
417 noOcc (n, _) (_, Let _ t v) = noOccurrence n t && noOccurrence n v
418 noOcc (n, _) (_, Guess t v) = noOccurrence n t && noOccurrence n v
419 noOcc (n, _) (_, b) = noOccurrence n (binderTy b)
421focus :: Name -> RunTactic
422focus n ctxt env t = do action (\ps -> let hs = holes ps in
423 if n `elem` hs
424 then ps { holes = n : (hs \\ [n]) }
425 else ps)
426 ps <- get
427 return t
429movelast :: Name -> RunTactic
430movelast n ctxt env t = do action (\ps -> let hs = holes ps in
431 if n `elem` hs
432 then ps { holes = (hs \\ [n]) ++ [n] }
433 else ps)
434 return t
436implementationArg :: Name -> RunTactic
437implementationArg n ctxt env (Bind x (Hole t) sc)
438 = do action (\ps -> let hs = holes ps
439 is = implementations ps in
440 ps { holes = (hs \\ [x]) ++ [x],
441 implementations = x:is })
442 return (Bind x (Hole t) sc)
443implementationArg n ctxt env _
444 = fail "The current focus is not a hole."
446autoArg :: Name -> RunTactic
447autoArg n ctxt env (Bind x (Hole t) sc)
448 = do action (\ps -> case lookup x (autos ps) of
449 Nothing ->
450 let hs = holes ps in
451 ps { holes = (hs \\ [x]) ++ [x],
452 autos = (x, (while_elaborating ps, refsIn t)) : autos ps }
453 Just _ -> ps)
454 return (Bind x (Hole t) sc)
455autoArg n ctxt env _
456 = fail "The current focus not a hole."
458setinj :: Name -> RunTactic
459setinj n ctxt env (Bind x b sc)
460 = do action (\ps -> let is = injective ps in
461 ps { injective = n : is })
462 return (Bind x b sc)
464defer :: [Name] -> [Name] -> Name -> RunTactic
465defer dropped linused n ctxt env (Bind x (Hole t) (P nt x' ty)) | x == x' =
466 do let env' = filter (\(n, _, t) -> n `notElem` dropped) env
467 action (\ps -> let hs = holes ps in
468 ps { usedns = n : usedns ps,
469 holes = hs \\ [x] })
470 ps <- get
471 return (Bind n (GHole (length env') (psnames ps) (mkTy (reverse env') t))
472 (mkApp (P Ref n ty) (map getP (reverse env'))))
473 where
474 mkTy [] t = t
475 mkTy ((n,rig,b) : bs) t = Bind n (Pi (setRig rig n) Nothing (binderTy b) (TType (UVar [] 0))) (mkTy bs t)
477 setRig Rig1 n | n `elem` linused = Rig0
478 setRig rig n = rig
480 getP (n, rig, b) = P Bound n (binderTy b)
481defer dropped linused n ctxt env _ = fail "Can't defer a non-hole focus."
483-- as defer, but build the type and application explicitly
484deferType :: Name -> Raw -> [Name] -> RunTactic
485deferType n fty_in args ctxt env (Bind x (Hole t) (P nt x' ty)) | x == x' =
486 do (fty, _) <- lift $ check ctxt env fty_in
487 action (\ps -> let hs = holes ps
488 ds = deferred ps in
489 ps { holes = hs \\ [x],
490 deferred = n : ds })
491 return (Bind n (GHole 0 [] fty)
492 (mkApp (P Ref n ty) (map getP args)))
493 where
494 getP n = case lookupBinder n env of
495 Just b -> P Bound n (binderTy b)
496 Nothing -> error ("deferType can't find " ++ show n)
497deferType _ _ _ _ _ _ = fail "Can't defer a non-hole focus."
499unifyGoal :: Raw -> RunTactic
500unifyGoal tm ctxt env h@(Bind x b sc) =
501 do (tmv, _) <- lift $ check ctxt env tm
502 ns' <- unify' ctxt env (binderTy b, Nothing) (tmv, Nothing)
503 return h
504unifyGoal _ _ _ _ = fail "The focus is not a binder."
506unifyTerms :: Raw -> Raw -> RunTactic
507unifyTerms tm1 tm2 ctxt env h =
508 do (tm1v, _) <- lift $ check ctxt env tm1
509 (tm2v, _) <- lift $ check ctxt env tm2
510 ns' <- unify' ctxt env (tm1v, Nothing) (tm2v, Nothing)
511 return h
513exact :: Raw -> RunTactic
514exact guess ctxt env (Bind x (Hole ty) sc) =
515 do (val, valty) <- lift $ check ctxt env guess
516 lift $ converts ctxt env valty ty
517 return $ Bind x (Guess ty val) sc
518exact _ _ _ _ = fail "Can't fill here."
520-- As exact, but attempts to solve other goals by unification
522fill :: Raw -> RunTactic
523fill guess ctxt env (Bind x (Hole ty) sc) =
524 do (val, valty) <- lift $ check ctxt env guess
525 unify' ctxt env (valty, Just $ SourceTerm val)
526 (ty, Just (chkPurpose val ty))
527 return $ Bind x (Guess ty val) sc
528 where
529 -- some expected types show up commonly in errors and indicate a
530 -- specific problem.
531 -- argTy -> retTy suggests a function applied to too many arguments
532 chkPurpose val (Bind _ (Pi _ _ (P _ (MN _ _) _) _) (P _ (MN _ _) _))
533 = TooManyArgs val
534 chkPurpose _ _ = ExpectedType
535fill _ _ _ _ = fail "Can't fill here."
537-- As fill, but attempts to solve other goals by matching
539match_fill :: Raw -> RunTactic
540match_fill guess ctxt env (Bind x (Hole ty) sc) =
541 do (val, valty) <- lift $ check ctxt env guess
542 match_unify' ctxt env (valty, Just $ SourceTerm val)
543 (ty, Just ExpectedType)
544 return $ Bind x (Guess ty val) sc
545match_fill _ _ _ _ = fail "Can't fill here."
547prep_fill :: Name -> [Name] -> RunTactic
548prep_fill f as ctxt env (Bind x (Hole ty) sc) =
549 do let val = mkApp (P Ref f Erased) (map (\n -> P Ref n Erased) as)
550 return $ Bind x (Guess ty val) sc
551prep_fill f as ctxt env t = fail $ "Can't prepare fill at " ++ show t
553complete_fill :: RunTactic
554complete_fill ctxt env (Bind x (Guess ty val) sc) =
555 do let guess = forget val
556 (val', valty) <- lift $ check ctxt env guess
557 unify' ctxt env (valty, Just $ SourceTerm val')
558 (ty, Just ExpectedType)
559 return $ Bind x (Guess ty val) sc
560complete_fill ctxt env t = fail $ "Can't complete fill at " ++ show t
562-- When solving something in the 'dont unify' set, we should check
563-- that the guess we are solving it with unifies with the thing unification
564-- found for it, if anything.
566solve :: RunTactic
567solve ctxt env (Bind x (Guess ty val) sc)
568 = do ps <- get
569 dropdots <-
570 case lookup x (notunified ps) of
571 Just (P _ h _)
572 | h == x -> return [] -- Can't solve itself!
573 Just tm -> -- trace ("NEED MATCH: " ++ show (x, tm, val) ++ "\nIN " ++ show (pterm ps)) $
574 do match_unify' ctxt env (tm, Just InferredVal)
575 (val, Just GivenVal)
576 return [x]
577 _ -> return []
578 action (\ps -> ps { holes = traceWhen (unifylog ps) ("Dropping hole " ++ show x) $
579 holes ps \\ [x],
580 solved = Just (x, val),
581 dontunify = filter (/= x) (dontunify ps),
582 notunified = updateNotunified [(x,val)]
583 (notunified ps),
584 recents = x : recents ps,
585 implementations = implementations ps \\ [x],
586 dotted = dropUnified dropdots (dotted ps) })
587 let (locked, _) = tryLock (holes ps \\ [x]) (updsubst x val sc) in
588 return locked
589 where dropUnified ddots [] = []
590 dropUnified ddots ((x, es) : ds)
591 | x `elem` ddots || any (\e -> e `elem` ddots) es
592 = dropUnified ddots ds
593 | otherwise = (x, es) : dropUnified ddots ds
595 tryLock :: [Name] -> Term -> (Term, Bool)
596 tryLock hs tm@(App Complete _ _) = (tm, True)
597 tryLock hs tm@(App s f a) =
598 let (f', fl) = tryLock hs f
599 (a', al) = tryLock hs a in
600 if fl && al then (App Complete f' a', True)
601 else (App s f' a', False)
602 tryLock hs t@(P _ n _) = (t, not $ n `elem` hs)
603 tryLock hs t@(Bind n (Hole _) sc) = (t, False)
604 tryLock hs t@(Bind n (Guess _ _) sc) = (t, False)
605 tryLock hs t@(Bind n (Let r ty val) sc)
606 = let (ty', tyl) = tryLock hs ty
607 (val', vall) = tryLock hs val
608 (sc', scl) = tryLock hs sc in
609 (Bind n (Let r ty' val') sc', tyl && vall && scl)
610 tryLock hs t@(Bind n b sc)
611 = let (bt', btl) = tryLock hs (binderTy b)
612 (sc', scl) = tryLock hs sc in
613 (Bind n (b { binderTy = bt' }) sc', btl && scl)
614 tryLock hs t = (t, True)
616solve _ _ h@(Bind x t sc)
617 = do ps <- get
618 case findType x sc of
619 Just t -> lift $ tfail (CantInferType (show t))
620 _ -> lift $ tfail (IncompleteTerm h)
621 where findType x (Bind n (Let r t v) sc)
622 = findType x v `mplus` findType x sc
623 findType x (Bind n t sc)
624 | P _ x' _ <- binderTy t, x == x' = Just n
625 | otherwise = findType x sc
626 findType x _ = Nothing
628introTy :: Raw -> Maybe Name -> RunTactic
629introTy ty mn ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
630 do let n = case mn of
631 Just name -> name
632 Nothing -> x
633 let t' = case t of
634 x@(Bind y (Pi _ _ s _) _) -> x
635 _ -> normalise ctxt env t
636 (tyv, tyt) <- lift $ check ctxt env ty
637 case t' of
638 Bind y (Pi rig _ s _) t -> let t' = updsubst y (P Bound n s) t in
639 do unify' ctxt env (s, Nothing) (tyv, Nothing)
640 return $ Bind n (Lam rig tyv) (Bind x (Hole t') (P Bound x t'))
641 _ -> lift $ tfail $ CantIntroduce t'
642introTy ty n ctxt env _ = fail "Can't introduce here."
644intro :: Maybe Name -> RunTactic
645intro mn ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
646 do let t' = case t of
647 x@(Bind y (Pi _ _ s _) _) -> x
648 _ -> normalise ctxt env t
649 case t' of
650 Bind y (Pi rig _ s _) t ->
651 let n = case mn of
652 Just name -> name
653 Nothing -> y
654 -- It's important that this be subst and not updsubst,
655 -- because we want to substitute even in portions of
656 -- terms that we know do not contain holes.
657 t' = subst y (P Bound n s) t
658 in return $ Bind n (Lam rig s) (Bind x (Hole t') (P Bound x t'))
659 _ -> lift $ tfail $ CantIntroduce t'
660intro n ctxt env _ = fail "Can't introduce here."
662forall :: Name -> RigCount -> Maybe ImplicitInfo -> Raw -> RunTactic
663forall n rig impl ty ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
664 do (tyv, tyt) <- lift $ check ctxt env ty
665 unify' ctxt env (tyt, Nothing) (TType (UVar [] 0), Nothing)
666 unify' ctxt env (t, Nothing) (TType (UVar [] 0), Nothing)
667 return $ Bind n (Pi rig impl tyv (TType (UVar [] 0))) (Bind x (Hole t) (P Bound x t))
668forall n rig impl ty ctxt env _ = fail "Can't pi bind here"
670patvar :: Name -> RunTactic
671patvar n ctxt env (Bind x (Hole t) sc) =
672 do action (\ps -> ps { holes = traceWhen (unifylog ps) ("Dropping pattern hole " ++ show x) $
673 holes ps \\ [x],
674 solved = Just (x, P Bound n t),
675 dontunify = filter (/=x) (dontunify ps),
676 notunified = updateNotunified [(x,P Bound n t)]
677 (notunified ps),
678 injective = addInj n x (injective ps)
679 })
680 return $ Bind n (PVar RigW t) (updsubst x (P Bound n t) sc)
681 where addInj n x ps | x `elem` ps = n : ps
682 | otherwise = ps
683patvar n ctxt env tm = fail $ "Can't add pattern var at " ++ show tm
685letbind :: Name -> RigCount -> Raw -> Raw -> RunTactic
686letbind n rig ty val ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
687 do (tyv, tyt) <- lift $ check ctxt env ty
688 (valv, valt) <- lift $ check ctxt env val
689 lift $ isType ctxt env tyt
690 return $ Bind n (Let rig tyv valv) (Bind x (Hole t) (P Bound x t))
691letbind n rig ty val ctxt env _ = fail "Can't let bind here"
693expandLet :: Name -> Term -> RunTactic
694expandLet n v ctxt env tm =
695 return $ updsubst n v tm
697rewrite :: Raw -> RunTactic
698rewrite tm ctxt env (Bind x (Hole t) xp@(P _ x' _)) | x == x' =
699 do (tmv, tmt) <- lift $ check ctxt env tm
700 let tmt' = normalise ctxt env tmt
701 case unApply tmt' of
702 (P _ (UN q) _, [lt,rt,l,r]) | q == txt "=" ->
703 do let p = Bind rname (Lam RigW lt) (mkP (P Bound rname lt) r l t)
704 let newt = mkP l r l t
705 let sc = forget $ (Bind x (Hole newt)
706 (mkApp (P Ref (sUN "replace") (TType (UVal 0)))
707 [lt, l, r, p, tmv, xp]))
708 (scv, sct) <- lift $ check ctxt env sc
709 return scv
710 _ -> lift $ tfail (NotEquality tmv tmt')
711 where rname = sMN 0 "replaced"
712rewrite _ _ _ _ = fail "Can't rewrite here"
714-- To make the P for rewrite, replace syntactic occurrences of l in ty with
715-- an x, and put \x : lt in front
716mkP :: TT Name -> TT Name -> TT Name -> TT Name -> TT Name
717mkP lt l r ty | l == ty = lt
718mkP lt l r (App s f a) = let f' = if (r /= f) then mkP lt l r f else f
719 a' = if (r /= a) then mkP lt l r a else a in
720 App s f' a'
721mkP lt l r (Bind n b sc)
722 = let b' = mkPB b
723 sc' = if (r /= sc) then mkP lt l r sc else sc in
724 Bind n b' sc'
725 where mkPB (Let c t v) = let t' = if (r /= t) then mkP lt l r t else t
726 v' = if (r /= v) then mkP lt l r v else v in
727 Let c t' v'
728 mkPB b = let ty = binderTy b
729 ty' = if (r /= ty) then mkP lt l r ty else ty in
730 b { binderTy = ty' }
731mkP lt l r x = x
733equiv :: Raw -> RunTactic
734equiv tm ctxt env (Bind x (Hole t) sc) =
735 do (tmv, tmt) <- lift $ check ctxt env tm
736 lift $ converts ctxt env tmv t
737 return $ Bind x (Hole tmv) sc
738equiv tm ctxt env _ = fail "Can't equiv here"
740patbind :: Name -> RigCount -> RunTactic
741patbind n rig ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
742 do let t' = case t of
743 x@(Bind y (PVTy s) t) -> x
744 _ -> normalise ctxt env t
745 case t' of
746 Bind y (PVTy s) t -> let t' = updsubst y (P Bound n s) t in
747 return $ Bind n (PVar rig s) (Bind x (Hole t') (P Bound x t'))
748 _ -> fail "Nothing to pattern bind"
749patbind n _ ctxt env _ = fail "Can't pattern bind here"
751compute :: RunTactic
752compute ctxt env (Bind x (Hole ty) sc) =
753 do return $ Bind x (Hole (normalise ctxt env ty)) sc
754compute ctxt env t = return t
756whnf_compute :: RunTactic
757whnf_compute ctxt env (Bind x (Hole ty) sc) =
758 do let ty' = whnf ctxt env ty in
759 return $ Bind x (Hole ty') sc
760whnf_compute ctxt env t = return t
762whnf_compute_args :: RunTactic
763whnf_compute_args ctxt env (Bind x (Hole ty) sc) =
764 do let ty' = whnfArgs ctxt env ty in
765 return $ Bind x (Hole ty') sc
766whnf_compute_args ctxt env t = return t
768-- reduce let bindings only
769simplify :: RunTactic
770simplify ctxt env (Bind x (Hole ty) sc) =
771 do return $ Bind x (Hole (fst (specialise ctxt env [] ty))) sc
772simplify ctxt env t = return t
774check_in :: Raw -> RunTactic
775check_in t ctxt env tm =
776 do (val, valty) <- lift $ check ctxt env t
777 addLog (showEnv env val ++ " : " ++ showEnv env valty)
778 return tm
780eval_in :: Raw -> RunTactic
781eval_in t ctxt env tm =
782 do (val, valty) <- lift $ check ctxt env t
783 let val' = normalise ctxt env val
784 let valty' = normalise ctxt env valty
785 addLog (showEnv env val ++ " : " ++
786 showEnv env valty ++
787-- " in " ++ show env ++
788 " ==>\n " ++
789 showEnv env val' ++ " : " ++
790 showEnv env valty')
791 return tm
793start_unify :: Name -> RunTactic
794start_unify n ctxt env tm = do -- action (\ps -> ps { unified = (n, []) })
795 return tm
797solve_unified :: RunTactic
798solve_unified ctxt env tm =
799 do ps <- get
800 let (_, ns) = unified ps
801 let unify = dropGiven (dontunify ps) ns (holes ps)
802 action (\ps -> ps { holes = traceWhen (unifylog ps) ("Dropping holes " ++ show (map fst unify)) $
803 holes ps \\ map fst unify,
804 recents = recents ps ++ map fst unify })
805 action (\ps -> ps { pterm = updateSolved unify (pterm ps) })
806 return (updateSolvedTerm unify tm)
808dropGiven du [] hs = []
809dropGiven du ((n, P Bound t ty) : us) hs
810 | n `elem` du && not (t `elem` du)
811 && n `elem` hs && t `elem` hs
812 = (t, P Bound n ty) : dropGiven du us hs
813dropGiven du (u@(n, _) : us) hs
814 | n `elem` du = dropGiven du us hs
815-- dropGiven du (u@(_, P a n ty) : us) | n `elem` du = dropGiven du us
816dropGiven du (u : us) hs = u : dropGiven du us hs
818keepGiven du [] hs = []
819keepGiven du ((n, P Bound t ty) : us) hs
820 | n `elem` du && not (t `elem` du)
821 && n `elem` hs && t `elem` hs
822 = keepGiven du us hs
823keepGiven du (u@(n, _) : us) hs
824 | n `elem` du = u : keepGiven du us hs
825keepGiven du (u : us) hs = keepGiven du us hs
827updateEnv [] e = e
828updateEnv ns [] = []
829updateEnv ns ((n, rig, b) : env)
830 = (n, rig, fmap (updateSolvedTerm ns) b) : updateEnv ns env
832updateProv ns (SourceTerm t) = SourceTerm $ updateSolvedTerm ns t
833updateProv ns p = p
835updateError [] err = err
836updateError ns (At f e) = At f (updateError ns e)
837updateError ns (Elaborating s n ty e) = Elaborating s n ty (updateError ns e)
838updateError ns (ElaboratingArg f a env e)
839 = ElaboratingArg f a env (updateError ns e)
840updateError ns (CantUnify b (l,lp) (r,rp) e xs sc)
841 = CantUnify b (updateSolvedTerm ns l, fmap (updateProv ns) lp)
842 (updateSolvedTerm ns r, fmap (updateProv ns) rp) (updateError ns e) xs sc
843updateError ns e = e
845mergeNotunified :: Env -> [Name] -> [(Name, Term)] -> ([(Name, Term)], Fails)
846mergeNotunified env holes ns = mnu ns [] [] where
847 mnu [] ns_acc ps_acc = (reverse ns_acc, reverse ps_acc)
848 mnu ((n, t):ns) ns_acc ps_acc
849 | Just t' <- lookup n ns, t /= t'
850 = mnu ns ((n,t') : ns_acc)
851 ((t,t',True, env,CantUnify True (t, Nothing) (t', Nothing) (Msg "") [] 0, [],Match) : ps_acc)
852 | otherwise = mnu ns ((n,t) : ns_acc) ps_acc
854updateNotunified [] nu = nu
855updateNotunified ns nu = up nu where
856 up [] = []
857 up ((n, t) : nus) = let t' = updateSolvedTerm ns t in
858 ((n, t') : up nus)
860getProvenance :: Err -> (Maybe Provenance, Maybe Provenance)
861getProvenance (CantUnify _ (_, lp) (_, rp) _ _ _) = (lp, rp)
862getProvenance _ = (Nothing, Nothing)
864setReady (x, y, _, env, err, c, at) = (x, y, True, env, err, c, at)
866updateProblems :: ProofState -> [(Name, TT Name)] -> Fails
867 -> ([(Name, TT Name)], Fails)
868-- updateProblems ctxt [] ps inj holes = ([], ps)
869updateProblems ps updates probs = rec 10 updates probs
870 where
871 -- keep trying if we make progress, but not too many times...
872 rec 0 us fs = (us, fs)
873 rec n us fs = case up us [] fs of
874 res@(_, []) -> res
875 res@(us', _) | length us' == length us -> res
876 (us', fs') -> rec (n - 1) us' fs'
878 hs = holes ps
879 inj = injective ps
880 ctxt = context ps
881 ulog = unifylog ps
882 usupp = map fst (notunified ps)
883 dont = dontunify ps
885 up ns acc [] = (ns, map (updateNs ns) (reverse acc))
886 up ns acc (prob@(x, y, ready, env, err, while, um) : ps) =
887 let (x', newx) = updateSolvedTerm' ns x
888 (y', newy) = updateSolvedTerm' ns y
889 (lp, rp) = getProvenance err
890 err' = updateError ns err
891 env' = updateEnv ns env in
892 if newx || newy || ready ||
893 any (\n -> n `elem` inj) (refsIn x ++ refsIn y) then
894 case unify ctxt env' (x', lp) (y', rp) inj hs usupp while of
895 OK (v, []) -> traceWhen ulog ("DID " ++ show (x',y',ready,v,dont)) $
896 let v' = filter (\(n, _) -> n `notElem` dont) v in
897 up (ns ++ v') acc ps
898 e -> -- trace ("FAILED " ++ show (x',y',ready,e)) $
899 up ns ((x',y',False,env',err',while,um) : acc) ps
900 else -- trace ("SKIPPING " ++ show (x,y,ready)) $
901 up ns ((x',y', False, env',err', while, um) : acc) ps
903 updateNs ns (x, y, t, env, err, fc, fa)
904 = let (x', newx) = updateSolvedTerm' ns x
905 (y', newy) = updateSolvedTerm' ns y in
906 (x', y', newx || newy,
907 updateEnv ns env, updateError ns err, fc, fa)
909orderUpdateSolved :: [(Name, Term)] -> ProofTerm -> (ProofTerm, [Name])
910orderUpdateSolved ns tm = update [] ns tm
911 where
912 update done [] t = (t, done)
913 update done ((n, P _ n' _) : ns) t | n == n' = update done ns t
914 update done (n : ns) t = update (fst n : done)
915 (map (updateMatch n) ns)
916 (updateSolved [n] t)
918 -- Update the later solutions too
919 updateMatch n (x, tm) = (x, updateSolvedTerm [n] tm)
921-- attempt to solve remaining problems with match_unify
922matchProblems :: Bool -> ProofState -> [(Name, TT Name)] -> Fails
923 -> ([(Name, TT Name)], Fails)
924matchProblems all ps updates probs = up updates probs where
925 hs = holes ps
926 inj = injective ps
927 ctxt = context ps
929 up ns [] = (ns, [])
930 up ns ((x, y, ready, env, err, while, um) : ps)
931 | all || um == Match =
932 let x' = updateSolvedTerm ns x
933 y' = updateSolvedTerm ns y
934 (lp, rp) = getProvenance err
935 err' = updateError ns err
936 env' = updateEnv ns env in
937 case match_unify ctxt env' (x', lp) (y', rp) inj hs while of
938 OK v -> -- trace ("Added " ++ show v ++ " from " ++ show (x', y')) $
939 up (ns ++ v) ps
940 _ -> let (ns', ps') = up ns ps in
941 (ns', (x', y', True, env', err', while, um) : ps')
942 up ns (p : ps) = let (ns', ps') = up ns ps in
943 (ns', p : ps')
945processTactic :: Tactic -> ProofState -> TC (ProofState, String)
946processTactic QED ps = case holes ps of
947 [] -> do let tm = {- normalise (context ps) [] -} getProofTerm (pterm ps)
948 (tm', ty', _) <- recheck (constraint_ns ps) (context ps) [] (forget tm) tm
949 return (ps { done = True, pterm = mkProofTerm tm' },
950 "Proof complete: " ++ showEnv [] tm')
951 _ -> fail "Still holes to fill."
952processTactic ProofState ps = return (ps, showEnv [] (getProofTerm (pterm ps)))
953processTactic Undo ps = case previous ps of
954 Nothing -> Error . Msg $ "Nothing to undo."
955 Just pold -> return (pold, "")
956processTactic EndUnify ps
957 = let (h, ns_in) = unified ps
958 ns = dropGiven (dontunify ps) ns_in (holes ps)
959 ns' = map (\ (n, t) -> (n, updateSolvedTerm ns t)) ns
960 (ns'', probs') = updateProblems ps ns' (problems ps)
961 tm' = updateSolved ns'' (pterm ps) in
962 traceWhen (unifylog ps) ("(EndUnify) Dropping holes: " ++ show (map fst ns'')) $
963 return (ps { pterm = tm',
964 unified = (h, []),
965 problems = probs',
966 notunified = updateNotunified ns'' (notunified ps),
967 recents = recents ps ++ map fst ns'',
968 holes = holes ps \\ map fst ns'' }, "")
969processTactic UnifyAll ps
970 = let tm' = updateSolved (notunified ps) (pterm ps) in
971 return (ps { pterm = tm',
972 notunified = [],
973 recents = recents ps ++ map fst (notunified ps),
974 holes = holes ps \\ map fst (notunified ps) }, "")
975processTactic (Reorder n) ps
976 = do ps' <- execStateT (tactic (Just n) reorder_claims) ps
977 return (ps' { previous = Just ps, plog = "" }, plog ps')
978processTactic (ComputeLet n) ps
979 = return (ps { pterm = mkProofTerm $
980 computeLet (context ps) n
981 (getProofTerm (pterm ps)) }, "")
982processTactic UnifyProblems ps
983 = do let (ns', probs') = updateProblems ps [] (map setReady (problems ps))
984 (pterm', dropped) = orderUpdateSolved ns' (pterm ps)
985 traceWhen (unifylog ps) ("(UnifyProblems) Dropping holes: " ++ show dropped) $
986 return (ps { pterm = pterm', solved = Nothing, problems = probs',
987 previous = Just ps, plog = "",
988 notunified = updateNotunified ns' (notunified ps),
989 recents = recents ps ++ dropped,
990 dotted = filter (notIn ns') (dotted ps),
991 holes = holes ps \\ dropped }, plog ps)
992 where notIn ns (h, _) = h `notElem` map fst ns
993processTactic (MatchProblems all) ps
994 = do let (ns', probs') = matchProblems all ps [] (map setReady (problems ps))
995 (ns'', probs'') = matchProblems all ps ns' probs'
996 (pterm', dropped) = orderUpdateSolved ns'' (resetProofTerm (pterm ps))
997 traceWhen (unifylog ps) ("(MatchProblems) Dropping holes: " ++ show dropped) $
998 return (ps { pterm = pterm', solved = Nothing, problems = probs'',
999 previous = Just ps, plog = "",
1000 notunified = updateNotunified ns'' (notunified ps),
1001 recents = recents ps ++ dropped,
1002 holes = holes ps \\ dropped }, plog ps)
1003processTactic t ps
1004 = case holes ps of
1005 [] -> case t of
1006 Focus _ -> return (ps, "") -- harmless to refocus when done, since
1007 -- 'focus' doesn't fail
1008 _ -> fail $ "Proof done, nothing to run tactic on: " ++ show t ++
1009 "\n" ++ show (getProofTerm (pterm ps))
1010 (h:_) -> do ps' <- execStateT (process t h) ps
1011 let (ns_in, probs')
1012 = case solved ps' of
1013 Just s -> traceWhen (unifylog ps)
1014 ("SOLVED " ++ show s ++ " " ++ show (dontunify ps')) $
1015 updateProblems ps' [s] (problems ps')
1016 _ -> ([], problems ps')
1017 -- rechecking problems may find more solutions, so
1018 -- apply them here
1019 let ns' = dropGiven (dontunify ps') ns_in (holes ps')
1020 let pterm'' = updateSolved ns' (pterm ps')
1021 traceWhen (unifylog ps)
1022 ("Updated problems after solve " ++ qshow probs' ++ "\n" ++
1023 "(Toplevel) Dropping holes: " ++ show (map fst ns') ++ "\n" ++
1024 "Holes were: " ++ show (holes ps')) $
1025 return (ps' { pterm = pterm'',
1026 solved = Nothing,
1027 problems = probs',
1028 notunified = updateNotunified ns' (notunified ps'),
1029 previous = Just ps, plog = "",
1030 recents = recents ps' ++ map fst ns',
1031 holes = holes ps' \\ (map fst ns')
1032 }, plog ps')
1034process :: Tactic -> Name -> StateT TState TC ()
1035process EndUnify _
1036 = do ps <- get
1037 let (h, _) = unified ps
1038 tactic (Just h) solve_unified
1039process t h = tactic (Just h) (mktac t)
1040 where mktac Attack = attack
1041 mktac (Claim n r) = claim n r
1042 mktac (ClaimFn n bn r) = claimFn n bn r
1043 mktac (Exact r) = exact r
1044 mktac (Fill r) = fill r
1045 mktac (MatchFill r) = match_fill r
1046 mktac (PrepFill n ns) = prep_fill n ns
1047 mktac CompleteFill = complete_fill
1048 mktac Solve = solve
1049 mktac (StartUnify n) = start_unify n
1050 mktac Compute = compute
1051 mktac Simplify = Idris.Core.ProofState.simplify
1052 mktac WHNF_Compute = whnf_compute
1053 mktac WHNF_ComputeArgs = whnf_compute_args
1054 mktac (Intro n) = intro n
1055 mktac (IntroTy ty n) = introTy ty n
1056 mktac (Forall n r i t) = forall n r i t
1057 mktac (LetBind n r t v) = letbind n r t v
1058 mktac (ExpandLet n b) = expandLet n b
1059 mktac (Rewrite t) = rewrite t
1060 mktac (Equiv t) = equiv t
1061 mktac (PatVar n) = patvar n
1062 mktac (PatBind n rig) = patbind n rig
1063 mktac (CheckIn r) = check_in r
1064 mktac (EvalIn r) = eval_in r
1065 mktac (Focus n) = focus n
1066 mktac (Defer ns ls n) = defer ns ls n
1067 mktac (DeferType n t a) = deferType n t a
1068 mktac (Implementation n)= implementationArg n
1069 mktac (AutoArg n) = autoArg n
1070 mktac (SetInjective n) = setinj n
1071 mktac (MoveLast n) = movelast n
1072 mktac (UnifyGoal r) = unifyGoal r
1073 mktac (UnifyTerms x y) = unifyTerms x y