@@ -479,6 +479,66 @@ elab ist info emode opts fn tm
479479 trySeq' deferr (x : xs)
480480 = try' (do elab' ina fc x
481481 solveAutos ist fn False) (trySeq' deferr xs) True
482+ elab' ina fc (PAlternative TryImplicit (orig : alts)) = do
483+ env <- get_env
484+ let doelab = elab' ina fc orig
485+ tryCatch doelab
486+ (\err ->
487+ if recoverableErr err
488+ then -- trace ("NEED IMPLICIT! " ++ show orig ++ "\n" ++
489+ -- show alts ++ "\n" ++
490+ -- showQuick err) $
491+ -- Prune the coercions so that only the ones
492+ -- with the right type to fix the error will be tried!
493+ case pruneAlts err alts env of
494+ [] -> lift $ tfail err
495+ alts' ->
496+ try' (elab' ina fc (PAlternative (ExactlyOne False) alts'))
497+ (lift $ tfail err) -- take error from original if all fail
498+ True
499+ else lift $ tfail err)
500+ where
501+ recoverableErr (CantUnify _ _ _ _ _ _) = True
502+ recoverableErr (CantSolveGoal _ _) = False
503+ recoverableErr (CantResolveAlts _) = False
504+ recoverableErr (ProofSearchFail (Msg _)) = True
505+ recoverableErr (ProofSearchFail _) = False
506+ recoverableErr (ElaboratingArg _ _ _ e) = recoverableErr e
507+ recoverableErr (At _ e) = recoverableErr e
508+ recoverableErr (ElabScriptDebug _ _ _) = False
509+ recoverableErr _ = True
510+
511+ pruneAlts (CantUnify _ (inc, _) (outc, _) _ _ _) alts env
512+ = case unApply (normalise (tt_ctxt ist) env inc) of
513+ (P (TCon _ _) n _, _) -> filter (hasArg n env) alts
514+ (Constant _, _) -> alts
515+ _ -> filter isLend alts -- special case hack for 'Borrowed'
516+ pruneAlts (ElaboratingArg _ _ _ e) alts env = pruneAlts e alts env
517+ pruneAlts (At _ e) alts env = pruneAlts e alts env
518+ pruneAlts _ alts _ = filter isLend alts
519+
520+ hasArg n env ap | isLend ap = True -- special case hack for 'Borrowed'
521+ hasArg n env (PApp _ (PRef _ a) _)
522+ = case lookupTyExact a (tt_ctxt ist) of
523+ Just ty -> let args = map snd (getArgTys (normalise (tt_ctxt ist) env ty)) in
524+ any (fnIs n) args
525+ Nothing -> False
526+ hasArg n _ _ = False
527+
528+ isLend (PApp _ (PRef _ l) _) = l == sNS (sUN "lend") ["Ownership"]
529+ isLend _ = False
530+
531+ fnIs n ty = case unApply ty of
532+ (P _ n' _, _) -> n == n'
533+ _ -> False
534+
535+ showQuick (CantUnify _ (l, _) (r, _) _ _ _)
536+ = show (l, r)
537+ showQuick (ElaboratingArg _ _ _ e) = showQuick e
538+ showQuick (At _ e) = showQuick e
539+ showQuick (ProofSearchFail (Msg _)) = "search fail"
540+ showQuick _ = "No chance"
541+
482542 elab' ina _ (PPatvar fc n) | bindfree
483543 = do patvar n
484544 update_term liftPats
@@ -1299,9 +1359,8 @@ elab ist info emode opts fn tm
12991359 let t' = case (t, cs) of
13001360 (PCoerced tm, _) -> tm
13011361 (_, []) -> t
1302− (_, cs) -> PAlternative FirstSuccess [t ,
1303− PAlternative (ExactlyOne False)
1304− (map (mkCoerce env t) cs)]
1362+ (_, cs) -> PAlternative TryImplicit
1363+ (t : map (mkCoerce env t) cs)
13051364 return t'
13061365 where
13071366 mkCoerce env t n = let fc = maybe (fileFC "Coercion") id (highestFC t) in