@@ -1481,31 +1481,34 @@ implicitise syn ignore ist tm = -- trace ("INCOMING " ++ showImp True tm) $
14811481
14821482-- Add implicit arguments in function calls
14831483addImplPat :: IState -> PTerm -> PTerm
1484−addImplPat = addImpl' True [] []
1484+addImplPat = addImpl' True [] [] []
14851485
14861486addImplBound :: IState -> [Name] -> PTerm -> PTerm
1487−addImplBound ist ns = addImpl' False ns [] ist
1487+addImplBound ist ns = addImpl' False ns [] [] ist
14881488
14891489addImplBoundInf :: IState -> [Name] -> [Name] -> PTerm -> PTerm
1490−addImplBoundInf ist ns inf = addImpl' False ns inf ist
1490+addImplBoundInf ist ns inf = addImpl' False ns inf [] ist
14911491
14921492-- | Add the implicit arguments to applications in the term
1493−addImpl :: IState -> PTerm -> PTerm
1493+-- [Name] gives the names to always expend, even when under a binder of
1494+-- that name (this is to expand methods with implicit arguments in dependent
1495+-- type classes).
1496+addImpl :: [Name] -> IState -> PTerm -> PTerm
14941497addImpl = addImpl' False [] []
14951498
14961499-- TODO: in patterns, don't add implicits to function names guarded by constructors
14971500-- and *not* inside a PHidden
14981501
1499−addImpl' :: Bool -> [Name] -> [Name] -> IState -> PTerm -> PTerm
1500−addImpl' inpat env infns ist ptm
1502+addImpl' :: Bool -> [Name] -> [Name] -> [Name] -> IState -> PTerm -> PTerm
1503+addImpl' inpat env infns imp_meths ist ptm
15011504 = mkUniqueNames env (ai False (zip env (repeat Nothing)) [] ptm)
15021505 where
15031506 ai :: Bool -> [(Name, Maybe PTerm)] -> [[T.Text]] -> PTerm -> PTerm
15041507 ai qq env ds (PRef fc f)
15051508 | f `elem` infns = PInferRef fc f
1506− | not (f `elem` map fst env) = handleErr $ aiFn inpat inpat qq ist fc f ds []
1509+ | not (f `elem` map fst env) = handleErr $ aiFn inpat inpat qq imp_meths ist fc f ds []
15071510 ai qq env ds (PHidden (PRef fc f))
1508− | not (f `elem` map fst env) = PHidden (handleErr $ aiFn inpat False qq ist fc f ds [])
1511+ | not (f `elem` map fst env) = PHidden (handleErr $ aiFn inpat False qq imp_meths ist fc f ds [])
15091512 ai qq env ds (PEq fc lt rt l r)
15101513 = let lt' = ai qq env ds lt
15111514 rt' = ai qq env ds rt
@@ -1541,7 +1544,7 @@ addImpl' inpat env infns ist ptm
15411544 | f `elem` infns = ai qq env ds (PApp fc (PInferRef fc f) as)
15421545 | not (f `elem` map fst env)
15431546 = let as' = map (fmap (ai qq env ds)) as in
1544− handleErr $ aiFn inpat False qq ist fc f ds as'
1547+ handleErr $ aiFn inpat False qq imp_meths ist fc f ds as'
15451548 | Just (Just ty) <- lookup f env =
15461549 let as' = map (fmap (ai qq env ds)) as
15471550 arity = getPArity ty in
@@ -1576,7 +1579,9 @@ addImpl' inpat env infns ist ptm
15761579 _ -> ai qq env ds (PCase fc val [(PRef fc n, sc)])
15771580 ai qq env ds (PPi p n ty sc)
15781581 = let ty' = ai qq env ds ty
1579− sc' = ai qq ((n, Just ty):env) ds sc in
1582+ env' = if n `elem` imp_meths then env
1583+ else ((n, Just ty) : env)
1584+ sc' = ai qq env' ds sc in
15801585 PPi p n ty' sc'
15811586 ai qq env ds (PGoal fc r n sc)
15821587 = let r' = ai qq env ds r
@@ -1600,8 +1605,8 @@ addImpl' inpat env infns ist ptm
16001605-- if in a pattern, and there are no arguments, and there's no possible
16011606-- names with zero explicit arguments, don't add implicits.
16021607
1603−aiFn :: Bool -> Bool -> Bool -> IState -> FC -> Name -> [[T.Text]] -> [PArg] -> Either Err PTerm
1604−aiFn inpat True qq ist fc f ds []
1608+aiFn :: Bool -> Bool -> Bool -> [Name] -> IState -> FC -> Name -> [[T.Text]] -> [PArg] -> Either Err PTerm
1609+aiFn inpat True qq imp_meths ist fc f ds []
16051610 = case lookupDef f (tt_ctxt ist) of
16061611 [] -> Right $ PPatvar fc f
16071612 alts -> let ialts = lookupCtxtName f (idris_implicits ist) in
@@ -1609,7 +1614,7 @@ aiFn inpat True qq ist fc f ds []
16091614 if (not (vname f) || tcname f
16101615 || any (conCaf (tt_ctxt ist)) ialts)
16111616-- any constructor alts || any allImp ialts))
1612− then aiFn inpat False qq ist fc f ds [] -- use it as a constructor
1617+ then aiFn inpat False qq imp_meths ist fc f ds [] -- use it as a constructor
16131618 else Right $ PPatvar fc f
16141619 where imp (PExp _ _ _ _) = False
16151620 imp _ = True
@@ -1623,9 +1628,9 @@ aiFn inpat True qq ist fc f ds []
16231628 vname (UN n) = True -- non qualified
16241629 vname _ = False
16251630
1626−aiFn inpat expat qq ist fc f ds as
1631+aiFn inpat expat qq imp_meths ist fc f ds as
16271632 | f `elem` primNames = Right $ PApp fc (PRef fc f) as
1628−aiFn inpat expat qq ist fc f ds as
1633+aiFn inpat expat qq imp_meths ist fc f ds as
16291634 -- This is where namespaces get resolved by adding PAlternative
16301635 = do let ns = lookupCtxtName f (idris_implicits ist)
16311636 let nh = filter (\(n, _) -> notHidden n) ns
@@ -1633,15 +1638,20 @@ aiFn inpat expat qq ist fc f ds as
16331638 [] -> nh
16341639 x -> x
16351640 case ns' of
1636− [(f',ns)] -> Right $ mkPApp fc (length ns) (PRef fc f') (insertImpl ns as)
1641+ [(f',ns)] -> Right $ mkPApp fc (length ns) (PRef fc (isImpName f f')) (insertImpl ns as)
16371642 [] -> if f `elem` (map fst (idris_metavars ist))
16381643 then Right $ PApp fc (PRef fc f) as
16391644 else Right $ mkPApp fc (length as) (PRef fc f) as
16401645 alts -> Right $
16411646 PAlternative True $
1642− map (\(f', ns) -> mkPApp fc (length ns) (PRef fc f')
1647+ map (\(f', ns) -> mkPApp fc (length ns) (PRef fc (isImpName f f'))
16431648 (insertImpl ns as)) alts
16441649 where
1650+ -- if the name is in imp_meths, we should actually refer to the bound
1651+ -- name rather than the global one after expanding implicits
1652+ isImpName f f' | f `elem` imp_meths = f
1653+ | otherwise = f'
1654+
16451655 trimAlts [] alts = alts
16461656 trimAlts ns alts
16471657 = filter (\(x, _) -> any (\d -> d `isPrefixOf` nspace x) ns) alts
@@ -1685,7 +1695,7 @@ aiFn inpat expat qq ist fc f ds as
16851695 PImp p True l n Placeholder :
16861696 insImpAcc (M.insert n Placeholder pnas) ps given imps
16871697 insImpAcc pnas (PTacImplicit p l n sc' ty : ps) given imps =
1688− let sc = addImpl ist (substMatches (M.toList pnas) sc') in
1698+ let sc = addImpl imp_meths ist (substMatches (M.toList pnas) sc') in
16891699 case find n imps [] of
16901700 Just (tm, imps') ->
16911701 PTacImplicit p l n sc tm :