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

Commits:

22 changed files:

Changes:

  • compiler/GHC/Core/Coercion.hs
    ... ... @@ -2452,6 +2452,7 @@ seqMCo (MCo co) = seqCo co
    2452 2452
     seqCastCoercion :: CastCoercion -> ()
    
    2453 2453
     seqCastCoercion (CCoercion co) = seqCo co
    
    2454 2454
     seqCastCoercion (ZCoercion ty cos) = seqType ty `seq` seqVarSet cos
    
    2455
    +seqCastCoercion ReflCastCo = ()
    
    2455 2456
     
    
    2456 2457
     seqCo :: Coercion -> ()
    
    2457 2458
     seqCo (Refl ty)             = seqType ty
    
    ... ... @@ -2874,45 +2875,56 @@ See Note [Zapped casts] in GHC.Core.TyCo.Rep.
    2874 2875
     castCoercionLKind :: HasDebugCallStack => Type -> CastCoercion -> Type
    
    2875 2876
     castCoercionLKind _ (CCoercion co) = coercionLKind co
    
    2876 2877
     castCoercionLKind lhs_ty (ZCoercion _ _) = lhs_ty
    
    2878
    +castCoercionLKind lhs_ty ReflCastCo = lhs_ty
    
    2877 2879
     
    
    2878 2880
     -- | Compute the right type of a 'CastCoercion', like 'coercionRKind'.
    
    2879
    -castCoercionRKind :: HasDebugCallStack => CastCoercion -> Type
    
    2880
    -castCoercionRKind (CCoercion co) = coercionRKind co
    
    2881
    -castCoercionRKind (ZCoercion ty _) = ty
    
    2881
    +-- Corresponds to 'coercionRKind', but requires the type to be supplied by the
    
    2882
    +-- caller because it cannot be recovered in the 'ReflCastCo' case.
    
    2883
    +castCoercionRKind :: HasDebugCallStack => Type -> CastCoercion -> Type
    
    2884
    +castCoercionRKind _ (CCoercion co) = coercionRKind co
    
    2885
    +castCoercionRKind _ (ZCoercion rhs_ty _) = rhs_ty
    
    2886
    +castCoercionRKind lhs_ty ReflCastCo = lhs_ty
    
    2882 2887
     
    
    2883 2888
     -- | Equality test on 'CastCoercion', where the LHS type is the same for both
    
    2884 2889
     -- coercions, so we merely need to compare the RHS types.
    
    2885
    -eqCastCoercion :: CastCoercion -> CastCoercion -> Bool
    
    2886
    -eqCastCoercion cco1 cco2 = castCoercionRKind cco1 `eqType` castCoercionRKind cco2
    
    2890
    +eqCastCoercion :: Type -> CastCoercion -> CastCoercion -> Bool
    
    2891
    +eqCastCoercion _ ReflCastCo ReflCastCo = True
    
    2892
    +eqCastCoercion lhs_ty cco1 cco2 = castCoercionRKind lhs_ty cco1 `eqType` castCoercionRKind lhs_ty cco2
    
    2887 2893
     
    
    2888
    -eqCastCoercionX :: RnEnv2 -> CastCoercion -> CastCoercion -> Bool
    
    2889
    -eqCastCoercionX env = eqTypeX env `on` castCoercionRKind
    
    2894
    +eqCastCoercionX :: RnEnv2 -> Type -> CastCoercion -> Type -> CastCoercion -> Bool
    
    2895
    +eqCastCoercionX _ _ ReflCastCo _ ReflCastCo = True
    
    2896
    +eqCastCoercionX env ty1 co1 ty2 co2 = eqTypeX env ty1 ty2
    
    2897
    +                                   && eqTypeX env (castCoercionRKind ty1 co1) (castCoercionRKind ty2 co2)
    
    2890 2898
     
    
    2891 2899
     -- | Convert a 'CastCoercion' back into a 'Coercion', using a 'UnivCo' if we
    
    2892 2900
     -- have discarded the original 'Coercion'.
    
    2893 2901
     castCoToCo :: Type -> CastCoercion -> CoercionR
    
    2894 2902
     castCoToCo _      (CCoercion co)         = co
    
    2895 2903
     castCoToCo lhs_ty (ZCoercion rhs_ty cos) = mkUnivCo ZCoercionProv (map CoVarCo (nonDetEltsUniqSet cos)) Representational lhs_ty rhs_ty
    
    2904
    +castCoToCo lhs_ty ReflCastCo             = mkRepReflCo lhs_ty
    
    2896 2905
     
    
    2897 2906
     -- | Compose two 'CastCoercion's transitively, like 'mkTransCo'.  If either is
    
    2898 2907
     -- zapped the whole result will be zapped.
    
    2899 2908
     mkTransCastCo :: HasDebugCallStack => CastCoercion -> CastCoercion -> CastCoercion
    
    2900 2909
     mkTransCastCo cco (CCoercion co)     = mkTransCastCoCo cco co
    
    2901 2910
     mkTransCastCo cco (ZCoercion ty cos) = ZCoercion ty (shallowCoVarsOfCastCo cco `unionVarSet` cos)
    
    2911
    +mkTransCastCo cco ReflCastCo         = cco
    
    2902 2912
     
    
    2903 2913
     -- | Transitively compose a 'CastCoercion' followed by a 'Coercion'.
    
    2904 2914
     mkTransCastCoCo :: HasDebugCallStack => CastCoercion -> Coercion -> CastCoercion
    
    2905 2915
     mkTransCastCoCo (CCoercion co1)   co2 = CCoercion (mkTransCo co1 co2)
    
    2906 2916
     mkTransCastCoCo (ZCoercion _ cos) co2 = ZCoercion (coercionRKind co2) (shallowCoVarsOfCo co2 `unionVarSet` cos)
    
    2917
    +mkTransCastCoCo ReflCastCo        co2 = CCoercion co2
    
    2907 2918
     
    
    2908 2919
     -- | Transitively compose a 'Coercion' followed by a 'CastCoercion'.
    
    2909 2920
     mkTransCoCastCo :: HasDebugCallStack => Coercion -> CastCoercion -> CastCoercion
    
    2910 2921
     mkTransCoCastCo co1 (CCoercion co2)    = CCoercion (mkTransCo co1 co2)
    
    2911 2922
     mkTransCoCastCo co1 (ZCoercion ty cos) = ZCoercion ty (shallowCoVarsOfCo co1 `unionVarSet` cos)
    
    2923
    +mkTransCoCastCo co1 ReflCastCo         = CCoercion co1
    
    2912 2924
     
    
    2913 2925
     -- | Slowly checks if the coercion is reflexive. Don't call this in a loop,
    
    2914 2926
     -- as it walks over the entire coercion.
    
    2915 2927
     isReflexiveCastCo :: Type -> CastCoercion -> Bool
    
    2916 2928
     isReflexiveCastCo _      (CCoercion co) = isReflexiveCo co
    
    2917 2929
     isReflexiveCastCo lhs_ty (ZCoercion rhs_ty _) = lhs_ty `eqType` rhs_ty
    
    2918
    -
    2930
    +isReflexiveCastCo _      ReflCastCo           = True

  • compiler/GHC/Core/FVs.hs
    ... ... @@ -280,6 +280,7 @@ exprFVs (Let (Rec pairs) body) fv_cand in_scope acc
    280 280
     cast_co_fvs :: CastCoercion -> FV
    
    281 281
     cast_co_fvs (CCoercion co)     fv_cand in_scope acc = (tyCoFVsOfCo co) fv_cand in_scope acc
    
    282 282
     cast_co_fvs (ZCoercion ty cos) fv_cand in_scope acc = (tyCoFVsOfType ty `unionFV` tyCoFVsOfCoVarSet cos) fv_cand in_scope acc
    
    283
    +cast_co_fvs ReflCastCo         _       _        acc = acc
    
    283 284
     
    
    284 285
     ---------
    
    285 286
     rhs_fvs :: (Id, CoreExpr) -> FV
    

  • compiler/GHC/Core/Map/Expr.hs
    ... ... @@ -30,7 +30,9 @@ import GHC.Prelude
    30 30
     import GHC.Data.TrieMap
    
    31 31
     import GHC.Core.Map.Type
    
    32 32
     import GHC.Core
    
    33
    +import GHC.Core.Coercion
    
    33 34
     import GHC.Core.Type
    
    35
    +import GHC.Core.Utils
    
    34 36
     import GHC.Types.Tickish
    
    35 37
     import GHC.Types.Var
    
    36 38
     
    
    ... ... @@ -159,7 +161,7 @@ eqDeBruijnExpr (D env1 e1) (D env2 e2) = go e1 e2 where
    159 161
         go (Type t1)    (Type t2)        = eqDeBruijnType (D env1 t1) (D env2 t2)
    
    160 162
         -- See Note [Alpha-equality for Coercion arguments]
    
    161 163
         go (Coercion {}) (Coercion {}) = True
    
    162
    -    go (Cast e1 co1) (Cast e2 co2) = D env1 co1 == D env2 co2 && go e1 e2
    
    164
    +    go (Cast e1 co1) (Cast e2 co2) = D env1 (castCoercionRKind (exprType e1) co1) == D env2 (castCoercionRKind (exprType e2) co2) && go e1 e2
    
    163 165
         go (App f1 a1)   (App f2 a2)   = go f1 f2 && go a1 a2
    
    164 166
         go (Tick n1 e1) (Tick n2 e2)
    
    165 167
           =  eqDeBruijnTickish (D env1 n1) (D env2 n2)
    
    ... ... @@ -343,7 +345,7 @@ lkE (D env expr) cm = go expr cm
    343 345
         go (Lit l)              = cm_lit  >.> lookupTM l
    
    344 346
         go (Type t)             = cm_type >.> lkG (D env t)
    
    345 347
         go (Coercion c)         = cm_co   >.> lkG (D env c)
    
    346
    -    go (Cast e c)           = cm_cast >.> lkG (D env e) >=> lkG (D env c)
    
    348
    +    go (Cast e c)           = cm_cast >.> lkG (D env e) >=> lkG (D env (castCoercionRKind (exprType e) c))
    
    347 349
         go (Tick tickish e)     = cm_tick >.> lkG (D env e) >=> lkTickish tickish
    
    348 350
         go (App e1 e2)          = cm_app  >.> lkG (D env e2) >=> lkG (D env e1)
    
    349 351
         go (Lam v e)            = cm_lam  >.> lkG (D (extendCME env v) e)
    
    ... ... @@ -370,7 +372,7 @@ xtE (D env (Coercion c)) f m = m { cm_co = cm_co m
    370 372
                                                      |> xtG (D env c) f }
    
    371 373
     xtE (D _   (Lit l))              f m = m { cm_lit  = cm_lit m  |> alterTM l f }
    
    372 374
     xtE (D env (Cast e c))           f m = m { cm_cast = cm_cast m |> xtG (D env e)
    
    373
    -                                                 |>> xtG (D env c) f }
    
    375
    +                                                 |>> xtG (D env (castCoercionRKind (exprType e) c)) f }
    
    374 376
     xtE (D env (Tick t e))           f m = m { cm_tick = cm_tick m |> xtG (D env e)
    
    375 377
                                                      |>> xtTickish t f }
    
    376 378
     xtE (D env (App e1 e2))          f m = m { cm_app = cm_app m |> xtG (D env e2)
    

  • compiler/GHC/Core/Map/Type.hs
    ... ... @@ -147,7 +147,7 @@ instance Functor CastCoercionMap where
    147 147
         {-# INLINE fmap #-}
    
    148 148
     
    
    149 149
     instance TrieMap CastCoercionMap where
    
    150
    -   type Key CastCoercionMap = CastCoercion
    
    150
    +   type Key CastCoercionMap = Type
    
    151 151
        emptyTM                     = CastCoercionMap emptyTM
    
    152 152
        lookupTM k   (CastCoercionMap m) = lookupTM (deBruijnize k) m
    
    153 153
        alterTM k f  (CastCoercionMap m) = CastCoercionMap (alterTM (deBruijnize k) f m)
    
    ... ... @@ -164,7 +164,7 @@ instance Functor CastCoercionMapX where
    164 164
         {-# INLINE fmap #-}
    
    165 165
     
    
    166 166
     instance TrieMap CastCoercionMapX where
    
    167
    -  type Key CastCoercionMapX = DeBruijn CastCoercion
    
    167
    +  type Key CastCoercionMapX = DeBruijn Type
    
    168 168
       emptyTM = CastCoercionMapX emptyTM
    
    169 169
       lookupTM = lkX
    
    170 170
       alterTM  = xtX
    
    ... ... @@ -172,18 +172,12 @@ instance TrieMap CastCoercionMapX where
    172 172
       filterTM f (CastCoercionMapX core_tm) = CastCoercionMapX (filterTM f core_tm)
    
    173 173
       mapMaybeTM f (CastCoercionMapX core_tm) = CastCoercionMapX (mapMaybeTM f core_tm)
    
    174 174
     
    
    175
    -instance Eq (DeBruijn CastCoercion) where
    
    176
    -  D env1 co1 == D env2 co2
    
    177
    -    = D env1 (castCoercionRKind co1) ==
    
    178
    -      D env2 (castCoercionRKind co2)
    
    179
    -
    
    180
    -lkX :: DeBruijn CastCoercion -> CastCoercionMapX a -> Maybe a
    
    181
    -lkX (D env co) (CastCoercionMapX core_tm) = lkT (D env $ castCoercionRKind co)
    
    182
    -                                        core_tm
    
    175
    +lkX :: DeBruijn Type -> CastCoercionMapX a -> Maybe a
    
    176
    +lkX (D env co_ty) (CastCoercionMapX core_tm) = lkT (D env co_ty) core_tm
    
    183 177
     
    
    184
    -xtX :: DeBruijn CastCoercion -> XT a -> CastCoercionMapX a -> CastCoercionMapX a
    
    185
    -xtX (D env co) f (CastCoercionMapX m)
    
    186
    -  = CastCoercionMapX (xtT (D env $ castCoercionRKind co) f m)
    
    178
    +xtX :: DeBruijn Type -> XT a -> CastCoercionMapX a -> CastCoercionMapX a
    
    179
    +xtX (D env co_ty) f (CastCoercionMapX m)
    
    180
    +  = CastCoercionMapX (xtT (D env co_ty) f m)
    
    187 181
     
    
    188 182
     
    
    189 183
     {-
    

  • compiler/GHC/Core/Opt/Arity.hs
    ... ... @@ -3119,27 +3119,28 @@ collectBindersPushingCo e
    3119 3119
         go :: [Var] -> CoreExpr -> ([Var], CoreExpr)
    
    3120 3120
         -- The accumulator is in reverse order
    
    3121 3121
         go bs (Lam b e)   = go (b:bs) e
    
    3122
    -    go bs (Cast e co) = go_c bs e (castCoToCo (exprType e) co) -- TODO: can we do better?
    
    3122
    +    go bs (Cast e co) = go_c bs e co
    
    3123 3123
         go bs e           = (reverse bs, e)
    
    3124 3124
     
    
    3125 3125
         -- We are in a cast; peel off casts until we hit a lambda.
    
    3126
    -    go_c :: [Var] -> CoreExpr -> Coercion -> ([Var], CoreExpr)
    
    3126
    +    go_c :: [Var] -> CoreExpr -> CastCoercion -> ([Var], CoreExpr)
    
    3127 3127
         -- (go_c bs e c) is same as (go bs e (e |> c))
    
    3128
    -    go_c bs (Cast e co1) co2 = go_c bs e (castCoToCo (exprType e) co1 `mkTransCo` co2) -- TODO: can we do better?
    
    3128
    +    go_c bs (Cast e co1) co2 = go_c bs e (co1 `mkTransCastCo` co2)
    
    3129 3129
         go_c bs (Lam b e)    co  = go_lam bs b e co
    
    3130
    -    go_c bs e            co  = (reverse bs, mkCast e co)
    
    3130
    +    go_c bs e            co  = (reverse bs, mkCastCo e co)
    
    3131 3131
     
    
    3132 3132
         -- We are in a lambda under a cast; peel off lambdas and build a
    
    3133 3133
         -- new coercion for the body.
    
    3134
    -    go_lam :: [Var] -> Var -> CoreExpr -> CoercionR -> ([Var], CoreExpr)
    
    3134
    +    go_lam :: [Var] -> Var -> CoreExpr -> CastCoercion -> ([Var], CoreExpr)
    
    3135 3135
         -- (go_lam bs b e c) is same as (go_c bs (\b.e) c)
    
    3136
    -    go_lam bs b e co
    
    3136
    +    -- TODO: does it matter that ZCoercion will not do any of this?
    
    3137
    +    go_lam bs b e (CCoercion co)
    
    3137 3138
           | isTyVar b
    
    3138 3139
           , let Pair tyL tyR = coercionKind co
    
    3139 3140
           , assert (isForAllTy_ty tyL) $
    
    3140 3141
             isForAllTy_ty tyR
    
    3141 3142
           , isReflCo (mkSelCo SelForAll co)  -- See Note [collectBindersPushingCo]
    
    3142
    -      = go_c (b:bs) e (mkInstCo co (mkNomReflCo (mkTyVarTy b)))
    
    3143
    +      = go_c (b:bs) e (CCoercion (mkInstCo co (mkNomReflCo (mkTyVarTy b))))
    
    3143 3144
     
    
    3144 3145
           | isCoVar b
    
    3145 3146
           , let Pair tyL tyR = coercionKind co
    
    ... ... @@ -3147,7 +3148,7 @@ collectBindersPushingCo e
    3147 3148
             isForAllTy_co tyR
    
    3148 3149
           , isReflCo (mkSelCo SelForAll co)  -- See Note [collectBindersPushingCo]
    
    3149 3150
           , let cov = mkCoVarCo b
    
    3150
    -      = go_c (b:bs) e (mkInstCo co (mkNomReflCo (mkCoercionTy cov)))
    
    3151
    +      = go_c (b:bs) e (CCoercion (mkInstCo co (mkNomReflCo (mkCoercionTy cov))))
    
    3151 3152
     
    
    3152 3153
           | isId b
    
    3153 3154
           , let Pair tyL tyR = coercionKind co
    
    ... ... @@ -3155,9 +3156,9 @@ collectBindersPushingCo e
    3155 3156
           , (co_mult, co_arg, co_res) <- decomposeFunCo co
    
    3156 3157
           , isReflCo co_mult -- See Note [collectBindersPushingCo]
    
    3157 3158
           , isReflCo co_arg  -- See Note [collectBindersPushingCo]
    
    3158
    -      = go_c (b:bs) e co_res
    
    3159
    +      = go_c (b:bs) e (CCoercion co_res)
    
    3159 3160
     
    
    3160
    -      | otherwise = (reverse bs, mkCast (Lam b e) co)
    
    3161
    +    go_lam bs b e cco = (reverse bs, mkCastCo (Lam b e) cco)
    
    3161 3162
     
    
    3162 3163
     {- Note [collectBindersPushingCo]
    
    3163 3164
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    

  • compiler/GHC/Core/Opt/CprAnal.hs
    ... ... @@ -224,9 +224,9 @@ cprAnal' env (Cast e co)
    224 224
       where
    
    225 225
         (cpr_ty, e') = cprAnal env e
    
    226 226
         cpr_ty'
    
    227
    -      | cpr_ty == topCprType                        = topCprType -- cheap case first
    
    228
    -      | isRecNewTyConApp env (castCoercionRKind co) = topCprType -- See Note [CPR for recursive data constructors]
    
    229
    -      | otherwise                                   = cpr_ty
    
    227
    +      | cpr_ty == topCprType                                     = topCprType -- cheap case first
    
    228
    +      | isRecNewTyConApp env (castCoercionRKind (exprType e) co) = topCprType -- See Note [CPR for recursive data constructors]
    
    229
    +      | otherwise                                                = cpr_ty
    
    230 230
     
    
    231 231
     cprAnal' env (Tick t e)
    
    232 232
       = (cpr_ty, Tick t e')
    

  • compiler/GHC/Core/Opt/DmdAnal.hs
    ... ... @@ -2331,6 +2331,7 @@ coercionDmdEnv co = coercionsDmdEnv [co]
    2331 2331
     castCoercionDmdEnv :: CastCoercion -> DmdEnv
    
    2332 2332
     castCoercionDmdEnv (CCoercion co)    = coercionDmdEnv co
    
    2333 2333
     castCoercionDmdEnv (ZCoercion _ cos) = coVarSetDmdEnv cos
    
    2334
    +castCoercionDmdEnv ReflCastCo        = nopDmdEnv
    
    2334 2335
     
    
    2335 2336
     coercionsDmdEnv :: [Coercion] -> DmdEnv
    
    2336 2337
     coercionsDmdEnv cos
    

  • compiler/GHC/Core/Opt/SpecConstr.hs
    ... ... @@ -2693,7 +2693,7 @@ argToPat1 env in_scope val_env (Cast arg co) arg_occ arg_str
    2693 2693
               else
    
    2694 2694
                     return (interesting, Cast arg' co, strict_args) }
    
    2695 2695
       where
    
    2696
    -    ty2 = castCoercionRKind co
    
    2696
    +    ty2 = castCoercionRKind (exprType arg) co
    
    2697 2697
     
    
    2698 2698
       -- Check for a constructor application
    
    2699 2699
       -- NB: this *precedes* the Var case, so that we catch nullary constrs
    

  • compiler/GHC/Core/Ppr.hs
    ... ... @@ -173,6 +173,7 @@ noParens pp = pp
    173 173
     pprOptCastCoercion :: CastCoercion -> SDoc
    
    174 174
     pprOptCastCoercion (CCoercion co) = pprOptCo co
    
    175 175
     pprOptCastCoercion (ZCoercion ty cos) = pprOptZappedCo ty cos
    
    176
    +pprOptCastCoercion ReflCastCo         = text "ReflCastCo"
    
    176 177
     
    
    177 178
     pprOptZappedCo :: Type -> CoVarSet -> SDoc
    
    178 179
     pprOptZappedCo ty cos = sdocOption sdocSuppressCoercions $ \case
    

  • compiler/GHC/Core/TyCo/FVs.hs
    ... ... @@ -316,6 +316,7 @@ runTyCoVars f = appEndoOS f emptyVarSet
    316 316
     tyCoVarsOfCastCo :: CastCoercion -> TyCoVarSet
    
    317 317
     tyCoVarsOfCastCo (CCoercion co)     = coVarsOfCo co
    
    318 318
     tyCoVarsOfCastCo (ZCoercion ty cos) = tyCoVarsOfType ty `unionVarSet` cos
    
    319
    +tyCoVarsOfCastCo ReflCastCo         = emptyVarSet
    
    319 320
     
    
    320 321
     tyCoVarsOfType :: Type -> TyCoVarSet
    
    321 322
     -- The "deep" TyCoVars of the the type
    
    ... ... @@ -441,6 +442,7 @@ shallowCoVarsOfType ty = filterVarSet isCoVar $ shallowTyCoVarsOfType ty
    441 442
     shallowCoVarsOfCastCo :: CastCoercion -> CoVarSet
    
    442 443
     shallowCoVarsOfCastCo (CCoercion co) = shallowCoVarsOfCo co
    
    443 444
     shallowCoVarsOfCastCo (ZCoercion ty cos) = shallowCoVarsOfType ty `unionVarSet` cos
    
    445
    +shallowCoVarsOfCastCo ReflCastCo = emptyVarSet
    
    444 446
     
    
    445 447
     
    
    446 448
     {- *********************************************************************
    
    ... ... @@ -468,6 +470,7 @@ See #14880.
    468 470
     coVarsOfCastCo :: CastCoercion -> CoVarSet
    
    469 471
     coVarsOfCastCo (CCoercion co) = coVarsOfCo co
    
    470 472
     coVarsOfCastCo (ZCoercion ty cos) = coVarsOfType ty `unionVarSet` cos -- TODO cos doesn't include deep, this isn't enough?
    
    473
    +coVarsOfCastCo ReflCastCo = emptyVarSet
    
    471 474
     
    
    472 475
     -- See Note [Finding free coercion variables]
    
    473 476
     coVarsOfType  :: Type       -> CoVarSet
    
    ... ... @@ -705,6 +708,7 @@ tyCoFVsOfMCo mco fv_cand in_scope acc
    705 708
     tyCoFVsOfCastCoercion :: CastCoercion -> FV
    
    706 709
     tyCoFVsOfCastCoercion (CCoercion co) = tyCoFVsOfCo co
    
    707 710
     tyCoFVsOfCastCoercion (ZCoercion ty cos) = tyCoFVsOfType ty `unionFV` tyCoFVsOfCoVarSet cos
    
    711
    +tyCoFVsOfCastCoercion ReflCastCo = mempty
    
    708 712
     
    
    709 713
     tyCoFVsOfCoVarSet :: CoVarSet -> FV
    
    710 714
     tyCoFVsOfCoVarSet = nonDetStrictFoldVarSet (unionFV . tyCoFVsOfCoVar) emptyFV -- TODO better way? Nondeterminism?
    

  • compiler/GHC/Core/TyCo/Ppr.hs
    ... ... @@ -142,6 +142,7 @@ pprCastCo co = getPprStyle $ \ sty -> pprIfaceCastCoercion (tidyToIfaceCastCoSty
    142 142
     tidyToIfaceCastCoSty :: CastCoercion -> PprStyle -> IfaceCastCoercion
    
    143 143
     tidyToIfaceCastCoSty (CCoercion co)     sty = IfaceCCoercion (tidyToIfaceCoSty co sty)
    
    144 144
     tidyToIfaceCastCoSty (ZCoercion ty cos) sty = IfaceZCoercion (tidyToIfaceType ty) (map (flip tidyToIfaceCoSty sty . CoVarCo) (nonDetEltsUniqSet cos)) -- TODO
    
    145
    +tidyToIfaceCastCoSty ReflCastCo         _   = IfaceReflCastCo
    
    145 146
     
    
    146 147
     tidyToIfaceCoSty :: Coercion -> PprStyle -> IfaceCoercion
    
    147 148
     tidyToIfaceCoSty co sty
    

  • compiler/GHC/Core/TyCo/Rep.hs
    ... ... @@ -904,7 +904,7 @@ type KindMCoercion = MCoercionN -- See Note [KindCoercion]
    904 904
     data CastCoercion
    
    905 905
       = CCoercion CoercionR        -- Not zapped; the Coercion has Representational role
    
    906 906
       | ZCoercion Type CoVarSet    -- Zapped; stores only the RHS type and free CoVars
    
    907
    -  -- | ReflCastCo -- TODO
    
    907
    +  | ReflCastCo
    
    908 908
       deriving Data.Data
    
    909 909
     
    
    910 910
     -- | A 'Coercion' is concrete evidence of the equality/convertibility
    
    ... ... @@ -2143,6 +2143,7 @@ typesSize tys = foldr ((+) . typeSize) 0 tys
    2143 2143
     castCoercionSize :: CastCoercion -> Int
    
    2144 2144
     castCoercionSize (CCoercion co) = coercionSize co
    
    2145 2145
     castCoercionSize (ZCoercion ty cos) = typeSize ty + sizeVarSet cos
    
    2146
    +castCoercionSize ReflCastCo = 1
    
    2146 2147
     
    
    2147 2148
     coercionSize :: Coercion -> Int
    
    2148 2149
     coercionSize (Refl ty)             = typeSize ty
    

  • compiler/GHC/Core/TyCo/Subst.hs
    ... ... @@ -837,6 +837,7 @@ lookupTyVar (Subst _ _ tenv _) tv
    837 837
     substCastCo :: HasDebugCallStack => Subst -> CastCoercion -> CastCoercion
    
    838 838
     substCastCo subst (CCoercion co)     = CCoercion (substCo subst co)
    
    839 839
     substCastCo subst (ZCoercion ty cos) = ZCoercion (substTy subst ty) (substCoVarSet subst cos)
    
    840
    +substCastCo _     ReflCastCo         = ReflCastCo
    
    840 841
     
    
    841 842
     substCoVarSet :: HasDebugCallStack => Subst -> CoVarSet -> CoVarSet
    
    842 843
     substCoVarSet subst = nonDetStrictFoldVarSet (unionVarSet . shallowCoVarsOfCo . substCoVar subst) emptyVarSet -- TODO better impl; determinism?
    

  • compiler/GHC/Core/TyCo/Tidy.hs
    ... ... @@ -366,5 +366,6 @@ tidyCos :: TidyEnv -> [Coercion] -> [Coercion]
    366 366
     tidyCos env = strictMap (tidyCo env)
    
    367 367
     
    
    368 368
     tidyCastCo :: TidyEnv -> CastCoercion -> CastCoercion
    
    369
    -tidyCastCo env (CCoercion co) = CCoercion (tidyCo env co)
    
    369
    +tidyCastCo env (CCoercion co)     = CCoercion (tidyCo env co)
    
    370 370
     tidyCastCo env (ZCoercion ty cos) = ZCoercion (tidyType env ty) (mapVarSet (tidyTyCoVarOcc env) cos)
    
    371
    +tidyCastCo _   ReflCastCo         = ReflCastCo

  • compiler/GHC/Core/Utils.hs
    ... ... @@ -141,7 +141,7 @@ exprType (Let bind body)
    141 141
       , Type ty <- rhs           = substTyWithUnchecked [tv] [ty] (exprType body)
    
    142 142
       | otherwise                = exprType body
    
    143 143
     exprType (Case _ _ ty _)     = ty
    
    144
    -exprType (Cast _ co)         = castCoercionRKind co
    
    144
    +exprType (Cast e co)         = castCoercionRKind (exprType e) co
    
    145 145
     exprType (Tick _ e)          = exprType e
    
    146 146
     exprType (Lam binder expr)   = mkLamType binder (exprType expr)
    
    147 147
     exprType e@(App _ _)
    
    ... ... @@ -271,6 +271,7 @@ mkPiMCo v (MCo co) = MCo (mkPiCo Representational v co)
    271 271
     mkCastCo :: HasDebugCallStack => CoreExpr -> CastCoercion -> CoreExpr
    
    272 272
     mkCastCo expr (CCoercion co)     = mkCast expr co
    
    273 273
     mkCastCo expr (ZCoercion ty cos) = mkCastZ expr ty cos
    
    274
    +mkCastCo expr ReflCastCo         = expr
    
    274 275
     
    
    275 276
     -- | Wrap the given expression in the coercion safely, dropping
    
    276 277
     -- identity coercions and coalescing nested coercions
    
    ... ... @@ -2512,11 +2513,11 @@ c.f. add_evals in GHC.Core.Opt.Simplify.simplAlt
    2512 2513
     -- | A cheap equality test which bales out fast!
    
    2513 2514
     --      If it returns @True@ the arguments are definitely equal,
    
    2514 2515
     --      otherwise, they may or may not be equal.
    
    2515
    -cheapEqExpr :: Expr b -> Expr b -> Bool
    
    2516
    +cheapEqExpr :: CoreExpr -> CoreExpr -> Bool
    
    2516 2517
     cheapEqExpr = cheapEqExpr' (const False)
    
    2517 2518
     
    
    2518 2519
     -- | Cheap expression equality test, can ignore ticks by type.
    
    2519
    -cheapEqExpr' :: (CoreTickish -> Bool) -> Expr b -> Expr b -> Bool
    
    2520
    +cheapEqExpr' :: (CoreTickish -> Bool) -> CoreExpr -> CoreExpr -> Bool
    
    2520 2521
     {-# INLINE cheapEqExpr' #-}
    
    2521 2522
     cheapEqExpr' ignoreTick e1 e2
    
    2522 2523
       = go e1 e2
    
    ... ... @@ -2526,7 +2527,7 @@ cheapEqExpr' ignoreTick e1 e2
    2526 2527
         go (Type t1)  (Type t2)        = t1 `eqType` t2
    
    2527 2528
         go (Coercion c1) (Coercion c2) = c1 `eqCoercion` c2
    
    2528 2529
         go (App f1 a1) (App f2 a2)     = f1 `go` f2 && a1 `go` a2
    
    2529
    -    go (Cast e1 co1) (Cast e2 co2) = e1 `go` e2 && eqCastCoercion co1 co2
    
    2530
    +    go (Cast e1 co1) (Cast e2 co2) = e1 `go` e2 && eqCastCoercion (exprType e1) co1 co2
    
    2530 2531
     
    
    2531 2532
         go (Tick t1 e1) e2 | ignoreTick t1 = go e1 e2
    
    2532 2533
         go e1 (Tick t2 e2) | ignoreTick t2 = go e1 e2
    
    ... ... @@ -2622,7 +2623,7 @@ diffExpr _ env (Type t1) (Type t2) | eqTypeX env t1 t2 = []
    2622 2623
     diffExpr _   env (Coercion co1) (Coercion co2)
    
    2623 2624
                                            | eqCoercionX env co1 co2        = []
    
    2624 2625
     diffExpr top env (Cast e1 co1)  (Cast e2 co2)
    
    2625
    -  | eqCastCoercionX env co1 co2            = diffExpr top env e1 e2
    
    2626
    +  | eqCastCoercionX env (exprType e1) co1 (exprType e2) co2 = diffExpr top env e1 e2
    
    2626 2627
     diffExpr top env (Tick n1 e1)   e2
    
    2627 2628
       | not (tickishIsCode n1)                 = diffExpr top env e1 e2
    
    2628 2629
     diffExpr top env e1             (Tick n2 e2)
    

  • compiler/GHC/CoreToIface.hs
    ... ... @@ -273,6 +273,7 @@ toIfaceTyLit (CharTyLit x) = IfaceCharTyLit x
    273 273
     toIfaceCastCoercion :: CastCoercion -> IfaceCastCoercion
    
    274 274
     toIfaceCastCoercion (CCoercion co) = IfaceCCoercion (toIfaceCoercion co)
    
    275 275
     toIfaceCastCoercion (ZCoercion ty cos) = IfaceZCoercion (toIfaceType ty) (map (toIfaceCoercion . CoVarCo) (nonDetEltsUniqSet cos)) -- TODO determinism
    
    276
    +toIfaceCastCoercion ReflCastCo         = IfaceReflCastCo
    
    276 277
     
    
    277 278
     toIfaceCoercion :: Coercion -> IfaceCoercion
    
    278 279
     toIfaceCoercion = toIfaceCoercionX emptyVarSet
    

  • compiler/GHC/Iface/Rename.hs
    ... ... @@ -888,8 +888,9 @@ rnIfaceMCo IfaceMRefl = pure IfaceMRefl
    888 888
     rnIfaceMCo (IfaceMCo co) = IfaceMCo <$> rnIfaceCo co
    
    889 889
     
    
    890 890
     rnIfaceCastCo :: Rename IfaceCastCoercion
    
    891
    -rnIfaceCastCo (IfaceCCoercion co) = IfaceCCoercion <$> rnIfaceCo co
    
    891
    +rnIfaceCastCo (IfaceCCoercion co)     = IfaceCCoercion <$> rnIfaceCo co
    
    892 892
     rnIfaceCastCo (IfaceZCoercion ty cos) = IfaceZCoercion <$> rnIfaceType ty <*> mapM rnIfaceCo cos
    
    893
    +rnIfaceCastCo IfaceReflCastCo         = pure IfaceReflCastCo
    
    893 894
     
    
    894 895
     rnIfaceCo :: Rename IfaceCoercion
    
    895 896
     rnIfaceCo (IfaceReflCo ty)              = IfaceReflCo <$> rnIfaceType ty
    

  • compiler/GHC/Iface/Syntax.hs
    ... ... @@ -2075,8 +2075,9 @@ freeNamesIfMCoercion IfaceMRefl = emptyNameSet
    2075 2075
     freeNamesIfMCoercion (IfaceMCo co) = freeNamesIfCoercion co
    
    2076 2076
     
    
    2077 2077
     freeNamesIfCastCoercion :: IfaceCastCoercion -> NameSet
    
    2078
    -freeNamesIfCastCoercion (IfaceCCoercion co) = freeNamesIfCoercion co
    
    2078
    +freeNamesIfCastCoercion (IfaceCCoercion co)     = freeNamesIfCoercion co
    
    2079 2079
     freeNamesIfCastCoercion (IfaceZCoercion ty cos) = freeNamesIfType ty &&& fnList freeNamesIfCoercion cos
    
    2080
    +freeNamesIfCastCoercion IfaceReflCastCo         = emptyNameSet
    
    2080 2081
     
    
    2081 2082
     freeNamesIfCoercion :: IfaceCoercion -> NameSet
    
    2082 2083
     freeNamesIfCoercion (IfaceReflCo t) = freeNamesIfType t
    

  • compiler/GHC/Iface/Type.hs
    ... ... @@ -480,6 +480,7 @@ data IfaceMCoercion
    480 480
     data IfaceCastCoercion
    
    481 481
       = IfaceCCoercion IfaceCoercion
    
    482 482
       | IfaceZCoercion IfaceType [IfaceCoercion]
    
    483
    +  | IfaceReflCastCo
    
    483 484
       deriving (Eq, Ord)
    
    484 485
     
    
    485 486
     data IfaceCoercion
    
    ... ... @@ -2040,10 +2041,12 @@ pprIfaceTyLit (IfaceCharTyLit c) = text (show c)
    2040 2041
     pprIfaceCastCoercion :: IfaceCastCoercion -> SDoc
    
    2041 2042
     pprIfaceCastCoercion (IfaceCCoercion co) = pprIfaceCoercion co
    
    2042 2043
     pprIfaceCastCoercion (IfaceZCoercion ty cos) = text "Zap" <+> pprParendIfaceType ty <+> ppr cos
    
    2044
    +pprIfaceCastCoercion IfaceReflCastCo = text "ReflCastCo"
    
    2043 2045
     
    
    2044 2046
     pprParendIfaceCastCoercion :: IfaceCastCoercion -> SDoc
    
    2045 2047
     pprParendIfaceCastCoercion (IfaceCCoercion co) = pprParendIfaceCoercion co
    
    2046 2048
     pprParendIfaceCastCoercion (IfaceZCoercion ty cos) = parens (pprIfaceCastCoercion (IfaceZCoercion ty cos))
    
    2049
    +pprParendIfaceCastCoercion IfaceReflCastCo = text "ReflCastCo"
    
    2047 2050
     
    
    2048 2051
     pprIfaceCoercion, pprParendIfaceCoercion :: IfaceCoercion -> SDoc
    
    2049 2052
     pprIfaceCoercion = ppr_co topPrec
    
    ... ... @@ -2447,6 +2450,7 @@ instance Binary IfaceCastCoercion where
    2447 2450
               putByte bh 2
    
    2448 2451
               put_ bh a
    
    2449 2452
               put_ bh b
    
    2453
    +  put_ bh IfaceReflCastCo = putByte bh 3
    
    2450 2454
     
    
    2451 2455
       get bh = do
    
    2452 2456
         tag <- getByte bh
    
    ... ... @@ -2456,6 +2460,7 @@ instance Binary IfaceCastCoercion where
    2456 2460
              2 -> do a <- get bh
    
    2457 2461
                      b <- get bh
    
    2458 2462
                      return $ IfaceZCoercion a b
    
    2463
    +         3 -> return IfaceReflCastCo
    
    2459 2464
              _ -> panic ("get IfaceCastCoercion " ++ show tag)
    
    2460 2465
     
    
    2461 2466
     
    
    ... ... @@ -2643,6 +2648,7 @@ instance NFData IfaceCastCoercion where
    2643 2648
       rnf = \case
    
    2644 2649
         IfaceCCoercion f1 -> rnf f1
    
    2645 2650
         IfaceZCoercion f1 f2 -> rnf f1 `seq` rnf f2
    
    2651
    +    IfaceReflCastCo -> ()
    
    2646 2652
     
    
    2647 2653
     instance NFData IfaceCoercion where
    
    2648 2654
       rnf = \case
    

  • compiler/GHC/IfaceToCore.hs
    ... ... @@ -1582,6 +1582,7 @@ tcIfaceTyLit (IfaceCharTyLit n) = return (CharTyLit n)
    1582 1582
     tcIfaceCastCoercion :: IfaceCastCoercion -> IfL CastCoercion
    
    1583 1583
     tcIfaceCastCoercion (IfaceCCoercion co)     = CCoercion <$> tcIfaceCo co
    
    1584 1584
     tcIfaceCastCoercion (IfaceZCoercion ty cos) = ZCoercion <$> tcIfaceType ty <*> (shallowCoVarsOfCos <$> mapM tcIfaceCo cos)
    
    1585
    +tcIfaceCastCoercion IfaceReflCastCo         = pure ReflCastCo
    
    1585 1586
     
    
    1586 1587
     tcIfaceCo :: IfaceCoercion -> IfL Coercion
    
    1587 1588
     tcIfaceCo = go
    

  • compiler/GHC/Tc/Types/Evidence.hs
    ... ... @@ -649,6 +649,7 @@ optSubTypeHsWrapper wrap
    649 649
           CCoercion co     -> not (anyFreeVarsOfCo (== v) co)
    
    650 650
           ZCoercion ty cvs -> not (anyFreeVarsOfType (== v) ty)
    
    651 651
                            && not (v `elemVarSet` cvs)
    
    652
    +      ReflCastCo       -> True
    
    652 653
     
    
    653 654
         not_in_submult :: TyVar -> SubMultCo -> Bool
    
    654 655
         not_in_submult v = \case
    

  • compiler/GHC/Tc/Zonk/Type.hs
    ... ... @@ -551,6 +551,7 @@ _zonkCosToCos :: [Coercion] -> ZonkTcM [Coercion]
    551 551
     zonkCastCo :: CastCoercion -> ZonkTcM CastCoercion
    
    552 552
     zonkCastCo (CCoercion co) = CCoercion <$> zonkCoToCo co
    
    553 553
     zonkCastCo (ZCoercion ty cos) = ZCoercion <$> zonkTcTypeToTypeX ty <*> zonkCoVarSet cos
    
    554
    +zonkCastCo ReflCastCo = pure ReflCastCo
    
    554 555
     
    
    555 556
     zonkCoVarSet :: CoVarSet -> ZonkTcM CoVarSet
    
    556 557
     zonkCoVarSet = fmap shallowCoVarsOfCos . mapM zonkCoVarOcc . nonDetEltsUniqSet
    
    ... ... @@ -1868,6 +1869,8 @@ zonkEvTerm (EvCastExpr e (CCoercion co) co_res_ty)
    1868 1869
            }
    
    1869 1870
     zonkEvTerm ev@(EvCastExpr _ (ZCoercion{}) _)
    
    1870 1871
       = pprPanic "zonkEvTerm: ZCoercion" (ppr ev)
    
    1872
    +zonkEvTerm (EvCastExpr e ReflCastCo _)
    
    1873
    +  = EvExpr <$> zonkCoreExpr e
    
    1871 1874
     zonkEvTerm (EvTypeable ty ev)
    
    1872 1875
       = EvTypeable <$> zonkTcTypeToTypeX ty <*> zonkEvTypeable ev
    
    1873 1876
     zonkEvTerm (EvFun { et_tvs = tvs, et_given = evs