data-accessor-monads-fd (empty) → 0.2
raw patch · 4 files changed
+128/−0 lines, 4 filesdep +basedep +data-accessordep +monads-fdsetup-changed
Dependencies added: base, data-accessor, monads-fd, transformers
Files
- LICENSE +26/−0
- Setup.lhs +3/−0
- data-accessor-monads-fd.cabal +19/−0
- src/Data/Accessor/Monad/FD/State.hs +80/−0
+ LICENSE view
@@ -0,0 +1,26 @@+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 the <organization>; 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 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.lhs view
@@ -0,0 +1,3 @@+#! /usr/bin/env runhaskell+> import Distribution.Simple+> main = defaultMain
+ data-accessor-monads-fd.cabal view
@@ -0,0 +1,19 @@+Name: data-accessor-monads-fd+Version: 0.2+License: BSD3+License-File: LICENSE+Author: Henning Thielemann <haskell@henning-thielemann.de>+Maintainer: Henning Thielemann <haskell@henning-thielemann.de>+Homepage: http://www.haskell.org/haskellwiki/Record_access+Package-URL: http://code.haskell.org/data-accessor/+Category: Data+-- Portability: Haskell98+Build-Type: Simple+Build-Depends: base>=1.0, data-accessor >=0.2 && <0.3, monads-fd >=0.0 && <0.1, transformers >=0.0.1 && <0.2+Synopsis: Use Accessor to access state in monads-fd State monad class+Description: Use Accessor to access state in monads-fd State monad class+GHC-Options: -Wall+Tested-With: GHC==6.8.2+Hs-Source-Dirs: src+Exposed-Modules:+ Data.Accessor.Monad.FD.State
+ src/Data/Accessor/Monad/FD/State.hs view
@@ -0,0 +1,80 @@+{- | Access helper functions in the State monad class -}+module Data.Accessor.Monad.FD.State where++import qualified Data.Accessor.Basic as Accessor+import qualified Control.Monad.State as State+import qualified Control.Monad.Trans as Trans+import Control.Monad.Trans.State (State, runState, StateT, runStateT, )+import Control.Monad.Trans (MonadTrans, )+import Control.Monad.State (MonadState, )++-- * accessors in the form of actions in the state monad++set :: MonadState r m => Accessor.T r a -> a -> m ()+set f x = State.modify (Accessor.set f x)++get :: MonadState r m => Accessor.T r a -> m a+get f = State.gets (Accessor.get f)++modify :: MonadState r m => Accessor.T r a -> (a -> a) -> m ()+modify f g = State.modify (Accessor.modify f g)++{- |+Modify a record element and return its old value.+-}+getAndModify :: MonadState r m => Accessor.T r a -> (a -> a) -> m a+getAndModify f g =+ do x <- get f+ modify f g+ return x++{- |+Modify a record element and return its new value.+-}+modifyAndGet :: MonadState r m => Accessor.T r a -> (a -> a) -> m a+modifyAndGet f g =+ do modify f g+ get f++++infix 1 %=, %:++{- |+Infix variant of 'set'.+-}+(%=) :: MonadState r m => Accessor.T r a -> a -> m ()+(%=) = set++{- |+Infix variant of 'modify'.+-}+(%:) :: MonadState r m => Accessor.T r a -> (a -> a) -> m ()+(%:) = modify++++-- * lift a state monadic accessor to an accessor of a parent record++lift :: (MonadState r mr) =>+ Accessor.T r s -> State s a -> mr a+lift f m =+ do s0 <- get f+ let (a,s1) = runState m s0+ set f s1+ return a++-- liftT :: (Monad m) =>+-- Accessor.T r s -> StateT s m a -> StateT r m a+liftT :: (Monad m, MonadTrans t, MonadState r (t m)) =>+ Accessor.T r s -> StateT s m a -> t m a+liftT f m =+ do s0 <- get f+ (a,s1) <- Trans.lift $ runStateT m s0+ set f s1+ return a++{- not possible in this generality+lift :: (MonadState r mr, MonadState s ms) =>+ Accessor.T r s -> ms a -> mr a+-}