{-# LANGUAGE CPP #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE Trustworthy #-}
module Control.Lens.Yocto (
Optic, Optic',
LensLike, LensLike',
Lens, Lens',
Traversal, Traversal',
#ifdef MIN_VERSION_profunctors
Prism, Prism',
Iso, Iso',
#endif
view, set, over,
(<&>),
#ifdef MIN_VERSION_profunctors
iso,
prism,
prism',
#endif
)where
import Control.Applicative (Applicative (..), Const (..))
import Data.Coerce (coerce)
import Data.Functor.Identity (Identity (..))
import Prelude (Functor (..), const, (.))
#ifdef MIN_VERSION_profunctors
import Data.Profunctor (Choice (..), Profunctor (..))
import Prelude (Either (..), Maybe, either, maybe, (.))
#endif
import Data.Functor ((<&>))
type Optic p f s t a b = p a (f b) -> p s (f t)
type Optic' p f s a = Optic p f s s a a
type LensLike f s t a b = Optic (->) f s t a b
type LensLike' f s a = LensLike f s s a a
type Lens s t a b = forall f. Functor f => LensLike f s t a b
type Lens' s a = Lens s s a a
type Traversal s t a b = forall f. Applicative f => LensLike f s t a b
type Traversal' s a = Lens s s a a
#ifdef MIN_VERSION_profunctors
type Prism s t a b = forall p f. (Choice p, Applicative f) => Optic p f s t a b
type Prism' s a = Prism s s a a
type Iso s t a b = forall p f. (Profunctor p, Functor f) => Optic p f s t a b
type Iso' s a = Iso s s a a
#endif
view :: LensLike' (Const a) s a -> s -> a
view :: forall a s. LensLike' (Const @(*) a) s a -> s -> a
view LensLike' (Const @(*) a) s a
l = (->) @LiftedRep @LiftedRep s (Const @(*) a s) -> s -> a
forall a b. Coercible @(*) a b => a -> b
coerce (LensLike' (Const @(*) a) s a
l a -> Const @(*) a a
forall {k} a (b :: k). a -> Const @k a b
Const)
{-# INLINE view #-}
set :: LensLike Identity s t a b -> b -> s -> t
set :: forall s t a b. LensLike Identity s t a b -> b -> s -> t
set LensLike Identity s t a b
l = LensLike Identity s t a b -> (a -> b) -> s -> t
forall s t a b. LensLike Identity s t a b -> (a -> b) -> s -> t
over LensLike Identity s t a b
l ((a -> b) -> s -> t) -> (b -> a -> b) -> b -> s -> t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> a -> b
forall a b. a -> b -> a
const
{-# INLINE set #-}
over :: LensLike Identity s t a b -> (a -> b) -> s -> t
over :: forall s t a b. LensLike Identity s t a b -> (a -> b) -> s -> t
over = LensLike Identity s t a b -> (a -> b) -> s -> t
forall a b. Coercible @(*) a b => a -> b
coerce
{-# INLINE over #-}
#ifdef MIN_VERSION_profunctors
iso :: (s -> a) -> (b -> t) -> Iso s t a b
iso sa bt = dimap sa (fmap bt)
{-# INLINE iso #-}
prism :: (b -> t) -> (s -> Either t a) -> Prism s t a b
prism bt seta = dimap seta (either pure (fmap bt)) . right'
{-# INLINE prism #-}
prism' :: (b -> s) -> (s -> Maybe a) -> Prism s s a b
prism' bs sma = prism bs (\s -> maybe (Left s) Right (sma s))
{-# INLINE prism' #-}
#endif