Safe Haskell | None |
---|---|
Language | Haskell2010 |
Module exporting typical type classes that are newtype-derived by Carriers
Synopsis
- class Applicative f => Alternative (f :: Type -> Type)
- class (Alternative m, Monad m) => MonadPlus (m :: Type -> Type)
- class Monad m => MonadFix (m :: Type -> Type)
- class Monad m => MonadFail (m :: Type -> Type)
- class Monad m => MonadIO (m :: Type -> Type)
- class Monad m => MonadThrow (m :: Type -> Type)
- class MonadThrow m => MonadCatch (m :: Type -> Type)
- class MonadCatch m => MonadMask (m :: Type -> Type)
- class (Applicative b, Applicative m, Monad b, Monad m) => MonadBase (b :: Type -> Type) (m :: Type -> Type) | m -> b
- class MonadBase b m => MonadBaseControl (b :: Type -> Type) (m :: Type -> Type) | m -> b
- class MonadTrans (t :: (Type -> Type) -> Type -> Type)
- class MonadTrans t => MonadTransControl (t :: (Type -> Type) -> Type -> Type)
- data IdentityT (f :: k -> Type) (a :: k)
Documentation
class Applicative f => Alternative (f :: Type -> Type) #
A monoid on applicative functors.
If defined, some
and many
should be the least solutions
of the equations:
Instances
class (Alternative m, Monad m) => MonadPlus (m :: Type -> Type) #
Monads that also support choice and failure.
Instances
class Monad m => MonadFix (m :: Type -> Type) #
Monads having fixed points with a 'knot-tying' semantics.
Instances of MonadFix
should satisfy the following laws:
- Purity
mfix
(return
. h) =return
(fix
h)- Left shrinking (or Tightening)
mfix
(\x -> a >>= \y -> f x y) = a >>= \y ->mfix
(\x -> f x y)- Sliding
, for strictmfix
(liftM
h . f) =liftM
h (mfix
(f . h))h
.- Nesting
mfix
(\x ->mfix
(\y -> f x y)) =mfix
(\x -> f x x)
This class is used in the translation of the recursive do
notation
supported by GHC and Hugs.
Instances
class Monad m => MonadFail (m :: Type -> Type) #
When a value is bound in do
-notation, the pattern on the left
hand side of <-
might not match. In this case, this class
provides a function to recover.
A Monad
without a MonadFail
instance may only be used in conjunction
with pattern that always match, such as newtypes, tuples, data types with
only a single data constructor, and irrefutable patterns (~pat
).
Instances of MonadFail
should satisfy the following law: fail s
should
be a left zero for >>=
,
fail s >>= f = fail s
If your Monad
is also MonadPlus
, a popular definition is
fail _ = mzero
Since: base-4.9.0.0
Instances
class Monad m => MonadIO (m :: Type -> Type) #
Monads in which IO
computations may be embedded.
Any monad built by applying a sequence of monad transformers to the
IO
monad will be an instance of this class.
Instances should satisfy the following laws, which state that liftIO
is a transformer of monads:
Instances
class Monad m => MonadThrow (m :: Type -> Type) #
A class for monads in which exceptions may be thrown.
Instances should obey the following law:
throwM e >> x = throwM e
In other words, throwing an exception short-circuits the rest of the monadic computation.
Instances
class MonadThrow m => MonadCatch (m :: Type -> Type) #
A class for monads which allow exceptions to be caught, in particular
exceptions which were thrown by throwM
.
Instances should obey the following law:
catch (throwM e) f = f e
Note that the ability to catch an exception does not guarantee that we can
deal with all possible exit points from a computation. Some monads, such as
continuation-based stacks, allow for more than just a success/failure
strategy, and therefore catch
cannot be used by those monads to properly
implement a function such as finally
. For more information, see
MonadMask
.
Instances
class MonadCatch m => MonadMask (m :: Type -> Type) #
A class for monads which provide for the ability to account for all possible exit points from a computation, and to mask asynchronous exceptions. Continuation-based monads are invalid instances of this class.
Instances should ensure that, in the following code:
fg = f `finally` g
The action g
is called regardless of what occurs within f
, including
async exceptions. Some monads allow f
to abort the computation via other
effects than throwing an exception. For simplicity, we will consider aborting
and throwing an exception to be two forms of "throwing an error".
If f
and g
both throw an error, the error thrown by fg
depends on which
errors we're talking about. In a monad transformer stack, the deeper layers
override the effects of the inner layers; for example, ExceptT e1 (Except
e2) a
represents a value of type Either e2 (Either e1 a)
, so throwing both
an e1
and an e2
will result in Left e2
. If f
and g
both throw an
error from the same layer, instances should ensure that the error from g
wins.
Effects other than throwing an error are also overriden by the deeper layers.
For example, StateT s Maybe a
represents a value of type s -> Maybe (a,
s)
, so if an error thrown from f
causes this function to return Nothing
,
any changes to the state which f
also performed will be erased. As a
result, g
will see the state as it was before f
. Once g
completes,
f
's error will be rethrown, so g
' state changes will be erased as well.
This is the normal interaction between effects in a monad transformer stack.
By contrast, lifted-base's
version of finally
always discards all of g
's non-IO effects, and g
never sees any of f
's non-IO effects, regardless of the layer ordering and
regardless of whether f
throws an error. This is not the result of
interacting effects, but a consequence of MonadBaseControl
's approach.
Instances
class (Applicative b, Applicative m, Monad b, Monad m) => MonadBase (b :: Type -> Type) (m :: Type -> Type) | m -> b #
Instances
class MonadBase b m => MonadBaseControl (b :: Type -> Type) (m :: Type -> Type) | m -> b #
Writing instances
The usual way to write a
instance for a transformer
stack over a base monad MonadBaseControl
B
is to write an instance MonadBaseControl B B
for the base monad, and MonadTransControl T
instances for every transformer
T
. Instances for
are then simply implemented using
MonadBaseControl
, ComposeSt
, defaultLiftBaseWith
.defaultRestoreM
Instances
class MonadTrans (t :: (Type -> Type) -> Type -> Type) #
The class of monad transformers. Instances should satisfy the
following laws, which state that lift
is a monad transformation:
Instances
class MonadTrans t => MonadTransControl (t :: (Type -> Type) -> Type -> Type) #
The MonadTransControl
type class is a stronger version of
:MonadTrans
Instances of
know how to MonadTrans
actions in the base monad to
the transformed monad. These lifted actions, however, are completely unaware
of the monadic state added by the transformer.lift
instances are aware of the monadic state of the
transformer and allow to save and restore this state.MonadTransControl
This allows to lift functions that have a monad transformer in both positive and negative position. Take, for example, the function
withFile :: FilePath -> IOMode -> (Handle -> IO r) -> IO r
instances can only lift the return type of the MonadTrans
withFile
function:
withFileLifted :: MonadTrans t => FilePath -> IOMode -> (Handle -> IO r) -> t IO r withFileLifted file mode action = lift (withFile file mode action)
However,
is not powerful enough to make MonadTrans
withFileLifted
accept a function that returns t IO
. The reason is that we need to take
away the transformer layer in order to pass the function to
.
withFile
allows us to do this:MonadTransControl
withFileLifted' :: (Monad (t IO), MonadTransControl t) => FilePath -> IOMode -> (Handle -> t IO r) -> t IO r withFileLifted' file mode action = liftWith (\run -> withFile file mode (run . action)) >>= restoreT . return
Instances
data IdentityT (f :: k -> Type) (a :: k) #
The trivial monad transformer, which maps a monad to an equivalent monad.