@@ -164,13 +164,14 @@ getUnmatchable ctxt n | isDConName n ctxt && n /= inferCon
164164getUnmatchable ctxt n = []
165165
166166data ElabCtxt = ElabCtxt { e_inarg :: Bool,
167+ e_isfn :: Bool, -- ^ Function part of application
167168 e_guarded :: Bool,
168169 e_intype :: Bool,
169170 e_qq :: Bool,
170171 e_nomatching :: Bool -- ^ can't pattern match
171172 }
172173
173−initElabCtxt = ElabCtxt False False False False False
174+initElabCtxt = ElabCtxt False False False False False False
174175
175176goal_polymorphic :: ElabD Bool
176177goal_polymorphic =
@@ -244,7 +245,7 @@ elab ist info emode opts fn tm
244245 then solveAuto ist fn False a
245246 else return ()) as
246247
247− itm <- if not pattern then insertImpLam t else return t
248+ itm <- if not pattern then insertImpLam ina t else return t
248249 ct <- insertCoerce ina itm
249250 t' <- insertLazy ct
250251 g <- goal
@@ -596,17 +597,13 @@ elab ist info emode opts fn tm
596597 _ -> lift $ tfail (NoSuchVariable fn)
597598 ns <- match_apply (Var fn') (map (\x -> (x,0)) imps)
598599 solve
599− -- This isn't a sound way of checking for Typecase - we need a
600− -- better way!
601−-- elab' (_, _, inty, qq) (PApp fc (PRef _ f) args')
602−-- | isTConName f (tt_ctxt ist) && pattern && not reflection && not inty && not qq
603−-- = lift $ tfail (Msg "Typecase is not allowed")
604600 -- if f is local, just do a simple_app
605− elab' ina _ tm@(PApp fc (PRef _ f) args_in)
601+ -- FIXME: Anyone feel like refactoring this mess? - EB
602+ elab' ina topfc tm@(PApp fc (PRef _ f) args_in)
606603 | pattern && not reflection && e_nomatching ina
607604 = lift $ tfail $ Msg ("Attempting concrete match on polymorphic argument: " ++ show tm)
608− | otherwise
609− = do env <- get_env
605+ | otherwise = implicitApp $
606+ do env <- get_env
610607 ty <- goal
611608 fty <- get_type (Var f)
612609 ctxt <- get_context
@@ -614,18 +611,18 @@ elab ist info emode opts fn tm
614611 let unmatchableArgs = if pattern
615612 then getUnmatchable (tt_ctxt ist) f
616613 else []
617− -- Dot it if we're in a pattern and it isn't a constructor
618−-- when (pattern && not (isDConName f ctxt) && f /= fn) $ dotterm
619−
614+-- trace ("BEFORE " ++ show f ++ ": " ++ show ty) $
620615 when (pattern && not reflection && not (e_qq ina) && not (e_intype ina)
621616 && isTConName f (tt_ctxt ist)) $
622617 lift $ tfail $ Msg ("No explicit types on left hand side: " ++ show tm)
623618 if (f `elem` map fst env && length args == 1 && length args_in == 1)
624619 then -- simple app, as below
625− do simple_app False (elabE ina (Just fc) (PRef fc f))
620+ do simple_app False
621+ (elabE (ina { e_isfn = True }) (Just fc) (PRef fc f))
626622 (elabE (ina { e_inarg = True }) (Just fc) (getTm (head args)))
627623 (show tm)
628624 solve
625+ return []
629626 else
630627 do ivs <- get_instances
631628 ps <- get_probs
@@ -656,22 +653,58 @@ elab ist info emode opts fn tm
656653 (zip ns' (unmatchableArgs ++ repeat False))
657654 (f == sUN "Force")
658655 (map (\x -> getTm x) eargs) -- TODO: remove this False arg
659− solve
660− ivs' <- get_instances
661− -- Attempt to resolve any type classes which have 'complete' types,
662− -- i.e. no holes in them
663− when (not pattern || (e_inarg ina && not tcgen &&
664− not (e_guarded ina))) $
665− mapM_ (\n -> do focus n
666− g <- goal
667− env <- get_env
668− hs <- get_holes
669− if all (\n -> not (n `elem` hs)) (freeNames g)
670− then try (resolveTC False 7 g fn ist)
671− (movelast n)
672− else movelast n)
673− (ivs' \\ ivs)
674− where -- normal < alternatives < lambdas < rewrites < tactic < default tactic
656+ imp <- if (e_isfn ina) then
657+ do guess <- get_guess
658+ gty <- get_type (forget guess)
659+ env <- get_env
660+ let ty_n = normalise ctxt env gty
661+ return $ getReqImps ty_n
662+ else return []
663+ -- Now we find out how many implicits we needed at the
664+ -- end of the application by looking at the goal again
665+ -- - Have another go, but this time add the
666+ -- implicits (can't think of a better way than this...)
667+ case imp of
668+ rs@(_:_) | not pattern -> return rs -- quit, try again
669+ _ -> do solve
670+ hs <- get_holes
671+ ivs' <- get_instances
672+ -- Attempt to resolve any type classes which have 'complete' types,
673+ -- i.e. no holes in them
674+ when (not pattern || (e_inarg ina && not tcgen &&
675+ not (e_guarded ina))) $
676+ mapM_ (\n -> do focus n
677+ g <- goal
678+ env <- get_env
679+ hs <- get_holes
680+ if all (\n -> not (n `elem` hs)) (freeNames g)
681+ then try (resolveTC False 7 g fn ist)
682+ (movelast n)
683+ else movelast n)
684+ (ivs' \\ ivs)
685+ return []
686+ where
687+ -- Run the elaborator, which returns how many implicit
688+ -- args were needed, then run it again with those args. We need
689+ -- this because we have to elaborate the whole application to
690+ -- find out whether any computations have caused more implicits
691+ -- to be needed.
692+ implicitApp :: ElabD [ImplicitInfo] -> ElabD ()
693+ implicitApp elab
694+ | pattern = do elab; return ()
695+ | otherwise
696+ = do s <- get
697+ imps <- elab
698+ case imps of
699+ [] -> return ()
700+ es -> do put s
701+ elab' ina topfc (PAppImpl tm es)
702+
703+ getReqImps (Bind x (Pi (Just i) ty _) sc)
704+ = i : getReqImps sc
705+ getReqImps _ = []
706+
707+ -- normal < alternatives < lambdas < rewrites < tactic < default tactic
675708 -- reason for lambdas after alternatives is that having
676709 -- the alternative resolved can help with typechecking the lambda
677710 -- or the rewrite. Rewrites/tactics need as much information
@@ -744,15 +777,24 @@ elab ist info emode opts fn tm
744777 setInjective (PApp _ (PRef _ n) _) = setinj n
745778 setInjective _ = return ()
746779
747− elab' ina _ tm@(PApp fc f [arg])
748− = erun fc $
780+ elab' ina _ tm@(PApp fc f [arg]) =
781+ erun fc $
749782 do simple_app (not $ headRef f)
750− (elabE ina (Just fc) f) (elabE (ina { e_inarg = True }) (Just fc) (getTm arg))
783+ (elabE (ina { e_isfn = True }) (Just fc) f)
784+ (elabE (ina { e_inarg = True }) (Just fc) (getTm arg))
751785 (show tm)
752786 solve
753787 where headRef (PRef _ _) = True
754788 headRef (PApp _ f _) = headRef f
755789 headRef _ = False
790+
791+ elab' ina fc (PAppImpl f es) = do appImpl (reverse es) -- not that we look...
792+ solve
793+ where appImpl [] = elab' (ina { e_isfn = False }) fc f -- e_isfn not set, so no recursive expansion of implicits
794+ appImpl (e : es) = simple_app False
795+ (appImpl es)
796+ (elab' ina fc Placeholder)
797+ (show f)
756798 elab' ina fc Placeholder
757799 = do (h : hs) <- get_holes
758800 movelast h
@@ -1072,16 +1114,20 @@ elab ist info emode opts fn tm
10721114 = x : insertScopedImps fc sc xs
10731115 insertScopedImps _ _ xs = xs
10741116
1075− insertImpLam t =
1117+ insertImpLam ina t =
10761118 do ty <- goal
10771119 env <- get_env
10781120 let ty' = normalise (tt_ctxt ist) env ty
10791121 addLam ty' t
10801122 where
10811123 -- just one level at a time
1082− addLam (Bind n (Pi (Just _) _ _) sc) t
1083− = do impn <- unique_hole (sMN 0 "imp")
1084− return (PLam emptyFC impn Placeholder t)
1124+ addLam (Bind n (Pi (Just _) _ _) sc) t =
1125+ do impn <- unique_hole (sMN 0 "imp")
1126+ if e_isfn ina -- apply to an implicit immediately
1127+ then return (PApp emptyFC
1128+ (PLam emptyFC impn Placeholder t)
1129+ [pexp Placeholder])
1130+ else return (PLam emptyFC impn Placeholder t)
10851131 addLam _ t = return t
10861132
10871133 insertCoerce ina t@(PCase _ _ _) = return t