{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
-- |
-- Module       : Data.Can.Lens
-- Copyright 	: (c) 2020 Emily Pillmore
-- License	: BSD-style
--
-- Maintainer	: Emily Pillmore <emilypi@cohomolo.gy>
-- Stability	: Experimental
-- Portability	: FlexibleInstances, MPTC, Type Families, UndecideableInstances
--
-- 'Prism's and 'Traversal's for the 'Can' datatype.
--
module Data.Can.Lens
( -- * Prisms
  _Non
, _One
, _Eno
, _Two
  -- * Traversals
, oneing
, enoing
, twoed
, twoing
) where


import Control.Lens

import Data.Can

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

-- | A 'Control.Lens.Traversal' of the first parameter, suitable for use
-- with "Control.Lens".
--
oneing :: Traversal (Can a c) (Can b c) a b
oneing :: (a -> f b) -> Can a c -> f (Can b c)
oneing a -> f b
f = \case
  Can a c
Non -> Can b c -> f (Can b c)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Can b c
forall a b. Can a b
Non
  One a
a -> b -> Can b c
forall a b. a -> Can a b
One (b -> Can b c) -> f b -> f (Can b c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
a
  Eno c
c -> Can b c -> f (Can b c)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (c -> Can b c
forall a b. b -> Can a b
Eno c
c)
  Two a
a c
c -> (b -> c -> Can b c) -> c -> b -> Can b c
forall a b c. (a -> b -> c) -> b -> a -> c
flip b -> c -> Can b c
forall a b. a -> b -> Can a b
Two c
c (b -> Can b c) -> f b -> f (Can b c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
a

-- | A 'Control.Lens.Traversal' of the second parameter, suitable for use
-- with "Control.Lens".
--
enoing :: Traversal (Can a b) (Can a c) b c
enoing :: (b -> f c) -> Can a b -> f (Can a c)
enoing b -> f c
f = \case
  Can a b
Non -> Can a c -> f (Can a c)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Can a c
forall a b. Can a b
Non
  One a
a -> Can a c -> f (Can a c)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> Can a c
forall a b. a -> Can a b
One a
a)
  Eno b
b -> c -> Can a c
forall a b. b -> Can a b
Eno (c -> Can a c) -> f c -> f (Can a c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> b -> f c
f b
b
  Two a
a b
b -> a -> c -> Can a c
forall a b. a -> b -> Can a b
Two a
a (c -> Can a c) -> f c -> f (Can a c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> b -> f c
f b
b

-- | A 'Control.Lens.Traversal' of the pair, suitable for use
-- with "Control.Lens".
--
twoed :: Traversal' (Can a b) (a,b)
twoed :: ((a, b) -> f (a, b)) -> Can a b -> f (Can a b)
twoed (a, b) -> f (a, b)
f = \case
  Can a b
Non -> Can a b -> f (Can a b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Can a b
forall a b. Can a b
Non
  One a
a -> Can a b -> f (Can a b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> Can a b
forall a b. a -> Can a b
One a
a)
  Eno b
b -> Can a b -> f (Can a b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (b -> Can a b
forall a b. b -> Can a b
Eno b
b)
  Two a
a b
b -> (a -> b -> Can a b) -> (a, b) -> Can a b
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry a -> b -> Can a b
forall a b. a -> b -> Can a b
Two ((a, b) -> Can a b) -> f (a, b) -> f (Can a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a, b) -> f (a, b)
f (a
a,b
b)

-- | A 'Control.Lens.Traversal' of the pair ala 'both', suitable for use
-- with "Control.Lens".
--
twoing :: Traversal (Can a a) (Can b b) a b
twoing :: (a -> f b) -> Can a a -> f (Can b b)
twoing a -> f b
f = \case
  Can a a
Non -> Can b b -> f (Can b b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Can b b
forall a b. Can a b
Non
  One a
a -> b -> Can b b
forall a b. a -> Can a b
One (b -> Can b b) -> f b -> f (Can b b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
a
  Eno a
a -> b -> Can b b
forall a b. b -> Can a b
Eno (b -> Can b b) -> f b -> f (Can b b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
a
  Two a
a a
b -> b -> b -> Can b b
forall a b. a -> b -> Can a b
Two (b -> b -> Can b b) -> f b -> f (b -> Can b b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
a f (b -> Can b b) -> f b -> f (Can b b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a -> f b
f a
b

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

-- | A 'Control.Lens.Prism'' selecting the 'Non' constructor.
--
-- /Note:/ cannot change type.
--
_Non :: Prism' (Can a b) ()
_Non :: p () (f ()) -> p (Can a b) (f (Can a b))
_Non = (() -> Can a b)
-> (Can a b -> Either (Can a b) ())
-> Prism (Can a b) (Can a b) () ()
forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism (Can a b -> () -> Can a b
forall a b. a -> b -> a
const Can a b
forall a b. Can a b
Non) ((Can a b -> Either (Can a b) ())
 -> Prism (Can a b) (Can a b) () ())
-> (Can a b -> Either (Can a b) ())
-> Prism (Can a b) (Can a b) () ()
forall a b. (a -> b) -> a -> b
$ \case
  Can a b
Non -> () -> Either (Can a b) ()
forall a b. b -> Either a b
Right ()
  One a
a -> Can a b -> Either (Can a b) ()
forall a b. a -> Either a b
Left (a -> Can a b
forall a b. a -> Can a b
One a
a)
  Eno b
b -> Can a b -> Either (Can a b) ()
forall a b. a -> Either a b
Left (b -> Can a b
forall a b. b -> Can a b
Eno b
b)
  Two a
a b
b -> Can a b -> Either (Can a b) ()
forall a b. a -> Either a b
Left (a -> b -> Can a b
forall a b. a -> b -> Can a b
Two a
a b
b)

-- | A 'Control.Lens.Prism'' selecting the 'One' constructor.
--
-- /Note:/ cannot change type.
--
_One :: Prism' (Can a b) a
_One :: p a (f a) -> p (Can a b) (f (Can a b))
_One = (a -> Can a b)
-> (Can a b -> Either (Can a b) a) -> Prism (Can a b) (Can a b) a a
forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism a -> Can a b
forall a b. a -> Can a b
One ((Can a b -> Either (Can a b) a) -> Prism (Can a b) (Can a b) a a)
-> (Can a b -> Either (Can a b) a) -> Prism (Can a b) (Can a b) a a
forall a b. (a -> b) -> a -> b
$ \case
  Can a b
Non -> Can a b -> Either (Can a b) a
forall a b. a -> Either a b
Left Can a b
forall a b. Can a b
Non
  One a
a -> a -> Either (Can a b) a
forall a b. b -> Either a b
Right a
a
  Eno b
b -> Can a b -> Either (Can a b) a
forall a b. a -> Either a b
Left (b -> Can a b
forall a b. b -> Can a b
Eno b
b)
  Two a
a b
b -> Can a b -> Either (Can a b) a
forall a b. a -> Either a b
Left (a -> b -> Can a b
forall a b. a -> b -> Can a b
Two a
a b
b)

-- | A 'Control.Lens.Prism'' selecting the 'Eno' constructor.
--
-- /Note:/ cannot change type.
--
_Eno :: Prism' (Can a b) b
_Eno :: p b (f b) -> p (Can a b) (f (Can a b))
_Eno = (b -> Can a b)
-> (Can a b -> Either (Can a b) b) -> Prism (Can a b) (Can a b) b b
forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism b -> Can a b
forall a b. b -> Can a b
Eno ((Can a b -> Either (Can a b) b) -> Prism (Can a b) (Can a b) b b)
-> (Can a b -> Either (Can a b) b) -> Prism (Can a b) (Can a b) b b
forall a b. (a -> b) -> a -> b
$ \case
  Can a b
Non -> Can a b -> Either (Can a b) b
forall a b. a -> Either a b
Left Can a b
forall a b. Can a b
Non
  One a
a -> Can a b -> Either (Can a b) b
forall a b. a -> Either a b
Left (a -> Can a b
forall a b. a -> Can a b
One a
a)
  Eno b
b -> b -> Either (Can a b) b
forall a b. b -> Either a b
Right b
b
  Two a
a b
b -> Can a b -> Either (Can a b) b
forall a b. a -> Either a b
Left (a -> b -> Can a b
forall a b. a -> b -> Can a b
Two a
a b
b)

-- | A 'Control.Lens.Prism'' selecting the 'Two' constructor.
--
-- /Note:/ cannot change type.
--
_Two :: Prism' (Can a b) (a,b)
_Two :: p (a, b) (f (a, b)) -> p (Can a b) (f (Can a b))
_Two = ((a, b) -> Can a b)
-> (Can a b -> Either (Can a b) (a, b))
-> Prism (Can a b) (Can 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 -> Can a b) -> (a, b) -> Can a b
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry a -> b -> Can a b
forall a b. a -> b -> Can a b
Two) ((Can a b -> Either (Can a b) (a, b))
 -> Prism (Can a b) (Can a b) (a, b) (a, b))
-> (Can a b -> Either (Can a b) (a, b))
-> Prism (Can a b) (Can a b) (a, b) (a, b)
forall a b. (a -> b) -> a -> b
$ \case
  Can a b
Non -> Can a b -> Either (Can a b) (a, b)
forall a b. a -> Either a b
Left Can a b
forall a b. Can a b
Non
  One a
a -> Can a b -> Either (Can a b) (a, b)
forall a b. a -> Either a b
Left (a -> Can a b
forall a b. a -> Can a b
One a
a)
  Eno b
b -> Can a b -> Either (Can a b) (a, b)
forall a b. a -> Either a b
Left (b -> Can a b
forall a b. b -> Can a b
Eno b
b)
  Two a
a b
b -> (a, b) -> Either (Can a b) (a, b)
forall a b. b -> Either a b
Right (a
a,b
b)

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

instance Swapped Can where
  swapped :: p (Can b a) (f (Can d c)) -> p (Can a b) (f (Can c d))
swapped = (Can a b -> Can b a)
-> (Can d c -> Can c d)
-> Iso (Can a b) (Can c d) (Can b a) (Can d c)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso Can a b -> Can b a
forall a b. Can a b -> Can b a
swapCan Can d c -> Can c d
forall a b. Can a b -> Can b a
swapCan

instance (a ~ a', b ~ b') => Each (Can a a') (Can b b') a b where
  each :: (a -> f b) -> Can a a' -> f (Can b b')
each a -> f b
_ Can a a'
Non = Can b b' -> f (Can b b')
forall (f :: * -> *) a. Applicative f => a -> f a
pure Can b b'
forall a b. Can a b
Non
  each a -> f b
f (One a
a) = b -> Can b b'
forall a b. a -> Can a b
One (b -> Can b b') -> f b -> f (Can b b')
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
a
  each a -> f b
f (Eno a'
a) = b -> Can b b
forall a b. b -> Can a b
Eno (b -> Can b b) -> f b -> f (Can b b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
a'
a
  each a -> f b
f (Two a
a a'
b) = b -> b -> Can b b
forall a b. a -> b -> Can a b
Two (b -> b -> Can b b) -> f b -> f (b -> Can b b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
a f (b -> Can b b) -> f b -> f (Can b b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a -> f b
f a
a'
b