GHC.Core.Rules

ghc-9.10.0.20240426: The GHC API

Safe Haskell | None
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) Source #

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]) Source #

## RuleBase, RuleEnv

type RuleBase = NameEnv [CoreRule] Source #

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

data RuleEnv Source #

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 Source #

emptyRuleEnv :: RuleEnv Source #

updExternalPackageRules :: RuleEnv -> RuleBase -> RuleEnv Source #

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

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

emptyRuleBase :: RuleBase Source #

mkRuleBase :: [CoreRule] -> RuleBase Source #

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

pprRuleBase :: RuleBase -> SDoc Source #

## Checking rule applications

ruleCheckProgram Source #

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 Source #

addRuleInfo :: RuleInfo -> RuleInfo -> RuleInfo Source #

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

addRulesToId :: RuleBase -> Id -> Id Source #

## RuleBase and RuleEnv

# Misc. CoreRule helpers

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

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

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

pprRulesForUser :: [CoreRule] -> SDoc Source #

# Making rules

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

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 Source #

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

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