Copyright | 2019 Daniel YU |
---|---|
License | MIT |
Maintainer | leptonyu@gmail.com |
Stability | experimental |
Portability | portable |
Safe Haskell | None |
Language | Haskell2010 |
IoC Monad in Haskell.
- Motivation
Simplify to create an application in Haskell.
When we decide to create an application using Haskell.
We may need using configurations, loggers as basic functions.
If this application needs storages, caches, etc.,
then we have to weaving the management of connection of these facilities into the application.
Connections need to be created before and be destroyed after using them.
There is a common strategy to manage connections, that is using Cont
.
Then we can encapsulate the management of connections separately.
For example, we can write a database plugin Factory
m
cxt
DBConnection
,
which can manage the database connections in monad m
with context cxt
.
Context cxt
may be requested for getting configurations or logging functions.
When all the components of application are encapsulated by plugins, then running an application will be simplified.
- Factory
Factory
has an environment env
, which provides anything needs by the factory. component
is the production of
the factory, it will be used by other Factory
. Finally to build a complete Factory
m () (m ()), which can be boot
.
For example:
factory = do log <- logFactory conf <- confFactory within (log, conf) $ do a <- withFactory fst aFactory b <- withFactory snd bFactory polish AB{..} [ xFactory , yFactory ] >>> bootFactory
Synopsis
- data Factory m env component
- running :: env -> Factory m env c -> (c -> m ()) -> m ()
- boot :: Monad m => Factory m () (m ()) -> m ()
- withFactory :: (env' -> env) -> Factory m env component -> Factory m env' component
- within :: env -> Factory m env component -> Factory m env' component
- polish :: component -> [Factory m component component] -> Factory m env' component
- natTrans :: (n () -> m ()) -> (m () -> n ()) -> Factory n env component -> Factory m env component
- wrap :: ((c -> m ()) -> m ()) -> Factory m env c
- bracket :: MonadCatch m => m res -> (res -> m ()) -> Factory m env res
- offer :: Monad m => m a -> Factory m env a
- delay :: MonadCatch m => m () -> Factory m env ()
- (>>>) :: Category cat => cat a b -> cat b c -> cat a c
- (<<<) :: Category cat => cat b c -> cat a b -> cat a c
- (<>) :: Semigroup a => a -> a -> a
- class Monad m => MonadThrow (m :: Type -> Type) where
- class MonadThrow m => MonadCatch (m :: Type -> Type)
- class Monad m => MonadReader r (m :: Type -> Type) | m -> r where
- asks :: MonadReader r m => (r -> a) -> m a
- class Monad m => MonadIO (m :: Type -> Type) where
- lift :: (MonadTrans t, Monad m) => m a -> t m a
Definition
data Factory m env component Source #
Factory defines how to generate a component
under the environment env
in monad m
.
It is similar to IoC container in oop, env
will provide anything to be wanted to generate component
.
Instances
MonadReader env (Factory m env) Source # | |
Category (Factory m :: Type -> Type -> Type) Source # | |
Monad (Factory m env) Source # | |
Functor (Factory m env) Source # | |
Applicative (Factory m env) Source # | |
Defined in Boots.Factory pure :: a -> Factory m env a # (<*>) :: Factory m env (a -> b) -> Factory m env a -> Factory m env b # liftA2 :: (a -> b -> c) -> Factory m env a -> Factory m env b -> Factory m env c # (*>) :: Factory m env a -> Factory m env b -> Factory m env b # (<*) :: Factory m env a -> Factory m env b -> Factory m env a # | |
MonadIO m => MonadIO (Factory m env) Source # | |
Defined in Boots.Factory | |
MonadThrow m => MonadThrow (Factory m env) Source # | |
Defined in Boots.Factory | |
Monad m => MonadCont (Factory m env) Source # | |
Semigroup (Factory m env env) Source # | |
Monoid (Factory m env env) Source # | |
Run functions
boot :: Monad m => Factory m () (m ()) -> m () Source #
Run the application using a specified factory.
Factory Construction
With
withFactory :: (env' -> env) -> Factory m env component -> Factory m env' component Source #
Switch factory environment.
within :: env -> Factory m env component -> Factory m env' component Source #
Construct factory under env
, and adapt it to fit another env'
.
Polish
polish :: component -> [Factory m component component] -> Factory m env' component Source #
Polish component
by a sequence of Factory
, and construct a unified one.
Nature Transformation
natTrans :: (n () -> m ()) -> (m () -> n ()) -> Factory n env component -> Factory m env component Source #
Nature transform of one Factory
with monad n
into another with monad m
.
Resource
bracket :: MonadCatch m => m res -> (res -> m ()) -> Factory m env res Source #
Construct open-close resource into a Factory
.
delay :: MonadCatch m => m () -> Factory m env () Source #
Put a delay action into Factory
, it will run at close phase.
Reexport Function
Category Arrow
Monoid Join
Other
class Monad m => MonadThrow (m :: Type -> Type) where #
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.
throwM :: Exception e => e -> m a #
Throw an exception. Note that this throws when this action is run in
the monad m
, not when it is applied. It is a generalization of
Control.Exception's throwIO
.
Should satisfy the law:
throwM e >> f = throwM e
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
MonadCatch IO | |
MonadCatch STM | |
e ~ SomeException => MonadCatch (Either e) | Since: exceptions-0.8.3 |
MonadCatch m => MonadCatch (MaybeT m) | Catches exceptions from the base monad. |
MonadCatch m => MonadCatch (ListT m) | |
MonadCatch m => MonadCatch (ExceptT e m) | Catches exceptions from the base monad. |
MonadCatch m => MonadCatch (IdentityT m) | |
(Error e, MonadCatch m) => MonadCatch (ErrorT e m) | Catches exceptions from the base monad. |
MonadCatch m => MonadCatch (StateT s m) | |
MonadCatch m => MonadCatch (StateT s m) | |
(MonadCatch m, Monoid w) => MonadCatch (WriterT w m) | |
(MonadCatch m, Monoid w) => MonadCatch (WriterT w m) | |
MonadCatch m => MonadCatch (ReaderT r m) | |
(MonadCatch m, Monoid w) => MonadCatch (RWST r w s m) | |
(MonadCatch m, Monoid w) => MonadCatch (RWST r w s m) | |
class Monad m => MonadReader r (m :: Type -> Type) | m -> r where #
See examples in Control.Monad.Reader.
Note, the partially applied function type (->) r
is a simple reader monad.
See the instance
declaration below.
Retrieves the monad environment.
:: (r -> r) | The function to modify the environment. |
-> m a |
|
-> m a |
Executes a computation in a modified environment.
:: (r -> a) | The selector function to apply to the environment. |
-> m a |
Retrieves a function of the current environment.
Instances
MonadReader r m => MonadReader r (MaybeT m) | |
MonadReader r m => MonadReader r (ListT m) | |
MonadReader env (Factory m env) Source # | |
(Monoid w, MonadReader r m) => MonadReader r (WriterT w m) | |
(Monoid w, MonadReader r m) => MonadReader r (WriterT w m) | |
MonadReader r m => MonadReader r (StateT s m) | |
MonadReader r m => MonadReader r (StateT s m) | |
MonadReader r m => MonadReader r (IdentityT m) | |
MonadReader r m => MonadReader r (ExceptT e m) | Since: mtl-2.2 |
(Error e, MonadReader r m) => MonadReader r (ErrorT e m) | |
Monad m => MonadReader r (ReaderT r m) | |
MonadReader r ((->) r :: Type -> Type) | |
MonadReader r' m => MonadReader r' (ContT r m) | |
(Monad m, Monoid w) => MonadReader r (RWST r w s m) | |
(Monad m, Monoid w) => MonadReader r (RWST r w s m) | |
:: MonadReader r m | |
=> (r -> a) | The selector function to apply to the environment. |
-> m a |
Retrieves a function of the current environment.
class Monad m => MonadIO (m :: Type -> Type) where #
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
lift :: (MonadTrans t, Monad m) => m a -> t m a #
Lift a computation from the argument monad to the constructed monad.