Simon Peyton Jones pushed to branch wip/T27828 at Glasgow Haskell Compiler / GHC

Commits:

4 changed files:

Changes:

  • compiler/GHC/Core/Coercion/Axiom.hs
    ... ... @@ -197,14 +197,19 @@ Note [Storing compatibility]
    197 197
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    198 198
     During axiom application, we need to be aware of which branches are compatible
    
    199 199
     with which others. The full explanation is in Note [Compatibility] in
    
    200
    -GHc.Core.FamInstEnv. (The code is placed there to avoid a dependency from
    
    200
    +GHC.Core.FamInstEnv. (The code is placed there to avoid a dependency from
    
    201 201
     GHC.Core.Coercion.Axiom on the unification algorithm.) Although we could
    
    202 202
     theoretically compute compatibility on the fly, this is silly, so we store it
    
    203 203
     in a CoAxiom.
    
    204 204
     
    
    205
    -Specifically, each branch refers to all other branches with which it is
    
    206
    -incompatible. This list might well be empty, and it will always be for the
    
    207
    -first branch of any axiom.
    
    205
    +Specifically, the `cab_incomps :: [CoAxBranch]` field of a `CoAxBranch`
    
    206
    +lists all /earlier/ branches with which it is /incompatible/.  That is,
    
    207
    +earlier branches where
    
    208
    +  1) the patterns are not apart from the patterns of this branch, and
    
    209
    +  2) if the patterns unify, the rhss are different
    
    210
    +
    
    211
    +This list might well be empty, and it will always be for the first branch of
    
    212
    +any axiom.
    
    208 213
     
    
    209 214
     CoAxBranches that do not (yet) belong to a CoAxiom should have a panic thunk
    
    210 215
     stored in cab_incomps. The incompatibilities are properly a property of the
    

  • compiler/GHC/Core/FamInstEnv.hs
    ... ... @@ -19,7 +19,7 @@ module GHC.Core.FamInstEnv (
    19 19
             FamInstMatch(..),
    
    20 20
             lookupFamInstEnv, lookupFamInstEnvConflicts, lookupFamInstEnvByTyCon,
    
    21 21
     
    
    22
    -        isDominatedBy, apartnessCheck, compatibleBranches,
    
    22
    +        isDominatedBy, apartnessCheck, noEarlierMatchCheck, compatibleBranches,
    
    23 23
     
    
    24 24
             -- Injectivity
    
    25 25
             InjectivityCheckResult(..),
    
    ... ... @@ -488,9 +488,11 @@ in GHC.Core.Unify.)
    488 488
     Note [Compatibility]
    
    489 489
     ~~~~~~~~~~~~~~~~~~~~
    
    490 490
     Two patterns are /compatible/ if either of the following conditions hold:
    
    491
    -1) The patterns are apart.
    
    492
    -2) The patterns unify with a substitution S, and their right hand sides
    
    493
    -equal under that substitution.
    
    491
    +
    
    492
    +  1) The patterns are apart.
    
    493
    +
    
    494
    +  2) The patterns unify with a substitution S, and their right hand sides
    
    495
    +     are equal under that substitution.
    
    494 496
     
    
    495 497
     For open type families, only compatible instances are allowed. For closed
    
    496 498
     type families, the story is slightly more complicated. Consider the following:
    
    ... ... @@ -603,8 +605,7 @@ injectiveBranches injectivity
    603 605
                       ax1@(CoAxBranch { cab_lhs = lhs1, cab_rhs = rhs1 })
    
    604 606
                       ax2@(CoAxBranch { cab_lhs = lhs2, cab_rhs = rhs2 })
    
    605 607
       -- See Note [Verifying injectivity annotation], case 1.
    
    606
    -  = case tcUnifyTysForInjectivity True [rhs1] [rhs2] of
    
    607
    -             -- True = two-way pre-unification
    
    608
    +  = case tcUnifyTysForInjectivity Unifying [rhs1] [rhs2] of
    
    608 609
            Nothing -> InjectivityAccepted
    
    609 610
              -- RHS are different, so equations are injective.
    
    610 611
              -- This is case 1A from Note [Verifying injectivity annotation]
    
    ... ... @@ -1258,6 +1259,17 @@ apartnessCheck target (CoAxBranch { cab_incomps = incomps })
    1258 1259
         isSurelyApart SurelyApart = True
    
    1259 1260
         isSurelyApart _           = False
    
    1260 1261
     
    
    1262
    +-- | Check whether an earlier branch matches these target types
    
    1263
    +noEarlierMatchCheck
    
    1264
    +  :: [Type]
    
    1265
    +  -> CoAxBranch -- ^ The candidate equation we wish to use
    
    1266
    +  -> Bool       -- ^ True <=> equation can fire
    
    1267
    +noEarlierMatchCheck target (CoAxBranch { cab_incomps = incomps })
    
    1268
    +  = not (any matches incomps)
    
    1269
    +  where
    
    1270
    +    matches earlier_branch
    
    1271
    +      = isJust (tcMatchTys (coAxBranchLHS earlier_branch) target)
    
    1272
    +
    
    1261 1273
     {-
    
    1262 1274
     ************************************************************************
    
    1263 1275
     *                                                                      *
    

  • compiler/GHC/Core/Unify.hs
    ... ... @@ -9,6 +9,7 @@ module GHC.Core.Unify (
    9 9
             tcMatchTyX_BM, ruleMatchTyKiX,
    
    10 10
     
    
    11 11
             -- Side-effect free unification
    
    12
    +        AmIUnifying(..),
    
    12 13
             tcUnifyTy, tcUnifyTys, tcUnifyFunDeps, tcUnifyDebugger,
    
    13 14
             tcUnifyTysFG, tcUnifyTysForInjectivity,
    
    14 15
             BindTvFun, BindFamFun, BindFlag(..),
    
    ... ... @@ -597,14 +598,14 @@ tcMatchTyX_BM :: HasDebugCallStack
    597 598
                   => BindTvFun -> Subst
    
    598 599
                   -> Type -> Type -> Maybe Subst
    
    599 600
     tcMatchTyX_BM bind_tv subst ty1 ty2
    
    600
    -  = tc_match_tys_x bind_tv False subst [ty1] [ty2]
    
    601
    +  = tc_match_tys_x bind_tv IgnoreOuterKinds subst [ty1] [ty2]
    
    601 602
     
    
    602 603
     -- | Like 'tcMatchTy', but allows the kinds of the types to differ,
    
    603 604
     -- and thus matches them as well.
    
    604 605
     -- See also Note [tcMatchTy vs tcMatchTyKi]
    
    605 606
     tcMatchTyKi :: HasDebugCallStack => Type -> Type -> Maybe Subst
    
    606 607
     tcMatchTyKi ty1 ty2
    
    607
    -  = tc_match_tys alwaysBindTv True [ty1] [ty2]
    
    608
    +  = tc_match_tys alwaysBindTv CheckOuterKinds [ty1] [ty2]
    
    608 609
     
    
    609 610
     -- | This is similar to 'tcMatchTy', but extends a substitution
    
    610 611
     -- See also Note [tcMatchTy vs tcMatchTyKi]
    
    ... ... @@ -614,7 +615,7 @@ tcMatchTyX :: HasDebugCallStack
    614 615
                -> Type                -- ^ Target
    
    615 616
                -> Maybe Subst
    
    616 617
     tcMatchTyX subst ty1 ty2
    
    617
    -  = tc_match_tys_x alwaysBindTv False subst [ty1] [ty2]
    
    618
    +  = tc_match_tys_x alwaysBindTv IgnoreOuterKinds subst [ty1] [ty2]
    
    618 619
     
    
    619 620
     -- | Like 'tcMatchTy' but over a list of types.
    
    620 621
     -- See also Note [tcMatchTy vs tcMatchTyKi]
    
    ... ... @@ -625,7 +626,7 @@ tcMatchTys :: HasDebugCallStack
    625 626
                                  -- variables could be free in the target
    
    626 627
                                  -- See (CU6) in Note [The Core unifier]
    
    627 628
     tcMatchTys tys1 tys2
    
    628
    -  = tc_match_tys alwaysBindTv False tys1 tys2
    
    629
    +  = tc_match_tys alwaysBindTv IgnoreOuterKinds tys1 tys2
    
    629 630
     
    
    630 631
     -- | Like 'tcMatchTyKi' but over a list of types.
    
    631 632
     -- See also Note [tcMatchTy vs tcMatchTyKi]
    
    ... ... @@ -635,7 +636,7 @@ tcMatchTyKis :: HasDebugCallStack
    635 636
                  -> Maybe Subst    -- ^ One-shot substitution
    
    636 637
                                    -- See (CU6) in Note [The Core unifier]
    
    637 638
     tcMatchTyKis tys1 tys2
    
    638
    -  = tc_match_tys alwaysBindTv True tys1 tys2
    
    639
    +  = tc_match_tys alwaysBindTv CheckOuterKinds tys1 tys2
    
    639 640
     
    
    640 641
     -- | Like 'tcMatchTys', but extending a substitution
    
    641 642
     -- See also Note [tcMatchTy vs tcMatchTyKi]
    
    ... ... @@ -645,7 +646,7 @@ tcMatchTysX :: HasDebugCallStack
    645 646
                 -> [Type]         -- ^ Target
    
    646 647
                 -> Maybe Subst    -- ^ One-shot substitution
    
    647 648
     tcMatchTysX subst tys1 tys2
    
    648
    -  = tc_match_tys_x alwaysBindTv False subst tys1 tys2
    
    649
    +  = tc_match_tys_x alwaysBindTv IgnoreOuterKinds subst tys1 tys2
    
    649 650
     
    
    650 651
     -- | Like 'tcMatchTyKis', but extending a substitution
    
    651 652
     -- See also Note [tcMatchTy vs tcMatchTyKi]
    
    ... ... @@ -655,12 +656,12 @@ tcMatchTyKisX :: HasDebugCallStack
    655 656
                   -> [Type]       -- ^ Target
    
    656 657
                   -> Maybe Subst  -- ^ One-shot substitution
    
    657 658
     tcMatchTyKisX subst tys1 tys2
    
    658
    -  = tc_match_tys_x alwaysBindTv True subst tys1 tys2
    
    659
    +  = tc_match_tys_x alwaysBindTv CheckOuterKinds subst tys1 tys2
    
    659 660
     
    
    660 661
     -- | Same as tc_match_tys_x, but starts with an empty substitution
    
    661 662
     tc_match_tys :: HasDebugCallStack
    
    662 663
                  => BindTvFun
    
    663
    -             -> Bool          -- ^ match kinds?
    
    664
    +             -> OuterKindsFlag   -- ^ Match kinds?
    
    664 665
                  -> [Type]
    
    665 666
                  -> [Type]
    
    666 667
                  -> Maybe Subst
    
    ... ... @@ -672,7 +673,7 @@ tc_match_tys bind_me match_kis tys1 tys2
    672 673
     -- | Worker for 'tcMatchTysX' and 'tcMatchTyKisX'
    
    673 674
     tc_match_tys_x :: HasDebugCallStack
    
    674 675
                    => BindTvFun
    
    675
    -               -> Bool          -- ^ match kinds?
    
    676
    +               -> OuterKindsFlag   -- ^ Match kinds?
    
    676 677
                    -> Subst
    
    677 678
                    -> [Type]
    
    678 679
                    -> [Type]
    
    ... ... @@ -680,8 +681,8 @@ tc_match_tys_x :: HasDebugCallStack
    680 681
     tc_match_tys_x bind_tv match_kis (Subst in_scope id_env tv_env cv_env) tys1 tys2
    
    681 682
       = case tc_unify_tys neverBindFam  -- (ATF7) in Note [Apartness and type families]
    
    682 683
                           bind_tv
    
    683
    -                      False  -- Matching, not unifying
    
    684
    -                      False  -- Not an injectivity check
    
    684
    +                      Matching  -- Matching, not unifying
    
    685
    +                      False     -- Not an injectivity check
    
    685 686
                           match_kis
    
    686 687
                           RespectMultiplicities
    
    687 688
                           (mkRnEnv2 in_scope) tv_env cv_env tys1 tys2 of
    
    ... ... @@ -706,9 +707,9 @@ ruleMatchTyKiX tmpl_tvs rn_env tenv tmpl target
    706 707
           -- But even if it did, (ATF8) in Note [Apartness and type families]
    
    707 708
           -- says it doesn't matter becuase here we only care about Unifiable.
    
    708 709
           -- So neverBindFam is efficient, and sufficient.
    
    709
    -                      False    -- Matching, not unifying
    
    710
    +                      Matching -- Matching, not unifying
    
    710 711
                           False    -- No doing an injectivity check
    
    711
    -                      True     -- Match the kinds
    
    712
    +                      CheckOuterKinds  -- Match the kinds
    
    712 713
                           IgnoreMultiplicities
    
    713 714
                             -- See Note [Rewrite rules ignore multiplicities in FunTy]
    
    714 715
                           rn_env tenv emptyCvSubstEnv [tmpl] [target] of
    
    ... ... @@ -784,7 +785,7 @@ tcUnifyTy t1 t2 = tcUnifyTys alwaysBindTv [t1] [t2]
    784 785
     tcUnifyDebugger :: Type -> Type -> Maybe Subst
    
    785 786
     tcUnifyDebugger t1 t2
    
    786 787
       = case tc_unify_tys_fg
    
    787
    -             True            -- Unify kinds
    
    788
    +             CheckOuterKinds -- Unify kinds
    
    788 789
                  neverBindFam    -- Does not affect Unifiable, so pick max efficient
    
    789 790
                                  -- See (ATF8) in Note [Apartness and type families]
    
    790 791
                  alwaysBindTv
    
    ... ... @@ -798,7 +799,7 @@ tcUnifyFunDeps :: TyCoVarSet
    798 799
                    -> Maybe Subst
    
    799 800
     tcUnifyFunDeps qtvs tys1 tys2
    
    800 801
       = case tc_unify_tys_fg
    
    801
    -             True               -- Unify kinds
    
    802
    +             CheckOuterKinds    -- Unify kinds
    
    802 803
                  dontCareBindFam    -- Class-instance heads never mention type families
    
    803 804
                  (matchBindTv qtvs)
    
    804 805
                  tys1 tys2 of
    
    ... ... @@ -821,7 +822,7 @@ tcUnifyTysForInjectivity unif tys1 tys2
    821 822
       = case tc_unify_tys alwaysBindFam alwaysBindTv
    
    822 823
                            unif   -- Am I unifying?
    
    823 824
                            True   -- Do injectivity checks
    
    824
    -                       False  -- Don't check outermost kinds
    
    825
    +                       IgnoreOuterKinds  -- Don't check outermost kinds
    
    825 826
                            RespectMultiplicities
    
    826 827
                            rn_env emptyTvSubstEnv emptyCvSubstEnv
    
    827 828
                            tys1 tys2 of
    
    ... ... @@ -838,8 +839,9 @@ tcUnifyTysForInjectivity unif tys1 tys2
    838 839
                    -- should never actually be done
    
    839 840
     
    
    840 841
         maybe_fix tv_subst
    
    841
    -      | unif      = niFixSubst in_scope tv_subst
    
    842
    -      | otherwise = mkTvSubst  in_scope tv_subst
    
    842
    +      = case unif of
    
    843
    +          Unifying -> niFixSubst in_scope tv_subst
    
    844
    +          Matching -> mkTvSubst  in_scope tv_subst
    
    843 845
           -- When matching, don't confuse domain with range; no fixpoint!
    
    844 846
     
    
    845 847
     -----------------
    
    ... ... @@ -866,15 +868,15 @@ tcUnifyTysFG :: BindFamFun -> BindTvFun
    866 868
                  -> [Type] -> [Type]
    
    867 869
                  -> UnifyResult
    
    868 870
     tcUnifyTysFG bind_fam bind_tv tys1 tys2
    
    869
    -  = tc_unify_tys_fg False bind_fam bind_tv tys1 tys2
    
    871
    +  = tc_unify_tys_fg IgnoreOuterKinds bind_fam bind_tv tys1 tys2
    
    870 872
     
    
    871
    -tc_unify_tys_fg :: Bool
    
    873
    +tc_unify_tys_fg :: OuterKindsFlag
    
    872 874
                     -> BindFamFun -> BindTvFun
    
    873 875
                     -> [Type] -> [Type]
    
    874 876
                     -> UnifyResult
    
    875 877
     tc_unify_tys_fg match_kis bind_fam bind_tv tys1 tys2
    
    876 878
       = do { (tv_env, _) <- tc_unify_tys bind_fam bind_tv
    
    877
    -                                  True       -- Unifying
    
    879
    +                                  Unifying
    
    878 880
                                       False      -- Not doing an injectivity check
    
    879 881
                                       match_kis  -- Match outer kinds
    
    880 882
                                       RespectMultiplicities rn_env
    
    ... ... @@ -888,10 +890,10 @@ tc_unify_tys_fg match_kis bind_fam bind_tv tys1 tys2
    888 890
     -- | This function is actually the one to call the unifier -- a little
    
    889 891
     -- too general for outside clients, though.
    
    890 892
     tc_unify_tys :: BindFamFun -> BindTvFun
    
    891
    -             -> AmIUnifying -- ^ True <=> unify; False <=> match
    
    892
    -             -> Bool        -- ^ True <=> doing an injectivity check
    
    893
    -             -> Bool        -- ^ True <=> treat the kinds as well
    
    894
    -             -> MultiplicityFlag -- ^ see Note [Rewrite rules ignore multiplicities in FunTy] in GHC.Core.Unify
    
    893
    +             -> AmIUnifying    -- ^ Unifying or matching
    
    894
    +             -> Bool           -- ^ True <=> doing an injectivity check
    
    895
    +             -> OuterKindsFlag -- ^ Check outer kinds or not
    
    896
    +             -> MultiplicityFlag  -- ^ see Note [Rewrite rules ignore multiplicities in FunTy] in GHC.Core.Unify
    
    895 897
                  -> RnEnv2
    
    896 898
                  -> TvSubstEnv  -- ^ substitution to extend
    
    897 899
                  -> CvSubstEnv
    
    ... ... @@ -910,8 +912,9 @@ tc_unify_tys :: BindFamFun -> BindTvFun
    910 912
     -- See also Note [tcMatchTy vs tcMatchTyKi].
    
    911 913
     tc_unify_tys bind_fam bind_tv unif inj_check match_kis match_mults rn_env tv_env cv_env tys1 tys2
    
    912 914
       = initUM tv_env cv_env $
    
    913
    -    do { when match_kis $
    
    914
    -         unify_tys env kis1 kis2
    
    915
    +    do { case match_kis of
    
    916
    +            CheckOuterKinds  -> unify_tys env kis1 kis2
    
    917
    +            IgnoreOuterKinds -> return ()
    
    915 918
            ; unify_tys env tys1 tys2 }
    
    916 919
       where
    
    917 920
         env = UMEnv { um_bind_tv_fun  = bind_tv
    
    ... ... @@ -926,6 +929,11 @@ tc_unify_tys bind_fam bind_tv unif inj_check match_kis match_mults rn_env tv_env
    926 929
         kis2 = map typeKind tys2
    
    927 930
     
    
    928 931
     
    
    932
    +data OuterKindsFlag
    
    933
    +  = CheckOuterKinds    -- Unify or match the /kind/ of the two types
    
    934
    +  | IgnoreOuterKinds   -- Just unify of match the types, ignoring kinds
    
    935
    +                       --  (typically, we already know they are equal)
    
    936
    +
    
    929 937
     {- *********************************************************************
    
    930 938
     *                                                                      *
    
    931 939
                     UnifyResult, MaybeApart etc
    
    ... ... @@ -1583,8 +1591,7 @@ c.f. Note [Comparing type synonyms] in GHC.Core.TyCo.Compare
    1583 1591
     
    
    1584 1592
     -------------- unify_ty: the main workhorse -----------
    
    1585 1593
     
    
    1586
    -type AmIUnifying = Bool   -- True  <=> Unifying
    
    1587
    -                          -- False <=> Matching
    
    1594
    +data AmIUnifying = Matching | Unifying
    
    1588 1595
     
    
    1589 1596
     type InType      = Type       -- Before applying the RnEnv2
    
    1590 1597
     type OutCoercion = Coercion   -- After applying the RnEnv2
    
    ... ... @@ -1618,7 +1625,7 @@ unify_ty env (CastTy ty1 co1) ty2 kco
    1618 1625
         -- See (KCU1) in Note [Kind coercions in Unify]
    
    1619 1626
       = maybeApart MARCast  -- See (KCU2)
    
    1620 1627
     
    
    1621
    -  | um_unif env
    
    1628
    +  | Unifying <- um_unif env
    
    1622 1629
       = unify_ty env ty1 ty2 (co1 `mkTransCo` kco)
    
    1623 1630
     
    
    1624 1631
       | otherwise -- We are matching, not unifying
    
    ... ... @@ -1661,7 +1668,7 @@ unify_ty env (CoercionTy co1) (CoercionTy co2) kco
    1661 1668
       = do { c_subst <- getCvSubstEnv
    
    1662 1669
            ; case co1 of
    
    1663 1670
                CoVarCo cv
    
    1664
    -             | not (um_unif env)
    
    1671
    +             | Matching <- um_unif env
    
    1665 1672
                  , not (cv `elemVarEnv` c_subst)   -- Not forall-bound
    
    1666 1673
                  , let (_mult_co, co_l, co_r) = decomposeFunCo kco
    
    1667 1674
                          -- Because the coercion is used in a type, it should be safe to
    
    ... ... @@ -1682,7 +1689,7 @@ unify_ty env (TyVarTy tv1) ty2 kco
    1682 1689
       = uVarOrFam env (TyVarLHS tv1) ty2 kco
    
    1683 1690
     
    
    1684 1691
     unify_ty env ty1 (TyVarTy tv2) kco
    
    1685
    -  | um_unif env  -- If unifying, can swap args; but not when matching
    
    1692
    +  | Unifying <- um_unif env  -- If unifying, can swap args; but not when matching
    
    1686 1693
       = uVarOrFam (umSwapRn env) (TyVarLHS tv2) ty1 (mkSymCo kco)
    
    1687 1694
     
    
    1688 1695
     -- Deal with TyConApps
    
    ... ... @@ -1692,7 +1699,7 @@ unify_ty env ty1 ty2 kco
    1692 1699
       | Just (tc,tys) <- mb_sat_fam_app1
    
    1693 1700
       = uVarOrFam env (TyFamLHS tc tys) ty2 kco
    
    1694 1701
     
    
    1695
    -  | um_unif env
    
    1702
    +  | Unifying <- um_unif env
    
    1696 1703
       , Just (tc,tys) <- mb_sat_fam_app2
    
    1697 1704
       = uVarOrFam (umSwapRn env) (TyFamLHS tc tys) ty1 (mkSymCo kco)
    
    1698 1705
     
    
    ... ... @@ -1820,7 +1827,7 @@ uVarOrFam env ty1 ty2 kco
    1820 1827
         go swapped substs lhs@(TyVarLHS tv1) ty2 kco
    
    1821 1828
           | Just ty1' <- lookupVarEnv (um_tv_env substs) tv1'
    
    1822 1829
           = -- We already have a substitution for tv1
    
    1823
    -        if | um_unif env                          -> unify_ty env ty1' ty2 kco
    
    1830
    +        if | Unifying <- um_unif env              -> unify_ty env ty1' ty2 kco
    
    1824 1831
                | (ty1' `mkCastTy` kco) `tcEqType` ty2 -> return ()
    
    1825 1832
                | otherwise                            -> surelyApart
    
    1826 1833
                -- Unifying: recurse into unify_ty
    
    ... ... @@ -1842,9 +1849,9 @@ uVarOrFam env ty1 ty2 kco
    1842 1849
           | TyVarTy tv2 <- ty2
    
    1843 1850
           , let tv2' = umRnOccR env tv2
    
    1844 1851
           , tv1' == tv2'
    
    1845
    -      = if | um_unif env     -> return ()
    
    1846
    -           | tv1_is_bindable -> extendTvEnv tv1' ty2
    
    1847
    -           | otherwise       -> return ()
    
    1852
    +      = if | Unifying <- um_unif env -> return ()
    
    1853
    +           | tv1_is_bindable         -> extendTvEnv tv1' ty2
    
    1854
    +           | otherwise               -> return ()
    
    1848 1855
     
    
    1849 1856
           | tv1_is_bindable
    
    1850 1857
           , not (mentionsForAllBoundTyVarsR env ty2_fvs)
    
    ... ... @@ -1858,9 +1865,9 @@ uVarOrFam env ty1 ty2 kco
    1858 1865
           -- e.g.   a    ~ F p q       with `a` not bindable: we might succeed with go_fam
    
    1859 1866
           -- e.g.   a    ~ beta        with `a` not bindable: we might be able to bind `beta`
    
    1860 1867
           -- e.g.   beta ~ F beta Int  occurs check; but MaybeApart after swapping
    
    1861
    -      | um_unif env
    
    1868
    +      | Unifying   <- um_unif env
    
    1862 1869
           , NotSwapped <- swapped  -- If we have swapped already, don't do so again
    
    1863
    -      , Just lhs2 <- canEqLHS_maybe ty2
    
    1870
    +      , Just lhs2  <- canEqLHS_maybe ty2
    
    1864 1871
           = go IsSwapped substs lhs2 (mkTyVarTy tv1) (mkSymCo kco)
    
    1865 1872
     
    
    1866 1873
           | occurs_check = maybeApart MARInfinite   -- Occurs check
    
    ... ... @@ -1879,7 +1886,9 @@ uVarOrFam env ty1 ty2 kco
    1879 1886
                             | otherwise
    
    1880 1887
                             = False
    
    1881 1888
     
    
    1882
    -        occurs_check = um_unif env && uOccursCheck substs foralld_tvs lhs rhs
    
    1889
    +        occurs_check = case um_unif env of
    
    1890
    +                         Unifying -> uOccursCheck substs foralld_tvs lhs rhs
    
    1891
    +                         Matching -> False
    
    1883 1892
               -- Occurs check, only when unifying
    
    1884 1893
               -- see Note [Infinitary substitutions]
    
    1885 1894
               -- Make sure you include `kco` in rhs #14846
    
    ... ... @@ -1893,7 +1902,7 @@ uVarOrFam env ty1 ty2 kco
    1893 1902
           -- Hence the RnEnv2 is empty
    
    1894 1903
           | not under_forall
    
    1895 1904
           , Just ty1' <- lookupFamEnv (um_fam_env substs) tc1 tys1
    
    1896
    -      = if | um_unif env                          -> unify_ty env ty1' ty2 kco
    
    1905
    +      = if | Unifying <- um_unif env              -> unify_ty env ty1' ty2 kco
    
    1897 1906
                -- Below here we are matching
    
    1898 1907
                -- The return () case deals with:
    
    1899 1908
                --    Template:   (F a)..(F a)
    
    ... ... @@ -1930,9 +1939,9 @@ uVarOrFam env ty1 ty2 kco
    1930 1939
           -- NB: a type family can appear on the template when matching
    
    1931 1940
           --     see (ATF6) in Note [Apartness and type families]
    
    1932 1941
           -- (Only worth doing this if we are not under a forall.)
    
    1933
    -      | um_unif env
    
    1942
    +      | Unifying   <- um_unif env
    
    1934 1943
           , NotSwapped <- swapped
    
    1935
    -      , Just lhs2 <- canEqLHS_maybe ty2
    
    1944
    +      , Just lhs2  <- canEqLHS_maybe ty2
    
    1936 1945
           = go IsSwapped substs lhs2 (mkTyConApp tc1 tys1) (mkSymCo kco)
    
    1937 1946
     
    
    1938 1947
           | otherwise   -- See (ATF5) in Note [Apartness and type families]
    
    ... ... @@ -1962,9 +1971,9 @@ uVarOrFam env ty1 ty2 kco
    1962 1971
            (inj_tys2, noninj_tys2) = partitionByList inj tys2
    
    1963 1972
     
    
    1964 1973
            bind_fam_if_poss
    
    1965
    -         | not (um_unif env)  -- Not when matching (ATF11-1)
    
    1974
    +         | Matching <- um_unif env  -- Not when matching (ATF11-1)
    
    1966 1975
              = return ()
    
    1967
    -         | under_forall       -- Not under a forall (ATF3)
    
    1976
    +         | under_forall             -- Not under a forall (ATF3)
    
    1968 1977
              = return ()
    
    1969 1978
              | BindMe <- um_bind_fam_fun env tc tys1 rhs1
    
    1970 1979
              = unless (uOccursCheck substs emptyVarSet (TyFamLHS tc tys1) rhs1) $
    

  • compiler/GHC/Tc/Solver/FunDeps.hs
    ... ... @@ -26,7 +26,8 @@ import GHC.Core.Coercion
    26 26
     import GHC.Core.TyCo.Rep( Type(..) )
    
    27 27
     import GHC.Core.Predicate( EqRel(..) )
    
    28 28
     import GHC.Core.TyCon
    
    29
    -import GHC.Core.Unify( tcUnifyTysForInjectivity, typeListsAreApart )
    
    29
    +import GHC.Core.Unify( AmIUnifying(..)
    
    30
    +                     , tcUnifyTysForInjectivity, typeListsAreApart )
    
    30 31
     import GHC.Core.Coercion.Axiom
    
    31 32
     import GHC.Core.TyCo.Subst( elemSubst )
    
    32 33
     
    
    ... ... @@ -685,7 +686,7 @@ hasRelevantGiven eqs_for_me work_args (EqCt { eq_rhs = work_rhs })
    685 686
         relevant (EqCt { eq_ev = ev, eq_lhs = lhs, eq_rhs = rhs_ty })
    
    686 687
            | isGiven ev
    
    687 688
            , TyFamLHS _ lhs_tys <- lhs
    
    688
    -       = isJust (tcUnifyTysForInjectivity True work_tys (rhs_ty:lhs_tys))
    
    689
    +       = isJust (tcUnifyTysForInjectivity Unifying work_tys (rhs_ty:lhs_tys))
    
    689 690
            | otherwise
    
    690 691
            = False
    
    691 692
     
    
    ... ... @@ -693,14 +694,15 @@ getRelevantBranches :: CoAxiom Branched -> [TcType] -> Xi -> [FunDepEqns]
    693 694
     -- Return the FunDepEqns that arise from each relevant branch
    
    694 695
     -- Implements Definition [Relevance]
    
    695 696
     getRelevantBranches ax work_args work_rhs
    
    696
    -  = let res = go [] (fromBranches (coAxiomBranches ax))
    
    697
    -    in pprTrace "getRelevantBranches"
    
    698
    -          (vcat [ text "ax" <+> ppr ax
    
    699
    -                , text "work_args" <+> ppr work_args
    
    700
    -                , text "work_rhs" <+> ppr work_rhs
    
    701
    -                , text "work_tys" <+> ppr work_tys
    
    702
    -                , text "res" <+> ppr res])
    
    703
    -       res
    
    697
    +  = go [] (fromBranches (coAxiomBranches ax))
    
    698
    +--  = let res = go [] (fromBranches (coAxiomBranches ax))
    
    699
    +--    in pprTrace "getRelevantBranches"
    
    700
    +--          (vcat [ text "ax" <+> ppr ax
    
    701
    +--                , text "work_args" <+> ppr work_args
    
    702
    +--                , text "work_rhs" <+> ppr work_rhs
    
    703
    +--                , text "work_tys" <+> ppr work_tys
    
    704
    +--                , text "res" <+> ppr res])
    
    705
    +--       res
    
    704 706
       where
    
    705 707
         work_tys = work_rhs : work_args
    
    706 708
     
    
    ... ... @@ -713,7 +715,7 @@ getRelevantBranches ax work_args work_rhs
    713 715
              is_relevant branch@(CoAxBranch { cab_tvs = qtvs
    
    714 716
                                             , cab_lhs = lhs_tys
    
    715 717
                                             , cab_rhs = rhs_ty })
    
    716
    -            | Just subst <- tcUnifyTysForInjectivity True (rhs_ty:lhs_tys) work_tys
    
    718
    +            | Just subst <- tcUnifyTysForInjectivity Unifying (rhs_ty:lhs_tys) work_tys
    
    717 719
                   -- See (RW1)-(RW3) in Note [Relevance]
    
    718 720
                   -- This is a pre-/unification/ check (not matching)
    
    719 721
                   -- so `subst` can bind both `qtvs` and fvs(work_tys)
    
    ... ... @@ -721,7 +723,7 @@ getRelevantBranches ax work_args work_rhs
    721 723
                       lhs_tys'       = substTys subst' lhs_tys
    
    722 724
                       rhs_ty'        = substTy  subst' rhs_ty
    
    723 725
                       branch_tys'    = rhs_ty' : lhs_tys'
    
    724
    -            , apartnessCheck lhs_tys' branch  -- See (RW4) in Note [Relevance]
    
    726
    +            , noEarlierMatchCheck lhs_tys' branch  -- See (RW4) in Note [Relevance]
    
    725 727
                 = Just (FDEqns { fd_qtvs = qtvs'
    
    726 728
                                , fd_eqs  = zipWith Pair branch_tys' work_tys })
    
    727 729
                                   -- Do not apply the substition to `work_tys`;
    
    ... ... @@ -742,8 +744,7 @@ mkTopOpenFamEqFDs fam_tc inj_flags work_args (EqCt { eq_rhs = work_rhs })
    742 744
         do_one (CoAxBranch { cab_tvs = branch_qtvs
    
    743 745
                            , cab_lhs = branch_lhs_tys
    
    744 746
                            , cab_rhs = branch_rhs })
    
    745
    -      | Just subst <- tcUnifyTysForInjectivity False [branch_rhs] [work_rhs]
    
    746
    -                      -- False: matching, not unifying
    
    747
    +      | Just subst <- tcUnifyTysForInjectivity Matching [branch_rhs] [work_rhs]
    
    747 748
           , let (subst', qtvs') = trim_qtvs subst branch_qtvs
    
    748 749
                 branch_lhs_tys' = substTys subst' branch_lhs_tys
    
    749 750
     -- ToDo
    
    ... ... @@ -1075,7 +1076,7 @@ Note [Exploiting closed type families].
    1075 1076
         A watertight proof that this is the Right Thing is not very easy.  See more
    
    1076 1077
         discussion in #23162, and Section 5.2 of the paper.
    
    1077 1078
     
    
    1078
    -    This test is immplemented in `apartnessCheck` in `getRelevantBranches`.
    
    1079
    +    This test is implemented in `apartnessCheck` in `getRelevantBranches`.
    
    1079 1080
     
    
    1080 1081
     Note [Exploiting open type families]
    
    1081 1082
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~