{-# LANGUAGE Rank2Types #-}
module Lens.Family2.State.Strict
( LFS.zoom
, use, uses
, (%=)
, assign, (.=)
, (%%=)
, (<~)
, (+=), (-=), (*=)
, (//=)
, (&&=), (||=)
, (<>=)
, (%!=)
, (+!=), (-!=), (*!=)
, (//!=)
, (&&!=), (||!=)
, (<>!=)
, LFS.Zooming
, LensLike, LensLike'
, FoldLike, Constant
, Setter, Setter', Identical
, LFS.StateT, MonadState, Writer
) where
import Data.Tuple (swap)
import Control.Monad (liftM)
import Control.Monad.Trans.Writer.Lazy (Writer, writer, runWriter)
import Control.Monad.State.Strict (MonadState, get, modify, modify', state)
import Lens.Family2
import qualified Lens.Family.State.Strict as LFS
use :: MonadState s m => FoldLike a s t a b -> m a
use l = view l `liftM` get
uses :: MonadState s m => FoldLike r s t a b -> (a -> r) -> m r
uses l f = views l f `liftM` get
infix 4 %=
(%=) :: MonadState s m => Setter s s a b -> (a -> b) -> m ()
l %= f = modify (l %~ f)
infix 4 .=
(.=) :: MonadState s m => Setter s s a b -> b -> m ()
l .= v = l %= const v
assign :: MonadState s m => Setter s s a b -> b -> m ()
assign = (.=)
infixr 2 <~
(<~) :: MonadState s m => Setter s s a b -> m b -> m ()
l <~ v = assign l =<< v
infix 4 %%=
(%%=) :: MonadState s m => LensLike (Writer c) s s a b -> (a -> (c, b)) -> m c
l %%= f = state (swap . runWriter . l (writer . swap . f))
infixr 4 +=, -=, *=
(+=), (-=), (*=) :: (MonadState s m, Num a) => Setter' s a -> a -> m ()
l += a = l %= (+ a)
l -= a = l %= subtract a
l *= a = l %= (* a)
infixr 4 //=
(//=) :: (MonadState s m, Fractional a) => Setter' s a -> a -> m ()
l //= a = l %= (/ a)
infixr 4 &&=, ||=
(&&=), (||=) :: MonadState s m => Setter' s Bool -> Bool -> m ()
l &&= a = l %= (&& a)
l ||= a = l %= (|| a)
infixr 4 <>=
(<>=) :: (MonadState s m, Monoid a) => Setter' s a -> a -> m ()
l <>= a = l %= (<> a)
infix 4 %!=
(%!=) :: MonadState s m => Setter s s a b -> (a -> b) -> m ()
l %!= f = modify' (l %~ f)
infixr 4 +!=, -!=, *!=
(+!=), (-!=), (*!=) :: (MonadState s m, Num a) => Setter' s a -> a -> m ()
l +!= a = l %!= (+ a)
l -!= a = l %!= subtract a
l *!= a = l %!= (* a)
infixr 4 //!=
(//!=) :: (MonadState s m, Fractional a) => Setter' s a -> a -> m ()
l //!= a = l %!= (/ a)
infixr 4 &&!=, ||!=
(&&!=), (||!=) :: MonadState s m => Setter' s Bool -> Bool -> m ()
l &&!= a = l %!= (&& a)
l ||!= a = l %!= (|| a)
infixr 4 <>!=
(<>!=) :: (MonadState s m, Monoid a) => Setter' s a -> a -> m ()
l <>!= a = l %!= (<> a)