monad-state 0.1.1.2 → 0.2.0.1
raw patch · 5 files changed
+134/−69 lines, 5 filesdep ~fclabelsdep ~monads-tf
Dependency ranges changed: fclabels, monads-tf
Files
- monad-state.cabal +59/−21
- src/Control/Monad/Abort.hs +0/−2
- src/Control/Monad/Abort/Class.hs +0/−1
- src/Control/Monad/Abort/Instances.hs +0/−3
- src/Control/Monad/Record.hs +75/−42
monad-state.cabal view
@@ -1,25 +1,63 @@-name: monad-state-version: 0.1.1.2-cabal-version: >= 1.10-build-type: Simple-license: BSD3-license-file: LICENSE-copyright: Copyright (C) 2010 Byron James Johnson-author: Byron James Johnson-maintainer: KrabbyKrap@gmail.com-synopsis: Utility library for monads, particularly those involving state-description: Utility library for monads, particularly those involving state-category: Control, Data, Monads-tested-with: GHC == 7.0.2+name: monad-state+-- Don't forget to bump the tag too.+version: 0.2.0.1+cabal-version: >= 1.10+build-type: Simple+license: BSD3+license-file: LICENSE+copyright: Copyright (C) 2010 Byron James Johnson+author: Byron James Johnson+maintainer: ByronJohnsonFP@gmail.com+category: Control, Data, Monads+tested-with: GHC == 7.0.2+synopsis: Utility library for monads, particularly those involving state+description:+ Utility library for monads, particularly those involving state library- default-language: Haskell2010- hs-source-dirs: src- build-depends: base >= 4 && < 5, fclabels, transformers, monads-tf, AbortT-transformers- exposed-modules: Control.Monad.Abort, Control.Monad.Abort.Class, Control.Monad.Abort.Instances, Control.Monad.Record, Control.Monad.Runnable, Control.Monad.Short, System.Timeout.Monad- ghc-options: -Wall- other-extensions: TypeFamilies, MultiParamTypeClasses, TypeOperators, PostfixOperators, FlexibleInstances, FlexibleContexts, TypeSynonymInstances+ default-language: Haskell2010+ hs-source-dirs: src+ ghc-options: -Wall+ default-extensions:+ --,GADTs+ --,TemplateHaskell+ --,DeriveDataTypeable+ other-extensions:+ TypeFamilies+ ,MultiParamTypeClasses+ ,TypeOperators+ ,PostfixOperators+ ,FlexibleInstances+ ,FlexibleContexts+ ,TypeSynonymInstances+ -- TODO: add version constraints+ build-depends:+ base >= 4 && < 5+ -- fclabels first introduces the Data.Label module in 0.11.0.+ -- fclabels first introduces the 'modify' function in 1.0.+ -- fclabels first introduces the 'Total' type synonym in 2.0. (This+ -- package can be trivially updated to support 1.0; c.f. documentation in+ -- "Control.Monad.Record". Supporting both versions is yet to be done;+ -- CPP can be used for this.+ --,fclabels >= 1.0 && < 2.1+ ,fclabels >= 2.0 && < 2.1+ ,transformers+ ,monads-tf+ ,AbortT-transformers+ exposed-modules:+ Control.Monad.Abort+ ,Control.Monad.Abort.Class+ ,Control.Monad.Abort.Instances+ ,Control.Monad.Record+ ,Control.Monad.Runnable+ ,Control.Monad.Short+ ,System.Timeout.Monad source-repository head- type: darcs- location: http://patch-tag.com/r/bob/monad-state+ type: git+ location: git@github.com:bairyn/monad-state.git++source-repository this+ type: git+ location: git@github.com:bairyn/monad-state.git+ tag: v0.2.0.1
src/Control/Monad/Abort.hs view
@@ -15,5 +15,3 @@ import Control.Monad.Abort.Class import Control.Monad.Abort.Instances (mapAbortT) import Control.Monad.Trans.Abort (runAbort, runAbortT)--import Control.Monad.Instances ()
src/Control/Monad/Abort/Class.hs view
@@ -22,7 +22,6 @@ import Control.Monad.Trans import Control.Monad ()-import Control.Monad.Instances () import Data.Monoid class (Monad m) => MonadAbort m where
src/Control/Monad/Abort/Instances.hs view
@@ -10,9 +10,6 @@ import Control.Monad.Cont import Control.Monad.Error import Control.Monad.RWS-import Control.Monad.Reader-import Control.Monad.Writer-import Control.Monad.State mapAbortT :: (m (Either r a) -> n (Either r' b)) -> AbortT r m a -> AbortT r' n b mapAbortT f = AbortT . f . unwrapAbortT
src/Control/Monad/Record.hs view
@@ -1,8 +1,11 @@ {-# LANGUAGE MultiParamTypeClasses, TypeFamilies, TypeOperators, PostfixOperators, FlexibleInstances #-} +-- TODO: support fclabels-1.0 with CPP; c.f. documentation for one of the+-- instance declaration of 'MLens' module Control.Monad.Record ( maybeAbort , maybeAbortM+ , lensGS , MLens(..) , (:-->)(..) , getM@@ -30,16 +33,23 @@ , (>$<) , (>$>) , (>$$>)- , module Data.Record.Label+ , module Data.Label+ --, module Data.Label.Abstract+ , module Data.Label.Poly+ , module Data.Label.Point ) where -import Prelude hiding ((.), id)-import Control.Category-import Control.Monad.Abort-import Control.Monad.Reader-import Control.Monad.State.Strict-import Control.Monad.Trans.Maybe-import Data.Record.Label hiding (getM, setM, modM, (=:), askM, localM)+import Prelude hiding ((.), id)+import Control.Category+import Control.Monad.Abort+import Control.Monad.Reader+import Control.Monad.State.Strict hiding (get, modify)+import qualified Control.Monad.State.Strict as S (modify)+import Control.Monad.Trans.Maybe+import Data.Label+--import Data.Label.Abstract (Lens)+import Data.Label.Poly (Lens)+import Data.Label.Point (Total) maybeAbort :: (Monad m) => r -> Maybe a -> AbortT r m a maybeAbort _ (Just x) = return x@@ -52,46 +62,69 @@ (Just a) -> return a (Nothing) -> abort r -class MLens l a where- type MLensA l a- toLens :: l f a -> f :-> Maybe (MLensA l a)+-- | Create a lens out of a getter and setter.+lensGS :: (f -> a) -> (a -> f -> f) -> f :-> a+-- This function definition can be made to work with the following versions of+-- fc-labels; all ranges are inclusive:+-- * 1.0 - 1.1.7.1:+--lensGS = lens+-- * 2.0 - onward:+lensGS getter setter = lens getter (\modifier f -> setter (modifier (getter f)) f) -instance MLens (:->) (Maybe a) where- type MLensA (:->) (Maybe a) = a+class MLens l a f where+ type MLensA l a f+ type MLensF l a f+ toLens :: l f a -> (MLensF l a f) :-> Maybe (MLensA l a f)+ --toLens :: l f a -> f :-> Maybe (MLensA l a f)++--instance MLens (:->) (Maybe a) (Maybe o) where+-- fc-labels versions as old as 1.0 will work with this package so long as+-- @Total@ is replaced with @(->)@, and in the import and export of+-- Data.Label.Poly, @Mono@ is replaced with @Abstract@, and finally in this+-- instance declaration, @Maybe i -> Maybe i@ is replaced with @Maybe i@ and+-- @Maybe o -> Maybe o@ is replaced with @Maybe o@; and the instructions in the+-- documentation for lensGS are applied.+--instance MLens (Lens (->)) (Maybe i) (Maybe o) where+instance MLens (Lens Total) (Maybe i -> Maybe i) (Maybe o -> Maybe o) where+ --type MLensA (Lens (->)) (Maybe i) (Maybe o) = i+ type MLensA (Lens Total) (Maybe i -> Maybe i) (Maybe o -> Maybe o) = i+ --type MLensF (Lens (->)) (Maybe i) (Maybe o) = Maybe o+ type MLensF (Lens Total) (Maybe i -> Maybe i) (Maybe o -> Maybe o) = Maybe o toLens = id -instance MLens (:-->) a where- type MLensA (:-->) a = a+instance MLens (:-->) a f where+ type MLensA (:-->) a f = a+ type MLensF (:-->) a f = f toLens = unMaybeLens newtype f :--> a = MaybeLens {unMaybeLens :: f :-> Maybe a} instance Category (:-->) where- id = MaybeLens $ lens Just (\a f -> maybe f id a)- MaybeLens a . MaybeLens b = MaybeLens $ lens getter setter- where getter f = getL a =<< getL b f- setter = modL b . fmap . setL a+ id = MaybeLens $ lensGS Just (\a f -> maybe f id a)+ MaybeLens a . MaybeLens b = MaybeLens $ lensGS getter setter+ where getter f = get a =<< get b f+ setter = modify b . fmap . set a getM :: (MonadState m) => (StateType m :-> a) -> m a-getM = gets . getL+getM = gets . get setM :: (MonadState m) => (StateType m :-> a) -> a -> m ()-setM l = modify . setL l+setM l = S.modify . set l modM :: (MonadState m) => (StateType m :-> a) -> (a -> a) -> m ()-modM l = modify . modL l+modM l = S.modify . modify l -getMAbort :: (MonadState m, MLens l b) => r -> l (StateType (AbortT r m)) b -> (MLensA l b :-> a) -> AbortT r m a-getMAbort r b l = liftM (getL l) $ maybeAbort r =<< gets (getL $ toLens b)+getMAbort :: (MonadState m, MLens l b f, MLensF l b f ~ StateType m) => r -> l f b -> (MLensA l b f :-> a) -> AbortT r m a+getMAbort r b l = liftM (get l) $ maybeAbort r =<< gets (get $ toLens b) -setMAbort :: (MonadState m, MLens l b) => l (StateType m) b -> (MLensA l b :-> a) -> a -> m ()-setMAbort b l x = modify . modL (toLens b) . fmap $ setL l x+setMAbort :: (MonadState m, MLens l b f, MLensF l b f ~ StateType m) => l f b -> (MLensA l b f :-> a) -> a -> m ()+setMAbort b l x = S.modify . modify (toLens b) . fmap $ set l x -modMAbort :: (MonadState m, MLens l b) => l (StateType m) b -> (MLensA l b :-> a) -> (a -> a) -> m ()-modMAbort b l f = modify . modL (toLens b) . fmap $ modL l f+modMAbort :: (MonadState m, MLens l b f, MLensF l b f ~ StateType m) => l f b -> (MLensA l b f :-> a) -> (a -> a) -> m ()+modMAbort b l f = S.modify . modify (toLens b) . fmap $ modify l f askM :: (MonadReader m) => (EnvType m :-> a) -> m a-askM = asks . getL+askM = asks . get liftState :: (MonadState m) => (StateType m :-> s) -> StateT s m a -> m a liftState l n = do@@ -106,7 +139,7 @@ setM l s' return a -liftSubMaybeState :: (Monad m, MonadTrans t, MonadState (t m), MLens l a) => l (StateType (t m)) a -> StateT (MLensA l a) m a1 -> MaybeT (t m) a1+liftSubMaybeState :: (Monad m, MonadTrans t, MonadState (t m), MLens l b f, MLensF l b f ~ StateType (t m)) => l f b -> StateT (MLensA l b f) m a -> MaybeT (t m) a liftSubMaybeState l m = MaybeT $ do sw <- getM l' case sw of@@ -118,18 +151,18 @@ return Nothing where l' = toLens l --- | 'getL'+-- | 'get' infixr 8 <:: (<:) :: (f :-> a) -> f -> a-(<:) = getL--- | 'setL'+(<:) = get+-- | 'set' infixr 5 =:: (=:) :: (f :-> a) -> a -> f -> f-(=:) = setL--- | 'modL'+(=:) = set+-- | 'modify' infixr 8 $:: ($:) :: (f :-> a) -> (a -> a) -> f -> f-($:) = modL+($:) = modify -- | 'getM' infixr 8 <:@@ -146,24 +179,24 @@ -- | 'getMAbort' infixr 8 <<:-(<<:) :: (MonadState m, MLens l b) => r -> l (StateType (AbortT r m)) b -> (MLensA l b :-> a) -> AbortT r m a+(<<:) :: (MonadState m, MLens l b f, MLensF l b f ~ StateType m) => r -> l f b -> (MLensA l b f :-> a) -> AbortT r m a (<<:) = getMAbort -- | 'setMAbort' infixr 5 <=:-(<=:) :: (MonadState m, MLens l b) => l (StateType m) b -> (MLensA l b :-> a) -> a -> m ()+(<=:) :: (MonadState m, MLens l b f, MLensF l b f ~ StateType m) => l f b -> (MLensA l b f :-> a) -> a -> m () (<=:) = setMAbort -- | 'modMAbort' infixr 8 <$:-(<$:) :: (MonadState m, MLens l b) => l (StateType m) b -> (MLensA l b :-> a) -> (a -> a) -> m ()+(<$:) :: (MonadState m, MLens l b f, MLensF l b f ~ StateType m) => l f b -> (MLensA l b f :-> a) -> (a -> a) -> m () (<$:) = modMAbort -- | 'getMAbort' () infixr 8 <<::-(<<::) :: (MonadState m, MLens l b) => l (StateType (AbortT r m)) b -> (MLensA l b :-> a) -> AbortT () m a+(<<::) :: (MonadState m, MLens l b f, MLensF l b f ~ StateType m) => l f b -> (MLensA l b f :-> a) -> AbortT () m a (<<::) = getMAbort () infixr 5 <->-(<->) :: (MLens l a, MLens l' a') => l (MLensA l' a') a -> l' f a' -> (f :--> MLensA l a)+(<->) :: (MLens l a f, MLens l' a' f', MLensA l a f ~ MLensF l' a' f') => l' f' a' -> l f a -> MLensF l a f :--> MLensA l' a' f' a <-> b = (MaybeLens . toLens $ a) . (MaybeLens . toLens $ b) -- | 'askM'@@ -185,5 +218,5 @@ -- | 'liftSubMaybeState' infixr 4 >$$>-(>$$>) :: (Monad m, MLens l a) => l s a -> StateT (MLensA l a) m a1 -> MaybeT (StateT s m) a1+(>$$>) :: (Monad m, MonadTrans t, MonadState (t m), MLens l b f, MLensF l b f ~ StateType (t m)) => l f b -> StateT (MLensA l b f) m a -> MaybeT (t m) a (>$$>) = liftSubMaybeState