@@ -112,9 +112,9 @@ build ist info emode opts fn tm
112 update_term orderPats)
112 update_term orderPats)
113 EState is _ impls <- getAux
113 EState is _ impls <- getAux
114 tt <- get_term
114 tt <- get_term
115− let (tm, ds) = runState (collectDeferred (Just fn) tt) []
117 ctxt <- get_context
115 ctxt <- get_context
116+ let (tm, ds) = runState (collectDeferred (Just fn) ctxt tt) []
118 if (log /= "") then trace log $ return (ElabResult tm ds is ctxt impls)
118 if (log /= "") then trace log $ return (ElabResult tm ds is ctxt impls)
119 else return (ElabResult tm ds is ctxt impls)
119 else return (ElabResult tm ds is ctxt impls)
120 where pattern = emode == ELHS
120 where pattern = emode == ELHS
@@ -154,9 +154,9 @@ buildTC ist info emode opts fn tm
154 lift (Error (CantMatch (getInferTerm tm)))
154 lift (Error (CantMatch (getInferTerm tm)))
155 EState is _ impls <- getAux
155 EState is _ impls <- getAux
156 tt <- get_term
156 tt <- get_term
157− let (tm, ds) = runState (collectDeferred (Just fn) tt) []
159 ctxt <- get_context
157 ctxt <- get_context
158+ let (tm, ds) = runState (collectDeferred (Just fn) ctxt tt) []
160 if (log /= "") then trace log $ return (ElabResult tm ds is ctxt impls)
160 if (log /= "") then trace log $ return (ElabResult tm ds is ctxt impls)
161 else return (ElabResult tm ds is ctxt impls)
161 else return (ElabResult tm ds is ctxt impls)
162 where pattern = emode == ELHS
162 where pattern = emode == ELHS
@@ -1547,23 +1547,40 @@ resTC' tcs defaultOn topholes depth topg fn ist
1547 where isImp (PImp p _ _ _ _) = (True, p)
1547 where isImp (PImp p _ _ _ _) = (True, p)
1548 isImp arg = (False, priority arg)
1548 isImp arg = (False, priority arg)
1550−collectDeferred :: Maybe Name ->
1550+collectDeferred :: Maybe Name -> Context ->
1551 Term -> State [(Name, (Int, Maybe Name, Type))] Term
1551 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) =
1553 do ds <- get
1553 do ds <- get
1554− t' <- collectDeferred top t
1554+ t' <- collectDeferred top ctxt t
1555− when (not (n `elem` map fst ds)) $ put (ds ++ [(n, (i, top, t'))])
1555+ when (not (n `elem` map fst ds)) $ put (ds ++ [(n, (i, top, tidyArg [] t'))])
1556− collectDeferred top app
1556+ collectDeferred top ctxt app
1558+ -- Evaluate the top level functions in arguments, if possible, so that
1559+ -- any immediate specialisation of the function applied to constructors
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)
1566+ tidy ctxt env t | (f, args) <- unApply t,
1568+ = fst $ specialise ctxt env [(n, 99999)] t
1569+ tidy ctxt env t = t
1571+ getFn (Bind n (Lam _) t) = getFn t
1572+ getFn t | (f, a) <- unApply t = f
1557−collectDeferred top (Bind n b t) = do b' <- cdb b
1574+collectDeferred top ctxt (Bind n b t) = do b' <- cdb b
1558− t' <- collectDeferred top t
1575+ t' <- collectDeferred top ctxt t
1559− return (Bind n b' t')
1576+ return (Bind n b' t')
1561− cdb (Let t v) = liftM2 Let (collectDeferred top t) (collectDeferred top v)
1578+ cdb (Let t v) = liftM2 Let (collectDeferred top ctxt t) (collectDeferred top ctxt v)
1562− cdb (Guess t v) = liftM2 Guess (collectDeferred top t) (collectDeferred top v)
1579+ cdb (Guess t v) = liftM2 Guess (collectDeferred top ctxt t) (collectDeferred top ctxt v)
1563− cdb b = do ty' <- collectDeferred top (binderTy b)
1580+ cdb b = do ty' <- collectDeferred top ctxt (binderTy b)
1564 return (b { binderTy = ty' })
1581 return (b { binderTy = ty' })
1565−collectDeferred top (App s f a) = liftM2 (App s) (collectDeferred top f) (collectDeferred top a)
1582+collectDeferred top ctxt (App s f a) = liftM2 (App s) (collectDeferred top ctxt f) (collectDeferred top ctxt a)
1566−collectDeferred top t = return t
1583+collectDeferred top ctxt t = return t
1568case_ :: Bool -> Bool -> IState -> Name -> PTerm -> ElabD ()
1585case_ :: Bool -> Bool -> IState -> Name -> PTerm -> ElabD ()
1569case_ ind autoSolve ist fn tm = do
1586case_ ind autoSolve ist fn tm = do