mtl-unleashed 0.2.2 → 0.3
raw patch · 4 files changed
+71/−6 lines, 4 filesdep +contravariantdep +hspecdep +hspec-coredep ~base
Dependencies added: contravariant, hspec, hspec-core, lens, mtl-unleashed, profunctors
Dependency ranges changed: base
Files
- Control/Monad/Readers.hs +17/−2
- Control/Monad/States.hs +17/−2
- Tests/Main.hs +28/−0
- mtl-unleashed.cabal +9/−2
Control/Monad/Readers.hs view
@@ -6,14 +6,17 @@ ( module Control.Monad.Reader , MonadReaders(ask, local) , asks+ , view, views, iview, iviews ) where +import Control.Lens as Lens hiding (view, iview, views, uses, iviews) import Control.Monad.Reader hiding (MonadReader(ask, local, reader), asks) import qualified Control.Monad.Reader as MTL (ask, local)-import Control.Monad.State (StateT, mapStateT)+import Control.Monad.State (mapStateT, StateT) import Control.Monad.Writer (WriterT, mapWriterT)-import Data.Monoid (Monoid) +import Data.Profunctor.Unsafe+ -- | Version of MonadReader modified to remove the functional dependency. class Monad m => MonadReaders r m where ask :: m r@@ -54,3 +57,15 @@ => (r -> a) -- ^ The selector function to apply to the environment. -> m a asks = reader++view :: MonadReaders s m => Getting a s a -> m a+view l = Control.Monad.Readers.asks (getConst #. l Const)++views :: (Profunctor p, MonadReaders s m) => Optical p (->) (Const r) s s a a -> p a r -> m r+views l f = Control.Monad.Readers.asks (getConst #. l (Const #. f))++iview :: MonadReaders s m => IndexedGetting i (i,a) s a -> m (i,a)+iview l = Control.Monad.Readers.asks (getConst #. l (Indexed $ \i -> Const #. (,) i))++iviews :: MonadReaders s m => IndexedGetting i r s a -> (i -> a -> r) -> m r+iviews l = views l .# Indexed
Control/Monad/States.hs view
@@ -7,13 +7,16 @@ , modify , modify' , gets+ , Control.Monad.States.use, Control.Monad.States.iuse, Control.Monad.States.uses, Control.Monad.States.iuses ) where -import Control.Monad.Reader+import qualified Control.Lens as Lens (Optical, view, views)+import Control.Lens hiding (view, views, iview, iviews, uses)+import Control.Monad.Readers import Control.Monad.State hiding (MonadState(get, put, state), modify, modify', gets) import qualified Control.Monad.State as MTL (get, put) import Control.Monad.Writer (WriterT)-import Data.Monoid (Monoid)+import Data.Profunctor.Unsafe ((#.), (.#)) -- | Copy of 'Control.Monad.State.MonadState' with functional dependency m -> s removed. class Monad m => MonadStates s m where@@ -53,3 +56,15 @@ instance (Monad m, Monoid w, MonadStates s m) => MonadStates s (WriterT w m) where get = lift get put = lift . put++use :: MonadStates s m => Getting a s a -> m a+use l = Control.Monad.States.gets (Lens.view l)++iuse :: MonadStates s m => IndexedGetting i (i,a) s a -> m (i,a)+iuse l = Control.Monad.States.gets (getConst #. l (Indexed $ \i -> Const #. (,) i))++uses :: (Profunctor p, MonadStates s m) => Lens.Optical p (->) (Const r) s s a a -> p a r -> m r+uses l f = Control.Monad.States.gets (Lens.views l f)++iuses :: MonadStates s m => IndexedGetting i r s a -> (i -> a -> r) -> m r+iuses l = uses l .# Indexed
+ Tests/Main.hs view
@@ -0,0 +1,28 @@+module Main where++import Control.Lens+import Control.Monad.Reader (runReader)+import Control.Monad.State (evalState)+import Test.Hspec hiding (runIO)+import Test.Hspec.Core.Spec (SpecM)+import Types++main :: IO ()+main = hspec $ tests++tests :: SpecM () ()+tests = do+ it "runs a test" $ do+ 1 `shouldBe` 1++ it "sees some state" $ do+ evalState (use foo) (T 5 'r') `shouldBe` 5++ it "sees T using MonadReaders class and lens" $ do+ runReader monadReaders1 (T 5 'r') `shouldBe` 5++ it "sees T using MonadReaders class and modified lens" $ do+ runReader monadReaders2 (T 5 'r') `shouldBe` 5++ it "sees T using MonadStates class" $ do+ evalState monadStates1 (T 5 'r') `shouldBe` 5
mtl-unleashed.cabal view
@@ -1,5 +1,5 @@ name: mtl-unleashed-version: 0.2.2+version: 0.3 cabal-version: >= 1.10 build-type: Simple license: BSD3@@ -19,11 +19,18 @@ extra type signatures. library- build-depends: base < 5, mtl+ build-depends: base < 5, lens, mtl, contravariant, profunctors ghc-options: -Wall -O2 exposed-modules: Control.Monad.Readers Control.Monad.States+ default-language: Haskell2010++test-suite mtl-unleashed-tests+ type: exitcode-stdio-1.0+ hs-source-dirs: Tests+ main-is: Main.hs+ build-depends: base, hspec, hspec-core, lens, mtl, mtl-unleashed, contravariant, profunctors default-language: Haskell2010 source-repository head