module Optics.Lens
(
Lens
, Lens'
, lens
, equality'
, alongside
, united
, withLens
, A_Lens
, LensVL
, LensVL'
, lensVL
, toLensVL
, withLensVL
)
where
import Data.Profunctor.Indexed
import Optics.Internal.Optic
type Lens s t a b = Optic A_Lens NoIx s t a b
type Lens' s a = Optic' A_Lens NoIx s a
type LensVL s t a b = forall f. Functor f => (a -> f b) -> s -> f t
type LensVL' s a = LensVL s s a a
lens :: (s -> a) -> (s -> b -> t) -> Lens s t a b
lens :: (s -> a) -> (s -> b -> t) -> Lens s t a b
lens s -> a
get s -> b -> t
set = (forall (p :: * -> * -> * -> *) i.
Profunctor p =>
Optic_ A_Lens p i (Curry NoIx i) s t a b)
-> Lens s t a b
forall k (is :: IxList) s t a b.
(forall (p :: * -> * -> * -> *) i.
Profunctor p =>
Optic_ k p i (Curry is i) s t a b)
-> Optic k is s t a b
Optic ((forall (p :: * -> * -> * -> *) i.
Profunctor p =>
Optic_ A_Lens p i (Curry NoIx i) s t a b)
-> Lens s t a b)
-> (forall (p :: * -> * -> * -> *) i.
Profunctor p =>
Optic_ A_Lens p i (Curry NoIx i) s t a b)
-> Lens s t a b
forall a b. (a -> b) -> a -> b
$
(s -> (a, s)) -> ((b, s) -> t) -> p i (a, s) (b, s) -> p i s t
forall (p :: * -> * -> * -> *) a b c d i.
Profunctor p =>
(a -> b) -> (c -> d) -> p i b c -> p i a d
dimap (\s
s -> (s -> a
get s
s, s
s))
(\(b
b, s
s) -> s -> b -> t
set s
s b
b)
(p i (a, s) (b, s) -> p i s t)
-> (p i a b -> p i (a, s) (b, s)) -> p i a b -> p i s t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p i a b -> p i (a, s) (b, s)
forall (p :: * -> * -> * -> *) i a b c.
Strong p =>
p i a b -> p i (a, c) (b, c)
first'
{-# INLINE lens #-}
withLens
:: Is k A_Lens
=> Optic k is s t a b
-> ((s -> a) -> (s -> b -> t) -> r)
-> r
withLens :: Optic k is s t a b -> ((s -> a) -> (s -> b -> t) -> r) -> r
withLens Optic k is s t a b
o (s -> a) -> (s -> b -> t) -> r
k = case Optic A_Lens is s t a b
-> Optic_ A_Lens (Store a b) Any (Curry is Any) s t a b
forall (p :: * -> * -> * -> *) k (is :: IxList) s t a b i.
Profunctor p =>
Optic k is s t a b -> Optic_ k p i (Curry is i) s t a b
getOptic (Optic k is s t a b -> Optic A_Lens is s t a b
forall destKind srcKind (is :: IxList) s t a b.
Is srcKind destKind =>
Optic srcKind is s t a b -> Optic destKind is s t a b
castOptic @A_Lens Optic k is s t a b
o) Optic__ (Store a b) Any (Curry is Any) s t a b
-> Optic__ (Store a b) Any (Curry is Any) s t a b
forall a b. (a -> b) -> a -> b
$ (a -> a) -> (a -> b -> b) -> Store a b Any a b
forall a b i s t. (s -> a) -> (s -> b -> t) -> Store a b i s t
Store a -> a
forall a. a -> a
id (\a
_ -> b -> b
forall a. a -> a
id) of
Store get set -> (s -> a) -> (s -> b -> t) -> r
k s -> a
get s -> b -> t
set
{-# INLINE withLens #-}
lensVL :: LensVL s t a b -> Lens s t a b
lensVL :: LensVL s t a b -> Lens s t a b
lensVL LensVL s t a b
l = (forall (p :: * -> * -> * -> *) i.
Profunctor p =>
Optic_ A_Lens p i (Curry NoIx i) s t a b)
-> Lens s t a b
forall k (is :: IxList) s t a b.
(forall (p :: * -> * -> * -> *) i.
Profunctor p =>
Optic_ k p i (Curry is i) s t a b)
-> Optic k is s t a b
Optic (LensVL s t a b -> p i a b -> p i s t
forall (p :: * -> * -> * -> *) a b s t i.
Strong p =>
(forall (f :: * -> *). Functor f => (a -> f b) -> s -> f t)
-> p i a b -> p i s t
linear LensVL s t a b
l)
{-# INLINE lensVL #-}
toLensVL :: Is k A_Lens => Optic k is s t a b -> LensVL s t a b
toLensVL :: Optic k is s t a b -> LensVL s t a b
toLensVL Optic k is s t a b
o = Star f (Curry is Any) s t -> s -> f t
forall (f :: * -> *) i a b. Star f i a b -> a -> f b
runStar (Star f (Curry is Any) s t -> s -> f t)
-> (Star f Any a b -> Star f (Curry is Any) s t)
-> Star f Any a b
-> s
-> f t
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. Optic A_Lens is s t a b
-> Optic_ A_Lens (Star f) Any (Curry is Any) s t a b
forall (p :: * -> * -> * -> *) k (is :: IxList) s t a b i.
Profunctor p =>
Optic k is s t a b -> Optic_ k p i (Curry is i) s t a b
getOptic (Optic k is s t a b -> Optic A_Lens is s t a b
forall destKind srcKind (is :: IxList) s t a b.
Is srcKind destKind =>
Optic srcKind is s t a b -> Optic destKind is s t a b
castOptic @A_Lens Optic k is s t a b
o) (Star f Any a b -> s -> f t)
-> ((a -> f b) -> Star f Any a b) -> (a -> f b) -> s -> f t
forall a b c. Coercible a b => (b -> c) -> (a -> b) -> a -> c
.# (a -> f b) -> Star f Any a b
forall (f :: * -> *) i a b. (a -> f b) -> Star f i a b
Star
{-# INLINE toLensVL #-}
withLensVL
:: Is k A_Lens
=> Optic k is s t a b
-> (LensVL s t a b -> r)
-> r
withLensVL :: Optic k is s t a b -> (LensVL s t a b -> r) -> r
withLensVL Optic k is s t a b
o LensVL s t a b -> r
k = LensVL s t a b -> r
k (Optic k is s t a b -> LensVL s t a b
forall k (is :: IxList) s t a b.
Is k A_Lens =>
Optic k is s t a b -> LensVL s t a b
toLensVL Optic k is s t a b
o)
{-# INLINE withLensVL #-}
equality' :: Lens a b a b
equality' :: Lens a b a b
equality' = LensVL a b a b -> Lens a b a b
forall s t a b. LensVL s t a b -> Lens s t a b
lensVL forall a b. (a -> b) -> a -> b
LensVL a b a b
($!)
{-# INLINE equality' #-}
alongside
:: (Is k A_Lens, Is l A_Lens)
=> Optic k is s t a b
-> Optic l js s' t' a' b'
-> Lens (s, s') (t, t') (a, a') (b, b')
alongside :: Optic k is s t a b
-> Optic l js s' t' a' b' -> Lens (s, s') (t, t') (a, a') (b, b')
alongside Optic k is s t a b
l Optic l js s' t' a' b'
r = Optic k is s t a b
-> ((s -> a)
-> (s -> b -> t) -> Lens (s, s') (t, t') (a, a') (b, b'))
-> Lens (s, s') (t, t') (a, a') (b, b')
forall k (is :: IxList) s t a b r.
Is k A_Lens =>
Optic k is s t a b -> ((s -> a) -> (s -> b -> t) -> r) -> r
withLens Optic k is s t a b
l (((s -> a)
-> (s -> b -> t) -> Lens (s, s') (t, t') (a, a') (b, b'))
-> Lens (s, s') (t, t') (a, a') (b, b'))
-> ((s -> a)
-> (s -> b -> t) -> Lens (s, s') (t, t') (a, a') (b, b'))
-> Lens (s, s') (t, t') (a, a') (b, b')
forall a b. (a -> b) -> a -> b
$ \s -> a
getl s -> b -> t
setl ->
Optic l js s' t' a' b'
-> ((s' -> a')
-> (s' -> b' -> t') -> Lens (s, s') (t, t') (a, a') (b, b'))
-> Lens (s, s') (t, t') (a, a') (b, b')
forall k (is :: IxList) s t a b r.
Is k A_Lens =>
Optic k is s t a b -> ((s -> a) -> (s -> b -> t) -> r) -> r
withLens Optic l js s' t' a' b'
r (((s' -> a')
-> (s' -> b' -> t') -> Lens (s, s') (t, t') (a, a') (b, b'))
-> Lens (s, s') (t, t') (a, a') (b, b'))
-> ((s' -> a')
-> (s' -> b' -> t') -> Lens (s, s') (t, t') (a, a') (b, b'))
-> Lens (s, s') (t, t') (a, a') (b, b')
forall a b. (a -> b) -> a -> b
$ \s' -> a'
getr s' -> b' -> t'
setr ->
((s, s') -> (a, a'))
-> ((s, s') -> (b, b') -> (t, t'))
-> Lens (s, s') (t, t') (a, a') (b, b')
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens (\(s
s, s'
s') -> (s -> a
getl s
s, s' -> a'
getr s'
s' ))
(\(s
s, s'
s') (b
b, b'
b') -> (s -> b -> t
setl s
s b
b, s' -> b' -> t'
setr s'
s' b'
b'))
{-# INLINE alongside #-}
united :: Lens' a ()
united :: Lens' a ()
united = (a -> ()) -> (a -> () -> a) -> Lens' a ()
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens (() -> a -> ()
forall a b. a -> b -> a
const ()) a -> () -> a
forall a b. a -> b -> a
const
{-# INLINE united #-}