fused-effects-lens 0.2.0.0 → 1.0.0.0
raw patch · 5 files changed
+79/−63 lines, 5 filesdep −lensdep ~basedep ~fused-effectsdep ~microlensPVP ok
version bump matches the API change (PVP)
Dependencies removed: lens
Dependency ranges changed: base, fused-effects, microlens
API changes (from Hackage documentation)
- Control.Effect.Lens: (%=) :: forall s a b sig m. (Member (State s) sig, Carrier sig m, Monad m) => ASetter s s a b -> (a -> b) -> m ()
+ Control.Effect.Lens: (%=) :: forall s a b sig m. Has (State s) sig m => ASetter s s a b -> (a -> b) -> m ()
- Control.Effect.Lens: (.=) :: forall s a b sig m. (Member (State s) sig, Carrier sig m, Monad m) => ASetter s s a b -> b -> m ()
+ Control.Effect.Lens: (.=) :: forall s a b sig m. Has (State s) sig m => ASetter s s a b -> b -> m ()
- Control.Effect.Lens: assign :: forall s a b sig m. (Member (State s) sig, Carrier sig m, Monad m) => ASetter s s a b -> b -> m ()
+ Control.Effect.Lens: assign :: forall s a b sig m. Has (State s) sig m => ASetter s s a b -> b -> m ()
- Control.Effect.Lens: modifying :: forall s a b sig m. (Member (State s) sig, Carrier sig m, Monad m) => ASetter s s a b -> (a -> b) -> m ()
+ Control.Effect.Lens: modifying :: forall s a b sig m. Has (State s) sig m => ASetter s s a b -> (a -> b) -> m ()
- Control.Effect.Lens: use :: forall s a sig m. (Member (State s) sig, Carrier sig m, Monad m) => Getting a s a -> m a
+ Control.Effect.Lens: use :: forall s a sig m. Has (State s) sig m => Getting a s a -> m a
- Control.Effect.Lens: uses :: forall s a b f sig. (Carrier sig f, Functor f, Member (State s) sig) => Getting a s a -> (a -> b) -> f b
+ Control.Effect.Lens: uses :: forall s a b f sig. Has (State s) sig f => Getting a s a -> (a -> b) -> f b
- Control.Effect.Lens: view :: forall r a sig m. (Member (Reader r) sig, Carrier sig m, Functor m) => Getting a r a -> m a
+ Control.Effect.Lens: view :: forall r a sig m. Has (Reader r) sig m => Getting a r a -> m a
- Control.Effect.Lens: views :: forall s a b sig m. (Member (Reader s) sig, Carrier sig m, Functor m) => Getting a s a -> (a -> b) -> m b
+ Control.Effect.Lens: views :: forall s a b sig m. Has (Reader s) sig m => Getting a s a -> (a -> b) -> m b
Files
- CHANGES.md +4/−0
- README.md +5/−1
- fused-effects-lens.cabal +11/−10
- src/Control/Effect/Lens.hs +35/−36
- test/Main.hs +24/−16
CHANGES.md view
@@ -1,3 +1,7 @@+# v1.0.0.0++Upgrade to fused-effects v1.+ # v0.2.0.0 * Use `microlens` for lens primitives to enable use with other lens libraries.
README.md view
@@ -1,5 +1,9 @@ # fused-effects-lens +[](https://hackage.haskell.org/package/fused-effects-lens)+[](LICENSE)+[](https://github.com/fused-effects/fused-effects-lens/actions)+ This package provides an interface to the [`lens`](https://github.com/ekmett/lens) library (and other such libraries such as `microlens` and `generic-lens`) that is compatible with [`fused-effects`](https://github.com/robrix/fused-effects). The standard formulation of `lens` combinators for operating in `MonadState` contexts—`use`, `.=`, et al—rely on `mtl` for `MonadState` and `MonadReader`, which is not applicable to `Reader` and `State` effects. This package is meant to be used alongside `lens`, like so:@@ -25,7 +29,7 @@ We can can use the `use` combinators to extract a lens target from the current state, and `assign` to write to a field of that state: ``` haskell-stateTest :: (Member (State Context) sig, Carrier sig m, Monad m) => m Int+stateTest :: (Has (State Context) sig m) => m Int stateTest = do initial <- use amount amount .= (initial + 1)
fused-effects-lens.cabal view
@@ -1,16 +1,17 @@+cabal-version: 2.4+ name: fused-effects-lens-version: 0.2.0.0+version: 1.0.0.0 synopsis: Monadic lens combinators for fused-effects. description: Provides combinators for the lens-based manipulation of state and context types provided by the fused-effects library, similar to those provided for mtl-based monad transformers. homepage: https://github.com/fused-effects/fused-effects-lens#readme-license: BSD3+license: BSD-3-Clause license-file: LICENSE author: Patrick Thomson maintainer: patrickt@github.com copyright: 2018 Patrick Thomson category: Web build-type: Simple-cabal-version: >=1.10 extra-source-files: README.md CHANGES.md@@ -19,9 +20,9 @@ hs-source-dirs: src exposed-modules: Control.Effect.Lens default-language: Haskell2010- build-depends: base >= 4.7 && < 5- , microlens >= 0.4 && < 1- , fused-effects >= 0.1.2 && < 1+ build-depends: base >= 4.7 && < 5+ , microlens >= 0.4 && < 1+ , fused-effects ^>= 1.0 ghc-options: -Wall test-suite test@@ -29,13 +30,13 @@ main-is: Main.hs hs-source-dirs: test default-language: Haskell2010- build-depends: base >=4.7 && < 5.0+ build-depends: base , fused-effects-lens- , lens >=4 && < 5- , fused-effects >= 0.1.2 && < 1+ , microlens+ , fused-effects , hspec >= 2.4.1 source-repository head type: git- location: https://github.com/fused-effects/fused-effects+ location: https://github.com/fused-effects/fused-effects-lens
src/Control/Effect/Lens.hs view
@@ -3,7 +3,7 @@ -- context types provided by the fused-effects library, similar to -- those provided for mtl-based monad transformers. module Control.Effect.Lens- ( view+ ( Control.Effect.Lens.view , views , use , uses@@ -13,76 +13,75 @@ , (%=) ) where -import Control.Effect-import qualified Control.Effect.State as State-import qualified Control.Effect.Reader as Reader-import qualified Lens.Micro as Lens-import qualified Lens.Micro.Extras as Lens-import Lens.Micro.Type (Getting, ASetter)+import Control.Algebra+import Control.Effect.Reader as Reader+import Control.Effect.State as State+import Lens.Micro as Lens+import Lens.Micro.Extras as Lens+import Lens.Micro.Type (ASetter, Getting) --- | View the value pointed to by a getter or lens, or the result--- of folding over all the results of a fold or traversal, when--- applied to the 'Reader' context of the given monadic carrier.-view :: forall r a sig m . (Member (Reader r) sig, Carrier sig m, Functor m) => Getting a r a -> m a+-- | View the value pointed to by a @Getter@, 'Lens', 'Traversal', or+-- @Fold@ corresponding to the 'Reader' context of the given monadic+-- carrier.+view :: forall r a sig m . (Has (Reader.Reader r) sig m) => Getting a r a -> m a view l = Reader.asks (Lens.view l) {-# INLINE view #-} --- | View a function of the value pointed to by a getter or lens,--- or the result of folding over all the results of a fold or--- traversal, when applied to the 'Reader' context of the given+-- | View a function of the value pointed to by a @Getter@ or 'Lens',+-- or the result of folding over all the results of a @Fold@ or+-- 'Traversal', when applied to the 'Reader' context of the given -- monadic carrier. ----- This is slightly more general in @lens@ itself, but should suffice for our purposes.-views :: forall s a b sig m . (Member (Reader s) sig, Carrier sig m, Functor m) => Getting a s a -> (a -> b) -> m b+-- This is slightly more general in lens itself, but should suffice for our purposes.+views :: forall s a b sig m . (Has (Reader.Reader s) sig m) => Getting a s a -> (a -> b) -> m b views l f = fmap f (Reader.asks (Lens.view l)) {-# INLINE views #-} --- | Extract the target of a lens or getter, or the result--- of folding over all the results of a fold or traversal,--- applied to the 'State' context of the the given monadic carrier.-use :: forall s a sig m . (Member (State s) sig, Carrier sig m, Monad m) => Getting a s a -> m a+-- | Extract the target of a 'Lens' or @Getter@, or use a summary of a+-- @Fold@ or 'Traversal' that points to a monoidal value.+use :: forall s a sig m . (Has (State.State s) sig m) => Getting a s a -> m a use l = State.gets (Lens.view l) {-# INLINE use #-} --- | Extract a function of the target of a lens or getter, or the--- result of folding over all the results of a fold or traversal,--- applied to the 'State' context of the the given monadic carrier.-uses :: forall s a b f sig . (Carrier sig f, Functor f, Member (State s) sig) => Getting a s a -> (a -> b) -> f b+-- | Use a function of the target of a 'Lens' or @Getter@ in the+-- current state, or use a summary of a @Fold@ or 'Traversal' that+-- points to a monoidal value.+uses :: forall s a b f sig . (Has (State.State s) sig f) => Getting a s a -> (a -> b) -> f b uses l f = fmap f (State.gets (Lens.view l)) {-# INLINE uses #-} --- | Replace the target of a lens (or all the targets of a setter--- or traversal) within the current monadic state, irrespective of+-- | Replace the target of a 'Lens' (or all the targets of a @Setter@+-- or 'Traversal') within the current monadic state, irrespective of -- the old value. -- -- This is a prefix version of '.='.-assign :: forall s a b sig m . (Member (State s) sig, Carrier sig m, Monad m) => ASetter s s a b -> b -> m ()+assign :: forall s a b sig m . (Has (State.State s) sig m) => ASetter s s a b -> b -> m () assign l b = State.modify (Lens.set l b) {-# INLINE assign #-} --- | Replace the target of a lens (or all the targets of a setter--- or traversal) within the current monadic state, irrespective of+-- | Replace the target of a 'Lens' (or all the targets of a @Setter@+-- or 'Traversal') within the current monadic state, irrespective of -- the old value. -- -- This is an infix version of 'assign'. infixr 4 .=-(.=) :: forall s a b sig m . (Member (State s) sig, Carrier sig m, Monad m) => ASetter s s a b -> b -> m ()+(.=) :: forall s a b sig m . (Has (State.State s) sig m) => ASetter s s a b -> b -> m () (.=) = assign {-# INLINE (.=) #-} --- | Map over the target of a lens, or all of the targets of a setter--- or traversal, in the current monadic state.+-- | Map over the target of a 'Lens', or all of the targets of a @Setter@+-- or 'Traversal', in the current monadic state. -- -- This is a prefix version of '%='.-modifying :: forall s a b sig m . (Member (State s) sig, Carrier sig m, Monad m) => ASetter s s a b -> (a -> b) -> m ()+modifying :: forall s a b sig m . (Has (State.State s) sig m) => ASetter s s a b -> (a -> b) -> m () modifying l f = State.modify (Lens.over l f) {-# INLINE modifying #-} --- | Map over the target of a lens, or all of the targets of a setter--- or traversal, in the current monadic state.+-- | Map over the target of a 'Lens', or all of the targets of a @Setter@+-- or 'Traversal', in the current monadic state. -- -- This is an infix version of 'modifying'. infixr 4 %=-(%=) :: forall s a b sig m . (Member (State s) sig, Carrier sig m, Monad m) => ASetter s s a b -> (a -> b) -> m ()+(%=) :: forall s a b sig m . (Has (State.State s) sig m) => ASetter s s a b -> (a -> b) -> m () (%=) = modifying {-# INLINE (%=) #-}
test/Main.hs view
@@ -1,48 +1,56 @@-{-# LANGUAGE TypeApplications, FlexibleContexts, MultiParamTypeClasses, TemplateHaskell, TypeFamilies, FlexibleInstances #-}+{-# LANGUAGE FlexibleContexts, FlexibleInstances, FunctionalDependencies, MultiParamTypeClasses, TypeApplications, TypeFamilies #-} module Main where -import Control.Effect-import Control.Effect.Reader-import Control.Effect.State-import Control.Lens.Wrapped-import Control.Lens.TH+import Control.Algebra+import Control.Carrier.Reader+import Control.Carrier.State.Strict+import Lens.Micro import Test.Hspec import Control.Effect.Lens data Context = Context- { _amount :: Int+ { _amount :: Int , _sweatshirt :: Bool } deriving (Eq, Show) +amount :: Lens' Context Int+amount = lens _amount (\c a -> c { _amount = a })++sweatshirt :: Lens' Context Bool+sweatshirt = lens _sweatshirt (\c s -> c { _sweatshirt = s })+ initial :: Context initial = Context 0 False -makeLenses ''Context--stateTest :: (Member (State Context) sig, Carrier sig m, Monad m) => m Int+stateTest :: (Has (State Context) sig m) => m Int stateTest = do initial <- use amount assign amount (initial + 1) assign sweatshirt True use amount +class PolymorphicLens t a | t -> a where+ wrapped :: Lens' t a+ newtype Foo = Foo { _unFoo :: Int } deriving (Eq, Show) -makeWrapped ''Foo+instance PolymorphicLens Foo Int where+ wrapped = lens _unFoo (\_ i -> Foo i) newtype Bar = Bar { _unBar :: Float } deriving (Eq, Show) -makeWrapped ''Bar+instance PolymorphicLens Bar Float where+ wrapped = lens _unBar (\_ i -> Bar i) -doubleStateTest :: (Member (State Bar) sig, Member (State Foo) sig, Carrier sig m, Monad m) => m Int+doubleStateTest :: (Has (State Bar) sig m, Has (State Foo) sig m) => m Int doubleStateTest = do- assign @Foo _Wrapped 5- assign @Bar _Wrapped 30.5+ assign @Foo wrapped 5+ assign @Bar wrapped 30.5 pure 50 -readerTest :: (Member (Reader Context) sig, Carrier sig m, Monad m) => m Int+readerTest :: (Has (Reader Context) sig m) => m Int readerTest = succ <$> view amount spec :: Spec