@@ -164,13 +164,14 @@ getUnmatchable ctxt n | isDConName n ctxt && n /= inferCon
164getUnmatchable ctxt n = []
164getUnmatchable ctxt n = []
166data ElabCtxt = ElabCtxt { e_inarg :: Bool,
166data ElabCtxt = ElabCtxt { e_inarg :: Bool,
167+ e_isfn :: Bool, -- ^ Function part of application
167 e_guarded :: Bool,
168 e_guarded :: Bool,
168 e_intype :: Bool,
169 e_intype :: Bool,
169 e_qq :: Bool,
170 e_qq :: Bool,
170 e_nomatching :: Bool -- ^ can't pattern match
171 e_nomatching :: Bool -- ^ can't pattern match
173−initElabCtxt = ElabCtxt False False False False False
174+initElabCtxt = ElabCtxt False False False False False False
175goal_polymorphic :: ElabD Bool
176goal_polymorphic :: ElabD Bool
176goal_polymorphic =
177goal_polymorphic =
@@ -244,7 +245,7 @@ elab ist info emode opts fn tm
244 then solveAuto ist fn False a
245 then solveAuto ist fn False a
245 else return ()) as
246 else return ()) as
247− itm <- if not pattern then insertImpLam t else return t
248+ itm <- if not pattern then insertImpLam ina t else return t
248 ct <- insertCoerce ina itm
249 ct <- insertCoerce ina itm
249 t' <- insertLazy ct
250 t' <- insertLazy ct
250 g <- goal
251 g <- goal
@@ -596,17 +597,13 @@ elab ist info emode opts fn tm
596 _ -> lift $ tfail (NoSuchVariable fn)
597 _ -> lift $ tfail (NoSuchVariable fn)
597 ns <- match_apply (Var fn') (map (\x -> (x,0)) imps)
598 ns <- match_apply (Var fn') (map (\x -> (x,0)) imps)
599− -- This isn't a sound way of checking for Typecase - we need a
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")
604 -- if f is local, just do a simple_app
600 -- if f is local, just do a simple_app
601+ -- FIXME: Anyone feel like refactoring this mess? - EB
605− elab' ina _ tm@(PApp fc (PRef _ f) args_in)
602+ elab' ina topfc tm@(PApp fc (PRef _ f) args_in)
606 | pattern && not reflection && e_nomatching ina
603 | pattern && not reflection && e_nomatching ina
607 = lift $ tfail $ Msg ("Attempting concrete match on polymorphic argument: " ++ show tm)
604 = lift $ tfail $ Msg ("Attempting concrete match on polymorphic argument: " ++ show tm)
608− | otherwise
605+ | otherwise = implicitApp $
609− = do env <- get_env
606+ do env <- get_env
610 ty <- goal
607 ty <- goal
611 fty <- get_type (Var f)
608 fty <- get_type (Var f)
612 ctxt <- get_context
609 ctxt <- get_context
@@ -614,18 +611,18 @@ elab ist info emode opts fn tm
614 let unmatchableArgs = if pattern
611 let unmatchableArgs = if pattern
615 then getUnmatchable (tt_ctxt ist) f
612 then getUnmatchable (tt_ctxt ist) f
617− -- Dot it if we're in a pattern and it isn't a constructor
614+-- trace ("BEFORE " ++ show f ++ ": " ++ show ty) $
618−-- when (pattern && not (isDConName f ctxt) && f /= fn) $ dotterm
620 when (pattern && not reflection && not (e_qq ina) && not (e_intype ina)
615 when (pattern && not reflection && not (e_qq ina) && not (e_intype ina)
621 && isTConName f (tt_ctxt ist)) $
616 && isTConName f (tt_ctxt ist)) $
622 lift $ tfail $ Msg ("No explicit types on left hand side: " ++ show tm)
617 lift $ tfail $ Msg ("No explicit types on left hand side: " ++ show tm)
623 if (f `elem` map fst env && length args == 1 && length args_in == 1)
618 if (f `elem` map fst env && length args == 1 && length args_in == 1)
624 then -- simple app, as below
619 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))
626 (elabE (ina { e_inarg = True }) (Just fc) (getTm (head args)))
622 (elabE (ina { e_inarg = True }) (Just fc) (getTm (head args)))
627 (show tm)
623 (show tm)
630 do ivs <- get_instances
627 do ivs <- get_instances
631 ps <- get_probs
628 ps <- get_probs
@@ -656,22 +653,58 @@ elab ist info emode opts fn tm
656 (zip ns' (unmatchableArgs ++ repeat False))
653 (zip ns' (unmatchableArgs ++ repeat False))
657 (f == sUN "Force")
654 (f == sUN "Force")
658 (map (\x -> getTm x) eargs) -- TODO: remove this False arg
655 (map (\x -> getTm x) eargs) -- TODO: remove this False arg
656+ imp <- if (e_isfn ina) then
657+ do guess <- get_guess
658+ gty <- get_type (forget guess)
660+ let ty_n = normalise ctxt env gty
661+ return $ getReqImps ty_n
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...)
668+ rs@(_:_) | not pattern -> return rs -- quit, try again
659− solve
669+ _ -> do solve
660− ivs' <- get_instances
671+ ivs' <- get_instances
661− -- Attempt to resolve any type classes which have 'complete' types,
672+ -- Attempt to resolve any type classes which have 'complete' types,
662− -- i.e. no holes in them
673+ -- i.e. no holes in them
663− when (not pattern || (e_inarg ina && not tcgen &&
674+ when (not pattern || (e_inarg ina && not tcgen &&
664− not (e_guarded ina))) $
675+ not (e_guarded ina))) $
665− mapM_ (\n -> do focus n
676+ mapM_ (\n -> do focus n
666− g <- goal
677+ g <- goal
667− env <- get_env
678+ env <- get_env
668− hs <- get_holes
679+ hs <- get_holes
669− if all (\n -> not (n `elem` hs)) (freeNames g)
680+ if all (\n -> not (n `elem` hs)) (freeNames g)
670− then try (resolveTC False 7 g fn ist)
681+ then try (resolveTC False 7 g fn ist)
671− (movelast n)
682+ (movelast n)
672− else movelast n)
683+ else movelast n)
673− (ivs' \\ ivs)
684+ (ivs' \\ ivs)
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
692+ implicitApp :: ElabD [ImplicitInfo] -> ElabD ()
694+ | pattern = do elab; return ()
701+ elab' ina topfc (PAppImpl tm es)
703+ getReqImps (Bind x (Pi (Just i) ty _) sc)
674− where -- normal < alternatives < lambdas < rewrites < tactic < default tactic
707+ -- normal < alternatives < lambdas < rewrites < tactic < default tactic
675 -- reason for lambdas after alternatives is that having
708 -- reason for lambdas after alternatives is that having
676 -- the alternative resolved can help with typechecking the lambda
709 -- the alternative resolved can help with typechecking the lambda
677 -- or the rewrite. Rewrites/tactics need as much information
710 -- or the rewrite. Rewrites/tactics need as much information
@@ -744,15 +777,24 @@ elab ist info emode opts fn tm
744 setInjective (PApp _ (PRef _ n) _) = setinj n
777 setInjective (PApp _ (PRef _ n) _) = setinj n
745 setInjective _ = return ()
778 setInjective _ = return ()
747− elab' ina _ tm@(PApp fc f [arg])
780+ elab' ina _ tm@(PApp fc f [arg]) =
748− = erun fc $
781+ erun fc $
749 do simple_app (not $ headRef f)
782 do simple_app (not $ headRef f)
783+ (elabE (ina { e_isfn = True }) (Just fc) f)
750− (elabE ina (Just fc) f) (elabE (ina { e_inarg = True }) (Just fc) (getTm arg))
784+ (elabE (ina { e_inarg = True }) (Just fc) (getTm arg))
751 (show tm)
785 (show tm)
753 where headRef (PRef _ _) = True
787 where headRef (PRef _ _) = True
754 headRef (PApp _ f _) = headRef f
788 headRef (PApp _ f _) = headRef f
755 headRef _ = False
789 headRef _ = False
791+ elab' ina fc (PAppImpl f es) = do appImpl (reverse es) -- not that we look...
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
796+ (elab' ina fc Placeholder)
756 elab' ina fc Placeholder
798 elab' ina fc Placeholder
757 = do (h : hs) <- get_holes
799 = do (h : hs) <- get_holes
758 movelast h
800 movelast h
@@ -1072,16 +1114,20 @@ elab ist info emode opts fn tm
1072 = x : insertScopedImps fc sc xs
1114 = x : insertScopedImps fc sc xs
1073 insertScopedImps _ _ xs = xs
1115 insertScopedImps _ _ xs = xs
1075− insertImpLam t =
1117+ insertImpLam ina t =
1076 do ty <- goal
1118 do ty <- goal
1077 env <- get_env
1119 env <- get_env
1078 let ty' = normalise (tt_ctxt ist) env ty
1120 let ty' = normalise (tt_ctxt ist) env ty
1079 addLam ty' t
1121 addLam ty' t
1081 -- just one level at a time
1123 -- just one level at a time
1082− addLam (Bind n (Pi (Just _) _ _) sc) t
1124+ addLam (Bind n (Pi (Just _) _ _) sc) t =
1083− = do impn <- unique_hole (sMN 0 "imp")
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])
1084− return (PLam emptyFC impn Placeholder t)
1130+ else return (PLam emptyFC impn Placeholder t)
1085 addLam _ t = return t
1131 addLam _ t = return t
1087 insertCoerce ina t@(PCase _ _ _) = return t
1133 insertCoerce ina t@(PCase _ _ _) = return t