{-# LANGUAGE TypeOperators #-}
module Data.Label.Monadic
(
gets
, puts
, modify
, modifyAndGet
, (=:)
, (=.)
, asks
, local
)
where
import Control.Monad
import Data.Label.Mono (Lens)
import qualified Data.Label.Total as Total
import qualified Control.Monad.Reader as Reader
import qualified Control.Monad.State as State
gets :: State.MonadState f m => Lens (->) f o -> m o
gets = State.gets . Total.get
puts :: State.MonadState f m => Lens (->) f o -> o -> m ()
puts l = State.modify . Total.set l
modify :: State.MonadState f m => Lens (->) f o -> (o -> o) -> m ()
modify l = State.modify . Total.modify l
infixr 2 =:
(=:) :: State.MonadState f m => Lens (->) f o -> o -> m ()
(=:) = puts
infixr 2 =.
(=.) :: State.MonadState f m => Lens (->) f o -> (o -> o) -> m ()
(=.) = modify
asks :: Reader.MonadReader f m => (Lens (->) f o) -> m o
asks = Reader.asks . Total.get
local :: Reader.MonadReader f m => (Lens (->) f o) -> (o -> o) -> m a -> m a
local l f = Reader.local (Total.modify l f)
modifyAndGet :: State.MonadState f m => (Lens (->) f o) -> (o -> (a, o)) -> m a
modifyAndGet l f =
do (b, a) <- f `liftM` gets l
puts l a
return b