{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
-- |
-- Module       : Data.Can.Microlens
-- 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.Microlens
( -- * Traversals
  _Non
, _One
, _Eno
, _Two
, oneing
, enoing
, twoed
, twoing
) where


import Lens.Micro

import Data.Can


-- | A '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 '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 '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 '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

-- | A 'Traversal'' selecting the 'Non' constructor.
--
-- /Note:/ cannot change type.
--
_Non :: Traversal' (Can a b) ()
_Non :: (() -> f ()) -> Can a b -> f (Can a b)
_Non () -> f ()
f = \case
  Can a b
Non -> Can a b
forall a b. Can a b
Non Can a b -> f () -> f (Can a b)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ () -> f ()
f ()
  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 -> Can a b -> f (Can a b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> b -> Can a b
forall a b. a -> b -> Can a b
Two a
a b
b)

-- | A 'Traversal'' selecting the 'One' constructor.
--
-- /Note:/ cannot change type.
--
_One :: Traversal' (Can a b) a
_One :: (a -> f a) -> Can a b -> f (Can a b)
_One a -> f a
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 -> a -> Can a b
forall a b. a -> Can a b
One (a -> Can a b) -> f a -> f (Can a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f a
f 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 -> Can a b -> f (Can a b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> b -> Can a b
forall a b. a -> b -> Can a b
Two a
a b
b)

-- | A 'Traversal'' selecting the 'Eno' constructor.
--
-- /Note:/ cannot change type.
--
_Eno :: Traversal' (Can a b) b
_Eno :: (b -> f b) -> Can a b -> f (Can a b)
_Eno b -> f 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 -> b -> Can a b
forall a b. b -> Can a b
Eno (b -> Can a b) -> f b -> f (Can a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> b -> f b
f b
b
  Two a
a b
b -> Can a b -> f (Can a b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> b -> Can a b
forall a b. a -> b -> Can a b
Two a
a b
b)

-- | A 'Traversal'' selecting the 'Two' constructor.
--
-- /Note:/ cannot change type.
--
_Two :: Traversal' (Can a b) (a,b)
_Two :: ((a, b) -> f (a, b)) -> Can a b -> f (Can a b)
_Two (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)