Simon Peyton Jones pushed to branch wip/T27994 at Glasgow Haskell Compiler / GHC
Commits:
-
f9fcb585
by Simon Peyton Jones at 2026-10-22T17:48:02+01:00
5 changed files:
- compiler/GHC/Core/Coercion/Axiom.hs
- compiler/GHC/Core/Predicate.hs
- compiler/GHC/Core/Unify.hs
- compiler/GHC/Tc/Instance/Family.hs
- compiler/GHC/Tc/Solver/FunDeps.hs
Changes:
| ... | ... | @@ -277,8 +277,8 @@ data CoAxBranch |
| 277 | 277 | -- See Note [CoAxBranch type variables]
|
| 278 | 278 | , cab_eta_tvs :: [TyVar]
|
| 279 | 279 | -- ^ Eta-reduced tyvars
|
| 280 | - -- cab_tvs and cab_lhs may be eta-reduced; see
|
|
| 281 | - -- Note [Eta reduction for data families]
|
|
| 280 | + -- For a data family (only) cab_tvs and cab_lhs may be eta-reduced
|
|
| 281 | + -- See Note [Eta reduction for data families]
|
|
| 282 | 282 | , cab_cvs :: [CoVar]
|
| 283 | 283 | -- ^ Bound coercion variables
|
| 284 | 284 | -- Always empty, for now.
|
| ... | ... | @@ -286,8 +286,14 @@ data CoAxBranch |
| 286 | 286 | -- in GHC.Tc.TyCl
|
| 287 | 287 | , cab_roles :: [Role]
|
| 288 | 288 | -- ^ See Note [CoAxBranch roles]
|
| 289 | + |
|
| 289 | 290 | , cab_lhs :: [Type]
|
| 290 | 291 | -- ^ Type patterns to match against
|
| 292 | + -- For a data family `cab_lhs` may be shorter than the arity
|
|
| 293 | + -- of the data type
|
|
| 294 | + -- For a type family `cab_lhs` is exactly the family arity
|
|
| 295 | + -- See Note [Eta reduction for data families]
|
|
| 296 | + |
|
| 291 | 297 | , cab_rhs :: Type
|
| 292 | 298 | -- ^ Right-hand side of the equality
|
| 293 | 299 | -- See Note [CoAxioms are homogeneous]
|
| ... | ... | @@ -36,7 +36,7 @@ module GHC.Core.Predicate ( |
| 36 | 36 | |
| 37 | 37 | -- Equality left-hand sides
|
| 38 | 38 | CanEqLHS(..), canEqLHS_maybe, canTyFamEqLHS_maybe,
|
| 39 | - canEqLHSKind, canEqLHSType, eqCanEqLHS
|
|
| 39 | + canEqLHSKind, canEqLHSType, eqCanEqLHS, saturatedFamApp_maybe
|
|
| 40 | 40 | |
| 41 | 41 | ) where
|
| 42 | 42 | |
| ... | ... | @@ -61,6 +61,8 @@ import GHC.Utils.Misc |
| 61 | 61 | import GHC.Utils.Panic
|
| 62 | 62 | import GHC.Data.FastString
|
| 63 | 63 | |
| 64 | +import GHC.Exts( inline )
|
|
| 65 | + |
|
| 64 | 66 | |
| 65 | 67 | {- *********************************************************************
|
| 66 | 68 | * *
|
| ... | ... | @@ -783,13 +785,19 @@ canEqLHS_maybe xi |
| 783 | 785 | | otherwise
|
| 784 | 786 | = canTyFamEqLHS_maybe xi
|
| 785 | 787 | |
| 786 | -canTyFamEqLHS_maybe :: Type -> Maybe CanEqLHS
|
|
| 787 | -canTyFamEqLHS_maybe xi
|
|
| 788 | +saturatedFamApp_maybe :: Type -> Maybe (TyCon,[Type])
|
|
| 789 | +saturatedFamApp_maybe xi
|
|
| 788 | 790 | | Just (tc, args) <- tcSplitTyConApp_maybe xi
|
| 789 | 791 | , isTypeFamilyTyCon tc
|
| 790 | 792 | , args `lengthIs` tyConArity tc
|
| 791 | - = Just $ TyFamLHS tc args
|
|
| 793 | + = Just (tc,args)
|
|
| 794 | + | otherwise
|
|
| 795 | + = Nothing
|
|
| 792 | 796 | |
| 797 | +canTyFamEqLHS_maybe :: Type -> Maybe CanEqLHS
|
|
| 798 | +canTyFamEqLHS_maybe xi
|
|
| 799 | + | Just (tc, args) <- inline saturatedFamApp_maybe xi
|
|
| 800 | + = Just $ TyFamLHS tc args
|
|
| 793 | 801 | | otherwise
|
| 794 | 802 | = Nothing
|
| 795 | 803 |
| ... | ... | @@ -17,6 +17,9 @@ module GHC.Core.Unify ( |
| 17 | 17 | UnifyResult, UnifyResultM(..), MaybeApartReason(..),
|
| 18 | 18 | typesCantMatch, typesAreApart, typeListsAreApart,
|
| 19 | 19 | |
| 20 | + -- Finding the fixpoint of a substitution
|
|
| 21 | + niFixSubst,
|
|
| 22 | + |
|
| 20 | 23 | -- Matching a type against a lifted type (coercion)
|
| 21 | 24 | liftCoMatch
|
| 22 | 25 | ) where
|
| ... | ... | @@ -16,6 +16,7 @@ import GHC.Prelude |
| 16 | 16 | import GHC.Driver.DynFlags
|
| 17 | 17 | import GHC.Driver.Env
|
| 18 | 18 | |
| 19 | +import GHC.Core.Predicate( saturatedFamApp_maybe )
|
|
| 19 | 20 | import GHC.Core.FamInstEnv
|
| 20 | 21 | import GHC.Core.Coercion
|
| 21 | 22 | import GHC.Core.TyCon
|
| ... | ... | @@ -715,15 +716,9 @@ reportInjectivityErrors dflags fi_ax axiom inj |
| 715 | 716 | when wrong_bare_rhs $ reportBareVariableInRHSErr fam_tc bare_variables axiom
|
| 716 | 717 | |
| 717 | 718 | -- | Is type headed by a type family application?
|
| 718 | -isTFHeaded :: Type -> Bool
|
|
| 719 | 719 | -- See Note [Verifying injectivity annotation], case 3.
|
| 720 | -isTFHeaded ty | Just ty' <- coreView ty
|
|
| 721 | - = isTFHeaded ty'
|
|
| 722 | -isTFHeaded ty | (TyConApp tc args) <- ty
|
|
| 723 | - , isTypeFamilyTyCon tc
|
|
| 724 | - = args `lengthIs` tyConArity tc
|
|
| 725 | -isTFHeaded _ = False
|
|
| 726 | - |
|
| 720 | +isTFHeaded :: Type -> Bool
|
|
| 721 | +isTFHeaded ty = isJust (saturatedFamApp_maybe ty)
|
|
| 727 | 722 | |
| 728 | 723 | -- | If a RHS is a bare type variable return a set of LHS patterns that are not
|
| 729 | 724 | -- bare type variables.
|
| ... | ... | @@ -24,7 +24,7 @@ import GHC.Tc.Types.Constraint |
| 24 | 24 | import GHC.Core.FamInstEnv
|
| 25 | 25 | import GHC.Core.Coercion
|
| 26 | 26 | import GHC.Core.TyCo.Rep( Type(..) )
|
| 27 | -import GHC.Core.Predicate( EqRel(..) )
|
|
| 27 | +import GHC.Core.Predicate( EqRel(..), saturatedFamApp_maybe )
|
|
| 28 | 28 | import GHC.Core.TyCon
|
| 29 | 29 | import GHC.Core.Unify( AmIUnifying(..)
|
| 30 | 30 | , tcUnifyTysForInjectivity, typeListsAreApart )
|
| ... | ... | @@ -32,7 +32,9 @@ import GHC.Core.Coercion.Axiom |
| 32 | 32 | |
| 33 | 33 | import GHC.Builtin.WiredIn.TypeLits( tryInteractTopFam, tryInteractInertFam )
|
| 34 | 34 | |
| 35 | +import GHC.Types.Var( tyVarKind )
|
|
| 35 | 36 | import GHC.Types.Var.Set
|
| 37 | +import GHC.Types.Var.Env( mkInScopeSet )
|
|
| 36 | 38 | |
| 37 | 39 | import GHC.Utils.Outputable
|
| 38 | 40 | import GHC.Utils.Panic
|
| ... | ... | @@ -609,6 +611,7 @@ tryFamEqFunDeps mode eqs_for_me fam_tc work_args |
| 609 | 611 | |
| 610 | 612 | mkTopFamEqFDs :: TyCon -> [TcType] -> EqCt -> SolverStage [FunDepEqns]
|
| 611 | 613 | mkTopFamEqFDs fam_tc work_args work_item
|
| 614 | + -- NB: work_args exactly saturate fam_tc
|
|
| 612 | 615 | | isOpenTypeFamilyTyCon fam_tc
|
| 613 | 616 | , Injective inj_flags <- tyConInjectivityInfo fam_tc
|
| 614 | 617 | = -- Open, injective type families
|
| ... | ... | @@ -721,6 +724,7 @@ getRelevantBranches ax work_args work_rhs |
| 721 | 724 | mkTopOpenFamEqFDs :: TyCon -> [Bool] -> [TcType] -> EqCt -> TcS [FunDepEqns]
|
| 722 | 725 | -- Implements (INJFAM:Wanted/top-open)
|
| 723 | 726 | -- See Note [Exploiting open type families]
|
| 727 | +-- NB: work_args exactly saturate fam_tc
|
|
| 724 | 728 | mkTopOpenFamEqFDs fam_tc inj_flags work_args (EqCt { eq_rhs = work_rhs })
|
| 725 | 729 | = do { fam_envs <- getFamInstEnvs
|
| 726 | 730 | ; let branches :: [CoAxBranch]
|
| ... | ... | @@ -735,6 +739,7 @@ mkTopOpenFamEqFDs fam_tc inj_flags work_args (EqCt { eq_rhs = work_rhs }) |
| 735 | 739 | | Just {} <- tcUnifyTysForInjectivity Matching [branch_rhs] [work_rhs]
|
| 736 | 740 | -- No need for an apartness check here;
|
| 737 | 741 | -- for open type families there are no "preceding" equations
|
| 742 | + -- Both branch_lhs_tys and work_args exactly saturate fam_tc
|
|
| 738 | 743 | = Just (mkInjectivityFDEqn branch_qtvs (True: inj_flags)
|
| 739 | 744 | (branch_rhs: branch_lhs_tys)
|
| 740 | 745 | (work_rhs: work_args))
|
| ... | ... | @@ -750,9 +755,9 @@ mkLocalFamEqFDs eqs_for_me fam_tc inj_flags work_args work_rhs |
| 750 | 755 | = do { let -- eqns_from_inerts: see (INJFAM:Wanted/other)
|
| 751 | 756 | eqns_from_inerts = mapMaybe do_one eqs_for_me
|
| 752 | 757 | -- eqns_from_self: see (INJFAM:Wanted/Self)
|
| 753 | - eqns_from_self = case tcSplitTyConApp_maybe work_rhs of
|
|
| 754 | - Just (tc,rhs_tys) | tc==fam_tc -> [mk_eqn rhs_tys]
|
|
| 755 | - _ -> []
|
|
| 758 | + eqns_from_self = case saturatedFamApp_maybe work_rhs of
|
|
| 759 | + Just (tc, rhs_tys) | fam_tc == tc -> [mk_eqn rhs_tys]
|
|
| 760 | + _ -> []
|
|
| 756 | 761 | |
| 757 | 762 | ; return (eqns_from_inerts ++ eqns_from_self) }
|
| 758 | 763 | where
|
| ... | ... | @@ -1541,12 +1546,10 @@ instantiateFunDepEqns (FDEqns { fd_qtvs = qtvs, fd_eqs = eqs }) |
| 1541 | 1546 | rev_eqs = reverse eqs
|
| 1542 | 1547 | -- (reverse eqs): See Note [Reverse order of fundep equations]
|
| 1543 | 1548 | |
| 1544 | - empty_subst = emptySubst
|
|
| 1545 | - -- If you re-instate `trim_qtvs`, take care here:
|
|
| 1546 | - -- The kinds of `qtvs` can have free variables, because of `trim_qtvs`
|
|
| 1547 | - -- Example: T27828b
|
|
| 1548 | - -- empty_subst = mkEmptySubst $ mkInScopeSet $
|
|
| 1549 | - -- tyCoVarsOfTypes (map tyVarKind qtvs)
|
|
| 1549 | + -- The kinds of `qtvs` can have free variables;
|
|
| 1550 | + -- in the FDEqns produced by GHC.Tc.Instance.improveFromInstEnv
|
|
| 1551 | + empty_subst = mkEmptySubst $ mkInScopeSet $
|
|
| 1552 | + tyCoVarsOfTypes (map tyVarKind qtvs)
|
|
| 1550 | 1553 | |
| 1551 | 1554 | subst_pair subst (Pair ty1 ty2)
|
| 1552 | 1555 | = Pair (substTy subst' ty1) ty2
|