```{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DeriveFunctor  #-}

-- | This module is a simplified version of
-- which is copyrighted by Emily Pillmore and originally pulished using
--
-- copyright: Emily Pillmore 2020-2021, iohk 2021
--
module Data.Wedge where

import           Data.Bifoldable
import           Data.Bifunctor
import           Data.Bitraversable

-- | A wedge product
--
data Wedge a b =
Nowhere
| Here a
| There b
deriving (Wedge a b -> Wedge a b -> Bool
(Wedge a b -> Wedge a b -> Bool)
-> (Wedge a b -> Wedge a b -> Bool) -> Eq (Wedge a b)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall a b. (Eq a, Eq b) => Wedge a b -> Wedge a b -> Bool
/= :: Wedge a b -> Wedge a b -> Bool
\$c/= :: forall a b. (Eq a, Eq b) => Wedge a b -> Wedge a b -> Bool
== :: Wedge a b -> Wedge a b -> Bool
\$c== :: forall a b. (Eq a, Eq b) => Wedge a b -> Wedge a b -> Bool
Eq, Eq (Wedge a b)
Eq (Wedge a b)
-> (Wedge a b -> Wedge a b -> Ordering)
-> (Wedge a b -> Wedge a b -> Bool)
-> (Wedge a b -> Wedge a b -> Bool)
-> (Wedge a b -> Wedge a b -> Bool)
-> (Wedge a b -> Wedge a b -> Bool)
-> (Wedge a b -> Wedge a b -> Wedge a b)
-> (Wedge a b -> Wedge a b -> Wedge a b)
-> Ord (Wedge a b)
Wedge a b -> Wedge a b -> Bool
Wedge a b -> Wedge a b -> Ordering
Wedge a b -> Wedge a b -> Wedge a b
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a b. (Ord a, Ord b) => Eq (Wedge a b)
forall a b. (Ord a, Ord b) => Wedge a b -> Wedge a b -> Bool
forall a b. (Ord a, Ord b) => Wedge a b -> Wedge a b -> Ordering
forall a b. (Ord a, Ord b) => Wedge a b -> Wedge a b -> Wedge a b
min :: Wedge a b -> Wedge a b -> Wedge a b
\$cmin :: forall a b. (Ord a, Ord b) => Wedge a b -> Wedge a b -> Wedge a b
max :: Wedge a b -> Wedge a b -> Wedge a b
\$cmax :: forall a b. (Ord a, Ord b) => Wedge a b -> Wedge a b -> Wedge a b
>= :: Wedge a b -> Wedge a b -> Bool
\$c>= :: forall a b. (Ord a, Ord b) => Wedge a b -> Wedge a b -> Bool
> :: Wedge a b -> Wedge a b -> Bool
\$c> :: forall a b. (Ord a, Ord b) => Wedge a b -> Wedge a b -> Bool
<= :: Wedge a b -> Wedge a b -> Bool
\$c<= :: forall a b. (Ord a, Ord b) => Wedge a b -> Wedge a b -> Bool
< :: Wedge a b -> Wedge a b -> Bool
\$c< :: forall a b. (Ord a, Ord b) => Wedge a b -> Wedge a b -> Bool
compare :: Wedge a b -> Wedge a b -> Ordering
\$ccompare :: forall a b. (Ord a, Ord b) => Wedge a b -> Wedge a b -> Ordering
\$cp1Ord :: forall a b. (Ord a, Ord b) => Eq (Wedge a b)
Ord, Wedge a a -> Bool
(a -> m) -> Wedge a a -> m
(a -> b -> b) -> b -> Wedge a a -> b
(forall m. Monoid m => Wedge a m -> m)
-> (forall m a. Monoid m => (a -> m) -> Wedge a a -> m)
-> (forall m a. Monoid m => (a -> m) -> Wedge a a -> m)
-> (forall a b. (a -> b -> b) -> b -> Wedge a a -> b)
-> (forall a b. (a -> b -> b) -> b -> Wedge a a -> b)
-> (forall b a. (b -> a -> b) -> b -> Wedge a a -> b)
-> (forall b a. (b -> a -> b) -> b -> Wedge a a -> b)
-> (forall a. (a -> a -> a) -> Wedge a a -> a)
-> (forall a. (a -> a -> a) -> Wedge a a -> a)
-> (forall a. Wedge a a -> [a])
-> (forall a. Wedge a a -> Bool)
-> (forall a. Wedge a a -> Int)
-> (forall a. Eq a => a -> Wedge a a -> Bool)
-> (forall a. Ord a => Wedge a a -> a)
-> (forall a. Ord a => Wedge a a -> a)
-> (forall a. Num a => Wedge a a -> a)
-> (forall a. Num a => Wedge a a -> a)
-> Foldable (Wedge a)
forall a. Eq a => a -> Wedge a a -> Bool
forall a. Num a => Wedge a a -> a
forall a. Ord a => Wedge a a -> a
forall m. Monoid m => Wedge a m -> m
forall a. Wedge a a -> Bool
forall a. Wedge a a -> Int
forall a. Wedge a a -> [a]
forall a. (a -> a -> a) -> Wedge a a -> a
forall a a. Eq a => a -> Wedge a a -> Bool
forall a a. Num a => Wedge a a -> a
forall a a. Ord a => Wedge a a -> a
forall m a. Monoid m => (a -> m) -> Wedge a a -> m
forall a m. Monoid m => Wedge a m -> m
forall a a. Wedge a a -> Bool
forall a a. Wedge a a -> Int
forall a a. Wedge a a -> [a]
forall b a. (b -> a -> b) -> b -> Wedge a a -> b
forall a b. (a -> b -> b) -> b -> Wedge a a -> b
forall a a. (a -> a -> a) -> Wedge a a -> a
forall a m a. Monoid m => (a -> m) -> Wedge a a -> m
forall a b a. (b -> a -> b) -> b -> Wedge a a -> b
forall a a b. (a -> b -> b) -> b -> Wedge a a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: Wedge a a -> a
\$cproduct :: forall a a. Num a => Wedge a a -> a
sum :: Wedge a a -> a
\$csum :: forall a a. Num a => Wedge a a -> a
minimum :: Wedge a a -> a
\$cminimum :: forall a a. Ord a => Wedge a a -> a
maximum :: Wedge a a -> a
\$cmaximum :: forall a a. Ord a => Wedge a a -> a
elem :: a -> Wedge a a -> Bool
\$celem :: forall a a. Eq a => a -> Wedge a a -> Bool
length :: Wedge a a -> Int
\$clength :: forall a a. Wedge a a -> Int
null :: Wedge a a -> Bool
\$cnull :: forall a a. Wedge a a -> Bool
toList :: Wedge a a -> [a]
\$ctoList :: forall a a. Wedge a a -> [a]
foldl1 :: (a -> a -> a) -> Wedge a a -> a
\$cfoldl1 :: forall a a. (a -> a -> a) -> Wedge a a -> a
foldr1 :: (a -> a -> a) -> Wedge a a -> a
\$cfoldr1 :: forall a a. (a -> a -> a) -> Wedge a a -> a
foldl' :: (b -> a -> b) -> b -> Wedge a a -> b
\$cfoldl' :: forall a b a. (b -> a -> b) -> b -> Wedge a a -> b
foldl :: (b -> a -> b) -> b -> Wedge a a -> b
\$cfoldl :: forall a b a. (b -> a -> b) -> b -> Wedge a a -> b
foldr' :: (a -> b -> b) -> b -> Wedge a a -> b
\$cfoldr' :: forall a a b. (a -> b -> b) -> b -> Wedge a a -> b
foldr :: (a -> b -> b) -> b -> Wedge a a -> b
\$cfoldr :: forall a a b. (a -> b -> b) -> b -> Wedge a a -> b
foldMap' :: (a -> m) -> Wedge a a -> m
\$cfoldMap' :: forall a m a. Monoid m => (a -> m) -> Wedge a a -> m
foldMap :: (a -> m) -> Wedge a a -> m
\$cfoldMap :: forall a m a. Monoid m => (a -> m) -> Wedge a a -> m
fold :: Wedge a m -> m
\$cfold :: forall a m. Monoid m => Wedge a m -> m
Foldable, a -> Wedge a b -> Wedge a a
(a -> b) -> Wedge a a -> Wedge a b
(forall a b. (a -> b) -> Wedge a a -> Wedge a b)
-> (forall a b. a -> Wedge a b -> Wedge a a) -> Functor (Wedge a)
forall a b. a -> Wedge a b -> Wedge a a
forall a b. (a -> b) -> Wedge a a -> Wedge a b
forall a a b. a -> Wedge a b -> Wedge a a
forall a a b. (a -> b) -> Wedge a a -> Wedge a b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<\$ :: a -> Wedge a b -> Wedge a a
\$c<\$ :: forall a a b. a -> Wedge a b -> Wedge a a
fmap :: (a -> b) -> Wedge a a -> Wedge a b
\$cfmap :: forall a a b. (a -> b) -> Wedge a a -> Wedge a b
Functor, Int -> Wedge a b -> ShowS
[Wedge a b] -> ShowS
Wedge a b -> String
(Int -> Wedge a b -> ShowS)
-> (Wedge a b -> String)
-> ([Wedge a b] -> ShowS)
-> Show (Wedge a b)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall a b. (Show a, Show b) => Int -> Wedge a b -> ShowS
forall a b. (Show a, Show b) => [Wedge a b] -> ShowS
forall a b. (Show a, Show b) => Wedge a b -> String
showList :: [Wedge a b] -> ShowS
\$cshowList :: forall a b. (Show a, Show b) => [Wedge a b] -> ShowS
show :: Wedge a b -> String
\$cshow :: forall a b. (Show a, Show b) => Wedge a b -> String
showsPrec :: Int -> Wedge a b -> ShowS
\$cshowsPrec :: forall a b. (Show a, Show b) => Int -> Wedge a b -> ShowS
Show)

instance Bifunctor Wedge where
bimap :: (a -> b) -> (c -> d) -> Wedge a c -> Wedge b d
bimap a -> b
_ c -> d
_ Wedge a c
Nowhere   = Wedge b d
forall a b. Wedge a b
Nowhere
bimap a -> b
f c -> d
_ (Here a
a)  = b -> Wedge b d
forall a b. a -> Wedge a b
Here  (a -> b
f a
a)
bimap a -> b
_ c -> d
g (There c
b) = d -> Wedge b d
forall a b. b -> Wedge a b
There (c -> d
g c
b)

instance Bifoldable Wedge where
bifoldMap :: (a -> m) -> (b -> m) -> Wedge a b -> m
bifoldMap a -> m
_ b -> m
_ Wedge a b
Nowhere   = m
forall a. Monoid a => a
mempty
bifoldMap a -> m
f b -> m
_ (Here a
a)  = a -> m
f a
a
bifoldMap a -> m
_ b -> m
g (There b
b) = b -> m
g b
b

instance Bitraversable Wedge where
bitraverse :: (a -> f c) -> (b -> f d) -> Wedge a b -> f (Wedge c d)
bitraverse a -> f c
_ b -> f d
_ Wedge a b
Nowhere   = Wedge c d -> f (Wedge c d)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Wedge c d
forall a b. Wedge a b
Nowhere
bitraverse a -> f c
f b -> f d
_ (Here a
a)  = c -> Wedge c d
forall a b. a -> Wedge a b
Here (c -> Wedge c d) -> f c -> f (Wedge c d)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<\$> a -> f c
f a
a
bitraverse a -> f c
_ b -> f d
g (There b
b) = d -> Wedge c d
forall a b. b -> Wedge a b
There (d -> Wedge c d) -> f d -> f (Wedge c d)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<\$> b -> f d
g b
b

instance Applicative (Wedge a) where
pure :: a -> Wedge a a
pure  = a -> Wedge a a
forall a b. b -> Wedge a b
There
<*> :: Wedge a (a -> b) -> Wedge a a -> Wedge a b
(<*>) = Wedge a (a -> b) -> Wedge a a -> Wedge a b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap

Wedge a a
Nowhere >>= :: Wedge a a -> (a -> Wedge a b) -> Wedge a b
>>= a -> Wedge a b
_ = Wedge a b
forall a b. Wedge a b
Nowhere
Here a
a  >>= a -> Wedge a b
_ = a -> Wedge a b
forall a b. a -> Wedge a b
Here a
a
There a
a >>= a -> Wedge a b
f = a -> Wedge a b
f a
a
```