-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Explicit Sharing of Monadic Effects -- -- This package implements a monad transformer for sharing monadic -- effects. @package explicit-sharing @version 0.6 -- | Automatic deriving of monadic data types and corresponding instances. module Data.Monadic.Derive -- | Derives monadic datatypes and instances for explicit sharing as well -- as conversion. Combines the other three derivations which provide the -- same functionality split into different parts. -- -- You usually need the following preamble when deriving monadic code: -- --
-- {-# LANGUAGE TemplateHaskell
-- , KindSignatures
-- , MultiParamTypeClasses
-- , FlexibleInstances
-- #-}
-- import Control.Monad.Sharing
-- import Data.Monadic.Derive
-- import Data.DeriveTH
--
--
-- If your types contain lists, you also need to
--
-- -- import Data.Monadic.List ---- -- With this prerequisites, you can derive a monadic Maybe type by -- --
-- $(derive monadic ''Maybe) --monadic :: Derivation -- | Generates a monadic datatype and corresponding con- and destructor -- functions from a Haskell datatype. -- -- For example, the datatype -- --
-- data Maybe a = Nothing | Just a ---- -- can be translated into its monadic counterpart by typing -- --
-- $(derive makeMData ''Maybe) ---- -- This call generates the following datatype -- --
-- data MMaybe m a = MNothing | MJust (m a) ---- -- and the following auxiliary functions for constructing and matching -- monadic values: -- --
-- mNothing :: Monad m => m (MMaybe m a) -- mNothing = return MNothing ---- --
-- mJust :: Monad m => m a -> m (MMaybe m a) -- mJust a = return (MJust a) ---- --
-- matchMMaybe :: Monad m => m (MMaybe m a) -> m b -> (m a -> m b) -> m b
-- matchMMaybe x n j = x >>= \x -> case x of { MNothing -> n; MJust a -> j a }
--
makeMData :: Derivation
-- | Generates a Shareable instance for a monadic datatype.
--
-- For example the call
--
-- -- $(derive makeShareable ''Maybe) ---- -- generates the following instance: -- --
-- instance (Monad m, Shareable m a) => Shareable (Maybe m a) where -- shareArgs fun MNothing = return MNothing -- shareArgs fun (MJust a) = fun a >>= \a -> mJust a --makeShareable :: Derivation -- | Generates Convertible instances to convert between monadic -- and non-monadic datatypes. -- -- For example, the call -- --
-- $(derive makeConvertible ''Maybe) ---- -- generates the following instances: -- --
-- instance (Monad m, Convertible m a a') -- => Convertible m (Maybe a) (MMaybe m a') where -- convArgs fun Nothing = mNothing -- convArgs fun (Just a) = mJust (fun a) -- -- instance (Monad m, Convertible m a' a) -- => Convertible m (MMaybe m a') (Maybe a) where -- convArgs fun MNothing = return Nothing -- convArgs fun (MJust a) = (a >>= fun) >>= \a -> return (Just a) --makeConvertible :: Derivation -- | This library provides type classes for explicit sharing of monadic -- effects. Usually you don't need to import this library as it is -- reexported by the module Control.Monad.Sharing. You may want -- to do so, however, when writing your own implementation of explicit -- sharing. module Control.Monad.Sharing.Classes -- | Interface of monads that support explicit sharing. class Sharing m share :: (Sharing m, Shareable m a) => m a -> m (m a) -- | Interface of shareable nested monadic data types. The provided -- function shareArgs is supposed to map the given function on -- every monadic argument. -- -- We provide instances of the Shareable class for some predefined -- Haskell types. For flat types the function shareArgs just -- returns its argument which has no arguments to which the given -- function could be applied. class Shareable m a shareArgs :: (Shareable m a, Monad n) => (forall b. (Shareable m b) => m b -> n (m b)) -> a -> n a -- | Interface for convertible datatypes. The provided function -- convArgs is supposed to map the given function on every -- argument of the given value and combine the results to give the -- converted value. -- -- We provide instances of the Convertible class for some -- predefined Haskell types. For flat types the function convArgs -- just returns its argument which has no arguments to which the given -- function could be applied. class Convertible m a b convArgs :: (Convertible m a b) => (forall c d. (Convertible m c d) => c -> m d) -> a -> m b -- | Converts a convertible value recursively. convert :: (Convertible m a b) => a -> m b type MInt m = Int type MChar m = Char type MBool m = Bool instance (Monad m, Convertible m a b) => Convertible m [m a] [b] instance (Monad m, Convertible m a b) => Convertible m [a] [m b] instance (Monad m) => Convertible m [Char] [Char] instance (Monad m) => Convertible m [Int] [Int] instance (Monad m) => Convertible m [Bool] [Bool] instance (Monad m) => Convertible m Char Char instance (Monad m) => Convertible m Int Int instance (Monad m) => Convertible m Bool Bool instance (Monad m, Shareable m a) => Shareable m [m a] instance (Monad m) => Shareable m (a -> b) instance (Monad m) => Shareable m [Char] instance (Monad m) => Shareable m [Int] instance (Monad m) => Shareable m [Bool] instance (Monad m) => Shareable m Char instance (Monad m) => Shareable m Int instance (Monad m) => Shareable m Bool -- | This library provides an interface to monads that support explicit -- sharing based on two-level types. This implementation is not as -- efficient as the default implementation but supports a restricted form -- of sharing across non-determinism if a first-order data type is used -- as underlying monad. module Control.Monad.Sharing.FirstOrder -- | Interface of monads that support explicit sharing. class Sharing m share :: (Sharing m, Shareable m a) => m a -> m (m a) -- | Interface of shareable nested monadic data types. The provided -- function shareArgs is supposed to map the given function on -- every monadic argument. -- -- We provide instances of the Shareable class for some predefined -- Haskell types. For flat types the function shareArgs just -- returns its argument which has no arguments to which the given -- function could be applied. class Shareable m a shareArgs :: (Shareable m a, Monad n) => (forall b. (Shareable m b) => m b -> n (m b)) -> a -> n a -- | Interface for convertible datatypes. The provided function -- convArgs is supposed to map the given function on every -- argument of the given value and combine the results to give the -- converted value. -- -- We provide instances of the Convertible class for some -- predefined Haskell types. For flat types the function convArgs -- just returns its argument which has no arguments to which the given -- function could be applied. class Convertible m a b convArgs :: (Convertible m a b) => (forall c d. (Convertible m c d) => c -> m d) -> a -> m b -- | Converts a convertible value recursively. convert :: (Convertible m a b) => a -> m b -- | Monad transformer that adds explicit sharing capability to every -- monad. data Lazy m a -- | Lifts all monadic effects to the top-level and unwraps the monad -- transformer for explicit sharing. evalLazy :: (Monad m, Shareable (Lazy m) a, Convertible (Lazy m) a b) => Lazy m a -> m b -- | This library provides lists with monadic head and tail as an example -- for nested monadic data that can be used with the combinator -- share for explicit sharing. module Data.Monadic.List -- | Data type for lists where both the head and tail are monadic. data List m a Nil :: List m a Cons :: (m a) -> (m (List m a)) -> List m a -- | The empty monadic list. nil :: (Monad m) => m (List m a) -- | Constructs a non-empty monadic list. cons :: (Monad m) => m a -> m (List m a) -> m (List m a) -- | Checks if monadic list is empty. isEmpty :: (Monad m) => m (List m a) -> m Bool -- | Yields the head of a monadic list. Relies on MonadPlus instance -- to provide a failing implementation of fail. first :: (MonadPlus m) => m (List m a) -> m a -- | Yields the tail of a monadic list. Relies on MonadPlus instance -- to provide a failing implementation of fail. rest :: (MonadPlus m) => m (List m a) -> m (List m a) instance (Monad m, Convertible m a b) => Convertible m (List m a) [b] instance (Monad m, Convertible m a b) => Convertible m [a] (List m b) instance (Monad m, Shareable m a) => Shareable m (List m a) -- | This library provides an interface to monads that support explicit -- sharing. A project website with tutorials can be found at -- http://sebfisch.github.com/explicit-sharing. module Control.Monad.Sharing -- | Interface of monads that support explicit sharing. class Sharing m share :: (Sharing m, Shareable m a) => m a -> m (m a) -- | Interface of shareable nested monadic data types. The provided -- function shareArgs is supposed to map the given function on -- every monadic argument. -- -- We provide instances of the Shareable class for some predefined -- Haskell types. For flat types the function shareArgs just -- returns its argument which has no arguments to which the given -- function could be applied. class Shareable m a shareArgs :: (Shareable m a, Monad n) => (forall b. (Shareable m b) => m b -> n (m b)) -> a -> n a -- | Interface for convertible datatypes. The provided function -- convArgs is supposed to map the given function on every -- argument of the given value and combine the results to give the -- converted value. -- -- We provide instances of the Convertible class for some -- predefined Haskell types. For flat types the function convArgs -- just returns its argument which has no arguments to which the given -- function could be applied. class Convertible m a b convArgs :: (Convertible m a b) => (forall c d. (Convertible m c d) => c -> m d) -> a -> m b -- | Converts a convertible value recursively. convert :: (Convertible m a b) => a -> m b -- | Continuation-based, store-passing implementation of explicit sharing. -- It is an inlined version of ContT (ReaderT Store m) where the -- result type of continuations is polymorphic. data Lazy m a -- | Lifts all monadic effects to the top-level and unwraps the monad -- transformer for explicit sharing. evalLazy :: (Monad m, Convertible (Lazy m) a b) => Lazy m a -> m b