{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
-- |
-- Module       : Data.Wedge.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 'Wedge' datatype.
--
module Data.Wedge.Microlens
( -- * Traversals
  here
, there
, _Nowhere
, _Here
, _There
) where


import Lens.Micro

import Data.Wedge

-- | A 'Control.Lens.Traversal' of the 'Here' case of a 'Wedge',
-- suitable for use with "Control.Lens".
--
-- >>> over here show (Here 1)
-- Here "1"
--
-- >>> over here show (There 'a')
-- There 'a'
--
here :: Traversal' (Wedge a b) a
here :: (a -> f a) -> Wedge a b -> f (Wedge a b)
here a -> f a
f = \case
  Wedge a b
Nowhere -> Wedge a b -> f (Wedge a b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Wedge a b
forall a b. Wedge a b
Nowhere
  Here a
a -> a -> Wedge a b
forall a b. a -> Wedge a b
Here (a -> Wedge a b) -> f a -> f (Wedge a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f a
f a
a
  There b
b -> Wedge a b -> f (Wedge a b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (b -> Wedge a b
forall a b. b -> Wedge a b
There b
b)

-- | A 'Control.Lens.Traversal' of the 'There' case of a 'Wedge',
-- suitable for use with "Control.Lens".
--
-- >>> over there show (Here 1)
-- Here 1
--
-- >>> over there show (There 'a')
-- There "'a'"
--
there :: Traversal' (Wedge a b) b
there :: (b -> f b) -> Wedge a b -> f (Wedge a b)
there b -> f b
f = \case
  Wedge a b
Nowhere -> Wedge a b -> f (Wedge a b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Wedge a b
forall a b. Wedge a b
Nowhere
  Here a
a -> Wedge a b -> f (Wedge a b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> Wedge a b
forall a b. a -> Wedge a b
Here a
a)
  There b
b -> b -> Wedge a b
forall a b. b -> Wedge a b
There (b -> Wedge a b) -> f b -> f (Wedge a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> b -> f b
f b
b

-- | A 'Traversal'' selecting the 'Nowhere' constructor.
--
-- /Note:/ cannot change type.
--
_Nowhere :: Traversal' (Wedge a b) ()
_Nowhere :: (() -> f ()) -> Wedge a b -> f (Wedge a b)
_Nowhere () -> f ()
f = \case
  Wedge a b
Nowhere -> Wedge a b
forall a b. Wedge a b
Nowhere Wedge a b -> f () -> f (Wedge a b)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ () -> f ()
f ()
  Here a
a -> Wedge a b -> f (Wedge a b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> Wedge a b
forall a b. a -> Wedge a b
Here a
a)
  There b
b -> Wedge a b -> f (Wedge a b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (b -> Wedge a b
forall a b. b -> Wedge a b
There b
b)

-- | A 'Traversal'' selecting the 'Here' constructor.
--
-- /Note:/ cannot change type.
--
_Here :: Traversal (Wedge a b) (Wedge c b) a c
_Here :: (a -> f c) -> Wedge a b -> f (Wedge c b)
_Here a -> f c
f = \case
  Here a
a -> c -> Wedge c b
forall a b. a -> Wedge a b
Here (c -> Wedge c b) -> f c -> f (Wedge c b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f c
f a
a
  There b
b -> Wedge c b -> f (Wedge c b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (b -> Wedge c b
forall a b. b -> Wedge a b
There b
b)
  Wedge a b
Nowhere -> Wedge c b -> f (Wedge c b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Wedge c b
forall a b. Wedge a b
Nowhere

-- | A 'Traversal'' selecting the 'There' constructor.
--
-- /Note:/ cannot change type.
--
_There :: Traversal (Wedge a b) (Wedge a d) b d
_There :: (b -> f d) -> Wedge a b -> f (Wedge a d)
_There b -> f d
f = \case
  There b
b -> d -> Wedge a d
forall a b. b -> Wedge a b
There (d -> Wedge a d) -> f d -> f (Wedge a d)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> b -> f d
f b
b
  Here a
a -> Wedge a d -> f (Wedge a d)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> Wedge a d
forall a b. a -> Wedge a b
Here a
a)
  Wedge a b
Nowhere -> Wedge a d -> f (Wedge a d)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Wedge a d
forall a b. Wedge a b
Nowhere