ghc-lib-parser-9.10.1.20250103: The GHC API, decoupled from GHC versions

Safe Haskell | Ignore
Language | GHC2021

GHC.Core.Rules

Contents

- Looking up rules
- RuleBase, RuleEnv
- Checking rule applications
- Manipulating `RuleInfo` rules
- RuleBase and RuleEnv
- Misc. CoreRule helpers
- Making rules

Description

Functions for collecting together and applying rewrite rules to a module. The `CoreRule` datatype itself is declared elsewhere.

Synopsis

- lookupRule :: RuleOpts -> InScopeEnv -> (Activation -> Bool) -> Id -> [CoreExpr] -> [CoreRule] -> Maybe (CoreRule, CoreExpr)
- matchExprs :: InScopeEnv -> [Var] -> [CoreExpr] -> [CoreExpr] -> Maybe (BindWrapper, [CoreExpr])
- type RuleBase = NameEnv [CoreRule]
- data RuleEnv = RuleEnv {
  - re_local_rules :: !RuleBase
  - re_home_rules :: !RuleBase
  - re_eps_rules :: !RuleBase
  - re_visible_orphs :: !ModuleSet
  }
- mkRuleEnv :: ModGuts -> RuleBase -> RuleBase -> RuleEnv
- emptyRuleEnv :: RuleEnv
- updExternalPackageRules :: RuleEnv -> RuleBase -> RuleEnv
- addLocalRules :: RuleEnv -> [CoreRule] -> RuleEnv
- updLocalRules :: RuleEnv -> [CoreRule] -> RuleEnv
- emptyRuleBase :: RuleBase
- mkRuleBase :: [CoreRule] -> RuleBase
- extendRuleBaseList :: RuleBase -> [CoreRule] -> RuleBase
- pprRuleBase :: RuleBase -> SDoc
- ruleCheckProgram :: RuleOpts -> CompilerPhase -> String -> (Id -> [CoreRule]) -> CoreProgram -> SDoc
- extendRuleInfo :: RuleInfo -> [CoreRule] -> RuleInfo
- addRuleInfo :: RuleInfo -> RuleInfo -> RuleInfo
- addIdSpecialisations :: Id -> [CoreRule] -> Id
- addRulesToId :: RuleBase -> Id -> Id
- rulesOfBinds :: [CoreBind] -> [CoreRule]
- getRules :: RuleEnv -> Id -> [CoreRule]
- pprRulesForUser :: [CoreRule] -> SDoc
- mkRule :: Module -> Bool -> Bool -> RuleName -> Activation -> Name -> [CoreBndr] -> [CoreExpr] -> CoreExpr -> CoreRule
- mkSpecRule :: DynFlags -> Module -> Bool -> Activation -> SDoc -> Id -> [CoreBndr] -> [CoreExpr] -> CoreExpr -> CoreRule
- roughTopNames :: [CoreExpr] -> [Maybe Name]

## Looking up rules

lookupRule :: RuleOpts -> InScopeEnv -> (Activation -> Bool) -> Id -> [CoreExpr] -> [CoreRule] -> Maybe (CoreRule, CoreExpr) #

The main rule matching function. Attempts to apply all (active) supplied rules to this instance of an application in a given context, returning the rule applied and the resulting expression if successful.

matchExprs :: InScopeEnv -> [Var] -> [CoreExpr] -> [CoreExpr] -> Maybe (BindWrapper, [CoreExpr]) #

## RuleBase, RuleEnv

type RuleBase = NameEnv [CoreRule] #

Gathers a collection of `CoreRule`s. Maps (the name of) an `Id` to its rules

data RuleEnv #

A full rule environment which we can apply rules from. Like a `RuleBase`, but it also includes the set of visible orphans we use to filter out orphan rules which are not visible (even though we can see them...) See Note [Orphans] in GHC.Core

Constructors

RuleEnv |

Fields

- re_local_rules :: !RuleBase
- re_home_rules :: !RuleBase
- re_eps_rules :: !RuleBase
- re_visible_orphs :: !ModuleSet

mkRuleEnv :: ModGuts -> RuleBase -> RuleBase -> RuleEnv #

emptyRuleEnv :: RuleEnv #

updExternalPackageRules :: RuleEnv -> RuleBase -> RuleEnv #

addLocalRules :: RuleEnv -> [CoreRule] -> RuleEnv #

updLocalRules :: RuleEnv -> [CoreRule] -> RuleEnv #

emptyRuleBase :: RuleBase #

mkRuleBase :: [CoreRule] -> RuleBase #

extendRuleBaseList :: RuleBase -> [CoreRule] -> RuleBase #

pprRuleBase :: RuleBase -> SDoc #

## Checking rule applications

ruleCheckProgram #

Arguments

:: RuleOpts | Rule options
-> CompilerPhase | Rule activation test
-> String | Rule pattern
-> (Id -> [CoreRule]) | Rules for an Id
-> CoreProgram | Bindings to check in
-> SDoc | Resulting check message

Report partial matches for rules beginning with the specified string for the purposes of error reporting

## Manipulating `RuleInfo` rules

extendRuleInfo :: RuleInfo -> [CoreRule] -> RuleInfo #

addRuleInfo :: RuleInfo -> RuleInfo -> RuleInfo #

addIdSpecialisations :: Id -> [CoreRule] -> Id #

addRulesToId :: RuleBase -> Id -> Id #

## RuleBase and RuleEnv

# Misc. CoreRule helpers

rulesOfBinds :: [CoreBind] -> [CoreRule] #

Gather all the rules for locally bound identifiers from the supplied bindings

getRules :: RuleEnv -> Id -> [CoreRule] #

pprRulesForUser :: [CoreRule] -> SDoc #

# Making rules

mkRule :: Module -> Bool -> Bool -> RuleName -> Activation -> Name -> [CoreBndr] -> [CoreExpr] -> CoreExpr -> CoreRule #

Used to make `CoreRule` for an `Id` defined in the module being compiled. See also `CoreRule`

mkSpecRule :: DynFlags -> Module -> Bool -> Activation -> SDoc -> Id -> [CoreBndr] -> [CoreExpr] -> CoreExpr -> CoreRule #

roughTopNames :: [CoreExpr] -> [Maybe Name] #

Find the "top" free names of several expressions. Such names are either:

1. The function finally being applied to in an application chain (if that name is a GlobalId: see GHC.Types.Var), or
2. The `TyCon` if the expression is a `Type`

This is used for the fast-match-check for rules; if the top names don't match, the rest can't