transformers-0.3.0.0/0000755000000000000000000000000011732646061012571 5ustar0000000000000000transformers-0.3.0.0/LICENSE0000644000000000000000000000310711732646061013577 0ustar0000000000000000The Glasgow Haskell Compiler License Copyright 2004, The University Court of the University of Glasgow. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - Neither name of the University nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY COURT OF THE UNIVERSITY OF GLASGOW AND THE CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY COURT OF THE UNIVERSITY OF GLASGOW OR THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. transformers-0.3.0.0/Setup.hs0000644000000000000000000000005611732646061014226 0ustar0000000000000000import Distribution.Simple main = defaultMain transformers-0.3.0.0/transformers.cabal0000644000000000000000000000427111732646061016306 0ustar0000000000000000name: transformers version: 0.3.0.0 license: BSD3 license-file: LICENSE author: Andy Gill, Ross Paterson maintainer: Ross Paterson category: Control synopsis: Concrete functor and monad transformers description: A portable library of functor and monad transformers, inspired by the paper \"Functional Programming with Overloading and Higher-Order Polymorphism\", by Mark P Jones, in /Advanced School of Functional Programming/, 1995 (). . This package contains: . * the monad transformer class (in "Control.Monad.Trans.Class") . * concrete functor and monad transformers, each with associated operations and functions to lift operations associated with other transformers. . It can be used on its own in portable Haskell code, or with the monad classes in the @mtl@ or @monads-tf@ packages, which automatically lift operations introduced by monad transformers through other transformers. build-type: Simple cabal-version: >= 1.6 source-repository head type: darcs location: http://code.haskell.org/~ross/transformers flag ApplicativeInBase description: Use the current base package, including Applicative and other Functor classes. library if flag(ApplicativeInBase) build-depends: base >= 2 && < 6 else build-depends: base >= 1.0 && < 2, special-functors >= 1.0 && < 1.1 exposed-modules: Control.Applicative.Backwards Control.Applicative.Lift Control.Monad.IO.Class Control.Monad.Trans.Class Control.Monad.Trans.Cont Control.Monad.Trans.Error Control.Monad.Trans.Identity Control.Monad.Trans.List Control.Monad.Trans.Maybe Control.Monad.Trans.Reader Control.Monad.Trans.RWS Control.Monad.Trans.RWS.Lazy Control.Monad.Trans.RWS.Strict Control.Monad.Trans.State Control.Monad.Trans.State.Lazy Control.Monad.Trans.State.Strict Control.Monad.Trans.Writer Control.Monad.Trans.Writer.Lazy Control.Monad.Trans.Writer.Strict Data.Functor.Compose Data.Functor.Constant Data.Functor.Identity Data.Functor.Product Data.Functor.Reverse transformers-0.3.0.0/Data/0000755000000000000000000000000011732646061013442 5ustar0000000000000000transformers-0.3.0.0/Data/Functor/0000755000000000000000000000000011732646061015062 5ustar0000000000000000transformers-0.3.0.0/Data/Functor/Product.hs0000644000000000000000000000361311732646061017041 0ustar0000000000000000-- | -- Module : Data.Functor.Product -- Copyright : (c) Ross Paterson 2010 -- License : BSD-style (see the file LICENSE) -- -- Maintainer : ross@soi.city.ac.uk -- Stability : experimental -- Portability : portable -- -- Products, lifted to functors. module Data.Functor.Product ( Product(..), ) where import Control.Applicative import Control.Monad (MonadPlus(..)) import Control.Monad.Fix (MonadFix(..)) import Data.Foldable (Foldable(foldMap)) import Data.Monoid (mappend) import Data.Traversable (Traversable(traverse)) -- | Lifted product of functors. data Product f g a = Pair (f a) (g a) instance (Functor f, Functor g) => Functor (Product f g) where fmap f (Pair x y) = Pair (fmap f x) (fmap f y) instance (Foldable f, Foldable g) => Foldable (Product f g) where foldMap f (Pair x y) = foldMap f x `mappend` foldMap f y instance (Traversable f, Traversable g) => Traversable (Product f g) where traverse f (Pair x y) = Pair <$> traverse f x <*> traverse f y instance (Applicative f, Applicative g) => Applicative (Product f g) where pure x = Pair (pure x) (pure x) Pair f g <*> Pair x y = Pair (f <*> x) (g <*> y) instance (Alternative f, Alternative g) => Alternative (Product f g) where empty = Pair empty empty Pair x1 y1 <|> Pair x2 y2 = Pair (x1 <|> x2) (y1 <|> y2) instance (Monad f, Monad g) => Monad (Product f g) where return x = Pair (return x) (return x) Pair m n >>= f = Pair (m >>= fstP . f) (n >>= sndP . f) where fstP (Pair a _) = a sndP (Pair _ b) = b instance (MonadPlus f, MonadPlus g) => MonadPlus (Product f g) where mzero = Pair mzero mzero Pair x1 y1 `mplus` Pair x2 y2 = Pair (x1 `mplus` x2) (y1 `mplus` y2) instance (MonadFix f, MonadFix g) => MonadFix (Product f g) where mfix f = Pair (mfix (fstP . f)) (mfix (sndP . f)) where fstP (Pair a _) = a sndP (Pair _ b) = b transformers-0.3.0.0/Data/Functor/Reverse.hs0000644000000000000000000000335211732646061017034 0ustar0000000000000000-- | -- Module : Data.Functor.Reverse -- Copyright : (c) Russell O'Connor 2009 -- License : BSD-style (see the file LICENSE) -- -- Maintainer : libraries@haskell.org -- Stability : experimental -- Portability : portable -- -- Making functors whose elements are notionally in the reverse order -- from the original functor. module Data.Functor.Reverse where import Control.Applicative.Backwards import Prelude hiding (foldr, foldr1, foldl, foldl1) import Control.Applicative import Data.Foldable import Data.Traversable import Data.Monoid -- | The same functor, but with 'Foldable' and 'Traversable' instances -- that process the elements in the reverse order. newtype Reverse f a = Reverse { getReverse :: f a } -- | Derived instance. instance (Functor f) => Functor (Reverse f) where fmap f (Reverse a) = Reverse (fmap f a) -- | Derived instance. instance (Applicative f) => Applicative (Reverse f) where pure a = Reverse (pure a) Reverse f <*> Reverse a = Reverse (f <*> a) -- | Derived instance. instance (Alternative f) => Alternative (Reverse f) where empty = Reverse empty Reverse x <|> Reverse y = Reverse (x <|> y) -- | Fold from right to left. instance (Foldable f) => Foldable (Reverse f) where foldMap f (Reverse t) = getDual (foldMap (Dual . f) t) foldr f z (Reverse t) = foldl (flip f) z t foldl f z (Reverse t) = foldr (flip f) z t foldr1 f (Reverse t) = foldl1 (flip f) t foldl1 f (Reverse t) = foldr1 (flip f) t -- | Traverse from right to left. instance (Traversable f) => Traversable (Reverse f) where traverse f (Reverse t) = fmap Reverse . forwards $ traverse (Backwards . f) t sequenceA (Reverse t) = fmap Reverse . forwards $ sequenceA (fmap Backwards t) transformers-0.3.0.0/Data/Functor/Compose.hs0000644000000000000000000000252111732646061017023 0ustar0000000000000000-- | -- Module : Data.Functor.Compose -- Copyright : (c) Ross Paterson 2010 -- License : BSD-style (see the file LICENSE) -- -- Maintainer : ross@soi.city.ac.uk -- Stability : experimental -- Portability : portable -- -- Composition of functors. module Data.Functor.Compose ( Compose(..), ) where import Control.Applicative import Data.Foldable (Foldable(foldMap)) import Data.Traversable (Traversable(traverse)) -- | Right-to-left composition of functors. -- The composition of applicative functors is always applicative, -- but the composition of monads is not always a monad. newtype Compose f g a = Compose { getCompose :: f (g a) } instance (Functor f, Functor g) => Functor (Compose f g) where fmap f (Compose x) = Compose (fmap (fmap f) x) instance (Foldable f, Foldable g) => Foldable (Compose f g) where foldMap f (Compose t) = foldMap (foldMap f) t instance (Traversable f, Traversable g) => Traversable (Compose f g) where traverse f (Compose t) = Compose <$> traverse (traverse f) t instance (Applicative f, Applicative g) => Applicative (Compose f g) where pure x = Compose (pure (pure x)) Compose f <*> Compose x = Compose ((<*>) <$> f <*> x) instance (Alternative f, Applicative g) => Alternative (Compose f g) where empty = Compose empty Compose x <|> Compose y = Compose (x <|> y) transformers-0.3.0.0/Data/Functor/Identity.hs0000644000000000000000000000331611732646061017212 0ustar0000000000000000-- | -- Module : Data.Functor.Identity -- Copyright : (c) Andy Gill 2001, -- (c) Oregon Graduate Institute of Science and Technology 2001 -- License : BSD-style (see the file LICENSE) -- -- Maintainer : ross@soi.city.ac.uk -- Stability : experimental -- Portability : portable -- -- The identity functor and monad. -- -- This trivial type constructor serves two purposes: -- -- * It can be used with functions parameterized by functor or monad classes. -- -- * It can be used as a base monad to which a series of monad -- transformers may be applied to construct a composite monad. -- Most monad transformer modules include the special case of -- applying the transformer to 'Identity'. For example, @State s@ -- is an abbreviation for @StateT s 'Identity'@. module Data.Functor.Identity ( Identity(..), ) where import Control.Applicative import Control.Monad import Control.Monad.Fix import Data.Foldable (Foldable(foldMap)) import Data.Traversable (Traversable(traverse)) -- | Identity functor and monad. newtype Identity a = Identity { runIdentity :: a } -- --------------------------------------------------------------------------- -- Identity instances for Functor and Monad instance Functor Identity where fmap f m = Identity (f (runIdentity m)) instance Foldable Identity where foldMap f (Identity x) = f x instance Traversable Identity where traverse f (Identity x) = Identity <$> f x instance Applicative Identity where pure a = Identity a Identity f <*> Identity x = Identity (f x) instance Monad Identity where return a = Identity a m >>= k = k (runIdentity m) instance MonadFix Identity where mfix f = Identity (fix (runIdentity . f)) transformers-0.3.0.0/Data/Functor/Constant.hs0000644000000000000000000000164111732646061017211 0ustar0000000000000000-- | -- Module : Data.Functor.Constant -- Copyright : (c) Ross Paterson 2010 -- License : BSD-style (see the file LICENSE) -- -- Maintainer : ross@soi.city.ac.uk -- Stability : experimental -- Portability : portable -- -- The constant functor. module Data.Functor.Constant ( Constant(..), ) where import Control.Applicative import Data.Foldable (Foldable(foldMap)) import Data.Monoid (Monoid(..)) import Data.Traversable (Traversable(traverse)) -- | Constant functor. newtype Constant a b = Constant { getConstant :: a } instance Functor (Constant a) where fmap f (Constant x) = Constant x instance Foldable (Constant a) where foldMap f (Constant x) = mempty instance Traversable (Constant a) where traverse f (Constant x) = pure (Constant x) instance (Monoid a) => Applicative (Constant a) where pure _ = Constant mempty Constant x <*> Constant y = Constant (x `mappend` y) transformers-0.3.0.0/Control/0000755000000000000000000000000011732646061014211 5ustar0000000000000000transformers-0.3.0.0/Control/Monad/0000755000000000000000000000000011732646061015247 5ustar0000000000000000transformers-0.3.0.0/Control/Monad/IO/0000755000000000000000000000000011732646061015556 5ustar0000000000000000transformers-0.3.0.0/Control/Monad/IO/Class.hs0000644000000000000000000000215211732646061017157 0ustar0000000000000000----------------------------------------------------------------------------- -- | -- Module : Control.Monad.IO.Class -- Copyright : (c) Andy Gill 2001, -- (c) Oregon Graduate Institute of Science and Technology, 2001 -- License : BSD-style (see the file LICENSE) -- -- Maintainer : ross@soi.city.ac.uk -- Stability : experimental -- Portability : portable -- -- Class of monads based on @IO@. ----------------------------------------------------------------------------- module Control.Monad.IO.Class ( MonadIO(..) ) where import System.IO (IO) -- | 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: -- -- * @'liftIO' . 'return' = 'return'@ -- -- * @'liftIO' (m >>= f) = 'liftIO' m >>= ('liftIO' . f)@ class (Monad m) => MonadIO m where -- | Lift a computation from the 'IO' monad. liftIO :: IO a -> m a instance MonadIO IO where liftIO = id transformers-0.3.0.0/Control/Monad/Trans/0000755000000000000000000000000011732646061016336 5ustar0000000000000000transformers-0.3.0.0/Control/Monad/Trans/State.hs0000644000000000000000000000201011732646061017743 0ustar0000000000000000----------------------------------------------------------------------------- -- | -- Module : Control.Monad.Trans.State -- Copyright : (c) Andy Gill 2001, -- (c) Oregon Graduate Institute of Science and Technology, 2001 -- License : BSD-style (see the file LICENSE) -- -- Maintainer : ross@soi.city.ac.uk -- Stability : experimental -- Portability : portable -- -- State monads, passing an updatable state through a computation. -- -- Some computations may not require the full power of state transformers: -- -- * For a read-only state, see "Control.Monad.Trans.Reader". -- -- * To accumulate a value without using it on the way, see -- "Control.Monad.Trans.Writer". -- -- This version is lazy; for a strict version, see -- "Control.Monad.Trans.State.Strict", which has the same interface. ----------------------------------------------------------------------------- module Control.Monad.Trans.State ( module Control.Monad.Trans.State.Lazy ) where import Control.Monad.Trans.State.Lazy transformers-0.3.0.0/Control/Monad/Trans/Cont.hs0000644000000000000000000001111511732646061017574 0ustar0000000000000000----------------------------------------------------------------------------- -- | -- Module : Control.Monad.Trans.Cont -- Copyright : (c) The University of Glasgow 2001 -- License : BSD-style (see the file LICENSE) -- -- Maintainer : ross@soi.city.ac.uk -- Stability : experimental -- Portability : portable -- -- Continuation monads. -- ----------------------------------------------------------------------------- module Control.Monad.Trans.Cont ( -- * The Cont monad Cont, cont, runCont, mapCont, withCont, -- * The ContT monad transformer ContT(..), mapContT, withContT, callCC, -- * Lifting other operations liftLocal, ) where import Control.Monad.IO.Class import Control.Monad.Trans.Class import Data.Functor.Identity import Control.Applicative import Control.Monad {- | Continuation monad. @Cont r a@ is a CPS computation that produces an intermediate result of type @a@ within a CPS computation whose final result type is @r@. The @return@ function simply creates a continuation which passes the value on. The @>>=@ operator adds the bound function into the continuation chain. -} type Cont r = ContT r Identity -- | Construct a continuation-passing computation from a function. -- (The inverse of 'runCont'.) cont :: ((a -> r) -> r) -> Cont r a cont f = ContT (\ k -> Identity (f (runIdentity . k))) -- | Runs a CPS computation, returns its result after applying the final -- continuation to it. -- (The inverse of 'cont'.) runCont :: Cont r a -- ^ continuation computation (@Cont@). -> (a -> r) -- ^ the final continuation, which produces -- the final result (often 'id'). -> r runCont m k = runIdentity (runContT m (Identity . k)) -- | Apply a function to transform the result of a continuation-passing -- computation. -- -- * @'runCont' ('mapCont' f m) = f . 'runCont' m@ mapCont :: (r -> r) -> Cont r a -> Cont r a mapCont f = mapContT (Identity . f . runIdentity) -- | Apply a function to transform the continuation passed to a CPS -- computation. -- -- * @'runCont' ('withCont' f m) = 'runCont' m . f@ withCont :: ((b -> r) -> (a -> r)) -> Cont r a -> Cont r b withCont f = withContT ((Identity .) . f . (runIdentity .)) {- | The continuation monad transformer. Can be used to add continuation handling to other monads. -} newtype ContT r m a = ContT { runContT :: (a -> m r) -> m r } -- | Apply a function to transform the result of a continuation-passing -- computation. -- -- * @'runContT' ('mapContT' f m) = f . 'runContT' m@ mapContT :: (m r -> m r) -> ContT r m a -> ContT r m a mapContT f m = ContT $ f . runContT m -- | Apply a function to transform the continuation passed to a CPS -- computation. -- -- * @'runContT' ('withContT' f m) = 'runContT' m . f@ withContT :: ((b -> m r) -> (a -> m r)) -> ContT r m a -> ContT r m b withContT f m = ContT $ runContT m . f instance Functor (ContT r m) where fmap f m = ContT $ \c -> runContT m (c . f) instance Applicative (ContT r m) where pure a = ContT ($ a) f <*> v = ContT $ \ k -> runContT f $ \ g -> runContT v (k . g) instance Monad (ContT r m) where return a = ContT ($ a) m >>= k = ContT $ \c -> runContT m (\a -> runContT (k a) c) instance MonadTrans (ContT r) where lift m = ContT (m >>=) instance (MonadIO m) => MonadIO (ContT r m) where liftIO = lift . liftIO -- | @callCC@ (call-with-current-continuation) calls its argument -- function, passing it the current continuation. It provides -- an escape continuation mechanism for use with continuation -- monads. Escape continuations one allow to abort the current -- computation and return a value immediately. They achieve a -- similar effect to 'Control.Monad.Trans.Error.throwError' -- and 'Control.Monad.Trans.Error.catchError' within an -- 'Control.Monad.Trans.Error.ErrorT' monad. The advantage of this -- function over calling 'return' is that it makes the continuation -- explicit, allowing more flexibility and better control. -- -- The standard idiom used with @callCC@ is to provide a lambda-expression -- to name the continuation. Then calling the named continuation anywhere -- within its scope will escape from the computation, even if it is many -- layers deep within nested computations. callCC :: ((a -> ContT r m b) -> ContT r m a) -> ContT r m a callCC f = ContT $ \c -> runContT (f (\a -> ContT $ \_ -> c a)) c -- | @'liftLocal' ask local@ yields a @local@ function for @'ContT' r m@. liftLocal :: Monad m => m r' -> ((r' -> r') -> m r -> m r) -> (r' -> r') -> ContT r m a -> ContT r m a liftLocal ask local f m = ContT $ \c -> do r <- ask local f (runContT m (local (const r) . c)) transformers-0.3.0.0/Control/Monad/Trans/Class.hs0000644000000000000000000000771411732646061017750 0ustar0000000000000000----------------------------------------------------------------------------- -- | -- Module : Control.Monad.Trans.Class -- Copyright : (c) Andy Gill 2001, -- (c) Oregon Graduate Institute of Science and Technology, 2001 -- License : BSD-style (see the file LICENSE) -- -- Maintainer : ross@soi.city.ac.uk -- Stability : experimental -- Portability : portable -- -- Classes for monad transformers. -- -- A monad transformer makes a new monad out of an existing monad, such -- that computations of the old monad may be embedded in the new one. -- To construct a monad with a desired set of features, one typically -- starts with a base monad, such as @Identity@, @[]@ or 'IO', and -- applies a sequence of monad transformers. -- -- Most monad transformer modules include the special case of applying the -- transformer to @Identity@. For example, @State s@ is an abbreviation -- for @StateT s Identity@. -- -- Each monad transformer also comes with an operation @run@/XXX/ to -- unwrap the transformer, exposing a computation of the inner monad. ----------------------------------------------------------------------------- module Control.Monad.Trans.Class ( -- * Transformer class MonadTrans(..) -- * Examples -- ** Parsing -- $example1 -- ** Parsing and counting -- $example2 ) where -- | The class of monad transformers. Instances should satisfy the -- following laws, which state that 'lift' is a transformer of monads: -- -- * @'lift' . 'return' = 'return'@ -- -- * @'lift' (m >>= f) = 'lift' m >>= ('lift' . f)@ class MonadTrans t where -- | Lift a computation from the argument monad to the constructed monad. lift :: Monad m => m a -> t m a {- $example1 One might define a parsing monad by adding a state (the 'String' remaining to be parsed) to the @[]@ monad, which provides non-determinism: > import Control.Monad.Trans.State > > type Parser = StateT String [] Then @Parser@ is an instance of @MonadPlus@: monadic sequencing implements concatenation of parsers, while @mplus@ provides choice. To use parsers, we need a primitive to run a constructed parser on an input string: > runParser :: Parser a -> String -> [a] > runParser p s = [x | (x, "") <- runStateT p s] Finally, we need a primitive parser that matches a single character, from which arbitrarily complex parsers may be constructed: > item :: Parser Char > item = do > c:cs <- get > put cs > return c In this example we use the operations @get@ and @put@ from "Control.Monad.Trans.State", which are defined only for monads that are applications of @StateT@. Alternatively one could use monad classes from the @mtl@ package or similar, which contain methods @get@ and @put@ with types generalized over all suitable monads. -} {- $example2 We can define a parser that also counts by adding a @WriterT@ transformer: > import Control.Monad.Trans.Class > import Control.Monad.Trans.State > import Control.Monad.Trans.Writer > import Data.Monoid > > type Parser = WriterT (Sum Int) (StateT String []) The function that applies a parser must now unwrap each of the monad transformers in turn: > runParser :: Parser a -> String -> [(a, Int)] > runParser p s = [(x, n) | ((x, Sum n), "") <- runStateT (runWriterT p) s] To define @item@ parser, we need to lift the @StateT@ operations through the @WriterT@ transformers. > item :: Parser Char > item = do > c:cs <- lift get > lift (put cs) > return c In this case, we were able to do this with 'lift', but operations with more complex types require special lifting functions, which are provided by monad transformers for which they can be implemented. If you use the monad classes of the @mtl@ package or similar, this lifting is handled automatically by the instances of the classes, and you need only use the generalized methods @get@ and @put@. We can also define a primitive using the Writer: > tick :: Parser () > tick = tell (Sum 1) Then the parser will keep track of how many @tick@s it executes. -} transformers-0.3.0.0/Control/Monad/Trans/Identity.hs0000644000000000000000000000600711732646061020466 0ustar0000000000000000----------------------------------------------------------------------------- -- | -- Module : Control.Monad.Trans.Identity -- Copyright : (c) 2007 Magnus Therning -- License : BSD-style (see the file LICENSE) -- -- Maintainer : ross@soi.city.ac.uk -- Stability : experimental -- Portability : portable -- -- The identity monad transformer. -- -- This is useful for functions parameterized by a monad transformer. ----------------------------------------------------------------------------- module Control.Monad.Trans.Identity ( -- * The identity monad transformer IdentityT(..), mapIdentityT, -- * Lifting other operations liftCatch, liftCallCC, ) where import Control.Applicative import Control.Monad (MonadPlus(mzero, mplus)) import Control.Monad.Fix (MonadFix(mfix)) import Control.Monad.IO.Class (MonadIO(liftIO)) import Control.Monad.Trans.Class (MonadTrans(lift)) import Data.Foldable (Foldable(foldMap)) import Data.Traversable (Traversable(traverse)) -- | The trivial monad transformer, which maps a monad to an equivalent monad. newtype IdentityT m a = IdentityT { runIdentityT :: m a } instance (Functor m) => Functor (IdentityT m) where fmap f = mapIdentityT (fmap f) instance (Foldable f) => Foldable (IdentityT f) where foldMap f (IdentityT a) = foldMap f a instance (Traversable f) => Traversable (IdentityT f) where traverse f (IdentityT a) = IdentityT <$> traverse f a instance (Applicative m) => Applicative (IdentityT m) where pure x = IdentityT (pure x) (<*>) = lift2IdentityT (<*>) instance (Alternative m) => Alternative (IdentityT m) where empty = IdentityT empty (<|>) = lift2IdentityT (<|>) instance (Monad m) => Monad (IdentityT m) where return = IdentityT . return m >>= k = IdentityT $ runIdentityT . k =<< runIdentityT m fail msg = IdentityT $ fail msg instance (MonadPlus m) => MonadPlus (IdentityT m) where mzero = IdentityT mzero mplus = lift2IdentityT mplus instance (MonadFix m) => MonadFix (IdentityT m) where mfix f = IdentityT (mfix (runIdentityT . f)) instance (MonadIO m) => MonadIO (IdentityT m) where liftIO = IdentityT . liftIO instance MonadTrans IdentityT where lift = IdentityT -- | Lift a unary operation to the new monad. mapIdentityT :: (m a -> n b) -> IdentityT m a -> IdentityT n b mapIdentityT f = IdentityT . f . runIdentityT -- | Lift a binary operation to the new monad. lift2IdentityT :: (m a -> n b -> p c) -> IdentityT m a -> IdentityT n b -> IdentityT p c lift2IdentityT f a b = IdentityT (f (runIdentityT a) (runIdentityT b)) -- | Lift a @callCC@ operation to the new monad. liftCallCC :: (((a -> m b) -> m a) -> m a) -> ((a -> IdentityT m b) -> IdentityT m a) -> IdentityT m a liftCallCC callCC f = IdentityT $ callCC $ \ c -> runIdentityT (f (IdentityT . c)) -- | Lift a @catchError@ operation to the new monad. liftCatch :: (m a -> (e -> m a) -> m a) -> IdentityT m a -> (e -> IdentityT m a) -> IdentityT m a liftCatch f m h = IdentityT $ f (runIdentityT m) (runIdentityT . h) transformers-0.3.0.0/Control/Monad/Trans/RWS.hs0000644000000000000000000000142111732646061017343 0ustar0000000000000000----------------------------------------------------------------------------- -- | -- Module : Control.Monad.Trans.RWS -- Copyright : (c) Andy Gill 2001, -- (c) Oregon Graduate Institute of Science and Technology, 2001 -- License : BSD-style (see the file LICENSE) -- -- Maintainer : ross@soi.city.ac.uk -- Stability : experimental -- Portability : portable -- -- A monad transformer that combines 'ReaderT', 'WriterT' and 'StateT'. -- This version is lazy; for a strict version, see -- "Control.Monad.Trans.RWS.Strict", which has the same interface. ----------------------------------------------------------------------------- module Control.Monad.Trans.RWS ( module Control.Monad.Trans.RWS.Lazy ) where import Control.Monad.Trans.RWS.Lazy transformers-0.3.0.0/Control/Monad/Trans/List.hs0000644000000000000000000000563011732646061017611 0ustar0000000000000000----------------------------------------------------------------------------- -- | -- Module : Control.Monad.Trans.List -- Copyright : (c) Andy Gill 2001, -- (c) Oregon Graduate Institute of Science and Technology, 2001 -- License : BSD-style (see the file LICENSE) -- -- Maintainer : ross@soi.city.ac.uk -- Stability : experimental -- Portability : portable -- -- The ListT monad transformer, adding backtracking to a given monad, -- which must be commutative. ----------------------------------------------------------------------------- module Control.Monad.Trans.List ( -- * The ListT monad transformer ListT(..), mapListT, -- * Lifting other operations liftCallCC, liftCatch, ) where import Control.Monad.IO.Class import Control.Monad.Trans.Class import Control.Applicative import Control.Monad import Data.Foldable (Foldable(foldMap)) import Data.Traversable (Traversable(traverse)) -- | Parameterizable list monad, with an inner monad. -- -- /Note:/ this does not yield a monad unless the argument monad is commutative. newtype ListT m a = ListT { runListT :: m [a] } -- | Map between 'ListT' computations. -- -- * @'runListT' ('mapListT' f m) = f ('runListT' m)@ mapListT :: (m [a] -> n [b]) -> ListT m a -> ListT n b mapListT f m = ListT $ f (runListT m) instance (Functor m) => Functor (ListT m) where fmap f = mapListT $ fmap $ map f instance Foldable f => Foldable (ListT f) where foldMap f (ListT a) = foldMap (foldMap f) a instance Traversable f => Traversable (ListT f) where traverse f (ListT a) = ListT <$> traverse (traverse f) a instance (Applicative m) => Applicative (ListT m) where pure a = ListT $ pure [a] f <*> v = ListT $ (<*>) <$> runListT f <*> runListT v instance (Applicative m) => Alternative (ListT m) where empty = ListT $ pure [] m <|> n = ListT $ (++) <$> runListT m <*> runListT n instance (Monad m) => Monad (ListT m) where return a = ListT $ return [a] m >>= k = ListT $ do a <- runListT m b <- mapM (runListT . k) a return (concat b) fail _ = ListT $ return [] instance (Monad m) => MonadPlus (ListT m) where mzero = ListT $ return [] m `mplus` n = ListT $ do a <- runListT m b <- runListT n return (a ++ b) instance MonadTrans ListT where lift m = ListT $ do a <- m return [a] instance (MonadIO m) => MonadIO (ListT m) where liftIO = lift . liftIO -- | Lift a @callCC@ operation to the new monad. liftCallCC :: ((([a] -> m [b]) -> m [a]) -> m [a]) -> ((a -> ListT m b) -> ListT m a) -> ListT m a liftCallCC callCC f = ListT $ callCC $ \c -> runListT (f (\a -> ListT $ c [a])) -- | Lift a @catchError@ operation to the new monad. liftCatch :: (m [a] -> (e -> m [a]) -> m [a]) -> ListT m a -> (e -> ListT m a) -> ListT m a liftCatch catchError m h = ListT $ runListT m `catchError` \e -> runListT (h e) transformers-0.3.0.0/Control/Monad/Trans/Reader.hs0000644000000000000000000001342511732646061020101 0ustar0000000000000000----------------------------------------------------------------------------- -- | -- Module : Control.Monad.Trans.Reader -- Copyright : (c) Andy Gill 2001, -- (c) Oregon Graduate Institute of Science and Technology, 2001 -- License : BSD-style (see the file LICENSE) -- -- Maintainer : ross@soi.city.ac.uk -- Stability : experimental -- Portability : portable -- -- Declaration of the 'ReaderT' monad transformer, which adds a static -- environment to a given monad. -- -- If the computation is to modify the stored information, use -- "Control.Monad.Trans.State" instead. ----------------------------------------------------------------------------- module Control.Monad.Trans.Reader ( -- * The Reader monad Reader, reader, runReader, mapReader, withReader, -- * The ReaderT monad transformer ReaderT(..), mapReaderT, withReaderT, -- * Reader operations ask, local, asks, -- * Lifting other operations liftCallCC, liftCatch, ) where import Control.Monad.IO.Class import Control.Monad.Trans.Class import Data.Functor.Identity import Control.Applicative import Control.Monad import Control.Monad.Fix import Control.Monad.Instances () -- | The parameterizable reader monad. -- -- Computations are functions of a shared environment. -- -- The 'return' function ignores the environment, while @>>=@ passes -- the inherited environment to both subcomputations. type Reader r = ReaderT r Identity -- | Constructor for computations in the reader monad (equivalent to 'asks'). reader :: Monad m => (r -> a) -> ReaderT r m a reader f = ReaderT (return . f) -- | Runs a @Reader@ and extracts the final value from it. -- (The inverse of 'reader'.) runReader :: Reader r a -- ^ A @Reader@ to run. -> r -- ^ An initial environment. -> a runReader m = runIdentity . runReaderT m -- | Transform the value returned by a @Reader@. -- -- * @'runReader' ('mapReader' f m) = f . 'runReader' m@ mapReader :: (a -> b) -> Reader r a -> Reader r b mapReader f = mapReaderT (Identity . f . runIdentity) -- | Execute a computation in a modified environment -- (a specialization of 'withReaderT'). -- -- * @'runReader' ('withReader' f m) = 'runReader' m . f@ withReader :: (r' -> r) -- ^ The function to modify the environment. -> Reader r a -- ^ Computation to run in the modified environment. -> Reader r' a withReader = withReaderT -- | The reader monad transformer, -- which adds a read-only environment to the given monad. -- -- The 'return' function ignores the environment, while @>>=@ passes -- the inherited environment to both subcomputations. newtype ReaderT r m a = ReaderT { -- | The underlying computation, as a function of the environment. runReaderT :: r -> m a } -- | Transform the computation inside a @ReaderT@. -- -- * @'runReaderT' ('mapReaderT' f m) = f . 'runReaderT' m@ mapReaderT :: (m a -> n b) -> ReaderT r m a -> ReaderT r n b mapReaderT f m = ReaderT $ f . runReaderT m -- | Execute a computation in a modified environment -- (a more general version of 'local'). -- -- * @'runReaderT' ('withReaderT' f m) = 'runReaderT' m . f@ withReaderT :: (r' -> r) -- ^ The function to modify the environment. -> ReaderT r m a -- ^ Computation to run in the modified environment. -> ReaderT r' m a withReaderT f m = ReaderT $ runReaderT m . f instance (Functor m) => Functor (ReaderT r m) where fmap f = mapReaderT (fmap f) instance (Applicative m) => Applicative (ReaderT r m) where pure = liftReaderT . pure f <*> v = ReaderT $ \ r -> runReaderT f r <*> runReaderT v r instance (Alternative m) => Alternative (ReaderT r m) where empty = liftReaderT empty m <|> n = ReaderT $ \ r -> runReaderT m r <|> runReaderT n r instance (Monad m) => Monad (ReaderT r m) where return = lift . return m >>= k = ReaderT $ \ r -> do a <- runReaderT m r runReaderT (k a) r fail msg = lift (fail msg) instance (MonadPlus m) => MonadPlus (ReaderT r m) where mzero = lift mzero m `mplus` n = ReaderT $ \ r -> runReaderT m r `mplus` runReaderT n r instance (MonadFix m) => MonadFix (ReaderT r m) where mfix f = ReaderT $ \ r -> mfix $ \ a -> runReaderT (f a) r instance MonadTrans (ReaderT r) where lift = liftReaderT instance (MonadIO m) => MonadIO (ReaderT r m) where liftIO = lift . liftIO liftReaderT :: m a -> ReaderT r m a liftReaderT m = ReaderT (const m) -- | Fetch the value of the environment. ask :: (Monad m) => ReaderT r m r ask = ReaderT return -- | Execute a computation in a modified environment -- (a specialization of 'withReaderT'). -- -- * @'runReaderT' ('local' f m) = 'runReaderT' m . f@ local :: (Monad m) => (r -> r) -- ^ The function to modify the environment. -> ReaderT r m a -- ^ Computation to run in the modified environment. -> ReaderT r m a local = withReaderT -- | Retrieve a function of the current environment. -- -- * @'asks' f = 'liftM' f 'ask'@ asks :: (Monad m) => (r -> a) -- ^ The selector function to apply to the environment. -> ReaderT r m a asks f = ReaderT (return . f) -- | Lift a @callCC@ operation to the new monad. liftCallCC :: (((a -> m b) -> m a) -> m a) -- ^ @callCC@ on the argument monad. -> ((a -> ReaderT r m b) -> ReaderT r m a) -> ReaderT r m a liftCallCC callCC f = ReaderT $ \ r -> callCC $ \ c -> runReaderT (f (ReaderT . const . c)) r -- | Lift a @catchError@ operation to the new monad. liftCatch :: (m a -> (e -> m a) -> m a) -- ^ @catch@ on the argument monad. -> ReaderT r m a -- ^ Computation to attempt. -> (e -> ReaderT r m a) -- ^ Exception handler. -> ReaderT r m a liftCatch f m h = ReaderT $ \ r -> f (runReaderT m r) (\ e -> runReaderT (h e) r) transformers-0.3.0.0/Control/Monad/Trans/Maybe.hs0000644000000000000000000001015611732646061017732 0ustar0000000000000000----------------------------------------------------------------------------- -- | -- Module : Control.Monad.Trans.Maybe -- Copyright : (c) 2007 Yitzak Gale, Eric Kidd -- License : BSD-style (see the file LICENSE) -- -- Maintainer : ross@soi.city.ac.uk -- Stability : experimental -- Portability : portable -- -- The 'MaybeT' monad transformer adds the ability to fail to a monad. -- -- A sequence of actions succeeds, producing a value, only if all the -- actions in the sequence are successful. If one fails, the rest of -- the sequence is skipped and the composite action fails. -- -- For a variant allowing a range of error values, see -- "Control.Monad.Trans.Error". ----------------------------------------------------------------------------- module Control.Monad.Trans.Maybe ( -- * The MaybeT monad transformer MaybeT(..), mapMaybeT, -- * Lifting other operations liftCallCC, liftCatch, liftListen, liftPass, ) where import Control.Monad.IO.Class import Control.Monad.Trans.Class import Control.Applicative import Control.Monad (MonadPlus(mzero, mplus), liftM, ap) import Control.Monad.Fix (MonadFix(mfix)) import Data.Foldable (Foldable(foldMap)) import Data.Maybe (fromMaybe) import Data.Traversable (Traversable(traverse)) -- | The parameterizable maybe monad, obtained by composing an arbitrary -- monad with the 'Maybe' monad. -- -- Computations are actions that may produce a value or fail. -- -- The 'return' function yields a successful computation, while @>>=@ -- sequences two subcomputations, failing on the first error. newtype MaybeT m a = MaybeT { runMaybeT :: m (Maybe a) } -- | Transform the computation inside a @MaybeT@. -- -- * @'runMaybeT' ('mapMaybeT' f m) = f ('runMaybeT' m)@ mapMaybeT :: (m (Maybe a) -> n (Maybe b)) -> MaybeT m a -> MaybeT n b mapMaybeT f = MaybeT . f . runMaybeT instance (Functor m) => Functor (MaybeT m) where fmap f = mapMaybeT (fmap (fmap f)) instance (Foldable f) => Foldable (MaybeT f) where foldMap f (MaybeT a) = foldMap (foldMap f) a instance (Traversable f) => Traversable (MaybeT f) where traverse f (MaybeT a) = MaybeT <$> traverse (traverse f) a instance (Functor m, Monad m) => Applicative (MaybeT m) where pure = return (<*>) = ap instance (Functor m, Monad m) => Alternative (MaybeT m) where empty = mzero (<|>) = mplus instance (Monad m) => Monad (MaybeT m) where fail _ = MaybeT (return Nothing) return = lift . return x >>= f = MaybeT $ do v <- runMaybeT x case v of Nothing -> return Nothing Just y -> runMaybeT (f y) instance (Monad m) => MonadPlus (MaybeT m) where mzero = MaybeT (return Nothing) mplus x y = MaybeT $ do v <- runMaybeT x case v of Nothing -> runMaybeT y Just _ -> return v instance (MonadFix m) => MonadFix (MaybeT m) where mfix f = MaybeT (mfix (runMaybeT . f . unJust)) where unJust = fromMaybe (error "mfix MaybeT: Nothing") instance MonadTrans MaybeT where lift = MaybeT . liftM Just instance (MonadIO m) => MonadIO (MaybeT m) where liftIO = lift . liftIO -- | Lift a @callCC@ operation to the new monad. liftCallCC :: (((Maybe a -> m (Maybe b)) -> m (Maybe a)) -> m (Maybe a)) -> ((a -> MaybeT m b) -> MaybeT m a) -> MaybeT m a liftCallCC callCC f = MaybeT $ callCC $ \ c -> runMaybeT (f (MaybeT . c . Just)) -- | Lift a @catchError@ operation to the new monad. liftCatch :: (m (Maybe a) -> (e -> m (Maybe a)) -> m (Maybe a)) -> MaybeT m a -> (e -> MaybeT m a) -> MaybeT m a liftCatch f m h = MaybeT $ f (runMaybeT m) (runMaybeT . h) -- | Lift a @listen@ operation to the new monad. liftListen :: Monad m => (m (Maybe a) -> m (Maybe a,w)) -> MaybeT m a -> MaybeT m (a,w) liftListen listen = mapMaybeT $ \ m -> do (a, w) <- listen m return $! fmap (\ r -> (r, w)) a -- | Lift a @pass@ operation to the new monad. liftPass :: Monad m => (m (Maybe a,w -> w) -> m (Maybe a)) -> MaybeT m (a,w -> w) -> MaybeT m a liftPass pass = mapMaybeT $ \ m -> pass $ do a <- m return $! case a of Nothing -> (Nothing, id) Just (v, f) -> (Just v, f) transformers-0.3.0.0/Control/Monad/Trans/Writer.hs0000644000000000000000000000137211732646061020151 0ustar0000000000000000----------------------------------------------------------------------------- -- | -- Module : Control.Monad.Trans.Writer -- Copyright : (c) Andy Gill 2001, -- (c) Oregon Graduate Institute of Science and Technology, 2001 -- License : BSD-style (see the file LICENSE) -- -- Maintainer : ross@soi.city.ac.uk -- Stability : experimental -- Portability : portable -- -- The WriterT monad transformer. -- This version is lazy; for a strict version, see -- "Control.Monad.Trans.Writer.Strict", which has the same interface. ----------------------------------------------------------------------------- module Control.Monad.Trans.Writer ( module Control.Monad.Trans.Writer.Lazy ) where import Control.Monad.Trans.Writer.Lazy transformers-0.3.0.0/Control/Monad/Trans/Error.hs0000644000000000000000000001747011732646061017774 0ustar0000000000000000{-# LANGUAGE CPP #-} ----------------------------------------------------------------------------- -- | -- Module : Control.Monad.Trans.Error -- Copyright : (c) Michael Weber 2001, -- (c) Jeff Newbern 2003-2006, -- (c) Andriy Palamarchuk 2006 -- License : BSD-style (see the file LICENSE) -- -- Maintainer : ross@soi.city.ac.uk -- Stability : experimental -- Portability : portable -- -- This monad transformer adds the ability to fail or throw exceptions -- to a monad. -- -- A sequence of actions succeeds, producing a value, only if all the -- actions in the sequence are successful. If one fails with an error, -- the rest of the sequence is skipped and the composite action fails -- with that error. -- -- If the value of the error is not required, the variant in -- "Control.Monad.Trans.Maybe" may be used instead. ----------------------------------------------------------------------------- module Control.Monad.Trans.Error ( -- * The ErrorT monad transformer Error(..), ErrorList(..), ErrorT(..), mapErrorT, -- * Error operations throwError, catchError, -- * Lifting other operations liftCallCC, liftListen, liftPass, -- * Examples -- $examples ) where import Control.Monad.IO.Class import Control.Monad.Trans.Class import Control.Applicative import Control.Exception (IOException) import Control.Monad import Control.Monad.Fix import Control.Monad.Instances () import Data.Foldable (Foldable(foldMap)) import Data.Monoid (mempty) import Data.Traversable (Traversable(traverse)) import System.IO.Error instance MonadPlus IO where mzero = ioError (userError "mzero") m `mplus` n = m `catchIOError` \_ -> n #if !(MIN_VERSION_base(4,4,0)) -- exported by System.IO.Error from base-4.4 catchIOError :: IO a -> (IOError -> IO a) -> IO a catchIOError = catch #endif -- | An exception to be thrown. -- -- Minimal complete definition: 'noMsg' or 'strMsg'. class Error a where -- | Creates an exception without a message. -- The default implementation is @'strMsg' \"\"@. noMsg :: a -- | Creates an exception with a message. -- The default implementation of @'strMsg' s@ is 'noMsg'. strMsg :: String -> a noMsg = strMsg "" strMsg _ = noMsg instance Error IOException where strMsg = userError -- | A string can be thrown as an error. instance ErrorList a => Error [a] where strMsg = listMsg -- | Workaround so that we can have a Haskell 98 instance @'Error' 'String'@. class ErrorList a where listMsg :: String -> [a] instance ErrorList Char where listMsg = id -- --------------------------------------------------------------------------- -- Our parameterizable error monad #if !(MIN_VERSION_base(4,3,0)) -- These instances are in base-4.3 instance Applicative (Either e) where pure = Right Left e <*> _ = Left e Right f <*> r = fmap f r instance Monad (Either e) where return = Right Left l >>= _ = Left l Right r >>= k = k r instance MonadFix (Either e) where mfix f = let a = f $ case a of Right r -> r _ -> error "empty mfix argument" in a #endif /* base to 4.2.0.x */ instance (Error e) => Alternative (Either e) where empty = Left noMsg Left _ <|> n = n m <|> _ = m instance (Error e) => MonadPlus (Either e) where mzero = Left noMsg Left _ `mplus` n = n m `mplus` _ = m -- | The error monad transformer. It can be used to add error handling -- to other monads. -- -- The @ErrorT@ Monad structure is parameterized over two things: -- -- * e - The error type. -- -- * m - The inner monad. -- -- The 'return' function yields a successful computation, while @>>=@ -- sequences two subcomputations, failing on the first error. newtype ErrorT e m a = ErrorT { runErrorT :: m (Either e a) } -- | Map the unwrapped computation using the given function. -- -- * @'runErrorT' ('mapErrorT' f m) = f ('runErrorT' m)@ mapErrorT :: (m (Either e a) -> n (Either e' b)) -> ErrorT e m a -> ErrorT e' n b mapErrorT f m = ErrorT $ f (runErrorT m) instance (Functor m) => Functor (ErrorT e m) where fmap f = ErrorT . fmap (fmap f) . runErrorT instance (Foldable f) => Foldable (ErrorT e f) where foldMap f (ErrorT a) = foldMap (either (const mempty) f) a instance (Traversable f) => Traversable (ErrorT e f) where traverse f (ErrorT a) = ErrorT <$> traverse (either (pure . Left) (fmap Right . f)) a instance (Functor m, Monad m) => Applicative (ErrorT e m) where pure a = ErrorT $ return (Right a) f <*> v = ErrorT $ do mf <- runErrorT f case mf of Left e -> return (Left e) Right k -> do mv <- runErrorT v case mv of Left e -> return (Left e) Right x -> return (Right (k x)) instance (Functor m, Monad m, Error e) => Alternative (ErrorT e m) where empty = mzero (<|>) = mplus instance (Monad m, Error e) => Monad (ErrorT e m) where return a = ErrorT $ return (Right a) m >>= k = ErrorT $ do a <- runErrorT m case a of Left l -> return (Left l) Right r -> runErrorT (k r) fail msg = ErrorT $ return (Left (strMsg msg)) instance (Monad m, Error e) => MonadPlus (ErrorT e m) where mzero = ErrorT $ return (Left noMsg) m `mplus` n = ErrorT $ do a <- runErrorT m case a of Left _ -> runErrorT n Right r -> return (Right r) instance (MonadFix m, Error e) => MonadFix (ErrorT e m) where mfix f = ErrorT $ mfix $ \a -> runErrorT $ f $ case a of Right r -> r _ -> error "empty mfix argument" instance (Error e) => MonadTrans (ErrorT e) where lift m = ErrorT $ do a <- m return (Right a) instance (Error e, MonadIO m) => MonadIO (ErrorT e m) where liftIO = lift . liftIO -- | Signal an error value @e@. -- -- * @'runErrorT' ('throwError' e) = 'return' ('Left' e)@ -- -- * @'throwError' e >>= m = 'throwError' e@ throwError :: (Monad m, Error e) => e -> ErrorT e m a throwError l = ErrorT $ return (Left l) -- | Handle an error. -- -- * @'catchError' h ('lift' m) = 'lift' m@ -- -- * @'catchError' h ('throwError' e) = h e@ catchError :: (Monad m, Error e) => ErrorT e m a -- ^ the inner computation -> (e -> ErrorT e m a) -- ^ a handler for errors in the inner -- computation -> ErrorT e m a m `catchError` h = ErrorT $ do a <- runErrorT m case a of Left l -> runErrorT (h l) Right r -> return (Right r) -- | Lift a @callCC@ operation to the new monad. liftCallCC :: (((Either e a -> m (Either e b)) -> m (Either e a)) -> m (Either e a)) -> ((a -> ErrorT e m b) -> ErrorT e m a) -> ErrorT e m a liftCallCC callCC f = ErrorT $ callCC $ \c -> runErrorT (f (\a -> ErrorT $ c (Right a))) -- | Lift a @listen@ operation to the new monad. liftListen :: Monad m => (m (Either e a) -> m (Either e a,w)) -> ErrorT e m a -> ErrorT e m (a,w) liftListen listen = mapErrorT $ \ m -> do (a, w) <- listen m return $! fmap (\ r -> (r, w)) a -- | Lift a @pass@ operation to the new monad. liftPass :: Monad m => (m (Either e a,w -> w) -> m (Either e a)) -> ErrorT e m (a,w -> w) -> ErrorT e m a liftPass pass = mapErrorT $ \ m -> pass $ do a <- m return $! case a of Left l -> (Left l, id) Right (r, f) -> (Right r, f) {- $examples Wrapping an IO action that can throw an error @e@: > type ErrorWithIO e a = ErrorT e IO a > ==> ErrorT (IO (Either e a)) An IO monad wrapped in @StateT@ inside of @ErrorT@: > type ErrorAndStateWithIO e s a = ErrorT e (StateT s IO) a > ==> ErrorT (StateT s IO (Either e a)) > ==> ErrorT (StateT (s -> IO (Either e a,s))) -} transformers-0.3.0.0/Control/Monad/Trans/State/0000755000000000000000000000000011732646061017416 5ustar0000000000000000transformers-0.3.0.0/Control/Monad/Trans/State/Strict.hs0000644000000000000000000002656611732646061021241 0ustar0000000000000000----------------------------------------------------------------------------- -- | -- Module : Control.Monad.Trans.State.Strict -- Copyright : (c) Andy Gill 2001, -- (c) Oregon Graduate Institute of Science and Technology, 2001 -- License : BSD-style (see the file LICENSE) -- -- Maintainer : ross@soi.city.ac.uk -- Stability : experimental -- Portability : portable -- -- Strict state monads, passing an updatable state through a computation. -- See below for examples. -- -- In this version, sequencing of computations is strict. -- For a lazy version, see "Control.Monad.Trans.State.Lazy", which -- has the same interface. -- -- Some computations may not require the full power of state transformers: -- -- * For a read-only state, see "Control.Monad.Trans.Reader". -- -- * To accumulate a value without using it on the way, see -- "Control.Monad.Trans.Writer". ----------------------------------------------------------------------------- module Control.Monad.Trans.State.Strict ( -- * The State monad State, state, runState, evalState, execState, mapState, withState, -- * The StateT monad transformer StateT(..), evalStateT, execStateT, mapStateT, withStateT, -- * State operations get, put, modify, gets, -- * Lifting other operations liftCallCC, liftCallCC', liftCatch, liftListen, liftPass, -- * Examples -- ** State monads -- $examples -- ** Counting -- $counting -- ** Labelling trees -- $labelling ) where import Control.Monad.IO.Class import Control.Monad.Trans.Class import Data.Functor.Identity import Control.Applicative import Control.Monad import Control.Monad.Fix -- --------------------------------------------------------------------------- -- | A state monad parameterized by the type @s@ of the state to carry. -- -- The 'return' function leaves the state unchanged, while @>>=@ uses -- the final state of the first computation as the initial state of -- the second. type State s = StateT s Identity -- | Construct a state monad computation from a function. -- (The inverse of 'runState'.) state :: Monad m => (s -> (a, s)) -- ^pure state transformer -> StateT s m a -- ^equivalent state-passing computation state f = StateT (return . f) -- | Unwrap a state monad computation as a function. -- (The inverse of 'state'.) runState :: State s a -- ^state-passing computation to execute -> s -- ^initial state -> (a, s) -- ^return value and final state runState m = runIdentity . runStateT m -- | Evaluate a state computation with the given initial state -- and return the final value, discarding the final state. -- -- * @'evalState' m s = 'fst' ('runState' m s)@ evalState :: State s a -- ^state-passing computation to execute -> s -- ^initial value -> a -- ^return value of the state computation evalState m s = fst (runState m s) -- | Evaluate a state computation with the given initial state -- and return the final state, discarding the final value. -- -- * @'execState' m s = 'snd' ('runState' m s)@ execState :: State s a -- ^state-passing computation to execute -> s -- ^initial value -> s -- ^final state execState m s = snd (runState m s) -- | Map both the return value and final state of a computation using -- the given function. -- -- * @'runState' ('mapState' f m) = f . 'runState' m@ mapState :: ((a, s) -> (b, s)) -> State s a -> State s b mapState f = mapStateT (Identity . f . runIdentity) -- | @'withState' f m@ executes action @m@ on a state modified by -- applying @f@. -- -- * @'withState' f m = 'modify' f >> m@ withState :: (s -> s) -> State s a -> State s a withState = withStateT -- --------------------------------------------------------------------------- -- | A state transformer monad parameterized by: -- -- * @s@ - The state. -- -- * @m@ - The inner monad. -- -- The 'return' function leaves the state unchanged, while @>>=@ uses -- the final state of the first computation as the initial state of -- the second. newtype StateT s m a = StateT { runStateT :: s -> m (a,s) } -- | Evaluate a state computation with the given initial state -- and return the final value, discarding the final state. -- -- * @'evalStateT' m s = 'liftM' 'fst' ('runStateT' m s)@ evalStateT :: (Monad m) => StateT s m a -> s -> m a evalStateT m s = do (a, _) <- runStateT m s return a -- | Evaluate a state computation with the given initial state -- and return the final state, discarding the final value. -- -- * @'execStateT' m s = 'liftM' 'snd' ('runStateT' m s)@ execStateT :: (Monad m) => StateT s m a -> s -> m s execStateT m s = do (_, s') <- runStateT m s return s' -- | Map both the return value and final state of a computation using -- the given function. -- -- * @'runStateT' ('mapStateT' f m) = f . 'runStateT' m@ mapStateT :: (m (a, s) -> n (b, s)) -> StateT s m a -> StateT s n b mapStateT f m = StateT $ f . runStateT m -- | @'withStateT' f m@ executes action @m@ on a state modified by -- applying @f@. -- -- * @'withStateT' f m = 'modify' f >> m@ withStateT :: (s -> s) -> StateT s m a -> StateT s m a withStateT f m = StateT $ runStateT m . f instance (Functor m) => Functor (StateT s m) where fmap f m = StateT $ \ s -> fmap (\ (a, s') -> (f a, s')) $ runStateT m s instance (Functor m, Monad m) => Applicative (StateT s m) where pure = return (<*>) = ap instance (Functor m, MonadPlus m) => Alternative (StateT s m) where empty = mzero (<|>) = mplus instance (Monad m) => Monad (StateT s m) where return a = state $ \s -> (a, s) m >>= k = StateT $ \s -> do (a, s') <- runStateT m s runStateT (k a) s' fail str = StateT $ \_ -> fail str instance (MonadPlus m) => MonadPlus (StateT s m) where mzero = StateT $ \_ -> mzero m `mplus` n = StateT $ \s -> runStateT m s `mplus` runStateT n s instance (MonadFix m) => MonadFix (StateT s m) where mfix f = StateT $ \s -> mfix $ \ ~(a, _) -> runStateT (f a) s instance MonadTrans (StateT s) where lift m = StateT $ \s -> do a <- m return (a, s) instance (MonadIO m) => MonadIO (StateT s m) where liftIO = lift . liftIO -- | Fetch the current value of the state within the monad. get :: (Monad m) => StateT s m s get = state $ \s -> (s, s) -- | @'put' s@ sets the state within the monad to @s@. put :: (Monad m) => s -> StateT s m () put s = state $ \_ -> ((), s) -- | @'modify' f@ is an action that updates the state to the result of -- applying @f@ to the current state. -- -- * @'modify' f = 'get' >>= ('put' . f)@ modify :: (Monad m) => (s -> s) -> StateT s m () modify f = state $ \s -> ((), f s) -- | Get a specific component of the state, using a projection function -- supplied. -- -- * @'gets' f = 'liftM' f 'get'@ gets :: (Monad m) => (s -> a) -> StateT s m a gets f = state $ \s -> (f s, s) -- | Uniform lifting of a @callCC@ operation to the new monad. -- This version rolls back to the original state on entering the -- continuation. liftCallCC :: ((((a,s) -> m (b,s)) -> m (a,s)) -> m (a,s)) -> ((a -> StateT s m b) -> StateT s m a) -> StateT s m a liftCallCC callCC f = StateT $ \s -> callCC $ \c -> runStateT (f (\a -> StateT $ \ _ -> c (a, s))) s -- | In-situ lifting of a @callCC@ operation to the new monad. -- This version uses the current state on entering the continuation. -- It does not satisfy the laws of a monad transformer. liftCallCC' :: ((((a,s) -> m (b,s)) -> m (a,s)) -> m (a,s)) -> ((a -> StateT s m b) -> StateT s m a) -> StateT s m a liftCallCC' callCC f = StateT $ \s -> callCC $ \c -> runStateT (f (\a -> StateT $ \s' -> c (a, s'))) s -- | Lift a @catchError@ operation to the new monad. liftCatch :: (m (a,s) -> (e -> m (a,s)) -> m (a,s)) -> StateT s m a -> (e -> StateT s m a) -> StateT s m a liftCatch catchError m h = StateT $ \s -> runStateT m s `catchError` \e -> runStateT (h e) s -- | Lift a @listen@ operation to the new monad. liftListen :: Monad m => (m (a,s) -> m ((a,s),w)) -> StateT s m a -> StateT s m (a,w) liftListen listen m = StateT $ \s -> do ((a, s'), w) <- listen (runStateT m s) return ((a, w), s') -- | Lift a @pass@ operation to the new monad. liftPass :: Monad m => (m ((a,s),b) -> m (a,s)) -> StateT s m (a,b) -> StateT s m a liftPass pass m = StateT $ \s -> pass $ do ((a, f), s') <- runStateT m s return ((a, s'), f) {- $examples Parser from ParseLib with Hugs: > type Parser a = StateT String [] a > ==> StateT (String -> [(a,String)]) For example, item can be written as: > item = do (x:xs) <- get > put xs > return x > > type BoringState s a = StateT s Identity a > ==> StateT (s -> Identity (a,s)) > > type StateWithIO s a = StateT s IO a > ==> StateT (s -> IO (a,s)) > > type StateWithErr s a = StateT s Maybe a > ==> StateT (s -> Maybe (a,s)) -} {- $counting A function to increment a counter. Taken from the paper \"Generalising Monads to Arrows\", John Hughes (), November 1998: > tick :: State Int Int > tick = do n <- get > put (n+1) > return n Add one to the given number using the state monad: > plusOne :: Int -> Int > plusOne n = execState tick n A contrived addition example. Works only with positive numbers: > plus :: Int -> Int -> Int > plus n x = execState (sequence $ replicate n tick) x -} {- $labelling An example from /The Craft of Functional Programming/, Simon Thompson (), Addison-Wesley 1999: \"Given an arbitrary tree, transform it to a tree of integers in which the original elements are replaced by natural numbers, starting from 0. The same element has to be replaced by the same number at every occurrence, and when we meet an as-yet-unvisited element we have to find a \'new\' number to match it with:\" > data Tree a = Nil | Node a (Tree a) (Tree a) deriving (Show, Eq) > type Table a = [a] > numberTree :: Eq a => Tree a -> State (Table a) (Tree Int) > numberTree Nil = return Nil > numberTree (Node x t1 t2) > = do num <- numberNode x > nt1 <- numberTree t1 > nt2 <- numberTree t2 > return (Node num nt1 nt2) > where > numberNode :: Eq a => a -> State (Table a) Int > numberNode x > = do table <- get > (newTable, newPos) <- return (nNode x table) > put newTable > return newPos > nNode:: (Eq a) => a -> Table a -> (Table a, Int) > nNode x table > = case (findIndexInList (== x) table) of > Nothing -> (table ++ [x], length table) > Just i -> (table, i) > findIndexInList :: (a -> Bool) -> [a] -> Maybe Int > findIndexInList = findIndexInListHelp 0 > findIndexInListHelp _ _ [] = Nothing > findIndexInListHelp count f (h:t) > = if (f h) > then Just count > else findIndexInListHelp (count+1) f t numTree applies numberTree with an initial state: > numTree :: (Eq a) => Tree a -> Tree Int > numTree t = evalState (numberTree t) [] > testTree = Node "Zero" (Node "One" (Node "Two" Nil Nil) (Node "One" (Node "Zero" Nil Nil) Nil)) Nil > numTree testTree => Node 0 (Node 1 (Node 2 Nil Nil) (Node 1 (Node 0 Nil Nil) Nil)) Nil sumTree is a little helper function that does not use the State monad: > sumTree :: (Num a) => Tree a -> a > sumTree Nil = 0 > sumTree (Node e t1 t2) = e + (sumTree t1) + (sumTree t2) -} transformers-0.3.0.0/Control/Monad/Trans/State/Lazy.hs0000644000000000000000000002657011732646061020703 0ustar0000000000000000----------------------------------------------------------------------------- -- | -- Module : Control.Monad.Trans.State.Lazy -- Copyright : (c) Andy Gill 2001, -- (c) Oregon Graduate Institute of Science and Technology, 2001 -- License : BSD-style (see the file LICENSE) -- -- Maintainer : ross@soi.city.ac.uk -- Stability : experimental -- Portability : portable -- -- Lazy state monads, passing an updatable state through a computation. -- See below for examples. -- -- In this version, sequencing of computations is lazy. -- For a strict version, see "Control.Monad.Trans.State.Strict", which -- has the same interface. -- -- Some computations may not require the full power of state transformers: -- -- * For a read-only state, see "Control.Monad.Trans.Reader". -- -- * To accumulate a value without using it on the way, see -- "Control.Monad.Trans.Writer". ----------------------------------------------------------------------------- module Control.Monad.Trans.State.Lazy ( -- * The State monad State, state, runState, evalState, execState, mapState, withState, -- * The StateT monad transformer StateT(..), evalStateT, execStateT, mapStateT, withStateT, -- * State operations get, put, modify, gets, -- * Lifting other operations liftCallCC, liftCallCC', liftCatch, liftListen, liftPass, -- * Examples -- ** State monads -- $examples -- ** Counting -- $counting -- ** Labelling trees -- $labelling ) where import Control.Monad.IO.Class import Control.Monad.Trans.Class import Data.Functor.Identity import Control.Applicative import Control.Monad import Control.Monad.Fix -- --------------------------------------------------------------------------- -- | A state monad parameterized by the type @s@ of the state to carry. -- -- The 'return' function leaves the state unchanged, while @>>=@ uses -- the final state of the first computation as the initial state of -- the second. type State s = StateT s Identity -- | Construct a state monad computation from a function. -- (The inverse of 'runState'.) state :: Monad m => (s -> (a, s)) -- ^pure state transformer -> StateT s m a -- ^equivalent state-passing computation state f = StateT (return . f) -- | Unwrap a state monad computation as a function. -- (The inverse of 'state'.) runState :: State s a -- ^state-passing computation to execute -> s -- ^initial state -> (a, s) -- ^return value and final state runState m = runIdentity . runStateT m -- | Evaluate a state computation with the given initial state -- and return the final value, discarding the final state. -- -- * @'evalState' m s = 'fst' ('runState' m s)@ evalState :: State s a -- ^state-passing computation to execute -> s -- ^initial value -> a -- ^return value of the state computation evalState m s = fst (runState m s) -- | Evaluate a state computation with the given initial state -- and return the final state, discarding the final value. -- -- * @'execState' m s = 'snd' ('runState' m s)@ execState :: State s a -- ^state-passing computation to execute -> s -- ^initial value -> s -- ^final state execState m s = snd (runState m s) -- | Map both the return value and final state of a computation using -- the given function. -- -- * @'runState' ('mapState' f m) = f . 'runState' m@ mapState :: ((a, s) -> (b, s)) -> State s a -> State s b mapState f = mapStateT (Identity . f . runIdentity) -- | @'withState' f m@ executes action @m@ on a state modified by -- applying @f@. -- -- * @'withState' f m = 'modify' f >> m@ withState :: (s -> s) -> State s a -> State s a withState = withStateT -- --------------------------------------------------------------------------- -- | A state transformer monad parameterized by: -- -- * @s@ - The state. -- -- * @m@ - The inner monad. -- -- The 'return' function leaves the state unchanged, while @>>=@ uses -- the final state of the first computation as the initial state of -- the second. newtype StateT s m a = StateT { runStateT :: s -> m (a,s) } -- | Evaluate a state computation with the given initial state -- and return the final value, discarding the final state. -- -- * @'evalStateT' m s = 'liftM' 'fst' ('runStateT' m s)@ evalStateT :: (Monad m) => StateT s m a -> s -> m a evalStateT m s = do ~(a, _) <- runStateT m s return a -- | Evaluate a state computation with the given initial state -- and return the final state, discarding the final value. -- -- * @'execStateT' m s = 'liftM' 'snd' ('runStateT' m s)@ execStateT :: (Monad m) => StateT s m a -> s -> m s execStateT m s = do ~(_, s') <- runStateT m s return s' -- | Map both the return value and final state of a computation using -- the given function. -- -- * @'runStateT' ('mapStateT' f m) = f . 'runStateT' m@ mapStateT :: (m (a, s) -> n (b, s)) -> StateT s m a -> StateT s n b mapStateT f m = StateT $ f . runStateT m -- | @'withStateT' f m@ executes action @m@ on a state modified by -- applying @f@. -- -- * @'withStateT' f m = 'modify' f >> m@ withStateT :: (s -> s) -> StateT s m a -> StateT s m a withStateT f m = StateT $ runStateT m . f instance (Functor m) => Functor (StateT s m) where fmap f m = StateT $ \ s -> fmap (\ ~(a, s') -> (f a, s')) $ runStateT m s instance (Functor m, Monad m) => Applicative (StateT s m) where pure = return (<*>) = ap instance (Functor m, MonadPlus m) => Alternative (StateT s m) where empty = mzero (<|>) = mplus instance (Monad m) => Monad (StateT s m) where return a = state $ \s -> (a, s) m >>= k = StateT $ \s -> do ~(a, s') <- runStateT m s runStateT (k a) s' fail str = StateT $ \_ -> fail str instance (MonadPlus m) => MonadPlus (StateT s m) where mzero = StateT $ \_ -> mzero m `mplus` n = StateT $ \s -> runStateT m s `mplus` runStateT n s instance (MonadFix m) => MonadFix (StateT s m) where mfix f = StateT $ \s -> mfix $ \ ~(a, _) -> runStateT (f a) s instance MonadTrans (StateT s) where lift m = StateT $ \s -> do a <- m return (a, s) instance (MonadIO m) => MonadIO (StateT s m) where liftIO = lift . liftIO -- | Fetch the current value of the state within the monad. get :: (Monad m) => StateT s m s get = state $ \s -> (s, s) -- | @'put' s@ sets the state within the monad to @s@. put :: (Monad m) => s -> StateT s m () put s = state $ \_ -> ((), s) -- | @'modify' f@ is an action that updates the state to the result of -- applying @f@ to the current state. -- -- * @'modify' f = 'get' >>= ('put' . f)@ modify :: (Monad m) => (s -> s) -> StateT s m () modify f = state $ \s -> ((), f s) -- | Get a specific component of the state, using a projection function -- supplied. -- -- * @'gets' f = 'liftM' f 'get'@ gets :: (Monad m) => (s -> a) -> StateT s m a gets f = state $ \s -> (f s, s) -- | Uniform lifting of a @callCC@ operation to the new monad. -- This version rolls back to the original state on entering the -- continuation. liftCallCC :: ((((a,s) -> m (b,s)) -> m (a,s)) -> m (a,s)) -> ((a -> StateT s m b) -> StateT s m a) -> StateT s m a liftCallCC callCC f = StateT $ \s -> callCC $ \c -> runStateT (f (\a -> StateT $ \ _ -> c (a, s))) s -- | In-situ lifting of a @callCC@ operation to the new monad. -- This version uses the current state on entering the continuation. -- It does not satisfy the laws of a monad transformer. liftCallCC' :: ((((a,s) -> m (b,s)) -> m (a,s)) -> m (a,s)) -> ((a -> StateT s m b) -> StateT s m a) -> StateT s m a liftCallCC' callCC f = StateT $ \s -> callCC $ \c -> runStateT (f (\a -> StateT $ \s' -> c (a, s'))) s -- | Lift a @catchError@ operation to the new monad. liftCatch :: (m (a,s) -> (e -> m (a,s)) -> m (a,s)) -> StateT s m a -> (e -> StateT s m a) -> StateT s m a liftCatch catchError m h = StateT $ \s -> runStateT m s `catchError` \e -> runStateT (h e) s -- | Lift a @listen@ operation to the new monad. liftListen :: Monad m => (m (a,s) -> m ((a,s),w)) -> StateT s m a -> StateT s m (a,w) liftListen listen m = StateT $ \s -> do ~((a, s'), w) <- listen (runStateT m s) return ((a, w), s') -- | Lift a @pass@ operation to the new monad. liftPass :: Monad m => (m ((a,s),b) -> m (a,s)) -> StateT s m (a,b) -> StateT s m a liftPass pass m = StateT $ \s -> pass $ do ~((a, f), s') <- runStateT m s return ((a, s'), f) {- $examples Parser from ParseLib with Hugs: > type Parser a = StateT String [] a > ==> StateT (String -> [(a,String)]) For example, item can be written as: > item = do (x:xs) <- get > put xs > return x > > type BoringState s a = StateT s Identity a > ==> StateT (s -> Identity (a,s)) > > type StateWithIO s a = StateT s IO a > ==> StateT (s -> IO (a,s)) > > type StateWithErr s a = StateT s Maybe a > ==> StateT (s -> Maybe (a,s)) -} {- $counting A function to increment a counter. Taken from the paper \"Generalising Monads to Arrows\", John Hughes (), November 1998: > tick :: State Int Int > tick = do n <- get > put (n+1) > return n Add one to the given number using the state monad: > plusOne :: Int -> Int > plusOne n = execState tick n A contrived addition example. Works only with positive numbers: > plus :: Int -> Int -> Int > plus n x = execState (sequence $ replicate n tick) x -} {- $labelling An example from /The Craft of Functional Programming/, Simon Thompson (), Addison-Wesley 1999: \"Given an arbitrary tree, transform it to a tree of integers in which the original elements are replaced by natural numbers, starting from 0. The same element has to be replaced by the same number at every occurrence, and when we meet an as-yet-unvisited element we have to find a \'new\' number to match it with:\" > data Tree a = Nil | Node a (Tree a) (Tree a) deriving (Show, Eq) > type Table a = [a] > numberTree :: Eq a => Tree a -> State (Table a) (Tree Int) > numberTree Nil = return Nil > numberTree (Node x t1 t2) > = do num <- numberNode x > nt1 <- numberTree t1 > nt2 <- numberTree t2 > return (Node num nt1 nt2) > where > numberNode :: Eq a => a -> State (Table a) Int > numberNode x > = do table <- get > (newTable, newPos) <- return (nNode x table) > put newTable > return newPos > nNode:: (Eq a) => a -> Table a -> (Table a, Int) > nNode x table > = case (findIndexInList (== x) table) of > Nothing -> (table ++ [x], length table) > Just i -> (table, i) > findIndexInList :: (a -> Bool) -> [a] -> Maybe Int > findIndexInList = findIndexInListHelp 0 > findIndexInListHelp _ _ [] = Nothing > findIndexInListHelp count f (h:t) > = if (f h) > then Just count > else findIndexInListHelp (count+1) f t numTree applies numberTree with an initial state: > numTree :: (Eq a) => Tree a -> Tree Int > numTree t = evalState (numberTree t) [] > testTree = Node "Zero" (Node "One" (Node "Two" Nil Nil) (Node "One" (Node "Zero" Nil Nil) Nil)) Nil > numTree testTree => Node 0 (Node 1 (Node 2 Nil Nil) (Node 1 (Node 0 Nil Nil) Nil)) Nil sumTree is a little helper function that does not use the State monad: > sumTree :: (Num a) => Tree a -> a > sumTree Nil = 0 > sumTree (Node e t1 t2) = e + (sumTree t1) + (sumTree t2) -} transformers-0.3.0.0/Control/Monad/Trans/Writer/0000755000000000000000000000000011732646061017612 5ustar0000000000000000transformers-0.3.0.0/Control/Monad/Trans/Writer/Strict.hs0000644000000000000000000001637111732646061021426 0ustar0000000000000000----------------------------------------------------------------------------- -- | -- Module : Control.Monad.Trans.Writer.Strict -- Copyright : (c) Andy Gill 2001, -- (c) Oregon Graduate Institute of Science and Technology, 2001 -- License : BSD-style (see the file LICENSE) -- -- Maintainer : ross@soi.city.ac.uk -- Stability : experimental -- Portability : portable -- -- The strict 'WriterT' monad transformer, which adds collection of -- outputs (such as a count or string output) to a given monad. -- -- This version builds its output strictly; for a lazy version, see -- "Control.Monad.Trans.Writer.Lazy", which has the same interface. -- -- This monad transformer provides only limited access to the output -- during the computation. For more general access, use -- "Control.Monad.Trans.State" instead. ----------------------------------------------------------------------------- module Control.Monad.Trans.Writer.Strict ( -- * The Writer monad Writer, writer, runWriter, execWriter, mapWriter, -- * The WriterT monad transformer WriterT(..), execWriterT, mapWriterT, -- * Writer operations tell, listen, listens, pass, censor, -- * Lifting other operations liftCallCC, liftCatch, ) where import Control.Monad.IO.Class import Control.Monad.Trans.Class import Data.Functor.Identity import Control.Applicative import Control.Monad import Control.Monad.Fix import Data.Foldable (Foldable(foldMap)) import Data.Monoid import Data.Traversable (Traversable(traverse)) -- --------------------------------------------------------------------------- -- | A writer monad parameterized by the type @w@ of output to accumulate. -- -- The 'return' function produces the output 'mempty', while @>>=@ -- combines the outputs of the subcomputations using 'mappend'. type Writer w = WriterT w Identity -- | Construct a writer computation from a (result, output) pair. -- (The inverse of 'runWriter'.) writer :: Monad m => (a, w) -> WriterT w m a writer = WriterT . return -- | Unwrap a writer computation as a (result, output) pair. -- (The inverse of 'writer'.) runWriter :: Writer w a -> (a, w) runWriter = runIdentity . runWriterT -- | Extract the output from a writer computation. -- -- * @'execWriter' m = 'snd' ('runWriter' m)@ execWriter :: Writer w a -> w execWriter m = snd (runWriter m) -- | Map both the return value and output of a computation using -- the given function. -- -- * @'runWriter' ('mapWriter' f m) = f ('runWriter' m)@ mapWriter :: ((a, w) -> (b, w')) -> Writer w a -> Writer w' b mapWriter f = mapWriterT (Identity . f . runIdentity) -- --------------------------------------------------------------------------- -- | A writer monad parameterized by: -- -- * @w@ - the output to accumulate. -- -- * @m@ - The inner monad. -- -- The 'return' function produces the output 'mempty', while @>>=@ -- combines the outputs of the subcomputations using 'mappend'. newtype WriterT w m a = WriterT { runWriterT :: m (a, w) } -- | Extract the output from a writer computation. -- -- * @'execWriterT' m = 'liftM' 'snd' ('runWriterT' m)@ execWriterT :: Monad m => WriterT w m a -> m w execWriterT m = do (_, w) <- runWriterT m return w -- | Map both the return value and output of a computation using -- the given function. -- -- * @'runWriterT' ('mapWriterT' f m) = f ('runWriterT' m)@ mapWriterT :: (m (a, w) -> n (b, w')) -> WriterT w m a -> WriterT w' n b mapWriterT f m = WriterT $ f (runWriterT m) instance (Functor m) => Functor (WriterT w m) where fmap f = mapWriterT $ fmap $ \ (a, w) -> (f a, w) instance (Foldable f) => Foldable (WriterT w f) where foldMap f (WriterT a) = foldMap (f . fst) a instance (Traversable f) => Traversable (WriterT w f) where traverse f (WriterT a) = WriterT <$> traverse f' a where f' (a, b) = fmap (\c -> (c, b)) (f a) instance (Monoid w, Applicative m) => Applicative (WriterT w m) where pure a = WriterT $ pure (a, mempty) f <*> v = WriterT $ liftA2 k (runWriterT f) (runWriterT v) where k (a, w) (b, w') = (a b, w `mappend` w') instance (Monoid w, Alternative m) => Alternative (WriterT w m) where empty = WriterT empty m <|> n = WriterT $ runWriterT m <|> runWriterT n instance (Monoid w, Monad m) => Monad (WriterT w m) where return a = WriterT $ return (a, mempty) m >>= k = WriterT $ do (a, w) <- runWriterT m (b, w') <- runWriterT (k a) return (b, w `mappend` w') fail msg = WriterT $ fail msg instance (Monoid w, MonadPlus m) => MonadPlus (WriterT w m) where mzero = WriterT mzero m `mplus` n = WriterT $ runWriterT m `mplus` runWriterT n instance (Monoid w, MonadFix m) => MonadFix (WriterT w m) where mfix m = WriterT $ mfix $ \ ~(a, _) -> runWriterT (m a) instance (Monoid w) => MonadTrans (WriterT w) where lift m = WriterT $ do a <- m return (a, mempty) instance (Monoid w, MonadIO m) => MonadIO (WriterT w m) where liftIO = lift . liftIO -- | @'tell' w@ is an action that produces the output @w@. tell :: (Monoid w, Monad m) => w -> WriterT w m () tell w = WriterT $ return ((), w) -- | @'listen' m@ is an action that executes the action @m@ and adds its -- output to the value of the computation. -- -- * @'runWriterT' ('listen' m) = 'liftM' (\\(a, w) -> ((a, w), w)) ('runWriterT' m)@ listen :: (Monoid w, Monad m) => WriterT w m a -> WriterT w m (a, w) listen m = WriterT $ do (a, w) <- runWriterT m return ((a, w), w) -- | @'listens' f m@ is an action that executes the action @m@ and adds -- the result of applying @f@ to the output to the value of the computation. -- -- * @'listens' f m = 'liftM' (id *** f) ('listen' m)@ -- -- * @'runWriterT' ('listens' f m) = 'liftM' (\\(a, w) -> ((a, f w), w)) ('runWriterT' m)@ listens :: (Monoid w, Monad m) => (w -> b) -> WriterT w m a -> WriterT w m (a, b) listens f m = WriterT $ do (a, w) <- runWriterT m return ((a, f w), w) -- | @'pass' m@ is an action that executes the action @m@, which returns -- a value and a function, and returns the value, applying the function -- to the output. -- -- * @'runWriterT' ('pass' m) = 'liftM' (\\((a, f), w) -> (a, f w)) ('runWriterT' m)@ pass :: (Monoid w, Monad m) => WriterT w m (a, w -> w) -> WriterT w m a pass m = WriterT $ do ((a, f), w) <- runWriterT m return (a, f w) -- | @'censor' f m@ is an action that executes the action @m@ and -- applies the function @f@ to its output, leaving the return value -- unchanged. -- -- * @'censor' f m = 'pass' ('liftM' (\\x -> (x,f)) m)@ -- -- * @'runWriterT' ('censor' f m) = 'liftM' (\\(a, w) -> (a, f w)) ('runWriterT' m)@ censor :: (Monoid w, Monad m) => (w -> w) -> WriterT w m a -> WriterT w m a censor f m = WriterT $ do (a, w) <- runWriterT m return (a, f w) -- | Lift a @callCC@ operation to the new monad. liftCallCC :: (Monoid w) => ((((a,w) -> m (b,w)) -> m (a,w)) -> m (a,w)) -> ((a -> WriterT w m b) -> WriterT w m a) -> WriterT w m a liftCallCC callCC f = WriterT $ callCC $ \c -> runWriterT (f (\a -> WriterT $ c (a, mempty))) -- | Lift a @catchError@ operation to the new monad. liftCatch :: (m (a,w) -> (e -> m (a,w)) -> m (a,w)) -> WriterT w m a -> (e -> WriterT w m a) -> WriterT w m a liftCatch catchError m h = WriterT $ runWriterT m `catchError` \e -> runWriterT (h e) transformers-0.3.0.0/Control/Monad/Trans/Writer/Lazy.hs0000644000000000000000000001634711732646061021100 0ustar0000000000000000----------------------------------------------------------------------------- -- | -- Module : Control.Monad.Trans.Writer.Lazy -- Copyright : (c) Andy Gill 2001, -- (c) Oregon Graduate Institute of Science and Technology, 2001 -- License : BSD-style (see the file LICENSE) -- -- Maintainer : ross@soi.city.ac.uk -- Stability : experimental -- Portability : portable -- -- The lazy 'WriterT' monad transformer, which adds collection of -- outputs (such as a count or string output) to a given monad. -- -- This version builds its output lazily; for a strict version, see -- "Control.Monad.Trans.Writer.Strict", which has the same interface. -- -- This monad transformer provides only limited access to the output -- during the computation. For more general access, use -- "Control.Monad.Trans.State" instead. ----------------------------------------------------------------------------- module Control.Monad.Trans.Writer.Lazy ( -- * The Writer monad Writer, writer, runWriter, execWriter, mapWriter, -- * The WriterT monad transformer WriterT(..), execWriterT, mapWriterT, -- * Writer operations tell, listen, listens, pass, censor, -- * Lifting other operations liftCallCC, liftCatch, ) where import Control.Monad.IO.Class import Control.Monad.Trans.Class import Data.Functor.Identity import Control.Applicative import Control.Monad import Control.Monad.Fix import Data.Foldable (Foldable(foldMap)) import Data.Monoid import Data.Traversable (Traversable(traverse)) -- --------------------------------------------------------------------------- -- | A writer monad parameterized by the type @w@ of output to accumulate. -- -- The 'return' function produces the output 'mempty', while @>>=@ -- combines the outputs of the subcomputations using 'mappend'. type Writer w = WriterT w Identity -- | Construct a writer computation from a (result, output) pair. -- (The inverse of 'runWriter'.) writer :: Monad m => (a, w) -> WriterT w m a writer = WriterT . return -- | Unwrap a writer computation as a (result, output) pair. -- (The inverse of 'writer'.) runWriter :: Writer w a -> (a, w) runWriter = runIdentity . runWriterT -- | Extract the output from a writer computation. -- -- * @'execWriter' m = 'snd' ('runWriter' m)@ execWriter :: Writer w a -> w execWriter m = snd (runWriter m) -- | Map both the return value and output of a computation using -- the given function. -- -- * @'runWriter' ('mapWriter' f m) = f ('runWriter' m)@ mapWriter :: ((a, w) -> (b, w')) -> Writer w a -> Writer w' b mapWriter f = mapWriterT (Identity . f . runIdentity) -- --------------------------------------------------------------------------- -- | A writer monad parameterized by: -- -- * @w@ - the output to accumulate. -- -- * @m@ - The inner monad. -- -- The 'return' function produces the output 'mempty', while @>>=@ -- combines the outputs of the subcomputations using 'mappend'. newtype WriterT w m a = WriterT { runWriterT :: m (a, w) } -- | Extract the output from a writer computation. -- -- * @'execWriterT' m = 'liftM' 'snd' ('runWriterT' m)@ execWriterT :: Monad m => WriterT w m a -> m w execWriterT m = do ~(_, w) <- runWriterT m return w -- | Map both the return value and output of a computation using -- the given function. -- -- * @'runWriterT' ('mapWriterT' f m) = f ('runWriterT' m)@ mapWriterT :: (m (a, w) -> n (b, w')) -> WriterT w m a -> WriterT w' n b mapWriterT f m = WriterT $ f (runWriterT m) instance (Functor m) => Functor (WriterT w m) where fmap f = mapWriterT $ fmap $ \ ~(a, w) -> (f a, w) instance (Monoid w, Applicative m) => Applicative (WriterT w m) where pure a = WriterT $ pure (a, mempty) f <*> v = WriterT $ liftA2 k (runWriterT f) (runWriterT v) where k ~(a, w) ~(b, w') = (a b, w `mappend` w') instance (Monoid w, Alternative m) => Alternative (WriterT w m) where empty = WriterT empty m <|> n = WriterT $ runWriterT m <|> runWriterT n instance (Monoid w, Monad m) => Monad (WriterT w m) where return a = writer (a, mempty) m >>= k = WriterT $ do ~(a, w) <- runWriterT m ~(b, w') <- runWriterT (k a) return (b, w `mappend` w') fail msg = WriterT $ fail msg instance (Monoid w, MonadPlus m) => MonadPlus (WriterT w m) where mzero = WriterT mzero m `mplus` n = WriterT $ runWriterT m `mplus` runWriterT n instance (Monoid w, MonadFix m) => MonadFix (WriterT w m) where mfix m = WriterT $ mfix $ \ ~(a, _) -> runWriterT (m a) instance (Monoid w) => MonadTrans (WriterT w) where lift m = WriterT $ do a <- m return (a, mempty) instance (Monoid w, MonadIO m) => MonadIO (WriterT w m) where liftIO = lift . liftIO instance Foldable f => Foldable (WriterT w f) where foldMap f (WriterT a) = foldMap (f . fst) a instance Traversable f => Traversable (WriterT w f) where traverse f (WriterT a) = WriterT <$> traverse f' a where f' (a, b) = fmap (\c -> (c, b)) (f a) -- | @'tell' w@ is an action that produces the output @w@. tell :: (Monoid w, Monad m) => w -> WriterT w m () tell w = writer ((), w) -- | @'listen' m@ is an action that executes the action @m@ and adds its -- output to the value of the computation. -- -- * @'runWriterT' ('listen' m) = 'liftM' (\\(a, w) -> ((a, w), w)) ('runWriterT' m)@ listen :: (Monoid w, Monad m) => WriterT w m a -> WriterT w m (a, w) listen m = WriterT $ do ~(a, w) <- runWriterT m return ((a, w), w) -- | @'listens' f m@ is an action that executes the action @m@ and adds -- the result of applying @f@ to the output to the value of the computation. -- -- * @'listens' f m = 'liftM' (id *** f) ('listen' m)@ -- -- * @'runWriterT' ('listens' f m) = 'liftM' (\\(a, w) -> ((a, f w), w)) ('runWriterT' m)@ listens :: (Monoid w, Monad m) => (w -> b) -> WriterT w m a -> WriterT w m (a, b) listens f m = WriterT $ do ~(a, w) <- runWriterT m return ((a, f w), w) -- | @'pass' m@ is an action that executes the action @m@, which returns -- a value and a function, and returns the value, applying the function -- to the output. -- -- * @'runWriterT' ('pass' m) = 'liftM' (\\((a, f), w) -> (a, f w)) ('runWriterT' m)@ pass :: (Monoid w, Monad m) => WriterT w m (a, w -> w) -> WriterT w m a pass m = WriterT $ do ~((a, f), w) <- runWriterT m return (a, f w) -- | @'censor' f m@ is an action that executes the action @m@ and -- applies the function @f@ to its output, leaving the return value -- unchanged. -- -- * @'censor' f m = 'pass' ('liftM' (\\x -> (x,f)) m)@ -- -- * @'runWriterT' ('censor' f m) = 'liftM' (\\(a, w) -> (a, f w)) ('runWriterT' m)@ censor :: (Monoid w, Monad m) => (w -> w) -> WriterT w m a -> WriterT w m a censor f m = WriterT $ do ~(a, w) <- runWriterT m return (a, f w) -- | Lift a @callCC@ operation to the new monad. liftCallCC :: (Monoid w) => ((((a,w) -> m (b,w)) -> m (a,w)) -> m (a,w)) -> ((a -> WriterT w m b) -> WriterT w m a) -> WriterT w m a liftCallCC callCC f = WriterT $ callCC $ \c -> runWriterT (f (\a -> WriterT $ c (a, mempty))) -- | Lift a @catchError@ operation to the new monad. liftCatch :: (m (a,w) -> (e -> m (a,w)) -> m (a,w)) -> WriterT w m a -> (e -> WriterT w m a) -> WriterT w m a liftCatch catchError m h = WriterT $ runWriterT m `catchError` \e -> runWriterT (h e) transformers-0.3.0.0/Control/Monad/Trans/RWS/0000755000000000000000000000000011732646061017011 5ustar0000000000000000transformers-0.3.0.0/Control/Monad/Trans/RWS/Strict.hs0000644000000000000000000002627511732646061020631 0ustar0000000000000000----------------------------------------------------------------------------- -- | -- Module : Control.Monad.Trans.RWS.Strict -- Copyright : (c) Andy Gill 2001, -- (c) Oregon Graduate Institute of Science and Technology, 2001 -- License : BSD-style (see the file LICENSE) -- -- Maintainer : ross@soi.city.ac.uk -- Stability : experimental -- Portability : portable -- -- A monad transformer that combines 'ReaderT', 'WriterT' and 'StateT'. -- This version is strict; for a lazy version, see -- "Control.Monad.Trans.RWS.Lazy", which has the same interface. ----------------------------------------------------------------------------- module Control.Monad.Trans.RWS.Strict ( -- * The RWS monad RWS, rws, runRWS, evalRWS, execRWS, mapRWS, withRWS, -- * The RWST monad transformer RWST(..), evalRWST, execRWST, mapRWST, withRWST, -- * Reader operations reader, ask, local, asks, -- * Writer operations writer, tell, listen, listens, pass, censor, -- * State operations state, get, put, modify, gets, -- * Lifting other operations liftCallCC, liftCallCC', liftCatch, ) where import Control.Monad.IO.Class import Control.Monad.Trans.Class import Data.Functor.Identity import Control.Applicative import Control.Monad import Control.Monad.Fix import Data.Monoid -- | A monad containing an environment of type @r@, output of type @w@ -- and an updatable state of type @s@. type RWS r w s = RWST r w s Identity -- | Construct an RWS computation from a function. -- (The inverse of 'runRWS'.) rws :: (r -> s -> (a, s, w)) -> RWS r w s a rws f = RWST (\ r s -> Identity (f r s)) -- | Unwrap an RWS computation as a function. -- (The inverse of 'rws'.) runRWS :: RWS r w s a -> r -> s -> (a, s, w) runRWS m r s = runIdentity (runRWST m r s) -- | Evaluate a computation with the given initial state and environment, -- returning the final value and output, discarding the final state. evalRWS :: RWS r w s a -- ^RWS computation to execute -> r -- ^initial environment -> s -- ^initial value -> (a, w) -- ^final value and output evalRWS m r s = let (a, _, w) = runRWS m r s in (a, w) -- | Evaluate a computation with the given initial state and environment, -- returning the final state and output, discarding the final value. execRWS :: RWS r w s a -- ^RWS computation to execute -> r -- ^initial environment -> s -- ^initial value -> (s, w) -- ^final state and output execRWS m r s = let (_, s', w) = runRWS m r s in (s', w) -- | Map the return value, final state and output of a computation using -- the given function. -- -- * @'runRWS' ('mapRWS' f m) r s = f ('runRWS' m r s)@ mapRWS :: ((a, s, w) -> (b, s, w')) -> RWS r w s a -> RWS r w' s b mapRWS f = mapRWST (Identity . f . runIdentity) -- | @'withRWS' f m@ executes action @m@ with an initial environment -- and state modified by applying @f@. -- -- * @'runRWS' ('withRWS' f m) r s = 'uncurry' ('runRWS' m) (f r s)@ withRWS :: (r' -> s -> (r, s)) -> RWS r w s a -> RWS r' w s a withRWS = withRWST -- --------------------------------------------------------------------------- -- | A monad transformer adding reading an environment of type @r@, -- collecting an output of type @w@ and updating a state of type @s@ -- to an inner monad @m@. newtype RWST r w s m a = RWST { runRWST :: r -> s -> m (a, s, w) } -- | Evaluate a computation with the given initial state and environment, -- returning the final value and output, discarding the final state. evalRWST :: (Monad m) => RWST r w s m a -- ^computation to execute -> r -- ^initial environment -> s -- ^initial value -> m (a, w) -- ^computation yielding final value and output evalRWST m r s = do (a, _, w) <- runRWST m r s return (a, w) -- | Evaluate a computation with the given initial state and environment, -- returning the final state and output, discarding the final value. execRWST :: (Monad m) => RWST r w s m a -- ^computation to execute -> r -- ^initial environment -> s -- ^initial value -> m (s, w) -- ^computation yielding final state and output execRWST m r s = do (_, s', w) <- runRWST m r s return (s', w) -- | Map the inner computation using the given function. -- -- * @'runRWST' ('mapRWST' f m) r s = f ('runRWST' m r s)@ mapRWST :: (m (a, s, w) -> n (b, s, w')) -> RWST r w s m a -> RWST r w' s n b mapRWST f m = RWST $ \r s -> f (runRWST m r s) -- | @'withRWST' f m@ executes action @m@ with an initial environment -- and state modified by applying @f@. -- -- * @'runRWST' ('withRWST' f m) r s = 'uncurry' ('runRWST' m) (f r s)@ withRWST :: (r' -> s -> (r, s)) -> RWST r w s m a -> RWST r' w s m a withRWST f m = RWST $ \r s -> uncurry (runRWST m) (f r s) instance (Functor m) => Functor (RWST r w s m) where fmap f m = RWST $ \r s -> fmap (\ (a, s', w) -> (f a, s', w)) $ runRWST m r s instance (Monoid w, Functor m, Monad m) => Applicative (RWST r w s m) where pure = return (<*>) = ap instance (Monoid w, Functor m, MonadPlus m) => Alternative (RWST r w s m) where empty = mzero (<|>) = mplus instance (Monoid w, Monad m) => Monad (RWST r w s m) where return a = RWST $ \_ s -> return (a, s, mempty) m >>= k = RWST $ \r s -> do (a, s', w) <- runRWST m r s (b, s'',w') <- runRWST (k a) r s' return (b, s'', w `mappend` w') fail msg = RWST $ \_ _ -> fail msg instance (Monoid w, MonadPlus m) => MonadPlus (RWST r w s m) where mzero = RWST $ \_ _ -> mzero m `mplus` n = RWST $ \r s -> runRWST m r s `mplus` runRWST n r s instance (Monoid w, MonadFix m) => MonadFix (RWST r w s m) where mfix f = RWST $ \r s -> mfix $ \ ~(a, _, _) -> runRWST (f a) r s instance (Monoid w) => MonadTrans (RWST r w s) where lift m = RWST $ \_ s -> do a <- m return (a, s, mempty) instance (Monoid w, MonadIO m) => MonadIO (RWST r w s m) where liftIO = lift . liftIO -- --------------------------------------------------------------------------- -- Reader operations -- | Constructor for computations in the reader monad (equivalent to 'asks'). reader :: (Monoid w, Monad m) => (r -> a) -> RWST r w s m a reader = asks -- | Fetch the value of the environment. ask :: (Monoid w, Monad m) => RWST r w s m r ask = RWST $ \r s -> return (r, s, mempty) -- | Execute a computation in a modified environment -- -- * @'runRWST' ('local' f m) r s = 'runRWST' m (f r) s@ local :: (Monoid w, Monad m) => (r -> r) -> RWST r w s m a -> RWST r w s m a local f m = RWST $ \r s -> runRWST m (f r) s -- | Retrieve a function of the current environment. -- -- * @'asks' f = 'liftM' f 'ask'@ asks :: (Monoid w, Monad m) => (r -> a) -> RWST r w s m a asks f = RWST $ \r s -> return (f r, s, mempty) -- --------------------------------------------------------------------------- -- Writer operations -- | Construct a writer computation from a (result, output) pair. writer :: Monad m => (a, w) -> RWST r w s m a writer (a, w) = RWST $ \_ s -> return (a, s, w) -- | @'tell' w@ is an action that produces the output @w@. tell :: (Monoid w, Monad m) => w -> RWST r w s m () tell w = RWST $ \_ s -> return ((),s,w) -- | @'listen' m@ is an action that executes the action @m@ and adds its -- output to the value of the computation. -- -- * @'runRWST' ('listen' m) r s = 'liftM' (\\(a, w) -> ((a, w), w)) ('runRWST' m r s)@ listen :: (Monoid w, Monad m) => RWST r w s m a -> RWST r w s m (a, w) listen m = RWST $ \r s -> do (a, s', w) <- runRWST m r s return ((a, w), s', w) -- | @'listens' f m@ is an action that executes the action @m@ and adds -- the result of applying @f@ to the output to the value of the computation. -- -- * @'listens' f m = 'liftM' (id *** f) ('listen' m)@ -- -- * @'runRWST' ('listens' f m) r s = 'liftM' (\\(a, w) -> ((a, f w), w)) ('runRWST' m r s)@ listens :: (Monoid w, Monad m) => (w -> b) -> RWST r w s m a -> RWST r w s m (a, b) listens f m = RWST $ \r s -> do (a, s', w) <- runRWST m r s return ((a, f w), s', w) -- | @'pass' m@ is an action that executes the action @m@, which returns -- a value and a function, and returns the value, applying the function -- to the output. -- -- * @'runRWST' ('pass' m) r s = 'liftM' (\\((a, f), w) -> (a, f w)) ('runRWST' m r s)@ pass :: (Monoid w, Monad m) => RWST r w s m (a, w -> w) -> RWST r w s m a pass m = RWST $ \r s -> do ((a, f), s', w) <- runRWST m r s return (a, s', f w) -- | @'censor' f m@ is an action that executes the action @m@ and -- applies the function @f@ to its output, leaving the return value -- unchanged. -- -- * @'censor' f m = 'pass' ('liftM' (\\x -> (x,f)) m)@ -- -- * @'runRWST' ('censor' f m) r s = 'liftM' (\\(a, w) -> (a, f w)) ('runRWST' m r s)@ censor :: (Monoid w, Monad m) => (w -> w) -> RWST r w s m a -> RWST r w s m a censor f m = RWST $ \r s -> do (a, s', w) <- runRWST m r s return (a, s', f w) -- --------------------------------------------------------------------------- -- State operations -- | Construct a state monad computation from a state transformer function. state :: (Monoid w, Monad m) => (s -> (a,s)) -> RWST r w s m a state f = RWST $ \_ s -> case f s of (a,s') -> return (a, s', mempty) -- | Fetch the current value of the state within the monad. get :: (Monoid w, Monad m) => RWST r w s m s get = RWST $ \_ s -> return (s, s, mempty) -- | @'put' s@ sets the state within the monad to @s@. put :: (Monoid w, Monad m) => s -> RWST r w s m () put s = RWST $ \_ _ -> return ((), s, mempty) -- | @'modify' f@ is an action that updates the state to the result of -- applying @f@ to the current state. -- -- * @'modify' f = 'get' >>= ('put' . f)@ modify :: (Monoid w, Monad m) => (s -> s) -> RWST r w s m () modify f = RWST $ \_ s -> return ((), f s, mempty) -- | Get a specific component of the state, using a projection function -- supplied. -- -- * @'gets' f = 'liftM' f 'get'@ gets :: (Monoid w, Monad m) => (s -> a) -> RWST r w s m a gets f = RWST $ \_ s -> return (f s, s, mempty) -- | Uniform lifting of a @callCC@ operation to the new monad. -- This version rolls back to the original state on entering the -- continuation. liftCallCC :: (Monoid w) => ((((a,s,w) -> m (b,s,w)) -> m (a,s,w)) -> m (a,s,w)) -> ((a -> RWST r w s m b) -> RWST r w s m a) -> RWST r w s m a liftCallCC callCC f = RWST $ \r s -> callCC $ \c -> runRWST (f (\a -> RWST $ \_ _ -> c (a, s, mempty))) r s -- | In-situ lifting of a @callCC@ operation to the new monad. -- This version uses the current state on entering the continuation. liftCallCC' :: (Monoid w) => ((((a,s,w) -> m (b,s,w)) -> m (a,s,w)) -> m (a,s,w)) -> ((a -> RWST r w s m b) -> RWST r w s m a) -> RWST r w s m a liftCallCC' callCC f = RWST $ \r s -> callCC $ \c -> runRWST (f (\a -> RWST $ \_ s' -> c (a, s', mempty))) r s -- | Lift a @catchError@ operation to the new monad. liftCatch :: (m (a,s,w) -> (e -> m (a,s,w)) -> m (a,s,w)) -> RWST l w s m a -> (e -> RWST l w s m a) -> RWST l w s m a liftCatch catchError m h = RWST $ \r s -> runRWST m r s `catchError` \e -> runRWST (h e) r s transformers-0.3.0.0/Control/Monad/Trans/RWS/Lazy.hs0000644000000000000000000002630411732646061020271 0ustar0000000000000000----------------------------------------------------------------------------- -- | -- Module : Control.Monad.Trans.RWS.Lazy -- Copyright : (c) Andy Gill 2001, -- (c) Oregon Graduate Institute of Science and Technology, 2001 -- License : BSD-style (see the file LICENSE) -- -- Maintainer : ross@soi.city.ac.uk -- Stability : experimental -- Portability : portable -- -- A monad transformer that combines 'ReaderT', 'WriterT' and 'StateT'. -- This version is lazy; for a strict version, see -- "Control.Monad.Trans.RWS.Strict", which has the same interface. ----------------------------------------------------------------------------- module Control.Monad.Trans.RWS.Lazy ( -- * The RWS monad RWS, rws, runRWS, evalRWS, execRWS, mapRWS, withRWS, -- * The RWST monad transformer RWST(..), evalRWST, execRWST, mapRWST, withRWST, -- * Reader operations reader, ask, local, asks, -- * Writer operations writer, tell, listen, listens, pass, censor, -- * State operations state, get, put, modify, gets, -- * Lifting other operations liftCallCC, liftCallCC', liftCatch, ) where import Control.Monad.IO.Class import Control.Monad.Trans.Class import Data.Functor.Identity import Control.Applicative import Control.Monad import Control.Monad.Fix import Data.Monoid -- | A monad containing an environment of type @r@, output of type @w@ -- and an updatable state of type @s@. type RWS r w s = RWST r w s Identity -- | Construct an RWS computation from a function. -- (The inverse of 'runRWS'.) rws :: (r -> s -> (a, s, w)) -> RWS r w s a rws f = RWST (\ r s -> Identity (f r s)) -- | Unwrap an RWS computation as a function. -- (The inverse of 'rws'.) runRWS :: RWS r w s a -> r -> s -> (a, s, w) runRWS m r s = runIdentity (runRWST m r s) -- | Evaluate a computation with the given initial state and environment, -- returning the final value and output, discarding the final state. evalRWS :: RWS r w s a -- ^RWS computation to execute -> r -- ^initial environment -> s -- ^initial value -> (a, w) -- ^final value and output evalRWS m r s = let (a, _, w) = runRWS m r s in (a, w) -- | Evaluate a computation with the given initial state and environment, -- returning the final state and output, discarding the final value. execRWS :: RWS r w s a -- ^RWS computation to execute -> r -- ^initial environment -> s -- ^initial value -> (s, w) -- ^final state and output execRWS m r s = let (_, s', w) = runRWS m r s in (s', w) -- | Map the return value, final state and output of a computation using -- the given function. -- -- * @'runRWS' ('mapRWS' f m) r s = f ('runRWS' m r s)@ mapRWS :: ((a, s, w) -> (b, s, w')) -> RWS r w s a -> RWS r w' s b mapRWS f = mapRWST (Identity . f . runIdentity) -- | @'withRWS' f m@ executes action @m@ with an initial environment -- and state modified by applying @f@. -- -- * @'runRWS' ('withRWS' f m) r s = 'uncurry' ('runRWS' m) (f r s)@ withRWS :: (r' -> s -> (r, s)) -> RWS r w s a -> RWS r' w s a withRWS = withRWST -- --------------------------------------------------------------------------- -- | A monad transformer adding reading an environment of type @r@, -- collecting an output of type @w@ and updating a state of type @s@ -- to an inner monad @m@. newtype RWST r w s m a = RWST { runRWST :: r -> s -> m (a, s, w) } -- | Evaluate a computation with the given initial state and environment, -- returning the final value and output, discarding the final state. evalRWST :: (Monad m) => RWST r w s m a -- ^computation to execute -> r -- ^initial environment -> s -- ^initial value -> m (a, w) -- ^computation yielding final value and output evalRWST m r s = do ~(a, _, w) <- runRWST m r s return (a, w) -- | Evaluate a computation with the given initial state and environment, -- returning the final state and output, discarding the final value. execRWST :: (Monad m) => RWST r w s m a -- ^computation to execute -> r -- ^initial environment -> s -- ^initial value -> m (s, w) -- ^computation yielding final state and output execRWST m r s = do ~(_, s', w) <- runRWST m r s return (s', w) -- | Map the inner computation using the given function. -- -- * @'runRWST' ('mapRWST' f m) r s = f ('runRWST' m r s)@ mapRWST :: (m (a, s, w) -> n (b, s, w')) -> RWST r w s m a -> RWST r w' s n b mapRWST f m = RWST $ \r s -> f (runRWST m r s) -- | @'withRWST' f m@ executes action @m@ with an initial environment -- and state modified by applying @f@. -- -- * @'runRWST' ('withRWST' f m) r s = 'uncurry' ('runRWST' m) (f r s)@ withRWST :: (r' -> s -> (r, s)) -> RWST r w s m a -> RWST r' w s m a withRWST f m = RWST $ \r s -> uncurry (runRWST m) (f r s) instance (Functor m) => Functor (RWST r w s m) where fmap f m = RWST $ \r s -> fmap (\ ~(a, s', w) -> (f a, s', w)) $ runRWST m r s instance (Monoid w, Functor m, Monad m) => Applicative (RWST r w s m) where pure = return (<*>) = ap instance (Monoid w, Functor m, MonadPlus m) => Alternative (RWST r w s m) where empty = mzero (<|>) = mplus instance (Monoid w, Monad m) => Monad (RWST r w s m) where return a = RWST $ \_ s -> return (a, s, mempty) m >>= k = RWST $ \r s -> do ~(a, s', w) <- runRWST m r s ~(b, s'',w') <- runRWST (k a) r s' return (b, s'', w `mappend` w') fail msg = RWST $ \_ _ -> fail msg instance (Monoid w, MonadPlus m) => MonadPlus (RWST r w s m) where mzero = RWST $ \_ _ -> mzero m `mplus` n = RWST $ \r s -> runRWST m r s `mplus` runRWST n r s instance (Monoid w, MonadFix m) => MonadFix (RWST r w s m) where mfix f = RWST $ \r s -> mfix $ \ ~(a, _, _) -> runRWST (f a) r s instance (Monoid w) => MonadTrans (RWST r w s) where lift m = RWST $ \_ s -> do a <- m return (a, s, mempty) instance (Monoid w, MonadIO m) => MonadIO (RWST r w s m) where liftIO = lift . liftIO -- --------------------------------------------------------------------------- -- Reader operations -- | Constructor for computations in the reader monad (equivalent to 'asks'). reader :: (Monoid w, Monad m) => (r -> a) -> RWST r w s m a reader = asks -- | Fetch the value of the environment. ask :: (Monoid w, Monad m) => RWST r w s m r ask = RWST $ \r s -> return (r, s, mempty) -- | Execute a computation in a modified environment -- -- * @'runRWST' ('local' f m) r s = 'runRWST' m (f r) s@ local :: (Monoid w, Monad m) => (r -> r) -> RWST r w s m a -> RWST r w s m a local f m = RWST $ \r s -> runRWST m (f r) s -- | Retrieve a function of the current environment. -- -- * @'asks' f = 'liftM' f 'ask'@ asks :: (Monoid w, Monad m) => (r -> a) -> RWST r w s m a asks f = RWST $ \r s -> return (f r, s, mempty) -- --------------------------------------------------------------------------- -- Writer operations -- | Construct a writer computation from a (result, output) pair. writer :: Monad m => (a, w) -> RWST r w s m a writer (a, w) = RWST $ \_ s -> return (a, s, w) -- | @'tell' w@ is an action that produces the output @w@. tell :: (Monoid w, Monad m) => w -> RWST r w s m () tell w = RWST $ \_ s -> return ((),s,w) -- | @'listen' m@ is an action that executes the action @m@ and adds its -- output to the value of the computation. -- -- * @'runRWST' ('listen' m) r s = 'liftM' (\\(a, w) -> ((a, w), w)) ('runRWST' m r s)@ listen :: (Monoid w, Monad m) => RWST r w s m a -> RWST r w s m (a, w) listen m = RWST $ \r s -> do ~(a, s', w) <- runRWST m r s return ((a, w), s', w) -- | @'listens' f m@ is an action that executes the action @m@ and adds -- the result of applying @f@ to the output to the value of the computation. -- -- * @'listens' f m = 'liftM' (id *** f) ('listen' m)@ -- -- * @'runRWST' ('listens' f m) r s = 'liftM' (\\(a, w) -> ((a, f w), w)) ('runRWST' m r s)@ listens :: (Monoid w, Monad m) => (w -> b) -> RWST r w s m a -> RWST r w s m (a, b) listens f m = RWST $ \r s -> do ~(a, s', w) <- runRWST m r s return ((a, f w), s', w) -- | @'pass' m@ is an action that executes the action @m@, which returns -- a value and a function, and returns the value, applying the function -- to the output. -- -- * @'runRWST' ('pass' m) r s = 'liftM' (\\((a, f), w) -> (a, f w)) ('runRWST' m r s)@ pass :: (Monoid w, Monad m) => RWST r w s m (a, w -> w) -> RWST r w s m a pass m = RWST $ \r s -> do ~((a, f), s', w) <- runRWST m r s return (a, s', f w) -- | @'censor' f m@ is an action that executes the action @m@ and -- applies the function @f@ to its output, leaving the return value -- unchanged. -- -- * @'censor' f m = 'pass' ('liftM' (\\x -> (x,f)) m)@ -- -- * @'runRWST' ('censor' f m) r s = 'liftM' (\\(a, w) -> (a, f w)) ('runRWST' m r s)@ censor :: (Monoid w, Monad m) => (w -> w) -> RWST r w s m a -> RWST r w s m a censor f m = RWST $ \r s -> do ~(a, s', w) <- runRWST m r s return (a, s', f w) -- --------------------------------------------------------------------------- -- State operations -- | Construct a state monad computation from a state transformer function. state :: (Monoid w, Monad m) => (s -> (a,s)) -> RWST r w s m a state f = RWST $ \_ s -> let (a,s') = f s in return (a, s', mempty) -- | Fetch the current value of the state within the monad. get :: (Monoid w, Monad m) => RWST r w s m s get = RWST $ \_ s -> return (s, s, mempty) -- | @'put' s@ sets the state within the monad to @s@. put :: (Monoid w, Monad m) => s -> RWST r w s m () put s = RWST $ \_ _ -> return ((), s, mempty) -- | @'modify' f@ is an action that updates the state to the result of -- applying @f@ to the current state. -- -- * @'modify' f = 'get' >>= ('put' . f)@ modify :: (Monoid w, Monad m) => (s -> s) -> RWST r w s m () modify f = RWST $ \_ s -> return ((), f s, mempty) -- | Get a specific component of the state, using a projection function -- supplied. -- -- * @'gets' f = 'liftM' f 'get'@ gets :: (Monoid w, Monad m) => (s -> a) -> RWST r w s m a gets f = RWST $ \_ s -> return (f s, s, mempty) -- | Uniform lifting of a @callCC@ operation to the new monad. -- This version rolls back to the original state on entering the -- continuation. liftCallCC :: (Monoid w) => ((((a,s,w) -> m (b,s,w)) -> m (a,s,w)) -> m (a,s,w)) -> ((a -> RWST r w s m b) -> RWST r w s m a) -> RWST r w s m a liftCallCC callCC f = RWST $ \r s -> callCC $ \c -> runRWST (f (\a -> RWST $ \_ _ -> c (a, s, mempty))) r s -- | In-situ lifting of a @callCC@ operation to the new monad. -- This version uses the current state on entering the continuation. liftCallCC' :: (Monoid w) => ((((a,s,w) -> m (b,s,w)) -> m (a,s,w)) -> m (a,s,w)) -> ((a -> RWST r w s m b) -> RWST r w s m a) -> RWST r w s m a liftCallCC' callCC f = RWST $ \r s -> callCC $ \c -> runRWST (f (\a -> RWST $ \_ s' -> c (a, s', mempty))) r s -- | Lift a @catchError@ operation to the new monad. liftCatch :: (m (a,s,w) -> (e -> m (a,s,w)) -> m (a,s,w)) -> RWST l w s m a -> (e -> RWST l w s m a) -> RWST l w s m a liftCatch catchError m h = RWST $ \r s -> runRWST m r s `catchError` \e -> runRWST (h e) r s transformers-0.3.0.0/Control/Applicative/0000755000000000000000000000000011732646061016452 5ustar0000000000000000transformers-0.3.0.0/Control/Applicative/Lift.hs0000644000000000000000000000420711732646061017707 0ustar0000000000000000-- | -- Module : Control.Applicative.Lift -- Copyright : (c) Ross Paterson 2010 -- License : BSD-style (see the file LICENSE) -- -- Maintainer : ross@soi.city.ac.uk -- Stability : experimental -- Portability : portable -- -- Adding a new kind of pure computation to an applicative functor. module Control.Applicative.Lift ( Lift(..), unLift, -- * Collecting errors Errors, failure ) where import Control.Applicative import Data.Foldable (Foldable(foldMap)) import Data.Functor.Constant import Data.Monoid (Monoid(mappend)) import Data.Traversable (Traversable(traverse)) -- | Applicative functor formed by adding pure computations to a given -- applicative functor. data Lift f a = Pure a | Other (f a) instance (Functor f) => Functor (Lift f) where fmap f (Pure x) = Pure (f x) fmap f (Other y) = Other (fmap f y) instance (Foldable f) => Foldable (Lift f) where foldMap f (Pure x) = f x foldMap f (Other y) = foldMap f y instance (Traversable f) => Traversable (Lift f) where traverse f (Pure x) = Pure <$> f x traverse f (Other y) = Other <$> traverse f y -- | A combination is 'Pure' only if both parts are. instance (Applicative f) => Applicative (Lift f) where pure = Pure Pure f <*> Pure x = Pure (f x) Pure f <*> Other y = Other (f <$> y) Other f <*> Pure x = Other (($ x) <$> f) Other f <*> Other y = Other (f <*> y) -- | A combination is 'Pure' only either part is. instance Alternative f => Alternative (Lift f) where empty = Other empty Pure x <|> _ = Pure x Other _ <|> Pure y = Pure y Other x <|> Other y = Other (x <|> y) -- | Projection to the other functor. unLift :: Applicative f => Lift f a -> f a unLift (Pure x) = pure x unLift (Other e) = e -- | An applicative functor that collects a monoid (e.g. lists) of errors. -- A sequence of computations fails if any of its components do, but -- unlike monads made with 'ErrorT' from "Control.Monad.Trans.Error", -- these computations continue after an error, collecting all the errors. type Errors e = Lift (Constant e) -- | Report an error. failure :: Monoid e => e -> Errors e a failure e = Other (Constant e) transformers-0.3.0.0/Control/Applicative/Backwards.hs0000644000000000000000000000321311732646061020706 0ustar0000000000000000-- | -- Module : Control.Applicative.Backwards -- Copyright : (c) Russell O'Connor 2009 -- License : BSD-style (see the file LICENSE) -- -- Maintainer : libraries@haskell.org -- Stability : experimental -- Portability : portable -- -- Making functors with an 'Applicative' instance that performs actions -- in the reverse order. module Control.Applicative.Backwards where import Prelude hiding (foldr, foldr1, foldl, foldl1) import Control.Applicative import Data.Foldable import Data.Traversable -- | The same functor, but with an 'Applicative' instance that performs -- actions in the reverse order. newtype Backwards f a = Backwards { forwards :: f a } -- | Derived instance. instance (Functor f) => Functor (Backwards f) where fmap f (Backwards a) = Backwards (fmap f a) -- | Apply @f@-actions in the reverse order. instance (Applicative f) => Applicative (Backwards f) where pure a = Backwards (pure a) Backwards f <*> Backwards a = Backwards (a <**> f) -- | Try alternatives in the same order as @f@. instance (Alternative f) => Alternative (Backwards f) where empty = Backwards empty Backwards x <|> Backwards y = Backwards (x <|> y) -- | Derived instance. instance (Foldable f) => Foldable (Backwards f) where foldMap f (Backwards t) = foldMap f t foldr f z (Backwards t) = foldr f z t foldl f z (Backwards t) = foldl f z t foldr1 f (Backwards t) = foldl1 f t foldl1 f (Backwards t) = foldr1 f t -- | Derived instance. instance (Traversable f) => Traversable (Backwards f) where traverse f (Backwards t) = fmap Backwards (traverse f t) sequenceA (Backwards t) = fmap Backwards (sequenceA t)