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

Commits:

18 changed files:

Changes:

  • compiler/GHC/Core/Coercion.hs
    ... ... @@ -78,7 +78,6 @@ module GHC.Core.Coercion (
    78 78
     
    
    79 79
             -- ** Free variables
    
    80 80
             tyCoVarsOfCo, tyCoVarsOfCos, coVarsOfCo,
    
    81
    -        tyCoFVsOfCo, tyCoVarsOfCoDSet,
    
    82 81
             coercionSize, anyFreeVarsOfCo,
    
    83 82
     
    
    84 83
             -- ** Substitution
    

  • compiler/GHC/Core/FVs.hs
    ... ... @@ -35,7 +35,7 @@ module GHC.Core.FVs (
    35 35
             ruleLhsFreeIds, ruleLhsFreeIdsList,
    
    36 36
             ruleRhsFreeVars, rulesRhsFreeIds,
    
    37 37
     
    
    38
    -        exprFVs, addBndrFV, addBndrsFV, unitFV,
    
    38
    +        exprFVs, addCoreBndrFV, addCoreBndrsFV, unitFV,
    
    39 39
     
    
    40 40
             -- * Orphan names
    
    41 41
             orphNamesOfType, orphNamesOfTypes, orphNamesOfAxiomLHS,
    
    ... ... @@ -61,8 +61,9 @@ import GHC.Types.Id.Info
    61 61
     import GHC.Types.Name.Set
    
    62 62
     import GHC.Types.Name
    
    63 63
     import GHC.Types.Tickish
    
    64
    -import GHC.Types.Var.Set
    
    65 64
     import GHC.Types.Var
    
    65
    +import GHC.Types.Var.Set
    
    66
    +import GHC.Types.Var.FV
    
    66 67
     import GHC.Core.Type
    
    67 68
     import GHC.Core.TyCo.Rep
    
    68 69
     import GHC.Core.TyCo.FVs
    
    ... ... @@ -154,10 +155,10 @@ exprsFreeIdsList = dVarSetElems . exprsFreeIdsDSet
    154 155
     bindFreeVars :: CoreBind -> VarSet
    
    155 156
     bindFreeVars = runFVSelectiveSet isLocalVar . bind_fvs
    
    156 157
     
    
    157
    -bind_fvs :: CoreBind -> SelectiveFVRes
    
    158
    +bind_fvs :: CoreBind -> SelectiveFV
    
    158 159
     bind_fvs (NonRec b r) = rhs_fvs (b,r)
    
    159
    -bind_fvs (Rec prs)    = addBndrsSelectiveFVRes (map fst prs) $
    
    160
    -                        mapUnionFVRes rhs_fvs prs
    
    160
    +bind_fvs (Rec prs)    = addBndrsSelectiveFV (map fst prs) $
    
    161
    +                        mapUnionFV rhs_fvs prs
    
    161 162
     
    
    162 163
     -- | Finds free variables in an expression selected by a predicate
    
    163 164
     exprSomeFreeVars :: InterestingVarFun   -- ^ Says which 'Var's are interesting
    
    ... ... @@ -199,20 +200,20 @@ exprsSomeFreeVarsDSet :: InterestingVarFun -- ^ Says which 'Var's are interestin
    199 200
                           -> DVarSet
    
    200 201
     exprsSomeFreeVarsDSet fv_cand = runFVSelective fv_cand . exprsFVs
    
    201 202
     
    
    202
    -addBndrFV :: CoreBndr -> SelectiveFVRes -> SelectiveFVRes
    
    203
    -addBndrFV bndr fvr
    
    203
    +addCoreBndrFV :: CoreBndr -> SelectiveFV -> SelectiveFV
    
    204
    +addCoreBndrFV bndr fvr
    
    204 205
       = bndrTypeTyCoFVs bndr `mappend`
    
    205 206
             -- Include type variables in the binder's type
    
    206 207
             --      (not just Ids; coercion variables too!)
    
    207
    -    addBndrSelectiveFVRes bndr fvr
    
    208
    +    addBndrSelectiveFV bndr fvr
    
    208 209
     
    
    209
    -addBndrsFV :: [CoreBndr] -> SelectiveFVRes -> SelectiveFVRes
    
    210
    -addBndrsFV bndrs fv = foldr addBndrFV fv bndrs
    
    210
    +addCoreBndrsFV :: [CoreBndr] -> SelectiveFV -> SelectiveFV
    
    211
    +addCoreBndrsFV bndrs fv = foldr addCoreBndrFV fv bndrs
    
    211 212
     
    
    212
    -unitFV :: Var -> SelectiveFVRes
    
    213
    +unitFV :: Var -> SelectiveFV
    
    213 214
     -- Deals with an occurrence
    
    214 215
     -- Shallow: does not look at the kind
    
    215
    -unitFV v = FVRes (\bvs -> EndoOS (do_it bvs))
    
    216
    +unitFV v = MkFV (\bvs -> EndoOS (do_it bvs))
    
    216 217
       where
    
    217 218
         do_it (is_interesting,bvs) acc
    
    218 219
           | not (is_interesting v) = acc  -- The "selective" bit
    
    ... ... @@ -220,49 +221,49 @@ unitFV v = FVRes (\bvs -> EndoOS (do_it bvs))
    220 221
           | v `elemDVarSet` acc    = acc
    
    221 222
           | otherwise              = acc `extendDVarSet` v
    
    222 223
     
    
    223
    -exprsFVs :: [CoreExpr] -> SelectiveFVRes
    
    224
    -exprsFVs = mapUnionFVRes exprFVs
    
    224
    +exprsFVs :: [CoreExpr] -> SelectiveFV
    
    225
    +exprsFVs = mapUnionFV exprFVs
    
    225 226
     
    
    226
    -exprFVs :: CoreExpr -> SelectiveFVRes
    
    227
    -exprFVs (Type ty)       = tyCoFVsOfType ty
    
    228
    -exprFVs (Coercion co)   = tyCoFVsOfCo co
    
    227
    +exprFVs :: CoreExpr -> SelectiveFV
    
    228
    +exprFVs (Type ty)       = shallowSelTypeFV ty
    
    229
    +exprFVs (Coercion co)   = shallowSelCoFV co
    
    229 230
     exprFVs (Var var)       = unitFV var
    
    230 231
     exprFVs (Lit _)         = mempty
    
    231 232
     exprFVs (Tick t expr)   = tickish_fvs t `mappend` exprFVs expr
    
    232 233
     exprFVs (App fun arg)   = exprFVs fun `mappend` exprFVs arg
    
    233
    -exprFVs (Lam bndr body) = addBndrFV bndr (exprFVs body)
    
    234
    -exprFVs (Cast expr co)  = exprFVs expr `mappend` tyCoFVsOfCo co
    
    234
    +exprFVs (Lam bndr body) = addCoreBndrFV bndr (exprFVs body)
    
    235
    +exprFVs (Cast expr co)  = exprFVs expr `mappend` shallowSelCoFV co
    
    235 236
     exprFVs (Case scrut bndr ty alts)
    
    236
    -  = exprFVs scrut `mappend` tyCoFVsOfType ty `mappend`
    
    237
    -    addBndrFV bndr (mapUnionFVRes alt_fvs alts)
    
    237
    +  = exprFVs scrut `mappend` shallowSelTypeFV ty `mappend`
    
    238
    +    addCoreBndrFV bndr (mapUnionFV alt_fvs alts)
    
    238 239
       where
    
    239
    -    alt_fvs (Alt _ bndrs rhs) = addBndrsFV bndrs (exprFVs rhs)
    
    240
    +    alt_fvs (Alt _ bndrs rhs) = addCoreBndrsFV bndrs (exprFVs rhs)
    
    240 241
     exprFVs (Let (NonRec bndr rhs) body)
    
    241
    -  = rhs_fvs (bndr, rhs) `mappend` addBndrFV bndr (exprFVs body)
    
    242
    +  = rhs_fvs (bndr, rhs) `mappend` addCoreBndrFV bndr (exprFVs body)
    
    242 243
     exprFVs (Let (Rec pairs) body)
    
    243
    -  = addBndrsFV (map fst pairs) $
    
    244
    -    mapUnionFVRes rhs_fvs pairs `mappend` exprFVs body
    
    244
    +  = addCoreBndrsFV (map fst pairs) $
    
    245
    +    mapUnionFV rhs_fvs pairs `mappend` exprFVs body
    
    245 246
     
    
    246 247
     ---------
    
    247
    -rhs_fvs :: (Id, CoreExpr) -> SelectiveFVRes
    
    248
    +rhs_fvs :: (Id, CoreExpr) -> SelectiveFV
    
    248 249
     rhs_fvs (bndr, rhs) = exprFVs rhs `mappend`
    
    249 250
                           bndrRuleAndUnfoldingFVs bndr
    
    250 251
             -- Treat any RULES as extra RHSs of the binding
    
    251 252
     
    
    252 253
     ---------
    
    253
    -tickish_fvs :: CoreTickish -> SelectiveFVRes
    
    254
    -tickish_fvs (Breakpoint _ _ ids) = mapUnionFVRes unitFV ids
    
    254
    +tickish_fvs :: CoreTickish -> SelectiveFV
    
    255
    +tickish_fvs (Breakpoint _ _ ids) = mapUnionFV unitFV ids
    
    255 256
     tickish_fvs _ = mempty
    
    256 257
     
    
    257 258
     ---------
    
    258
    -bndrTypeTyCoFVs :: Var -> SelectiveFVRes
    
    259
    +bndrTypeTyCoFVs :: Var -> SelectiveFV
    
    259 260
     -- Find the free variables of a binder.
    
    260 261
     -- In the case of ids, don't forget the multiplicity field!
    
    261 262
     bndrTypeTyCoFVs var
    
    262
    -  = tyCoFVsOfType (varType var) `mappend` mult_fvs
    
    263
    +  = shallowSelTypeFV (varType var) `mappend` mult_fvs
    
    263 264
       where
    
    264 265
         mult_fvs = case varMultMaybe var of
    
    265
    -                 Just mult -> tyCoFVsOfType mult
    
    266
    +                 Just mult -> shallowSelTypeFV mult
    
    266 267
                      Nothing   -> mempty
    
    267 268
     
    
    268 269
     dBndrTypeTyCoVars :: Var -> DTyCoVarSet
    
    ... ... @@ -278,7 +279,7 @@ dBndrFreeVars :: Id -> DVarSet
    278 279
     -- Shallow free vars
    
    279 280
     dBndrFreeVars id = runFVSelective isLocalVar $ bndrFVs id
    
    280 281
     
    
    281
    -bndrFVs :: Id -> SelectiveFVRes
    
    282
    +bndrFVs :: Id -> SelectiveFV
    
    282 283
     -- Shallow free vars of types, rules, and inlining
    
    283 284
     bndrFVs id = assert (isId id) $
    
    284 285
                  bndrTypeTyCoFVs id `mappend`
    
    ... ... @@ -290,7 +291,7 @@ bndrRuleAndUnfoldingVarsDSet = runFVSelective isLocalVar . bndrRuleAndUnfoldingF
    290 291
     bndrRuleAndUnfoldingVars :: Id -> VarSet
    
    291 292
     bndrRuleAndUnfoldingVars = dVarSetToVarSet . bndrRuleAndUnfoldingVarsDSet
    
    292 293
     
    
    293
    -bndrRuleAndUnfoldingFVs :: Id -> SelectiveFVRes
    
    294
    +bndrRuleAndUnfoldingFVs :: Id -> SelectiveFV
    
    294 295
     bndrRuleAndUnfoldingFVs id
    
    295 296
       | isId id   = idRuleFVs id `mappend` idUnfoldingFVs id
    
    296 297
       | otherwise = mempty
    
    ... ... @@ -298,7 +299,7 @@ bndrRuleAndUnfoldingFVs id
    298 299
     idRuleVars :: Id -> VarSet  -- Does *not* include CoreUnfolding vars
    
    299 300
     idRuleVars = dVarSetToVarSet . ruleInfoFreeVars . idSpecialisation
    
    300 301
     
    
    301
    -idRuleFVs :: Id -> SelectiveFVRes
    
    302
    +idRuleFVs :: Id -> SelectiveFV
    
    302 303
     idRuleFVs id = assert (isId id) $
    
    303 304
                    strictFoldDVarSet (mappend . unitFV) mempty $
    
    304 305
                    ruleInfoFreeVars (idSpecialisation id)
    
    ... ... @@ -311,21 +312,21 @@ idUnfoldingVars :: Id -> VarSet
    311 312
     -- we might get out-of-scope variables
    
    312 313
     idUnfoldingVars = runFVSelectiveSet isLocalVar . idUnfoldingFVs
    
    313 314
     
    
    314
    -idUnfoldingFVs :: Id -> SelectiveFVRes
    
    315
    +idUnfoldingFVs :: Id -> SelectiveFV
    
    315 316
     idUnfoldingFVs id = stableUnfoldingFVs (realIdUnfolding id) `orElse` mempty
    
    316 317
     
    
    317 318
     stableUnfoldingVars :: Unfolding -> Maybe VarSet
    
    318 319
     stableUnfoldingVars unf = fmap (runFVSelectiveSet isLocalVar) $
    
    319 320
                               stableUnfoldingFVs unf
    
    320 321
     
    
    321
    -stableUnfoldingFVs :: Unfolding -> Maybe SelectiveFVRes
    
    322
    +stableUnfoldingFVs :: Unfolding -> Maybe SelectiveFV
    
    322 323
     stableUnfoldingFVs unf
    
    323 324
       = case unf of
    
    324 325
           CoreUnfolding { uf_tmpl = rhs, uf_src = src }
    
    325 326
              | isStableSource src
    
    326 327
              -> Just (exprFVs rhs)
    
    327 328
           DFunUnfolding { df_bndrs = bndrs, df_args = args }
    
    328
    -         -> Just (addBndrsFV bndrs (exprsFVs args))
    
    329
    +         -> Just (addCoreBndrsFV bndrs (exprsFVs args))
    
    329 330
                 -- DFuns are top level, so no fvs from types of bndrs
    
    330 331
           _other -> Nothing
    
    331 332
     
    
    ... ... @@ -497,13 +498,13 @@ data RuleFVsFrom
    497 498
     
    
    498 499
     -- | Those locally-defined variables free in the left and/or right hand sides
    
    499 500
     -- of the rule, depending on the first argument.
    
    500
    -ruleFVs :: RuleFVsFrom -> CoreRule -> SelectiveFVRes
    
    501
    +ruleFVs :: RuleFVsFrom -> CoreRule -> SelectiveFV
    
    501 502
     ruleFVs !_   (BuiltinRule {}) = mempty
    
    502 503
     ruleFVs from (Rule { ru_fn = _do_not_include
    
    503 504
                          -- See Note [Rule free var hack]
    
    504 505
                        , ru_bndrs = bndrs
    
    505 506
                        , ru_rhs = rhs, ru_args = args })
    
    506
    -  = addBndrsFV bndrs (exprsFVs exprs)
    
    507
    +  = addCoreBndrsFV bndrs (exprsFVs exprs)
    
    507 508
       where
    
    508 509
         exprs = case from of
    
    509 510
           LhsOnly   -> args
    
    ... ... @@ -512,8 +513,8 @@ ruleFVs from (Rule { ru_fn = _do_not_include
    512 513
     
    
    513 514
     -- | Those locally-defined variables free in the left and/or right hand sides
    
    514 515
     -- from several rules, depending on the first argument.
    
    515
    -rulesFVs :: RuleFVsFrom -> [CoreRule] -> SelectiveFVRes
    
    516
    -rulesFVs from = mapUnionFVRes (ruleFVs from)
    
    516
    +rulesFVs :: RuleFVsFrom -> [CoreRule] -> SelectiveFV
    
    517
    +rulesFVs from = mapUnionFV (ruleFVs from)
    
    517 518
     
    
    518 519
     -- | Those variables free in the right hand side of a rule returned as a
    
    519 520
     -- non-deterministic set
    
    ... ... @@ -666,7 +667,7 @@ freeVarsBind (Rec binds) body_fvs
    666 667
         (binders, rhss) = unzip binds
    
    667 668
         rhss2        = map freeVars rhss
    
    668 669
         rhs_body_fvs = foldr (unionDVarSet . freeVarsOf) body_fvs rhss2
    
    669
    -    binders_fvs  = runFVSelective isLocalVar $ mapUnionFVRes bndrRuleAndUnfoldingFVs binders
    
    670
    +    binders_fvs  = runFVSelective isLocalVar $ mapUnionFV bndrRuleAndUnfoldingFVs binders
    
    670 671
                        -- See Note [The FVAnn invariant]
    
    671 672
         all_fvs      = rhs_body_fvs `unionDVarSet` binders_fvs
    
    672 673
                 -- The "delBinderFV" happens after adding the idSpecVars,
    

  • compiler/GHC/Core/Opt/SetLevels.hs
    ... ... @@ -101,6 +101,7 @@ import GHC.Types.Id
    101 101
     import GHC.Types.Id.Info
    
    102 102
     import GHC.Types.Var
    
    103 103
     import GHC.Types.Var.Set
    
    104
    +import GHC.Types.Var.FV
    
    104 105
     import GHC.Types.Unique.Set   ( nonDetStrictFoldUniqSet )
    
    105 106
     import GHC.Types.Unique.DSet  ( getUniqDSet )
    
    106 107
     import GHC.Types.Var.Env
    
    ... ... @@ -1380,7 +1381,7 @@ lvlBind env (AnnRec pairs)
    1380 1381
         bind_fvs = ((unionDVarSets [ freeVarsOf rhs | (_, rhs) <- pairs])
    
    1381 1382
                     `unionDVarSet`
    
    1382 1383
                     (runFVSelective isLocalVar $
    
    1383
    -                 mapUnionFVRes (\(bndr,_) -> bndrFVs bndr) pairs))
    
    1384
    +                 mapUnionFV (\(bndr,_) -> bndrFVs bndr) pairs))
    
    1384 1385
                    `delDVarSetList`
    
    1385 1386
                     bndrs
    
    1386 1387
     
    

  • compiler/GHC/Core/Opt/Specialise.hs
    ... ... @@ -54,6 +54,7 @@ import GHC.Types.Id.Make ( voidArgId, voidPrimId )
    54 54
     import GHC.Types.Var
    
    55 55
     import GHC.Types.Var.Set
    
    56 56
     import GHC.Types.Var.Env
    
    57
    +import GHC.Types.Var.FV
    
    57 58
     import GHC.Types.Id
    
    58 59
     import GHC.Types.Id.Info
    
    59 60
     import GHC.Types.InlinePragma
    
    ... ... @@ -2512,10 +2513,10 @@ specArgsFVs :: InterestingVarFun -> [SpecArg] -> VarSet
    2512 2513
     -- Find the shallow deep free vars of the SpecArgs that are not already in scope
    
    2513 2514
     specArgsFVs interesting args
    
    2514 2515
       = runFVSelectiveSet interesting $
    
    2515
    -    mapUnionFVRes get args
    
    2516
    +    mapUnionFV get args
    
    2516 2517
       where
    
    2517
    -    get :: SpecArg -> SelectiveFVRes
    
    2518
    -    get (SpecType ty)   = tyCoFVsOfType ty
    
    2518
    +    get :: SpecArg -> SelectiveFV
    
    2519
    +    get (SpecType ty)   = shallowSelTypeFV ty
    
    2519 2520
         get (SpecDict dx)   = exprFVs dx
    
    2520 2521
         get UnspecType      = mempty
    
    2521 2522
         get UnspecArg       = mempty
    

  • compiler/GHC/Core/Subst.hs
    ... ... @@ -48,6 +48,7 @@ import GHC.Core.Coercion( mkCoVarCo, substCoVarBndr )
    48 48
     import GHC.Core.TyCo.FVs
    
    49 49
     
    
    50 50
     import GHC.Types.Var.Set
    
    51
    +import GHC.Types.Var.FV
    
    51 52
     import GHC.Types.Var.Env as InScopeSet
    
    52 53
     import GHC.Types.Id
    
    53 54
     import GHC.Types.Name     ( Name )
    
    ... ... @@ -594,10 +595,10 @@ substDVarSet subst@(Subst _ _ tv_env cv_env) fvs
    594 595
       = runFVSelective isLocalVar $
    
    595 596
         strictFoldDVarSet (mappend . do_one) mempty fvs
    
    596 597
       where
    
    597
    -  do_one :: Var -> SelectiveFVRes
    
    598
    +  do_one :: Var -> SelectiveFV
    
    598 599
       do_one fv
    
    599
    -     | isTyVar fv = tyCoFVsOfType (lookupVarEnv tv_env fv `orElse` mkTyVarTy fv)
    
    600
    -     | isCoVar fv = tyCoFVsOfCo   (lookupVarEnv cv_env fv `orElse` mkCoVarCo fv)
    
    600
    +     | isTyVar fv = shallowSelTypeFV (lookupVarEnv tv_env fv `orElse` mkTyVarTy fv)
    
    601
    +     | isCoVar fv = shallowSelCoFV   (lookupVarEnv cv_env fv `orElse` mkCoVarCo fv)
    
    601 602
          | otherwise  = exprFVs (lookupIdSubst subst fv)
    
    602 603
     
    
    603 604
     ------------------
    

  • compiler/GHC/Core/TyCo/FVs.hs
    1
    -{-# LANGUAGE MultiWayIf, PatternSynonyms #-}
    
    1
    +{-# LANGUAGE MultiWayIf #-}
    
    2 2
     
    
    3 3
     module GHC.Core.TyCo.FVs
    
    4
    -  (     -- FVRes and friends
    
    5
    -        FVRes( runFV, FVRes ),
    
    6
    -        addBndrFVRes, addBndrsFVRes, addBndrSelectiveFVRes, addBndrsSelectiveFVRes,
    
    7
    -        mapUnionFVRes, shallowUnitFVRes, deepUnitFVRes,
    
    8
    -        BoundVars, VarSetFVRes, DVarSetFVRes, SelectiveFVRes,
    
    9
    -        TyCoFVRes, DTyCoFVRes,
    
    10
    -        runFVTop, runFVAcc, runTyCoVars, runTyCoVarsDSet,
    
    11
    -        runFVSelective, runFVSelectiveList, runFVSelectiveSet,
    
    12
    -        InterestingVarFun,
    
    13
    -
    
    14
    -        -- Shallow
    
    4
    +  (     -- Shallow
    
    15 5
             shallowTyCoVarsOfType, shallowTyCoVarsOfTypes,
    
    16 6
             shallowTyCoVarsOfCo, shallowTyCoVarsOfCos,
    
    17 7
             shallowTyCoVarsOfTyVarEnv, shallowTyCoVarsOfCoVarEnv,
    
    ... ... @@ -20,13 +10,13 @@ module GHC.Core.TyCo.FVs
    20 10
             tyCoVarsOfType, tyCoVarsOfTypes, tyCoVarsOfTypesList,
    
    21 11
             tyCoVarsOfThings,
    
    22 12
             tyCoVarsOfCo, tyCoVarsOfCos, tyCoVarsOfMCo,
    
    23
    -        deepTcvFolder, deepTypeFV, deepCoFV,
    
    13
    +        deepTcvFolder, deepTypeFV, deepTypesFV, deepCoFV,
    
    24 14
     
    
    25 15
             -- Deep, deterministic
    
    26 16
             tyCoVarsOfTypeDSet, tyCoVarsOfTypesDSet, tyCoVarsOfTypeList,
    
    27 17
             tyCoVarsOfCoDSet, tyCoVarsOfCoList,
    
    28 18
             tyCoVarsOfThingsDSet,
    
    29
    -        detTyCoVarsOfType, detTyCoVarsOfTypes, detTyCoVarsOfCo,
    
    19
    +        deepDetTypeFV, deepDetTypesFV, deepDetCoFV,
    
    30 20
     
    
    31 21
             -- Selective
    
    32 22
             someTyCoVarsOfType, someTyCoVarsOfTypes,
    
    ... ... @@ -37,7 +27,7 @@ module GHC.Core.TyCo.FVs
    37 27
             coVarsOfCoDSet, coVarsOfCosDSet,
    
    38 28
     
    
    39 29
             -- Shallow, deterministic, composable
    
    40
    -        tyCoFVsOfType, tyCoFVsOfCo,
    
    30
    +        shallowSelTypeFV, shallowSelCoFV,
    
    41 31
     
    
    42 32
             -- Almost devoid
    
    43 33
             almostDevoidCoVarOfCo,
    
    ... ... @@ -76,6 +66,7 @@ import GHC.Core.TyCon
    76 66
     import GHC.Core.Coercion.Axiom( CoAxiomRule(..), BuiltInFamRewrite(..), coAxiomTyCon )
    
    77 67
     
    
    78 68
     import GHC.Types.Var
    
    69
    +import GHC.Types.Var.FV
    
    79 70
     import GHC.Types.Unique.FM
    
    80 71
     import GHC.Types.Unique.Set
    
    81 72
     
    
    ... ... @@ -86,7 +77,6 @@ import GHC.Utils.Misc
    86 77
     import GHC.Utils.EndoOS
    
    87 78
     
    
    88 79
     import GHC.Data.Pair
    
    89
    -import GHC.Exts (oneShot)
    
    90 80
     
    
    91 81
     import Data.Semigroup
    
    92 82
     
    
    ... ... @@ -120,97 +110,42 @@ Examples:
    120 110
       (a : (k:Type))           {a}        {a,k}
    
    121 111
       forall (a:(k:Type)). a   {k}        {k}
    
    122 112
       (a:k->Type) (b:k)        {a,b}      {a,b,k}
    
    123
    --}
    
    124
    -
    
    125
    -
    
    126
    -{- Note [Free variables of types]
    
    127
    -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    128
    -The family of functions tyCoVarsOfType, tyCoVarsOfTypes etc, returns
    
    129
    -a VarSet that is closed over the types of its variables.  More precisely,
    
    130
    -  if    S = tyCoVarsOfType( t )
    
    131
    -  and   (a:k) is in S
    
    132
    -  then  tyCoVarsOftype( k ) is a subset of S
    
    133
    -
    
    134
    -Example: The tyCoVars of this ((a:* -> k) Int) is {a, k}.
    
    135
    -
    
    136
    -We could /not/ close over the kinds of the variable occurrences, and
    
    137
    -instead do so at call sites, but it seems that we always want to do
    
    138
    -so, so it's easiest to do it here.
    
    139
    -
    
    140
    -It turns out that getting the free variables of types is performance critical,
    
    141
    -so we profiled several versions, exploring different implementation strategies.
    
    142
    -
    
    143
    -1. Baseline version: uses FV naively. Essentially:
    
    144
    -
    
    145
    -   tyCoVarsOfType ty = fvVarSet $ tyCoFVsOfType ty
    
    146
    -
    
    147
    -   This is not nice, because FV introduces some overhead to implement
    
    148
    -   determinism, and through its "interesting var" function, neither of which
    
    149
    -   we need here, so they are a complete waste.
    
    150 113
     
    
    151
    -2. UnionVarSet version: instead of reusing the FV-based code, we simply used
    
    152
    -   VarSets directly, trying to avoid the overhead of FV. E.g.:
    
    153 114
     
    
    154
    -   -- FV version:
    
    155
    -   tyCoFVsOfType (AppTy fun arg)    a b c = (tyCoFVsOfType fun `unionFV` tyCoFVsOfType arg) a b c
    
    115
    +Note [Computing deep free variables]
    
    116
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    117
    +tyCoVarsOfType computes the /deep/ free variables of a type; that is, if
    
    118
    +`a::k` is in the result, then so are the free vars of `k`.  We say that the
    
    119
    +resulting set is "closed over kinds".
    
    156 120
     
    
    157
    -   -- UnionVarSet version:
    
    158
    -   tyCoVarsOfType (AppTy fun arg)    = (tyCoVarsOfType fun `unionVarSet` tyCoVarsOfType arg)
    
    121
    +But we must take care (see #14880):
    
    159 122
     
    
    160
    -   This looks deceptively similar, but while FV internally builds a list- and
    
    161
    -   set-generating function, the VarSet functions manipulate sets directly, and
    
    162
    -   the latter performs a lot worse than the naive FV version.
    
    123
    +1. Efficiency. If we have Proxy (a::ki) -> Proxy (a::ki) -> Proxy (a::ki), then
    
    124
    +   we don't want to have to traverse ki more than once.
    
    163 125
     
    
    164
    -3. Accumulator-style VarSet version: this is what we use now. We do use VarSet
    
    165
    -   as our data structure, but delegate the actual work to a new
    
    166
    -   ty_co_vars_of_...  family of functions, which use accumulator style and the
    
    167
    -   "in-scope set" filter found in the internals of FV, but without the
    
    168
    -   determinism overhead.
    
    169
    -
    
    170
    -See #14880.
    
    171
    -
    
    172
    -Note [Closing over free variable kinds]
    
    173
    -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    174
    -tyCoVarsOfType and tyCoFVsOfType, while traversing a type, will also close over
    
    175
    -free variable kinds. In previous GHC versions, this happened naively: whenever
    
    176
    -we would encounter an occurrence of a free type variable, we would close over
    
    177
    -its kind. This, however is wrong for two reasons (see #14880):
    
    178
    -
    
    179
    -1. Efficiency. If we have Proxy (a::k) -> Proxy (a::k) -> Proxy (a::k), then
    
    180
    -   we don't want to have to traverse k more than once.
    
    181
    -
    
    182
    -2. Correctness. Imagine we have forall k. b -> k, where b has
    
    183
    -   kind k, for some k bound in an outer scope. If we look at b's kind inside
    
    126
    +2. Correctness. Imagine we have forall k. (b::k) -> k, where b has
    
    127
    +   kind k, for some k bound in an /outer/ scope. If we look at b's kind inside
    
    184 128
        the forall, we'll collect that k is free and then remove k from the set of
    
    185 129
        free variables. This is plain wrong. We must instead compute that b is free
    
    186 130
        and then conclude that b's kind is free.
    
    187 131
     
    
    188
    -An obvious first approach is to move the closing-over-kinds from the
    
    189
    -occurrences of a type variable to after finding the free vars - however, this
    
    190
    -turns out to introduce performance regressions, and isn't even entirely
    
    191
    -correct.
    
    192
    -
    
    193
    -In fact, it isn't even important *when* we close over kinds; what matters is
    
    194
    -that we handle each type var exactly once, and that we do it in the right
    
    195
    -context.
    
    132
    +An obvious first approach is to compute the /shallow/ free variables of the type,
    
    133
    +and /then/ close over kinds.   But that turns out not to be very efficient.
    
    134
    +Fortunately, there is a simpler way, which works with the accumulating
    
    135
    +free-var story described in (FV1) of Note [Finding free variables] in
    
    136
    +GHC.Types.Var.FV.  At an occurrence of a variable (a::k)
    
    196 137
     
    
    197
    -So the next approach we tried was to use the "in-scope set" part of FV or the
    
    198
    -equivalent argument in the accumulator-style `ty_co_vars_of_type` function, to
    
    199
    -say "don't bother with variables we have already closed over". This should work
    
    200
    -fine in theory, but the code is complicated and doesn't perform well.
    
    138
    +* Check if `a` is a locally-bound var; if so, ignore it.
    
    201 139
     
    
    202
    -But there is a simpler way, which is implemented here. Consider the two points
    
    203
    -above:
    
    140
    +* Check if `a` is already in the accumulator; if so, ignore it because we have
    
    141
    +  deal with its kind already. Also pre-checking set membership before inserting
    
    142
    +  ends up not only being faster,
    
    204 143
     
    
    205
    -1. Efficiency: we now have an accumulator, so the second time we encounter 'a',
    
    206
    -   we'll ignore it, certainly not looking at its kind - this is why
    
    207
    -   pre-checking set membership before inserting ends up not only being faster,
    
    208
    -   but also being correct.
    
    144
    +* Otherwise add `a` to the accumulator,
    
    145
    +  AND add on the free vars of its kind `k`.
    
    146
    +  BUT in this latter step, start with an empty BoundVars set.
    
    209 147
     
    
    210
    -2. Correctness: we have an "in-scope set" (I think we should call it it a
    
    211
    -  "bound-var set"), specifying variables that are bound by a forall in the type
    
    212
    -  we are traversing; we simply ignore these variables, certainly not looking at
    
    213
    -  their kind.
    
    148
    +This twist is implemented in `deepUnitFV`
    
    214 149
     
    
    215 150
     So now consider:
    
    216 151
     
    
    ... ... @@ -222,22 +157,6 @@ this is our first encounter with b; we want the free vars of its kind. But we
    222 157
     want to behave as if we took the free vars of its kind at the end; that is,
    
    223 158
     with no bound vars in scope.
    
    224 159
     
    
    225
    -So the solution is easy. The old code was this:
    
    226
    -
    
    227
    -  ty_co_vars_of_type (TyVarTy v) is acc
    
    228
    -    | v `elemVarSet` is  = acc
    
    229
    -    | v `elemVarSet` acc = acc
    
    230
    -    | otherwise          = ty_co_vars_of_type (tyVarKind v) is (extendVarSet acc v)
    
    231
    -
    
    232
    -Now all we need to do is take the free vars of tyVarKind v *with an empty
    
    233
    -bound-var set*, thus:
    
    234
    -
    
    235
    -ty_co_vars_of_type (TyVarTy v) is acc
    
    236
    -  | v `elemVarSet` is  = acc
    
    237
    -  | v `elemVarSet` acc = acc
    
    238
    -  | otherwise          = ty_co_vars_of_type (tyVarKind v) emptyVarSet (extendVarSet acc v)
    
    239
    -                                                          ^^^^^^^^^^^
    
    240
    -
    
    241 160
     And that's it. This works because a variable is either bound or free. If it is bound,
    
    242 161
     then we won't look at it at all. If it is free, then all the variables free in its
    
    243 162
     kind are free -- regardless of whether some local variable has the same Unique.
    
    ... ... @@ -266,147 +185,6 @@ The sole reason is in Note [Emitting the residual implication in simplifyInfer]
    266 185
     in GHC.Tc.Solver.  Yuk.  This is not pretty.
    
    267 186
     -}
    
    268 187
     
    
    269
    -{- *********************************************************************
    
    270
    -*                                                                      *
    
    271
    -          Endo for free variables
    
    272
    -*                                                                      *
    
    273
    -********************************************************************* -}
    
    274
    -
    
    275
    -{- Note [Accumulating parameter free variables]
    
    276
    -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    277
    -We can use foldType to build an accumulating-parameter version of a
    
    278
    -free-var finder, thus:
    
    279
    -
    
    280
    -    fvs :: Type -> TyCoVarSet
    
    281
    -    fvs ty = appEndo (foldType folder ty) emptyVarSet
    
    282
    -
    
    283
    -Recall that
    
    284
    -    foldType :: TyCoFolder env a -> env -> Type -> a
    
    285
    -
    
    286
    -    newtype Endo a = Endo (a -> a)   -- In Data.Monoid
    
    287
    -    instance Monoid a => Monoid (Endo a) where
    
    288
    -       (Endo f) `mappend` (Endo g) = Endo (f.g)
    
    289
    -
    
    290
    -    appEndo :: Endo a -> a -> a
    
    291
    -    appEndo (Endo f) x = f x
    
    292
    -
    
    293
    -So `mappend` for Endos is just function composition.
    
    294
    -
    
    295
    -It's very important that, after optimisation, we end up with
    
    296
    -* an arity-three function
    
    297
    -* that is strict in the accumulator
    
    298
    -
    
    299
    -   fvs env (TyVarTy v) acc
    
    300
    -      | v `elemVarSet` env = acc
    
    301
    -      | v `elemVarSet` acc = acc
    
    302
    -      | otherwise          = acc `extendVarSet` v
    
    303
    -   fvs env (AppTy t1 t2)   = fvs env t1 (fvs env t2 acc)
    
    304
    -   ...
    
    305
    -
    
    306
    -The "strict in the accumulator" part is to ensure that in the
    
    307
    -AppTy equation we don't build a thunk for (fvs env t2 acc).
    
    308
    -
    
    309
    -The optimiser does do all this, but not very robustly. It depends
    
    310
    -critically on the basic arity-2 function not being exported, so that
    
    311
    -all its calls are visibly to three arguments. This analysis is
    
    312
    -done by the Call Arity pass.
    
    313
    -
    
    314
    -TL;DR: check this regularly!
    
    315
    --}
    
    316
    -
    
    317
    -
    
    318
    -{- *********************************************************************
    
    319
    -*                                                                      *
    
    320
    -          Free-var result type
    
    321
    -*                                                                      *
    
    322
    -********************************************************************* -}
    
    323
    -
    
    324
    -type InterestingVarFun = Var -> Bool
    
    325
    -
    
    326
    -newtype FVRes env acc = FVRes' { runFV :: env -> acc }
    
    327
    -  -- Caries an environment (typically empty, or a set of in-scope variables)
    
    328
    -  -- and a composable accumulator
    
    329
    -
    
    330
    -pattern FVRes :: (env -> acc) -> FVRes env acc
    
    331
    -pattern FVRes f <- FVRes' f
    
    332
    -      where
    
    333
    -        FVRes f = FVRes' (oneShot f)
    
    334
    -         -- oneShot: this is the core of the one-shot trick!
    
    335
    -         -- Note [The one-shot state monad trick] in  GHC.Utils.Monad.
    
    336
    -
    
    337
    -instance Semigroup a => Semigroup (FVRes env a) where
    
    338
    -  f1 <>  f2 = FVRes (\env -> runFV f1 env <> runFV f2 env)
    
    339
    -
    
    340
    -instance Monoid a => Monoid (FVRes env a) where
    
    341
    -  mempty  = FVRes (\_ -> mempty)
    
    342
    -
    
    343
    -addBndrFV :: (env -> env) -> FVRes env a -> FVRes env a
    
    344
    -{-# INLINE addBndrFV #-}
    
    345
    -addBndrFV upd f = FVRes (\bvs -> runFV f $! upd bvs)
    
    346
    -    -- Strict application to avoid making a thunk
    
    347
    -
    
    348
    -addBndrFVRes :: TyCoVar -> FVRes BoundVars a -> FVRes BoundVars a
    
    349
    -addBndrFVRes tcv = addBndrFV (\bvs -> extendVarSet bvs tcv)
    
    350
    -
    
    351
    -addBndrsFVRes :: [Var] -> FVRes BoundVars a -> FVRes BoundVars a
    
    352
    -addBndrsFVRes tcvs = addBndrFV (\bvs -> extendVarSetList bvs tcvs)
    
    353
    -
    
    354
    -addBndrSelectiveFVRes :: TyCoVar -> FVRes (f, BoundVars) a -> FVRes (f, BoundVars) a
    
    355
    -addBndrSelectiveFVRes tcv
    
    356
    -  = addBndrFV (\(f,bvs) -> let !bvs' = extendVarSet bvs tcv
    
    357
    -                               -- Strict let to avoid thunks
    
    358
    -                           in (f,bvs'))
    
    359
    -addBndrsSelectiveFVRes :: [Var] -> FVRes (f, BoundVars) a -> FVRes (f, BoundVars) a
    
    360
    -addBndrsSelectiveFVRes bs
    
    361
    -  = addBndrFV (\(f,bvs) -> let !bvs' = extendVarSetList bvs bs
    
    362
    -                               -- Strict let to avoid thunks
    
    363
    -                           in (f,bvs'))
    
    364
    -
    
    365
    -mapUnionFVRes :: (Foldable t, Monoid acc)
    
    366
    -          => (a -> FVRes env acc) -> t a -> FVRes env acc
    
    367
    -{-# INLINE mapUnionFVRes #-}
    
    368
    -mapUnionFVRes f xs = foldr (mappend . f) mempty xs
    
    369
    -
    
    370
    -
    
    371
    -type BoundVars = TyCoVarSet
    
    372
    -
    
    373
    -
    
    374
    -type VarSetFVRes    = FVRes BoundVars (EndoOS TyCoVarSet)
    
    375
    -type DVarSetFVRes   = FVRes BoundVars (EndoOS DTyCoVarSet)
    
    376
    -type SelectiveFVRes = FVRes (InterestingVarFun, BoundVars) (EndoOS DVarSet)
    
    377
    --- VarSetFVRes:    collects a VarSet
    
    378
    --- DVarSetFVRes:   collects a DVarSet (deterministic)
    
    379
    --- SelectiveFVRes: selectively collects a DVarSet
    
    380
    -
    
    381
    -type TyCoFVRes  = VarSetFVRes
    
    382
    -type DTyCoFVRes = DVarSetFVRes
    
    383
    -
    
    384
    -runFVTop :: FVRes BoundVars a -> a
    
    385
    -{-# INLINE runFVTop #-}
    
    386
    -runFVTop f = runFV f emptyVarSet
    
    387
    -
    
    388
    -runFVAcc :: FVRes BoundVars (EndoOS a) -> a -> a
    
    389
    -{-# INLINE runFVAcc #-}
    
    390
    -runFVAcc f = runEndoOS (runFVTop f)
    
    391
    -
    
    392
    -runTyCoVars :: TyCoFVRes -> TyCoVarSet
    
    393
    -{-# INLINE runTyCoVars #-}
    
    394
    -runTyCoVars f = runFVAcc f emptyVarSet
    
    395
    -
    
    396
    -runTyCoVarsDSet :: DTyCoFVRes -> DTyCoVarSet
    
    397
    -{-# INLINE runTyCoVarsDSet #-}
    
    398
    -runTyCoVarsDSet f = runFVAcc f emptyDVarSet
    
    399
    -
    
    400
    -runFVSelective :: InterestingVarFun -> SelectiveFVRes -> DVarSet
    
    401
    -runFVSelective interesting f
    
    402
    -  = runEndoOS (runFV f (interesting, emptyVarSet)) emptyDVarSet
    
    403
    -
    
    404
    -runFVSelectiveList :: InterestingVarFun -> SelectiveFVRes -> [Var]
    
    405
    -runFVSelectiveList interesting f = dVarSetElems (runFVSelective interesting f)
    
    406
    -
    
    407
    -runFVSelectiveSet :: InterestingVarFun -> SelectiveFVRes -> VarSet
    
    408
    -runFVSelectiveSet interesting f = dVarSetToVarSet (runFVSelective interesting f)
    
    409
    -
    
    410 188
     
    
    411 189
     {- *********************************************************************
    
    412 190
     *                                                                      *
    
    ... ... @@ -429,7 +207,7 @@ tyCoVarsOfTypes tys = runTyCoVars (deepTypesFV tys)
    429 207
     
    
    430 208
     tyCoVarsOfCo :: Coercion -> TyCoVarSet
    
    431 209
     -- The "deep" TyCoVars of the the coercion
    
    432
    --- See Note [Free variables of types]
    
    210
    +-- See Note [Computing deep free variables]
    
    433 211
     tyCoVarsOfCo co = runTyCoVars (deepCoFV co)
    
    434 212
     
    
    435 213
     tyCoVarsOfMCo :: MCoercion -> TyCoVarSet
    
    ... ... @@ -441,17 +219,17 @@ tyCoVarsOfCos cos = runTyCoVars (deepCosFV cos)
    441 219
     
    
    442 220
     tyCoVarsOfThings :: Foldable t => (a -> Type) -> t a -> TyCoVarSet
    
    443 221
     -- Works over a collection of things from which we can extract a type
    
    444
    --- See Note [Free variables of types]
    
    222
    +-- See Note [Computing deep free variables]
    
    445 223
     tyCoVarsOfThings get_ty things
    
    446
    -  = runTyCoVars $ mapUnionFVRes (deepTypeFV . get_ty) things
    
    224
    +  = runTyCoVars $ mapUnionFV (deepTypeFV . get_ty) things
    
    447 225
     
    
    448
    -deepTypeFV  :: Type       -> TyCoFVRes
    
    449
    -deepTypesFV :: [Type]     -> TyCoFVRes
    
    450
    -deepCoFV    :: Coercion   -> TyCoFVRes
    
    451
    -deepCosFV   :: [Coercion] -> TyCoFVRes
    
    226
    +deepTypeFV  :: Type       -> TyCoFV
    
    227
    +deepTypesFV :: [Type]     -> TyCoFV
    
    228
    +deepCoFV    :: Coercion   -> TyCoFV
    
    229
    +deepCosFV   :: [Coercion] -> TyCoFV
    
    452 230
     (deepTypeFV, deepTypesFV, deepCoFV, deepCosFV) = foldTyCo deepTcvFolder
    
    453 231
     
    
    454
    -deepTcvFolder :: TyCoFolder TyCoFVRes
    
    232
    +deepTcvFolder :: TyCoFolder TyCoFV
    
    455 233
     -- It's important that we use a one-shot EndoOS, to ensure that all
    
    456 234
     -- the free-variable finders are eta-expanded.  Lacking the one-shot-ness
    
    457 235
     -- led to some big slow downs.  See Note [The one-shot state monad trick]
    
    ... ... @@ -459,22 +237,23 @@ deepTcvFolder :: TyCoFolder TyCoFVRes
    459 237
     deepTcvFolder = TyCoFolder { tcf_view = noView  -- See Note [Free vars and synonyms]
    
    460 238
                                , tcf_tyvar = do_tcv, tcf_covar = do_tcv
    
    461 239
                                , tcf_hole  = do_hole
    
    462
    -                           , tcf_tycobinder = addBndrFVRes }
    
    240
    +                           , tcf_tycobinder = addBndrFV }
    
    463 241
       where
    
    464
    -    do_tcv :: TyVar -> TyCoFVRes
    
    465
    -    do_tcv = deepUnitFVRes deepTypeFV
    
    242
    +    do_tcv :: TyVar -> TyCoFV
    
    243
    +    do_tcv = deepUnitFV deepTypeFV
    
    466 244
     
    
    467
    -    do_hole :: CoercionHole -> TyCoFVRes
    
    245
    +    do_hole :: CoercionHole -> TyCoFV
    
    468 246
         do_hole hole = deepTypeFV (varType (coHoleCoVar hole))
    
    469 247
                          -- We don't collect the CoercionHole itself, but we /do/
    
    470 248
                          -- need to collect the free variables of its /kind/
    
    471 249
                          -- See Note [CoercionHoles and coercion free variables]
    
    472 250
     
    
    473
    -deepUnitFVRes :: (Type -> TyCoFVRes) -> TyCoVar -> TyCoFVRes
    
    251
    +deepUnitFV :: (Type -> TyCoFV) -> TyCoVar -> TyCoFV
    
    474 252
     -- Deal with a single TyCoVar
    
    475 253
     -- Takes a function to find free vars of the kind
    
    476
    -deepUnitFVRes fvs_of_kind v
    
    477
    -  = FVRes (\bvs -> EndoOS (do_it bvs))
    
    254
    +-- See Note [Computing deep free variables]
    
    255
    +deepUnitFV fvs_of_kind v
    
    256
    +  = MkFV (\bvs -> EndoOS (do_it bvs))
    
    478 257
       where
    
    479 258
         do_it :: BoundVars -> TyCoVarSet -> TyCoVarSet
    
    480 259
         do_it bvs acc | v `elemVarSet` bvs = acc
    
    ... ... @@ -490,23 +269,23 @@ deepUnitFVRes fvs_of_kind v
    490 269
     ********************************************************************* -}
    
    491 270
     
    
    492 271
     shallowTyCoVarsOfType :: Type -> TyCoVarSet
    
    493
    --- See Note [Free variables of types]
    
    494
    -shallowTyCoVarsOfType ty = runTyCoVars (shallow_ty ty)
    
    272
    +-- See Note [Shallow and deep free variables]
    
    273
    +shallowTyCoVarsOfType ty = runTyCoVars (shallowTypeFV ty)
    
    495 274
     
    
    496 275
     shallowTyCoVarsOfTypes :: [Type] -> TyCoVarSet
    
    497
    -shallowTyCoVarsOfTypes tys = runTyCoVars (shallow_tys tys)
    
    276
    +shallowTyCoVarsOfTypes tys = runTyCoVars (shallowTypesFV tys)
    
    498 277
     
    
    499 278
     shallowTyCoVarsOfCo :: Coercion -> TyCoVarSet
    
    500
    -shallowTyCoVarsOfCo co = runTyCoVars (shallow_co co)
    
    279
    +shallowTyCoVarsOfCo co = runTyCoVars (shallowCoFV co)
    
    501 280
     
    
    502 281
     shallowTyCoVarsOfCos :: [Coercion] -> TyCoVarSet
    
    503
    -shallowTyCoVarsOfCos cos = runTyCoVars (shallow_cos cos)
    
    282
    +shallowTyCoVarsOfCos cos = runTyCoVars (shallowCosFV cos)
    
    504 283
     
    
    505 284
     -- | Returns free variables of types, including kind variables as
    
    506 285
     -- a non-deterministic set. For type synonyms it does /not/ expand the
    
    507 286
     -- synonym.
    
    508 287
     shallowTyCoVarsOfTyVarEnv :: TyVarEnv Type -> TyCoVarSet
    
    509
    --- See Note [Free variables of types]
    
    288
    +-- See Note [Shallow and deep free variables]of types]
    
    510 289
     shallowTyCoVarsOfTyVarEnv tys = shallowTyCoVarsOfTypes (nonDetEltsUFM tys)
    
    511 290
       -- It's OK to use nonDetEltsUFM here because we immediately
    
    512 291
       -- forget the ordering by returning a set
    
    ... ... @@ -516,25 +295,25 @@ shallowTyCoVarsOfCoVarEnv cos = shallowTyCoVarsOfCos (nonDetEltsUFM cos)
    516 295
       -- It's OK to use nonDetEltsUFM here because we immediately
    
    517 296
       -- forget the ordering by returning a set
    
    518 297
     
    
    519
    -shallow_ty  :: Type       -> TyCoFVRes
    
    520
    -shallow_tys :: [Type]     -> TyCoFVRes
    
    521
    -shallow_co  :: Coercion   -> TyCoFVRes
    
    522
    -shallow_cos :: [Coercion] -> TyCoFVRes
    
    523
    -(shallow_ty, shallow_tys, shallow_co, shallow_cos)
    
    298
    +shallowTypeFV  :: Type       -> TyCoFV
    
    299
    +shallowTypesFV :: [Type]     -> TyCoFV
    
    300
    +shallowCoFV    :: Coercion   -> TyCoFV
    
    301
    +shallowCosFV   :: [Coercion] -> TyCoFV
    
    302
    +(shallowTypeFV, shallowTypesFV, shallowCoFV, shallowCosFV)
    
    524 303
        = foldTyCo shallowTcvFolder
    
    525 304
     
    
    526
    -shallowTcvFolder :: TyCoFolder TyCoFVRes
    
    305
    +shallowTcvFolder :: TyCoFolder TyCoFV
    
    527 306
     shallowTcvFolder = TyCoFolder { tcf_view = noView  -- See Note [Free vars and synonyms]
    
    528 307
                                   , tcf_tyvar = do_tcv, tcf_covar = do_tcv
    
    529 308
                                   , tcf_hole  = do_hole
    
    530
    -                              , tcf_tycobinder = addBndrFVRes }
    
    309
    +                              , tcf_tycobinder = addBndrFV }
    
    531 310
       where
    
    532
    -    do_tcv = shallowUnitFVRes
    
    311
    +    do_tcv = shallowUnitFV
    
    533 312
         do_hole _  = mempty   -- Ignore coercion holes
    
    534 313
     
    
    535
    -shallowUnitFVRes :: TyCoVar -> TyCoFVRes
    
    536
    -shallowUnitFVRes v
    
    537
    -  = FVRes (\bvs -> EndoOS (do_it bvs))
    
    314
    +shallowUnitFV :: TyCoVar -> TyCoFV
    
    315
    +shallowUnitFV v
    
    316
    +  = MkFV (\bvs -> EndoOS (do_it bvs))
    
    538 317
       where
    
    539 318
         do_it bvs acc | v `elemVarSet` bvs = acc
    
    540 319
                       | v `elemVarSet` acc = acc
    
    ... ... @@ -547,71 +326,71 @@ shallowUnitFVRes v
    547 326
     *                                                                      *
    
    548 327
     ********************************************************************* -}
    
    549 328
     
    
    550
    --- | `tyCoVarsOfTypeDSet` that returns free variables of a type in a deterministic
    
    551
    --- set. For explanation of why using `VarSet` is not deterministic see
    
    552
    --- Note [Deterministic FV] in "GHC.Utils.FV".
    
    329
    +-- | `tyCoVarsOfTypeDSet` that returns deep free variables of a type in a
    
    330
    +-- deterministic-- set. For explanation of why using `VarSet` is not deterministic
    
    331
    +-- see Note [Deterministic FV] in "GHC.TYpes.Var.FV".
    
    553 332
     tyCoVarsOfTypeDSet :: Type -> DTyCoVarSet
    
    554
    --- See Note [Free variables of types]
    
    555
    -tyCoVarsOfTypeDSet ty = runTyCoVarsDSet (detTyCoVarsOfType ty)
    
    333
    +-- See Note [Computing deep free variables]
    
    334
    +tyCoVarsOfTypeDSet ty = runTyCoVarsDSet (deepDetTypeFV ty)
    
    556 335
     
    
    557 336
     -- | Returns free variables of types, including kind variables as
    
    558 337
     -- a deterministic set. For type synonyms it does /not/ expand the
    
    559 338
     -- synonym.
    
    560 339
     tyCoVarsOfTypesDSet :: [Type] -> DTyCoVarSet
    
    561
    --- See Note [Free variables of types]
    
    562
    -tyCoVarsOfTypesDSet tys = runTyCoVarsDSet (detTyCoVarsOfTypes tys)
    
    340
    +-- See Note [Computing deep free variables]
    
    341
    +tyCoVarsOfTypesDSet tys = runTyCoVarsDSet (deepDetTypesFV tys)
    
    563 342
     
    
    564 343
     tyCoVarsOfThingsDSet :: Foldable t => (a -> Type) -> t a -> DTyCoVarSet
    
    565 344
     -- Works over a collection of things from which we can extract a type
    
    566
    --- See Note [Free variables of types]
    
    345
    +-- See Note [Computing deep free variables]
    
    567 346
     tyCoVarsOfThingsDSet get_ty things
    
    568
    -  = runTyCoVarsDSet (mapUnionFVRes (detTyCoVarsOfType . get_ty) things)
    
    347
    +  = runTyCoVarsDSet (mapUnionFV (deepDetTypeFV . get_ty) things)
    
    569 348
     
    
    570 349
     -- | `tyCoVarsOfTypeList` returns free variables of a type in deterministic
    
    571 350
     -- order. For explanation of why using `VarSet` is not deterministic see
    
    572
    --- Note [Deterministic FV] in "GHC.Utils.FV".
    
    351
    +-- Note [Deterministic FV] in "GHC.Types.Var.FV".
    
    573 352
     tyCoVarsOfTypeList :: Type -> [TyCoVar]
    
    574
    --- See Note [Free variables of types]
    
    353
    +-- See Note [Computing deep free variables]
    
    575 354
     tyCoVarsOfTypeList ty = dVarSetElems $ tyCoVarsOfTypeDSet ty
    
    576 355
     
    
    577 356
     tyCoVarsOfCoDSet :: Coercion -> DTyCoVarSet
    
    578
    --- See Note [Free variables of types]
    
    579
    -tyCoVarsOfCoDSet ty = runTyCoVarsDSet (detTyCoVarsOfCo ty)
    
    357
    +-- See Note [Computing deep free variables]
    
    358
    +tyCoVarsOfCoDSet ty = runTyCoVarsDSet (deepDetCoFV ty)
    
    580 359
     
    
    581 360
     tyCoVarsOfCoList :: Coercion -> [TyCoVar]
    
    582
    --- See Note [Free variables of types]
    
    361
    +-- See Note [Computing deep free variables]
    
    583 362
     tyCoVarsOfCoList ty = dVarSetElems $ tyCoVarsOfCoDSet ty
    
    584 363
     
    
    585 364
     -- | Returns free variables of types, including kind variables as
    
    586 365
     -- a deterministically ordered list. For type synonyms it does /not/ expand the
    
    587 366
     -- synonym.
    
    588 367
     tyCoVarsOfTypesList :: [Type] -> [TyCoVar]
    
    589
    --- See Note [Free variables of types]
    
    368
    +-- See Note [Computing deep free variables]
    
    590 369
     tyCoVarsOfTypesList tys = dVarSetElems $ tyCoVarsOfTypesDSet tys
    
    591 370
     
    
    592
    -detTyCoVarsOfType  :: Type   -> DTyCoFVRes
    
    593
    -detTyCoVarsOfTypes :: [Type] -> DTyCoFVRes
    
    594
    -detTyCoVarsOfCo    :: Coercion -> DTyCoFVRes
    
    595
    -(detTyCoVarsOfType, detTyCoVarsOfTypes, detTyCoVarsOfCo, _)
    
    596
    -  = foldTyCo deepDetTcvFolder
    
    371
    +deepDetTypeFV  :: Type   -> DTyCoFV
    
    372
    +deepDetTypesFV :: [Type] -> DTyCoFV
    
    373
    +deepDetCoFV    :: Coercion -> DTyCoFV
    
    374
    +(deepDetTypeFV, deepDetTypesFV, deepDetCoFV, _) = foldTyCo deepDetTcvFolder
    
    597 375
     
    
    598
    -deepDetTcvFolder :: TyCoFolder DTyCoFVRes
    
    376
    +deepDetTcvFolder :: TyCoFolder DTyCoFV
    
    599 377
     -- This one returns a /deterministic/ list
    
    600 378
     -- See `deepTcvFolder` for the general pattern
    
    601 379
     deepDetTcvFolder
    
    602 380
       = TyCoFolder { tcf_view = noView
    
    603 381
                    , tcf_tyvar = do_tcv, tcf_covar = do_tcv
    
    604 382
                    , tcf_hole  = do_hole
    
    605
    -               , tcf_tycobinder = addBndrFVRes }
    
    383
    +               , tcf_tycobinder = addBndrFV }
    
    606 384
       where
    
    607
    -    do_tcv = deepDetUnitFVRes detTyCoVarsOfType
    
    608
    -    do_hole hole = detTyCoVarsOfType (varType (coHoleCoVar hole))
    
    385
    +    do_tcv = deepDetUnitFV deepDetTypeFV
    
    386
    +    do_hole hole = deepDetTypeFV (varType (coHoleCoVar hole))
    
    609 387
     
    
    610
    -deepDetUnitFVRes :: (Type -> DTyCoFVRes) -> TyCoVar -> DTyCoFVRes
    
    388
    +deepDetUnitFV :: (Type -> DTyCoFV) -> TyCoVar -> DTyCoFV
    
    611 389
     -- Deal with a single TyCoVar
    
    612 390
     -- Takes a function to find free vars of the kind
    
    613
    -deepDetUnitFVRes fvs_of_kind v
    
    614
    -  = FVRes (\bvs -> EndoOS (do_it bvs))
    
    391
    +-- See Note [Computing deep free variables]
    
    392
    +deepDetUnitFV fvs_of_kind v
    
    393
    +  = MkFV (\bvs -> EndoOS (do_it bvs))
    
    615 394
       where
    
    616 395
         do_it :: BoundVars -> DTyCoVarSet -> DTyCoVarSet
    
    617 396
         do_it bvs acc | v `elemVarSet` bvs  = acc
    
    ... ... @@ -627,28 +406,28 @@ deepDetUnitFVRes fvs_of_kind v
    627 406
     
    
    628 407
     someTyCoVarsOfType :: (TyCoVar -> Bool) -> Type -> [TyCoVar]
    
    629 408
     someTyCoVarsOfType interesting
    
    630
    -  = runFVSelectiveList interesting . tyCoFVsOfType
    
    409
    +  = runFVSelectiveList interesting . shallowSelTypeFV
    
    631 410
     
    
    632 411
     someTyCoVarsOfTypes :: (TyCoVar -> Bool) -> [Type] -> [TyCoVar]
    
    633 412
     someTyCoVarsOfTypes interesting
    
    634
    -  = runFVSelectiveList interesting . mapUnionFVRes tyCoFVsOfType
    
    413
    +  = runFVSelectiveList interesting . mapUnionFV shallowSelTypeFV
    
    635 414
     
    
    636
    -tyCoFVsOfType :: Type -> SelectiveFVRes
    
    637
    -tyCoFVsOfCo   :: Coercion -> SelectiveFVRes
    
    415
    +shallowSelTypeFV :: Type -> SelectiveFV
    
    416
    +shallowSelCoFV   :: Coercion -> SelectiveFV
    
    638 417
     -- Returns shallow free vars
    
    639
    --- See Note [Free variables of types]
    
    640
    -(tyCoFVsOfType, _, tyCoFVsOfCo, _) = foldTyCo selectiveTcvFolder
    
    418
    +-- See Note [Shallow and deep free variables]
    
    419
    +(shallowSelTypeFV, _, shallowSelCoFV, _) = foldTyCo selectiveTcvFolder
    
    641 420
     
    
    642
    -selectiveTcvFolder :: TyCoFolder SelectiveFVRes
    
    421
    +selectiveTcvFolder :: TyCoFolder SelectiveFV
    
    643 422
     -- This one takes an `InterestingVarFun`, and returns shallow free vars
    
    644 423
     -- See `shallowTcvFolder` for the general pattern
    
    645 424
     selectiveTcvFolder
    
    646 425
       = TyCoFolder { tcf_view  = noView  -- See Note [Free vars and synonyms]
    
    647 426
                    , tcf_tyvar = do_tcv, tcf_covar = do_tcv
    
    648 427
                    , tcf_hole  = do_hole
    
    649
    -               , tcf_tycobinder = addBndrSelectiveFVRes }
    
    428
    +               , tcf_tycobinder = addBndrSelectiveFV }
    
    650 429
       where
    
    651
    -    do_tcv v = FVRes (\bvs -> EndoOS (do_it bvs))
    
    430
    +    do_tcv v = MkFV (\bvs -> EndoOS (do_it bvs))
    
    652 431
           where
    
    653 432
             do_it (is_interesting,bvs) acc
    
    654 433
               | not (is_interesting v) = acc  -- The "selective" bit
    
    ... ... @@ -656,7 +435,7 @@ selectiveTcvFolder
    656 435
               | v `elemDVarSet` acc    = acc
    
    657 436
               | otherwise              = acc `extendDVarSet` v
    
    658 437
     
    
    659
    -    do_hole hole = tyCoFVsOfType (varType (coHoleCoVar hole))
    
    438
    +    do_hole hole = shallowSelTypeFV (varType (coHoleCoVar hole))
    
    660 439
     
    
    661 440
     
    
    662 441
     {- *********************************************************************
    
    ... ... @@ -686,24 +465,24 @@ coVarsOfTypes :: [Type] -> CoVarSet
    686 465
     coVarsOfCo    :: Coercion   -> CoVarSet
    
    687 466
     coVarsOfCos   :: [Coercion] -> CoVarSet
    
    688 467
     
    
    689
    -coVarsOfType  ty  = runTyCoVars (deep_cv_ty ty)
    
    690
    -coVarsOfTypes tys = runTyCoVars (deep_cv_tys tys)
    
    691
    -coVarsOfCo    co  = runTyCoVars (deep_cv_co co)
    
    692
    -coVarsOfCos   cos = runTyCoVars (deep_cv_cos cos)
    
    468
    +coVarsOfType  ty  = runTyCoVars (deepCoVarTypeFV ty)
    
    469
    +coVarsOfTypes tys = runTyCoVars (deepCoVarTypesFV tys)
    
    470
    +coVarsOfCo    co  = runTyCoVars (deepCoVarCoFV co)
    
    471
    +coVarsOfCos   cos = runTyCoVars (deepCoVarCosFV cos)
    
    693 472
     
    
    694
    -type CoVarFVRes  = FVRes BoundVars (EndoOS CoVarSet)
    
    473
    +type CoVarFV  = FV BoundVars (EndoOS CoVarSet)
    
    695 474
     
    
    696
    -deep_cv_ty  :: Type       -> CoVarFVRes
    
    697
    -deep_cv_tys :: [Type]     -> CoVarFVRes
    
    698
    -deep_cv_co  :: Coercion   -> CoVarFVRes
    
    699
    -deep_cv_cos :: [Coercion] -> CoVarFVRes
    
    700
    -(deep_cv_ty, deep_cv_tys, deep_cv_co, deep_cv_cos) = foldTyCo deepCoVarFolder
    
    475
    +deepCoVarTypeFV  :: Type       -> CoVarFV
    
    476
    +deepCoVarTypesFV :: [Type]     -> CoVarFV
    
    477
    +deepCoVarCoFV  :: Coercion   -> CoVarFV
    
    478
    +deepCoVarCosFV :: [Coercion] -> CoVarFV
    
    479
    +(deepCoVarTypeFV, deepCoVarTypesFV, deepCoVarCoFV, deepCoVarCosFV) = foldTyCo deepCoVarFolder
    
    701 480
     
    
    702
    -deepCoVarFolder :: TyCoFolder CoVarFVRes
    
    481
    +deepCoVarFolder :: TyCoFolder CoVarFV
    
    703 482
     deepCoVarFolder = TyCoFolder { tcf_view = noView
    
    704 483
                                  , tcf_tyvar = do_tyvar, tcf_covar = do_covar
    
    705 484
                                  , tcf_hole  = do_hole
    
    706
    -                             , tcf_tycobinder = addBndrFVRes }
    
    485
    +                             , tcf_tycobinder = addBndrFV }
    
    707 486
       where
    
    708 487
         do_tyvar _  = mempty
    
    709 488
           -- This do_tyvar means we won't see any CoVars in this
    
    ... ... @@ -712,14 +491,14 @@ deepCoVarFolder = TyCoFolder { tcf_view = noView
    712 491
           -- the tyvar won't end up in the accumulator, so
    
    713 492
           -- we'd look repeatedly.  Blargh.
    
    714 493
     
    
    715
    -    do_covar = deepUnitFVRes deep_cv_ty
    
    494
    +    do_covar = deepUnitFV deepCoVarTypeFV
    
    716 495
     
    
    717 496
         do_hole hole  = do_covar (coHoleCoVar hole)
    
    718 497
           -- We /do/ treat a CoercionHole as a free variable
    
    719 498
           -- See Note [CoercionHoles and coercion free variables]
    
    720 499
     
    
    721 500
     -------------- Deterministic versions ------------------
    
    722
    -type DCoVarFVRes  = FVRes BoundVars (EndoOS DCoVarSet)
    
    501
    +type DCoVarFV  = FV BoundVars (EndoOS DCoVarSet)
    
    723 502
     
    
    724 503
     coVarsOfCoDSet :: Coercion -> DCoVarSet
    
    725 504
     coVarsOfCoDSet co = runTyCoVarsDSet (det_co co)
    
    ... ... @@ -727,23 +506,23 @@ coVarsOfCoDSet co = runTyCoVarsDSet (det_co co)
    727 506
     coVarsOfCosDSet :: [Coercion] -> DCoVarSet
    
    728 507
     coVarsOfCosDSet cos = runTyCoVarsDSet (det_cos cos)
    
    729 508
     
    
    730
    -det_ty  :: Type       -> DCoVarFVRes
    
    731
    -det_co  :: Coercion   -> DCoVarFVRes
    
    732
    -det_cos :: [Coercion] -> DCoVarFVRes
    
    509
    +det_ty  :: Type       -> DCoVarFV
    
    510
    +det_co  :: Coercion   -> DCoVarFV
    
    511
    +det_cos :: [Coercion] -> DCoVarFV
    
    733 512
     (det_ty, _, det_co, det_cos) = foldTyCo deepDetCoVarFolder
    
    734 513
     
    
    735
    -deepDetCoVarFolder :: TyCoFolder DCoVarFVRes
    
    514
    +deepDetCoVarFolder :: TyCoFolder DCoVarFV
    
    736 515
     -- Follows deepCoVarFolders, but returns a /deterministic/ set
    
    737 516
     deepDetCoVarFolder = TyCoFolder { tcf_view = noView
    
    738 517
                                     , tcf_tyvar = do_tyvar
    
    739 518
                                     , tcf_covar = do_covar
    
    740 519
                                     , tcf_hole  = do_hole
    
    741
    -                                , tcf_tycobinder = addBndrFVRes }
    
    520
    +                                , tcf_tycobinder = addBndrFV }
    
    742 521
       where
    
    743 522
         do_tyvar _  = mempty
    
    744 523
     
    
    745
    -    do_covar :: CoVar -> DCoVarFVRes
    
    746
    -    do_covar = deepDetUnitFVRes det_ty
    
    524
    +    do_covar :: CoVar -> DCoVarFV
    
    525
    +    do_covar = deepDetUnitFV det_ty
    
    747 526
     
    
    748 527
         do_hole hole  = do_covar (coHoleCoVar hole)
    
    749 528
     
    
    ... ... @@ -768,7 +547,7 @@ closeOverKinds vs = nonDetStrictFoldVarSet do_one vs vs
    768 547
     closeOverKindsDSet :: DTyVarSet -> DTyVarSet
    
    769 548
     closeOverKindsDSet vs = nonDetStrictFoldDVarSet do_one vs vs
    
    770 549
       where
    
    771
    -    do_one v = runFVAcc (detTyCoVarsOfType (varType v))
    
    550
    +    do_one v = runFVAcc (deepDetTypeFV (varType v))
    
    772 551
     
    
    773 552
     {- --------------- Alternative version 1 (using FV) ------------
    
    774 553
     closeOverKinds = fvVarSet . closeOverKindsFV . nonDetEltsUniqSet
    
    ... ... @@ -1033,18 +812,18 @@ injectiveVarsOfTypes :: Bool -- ^ look under injective type families?
    1033 812
                                  -- in "GHC.Tc.Instance.Family".
    
    1034 813
                          -> [Type] -> VarSet
    
    1035 814
     injectiveVarsOfTypes look_under_tfs tys
    
    1036
    -  = runTyCoVars $ mapUnionFVRes (inj_vars_of_type look_under_tfs) tys
    
    815
    +  = runTyCoVars $ mapUnionFV (inj_vars_of_type look_under_tfs) tys
    
    1037 816
     
    
    1038
    -inj_vars_of_type :: Bool -> Type -> TyCoFVRes
    
    817
    +inj_vars_of_type :: Bool -> Type -> TyCoFV
    
    1039 818
     inj_vars_of_type look_under_tfs = go
    
    1040 819
       where
    
    1041 820
         go ty | Just ty' <- rewriterView ty = go ty'
    
    1042
    -    go (TyVarTy v)                      = deepUnitFVRes go v
    
    821
    +    go (TyVarTy v)                      = deepUnitFV go v
    
    1043 822
         go (AppTy f a)                      = go f `mappend` go a
    
    1044 823
         go (FunTy _ w ty1 ty2)              = go w `mappend` go ty1 `mappend` go ty2
    
    1045 824
         go (TyConApp tc tys)                = go_tc tc tys
    
    1046 825
         go (ForAllTy (Bndr tv _) ty)        = go (tyVarKind tv) `mappend`
    
    1047
    -                                          addBndrFVRes tv (go ty)
    
    826
    +                                          addBndrFV tv (go ty)
    
    1048 827
         go LitTy{}                          = mempty
    
    1049 828
         go (CastTy ty _)                    = go ty
    
    1050 829
         go CoercionTy{}                     = mempty
    
    ... ... @@ -1053,7 +832,7 @@ inj_vars_of_type look_under_tfs = go
    1053 832
           | isTypeFamilyTyCon tc
    
    1054 833
           = if | look_under_tfs
    
    1055 834
                , Injective flags <- tyConInjectivityInfo tc
    
    1056
    -           -> mapUnionFVRes go $
    
    835
    +           -> mapUnionFV go $
    
    1057 836
                   filterByList (flags ++ repeat True) tys
    
    1058 837
                              -- Oversaturated arguments to a tycon are
    
    1059 838
                              -- always injective, hence the repeat True
    
    ... ... @@ -1061,7 +840,7 @@ inj_vars_of_type look_under_tfs = go
    1061 840
                -> mempty
    
    1062 841
     
    
    1063 842
           | otherwise        -- Data type, injective in all positions
    
    1064
    -      = mapUnionFVRes go tys
    
    843
    +      = mapUnionFV go tys
    
    1065 844
     
    
    1066 845
     
    
    1067 846
     
    
    ... ... @@ -1110,15 +889,15 @@ invisibleVarsOfTypes = foldr (unionVarSet . invisibleVarsOfType) emptyVarSet
    1110 889
     ********************************************************************* -}
    
    1111 890
     
    
    1112 891
     {-# INLINE afvFolder #-}   -- so that specialization to (const True) works
    
    1113
    -afvFolder :: (TyCoVar -> Bool) -> TyCoFolder (FVRes TyCoVarSet DM.Any)
    
    892
    +afvFolder :: (TyCoVar -> Bool) -> TyCoFolder (FV TyCoVarSet DM.Any)
    
    1114 893
     -- 'afvFolder' is short for "any-free-var folder", good for checking
    
    1115 894
     -- if any free var of a type satisfies a predicate `check_fv`
    
    1116 895
     afvFolder check_fv = TyCoFolder { tcf_view = noView  -- See Note [Free vars and synonyms]
    
    1117 896
                                     , tcf_tyvar = do_tcv, tcf_covar = do_tcv
    
    1118 897
                                     , tcf_hole = do_hole
    
    1119
    -                                , tcf_tycobinder = addBndrFVRes }
    
    898
    +                                , tcf_tycobinder = addBndrFV }
    
    1120 899
       where
    
    1121
    -    do_tcv tv = FVRes $ \ bvs ->
    
    900
    +    do_tcv tv = MkFV $ \ bvs ->
    
    1122 901
                     Any (not (tv `elemVarSet` bvs) && check_fv tv)
    
    1123 902
         do_hole _ = mempty    -- I'm unsure; probably never happens
    
    1124 903
     
    

  • compiler/GHC/Core/Type.hs
    ... ... @@ -159,11 +159,8 @@ module GHC.Core.Type (
    159 159
             liftedTypeKind, unliftedTypeKind,
    
    160 160
     
    
    161 161
             -- * Type free variables
    
    162
    -        tyCoFVsOfType,
    
    163
    -        tyCoVarsOfType, tyCoVarsOfTypes,
    
    164
    -        tyCoVarsOfTypeDSet,
    
    165
    -        coVarsOfType,
    
    166
    -        coVarsOfTypes,
    
    162
    +        tyCoVarsOfType, tyCoVarsOfTypes, tyCoVarsOfTypeDSet,
    
    163
    +        coVarsOfType, coVarsOfTypes,
    
    167 164
     
    
    168 165
             anyFreeVarsOfType, anyFreeVarsOfTypes,
    
    169 166
             noFreeVarsOfType,
    

  • compiler/GHC/Iface/Ext/Ast.hs
    ... ... @@ -23,7 +23,6 @@ import GHC.Core.DataCon ( dataConWrapperType )
    23 23
     import GHC.Core.Type              ( Type, ForAllTyFlag(..) )
    
    24 24
     import GHC.Core.TyCon             ( TyCon, tyConClass_maybe )
    
    25 25
     import GHC.Core.InstEnv
    
    26
    -import GHC.Core.TyCo.FVs
    
    27 26
     import GHC.Core.Predicate         ( isEvId )
    
    28 27
     
    
    29 28
     import GHC.Hs
    
    ... ... @@ -40,6 +39,7 @@ import GHC.Types.Name.Reader ( RecFieldInfo(..), WithUserRdr(..) )
    40 39
     import GHC.Types.SrcLoc
    
    41 40
     import GHC.Types.Var              ( Id, Var, EvId, varName, varType, varUnique )
    
    42 41
     import GHC.Types.Var.Env
    
    42
    +import GHC.Types.Var.FV
    
    43 43
     
    
    44 44
     import GHC.Tc.Types
    
    45 45
     import GHC.Tc.Types.Evidence
    

  • compiler/GHC/Tc/Gen/App.hs
    ... ... @@ -37,7 +37,6 @@ import GHC.Core.DataCon ( dataConConcreteTyVars, isNewDataCon, dataConOrigArgTys
    37 37
     import GHC.Core.TyCon
    
    38 38
     import GHC.Core.TyCo.Rep
    
    39 39
     import GHC.Core.TyCo.Ppr
    
    40
    -import GHC.Core.TyCo.FVs
    
    41 40
     import GHC.Core.TyCo.Subst ( substTyWithInScope )
    
    42 41
     import GHC.Core.Type
    
    43 42
     import GHC.Core.Coercion
    
    ... ... @@ -47,6 +46,7 @@ import GHC.Builtin.PrimOps( tagToEnumKey )
    47 46
     import GHC.Builtin.Names
    
    48 47
     
    
    49 48
     import GHC.Types.Var
    
    49
    +import GHC.Types.Var.FV
    
    50 50
     import GHC.Types.Name
    
    51 51
     import GHC.Types.Name.Env
    
    52 52
     import GHC.Types.Name.Reader
    
    ... ... @@ -2082,7 +2082,7 @@ foldQLInstVars check_tv ty
    2082 2082
       where
    
    2083 2083
         (do_ty, _, _, _) = foldTyCo folder
    
    2084 2084
     
    
    2085
    -    folder :: TyCoFolder (FVRes () a)
    
    2085
    +    folder :: TyCoFolder (FV () a)
    
    2086 2086
         folder = TyCoFolder { tcf_view = noView  -- See Note [Free vars and synonyms]
    
    2087 2087
                                                  -- in GHC.Core.TyCo.FVs
    
    2088 2088
                             , tcf_tyvar = do_tv, tcf_covar = mempty
    
    ... ... @@ -2093,8 +2093,8 @@ foldQLInstVars check_tv ty
    2093 2093
         do_hole hole = do_ty (coVarKind (coHoleCoVar hole))
    
    2094 2094
                          -- See (MIV2) in Note [Monomorphise instantiation variables]
    
    2095 2095
     
    
    2096
    -    do_tv :: TcTyVar -> FVRes () a
    
    2097
    -    do_tv tv | isQLInstTyVar tv = FVRes $ \_ -> check_tv tv
    
    2096
    +    do_tv :: TcTyVar -> FV () a
    
    2097
    +    do_tv tv | isQLInstTyVar tv = MkFV $ \_ -> check_tv tv
    
    2098 2098
                  | otherwise        = mempty
    
    2099 2099
     
    
    2100 2100
     {- *********************************************************************
    

  • compiler/GHC/Tc/Types/Constraint.hs
    ... ... @@ -117,6 +117,7 @@ import GHC.Core.TyCo.FVs
    117 117
     
    
    118 118
     import GHC.Types.Name
    
    119 119
     import GHC.Types.Var
    
    120
    +import GHC.Types.Var.FV
    
    120 121
     
    
    121 122
     import GHC.Tc.Utils.TcType
    
    122 123
     import GHC.Tc.Types.Evidence
    
    ... ... @@ -809,7 +810,7 @@ tyCoVarsOfCtList :: Ct -> [TcTyCoVar]
    809 810
     tyCoVarsOfCtList = tyCoVarsOfTypeList . ctPred
    
    810 811
     
    
    811 812
     -- | Returns free variables of constraints as a deterministically ordered
    
    812
    --- list. See Note [Deterministic FV] in GHC.Utils.FV.
    
    813
    +-- list. See Note [Deterministic FV] in GHC.Types.Var.FV.
    
    813 814
     tyCoVarsOfCtEvList :: CtEvidence -> [TcTyCoVar]
    
    814 815
     tyCoVarsOfCtEvList = tyCoVarsOfTypeList . ctEvPred
    
    815 816
     
    
    ... ... @@ -819,49 +820,49 @@ tyCoVarsOfCts :: Cts -> TcTyCoVarSet
    819 820
     tyCoVarsOfCts = dVarSetToVarSet . runTyCoVarsDSet . tcvs_of_cts
    
    820 821
     
    
    821 822
     -- | Returns free variables of a bag of constraints as a deterministically
    
    822
    --- ordered list. See Note [Deterministic FV] in "GHC.Utils.FV".
    
    823
    +-- ordered list. See Note [Deterministic FV] in "GHC.Types.Var.FV".
    
    823 824
     tyCoVarsOfCtsList :: Cts -> [TcTyCoVar]
    
    824 825
     tyCoVarsOfCtsList = dVarSetElems . tyCoVarsOfThingsDSet ctPred
    
    825 826
     
    
    826 827
     -- | Returns free variables of a bag of constraints as a deterministically
    
    827
    --- ordered list. See Note [Deterministic FV] in GHC.Utils.FV.
    
    828
    +-- ordered list. See Note [Deterministic FV] in GHC.Types.Var.FV.
    
    828 829
     tyCoVarsOfCtEvsList :: [CtEvidence] -> [TcTyCoVar]
    
    829 830
     tyCoVarsOfCtEvsList = dVarSetElems . runTyCoVarsDSet
    
    830
    -                      . mapUnionFVRes (detTyCoVarsOfType . ctEvPred)
    
    831
    +                      . mapUnionFV (deepDetTypeFV . ctEvPred)
    
    831 832
     
    
    832 833
     -- | Returns free variables of WantedConstraints as a non-deterministic
    
    833
    --- set. See Note [Deterministic FV] in "GHC.Utils.FV".
    
    834
    +-- set. See Note [Deterministic FV] in "GHC.Types.Var.FV".
    
    834 835
     tyCoVarsOfWC :: WantedConstraints -> TyCoVarSet
    
    835 836
     -- Only called on *zonked* things
    
    836 837
     tyCoVarsOfWC = dVarSetToVarSet . tyCoVarsOfWcDSet
    
    837 838
     
    
    838 839
     -- | Returns free variables of WantedConstraints as a deterministically
    
    839
    --- ordered list. See Note [Deterministic FV] in "GHC.Utils.FV".
    
    840
    +-- ordered list. See Note [Deterministic FV] in "GHC.Types.Var.FV".
    
    840 841
     tyCoVarsOfWCList :: WantedConstraints -> [TyCoVar]
    
    841 842
     -- Only called on *zonked* things
    
    842 843
     tyCoVarsOfWCList = dVarSetElems . tyCoVarsOfWcDSet
    
    843 844
     
    
    844 845
     -- | Returns free variables of WantedConstraints as a composable FV
    
    845
    --- computation. See Note [Deterministic FV] in "GHC.Utils.FV".
    
    846
    +-- computation. See Note [Deterministic FV] in "GHC.Types.Var.FV".
    
    846 847
     tyCoVarsOfWcDSet :: WantedConstraints -> DTyCoVarSet
    
    847 848
     -- Only called on *zonked* things
    
    848 849
     tyCoVarsOfWcDSet = runTyCoVarsDSet . tcvs_of_wc
    
    849 850
     
    
    850
    -tcvs_of_wc :: WantedConstraints -> DTyCoFVRes
    
    851
    +tcvs_of_wc :: WantedConstraints -> DTyCoFV
    
    851 852
     tcvs_of_wc (WC { wc_simple = simple, wc_impl = implics, wc_errors = errors })
    
    852 853
       = tcvs_of_cts simple               `mappend`
    
    853
    -    mapUnionFVRes tcvs_of_implic implics `mappend`
    
    854
    -    mapUnionFVRes tcvs_of_errs errors
    
    854
    +    mapUnionFV tcvs_of_implic implics `mappend`
    
    855
    +    mapUnionFV tcvs_of_errs errors
    
    855 856
     
    
    856
    -tcvs_of_cts :: Cts -> DTyCoFVRes
    
    857
    -tcvs_of_cts = mapUnionFVRes tcvs_of_ct
    
    857
    +tcvs_of_cts :: Cts -> DTyCoFV
    
    858
    +tcvs_of_cts = mapUnionFV tcvs_of_ct
    
    858 859
     
    
    859
    -tcvs_of_ct :: Ct -> DTyCoFVRes
    
    860
    -tcvs_of_ct ct = detTyCoVarsOfType (ctPred ct)
    
    860
    +tcvs_of_ct :: Ct -> DTyCoFV
    
    861
    +tcvs_of_ct ct = deepDetTypeFV (ctPred ct)
    
    861 862
     
    
    862 863
     -- | Returns free variables of Implication as a composable FV computation.
    
    863
    --- See Note [Deterministic FV] in "GHC.Utils.FV".
    
    864
    -tcvs_of_implic :: Implication -> DTyCoFVRes
    
    864
    +-- See Note [Deterministic FV] in "GHC.Types.Var.FV".
    
    865
    +tcvs_of_implic :: Implication -> DTyCoFV
    
    865 866
     -- Only called on *zonked* things
    
    866 867
     tcvs_of_implic (Implic { ic_skols = skols
    
    867 868
                            , ic_given = givens
    
    ... ... @@ -869,17 +870,17 @@ tcvs_of_implic (Implic { ic_skols = skols
    869 870
       | isEmptyWC wanted
    
    870 871
       = mempty
    
    871 872
       | otherwise
    
    872
    -  = addBndrsFVRes skols  $
    
    873
    -    addBndrsFVRes givens $
    
    873
    +  = addBndrsFV skols  $
    
    874
    +    addBndrsFV givens $
    
    874 875
         tcvs_of_wc wanted
    
    875 876
     
    
    876
    -tcvs_of_errs :: DelayedError -> DTyCoFVRes
    
    877
    +tcvs_of_errs :: DelayedError -> DTyCoFV
    
    877 878
     tcvs_of_errs (DE_Hole hole)         = tcvs_of_hole hole
    
    878 879
     tcvs_of_errs (DE_NotConcrete {})    = mempty
    
    879
    -tcvs_of_errs (DE_Multiplicity co _) = detTyCoVarsOfCo co
    
    880
    +tcvs_of_errs (DE_Multiplicity co _) = deepDetCoFV co
    
    880 881
     
    
    881
    -tcvs_of_hole :: Hole -> DTyCoFVRes
    
    882
    -tcvs_of_hole (Hole { hole_ty = ty }) = detTyCoVarsOfType ty
    
    882
    +tcvs_of_hole :: Hole -> DTyCoFV
    
    883
    +tcvs_of_hole (Hole { hole_ty = ty }) = deepDetTypeFV ty
    
    883 884
     
    
    884 885
     {-
    
    885 886
     ************************************************************************
    

  • compiler/GHC/Tc/Types/Evidence.hs
    ... ... @@ -64,7 +64,6 @@ import GHC.Core.Ppr () -- Instance OutputableBndr TyVar
    64 64
     import GHC.Core.Predicate
    
    65 65
     import GHC.Core.Type
    
    66 66
     import GHC.Core.TyCo.Rep (UnivCoProvenance(..))
    
    67
    -import GHC.Core.TyCo.FVs
    
    68 67
     import GHC.Core.TyCon
    
    69 68
     import GHC.Core.Make    ( mkWildCase, mkRuntimeErrorApp, tYPE_ERROR_ID )
    
    70 69
     import GHC.Core.Class   ( classTyCon )
    
    ... ... @@ -80,6 +79,7 @@ import GHC.Types.Var
    80 79
     import GHC.Types.Id( idType )
    
    81 80
     import GHC.Types.Var.Env
    
    82 81
     import GHC.Types.Var.Set
    
    82
    +import GHC.Types.Var.FV
    
    83 83
     import GHC.Types.Basic
    
    84 84
     
    
    85 85
     import GHC.Builtin.Names
    
    ... ... @@ -1318,25 +1318,25 @@ nestedEvIdsOfTerm :: EvTerm -> VarSet
    1318 1318
     -- Returns only EvIds satisfying relevantEvId
    
    1319 1319
     nestedEvIdsOfTerm = runFVSelectiveSet isNestedEvId . evTermFVs
    
    1320 1320
     
    
    1321
    -evTermFVs :: EvTerm -> SelectiveFVRes
    
    1321
    +evTermFVs :: EvTerm -> SelectiveFV
    
    1322 1322
     evTermFVs (EvExpr e)         = exprFVs e
    
    1323 1323
     evTermFVs (EvTypeable _ ev)  = evFVsOfTypeable ev
    
    1324 1324
     evTermFVs (EvFun { et_tvs = tvs, et_given = given
    
    1325 1325
                      , et_binds = tc_ev_binds, et_body = v })
    
    1326 1326
       = case tc_ev_binds of
    
    1327 1327
           TcEvBinds {}  -> mempty  -- See Note [Free vars of EvFun]
    
    1328
    -      EvBinds binds -> addBndrsFV bndrs fvs
    
    1328
    +      EvBinds binds -> addBndrsSelectiveFV bndrs fvs
    
    1329 1329
             where
    
    1330 1330
               fvs = foldr (mappend . evTermFVs . eb_rhs) (unitFV v) binds
    
    1331 1331
               bndrs = foldr ((:) . eb_lhs) (tvs ++ given) binds
    
    1332 1332
     
    
    1333
    -evTermFVss :: [EvTerm] -> SelectiveFVRes
    
    1334
    -evTermFVss = mapUnionFVRes evTermFVs
    
    1333
    +evTermFVss :: [EvTerm] -> SelectiveFV
    
    1334
    +evTermFVss = mapUnionFV evTermFVs
    
    1335 1335
     
    
    1336
    -evFVsOfTypeable :: EvTypeable -> SelectiveFVRes
    
    1336
    +evFVsOfTypeable :: EvTypeable -> SelectiveFV
    
    1337 1337
     evFVsOfTypeable ev =
    
    1338 1338
       case ev of
    
    1339
    -    EvTypeableTyCon _ e      -> mapUnionFVRes evTermFVs e
    
    1339
    +    EvTypeableTyCon _ e      -> mapUnionFV evTermFVs e
    
    1340 1340
         EvTypeableTyApp e1 e2    -> evTermFVss [e1,e2]
    
    1341 1341
         EvTypeableTrFun em e1 e2 -> evTermFVss [em,e1,e2]
    
    1342 1342
         EvTypeableTyLit e        -> evTermFVs e
    

  • compiler/GHC/Tc/Utils/TcMType.hs
    ... ... @@ -1565,7 +1565,7 @@ against any specification -- just suboptimal and confounding to users.
    1565 1565
     
    
    1566 1566
     Note [Recurring into kinds for candidateQTyVars]
    
    1567 1567
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    1568
    -First, read Note [Closing over free variable kinds] in GHC.Core.TyCo.FVs, paying
    
    1568
    +First, read Note [Computing deep free variables] in GHC.Core.TyCo.FVs, paying
    
    1569 1569
     attention to the end of the Note about using an empty bound set when
    
    1570 1570
     traversing a variable's kind.
    
    1571 1571
     
    
    ... ... @@ -1582,7 +1582,7 @@ type inference, which is seeded by the renamer and its insistence to
    1582 1582
     use different Uniques for different variables. (In contrast, the Core
    
    1583 1583
     functions work on the output of optimizations, which may introduce
    
    1584 1584
     shadowing.) Without shadowing, the problem studied by
    
    1585
    -Note [Closing over free variable kinds] in GHC.Core.TyCo.FVs cannot happen.
    
    1585
    +Note [Computing deep free variables] in GHC.Core.TyCo.FVs cannot happen.
    
    1586 1586
     
    
    1587 1587
     Why it is necessary:
    
    1588 1588
     Wiping the bound set would be just plain wrong here. Consider
    
    ... ... @@ -1593,7 +1593,7 @@ We really don't want to think k1 and k2 are free here. (It's true that we'll
    1593 1593
     never be able to fill in `hole`, but we don't want to go off the rails just
    
    1594 1594
     because we have an insoluble coercion hole.) So: why is it wrong to wipe
    
    1595 1595
     the bound variables here but right in Core? Because the final statement
    
    1596
    -in Note [Closing over free variable kinds] in GHC.Core.TyCo.FVs is wrong: not
    
    1596
    +in Note [Computing deep free variables] in GHC.Core.TyCo.FVs is wrong: not
    
    1597 1597
     every variable is either free or bound. A variable can be a hole, too!
    
    1598 1598
     The reasoning in that Note then breaks down.
    
    1599 1599
     
    

  • compiler/GHC/Tc/Utils/TcType.hs
    ... ... @@ -219,16 +219,17 @@ import {-# SOURCE #-} GHC.Tc.Types.Origin
    219 219
       ( SkolemInfo, unkSkol
    
    220 220
       , FixedRuntimeRepOrigin, FixedRuntimeRepContext )
    
    221 221
     
    
    222
    --- others:
    
    222
    +import GHC.Types.Var.FV
    
    223 223
     import GHC.Types.Name as Name
    
    224 224
                 -- We use this to make dictionaries for type literals.
    
    225 225
                 -- Perhaps there's a better way to do this?
    
    226 226
     import GHC.Types.Name.Env
    
    227 227
     import GHC.Types.Name.Set
    
    228
    +import GHC.Types.Basic
    
    229
    +
    
    228 230
     import GHC.Builtin.Names
    
    229 231
     import GHC.Builtin.Types ( coercibleClass, eqClass, heqClass, unitTyConKey
    
    230 232
                              , listTyCon, constraintKind )
    
    231
    -import GHC.Types.Basic
    
    232 233
     import GHC.Data.Maybe
    
    233 234
     import GHC.Data.List.SetOps ( getNth, findDupsEq )
    
    234 235
     
    
    ... ... @@ -1178,11 +1179,11 @@ exactTyCoVarsOfTypes :: [Type] -> TyCoVarSet
    1178 1179
     exactTyCoVarsOfType  ty  = runTyCoVars (exact_ty ty)
    
    1179 1180
     exactTyCoVarsOfTypes tys = runTyCoVars (exact_tys tys)
    
    1180 1181
     
    
    1181
    -exact_ty  :: Type       -> TyCoFVRes
    
    1182
    -exact_tys :: [Type]     -> TyCoFVRes
    
    1182
    +exact_ty  :: Type       -> TyCoFV
    
    1183
    +exact_tys :: [Type]     -> TyCoFV
    
    1183 1184
     (exact_ty, exact_tys, _, _) = foldTyCo exactTcvFolder
    
    1184 1185
     
    
    1185
    -exactTcvFolder :: TyCoFolder TyCoFVRes
    
    1186
    +exactTcvFolder :: TyCoFolder TyCoFV
    
    1186 1187
     exactTcvFolder = deepTcvFolder { tcf_view = coreView }
    
    1187 1188
                      -- This is the key line
    
    1188 1189
     
    

  • compiler/GHC/Tc/Utils/Unify.hs
    ... ... @@ -101,6 +101,7 @@ import GHC.Types.Id( idType )
    101 101
     import GHC.Types.Var as Var
    
    102 102
     import GHC.Types.Var.Set
    
    103 103
     import GHC.Types.Var.Env
    
    104
    +import GHC.Types.Var.FV
    
    104 105
     import GHC.Types.Basic
    
    105 106
     import GHC.Types.Unique.Set (nonDetEltsUniqSet)
    
    106 107
     
    
    ... ... @@ -3572,20 +3573,20 @@ mkOccFolders :: Name -> (TcType -> Bool, TcCoercion -> Bool)
    3572 3573
     mkOccFolders lhs_tv = ( getAny . runFVTop . check_ty
    
    3573 3574
                           , getAny . runFVTop . check_co)
    
    3574 3575
       where
    
    3575
    -    check_ty :: Type -> FVRes BoundVars Any
    
    3576
    +    check_ty :: Type -> FV BoundVars Any
    
    3576 3577
         !(check_ty, _, check_co, _) = foldTyCo occ_folder
    
    3577 3578
     
    
    3578
    -    occ_folder :: TyCoFolder (FVRes BoundVars Any)
    
    3579
    +    occ_folder :: TyCoFolder (FV BoundVars Any)
    
    3579 3580
         occ_folder = TyCoFolder { tcf_view  = noView  -- Don't expand synonyms
    
    3580 3581
                                 , tcf_tyvar = do_tcv, tcf_covar = do_tcv
    
    3581 3582
                                 , tcf_hole  = do_hole
    
    3582
    -                            , tcf_tycobinder = addBndrFVRes }
    
    3583
    +                            , tcf_tycobinder = addBndrFV }
    
    3583 3584
     
    
    3584
    -    do_tcv v = (FVRes $ \ bvs ->
    
    3585
    +    do_tcv v = (MkFV $ \ bvs ->
    
    3585 3586
                     Any (not (v `elemVarSet` bvs) && tyVarName v == lhs_tv))
    
    3586 3587
                    `mappend` check_ty (varType v)
    
    3587 3588
     
    
    3588
    -    do_hole _hole = FVRes $ \ _bvs -> DM.Any True  -- Reject coercion holes
    
    3589
    +    do_hole _hole = MkFV $ \ _bvs -> DM.Any True  -- Reject coercion holes
    
    3589 3590
     
    
    3590 3591
     {- *********************************************************************
    
    3591 3592
     *                                                                      *
    

  • compiler/GHC/Utils/EndoOS.hs
    1 1
     {-# LANGUAGE PatternSynonyms #-}
    
    2 2
     
    
    3 3
     -- | One-shot endomorphisms
    
    4
    ---   Mostly for backwards compatibility.
    
    5 4
     
    
    6 5
     -- One-shot endomorphisms
    
    7 6
     -- Like GHC.Internal.Data.Semigroup.Internal.Endo, but using
    
    8 7
     -- the one-shot trick from
    
    9 8
     --    Note [The one-shot state monad trick] in  GHC.Utils.Monad.
    
    9
    +--
    
    10
    +-- It is also strict: see the (<>) method in he Semigroup instance
    
    10 11
     
    
    11 12
     module GHC.Utils.EndoOS( EndoOS(EndoOS, runEndoOS ), foldEndoOS ) where
    
    12 13
     
    
    ... ... @@ -19,13 +20,21 @@ newtype EndoOS a = EndoOS' { runEndoOS :: a -> a }
    19 20
     
    
    20 21
     
    
    21 22
     instance Semigroup (EndoOS a) where
    
    22
    -  f <> g = EndoOS (\x -> runEndoOS f $! (runEndoOS g x))
    
    23
    -           -- Strict application, to avoid thunk creation
    
    23
    +  EndoOS' f <> EndoOS' g = EndoOS (\x -> g $! f x)
    
    24
    +     -- NB1: Strict application, to avoid thunk creation
    
    25
    +     --      See (FV3) in Note [Finding free variables]
    
    26
    +     --      in GHC.Types.Var.FV
    
    27
    +     -- NB2: We apply `f` to the acccumulator first, then `g`
    
    28
    +     --      So if we traverse a type left-to-right, the insertion
    
    29
    +     --      order for (say) free type variables is left-to-right
    
    30
    +     --      See (FV4) in Note [Finding free variables]
    
    31
    +     --      in GHC.Types.Var.FV
    
    24 32
     
    
    25 33
     instance Monoid (EndoOS a) where
    
    26 34
        mempty  = EndoOS id
    
    27 35
     
    
    28 36
     pattern EndoOS :: (a->a) -> EndoOS a
    
    37
    +{-# COMPLETE EndoOS #-}
    
    29 38
     pattern EndoOS f <- EndoOS' f
    
    30 39
           where
    
    31 40
             EndoOS f = EndoOS' (oneShot f)
    

  • compiler/GHC/Utils/FV.hs deleted
    1
    -{-
    
    2
    -(c) Bartosz Nitka, Facebook 2015
    
    3
    -
    
    4
    --}
    
    5
    -
    
    6
    --- | Utilities for efficiently and deterministically computing free variables.
    
    7
    -module GHC.Utils.FV (
    
    8
    -        -- * Deterministic free vars computations
    
    9
    -        FV, InterestingVarFun,
    
    10
    -
    
    11
    -        -- * Running the computations
    
    12
    -        fvVarList, fvVarSet, fvDVarSet,
    
    13
    -
    
    14
    -        -- ** Manipulating those computations
    
    15
    -        unitFV,
    
    16
    -        emptyFV,
    
    17
    -        mkFVs,
    
    18
    -        unionFV,
    
    19
    -        unionsFV,
    
    20
    -        delFV,
    
    21
    -        delFVs,
    
    22
    -        filterFV,
    
    23
    -        mapUnionFV,
    
    24
    -        fvDVarSetSome,
    
    25
    -    ) where
    
    26
    -
    
    27
    -import GHC.Prelude
    
    28
    -
    
    29
    -import GHC.Types.Var
    
    30
    -import GHC.Types.Var.Set
    
    31
    -
    
    32
    --- | Predicate on possible free variables: returns @True@ iff the variable is
    
    33
    --- interesting
    
    34
    -type InterestingVarFun = Var -> Bool
    
    35
    -
    
    36
    --- Note [Deterministic FV]
    
    37
    --- ~~~~~~~~~~~~~~~~~~~~~~~
    
    38
    --- When computing free variables, the order in which you get them affects
    
    39
    --- the results of floating and specialization. If you use UniqFM to collect
    
    40
    --- them and then turn that into a list, you get them in nondeterministic
    
    41
    --- order as described in Note [Deterministic UniqFM] in GHC.Types.Unique.DFM.
    
    42
    -
    
    43
    --- A naive algorithm for free variables relies on merging sets of variables.
    
    44
    --- Merging costs O(n+m) for UniqFM and for UniqDFM there's an additional log
    
    45
    --- factor. It's cheaper to incrementally add to a list and use a set to check
    
    46
    --- for duplicates.
    
    47
    -type FV = InterestingVarFun -- Used for filtering sets as we build them
    
    48
    -        -> VarSet           -- Locally bound variables
    
    49
    -        -> VarAcc           -- Accumulator
    
    50
    -        -> VarAcc
    
    51
    -
    
    52
    -type VarAcc = ([Var], VarSet)  -- List to preserve ordering and set to check for membership,
    
    53
    -                               -- so that the list doesn't have duplicates
    
    54
    -                               -- For explanation of why using `VarSet` is not deterministic see
    
    55
    -                               -- Note [Deterministic UniqFM] in GHC.Types.Unique.DFM.
    
    56
    -
    
    57
    --- Note [FV naming conventions]
    
    58
    --- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    59
    --- To get the performance and determinism that FV provides, FV computations
    
    60
    --- need to built up from smaller FV computations and then evaluated with
    
    61
    --- one of `fvVarList`, `fvDVarSet` That means the functions
    
    62
    --- returning FV need to be exported.
    
    63
    ---
    
    64
    --- The conventions are:
    
    65
    ---
    
    66
    --- a) non-deterministic functions:
    
    67
    ---   * a function that returns VarSet
    
    68
    ---       e.g. `tyVarsOfType`
    
    69
    --- b) deterministic functions:
    
    70
    ---   * a worker that returns FV
    
    71
    ---       e.g. `tyFVsOfType`
    
    72
    ---   * a function that returns [Var]
    
    73
    ---       e.g. `tyVarsOfTypeList`
    
    74
    ---   * a function that returns DVarSet
    
    75
    ---       e.g. `tyVarsOfTypeDSet`
    
    76
    ---
    
    77
    --- Where tyVarsOfType, tyVarsOfTypeList, tyVarsOfTypeDSet are implemented
    
    78
    --- in terms of the worker evaluated with fvVarSet, fvVarList, fvDVarSet
    
    79
    --- respectively.
    
    80
    -
    
    81
    --- | Run a free variable computation, returning a list of distinct free
    
    82
    --- variables in deterministic order and a non-deterministic set containing
    
    83
    --- those variables.
    
    84
    -fvVarAcc :: FV ->  ([Var], VarSet)
    
    85
    -fvVarAcc fv = fv (const True) emptyVarSet ([], emptyVarSet)
    
    86
    -
    
    87
    --- | Run a free variable computation, returning a list of distinct free
    
    88
    --- variables in deterministic order.
    
    89
    -fvVarList :: FV -> [Var]
    
    90
    -fvVarList = fst . fvVarAcc
    
    91
    -
    
    92
    --- | Run a free variable computation, returning a deterministic set of free
    
    93
    --- variables. Note that this is just a wrapper around the version that
    
    94
    --- returns a deterministic list. If you need a list you should use
    
    95
    --- `fvVarList`.
    
    96
    -fvDVarSet :: FV -> DVarSet
    
    97
    -fvDVarSet = mkDVarSet . fvVarList
    
    98
    -
    
    99
    --- | Run a free variable computation, returning a non-deterministic set of
    
    100
    --- free variables. Don't use if the set will be later converted to a list
    
    101
    --- and the order of that list will impact the generated code.
    
    102
    -fvVarSet :: FV -> VarSet
    
    103
    -fvVarSet = snd . fvVarAcc
    
    104
    -
    
    105
    --- Note [FV eta expansion]
    
    106
    --- ~~~~~~~~~~~~~~~~~~~~~~~
    
    107
    --- Let's consider an eta-reduced implementation of freeVarsOf using FV:
    
    108
    ---
    
    109
    --- freeVarsOf (App a b) = freeVarsOf a `unionFV` freeVarsOf b
    
    110
    ---
    
    111
    --- If GHC doesn't eta-expand it, after inlining unionFV we end up with
    
    112
    ---
    
    113
    --- freeVarsOf = \x ->
    
    114
    ---   case x of
    
    115
    ---     App a b -> \fv_cand in_scope acc ->
    
    116
    ---       freeVarsOf a fv_cand in_scope $! freeVarsOf b fv_cand in_scope $! acc
    
    117
    ---
    
    118
    --- which has to create a thunk, resulting in more allocations.
    
    119
    ---
    
    120
    --- On the other hand if it is eta-expanded:
    
    121
    ---
    
    122
    --- freeVarsOf (App a b) fv_cand in_scope acc =
    
    123
    ---   (freeVarsOf a `unionFV` freeVarsOf b) fv_cand in_scope acc
    
    124
    ---
    
    125
    --- after inlining unionFV we have:
    
    126
    ---
    
    127
    --- freeVarsOf = \x fv_cand in_scope acc ->
    
    128
    ---   case x of
    
    129
    ---     App a b ->
    
    130
    ---       freeVarsOf a fv_cand in_scope $! freeVarsOf b fv_cand in_scope $! acc
    
    131
    ---
    
    132
    --- which saves allocations.
    
    133
    ---
    
    134
    --- GHC when presented with knowledge about all the call sites, correctly
    
    135
    --- eta-expands in this case. Unfortunately due to the fact that freeVarsOf gets
    
    136
    --- exported to be composed with other functions, GHC doesn't have that
    
    137
    --- information and has to be more conservative here.
    
    138
    ---
    
    139
    --- Hence functions that get exported and return FV need to be manually
    
    140
    --- eta-expanded. See also #11146.
    
    141
    -
    
    142
    --- | Add a variable - when free, to the returned free variables.
    
    143
    --- Ignores duplicates and respects the filtering function.
    
    144
    -unitFV :: Id -> FV
    
    145
    -unitFV var fv_cand in_scope acc@(have, haveSet)
    
    146
    -  | var `elemVarSet` in_scope = acc
    
    147
    -  | var `elemVarSet` haveSet = acc
    
    148
    -  | fv_cand var = (var:have, extendVarSet haveSet var)
    
    149
    -  | otherwise = acc
    
    150
    -{-# INLINE unitFV #-}
    
    151
    -
    
    152
    --- | Return no free variables.
    
    153
    -emptyFV :: FV
    
    154
    -emptyFV _ _ acc = acc
    
    155
    -{-# INLINE emptyFV #-}
    
    156
    -
    
    157
    --- | Union two free variable computations.
    
    158
    -unionFV :: FV -> FV -> FV
    
    159
    -unionFV fv1 fv2 fv_cand in_scope acc =
    
    160
    -  fv1 fv_cand in_scope $! fv2 fv_cand in_scope $! acc
    
    161
    -{-# INLINE unionFV #-}
    
    162
    -
    
    163
    --- | Mark the variable as not free by putting it in scope.
    
    164
    -delFV :: Var -> FV -> FV
    
    165
    -delFV var fv fv_cand !in_scope acc =
    
    166
    -  fv fv_cand (extendVarSet in_scope var) acc
    
    167
    -{-# INLINE delFV #-}
    
    168
    -
    
    169
    --- | Mark many free variables as not free.
    
    170
    -delFVs :: VarSet -> FV -> FV
    
    171
    -delFVs vars fv fv_cand !in_scope acc =
    
    172
    -  fv fv_cand (in_scope `unionVarSet` vars) acc
    
    173
    -{-# INLINE delFVs #-}
    
    174
    -
    
    175
    --- | Filter a free variable computation.
    
    176
    -filterFV :: InterestingVarFun -> FV -> FV
    
    177
    -filterFV fv_cand2 fv fv_cand1 in_scope acc =
    
    178
    -  fv (\v -> fv_cand1 v && fv_cand2 v) in_scope acc
    
    179
    -{-# INLINE filterFV #-}
    
    180
    -
    
    181
    --- | Map a free variable computation over a list and union the results.
    
    182
    -mapUnionFV :: (a -> FV) -> [a] -> FV
    
    183
    -mapUnionFV _f [] _fv_cand _in_scope acc = acc
    
    184
    -mapUnionFV f (a:as) fv_cand in_scope acc =
    
    185
    -  mapUnionFV f as fv_cand in_scope $! f a fv_cand in_scope $! acc
    
    186
    -{-# INLINABLE mapUnionFV #-}
    
    187
    -
    
    188
    --- | Union many free variable computations.
    
    189
    -unionsFV :: [FV] -> FV
    
    190
    -unionsFV fvs fv_cand in_scope acc = mapUnionFV id fvs fv_cand in_scope acc
    
    191
    -{-# INLINE unionsFV #-}
    
    192
    -
    
    193
    --- | Add multiple variables - when free, to the returned free variables.
    
    194
    --- Ignores duplicates and respects the filtering function.
    
    195
    -mkFVs :: [Var] -> FV
    
    196
    -mkFVs vars fv_cand in_scope acc =
    
    197
    -  mapUnionFV unitFV vars fv_cand in_scope acc
    
    198
    -{-# INLINE mkFVs #-}
    
    199
    -
    
    200
    -fvDVarSetSome :: InterestingVarFun -> FV -> DVarSet
    
    201
    -fvDVarSetSome interesting_var fv =
    
    202
    -  mkDVarSet $ fst $ fv interesting_var emptyVarSet ([], emptyVarSet)

  • compiler/ghc.cabal.in
    ... ... @@ -951,6 +951,7 @@ Library
    951 951
             GHC.Types.Var
    
    952 952
             GHC.Types.Var.Env
    
    953 953
             GHC.Types.Var.Set
    
    954
    +        GHC.Types.Var.FV
    
    954 955
             GHC.Unit
    
    955 956
             GHC.Unit.Env
    
    956 957
             GHC.Unit.External
    
    ... ... @@ -992,7 +993,6 @@ Library
    992 993
             GHC.Utils.EndoOS
    
    993 994
             GHC.Utils.Exception
    
    994 995
             GHC.Utils.Fingerprint
    
    995
    -        GHC.Utils.FV
    
    996 996
             GHC.Utils.GlobalVars
    
    997 997
             GHC.Utils.IO.Unsafe
    
    998 998
             GHC.Utils.Json
    

  • utils/haddock/haddock-api/src/Haddock/GhcUtils.hs
    ... ... @@ -43,7 +43,8 @@ import qualified Data.Set as Set
    43 43
     import GHC hiding (HsTypeGhcPsExt (..))
    
    44 44
     import GHC.Builtin.Types (liftedRepTy)
    
    45 45
     import GHC.Core.TyCo.Rep (Type (..))
    
    46
    -import GHC.Core.Type (binderVar, isRuntimeRepVar)
    
    46
    +import GHC.Core.TyCo.FVs ( deepDetTypesFV )
    
    47
    +import GHC.Core.Type (isRuntimeRepVar)
    
    47 48
     import GHC.Data.StringBuffer (StringBuffer)
    
    48 49
     import qualified GHC.Data.StringBuffer as S
    
    49 50
     import GHC.Driver.Session
    
    ... ... @@ -52,17 +53,11 @@ import GHC.Types.Name
    52 53
     import GHC.Types.SrcLoc (advanceSrcLoc)
    
    53 54
     import GHC.Types.SourceText (SourceText(..))
    
    54 55
     import GHC.Types.Var
    
    55
    -  ( Specificity
    
    56
    -  , TyVarBinder
    
    57
    -  , VarBndr (..)
    
    58
    -  , isInvisibleForAllTyFlag
    
    59
    -  , tyVarKind
    
    60
    -  , updateTyVarKind
    
    61
    -  )
    
    62
    -import GHC.Types.Var.Env (TyVarEnv, elemVarEnv, emptyVarEnv, extendVarEnv)
    
    63
    -import GHC.Types.Var.Set (VarSet, emptyVarSet)
    
    64
    -import GHC.Utils.FV as FV
    
    56
    +import GHC.Types.Var.Env
    
    57
    +import GHC.Types.Var.Set
    
    58
    +import GHC.Types.Var.FV
    
    65 59
     import GHC.Utils.Outputable (Outputable, SDocContext, ppr)
    
    60
    +import GHC.Utils.EndoOS
    
    66 61
     import qualified GHC.Utils.Outputable as Outputable
    
    67 62
     import GHC.Utils.Panic (panic)
    
    68 63
     
    
    ... ... @@ -86,6 +81,7 @@ filterSigNames p orig@(SpecSig _ n _ _) = ifTrueJust (p $ unLoc n) orig
    86 81
     filterSigNames p orig@(InlineSig _ n _) = ifTrueJust (p $ unLoc n) orig
    
    87 82
     filterSigNames p (FixSig _ (FixitySig ns_spec ns ty)) =
    
    88 83
       case filter (p . unLoc) ns of
    
    84
    +
    
    89 85
         [] -> Nothing
    
    90 86
         filtered -> Just (FixSig noAnn (FixitySig ns_spec filtered ty))
    
    91 87
     filterSigNames _ orig@(MinimalSig _ _) = Just orig
    
    ... ... @@ -817,66 +813,14 @@ isTypeHidden expInfo = typeHidden
    817 813
     -- | Get free type variables in a 'Type' in their order of appearance.
    
    818 814
     -- See [Ordering of implicit variables].
    
    819 815
     orderedFVs
    
    820
    -  :: VarSet
    
    821
    -  -- ^ free variables to ignore
    
    822
    -  -> [Type]
    
    823
    -  -- ^ types to traverse (in order) looking for free variables
    
    824
    -  -> [TyVar]
    
    825
    -  -- ^ free type variables, in the order they appear in
    
    826
    -orderedFVs vs tys =
    
    827
    -  reverse . fst $ tyCoFVsOfTypes' tys (const True) vs ([], emptyVarSet)
    
    828
    -
    
    829
    --- See the "Free variables of types and coercions" section in 'TyCoRep', or
    
    830
    --- check out Note [Free variables of types]. The functions in this section
    
    831
    --- don't output type variables in the order they first appear in in the 'Type'.
    
    832
    ---
    
    833
    --- For example, 'tyCoVarsOfTypeList' reports an incorrect order for the type
    
    834
    --- of 'const :: a -> b -> a':
    
    835
    ---
    
    836
    --- >>> import GHC.Types.Name
    
    837
    --- >>> import TyCoRep
    
    838
    --- >>> import GHC.Builtin.Types.Prim
    
    839
    --- >>> import GHC.Types.Var
    
    840
    --- >>> a = TyVarTy alphaTyVar
    
    841
    --- >>> b = TyVarTy betaTyVar
    
    842
    --- >>> constTy = mkFunTys [a, b] a
    
    843
    --- >>> map (getOccString . tyVarName) (tyCoVarsOfTypeList constTy)
    
    844
    --- ["b","a"]
    
    845
    ---
    
    846
    --- However, we want to reuse the very optimized traversal machinery there, so
    
    847
    --- so we make our own `tyCoFVsOfType'`, `tyCoFVsBndr'`, and `tyCoVarsOfTypes'`.
    
    848
    --- All these do differently is traverse in a different order and ignore
    
    849
    --- coercion variables.
    
    850
    -
    
    851
    --- | Just like 'tyCoFVsOfType', but traverses type variables in reverse order
    
    852
    --- of  appearance.
    
    853
    -tyCoFVsOfType' :: Type -> FV
    
    854
    -tyCoFVsOfType' (TyVarTy v) a b c = (FV.unitFV v `unionFV` tyCoFVsOfType' (tyVarKind v)) a b c
    
    855
    -tyCoFVsOfType' (TyConApp _ tys) a b c = tyCoFVsOfTypes' tys a b c
    
    856
    -tyCoFVsOfType' (LitTy{}) a b c = emptyFV a b c
    
    857
    -tyCoFVsOfType' (AppTy fun arg) a b c = (tyCoFVsOfType' arg `unionFV` tyCoFVsOfType' fun) a b c
    
    858
    -tyCoFVsOfType' (FunTy _ w arg res) a b c =
    
    859
    -  ( tyCoFVsOfType' res
    
    860
    -      `unionFV` tyCoFVsOfType' w
    
    861
    -      `unionFV` tyCoFVsOfType' arg
    
    862
    -  )
    
    863
    -    a
    
    864
    -    b
    
    865
    -    c
    
    866
    -tyCoFVsOfType' (ForAllTy bndr ty) a b c = tyCoFVsBndr' bndr (tyCoFVsOfType' ty) a b c
    
    867
    -tyCoFVsOfType' (CastTy ty _) a b c = (tyCoFVsOfType' ty) a b c
    
    868
    -tyCoFVsOfType' (CoercionTy _) a b c = emptyFV a b c
    
    869
    -
    
    870
    --- | Just like 'tyCoFVsOfTypes', but traverses type variables in reverse order
    
    871
    --- of appearance.
    
    872
    -tyCoFVsOfTypes' :: [Type] -> FV
    
    873
    -tyCoFVsOfTypes' (ty : tys) fv_cand in_scope acc = (tyCoFVsOfTypes' tys `unionFV` tyCoFVsOfType' ty) fv_cand in_scope acc
    
    874
    -tyCoFVsOfTypes' [] fv_cand in_scope acc = emptyFV fv_cand in_scope acc
    
    875
    -
    
    876
    --- | Just like 'tyCoFVsBndr', but traverses type variables in reverse order of
    
    877
    --- appearance.
    
    878
    -tyCoFVsBndr' :: TyVarBinder -> FV -> FV
    
    879
    -tyCoFVsBndr' (Bndr tv _) fvs = FV.delFV tv fvs `unionFV` tyCoFVsOfType' (tyVarKind tv)
    
    816
    +  :: VarSet  -- ^ Free variables to ignore
    
    817
    +  -> [Type]  -- ^ Types to traverse (in order) looking for free variables
    
    818
    +  -> [TyVar] -- ^ Free type variables, /in the order in which they appear/
    
    819
    +orderedFVs ignore_tvs tys
    
    820
    +  = dVarSetElems (runEndoOS (runFV get_fvs ignore_tvs) emptyDVarSet)
    
    821
    +  where
    
    822
    +    get_fvs :: DVarSetFV
    
    823
    +    get_fvs = deepDetTypesFV tys
    
    880 824
     
    
    881 825
     -------------------------------------------------------------------------------
    
    882 826