contstuff 0.7.0 → 1.0.0
raw patch · 7 files changed
+244/−232 lines, 7 filesdep +transformers
Dependencies added: transformers
Files
- Control/ContStuff.hs +5/−1
- Control/ContStuff/Classes.hs +8/−59
- Control/ContStuff/Instances.hs +3/−39
- Control/ContStuff/Monads.hs +15/−14
- Control/ContStuff/Simple.hs +130/−0
- Control/ContStuff/Trans.hs +59/−99
- contstuff.cabal +24/−20
Control/ContStuff.hs view
@@ -15,7 +15,9 @@ -- * Convenience reexports module Control.Applicative,- module Control.Monad+ module Control.Monad,+ module Control.Monad.IO.Class,+ module Control.Monad.Trans.Class ) where @@ -26,3 +28,5 @@ import Control.ContStuff.Monads import Control.ContStuff.Trans import Control.Monad+import Control.Monad.IO.Class+import Control.Monad.Trans.Class
Control/ContStuff/Classes.hs view
@@ -17,16 +17,12 @@ -- ** Call with current continuation CallCC(..), Label, labelCC, goto, -- ** Exceptions- HasExceptions(..), bracket, bracket_, catch, finally, forbid,- handle, raiseUnless, raiseWhen, require,- -- ** Lifting- Transformer(..),- LiftBase(..), io,- -- ** Running- Runnable(..),+ HasExceptions(..),+ bracket, bracket_, catch, finally, forbid, handle, raiseUnless,+ raiseWhen, require, -- ** State- Stateful(..), getField, modify, modifyField, modifyFieldLazy,- modifyLazy,+ Stateful(..),+ getField, modify, modifyField, modifyFieldLazy, modifyLazy, -- ** Logging support (writers) Writable(..) )@@ -34,6 +30,7 @@ import Control.Applicative import Control.Monad+import Control.Monad.Trans.Class import Prelude hiding (catch) @@ -146,7 +143,7 @@ forbid :: ( Exception (t m) ~ (), HasExceptions (t m),- Monad m, Monad (t m), Transformer t ) =>+ Monad m, Monad (t m), MonadTrans t ) => m Bool -> t m () forbid = raiseWhen () . lift @@ -174,46 +171,11 @@ require :: ( Exception (t m) ~ (), HasExceptions (t m),- Monad m, Monad (t m), Transformer t ) =>+ Monad m, Monad (t m), MonadTrans t ) => m Bool -> t m () require = raiseUnless () . lift ----------------- Lifting -------------------- | Monads, which support lifting base monad computations.--class LiftBase m where- -- | Base monad of @m@.- type Base m :: * -> *-- -- | Promote a base monad computation.- base :: Base m a -> m a----- | Handy alias for lifting 'IO' computations.--io :: (LiftBase m, Base m ~ IO) => Base m a -> m a-io = base------------------- Running -------------------- | Every monad transformer @t@ that supports transforming @t m a@ to--- @m a@ can be an instance of this class.--class Runnable t r m a where- -- | Arguments needed to run.- type Argument t r m a-- -- | Run the transformer.- runT :: Argument t r m a -> t m a -> m r-- ----------- -- State -- -----------@@ -267,19 +229,6 @@ modifyLazy :: (Monad m, Stateful m) => (StateOf m -> StateOf m) -> m () modifyLazy f = liftM f get >>= putLazy-------------------------------- Monad transformation --------------------------------- | The monad transformer class. Lifting computations one level down--- the monad stack, or stated differently promoting a computation of the--- underlying monad to the transformer.--class Transformer t where- -- | Promote a monadic computation to the transformer.- lift :: Monad m => m a -> t m a -------------
Control/ContStuff/Instances.hs view
@@ -14,7 +14,7 @@ import qualified Control.Exception as E import Control.ContStuff.Classes import Control.ContStuff.Trans-import Control.Monad.ST+import Control.Monad.Trans.Class ----------------@@ -38,42 +38,6 @@ ----------------- LiftBase -------------------instance LiftBase Id where type Base Id = Id; base = id-instance LiftBase IO where type Base IO = IO; base = id-instance LiftBase Maybe where type Base Maybe = Maybe; base = id-instance LiftBase (ST s) where type Base (ST s) = ST s; base = id-instance LiftBase [] where type Base [] = []; base = id-instance LiftBase ((->) r) where type Base ((->) r) = (->) r; base = id--instance (LiftBase m, Monad m) => LiftBase (ChoiceT r i m) where- type Base (ChoiceT r i m) = Base m- base = lift . base--instance (LiftBase m, Monad m) => LiftBase (ContT r m) where- type Base (ContT r m) = Base m- base = lift . base--instance (LiftBase m, Monad m) => LiftBase (EitherT r e m) where- type Base (EitherT r e m) = Base m- base = lift . base--instance (LiftBase m, Monad m) => LiftBase (IdT m) where- type Base (IdT m) = Base m- base = lift . base--instance (LiftBase m, Monad m) => LiftBase (MaybeT r m) where- type Base (MaybeT r m) = Base m- base = lift . base--instance (LiftBase m, Monad m) => LiftBase (StateT r s m) where- type Base (StateT r s m) = Base m- base = lift . base----------------- -- Stateful -- -------------- @@ -95,8 +59,8 @@ put = lift . put putLazy = lift . putLazy -instance (Monad m, Stateful m) => Stateful (IdT m) where- type StateOf (IdT m) = StateOf m+instance (Monad m, Stateful m) => Stateful (IdentityT m) where+ type StateOf (IdentityT m) = StateOf m get = lift get put = lift . put putLazy = lift . putLazy
Control/ContStuff/Monads.hs view
@@ -23,6 +23,7 @@ import Control.Applicative import Control.ContStuff.Trans+import Data.Functor.Identity import Data.Monoid @@ -32,19 +33,19 @@ -- | The choice monad. Derived from 'ChoiceT'. -type Choice r i a = ChoiceT r i Id a+type Choice r i a = ChoiceT r i Identity a -- | Get list of solutions. listChoice :: Choice [a] [a] a -> [a]-listChoice = getId . listChoiceT+listChoice = runIdentity . listChoiceT -- | Get one solution. maybeChoice :: Choice (Maybe a) (Maybe a) a -> Maybe a-maybeChoice = getId . maybeChoiceT+maybeChoice = runIdentity . maybeChoiceT ----------@@ -53,19 +54,19 @@ -- | Pure CPS monad derived from ContT. -type Cont r a = ContT r Id a+type Cont r a = ContT r Identity a -- | Run a pure CPS computation. runCont :: (a -> r) -> Cont r a -> r-runCont k (ContT c) = getId $ c (Id . k)+runCont k (ContT c) = runIdentity $ c (Identity . k) -- | Evaluate a pure CPS computation to its final result. evalCont :: Cont r r -> r-evalCont (ContT c) = getId $ c pure+evalCont (ContT c) = runIdentity $ c pure -- | Modify the result of a CPS computation along the way.@@ -80,25 +81,25 @@ -- | The traditional writer monad. -type OldWriter r w a = ContT (r, w) Id a+type OldWriter r w a = ContT (r, w) Identity a -- | Run a traditional writer computation. runOldWriter :: Monoid w => OldWriter r w r -> (r, w)-runOldWriter = getId . runOldWriterT+runOldWriter = runIdentity . runOldWriterT -- | Run a traditional writer computation and return its result. evalOldWriter :: Monoid w => OldWriter r w r -> r-evalOldWriter = fst . getId . runOldWriterT+evalOldWriter = fst . runIdentity . runOldWriterT -- | Run a traditional writer computation and return its log. execOldWriter :: Monoid w => OldWriter r w r -> w-execOldWriter = snd . getId . runOldWriterT+execOldWriter = snd . runIdentity . runOldWriterT -----------@@ -107,22 +108,22 @@ -- | Pure state monad derived from StateT. -type State r s a = StateT r s Id a+type State r s a = StateT r s Identity a -- | Run a stateful computation. runState :: s -> (s -> a -> r) -> State r s a -> r-runState s0 k c = getId $ runStateT s0 (\s1 -> Id . k s1) c+runState s0 k c = runIdentity $ runStateT s0 (\s1 -> Identity . k s1) c -- | Run a stateful computation returning its result. evalState :: s -> State r s r -> r-evalState = (getId .) . evalStateT+evalState = (runIdentity .) . evalStateT -- | Run a stateful computation returning its result. execState :: s -> State s s a -> s-execState = (getId .) . execStateT+execState = (runIdentity .) . execStateT
+ Control/ContStuff/Simple.hs view
@@ -0,0 +1,130 @@+-- |+-- Module: Control.ContStuff.Simple+-- Copyright: (c) 2010 Ertugrul Soeylemez+-- License: BSD3+-- Maintainer: Ertugrul Soeylemez <es@ertes.de>+-- Stability: experimental+--+-- This module provides all the transformers from+-- "Control.ContStuff.Trans", but with a simplified interface, hiding+-- the underlying CPS machinery.++{-# LANGUAGE RankNTypes, TypeFamilies #-}++module Control.ContStuff.Simple+ ( -- * Choice/nondeterminism+ ChoiceT,+ choice, findAll, findAll_, findFirst, findFirst_,+ T.listA, listChoiceT, maybeChoiceT,++ -- * Exceptions+ EitherT,+ evalEitherT, testEitherT,++ MaybeT,+ evalMaybeT, testMaybeT,++ -- * State+ StateT,+ evalStateT, execStateT,++ -- * Writer+ WriterT,+ runWriterT, evalWriterT, execWriterT,++ -- * Reexports+ module Control.Applicative,+ module Control.ContStuff.Classes,+ module Control.Monad+ )+ where++import qualified Control.ContStuff.Trans as T+import Control.Applicative+import Control.ContStuff.Classes+import Control.ContStuff.Instances ()+import Control.Monad+import Data.Monoid+++-------------+-- ChoiceT --+-------------++type ChoiceT m a = forall r i. T.ChoiceT r i m a++choice :: [a] -> ChoiceT m a+choice xs = T.choice xs++findAll :: (Alternative f, Applicative m) => ChoiceT m a -> m (f a)+findAll c = T.findAll c++findAll_ :: Applicative m => ChoiceT m a -> m ()+findAll_ c = T.findAll_ c++findFirst :: (Alternative f, Applicative m) => ChoiceT m a -> m (f a)+findFirst c = T.findFirst c++findFirst_ :: Applicative m => ChoiceT m a -> m ()+findFirst_ c = T.findFirst_ c++listChoiceT :: Applicative m => ChoiceT m a -> m [a]+listChoiceT c = T.listChoiceT c++maybeChoiceT :: Applicative m => ChoiceT m a -> m (Maybe a)+maybeChoiceT c = T.maybeChoiceT c+++-------------+-- EitherT --+-------------++type EitherT e m a = forall r. T.EitherT r e m a++evalEitherT :: Applicative m => EitherT e m a -> m (Either e a)+evalEitherT c = T.evalEitherT c++testEitherT :: Applicative m => EitherT e m a -> m Bool+testEitherT c = T.testEitherT c+++------------+-- MaybeT --+------------++type MaybeT m a = forall r. T.MaybeT r m a++evalMaybeT :: Applicative m => MaybeT m a -> m (Maybe a)+evalMaybeT c = T.evalMaybeT c++testMaybeT :: Applicative m => MaybeT m a -> m Bool+testMaybeT c = T.testMaybeT c+++------------+-- StateT --+------------++type StateT s m a = forall r. T.StateT r s m a++evalStateT :: Applicative m => s -> StateT s m a -> m a+evalStateT s0 c = T.evalStateT s0 c++execStateT :: Applicative m => s -> StateT s m a -> m s+execStateT s0 c = T.execStateT s0 c+++-------------+-- WriterT --+-------------++type WriterT w m a = forall r. T.OldWriterT r w m a++runWriterT :: (Applicative m, Monoid w) => WriterT w m a -> m (a, w)+runWriterT c = T.runOldWriterT c++evalWriterT :: (Applicative m, Monoid w) => WriterT w m a -> m a+evalWriterT c = fmap fst . T.runOldWriterT $ c++execWriterT :: (Applicative m, Monoid w) => WriterT w m a -> m w+execWriterT c = fmap snd . T.runOldWriterT $ c
Control/ContStuff/Trans.hs view
@@ -11,25 +11,36 @@ {-# LANGUAGE FlexibleInstances, MultiParamTypeClasses, TypeFamilies #-} module Control.ContStuff.Trans- ( -- * The identity monad- Id(..),-- -- * Monad transformers+ ( -- * Monad transformers -- ** Identity transformer- IdT(..),+ IdentityT(..),+ -- ** ContT- ContT(..), runContT, evalContT, modifyContT,+ ContT(..),+ runContT, evalContT, modifyContT,+ -- ** Choice/nondeterminism- ChoiceT(..), runChoiceT, choice, findAll, findAll_, findFirst,- findFirst_, listA, listChoiceT, maybeChoiceT,+ ChoiceT(..),+ runChoiceT, choice, findAll, findAll_, findFirst,+ findFirst_, listA, listChoiceT, maybeChoiceT,+ -- ** Exceptions- EitherT(..), runEitherT, evalEitherT, modifyEitherT,- MaybeT(..), runMaybeT, evalMaybeT, modifyMaybeT,+ EitherT(..),+ runEitherT, evalEitherT, modifyEitherT, testEitherT,++ MaybeT(..),+ runMaybeT, evalMaybeT, modifyMaybeT, testMaybeT,+ -- ** State- StateT(..), runStateT, evalStateT, execStateT,+ StateT(..),+ runStateT, evalStateT, execStateT,+ -- ** Writer monads- WriterT, runWriterT,- OldWriterT, runOldWriterT, evalOldWriterT, execOldWriterT+ WriterT,+ runWriterT,++ OldWriterT,+ runOldWriterT, evalOldWriterT, execOldWriterT ) where @@ -37,37 +48,12 @@ import Control.Arrow import Control.ContStuff.Classes import Control.Monad-import Control.Monad.Fix+import Control.Monad.IO.Class+import Control.Monad.Trans.Class+import Control.Monad.Trans.Identity import Data.Monoid ---------------------------- The identity monad ------------------------------- | The identity monad. This monad represents values themselves,--- i.e. computations without effects.--newtype Id a = Id { getId :: a }--instance Functor Id where- fmap f (Id x) = Id (f x)--instance Applicative Id where- pure = Id- Id f <*> Id x = Id (f x)--instance Monad Id where- return = Id- Id x >>= f = f x--instance MonadFix Id where- mfix f = fix (f . getId)--instance Show a => Show (Id a) where- show x = "Id " ++ show x-- ------------- -- ChoiceT -- -------------@@ -110,11 +96,14 @@ ChoiceT $ \fold z k -> c (\x y kc -> getChoiceT (f y) fold x kc) z k +instance MonadIO m => MonadIO (ChoiceT r i m) where+ liftIO = lift . liftIO+ instance MonadPlus (ChoiceT r i m) where mzero = empty mplus = (<|>) -instance Transformer (ChoiceT r i) where+instance MonadTrans (ChoiceT r i) where lift c = ChoiceT $ \fold z k -> c >>= \x -> fold z x k @@ -219,11 +208,10 @@ mzero = empty mplus = (<|>) -instance Runnable (ContT r) r m a where- type Argument (ContT r) r m a = a -> m r- runT k (ContT c) = c k+instance MonadIO m => MonadIO (ContT r m) where+ liftIO = lift . liftIO -instance Transformer (ContT r) where+instance MonadTrans (ContT r) where lift c = ContT $ \k -> c >>= k instance Alternative m => Writable (ContT r m) r where@@ -295,15 +283,14 @@ EitherT $ \k expk -> c (\x -> getEitherT (f x) k expk) expk +instance MonadIO m => MonadIO (EitherT r e m) where+ liftIO = lift . liftIO+ instance Alternative m => MonadPlus (EitherT r e m) where mzero = empty mplus = (<|>) -instance Runnable (EitherT r e) r m a where- type Argument (EitherT r e) r m a = (a -> m r, e -> m r)- runT (k, expk) (EitherT c) = c k expk--instance Transformer (EitherT r e) where+instance MonadTrans (EitherT r e) where lift c = EitherT $ \k _ -> c >>= k instance Alternative m => Writable (EitherT r e m) r where@@ -331,45 +318,13 @@ modifyEitherT f = EitherT $ \k _ -> fmap f (k ()) ------------- IdT ---------------- | The identity monad transformer. This monad transformer represents--- computations themselves without further side effects. Unlike most--- other monad transformers in this module it is not implemented in--- terms of continuation passing style.--newtype IdT m a = IdT { getIdT :: m a }--instance Alternative m => Alternative (IdT m) where- empty = IdT empty- IdT c <|> IdT d = IdT (c <|> d)--instance Applicative m => Applicative (IdT m) where- pure = IdT . pure- IdT cf <*> IdT cx = IdT $ cf <*> cx--instance Functor m => Functor (IdT m) where- fmap f (IdT c) = IdT (fmap f c)--instance Monad m => Monad (IdT m) where- return = IdT . return- IdT c >>= f = IdT $ c >>= getIdT . f--instance (Alternative m, Monad m) => MonadPlus (IdT m) where- mzero = empty- mplus = (<|>)--instance MonadFix m => MonadFix (IdT m) where- mfix f = IdT $ mfix (getIdT . f)--instance Runnable IdT r m r where- type Argument IdT r m r = ()- runT _ (IdT c) = c+-- | Run the 'EitherT' computation and return 'True', if it results in a+-- right value, 'False' otherwise. -instance Transformer IdT where- lift = IdT+testEitherT :: Applicative m => EitherT Bool e m a -> m Bool+testEitherT =+ let pc x = pure . const x+ in runEitherT (pc True) (pc False) ------------@@ -417,15 +372,14 @@ MaybeT $ \just noth -> c (\x -> getMaybeT (f x) just noth) noth +instance MonadIO m => MonadIO (MaybeT r m) where+ liftIO = lift . liftIO+ instance Alternative m => MonadPlus (MaybeT r m) where mzero = empty mplus = (<|>) -instance Runnable (MaybeT r) r m a where- type Argument (MaybeT r) r m a = (a -> m r, m r)- runT (just, noth) (MaybeT c) = c just noth--instance Transformer (MaybeT r) where+instance MonadTrans (MaybeT r) where lift c = MaybeT $ \just _ -> c >>= just instance Alternative m => Writable (MaybeT r m) r where@@ -453,6 +407,13 @@ modifyMaybeT f = MaybeT $ \just _ -> fmap f (just ()) +-- | Run the 'MaybeT' computation and return 'True', if it results in a+-- Just value, 'False' otherwise.++testMaybeT :: Applicative m => MaybeT Bool m a -> m Bool+testMaybeT = runMaybeT (pure . const True) (pure False)++ ---------------- -- OldWriterT -- ----------------@@ -517,21 +478,20 @@ StateT c >>= f = StateT $ \s0 k -> c s0 (\s1 x -> getStateT (f x) s1 k) +instance MonadIO m => MonadIO (StateT r s m) where+ liftIO = lift . liftIO+ instance Alternative m => MonadPlus (StateT r s m) where mzero = empty mplus = (<|>) -instance Runnable (StateT r s) r m a where- type Argument (StateT r s) r m a = (s, s -> a -> m r)- runT (s0, k) (StateT c) = c s0 k- instance Stateful (StateT r s m) where type StateOf (StateT r s m) = s get = StateT $ \s0 k -> k s0 s0 put s1 = s1 `seq` StateT $ \_ k -> k s1 () putLazy s1 = StateT $ \_ k -> k s1 () -instance Transformer (StateT r s) where+instance MonadTrans (StateT r s) where lift c = StateT $ \s0 k -> c >>= k s0 instance Alternative m => Writable (StateT r s m) r where
contstuff.cabal view
@@ -1,7 +1,7 @@ Name: contstuff-Version: 0.7.0+Version: 1.0.0 Category: Control, Monads-Synopsis: Fast, easy to use CPS-based monads+Synopsis: Fast, easy to use CPS-based monad transformers Maintainer: Ertugrul Söylemez <es@ertes.de> Author: Ertugrul Söylemez <es@ertes.de> Copyright: (c) 2010 Ertugrul Söylemez@@ -13,23 +13,27 @@ Cabal-version: >= 1.6 Description: - This library implements fast and easy to use CPS-based monad- transformers. Most of the usual monad transformers are implemented,- including ChoiceT, ContT, EitherT, MaybeT and StateT. Because of the- design of this library, many other monad transformers are just special- cases of those, including e.g. WriterT.+ This library implements fast and easy to use CPS-based monad+ transformers. Most of the usual monad transformers are implemented,+ including ChoiceT, ContT, EitherT, MaybeT and StateT. Because of+ the design of this library, many other monad transformers are just+ special cases of those, including e.g. WriterT. + The Control.ContStuff.Simple module also provides simplified monad+ transformer wrappers, which hide the underlying CPS, so you get the+ full performance, but with a simplified interface. Currently the+ simplified monad transformers are implemented as type synonyms, so+ their flexibility is slightly limited.+ Library- Build-depends:- base >= 4 && <= 5- GHC-Options: -W- Extensions:- FlexibleInstances,- MultiParamTypeClasses,- TypeFamilies- Exposed-modules:- Control.ContStuff- Control.ContStuff.Classes- Control.ContStuff.Instances- Control.ContStuff.Monads- Control.ContStuff.Trans+ Build-depends:+ base >= 4 && <= 5,+ transformers >= 0.2.2.0+ GHC-Options: -W+ Exposed-modules:+ Control.ContStuff+ Control.ContStuff.Classes+ Control.ContStuff.Instances+ Control.ContStuff.Monads+ Control.ContStuff.Simple+ Control.ContStuff.Trans