free-compiler-0.3.0.0: A Haskell to Coq compiler.

Safe HaskellSafe
LanguageHaskell2010

FreeC.Monad.Reporter

Contents

Description

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

Messages

data Message Source #

A message reported by the compiler.

Instances
Eq Message Source # 
Instance details

Defined in FreeC.Monad.Reporter

Show Message Source # 
Instance details

Defined in FreeC.Monad.Reporter

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.

Instance details

Defined in FreeC.Monad.Reporter

Methods

pretty :: Message -> Doc #

prettyList :: [Message] -> Doc #

data Severity Source #

The severity of a message reported by the compiler.

Constructors

Internal 
Error 
Warning 
Info 
Debug 
Instances
Eq Severity Source # 
Instance details

Defined in FreeC.Monad.Reporter

Show Severity Source # 
Instance details

Defined in FreeC.Monad.Reporter

Pretty Severity Source #

Pretty instance for message severity levels.

Instance details

Defined in FreeC.Monad.Reporter

Methods

pretty :: Severity -> Doc #

prettyList :: [Severity] -> Doc #

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

data ReporterT m a Source #

A reporter monad parameterized by the inner monad m.

Instances
MonadTrans ReporterT Source #

MonadTrans instance for ReporterT.

Instance details

Defined in FreeC.Monad.Reporter

Methods

lift :: Monad m => m a -> ReporterT m a Source #

UnHoistable ReporterT Source #

hoist can be undone for reporters.

Instance details

Defined in FreeC.Monad.Reporter

Methods

unhoist :: Monad m => ReporterT m a -> m (ReporterT Identity a) Source #

Hoistable ReporterT Source #

The reporter monad can be lifted to any reporter transformer.

Instance details

Defined in FreeC.Monad.Reporter

Methods

hoist :: Monad m => ReporterT Identity a -> ReporterT m a Source #

Monad m => Monad (ReporterT m) Source #

The Monad instance for ReporterT.

Instance details

Defined in FreeC.Monad.Reporter

Methods

(>>=) :: ReporterT m a -> (a -> ReporterT m b) -> ReporterT m b Source #

(>>) :: ReporterT m a -> ReporterT m b -> ReporterT m b Source #

return :: a -> ReporterT m a Source #

fail :: String -> ReporterT m a Source #

Monad m => Functor (ReporterT m) Source #

The Functor instance for ReporterT is needed to define the Monad instance.

Instance details

Defined in FreeC.Monad.Reporter

Methods

fmap :: (a -> b) -> ReporterT m a -> ReporterT m b Source #

(<$) :: a -> ReporterT m b -> ReporterT m a Source #

Monad m => MonadFail (ReporterT m) Source #

Internal errors (e.g. pattern matching failures in do-blocks) are cause fatal error messages to be reported.

Instance details

Defined in FreeC.Monad.Reporter

Methods

fail :: String -> ReporterT m a Source #

Monad m => Applicative (ReporterT m) Source #

The Applicative instance for ReporterT is needed to define the Monad instance.

Instance details

Defined in FreeC.Monad.Reporter

Methods

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 reportIOError).

Instance details

Defined in FreeC.Monad.Reporter

Methods

liftIO :: IO a -> ReporterT m a Source #

Monad m => MonadReporter (ReporterT m) Source #

Reporters can be trivially promoted to any reporter transformer.

Instance details

Defined in FreeC.Monad.Reporter

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.

unhoist :: (UnHoistable t, Monad m) => t m a -> m (t Identity a) Source #

Undoes hoist.

Reporting Messages

class Monad r => MonadReporter r where Source #

Type class for all monads within which Messages can be reported.

Methods

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.

Instance details

Defined in FreeC.Monad.Reporter

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 report and reportFatal to be used directly in do-blocks of the Converter monad without explicitly lifting reporters.

Instance details

Defined in FreeC.Monad.Converter

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.

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

Lift a computation from the IO 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.

messages :: Reporter a -> [Message] Source #

Gets the messages 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).

reportToOrExit :: MonadIO m => Handle -> ReporterT m a -> m a Source #

Runs the given reporter, prints all reported messages to stderr and exits the application if a fatal message has been reported.

See reportTo for usage information.