Adam Gundry pushed to branch wip/amg/castz at Glasgow Haskell Compiler / GHC

Commits:

29 changed files:

Changes:

  • compiler/GHC/Core/Coercion.hs
    ... ... @@ -64,6 +64,8 @@ module GHC.Core.Coercion (
    64 64
             mkForAllCastCo,
    
    65 65
             mkFunResCastCo,
    
    66 66
             mkFunCastCoNoFTF,
    
    67
    +        mkGReflLeftCastCo,
    
    68
    +        mkGReflRightCastCo,
    
    67 69
             applyForAllTy,
    
    68 70
             decomposeFunCastCo,
    
    69 71
     
    
    ... ... @@ -397,7 +399,7 @@ mkSymMCo (MCo co) = MCo (mkSymCo co)
    397 399
     -- | Cast a type by an 'MCoercion'
    
    398 400
     mkCastTyMCo :: Type -> MCoercion -> Type
    
    399 401
     mkCastTyMCo ty MRefl    = ty
    
    400
    -mkCastTyMCo ty (MCo co) = ty `mkCastTy` co
    
    402
    +mkCastTyMCo ty (MCo co) = ty `mkCastTy` CCoercion co
    
    401 403
     
    
    402 404
     mkFunResMCo :: Id -> CastCoercion -> CastCoercion
    
    403 405
     mkFunResMCo _      ReflCastCo    = ReflCastCo
    
    ... ... @@ -412,6 +414,17 @@ mkGReflRightMCo :: Role -> Type -> MCoercionN -> Coercion
    412 414
     mkGReflRightMCo r ty MRefl    = mkReflCo r ty
    
    413 415
     mkGReflRightMCo r ty (MCo co) = mkGReflRightCo r ty co
    
    414 416
     
    
    417
    +mkGReflLeftCastCo :: Role -> Type -> CastCoercion -> Coercion
    
    418
    +mkGReflLeftCastCo r ty ReflCastCo = mkReflCo r ty
    
    419
    +mkGReflLeftCastCo r ty (CCoercion co) = mkGReflLeftCo r ty co
    
    420
    +mkGReflLeftCastCo _r _ty (ZCoercion _ki _cos) = error "AMG TODO mkGReflLeftCastCo"
    
    421
    +
    
    422
    +mkGReflRightCastCo :: Role -> Type -> CastCoercion -> Coercion
    
    423
    +mkGReflRightCastCo r ty ReflCastCo  = mkReflCo r ty
    
    424
    +mkGReflRightCastCo r ty (CCoercion co) = mkGReflRightCo r ty co
    
    425
    +mkGReflRightCastCo _r _ty (ZCoercion _ki _cos) = error "AMG TODO mkGReflRightCastCo"
    
    426
    +
    
    427
    +
    
    415 428
     -- | Like 'mkCoherenceRightCo', but with an 'MCoercion'
    
    416 429
     mkCoherenceRightMCo :: Role -> Type -> MCoercionN -> Coercion -> Coercion
    
    417 430
     mkCoherenceRightMCo _ _  MRefl    co2 = co2
    
    ... ... @@ -526,7 +539,7 @@ decomposePiCos orig_co (Pair orig_k1 orig_k2) orig_args
    526 539
             --      res_co :: t1[ty |> arg_co / a] ~ t2[ty / b]
    
    527 540
           = let arg_co  = mkSelCo SelForAll (mkSymCo co)
    
    528 541
                 res_co  = mkInstCo co (mkGReflLeftCo Nominal ty arg_co)
    
    529
    -            subst1' = extendTCvSubst subst1 a (ty `CastTy` arg_co)
    
    542
    +            subst1' = extendTCvSubst subst1 a (ty `CastTy` CCoercion arg_co)
    
    530 543
                 subst2' = extendTCvSubst subst2 b ty
    
    531 544
             in
    
    532 545
             go (arg_co : acc_arg_cos) (subst1', t1) res_co (subst2', t2) tys
    
    ... ... @@ -1797,10 +1810,10 @@ castCoercionKind1 :: Coercion -> Role -> Type -> Type
    1797 1810
     castCoercionKind1 g r t1 t2 h
    
    1798 1811
       = case g of
    
    1799 1812
           Refl {} -> assert (r == Nominal) $ -- Refl is always Nominal
    
    1800
    -                 mkNomReflCo (mkCastTy t2 h)
    
    1813
    +                 mkNomReflCo (mkCastTy t2 (CCoercion h))
    
    1801 1814
           GRefl _ _ mco -> case mco of
    
    1802
    -           MRefl       -> mkReflCo r (mkCastTy t2 h)
    
    1803
    -           MCo kind_co -> mkGReflMCo r (mkCastTy t1 h)
    
    1815
    +           MRefl       -> mkReflCo r (mkCastTy t2 (CCoercion h))
    
    1816
    +           MCo kind_co -> mkGReflMCo r (mkCastTy t1 (CCoercion h))
    
    1804 1817
                                    (mkSymCo h `mkTransCo` kind_co `mkTransCo` h)
    
    1805 1818
           _ -> castCoercionKind2 g r t1 t2 h h
    
    1806 1819
     
    
    ... ... @@ -2293,8 +2306,10 @@ ty_co_subst !lc role ty
    2293 2306
              else pprPanic "ty_co_subst: covar is not almost devoid" (ppr t)
    
    2294 2307
         go r ty@(LitTy {})     = assert (r == Nominal) $
    
    2295 2308
                                  mkNomReflCo ty
    
    2296
    -    go r (CastTy ty co)    = castCoercionKind (go r ty) (substLeftCo lc co)
    
    2309
    +    go r (CastTy ty cco)    = castCoercionKind (go r ty) (substLeftCo lc co)
    
    2297 2310
                                                             (substRightCo lc co)
    
    2311
    +      where
    
    2312
    +        co = castCoToCo (typeKind ty) cco
    
    2298 2313
         go r (CoercionTy co)   = mkProofIrrelCo r kco (substLeftCo lc co)
    
    2299 2314
                                                       (substRightCo lc co)
    
    2300 2315
           where kco = go Nominal (coercionType co)
    
    ... ... @@ -2617,7 +2632,7 @@ coercion_lr_kind which orig_co
    2617 2632
       where
    
    2618 2633
         go (Refl ty)              = ty
    
    2619 2634
         go (GRefl _ ty MRefl)     = ty
    
    2620
    -    go (GRefl _ ty (MCo co1)) = pickLR which (ty, mkCastTy ty co1)
    
    2635
    +    go (GRefl _ ty (MCo co1)) = pickLR which (ty, mkCastTy ty (CCoercion co1))
    
    2621 2636
         go (TyConAppCo _ tc cos)  = mkTyConApp tc (map go cos)
    
    2622 2637
         go (AppCo co1 co2)        = mkAppTy (go co1) (go co2)
    
    2623 2638
         go (CoVarCo cv)           = go_covar cv
    
    ... ... @@ -2710,7 +2725,7 @@ coercion_lr_kind which orig_co
    2710 2725
                   k2  = coercionRKind k_co
    
    2711 2726
                   tv2 = setTyVarKind tv1 (substTy subst k2)
    
    2712 2727
                   subst' = extendTvSubst (extendSubstInScope subst tv2) tv1 $
    
    2713
    -                       TyVarTy tv2 `mkCastTy` mkSymCo k_co
    
    2728
    +                       TyVarTy tv2 `mkCastTy` CCoercion (mkSymCo k_co)
    
    2714 2729
     
    
    2715 2730
         go_forall_right subst (ForAllCo { fco_tcv = cv1, fco_visR = visR
    
    2716 2731
                                         , fco_kind = k_mco, fco_body = co })
    
    ... ... @@ -2815,13 +2830,15 @@ buildCoercion orig_ty1 orig_ty2 = go orig_ty1 orig_ty2
    2815 2830
         go ty1 ty2 | Just ty1' <- coreView ty1 = go ty1' ty2
    
    2816 2831
                    | Just ty2' <- coreView ty2 = go ty1 ty2'
    
    2817 2832
     
    
    2818
    -    go (CastTy ty1 co) ty2
    
    2819
    -      = let co' = go ty1 ty2
    
    2833
    +    go (CastTy ty1 cco) ty2
    
    2834
    +      = let co = castCoToCo (typeKind ty1) cco
    
    2835
    +            co' = go ty1 ty2
    
    2820 2836
                 r = coercionRole co'
    
    2821 2837
             in  mkCoherenceLeftCo r ty1 co co'
    
    2822 2838
     
    
    2823
    -    go ty1 (CastTy ty2 co)
    
    2824
    -      = let co' = go ty1 ty2
    
    2839
    +    go ty1 (CastTy ty2 cco)
    
    2840
    +      = let co = castCoToCo (typeKind ty2) cco
    
    2841
    +            co' = go ty1 ty2
    
    2825 2842
                 r = coercionRole co'
    
    2826 2843
             in  mkCoherenceRightCo r ty2 co co'
    
    2827 2844
     
    
    ... ... @@ -2858,7 +2875,7 @@ buildCoercion orig_ty1 orig_ty2 = go orig_ty1 orig_ty2
    2858 2875
           where kind_co  = go (tyVarKind tv1) (tyVarKind tv2)
    
    2859 2876
                 in_scope = mkInScopeSet $ tyCoVarsOfType ty2 `unionVarSet` tyCoVarsOfCo kind_co
    
    2860 2877
                 ty2'     = substTyWithInScope in_scope [tv2]
    
    2861
    -                         [mkTyVarTy tv1 `mkCastTy` kind_co]
    
    2878
    +                         [mkTyVarTy tv1 `mkCastTy` CCoercion kind_co]
    
    2862 2879
                              ty2
    
    2863 2880
     
    
    2864 2881
         go (ForAllTy (Bndr cv1 flag1) ty1) (ForAllTy (Bndr cv2 flag2) ty2)
    
    ... ... @@ -2972,7 +2989,7 @@ eqCastCoercionX env ty1 co1 ty2 co2 = eqTypeX env ty1 ty2
    2972 2989
     
    
    2973 2990
     -- | Convert a 'CastCoercion' back into a 'Coercion', using a 'UnivCo' if we
    
    2974 2991
     -- have discarded the original 'Coercion'.
    
    2975
    -castCoToCo :: Type -> CastCoercion -> CoercionR
    
    2992
    +castCoToCo :: Type -> CastCoercion -> Coercion
    
    2976 2993
     castCoToCo _      (CCoercion co)         = co
    
    2977 2994
     castCoToCo lhs_ty (ZCoercion rhs_ty cos) = mkUnivCo ZCoercionProv (map CoVarCo (nonDetEltsUniqSet cos)) Representational lhs_ty rhs_ty
    
    2978 2995
     castCoToCo lhs_ty ReflCastCo             = mkRepReflCo lhs_ty
    

  • compiler/GHC/Core/Coercion.hs-boot
    ... ... @@ -55,3 +55,9 @@ coercionType :: Coercion -> Type
    55 55
     topNormaliseNewType_maybe :: Type -> Maybe (Coercion, Type)
    
    56 56
       -- used to look through newtypes to the right of
    
    57 57
       -- function arrows, in 'GHC.Core.Type.getRuntimeArgTys'
    
    58
    +
    
    59
    +castCoToCo :: Type -> CastCoercion -> Coercion
    
    60
    +isReflexiveCastCo :: Type -> CastCoercion -> Bool
    
    61
    +mkTransCastCo :: HasDebugCallStack => CastCoercion -> CastCoercion -> CastCoercion
    
    62
    +seqCastCoercion :: CastCoercion -> ()
    
    63
    +castCoercionRKind :: HasDebugCallStack => Type -> CastCoercion -> Type

  • compiler/GHC/Core/Coercion/Opt.hs
    ... ... @@ -884,7 +884,7 @@ opt_trans_rule is co1 co2
    884 884
                      (opt_trans is' r1 r2')
    
    885 885
         where
    
    886 886
           is' = is `extendInScopeSet` tv1
    
    887
    -      r2' = substCoWithInScope is' [tv2] [mkCastTy (TyVarTy tv1) kco1] r2
    
    887
    +      r2' = substCoWithInScope is' [tv2] [mkCastTyCo (TyVarTy tv1) kco1] r2
    
    888 888
     
    
    889 889
     -- Push transitivity inside forall
    
    890 890
     -- forall over coercions.
    
    ... ... @@ -1481,7 +1481,7 @@ optForAllCoBndr env sym tcv k_mco
    1481 1481
                    -- override the substitution for the original variable to the
    
    1482 1482
                    -- re-kinded one, suitably casted
    
    1483 1483
                    tv2    = tv1 `setTyVarKind` coercionLKind k_co'
    
    1484
    -               subst2 = (extendTvSubst subst1 tcv (mkTyVarTy tv2 `CastTy` k_co'))
    
    1484
    +               subst2 = (extendTvSubst subst1 tcv (mkTyVarTy tv2 `CastTy` CCoercion k_co'))
    
    1485 1485
                             `extendSubstInScope` tv2
    
    1486 1486
     
    
    1487 1487
               _ -> (subst1, tv1)
    

  • compiler/GHC/Core/FamInstEnv.hs
    ... ... @@ -1485,9 +1485,10 @@ normalise_type ty
    1485 1485
                ; redn <- withLC lc' $ normalise_type ty
    
    1486 1486
                ; return $ mkForAllRedn vis tv' k_redn redn }
    
    1487 1487
         go (TyVarTy tv)    = normalise_tyvar tv
    
    1488
    -    go (CastTy ty co)
    
    1488
    +    go (CastTy ty cco)
    
    1489 1489
           = do { redn <- go ty
    
    1490 1490
                ; lc <- getLC
    
    1491
    +           ; let co = castCoToCo (typeKind ty) cco
    
    1491 1492
                ; let co' = substRightCo lc co
    
    1492 1493
                ; return $ mkCastRedn2 Nominal ty co redn co'
    
    1493 1494
                  --       ^^^^^^^^^^^ uses castCoercionKind2
    

  • compiler/GHC/Core/Lint.hs
    ... ... @@ -1990,9 +1990,10 @@ lintType ty@(ForAllTy {})
    1990 1990
           = do { lintType body_ty
    
    1991 1991
                ; lintForAllBody tcvs body_ty }
    
    1992 1992
     
    
    1993
    -lintType (CastTy ty co)
    
    1993
    +lintType (CastTy ty cco)
    
    1994 1994
       = do { lintType ty
    
    1995 1995
            ; ty_kind <- substTyM (typeKind ty)
    
    1996
    +       ; let co = castCoToCo (typeKind ty) cco -- TODO: maybe show the actual cco in mkCastTyErr/mkCastErr?
    
    1996 1997
            ; co_lk <- lintStarCoercion co
    
    1997 1998
            ; ensureEqTys ty_kind co_lk (mkCastTyErr ty co ty_kind co_lk) }
    
    1998 1999
     
    

  • compiler/GHC/Core/Opt/Arity.hs
    ... ... @@ -2925,7 +2925,7 @@ pushCoTyArg co ty
    2925 2925
     
    
    2926 2926
       | isForAllTy_ty tyL
    
    2927 2927
       = assertPpr (isForAllTy_ty tyR) (ppr co $$ ppr ty) $
    
    2928
    -    Just (ty `mkCastTy` co1, coercionLKind co2, CCoercion co2)
    
    2928
    +    Just (ty `mkCastTyCo` co1, coercionLKind co2, CCoercion co2)
    
    2929 2929
     
    
    2930 2930
       | otherwise
    
    2931 2931
       = Nothing
    

  • compiler/GHC/Core/Reduction.hs
    ... ... @@ -227,7 +227,7 @@ mkGReflRightRedn :: Role -> Type -> CoercionN -> Reduction
    227 227
     mkGReflRightRedn role ty co
    
    228 228
       = mkReduction
    
    229 229
           (mkGReflRightCo role ty co)
    
    230
    -      (mkCastTy ty co)
    
    230
    +      (mkCastTyCo ty co)
    
    231 231
     {-# INLINE mkGReflRightRedn #-}
    
    232 232
     
    
    233 233
     -- | Create a 'Reduction' from a kind cast, in which
    
    ... ... @@ -236,11 +236,11 @@ mkGReflRightRedn role ty co
    236 236
     -- Given @ty :: k1@, @mco :: k1 ~ k2@,
    
    237 237
     -- produces the 'Reduction' @ty ~res_co~> (ty |> mco)@
    
    238 238
     -- at the given 'Role'.
    
    239
    -mkGReflRightMRedn :: Role -> Type -> MCoercionN -> Reduction
    
    239
    +mkGReflRightMRedn :: Role -> Type -> CastCoercion -> Reduction
    
    240 240
     mkGReflRightMRedn role ty mco
    
    241 241
       = mkReduction
    
    242
    -      (mkGReflRightMCo role ty mco)
    
    243
    -      (mkCastTyMCo ty mco)
    
    242
    +      (mkGReflRightCastCo role ty mco)
    
    243
    +      (mkCastTy ty mco)
    
    244 244
     {-# INLINE mkGReflRightMRedn #-}
    
    245 245
     
    
    246 246
     -- | Create a 'Reduction' from a kind cast, in which
    
    ... ... @@ -262,10 +262,10 @@ mkGReflLeftRedn role ty co
    262 262
     -- Given @ty :: k1@, @mco :: k1 ~ k2@,
    
    263 263
     -- produces the 'Reduction' @(ty |> mco) ~res_co~> ty@
    
    264 264
     -- at the given 'Role'.
    
    265
    -mkGReflLeftMRedn :: Role -> Type -> MCoercionN -> Reduction
    
    265
    +mkGReflLeftMRedn :: Role -> Type -> CastCoercion -> Reduction
    
    266 266
     mkGReflLeftMRedn role ty mco
    
    267 267
       = mkReduction
    
    268
    -      (mkGReflLeftMCo role ty mco)
    
    268
    +      (mkGReflLeftCastCo role ty mco)
    
    269 269
           ty
    
    270 270
     {-# INLINE mkGReflLeftMRedn #-}
    
    271 271
     
    
    ... ... @@ -279,7 +279,7 @@ mkCoherenceRightRedn :: Role -> Reduction -> CoercionN -> Reduction
    279 279
     mkCoherenceRightRedn r (Reduction co1 ty2) kco
    
    280 280
       = mkReduction
    
    281 281
           (mkCoherenceRightCo r ty2 kco co1)
    
    282
    -      (mkCastTy ty2 kco)
    
    282
    +      (mkCastTyCo ty2 kco)
    
    283 283
     {-# INLINE mkCoherenceRightRedn #-}
    
    284 284
     
    
    285 285
     -- | Apply a cast to the result of a 'Reduction', using an 'MCoercionN'.
    
    ... ... @@ -314,7 +314,7 @@ mkCastRedn1 r ty cast_co (Reduction co xi)
    314 314
       -- return_co :: (ty |> cast_co) ~r (ty' |> cast_co)
    
    315 315
       = mkReduction
    
    316 316
           (castCoercionKind1 co r ty xi cast_co)
    
    317
    -      (mkCastTy xi cast_co)
    
    317
    +      (mkCastTyCo xi cast_co)
    
    318 318
     {-# INLINE mkCastRedn1 #-}
    
    319 319
     
    
    320 320
     -- | Apply casts on both sides of a 'Reduction' (of the given 'Role').
    
    ... ... @@ -333,7 +333,7 @@ mkCastRedn2 :: Role
    333 333
     mkCastRedn2 r ty cast_co (Reduction nco nty) cast_co'
    
    334 334
       = mkReduction
    
    335 335
           (castCoercionKind2 nco r ty nty cast_co cast_co')
    
    336
    -      (mkCastTy nty cast_co')
    
    336
    +      (mkCastTyCo nty cast_co')
    
    337 337
     {-# INLINE mkCastRedn2 #-}
    
    338 338
     
    
    339 339
     -- | Apply one 'Reduction' to another.
    

  • compiler/GHC/Core/TyCo/FVs.hs
    ... ... @@ -76,6 +76,7 @@ import GHC.Utils.EndoOS
    76 76
     
    
    77 77
     import GHC.Data.Pair
    
    78 78
     
    
    79
    +import Data.Maybe
    
    79 80
     import Data.Semigroup
    
    80 81
     
    
    81 82
     {-
    
    ... ... @@ -667,7 +668,7 @@ tyCoFVsOfType (LitTy {}) f bound_vars acc = emptyFV f bound_vars acc
    667 668
     tyCoFVsOfType (AppTy fun arg)    f bound_vars acc = (tyCoFVsOfType fun `unionFV` tyCoFVsOfType arg) f bound_vars acc
    
    668 669
     tyCoFVsOfType (FunTy _ w arg res)  f bound_vars acc = (tyCoFVsOfType w `unionFV` tyCoFVsOfType arg `unionFV` tyCoFVsOfType res) f bound_vars acc
    
    669 670
     tyCoFVsOfType (ForAllTy bndr ty) f bound_vars acc = tyCoFVsBndr bndr (tyCoFVsOfType ty)  f bound_vars acc
    
    670
    -tyCoFVsOfType (CastTy ty co)     f bound_vars acc = (tyCoFVsOfType ty `unionFV` tyCoFVsOfCo co) f bound_vars acc
    
    671
    +tyCoFVsOfType (CastTy ty co)     f bound_vars acc = (tyCoFVsOfType ty `unionFV` tyCoFVsOfCastCoercion co) f bound_vars acc
    
    671 672
     tyCoFVsOfType (CoercionTy co)    f bound_vars acc = tyCoFVsOfCo co f bound_vars acc
    
    672 673
     
    
    673 674
     tyCoFVsBndr :: ForAllTyBinder -> FV -> FV
    
    ... ... @@ -765,6 +766,11 @@ almost_devoid_co_var_of_mco :: MCoercion -> CoVar -> Bool
    765 766
     almost_devoid_co_var_of_mco MRefl    _  = True
    
    766 767
     almost_devoid_co_var_of_mco (MCo co) cv = almost_devoid_co_var_of_co co cv
    
    767 768
     
    
    769
    +almost_devoid_co_var_of_cast_co :: CastCoercion -> CoVar -> Bool
    
    770
    +almost_devoid_co_var_of_cast_co ReflCastCo    _  = True
    
    771
    +almost_devoid_co_var_of_cast_co (CCoercion co) cv = almost_devoid_co_var_of_co co cv
    
    772
    +almost_devoid_co_var_of_cast_co (ZCoercion ty cos) cv = almost_devoid_co_var_of_type ty cv && not (elemVarSet cv cos)
    
    773
    +
    
    768 774
     almost_devoid_co_var_of_co :: Coercion -> CoVar -> Bool
    
    769 775
     almost_devoid_co_var_of_co (Refl {}) _ = True   -- covar is allowed in Refl and
    
    770 776
     almost_devoid_co_var_of_co (GRefl {}) _ = True  -- GRefl, so we don't look into
    
    ... ... @@ -829,7 +835,7 @@ almost_devoid_co_var_of_type (ForAllTy (Bndr v _) ty) cv
    829 835
       && (v == cv || almost_devoid_co_var_of_type ty cv)
    
    830 836
     almost_devoid_co_var_of_type (CastTy ty co) cv
    
    831 837
       = almost_devoid_co_var_of_type ty cv
    
    832
    -  && almost_devoid_co_var_of_co co cv
    
    838
    +  && almost_devoid_co_var_of_cast_co co cv
    
    833 839
     almost_devoid_co_var_of_type (CoercionTy co) cv
    
    834 840
       = almost_devoid_co_var_of_co co cv
    
    835 841
     
    
    ... ... @@ -866,7 +872,7 @@ visVarsOfType orig_ty = Pair invis_vars vis_vars
    866 872
           = ((`delVarSet` tv) <$> go ty) `mappend`
    
    867 873
             (invisible (tyCoVarsOfType $ varType tv))
    
    868 874
         go (LitTy {}) = mempty
    
    869
    -    go (CastTy ty co) = go ty `mappend` invisible (tyCoVarsOfCo co)
    
    875
    +    go (CastTy ty co) = go ty `mappend` invisible (tyCoVarsOfCastCo co)
    
    870 876
         go (CoercionTy co) = invisible $ tyCoVarsOfCo co
    
    871 877
     
    
    872 878
         invisible vs = Pair vs emptyVarSet
    
    ... ... @@ -1005,7 +1011,7 @@ invisibleVarsOfType = go
    1005 1011
           where (invisibles, visibles) = partitionInvisibleTypes tc tys
    
    1006 1012
         go (ForAllTy tvb ty)  = tyCoFVsBndr tvb $ go ty
    
    1007 1013
         go LitTy{}            = emptyFV
    
    1008
    -    go (CastTy ty co)     = tyCoFVsOfCo co `unionFV` go ty
    
    1014
    +    go (CastTy ty co)     = tyCoFVsOfCastCoercion co `unionFV` go ty
    
    1009 1015
         go (CoercionTy co)    = tyCoFVsOfCo co
    
    1010 1016
     
    
    1011 1017
     -- | Like 'invisibleVarsOfType', but for many types.
    
    ... ... @@ -1096,7 +1102,7 @@ tyConsOfType ty
    1096 1102
                                           go a `unionUniqSets` go b
    
    1097 1103
                                           `unionUniqSets` go_tc (funTyFlagTyCon af)
    
    1098 1104
          go (ForAllTy (Bndr tv _) ty)   = go ty `unionUniqSets` go (varType tv)
    
    1099
    -     go (CastTy ty co)              = go ty `unionUniqSets` go_co co
    
    1105
    +     go (CastTy ty co)              = go ty `unionUniqSets` go_cast_co co
    
    1100 1106
          go (CoercionTy co)             = go_co co
    
    1101 1107
     
    
    1102 1108
          go_co (Refl ty)               = go ty
    
    ... ... @@ -1123,6 +1129,10 @@ tyConsOfType ty
    1123 1129
          go_mco MRefl    = emptyUniqSet
    
    1124 1130
          go_mco (MCo co) = go_co co
    
    1125 1131
     
    
    1132
    +     go_cast_co ReflCastCo = emptyUniqSet
    
    1133
    +     go_cast_co (CCoercion co) = go_co co
    
    1134
    +     go_cast_co (ZCoercion ty _cos) = go ty
    
    1135
    +
    
    1126 1136
          go_cos cos   = foldr (unionUniqSets . go_co)  emptyUniqSet cos
    
    1127 1137
     
    
    1128 1138
          go_tc tc = unitUniqSet tc
    
    ... ... @@ -1256,7 +1266,7 @@ occCheckExpand vs_to_avoid ty
    1256 1266
                           -- Failing that, try to expand a synonym
    
    1257 1267
     
    
    1258 1268
         go cxt (CastTy ty co) =  do { ty' <- go cxt ty
    
    1259
    -                                ; co' <- go_co cxt co
    
    1269
    +                                ; co' <- go_cast_co cxt co
    
    1260 1270
                                     ; return (CastTy ty' co') }
    
    1261 1271
         go cxt (CoercionTy co) = do { co' <- go_co cxt co
    
    1262 1272
                                     ; return (CoercionTy co') }
    
    ... ... @@ -1273,6 +1283,14 @@ occCheckExpand vs_to_avoid ty
    1273 1283
         go_mco _   MRefl = return MRefl
    
    1274 1284
         go_mco ctx (MCo co) = MCo <$> go_co ctx co
    
    1275 1285
     
    
    1286
    +    go_cast_co _   ReflCastCo = return ReflCastCo
    
    1287
    +    go_cast_co ctx (CCoercion co) = CCoercion <$> go_co ctx co
    
    1288
    +    go_cast_co ctx (ZCoercion ty cos) = ZCoercion <$> go ctx ty <*> go_covars ctx cos
    
    1289
    +
    
    1290
    +    go_covars (as, env) cos
    
    1291
    +      | anyVarSet (bad_var_occ as) cos = Nothing
    
    1292
    +      | otherwise = return $ mapVarSet (\cv -> fromMaybe cv (lookupVarEnv env cv)) cos
    
    1293
    +
    
    1276 1294
         ------------------
    
    1277 1295
         go_co cxt (Refl ty)                 = do { ty' <- go cxt ty
    
    1278 1296
                                                  ; return (Refl ty') }
    

  • compiler/GHC/Core/TyCo/Rep.hs
    ... ... @@ -186,7 +186,7 @@ data Type
    186 186
     
    
    187 187
       | CastTy
    
    188 188
             Type
    
    189
    -        KindCoercion  -- ^ A kind cast. The coercion is always nominal.
    
    189
    +        CastCoercion  -- ^ A kind cast. The coercion is always nominal.
    
    190 190
                           -- INVARIANT: The cast is never reflexive \(EQ2)
    
    191 191
                           -- INVARIANT: The Type is not a CastTy (use TransCo instead) \(EQ3)
    
    192 192
                           -- INVARIANT: The Type is not a ForAllTy over a tyvar \(EQ4)
    
    ... ... @@ -2055,7 +2055,7 @@ foldTyCo (TyCoFolder { tcf_view = view
    2055 2055
         go_ty env (TyVarTy tv)      = tyvar env tv
    
    2056 2056
         go_ty env (AppTy t1 t2)     = go_ty env t1 `mappend` go_ty env t2
    
    2057 2057
         go_ty _   (LitTy {})        = mempty
    
    2058
    -    go_ty env (CastTy ty co)    = go_ty env ty `mappend` go_co env co
    
    2058
    +    go_ty env (CastTy ty co)    = go_ty env ty `mappend` go_cast_co env co
    
    2059 2059
         go_ty env (CoercionTy co)   = go_co env co
    
    2060 2060
         go_ty env (FunTy _ w arg res) = go_ty env w `mappend` go_ty env arg `mappend` go_ty env res
    
    2061 2061
         go_ty env (TyConApp _ tys)  = go_tys env tys
    
    ... ... @@ -2071,6 +2071,10 @@ foldTyCo (TyCoFolder { tcf_view = view
    2071 2071
         go_cos _   []     = mempty
    
    2072 2072
         go_cos env (c:cs) = go_co env c `mappend` go_cos env cs
    
    2073 2073
     
    
    2074
    +    go_cast_co _   ReflCastCo = mempty
    
    2075
    +    go_cast_co env (CCoercion co) = go_co env co
    
    2076
    +    go_cast_co env (ZCoercion ty cos) = go_ty env ty `mappend` nonDetStrictFoldVarSet (mappend . covar env) mempty cos
    
    2077
    +
    
    2074 2078
         go_co env (Refl ty)                = go_ty env ty
    
    2075 2079
         go_co env (GRefl _ ty MRefl)       = go_ty env ty
    
    2076 2080
         go_co env (GRefl _ ty (MCo co))    = go_ty env ty `mappend` go_co env co
    
    ... ... @@ -2134,7 +2138,7 @@ typeSize (AppTy t1 t2) = typeSize t1 + typeSize t2
    2134 2138
     typeSize (FunTy _ _ t1 t2)          = typeSize t1 + typeSize t2
    
    2135 2139
     typeSize (ForAllTy (Bndr tv _) t)   = typeSize (varType tv) + typeSize t
    
    2136 2140
     typeSize (TyConApp _ ts)            = 1 + typesSize ts
    
    2137
    -typeSize (CastTy ty co)             = typeSize ty + coercionSize co
    
    2141
    +typeSize (CastTy ty co)             = typeSize ty + castCoercionSize co
    
    2138 2142
     typeSize (CoercionTy co)            = coercionSize co
    
    2139 2143
     
    
    2140 2144
     typesSize :: [Type] -> Int
    

  • compiler/GHC/Core/TyCo/Subst.hs
    ... ... @@ -797,7 +797,7 @@ subst_ty subst ty
    797 797
             !(subst',tv') = substVarBndrUnchecked subst tv
    
    798 798
                             -- Unchecked because subst_ty is used from substTyUnchecked
    
    799 799
         go (LitTy n)         = LitTy $! n
    
    800
    -    go (CastTy ty co)    = (mkCastTy $! (go ty)) $! (subst_co subst co)
    
    800
    +    go (CastTy ty co)    = (mkCastTy $! (go ty)) $! (substCastCo subst co)
    
    801 801
         go (CoercionTy co)   = CoercionTy $! (subst_co subst co)
    
    802 802
     
    
    803 803
     substTyVar :: Subst -> TyVar -> Type
    

  • compiler/GHC/Core/TyCo/Tidy.hs
    ... ... @@ -235,7 +235,7 @@ tidyType env (TyVarTy tv) = TyVarTy $! tidyTyCoVarOcc env tv
    235 235
     tidyType _   t@(TyConApp _ [])      = t -- Preserve sharing if possible
    
    236 236
     tidyType env (TyConApp tycon tys)   = TyConApp tycon $! tidyTypes env tys
    
    237 237
     tidyType env (AppTy fun arg)        = (AppTy $! (tidyType env fun)) $! (tidyType env arg)
    
    238
    -tidyType env (CastTy ty co)         = (CastTy $! tidyType env ty) $! (tidyCo env co)
    
    238
    +tidyType env (CastTy ty co)         = (CastTy $! tidyType env ty) $! (tidyCastCo env co)
    
    239 239
     tidyType env (CoercionTy co)        = CoercionTy $! (tidyCo env co)
    
    240 240
     tidyType env ty@(FunTy _ w arg res) = let { !w'   = tidyType env w
    
    241 241
                                               ; !arg' = tidyType env arg
    

  • compiler/GHC/Core/Type.hs
    ... ... @@ -73,6 +73,7 @@ module GHC.Core.Type (
    73 73
             getLevity, levityType_maybe,
    
    74 74
     
    
    75 75
             mkCastTy, mkCoercionTy, splitCastTy_maybe,
    
    76
    +        mkCastTyCo,
    
    76 77
     
    
    77 78
             ErrorMsgType, pprUserTypeErrorTy,
    
    78 79
     
    
    ... ... @@ -236,6 +237,7 @@ import GHC.Core.TyCo.FVs
    236 237
     import GHC.Types.Var
    
    237 238
     import GHC.Types.Var.Env
    
    238 239
     import GHC.Types.Var.Set
    
    240
    +import GHC.Types.Unique.Set
    
    239 241
     
    
    240 242
     import GHC.Core.TyCon
    
    241 243
     import GHC.Builtin.Types.Prim
    
    ... ... @@ -258,9 +260,12 @@ import {-# SOURCE #-} GHC.Core.Coercion
    258 260
        , mkSymCo, mkTransCo, mkSelCo, mkLRCo, mkInstCo
    
    259 261
        , mkKindCo, mkSubCo, mkFunCo, funRole
    
    260 262
        , decomposePiCos, coercionKind
    
    261
    -   , coercionRKind, coercionType
    
    262
    -   , isReflexiveCo, seqCo
    
    263
    +   , coercionType
    
    264
    +   , isReflexiveCastCo, seqCo
    
    263 265
        , topNormaliseNewType_maybe
    
    266
    +   , mkTransCastCo
    
    267
    +   , seqCastCoercion, castCoercionRKind
    
    268
    +   , castCoToCo
    
    264 269
        )
    
    265 270
     import {-# SOURCE #-} GHC.Tc.Utils.TcType ( isConcreteTyVar )
    
    266 271
     
    
    ... ... @@ -514,12 +519,16 @@ expandTypeSynonyms ty
    514 519
         go subst (ForAllTy (Bndr tv vis) t)
    
    515 520
           = let (subst', tv') = substVarBndrUsing go subst tv in
    
    516 521
             ForAllTy (Bndr tv' vis) (go subst' t)
    
    517
    -    go subst (CastTy ty co)  = mkCastTy (go subst ty) (go_co subst co)
    
    522
    +    go subst (CastTy ty co)  = mkCastTy (go subst ty) (go_cast_co subst co)
    
    518 523
         go subst (CoercionTy co) = mkCoercionTy (go_co subst co)
    
    519 524
     
    
    520 525
         go_mco _     MRefl    = MRefl
    
    521 526
         go_mco subst (MCo co) = MCo (go_co subst co)
    
    522 527
     
    
    528
    +    go_cast_co _ ReflCastCo = ReflCastCo
    
    529
    +    go_cast_co subst (CCoercion co) = CCoercion (go_co subst co)
    
    530
    +    go_cast_co subst (ZCoercion ty cos) = ZCoercion (go subst ty) (substCoVarSet subst cos)
    
    531
    +
    
    523 532
         go_co subst (Refl ty)
    
    524 533
           = mkNomReflCo (go subst ty)
    
    525 534
         go_co subst (GRefl r ty mco)
    
    ... ... @@ -905,7 +914,7 @@ mapTyCoX (TyCoMapper { tcm_tyvar = tyvar
    905 914
         go_ty !env (TyVarTy tv)    = tyvar env tv
    
    906 915
         go_ty !env (AppTy t1 t2)   = mkAppTy <$> go_ty env t1 <*> go_ty env t2
    
    907 916
         go_ty !_   ty@(LitTy {})   = return ty
    
    908
    -    go_ty !env (CastTy ty co)  = mkCastTy <$> go_ty env ty <*> go_co env co
    
    917
    +    go_ty !env (CastTy ty co)  = mkCastTy <$> go_ty env ty <*> go_cast_co env co
    
    909 918
         go_ty !env (CoercionTy co) = CoercionTy <$> go_co env co
    
    910 919
     
    
    911 920
         go_ty !env ty@(FunTy _ w arg res)
    
    ... ... @@ -936,6 +945,10 @@ mapTyCoX (TyCoMapper { tcm_tyvar = tyvar
    936 945
         go_mco !_   MRefl    = return MRefl
    
    937 946
         go_mco !env (MCo co) = MCo <$> (go_co env co)
    
    938 947
     
    
    948
    +    go_cast_co !_   ReflCastCo = return ReflCastCo
    
    949
    +    go_cast_co !env (CCoercion co) = CCoercion <$> (go_co env co)
    
    950
    +    go_cast_co !env (ZCoercion ty cos) = ZCoercion <$> go_ty env ty <*> (coVarsOfCos <$> mapM (covar env) (nonDetEltsUniqSet cos)) -- TODO
    
    951
    +
    
    939 952
         go_co :: env -> Coercion -> m Coercion
    
    940 953
         go_co !env (Refl ty)                  = Refl <$> go_ty env ty
    
    941 954
         go_co !env (GRefl r ty mco)           = mkGReflCo r <$> go_ty env ty <*> go_mco env mco
    
    ... ... @@ -1020,10 +1033,10 @@ isTyVarTy ty = isJust (getTyVar_maybe ty)
    1020 1033
     
    
    1021 1034
     -- | If the type is a tyvar, possibly under a cast, returns it, along
    
    1022 1035
     -- with the coercion. Thus, the co is :: kind tv ~N kind ty
    
    1023
    -getCastedTyVar_maybe :: Type -> Maybe (TyVar, CoercionN)
    
    1036
    +getCastedTyVar_maybe :: Type -> Maybe (TyVar, CastCoercion)
    
    1024 1037
     getCastedTyVar_maybe ty = case coreFullView ty of
    
    1025 1038
       CastTy (TyVarTy tv) co -> Just (tv, co)
    
    1026
    -  TyVarTy tv             -> Just (tv, mkReflCo Nominal (tyVarKind tv))
    
    1039
    +  TyVarTy tv             -> Just (tv, ReflCastCo)
    
    1027 1040
       _                      -> Nothing
    
    1028 1041
     
    
    1029 1042
     
    
    ... ... @@ -1063,9 +1076,10 @@ type checker (e.g. when matching type-function equations).
    1063 1076
     -- | Applies a type to another, as in e.g. @k a@
    
    1064 1077
     mkAppTy :: Type -> Type -> Type
    
    1065 1078
       -- See Note [Respecting definitional equality], invariant (EQ1).
    
    1066
    -mkAppTy (CastTy fun_ty co) arg_ty
    
    1067
    -  | ([arg_co], res_co) <- decomposePiCos co (coercionKind co) [arg_ty]
    
    1068
    -  = (fun_ty `mkAppTy` (arg_ty `mkCastTy` arg_co)) `mkCastTy` res_co
    
    1079
    +mkAppTy (CastTy fun_ty cco) arg_ty
    
    1080
    +  | let co = castCoToCo (typeKind fun_ty) cco -- TOOD: can we get rid of this?
    
    1081
    +  , ([arg_co], res_co) <- decomposePiCos co (coercionKind co) [arg_ty]
    
    1082
    +  = (fun_ty `mkAppTy` (arg_ty `mkCastTy` CCoercion arg_co)) `mkCastTy` CCoercion res_co
    
    1069 1083
     
    
    1070 1084
     mkAppTy (TyConApp tc tys) ty2 = mkTyConApp tc (tys ++ [ty2])
    
    1071 1085
     mkAppTy ty1               ty2 = AppTy ty1 ty2
    
    ... ... @@ -1084,15 +1098,16 @@ mkAppTy ty1 ty2 = AppTy ty1 ty2
    1084 1098
     
    
    1085 1099
     mkAppTys :: Type -> [Type] -> Type
    
    1086 1100
     mkAppTys ty1                []   = ty1
    
    1087
    -mkAppTys (CastTy fun_ty co) arg_tys  -- much more efficient then nested mkAppTy
    
    1101
    +mkAppTys (CastTy fun_ty cco) arg_tys  -- much more efficient then nested mkAppTy
    
    1088 1102
                                          -- Why do this? See (EQ1) of
    
    1089 1103
                                          -- Note [Respecting definitional equality]
    
    1090 1104
                                          -- in GHC.Core.TyCo.Rep
    
    1091
    -  = foldl' AppTy ((mkAppTys fun_ty casted_arg_tys) `mkCastTy` res_co) leftovers
    
    1105
    +  = foldl' AppTy ((mkAppTys fun_ty casted_arg_tys) `mkCastTy` CCoercion res_co) leftovers
    
    1092 1106
       where
    
    1107
    +    co = castCoToCo (typeKind fun_ty) cco
    
    1093 1108
         (arg_cos, res_co) = decomposePiCos co (coercionKind co) arg_tys
    
    1094 1109
         (args_to_cast, leftovers) = splitAtList arg_cos arg_tys
    
    1095
    -    casted_arg_tys = zipWith mkCastTy args_to_cast arg_cos
    
    1110
    +    casted_arg_tys = zipWith (\ ty co -> mkCastTy ty (CCoercion co)) args_to_cast arg_cos
    
    1096 1111
     mkAppTys (TyConApp tc tys1) tys2 = mkTyConApp tc (tys1 ++ tys2)
    
    1097 1112
     mkAppTys ty1                tys2 = foldl' AppTy ty1 tys2
    
    1098 1113
     
    
    ... ... @@ -1647,16 +1662,19 @@ newTyConInstRhs tycon tys
    1647 1662
     *                                                                      *
    
    1648 1663
     ********************************************************************* -}
    
    1649 1664
     
    
    1650
    -splitCastTy_maybe :: Type -> Maybe (Type, Coercion)
    
    1665
    +splitCastTy_maybe :: Type -> Maybe (Type, CastCoercion)
    
    1651 1666
     splitCastTy_maybe ty
    
    1652 1667
       | CastTy ty' co <- coreFullView ty = Just (ty', co)
    
    1653 1668
       | otherwise                        = Nothing
    
    1654 1669
     
    
    1670
    +mkCastTyCo :: Type -> Coercion -> Type
    
    1671
    +mkCastTyCo ty co = mkCastTy ty (CCoercion co)
    
    1672
    +
    
    1655 1673
     -- | Make a 'CastTy'. The Coercion must be nominal. Checks the
    
    1656 1674
     -- Coercion for reflexivity, dropping it if it's reflexive.
    
    1657 1675
     -- See @Note [Respecting definitional equality]@ in "GHC.Core.TyCo.Rep"
    
    1658
    -mkCastTy :: Type -> Coercion -> Type
    
    1659
    -mkCastTy orig_ty co | isReflexiveCo co = orig_ty  -- (EQ2) from the Note
    
    1676
    +mkCastTy :: Type -> CastCoercion -> Type
    
    1677
    +mkCastTy orig_ty co | isReflexiveCastCo (typeKind orig_ty) co = orig_ty  -- (EQ2) from the Note
    
    1660 1678
     -- NB: Do the slow check here. This is important to keep the splitXXX
    
    1661 1679
     -- functions working properly. Otherwise, we may end up with something
    
    1662 1680
     -- like (((->) |> something_reflexive_but_not_obviously_so) biz baz)
    
    ... ... @@ -1666,7 +1684,7 @@ mkCastTy orig_ty co = mk_cast_ty orig_ty co
    1666 1684
     
    
    1667 1685
     -- | Like 'mkCastTy', but avoids checking the coercion for reflexivity,
    
    1668 1686
     -- as that can be expensive.
    
    1669
    -mk_cast_ty :: Type -> Coercion -> Type
    
    1687
    +mk_cast_ty :: Type -> CastCoercion -> Type
    
    1670 1688
     mk_cast_ty orig_ty co = go orig_ty
    
    1671 1689
       where
    
    1672 1690
         go :: Type -> Type
    
    ... ... @@ -1675,14 +1693,14 @@ mk_cast_ty orig_ty co = go orig_ty
    1675 1693
     
    
    1676 1694
         go (CastTy ty co1)
    
    1677 1695
           -- (EQ3) from the Note
    
    1678
    -      = mkCastTy ty (co1 `mkTransCo` co)
    
    1696
    +      = mkCastTy ty (co1 `mkTransCastCo` co)
    
    1679 1697
               -- call mkCastTy again for the reflexivity check
    
    1680 1698
     
    
    1681 1699
         go (ForAllTy (Bndr tv vis) inner_ty)
    
    1682 1700
           -- (EQ4) from the Note
    
    1683 1701
           -- See Note [Weird typing rule for ForAllTy] in GHC.Core.TyCo.Rep.
    
    1684 1702
           | isTyVar tv
    
    1685
    -      , let fvs = tyCoVarsOfCo co
    
    1703
    +      , let fvs = tyCoVarsOfCastCo co
    
    1686 1704
           = -- have to make sure that pushing the co in doesn't capture the bound var!
    
    1687 1705
             if tv `elemVarSet` fvs
    
    1688 1706
             then let empty_subst = mkEmptySubst (mkInScopeSet fvs)
    
    ... ... @@ -2546,7 +2564,7 @@ seqType (AppTy t1 t2) = seqType t1 `seq` seqType t2
    2546 2564
     seqType (FunTy _ w t1 t2)           = seqType w `seq` seqType t1 `seq` seqType t2
    
    2547 2565
     seqType (TyConApp tc tys)           = tc `seq` seqTypes tys
    
    2548 2566
     seqType (ForAllTy (Bndr tv _) ty)   = seqType (varType tv) `seq` seqType ty
    
    2549
    -seqType (CastTy ty co)              = seqType ty `seq` seqCo co
    
    2567
    +seqType (CastTy ty co)              = seqType ty `seq` seqCastCoercion co
    
    2550 2568
     seqType (CoercionTy co)             = seqCo co
    
    2551 2569
     
    
    2552 2570
     seqTypes :: [Type] -> ()
    
    ... ... @@ -2640,7 +2658,7 @@ typeKind (TyConApp tc tys) = piResultTys (tyConKind tc) tys
    2640 2658
     typeKind (LitTy l)              = typeLiteralKind l
    
    2641 2659
     typeKind (FunTy { ft_af = af }) = liftedTypeOrConstraintKind (funTyFlagResultTypeOrConstraint af)
    
    2642 2660
     typeKind (TyVarTy tyvar)        = tyVarKind tyvar
    
    2643
    -typeKind (CastTy _ty co)        = coercionRKind co
    
    2661
    +typeKind (CastTy ty co)         = castCoercionRKind (typeKind ty) co
    
    2644 2662
     typeKind (CoercionTy co)        = coercionType co
    
    2645 2663
     
    
    2646 2664
     typeKind (AppTy fun arg)
    

  • compiler/GHC/Core/Type.hs-boot
    ... ... @@ -4,7 +4,7 @@ module GHC.Core.Type where
    4 4
     
    
    5 5
     import GHC.Prelude
    
    6 6
     import {-# SOURCE #-} GHC.Core.TyCon
    
    7
    -import {-# SOURCE #-} GHC.Core.TyCo.Rep( Type, Coercion )
    
    7
    +import {-# SOURCE #-} GHC.Core.TyCo.Rep( Type, CastCoercion )
    
    8 8
     import GHC.Utils.Misc
    
    9 9
     import GHC.Types.Var( FunTyFlag, TyVar )
    
    10 10
     import GHC.Types.Basic( TypeOrConstraint )
    
    ... ... @@ -16,7 +16,7 @@ chooseFunTyFlag :: HasDebugCallStack => Type -> Type -> FunTyFlag
    16 16
     typeKind         :: HasDebugCallStack => Type -> Type
    
    17 17
     isCoercionTy     :: Type -> Bool
    
    18 18
     mkAppTy          :: Type -> Type -> Type
    
    19
    -mkCastTy         :: Type -> Coercion -> Type
    
    19
    +mkCastTy         :: Type -> CastCoercion -> Type
    
    20 20
     mkTyConApp       :: TyCon -> [Type] -> Type
    
    21 21
     getLevity        :: HasDebugCallStack => Type -> Type
    
    22 22
     getTyVar_maybe   :: Type -> Maybe TyVar
    

  • compiler/GHC/Core/Unify.hs
    ... ... @@ -1582,12 +1582,13 @@ type AmIUnifying = Bool -- True <=> Unifying
    1582 1582
                               -- False <=> Matching
    
    1583 1583
     
    
    1584 1584
     type InType      = Type       -- Before applying the RnEnv2
    
    1585
    -type OutCoercion = Coercion   -- After applying the RnEnv2
    
    1585
    +type OutCastCoercion = CastCoercion  -- After applying the RnEnv2
    
    1586
    +
    
    1586 1587
     
    
    1587 1588
     
    
    1588 1589
     unify_ty :: UMEnv
    
    1589 1590
              -> InType -> InType  -- Types to be unified
    
    1590
    -         -> OutCoercion       -- A nominal coercion between their kinds
    
    1591
    +         -> OutCastCoercion   -- A nominal coercion between their kinds
    
    1591 1592
                                   -- OutCoercion: the RnEnv has already been applied
    
    1592 1593
                                   -- When matching, the coercion is in "target space",
    
    1593 1594
                                   --   not "template space"
    
    ... ... @@ -1609,28 +1610,28 @@ unify_ty env ty1 ty2 kco
    1609 1610
       | Just ty2' <- coreView ty2 = unify_ty env ty1 ty2' kco
    
    1610 1611
     
    
    1611 1612
     unify_ty env (CastTy ty1 co1) ty2 kco
    
    1612
    -  | mentionsForAllBoundTyVarsL env (tyCoVarsOfCo co1)
    
    1613
    +  | mentionsForAllBoundTyVarsL env (tyCoVarsOfCastCo co1)
    
    1613 1614
         -- See (KCU1) in Note [Kind coercions in Unify]
    
    1614 1615
       = maybeApart MARCast  -- See (KCU2)
    
    1615 1616
     
    
    1616 1617
       | um_unif env
    
    1617
    -  = unify_ty env ty1 ty2 (co1 `mkTransCo` kco)
    
    1618
    +  = unify_ty env ty1 ty2 (co1 `mkTransCastCo` kco)
    
    1618 1619
     
    
    1619 1620
       | otherwise -- We are matching, not unifying
    
    1620 1621
       = do { subst <- getSubst env
    
    1621
    -       ; let co' = substCo subst co1
    
    1622
    +       ; let co' = substCastCo subst co1
    
    1622 1623
              -- We match left-to-right, so the free template vars of the
    
    1623 1624
              -- coercion should already have been matched.
    
    1624 1625
              -- See Note [Matching in the presence of casts (1)]
    
    1625 1626
              -- NB: co1 does not mention forall-bound vars, so no need to rename
    
    1626
    -       ; unify_ty env ty1 ty2 (co' `mkTransCo` kco) }
    
    1627
    +       ; unify_ty env ty1 ty2 (co' `mkTransCastCo` kco) }
    
    1627 1628
     
    
    1628 1629
     unify_ty env ty1 (CastTy ty2 co2) kco
    
    1629
    -  | mentionsForAllBoundTyVarsR env (tyCoVarsOfCo co2)
    
    1630
    +  | mentionsForAllBoundTyVarsR env (tyCoVarsOfCastCo co2)
    
    1630 1631
         -- See (KCU1) in Note [Kind coercions in Unify]
    
    1631 1632
       = maybeApart MARCast  -- See (KCU2)
    
    1632 1633
       | otherwise
    
    1633
    -  = unify_ty env ty1 ty2 (kco `mkTransCo` mkSymCo co2)
    
    1634
    +  = unify_ty env ty1 ty2 (kco `mkTransCastCo` mkSymCastCo (typeKind ty2) co2)
    
    1634 1635
         -- NB: co2 does not mention forall-bound variables
    
    1635 1636
     
    
    1636 1637
     -- Applications need a bit of care!
    
    ... ... @@ -1647,7 +1648,7 @@ unify_ty _ (LitTy x) (LitTy y) _kco | x == y = return ()
    1647 1648
     
    
    1648 1649
     unify_ty env (ForAllTy (Bndr tv1 _) ty1) (ForAllTy (Bndr tv2 _) ty2) kco
    
    1649 1650
       -- ToDo: See Note [Unifying coercion-foralls]
    
    1650
    -  = do { unify_ty env (varType tv1) (varType tv2) (mkNomReflCo liftedTypeKind)
    
    1651
    +  = do { unify_ty env (varType tv1) (varType tv2) ReflCastCo
    
    1651 1652
            ; let env' = umRnBndr2 env tv1 tv2
    
    1652 1653
            ; unify_ty env' ty1 ty2 kco }
    
    1653 1654
     
    
    ... ... @@ -1658,7 +1659,7 @@ unify_ty env (CoercionTy co1) (CoercionTy co2) kco
    1658 1659
                CoVarCo cv
    
    1659 1660
                  | not (um_unif env)
    
    1660 1661
                  , not (cv `elemVarEnv` c_subst)   -- Not forall-bound
    
    1661
    -             , let (_mult_co, co_l, co_r) = decomposeFunCo kco
    
    1662
    +             , let (_mult_co, co_l, co_r) = decomposeFunCo (castCoToCo (typeKind (CoercionTy co1)) kco)
    
    1662 1663
                          -- Because the coercion is used in a type, it should be safe to
    
    1663 1664
                          -- ignore the multiplicity coercion, _mult_co
    
    1664 1665
                           -- cv :: t1 ~ t2
    
    ... ... @@ -1678,7 +1679,7 @@ unify_ty env (TyVarTy tv1) ty2 kco
    1678 1679
     
    
    1679 1680
     unify_ty env ty1 (TyVarTy tv2) kco
    
    1680 1681
       | um_unif env  -- If unifying, can swap args; but not when matching
    
    1681
    -  = uVarOrFam (umSwapRn env) (TyVarLHS tv2) ty1 (mkSymCo kco)
    
    1682
    +  = uVarOrFam (umSwapRn env) (TyVarLHS tv2) ty1 (mkSymCastCo (typeKind ty1) kco)
    
    1682 1683
     
    
    1683 1684
     -- Deal with TyConApps
    
    1684 1685
     unify_ty env ty1 ty2 kco
    
    ... ... @@ -1689,7 +1690,7 @@ unify_ty env ty1 ty2 kco
    1689 1690
     
    
    1690 1691
       | um_unif env
    
    1691 1692
       , Just (tc,tys) <- mb_sat_fam_app2
    
    1692
    -  = uVarOrFam (umSwapRn env) (TyFamLHS tc tys) ty1 (mkSymCo kco)
    
    1693
    +  = uVarOrFam (umSwapRn env) (TyFamLHS tc tys) ty1 (mkSymCastCo (typeKind ty1) kco)
    
    1693 1694
     
    
    1694 1695
       -- Handle oversaturated type families. Suppose we have
    
    1695 1696
       --     (F a b) ~ (c d)    where F has arity 1
    
    ... ... @@ -1760,8 +1761,8 @@ unify_ty_app env ty1 ty1args ty2 ty2args
    1760 1761
       = do { let ki1 = typeKind ty1
    
    1761 1762
                  ki2 = typeKind ty2
    
    1762 1763
                -- See Note [Kind coercions in Unify]
    
    1763
    -       ; unify_ty  env ki1 ki2 (mkNomReflCo liftedTypeKind)
    
    1764
    -       ; unify_ty  env ty1 ty2 (mkNomReflCo ki2)
    
    1764
    +       ; unify_ty  env ki1 ki2 ReflCastCo
    
    1765
    +       ; unify_ty  env ty1 ty2 ReflCastCo -- TODO: simplify following comment?
    
    1765 1766
                      -- Very important: 'ki2' not 'ki1'
    
    1766 1767
                      -- See Note [Matching in the presence of casts (2)]
    
    1767 1768
            ; unify_tys env ty1args ty2args }
    
    ... ... @@ -1775,7 +1776,7 @@ unify_tys env orig_xs orig_ys
    1775 1776
         go []     []     = return ()
    
    1776 1777
         go (x:xs) (y:ys)
    
    1777 1778
           -- See Note [Kind coercions in Unify]
    
    1778
    -      = do { unify_ty env x y (mkNomReflCo $ typeKind y)
    
    1779
    +      = do { unify_ty env x y ReflCastCo -- TODO: simplify following comment?
    
    1779 1780
                      -- Very important: 'y' not 'x'
    
    1780 1781
                      -- See Note [Matching in the presence of casts (2)]
    
    1781 1782
                ; go xs ys }
    
    ... ... @@ -1784,7 +1785,7 @@ unify_tys env orig_xs orig_ys
    1784 1785
           -- See Note [Polykinded tycon applications]
    
    1785 1786
     
    
    1786 1787
     ---------------------------------
    
    1787
    -uVarOrFam :: UMEnv -> CanEqLHS -> InType -> OutCoercion -> UM ()
    
    1788
    +uVarOrFam :: UMEnv -> CanEqLHS -> InType -> OutCastCoercion -> UM ()
    
    1788 1789
     -- Invariants: (a) If ty1 is a TyFamLHS, then ty2 is NOT a TyVarTy
    
    1789 1790
     --             (b) both args have had coreView already applied
    
    1790 1791
     -- Why saturated?  See (ATF4) in Note [Apartness and type families]
    
    ... ... @@ -1811,7 +1812,7 @@ uVarOrFam env ty1 ty2 kco
    1811 1812
         -----------------------------
    
    1812 1813
         -- LHS is a type variable
    
    1813 1814
         -- The sequence of tests is very similar to go_tv
    
    1814
    -    go :: SwapFlag -> UMState -> CanEqLHS -> InType -> OutCoercion -> UM ()
    
    1815
    +    go :: SwapFlag -> UMState -> CanEqLHS -> InType -> OutCastCoercion -> UM ()
    
    1815 1816
         go swapped substs lhs@(TyVarLHS tv1) ty2 kco
    
    1816 1817
           | Just ty1' <- lookupVarEnv (um_tv_env substs) tv1'
    
    1817 1818
           = -- We already have a substitution for tv1
    
    ... ... @@ -1856,7 +1857,7 @@ uVarOrFam env ty1 ty2 kco
    1856 1857
           | um_unif env
    
    1857 1858
           , NotSwapped <- swapped  -- If we have swapped already, don't do so again
    
    1858 1859
           , Just lhs2 <- canEqLHS_maybe ty2
    
    1859
    -      = go IsSwapped substs lhs2 (mkTyVarTy tv1) (mkSymCo kco)
    
    1860
    +      = go IsSwapped substs lhs2 (mkTyVarTy tv1) (mkSymCastCo (varType tv1) kco)
    
    1860 1861
     
    
    1861 1862
           | occurs_check = maybeApart MARInfinite   -- Occurs check
    
    1862 1863
           | otherwise    = surelyApart
    
    ... ... @@ -1864,7 +1865,7 @@ uVarOrFam env ty1 ty2 kco
    1864 1865
           where
    
    1865 1866
             tv1'            = umRnOccL env tv1
    
    1866 1867
             ty2_fvs         = tyCoVarsOfType ty2
    
    1867
    -        rhs             = ty2 `mkCastTy` mkSymCo kco
    
    1868
    +        rhs             = ty2 `mkCastTy` mkSymCastCo (varType tv1') kco
    
    1868 1869
             tv1_is_bindable | not (tv1' `elemVarSet` foralld_tvs)
    
    1869 1870
                               -- tv1' is not forall-bound, but tv1 can still differ
    
    1870 1871
                               -- from tv1; see Note [Cloning the template binders]
    
    ... ... @@ -1928,13 +1929,14 @@ uVarOrFam env ty1 ty2 kco
    1928 1929
           | um_unif env
    
    1929 1930
           , NotSwapped <- swapped
    
    1930 1931
           , Just lhs2 <- canEqLHS_maybe ty2
    
    1931
    -      = go IsSwapped substs lhs2 (mkTyConApp tc1 tys1) (mkSymCo kco)
    
    1932
    +      , let ty1' = mkTyConApp tc1 tys1
    
    1933
    +      = go IsSwapped substs lhs2 ty1' (mkSymCastCo (typeKind ty1') kco)
    
    1932 1934
     
    
    1933 1935
           | otherwise   -- See (ATF5) in Note [Apartness and type families]
    
    1934 1936
           = surelyApart
    
    1935 1937
     
    
    1936 1938
           where
    
    1937
    -        rhs = ty2 `mkCastTy` mkSymCo kco
    
    1939
    +        rhs = ty2 `mkCastTy` mkSymCastCo (typeKind (mkTyConApp tc1 tys1)) kco
    
    1938 1940
     
    
    1939 1941
         -----------------------------
    
    1940 1942
         -- go_fam_fam: LHS and RHS are both saturated type-family applications,
    
    ... ... @@ -1971,7 +1973,7 @@ uVarOrFam env ty1 ty2 kco
    1971 1973
              | otherwise
    
    1972 1974
              = return ()
    
    1973 1975
     
    
    1974
    -       rhs1 = mkTyConApp tc tys2 `mkCastTy` mkSymCo kco
    
    1976
    +       rhs1 = mkTyConApp tc tys2 `mkCastTy` mkSymCastCo (typeKind (mkTyConApp tc tys1)) kco -- TODO: correct?
    
    1975 1977
            rhs2 = mkTyConApp tc tys1 `mkCastTy` kco
    
    1976 1978
     
    
    1977 1979
     
    
    ... ... @@ -2335,9 +2337,10 @@ ty_co_match menv subst ty co lkco rkco
    2335 2337
         noneSet f = allVarSet (not . f)
    
    2336 2338
     
    
    2337 2339
     ty_co_match menv subst ty co lkco rkco
    
    2338
    -  | CastTy ty' co' <- ty
    
    2340
    +  | CastTy ty' cco' <- ty
    
    2339 2341
          -- See Note [Matching in the presence of casts (1)]
    
    2340 2342
       = let empty_subst  = mkEmptySubst (rnInScopeSet (me_env menv))
    
    2343
    +        co' = castCoToCo (typeKind ty') cco'
    
    2341 2344
             substed_co_l = substCo (liftEnvSubstLeft empty_subst subst)  co'
    
    2342 2345
             substed_co_r = substCo (liftEnvSubstRight empty_subst subst) co'
    
    2343 2346
         in
    
    ... ... @@ -2448,7 +2451,7 @@ ty_co_match menv subst ty co1 lkco rkco
    2448 2451
       -- But transitive coercions are not helpful. Therefore we deal
    
    2449 2452
       -- with it here: we do recursion on the smaller reflexive coercion,
    
    2450 2453
       -- while propagating the correct kind coercions.
    
    2451
    -  = let kco' = mkSymCo co
    
    2454
    +  = let kco' = mkSymCo (castCoToCo (typeKind t) co)
    
    2452 2455
         in ty_co_match menv subst ty (mkReflCo r t) (lkco `mkTransCo` kco')
    
    2453 2456
                                                     (rkco `mkTransCo` kco')
    
    2454 2457
     
    

  • compiler/GHC/CoreToIface.hs
    ... ... @@ -192,7 +192,7 @@ toIfaceTypeX fr (ForAllTy b t) = IfaceForAllTy (toIfaceForAllBndrX fr b)
    192 192
                                                    (toIfaceTypeX (fr `delVarSet` binderVar b) t)
    
    193 193
     toIfaceTypeX fr (FunTy { ft_arg = t1, ft_mult = w, ft_res = t2, ft_af = af })
    
    194 194
       = IfaceFunTy af (toIfaceTypeX fr w) (toIfaceTypeX fr t1) (toIfaceTypeX fr t2)
    
    195
    -toIfaceTypeX fr (CastTy ty co)  = IfaceCastTy (toIfaceTypeX fr ty) (toIfaceCoercionX fr co)
    
    195
    +toIfaceTypeX fr (CastTy ty co)  = IfaceCastTy (toIfaceTypeX fr ty) (toIfaceCastCoercionX fr co)
    
    196 196
     toIfaceTypeX fr (CoercionTy co) = IfaceCoercionTy (toIfaceCoercionX fr co)
    
    197 197
     
    
    198 198
     toIfaceTypeX fr (TyConApp tc tys)
    
    ... ... @@ -271,9 +271,12 @@ toIfaceTyLit (CharTyLit x) = IfaceCharTyLit x
    271 271
     
    
    272 272
     ----------------
    
    273 273
     toIfaceCastCoercion :: CastCoercion -> IfaceCastCoercion
    
    274
    -toIfaceCastCoercion (CCoercion co) = IfaceCCoercion (toIfaceCoercion co)
    
    275
    -toIfaceCastCoercion (ZCoercion ty cos) = IfaceZCoercion (toIfaceType ty) (map (toIfaceCoercion . CoVarCo) (nonDetEltsUniqSet cos)) -- TODO determinism
    
    276
    -toIfaceCastCoercion ReflCastCo         = IfaceReflCastCo
    
    274
    +toIfaceCastCoercion = toIfaceCastCoercionX emptyVarSet
    
    275
    +
    
    276
    +toIfaceCastCoercionX :: VarSet -> CastCoercion -> IfaceCastCoercion
    
    277
    +toIfaceCastCoercionX fr (CCoercion co)     = IfaceCCoercion (toIfaceCoercionX fr co)
    
    278
    +toIfaceCastCoercionX fr (ZCoercion ty cos) = IfaceZCoercion (toIfaceTypeX fr ty) (map (toIfaceCoercionX fr . CoVarCo) (nonDetEltsUniqSet cos)) -- TODO determinism
    
    279
    +toIfaceCastCoercionX _  ReflCastCo         = IfaceReflCastCo
    
    277 280
     
    
    278 281
     toIfaceCoercion :: Coercion -> IfaceCoercion
    
    279 282
     toIfaceCoercion = toIfaceCoercionX emptyVarSet
    

  • compiler/GHC/HsToCore.hs
    ... ... @@ -748,7 +748,7 @@ mkUnsafeCoercePrimPair _old_id old_expr
    748 748
                                                                  runtimeRep1Ty
    
    749 749
                                                                  runtimeRep2Ty
    
    750 750
                  (scrut2, scrut2_ty, ab_cv_ty) = unsafe_equality (mkTYPEapp runtimeRep2Ty)
    
    751
    -                                                             (openAlphaTy `mkCastTy` alpha_co)
    
    751
    +                                                             (openAlphaTy `mkCastTyCo` alpha_co)
    
    752 752
                                                                  openBetaTy
    
    753 753
     
    
    754 754
                  -- alpha_co :: TYPE r1 ~# TYPE r2
    

  • compiler/GHC/Iface/Rename.hs
    ... ... @@ -947,7 +947,7 @@ rnIfaceType (IfaceForAllTy tv t)
    947 947
     rnIfaceType (IfaceCoercionTy co)
    
    948 948
         = IfaceCoercionTy <$> rnIfaceCo co
    
    949 949
     rnIfaceType (IfaceCastTy ty co)
    
    950
    -    = IfaceCastTy <$> rnIfaceType ty <*> rnIfaceCo co
    
    950
    +    = IfaceCastTy <$> rnIfaceType ty <*> rnIfaceCastCo co
    
    951 951
     
    
    952 952
     rnIfaceScaledType :: Rename (IfaceMult, IfaceType)
    
    953 953
     rnIfaceScaledType (m, t) = (,) <$> rnIfaceType m <*> rnIfaceType t
    

  • compiler/GHC/Iface/Syntax.hs
    ... ... @@ -2067,7 +2067,7 @@ freeNamesIfType (IfaceTupleTy _ _ ts) = freeNamesIfAppArgs ts
    2067 2067
     freeNamesIfType (IfaceLitTy _)        = emptyNameSet
    
    2068 2068
     freeNamesIfType (IfaceForAllTy tv t)  = freeNamesIfVarBndr tv &&& freeNamesIfType t
    
    2069 2069
     freeNamesIfType (IfaceFunTy _ w s t)  = freeNamesIfType s &&& freeNamesIfType t &&& freeNamesIfType w
    
    2070
    -freeNamesIfType (IfaceCastTy t c)     = freeNamesIfType t &&& freeNamesIfCoercion c
    
    2070
    +freeNamesIfType (IfaceCastTy t c)     = freeNamesIfType t &&& freeNamesIfCastCoercion c
    
    2071 2071
     freeNamesIfType (IfaceCoercionTy c)   = freeNamesIfCoercion c
    
    2072 2072
     
    
    2073 2073
     freeNamesIfMCoercion :: IfaceMCoercion -> NameSet
    

  • compiler/GHC/Iface/Type.hs
    ... ... @@ -190,7 +190,7 @@ data IfaceType
    190 190
       | IfaceForAllTy  IfaceForAllBndr IfaceType
    
    191 191
       | IfaceTyConApp  IfaceTyCon IfaceAppArgs  -- Not necessarily saturated
    
    192 192
                                                 -- Includes newtypes, synonyms, tuples
    
    193
    -  | IfaceCastTy     IfaceType IfaceCoercion
    
    193
    +  | IfaceCastTy     IfaceType IfaceCastCoercion
    
    194 194
       | IfaceCoercionTy IfaceCoercion
    
    195 195
     
    
    196 196
       | IfaceTupleTy                  -- Saturated tuples (unsaturated ones use IfaceTyConApp)
    
    ... ... @@ -776,12 +776,16 @@ substIfaceType env ty
    776 776
         go (IfaceTyConApp tc tys) = IfaceTyConApp tc (substIfaceAppArgs env tys)
    
    777 777
         go (IfaceTupleTy s i tys) = IfaceTupleTy s i (substIfaceAppArgs env tys)
    
    778 778
         go (IfaceForAllTy {})     = pprPanic "substIfaceType" (ppr ty)
    
    779
    -    go (IfaceCastTy ty co)    = IfaceCastTy (go ty) (go_co co)
    
    779
    +    go (IfaceCastTy ty co)    = IfaceCastTy (go ty) (go_cast_co co)
    
    780 780
         go (IfaceCoercionTy co)   = IfaceCoercionTy (go_co co)
    
    781 781
     
    
    782 782
         go_mco IfaceMRefl    = IfaceMRefl
    
    783 783
         go_mco (IfaceMCo co) = IfaceMCo $ go_co co
    
    784 784
     
    
    785
    +    go_cast_co IfaceReflCastCo = IfaceReflCastCo
    
    786
    +    go_cast_co (IfaceCCoercion co) = IfaceCCoercion (go_co co)
    
    787
    +    go_cast_co (IfaceZCoercion ty cos) = IfaceZCoercion (go ty) (map go_co cos)
    
    788
    +
    
    785 789
         go_co (IfaceReflCo ty)           = IfaceReflCo (go ty)
    
    786 790
         go_co (IfaceGReflCo r ty mco)    = IfaceGReflCo r (go ty) (go_mco mco)
    
    787 791
         go_co (IfaceFunCo r w c1 c2)     = IfaceFunCo r (go_co w) (go_co c1) (go_co c2)
    
    ... ... @@ -2191,6 +2195,9 @@ pprPromotionQuoteI IsPromoted = char '\''
    2191 2195
     instance Outputable IfaceCoercion where
    
    2192 2196
       ppr = pprIfaceCoercion
    
    2193 2197
     
    
    2198
    +instance Outputable IfaceCastCoercion where
    
    2199
    +  ppr = pprIfaceCastCoercion
    
    2200
    +
    
    2194 2201
     instance Binary IfaceTyCon where
    
    2195 2202
       put_ bh (IfaceTyCon n i) = put_ bh n >> put_ bh i
    
    2196 2203
     
    

  • compiler/GHC/IfaceToCore.hs
    ... ... @@ -1527,7 +1527,7 @@ tcIfaceType = go
    1527 1527
         go (IfaceForAllTy bndr t)
    
    1528 1528
           = bindIfaceForAllBndr bndr $ \ tv' vis ->
    
    1529 1529
             ForAllTy (Bndr tv' vis) <$> go t
    
    1530
    -    go (IfaceCastTy ty co)   = CastTy <$> go ty <*> tcIfaceCo co
    
    1530
    +    go (IfaceCastTy ty co)   = CastTy <$> go ty <*> tcIfaceCastCoercion co
    
    1531 1531
         go (IfaceCoercionTy co)  = CoercionTy <$> tcIfaceCo co
    
    1532 1532
     
    
    1533 1533
     tcIfaceTupleTy :: TupleSort -> PromotionFlag -> IfaceAppArgs -> IfL Type
    

  • compiler/GHC/Tc/Gen/App.hs
    ... ... @@ -813,7 +813,7 @@ tcInstFun do_ql inst_final (tc_fun, fun_ctxt) fun_sigma rn_args
    813 813
                -- NB: kappa is uninstantiated ('go' already checked that)
    
    814 814
                ; kind_co <- unifyKind Nothing liftedTypeKind (tyVarKind kappa)
    
    815 815
                      -- unifyKind: see (UQL3) in Note [QuickLook unification]
    
    816
    -           ; liftZonkM (writeMetaTyVar kappa (mkCastTy fun_ty' kind_co))
    
    816
    +           ; liftZonkM (writeMetaTyVar kappa (mkCastTyCo fun_ty' kind_co))
    
    817 817
     
    
    818 818
                ; let co_wrap = mkWpCastN (mkGReflLeftCo Nominal fun_ty' kind_co)
    
    819 819
                      acc'    = addArgWrap co_wrap acc
    
    ... ... @@ -2225,7 +2225,7 @@ qlUnify ty1 ty2
    2225 2225
                     do { co <- unifyKind (Just (TypeThing ty2)) ty2_kind kappa_kind
    
    2226 2226
                                -- unifyKind: see (UQL2) in Note [QuickLook unification]
    
    2227 2227
                                --            and (MIV2) in Note [Monomorphise instantiation variables]
    
    2228
    -                   ; let ty2' = mkCastTy ty2 co
    
    2228
    +                   ; let ty2' = mkCastTyCo ty2 co
    
    2229 2229
                        ; traceTc "qlUnify:update" $
    
    2230 2230
                          ppr kappa <+> text ":=" <+> ppr ty2
    
    2231 2231
                        ; liftZonkM $ writeMetaTyVar kappa ty2' }
    

  • compiler/GHC/Tc/Gen/Bind.hs
    ... ... @@ -58,7 +58,7 @@ import GHC.Core.Reduction ( Reduction(..) )
    58 58
     import GHC.Core.Multiplicity
    
    59 59
     import GHC.Core.FamInstEnv( normaliseType )
    
    60 60
     import GHC.Core.Class   ( Class )
    
    61
    -import GHC.Core.Coercion( mkSymCo )
    
    61
    +import GHC.Core.Coercion( mkSymCastCo )
    
    62 62
     import GHC.Core.Type (mkStrLitTy, mkCastTy)
    
    63 63
     import GHC.Core.TyCo.Ppr( pprTyVars )
    
    64 64
     import GHC.Core.TyCo.Tidy( tidyOpenTypeX )
    
    ... ... @@ -1093,7 +1093,7 @@ chooseInferredQuantifiers residual inferred_theta tau_tvs qtvs
    1093 1093
                    -- So, to make the kinds work out, we reverse the cast here.
    
    1094 1094
                    Just (wc_var, wc_co) -> liftZonkM $
    
    1095 1095
                                            writeMetaTyVar wc_var (mkConstraintTupleTy diff_theta
    
    1096
    -                                                              `mkCastTy` mkSymCo wc_co)
    
    1096
    +                                                              `mkCastTy` mkSymCastCo (varType wc_var) wc_co)
    
    1097 1097
                    Nothing              -> pprPanic "chooseInferredQuantifiers 1" (ppr wc_var_ty)
    
    1098 1098
     
    
    1099 1099
                ; traceTc "completeTheta" $
    

  • compiler/GHC/Tc/Gen/HsType.hs
    ... ... @@ -1696,7 +1696,7 @@ tcInferTyApps_nosat mode orig_hs_ty fun orig_hs_args
    1696 1696
                do { let arrows_needed = n_initial_val_args all_args
    
    1697 1697
                   ; co <- matchExpectedFunKind (HsTypeRnThing $ unLoc hs_ty) arrows_needed substed_fun_ki
    
    1698 1698
     
    
    1699
    -              ; fun' <- liftZonkM $ zonkTcType (fun `mkCastTy` co)
    
    1699
    +              ; fun' <- liftZonkM $ zonkTcType (fun `mkCastTyCo` co)
    
    1700 1700
                          -- This zonk is essential, to expose the fruits
    
    1701 1701
                          -- of matchExpectedFunKind to the 'go' loop
    
    1702 1702
     
    
    ... ... @@ -1963,7 +1963,7 @@ checkExpectedKind hs_ty ty act_kind exp_kind
    1963 1963
                      ; traceTc "checkExpectedKind" (vcat [ ppr act_kind
    
    1964 1964
                                                          , ppr exp_kind
    
    1965 1965
                                                          , ppr co_k ])
    
    1966
    -                ; return (res_ty `mkCastTy` co_k) } }
    
    1966
    +                ; return (res_ty `mkCastTyCo` co_k) } }
    
    1967 1967
         where
    
    1968 1968
           -- We need to make sure that both kinds have the same number of implicit
    
    1969 1969
           -- foralls and constraints out front. If the actual kind has more, instantiate
    
    ... ... @@ -1986,7 +1986,7 @@ checkExpKind _rn_ty ty ki (Infer cell) = do
    1986 1986
       -- NB: do not instantiate.
    
    1987 1987
       -- See Note [Do not always instantiate eagerly in types]
    
    1988 1988
       co <- fillInferResultNoInst ki cell
    
    1989
    -  pure (ty `mkCastTy` co)
    
    1989
    +  pure (ty `mkCastTyCo` co)
    
    1990 1990
     
    
    1991 1991
     ---------------------------
    
    1992 1992
     
    

  • compiler/GHC/Tc/Solver/Equality.hs
    ... ... @@ -359,11 +359,11 @@ can_eq_nc _rewritten rdr_env envs ev eq_rel ty1 ps_ty1 ty2 ps_ty2
    359 359
     can_eq_nc rewritten rdr_env envs ev eq_rel (CastTy ty1 co1) _ ty2 ps_ty2
    
    360 360
       | isNothing (canEqLHS_maybe ty2)
    
    361 361
       = -- See (EIK3) in Note [Equalities with heterogeneous kinds]
    
    362
    -    canEqCast rewritten rdr_env envs ev eq_rel NotSwapped ty1 co1 ty2 ps_ty2
    
    362
    +    canEqCast rewritten rdr_env envs ev eq_rel NotSwapped ty1 (castCoToCo (typeKind ty1) co1) ty2 ps_ty2
    
    363 363
     can_eq_nc rewritten rdr_env envs ev eq_rel ty1 ps_ty1 (CastTy ty2 co2) _
    
    364 364
       | isNothing (canEqLHS_maybe ty1)
    
    365 365
       = -- See (EIK3) in Note [Equalities with heterogeneous kinds]
    
    366
    -    canEqCast rewritten rdr_env envs ev eq_rel IsSwapped ty2 co2 ty1 ps_ty1
    
    366
    +    canEqCast rewritten rdr_env envs ev eq_rel IsSwapped ty2 (castCoToCo (typeKind ty2) co2) ty1 ps_ty1
    
    367 367
     
    
    368 368
     ----------------------
    
    369 369
     -- Otherwise try to decompose
    
    ... ... @@ -522,7 +522,7 @@ can_eq_nc_forall ev eq_rel s1 s2
    522 522
                                           (substTy subst2 (tyVarKind tv2))
    
    523 523
     
    
    524 524
                        ; let subst2' = extendTvSubstAndInScope subst2 tv2
    
    525
    -                                       (mkCastTy (mkTyVarTy skol_tv) kind_co)
    
    525
    +                                       (mkCastTyCo (mkTyVarTy skol_tv) kind_co)
    
    526 526
                              -- skol_tv is already in the in-scope set, but the
    
    527 527
                              -- free vars of kind_co are not; hence "...AndInScope"
    
    528 528
                        ; co <- go uenv skol_tvs subst2' bndrs1 bndrs2
    
    ... ... @@ -1741,7 +1741,7 @@ canEqCanLHSHetero ev eq_rel swapped lhs1 ps_xi1 ki1 xi2 ps_xi2 ki2
    1741 1741
             -- kind_co :: ki2 ~N ki1
    
    1742 1742
             lhs_redn    = mkReflRedn role ps_xi1
    
    1743 1743
             rhs_redn    = mkGReflRightRedn role xi2 kind_co
    
    1744
    -        new_xi2     = mkCastTy ps_xi2 kind_co
    
    1744
    +        new_xi2     = mkCastTyCo ps_xi2 kind_co
    
    1745 1745
     
    
    1746 1746
     canEqCanLHSHomo :: CtEvidence          -- lhs ~ rhs
    
    1747 1747
                                            -- or, if swapped: rhs ~ lhs
    
    ... ... @@ -1753,14 +1753,14 @@ canEqCanLHSHomo :: CtEvidence -- lhs ~ rhs
    1753 1753
     canEqCanLHSHomo ev eq_rel swapped lhs1 ps_xi1 xi2 ps_xi2
    
    1754 1754
       | (xi2', mco) <- split_cast_ty xi2
    
    1755 1755
       , Just lhs2 <- canEqLHS_maybe xi2'
    
    1756
    -  = canEqCanLHS2 ev eq_rel swapped lhs1 ps_xi1 lhs2 (ps_xi2 `mkCastTyMCo` mkSymMCo mco) mco
    
    1756
    +  = canEqCanLHS2 ev eq_rel swapped lhs1 ps_xi1 lhs2 (ps_xi2 `mkCastTy` mkSymCastCo (typeKind xi2') mco) mco
    
    1757 1757
     
    
    1758 1758
       | otherwise
    
    1759 1759
       = canEqCanLHSFinish ev eq_rel swapped lhs1 ps_xi2
    
    1760 1760
     
    
    1761 1761
       where
    
    1762
    -    split_cast_ty (CastTy ty co) = (ty, MCo co)
    
    1763
    -    split_cast_ty other          = (other, MRefl)
    
    1762
    +    split_cast_ty (CastTy ty co) = (ty, co)
    
    1763
    +    split_cast_ty other          = (other, ReflCastCo)
    
    1764 1764
     
    
    1765 1765
     -- This function deals with the case that both LHS and RHS are potential
    
    1766 1766
     -- CanEqLHSs.
    
    ... ... @@ -1771,7 +1771,7 @@ canEqCanLHS2 :: CtEvidence -- lhs ~ (rhs |> mco)
    1771 1771
                  -> TcType                  -- pretty lhs
    
    1772 1772
                  -> CanEqLHS                -- rhs
    
    1773 1773
                  -> TcType                  -- pretty rhs
    
    1774
    -             -> MCoercion               -- :: kind(rhs) ~N kind(lhs)
    
    1774
    +             -> CastCoercion            -- :: kind(rhs) ~N kind(lhs)
    
    1775 1775
                  -> TcS (StopOrContinue (Either IrredCt EqCt))
    
    1776 1776
     canEqCanLHS2 ev eq_rel swapped lhs1 ps_xi1 lhs2 ps_xi2 mco
    
    1777 1777
       | lhs1 `eqCanEqLHS` lhs2
    
    ... ... @@ -1823,13 +1823,13 @@ canEqCanLHS2 ev eq_rel swapped lhs1 ps_xi1 lhs2 ps_xi2 mco
    1823 1823
              then finish_with_swapping
    
    1824 1824
              else finish_without_swapping }
    
    1825 1825
       where
    
    1826
    -    sym_mco = mkSymMCo mco
    
    1826
    +    sym_mco = mkSymCastCo (canEqLHSKind lhs2) mco
    
    1827 1827
         role    = eqRelRole eq_rel
    
    1828 1828
         lhs1_ty  = canEqLHSType lhs1
    
    1829 1829
         lhs2_ty  = canEqLHSType lhs2
    
    1830 1830
     
    
    1831 1831
         finish_without_swapping
    
    1832
    -      = canEqCanLHSFinish ev eq_rel swapped lhs1 (ps_xi2 `mkCastTyMCo` mco)
    
    1832
    +      = canEqCanLHSFinish ev eq_rel swapped lhs1 (ps_xi2 `mkCastTy` mco)
    
    1833 1833
     
    
    1834 1834
         -- Swapping. We have   ev : lhs1 ~ lhs2 |> co
    
    1835 1835
         -- We swap to      new_ev : lhs2 ~ lhs1 |> sym co
    
    ... ... @@ -1840,7 +1840,7 @@ canEqCanLHS2 ev eq_rel swapped lhs1 ps_xi1 lhs2 ps_xi2 mco
    1840 1840
           = do { let lhs1_redn = mkGReflRightMRedn role lhs1_ty sym_mco
    
    1841 1841
                      lhs2_redn = mkGReflLeftMRedn  role lhs2_ty mco
    
    1842 1842
                ; new_ev <-rewriteEqEvidence ev swapped lhs1_redn lhs2_redn emptyCoHoleSet
    
    1843
    -           ; canEqCanLHSFinish new_ev eq_rel IsSwapped lhs2 (ps_xi1 `mkCastTyMCo` sym_mco) }
    
    1843
    +           ; canEqCanLHSFinish new_ev eq_rel IsSwapped lhs2 (ps_xi1 `mkCastTy` sym_mco) }
    
    1844 1844
     
    
    1845 1845
         put_tyvar_on_lhs = isWanted ev && eq_rel == NomEq
    
    1846 1846
         -- See Note [Orienting TyVarLHS/TyFamLHS]
    

  • compiler/GHC/Tc/Solver/Rewrite.hs
    ... ... @@ -561,8 +561,9 @@ rewrite_one ty@(ForAllTy {})
    561 561
            ; redn <- rewrite_one rho
    
    562 562
            ; return $ mkHomoForAllRedn bndrs redn }
    
    563 563
     
    
    564
    -rewrite_one (CastTy ty g)
    
    564
    +rewrite_one (CastTy ty cco)
    
    565 565
       = do { redn <- rewrite_one ty
    
    566
    +       ; let g = castCoToCo (typeKind ty) cco
    
    566 567
            ; g'   <- rewrite_co g
    
    567 568
            ; role <- getRole
    
    568 569
            ; return $ mkCastRedn1 role ty g' redn }
    

  • compiler/GHC/Tc/TyCl/Utils.hs
    ... ... @@ -37,7 +37,7 @@ import GHC.Builtin.Uniques ( mkBuiltinUnique )
    37 37
     
    
    38 38
     import GHC.Hs
    
    39 39
     
    
    40
    -import GHC.Core.TyCo.Rep( Type(..), Coercion(..), MCoercion(..) )
    
    40
    +import GHC.Core.TyCo.Rep( Type(..), Coercion(..), MCoercion(..), CastCoercion(..) )
    
    41 41
     import GHC.Core.Multiplicity
    
    42 42
     import GHC.Core.Predicate
    
    43 43
     import GHC.Core.Make( rEC_SEL_ERROR_ID )
    
    ... ... @@ -101,7 +101,7 @@ synonymTyConsOfType ty
    101 101
          go (AppTy a b)       = go a `plusNameEnv` go b
    
    102 102
          go (FunTy _ w a b)   = go w `plusNameEnv` go a `plusNameEnv` go b
    
    103 103
          go (ForAllTy _ ty)   = go ty
    
    104
    -     go (CastTy ty co)    = go ty `plusNameEnv` go_co co
    
    104
    +     go (CastTy ty co)    = go ty `plusNameEnv` go_cast_co co
    
    105 105
          go (CoercionTy co)   = go_co co
    
    106 106
     
    
    107 107
          -- Note [TyCon cycles through coercions?!]
    
    ... ... @@ -128,6 +128,10 @@ synonymTyConsOfType ty
    128 128
          go_mco MRefl    = emptyNameEnv
    
    129 129
          go_mco (MCo co) = go_co co
    
    130 130
     
    
    131
    +     go_cast_co ReflCastCo       = emptyNameEnv
    
    132
    +     go_cast_co (CCoercion co)   = go_co co
    
    133
    +     go_cast_co (ZCoercion ty _) = go ty
    
    134
    +
    
    131 135
          go_co (Refl ty)            = go ty
    
    132 136
          go_co (GRefl _ ty mco)     = go ty `plusNameEnv` go_mco mco
    
    133 137
          go_co (TyConAppCo _ tc cs) = go_tc tc `plusNameEnv` go_co_s cs
    

  • compiler/GHC/Tc/Utils/Instantiate.hs
    ... ... @@ -331,7 +331,7 @@ instTyVarsWith orig tvs tys
    331 331
           = go (extendTvSubstAndInScope subst tv ty) tvs tys
    
    332 332
           | otherwise
    
    333 333
           = do { co <- emitWantedEq orig KindLevel Nominal ty_kind tv_kind
    
    334
    -           ; go (extendTvSubstAndInScope subst tv (ty `mkCastTy` co)) tvs tys }
    
    334
    +           ; go (extendTvSubstAndInScope subst tv (ty `mkCastTyCo` co)) tvs tys }
    
    335 335
           where
    
    336 336
             tv_kind = substTy subst (tyVarKind tv)
    
    337 337
             ty_kind = typeKind ty
    

  • compiler/GHC/Tc/Utils/TcMType.hs
    ... ... @@ -1435,7 +1435,7 @@ collect_cand_qtvs orig_ty is_dep cur_lvl bound dvs ty
    1435 1435
         go dv (FunTy _ w arg res) = foldlM go dv [w, arg, res]
    
    1436 1436
         go dv (LitTy {})          = return dv
    
    1437 1437
         go dv (CastTy ty co)      = do { dv1 <- go dv ty
    
    1438
    -                                   ; collect_cand_qtvs_co orig_ty cur_lvl bound dv1 co }
    
    1438
    +                                   ; collect_cand_qtvs_cast_co orig_ty cur_lvl bound dv1 co }
    
    1439 1439
         go dv (CoercionTy co)     = collect_cand_qtvs_co orig_ty cur_lvl bound dv co
    
    1440 1440
     
    
    1441 1441
         go dv (TyVarTy tv)
    
    ... ... @@ -1516,6 +1516,18 @@ collect_cand_qtvs orig_ty is_dep cur_lvl bound dvs ty
    1516 1516
                             -- See Note [Recurring into kinds for candidateQTyVars]
    
    1517 1517
                           ; collect_cand_qtvs orig_ty True cur_lvl bound dv' tv_kind } }
    
    1518 1518
     
    
    1519
    +collect_cand_qtvs_cast_co :: TcType -- original type at top of recursion; for errors
    
    1520
    +                     -> TcLevel
    
    1521
    +                     -> VarSet -- bound variables
    
    1522
    +                     -> CandidatesQTvs -> CastCoercion
    
    1523
    +                     -> TcM CandidatesQTvs
    
    1524
    +collect_cand_qtvs_cast_co orig_ty cur_lvl bound dv cco = case cco of
    
    1525
    +    ReflCastCo -> return dv
    
    1526
    +    CCoercion co -> collect_cand_qtvs_co orig_ty cur_lvl bound dv co
    
    1527
    +    ZCoercion ty cos -> do { dv1 <- collect_cand_qtvs orig_ty True cur_lvl bound dv ty
    
    1528
    +                           ; foldM (\dv cv -> collect_cand_qtvs_co orig_ty cur_lvl bound dv (CoVarCo cv)) dv1 (nonDetEltsUniqSet cos) -- TODO
    
    1529
    +                           }
    
    1530
    +
    
    1519 1531
     collect_cand_qtvs_co :: TcType -- original type at top of recursion; for errors
    
    1520 1532
                          -> TcLevel
    
    1521 1533
                          -> VarSet -- bound variables
    

  • compiler/GHC/Tc/Utils/Unify.hs
    ... ... @@ -2696,11 +2696,11 @@ uType env@(UE { u_role = role }) orig_ty1 orig_ty2
    2696 2696
          -- didn't do it this way, and then the unification above was deferred.
    
    2697 2697
         go (CastTy t1 co1) t2
    
    2698 2698
           = do { co_tys <- uType env t1 t2
    
    2699
    -           ; return (mkCoherenceLeftCo role t1 co1 co_tys) }
    
    2699
    +           ; return (mkCoherenceLeftCo role t1 (castCoToCo (typeKind t1) co1) co_tys) }
    
    2700 2700
     
    
    2701 2701
         go t1 (CastTy t2 co2)
    
    2702 2702
           = do { co_tys <- uType env t1 t2
    
    2703
    -           ; return (mkCoherenceRightCo role t2 co2 co_tys) }
    
    2703
    +           ; return (mkCoherenceRightCo role t2 (castCoToCo (typeKind t2) co2) co_tys) }
    
    2704 2704
     
    
    2705 2705
             -- Variables; go for uUnfilledVar
    
    2706 2706
             -- Note that we pass in *original* (before synonym expansion),
    
    ... ... @@ -3484,7 +3484,7 @@ simpleUnifyCheck caller given_eq_lvl lhs_tv rhs
    3484 3484
       where
    
    3485 3485
         lhs_info = metaTyVarInfo lhs_tv
    
    3486 3486
     
    
    3487
    -    !(occ_in_ty, occ_in_co) = mkOccFolders (tyVarName lhs_tv)
    
    3487
    +    !(occ_in_ty, occ_in_co, occ_in_cast_co) = mkOccFolders (tyVarName lhs_tv)
    
    3488 3488
     
    
    3489 3489
         lhs_tv_lvl         = tcTyVarLevel lhs_tv
    
    3490 3490
         lhs_tv_is_concrete = isConcreteTyVar lhs_tv
    
    ... ... @@ -3525,17 +3525,17 @@ simpleUnifyCheck caller given_eq_lvl lhs_tv rhs
    3525 3525
           | otherwise = False
    
    3526 3526
     
    
    3527 3527
         rhs_is_ok (AppTy t1 t2)    = rhs_is_ok t1 && rhs_is_ok t2
    
    3528
    -    rhs_is_ok (CastTy ty co)   = not (occ_in_co co) && rhs_is_ok ty
    
    3528
    +    rhs_is_ok (CastTy ty co)   = not (occ_in_cast_co co) && rhs_is_ok ty
    
    3529 3529
         rhs_is_ok (CoercionTy co)  = not (occ_in_co co)
    
    3530 3530
         rhs_is_ok (LitTy {})       = True
    
    3531 3531
     
    
    3532 3532
     
    
    3533
    -mkOccFolders :: Name -> (TcType -> Bool, TcCoercion -> Bool)
    
    3533
    +mkOccFolders :: Name -> (TcType -> Bool, TcCoercion -> Bool, TcCastCoercion -> Bool)
    
    3534 3534
     -- These functions return True
    
    3535 3535
     --   * if lhs_tv occurs (incl deeply, in the kind of variable)
    
    3536 3536
     --   * if there is a coercion hole
    
    3537 3537
     -- No expansion of type synonyms
    
    3538
    -mkOccFolders lhs_tv = (getAny . check_ty, getAny . check_co)
    
    3538
    +mkOccFolders lhs_tv = (getAny . check_ty, getAny . check_co, getAny . check_cast_co)
    
    3539 3539
       where
    
    3540 3540
         !(check_ty, _, check_co, _) = foldTyCo occ_folder emptyVarSet
    
    3541 3541
         occ_folder = TyCoFolder { tcf_view  = noView  -- Don't expand synonyms
    
    ... ... @@ -3549,6 +3549,11 @@ mkOccFolders lhs_tv = (getAny . check_ty, getAny . check_co)
    3549 3549
         do_bndr is tcv _faf = extendVarSet is tcv
    
    3550 3550
         do_hole _is _hole = DM.Any True  -- Reject coercion holes
    
    3551 3551
     
    
    3552
    +    check_cast_co ReflCastCo = Any False
    
    3553
    +    check_cast_co (CCoercion co) = check_co co
    
    3554
    +    check_cast_co (ZCoercion _ty _cos) = error "AMG TODO check_cast_co"
    
    3555
    +
    
    3556
    +
    
    3552 3557
     {- *********************************************************************
    
    3553 3558
     *                                                                      *
    
    3554 3559
                      Equality invariant checking
    
    ... ... @@ -4144,7 +4149,7 @@ check_ty_eq_rhs flags ty
    4144 4149
                               ; return (mkAppRedn <$> fun_res <*> arg_res) }
    
    4145 4150
     
    
    4146 4151
           CastTy ty co  -> do { ty_res <- check_ty_eq_rhs flags ty
    
    4147
    -                          ; co_res <- checkCo flags co
    
    4152
    +                          ; co_res <- checkCo flags (castCoToCo (typeKind ty) co)
    
    4148 4153
                               ; return (mkCastRedn1 Nominal ty <$> co_res <*> ty_res) }
    
    4149 4154
     
    
    4150 4155
           CoercionTy co -> do { co_res <- checkCo flags co
    
    ... ... @@ -4540,7 +4545,7 @@ simpleOccursCheck (OC_Check lhs_tv occ_prob) occ_tv
    4540 4545
       | otherwise
    
    4541 4546
       = TyVarCheck_Success
    
    4542 4547
       where
    
    4543
    -    (check_kind, _) = mkOccFolders lhs_tv
    
    4548
    +    (check_kind, _, _) = mkOccFolders lhs_tv
    
    4544 4549
     
    
    4545 4550
     -------------------------
    
    4546 4551
     tyVarLevelCheck :: LevelCheck m -> TcTyVar -> TyVarCheckResult m