@@ -1475,31 +1475,34 @@ implicitise syn ignore ist tm = -- trace ("INCOMING " ++ showImp True tm) $
1476-- Add implicit arguments in function calls
1476-- Add implicit arguments in function calls
1477addImplPat :: IState -> PTerm -> PTerm
1477addImplPat :: IState -> PTerm -> PTerm
1478−addImplPat = addImpl' True [] []
1478+addImplPat = addImpl' True [] [] []
1480addImplBound :: IState -> [Name] -> PTerm -> PTerm
1480addImplBound :: IState -> [Name] -> PTerm -> PTerm
1481−addImplBound ist ns = addImpl' False ns [] ist
1481+addImplBound ist ns = addImpl' False ns [] [] ist
1483addImplBoundInf :: IState -> [Name] -> [Name] -> PTerm -> PTerm
1483addImplBoundInf :: IState -> [Name] -> [Name] -> PTerm -> PTerm
1484−addImplBoundInf ist ns inf = addImpl' False ns inf ist
1484+addImplBoundInf ist ns inf = addImpl' False ns inf [] ist
1486-- | Add the implicit arguments to applications in the term
1486-- | Add the implicit arguments to applications in the term
1487+-- [Name] gives the names to always expend, even when under a binder of
1488+-- that name (this is to expand methods with implicit arguments in dependent
1487−addImpl :: IState -> PTerm -> PTerm
1490+addImpl :: [Name] -> IState -> PTerm -> PTerm
1488addImpl = addImpl' False [] []
1491addImpl = addImpl' False [] []
1490-- TODO: in patterns, don't add implicits to function names guarded by constructors
1493-- TODO: in patterns, don't add implicits to function names guarded by constructors
1491-- and *not* inside a PHidden
1494-- and *not* inside a PHidden
1493−addImpl' :: Bool -> [Name] -> [Name] -> IState -> PTerm -> PTerm
1496+addImpl' :: Bool -> [Name] -> [Name] -> [Name] -> IState -> PTerm -> PTerm
1494−addImpl' inpat env infns ist ptm
1497+addImpl' inpat env infns imp_meths ist ptm
1495 = mkUniqueNames env (ai False (zip env (repeat Nothing)) [] ptm)
1498 = mkUniqueNames env (ai False (zip env (repeat Nothing)) [] ptm)
1497 ai :: Bool -> [(Name, Maybe PTerm)] -> [[T.Text]] -> PTerm -> PTerm
1500 ai :: Bool -> [(Name, Maybe PTerm)] -> [[T.Text]] -> PTerm -> PTerm
1498 ai qq env ds (PRef fc f)
1501 ai qq env ds (PRef fc f)
1499 | f `elem` infns = PInferRef fc f
1502 | f `elem` infns = PInferRef fc f
1500− | not (f `elem` map fst env) = handleErr $ aiFn inpat inpat qq ist fc f ds []
1503+ | not (f `elem` map fst env) = handleErr $ aiFn inpat inpat qq imp_meths ist fc f ds []
1501 ai qq env ds (PHidden (PRef fc f))
1504 ai qq env ds (PHidden (PRef fc f))
1502− | not (f `elem` map fst env) = PHidden (handleErr $ aiFn inpat False qq ist fc f ds [])
1505+ | not (f `elem` map fst env) = PHidden (handleErr $ aiFn inpat False qq imp_meths ist fc f ds [])
1503 ai qq env ds (PEq fc lt rt l r)
1506 ai qq env ds (PEq fc lt rt l r)
1504 = let lt' = ai qq env ds lt
1507 = let lt' = ai qq env ds lt
1505 rt' = ai qq env ds rt
1508 rt' = ai qq env ds rt
@@ -1535,7 +1538,7 @@ addImpl' inpat env infns ist ptm
1535 | f `elem` infns = ai qq env ds (PApp fc (PInferRef fc f) as)
1538 | f `elem` infns = ai qq env ds (PApp fc (PInferRef fc f) as)
1536 | not (f `elem` map fst env)
1539 | not (f `elem` map fst env)
1537 = let as' = map (fmap (ai qq env ds)) as in
1540 = let as' = map (fmap (ai qq env ds)) as in
1538− handleErr $ aiFn inpat False qq ist fc f ds as'
1541+ handleErr $ aiFn inpat False qq imp_meths ist fc f ds as'
1539 | Just (Just ty) <- lookup f env =
1542 | Just (Just ty) <- lookup f env =
1540 let as' = map (fmap (ai qq env ds)) as
1543 let as' = map (fmap (ai qq env ds)) as
1541 arity = getPArity ty in
1544 arity = getPArity ty in
@@ -1570,7 +1573,9 @@ addImpl' inpat env infns ist ptm
1570 _ -> ai qq env ds (PCase fc val [(PRef fc n, sc)])
1573 _ -> ai qq env ds (PCase fc val [(PRef fc n, sc)])
1571 ai qq env ds (PPi p n ty sc)
1574 ai qq env ds (PPi p n ty sc)
1572 = let ty' = ai qq env ds ty
1575 = let ty' = ai qq env ds ty
1576+ env' = if n `elem` imp_meths then env
1573− sc' = ai qq ((n, Just ty):env) ds sc in
1577+ else ((n, Just ty) : env)
1578+ sc' = ai qq env' ds sc in
1574 PPi p n ty' sc'
1579 PPi p n ty' sc'
1575 ai qq env ds (PGoal fc r n sc)
1580 ai qq env ds (PGoal fc r n sc)
1576 = let r' = ai qq env ds r
1581 = let r' = ai qq env ds r
@@ -1594,8 +1599,8 @@ addImpl' inpat env infns ist ptm
1594-- if in a pattern, and there are no arguments, and there's no possible
1599-- if in a pattern, and there are no arguments, and there's no possible
1595-- names with zero explicit arguments, don't add implicits.
1600-- names with zero explicit arguments, don't add implicits.
1597−aiFn :: Bool -> Bool -> Bool -> IState -> FC -> Name -> [[T.Text]] -> [PArg] -> Either Err PTerm
1602+aiFn :: Bool -> Bool -> Bool -> [Name] -> IState -> FC -> Name -> [[T.Text]] -> [PArg] -> Either Err PTerm
1598−aiFn inpat True qq ist fc f ds []
1603+aiFn inpat True qq imp_meths ist fc f ds []
1599 = case lookupDef f (tt_ctxt ist) of
1604 = case lookupDef f (tt_ctxt ist) of
1600 [] -> Right $ PPatvar fc f
1605 [] -> Right $ PPatvar fc f
1601 alts -> let ialts = lookupCtxtName f (idris_implicits ist) in
1606 alts -> let ialts = lookupCtxtName f (idris_implicits ist) in
@@ -1603,7 +1608,7 @@ aiFn inpat True qq ist fc f ds []
1603 if (not (vname f) || tcname f
1608 if (not (vname f) || tcname f
1604 || any (conCaf (tt_ctxt ist)) ialts)
1609 || any (conCaf (tt_ctxt ist)) ialts)
1605-- any constructor alts || any allImp ialts))
1610-- any constructor alts || any allImp ialts))
1606− then aiFn inpat False qq ist fc f ds [] -- use it as a constructor
1611+ then aiFn inpat False qq imp_meths ist fc f ds [] -- use it as a constructor
1607 else Right $ PPatvar fc f
1612 else Right $ PPatvar fc f
1608 where imp (PExp _ _ _ _) = False
1613 where imp (PExp _ _ _ _) = False
1609 imp _ = True
1614 imp _ = True
@@ -1617,9 +1622,9 @@ aiFn inpat True qq ist fc f ds []
1617 vname (UN n) = True -- non qualified
1622 vname (UN n) = True -- non qualified
1618 vname _ = False
1623 vname _ = False
1620−aiFn inpat expat qq ist fc f ds as
1625+aiFn inpat expat qq imp_meths ist fc f ds as
1621 | f `elem` primNames = Right $ PApp fc (PRef fc f) as
1626 | f `elem` primNames = Right $ PApp fc (PRef fc f) as
1622−aiFn inpat expat qq ist fc f ds as
1627+aiFn inpat expat qq imp_meths ist fc f ds as
1623 -- This is where namespaces get resolved by adding PAlternative
1628 -- This is where namespaces get resolved by adding PAlternative
1624 = do let ns = lookupCtxtName f (idris_implicits ist)
1629 = do let ns = lookupCtxtName f (idris_implicits ist)
1625 let nh = filter (\(n, _) -> notHidden n) ns
1630 let nh = filter (\(n, _) -> notHidden n) ns
@@ -1627,15 +1632,20 @@ aiFn inpat expat qq ist fc f ds as
1627 [] -> nh
1632 [] -> nh
1629 case ns' of
1634 case ns' of
1630− [(f',ns)] -> Right $ mkPApp fc (length ns) (PRef fc f') (insertImpl ns as)
1635+ [(f',ns)] -> Right $ mkPApp fc (length ns) (PRef fc (isImpName f f')) (insertImpl ns as)
1631 [] -> if f `elem` (map fst (idris_metavars ist))
1636 [] -> if f `elem` (map fst (idris_metavars ist))
1632 then Right $ PApp fc (PRef fc f) as
1637 then Right $ PApp fc (PRef fc f) as
1633 else Right $ mkPApp fc (length as) (PRef fc f) as
1638 else Right $ mkPApp fc (length as) (PRef fc f) as
1634 alts -> Right $
1639 alts -> Right $
1635 PAlternative True $
1640 PAlternative True $
1636− map (\(f', ns) -> mkPApp fc (length ns) (PRef fc f')
1641+ map (\(f', ns) -> mkPApp fc (length ns) (PRef fc (isImpName f f'))
1637 (insertImpl ns as)) alts
1642 (insertImpl ns as)) alts
1644+ -- if the name is in imp_meths, we should actually refer to the bound
1645+ -- name rather than the global one after expanding implicits
1646+ isImpName f f' | f `elem` imp_meths = f
1639 trimAlts [] alts = alts
1649 trimAlts [] alts = alts
1640 trimAlts ns alts
1650 trimAlts ns alts
1641 = filter (\(x, _) -> any (\d -> d `isPrefixOf` nspace x) ns) alts
1651 = filter (\(x, _) -> any (\d -> d `isPrefixOf` nspace x) ns) alts
@@ -1679,7 +1689,7 @@ aiFn inpat expat qq ist fc f ds as
1679 PImp p True l n Placeholder :
1689 PImp p True l n Placeholder :
1680 insImpAcc (M.insert n Placeholder pnas) ps given imps
1690 insImpAcc (M.insert n Placeholder pnas) ps given imps
1681 insImpAcc pnas (PTacImplicit p l n sc' ty : ps) given imps =
1691 insImpAcc pnas (PTacImplicit p l n sc' ty : ps) given imps =
1682− let sc = addImpl ist (substMatches (M.toList pnas) sc') in
1692+ let sc = addImpl imp_meths ist (substMatches (M.toList pnas) sc') in
1683 case find n imps [] of
1693 case find n imps [] of
1684 Just (tm, imps') ->
1694 Just (tm, imps') ->
1685 PTacImplicit p l n sc tm :
1695 PTacImplicit p l n sc tm :