@@ -6,7 +6,7 @@
66
77module Idris.Core.ProofState(ProofState(..), newProof, envAtFocus, goalAtFocus,
88 Tactic(..), Goal(..), processTactic, nowElaboratingPS, doneElaboratingAppPS,
9− doneElaboratingArgPS, dropGiven, keepGiven) where
9+ doneElaboratingArgPS, dropGiven, keepGiven, getProvenance) where
1010
1111import Idris.Core.Typecheck
1212import Idris.Core.Evaluate
@@ -154,9 +154,11 @@ holeName i = sMN i "hole"
154154qshow :: Fails -> String
155155qshow fs = show (map (\ (x, y, hs, _, _, _, t) -> (t, x, y, hs)) fs)
156156
157−match_unify' :: Context -> Env -> TT Name -> TT Name ->
157+match_unify' :: Context -> Env ->
158+ (TT Name, Maybe Provenance) ->
159+ (TT Name, Maybe Provenance) ->
158160 StateT TState TC [(Name, TT Name)]
159−match_unify' ctxt env topx topy =
161+match_unify' ctxt env (topx, xfrom) (topy, yfrom) =
160162 do ps <- get
161163 let while = while_elaborating ps
162164 let dont = dontunify ps
@@ -168,7 +170,7 @@ match_unify' ctxt env topx topy =
168170 ++ "\n"
169171 ++ "\n" ++ show (getProofTerm (pterm ps)) ++ "\n\n"
170172 ) $
171− case match_unify ctxt env topx topy inj (holes ps) while of
173+ case match_unify ctxt env (topx, xfrom) (topy, yfrom) inj (holes ps) while of
172174 OK u -> traceWhen (unifylog ps)
173175 ("Matched " ++ show u) $
174176 do let (h, ns) = unified ps
@@ -196,14 +198,16 @@ mergeSolutions env ns = merge [] ns
196198 = do ps <- get
197199 let probs = problems ps
198200 put (ps { problems = probs ++ [(t,t',True,env,
199− CantUnify True t t' (Msg "") (errEnv env) 0,
201+ CantUnify True (t, Nothing) (t', Nothing) (Msg "") (errEnv env) 0,
200202 [], Unify)] })
201203 merge acc ns
202204 | otherwise = merge ((n, t): acc) ns
203205
204−unify' :: Context -> Env -> TT Name -> TT Name ->
206+unify' :: Context -> Env ->
207+ (TT Name, Maybe Provenance) ->
208+ (TT Name, Maybe Provenance) ->
205209 StateT TState TC [(Name, TT Name)]
206−unify' ctxt env topx topy =
210+unify' ctxt env (topx, xfrom) (topy, yfrom) =
207211 do ps <- get
208212 let while = while_elaborating ps
209213 let dont = dontunify ps
@@ -216,7 +220,7 @@ unify' ctxt env topx topy =
216220 "\nHoles: " ++ show (holes ps)
217221 ++ "\nInjective: " ++ show (injective ps)
218222 ++ "\n") $
219− lift $ unify ctxt env topx topy inj (holes ps)
223+ lift $ unify ctxt env (topx, xfrom) (topy, yfrom) inj (holes ps)
220224 (map fst (notunified ps)) while
221225 let notu = filter (\ (n, t) -> case t of
222226-- P _ _ _ -> False
@@ -456,7 +460,7 @@ regret ctxt env (Bind x (Hole t) _)
456460unifyGoal :: Raw -> RunTactic
457461unifyGoal tm ctxt env h@(Bind x b sc) =
458462 do (tmv, _) <- lift $ check ctxt env tm
459− ns' <- unify' ctxt env (binderTy b) tmv
463+ ns' <- unify' ctxt env (binderTy b, Nothing) (tmv, Nothing)
460464 return h
461465
462466exact :: Raw -> RunTactic
@@ -473,7 +477,8 @@ fill guess ctxt env (Bind x (Hole ty) sc) =
473477 do (val, valty) <- lift $ check ctxt env guess
474478-- let valtyn = normalise ctxt env valty
475479-- let tyn = normalise ctxt env ty
476− ns <- unify' ctxt env valty ty
480+ ns <- unify' ctxt env (valty, Just $ SourceTerm val)
481+ (ty, Just ExpectedType)
477482 ps <- get
478483 let (uh, uns) = unified ps
479484-- put (ps { unified = (uh, uns ++ ns) })
@@ -488,7 +493,7 @@ match_fill guess ctxt env (Bind x (Hole ty) sc) =
488493 do (val, valty) <- lift $ check ctxt env guess
489494-- let valtyn = normalise ctxt env valty
490495-- let tyn = normalise ctxt env ty
491− ns <- match_unify' ctxt env valty ty
496+ ns <- match_unify' ctxt env (valty, Nothing) (ty, Nothing)
492497 ps <- get
493498 let (uh, uns) = unified ps
494499-- put (ps { unified = (uh, uns ++ ns) })
@@ -506,7 +511,7 @@ complete_fill :: RunTactic
506511complete_fill ctxt env (Bind x (Guess ty val) sc) =
507512 do let guess = forget val
508513 (val', valty) <- lift $ check ctxt env guess
509− ns <- unify' ctxt env valty ty
514+ ns <- unify' ctxt env (valty, Nothing) (ty, Nothing)
510515 ps <- get
511516 let (uh, uns) = unified ps
512517-- put (ps { unified = (uh, uns ++ ns) })
@@ -524,7 +529,7 @@ solve ctxt env (Bind x (Guess ty val) sc)
524529 dropdots <-
525530 case lookup x (notunified ps) of
526531 Just tm -> -- trace ("NEED MATCH: " ++ show (x, tm, val) ++ "\nIN " ++ show (pterm ps)) $
527− do match_unify' ctxt env tm val
532+ do match_unify' ctxt env (tm, Nothing) (val, Nothing)
528533 return [x]
529534 _ -> return []
530535 action (\ps -> ps { holes = traceWhen (unifylog ps) ("Dropping hole " ++ show x) $
@@ -567,7 +572,7 @@ introTy ty mn ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
567572-- ns <- lift $ unify ctxt env tyv t'
568573 case t' of
569574 Bind y (Pi _ s _) t -> let t' = subst y (P Bound n s) t in
570− do ns <- unify' ctxt env s tyv
575+ do ns <- unify' ctxt env (s, Nothing) (tyv, Nothing)
571576 ps <- get
572577 let (uh, uns) = unified ps
573578-- put (ps { unified = (uh, uns ++ ns) })
@@ -593,8 +598,8 @@ intro n ctxt env _ = fail "Can't introduce here."
593598forall :: Name -> Maybe ImplicitInfo -> Raw -> RunTactic
594599forall n impl ty ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
595600 do (tyv, tyt) <- lift $ check ctxt env ty
596− unify' ctxt env tyt (TType (UVar 0))
597− unify' ctxt env t (TType (UVar 0))
601+ unify' ctxt env (tyt, Nothing) (TType (UVar 0), Nothing)
602+ unify' ctxt env (t, Nothing) (TType (UVar 0), Nothing)
598603 return $ Bind n (Pi impl tyv (TType (UVar 0))) (Bind x (Hole t) (P Bound x t))
599604forall n impl ty ctxt env _ = fail "Can't pi bind here"
600605
@@ -822,8 +827,8 @@ updateError ns (At f e) = At f (updateError ns e)
822827updateError ns (Elaborating s n e) = Elaborating s n (updateError ns e)
823828updateError ns (ElaboratingArg f a env e)
824829 = ElaboratingArg f a env (updateError ns e)
825−updateError ns (CantUnify b l r e xs sc)
826− = CantUnify b (updateSolvedTerm ns l) (updateSolvedTerm ns r) (updateError ns e) xs sc
830+updateError ns (CantUnify b (l,lp) (r,rp) e xs sc)
831+ = CantUnify b (updateSolvedTerm ns l, lp) (updateSolvedTerm ns r, rp) (updateError ns e) xs sc
827832updateError ns e = e
828833
829834solveInProblems x val [] = []
@@ -837,7 +842,7 @@ mergeNotunified env holes ns = mnu ns [] [] where
837842 mnu ((n, t):ns) ns_acc ps_acc
838843 | Just t' <- lookup n ns, t /= t'
839844 = mnu ns ((n,t') : ns_acc)
840− ((t,t',True, env,CantUnify True t t' (Msg "") [] 0, [],Match) : ps_acc)
845+ ((t,t',True, env,CantUnify True (t, Nothing) (t', Nothing) (Msg "") [] 0, [],Match) : ps_acc)
841846 | otherwise = mnu ns ((n,t) : ns_acc) ps_acc
842847
843848updateNotunified [] nu = nu
@@ -846,6 +851,10 @@ updateNotunified ns nu = up nu where
846851 up ((n, t) : nus) = let t' = updateSolvedTerm ns t in
847852 ((n, t') : up nus)
848853
854+getProvenance :: Err -> (Maybe Provenance, Maybe Provenance)
855+getProvenance (CantUnify _ (_, lp) (_, rp) _ _ _) = (lp, rp)
856+getProvenance _ = (Nothing, Nothing)
857+
849858updateProblems :: ProofState -> [(Name, TT Name)] -> Fails
850859 -> ([(Name, TT Name)], Fails)
851860-- updateProblems ctxt [] ps inj holes = ([], ps)
@@ -860,11 +869,12 @@ updateProblems ps updates probs = up updates probs where
860869 up ns (prob@(x, y, ready, env, err, while, um) : ps) =
861870 let (x', newx) = updateSolvedTerm' ns x
862871 (y', newy) = updateSolvedTerm' ns y
872+ (lp, rp) = getProvenance err
863873 err' = updateError ns err
864874 env' = updateEnv ns env in
865875 if newx || newy || ready ||
866876 any (\n -> n `elem` inj) (refsIn x ++ refsIn y) then
867− case unify ctxt env' x' y' inj hs usupp while of
877+ case unify ctxt env' (x', lp) (y', rp) inj hs usupp while of
868878 OK (v, []) -> traceWhen ulog ("DID " ++ show (x',y',ready,v)) $
869879 up (ns ++ v) ps
870880 e -> -- trace ("FAILED " ++ show (x',y',ready,e)) $
@@ -887,9 +897,10 @@ matchProblems all ps updates probs = up updates probs where
887897 | all || um == Match =
888898 let x' = updateSolvedTerm ns x
889899 y' = updateSolvedTerm ns y
900+ (lp, rp) = getProvenance err
890901 err' = updateError ns err
891902 env' = updateEnv ns env in
892− case match_unify ctxt env' x' y' inj hs while of
903+ case match_unify ctxt env' (x', lp) (y', rp) inj hs while of
893904 OK v -> -- trace ("Added " ++ show v ++ " from " ++ show (x', y')) $
894905 up (ns ++ v) ps
895906 _ -> let (ns', ps') = up ns ps in