Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
This module contains the definition of a monad that is used by the compiler to report error messages, warnings and hints to the user without throwing an exception or performing IO actions.
During execution the Reporter
monad collects all reported messages
internally. Additionally the monad holds the result of the computation.
The computation can be interrupted without returning a result by reporting
a fatal error message.
The ReporterT
monad transformer is used to implement ReporterIO
which
simplifies combining IO actions with error reporting.
This module also provides functions for pretty printing the collected error messages in a similar way to how the GHC prints error messages.
Synopsis
- data Message = Message SrcSpan Severity String
- data Severity
- type Reporter = ReporterT Identity
- runReporter :: Reporter a -> (Maybe a, [Message])
- evalReporter :: Reporter a -> Maybe a
- data ReporterT m a
- runReporterT :: Monad m => ReporterT m a -> m (Maybe a, [Message])
- lift :: (MonadTrans t, Monad m) => m a -> t m a
- hoist :: (Hoistable t, Monad m) => t Identity a -> t m a
- unhoist :: (UnHoistable t, Monad m) => t m a -> m (t Identity a)
- class Monad r => MonadReporter r where
- liftReporter :: Reporter a -> r a
- report :: MonadReporter r => Message -> r ()
- reportFatal :: MonadReporter r => Message -> r a
- type ReporterIO = ReporterT IO
- liftIO :: MonadIO m => IO a -> m a
- reportIOError :: MonadReporter r => IOError -> r a
- isFatal :: Reporter a -> Bool
- messages :: Reporter a -> [Message]
- reportTo :: MonadIO m => Handle -> ReporterT m a -> m (Maybe a)
- reportToOrExit :: MonadIO m => Handle -> ReporterT m a -> m a
Messages
A message reported by the compiler.
Instances
Eq Message Source # | |
Show Message Source # | |
Pretty Message Source # | Pretty instance for messages. The format of the messages is based on the format used by GHC: [file]:[line]:[column]: [severity]: [message-contents] | [line] | [line of code ... culprit ... ] | ^^^^^^^ If no location information is attached to the message, a place holder is text displayed instead of the filename, and start position and no code snippet will be shown. Lists of messages are separated by a newline. |
Defined in FreeC.Monad.Reporter |
The severity of a message reported by the compiler.
Reporter Monad
type Reporter = ReporterT Identity Source #
A monad that collects the messages reported by the compiler and contains an optional value that is present only if the compiler did not encounter a fatal error.
This type behaves like (Maybe a, [Message])
.
runReporter :: Reporter a -> (Maybe a, [Message]) Source #
Runs the given reporter and returns the produced value as well as all
reported messages. If a fatal message has been reported the produced
value is Nothing
.
evalReporter :: Reporter a -> Maybe a Source #
Like runReporter
but discards the reported messages.
Reporter Monad Transformer
A reporter monad parameterized by the inner monad m
.
Instances
MonadTrans ReporterT Source # |
|
UnHoistable ReporterT Source # |
|
Hoistable ReporterT Source # | The reporter monad can be lifted to any reporter transformer. |
Monad m => Monad (ReporterT m) Source # | The |
Monad m => Functor (ReporterT m) Source # | The |
Monad m => MonadFail (ReporterT m) Source # | Internal errors (e.g. pattern matching failures in |
Monad m => Applicative (ReporterT m) Source # | The |
Defined in FreeC.Monad.Reporter pure :: a -> ReporterT m a Source # (<*>) :: ReporterT m (a -> b) -> ReporterT m a -> ReporterT m b Source # liftA2 :: (a -> b -> c) -> ReporterT m a -> ReporterT m b -> ReporterT m c Source # (*>) :: ReporterT m a -> ReporterT m b -> ReporterT m b Source # (<*) :: ReporterT m a -> ReporterT m b -> ReporterT m a Source # | |
MonadIO m => MonadIO (ReporterT m) Source # | IO actions can be embedded into reporters. If an IO error occurs, a fatal error is reported by the reporter instead.
IO errors do not have location information (see also |
Monad m => MonadReporter (ReporterT m) Source # | Reporters can be trivially promoted to any reporter transformer. |
Defined in FreeC.Monad.Reporter liftReporter :: Reporter a -> ReporterT m a Source # |
runReporterT :: Monad m => ReporterT m a -> m (Maybe a, [Message]) Source #
Runs the given reporter and returns the produced value as well as all
reported messages. If a fatal message has been reported the produced
value is Nothing
. The result is wrapped in the inner monad.
lift :: (MonadTrans t, Monad m) => m a -> t m a Source #
Lift a computation from the argument monad to the constructed monad.
hoist :: (Hoistable t, Monad m) => t Identity a -> t m a Source #
Lifts the transformed identity monad to any transformed monad.
Reporting Messages
class Monad r => MonadReporter r where Source #
Type class for all monads within which Message
s can be reported.
liftReporter :: Reporter a -> r a Source #
Promotes a reporter to r
.
Instances
Monad m => MonadReporter (ReporterT m) Source # | Reporters can be trivially promoted to any reporter transformer. |
Defined in FreeC.Monad.Reporter liftReporter :: Reporter a -> ReporterT m a Source # | |
Monad m => MonadReporter (ConverterT m) Source # | Promotes a reporter to a converter that produces the same result and ignores the environment. This type class instance allows |
Defined in FreeC.Monad.Converter liftReporter :: Reporter a -> ConverterT m a Source # |
report :: MonadReporter r => Message -> r () Source #
Creates a successful reporter that reports the given message.
reportFatal :: MonadReporter r => Message -> r a Source #
Creates a reporter that fails with the given message.
Reporting IO Errors
type ReporterIO = ReporterT IO Source #
A reporter with an IO action as its inner monad.
reportIOError :: MonadReporter r => IOError -> r a Source #
Reports the given IO error as a fatal error with no location information.
Handling Messages and Reporter Results
isFatal :: Reporter a -> Bool Source #
Tests whether a fatal error was reported to the given reporter.
Handling Reported Messages
reportTo :: MonadIO m => Handle -> ReporterT m a -> m (Maybe a) Source #
Runs the given reporter and prints all reported messages to the provided file handle.
If the inner monad of the reporter is an IO action, the IO action will
be executed before the messages are printed to the file handle.
To run an IO action after the messages have been reported, the reporter
needs to return the IO action (e.g. Reporter (IO ())
instead of
ReporterIO ()
). It is possible to combine both approaches (i.e. run an
IO action before the messages are printed and another action afterwards)
by using ReporterIO (IO ())
. In the latter case this function returns
a value of type IO (IO ())
. Thus an additional join
is needed:
join (reportTo h reporter)
.