@@ -217,7 +217,7 @@ elab ist info emode opts fn tm
217217 let fc = maybe "(unknown)"
218218 elabE initElabCtxt (elabFC info) tm -- (in argument, guarded, in type, in qquote)
219219 est <- getAux
220− sequence_ (delayed_elab est)
220+ sequence_ (get_delayed_elab est)
221221 end_unify
222222 ptm <- get_term
223223 when pattern -- convert remaining holes to pattern vars
@@ -230,6 +230,10 @@ elab ist info emode opts fn tm
230230 pattern = emode == ELHS
231231 bindfree = emode == ETyDecl || emode == ELHS
232232
233+ get_delayed_elab est =
234+ let ds = delayed_elab est in
235+ map snd $ sortBy (\(p1, _) (p2, _) -> compare p1 p2) ds
236+
233237 tcgen = Dictionary `elem` opts
234238 reflection = Reflection `elem` opts
235239
@@ -287,7 +291,7 @@ elab ist info emode opts fn tm
287291 (elab' ina fc' (PApp fc (PRef fc (sUN "Force"))
288292 [pimp (sUN "t") Placeholder True,
289293 pimp (sUN "a") Placeholder True,
290− pexp ct])) True
294+ pexp ct]))
291295
292296 forceErr orig env (CantUnify _ (t,_) (t',_) _ _ _)
293297 | (P _ (UN ht) _, _) <- unApply (normalise (tt_ctxt ist) env t),
@@ -428,7 +432,7 @@ elab ist info emode opts fn tm
428432 [pimp (sMN 0 "a") t False,
429433 pimp (sMN 0 "P") Placeholder True,
430434 pexp l, pexp r])
431− elab' ina fc (PAlternative True as)
435+ elab' ina fc (PAlternative (ExactlyOne delayok) as)
432436 = do hnf_compute
433437 ty <- goal
434438 ctxt <- get_context
@@ -438,12 +442,31 @@ elab ist info emode opts fn tm
438442-- trace (-- show tc ++ " " ++ show as ++ "\n ==> " ++
439443-- show (length as') ++ "\n" ++
440444-- showSep ", " (map showTmImpls as') ++ "\nEND") $
441− tryAll (zip (map (elab' ina fc) as') (map showHd as'))
445+ (h : hs) <- get_holes
446+ case as' of
447+ [x] -> elab' ina fc x
448+ -- If there's options, try now, and if that fails, postpone
449+ -- to later.
450+ _ -> handleError isAmbiguous
451+ (tryAll (zip (map (elab' ina fc) as')
452+ (map showHd as')))
453+ (do movelast h
454+ delayElab 5 $ do
455+ focus h
456+ tryAll (zip (map (elab' ina fc) as')
457+ (map showHd as')))
442458 where showHd (PApp _ (PRef _ n) _) = n
443459 showHd (PRef _ n) = n
444460 showHd (PApp _ h _) = showHd h
445461 showHd x = NErased -- We probably should do something better than this here
446− elab' ina fc (PAlternative False as)
462+
463+ isAmbiguous (CantResolveAlts _) = delayok
464+ isAmbiguous (Elaborating _ _ e) = isAmbiguous e
465+ isAmbiguous (ElaboratingArg _ _ _ e) = isAmbiguous e
466+ isAmbiguous (At _ e) = isAmbiguous e
467+ isAmbiguous _ = False
468+
469+ elab' ina fc (PAlternative FirstSuccess as)
447470 = trySeq as
448471 where -- if none work, take the error from the first
449472 trySeq (x : xs) = let e1 = elab' ina fc x in
@@ -1097,9 +1120,9 @@ elab ist info emode opts fn tm
10971120 -- Delay dotted things to the end, then when we elaborate them
10981121 -- we can check the result against what was inferred
10991122 movelast h
1100− delayElab $ do focus h
1101− dotterm
1102− elab' ina fc t
1123+ delayElab 10 $ do focus h
1124+ dotterm
1125+ elab' ina fc t
11031126 elab' ina fc (PRunElab fc' tm) =
11041127 do attack
11051128 n <- getNameFrom (sMN 0 "tacticScript")
@@ -1117,7 +1140,13 @@ elab ist info emode opts fn tm
11171140 solve
11181141 elab' ina fc x = fail $ "Unelaboratable syntactic form " ++ showTmImpls x
11191142
1120− delayElab t = updateAux (\e -> e { delayed_elab = delayed_elab e ++ [t] })
1143+ -- delay elaboration of 't', with priority 'pri' until after everything
1144+ -- else is done.
1145+ -- The delayed things with lower numbered priority will be elaborated
1146+ -- first. (In practice, this means delayed alternatives, then PHidden
1147+ -- things.)
1148+ delayElab pri t
1149+ = updateAux (\e -> e { delayed_elab = delayed_elab e ++ [(pri, t)] })
11211150
11221151 isScr :: PTerm -> (Name, Binder Term) -> (Name, (Bool, Binder Term))
11231152 isScr (PRef _ n) (n', b) = (n', (n == n', b))
@@ -1179,7 +1208,7 @@ elab ist info emode opts fn tm
11791208 let tries = if pattern then [t, mkDelay env t] else [mkDelay env t, t]
11801209 case tyh of
11811210 P _ (UN l) _ | l == txt "Lazy'"
1182− -> return (PAlternative False tries)
1211+ -> return (PAlternative FirstSuccess tries)
11831212 _ -> return t
11841213 where
11851214 mkDelay env (PAlternative b xs) = PAlternative b (map (mkDelay env) xs)
@@ -1197,7 +1226,7 @@ elab ist info emode opts fn tm
11971226 notImplicitable (PRef _ n)
11981227 | [opts] <- lookupCtxt n (idris_flags ist)
11991228 = NoImplicit `elem` opts
1200− notImplicitable (PAlternative True as) = any notImplicitable as
1229+ notImplicitable (PAlternative (ExactlyOne _) as) = any notImplicitable as
12011230 -- case is tricky enough without implicit coercions! If they are needed,
12021231 -- they can go in the branches separately.
12031232 notImplicitable (PCase _ _ _) = True
@@ -1240,8 +1269,9 @@ elab ist info emode opts fn tm
12401269 let t' = case (t, cs) of
12411270 (PCoerced tm, _) -> tm
12421271 (_, []) -> t
1243− (_, cs) -> PAlternative False [t ,
1244− PAlternative True (map (mkCoerce env t) cs)]
1272+ (_, cs) -> PAlternative FirstSuccess [t ,
1273+ PAlternative (ExactlyOne False)
1274+ (map (mkCoerce env t) cs)]
12451275 return t'
12461276 where
12471277 mkCoerce env t n = let fc = maybe (fileFC "Coercion") id (highestFC t) in