@@ -112,9 +112,9 @@ build ist info emode opts fn tm
112112 update_term orderPats)
113113 EState is _ impls <- getAux
114114 tt <- get_term
115− let (tm, ds) = runState (collectDeferred (Just fn) tt) []
116− log <- getLog
117115 ctxt <- get_context
116+ let (tm, ds) = runState (collectDeferred (Just fn) ctxt tt) []
117+ log <- getLog
118118 if (log /= "") then trace log $ return (ElabResult tm ds is ctxt impls)
119119 else return (ElabResult tm ds is ctxt impls)
120120 where pattern = emode == ELHS
@@ -154,9 +154,9 @@ buildTC ist info emode opts fn tm
154154 lift (Error (CantMatch (getInferTerm tm)))
155155 EState is _ impls <- getAux
156156 tt <- get_term
157− let (tm, ds) = runState (collectDeferred (Just fn) tt) []
158− log <- getLog
159157 ctxt <- get_context
158+ let (tm, ds) = runState (collectDeferred (Just fn) ctxt tt) []
159+ log <- getLog
160160 if (log /= "") then trace log $ return (ElabResult tm ds is ctxt impls)
161161 else return (ElabResult tm ds is ctxt impls)
162162 where pattern = emode == ELHS
@@ -1547,23 +1547,40 @@ resTC' tcs defaultOn topholes depth topg fn ist
15471547 where isImp (PImp p _ _ _ _) = (True, p)
15481548 isImp arg = (False, priority arg)
15491549
1550−collectDeferred :: Maybe Name ->
1550+collectDeferred :: Maybe Name -> Context ->
15511551 Term -> State [(Name, (Int, Maybe Name, Type))] Term
1552−collectDeferred top (Bind n (GHole i t) app) =
1552+collectDeferred top ctxt (Bind n (GHole i t) app) =
15531553 do ds <- get
1554− t' <- collectDeferred top t
1555− when (not (n `elem` map fst ds)) $ put (ds ++ [(n, (i, top, t'))])
1556− collectDeferred top app
1557−collectDeferred top (Bind n b t) = do b' <- cdb b
1558− t' <- collectDeferred top t
1559− return (Bind n b' t')
1554+ t' <- collectDeferred top ctxt t
1555+ when (not (n `elem` map fst ds)) $ put (ds ++ [(n, (i, top, tidyArg [] t'))])
1556+ collectDeferred top ctxt app
1557+ where
1558+ -- Evaluate the top level functions in arguments, if possible, so that
1559+ -- any immediate specialisation of the function applied to constructors
1560+ -- can be done
1561+ tidyArg env (Bind n b@(Pi im t k) sc)
1562+ = Bind n (Pi im (tidy ctxt env t) k)
1563+ (tidyArg ((n, b) : env) sc)
1564+ tidyArg env t = t
1565+
1566+ tidy ctxt env t | (f, args) <- unApply t,
1567+ P _ n _ <- getFn f
1568+ = fst $ specialise ctxt env [(n, 99999)] t
1569+ tidy ctxt env t = t
1570+
1571+ getFn (Bind n (Lam _) t) = getFn t
1572+ getFn t | (f, a) <- unApply t = f
1573+
1574+collectDeferred top ctxt (Bind n b t) = do b' <- cdb b
1575+ t' <- collectDeferred top ctxt t
1576+ return (Bind n b' t')
15601577 where
1561− cdb (Let t v) = liftM2 Let (collectDeferred top t) (collectDeferred top v)
1562− cdb (Guess t v) = liftM2 Guess (collectDeferred top t) (collectDeferred top v)
1563− cdb b = do ty' <- collectDeferred top (binderTy b)
1578+ cdb (Let t v) = liftM2 Let (collectDeferred top ctxt t) (collectDeferred top ctxt v)
1579+ cdb (Guess t v) = liftM2 Guess (collectDeferred top ctxt t) (collectDeferred top ctxt v)
1580+ cdb b = do ty' <- collectDeferred top ctxt (binderTy b)
15641581 return (b { binderTy = ty' })
1565−collectDeferred top (App s f a) = liftM2 (App s) (collectDeferred top f) (collectDeferred top a)
1566−collectDeferred top t = return t
1582+collectDeferred top ctxt (App s f a) = liftM2 (App s) (collectDeferred top ctxt f) (collectDeferred top ctxt a)
1583+collectDeferred top ctxt t = return t
15671584
15681585case_ :: Bool -> Bool -> IState -> Name -> PTerm -> ElabD ()
15691586case_ ind autoSolve ist fn tm = do