@@ -120,7 +120,9 @@ elab ist info pattern tcgen fn tm
120120 elabE ina t = {- do g <- goal
121121 tm <- get_term
122122 trace ("Elaborating " ++ show t ++ " : " ++ show g ++ "\n\tin " ++ show tm)
123− $ -} elab' ina t
123+ $ -}
124+ do t' <- insertCoerce ina t
125+ elab' ina t'
124126
125127 local f = do e <- get_env
126128 return (f `elem` map fst e)
@@ -191,9 +193,10 @@ elab ist info pattern tcgen fn tm
191193 = trySeq as
192194 where -- if none work, take the error from the first
193195 trySeq (x : xs) = let e1 = elab' ina x in
194− try e1 (trySeq' e1 xs)
195− trySeq' deferr [] = deferr
196− trySeq' deferr (x : xs) = try (elab' ina x) (trySeq' deferr xs)
196+ try' e1 (trySeq' e1 xs) True
197+ trySeq' deferr [] = proofFail deferr
198+ trySeq' deferr (x : xs)
199+ = try' (elab' ina x) (trySeq' deferr xs) True
197200 elab' ina (PPatvar fc n) | pattern = patvar n
198201 elab' (ina, guarded) (PRef fc n) | pattern && not (inparamBlock n)
199202 = do ctxt <- get_context
@@ -348,7 +351,7 @@ elab ist info pattern tcgen fn tm
348351 compare (priority x) (priority y))
349352 (zip ns args)
350353 elabArgs (ina || not isinf, guarded)
351− [] False ns' (map (\x -> (lazyarg x, getTm x)) eargs)
354+ [] fc False ns' (map (\x -> (lazyarg x, getTm x)) eargs)
352355 mkSpecialised ist fc f (map getTm args') tm
353356 solve
354357 ptm <- get_term
@@ -442,14 +445,29 @@ elab ist info pattern tcgen fn tm
442445 shadowed new (PRef _ n) | n `elem` new = Placeholder
443446 shadowed new t = t
444447
445− elabArgs ina failed retry [] _
448+ insertCoerce ina t =
449+ do ty <- goal
450+ -- Check for possible coercions to get to the goal
451+ -- and add them as 'alternatives'
452+ let cs = getCoercionsTo ist ty
453+ let t' = case (t, cs) of
454+ (PCoerced tm, _) -> tm
455+ (_, []) -> t
456+ (_, cs) -> PAlternative False
457+ (t : map (mkCoerce t) cs)
458+ return t'
459+ where
460+ mkCoerce t n = let fc = FC "Coercion" 0 in -- line never appears!
461+ addImpl ist (PApp fc (PRef fc n) [pexp (PCoerced t)])
462+
463+ elabArgs ina failed fc retry [] _
446464-- | retry = let (ns, ts) = unzip (reverse failed) in
447465-- elabArgs ina [] False ns ts
448466 | otherwise = return ()
449− elabArgs ina failed r (n:ns) ((_, Placeholder) : args)
450− = elabArgs ina failed r ns args
451− elabArgs ina failed r (n:ns) ((lazy, t) : args)
452− | lazy && not pattern
467+ elabArgs ina failed fc r (n:ns) ((_, Placeholder) : args)
468+ = elabArgs ina failed fc r ns args
469+ elabArgs ina failed fc r (n:ns) ((lazy, t) : args)
470+ | lazy && not pattern
453471 = do elabArg n (PApp bi (PRef bi (UN "lazy"))
454472 [pimp (UN "a") Placeholder,
455473 pexp t]);
@@ -457,15 +475,17 @@ elab ist info pattern tcgen fn tm
457475 where elabArg n t
458476 = do hs <- get_holes
459477 tm <- get_term
478+ t' <- insertCoerce ina t
460479 failed' <- -- trace (show (n, t, hs, tm)) $
480+ -- traceWhen (not (null cs)) (show ty ++ "\n" ++ showImp True t) $
461481 case n `elem` hs of
462482 True ->
463483-- if r
464484-- then try (do focus n; elabE ina t; return failed)
465485-- (return ((n,(lazy, t)):failed))
466− do focus n; elabE ina t; return failed
486+ do focus n; elabE ina t'; return failed
467487 False -> return failed
468− elabArgs ina failed r ns args
488+ elabArgs ina failed fc r ns args
469489
470490-- For every alternative, look at the function at the head. Automatically resolve
471491-- any nested alternatives where that function is also at the head