Safe Haskell | None |
---|---|
Language | Haskell2010 |
This module defines the MonadWarning
type class, which provides a common
interface for monads that support reporting warning messages without
aborting the computation (unlike with exceptions, Either
, or
MonadFail
, which either fail or return a value).
It also includes two functions that instantiate it into either a MonadIO
(runWarningIO
) or a StateT
monad with a [String]
as state
(runWarningStateT
) respectively.
In the case of MonadIO
, warnings are printed to stderr
.
In the case of StateT
, with a [String]
state, warnings are added to the
list in the state.
By using the MonadWarning
type class, users can write code that remains
agnostic to the specific monad in which it operates, and to easily change
it at a later stage if necessary.
Example usage:
computeWithWarning :: (MonadWarning m) => Int -> m Int
computeWithWarning x = do
when (x < 0) $ reportIssue "Input value is negative!"
return (x * 2)
-- Using IO
monad to perform computation and report warnings.
main :: IO ()
main = do
result <- runWarningIO $ computeWithWarning (-4)
putStrLn $ "Result: " ++ show result
Synopsis
- class Monad m => MonadWarning (m :: Type -> Type) where
- reportIssue :: String -> m ()
- data WarningIO (m :: Type -> Type) a
- data WarningStateT (m :: Type -> Type) a
- eitherToWarning :: MonadWarning m => a -> Either String a -> m a
- runWarningIO :: WarningIO m a -> m a
- runWarningStateT :: WarningStateT m a -> StateT [String] m a
Documentation
class Monad m => MonadWarning (m :: Type -> Type) where Source #
Type class for monads that support reporting warnings without aborting their execution in the process.
:: String | The warning message to report. |
-> m () | The action that reports the warning. |
Report a non-fatal issue.
Instances
MonadIO m => MonadWarning (WarningIO m) Source # | This instance prints the issue to |
Defined in Cardano.CLI.Types.MonadWarning reportIssue :: String -> WarningIO m () Source # | |
Monad m => MonadWarning (WarningStateT m) Source # | This instance adds the issue to the |
Defined in Cardano.CLI.Types.MonadWarning reportIssue :: String -> WarningStateT m () Source # |
data WarningIO (m :: Type -> Type) a Source #
Wrapper newtype for MonadIO
with MonadWarning
instance.
This type is not meant to be constructed directly but just to serve
as an instance of MonadWarning
that can be converted to MonadIO
.
It is only necessary in order to avoid overlapping instances.
Instances
MonadIO m => MonadIO (WarningIO m) Source # | |
Applicative m => Applicative (WarningIO m) Source # | |
Defined in Cardano.CLI.Types.MonadWarning pure :: a -> WarningIO m a Source # (<*>) :: WarningIO m (a -> b) -> WarningIO m a -> WarningIO m b Source # liftA2 :: (a -> b -> c) -> WarningIO m a -> WarningIO m b -> WarningIO m c Source # (*>) :: WarningIO m a -> WarningIO m b -> WarningIO m b Source # (<*) :: WarningIO m a -> WarningIO m b -> WarningIO m a Source # | |
Functor m => Functor (WarningIO m) Source # | |
Monad m => Monad (WarningIO m) Source # | |
MonadIO m => MonadWarning (WarningIO m) Source # | This instance prints the issue to |
Defined in Cardano.CLI.Types.MonadWarning reportIssue :: String -> WarningIO m () Source # |
data WarningStateT (m :: Type -> Type) a Source #
Wrapper newtype for StateT [String]
with MonadWarning
instance.
This type is not meant to be constructed directly but just to serve
as an instance of MonadWarning
that can be converted to StateT
.
It is only necessary in order to avoid overlapping instances.
Instances
eitherToWarning :: MonadWarning m => a -> Either String a -> m a Source #
runWarningIO :: WarningIO m a -> m a Source #
Interpret a MonadWarning
as a MonadIO
by reporting
warnings to stderr
.
runWarningStateT :: WarningStateT m a -> StateT [String] m a Source #
Interpret a MonadWarning
as a StateT [String]
monad,
by accumulating warnings into the state.