@@ -2204,7 +2204,7 @@ stripUnmatchable i (PApp fc fn args) = PApp fc fn (fmap (fmap su) args) where
22042204 su (PAlternative ms b alts)
22052205 = let alts' = filter (/= Placeholder) (map su alts) in
22062206 if null alts' then Placeholder
2207− else PAlternative ms b alts'
2207+ else liftHidden $ PAlternative ms b alts'
22082208 su (PPair fc hls p l r) = PPair fc hls p (su l) (su r)
22092209 su (PDPair fc hls p l t r) = PDPair fc hls p (su l) (su t) (su r)
22102210 su t@(PLam fc _ _ _ _) = PHidden t
@@ -2214,6 +2214,20 @@ stripUnmatchable i (PApp fc fn args) = PApp fc fn (fmap (fmap su) args) where
22142214
22152215 ctxt = tt_ctxt i
22162216
2217+ -- If the ambiguous terms are all hidden, the PHidden needs to be outside
2218+ -- because elaboration of PHidden gets delayed, and we need the elaboration
2219+ -- to resolve the ambiguity.
2220+ liftHidden tm@(PAlternative ms b as)
2221+ | allHidden as = PHidden (PAlternative ms b (map unHide as))
2222+ | otherwise = tm
2223+
2224+ allHidden [] = True
2225+ allHidden (PHidden _ : xs) = allHidden xs
2226+ allHidden (x : xs) = False
2227+
2228+ unHide (PHidden t) = t
2229+ unHide t = t
2230+
22172231stripUnmatchable i tm = tm
22182232
22192233mkPApp fc a f [] = f