Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Synopsis
- class (Functor m, ExceptionMonad m, HasDynFlags m, HasLogger m) => GhcMonad m where
- getSession :: m HscEnv
- setSession :: HscEnv -> m ()
- newtype Ghc a = Ghc {}
- newtype GhcT m a = GhcT {}
- liftGhcT :: m a -> GhcT m a
- reflectGhc :: Ghc a -> Session -> IO a
- reifyGhc :: (Session -> IO a) -> Ghc a
- getSessionDynFlags :: GhcMonad m => m DynFlags
- liftIO :: MonadIO m => IO a -> m a
- data Session = Session !(IORef HscEnv)
- withSession :: GhcMonad m => (HscEnv -> m a) -> m a
- modifySession :: GhcMonad m => (HscEnv -> HscEnv) -> m ()
- modifySessionM :: GhcMonad m => (HscEnv -> m HscEnv) -> m ()
- withTempSession :: GhcMonad m => (HscEnv -> HscEnv) -> m a -> m a
- modifyLogger :: GhcMonad m => (Logger -> Logger) -> m ()
- pushLogHookM :: GhcMonad m => (LogAction -> LogAction) -> m ()
- popLogHookM :: GhcMonad m => m ()
- putLogMsgM :: GhcMonad m => WarnReason -> Severity -> SrcSpan -> SDoc -> m ()
- putMsgM :: GhcMonad m => SDoc -> m ()
- withTimingM :: GhcMonad m => SDoc -> (b -> ()) -> m b -> m b
- logWarnings :: GhcMonad m => WarningMessages -> m ()
- printException :: GhcMonad m => SourceError -> m ()
- type WarnErrLogger = forall m. GhcMonad m => Maybe SourceError -> m ()
- defaultWarnErrLogger :: WarnErrLogger
Ghc
monad stuff
class (Functor m, ExceptionMonad m, HasDynFlags m, HasLogger m) => GhcMonad m where Source #
A monad that has all the features needed by GHC API calls.
In short, a GHC monad
- allows embedding of IO actions,
- can log warnings,
- allows handling of (extensible) exceptions, and
- maintains a current session.
If you do not use Ghc
or GhcT
, make sure to call initGhcMonad
before any call to the GHC API functions can occur.
getSession :: m HscEnv Source #
setSession :: HscEnv -> m () Source #
Instances
GhcMonad Ghc # | |
Defined in GHC.Driver.Monad getSession :: Ghc HscEnv Source # setSession :: HscEnv -> Ghc () Source # | |
ExceptionMonad m => GhcMonad (GhcT m) # | |
Defined in GHC.Driver.Monad getSession :: GhcT m HscEnv Source # setSession :: HscEnv -> GhcT m () Source # |
A minimal implementation of a GhcMonad
. If you need a custom monad,
e.g., to maintain additional state consider wrapping this monad or using
GhcT
.
Instances
MonadFix Ghc # | |
MonadIO Ghc # | |
Applicative Ghc # | |
Functor Ghc # | |
Monad Ghc # | |
MonadCatch Ghc # | |
MonadMask Ghc # | |
MonadThrow Ghc # | |
GhcMonad Ghc # | |
Defined in GHC.Driver.Monad getSession :: Ghc HscEnv Source # setSession :: HscEnv -> Ghc () Source # | |
HasDynFlags Ghc # | |
Defined in GHC.Driver.Monad | |
HasLogger Ghc # | |
A monad transformer to add GHC specific features to another monad.
Note that the wrapped monad must support IO and handling of exceptions.
Instances
MonadIO m => MonadIO (GhcT m) # | |
Applicative m => Applicative (GhcT m) # | |
Functor m => Functor (GhcT m) # | |
Monad m => Monad (GhcT m) # | |
MonadCatch m => MonadCatch (GhcT m) # | |
MonadMask m => MonadMask (GhcT m) # | |
MonadThrow m => MonadThrow (GhcT m) # | |
ExceptionMonad m => GhcMonad (GhcT m) # | |
Defined in GHC.Driver.Monad getSession :: GhcT m HscEnv Source # setSession :: HscEnv -> GhcT m () Source # | |
MonadIO m => HasDynFlags (GhcT m) # | |
Defined in GHC.Driver.Monad getDynFlags :: GhcT m DynFlags Source # | |
MonadIO m => HasLogger (GhcT m) # | |
reflectGhc :: Ghc a -> Session -> IO a Source #
Reflect a computation in the Ghc
monad into the IO
monad.
You can use this to call functions returning an action in the Ghc
monad
inside an IO
action. This is needed for some (too restrictive) callback
arguments of some library functions:
libFunc :: String -> (Int -> IO a) -> IO a ghcFunc :: Int -> Ghc a ghcFuncUsingLibFunc :: String -> Ghc a -> Ghc a ghcFuncUsingLibFunc str = reifyGhc $ \s -> libFunc $ \i -> do reflectGhc (ghcFunc i) s
getSessionDynFlags :: GhcMonad m => m DynFlags Source #
Grabs the DynFlags from the Session
liftIO :: MonadIO m => IO a -> m a Source #
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
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
, we would have ended up with this error:liftIO
• 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
and returns an IO
a(m a)
:
,
enabling us to run the program and see the expected results:liftIO
> evalStateT printState "hello" "hello" > evalStateT printState 3 3
The Session is a handle to the complete state of a compilation session. A compilation session consists of a set of modules constituting the current program or library, the context for interactive evaluation, and various caches.
withSession :: GhcMonad m => (HscEnv -> m a) -> m a Source #
Call the argument with the current session.
modifySession :: GhcMonad m => (HscEnv -> HscEnv) -> m () Source #
Set the current session to the result of applying the current session to the argument.
modifySessionM :: GhcMonad m => (HscEnv -> m HscEnv) -> m () Source #
Set the current session to the result of applying the current session to the argument.
withTempSession :: GhcMonad m => (HscEnv -> HscEnv) -> m a -> m a Source #
Call an action with a temporarily modified Session.
Logger
popLogHookM :: GhcMonad m => m () Source #
Pop a log hook from the stack
putLogMsgM :: GhcMonad m => WarnReason -> Severity -> SrcSpan -> SDoc -> m () Source #
Put a log message
withTimingM :: GhcMonad m => SDoc -> (b -> ()) -> m b -> m b Source #
Time an action
Warnings
logWarnings :: GhcMonad m => WarningMessages -> m () Source #
A monad that allows logging of warnings.
printException :: GhcMonad m => SourceError -> m () Source #
Print the error message and all warnings. Useful inside exception handlers. Clears warnings after printing.
type WarnErrLogger = forall m. GhcMonad m => Maybe SourceError -> m () Source #
A function called to log warnings and errors.