module Optics.Iso
(
Iso
, Iso'
, iso
, equality
, simple
, coerced
, coercedTo
, coerced1
, non
, non'
, anon
, curried
, uncurried
, flipped
, involuted
, Swapped(..)
, withIso
, au
, under
, An_Iso
)
where
import Data.Tuple
import Data.Bifunctor
import Data.Coerce
import Data.Maybe
import Data.Profunctor.Indexed
import Optics.AffineFold
import Optics.Prism
import Optics.Review
import Optics.Internal.Optic
type Iso s t a b = Optic An_Iso NoIx s t a b
type Iso' s a = Optic' An_Iso NoIx s a
iso :: (s -> a) -> (b -> t) -> Iso s t a b
iso :: forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso s -> a
f b -> t
g = 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 :: * -> * -> * -> *) a b c d i.
Profunctor p =>
(a -> b) -> (c -> d) -> p i b c -> p i a d
dimap s -> a
f b -> t
g)
{-# INLINE iso #-}
withIso :: Iso s t a b -> ((s -> a) -> (b -> t) -> r) -> r
withIso :: forall s t a b r. Iso s t a b -> ((s -> a) -> (b -> t) -> r) -> r
withIso Iso s t a b
o (s -> a) -> (b -> t) -> r
k = case 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 Iso s t a b
o (forall a b i s t. (s -> a) -> (b -> t) -> Exchange a b i s t
Exchange forall a. a -> a
id forall a. a -> a
id) of
Exchange s -> a
sa b -> t
bt -> (s -> a) -> (b -> t) -> r
k s -> a
sa b -> t
bt
{-# INLINE withIso #-}
au :: Functor f => Iso s t a b -> ((b -> t) -> f s) -> f a
au :: forall (f :: * -> *) s t a b.
Functor f =>
Iso s t a b -> ((b -> t) -> f s) -> f a
au Iso s t a b
k = forall s t a b r. Iso s t a b -> ((s -> a) -> (b -> t) -> r) -> r
withIso Iso s t a b
k forall a b. (a -> b) -> a -> b
$ \s -> a
sa b -> t
bt (b -> t) -> f s
f -> s -> a
sa forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (b -> t) -> f s
f b -> t
bt
{-# INLINE au #-}
under :: Iso s t a b -> (t -> s) -> b -> a
under :: forall s t a b. Iso s t a b -> (t -> s) -> b -> a
under Iso s t a b
k = forall s t a b r. Iso s t a b -> ((s -> a) -> (b -> t) -> r) -> r
withIso Iso s t a b
k forall a b. (a -> b) -> a -> b
$ \s -> a
sa b -> t
bt t -> s
ts -> s -> a
sa forall b c a. (b -> c) -> (a -> b) -> a -> c
. t -> s
ts forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> t
bt
{-# INLINE under #-}
equality :: (s ~ a, t ~ b) => Iso s t a b
equality :: forall s a t b. (s ~ a, t ~ b) => Iso s t a b
equality = 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 a. a -> a
id
{-# INLINE equality #-}
simple :: Iso' a a
simple :: forall a. Iso' a a
simple = 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 a. a -> a
id
{-# INLINE simple #-}
coerced :: (Coercible s a, Coercible t b) => Iso s t a b
coerced :: forall s a t b. (Coercible s a, Coercible t b) => Iso s t a b
coerced = 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 :: * -> * -> * -> *) a b i c.
(Profunctor p, Coercible a b) =>
p i a c -> p i b c
lcoerce' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (p :: * -> * -> * -> *) a b i c.
(Profunctor p, Coercible a b) =>
p i c a -> p i c b
rcoerce')
{-# INLINE coerced #-}
coercedTo :: forall a s. Coercible s a => Iso' s a
coercedTo :: forall a s. Coercible s a => Iso' s a
coercedTo = 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 :: * -> * -> * -> *) a b i c.
(Profunctor p, Coercible a b) =>
p i a c -> p i b c
lcoerce' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (p :: * -> * -> * -> *) a b i c.
(Profunctor p, Coercible a b) =>
p i c a -> p i c b
rcoerce')
{-# INLINE coercedTo #-}
coerced1
:: forall f s a. (Coercible s (f s), Coercible a (f a))
=> Iso (f s) (f a) s a
coerced1 :: forall (f :: * -> *) s a.
(Coercible s (f s), Coercible a (f a)) =>
Iso (f s) (f a) s a
coerced1 = 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 :: * -> * -> * -> *) a b i c.
(Profunctor p, Coercible a b) =>
p i a c -> p i b c
lcoerce' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (p :: * -> * -> * -> *) a b i c.
(Profunctor p, Coercible a b) =>
p i c a -> p i c b
rcoerce')
{-# INLINE coerced1 #-}
non :: Eq a => a -> Iso' (Maybe a) a
non :: forall a. Eq a => a -> Iso' (Maybe a) a
non = forall a. Prism' a () -> Iso' (Maybe a) a
non' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Eq a => a -> Prism' a ()
only
{-# INLINE non #-}
non' :: Prism' a () -> Iso' (Maybe a) a
non' :: forall a. Prism' a () -> Iso' (Maybe a) a
non' Prism' a ()
p = forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso (forall a. a -> Maybe a -> a
fromMaybe a
def) a -> Maybe a
go where
def :: a
def = forall k (is :: IxList) t b.
Is k A_Review =>
Optic' k is t b -> b -> t
review Prism' a ()
p ()
go :: a -> Maybe a
go a
b | Prism' a ()
p forall k (is :: IxList) s a.
Is k An_AffineFold =>
Optic' k is s a -> s -> Bool
`isn't` a
b = forall a. a -> Maybe a
Just a
b
| Bool
otherwise = forall a. Maybe a
Nothing
{-# INLINE non' #-}
anon :: a -> (a -> Bool) -> Iso' (Maybe a) a
anon :: forall a. a -> (a -> Bool) -> Iso' (Maybe a) a
anon a
a = forall a. Prism' a () -> Iso' (Maybe a) a
non' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> (a -> Bool) -> Prism' a ()
nearly a
a
{-# INLINE anon #-}
curried :: Iso ((a, b) -> c) ((d, e) -> f) (a -> b -> c) (d -> e -> f)
curried :: forall a b c d e f.
Iso ((a, b) -> c) ((d, e) -> f) (a -> b -> c) (d -> e -> f)
curried = forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso forall a b c. ((a, b) -> c) -> a -> b -> c
curry forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry
{-# INLINE curried #-}
uncurried :: Iso (a -> b -> c) (d -> e -> f) ((a, b) -> c) ((d, e) -> f)
uncurried :: forall a b c d e f.
Iso (a -> b -> c) (d -> e -> f) ((a, b) -> c) ((d, e) -> f)
uncurried = forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall a b c. ((a, b) -> c) -> a -> b -> c
curry
{-# INLINE uncurried #-}
flipped :: Iso (a -> b -> c) (a' -> b' -> c') (b -> a -> c) (b' -> a' -> c')
flipped :: forall a b c a' b' c'.
Iso (a -> b -> c) (a' -> b' -> c') (b -> a -> c) (b' -> a' -> c')
flipped = forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a b c. (a -> b -> c) -> b -> a -> c
flip
{-# INLINE flipped #-}
involuted :: (a -> a) -> Iso' a a
involuted :: forall a. (a -> a) -> Iso' a a
involuted a -> a
a = forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso a -> a
a a -> a
a
{-# INLINE involuted #-}
class Bifunctor p => Swapped p where
swapped :: Iso (p a b) (p c d) (p b a) (p d c)
instance Swapped (,) where
swapped :: forall a b c d. Iso (a, b) (c, d) (b, a) (d, c)
swapped = forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso forall a b. (a, b) -> (b, a)
swap forall a b. (a, b) -> (b, a)
swap
{-# INLINE swapped #-}
instance Swapped Either where
swapped :: forall a b c d.
Iso (Either a b) (Either c d) (Either b a) (Either d c)
swapped = forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso (forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall a b. b -> Either a b
Right forall a b. a -> Either a b
Left) (forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall a b. b -> Either a b
Right forall a b. a -> Either a b
Left)
{-# INLINE swapped #-}