{-# LANGUAGE TypeOperators, NoMonomorphismRestriction #-}
module CHR.Data.Lens.FCLabels
( (:->)
, Lens
, (^*)
, (^.)
, (^=)
, (^$=)
, getL
, (=.)
, (=:)
, (=$:)
, modifyAndGet, (=$^:), modL
, getl
, focus
, mkLabel
, fstl
, sndl
, fst3l
, snd3l
, trd3l
, isoMb
, isoMbWithDefault
)
where
import Prelude hiding ((.), id)
import qualified Control.Monad.State as MS
import Control.Monad.Trans
import Control.Category
import Data.Label hiding (Lens, lens)
import qualified Data.Label.Base as L
import qualified Data.Label as L
import Data.Label.Monadic((=:), (=.), modifyAndGet)
import qualified Data.Label.Monadic as M
import qualified Data.Label.Partial as P
import CHR.Utils
type Lens a b = a :-> b
infixl 9 ^*
(^*) :: (a :-> b) -> (b :-> c) -> (a :-> c)
f1 ^* f2 = f2 . f1
{-# INLINE (^*) #-}
infixl 8 ^.
(^.) :: a -> (a :-> b) -> b
a ^. f = get f a
{-# INLINE (^.) #-}
getL :: (f :-> a) -> f -> a
getL = get
{-# INLINE getL #-}
infixr 4 ^=
(^=) :: (a :-> b) -> b -> a -> a
(^=) = set
{-# INLINE (^=) #-}
infixr 4 ^$=
(^$=) :: (a :-> b) -> (b -> b) -> a -> a
(^$=) = modify
{-# INLINE (^$=) #-}
infixr 4 =$^:
(=$^:), modL :: MS.MonadState f m => (f :-> o) -> (o -> (a,o)) -> m a
(=$^:) = M.modifyAndGet
{-# INLINE (=$^:) #-}
modL = M.modifyAndGet
{-# INLINE modL #-}
infixr 4 =$:
(=$:) :: MS.MonadState f m => (f :-> o) -> (o -> o) -> m ()
(=$:) = M.modify
{-# INLINE (=$:) #-}
focus :: (MS.MonadState a m, MS.MonadState b m) => (a :-> b) -> m c -> m c
focus f m = do
a <- MS.get
(b,c) <- do {MS.put (get f a) ; c <- m ; b <- MS.get ; return (b,c)}
MS.put $ set f b a
return c
getl :: MS.MonadState f m => (f :-> o) -> m o
getl = M.gets
{-# INLINE getl #-}
fstl = L.fst
{-# INLINE fstl #-}
sndl = L.snd
{-# INLINE sndl #-}
fst3l = L.fst3
{-# INLINE fst3l #-}
snd3l = L.snd3
{-# INLINE snd3l #-}
trd3l = L.trd3
{-# INLINE trd3l #-}
isoMbWithDefault :: o -> (f :-> Maybe o) -> (f :-> o)
isoMbWithDefault dflt f = iso (Iso (maybe dflt id) (Just)) . f
isoMb :: String -> (f :-> Maybe o) -> (f :-> o)
isoMb msg f = iso (Iso (panicJust msg) (Just)) . f