{-# LANGUAGE FlexibleContexts, RankNTypes #-}
module Control.Effect.Lens
(
Control.Effect.Lens.view
, views
, use
, uses
, assign
, modifying
, (.=)
, (?=)
, (%=)
, (<~)
, (+=)
, (-=)
, (*=)
, (//=)
) where
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
view :: forall r a sig m . (Has (Reader.Reader r) sig m) => Getting a r a -> m a
view :: Getting a r a -> m a
view Getting a r a
l = (r -> a) -> m a
forall r (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Has (Reader r) sig m =>
(r -> a) -> m a
Reader.asks (Getting a r a -> r -> a
forall a s. Getting a s a -> s -> a
Lens.view Getting a r a
l)
{-# INLINE view #-}
views :: forall s a b sig m . (Has (Reader.Reader s) sig m) => Getting a s a -> (a -> b) -> m b
views :: Getting a s a -> (a -> b) -> m b
views Getting a s a
l a -> b
f = (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f ((s -> a) -> m a
forall r (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Has (Reader r) sig m =>
(r -> a) -> m a
Reader.asks (Getting a s a -> s -> a
forall a s. Getting a s a -> s -> a
Lens.view Getting a s a
l))
{-# INLINE views #-}
use :: forall s a sig m . (Has (State.State s) sig m) => Getting a s a -> m a
use :: Getting a s a -> m a
use Getting a s a
l = (s -> a) -> m a
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Has (State s) sig m =>
(s -> a) -> m a
State.gets (Getting a s a -> s -> a
forall a s. Getting a s a -> s -> a
Lens.view Getting a s a
l)
{-# INLINE use #-}
uses :: forall s a b f sig . (Has (State.State s) sig f) => Getting a s a -> (a -> b) -> f b
uses :: Getting a s a -> (a -> b) -> f b
uses Getting a s a
l a -> b
f = (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f ((s -> a) -> f a
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Has (State s) sig m =>
(s -> a) -> m a
State.gets (Getting a s a -> s -> a
forall a s. Getting a s a -> s -> a
Lens.view Getting a s a
l))
{-# INLINE uses #-}
assign :: forall s a b sig m . (Has (State.State s) sig m) => ASetter s s a b -> b -> m ()
assign :: ASetter s s a b -> b -> m ()
assign ASetter s s a b
l b
b = (s -> s) -> m ()
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *).
Has (State s) sig m =>
(s -> s) -> m ()
State.modify (ASetter s s a b -> b -> s -> s
forall s t a b. ASetter s t a b -> b -> s -> t
Lens.set ASetter s s a b
l b
b)
{-# INLINE assign #-}
modifying :: forall s a b sig m . (Has (State.State s) sig m) => ASetter s s a b -> (a -> b) -> m ()
modifying :: ASetter s s a b -> (a -> b) -> m ()
modifying ASetter s s a b
l a -> b
f = (s -> s) -> m ()
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *).
Has (State s) sig m =>
(s -> s) -> m ()
State.modify (ASetter s s a b -> (a -> b) -> s -> s
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
Lens.over ASetter s s a b
l a -> b
f)
{-# INLINE modifying #-}
infix 4 .=, %=, ?=, +=, -=, *=, //=
infixr 2 <~
(.=) :: forall s a b sig m . (Has (State.State s) sig m) => ASetter s s a b -> b -> m ()
.= :: ASetter s s a b -> b -> m ()
(.=) = ASetter s s a b -> b -> m ()
forall s a b (sig :: (* -> *) -> * -> *) (m :: * -> *).
Has (State s) sig m =>
ASetter s s a b -> b -> m ()
assign
{-# INLINE (.=) #-}
(?=) :: forall s a b sig m . (Has (State.State s) sig m) => ASetter s s a (Maybe b) -> b -> m ()
ASetter s s a (Maybe b)
setter ?= :: ASetter s s a (Maybe b) -> b -> m ()
?= b
item = ASetter s s a (Maybe b)
setter ASetter s s a (Maybe b) -> Maybe b -> m ()
forall s a b (sig :: (* -> *) -> * -> *) (m :: * -> *).
Has (State s) sig m =>
ASetter s s a b -> b -> m ()
.= b -> Maybe b
forall a. a -> Maybe a
Just b
item
{-# INLINE (?=) #-}
(<~) :: forall s a b sig m . (Has (State s) sig m) => ASetter s s a b -> m b -> m ()
ASetter s s a b
setter <~ :: ASetter s s a b -> m b -> m ()
<~ m b
act = m b
act m b -> (b -> m ()) -> m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ASetter s s a b -> b -> m ()
forall s a b (sig :: (* -> *) -> * -> *) (m :: * -> *).
Has (State s) sig m =>
ASetter s s a b -> b -> m ()
assign ASetter s s a b
setter
{-# INLINE (<~) #-}
(%=) :: forall s a b sig m . (Has (State.State s) sig m) => ASetter s s a b -> (a -> b) -> m ()
%= :: ASetter s s a b -> (a -> b) -> m ()
(%=) = ASetter s s a b -> (a -> b) -> m ()
forall s a b (sig :: (* -> *) -> * -> *) (m :: * -> *).
Has (State s) sig m =>
ASetter s s a b -> (a -> b) -> m ()
modifying
{-# INLINE (%=) #-}
(+=) :: forall s a sig m . (Has (State.State s) sig m, Num a) => ASetter' s a -> a -> m ()
ASetter' s a
l += :: ASetter' s a -> a -> m ()
+= a
v = (s -> s) -> m ()
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *).
Has (State s) sig m =>
(s -> s) -> m ()
State.modify (ASetter' s a
l ASetter' s a -> a -> s -> s
forall a s t. Num a => ASetter s t a a -> a -> s -> t
+~ a
v)
{-# INLINE (+=) #-}
(-=) :: forall s a sig m . (Has (State.State s) sig m, Num a) => ASetter' s a -> a -> m ()
ASetter' s a
l -= :: ASetter' s a -> a -> m ()
-= a
v = (s -> s) -> m ()
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *).
Has (State s) sig m =>
(s -> s) -> m ()
State.modify (ASetter' s a
l ASetter' s a -> a -> s -> s
forall a s t. Num a => ASetter s t a a -> a -> s -> t
-~ a
v)
{-# INLINE (-=) #-}
(*=) :: forall s a sig m . (Has (State.State s) sig m, Num a) => ASetter' s a -> a -> m ()
ASetter' s a
l *= :: ASetter' s a -> a -> m ()
*= a
v = ASetter' s a -> (a -> a) -> m ()
forall s a b (sig :: (* -> *) -> * -> *) (m :: * -> *).
Has (State s) sig m =>
ASetter s s a b -> (a -> b) -> m ()
modifying ASetter' s a
l (a -> a -> a
forall a. Num a => a -> a -> a
* a
v)
{-# INLINE (*=) #-}
(//=) :: forall s a sig m . (Has (State.State s) sig m, Fractional a) => ASetter' s a -> a -> m ()
ASetter' s a
l //= :: ASetter' s a -> a -> m ()
//= a
v = ASetter' s a -> (a -> a) -> m ()
forall s a b (sig :: (* -> *) -> * -> *) (m :: * -> *).
Has (State s) sig m =>
ASetter s s a b -> (a -> b) -> m ()
modifying ASetter' s a
l (a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
v)
{-# INLINE (//=) #-}