@@ -217,7 +217,7 @@ elab ist info emode opts fn tm
217 let fc = maybe "(unknown)"
217 let fc = maybe "(unknown)"
218 elabE initElabCtxt (elabFC info) tm -- (in argument, guarded, in type, in qquote)
218 elabE initElabCtxt (elabFC info) tm -- (in argument, guarded, in type, in qquote)
219 est <- getAux
219 est <- getAux
220− sequence_ (delayed_elab est)
220+ sequence_ (get_delayed_elab est)
221 end_unify
221 end_unify
222 ptm <- get_term
222 ptm <- get_term
223 when pattern -- convert remaining holes to pattern vars
223 when pattern -- convert remaining holes to pattern vars
@@ -230,6 +230,10 @@ elab ist info emode opts fn tm
230 pattern = emode == ELHS
230 pattern = emode == ELHS
231 bindfree = emode == ETyDecl || emode == ELHS
231 bindfree = emode == ETyDecl || emode == ELHS
233+ get_delayed_elab est =
234+ let ds = delayed_elab est in
235+ map snd $ sortBy (\(p1, _) (p2, _) -> compare p1 p2) ds
233 tcgen = Dictionary `elem` opts
237 tcgen = Dictionary `elem` opts
234 reflection = Reflection `elem` opts
238 reflection = Reflection `elem` opts
@@ -287,7 +291,7 @@ elab ist info emode opts fn tm
287 (elab' ina fc' (PApp fc (PRef fc (sUN "Force"))
291 (elab' ina fc' (PApp fc (PRef fc (sUN "Force"))
288 [pimp (sUN "t") Placeholder True,
292 [pimp (sUN "t") Placeholder True,
289 pimp (sUN "a") Placeholder True,
293 pimp (sUN "a") Placeholder True,
290− pexp ct])) True
294+ pexp ct]))
292 forceErr orig env (CantUnify _ (t,_) (t',_) _ _ _)
296 forceErr orig env (CantUnify _ (t,_) (t',_) _ _ _)
293 | (P _ (UN ht) _, _) <- unApply (normalise (tt_ctxt ist) env t),
297 | (P _ (UN ht) _, _) <- unApply (normalise (tt_ctxt ist) env t),
@@ -428,7 +432,7 @@ elab ist info emode opts fn tm
428 [pimp (sMN 0 "a") t False,
432 [pimp (sMN 0 "a") t False,
429 pimp (sMN 0 "P") Placeholder True,
433 pimp (sMN 0 "P") Placeholder True,
430 pexp l, pexp r])
434 pexp l, pexp r])
431− elab' ina fc (PAlternative True as)
435+ elab' ina fc (PAlternative (ExactlyOne delayok) as)
432 = do hnf_compute
436 = do hnf_compute
433 ty <- goal
437 ty <- goal
434 ctxt <- get_context
438 ctxt <- get_context
@@ -438,12 +442,31 @@ elab ist info emode opts fn tm
438-- trace (-- show tc ++ " " ++ show as ++ "\n ==> " ++
442-- trace (-- show tc ++ " " ++ show as ++ "\n ==> " ++
439-- show (length as') ++ "\n" ++
443-- show (length as') ++ "\n" ++
440-- showSep ", " (map showTmImpls as') ++ "\nEND") $
444-- showSep ", " (map showTmImpls as') ++ "\nEND") $
445+ (h : hs) <- get_holes
447+ [x] -> elab' ina fc x
448+ -- If there's options, try now, and if that fails, postpone
450+ _ -> handleError isAmbiguous
441− tryAll (zip (map (elab' ina fc) as') (map showHd as'))
451+ (tryAll (zip (map (elab' ina fc) as')
456+ tryAll (zip (map (elab' ina fc) as')
442 where showHd (PApp _ (PRef _ n) _) = n
458 where showHd (PApp _ (PRef _ n) _) = n
443 showHd (PRef _ n) = n
459 showHd (PRef _ n) = n
444 showHd (PApp _ h _) = showHd h
460 showHd (PApp _ h _) = showHd h
445 showHd x = NErased -- We probably should do something better than this here
461 showHd x = NErased -- We probably should do something better than this here
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
446− elab' ina fc (PAlternative False as)
469+ elab' ina fc (PAlternative FirstSuccess as)
447 = trySeq as
470 = trySeq as
448 where -- if none work, take the error from the first
471 where -- if none work, take the error from the first
449 trySeq (x : xs) = let e1 = elab' ina fc x in
472 trySeq (x : xs) = let e1 = elab' ina fc x in
@@ -1097,9 +1120,9 @@ elab ist info emode opts fn tm
1097 -- Delay dotted things to the end, then when we elaborate them
1120 -- Delay dotted things to the end, then when we elaborate them
1098 -- we can check the result against what was inferred
1121 -- we can check the result against what was inferred
1099 movelast h
1122 movelast h
1100− delayElab $ do focus h
1123+ delayElab 10 $ do focus h
1101− dotterm
1124+ dotterm
1102− elab' ina fc t
1125+ elab' ina fc t
1103 elab' ina fc (PRunElab fc' tm) =
1126 elab' ina fc (PRunElab fc' tm) =
1104 do attack
1127 do attack
1105 n <- getNameFrom (sMN 0 "tacticScript")
1128 n <- getNameFrom (sMN 0 "tacticScript")
@@ -1117,7 +1140,13 @@ elab ist info emode opts fn tm
1118 elab' ina fc x = fail $ "Unelaboratable syntactic form " ++ showTmImpls x
1141 elab' ina fc x = fail $ "Unelaboratable syntactic form " ++ showTmImpls x
1143+ -- delay elaboration of 't', with priority 'pri' until after everything
1145+ -- The delayed things with lower numbered priority will be elaborated
1146+ -- first. (In practice, this means delayed alternatives, then PHidden
1120− delayElab t = updateAux (\e -> e { delayed_elab = delayed_elab e ++ [t] })
1149+ = updateAux (\e -> e { delayed_elab = delayed_elab e ++ [(pri, t)] })
1122 isScr :: PTerm -> (Name, Binder Term) -> (Name, (Bool, Binder Term))
1151 isScr :: PTerm -> (Name, Binder Term) -> (Name, (Bool, Binder Term))
1123 isScr (PRef _ n) (n', b) = (n', (n == n', b))
1152 isScr (PRef _ n) (n', b) = (n', (n == n', b))
@@ -1179,7 +1208,7 @@ elab ist info emode opts fn tm
1179 let tries = if pattern then [t, mkDelay env t] else [mkDelay env t, t]
1208 let tries = if pattern then [t, mkDelay env t] else [mkDelay env t, t]
1180 case tyh of
1209 case tyh of
1181 P _ (UN l) _ | l == txt "Lazy'"
1210 P _ (UN l) _ | l == txt "Lazy'"
1182− -> return (PAlternative False tries)
1211+ -> return (PAlternative FirstSuccess tries)
1183 _ -> return t
1212 _ -> return t
1185 mkDelay env (PAlternative b xs) = PAlternative b (map (mkDelay env) xs)
1214 mkDelay env (PAlternative b xs) = PAlternative b (map (mkDelay env) xs)
@@ -1197,7 +1226,7 @@ elab ist info emode opts fn tm
1197 notImplicitable (PRef _ n)
1226 notImplicitable (PRef _ n)
1198 | [opts] <- lookupCtxt n (idris_flags ist)
1227 | [opts] <- lookupCtxt n (idris_flags ist)
1199 = NoImplicit `elem` opts
1228 = NoImplicit `elem` opts
1200− notImplicitable (PAlternative True as) = any notImplicitable as
1229+ notImplicitable (PAlternative (ExactlyOne _) as) = any notImplicitable as
1201 -- case is tricky enough without implicit coercions! If they are needed,
1230 -- case is tricky enough without implicit coercions! If they are needed,
1202 -- they can go in the branches separately.
1231 -- they can go in the branches separately.
1203 notImplicitable (PCase _ _ _) = True
1232 notImplicitable (PCase _ _ _) = True
@@ -1240,8 +1269,9 @@ elab ist info emode opts fn tm
1240 let t' = case (t, cs) of
1269 let t' = case (t, cs) of
1241 (PCoerced tm, _) -> tm
1270 (PCoerced tm, _) -> tm
1242 (_, []) -> t
1271 (_, []) -> t
1243− (_, cs) -> PAlternative False [t ,
1272+ (_, cs) -> PAlternative FirstSuccess [t ,
1273+ PAlternative (ExactlyOne False)
1244− PAlternative True (map (mkCoerce env t) cs)]
1274+ (map (mkCoerce env t) cs)]
1245 return t'
1275 return t'
1247 mkCoerce env t n = let fc = maybe (fileFC "Coercion") id (highestFC t) in
1277 mkCoerce env t n = let fc = maybe (fileFC "Coercion") id (highestFC t) in