fused-effects-lens 0.1.0.0 → 0.2.0.0
raw patch · 4 files changed
+53/−51 lines, 4 filesdep +microlensPVP ok
version bump matches the API change (PVP)
Dependencies added: microlens
API changes (from Hackage documentation)
- Control.Effect.Lens: uses :: (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. (Carrier sig f, Functor f, Member (State s) sig) => Getting a s a -> (a -> b) -> f b
- Control.Effect.Lens: views :: (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. (Member (Reader s) sig, Carrier sig m, Functor m) => Getting a s a -> (a -> b) -> m b
Files
- CHANGES.md +5/−0
- README.md +4/−4
- fused-effects-lens.cabal +11/−6
- src/Control/Effect/Lens.hs +33/−41
CHANGES.md view
@@ -1,3 +1,8 @@+# v0.2.0.0++* Use `microlens` for lens primitives to enable use with other lens libraries.+* Add explicit `forall`s to `views` and `uses`.+ # v0.1.0.0 Initial release.
README.md view
@@ -1,6 +1,6 @@ # fused-effects-lens -This package provides an interface to the [lens](github.com/ekmett/lens) library that is compatible with [fused-effects](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 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: @@ -18,7 +18,7 @@ { _amount :: Int , _disabled :: Bool } deriving (Eq, Show)- + makeLenses ''Context ``` @@ -28,8 +28,8 @@ stateTest :: (Member (State Context) sig, Carrier sig m, Monad m) => m Int stateTest = do initial <- use amount- assign amount (initial + 1)- assign disabled True+ amount .= (initial + 1)+ disabled .= True use amount ```
fused-effects-lens.cabal view
@@ -1,8 +1,8 @@ name: fused-effects-lens-version: 0.1.0.0+version: 0.2.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/patrickt/fused-effects-lens#readme+homepage: https://github.com/fused-effects/fused-effects-lens#readme license: BSD3 license-file: LICENSE author: Patrick Thomson@@ -13,14 +13,14 @@ cabal-version: >=1.10 extra-source-files: README.md- CHANGES.md + CHANGES.md library hs-source-dirs: src exposed-modules: Control.Effect.Lens default-language: Haskell2010- build-depends: base >= 4.7 && < 5- , lens >= 4 && < 5+ build-depends: base >= 4.7 && < 5+ , microlens >= 0.4 && < 1 , fused-effects >= 0.1.2 && < 1 ghc-options: -Wall @@ -29,8 +29,13 @@ main-is: Main.hs hs-source-dirs: test default-language: Haskell2010- build-depends: base >=4.7 && < 5.0+ build-depends: base >=4.7 && < 5.0 , fused-effects-lens , lens >=4 && < 5 , fused-effects >= 0.1.2 && < 1 , hspec >= 2.4.1++source-repository head+ type: git+ location: https://github.com/fused-effects/fused-effects+
src/Control/Effect/Lens.hs view
@@ -1,5 +1,7 @@ {-# LANGUAGE FlexibleContexts, RankNTypes #-}-+-- | 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. module Control.Effect.Lens ( view , views@@ -14,61 +16,53 @@ import Control.Effect import qualified Control.Effect.State as State import qualified Control.Effect.Reader as Reader-import qualified Control.Lens as Lens-import Control.Lens.Getter (Getting)-import Control.Lens.Setter (ASetter)+import qualified Lens.Micro as Lens+import qualified Lens.Micro.Extras as Lens+import Lens.Micro.Type (Getting, ASetter) --- | View the value pointed to by a 'Control.Lens.Getter.Getter',--- 'Control.Lens.Iso.Iso' or 'Control.Lens.Lens.Lens' or the result of--- folding over all the results of a 'Control.Lens.Fold.Fold' or--- 'Control.Lens.Traversal.Traversal' corresponding to the 'Reader'--- context of the given monadic carrier.+-- | 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 l = Reader.asks (Lens.view l) {-# INLINE view #-} --- | View a function of the value pointed to by a--- 'Control.Lens.Getter.Getter', 'Control.Lens.Iso.Iso' or--- 'Control.Lens.Lens.Lens' or the result of folding over all the--- results of a 'Control.Lens.Fold.Fold' or--- 'Control.Lens.Traversal.Traversal' corresponding to the 'Reader'--- context of the given monadic carrier.--- --- This is slightly more general in lens itself, but should suffice for our purposes.-views :: (Member (Reader s) sig, Carrier sig m, Functor m) => Getting a s a -> (a -> b) -> m b+-- | 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 views l f = fmap f (Reader.asks (Lens.view l)) {-# INLINE views #-} --- | Extract the target of a 'Control.Lens.Lens',--- 'Control.Lens.Iso.Iso', or 'Control.Lens.Getter.Getter' from the,--- or use a summary of a 'Control.Lens.Fold.Fold' or--- 'Control.Lens.Traversal.Traversal' that points to a monoidal value.+-- | 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 use l = State.gets (Lens.view l) {-# INLINE use #-} --- | Use a function of the target of a 'Control.Lens.Lens.Lens',--- 'Control.Lens.Iso.Iso', or 'Control.Lens.Getter.Getter' in the--- current state, or use a summary of a 'Control.Lens.Fold.Fold' or--- 'Control.Lens.Traversal.Traversal' that points to a monoidal value.-uses :: (Carrier sig f, Functor f, Member (State s) sig) => Getting a s a -> (a -> b) -> f b+-- | 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 uses l f = fmap f (State.gets (Lens.view l)) {-# INLINE uses #-} --- | Replace the target of a 'Control.Lens.Lens.Lens' or all of the--- targets of a 'Control.Lens.Setter.Setter' or--- 'Control.Lens.Traversal.Traversal' in our monadic state with a new--- value, irrespective of the old.+-- | 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 l b = State.modify (Lens.set l b) {-# INLINE assign #-} --- | Replace the target of a 'Control.Lens.Lens.Lens' or all of the--- targets of a 'Control.Lens.Setter.Setter' or--- 'Control.Lens.Traversal.Traversal' in our monadic state with a new--- value, irrespective of the old.+-- | 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 .=@@ -76,18 +70,16 @@ (.=) = assign {-# INLINE (.=) #-} --- | Map over the target of a 'Control.Lens.Lens.Lens' or all of the--- targets of a 'Control.Lens.Setter.Setter' or--- 'Control.Lens.Traversal.Traversal' in our 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 l f = State.modify (Lens.over l f) {-# INLINE modifying #-} --- | Map over the target of a 'Control.Lens.Lens.Lens' or all of the--- targets of a 'Control.Lens.Setter.Setter' or--- 'Control.Lens.Traversal.Traversal' in our 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 %=