--------------------------------------------------------------------------------
-- |
-- Module      : Data.Monus.Max
-- Copyright   : (c) Donnacha Oisín Kidney 2021
-- Maintainer  : mail@doisinkidney.com
-- Stability   : experimental
-- Portability : non-portable
--
-- A 'Monus' for for maximums.
--------------------------------------------------------------------------------

module Data.Monus.Max where

import Control.Applicative
import Control.Monad

import Data.Monus

import Test.QuickCheck
import Control.DeepSeq
import Data.Functor.Classes
import Text.Read

import Data.Data ( Data, Typeable )
import GHC.Generics ( Generic )
import GHC.Read (expectP)
import Data.Functor (($>))
import Control.Monad.Fix

-- | A type which adds a lower bound to some ordered type.
data Max a = Bot | In a
  deriving stock (Max a -> Max a -> Bool
(Max a -> Max a -> Bool) -> (Max a -> Max a -> Bool) -> Eq (Max a)
forall a. Eq a => Max a -> Max a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Max a -> Max a -> Bool
$c/= :: forall a. Eq a => Max a -> Max a -> Bool
== :: Max a -> Max a -> Bool
$c== :: forall a. Eq a => Max a -> Max a -> Bool
Eq, Typeable (Max a)
Typeable (Max a)
-> (forall (c :: Type -> Type).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> Max a -> c (Max a))
-> (forall (c :: Type -> Type).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (Max a))
-> (Max a -> Constr)
-> (Max a -> DataType)
-> (forall (t :: Type -> Type) (c :: Type -> Type).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c (Max a)))
-> (forall (t :: Type -> Type -> Type) (c :: Type -> Type).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Max a)))
-> ((forall b. Data b => b -> b) -> Max a -> Max a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Max a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Max a -> r)
-> (forall u. (forall d. Data d => d -> u) -> Max a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> Max a -> u)
-> (forall (m :: Type -> Type).
    Monad m =>
    (forall d. Data d => d -> m d) -> Max a -> m (Max a))
-> (forall (m :: Type -> Type).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Max a -> m (Max a))
-> (forall (m :: Type -> Type).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Max a -> m (Max a))
-> Data (Max a)
Max a -> DataType
Max a -> Constr
(forall b. Data b => b -> b) -> Max a -> Max a
forall {a}. Data a => Typeable (Max a)
forall a. Data a => Max a -> DataType
forall a. Data a => Max a -> Constr
forall a. Data a => (forall b. Data b => b -> b) -> Max a -> Max a
forall a u.
Data a =>
Int -> (forall d. Data d => d -> u) -> Max a -> u
forall a u. Data a => (forall d. Data d => d -> u) -> Max a -> [u]
forall a r r'.
Data a =>
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Max a -> r
forall a r r'.
Data a =>
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Max a -> r
forall a (m :: Type -> Type).
(Data a, Monad m) =>
(forall d. Data d => d -> m d) -> Max a -> m (Max a)
forall a (m :: Type -> Type).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Max a -> m (Max a)
forall a (c :: Type -> Type).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Max a)
forall a (c :: Type -> Type).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Max a -> c (Max a)
forall a (t :: Type -> Type) (c :: Type -> Type).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Max a))
forall a (t :: Type -> Type -> Type) (c :: Type -> Type).
(Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Max a))
forall a.
Typeable a
-> (forall (c :: Type -> Type).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: Type -> Type).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: Type -> Type) (c :: Type -> Type).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: Type -> Type -> Type) (c :: Type -> Type).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: Type -> Type).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: Type -> Type).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: Type -> Type).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> Max a -> u
forall u. (forall d. Data d => d -> u) -> Max a -> [u]
forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Max a -> r
forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Max a -> r
forall (m :: Type -> Type).
Monad m =>
(forall d. Data d => d -> m d) -> Max a -> m (Max a)
forall (m :: Type -> Type).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Max a -> m (Max a)
forall (c :: Type -> Type).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Max a)
forall (c :: Type -> Type).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Max a -> c (Max a)
forall (t :: Type -> Type) (c :: Type -> Type).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Max a))
forall (t :: Type -> Type -> Type) (c :: Type -> Type).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Max a))
gmapMo :: forall (m :: Type -> Type).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Max a -> m (Max a)
$cgmapMo :: forall a (m :: Type -> Type).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Max a -> m (Max a)
gmapMp :: forall (m :: Type -> Type).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Max a -> m (Max a)
$cgmapMp :: forall a (m :: Type -> Type).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Max a -> m (Max a)
gmapM :: forall (m :: Type -> Type).
Monad m =>
(forall d. Data d => d -> m d) -> Max a -> m (Max a)
$cgmapM :: forall a (m :: Type -> Type).
(Data a, Monad m) =>
(forall d. Data d => d -> m d) -> Max a -> m (Max a)
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Max a -> u
$cgmapQi :: forall a u.
Data a =>
Int -> (forall d. Data d => d -> u) -> Max a -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> Max a -> [u]
$cgmapQ :: forall a u. Data a => (forall d. Data d => d -> u) -> Max a -> [u]
gmapQr :: forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Max a -> r
$cgmapQr :: forall a r r'.
Data a =>
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Max a -> r
gmapQl :: forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Max a -> r
$cgmapQl :: forall a r r'.
Data a =>
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Max a -> r
gmapT :: (forall b. Data b => b -> b) -> Max a -> Max a
$cgmapT :: forall a. Data a => (forall b. Data b => b -> b) -> Max a -> Max a
dataCast2 :: forall (t :: Type -> Type -> Type) (c :: Type -> Type).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Max a))
$cdataCast2 :: forall a (t :: Type -> Type -> Type) (c :: Type -> Type).
(Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Max a))
dataCast1 :: forall (t :: Type -> Type) (c :: Type -> Type).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Max a))
$cdataCast1 :: forall a (t :: Type -> Type) (c :: Type -> Type).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Max a))
dataTypeOf :: Max a -> DataType
$cdataTypeOf :: forall a. Data a => Max a -> DataType
toConstr :: Max a -> Constr
$ctoConstr :: forall a. Data a => Max a -> Constr
gunfold :: forall (c :: Type -> Type).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Max a)
$cgunfold :: forall a (c :: Type -> Type).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Max a)
gfoldl :: forall (c :: Type -> Type).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Max a -> c (Max a)
$cgfoldl :: forall a (c :: Type -> Type).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Max a -> c (Max a)
Data, (forall x. Max a -> Rep (Max a) x)
-> (forall x. Rep (Max a) x -> Max a) -> Generic (Max a)
forall x. Rep (Max a) x -> Max a
forall x. Max a -> Rep (Max a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (Max a) x -> Max a
forall a x. Max a -> Rep (Max a) x
$cto :: forall a x. Rep (Max a) x -> Max a
$cfrom :: forall a x. Max a -> Rep (Max a) x
Generic, Typeable, (forall a b. (a -> b) -> Max a -> Max b)
-> (forall a b. a -> Max b -> Max a) -> Functor Max
forall a b. a -> Max b -> Max a
forall a b. (a -> b) -> Max a -> Max b
forall (f :: Type -> Type).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> Max b -> Max a
$c<$ :: forall a b. a -> Max b -> Max a
fmap :: forall a b. (a -> b) -> Max a -> Max b
$cfmap :: forall a b. (a -> b) -> Max a -> Max b
Functor, (forall m. Monoid m => Max m -> m)
-> (forall m a. Monoid m => (a -> m) -> Max a -> m)
-> (forall m a. Monoid m => (a -> m) -> Max a -> m)
-> (forall a b. (a -> b -> b) -> b -> Max a -> b)
-> (forall a b. (a -> b -> b) -> b -> Max a -> b)
-> (forall b a. (b -> a -> b) -> b -> Max a -> b)
-> (forall b a. (b -> a -> b) -> b -> Max a -> b)
-> (forall a. (a -> a -> a) -> Max a -> a)
-> (forall a. (a -> a -> a) -> Max a -> a)
-> (forall a. Max a -> [a])
-> (forall a. Max a -> Bool)
-> (forall a. Max a -> Int)
-> (forall a. Eq a => a -> Max a -> Bool)
-> (forall a. Ord a => Max a -> a)
-> (forall a. Ord a => Max a -> a)
-> (forall a. Num a => Max a -> a)
-> (forall a. Num a => Max a -> a)
-> Foldable Max
forall a. Eq a => a -> Max a -> Bool
forall a. Num a => Max a -> a
forall a. Ord a => Max a -> a
forall m. Monoid m => Max m -> m
forall a. Max a -> Bool
forall a. Max a -> Int
forall a. Max a -> [a]
forall a. (a -> a -> a) -> Max a -> a
forall m a. Monoid m => (a -> m) -> Max a -> m
forall b a. (b -> a -> b) -> b -> Max a -> b
forall a b. (a -> b -> b) -> b -> Max a -> b
forall (t :: Type -> Type).
(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 :: forall a. Num a => Max a -> a
$cproduct :: forall a. Num a => Max a -> a
sum :: forall a. Num a => Max a -> a
$csum :: forall a. Num a => Max a -> a
minimum :: forall a. Ord a => Max a -> a
$cminimum :: forall a. Ord a => Max a -> a
maximum :: forall a. Ord a => Max a -> a
$cmaximum :: forall a. Ord a => Max a -> a
elem :: forall a. Eq a => a -> Max a -> Bool
$celem :: forall a. Eq a => a -> Max a -> Bool
length :: forall a. Max a -> Int
$clength :: forall a. Max a -> Int
null :: forall a. Max a -> Bool
$cnull :: forall a. Max a -> Bool
toList :: forall a. Max a -> [a]
$ctoList :: forall a. Max a -> [a]
foldl1 :: forall a. (a -> a -> a) -> Max a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> Max a -> a
foldr1 :: forall a. (a -> a -> a) -> Max a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> Max a -> a
foldl' :: forall b a. (b -> a -> b) -> b -> Max a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> Max a -> b
foldl :: forall b a. (b -> a -> b) -> b -> Max a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> Max a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> Max a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> Max a -> b
foldr :: forall a b. (a -> b -> b) -> b -> Max a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> Max a -> b
foldMap' :: forall m a. Monoid m => (a -> m) -> Max a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> Max a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> Max a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> Max a -> m
fold :: forall m. Monoid m => Max m -> m
$cfold :: forall m. Monoid m => Max m -> m
Foldable, Functor Max
Foldable Max
Functor Max
-> Foldable Max
-> (forall (f :: Type -> Type) a b.
    Applicative f =>
    (a -> f b) -> Max a -> f (Max b))
-> (forall (f :: Type -> Type) a.
    Applicative f =>
    Max (f a) -> f (Max a))
-> (forall (m :: Type -> Type) a b.
    Monad m =>
    (a -> m b) -> Max a -> m (Max b))
-> (forall (m :: Type -> Type) a.
    Monad m =>
    Max (m a) -> m (Max a))
-> Traversable Max
forall (t :: Type -> Type).
Functor t
-> Foldable t
-> (forall (f :: Type -> Type) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: Type -> Type) a.
    Applicative f =>
    t (f a) -> f (t a))
-> (forall (m :: Type -> Type) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: Type -> Type) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: Type -> Type) a. Monad m => Max (m a) -> m (Max a)
forall (f :: Type -> Type) a.
Applicative f =>
Max (f a) -> f (Max a)
forall (m :: Type -> Type) a b.
Monad m =>
(a -> m b) -> Max a -> m (Max b)
forall (f :: Type -> Type) a b.
Applicative f =>
(a -> f b) -> Max a -> f (Max b)
sequence :: forall (m :: Type -> Type) a. Monad m => Max (m a) -> m (Max a)
$csequence :: forall (m :: Type -> Type) a. Monad m => Max (m a) -> m (Max a)
mapM :: forall (m :: Type -> Type) a b.
Monad m =>
(a -> m b) -> Max a -> m (Max b)
$cmapM :: forall (m :: Type -> Type) a b.
Monad m =>
(a -> m b) -> Max a -> m (Max b)
sequenceA :: forall (f :: Type -> Type) a.
Applicative f =>
Max (f a) -> f (Max a)
$csequenceA :: forall (f :: Type -> Type) a.
Applicative f =>
Max (f a) -> f (Max a)
traverse :: forall (f :: Type -> Type) a b.
Applicative f =>
(a -> f b) -> Max a -> f (Max b)
$ctraverse :: forall (f :: Type -> Type) a b.
Applicative f =>
(a -> f b) -> Max a -> f (Max b)
Traversable, Int -> Max a -> ShowS
[Max a] -> ShowS
Max a -> String
(Int -> Max a -> ShowS)
-> (Max a -> String) -> ([Max a] -> ShowS) -> Show (Max a)
forall a. Show a => Int -> Max a -> ShowS
forall a. Show a => [Max a] -> ShowS
forall a. Show a => Max a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Max a] -> ShowS
$cshowList :: forall a. Show a => [Max a] -> ShowS
show :: Max a -> String
$cshow :: forall a. Show a => Max a -> String
showsPrec :: Int -> Max a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> Max a -> ShowS
Show, ReadPrec [Max a]
ReadPrec (Max a)
Int -> ReadS (Max a)
ReadS [Max a]
(Int -> ReadS (Max a))
-> ReadS [Max a]
-> ReadPrec (Max a)
-> ReadPrec [Max a]
-> Read (Max a)
forall a. Read a => ReadPrec [Max a]
forall a. Read a => ReadPrec (Max a)
forall a. Read a => Int -> ReadS (Max a)
forall a. Read a => ReadS [Max a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Max a]
$creadListPrec :: forall a. Read a => ReadPrec [Max a]
readPrec :: ReadPrec (Max a)
$creadPrec :: forall a. Read a => ReadPrec (Max a)
readList :: ReadS [Max a]
$creadList :: forall a. Read a => ReadS [Max a]
readsPrec :: Int -> ReadS (Max a)
$creadsPrec :: forall a. Read a => Int -> ReadS (Max a)
Read)

instance Arbitrary a => Arbitrary (Max a) where
  arbitrary :: Gen (Max a)
arbitrary = Gen (Max a)
forall (f :: Type -> Type) a.
(Arbitrary1 f, Arbitrary a) =>
Gen (f a)
arbitrary1
  shrink :: Max a -> [Max a]
shrink = Max a -> [Max a]
forall (f :: Type -> Type) a.
(Arbitrary1 f, Arbitrary a) =>
f a -> [f a]
shrink1

instance NFData a => NFData (Max a) where
  rnf :: Max a -> ()
rnf Max a
Bot = ()
  rnf (In a
x) = a -> ()
forall a. NFData a => a -> ()
rnf a
x

instance Eq1 Max where
  liftEq :: forall a b. (a -> b -> Bool) -> Max a -> Max b -> Bool
liftEq a -> b -> Bool
_ Max a
Bot Max b
Bot = Bool
True
  liftEq a -> b -> Bool
eq (In a
x) (In b
y) = a -> b -> Bool
eq a
x b
y
  liftEq a -> b -> Bool
_ Max a
_ Max b
_ = Bool
False

instance Ord1 Max where
  liftCompare :: forall a b. (a -> b -> Ordering) -> Max a -> Max b -> Ordering
liftCompare a -> b -> Ordering
cmp Max a
Bot Max b
Bot = Ordering
EQ
  liftCompare a -> b -> Ordering
cmp Max a
Bot (In b
_) = Ordering
LT
  liftCompare a -> b -> Ordering
cmp (In a
_) Max b
Bot = Ordering
GT
  liftCompare a -> b -> Ordering
cmp (In a
x) (In b
y) = a -> b -> Ordering
cmp a
x b
y

instance Show1 Max where
  liftShowsPrec :: forall a.
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> Max a -> ShowS
liftShowsPrec Int -> a -> ShowS
sp [a] -> ShowS
sl Int
n Max a
Bot = String -> ShowS
showString String
"Bot"
  liftShowsPrec Int -> a -> ShowS
sp [a] -> ShowS
_ Int
d (In a
x) = (Int -> a -> ShowS) -> String -> Int -> a -> ShowS
forall a. (Int -> a -> ShowS) -> String -> Int -> a -> ShowS
showsUnaryWith Int -> a -> ShowS
sp String
"In" Int
d a
x

instance Read1 Max where
  liftReadPrec :: forall a. ReadPrec a -> ReadPrec [a] -> ReadPrec (Max a)
liftReadPrec ReadPrec a
rp ReadPrec [a]
_ =
      ReadPrec (Max a) -> ReadPrec (Max a)
forall a. ReadPrec a -> ReadPrec a
parens (Lexeme -> ReadPrec ()
expectP (String -> Lexeme
Ident String
"Bot") ReadPrec () -> Max a -> ReadPrec (Max a)
forall (f :: Type -> Type) a b. Functor f => f a -> b -> f b
$> Max a
forall a. Max a
Bot)
      ReadPrec (Max a) -> ReadPrec (Max a) -> ReadPrec (Max a)
forall (f :: Type -> Type) a. Alternative f => f a -> f a -> f a
<|>
      ReadPrec (Max a) -> ReadPrec (Max a)
forall a. ReadPrec a -> ReadPrec a
readData (ReadPrec a -> String -> (a -> Max a) -> ReadPrec (Max a)
forall a t. ReadPrec a -> String -> (a -> t) -> ReadPrec t
readUnaryWith ReadPrec a
rp String
"In" a -> Max a
forall a. a -> Max a
In)

  liftReadListPrec :: forall a. ReadPrec a -> ReadPrec [a] -> ReadPrec [Max a]
liftReadListPrec = ReadPrec a -> ReadPrec [a] -> ReadPrec [Max a]
forall (f :: Type -> Type) a.
Read1 f =>
ReadPrec a -> ReadPrec [a] -> ReadPrec [f a]
liftReadListPrecDefault
  liftReadList :: forall a. (Int -> ReadS a) -> ReadS [a] -> ReadS [Max a]
liftReadList     = (Int -> ReadS a) -> ReadS [a] -> ReadS [Max a]
forall (f :: Type -> Type) a.
Read1 f =>
(Int -> ReadS a) -> ReadS [a] -> ReadS [f a]
liftReadListDefault

instance Arbitrary1 Max where
  liftArbitrary :: forall a. Gen a -> Gen (Max a)
liftArbitrary Gen a
arb = (Maybe a -> Max a) -> Gen (Maybe a) -> Gen (Max a)
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap (Max a -> (a -> Max a) -> Maybe a -> Max a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Max a
forall a. Max a
Bot a -> Max a
forall a. a -> Max a
In) (Gen a -> Gen (Maybe a)
forall (f :: Type -> Type) a. Arbitrary1 f => Gen a -> Gen (f a)
liftArbitrary Gen a
arb)
  liftShrink :: forall a. (a -> [a]) -> Max a -> [Max a]
liftShrink a -> [a]
shr Max a
Bot = []
  liftShrink a -> [a]
shr (In a
x) = Max a
forall a. Max a
Bot Max a -> [Max a] -> [Max a]
forall a. a -> [a] -> [a]
: (a -> Max a) -> [a] -> [Max a]
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Max a
forall a. a -> Max a
In (a -> [a]
shr a
x)

instance Ord a => Ord (Max a) where
  Max a
Bot  <= :: Max a -> Max a -> Bool
<= Max a
_    = Bool
True
  In a
_ <= Max a
Bot  = Bool
False
  In a
x <= In a
y = a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
y

  >= :: Max a -> Max a -> Bool
(>=) = (Max a -> Max a -> Bool) -> Max a -> Max a -> Bool
forall a b c. (a -> b -> c) -> b -> a -> c
flip Max a -> Max a -> Bool
forall a. Ord a => a -> a -> Bool
(<=)

  Max a
x < :: Max a -> Max a -> Bool
< Max a
y = Bool -> Bool
not (Max a
x Max a -> Max a -> Bool
forall a. Ord a => a -> a -> Bool
>= Max a
y)

  > :: Max a -> Max a -> Bool
(>) = (Max a -> Max a -> Bool) -> Max a -> Max a -> Bool
forall a b c. (a -> b -> c) -> b -> a -> c
flip Max a -> Max a -> Bool
forall a. Ord a => a -> a -> Bool
(<)

  max :: Max a -> Max a -> Max a
max = Max a -> Max a -> Max a
forall a. Semigroup a => a -> a -> a
(<>)

  min :: Max a -> Max a -> Max a
min = (a -> a -> a) -> Max a -> Max a -> Max a
forall (f :: Type -> Type) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> a
forall a. Ord a => a -> a -> a
min

  compare :: Max a -> Max a -> Ordering
compare Max a
Bot    Max a
Bot    = Ordering
EQ
  compare Max a
Bot    (In a
_) = Ordering
LT
  compare (In a
_) Max a
Bot    = Ordering
GT
  compare (In a
x) (In a
y) = a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare a
x a
y

instance Applicative Max where
  pure :: forall a. a -> Max a
pure = a -> Max a
forall a. a -> Max a
In

  liftA2 :: forall a b c. (a -> b -> c) -> Max a -> Max b -> Max c
liftA2 a -> b -> c
f (In a
x) (In b
y) = c -> Max c
forall a. a -> Max a
In (a -> b -> c
f a
x b
y)
  liftA2 a -> b -> c
_ Max a
_ Max b
_ = Max c
forall a. Max a
Bot

  In a -> b
f <*> :: forall a b. Max (a -> b) -> Max a -> Max b
<*> In a
x = b -> Max b
forall a. a -> Max a
In (a -> b
f a
x)
  Max (a -> b)
_ <*> Max a
_ = Max b
forall a. Max a
Bot

instance Monad Max where
  Max a
Bot  >>= :: forall a b. Max a -> (a -> Max b) -> Max b
>>= a -> Max b
_ = Max b
forall a. Max a
Bot
  In a
x >>= a -> Max b
f = a -> Max b
f a
x

instance Alternative Max where
  empty :: forall a. Max a
empty = Max a
forall a. Max a
Bot

  Max a
Bot <|> :: forall a. Max a -> Max a -> Max a
<|> Max a
y = Max a
y
  Max a
x   <|> Max a
_ = Max a
x

instance MonadPlus Max

instance MonadFix Max where
  mfix :: forall a. (a -> Max a) -> Max a
mfix a -> Max a
f = Max a
r
    where
      r :: Max a
r = a -> Max a
f (Max a -> a
forall {a}. Max a -> a
unIn Max a
r)
      unIn :: Max a -> a
unIn (In a
x) = a
x
      unIn Max a
Bot = String -> a
forall a. String -> a
errorWithoutStackTrace String
"mfix Max: Bot"

instance Ord a => Semigroup (Max a) where
  Max a
Bot  <> :: Max a -> Max a -> Max a
<> Max a
y = Max a
y
  In a
x <> Max a
ys = a -> Max a
forall a. a -> Max a
In (case Max a
ys of
                    Max a
Bot -> a
x
                    In a
y -> a -> a -> a
forall a. Ord a => a -> a -> a
max a
x a
y)

instance Ord a => Monoid (Max a) where
  mempty :: Max a
mempty = Max a
forall a. Max a
Bot
  {-# INLINE mempty #-}

instance Ord a => Monus (Max a) where
  |-| :: Max a -> Max a -> Max a
(|-|) = Max a -> Max a -> Max a
forall a. Semigroup a => a -> a -> a
(<>)
  {-# INLINE (|-|) #-}