monad-gen-0.3.0.1/0000755000000000000000000000000012444117007011704 5ustar0000000000000000monad-gen-0.3.0.1/monad-gen.cabal0000644000000000000000000000146512444117007014543 0ustar0000000000000000name: monad-gen version: 0.3.0.1 synopsis: A simple monad for generating fresh integers description: This module provides a simple monad transformer @GenT@ to enumerate unique values within a monadic computation. It also plays nicely with everything in the MTL. license: MIT license-file: LICENSE author: Danny Gratzer maintainer: danny.gratzer@gmail.com category: Utility build-type: Simple cabal-version: >=1.10 source-repository head type: hg location: https://bitbucket.org/jozefg/monad-gen library exposed-modules: Control.Monad.Gen, Control.Monad.Gen.Class build-depends: base >=4 && <5.0, mtl == 2.*, transformers > 0.3 hs-source-dirs: src default-language: Haskell2010monad-gen-0.3.0.1/LICENSE0000644000000000000000000000204112444117007012706 0ustar0000000000000000Copyright (c) 2014 Danny Gratzer Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. monad-gen-0.3.0.1/Setup.hs0000644000000000000000000000005612444117007013341 0ustar0000000000000000import Distribution.Simple main = defaultMain monad-gen-0.3.0.1/src/0000755000000000000000000000000012444117007012473 5ustar0000000000000000monad-gen-0.3.0.1/src/Control/0000755000000000000000000000000012444117007014113 5ustar0000000000000000monad-gen-0.3.0.1/src/Control/Monad/0000755000000000000000000000000012444117007015151 5ustar0000000000000000monad-gen-0.3.0.1/src/Control/Monad/Gen.hs0000644000000000000000000000707612444117007016230 0ustar0000000000000000{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-} {-# LANGUAGE UndecidableInstances, DeriveFunctor #-} {-# LANGUAGE CPP #-} module Control.Monad.Gen ( GenT , Gen , module Control.Monad.Gen.Class , Successor , successor , runGenT , runGen , runGenTWith , runGenWith , runGenTFrom , runGenFrom) where #if MIN_VERSION_mtl(2, 2, 1) import Control.Monad.Except #else import Control.Monad.Error #endif import Control.Applicative import Control.Monad.Cont.Class import Control.Monad.Gen.Class import Control.Monad.Identity import Control.Monad.Reader import Control.Monad.State import Control.Monad.Writer.Class newtype Successor a = Successor {suc :: a -> a} -- | The monad transformer for generating fresh values. data GenT e m a = GenT {unGenT :: ReaderT (Successor e) (StateT e m) a} deriving(Functor) instance Monad m => MonadGen e (GenT e m) where gen = GenT $ do s <- asks suc modify s get instance Monad m => Monad (GenT e m) where return = GenT . return (GenT m) >>= f = GenT $ m >>= unGenT . f instance MonadPlus m => MonadPlus (GenT e m) where mzero = GenT mzero mplus (GenT m) (GenT m') = GenT $ mplus m m' instance (Functor f, Monad f) => Applicative (GenT e f) where pure = GenT . pure (GenT f) <*> (GenT a) = GenT $ f <*> a instance (Monad m, Functor m, MonadPlus m) => Alternative (GenT e m) where empty = mzero (<|>) = mplus type Gen e = GenT e Identity instance MonadTrans (GenT e) where lift = GenT . lift . lift instance MonadReader r m => MonadReader r (GenT e m) where local f m = GenT $ ask >>= lift . local f . runReaderT (unGenT m) ask = GenT (lift ask) instance MonadState s m => MonadState s (GenT e m) where get = GenT $ (lift . lift) get put = GenT . lift . lift . put instance (MonadWriter w m) => MonadWriter w (GenT e m) where tell m = lift $ tell m listen = GenT . listen . unGenT pass = GenT . pass . unGenT instance MonadFix m => MonadFix (GenT e m) where mfix = GenT . mfix . (unGenT .) instance MonadIO m => MonadIO (GenT e m) where liftIO = GenT . liftIO instance MonadCont m => MonadCont (GenT e m) where callCC f = GenT $ callCC (unGenT . f . (GenT .)) instance MonadError e m => MonadError e (GenT e' m) where throwError = GenT . throwError catchError m h = GenT $ catchError (unGenT m) (unGenT . h) successor :: (e -> e) -> Successor e successor = Successor enumSucc :: Enum e => Successor e enumSucc = Successor succ -- | Run a @GenT@ computation starting from the value -- @toEnum 0@ runGenT :: (Enum e, Monad m) => GenT e m a -> m a runGenT = runGenTFrom (toEnum 0) -- | Run a @Gen@ computation starting from the value -- @toEnum 0@ runGen :: Enum e => Gen e a -> a runGen = runGenFrom (toEnum 0) -- | Run a @GenT@ computation starting from a specific value @e@. runGenTFrom :: (Monad m, Enum e) => e -> GenT e m a -> m a runGenTFrom e = runGenTWith enumSucc e -- | Run a @Gen@ computation starting from a specific value @e@. runGenFrom :: Enum e => e -> Gen e a -> a runGenFrom e = runGenWith enumSucc e -- | Run a @GenT@ computation starting from a specific value @e@ with -- a the next fresh value determined by @Successor e@. runGenTWith :: Monad m => Successor e -> e -> GenT e m a -> m a runGenTWith s e = flip evalStateT e . flip runReaderT s . unGenT -- | Run a @GenT@ computation starting from a specific value @e@ with -- a the next fresh value determined by @Successor e@. runGenWith :: Successor e -> e -> Gen e a -> a runGenWith s e = runIdentity . runGenTWith s e monad-gen-0.3.0.1/src/Control/Monad/Gen/0000755000000000000000000000000012444117007015662 5ustar0000000000000000monad-gen-0.3.0.1/src/Control/Monad/Gen/Class.hs0000644000000000000000000000367412444117007017275 0ustar0000000000000000{-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE FunctionalDependencies #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE UndecidableInstances #-} {-# LANGUAGE CPP #-} module Control.Monad.Gen.Class where -- Import the non-depricated one #if MIN_VERSION_mtl(2, 2, 1) import Control.Monad.Except #else import Control.Monad.Trans.Error #endif import Control.Monad.Cont import Control.Monad.List import Control.Monad.RWS import Control.Monad.Reader import Control.Monad.State import qualified Control.Monad.State.Strict as SS import Control.Monad.Trans.Identity import Control.Monad.Trans.Maybe import Control.Monad.Writer import qualified Control.Monad.Writer.Strict as SW -- | The MTL style class for generating fresh values class Monad m => MonadGen e m | m -> e where -- | Generate a fresh value @e@, @gen@ should never produce the -- same value within a monadic computation. gen :: m e instance MonadGen e m => MonadGen e (IdentityT m) where gen = lift gen instance MonadGen e m => MonadGen e (StateT s m) where gen = lift gen instance MonadGen e m => MonadGen e (ReaderT s m) where gen = lift gen instance (MonadGen e m, Monoid s) => MonadGen e (WriterT s m) where gen = lift gen instance MonadGen e m => MonadGen e (ListT m) where gen = lift gen instance MonadGen e m => MonadGen e (MaybeT m) where gen = lift gen instance MonadGen e m => MonadGen e (ContT r m) where gen = lift gen instance (Monoid w, MonadGen e m) => MonadGen e (RWST r w s m) where gen = lift gen instance MonadGen e m => MonadGen e (SS.StateT s m) where gen = lift gen instance (Monoid w, MonadGen e m) => MonadGen e (SW.WriterT w m) where gen = lift gen #if MIN_VERSION_mtl(2, 2, 1) instance (MonadGen e m) => MonadGen e (ExceptT e' m) where gen = lift gen #else instance (MonadGen e m, Error e') => MonadGen e (ErrorT e' m) where gen = lift gen #endif