@@ -1481,31 +1481,34 @@ implicitise syn ignore ist tm = -- trace ("INCOMING " ++ showImp True tm) $
1482-- Add implicit arguments in function calls
1482-- Add implicit arguments in function calls
1483addImplPat :: IState -> PTerm -> PTerm
1483addImplPat :: IState -> PTerm -> PTerm
1484−addImplPat = addImpl' True [] []
1484+addImplPat = addImpl' True [] [] []
1486addImplBound :: IState -> [Name] -> PTerm -> PTerm
1486addImplBound :: IState -> [Name] -> PTerm -> PTerm
1487−addImplBound ist ns = addImpl' False ns [] ist
1487+addImplBound ist ns = addImpl' False ns [] [] ist
1489addImplBoundInf :: IState -> [Name] -> [Name] -> PTerm -> PTerm
1489addImplBoundInf :: 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
1492-- | Add the implicit arguments to applications in the term
1492-- | Add the implicit arguments to applications in the term
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
1493−addImpl :: IState -> PTerm -> PTerm
1496+addImpl :: [Name] -> IState -> PTerm -> PTerm
1494addImpl = addImpl' False [] []
1497addImpl = addImpl' False [] []
1496-- TODO: in patterns, don't add implicits to function names guarded by constructors
1499-- TODO: in patterns, don't add implicits to function names guarded by constructors
1497-- and *not* inside a PHidden
1500-- and *not* inside a PHidden
1499−addImpl' :: Bool -> [Name] -> [Name] -> IState -> PTerm -> PTerm
1502+addImpl' :: Bool -> [Name] -> [Name] -> [Name] -> IState -> PTerm -> PTerm
1500−addImpl' inpat env infns ist ptm
1503+addImpl' inpat env infns imp_meths ist ptm
1501 = mkUniqueNames env (ai False (zip env (repeat Nothing)) [] ptm)
1504 = mkUniqueNames env (ai False (zip env (repeat Nothing)) [] ptm)
1503 ai :: Bool -> [(Name, Maybe PTerm)] -> [[T.Text]] -> PTerm -> PTerm
1506 ai :: Bool -> [(Name, Maybe PTerm)] -> [[T.Text]] -> PTerm -> PTerm
1504 ai qq env ds (PRef fc f)
1507 ai qq env ds (PRef fc f)
1505 | f `elem` infns = PInferRef fc f
1508 | 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 []
1507 ai qq env ds (PHidden (PRef fc f))
1510 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 [])
1509 ai qq env ds (PEq fc lt rt l r)
1512 ai qq env ds (PEq fc lt rt l r)
1510 = let lt' = ai qq env ds lt
1513 = let lt' = ai qq env ds lt
1511 rt' = ai qq env ds rt
1514 rt' = ai qq env ds rt
@@ -1541,7 +1544,7 @@ addImpl' inpat env infns ist ptm
1541 | f `elem` infns = ai qq env ds (PApp fc (PInferRef fc f) as)
1544 | f `elem` infns = ai qq env ds (PApp fc (PInferRef fc f) as)
1542 | not (f `elem` map fst env)
1545 | not (f `elem` map fst env)
1543 = let as' = map (fmap (ai qq env ds)) as in
1546 = 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'
1545 | Just (Just ty) <- lookup f env =
1548 | Just (Just ty) <- lookup f env =
1546 let as' = map (fmap (ai qq env ds)) as
1549 let as' = map (fmap (ai qq env ds)) as
1547 arity = getPArity ty in
1550 arity = getPArity ty in
@@ -1576,7 +1579,9 @@ addImpl' inpat env infns ist ptm
1576 _ -> ai qq env ds (PCase fc val [(PRef fc n, sc)])
1579 _ -> ai qq env ds (PCase fc val [(PRef fc n, sc)])
1577 ai qq env ds (PPi p n ty sc)
1580 ai qq env ds (PPi p n ty sc)
1578 = let ty' = ai qq env ds ty
1581 = let ty' = ai qq env ds ty
1582+ env' = if n `elem` imp_meths then env
1579− sc' = ai qq ((n, Just ty):env) ds sc in
1583+ else ((n, Just ty) : env)
1584+ sc' = ai qq env' ds sc in
1580 PPi p n ty' sc'
1585 PPi p n ty' sc'
1581 ai qq env ds (PGoal fc r n sc)
1586 ai qq env ds (PGoal fc r n sc)
1582 = let r' = ai qq env ds r
1587 = let r' = ai qq env ds r
@@ -1600,8 +1605,8 @@ addImpl' inpat env infns ist ptm
1600-- if in a pattern, and there are no arguments, and there's no possible
1605-- if in a pattern, and there are no arguments, and there's no possible
1601-- names with zero explicit arguments, don't add implicits.
1606-- names with zero explicit arguments, don't add implicits.
1603−aiFn :: Bool -> Bool -> Bool -> IState -> FC -> Name -> [[T.Text]] -> [PArg] -> Either Err PTerm
1608+aiFn :: Bool -> Bool -> Bool -> [Name] -> IState -> FC -> Name -> [[T.Text]] -> [PArg] -> Either Err PTerm
1604−aiFn inpat True qq ist fc f ds []
1609+aiFn inpat True qq imp_meths ist fc f ds []
1605 = case lookupDef f (tt_ctxt ist) of
1610 = case lookupDef f (tt_ctxt ist) of
1606 [] -> Right $ PPatvar fc f
1611 [] -> Right $ PPatvar fc f
1607 alts -> let ialts = lookupCtxtName f (idris_implicits ist) in
1612 alts -> let ialts = lookupCtxtName f (idris_implicits ist) in
@@ -1609,7 +1614,7 @@ aiFn inpat True qq ist fc f ds []
1609 if (not (vname f) || tcname f
1614 if (not (vname f) || tcname f
1610 || any (conCaf (tt_ctxt ist)) ialts)
1615 || any (conCaf (tt_ctxt ist)) ialts)
1611-- any constructor alts || any allImp ialts))
1616-- 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
1613 else Right $ PPatvar fc f
1618 else Right $ PPatvar fc f
1614 where imp (PExp _ _ _ _) = False
1619 where imp (PExp _ _ _ _) = False
1615 imp _ = True
1620 imp _ = True
@@ -1623,9 +1628,9 @@ aiFn inpat True qq ist fc f ds []
1623 vname (UN n) = True -- non qualified
1628 vname (UN n) = True -- non qualified
1624 vname _ = False
1629 vname _ = False
1626−aiFn inpat expat qq ist fc f ds as
1631+aiFn inpat expat qq imp_meths ist fc f ds as
1627 | f `elem` primNames = Right $ PApp fc (PRef fc f) as
1632 | 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
1629 -- This is where namespaces get resolved by adding PAlternative
1634 -- This is where namespaces get resolved by adding PAlternative
1630 = do let ns = lookupCtxtName f (idris_implicits ist)
1635 = do let ns = lookupCtxtName f (idris_implicits ist)
1631 let nh = filter (\(n, _) -> notHidden n) ns
1636 let nh = filter (\(n, _) -> notHidden n) ns
@@ -1633,15 +1638,20 @@ aiFn inpat expat qq ist fc f ds as
1633 [] -> nh
1638 [] -> nh
1635 case ns' of
1640 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)
1637 [] -> if f `elem` (map fst (idris_metavars ist))
1642 [] -> if f `elem` (map fst (idris_metavars ist))
1638 then Right $ PApp fc (PRef fc f) as
1643 then Right $ PApp fc (PRef fc f) as
1639 else Right $ mkPApp fc (length as) (PRef fc f) as
1644 else Right $ mkPApp fc (length as) (PRef fc f) as
1640 alts -> Right $
1645 alts -> Right $
1641 PAlternative True $
1646 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'))
1643 (insertImpl ns as)) alts
1648 (insertImpl ns as)) alts
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
1645 trimAlts [] alts = alts
1655 trimAlts [] alts = alts
1646 trimAlts ns alts
1656 trimAlts ns alts
1647 = filter (\(x, _) -> any (\d -> d `isPrefixOf` nspace x) ns) alts
1657 = filter (\(x, _) -> any (\d -> d `isPrefixOf` nspace x) ns) alts
@@ -1685,7 +1695,7 @@ aiFn inpat expat qq ist fc f ds as
1685 PImp p True l n Placeholder :
1695 PImp p True l n Placeholder :
1686 insImpAcc (M.insert n Placeholder pnas) ps given imps
1696 insImpAcc (M.insert n Placeholder pnas) ps given imps
1687 insImpAcc pnas (PTacImplicit p l n sc' ty : ps) given imps =
1697 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
1689 case find n imps [] of
1699 case find n imps [] of
1690 Just (tm, imps') ->
1700 Just (tm, imps') ->
1691 PTacImplicit p l n sc tm :
1701 PTacImplicit p l n sc tm :