phantom-state (empty) → 0.1.0.0
raw patch · 4 files changed
+131/−0 lines, 4 filesdep +basedep +transformerssetup-changed
Dependencies added: base, transformers
Files
- Control/Monad/PhantomState.hs +77/−0
- LICENSE +30/−0
- Setup.hs +2/−0
- phantom-state.cabal +22/−0
+ Control/Monad/PhantomState.hs view
@@ -0,0 +1,77 @@++-- | Phantom Monad State Transformer constructor and functions.+module Control.Monad.PhantomState (+ PhantomStateT (..)+ , PhantomState+ , useState+ , changeState+ , runPhantomStateT+ , runPhantomState+ ) where++import Control.Applicative+import Control.Monad.Trans.Class+import Data.Functor.Identity++-- | The Phantom State Monad Transformer is like the+-- State Monad Transformer, but it does not hold+-- any value.+newtype PhantomStateT s m a = PhantomStateT (s -> m s)++-- | Type synonym of 'PhantomStateT' where the underlying 'Monad' is the 'Identity' monad.+type PhantomState s a = PhantomStateT s Identity a++-- | Perform an applicative action using the current state, leaving+-- the state unchanged.+useState :: Applicative m => (s -> m a) -> PhantomStateT s m ()+{-# INLINE useState #-}+useState f = PhantomStateT $ \x -> f x *> pure x++-- | Modify the state using a pure function.+changeState :: Applicative m => (s -> s) -> PhantomStateT s m ()+{-# INLINE changeState #-}+changeState f = PhantomStateT $ pure . f++-- | Perform a phantom state computation by setting an initial state+-- and running all the actions from there.+runPhantomStateT :: PhantomStateT s m a -- ^ Phantom state computation+ -> s -- ^ Initial state+ -> m s -- ^ Final result+{-# INLINE runPhantomStateT #-}+runPhantomStateT (PhantomStateT f) x = f x++-- | Specialized version of 'runPhantomStateT' where the underlying+-- 'Monad' is the 'Identity' monad.+runPhantomState :: PhantomState s a -- ^ Phantom state computation+ -> s -- ^ Initial state+ -> s -- ^ Final result+{-# INLINE runPhantomState #-}+runPhantomState f = runIdentity . runPhantomStateT f++-- Instances++instance Functor (PhantomStateT s m) where+ {-# INLINE fmap #-}+ fmap _ (PhantomStateT f) = PhantomStateT f++instance Monad m => Applicative (PhantomStateT s m) where+ {-# INLINE pure #-}+ pure _ = PhantomStateT return+ {-# INLINE (<*>) #-}+ PhantomStateT f <*> PhantomStateT g = PhantomStateT (\x -> f x >>= g)+ {-# INLINE (*>) #-}+ PhantomStateT f *> PhantomStateT g = PhantomStateT (\x -> f x >>= g)+ {-# INLINE (<*) #-}+ PhantomStateT f <* PhantomStateT g = PhantomStateT (\x -> f x >>= g)++instance Monad m => Monad (PhantomStateT s m) where+ {-# INLINE return #-}+ return = pure+ {-# INLINE (>>=) #-}+ x >>= f = x *> f undefined+ {-# INLINE (>>) #-}+ (>>) = (*>)++instance MonadTrans (PhantomStateT s) where+ {-# INLINE lift #-}+ lift m = PhantomStateT (\x -> m >> return x)
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2014, Daniel Díaz++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 the name of Daniel Díaz nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 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 COPYRIGHT+OWNER OR 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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ phantom-state.cabal view
@@ -0,0 +1,22 @@+name: phantom-state+version: 0.1.0.0+synopsis: Phantom State Monad Transformer. Like State Monad, but without values.+description: A monad transformer that mimics the State Monad Transformer from the+ <http://hackage.haskell.org/package/transformers transformers> package,+ but dropping the values. In those cases that you want to use the State+ Monad but you only care about how the state changes, use this library+ to earn a plus of efficiency.+license: BSD3+license-file: LICENSE+author: Daniel Díaz+maintainer: dhelta.diaz@gmail.com+category: Control+build-type: Simple+cabal-version: >=1.10++library+ exposed-modules: Control.Monad.PhantomState+ build-depends: base == 4.*+ , transformers >= 0.3 && < 0.5+ default-language: Haskell2010+ ghc-options: -O2 -Wall