CoreSubst

ghc-8.6.3: The GHC API

Safe Haskell | None
Language | Haskell2010

CoreSubst

Contents

- Main data types
  - Substituting into expressions and related types
  - Operations on substitutions
  - Substituting and cloning binders

Synopsis

- data Subst = Subst InScopeSet IdSubstEnv TvSubstEnv CvSubstEnv
- type TvSubstEnv = TyVarEnv Type
- type IdSubstEnv = IdEnv CoreExpr
- data InScopeSet
- deShadowBinds :: CoreProgram -> CoreProgram
- substSpec :: Subst -> Id -> RuleInfo -> RuleInfo
- substRulesForImportedIds :: Subst -> [CoreRule] -> [CoreRule]
- substTy :: Subst -> Type -> Type
- substCo :: Subst -> Coercion -> Coercion
- substExpr :: SDoc -> Subst -> CoreExpr -> CoreExpr
- substExprSC :: SDoc -> Subst -> CoreExpr -> CoreExpr
- substBind :: Subst -> CoreBind -> (Subst, CoreBind)
- substBindSC :: Subst -> CoreBind -> (Subst, CoreBind)
- substUnfolding :: Subst -> Unfolding -> Unfolding
- substUnfoldingSC :: Subst -> Unfolding -> Unfolding
- lookupIdSubst :: SDoc -> Subst -> Id -> CoreExpr
- lookupTCvSubst :: Subst -> TyVar -> Type
- substIdOcc :: Subst -> Id -> Id
- substTickish :: Subst -> Tickish Id -> Tickish Id
- substDVarSet :: Subst -> DVarSet -> DVarSet
- substIdInfo :: Subst -> Id -> IdInfo -> Maybe IdInfo
- emptySubst :: Subst
- mkEmptySubst :: InScopeSet -> Subst
- mkSubst :: InScopeSet -> TvSubstEnv -> CvSubstEnv -> IdSubstEnv -> Subst
- mkOpenSubst :: InScopeSet -> [(Var, CoreArg)] -> Subst
- substInScope :: Subst -> InScopeSet
- isEmptySubst :: Subst -> Bool
- extendIdSubst :: Subst -> Id -> CoreExpr -> Subst
- extendIdSubstList :: Subst -> [(Id, CoreExpr)] -> Subst
- extendTCvSubst :: TCvSubst -> TyCoVar -> Type -> TCvSubst
- extendTvSubstList :: Subst -> [(TyVar, Type)] -> Subst
- extendSubst :: Subst -> Var -> CoreArg -> Subst
- extendSubstList :: Subst -> [(Var, CoreArg)] -> Subst
- extendSubstWithVar :: Subst -> Var -> Var -> Subst
- zapSubstEnv :: Subst -> Subst
- addInScopeSet :: Subst -> VarSet -> Subst
- extendInScope :: Subst -> Var -> Subst
- extendInScopeList :: Subst -> [Var] -> Subst
- extendInScopeIds :: Subst -> [Id] -> Subst
- isInScope :: Var -> Subst -> Bool
- setInScope :: Subst -> InScopeSet -> Subst
- getTCvSubst :: Subst -> TCvSubst
- extendTvSubst :: Subst -> TyVar -> Type -> Subst
- extendCvSubst :: Subst -> CoVar -> Coercion -> Subst
- delBndr :: Subst -> Var -> Subst
- delBndrs :: Subst -> [Var] -> Subst
- substBndr :: Subst -> Var -> (Subst, Var)
- substBndrs :: Subst -> [Var] -> (Subst, [Var])
- substRecBndrs :: Subst -> [Id] -> (Subst, [Id])
- substTyVarBndr :: Subst -> TyVar -> (Subst, TyVar)
- substCoVarBndr :: Subst -> TyVar -> (Subst, TyVar)
- cloneBndr :: Subst -> Unique -> Var -> (Subst, Var)
- cloneBndrs :: Subst -> UniqSupply -> [Var] -> (Subst, [Var])
- cloneIdBndr :: Subst -> UniqSupply -> Id -> (Subst, Id)
- cloneIdBndrs :: Subst -> UniqSupply -> [Id] -> (Subst, [Id])
- cloneRecIdBndrs :: Subst -> UniqSupply -> [Id] -> (Subst, [Id])

# Main data types

data Subst #

A substitution environment, containing `Id`, `TyVar`, and `CoVar` substitutions.

Some invariants apply to how you use the substitution:

1. Note [The substitution invariant] in TyCoRep
2. Note [Substitutions apply only once] in TyCoRep

Constructors

Subst InScopeSet IdSubstEnv TvSubstEnv CvSubstEnv |

Instances

Outputable Subst # |
Instance details Defined in CoreSubst Methods ppr :: Subst -> SDoc # pprPrec :: Rational -> Subst -> SDoc #

type TvSubstEnv = TyVarEnv Type #

A substitution of `Type`s for `TyVar`s and `Kind`s for `KindVar`s

type IdSubstEnv = IdEnv CoreExpr #

An environment for substituting for `Id`s

data InScopeSet #

A set of variables that are in scope at some point "Secrets of the Glasgow Haskell Compiler inliner" Section 3.2 provides the motivation for this abstraction.

Instances

Outputable InScopeSet # |
Instance details Defined in VarEnv Methods ppr :: InScopeSet -> SDoc # pprPrec :: Rational -> InScopeSet -> SDoc #

## Substituting into expressions and related types

deShadowBinds :: CoreProgram -> CoreProgram #

De-shadowing the program is sometimes a useful pre-pass. It can be done simply by running over the bindings with an empty substitution, because substitution returns a result that has no-shadowing guaranteed.

(Actually, within a single type there might still be shadowing, because `substTy` is a no-op for the empty substitution, but that's probably OK.)

Aug 09
  This function is not used in GHC at the moment, but seems so short and simple that I'm going to leave it here

substSpec :: Subst -> Id -> RuleInfo -> RuleInfo #

Substitutes for the `Id`s within the `WorkerInfo` given the new function `Id`

substRulesForImportedIds :: Subst -> [CoreRule] -> [CoreRule] #

substTy :: Subst -> Type -> Type #

See `substTy`

substCo :: Subst -> Coercion -> Coercion #

See `substCo`

substExpr :: SDoc -> Subst -> CoreExpr -> CoreExpr #

substExprSC :: SDoc -> Subst -> CoreExpr -> CoreExpr #

Apply a substitution to an entire `CoreExpr`. Remember, you may only apply the substitution once: see Note [Substitutions apply only once] in TyCoRep

Do *not* attempt to short-cut in the case of an empty substitution! See Note [Extending the Subst]

substBind :: Subst -> CoreBind -> (Subst, CoreBind) #

Apply a substitution to an entire `CoreBind`, additionally returning an updated `Subst` that should be used by subsequent substitutions.

substBindSC :: Subst -> CoreBind -> (Subst, CoreBind) #

Apply a substitution to an entire `CoreBind`, additionally returning an updated `Subst` that should be used by subsequent substitutions.

substUnfolding :: Subst -> Unfolding -> Unfolding #

Substitutes for the `Id`s within an unfolding

substUnfoldingSC :: Subst -> Unfolding -> Unfolding #

Substitutes for the `Id`s within an unfolding

lookupIdSubst :: SDoc -> Subst -> Id -> CoreExpr #

Find the substitution for an `Id` in the `Subst`

lookupTCvSubst :: Subst -> TyVar -> Type #

Find the substitution for a `TyVar` in the `Subst`

substIdOcc :: Subst -> Id -> Id #

substTickish :: Subst -> Tickish Id -> Tickish Id #

substDVarSet :: Subst -> DVarSet -> DVarSet #

substIdInfo :: Subst -> Id -> IdInfo -> Maybe IdInfo #

Substitute into some `IdInfo` with regard to the supplied new `Id`.

## Operations on substitutions

emptySubst :: Subst #

mkEmptySubst :: InScopeSet -> Subst #

mkSubst :: InScopeSet -> TvSubstEnv -> CvSubstEnv -> IdSubstEnv -> Subst #

mkOpenSubst :: InScopeSet -> [(Var, CoreArg)] -> Subst #

Simultaneously substitute for a bunch of variables No left-right shadowing ie the substitution for (x y. e) a1 a2 so neither x nor y scope over a1 a2

substInScope :: Subst -> InScopeSet #

Find the in-scope set: see TyCORep Note [The substitution invariant]

isEmptySubst :: Subst -> Bool #

extendIdSubst :: Subst -> Id -> CoreExpr -> Subst #

Add a substitution for an `Id` to the `Subst`: you must ensure that the in-scope set is such that TyCORep Note [The substitution invariant] holds after extending the substitution like this

extendIdSubstList :: Subst -> [(Id, CoreExpr)] -> Subst #

Adds multiple `Id` substitutions to the `Subst`: see also `extendIdSubst`

extendTCvSubst :: TCvSubst -> TyCoVar -> Type -> TCvSubst #

extendTvSubstList :: Subst -> [(TyVar, Type)] -> Subst #

Adds multiple `TyVar` substitutions to the `Subst`: see also `extendTvSubst`

extendSubst :: Subst -> Var -> CoreArg -> Subst #

Add a substitution appropriate to the thing being substituted (whether an expression, type, or coercion). See also `extendIdSubst`, `extendTvSubst`, `extendCvSubst`

extendSubstList :: Subst -> [(Var, CoreArg)] -> Subst #

Add a substitution as appropriate to each of the terms being substituted (whether expressions, types, or coercions). See also `extendSubst`.

extendSubstWithVar :: Subst -> Var -> Var -> Subst #

zapSubstEnv :: Subst -> Subst #

Remove all substitutions for `Id`s and `Expr`s that might have been built up while preserving the in-scope set

addInScopeSet :: Subst -> VarSet -> Subst #

Add the `Expr` to the in-scope set, but do not remove any existing substitutions for it

extendInScope :: Subst -> Var -> Subst #

Add the `Expr` to the in-scope set: as a side effect, and remove any existing substitutions for it

extendInScopeList :: Subst -> [Var] -> Subst #

Add the `Expr`s to the in-scope set: see also `extendInScope`

extendInScopeIds :: Subst -> [Id] -> Subst #

Optimized version of `extendInScopeList` that can be used if you are certain all the things being added are `Id`s and hence none are `TyVar`s or `CoVar`s

isInScope :: Var -> Subst -> Bool #

setInScope :: Subst -> InScopeSet -> Subst #

getTCvSubst :: Subst -> TCvSubst #

extendTvSubst :: Subst -> TyVar -> Type -> Subst #

Add a substitution for a `TyVar` to the `Subst` The `TyVar` *must* be a real TyVar, and not a CoVar You must ensure that the in-scope set is such that TyCORep Note [The substitution invariant] holds after extending the substitution like this.

extendCvSubst :: Subst -> CoVar -> Coercion -> Subst #

Add a substitution from a `CoVar` to a `Expr` to the `Subst`: you must ensure that the in-scope set satisfies TyCORep Note [The substitution invariant] after extending the substitution like this

delBndr :: Subst -> Var -> Subst #

delBndrs :: Subst -> [Var] -> Subst #

## Substituting and cloning binders

substBndr :: Subst -> Var -> (Subst, Var) #

Substitutes a `Expr` for another one according to the `Subst` given, returning the result and an updated `Subst` that should be used by subsequent substitutions. `IdInfo` is preserved by this process, although it is substituted into appropriately.

substBndrs :: Subst -> [Var] -> (Subst, [Var]) #

Applies `substBndr` to a number of `Expr`s, accumulating a new `Subst` left-to-right

substRecBndrs :: Subst -> [Id] -> (Subst, [Id]) #

Substitute in a mutually recursive group of `Id`s

substTyVarBndr :: Subst -> TyVar -> (Subst, TyVar) #

substCoVarBndr :: Subst -> TyVar -> (Subst, TyVar) #

cloneBndr :: Subst -> Unique -> Var -> (Subst, Var) #

cloneBndrs :: Subst -> UniqSupply -> [Var] -> (Subst, [Var]) #

cloneIdBndr :: Subst -> UniqSupply -> Id -> (Subst, Id) #

Very similar to `substBndr`, but it always allocates a new `Unique` for each variable in its output. It substitutes the IdInfo though.

cloneIdBndrs :: Subst -> UniqSupply -> [Id] -> (Subst, [Id]) #

Applies `cloneIdBndr` to a number of `Id`s, accumulating a final substitution from left to right

cloneRecIdBndrs :: Subst -> UniqSupply -> [Id] -> (Subst, [Id]) #

Clone a mutually recursive group of `Id`s