{-# OPTIONS_GHC -Wno-redundant-constraints #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
module Prolens
(
Profunctor (..)
, Optic
, Lens
, Lens'
, Strong (..)
, set
, over
, view
, lens
, (^.)
, (.~)
, (%~)
, fstL
, sndL
, Prism
, Prism'
, Choice (..)
, prism
, prism'
, preview
, _Just
, _Left
, _Right
, Traversal
, Monoidal (..)
, traverseOf
, eachPair
, eachMaybe
, eachList
, Forget (..)
, Fun (..)
) where
import Control.Applicative (Const (..), liftA2)
import Data.Coerce (coerce)
import Data.Monoid (First (..))
class (forall a . Functor (p a)) => Profunctor p where
dimap
:: (in2 -> in1)
-> (out1 -> out2)
-> p in1 out1
-> p in2 out2
instance Profunctor (->) where
dimap :: (in2 -> in1) -> (out1 -> out2) -> (in1 -> out1) -> (in2 -> out2)
dimap :: (in2 -> in1) -> (out1 -> out2) -> (in1 -> out1) -> in2 -> out2
dimap in2 -> in1
in21 out1 -> out2
out12 in1 -> out1
f = out1 -> out2
out12 (out1 -> out2) -> (in2 -> out1) -> in2 -> out2
forall b c a. (b -> c) -> (a -> b) -> a -> c
. in1 -> out1
f (in1 -> out1) -> (in2 -> in1) -> in2 -> out1
forall b c a. (b -> c) -> (a -> b) -> a -> c
. in2 -> in1
in21
{-# INLINE dimap #-}
newtype Fun m a b = Fun
{ Fun m a b -> a -> m b
unFun :: a -> m b
}
instance Functor m => Functor (Fun m x) where
fmap :: (a -> b) -> Fun m x a -> Fun m x b
fmap :: (a -> b) -> Fun m x a -> Fun m x b
fmap a -> b
f (Fun x -> m a
xma) = (x -> m b) -> Fun m x b
forall (m :: * -> *) a b. (a -> m b) -> Fun m a b
Fun ((a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f (m a -> m b) -> (x -> m a) -> x -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. x -> m a
xma)
{-# INLINE fmap #-}
instance Functor m => Profunctor (Fun m) where
dimap :: (in2 -> in1) -> (out1 -> out2) -> Fun m in1 out1 -> Fun m in2 out2
dimap :: (in2 -> in1) -> (out1 -> out2) -> Fun m in1 out1 -> Fun m in2 out2
dimap in2 -> in1
in21 out1 -> out2
out12 (Fun in1 -> m out1
f) = (in2 -> m out2) -> Fun m in2 out2
forall (m :: * -> *) a b. (a -> m b) -> Fun m a b
Fun ((out1 -> out2) -> m out1 -> m out2
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap out1 -> out2
out12 (m out1 -> m out2) -> (in2 -> m out1) -> in2 -> m out2
forall b c a. (b -> c) -> (a -> b) -> a -> c
. in1 -> m out1
f (in1 -> m out1) -> (in2 -> in1) -> in2 -> m out1
forall b c a. (b -> c) -> (a -> b) -> a -> c
. in2 -> in1
in21)
{-# INLINE dimap #-}
class Profunctor p => Strong p where
first :: p a b -> p (a, c) (b, c)
second :: p a b -> p (c, a) (c, b)
instance Strong (->) where
first :: (a -> b) -> (a, c) -> (b, c)
first :: (a -> b) -> (a, c) -> (b, c)
first a -> b
ab (a
a, c
c) = (a -> b
ab a
a, c
c)
{-# INLINE first #-}
second :: (a -> b) -> (c, a) -> (c, b)
second :: (a -> b) -> (c, a) -> (c, b)
second a -> b
ab (c
c, a
a) = (c
c, a -> b
ab a
a)
{-# INLINE second #-}
instance (Functor m) => Strong (Fun m) where
first :: Fun m a b -> Fun m (a, c) (b, c)
first :: Fun m a b -> Fun m (a, c) (b, c)
first (Fun a -> m b
amb) = ((a, c) -> m (b, c)) -> Fun m (a, c) (b, c)
forall (m :: * -> *) a b. (a -> m b) -> Fun m a b
Fun (\(a
a, c
c) -> (b -> (b, c)) -> m b -> m (b, c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (, c
c) (a -> m b
amb a
a))
{-# INLINE first #-}
second :: Fun m a b -> Fun m (c, a) (c, b)
second :: Fun m a b -> Fun m (c, a) (c, b)
second (Fun a -> m b
amb) = ((c, a) -> m (c, b)) -> Fun m (c, a) (c, b)
forall (m :: * -> *) a b. (a -> m b) -> Fun m a b
Fun (\(c
c, a
a) -> (b -> (c, b)) -> m b -> m (c, b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (c
c,) (a -> m b
amb a
a))
{-# INLINE second #-}
class Profunctor p => Choice p where
left :: p a b -> p (Either a c) (Either b c)
right :: p a b -> p (Either c a) (Either c b)
instance Choice (->) where
left :: (a -> b) -> Either a c -> Either b c
left :: (a -> b) -> Either a c -> Either b c
left a -> b
ab = \case
Left a
a -> b -> Either b c
forall a b. a -> Either a b
Left (b -> Either b c) -> b -> Either b c
forall a b. (a -> b) -> a -> b
$ a -> b
ab a
a
Right c
c -> c -> Either b c
forall a b. b -> Either a b
Right c
c
{-# INLINE left #-}
right :: (a -> b) -> Either c a -> Either c b
right :: (a -> b) -> Either c a -> Either c b
right a -> b
ab = \case
Right a
a -> b -> Either c b
forall a b. b -> Either a b
Right (b -> Either c b) -> b -> Either c b
forall a b. (a -> b) -> a -> b
$ a -> b
ab a
a
Left c
c -> c -> Either c b
forall a b. a -> Either a b
Left c
c
{-# INLINE right #-}
instance (Applicative m) => Choice (Fun m) where
left :: Fun m a b -> Fun m (Either a c) (Either b c)
left :: Fun m a b -> Fun m (Either a c) (Either b c)
left (Fun a -> m b
amb)= (Either a c -> m (Either b c)) -> Fun m (Either a c) (Either b c)
forall (m :: * -> *) a b. (a -> m b) -> Fun m a b
Fun ((Either a c -> m (Either b c)) -> Fun m (Either a c) (Either b c))
-> (Either a c -> m (Either b c))
-> Fun m (Either a c) (Either b c)
forall a b. (a -> b) -> a -> b
$ \Either a c
eitherAc -> case Either a c
eitherAc of
Left a
a -> b -> Either b c
forall a b. a -> Either a b
Left (b -> Either b c) -> m b -> m (Either b c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> m b
amb a
a
Right c
c -> Either b c -> m (Either b c)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either b c -> m (Either b c)) -> Either b c -> m (Either b c)
forall a b. (a -> b) -> a -> b
$ c -> Either b c
forall a b. b -> Either a b
Right c
c
{-# INLINE left #-}
right :: Fun m a b -> Fun m (Either c a) (Either c b)
right :: Fun m a b -> Fun m (Either c a) (Either c b)
right (Fun a -> m b
amb)= (Either c a -> m (Either c b)) -> Fun m (Either c a) (Either c b)
forall (m :: * -> *) a b. (a -> m b) -> Fun m a b
Fun ((Either c a -> m (Either c b)) -> Fun m (Either c a) (Either c b))
-> (Either c a -> m (Either c b))
-> Fun m (Either c a) (Either c b)
forall a b. (a -> b) -> a -> b
$ \Either c a
eitherCa -> case Either c a
eitherCa of
Right a
a -> b -> Either c b
forall a b. b -> Either a b
Right (b -> Either c b) -> m b -> m (Either c b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> m b
amb a
a
Left c
c -> Either c b -> m (Either c b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either c b -> m (Either c b)) -> Either c b -> m (Either c b)
forall a b. (a -> b) -> a -> b
$ c -> Either c b
forall a b. a -> Either a b
Left c
c
{-# INLINE right #-}
class Strong p => Monoidal p where
pappend :: p a b -> p c d -> p (a, c) (b, d)
pempty :: p a a
instance Monoidal (->) where
pappend :: (a -> b) -> (c -> d) -> (a, c) -> (b, d)
pappend :: (a -> b) -> (c -> d) -> (a, c) -> (b, d)
pappend a -> b
ab c -> d
cd (a
a, c
c) = (a -> b
ab a
a, c -> d
cd c
c)
{-# INLINE pappend #-}
pempty :: a -> a
pempty :: a -> a
pempty = a -> a
forall a. a -> a
id
{-# INLINE pempty #-}
instance (Applicative m) => Monoidal (Fun m) where
pappend :: Fun m a b -> Fun m c d -> Fun m (a, c) (b, d)
pappend :: Fun m a b -> Fun m c d -> Fun m (a, c) (b, d)
pappend (Fun a -> m b
amb) (Fun c -> m d
cmd) = ((a, c) -> m (b, d)) -> Fun m (a, c) (b, d)
forall (m :: * -> *) a b. (a -> m b) -> Fun m a b
Fun (\(a
a, c
c) -> (b -> d -> (b, d)) -> m b -> m d -> m (b, d)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (,) (a -> m b
amb a
a) (c -> m d
cmd c
c))
{-# INLINE pappend #-}
pempty :: Fun m a a
pempty :: Fun m a a
pempty = (a -> m a) -> Fun m a a
forall (m :: * -> *) a b. (a -> m b) -> Fun m a b
Fun (a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> m a) -> (a -> a) -> a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
forall a. a -> a
id)
{-# INLINE pempty #-}
type Optic p source target a b = p a b -> p source target
type Lens source target a b = forall p . Strong p => Optic p source target a b
type Lens' source a = Lens source source a a
set :: (p ~ (->))
=> Optic p source target a b
-> b
-> source
-> target
set :: Optic p source target a b -> b -> source -> target
set Optic p source target a b
abst = Optic p source target a b
abst Optic p source target a b -> (b -> p a b) -> b -> p source target
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> p a b
forall a b. a -> b -> a
const
{-# INLINE set #-}
over
:: (p ~ (->))
=> Optic p source target a b
-> (a -> b)
-> source
-> target
over :: Optic p source target a b -> (a -> b) -> source -> target
over = Optic p source target a b -> (a -> b) -> source -> target
forall a. a -> a
id
{-# INLINE over #-}
view
:: (p ~ Fun (Const a))
=> Optic p source target a b
-> source
-> a
view :: Optic p source target a b -> source -> a
view Optic p source target a b
opt = p source target -> source -> a
coerce (Optic p source target a b
opt ((a -> Const a b) -> Fun (Const a) a b
forall (m :: * -> *) a b. (a -> m b) -> Fun m a b
Fun a -> Const a b
forall k a (b :: k). a -> Const a b
Const))
{-# INLINE view #-}
lens
:: (source -> a)
-> (source -> b -> target)
-> Lens source target a b
lens :: (source -> a) -> (source -> b -> target) -> Lens source target a b
lens source -> a
getter source -> b -> target
setter = (source -> (source, a))
-> ((source, b) -> target)
-> p (source, a) (source, b)
-> p source target
forall (p :: * -> * -> *) in2 in1 out1 out2.
Profunctor p =>
(in2 -> in1) -> (out1 -> out2) -> p in1 out1 -> p in2 out2
dimap (\source
s -> (source
s, source -> a
getter source
s)) ((source -> b -> target) -> (source, b) -> target
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry source -> b -> target
setter) (p (source, a) (source, b) -> p source target)
-> (p a b -> p (source, a) (source, b)) -> p a b -> p source target
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p a b -> p (source, a) (source, b)
forall (p :: * -> * -> *) a b c.
Strong p =>
p a b -> p (c, a) (c, b)
second
{-# INLINE lens #-}
infixl 8 ^.
(^.) :: source -> Lens' source a -> a
source
s ^. :: source -> Lens' source a -> a
^. Lens' source a
l = Optic (Fun (Const a)) source source a a -> source -> a
forall (p :: * -> * -> *) a source target b.
(p ~ Fun (Const a)) =>
Optic p source target a b -> source -> a
view Optic (Fun (Const a)) source source a a
Lens' source a
l source
s
{-# INLINE (^.) #-}
infixr 4 .~
(.~) :: Lens' source a -> a -> source -> source
.~ :: Lens' source a -> a -> source -> source
(.~) Lens' source a
l = Optic (->) source source a a -> a -> source -> source
forall (p :: * -> * -> *) source target a b.
(p ~ (->)) =>
Optic p source target a b -> b -> source -> target
set Optic (->) source source a a
Lens' source a
l
{-# INLINE (.~) #-}
infixr 4 %~
(%~) :: Lens' source a -> (a -> a) -> source -> source
%~ :: Lens' source a -> (a -> a) -> source -> source
(%~) Lens' source a
l = ((a -> a) -> source -> source) -> (a -> a) -> source -> source
forall (p :: * -> * -> *) source target a b.
(p ~ (->)) =>
Optic p source target a b -> (a -> b) -> source -> target
over (a -> a) -> source -> source
Lens' source a
l
{-# INLINE (%~) #-}
fstL :: Lens (a, c) (b, c) a b
fstL :: Optic p (a, c) (b, c) a b
fstL = ((a, c) -> a) -> ((a, c) -> b -> (b, c)) -> Lens (a, c) (b, c) a b
forall source a b target.
(source -> a) -> (source -> b -> target) -> Lens source target a b
lens (a, c) -> a
forall a b. (a, b) -> a
fst (((a, c) -> b -> (b, c)) -> Lens (a, c) (b, c) a b)
-> ((a, c) -> b -> (b, c)) -> Lens (a, c) (b, c) a b
forall a b. (a -> b) -> a -> b
$ \(a
_, c
b) b
new -> (b
new, c
b)
{-# INLINE fstL #-}
sndL :: Lens (x, a) (x, b) a b
sndL :: Optic p (x, a) (x, b) a b
sndL = ((x, a) -> a) -> ((x, a) -> b -> (x, b)) -> Lens (x, a) (x, b) a b
forall source a b target.
(source -> a) -> (source -> b -> target) -> Lens source target a b
lens (x, a) -> a
forall a b. (a, b) -> b
snd (((x, a) -> b -> (x, b)) -> Lens (x, a) (x, b) a b)
-> ((x, a) -> b -> (x, b)) -> Lens (x, a) (x, b) a b
forall a b. (a -> b) -> a -> b
$ \(x
a, a
_) b
new -> (x
a, b
new)
{-# INLINE sndL #-}
type Prism source target a b = forall p . Choice p => Optic p source target a b
type Prism' source a = Prism source source a a
newtype Forget r a b = Forget
{ Forget r a b -> a -> Maybe r
unForget :: a -> Maybe r
}
instance Functor (Forget r x) where
fmap :: (a -> b) -> Forget r x a -> Forget r x b
fmap :: (a -> b) -> Forget r x a -> Forget r x b
fmap a -> b
_ = Forget r x a -> Forget r x b
coerce
{-# INLINE fmap #-}
instance Profunctor (Forget r) where
dimap :: (a -> b) -> (c -> d) -> Forget r b c -> Forget r a d
dimap :: (a -> b) -> (c -> d) -> Forget r b c -> Forget r a d
dimap a -> b
ab c -> d
_cd (Forget b -> Maybe r
br) = (a -> Maybe r) -> Forget r a d
forall r a b. (a -> Maybe r) -> Forget r a b
Forget (b -> Maybe r
br (b -> Maybe r) -> (a -> b) -> a -> Maybe r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
ab)
{-# INLINE dimap #-}
instance Strong (Forget r) where
first :: Forget r a b -> Forget r (a, c) (b, c)
first :: Forget r a b -> Forget r (a, c) (b, c)
first (Forget a -> Maybe r
ar) = ((a, c) -> Maybe r) -> Forget r (a, c) (b, c)
forall r a b. (a -> Maybe r) -> Forget r a b
Forget (a -> Maybe r
ar (a -> Maybe r) -> ((a, c) -> a) -> (a, c) -> Maybe r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, c) -> a
forall a b. (a, b) -> a
fst)
{-# INLINE first #-}
second :: Forget r a b -> Forget r (c, a) (c, b)
second :: Forget r a b -> Forget r (c, a) (c, b)
second (Forget a -> Maybe r
ar) = ((c, a) -> Maybe r) -> Forget r (c, a) (c, b)
forall r a b. (a -> Maybe r) -> Forget r a b
Forget (a -> Maybe r
ar (a -> Maybe r) -> ((c, a) -> a) -> (c, a) -> Maybe r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (c, a) -> a
forall a b. (a, b) -> b
snd)
{-# INLINE second #-}
instance Choice (Forget r) where
left :: Forget r a b -> Forget r (Either a c) (Either b c)
left :: Forget r a b -> Forget r (Either a c) (Either b c)
left (Forget a -> Maybe r
ar) = (Either a c -> Maybe r) -> Forget r (Either a c) (Either b c)
forall r a b. (a -> Maybe r) -> Forget r a b
Forget ((a -> Maybe r) -> (c -> Maybe r) -> Either a c -> Maybe r
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either a -> Maybe r
ar (Maybe r -> c -> Maybe r
forall a b. a -> b -> a
const Maybe r
forall a. Maybe a
Nothing))
{-# INLINE left #-}
right :: Forget r a b -> Forget r (Either c a) (Either c b)
right :: Forget r a b -> Forget r (Either c a) (Either c b)
right (Forget a -> Maybe r
ar) = (Either c a -> Maybe r) -> Forget r (Either c a) (Either c b)
forall r a b. (a -> Maybe r) -> Forget r a b
Forget ((c -> Maybe r) -> (a -> Maybe r) -> Either c a -> Maybe r
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Maybe r -> c -> Maybe r
forall a b. a -> b -> a
const Maybe r
forall a. Maybe a
Nothing) a -> Maybe r
ar)
{-# INLINE right #-}
instance Monoidal (Forget r) where
pappend :: Forget r a b -> Forget r c d -> Forget r (a, c) (b, d)
pappend :: Forget r a b -> Forget r c d -> Forget r (a, c) (b, d)
pappend (Forget a -> Maybe r
ar) (Forget c -> Maybe r
cr) = ((a, c) -> Maybe r) -> Forget r (a, c) (b, d)
forall r a b. (a -> Maybe r) -> Forget r a b
Forget
(\(a
a, c
c) -> First r -> Maybe r
forall a. First a -> Maybe a
getFirst (First r -> Maybe r) -> First r -> Maybe r
forall a b. (a -> b) -> a -> b
$ Maybe r -> First r
forall a. Maybe a -> First a
First (a -> Maybe r
ar a
a) First r -> First r -> First r
forall a. Semigroup a => a -> a -> a
<> Maybe r -> First r
forall a. Maybe a -> First a
First (c -> Maybe r
cr c
c))
{-# INLINE pappend #-}
pempty :: Forget r a a
pempty :: Forget r a a
pempty = (a -> Maybe r) -> Forget r a a
forall r a b. (a -> Maybe r) -> Forget r a b
Forget (Maybe r -> a -> Maybe r
forall a b. a -> b -> a
const Maybe r
forall a. Maybe a
Nothing)
{-# INLINE pempty #-}
preview
:: forall a source p
. (p ~ Forget a)
=> Optic p source source a a
-> source
-> Maybe a
preview :: Optic p source source a a -> source -> Maybe a
preview Optic p source source a a
paapss = p source source -> source -> Maybe a
coerce (Optic p source source a a
paapss p a a
Forget a a a
wrap)
where
wrap :: Forget a a a
wrap :: Forget a a a
wrap = (a -> Maybe a) -> Forget a a a
coerce @(a -> Maybe a) @(Forget a a a) a -> Maybe a
forall a. a -> Maybe a
Just
{-# INLINE wrap #-}
{-# INLINE preview #-}
prism
:: (b -> target)
-> (source -> Either target a)
-> Prism source target a b
prism :: (b -> target)
-> (source -> Either target a) -> Prism source target a b
prism b -> target
ctor source -> Either target a
match = (source -> Either target a)
-> (Either target b -> target)
-> p (Either target a) (Either target b)
-> p source target
forall (p :: * -> * -> *) in2 in1 out1 out2.
Profunctor p =>
(in2 -> in1) -> (out1 -> out2) -> p in1 out1 -> p in2 out2
dimap source -> Either target a
match ((target -> target) -> (b -> target) -> Either target b -> target
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either target -> target
forall a. a -> a
id b -> target
ctor) (p (Either target a) (Either target b) -> p source target)
-> (p a b -> p (Either target a) (Either target b))
-> p a b
-> p source target
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p a b -> p (Either target a) (Either target b)
forall (p :: * -> * -> *) a b c.
Choice p =>
p a b -> p (Either c a) (Either c b)
right
{-# INLINE prism #-}
prism'
:: (a -> source)
-> (source -> Maybe a)
-> Prism' source a
prism' :: (a -> source) -> (source -> Maybe a) -> Prism' source a
prism' a -> source
ctor source -> Maybe a
match = (a -> source) -> (source -> Either source a) -> Prism' source a
forall b target source a.
(b -> target)
-> (source -> Either target a) -> Prism source target a b
prism a -> source
ctor (\source
s -> Either source a
-> (a -> Either source a) -> Maybe a -> Either source a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (source -> Either source a
forall a b. a -> Either a b
Left source
s) a -> Either source a
forall a b. b -> Either a b
Right (Maybe a -> Either source a) -> Maybe a -> Either source a
forall a b. (a -> b) -> a -> b
$ source -> Maybe a
match source
s)
{-# INLINE prism' #-}
_Just :: Prism (Maybe a) (Maybe b) a b
_Just :: Optic p (Maybe a) (Maybe b) a b
_Just = (b -> Maybe b)
-> (Maybe a -> Either (Maybe b) a) -> Prism (Maybe a) (Maybe b) a b
forall b target source a.
(b -> target)
-> (source -> Either target a) -> Prism source target a b
prism b -> Maybe b
forall a. a -> Maybe a
Just ((Maybe a -> Either (Maybe b) a) -> Prism (Maybe a) (Maybe b) a b)
-> (Maybe a -> Either (Maybe b) a) -> Prism (Maybe a) (Maybe b) a b
forall a b. (a -> b) -> a -> b
$ \case
Just a
a -> a -> Either (Maybe b) a
forall a b. b -> Either a b
Right a
a
Maybe a
Nothing -> Maybe b -> Either (Maybe b) a
forall a b. a -> Either a b
Left Maybe b
forall a. Maybe a
Nothing
{-# INLINE _Just #-}
_Left :: Prism (Either a x) (Either b x) a b
_Left :: Optic p (Either a x) (Either b x) a b
_Left = (b -> Either b x)
-> (Either a x -> Either (Either b x) a)
-> Prism (Either a x) (Either b x) a b
forall b target source a.
(b -> target)
-> (source -> Either target a) -> Prism source target a b
prism b -> Either b x
forall a b. a -> Either a b
Left ((Either a x -> Either (Either b x) a)
-> Prism (Either a x) (Either b x) a b)
-> (Either a x -> Either (Either b x) a)
-> Prism (Either a x) (Either b x) a b
forall a b. (a -> b) -> a -> b
$ \case
Left a
l -> a -> Either (Either b x) a
forall a b. b -> Either a b
Right a
l
Right x
r -> Either b x -> Either (Either b x) a
forall a b. a -> Either a b
Left (Either b x -> Either (Either b x) a)
-> Either b x -> Either (Either b x) a
forall a b. (a -> b) -> a -> b
$ x -> Either b x
forall a b. b -> Either a b
Right x
r
{-# INLINE _Left #-}
_Right :: Prism (Either x a) (Either x b) a b
_Right :: Optic p (Either x a) (Either x b) a b
_Right = (b -> Either x b)
-> (Either x a -> Either (Either x b) a)
-> Prism (Either x a) (Either x b) a b
forall b target source a.
(b -> target)
-> (source -> Either target a) -> Prism source target a b
prism b -> Either x b
forall a b. b -> Either a b
Right ((Either x a -> Either (Either x b) a)
-> Prism (Either x a) (Either x b) a b)
-> (Either x a -> Either (Either x b) a)
-> Prism (Either x a) (Either x b) a b
forall a b. (a -> b) -> a -> b
$ \case
Right a
a -> a -> Either (Either x b) a
forall a b. b -> Either a b
Right a
a
Left x
x -> Either x b -> Either (Either x b) a
forall a b. a -> Either a b
Left (Either x b -> Either (Either x b) a)
-> Either x b -> Either (Either x b) a
forall a b. (a -> b) -> a -> b
$ x -> Either x b
forall a b. a -> Either a b
Left x
x
{-# INLINE _Right #-}
type Traversal source target a b
= forall p
. (Choice p, Monoidal p)
=> Optic p source target a b
traverseOf
:: (Applicative f, p ~ Fun f)
=> Optic p source target a b
-> (a -> f b)
-> source
-> f target
traverseOf :: Optic p source target a b -> (a -> f b) -> source -> f target
traverseOf Optic p source target a b
pabPst a -> f b
aFb = Fun f source target -> source -> f target
forall (m :: * -> *) a b. Fun m a b -> a -> m b
unFun (Optic p source target a b
pabPst ((a -> f b) -> Fun f a b
forall (m :: * -> *) a b. (a -> m b) -> Fun m a b
Fun a -> f b
aFb))
eachPair :: Traversal (a, a) (b, b) a b
eachPair :: Optic p (a, a) (b, b) a b
eachPair p a b
pab = p a b -> Optic p (a, a) (b, b) a b
forall (p :: * -> * -> *) a b c d.
Monoidal p =>
p a b -> p c d -> p (a, c) (b, d)
pappend p a b
pab p a b
pab
eachMaybe :: Traversal (Maybe a) (Maybe b) a b
eachMaybe :: Optic p (Maybe a) (Maybe b) a b
eachMaybe p a b
pab = (Maybe a -> Either a ())
-> (Either b () -> Maybe b)
-> p (Either a ()) (Either b ())
-> p (Maybe a) (Maybe b)
forall (p :: * -> * -> *) in2 in1 out1 out2.
Profunctor p =>
(in2 -> in1) -> (out1 -> out2) -> p in1 out1 -> p in2 out2
dimap Maybe a -> Either a ()
forall a. Maybe a -> Either a ()
maybeToEither Either b () -> Maybe b
forall a. Either a () -> Maybe a
eitherToMaybe (p a b -> p (Either a ()) (Either b ())
forall (p :: * -> * -> *) a b c.
Choice p =>
p a b -> p (Either a c) (Either b c)
left p a b
pab)
where
maybeToEither :: Maybe a -> Either a ()
maybeToEither :: Maybe a -> Either a ()
maybeToEither = \case
Just a
a -> a -> Either a ()
forall a b. a -> Either a b
Left a
a
Maybe a
Nothing -> () -> Either a ()
forall a b. b -> Either a b
Right ()
eitherToMaybe :: Either a () -> Maybe a
eitherToMaybe :: Either a () -> Maybe a
eitherToMaybe = \case
Left a
a -> a -> Maybe a
forall a. a -> Maybe a
Just a
a
Right () -> Maybe a
forall a. Maybe a
Nothing
eachList :: Traversal [a] [b] a b
eachList :: Optic p [a] [b] a b
eachList p a b
pab = ([a] -> Either (a, [a]) ())
-> (Either (b, [b]) () -> [b])
-> p (Either (a, [a]) ()) (Either (b, [b]) ())
-> p [a] [b]
forall (p :: * -> * -> *) in2 in1 out1 out2.
Profunctor p =>
(in2 -> in1) -> (out1 -> out2) -> p in1 out1 -> p in2 out2
dimap [a] -> Either (a, [a]) ()
forall a. [a] -> Either (a, [a]) ()
listToEither Either (b, [b]) () -> [b]
forall a. Either (a, [a]) () -> [a]
eitherToList (p (Either (a, [a]) ()) (Either (b, [b]) ()) -> p [a] [b])
-> p (Either (a, [a]) ()) (Either (b, [b]) ()) -> p [a] [b]
forall a b. (a -> b) -> a -> b
$ p (a, [a]) (b, [b]) -> p (Either (a, [a]) ()) (Either (b, [b]) ())
forall (p :: * -> * -> *) a b c.
Choice p =>
p a b -> p (Either a c) (Either b c)
left (p (a, [a]) (b, [b])
-> p (Either (a, [a]) ()) (Either (b, [b]) ()))
-> p (a, [a]) (b, [b])
-> p (Either (a, [a]) ()) (Either (b, [b]) ())
forall a b. (a -> b) -> a -> b
$ p a b -> p [a] [b] -> p (a, [a]) (b, [b])
forall (p :: * -> * -> *) a b c d.
Monoidal p =>
p a b -> p c d -> p (a, c) (b, d)
pappend p a b
pab (Optic p [a] [b] a b
forall a b. Traversal [a] [b] a b
eachList p a b
pab)
where
listToEither :: [a] -> Either (a, [a]) ()
listToEither :: [a] -> Either (a, [a]) ()
listToEither = \case
[] -> () -> Either (a, [a]) ()
forall a b. b -> Either a b
Right ()
a
x:[a]
xs -> (a, [a]) -> Either (a, [a]) ()
forall a b. a -> Either a b
Left (a
x, [a]
xs)
eitherToList :: Either (a, [a]) () -> [a]
eitherToList :: Either (a, [a]) () -> [a]
eitherToList = \case
Right () -> []
Left (a
x, [a]
xs) -> a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
xs