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

Safe Haskell | Ignore
Language | GHC2021

GHC.Core.Opt.Monad

Contents

- Types used in core-to-core passes
- The monad
  - Reading from the monad
  - Writing to the monad
  - Lifting into the monad
  - Dealing with annotations
  - Screen output

Synopsis

- data FloatOutSwitches = FloatOutSwitches {
  - floatOutLambdas :: Maybe Int
  - floatOutConstants :: Bool
  - floatOutOverSatApps :: Bool
  - floatToTopLevelOnly :: Bool
  - floatJoinsToTop :: Bool
  }
- data CoreM a
- runCoreM :: HscEnv -> RuleBase -> Char -> Module -> NamePprCtx -> SrcSpan -> CoreM a -> IO (a, SimplCount)
- mapDynFlagsCoreM :: (DynFlags -> DynFlags) -> CoreM a -> CoreM a
- dropSimplCount :: CoreM a -> CoreM a
- getHscEnv :: CoreM HscEnv
- getModule :: HasModule m => m Module
- initRuleEnv :: ModGuts -> CoreM RuleEnv
- getExternalRuleBase :: CoreM RuleBase
- getDynFlags :: HasDynFlags m => m DynFlags
- getPackageFamInstEnv :: CoreM PackageFamInstEnv
- getInteractiveContext :: CoreM InteractiveContext
- getUniqTag :: CoreM Char
- getNamePprCtx :: CoreM NamePprCtx
- getSrcSpanM :: CoreM SrcSpan
- addSimplCount :: SimplCount -> CoreM ()
- liftIO :: MonadIO m => IO a -> m a
- liftIOWithCount :: IO (SimplCount, a) -> CoreM a
- getAnnotations :: Typeable a => ([Word8] -> a) -> ModGuts -> CoreM (ModuleEnv [a], NameEnv [a])
- getFirstAnnotations :: Typeable a => ([Word8] -> a) -> ModGuts -> CoreM (ModuleEnv a, NameEnv a)
- putMsg :: SDoc -> CoreM ()
- putMsgS :: String -> CoreM ()
- errorMsg :: SDoc -> CoreM ()
- msg :: MessageClass -> SDoc -> CoreM ()
- fatalErrorMsg :: SDoc -> CoreM ()
- fatalErrorMsgS :: String -> CoreM ()
- debugTraceMsg :: SDoc -> CoreM ()
- debugTraceMsgS :: String -> CoreM ()

# Types used in core-to-core passes

data FloatOutSwitches #

Constructors

FloatOutSwitches |
Fields floatOutLambdas :: Maybe Int Just n = float lambdas to top level, if doing so will abstract over n or fewer value variables Nothing = float all lambdas to top level, regardless of how many free variables Just 0 is the vanilla case: float a lambda iff it has no free vars floatOutConstants :: Bool True = float constants to top level, even if they do not escape a lambda floatOutOverSatApps :: Bool True = float out over-saturated applications based on arity information. See Note [Floating over-saturated applications] in GHC.Core.Opt.SetLevels floatToTopLevelOnly :: Bool Allow floating to the top level only. floatJoinsToTop :: Bool Float join points to top level if possible See Note [Floating join point bindings] in GHC.Core.Opt.SetLevels

#### Instances

Instances details

Outputable FloatOutSwitches # |
Instance details Defined in GHC.Core.Opt.Monad Methods ppr :: FloatOutSwitches -> SDoc #

# The monad

data CoreM a #

The monad used by Core-to-Core passes to register simplification statistics. Also used to have common state (in the form of UniqueSupply) for generating Uniques.

#### Instances

Instances details

MonadIO CoreM # |
Instance details Defined in GHC.Core.Opt.Monad Methods liftIO :: IO a -> CoreM a #
Alternative CoreM # |
Instance details Defined in GHC.Core.Opt.Monad Methods empty :: CoreM a # (<|>) :: CoreM a -> CoreM a -> CoreM a # some :: CoreM a -> CoreM [a] # many :: CoreM a -> CoreM [a] #
Applicative CoreM # |
Instance details Defined in GHC.Core.Opt.Monad Methods pure :: a -> CoreM a # (<*>) :: CoreM (a -> b) -> CoreM a -> CoreM b # liftA2 :: (a -> b -> c) -> CoreM a -> CoreM b -> CoreM c # (*>) :: CoreM a -> CoreM b -> CoreM b # (<*) :: CoreM a -> CoreM b -> CoreM a #
Functor CoreM # |
Instance details Defined in GHC.Core.Opt.Monad Methods fmap :: (a -> b) -> CoreM a -> CoreM b # (<$) :: a -> CoreM b -> CoreM a #
Monad CoreM # |
Instance details Defined in GHC.Core.Opt.Monad Methods (>>=) :: CoreM a -> (a -> CoreM b) -> CoreM b # (>>) :: CoreM a -> CoreM b -> CoreM b # return :: a -> CoreM a #
MonadPlus CoreM # |
Instance details Defined in GHC.Core.Opt.Monad Methods mzero :: CoreM a # mplus :: CoreM a -> CoreM a -> CoreM a #
HasDynFlags CoreM # |
Instance details Defined in GHC.Core.Opt.Monad Methods getDynFlags :: CoreM DynFlags #
MonadUnique CoreM # |
Instance details Defined in GHC.Core.Opt.Monad Methods getUniqueSupplyM :: CoreM UniqSupply # getUniqueM :: CoreM Unique # getUniquesM :: CoreM [Unique] #
HasModule CoreM # |
Instance details Defined in GHC.Core.Opt.Monad Methods getModule :: CoreM Module #
HasLogger CoreM # |
Instance details Defined in GHC.Core.Opt.Monad Methods getLogger :: CoreM Logger #

runCoreM #

Arguments

:: HscEnv |
-> RuleBase |
-> Char | Mask
-> Module |
-> NamePprCtx |
-> SrcSpan |
-> CoreM a |
-> IO (a, SimplCount) |

mapDynFlagsCoreM :: (DynFlags -> DynFlags) -> CoreM a -> CoreM a #

Adjust the dyn flags passed to the argument action

dropSimplCount :: CoreM a -> CoreM a #

Drop the single count of the argument action so it doesn't effect the total.

## Reading from the monad

getHscEnv :: CoreM HscEnv #

getModule :: HasModule m => m Module #

initRuleEnv :: ModGuts -> CoreM RuleEnv #

getExternalRuleBase :: CoreM RuleBase #

getDynFlags :: HasDynFlags m => m DynFlags #

getPackageFamInstEnv :: CoreM PackageFamInstEnv #

getInteractiveContext :: CoreM InteractiveContext #

getUniqTag :: CoreM Char #

getNamePprCtx :: CoreM NamePprCtx #

getSrcSpanM :: CoreM SrcSpan #

## Writing to the monad

addSimplCount :: SimplCount -> CoreM () #

## Lifting into the monad

liftIO :: MonadIO m => IO a -> m a #

Lift a computation from the `IO` monad. This allows us to run IO computations in any monadic stack, so long as it supports these kinds of operations (i.e. `IO` is the base monad for the stack).

### Example

Expand

    import Control.Monad.Trans.State -- from the "transformers" library

    printState :: Show s => StateT s IO ()
    printState = do
      state <- get
      liftIO $ print state

Had we omitted ``liftIO``, we would have ended up with this error:

    • Couldn't match type ‘IO’ with ‘StateT s IO’
     Expected type: StateT s IO ()
       Actual type: IO ()

The important part here is the mismatch between `StateT s IO ()` and ``IO` ()`.

Luckily, we know of a function that takes an ``IO` a` and returns an `(m a)`: ``liftIO``, enabling us to run the program and see the expected results:

    > evalStateT printState "hello"
    "hello"

    > evalStateT printState 3
    3

liftIOWithCount :: IO (SimplCount, a) -> CoreM a #

Lift an `IO` operation into `CoreM` while consuming its `SimplCount`

## Dealing with annotations

getAnnotations :: Typeable a => ([Word8] -> a) -> ModGuts -> CoreM (ModuleEnv [a], NameEnv [a]) #

Get all annotations of a given type. This happens lazily, that is no deserialization will take place until the [a] is actually demanded and the [a] can also be empty (the UniqFM is not filtered).

This should be done once at the start of a Core-to-Core pass that uses annotations.

See Note [Annotations]

getFirstAnnotations :: Typeable a => ([Word8] -> a) -> ModGuts -> CoreM (ModuleEnv a, NameEnv a) #

Get at most one annotation of a given type per annotatable item.

## Screen output

putMsg :: SDoc -> CoreM () #

Output a message to the screen

putMsgS :: String -> CoreM () #

Output a String message to the screen

errorMsg :: SDoc -> CoreM () #

Output an error to the screen. Does not cause the compiler to die.

msg :: MessageClass -> SDoc -> CoreM () #

fatalErrorMsg :: SDoc -> CoreM () #

Output a fatal error to the screen. Does not cause the compiler to die.

fatalErrorMsgS :: String -> CoreM () #

Output a fatal error to the screen. Does not cause the compiler to die.

debugTraceMsg :: SDoc -> CoreM () #

Outputs a debugging message at verbosity level of `-v` or higher

debugTraceMsgS :: String -> CoreM () #

Output a string debugging message at verbosity level of `-v` or higher