{-# LANGUAGE CPP #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE Trustworthy #-}
#if __GLASGOW_HASKELL__ >= 800
{-# OPTIONS_GHC -Wno-redundant-constraints -Wno-trustworthy-safe #-}
#endif
module Control.Lens.Review
(
Review
, AReview
, unto
, un
, re
, review, reviews
, reuse, reuses
, (#)
, Bifunctor(bimap)
, retagged
, Reviewable
, reviewing
) where
import Control.Monad.Reader as Reader
import Control.Monad.State as State
import Control.Lens.Getter
import Control.Lens.Internal.Review
import Control.Lens.Type
import Data.Bifunctor
import Data.Functor.Identity
import Data.Profunctor
import Data.Profunctor.Unsafe
import Data.Tagged
import Data.Void
infixr 8 #
unto :: (Profunctor p, Bifunctor p, Functor f) => (b -> t) -> Optic p f s t a b
unto :: (b -> t) -> Optic p f s t a b
unto b -> t
f = (Void -> s) -> p Void (f t) -> p s (f t)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first Void -> s
forall a. Void -> a
absurd (p Void (f t) -> p s (f t))
-> (p a (f b) -> p Void (f t)) -> Optic p f s t a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Void -> a) -> p a (f t) -> p Void (f t)
forall (p :: * -> * -> *) a b c.
Profunctor p =>
(a -> b) -> p b c -> p a c
lmap Void -> a
forall a. Void -> a
absurd (p a (f t) -> p Void (f t))
-> (p a (f b) -> p a (f t)) -> p a (f b) -> p Void (f t)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (f b -> f t) -> p a (f b) -> p a (f t)
forall (p :: * -> * -> *) b c a.
Profunctor p =>
(b -> c) -> p a b -> p a c
rmap ((b -> t) -> f b -> f t
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> t
f)
{-# INLINE unto #-}
un :: (Profunctor p, Bifunctor p, Functor f) => Getting a s a -> Optic' p f a s
un :: Getting a s a -> Optic' p f a s
un = (s -> a) -> Optic' p f a s
forall (p :: * -> * -> *) (f :: * -> *) b t s a.
(Profunctor p, Bifunctor p, Functor f) =>
(b -> t) -> Optic p f s t a b
unto ((s -> a) -> Optic' p f a s)
-> (Getting a s a -> s -> a) -> Getting a s a -> Optic' p f a s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Getting a s a -> s -> a
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view
re :: AReview t b -> Getter b t
re :: AReview t b -> Getter b t
re AReview t b
p = (b -> t) -> Optic' (->) f b t
forall (p :: * -> * -> *) (f :: * -> *) s a.
(Profunctor p, Contravariant f) =>
(s -> a) -> Optic' p f s a
to (Identity t -> t
forall a. Identity a -> a
runIdentity (Identity t -> t)
-> (Tagged b (Identity b) -> Identity t)
-> Tagged b (Identity b)
-> t
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. Tagged t (Identity t) -> Identity t
forall k (s :: k) b. Tagged s b -> b
unTagged (Tagged t (Identity t) -> Identity t)
-> AReview t b -> Tagged b (Identity b) -> Identity t
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. AReview t b
p (Tagged b (Identity b) -> t)
-> (Identity b -> Tagged b (Identity b)) -> Identity b -> t
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible b a) =>
p b c -> q a b -> p a c
.# Identity b -> Tagged b (Identity b)
forall k (s :: k) b. b -> Tagged s b
Tagged (Identity b -> t) -> (b -> Identity b) -> b -> t
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible b a) =>
p b c -> q a b -> p a c
.# b -> Identity b
forall a. a -> Identity a
Identity)
{-# INLINE re #-}
review :: MonadReader b m => AReview t b -> m t
review :: AReview t b -> m t
review AReview t b
p = (b -> t) -> m t
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (Identity t -> t
forall a. Identity a -> a
runIdentity (Identity t -> t)
-> (Tagged b (Identity b) -> Identity t)
-> Tagged b (Identity b)
-> t
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. Tagged t (Identity t) -> Identity t
forall k (s :: k) b. Tagged s b -> b
unTagged (Tagged t (Identity t) -> Identity t)
-> AReview t b -> Tagged b (Identity b) -> Identity t
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. AReview t b
p (Tagged b (Identity b) -> t)
-> (Identity b -> Tagged b (Identity b)) -> Identity b -> t
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible b a) =>
p b c -> q a b -> p a c
.# Identity b -> Tagged b (Identity b)
forall k (s :: k) b. b -> Tagged s b
Tagged (Identity b -> t) -> (b -> Identity b) -> b -> t
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible b a) =>
p b c -> q a b -> p a c
.# b -> Identity b
forall a. a -> Identity a
Identity)
{-# INLINE review #-}
(#) :: AReview t b -> b -> t
# :: AReview t b -> b -> t
(#) AReview t b
p = Identity t -> t
forall a. Identity a -> a
runIdentity (Identity t -> t)
-> (Tagged b (Identity b) -> Identity t)
-> Tagged b (Identity b)
-> t
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. Tagged t (Identity t) -> Identity t
forall k (s :: k) b. Tagged s b -> b
unTagged (Tagged t (Identity t) -> Identity t)
-> AReview t b -> Tagged b (Identity b) -> Identity t
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. AReview t b
p (Tagged b (Identity b) -> t)
-> (Identity b -> Tagged b (Identity b)) -> Identity b -> t
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible b a) =>
p b c -> q a b -> p a c
.# Identity b -> Tagged b (Identity b)
forall k (s :: k) b. b -> Tagged s b
Tagged (Identity b -> t) -> (b -> Identity b) -> b -> t
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible b a) =>
p b c -> q a b -> p a c
.# b -> Identity b
forall a. a -> Identity a
Identity
{-# INLINE (#) #-}
reviews :: MonadReader b m => AReview t b -> (t -> r) -> m r
reviews :: AReview t b -> (t -> r) -> m r
reviews AReview t b
p t -> r
tr = (b -> r) -> m r
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (t -> r
tr (t -> r)
-> (Tagged b (Identity b) -> t) -> Tagged b (Identity b) -> r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Identity t -> t
forall a. Identity a -> a
runIdentity (Identity t -> t)
-> (Tagged b (Identity b) -> Identity t)
-> Tagged b (Identity b)
-> t
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. Tagged t (Identity t) -> Identity t
forall k (s :: k) b. Tagged s b -> b
unTagged (Tagged t (Identity t) -> Identity t)
-> AReview t b -> Tagged b (Identity b) -> Identity t
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. AReview t b
p (Tagged b (Identity b) -> r)
-> (Identity b -> Tagged b (Identity b)) -> Identity b -> r
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible b a) =>
p b c -> q a b -> p a c
.# Identity b -> Tagged b (Identity b)
forall k (s :: k) b. b -> Tagged s b
Tagged (Identity b -> r) -> (b -> Identity b) -> b -> r
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible b a) =>
p b c -> q a b -> p a c
.# b -> Identity b
forall a. a -> Identity a
Identity)
{-# INLINE reviews #-}
reuse :: MonadState b m => AReview t b -> m t
reuse :: AReview t b -> m t
reuse AReview t b
p = (b -> t) -> m t
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets (Identity t -> t
forall a. Identity a -> a
runIdentity (Identity t -> t)
-> (Tagged b (Identity b) -> Identity t)
-> Tagged b (Identity b)
-> t
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. Tagged t (Identity t) -> Identity t
forall k (s :: k) b. Tagged s b -> b
unTagged (Tagged t (Identity t) -> Identity t)
-> AReview t b -> Tagged b (Identity b) -> Identity t
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. AReview t b
p (Tagged b (Identity b) -> t)
-> (Identity b -> Tagged b (Identity b)) -> Identity b -> t
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible b a) =>
p b c -> q a b -> p a c
.# Identity b -> Tagged b (Identity b)
forall k (s :: k) b. b -> Tagged s b
Tagged (Identity b -> t) -> (b -> Identity b) -> b -> t
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible b a) =>
p b c -> q a b -> p a c
.# b -> Identity b
forall a. a -> Identity a
Identity)
{-# INLINE reuse #-}
reuses :: MonadState b m => AReview t b -> (t -> r) -> m r
reuses :: AReview t b -> (t -> r) -> m r
reuses AReview t b
p t -> r
tr = (b -> r) -> m r
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets (t -> r
tr (t -> r)
-> (Tagged b (Identity b) -> t) -> Tagged b (Identity b) -> r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Identity t -> t
forall a. Identity a -> a
runIdentity (Identity t -> t)
-> (Tagged b (Identity b) -> Identity t)
-> Tagged b (Identity b)
-> t
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. Tagged t (Identity t) -> Identity t
forall k (s :: k) b. Tagged s b -> b
unTagged (Tagged t (Identity t) -> Identity t)
-> AReview t b -> Tagged b (Identity b) -> Identity t
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. AReview t b
p (Tagged b (Identity b) -> r)
-> (Identity b -> Tagged b (Identity b)) -> Identity b -> r
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible b a) =>
p b c -> q a b -> p a c
.# Identity b -> Tagged b (Identity b)
forall k (s :: k) b. b -> Tagged s b
Tagged (Identity b -> r) -> (b -> Identity b) -> b -> r
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible b a) =>
p b c -> q a b -> p a c
.# b -> Identity b
forall a. a -> Identity a
Identity)
{-# INLINE reuses #-}
reviewing :: (Bifunctor p, Functor f) => Optic Tagged Identity s t a b -> Optic' p f t b
reviewing :: Optic Tagged Identity s t a b -> Optic' p f t b
reviewing Optic Tagged Identity s t a b
p = (b -> t) -> (f b -> f t) -> Optic' p f t b
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap b -> t
f ((b -> t) -> f b -> f t
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> t
f) where
f :: b -> t
f = Identity t -> t
forall a. Identity a -> a
runIdentity (Identity t -> t) -> (b -> Identity t) -> b -> t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tagged s (Identity t) -> Identity t
forall k (s :: k) b. Tagged s b -> b
unTagged (Tagged s (Identity t) -> Identity t)
-> (b -> Tagged s (Identity t)) -> b -> Identity t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Optic Tagged Identity s t a b
p Optic Tagged Identity s t a b
-> (b -> Tagged a (Identity b)) -> b -> Tagged s (Identity t)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Identity b -> Tagged a (Identity b)
forall k (s :: k) b. b -> Tagged s b
Tagged (Identity b -> Tagged a (Identity b))
-> (b -> Identity b) -> b -> Tagged a (Identity b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Identity b
forall a. a -> Identity a
Identity