{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE FlexibleInstances #-}

module Data.Alignment(
-- * Types
  This(..)
-- * Type-classes
, Semialign(..)
, Align(..)
-- * Optics
, these
, those
, allThese
, allThese1
, allThese2
, allThose
, allThoseA
, allThoseA'
, allThoseAOr
, allThoseB
, allThoseB'
, allThoseBOr
, allTheseThoseA
, allTheseThoseB
) where

import Control.Applicative
    ( Applicative(liftA2, pure, (<*>)), (<$>), ZipList(ZipList) )
import Control.Category ( Category((.)) )
import Control.Lens
    ( Identity(Identity),
      _Just,
      _Left,
      _Right,
      over,
      Field1(_1),
      Field2(_2),
      Lens,
      Lens',
      Traversal' )
import Data.Bifoldable ( Bifoldable(bifoldMap) )
import Data.Bifunctor ( Bifunctor(bimap) )
import Data.Bifunctor.Swap ( Swap(..) )
import Data.Bitraversable ( Bitraversable(..) )
import Data.List.NonEmpty ( NonEmpty(..) )
import Data.Bool ( (&&) )
import Data.Either ( Either(..) )
import Data.Eq ( Eq((==)) )
import Data.Foldable ( Foldable(foldMap) )
import Data.Functor ( Functor(fmap), (<$) )
import Data.Functor.Apply ( Apply((<.>), liftF2) )
import Data.Functor.Classes
    ( compare1,
      eq1,
      showsPrec1,
      showsUnaryWith,
      Eq1(..),
      Ord1(..),
      Show1(..) )
import qualified Data.List.NonEmpty as NonEmpty(cons, toList)
import Data.Maybe ( Maybe(..) )
import Data.Monoid ( (<>), Monoid(mempty) )
import Data.Ord ( Ord(compare) )
import Data.Semigroup ( Semigroup )
import Data.Traversable ( Traversable(traverse) )
import GHC.Show ( Show(showsPrec) )

-- $setup
-- >>> import Prelude

data This f a b =
  This
    (f (a, b))
    (Maybe (Either (NonEmpty a) (NonEmpty b)))

instance (Eq1 f, Eq a, Eq b) => Eq (This f a b) where
  This f (a, b)
t1 Maybe (Either (NonEmpty a) (NonEmpty b))
r1 == :: This f a b -> This f a b -> Bool
== This f (a, b)
t2 Maybe (Either (NonEmpty a) (NonEmpty b))
r2 =
    f (a, b)
t1 f (a, b) -> f (a, b) -> Bool
forall (f :: * -> *) a. (Eq1 f, Eq a) => f a -> f a -> Bool
`eq1` f (a, b)
t2 Bool -> Bool -> Bool
&& Maybe (Either (NonEmpty a) (NonEmpty b))
r1 Maybe (Either (NonEmpty a) (NonEmpty b))
-> Maybe (Either (NonEmpty a) (NonEmpty b)) -> Bool
forall a. Eq a => a -> a -> Bool
== Maybe (Either (NonEmpty a) (NonEmpty b))
r2

instance (Eq1 f, Eq a) => Eq1 (This f a) where
  liftEq :: (a -> b -> Bool) -> This f a a -> This f a b -> Bool
liftEq a -> b -> Bool
f (This f (a, a)
t1 Maybe (Either (NonEmpty a) (NonEmpty a))
r1) (This f (a, b)
t2 Maybe (Either (NonEmpty a) (NonEmpty b))
r2) =
    ((a, a) -> (a, b) -> Bool) -> f (a, a) -> f (a, b) -> Bool
forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> f a -> f b -> Bool
liftEq ((a -> b -> Bool) -> (a, a) -> (a, b) -> Bool
forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> f a -> f b -> Bool
liftEq a -> b -> Bool
f) f (a, a)
t1 f (a, b)
t2 Bool -> Bool -> Bool
&& (Either (NonEmpty a) (NonEmpty a)
 -> Either (NonEmpty a) (NonEmpty b) -> Bool)
-> Maybe (Either (NonEmpty a) (NonEmpty a))
-> Maybe (Either (NonEmpty a) (NonEmpty b))
-> Bool
forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> f a -> f b -> Bool
liftEq ((NonEmpty a -> NonEmpty b -> Bool)
-> Either (NonEmpty a) (NonEmpty a)
-> Either (NonEmpty a) (NonEmpty b)
-> Bool
forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> f a -> f b -> Bool
liftEq ((a -> b -> Bool) -> NonEmpty a -> NonEmpty b -> Bool
forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> f a -> f b -> Bool
liftEq a -> b -> Bool
f)) Maybe (Either (NonEmpty a) (NonEmpty a))
r1 Maybe (Either (NonEmpty a) (NonEmpty b))
r2

instance (Ord1 f, Ord a, Ord b) => Ord (This f a b) where
  This f (a, b)
t1 Maybe (Either (NonEmpty a) (NonEmpty b))
r1 compare :: This f a b -> This f a b -> Ordering
`compare` This f (a, b)
t2 Maybe (Either (NonEmpty a) (NonEmpty b))
r2 =
    f (a, b)
t1 f (a, b) -> f (a, b) -> Ordering
forall (f :: * -> *) a. (Ord1 f, Ord a) => f a -> f a -> Ordering
`compare1` f (a, b)
t2 Ordering -> Ordering -> Ordering
forall a. Semigroup a => a -> a -> a
<> Maybe (Either (NonEmpty a) (NonEmpty b))
r1 Maybe (Either (NonEmpty a) (NonEmpty b))
-> Maybe (Either (NonEmpty a) (NonEmpty b)) -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` Maybe (Either (NonEmpty a) (NonEmpty b))
r2

instance (Ord1 f, Ord a) => Ord1 (This f a) where
  liftCompare :: (a -> b -> Ordering) -> This f a a -> This f a b -> Ordering
liftCompare a -> b -> Ordering
f (This f (a, a)
t1 Maybe (Either (NonEmpty a) (NonEmpty a))
r1) (This f (a, b)
t2 Maybe (Either (NonEmpty a) (NonEmpty b))
r2) =
    ((a, a) -> (a, b) -> Ordering) -> f (a, a) -> f (a, b) -> Ordering
forall (f :: * -> *) a b.
Ord1 f =>
(a -> b -> Ordering) -> f a -> f b -> Ordering
liftCompare ((a -> b -> Ordering) -> (a, a) -> (a, b) -> Ordering
forall (f :: * -> *) a b.
Ord1 f =>
(a -> b -> Ordering) -> f a -> f b -> Ordering
liftCompare a -> b -> Ordering
f) f (a, a)
t1 f (a, b)
t2 Ordering -> Ordering -> Ordering
forall a. Semigroup a => a -> a -> a
<> (Either (NonEmpty a) (NonEmpty a)
 -> Either (NonEmpty a) (NonEmpty b) -> Ordering)
-> Maybe (Either (NonEmpty a) (NonEmpty a))
-> Maybe (Either (NonEmpty a) (NonEmpty b))
-> Ordering
forall (f :: * -> *) a b.
Ord1 f =>
(a -> b -> Ordering) -> f a -> f b -> Ordering
liftCompare ((NonEmpty a -> NonEmpty b -> Ordering)
-> Either (NonEmpty a) (NonEmpty a)
-> Either (NonEmpty a) (NonEmpty b)
-> Ordering
forall (f :: * -> *) a b.
Ord1 f =>
(a -> b -> Ordering) -> f a -> f b -> Ordering
liftCompare ((a -> b -> Ordering) -> NonEmpty a -> NonEmpty b -> Ordering
forall (f :: * -> *) a b.
Ord1 f =>
(a -> b -> Ordering) -> f a -> f b -> Ordering
liftCompare a -> b -> Ordering
f)) Maybe (Either (NonEmpty a) (NonEmpty a))
r1 Maybe (Either (NonEmpty a) (NonEmpty b))
r2

instance (Show1 f, Show a, Show b) => Show (This f a b) where
  showsPrec :: Int -> This f a b -> ShowS
showsPrec Int
d (This f (a, b)
t Maybe (Either (NonEmpty a) (NonEmpty b))
r) =
    (Int -> f (a, b) -> ShowS) -> String -> Int -> f (a, b) -> ShowS
forall a. (Int -> a -> ShowS) -> String -> Int -> a -> ShowS
showsUnaryWith Int -> f (a, b) -> ShowS
forall (f :: * -> *) a. (Show1 f, Show a) => Int -> f a -> ShowS
showsPrec1 String
"This" Int
d f (a, b)
t ShowS -> ShowS -> ShowS
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (String
" " String -> ShowS
forall a. Semigroup a => a -> a -> a
<>) ShowS -> ShowS -> ShowS
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Int -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> ShowS
forall (f :: * -> *) a. (Show1 f, Show a) => Int -> f a -> ShowS
showsPrec1 Int
d Maybe (Either (NonEmpty a) (NonEmpty b))
r

instance (Show1 f, Show a) => Show1 (This f a) where
  liftShowsPrec :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> This f a a -> ShowS
liftShowsPrec Int -> a -> ShowS
sp [a] -> ShowS
sl Int
d (This f (a, a)
t Maybe (Either (NonEmpty a) (NonEmpty a))
r) =
    let showsPrecFt :: Int -> f (a, a) -> ShowS
showsPrecFt = (Int -> (a, a) -> ShowS)
-> ([(a, a)] -> ShowS) -> Int -> f (a, a) -> ShowS
forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS
liftShowsPrec ((Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> (a, a) -> ShowS
forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS
liftShowsPrec Int -> a -> ShowS
sp [a] -> ShowS
sl) ((Int -> a -> ShowS) -> ([a] -> ShowS) -> [(a, a)] -> ShowS
forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> ShowS) -> ([a] -> ShowS) -> [f a] -> ShowS
liftShowList Int -> a -> ShowS
sp [a] -> ShowS
sl)
        showsPrecFr :: Int -> Maybe (Either (NonEmpty a) (NonEmpty a)) -> ShowS
showsPrecFr = (Int -> Either (NonEmpty a) (NonEmpty a) -> ShowS)
-> ([Either (NonEmpty a) (NonEmpty a)] -> ShowS)
-> Int
-> Maybe (Either (NonEmpty a) (NonEmpty a))
-> ShowS
forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS
liftShowsPrec ((Int -> NonEmpty a -> ShowS)
-> ([NonEmpty a] -> ShowS)
-> Int
-> Either (NonEmpty a) (NonEmpty a)
-> ShowS
forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS
liftShowsPrec ((Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> NonEmpty a -> ShowS
forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS
liftShowsPrec Int -> a -> ShowS
sp [a] -> ShowS
sl) ((Int -> a -> ShowS) -> ([a] -> ShowS) -> [NonEmpty a] -> ShowS
forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> ShowS) -> ([a] -> ShowS) -> [f a] -> ShowS
liftShowList Int -> a -> ShowS
sp [a] -> ShowS
sl)) ((Int -> NonEmpty a -> ShowS)
-> ([NonEmpty a] -> ShowS)
-> [Either (NonEmpty a) (NonEmpty a)]
-> ShowS
forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> ShowS) -> ([a] -> ShowS) -> [f a] -> ShowS
liftShowList ((Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> NonEmpty a -> ShowS
forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS
liftShowsPrec Int -> a -> ShowS
sp [a] -> ShowS
sl) ((Int -> a -> ShowS) -> ([a] -> ShowS) -> [NonEmpty a] -> ShowS
forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> ShowS) -> ([a] -> ShowS) -> [f a] -> ShowS
liftShowList Int -> a -> ShowS
sp [a] -> ShowS
sl))
    in  (Int -> f (a, a) -> ShowS) -> String -> Int -> f (a, a) -> ShowS
forall a. (Int -> a -> ShowS) -> String -> Int -> a -> ShowS
showsUnaryWith Int -> f (a, a) -> ShowS
showsPrecFt String
"This" Int
d f (a, a)
t ShowS -> ShowS -> ShowS
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (String
" " String -> ShowS
forall a. Semigroup a => a -> a -> a
<>) ShowS -> ShowS -> ShowS
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Int -> Maybe (Either (NonEmpty a) (NonEmpty a)) -> ShowS
showsPrecFr Int
d Maybe (Either (NonEmpty a) (NonEmpty a))
r

instance Functor f => Bifunctor (This f) where
  bimap :: (a -> b) -> (c -> d) -> This f a c -> This f b d
bimap a -> b
f c -> d
g (This f (a, c)
t Maybe (Either (NonEmpty a) (NonEmpty c))
r) =
    f (b, d) -> Maybe (Either (NonEmpty b) (NonEmpty d)) -> This f b d
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This (((a, c) -> (b, d)) -> f (a, c) -> f (b, d)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a -> b) -> (c -> d) -> (a, c) -> (b, d)
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap a -> b
f c -> d
g) f (a, c)
t) ((Either (NonEmpty a) (NonEmpty c)
 -> Either (NonEmpty b) (NonEmpty d))
-> Maybe (Either (NonEmpty a) (NonEmpty c))
-> Maybe (Either (NonEmpty b) (NonEmpty d))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((NonEmpty a -> NonEmpty b)
-> (NonEmpty c -> NonEmpty d)
-> Either (NonEmpty a) (NonEmpty c)
-> Either (NonEmpty b) (NonEmpty d)
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap ((a -> b) -> NonEmpty a -> NonEmpty b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f) ((c -> d) -> NonEmpty c -> NonEmpty d
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap c -> d
g)) Maybe (Either (NonEmpty a) (NonEmpty c))
r)

instance Foldable f => Bifoldable (This f) where
  bifoldMap :: (a -> m) -> (b -> m) -> This f a b -> m
bifoldMap a -> m
f b -> m
g (This f (a, b)
t Maybe (Either (NonEmpty a) (NonEmpty b))
r) =
    ((a, b) -> m) -> f (a, b) -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap ((a -> m) -> (b -> m) -> (a, b) -> m
forall (p :: * -> * -> *) m a b.
(Bifoldable p, Monoid m) =>
(a -> m) -> (b -> m) -> p a b -> m
bifoldMap a -> m
f b -> m
g) f (a, b)
t m -> m -> m
forall a. Semigroup a => a -> a -> a
<> (Either (NonEmpty a) (NonEmpty b) -> m)
-> Maybe (Either (NonEmpty a) (NonEmpty b)) -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap ((NonEmpty a -> m)
-> (NonEmpty b -> m) -> Either (NonEmpty a) (NonEmpty b) -> m
forall (p :: * -> * -> *) m a b.
(Bifoldable p, Monoid m) =>
(a -> m) -> (b -> m) -> p a b -> m
bifoldMap ((a -> m) -> NonEmpty a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap a -> m
f) ((b -> m) -> NonEmpty b -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap b -> m
g)) Maybe (Either (NonEmpty a) (NonEmpty b))
r

instance Traversable f => Bitraversable (This f) where
  bitraverse :: (a -> f c) -> (b -> f d) -> This f a b -> f (This f c d)
bitraverse a -> f c
f b -> f d
g (This f (a, b)
t Maybe (Either (NonEmpty a) (NonEmpty b))
r) =
    f (c, d) -> Maybe (Either (NonEmpty c) (NonEmpty d)) -> This f c d
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This (f (c, d)
 -> Maybe (Either (NonEmpty c) (NonEmpty d)) -> This f c d)
-> f (f (c, d))
-> f (Maybe (Either (NonEmpty c) (NonEmpty d)) -> This f c d)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((a, b) -> f (c, d)) -> f (a, b) -> f (f (c, d))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse ((a -> f c) -> (b -> f d) -> (a, b) -> f (c, d)
forall (t :: * -> * -> *) (f :: * -> *) a c b d.
(Bitraversable t, Applicative f) =>
(a -> f c) -> (b -> f d) -> t a b -> f (t c d)
bitraverse a -> f c
f b -> f d
g) f (a, b)
t f (Maybe (Either (NonEmpty c) (NonEmpty d)) -> This f c d)
-> f (Maybe (Either (NonEmpty c) (NonEmpty d))) -> f (This f c d)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Either (NonEmpty a) (NonEmpty b)
 -> f (Either (NonEmpty c) (NonEmpty d)))
-> Maybe (Either (NonEmpty a) (NonEmpty b))
-> f (Maybe (Either (NonEmpty c) (NonEmpty d)))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse ((NonEmpty a -> f (NonEmpty c))
-> (NonEmpty b -> f (NonEmpty d))
-> Either (NonEmpty a) (NonEmpty b)
-> f (Either (NonEmpty c) (NonEmpty d))
forall (t :: * -> * -> *) (f :: * -> *) a c b d.
(Bitraversable t, Applicative f) =>
(a -> f c) -> (b -> f d) -> t a b -> f (t c d)
bitraverse ((a -> f c) -> NonEmpty a -> f (NonEmpty c)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f c
f) ((b -> f d) -> NonEmpty b -> f (NonEmpty d)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse b -> f d
g)) Maybe (Either (NonEmpty a) (NonEmpty b))
r

instance Functor f => Functor (This f a) where
  fmap :: (a -> b) -> This f a a -> This f a b
fmap =
    (a -> a) -> (a -> b) -> This f a a -> This f a b
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap (\a
x -> a
x)

-- |
--
-- >>> This [("a", id), ("c", id)] Nothing <.> This [("A", "B"), ("C", "D")] Nothing
-- This [("aA","B"),("aC","D"),("cA","B"),("cC","D")] Nothing
-- >>> This [("a", id), ("c", id)] Nothing <.> This [("A", "B"), ("C", "D")] (Just (Left ("x":|[])))
-- This [("aA","B"),("aC","D"),("cA","B"),("cC","D")] Nothing
-- >>> This [("abc", reverse), ("cde", reverse)] Nothing <.> This [("ABC", "DEF"), ("GHI", "JKL")] Nothing
-- This [("abcABC","FED"),("abcGHI","LKJ"),("cdeABC","FED"),("cdeGHI","LKJ")] Nothing
-- >>> This [("abc", reverse), ("cde", reverse)] Nothing <.> This [("ABC", "DEF"), ("GHI", "JKL")] (Just (Left ("xyz":|[])))
-- This [("abcABC","FED"),("abcGHI","LKJ"),("cdeABC","FED"),("cdeGHI","LKJ")] Nothing
-- >>> This [("abc", reverse), ("cde", reverse)] Nothing <.> This [("ABC", "DEF"), ("GHI", "JKL")] (Just (Right ("xyz":|[])))
-- This [("abcABC","FED"),("abcGHI","LKJ"),("cdeABC","FED"),("cdeGHI","LKJ")] Nothing
-- >>> This [("abc", reverse), ("cde", reverse)] (Just (Left ("stu":|[]))) <.> This [("ABC", "DEF"), ("GHI", "JKL")] Nothing
-- This [("abcABC","FED"),("abcGHI","LKJ"),("cdeABC","FED"),("cdeGHI","LKJ")] Nothing
-- >>> This [("abc", reverse), ("cde", reverse)] (Just (Right (id:|[reverse]))) <.> This [("ABC", "DEF"), ("GHI", "JKL")] Nothing
-- This [("abcABC","FED"),("abcGHI","LKJ"),("cdeABC","FED"),("cdeGHI","LKJ")] Nothing
-- >>> This [("abc", reverse), ("cde", reverse)] (Just (Left ("stu":|[]))) <.> This [("ABC", "DEF"), ("GHI", "JKL")] (Just (Left ("xyz":|[])))
-- This [("abcABC","FED"),("abcGHI","LKJ"),("cdeABC","FED"),("cdeGHI","LKJ")] Just (Left ("stu" :| []))
-- >>> This [("abc", reverse), ("cde", reverse)] (Just (Left ("stu":|[]))) <.> This [("ABC", "DEF"), ("GHI", "JKL")] (Just (Right ("xyz":|[])))
-- This [("abcABC","FED"),("abcGHI","LKJ"),("cdeABC","FED"),("cdeGHI","LKJ")] Just (Left ("stu" :| []))
-- >>> This [("abc", reverse), ("cde", reverse)] (Just (Right (id:|[reverse]))) <.> This [("ABC", "DEF"), ("GHI", "JKL")] (Just (Left ("xyz":|[])))
-- This [("abcABC","FED"),("abcGHI","LKJ"),("cdeABC","FED"),("cdeGHI","LKJ")] Just (Left ("xyz" :| []))
-- >>> This [("abc", reverse), ("cde", reverse)] (Just (Left ("stu":|[]))) <.> This [("ABC", "DEF"), ("GHI", "JKL")] (Just (Right ("xyz":|[])))
-- This [("abcABC","FED"),("abcGHI","LKJ"),("cdeABC","FED"),("cdeGHI","LKJ")] Just (Left ("stu" :| []))
instance (Semigroup a, Apply f) => Apply (This f a) where
  This f (a, a -> b)
t1 Maybe (Either (NonEmpty a) (NonEmpty (a -> b)))
r1 <.> :: This f a (a -> b) -> This f a a -> This f a b
<.> This f (a, a)
t2 Maybe (Either (NonEmpty a) (NonEmpty a))
r2 =
    f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This (((a, a -> b) -> (a, a) -> (a, b))
-> f (a, a -> b) -> f (a, a) -> f (a, b)
forall (f :: * -> *) a b c.
Apply f =>
(a -> b -> c) -> f a -> f b -> f c
liftF2 (a, a -> b) -> (a, a) -> (a, b)
forall (f :: * -> *) a b. Apply f => f (a -> b) -> f a -> f b
(<.>) f (a, a -> b)
t1 f (a, a)
t2) ((Either (NonEmpty a) (NonEmpty (a -> b))
 -> Either (NonEmpty a) (NonEmpty a)
 -> Either (NonEmpty a) (NonEmpty b))
-> Maybe (Either (NonEmpty a) (NonEmpty (a -> b)))
-> Maybe (Either (NonEmpty a) (NonEmpty a))
-> Maybe (Either (NonEmpty a) (NonEmpty b))
forall (f :: * -> *) a b c.
Apply f =>
(a -> b -> c) -> f a -> f b -> f c
liftF2 ((NonEmpty (a -> b) -> NonEmpty a -> NonEmpty b)
-> Either (NonEmpty a) (NonEmpty (a -> b))
-> Either (NonEmpty a) (NonEmpty a)
-> Either (NonEmpty a) (NonEmpty b)
forall (f :: * -> *) a b c.
Apply f =>
(a -> b -> c) -> f a -> f b -> f c
liftF2 NonEmpty (a -> b) -> NonEmpty a -> NonEmpty b
forall (f :: * -> *) a b. Apply f => f (a -> b) -> f a -> f b
(<.>)) Maybe (Either (NonEmpty a) (NonEmpty (a -> b)))
r1 Maybe (Either (NonEmpty a) (NonEmpty a))
r2)

instance (Monoid a, Applicative f) => Applicative (This f a) where
  pure :: a -> This f a a
pure a
a =
    f (a, a) -> Maybe (Either (NonEmpty a) (NonEmpty a)) -> This f a a
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This ((a, a) -> f (a, a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a
forall a. Monoid a => a
mempty, a
a)) (Either (NonEmpty a) (NonEmpty a)
-> Maybe (Either (NonEmpty a) (NonEmpty a))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NonEmpty a -> Either (NonEmpty a) (NonEmpty a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> NonEmpty a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a)))
  This f (a, a -> b)
t1 Maybe (Either (NonEmpty a) (NonEmpty (a -> b)))
r1 <*> :: This f a (a -> b) -> This f a a -> This f a b
<*> This f (a, a)
t2 Maybe (Either (NonEmpty a) (NonEmpty a))
r2 =
    f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This (((a, a -> b) -> (a, a) -> (a, b))
-> f (a, a -> b) -> f (a, a) -> f (a, b)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (a, a -> b) -> (a, a) -> (a, b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
(<*>) f (a, a -> b)
t1 f (a, a)
t2) ((Either (NonEmpty a) (NonEmpty (a -> b))
 -> Either (NonEmpty a) (NonEmpty a)
 -> Either (NonEmpty a) (NonEmpty b))
-> Maybe (Either (NonEmpty a) (NonEmpty (a -> b)))
-> Maybe (Either (NonEmpty a) (NonEmpty a))
-> Maybe (Either (NonEmpty a) (NonEmpty b))
forall (f :: * -> *) a b c.
Apply f =>
(a -> b -> c) -> f a -> f b -> f c
liftF2 ((NonEmpty (a -> b) -> NonEmpty a -> NonEmpty b)
-> Either (NonEmpty a) (NonEmpty (a -> b))
-> Either (NonEmpty a) (NonEmpty a)
-> Either (NonEmpty a) (NonEmpty b)
forall (f :: * -> *) a b c.
Apply f =>
(a -> b -> c) -> f a -> f b -> f c
liftF2 NonEmpty (a -> b) -> NonEmpty a -> NonEmpty b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
(<*>)) Maybe (Either (NonEmpty a) (NonEmpty (a -> b)))
r1 Maybe (Either (NonEmpty a) (NonEmpty a))
r2)

-- |
--
-- >>> swap (This [("abc", 'x'), ("def", 'y')] Nothing)
-- This [('x',"abc"),('y',"def")] Nothing
-- >>> swap (This [("abc", 'x'), ("def", 'y')] (Just (Left ("a":|[]))))
-- This [('x',"abc"),('y',"def")] Just (Right ("a" :| []))
-- >>> swap (This [("abc", 'x'), ("def", 'y')] (Just (Right ('a':|[]))))
-- This [('x',"abc"),('y',"def")] Just (Left ('a' :| ""))
instance Functor f => Swap (This f) where
  swap :: This f a b -> This f b a
swap (This f (a, b)
t Maybe (Either (NonEmpty a) (NonEmpty b))
r) =
    f (b, a) -> Maybe (Either (NonEmpty b) (NonEmpty a)) -> This f b a
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This (((a, b) -> (b, a)) -> f (a, b) -> f (b, a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a, b) -> (b, a)
forall (p :: * -> * -> *) a b. Swap p => p a b -> p b a
swap f (a, b)
t) ((Either (NonEmpty a) (NonEmpty b)
 -> Either (NonEmpty b) (NonEmpty a))
-> Maybe (Either (NonEmpty a) (NonEmpty b))
-> Maybe (Either (NonEmpty b) (NonEmpty a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Either (NonEmpty a) (NonEmpty b)
-> Either (NonEmpty b) (NonEmpty a)
forall (p :: * -> * -> *) a b. Swap p => p a b -> p b a
swap Maybe (Either (NonEmpty a) (NonEmpty b))
r)

class Functor f => Semialign f where
  align ::
    f a
    -> f b
    -> This f a b
  align =
    ((a, b) -> (a, b))
-> (a -> a) -> (b -> b) -> f a -> f b -> This f a b
forall (f :: * -> *) a b c d.
Semialign f =>
((a, b) -> (c, d))
-> (a -> c) -> (b -> d) -> f a -> f b -> This f c d
alignWith (\(a, b)
x -> (a, b)
x) (\a
x -> a
x) (\b
x -> b
x)
  alignWith ::
    ((a, b) -> (c, d))
    -> (a -> c)
    -> (b -> d)
    -> f a
    -> f b
    -> This f c d
  alignWith (a, b) -> (c, d)
f a -> c
g b -> d
h f a
t1 f b
t2 =
    case f a -> f b -> This f a b
forall (f :: * -> *) a b. Semialign f => f a -> f b -> This f a b
align f a
t1 f b
t2 of
      This f (a, b)
t Maybe (Either (NonEmpty a) (NonEmpty b))
r ->
        f (c, d) -> Maybe (Either (NonEmpty c) (NonEmpty d)) -> This f c d
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This (((a, b) -> (c, d)) -> f (a, b) -> f (c, d)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a, b) -> (c, d)
f f (a, b)
t) ((Either (NonEmpty a) (NonEmpty b)
 -> Either (NonEmpty c) (NonEmpty d))
-> Maybe (Either (NonEmpty a) (NonEmpty b))
-> Maybe (Either (NonEmpty c) (NonEmpty d))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((NonEmpty a -> NonEmpty c)
-> (NonEmpty b -> NonEmpty d)
-> Either (NonEmpty a) (NonEmpty b)
-> Either (NonEmpty c) (NonEmpty d)
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap ((a -> c) -> NonEmpty a -> NonEmpty c
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> c
g) ((b -> d) -> NonEmpty b -> NonEmpty d
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> d
h)) Maybe (Either (NonEmpty a) (NonEmpty b))
r)
  {-# MINIMAL align | alignWith #-}
  alignWith' ::
    (a -> c)
    -> (b -> d)
    -> f a
    -> f b
    -> This f c d
  alignWith' a -> c
f b -> d
g =
    ((a, b) -> (c, d))
-> (a -> c) -> (b -> d) -> f a -> f b -> This f c d
forall (f :: * -> *) a b c d.
Semialign f =>
((a, b) -> (c, d))
-> (a -> c) -> (b -> d) -> f a -> f b -> This f c d
alignWith ((a -> c) -> (b -> d) -> (a, b) -> (c, d)
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap a -> c
f b -> d
g) a -> c
f b -> d
g

-- |
--
-- >>> align "abc" "def"
-- This [('a','d'),('b','e'),('c','f')] Nothing
-- >>> align "abc" "defghi"
-- This [('a','d'),('b','e'),('c','f')] Just (Right ('g' :| "hi"))
-- >>> align "abcdef" "ghi"
-- This [('a','g'),('b','h'),('c','i')] Just (Left ('d' :| "ef"))
instance Semialign [] where
  align :: [a] -> [b] -> This [] a b
align (a
a:[a]
as) (b
b:[b]
bs) =
    let This [(a, b)]
t Maybe (Either (NonEmpty a) (NonEmpty b))
r = [a] -> [b] -> This [] a b
forall (f :: * -> *) a b. Semialign f => f a -> f b -> This f a b
align [a]
as [b]
bs
    in  [(a, b)] -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This [] a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This ((a
a,b
b)(a, b) -> [(a, b)] -> [(a, b)]
forall a. a -> [a] -> [a]
:[(a, b)]
t) Maybe (Either (NonEmpty a) (NonEmpty b))
r
  align (a
a:[a]
as) [] =
    [(a, b)] -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This [] a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This [] (Either (NonEmpty a) (NonEmpty b)
-> Maybe (Either (NonEmpty a) (NonEmpty b))
forall a. a -> Maybe a
Just (NonEmpty a -> Either (NonEmpty a) (NonEmpty b)
forall a b. a -> Either a b
Left (a
a a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| [a]
as)))
  align [] (b
b:[b]
bs) =
    [(a, b)] -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This [] a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This [] (Either (NonEmpty a) (NonEmpty b)
-> Maybe (Either (NonEmpty a) (NonEmpty b))
forall a. a -> Maybe a
Just (NonEmpty b -> Either (NonEmpty a) (NonEmpty b)
forall a b. b -> Either a b
Right (b
b b -> [b] -> NonEmpty b
forall a. a -> [a] -> NonEmpty a
:| [b]
bs)))
  align [] [] =
    [(a, b)] -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This [] a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This [] Maybe (Either (NonEmpty a) (NonEmpty b))
forall a. Maybe a
Nothing

-- |
--
-- >>> align (Just "x") (Just "y")
-- This (Just ("x","y")) Nothing
-- >>> align (Just "x") (Nothing :: Maybe String)
-- This Nothing Just (Left ("x" :| []))
-- >>> align (Nothing :: Maybe String) (Just "y")
-- This Nothing Just (Right ("y" :| []))
instance Semialign Maybe where
  align :: Maybe a -> Maybe b -> This Maybe a b
align (Just a
a) (Just b
b) =
    Maybe (a, b)
-> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This Maybe a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This ((a, b) -> Maybe (a, b)
forall a. a -> Maybe a
Just (a
a, b
b)) Maybe (Either (NonEmpty a) (NonEmpty b))
forall a. Maybe a
Nothing
  align (Just a
a) Maybe b
Nothing =
    Maybe (a, b)
-> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This Maybe a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This Maybe (a, b)
forall a. Maybe a
Nothing (Either (NonEmpty a) (NonEmpty b)
-> Maybe (Either (NonEmpty a) (NonEmpty b))
forall a. a -> Maybe a
Just (NonEmpty a -> Either (NonEmpty a) (NonEmpty b)
forall a b. a -> Either a b
Left (a
a a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| [])))
  align Maybe a
Nothing (Just b
b) =
    Maybe (a, b)
-> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This Maybe a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This Maybe (a, b)
forall a. Maybe a
Nothing (Either (NonEmpty a) (NonEmpty b)
-> Maybe (Either (NonEmpty a) (NonEmpty b))
forall a. a -> Maybe a
Just (NonEmpty b -> Either (NonEmpty a) (NonEmpty b)
forall a b. b -> Either a b
Right (b
b b -> [b] -> NonEmpty b
forall a. a -> [a] -> NonEmpty a
:| [])))
  align Maybe a
Nothing Maybe b
Nothing =
    Maybe (a, b)
-> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This Maybe a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This Maybe (a, b)
forall a. Maybe a
Nothing Maybe (Either (NonEmpty a) (NonEmpty b))
forall a. Maybe a
Nothing

-- |
--
-- >>> align (Identity "x") (Identity "y")
-- This (Identity ("x","y")) Nothing
instance Semialign Identity where
  align :: Identity a -> Identity b -> This Identity a b
align (Identity a
a) (Identity b
b) =
    Identity (a, b)
-> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This Identity a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This ((a, b) -> Identity (a, b)
forall a. a -> Identity a
Identity (a
a, b
b)) Maybe (Either (NonEmpty a) (NonEmpty b))
forall a. Maybe a
Nothing

-- |
--
-- >>> align ('a':|"bc") ('g':|"hi")
-- This (('a','g') :| [('b','h'),('c','i')]) Nothing
-- >>> align ('a':|"bc") ('g':|"hijkl")
-- This (('a','g') :| [('b','h'),('c','i')]) Just (Right ('j' :| "kl"))
-- >>> align ('a':|"bcdef") ('g':|"hi")
-- This (('a','g') :| [('b','h'),('c','i')]) Just (Left ('d' :| "ef"))
instance Semialign NonEmpty where
  align :: NonEmpty a -> NonEmpty b -> This NonEmpty a b
align (a
h1:|[]) (b
h2:|[]) =
    NonEmpty (a, b)
-> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This NonEmpty a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This ((a
h1, b
h2)(a, b) -> [(a, b)] -> NonEmpty (a, b)
forall a. a -> [a] -> NonEmpty a
:|[]) Maybe (Either (NonEmpty a) (NonEmpty b))
forall a. Maybe a
Nothing
  align (a
h1:|a
i1:[a]
r1) (b
h2:|[]) =
    NonEmpty (a, b)
-> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This NonEmpty a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This ((a
h1, b
h2)(a, b) -> [(a, b)] -> NonEmpty (a, b)
forall a. a -> [a] -> NonEmpty a
:|[]) (Either (NonEmpty a) (NonEmpty b)
-> Maybe (Either (NonEmpty a) (NonEmpty b))
forall a. a -> Maybe a
Just (NonEmpty a -> Either (NonEmpty a) (NonEmpty b)
forall a b. a -> Either a b
Left (a
i1a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:|[a]
r1)))
  align (a
h1:|[]) (b
h2:|b
i2:[b]
r2) =
    NonEmpty (a, b)
-> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This NonEmpty a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This ((a
h1, b
h2)(a, b) -> [(a, b)] -> NonEmpty (a, b)
forall a. a -> [a] -> NonEmpty a
:|[]) (Either (NonEmpty a) (NonEmpty b)
-> Maybe (Either (NonEmpty a) (NonEmpty b))
forall a. a -> Maybe a
Just (NonEmpty b -> Either (NonEmpty a) (NonEmpty b)
forall a b. b -> Either a b
Right (b
i2b -> [b] -> NonEmpty b
forall a. a -> [a] -> NonEmpty a
:|[b]
r2)))
  align (a
h1:|a
i1:[a]
r1) (b
h2:|b
i2:[b]
r2) =
    let This NonEmpty (a, b)
t Maybe (Either (NonEmpty a) (NonEmpty b))
r = NonEmpty a -> NonEmpty b -> This NonEmpty a b
forall (f :: * -> *) a b. Semialign f => f a -> f b -> This f a b
align (a
i1a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:|[a]
r1) (b
i2b -> [b] -> NonEmpty b
forall a. a -> [a] -> NonEmpty a
:|[b]
r2)
    in  NonEmpty (a, b)
-> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This NonEmpty a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This ((a
h1, b
h2) (a, b) -> NonEmpty (a, b) -> NonEmpty (a, b)
forall a. a -> NonEmpty a -> NonEmpty a
`NonEmpty.cons` NonEmpty (a, b)
t) Maybe (Either (NonEmpty a) (NonEmpty b))
r

instance Semialign ZipList where
  align :: ZipList a -> ZipList b -> This ZipList a b
align (ZipList [a]
a) (ZipList [b]
b) =
    ASetter (This [] a b) (This ZipList a b) [(a, b)] (ZipList (a, b))
-> ([(a, b)] -> ZipList (a, b)) -> This [] a b -> This ZipList a b
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over ASetter (This [] a b) (This ZipList a b) [(a, b)] (ZipList (a, b))
forall (f :: * -> *) a b (f' :: * -> *).
Lens (This f a b) (This f' a b) (f (a, b)) (f' (a, b))
these [(a, b)] -> ZipList (a, b)
forall a. [a] -> ZipList a
ZipList ([a] -> [b] -> This [] a b
forall (f :: * -> *) a b. Semialign f => f a -> f b -> This f a b
align [a]
a [b]
b)

class Semialign f => Align f where
  nil ::
    f a

instance Align [] where
  nil :: [a]
nil =
    []

instance Align Maybe where
  nil :: Maybe a
nil =
    Maybe a
forall a. Maybe a
Nothing

instance Align ZipList where
  nil :: ZipList a
nil =
    [a] -> ZipList a
forall a. [a] -> ZipList a
ZipList []

-- |
--
-- >>> This [("abc", 's'), ("def", 't')] Nothing <> This [("ghi", 'u'), ("jkl", 'v')] Nothing
-- This [("abc",'s'),("def",'t'),("ghi",'u'),("jkl",'v')] Nothing
-- >>> This [("abc", 's'), ("def", 't')] Nothing <> This [("ghi", 'u'), ("jkl", 'v')] (Just (Left ("mno":|["pqr"])))
-- This [("abc",'s'),("def",'t'),("ghi",'u'),("jkl",'v')] Just (Left ("mno" :| ["pqr"]))
-- >>> This [("abc", 's'), ("def", 't')] Nothing <> This [("ghi", 'u'), ("jkl", 'v')] (Just (Right ('o':|"pqr")))
-- This [("abc",'s'),("def",'t'),("ghi",'u'),("jkl",'v')] Just (Right ('o' :| "pqr"))
-- >>> This [("abc", 's'), ("def", 't')] (Just (Left ("mno":|["pqr"]))) <> This [("ghi", 'u'), ("jkl", 'v')] Nothing
-- This [("abc",'s'),("def",'t'),("ghi",'u'),("jkl",'v')] Just (Left ("mno" :| ["pqr"]))
-- >>> This [("abc", 's'), ("def", 't')] (Just (Right ('o':|"pqr"))) <> This [("ghi", 'u'), ("jkl", 'v')] Nothing
-- This [("abc",'s'),("def",'t'),("ghi",'u'),("jkl",'v')] Just (Right ('o' :| "pqr"))
-- >>> This [("abc", 's'), ("def", 't')] (Just (Left ("mno":|["pqr"]))) <> This [("ghi", 'u'), ("jkl", 'v')] (Just (Left ("ccddee":|["ffgghh"])))
-- This [("abc",'s'),("def",'t'),("ghi",'u'),("jkl",'v')] Just (Left ("mno" :| ["pqr","ccddee","ffgghh"]))
-- >>> This [("abc", 's'), ("def", 't')] (Just (Left ("mno":|["pqr"]))) <> This [("ghi", 'u'), ("jkl", 'v')] (Just (Right ('c':|"ddeeff")))
-- This [("abc",'s'),("def",'t'),("ghi",'u'),("jkl",'v'),("mno",'c'),("pqr",'d')] Just (Right ('d' :| "eeff"))
-- >>> This [("abc", 's'), ("def", 't')] (Just (Right ('x':|"yyzz"))) <> This [("ghi", 'u'), ("jkl", 'v')] (Just (Right ('c':|"ddeeff")))
-- This [("abc",'s'),("def",'t'),("ghi",'u'),("jkl",'v')] Just (Right ('x' :| "yyzzcddeeff"))
-- >>> This [("abc", 's'), ("def", 't')] (Just (Right ('x':|"yyzz"))) <> This [("ghi", 'u'), ("jkl", 'v')] (Just (Left ("cc":|["ddeeff"])))
-- This [("abc",'s'),("def",'t'),("ghi",'u'),("jkl",'v'),("cc",'x'),("ddeeff",'y')] Just (Right ('y' :| "zz"))
instance Semigroup (This [] a b) where
  This [(a, b)]
t1 (Just (Left NonEmpty a
as1)) <> :: This [] a b -> This [] a b -> This [] a b
<> This [(a, b)]
t2 (Just (Left NonEmpty a
as2)) =
    [(a, b)] -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This [] a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This ([(a, b)]
t1 [(a, b)] -> [(a, b)] -> [(a, b)]
forall a. Semigroup a => a -> a -> a
<> [(a, b)]
t2) (Either (NonEmpty a) (NonEmpty b)
-> Maybe (Either (NonEmpty a) (NonEmpty b))
forall a. a -> Maybe a
Just (NonEmpty a -> Either (NonEmpty a) (NonEmpty b)
forall a b. a -> Either a b
Left (NonEmpty a
as1 NonEmpty a -> NonEmpty a -> NonEmpty a
forall a. Semigroup a => a -> a -> a
<> NonEmpty a
as2)))
  This [(a, b)]
t1 (Just (Left NonEmpty a
as1)) <> This [(a, b)]
t2 (Just (Right NonEmpty b
bs2)) =
    ASetter
  (This NonEmpty a b) (This [] a b) (NonEmpty (a, b)) [(a, b)]
-> (NonEmpty (a, b) -> [(a, b)])
-> This NonEmpty a b
-> This [] a b
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over ASetter
  (This NonEmpty a b) (This [] a b) (NonEmpty (a, b)) [(a, b)]
forall (f :: * -> *) a b (f' :: * -> *).
Lens (This f a b) (This f' a b) (f (a, b)) (f' (a, b))
these (\NonEmpty (a, b)
x -> [(a, b)]
t1 [(a, b)] -> [(a, b)] -> [(a, b)]
forall a. Semigroup a => a -> a -> a
<> [(a, b)]
t2 [(a, b)] -> [(a, b)] -> [(a, b)]
forall a. Semigroup a => a -> a -> a
<> NonEmpty (a, b) -> [(a, b)]
forall a. NonEmpty a -> [a]
NonEmpty.toList NonEmpty (a, b)
x) (NonEmpty a -> NonEmpty b -> This NonEmpty a b
forall (f :: * -> *) a b. Semialign f => f a -> f b -> This f a b
align NonEmpty a
as1 NonEmpty b
bs2)
  This [(a, b)]
t1 (Just (Left NonEmpty a
as1)) <> This [(a, b)]
t2 Maybe (Either (NonEmpty a) (NonEmpty b))
Nothing =
    [(a, b)] -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This [] a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This ([(a, b)]
t1 [(a, b)] -> [(a, b)] -> [(a, b)]
forall a. Semigroup a => a -> a -> a
<> [(a, b)]
t2) (Either (NonEmpty a) (NonEmpty b)
-> Maybe (Either (NonEmpty a) (NonEmpty b))
forall a. a -> Maybe a
Just (NonEmpty a -> Either (NonEmpty a) (NonEmpty b)
forall a b. a -> Either a b
Left NonEmpty a
as1))
  This [(a, b)]
t1 (Just (Right NonEmpty b
bs1)) <> This [(a, b)]
t2 (Just (Right NonEmpty b
bs2)) =
    [(a, b)] -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This [] a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This ([(a, b)]
t1 [(a, b)] -> [(a, b)] -> [(a, b)]
forall a. Semigroup a => a -> a -> a
<> [(a, b)]
t2) (Either (NonEmpty a) (NonEmpty b)
-> Maybe (Either (NonEmpty a) (NonEmpty b))
forall a. a -> Maybe a
Just (NonEmpty b -> Either (NonEmpty a) (NonEmpty b)
forall a b. b -> Either a b
Right (NonEmpty b
bs1 NonEmpty b -> NonEmpty b -> NonEmpty b
forall a. Semigroup a => a -> a -> a
<> NonEmpty b
bs2)))
  This [(a, b)]
t1 (Just (Right NonEmpty b
bs1)) <> This [(a, b)]
t2 (Just (Left NonEmpty a
as2)) =
    ASetter
  (This NonEmpty a b) (This [] a b) (NonEmpty (a, b)) [(a, b)]
-> (NonEmpty (a, b) -> [(a, b)])
-> This NonEmpty a b
-> This [] a b
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over ASetter
  (This NonEmpty a b) (This [] a b) (NonEmpty (a, b)) [(a, b)]
forall (f :: * -> *) a b (f' :: * -> *).
Lens (This f a b) (This f' a b) (f (a, b)) (f' (a, b))
these (\NonEmpty (a, b)
x -> [(a, b)]
t1 [(a, b)] -> [(a, b)] -> [(a, b)]
forall a. Semigroup a => a -> a -> a
<> [(a, b)]
t2 [(a, b)] -> [(a, b)] -> [(a, b)]
forall a. Semigroup a => a -> a -> a
<> NonEmpty (a, b) -> [(a, b)]
forall a. NonEmpty a -> [a]
NonEmpty.toList NonEmpty (a, b)
x) (NonEmpty a -> NonEmpty b -> This NonEmpty a b
forall (f :: * -> *) a b. Semialign f => f a -> f b -> This f a b
align NonEmpty a
as2 NonEmpty b
bs1)
  This [(a, b)]
t1 (Just (Right NonEmpty b
bs1)) <> This [(a, b)]
t2 Maybe (Either (NonEmpty a) (NonEmpty b))
Nothing =
    [(a, b)] -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This [] a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This ([(a, b)]
t1 [(a, b)] -> [(a, b)] -> [(a, b)]
forall a. Semigroup a => a -> a -> a
<> [(a, b)]
t2) (Either (NonEmpty a) (NonEmpty b)
-> Maybe (Either (NonEmpty a) (NonEmpty b))
forall a. a -> Maybe a
Just (NonEmpty b -> Either (NonEmpty a) (NonEmpty b)
forall a b. b -> Either a b
Right NonEmpty b
bs1))
  This [(a, b)]
t1 Maybe (Either (NonEmpty a) (NonEmpty b))
Nothing <> This [(a, b)]
t2 (Just (Left NonEmpty a
as2)) =
    [(a, b)] -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This [] a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This ([(a, b)]
t1 [(a, b)] -> [(a, b)] -> [(a, b)]
forall a. Semigroup a => a -> a -> a
<> [(a, b)]
t2) (Either (NonEmpty a) (NonEmpty b)
-> Maybe (Either (NonEmpty a) (NonEmpty b))
forall a. a -> Maybe a
Just (NonEmpty a -> Either (NonEmpty a) (NonEmpty b)
forall a b. a -> Either a b
Left NonEmpty a
as2))
  This [(a, b)]
t1 Maybe (Either (NonEmpty a) (NonEmpty b))
Nothing <> This [(a, b)]
t2 (Just (Right NonEmpty b
bs2)) =
    [(a, b)] -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This [] a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This ([(a, b)]
t1 [(a, b)] -> [(a, b)] -> [(a, b)]
forall a. Semigroup a => a -> a -> a
<> [(a, b)]
t2) (Either (NonEmpty a) (NonEmpty b)
-> Maybe (Either (NonEmpty a) (NonEmpty b))
forall a. a -> Maybe a
Just (NonEmpty b -> Either (NonEmpty a) (NonEmpty b)
forall a b. b -> Either a b
Right NonEmpty b
bs2))
  This [(a, b)]
t1 Maybe (Either (NonEmpty a) (NonEmpty b))
Nothing <> This [(a, b)]
t2 Maybe (Either (NonEmpty a) (NonEmpty b))
Nothing =
    [(a, b)] -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This [] a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This ([(a, b)]
t1 [(a, b)] -> [(a, b)] -> [(a, b)]
forall a. Semigroup a => a -> a -> a
<> [(a, b)]
t2) Maybe (Either (NonEmpty a) (NonEmpty b))
forall a. Maybe a
Nothing

instance Semigroup (This NonEmpty a b) where
  This NonEmpty (a, b)
t1 (Just (Left NonEmpty a
as1)) <> :: This NonEmpty a b -> This NonEmpty a b -> This NonEmpty a b
<> This NonEmpty (a, b)
t2 (Just (Left NonEmpty a
as2)) =
    NonEmpty (a, b)
-> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This NonEmpty a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This (NonEmpty (a, b)
t1 NonEmpty (a, b) -> NonEmpty (a, b) -> NonEmpty (a, b)
forall a. Semigroup a => a -> a -> a
<> NonEmpty (a, b)
t2) (Either (NonEmpty a) (NonEmpty b)
-> Maybe (Either (NonEmpty a) (NonEmpty b))
forall a. a -> Maybe a
Just (NonEmpty a -> Either (NonEmpty a) (NonEmpty b)
forall a b. a -> Either a b
Left (NonEmpty a
as1 NonEmpty a -> NonEmpty a -> NonEmpty a
forall a. Semigroup a => a -> a -> a
<> NonEmpty a
as2)))
  This NonEmpty (a, b)
t1 (Just (Left NonEmpty a
as1)) <> This NonEmpty (a, b)
t2 (Just (Right NonEmpty b
bs2)) =
    ASetter
  (This NonEmpty a b)
  (This NonEmpty a b)
  (NonEmpty (a, b))
  (NonEmpty (a, b))
-> (NonEmpty (a, b) -> NonEmpty (a, b))
-> This NonEmpty a b
-> This NonEmpty a b
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over ASetter
  (This NonEmpty a b)
  (This NonEmpty a b)
  (NonEmpty (a, b))
  (NonEmpty (a, b))
forall (f :: * -> *) a b (f' :: * -> *).
Lens (This f a b) (This f' a b) (f (a, b)) (f' (a, b))
these (\NonEmpty (a, b)
x -> NonEmpty (a, b)
t1 NonEmpty (a, b) -> NonEmpty (a, b) -> NonEmpty (a, b)
forall a. Semigroup a => a -> a -> a
<> NonEmpty (a, b)
t2 NonEmpty (a, b) -> NonEmpty (a, b) -> NonEmpty (a, b)
forall a. Semigroup a => a -> a -> a
<> NonEmpty (a, b)
x) (NonEmpty a -> NonEmpty b -> This NonEmpty a b
forall (f :: * -> *) a b. Semialign f => f a -> f b -> This f a b
align NonEmpty a
as1 NonEmpty b
bs2)
  This NonEmpty (a, b)
t1 (Just (Left NonEmpty a
as1)) <> This NonEmpty (a, b)
t2 Maybe (Either (NonEmpty a) (NonEmpty b))
Nothing =
    NonEmpty (a, b)
-> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This NonEmpty a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This (NonEmpty (a, b)
t1 NonEmpty (a, b) -> NonEmpty (a, b) -> NonEmpty (a, b)
forall a. Semigroup a => a -> a -> a
<> NonEmpty (a, b)
t2) (Either (NonEmpty a) (NonEmpty b)
-> Maybe (Either (NonEmpty a) (NonEmpty b))
forall a. a -> Maybe a
Just (NonEmpty a -> Either (NonEmpty a) (NonEmpty b)
forall a b. a -> Either a b
Left NonEmpty a
as1))
  This NonEmpty (a, b)
t1 (Just (Right NonEmpty b
bs1)) <> This NonEmpty (a, b)
t2 (Just (Right NonEmpty b
bs2)) =
    NonEmpty (a, b)
-> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This NonEmpty a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This (NonEmpty (a, b)
t1 NonEmpty (a, b) -> NonEmpty (a, b) -> NonEmpty (a, b)
forall a. Semigroup a => a -> a -> a
<> NonEmpty (a, b)
t2) (Either (NonEmpty a) (NonEmpty b)
-> Maybe (Either (NonEmpty a) (NonEmpty b))
forall a. a -> Maybe a
Just (NonEmpty b -> Either (NonEmpty a) (NonEmpty b)
forall a b. b -> Either a b
Right (NonEmpty b
bs1 NonEmpty b -> NonEmpty b -> NonEmpty b
forall a. Semigroup a => a -> a -> a
<> NonEmpty b
bs2)))
  This NonEmpty (a, b)
t1 (Just (Right NonEmpty b
bs1)) <> This NonEmpty (a, b)
t2 (Just (Left NonEmpty a
as2)) =
    ASetter
  (This NonEmpty a b)
  (This NonEmpty a b)
  (NonEmpty (a, b))
  (NonEmpty (a, b))
-> (NonEmpty (a, b) -> NonEmpty (a, b))
-> This NonEmpty a b
-> This NonEmpty a b
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over ASetter
  (This NonEmpty a b)
  (This NonEmpty a b)
  (NonEmpty (a, b))
  (NonEmpty (a, b))
forall (f :: * -> *) a b (f' :: * -> *).
Lens (This f a b) (This f' a b) (f (a, b)) (f' (a, b))
these (\NonEmpty (a, b)
x -> NonEmpty (a, b)
t1 NonEmpty (a, b) -> NonEmpty (a, b) -> NonEmpty (a, b)
forall a. Semigroup a => a -> a -> a
<> NonEmpty (a, b)
t2 NonEmpty (a, b) -> NonEmpty (a, b) -> NonEmpty (a, b)
forall a. Semigroup a => a -> a -> a
<> NonEmpty (a, b)
x) (NonEmpty a -> NonEmpty b -> This NonEmpty a b
forall (f :: * -> *) a b. Semialign f => f a -> f b -> This f a b
align NonEmpty a
as2 NonEmpty b
bs1)
  This NonEmpty (a, b)
t1 (Just (Right NonEmpty b
bs1)) <> This NonEmpty (a, b)
t2 Maybe (Either (NonEmpty a) (NonEmpty b))
Nothing =
    NonEmpty (a, b)
-> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This NonEmpty a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This (NonEmpty (a, b)
t1 NonEmpty (a, b) -> NonEmpty (a, b) -> NonEmpty (a, b)
forall a. Semigroup a => a -> a -> a
<> NonEmpty (a, b)
t2) (Either (NonEmpty a) (NonEmpty b)
-> Maybe (Either (NonEmpty a) (NonEmpty b))
forall a. a -> Maybe a
Just (NonEmpty b -> Either (NonEmpty a) (NonEmpty b)
forall a b. b -> Either a b
Right NonEmpty b
bs1))
  This NonEmpty (a, b)
t1 Maybe (Either (NonEmpty a) (NonEmpty b))
Nothing <> This NonEmpty (a, b)
t2 (Just (Left NonEmpty a
as2)) =
    NonEmpty (a, b)
-> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This NonEmpty a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This (NonEmpty (a, b)
t1 NonEmpty (a, b) -> NonEmpty (a, b) -> NonEmpty (a, b)
forall a. Semigroup a => a -> a -> a
<> NonEmpty (a, b)
t2) (Either (NonEmpty a) (NonEmpty b)
-> Maybe (Either (NonEmpty a) (NonEmpty b))
forall a. a -> Maybe a
Just (NonEmpty a -> Either (NonEmpty a) (NonEmpty b)
forall a b. a -> Either a b
Left NonEmpty a
as2))
  This NonEmpty (a, b)
t1 Maybe (Either (NonEmpty a) (NonEmpty b))
Nothing <> This NonEmpty (a, b)
t2 (Just (Right NonEmpty b
bs2)) =
    NonEmpty (a, b)
-> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This NonEmpty a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This (NonEmpty (a, b)
t1 NonEmpty (a, b) -> NonEmpty (a, b) -> NonEmpty (a, b)
forall a. Semigroup a => a -> a -> a
<> NonEmpty (a, b)
t2) (Either (NonEmpty a) (NonEmpty b)
-> Maybe (Either (NonEmpty a) (NonEmpty b))
forall a. a -> Maybe a
Just (NonEmpty b -> Either (NonEmpty a) (NonEmpty b)
forall a b. b -> Either a b
Right NonEmpty b
bs2))
  This NonEmpty (a, b)
t1 Maybe (Either (NonEmpty a) (NonEmpty b))
Nothing <> This NonEmpty (a, b)
t2 Maybe (Either (NonEmpty a) (NonEmpty b))
Nothing =
    NonEmpty (a, b)
-> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This NonEmpty a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This (NonEmpty (a, b)
t1 NonEmpty (a, b) -> NonEmpty (a, b) -> NonEmpty (a, b)
forall a. Semigroup a => a -> a -> a
<> NonEmpty (a, b)
t2) Maybe (Either (NonEmpty a) (NonEmpty b))
forall a. Maybe a
Nothing

instance Monoid (This [] a b) where
  mempty :: This [] a b
mempty =
    [(a, b)] -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This [] a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This [(a, b)]
forall a. Monoid a => a
mempty Maybe (Either (NonEmpty a) (NonEmpty b))
forall a. Maybe a
Nothing

-- |
--
-- >>> over these reverse (This [("abc", 'x'), ("def", 'y')] Nothing)
-- This [("def",'y'),("abc",'x')] Nothing
-- >>> over these reverse (This [("abc", 'x'), ("def", 'y')] (Just (Left ("ghi":|["jkl"]))))
-- This [("def",'y'),("abc",'x')] Just (Left ("ghi" :| ["jkl"]))
these ::
  Lens
    (This f a b)
    (This f' a b)
    (f (a, b))
    (f' (a, b))
these :: (f (a, b) -> f (f' (a, b))) -> This f a b -> f (This f' a b)
these f (a, b) -> f (f' (a, b))
f (This f (a, b)
t Maybe (Either (NonEmpty a) (NonEmpty b))
r) =
  (f' (a, b) -> This f' a b) -> f (f' (a, b)) -> f (This f' a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\f' (a, b)
t' -> f' (a, b)
-> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f' a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This f' (a, b)
t' Maybe (Either (NonEmpty a) (NonEmpty b))
r) (f (a, b) -> f (f' (a, b))
f f (a, b)
t)

-- |
--
-- >>> over those (fmap (bimap (fmap reverse) (fmap Data.Char.toUpper))) (This [("abc", 'x'), ("def", 'y')] Nothing)
-- This [("abc",'x'),("def",'y')] Nothing
-- >>> over those (fmap (bimap (fmap reverse) (fmap Data.Char.toUpper))) (This [("abc", 'x'), ("def", 'y')] (Just (Left ("abc":|["def"]))))
-- This [("abc",'x'),("def",'y')] Just (Left ("cba" :| ["fed"]))
-- >>> over those (fmap (bimap (fmap reverse) (fmap Data.Char.toUpper))) (This [("abc", 'x'), ("def", 'y')] (Just (Right ('a':|"bcde"))))
-- This [("abc",'x'),("def",'y')] Just (Right ('A' :| "BCDE"))
-- >>> Control.Lens.view those (This [("abc", 'x'), ("def", 'y')] Nothing)
-- Nothing
-- >>> Control.Lens.view those (This [("abc", 'x'), ("def", 'y')] (Just (Left ("abc":|["def"]))))
-- Just (Left ("abc" :| ["def"]))
-- >>> Control.Lens.view those (This [("abc", 'x'), ("def", 'y')] (Just (Right ('a':|"bcde"))))
-- Just (Right ('a' :| "bcde"))
those ::
  Lens'
    (This f a b)
    (Maybe (Either (NonEmpty a) (NonEmpty b)))
those :: (Maybe (Either (NonEmpty a) (NonEmpty b))
 -> f (Maybe (Either (NonEmpty a) (NonEmpty b))))
-> This f a b -> f (This f a b)
those Maybe (Either (NonEmpty a) (NonEmpty b))
-> f (Maybe (Either (NonEmpty a) (NonEmpty b)))
f (This f (a, b)
t Maybe (Either (NonEmpty a) (NonEmpty b))
r) =
  (Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b)
-> f (Maybe (Either (NonEmpty a) (NonEmpty b))) -> f (This f a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Maybe (Either (NonEmpty a) (NonEmpty b))
r' -> f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This f (a, b)
t Maybe (Either (NonEmpty a) (NonEmpty b))
r') (Maybe (Either (NonEmpty a) (NonEmpty b))
-> f (Maybe (Either (NonEmpty a) (NonEmpty b)))
f Maybe (Either (NonEmpty a) (NonEmpty b))
r)

-- |
--
-- >>> over allThese (bimap reverse Data.Char.toUpper) (This [("abc", 'x'), ("def", 'y')] Nothing)
-- This [("cba",'X'),("fed",'Y')] Nothing
-- >>> over allThese (bimap reverse Data.Char.toUpper) (This [("abc", 'x'), ("def", 'y')] (Just (Left ("abc":|["def"]))))
-- This [("cba",'X'),("fed",'Y')] Just (Left ("abc" :| ["def"]))
-- >>> over allThese (bimap reverse Data.Char.toUpper) (This [("abc", 'x'), ("def", 'y')] (Just (Right ('a':|"bcde"))))
-- This [("cba",'X'),("fed",'Y')] Just (Right ('a' :| "bcde"))
-- >>> Control.Lens.preview allThese (This [("abc", 'x'), ("def", 'y')] Nothing)
-- Just ("abc",'x')
-- >>> Control.Lens.preview allThese (This [("abc", 'x'), ("def", 'y')] (Just (Left ("abc":|["def"]))))
-- Just ("abc",'x')
-- >>> Control.Lens.preview allThese (This [("abc", 'x'), ("def", 'y')] (Just (Right ('a':|"bcde"))))
-- Just ("abc",'x')
allThese ::
  Traversable f =>
  Traversal'
    (This f a b)
    (a, b)
allThese :: Traversal' (This f a b) (a, b)
allThese =
  (f (a, b) -> f (f (a, b))) -> This f a b -> f (This f a b)
forall (f :: * -> *) a b (f' :: * -> *).
Lens (This f a b) (This f' a b) (f (a, b)) (f' (a, b))
these ((f (a, b) -> f (f (a, b))) -> This f a b -> f (This f a b))
-> (((a, b) -> f (a, b)) -> f (a, b) -> f (f (a, b)))
-> ((a, b) -> f (a, b))
-> This f a b
-> f (This f a b)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ((a, b) -> f (a, b)) -> f (a, b) -> f (f (a, b))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse

-- |
--
-- >>> over allThese1 reverse (This [("abc", 'x'), ("def", 'y')] Nothing)
-- This [("cba",'x'),("fed",'y')] Nothing
-- >>> over allThese1 reverse (This [("abc", 'x'), ("def", 'y')] (Just (Left ("abc":|["def"]))))
-- This [("cba",'x'),("fed",'y')] Just (Left ("abc" :| ["def"]))
-- >>> over allThese1 reverse (This [("abc", 'x'), ("def", 'y')] (Just (Right ('a':|"bcde"))))
-- This [("cba",'x'),("fed",'y')] Just (Right ('a' :| "bcde"))
-- >>> Control.Lens.preview allThese1 (This [("abc", 'x'), ("def", 'y')] Nothing)
-- Just "abc"
-- >>> Control.Lens.preview allThese1 (This [("abc", 'x'), ("def", 'y')] (Just (Left ("abc":|["def"]))))
-- Just "abc"
-- >>> Control.Lens.preview allThese1 (This [("abc", 'x'), ("def", 'y')] (Just (Right ('a':|"bcde"))))
-- Just "abc"
allThese1 ::
  Traversable f =>
  Traversal'
    (This f a b)
    a
allThese1 :: Traversal' (This f a b) a
allThese1 =
  ((a, b) -> f (a, b)) -> This f a b -> f (This f a b)
forall (f :: * -> *) a b.
Traversable f =>
Traversal' (This f a b) (a, b)
allThese (((a, b) -> f (a, b)) -> This f a b -> f (This f a b))
-> ((a -> f a) -> (a, b) -> f (a, b))
-> (a -> f a)
-> This f a b
-> f (This f a b)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (a -> f a) -> (a, b) -> f (a, b)
forall s t a b. Field1 s t a b => Lens s t a b
_1

-- |
--
-- >>> over allThese2 Data.Char.toUpper (This [("abc", 'x'), ("def", 'y')] Nothing)
-- This [("abc",'X'),("def",'Y')] Nothing
-- >>> over allThese2 Data.Char.toUpper (This [("abc", 'x'), ("def", 'y')] (Just (Left ("abc":|["def"]))))
-- This [("abc",'X'),("def",'Y')] Just (Left ("abc" :| ["def"]))
-- >>> over allThese2 Data.Char.toUpper (This [("abc", 'x'), ("def", 'y')] (Just (Right ('a':|"bcde"))))
-- This [("abc",'X'),("def",'Y')] Just (Right ('a' :| "bcde"))
-- >>> Control.Lens.preview allThese2 (This [("abc", 'x'), ("def", 'y')] Nothing)
-- Just 'x'
-- >>> Control.Lens.preview allThese2 (This [("abc", 'x'), ("def", 'y')] (Just (Left ("abc":|["def"]))))
-- Just 'x'
-- >>> Control.Lens.preview allThese2 (This [("abc", 'x'), ("def", 'y')] (Just (Right ('a':|"bcde"))))
-- Just 'x'
allThese2 ::
  Traversable f =>
  Traversal'
    (This f a b)
    b
allThese2 :: Traversal' (This f a b) b
allThese2 =
  ((a, b) -> f (a, b)) -> This f a b -> f (This f a b)
forall (f :: * -> *) a b.
Traversable f =>
Traversal' (This f a b) (a, b)
allThese (((a, b) -> f (a, b)) -> This f a b -> f (This f a b))
-> ((b -> f b) -> (a, b) -> f (a, b))
-> (b -> f b)
-> This f a b
-> f (This f a b)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (b -> f b) -> (a, b) -> f (a, b)
forall s t a b. Field2 s t a b => Lens s t a b
_2

-- |
--
-- >>> over allThose (bimap (fmap reverse) (fmap Data.Char.toUpper)) (This [("abc", 'x'), ("def", 'y')] Nothing)
-- This [("abc",'x'),("def",'y')] Nothing
-- >>> over allThose (bimap (fmap reverse) (fmap Data.Char.toUpper)) (This [("abc", 'x'), ("def", 'y')] (Just (Left ("abc":|["def"]))))
-- This [("abc",'x'),("def",'y')] Just (Left ("cba" :| ["fed"]))
-- >>> over allThose (bimap (fmap reverse) (fmap Data.Char.toUpper)) (This [("abc", 'x'), ("def", 'y')] (Just (Right ('a':|"bcde"))))
-- This [("abc",'x'),("def",'y')] Just (Right ('A' :| "BCDE"))
-- >>> Control.Lens.preview allThose (This [("abc", 'x'), ("def", 'y')] Nothing)
-- Nothing
-- >>> Control.Lens.preview allThose (This [("abc", 'x'), ("def", 'y')] (Just (Left ("abc":|["def"]))))
-- Just (Left ("abc" :| ["def"]))
-- >>> Control.Lens.preview allThose (This [("abc", 'x'), ("def", 'y')] (Just (Right ('a':|"bcde"))))
-- Just (Right ('a' :| "bcde"))
allThose ::
  Traversal'
    (This f a b)
    (Either (NonEmpty a) (NonEmpty b))
allThose :: (Either (NonEmpty a) (NonEmpty b)
 -> f (Either (NonEmpty a) (NonEmpty b)))
-> This f a b -> f (This f a b)
allThose =
  (Maybe (Either (NonEmpty a) (NonEmpty b))
 -> f (Maybe (Either (NonEmpty a) (NonEmpty b))))
-> This f a b -> f (This f a b)
forall (f :: * -> *) a b.
Lens' (This f a b) (Maybe (Either (NonEmpty a) (NonEmpty b)))
those ((Maybe (Either (NonEmpty a) (NonEmpty b))
  -> f (Maybe (Either (NonEmpty a) (NonEmpty b))))
 -> This f a b -> f (This f a b))
-> ((Either (NonEmpty a) (NonEmpty b)
     -> f (Either (NonEmpty a) (NonEmpty b)))
    -> Maybe (Either (NonEmpty a) (NonEmpty b))
    -> f (Maybe (Either (NonEmpty a) (NonEmpty b))))
-> (Either (NonEmpty a) (NonEmpty b)
    -> f (Either (NonEmpty a) (NonEmpty b)))
-> This f a b
-> f (This f a b)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Either (NonEmpty a) (NonEmpty b)
 -> f (Either (NonEmpty a) (NonEmpty b)))
-> Maybe (Either (NonEmpty a) (NonEmpty b))
-> f (Maybe (Either (NonEmpty a) (NonEmpty b)))
forall a b. Prism (Maybe a) (Maybe b) a b
_Just

-- |
--
-- >>> over allThoseA (fmap reverse) (This [("abc", 'x'), ("def", 'y')] Nothing)
-- This [("abc",'x'),("def",'y')] Nothing
-- >>> over allThoseA (fmap reverse) (This [("abc", 'x'), ("def", 'y')] (Just (Left ("abc":|["def"]))))
-- This [("abc",'x'),("def",'y')] Just (Left ("cba" :| ["fed"]))
-- >>> over allThoseA (fmap reverse) (This [("abc", 'x'), ("def", 'y')] (Just (Right ('a':|"bcde"))))
-- This [("abc",'x'),("def",'y')] Just (Right ('a' :| "bcde"))
-- >>> Control.Lens.preview allThoseA (This [("abc", 'x'), ("def", 'y')] Nothing)
-- Nothing
-- >>> Control.Lens.preview allThoseA (This [("abc", 'x'), ("def", 'y')] (Just (Left ("abc":|["def"]))))
-- Just ("abc" :| ["def"])
-- >>> Control.Lens.preview allThoseA (This [("abc", 'x'), ("def", 'y')] (Just (Right ('a':|"bcde"))))
-- Nothing
allThoseA ::
  Traversal'
    (This f a b)
    (NonEmpty a)
allThoseA :: (NonEmpty a -> f (NonEmpty a)) -> This f a b -> f (This f a b)
allThoseA =
  (Either (NonEmpty a) (NonEmpty b)
 -> f (Either (NonEmpty a) (NonEmpty b)))
-> This f a b -> f (This f a b)
forall (f :: * -> *) a b.
Traversal' (This f a b) (Either (NonEmpty a) (NonEmpty b))
allThose ((Either (NonEmpty a) (NonEmpty b)
  -> f (Either (NonEmpty a) (NonEmpty b)))
 -> This f a b -> f (This f a b))
-> ((NonEmpty a -> f (NonEmpty a))
    -> Either (NonEmpty a) (NonEmpty b)
    -> f (Either (NonEmpty a) (NonEmpty b)))
-> (NonEmpty a -> f (NonEmpty a))
-> This f a b
-> f (This f a b)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (NonEmpty a -> f (NonEmpty a))
-> Either (NonEmpty a) (NonEmpty b)
-> f (Either (NonEmpty a) (NonEmpty b))
forall a c b. Prism (Either a c) (Either b c) a b
_Left

-- |
--
-- >>> over allThoseA' reverse (This [("abc", 'x'), ("def", 'y')] Nothing)
-- This [("abc",'x'),("def",'y')] Nothing
-- >>> over allThoseA' reverse (This [("abc", 'x'), ("def", 'y')] (Just (Left ("abc":|["def"]))))
-- This [("abc",'x'),("def",'y')] Just (Left ("cba" :| ["fed"]))
-- >>> over allThoseA' reverse (This [("abc", 'x'), ("def", 'y')] (Just (Right ('a':|"bcde"))))
-- This [("abc",'x'),("def",'y')] Just (Right ('a' :| "bcde"))
-- >>> Control.Lens.preview allThoseA' (This [("abc", 'x'), ("def", 'y')] Nothing)
-- Nothing
-- >>> Control.Lens.preview allThoseA' (This [("abc", 'x'), ("def", 'y')] (Just (Left ("abc":|["def"]))))
-- Just "abc"
-- >>> Control.Lens.preview allThoseA' (This [("abc", 'x'), ("def", 'y')] (Just (Right ('a':|"bcde"))))
-- Nothing
allThoseA' ::
  Traversable f =>
  Traversal'
    (This f a b)
    a
allThoseA' :: Traversal' (This f a b) a
allThoseA' =
  (NonEmpty a -> f (NonEmpty a)) -> This f a b -> f (This f a b)
forall (f :: * -> *) a b. Traversal' (This f a b) (NonEmpty a)
allThoseA ((NonEmpty a -> f (NonEmpty a)) -> This f a b -> f (This f a b))
-> ((a -> f a) -> NonEmpty a -> f (NonEmpty a))
-> (a -> f a)
-> This f a b
-> f (This f a b)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (a -> f a) -> NonEmpty a -> f (NonEmpty a)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse

-- |
--
-- >>> over allThoseBOr reverse (This [("abc", 'x'), ("def", 'y')] Nothing)
-- This [("abc",'x'),("def",'y')] Nothing
-- >>> over allThoseBOr reverse (This [("abc", 'x'), ("def", 'y')] (Just (Left ("abc":|["def"]))))
-- This [("abc",'x'),("def",'y')] Just (Left ("abc" :| ["def"]))
-- >>> over allThoseBOr reverse (This [("abc", 'x'), ("def", 'y')] (Just (Right ('a':|"bcde"))))
-- This [("abc",'x'),("def",'y')] Just (Right ('e' :| "dcba"))
-- >>> Control.Lens.preview allThoseBOr (This [("abc", 'x'), ("def", 'y')] Nothing)
-- Just ""
-- >>> Control.Lens.preview allThoseBOr (This [("abc", 'x'), ("def", 'y')] (Just (Left ("abc":|["def"]))))
-- Nothing
-- >>> Control.Lens.preview allThoseBOr (This [("abc", 'x'), ("def", 'y')] (Just (Right ('a':|"bcde"))))
-- Just "abcde"
allThoseAOr ::
  Traversal'
    (This f a b)
    [a]
allThoseAOr :: ([a] -> f [a]) -> This f a b -> f (This f a b)
allThoseAOr [a] -> f [a]
f (This f (a, b)
t Maybe (Either (NonEmpty a) (NonEmpty b))
Nothing) =
  f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This f (a, b)
t (Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b)
-> f (Maybe (Either (NonEmpty a) (NonEmpty b))) -> f (This f a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Maybe (Either (NonEmpty a) (NonEmpty b))
forall a. Maybe a
Nothing Maybe (Either (NonEmpty a) (NonEmpty b))
-> f [a] -> f (Maybe (Either (NonEmpty a) (NonEmpty b)))
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ [a] -> f [a]
f [])
allThoseAOr [a] -> f [a]
_ th :: This f a b
th@(This f (a, b)
_ (Just (Right NonEmpty b
_))) =
  This f a b -> f (This f a b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure This f a b
th
allThoseAOr [a] -> f [a]
f (This f (a, b)
t (Just (Left NonEmpty a
a))) =
  let lst :: [a] -> Maybe (NonEmpty a)
lst [] = Maybe (NonEmpty a)
forall a. Maybe a
Nothing
      lst (a
x:[a]
y) = NonEmpty a -> Maybe (NonEmpty a)
forall a. a -> Maybe a
Just (a
xa -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:|[a]
y)
  in  f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This f (a, b)
t (Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b)
-> f (Maybe (Either (NonEmpty a) (NonEmpty b))) -> f (This f a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((NonEmpty a -> Either (NonEmpty a) (NonEmpty b))
-> Maybe (NonEmpty a) -> Maybe (Either (NonEmpty a) (NonEmpty b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap NonEmpty a -> Either (NonEmpty a) (NonEmpty b)
forall a b. a -> Either a b
Left (Maybe (NonEmpty a) -> Maybe (Either (NonEmpty a) (NonEmpty b)))
-> ([a] -> Maybe (NonEmpty a))
-> [a]
-> Maybe (Either (NonEmpty a) (NonEmpty b))
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [a] -> Maybe (NonEmpty a)
forall a. [a] -> Maybe (NonEmpty a)
lst ([a] -> Maybe (Either (NonEmpty a) (NonEmpty b)))
-> f [a] -> f (Maybe (Either (NonEmpty a) (NonEmpty b)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [a] -> f [a]
f (NonEmpty a -> [a]
forall a. NonEmpty a -> [a]
NonEmpty.toList NonEmpty a
a))

-- |
--
-- >>> over allThoseB (fmap Data.Char.toUpper) (This [("abc", 'x'), ("def", 'y')] Nothing)
-- This [("abc",'x'),("def",'y')] Nothing
-- >>> over allThoseB (fmap Data.Char.toUpper) (This [("abc", 'x'), ("def", 'y')] (Just (Left ("abc":|["def"]))))
-- This [("abc",'x'),("def",'y')] Just (Left ("abc" :| ["def"]))
-- >>> over allThoseB (fmap Data.Char.toUpper) (This [("abc", 'x'), ("def", 'y')] (Just (Right ('a':|"bcde"))))
-- This [("abc",'x'),("def",'y')] Just (Right ('A' :| "BCDE"))
-- >>> Control.Lens.preview allThoseB (This [("abc", 'x'), ("def", 'y')] Nothing)
-- Nothing
-- >>> Control.Lens.preview allThoseB (This [("abc", 'x'), ("def", 'y')] (Just (Left ("abc":|["def"]))))
-- Nothing
-- >>> Control.Lens.preview allThoseB (This [("abc", 'x'), ("def", 'y')] (Just (Right ('a':|"bcde"))))
-- Just ('a' :| "bcde")
allThoseB ::
  Traversal'
    (This f a b)
    (NonEmpty b)
allThoseB :: (NonEmpty b -> f (NonEmpty b)) -> This f a b -> f (This f a b)
allThoseB =
  (Either (NonEmpty a) (NonEmpty b)
 -> f (Either (NonEmpty a) (NonEmpty b)))
-> This f a b -> f (This f a b)
forall (f :: * -> *) a b.
Traversal' (This f a b) (Either (NonEmpty a) (NonEmpty b))
allThose ((Either (NonEmpty a) (NonEmpty b)
  -> f (Either (NonEmpty a) (NonEmpty b)))
 -> This f a b -> f (This f a b))
-> ((NonEmpty b -> f (NonEmpty b))
    -> Either (NonEmpty a) (NonEmpty b)
    -> f (Either (NonEmpty a) (NonEmpty b)))
-> (NonEmpty b -> f (NonEmpty b))
-> This f a b
-> f (This f a b)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (NonEmpty b -> f (NonEmpty b))
-> Either (NonEmpty a) (NonEmpty b)
-> f (Either (NonEmpty a) (NonEmpty b))
forall c a b. Prism (Either c a) (Either c b) a b
_Right

-- |
--
-- >>> over allThoseB' Data.Char.toUpper (This [("abc", 'x'), ("def", 'y')] Nothing)
-- This [("abc",'x'),("def",'y')] Nothing
-- >>> over allThoseB' Data.Char.toUpper (This [("abc", 'x'), ("def", 'y')] (Just (Left ("abc":|["def"]))))
-- This [("abc",'x'),("def",'y')] Just (Left ("abc" :| ["def"]))
-- >>> over allThoseB' Data.Char.toUpper (This [("abc", 'x'), ("def", 'y')] (Just (Right ('a':|"bcde"))))
-- This [("abc",'x'),("def",'y')] Just (Right ('A' :| "BCDE"))
-- >>> Control.Lens.preview allThoseB' (This [("abc", 'x'), ("def", 'y')] Nothing)
-- Nothing
-- >>> Control.Lens.preview allThoseB' (This [("abc", 'x'), ("def", 'y')] (Just (Left ("abc":|["def"]))))
-- Nothing
-- >>> Control.Lens.preview allThoseB' (This [("abc", 'x'), ("def", 'y')] (Just (Right ('a':|"bcde"))))
-- Just 'a'
allThoseB' ::
  Traversable f =>
  Traversal'
    (This f a b)
    b
allThoseB' :: Traversal' (This f a b) b
allThoseB' =
  (NonEmpty b -> f (NonEmpty b)) -> This f a b -> f (This f a b)
forall (f :: * -> *) a b. Traversal' (This f a b) (NonEmpty b)
allThoseB ((NonEmpty b -> f (NonEmpty b)) -> This f a b -> f (This f a b))
-> ((b -> f b) -> NonEmpty b -> f (NonEmpty b))
-> (b -> f b)
-> This f a b
-> f (This f a b)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (b -> f b) -> NonEmpty b -> f (NonEmpty b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse

-- |
--
-- >>> over allThoseBOr reverse (This [("abc", 'x'), ("def", 'y')] Nothing)
-- This [("abc",'x'),("def",'y')] Nothing
-- >>> over allThoseBOr reverse (This [("abc", 'x'), ("def", 'y')] (Just (Left ("abc":|["def"]))))
-- This [("abc",'x'),("def",'y')] Just (Left ("abc" :| ["def"]))
-- >>> over allThoseBOr reverse (This [("abc", 'x'), ("def", 'y')] (Just (Right ('a':|"bcde"))))
-- This [("abc",'x'),("def",'y')] Just (Right ('e' :| "dcba"))
-- >>> Control.Lens.preview allThoseBOr (This [("abc", 'x'), ("def", 'y')] Nothing)
-- Just ""
-- >>> Control.Lens.preview allThoseBOr (This [("abc", 'x'), ("def", 'y')] (Just (Left ("abc":|["def"]))))
-- Nothing
-- >>> Control.Lens.preview allThoseBOr (This [("abc", 'x'), ("def", 'y')] (Just (Right ('a':|"bcde"))))
-- Just "abcde"
allThoseBOr ::
  Traversal'
    (This f a b)
    [b]
allThoseBOr :: ([b] -> f [b]) -> This f a b -> f (This f a b)
allThoseBOr [b] -> f [b]
f (This f (a, b)
t Maybe (Either (NonEmpty a) (NonEmpty b))
Nothing) =
  f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This f (a, b)
t (Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b)
-> f (Maybe (Either (NonEmpty a) (NonEmpty b))) -> f (This f a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Maybe (Either (NonEmpty a) (NonEmpty b))
forall a. Maybe a
Nothing Maybe (Either (NonEmpty a) (NonEmpty b))
-> f [b] -> f (Maybe (Either (NonEmpty a) (NonEmpty b)))
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ [b] -> f [b]
f [])
allThoseBOr [b] -> f [b]
f (This f (a, b)
t (Just (Right NonEmpty b
b))) =
  let lst :: [a] -> Maybe (NonEmpty a)
lst [] = Maybe (NonEmpty a)
forall a. Maybe a
Nothing
      lst (a
x:[a]
y) = NonEmpty a -> Maybe (NonEmpty a)
forall a. a -> Maybe a
Just (a
xa -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:|[a]
y)
  in  f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This f (a, b)
t (Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b)
-> f (Maybe (Either (NonEmpty a) (NonEmpty b))) -> f (This f a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((NonEmpty b -> Either (NonEmpty a) (NonEmpty b))
-> Maybe (NonEmpty b) -> Maybe (Either (NonEmpty a) (NonEmpty b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap NonEmpty b -> Either (NonEmpty a) (NonEmpty b)
forall a b. b -> Either a b
Right (Maybe (NonEmpty b) -> Maybe (Either (NonEmpty a) (NonEmpty b)))
-> ([b] -> Maybe (NonEmpty b))
-> [b]
-> Maybe (Either (NonEmpty a) (NonEmpty b))
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [b] -> Maybe (NonEmpty b)
forall a. [a] -> Maybe (NonEmpty a)
lst ([b] -> Maybe (Either (NonEmpty a) (NonEmpty b)))
-> f [b] -> f (Maybe (Either (NonEmpty a) (NonEmpty b)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [b] -> f [b]
f (NonEmpty b -> [b]
forall a. NonEmpty a -> [a]
NonEmpty.toList NonEmpty b
b))
allThoseBOr [b] -> f [b]
_ th :: This f a b
th@(This f (a, b)
_ (Just (Left NonEmpty a
_))) =
  This f a b -> f (This f a b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure This f a b
th

-- |
--
-- >>> over allTheseThoseA (fmap Data.Char.toUpper) (This [("abc", 'x'), ("def", 'y')] Nothing)
-- This [("ABC",'x'),("DEF",'y')] Nothing
-- >>> over allTheseThoseA (fmap Data.Char.toUpper) (This [("abc", 'x'), ("def", 'y')] (Just (Left ("abc":|["def"]))))
-- This [("ABC",'x'),("DEF",'y')] Just (Left ("ABC" :| ["DEF"]))
-- >>> over allTheseThoseA (fmap Data.Char.toUpper) (This [("abc", 'x'), ("def", 'y')] (Just (Right ('a':|"bcde"))))
-- This [("ABC",'x'),("DEF",'y')] Just (Right ('a' :| "bcde"))
-- >>> Control.Lens.preview allTheseThoseA (This [("abc", 'x'), ("def", 'y')] Nothing)
-- Just "abc"
-- >>> Control.Lens.preview allTheseThoseA (This [("abc", 'x'), ("def", 'y')] (Just (Left ("abc":|["def"]))))
-- Just "abc"
-- >>> Control.Lens.preview allTheseThoseA (This [("abc", 'x'), ("def", 'y')] (Just (Right ('a':|"bcde"))))
-- Just "abc"
allTheseThoseA ::
  Traversable f =>
  Traversal'
    (This f a b)
    a
allTheseThoseA :: Traversal' (This f a b) a
allTheseThoseA a -> f a
f (This f (a, b)
t Maybe (Either (NonEmpty a) (NonEmpty b))
r) =
  let th :: f (Maybe (Either (NonEmpty a) (NonEmpty b)))
th =
        case Maybe (Either (NonEmpty a) (NonEmpty b))
r of
          Maybe (Either (NonEmpty a) (NonEmpty b))
Nothing ->
            Maybe (Either (NonEmpty a) (NonEmpty b))
-> f (Maybe (Either (NonEmpty a) (NonEmpty b)))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (Either (NonEmpty a) (NonEmpty b))
forall a. Maybe a
Nothing
          Just (Left NonEmpty a
as) ->
            Either (NonEmpty a) (NonEmpty b)
-> Maybe (Either (NonEmpty a) (NonEmpty b))
forall a. a -> Maybe a
Just (Either (NonEmpty a) (NonEmpty b)
 -> Maybe (Either (NonEmpty a) (NonEmpty b)))
-> (NonEmpty a -> Either (NonEmpty a) (NonEmpty b))
-> NonEmpty a
-> Maybe (Either (NonEmpty a) (NonEmpty b))
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. NonEmpty a -> Either (NonEmpty a) (NonEmpty b)
forall a b. a -> Either a b
Left (NonEmpty a -> Maybe (Either (NonEmpty a) (NonEmpty b)))
-> f (NonEmpty a) -> f (Maybe (Either (NonEmpty a) (NonEmpty b)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> f a) -> NonEmpty a -> f (NonEmpty a)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f a
f NonEmpty a
as
          Just (Right NonEmpty b
bs) ->
            Maybe (Either (NonEmpty a) (NonEmpty b))
-> f (Maybe (Either (NonEmpty a) (NonEmpty b)))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either (NonEmpty a) (NonEmpty b)
-> Maybe (Either (NonEmpty a) (NonEmpty b))
forall a. a -> Maybe a
Just (NonEmpty b -> Either (NonEmpty a) (NonEmpty b)
forall a b. b -> Either a b
Right NonEmpty b
bs))
  in  f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This (f (a, b)
 -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b)
-> f (f (a, b))
-> f (Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
        ((a, b) -> f (a, b)) -> f (a, b) -> f (f (a, b))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (\(a
a, b
b) -> (, b
b) (a -> (a, b)) -> f a -> f (a, b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f a
f a
a) f (a, b)
t f (Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b)
-> f (Maybe (Either (NonEmpty a) (NonEmpty b))) -> f (This f a b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> f (Maybe (Either (NonEmpty a) (NonEmpty b)))
th

-- |
--
-- >>> over allTheseThoseB Data.Char.toUpper (This [("abc", 'x'), ("def", 'y')] Nothing)
-- This [("abc",'X'),("def",'Y')] Nothing
-- >>> over allTheseThoseB Data.Char.toUpper (This [("abc", 'x'), ("def", 'y')] (Just (Left ("abc":|["def"]))))
-- This [("abc",'X'),("def",'Y')] Just (Left ("abc" :| ["def"]))
-- >>> over allTheseThoseB Data.Char.toUpper (This [("abc", 'x'), ("def", 'y')] (Just (Right ('a':|"bcde"))))
-- This [("abc",'X'),("def",'Y')] Just (Right ('A' :| "BCDE"))
-- >>> Control.Lens.preview allTheseThoseB (This [("abc", 'x'), ("def", 'y')] Nothing)
-- Just 'x'
-- >>> Control.Lens.preview allTheseThoseB (This [("abc", 'x'), ("def", 'y')] (Just (Left ("abc":|["def"]))))
-- Just 'x'
-- >>> Control.Lens.preview allTheseThoseB (This [("abc", 'x'), ("def", 'y')] (Just (Right ('a':|"bcde"))))
-- Just 'x'
allTheseThoseB ::
  Traversable f =>
  Traversal'
    (This f a b)
    b
allTheseThoseB :: Traversal' (This f a b) b
allTheseThoseB b -> f b
f (This f (a, b)
t Maybe (Either (NonEmpty a) (NonEmpty b))
r) =
  let th :: f (Maybe (Either (NonEmpty a) (NonEmpty b)))
th =
        case Maybe (Either (NonEmpty a) (NonEmpty b))
r of
          Maybe (Either (NonEmpty a) (NonEmpty b))
Nothing ->
            Maybe (Either (NonEmpty a) (NonEmpty b))
-> f (Maybe (Either (NonEmpty a) (NonEmpty b)))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (Either (NonEmpty a) (NonEmpty b))
forall a. Maybe a
Nothing
          Just (Left NonEmpty a
as) ->
            Maybe (Either (NonEmpty a) (NonEmpty b))
-> f (Maybe (Either (NonEmpty a) (NonEmpty b)))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either (NonEmpty a) (NonEmpty b)
-> Maybe (Either (NonEmpty a) (NonEmpty b))
forall a. a -> Maybe a
Just (NonEmpty a -> Either (NonEmpty a) (NonEmpty b)
forall a b. a -> Either a b
Left NonEmpty a
as))
          Just (Right NonEmpty b
bs) ->
            Either (NonEmpty a) (NonEmpty b)
-> Maybe (Either (NonEmpty a) (NonEmpty b))
forall a. a -> Maybe a
Just (Either (NonEmpty a) (NonEmpty b)
 -> Maybe (Either (NonEmpty a) (NonEmpty b)))
-> (NonEmpty b -> Either (NonEmpty a) (NonEmpty b))
-> NonEmpty b
-> Maybe (Either (NonEmpty a) (NonEmpty b))
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. NonEmpty b -> Either (NonEmpty a) (NonEmpty b)
forall a b. b -> Either a b
Right (NonEmpty b -> Maybe (Either (NonEmpty a) (NonEmpty b)))
-> f (NonEmpty b) -> f (Maybe (Either (NonEmpty a) (NonEmpty b)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (b -> f b) -> NonEmpty b -> f (NonEmpty b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse b -> f b
f NonEmpty b
bs
  in  f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
forall (f :: * -> *) a b.
f (a, b) -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b
This (f (a, b)
 -> Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b)
-> f (f (a, b))
-> f (Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
        ((a, b) -> f (a, b)) -> f (a, b) -> f (f (a, b))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (\(a
a, b
b) -> (a
a ,) (b -> (a, b)) -> f b -> f (a, b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> b -> f b
f b
b) f (a, b)
t f (Maybe (Either (NonEmpty a) (NonEmpty b)) -> This f a b)
-> f (Maybe (Either (NonEmpty a) (NonEmpty b))) -> f (This f a b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> f (Maybe (Either (NonEmpty a) (NonEmpty b)))
th