{-# LANGUAGE CPP                   #-}
{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE Trustworthy           #-}
{-# LANGUAGE TypeFamilies          #-}
{-# LANGUAGE UndecidableInstances  #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Data.These.Lens (
    -- * Traversals
    here, there,

    -- * Prisms
    _This, _That, _These,
    ) where

import Control.Applicative (pure, (<$>), (<*>))
import Prelude             (Either (..), flip, uncurry, ($), (.))

import Control.Lens (Prism', Traversal, prism)
import Data.These

#if !MIN_VERSION_lens(5,0,0)
import Control.Lens           (Each (..), Swapped (..), iso)
import Data.These.Combinators (swapThese)
#endif

-- $setup
-- >>> import Data.These
-- >>> import Control.Lens
-- >>> import Prelude (show)

-------------------------------------------------------------------------------
-- Traversals
-------------------------------------------------------------------------------

-- | A 'Control.Lens.Traversal' of the first half of a 'These', suitable for use with "Control.Lens".
--
-- >>> over here show (That 1)
-- That 1
--
-- >>> over here show (These 'a' 2)
-- These "'a'" 2
--
here :: Traversal (These a c) (These b c) a b
here :: (a -> f b) -> These a c -> f (These b c)
here a -> f b
f (This a
x) = b -> These b c
forall a b. a -> These a b
This (b -> These b c) -> f b -> f (These b c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
x
here a -> f b
f (These a
x c
y) = (b -> c -> These b c) -> c -> b -> These b c
forall a b c. (a -> b -> c) -> b -> a -> c
flip b -> c -> These b c
forall a b. a -> b -> These a b
These c
y (b -> These b c) -> f b -> f (These b c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
x
here a -> f b
_ (That c
x) = These b c -> f (These b c)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (c -> These b c
forall a b. b -> These a b
That c
x)

-- | A 'Control.Lens.Traversal' of the second half of a 'These', suitable for use with "Control.Lens".
--
-- @
-- 'there' :: 'Control.Lens.Traversal' ('These' t b) ('These' t b) a b
-- @
--
-- >>> over there show (That 1)
-- That "1"
--
-- >>> over there show (These 'a' 2)
-- These 'a' "2"
--
there :: Traversal (These c a) (These c b) a b
there :: (a -> f b) -> These c a -> f (These c b)
there a -> f b
_ (This c
x) = These c b -> f (These c b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (c -> These c b
forall a b. a -> These a b
This c
x)
there a -> f b
f (These c
x a
y) = c -> b -> These c b
forall a b. a -> b -> These a b
These c
x (b -> These c b) -> f b -> f (These c b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
y
there a -> f b
f (That a
x) = b -> These c b
forall a b. b -> These a b
That (b -> These c b) -> f b -> f (These c b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
x

-------------------------------------------------------------------------------
-- Prisms
-------------------------------------------------------------------------------

-- | A 'Control.Lens.Prism'' selecting the 'This' constructor.
--
-- /Note:/ cannot change type.
_This :: Prism' (These a b) a
_This :: p a (f a) -> p (These a b) (f (These a b))
_This = (a -> These a b)
-> (These a b -> Either (These a b) a)
-> Prism (These a b) (These a b) a a
forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism a -> These a b
forall a b. a -> These a b
This ((a -> Either (These a b) a)
-> (b -> Either (These a b) a)
-> (a -> b -> Either (These a b) a)
-> These a b
-> Either (These a b) a
forall a c b.
(a -> c) -> (b -> c) -> (a -> b -> c) -> These a b -> c
these a -> Either (These a b) a
forall a b. b -> Either a b
Right (These a b -> Either (These a b) a
forall a b. a -> Either a b
Left (These a b -> Either (These a b) a)
-> (b -> These a b) -> b -> Either (These a b) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> These a b
forall a b. b -> These a b
That) (\a
x b
y -> These a b -> Either (These a b) a
forall a b. a -> Either a b
Left (These a b -> Either (These a b) a)
-> These a b -> Either (These a b) a
forall a b. (a -> b) -> a -> b
$ a -> b -> These a b
forall a b. a -> b -> These a b
These a
x b
y))

-- | A 'Control.Lens.Prism'' selecting the 'That' constructor.
--
-- /Note:/ cannot change type.
_That :: Prism' (These a b) b
_That :: p b (f b) -> p (These a b) (f (These a b))
_That = (b -> These a b)
-> (These a b -> Either (These a b) b)
-> Prism (These a b) (These a b) b b
forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism b -> These a b
forall a b. b -> These a b
That ((a -> Either (These a b) b)
-> (b -> Either (These a b) b)
-> (a -> b -> Either (These a b) b)
-> These a b
-> Either (These a b) b
forall a c b.
(a -> c) -> (b -> c) -> (a -> b -> c) -> These a b -> c
these (These a b -> Either (These a b) b
forall a b. a -> Either a b
Left (These a b -> Either (These a b) b)
-> (a -> These a b) -> a -> Either (These a b) b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> These a b
forall a b. a -> These a b
This) b -> Either (These a b) b
forall a b. b -> Either a b
Right (\a
x b
y -> These a b -> Either (These a b) b
forall a b. a -> Either a b
Left (These a b -> Either (These a b) b)
-> These a b -> Either (These a b) b
forall a b. (a -> b) -> a -> b
$ a -> b -> These a b
forall a b. a -> b -> These a b
These a
x b
y))

-- | A 'Control.Lens.Prism'' selecting the 'These' constructor. 'These' names are ridiculous!
--
-- /Note:/ cannot change type.
_These :: Prism' (These a b) (a, b)
_These :: p (a, b) (f (a, b)) -> p (These a b) (f (These a b))
_These = ((a, b) -> These a b)
-> (These a b -> Either (These a b) (a, b))
-> Prism (These a b) (These a b) (a, b) (a, b)
forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism ((a -> b -> These a b) -> (a, b) -> These a b
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry a -> b -> These a b
forall a b. a -> b -> These a b
These) ((a -> Either (These a b) (a, b))
-> (b -> Either (These a b) (a, b))
-> (a -> b -> Either (These a b) (a, b))
-> These a b
-> Either (These a b) (a, b)
forall a c b.
(a -> c) -> (b -> c) -> (a -> b -> c) -> These a b -> c
these (These a b -> Either (These a b) (a, b)
forall a b. a -> Either a b
Left (These a b -> Either (These a b) (a, b))
-> (a -> These a b) -> a -> Either (These a b) (a, b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> These a b
forall a b. a -> These a b
This) (These a b -> Either (These a b) (a, b)
forall a b. a -> Either a b
Left (These a b -> Either (These a b) (a, b))
-> (b -> These a b) -> b -> Either (These a b) (a, b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> These a b
forall a b. b -> These a b
That) (\a
x b
y -> (a, b) -> Either (These a b) (a, b)
forall a b. b -> Either a b
Right (a
x, b
y)))

-------------------------------------------------------------------------------
-- Orphans
-------------------------------------------------------------------------------

#if !MIN_VERSION_lens(5,0,0)
instance Swapped These where
    swapped = iso swapThese swapThese

-- | @since 1.0.1
instance (a ~ a', b ~ b') => Each (These a a') (These b b') a b where
    each f (This a)    = This <$> f a
    each f (That b)    = That <$> f b
    each f (These a b) = These <$> f a <*> f b
#endif