{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE DeriveGeneric      #-}

-----------------------------------------------------------------------------
-- |
--
-- The strict variant of the standard Haskell 'L.Maybe' type and the
-- corresponding variants of the functions from "Data.Maybe".
--
-- Note that in contrast to the standard lazy 'L.Maybe' type, the strict
-- 'Maybe' type is not an applicative functor, and therefore also not a monad.
-- The problem is the /homomorphism/ law, which states that
--
--      @'pure' f '<*>' 'pure' x = 'pure' (f x)  -- must hold for all f@
--
-- This law does not hold for the expected applicative functor instance of
-- 'Maybe', as this instance does not satisfy @pure f \<*\> pure _|_ = pure (f
-- _|_)@ for @f = const@.
--
-----------------------------------------------------------------------------

module Data.Strict.Maybe (
    Maybe(..)
  , isJust
  , isNothing
  , fromJust
  , fromMaybe
  , maybe
  , listToMaybe
  , maybeToList
  , catMaybes
  , mapMaybe
) where

-- import parts explicitly, helps with compatibility
import           Prelude (Functor (..), Eq (..), Ord (..), Show (..), Read (..), Bool (..), (.)
                         ,error, Ordering (..), ($), showString, showParen, return, lex, readParen)
import           Control.Applicative (pure, (<$>))
import           Data.Monoid (Monoid (..))
import           Data.Semigroup (Semigroup (..))
import           Data.Foldable (Foldable (..))
import           Data.Traversable (Traversable (..))

-- Lazy variants
import qualified Prelude             as L

import           Control.DeepSeq      (NFData (..), NFData1 (..))
import           Data.Binary          (Binary (..))
import           Data.Data            (Data (..), Typeable)
import           Data.Hashable        (Hashable(..))
import           Data.Hashable.Lifted (Hashable1 (..))
import           GHC.Generics         (Generic, Generic1)

import Data.Functor.Classes (Eq1 (..), Ord1 (..), Read1 (..), Show1 (..))

-- | The type of strict optional values.
data Maybe a = Nothing | Just !a
  deriving (Maybe a -> Maybe a -> Bool
(Maybe a -> Maybe a -> Bool)
-> (Maybe a -> Maybe a -> Bool) -> Eq (Maybe a)
forall a. Eq a => Maybe a -> Maybe a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => Maybe a -> Maybe a -> Bool
== :: Maybe a -> Maybe a -> Bool
$c/= :: forall a. Eq a => Maybe a -> Maybe a -> Bool
/= :: Maybe a -> Maybe a -> Bool
Eq, Eq (Maybe a)
Eq (Maybe a) =>
(Maybe a -> Maybe a -> Ordering)
-> (Maybe a -> Maybe a -> Bool)
-> (Maybe a -> Maybe a -> Bool)
-> (Maybe a -> Maybe a -> Bool)
-> (Maybe a -> Maybe a -> Bool)
-> (Maybe a -> Maybe a -> Maybe a)
-> (Maybe a -> Maybe a -> Maybe a)
-> Ord (Maybe a)
Maybe a -> Maybe a -> Bool
Maybe a -> Maybe a -> Ordering
Maybe a -> Maybe a -> Maybe a
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. Ord a => Eq (Maybe a)
forall a. Ord a => Maybe a -> Maybe a -> Bool
forall a. Ord a => Maybe a -> Maybe a -> Ordering
forall a. Ord a => Maybe a -> Maybe a -> Maybe a
$ccompare :: forall a. Ord a => Maybe a -> Maybe a -> Ordering
compare :: Maybe a -> Maybe a -> Ordering
$c< :: forall a. Ord a => Maybe a -> Maybe a -> Bool
< :: Maybe a -> Maybe a -> Bool
$c<= :: forall a. Ord a => Maybe a -> Maybe a -> Bool
<= :: Maybe a -> Maybe a -> Bool
$c> :: forall a. Ord a => Maybe a -> Maybe a -> Bool
> :: Maybe a -> Maybe a -> Bool
$c>= :: forall a. Ord a => Maybe a -> Maybe a -> Bool
>= :: Maybe a -> Maybe a -> Bool
$cmax :: forall a. Ord a => Maybe a -> Maybe a -> Maybe a
max :: Maybe a -> Maybe a -> Maybe a
$cmin :: forall a. Ord a => Maybe a -> Maybe a -> Maybe a
min :: Maybe a -> Maybe a -> Maybe a
Ord, ReadPrec [Maybe a]
ReadPrec (Maybe a)
Int -> ReadS (Maybe a)
ReadS [Maybe a]
(Int -> ReadS (Maybe a))
-> ReadS [Maybe a]
-> ReadPrec (Maybe a)
-> ReadPrec [Maybe a]
-> Read (Maybe a)
forall a. Read a => ReadPrec [Maybe a]
forall a. Read a => ReadPrec (Maybe a)
forall a. Read a => Int -> ReadS (Maybe a)
forall a. Read a => ReadS [Maybe a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: forall a. Read a => Int -> ReadS (Maybe a)
readsPrec :: Int -> ReadS (Maybe a)
$creadList :: forall a. Read a => ReadS [Maybe a]
readList :: ReadS [Maybe a]
$creadPrec :: forall a. Read a => ReadPrec (Maybe a)
readPrec :: ReadPrec (Maybe a)
$creadListPrec :: forall a. Read a => ReadPrec [Maybe a]
readListPrec :: ReadPrec [Maybe a]
Read, Int -> Maybe a -> ShowS
[Maybe a] -> ShowS
Maybe a -> String
(Int -> Maybe a -> ShowS)
-> (Maybe a -> String) -> ([Maybe a] -> ShowS) -> Show (Maybe a)
forall a. Show a => Int -> Maybe a -> ShowS
forall a. Show a => [Maybe a] -> ShowS
forall a. Show a => Maybe a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> Maybe a -> ShowS
showsPrec :: Int -> Maybe a -> ShowS
$cshow :: forall a. Show a => Maybe a -> String
show :: Maybe a -> String
$cshowList :: forall a. Show a => [Maybe a] -> ShowS
showList :: [Maybe a] -> ShowS
Show, Typeable, Typeable (Maybe a)
Typeable (Maybe a) =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> Maybe a -> c (Maybe a))
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (Maybe a))
-> (Maybe a -> Constr)
-> (Maybe a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c (Maybe a)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Maybe a)))
-> ((forall b. Data b => b -> b) -> Maybe a -> Maybe a)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> Maybe a -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> Maybe a -> r)
-> (forall u. (forall d. Data d => d -> u) -> Maybe a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> Maybe a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> Maybe a -> m (Maybe a))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Maybe a -> m (Maybe a))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Maybe a -> m (Maybe a))
-> Data (Maybe a)
Maybe a -> Constr
Maybe a -> DataType
(forall b. Data b => b -> b) -> Maybe a -> Maybe a
forall a. Data a => Typeable (Maybe a)
forall a. Data a => Maybe a -> Constr
forall a. Data a => Maybe a -> DataType
forall a.
Data a =>
(forall b. Data b => b -> b) -> Maybe a -> Maybe a
forall a u.
Data a =>
Int -> (forall d. Data d => d -> u) -> Maybe a -> u
forall a u.
Data a =>
(forall d. Data d => d -> u) -> Maybe a -> [u]
forall a r r'.
Data a =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Maybe a -> r
forall a r r'.
Data a =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Maybe a -> r
forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d) -> Maybe a -> m (Maybe a)
forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Maybe a -> m (Maybe a)
forall a (c :: * -> *).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Maybe a)
forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Maybe a -> c (Maybe a)
forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Maybe a))
forall a (t :: * -> * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Maybe a))
forall a.
Typeable a =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    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 :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> Maybe a -> u
forall u. (forall d. Data d => d -> u) -> Maybe a -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Maybe a -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Maybe a -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Maybe a -> m (Maybe a)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Maybe a -> m (Maybe a)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Maybe a)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Maybe a -> c (Maybe a)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Maybe a))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Maybe a))
$cgfoldl :: forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Maybe a -> c (Maybe a)
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Maybe a -> c (Maybe a)
$cgunfold :: forall a (c :: * -> *).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Maybe a)
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Maybe a)
$ctoConstr :: forall a. Data a => Maybe a -> Constr
toConstr :: Maybe a -> Constr
$cdataTypeOf :: forall a. Data a => Maybe a -> DataType
dataTypeOf :: Maybe a -> DataType
$cdataCast1 :: forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Maybe a))
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Maybe a))
$cdataCast2 :: forall a (t :: * -> * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Maybe a))
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Maybe a))
$cgmapT :: forall a.
Data a =>
(forall b. Data b => b -> b) -> Maybe a -> Maybe a
gmapT :: (forall b. Data b => b -> b) -> Maybe a -> Maybe a
$cgmapQl :: forall a r r'.
Data a =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Maybe a -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Maybe a -> r
$cgmapQr :: forall a r r'.
Data a =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Maybe a -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Maybe a -> r
$cgmapQ :: forall a u.
Data a =>
(forall d. Data d => d -> u) -> Maybe a -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> Maybe a -> [u]
$cgmapQi :: forall a u.
Data a =>
Int -> (forall d. Data d => d -> u) -> Maybe a -> u
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Maybe a -> u
$cgmapM :: forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d) -> Maybe a -> m (Maybe a)
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Maybe a -> m (Maybe a)
$cgmapMp :: forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Maybe a -> m (Maybe a)
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Maybe a -> m (Maybe a)
$cgmapMo :: forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Maybe a -> m (Maybe a)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Maybe a -> m (Maybe a)
Data, (forall x. Maybe a -> Rep (Maybe a) x)
-> (forall x. Rep (Maybe a) x -> Maybe a) -> Generic (Maybe a)
forall x. Rep (Maybe a) x -> Maybe a
forall x. Maybe a -> Rep (Maybe a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (Maybe a) x -> Maybe a
forall a x. Maybe a -> Rep (Maybe a) x
$cfrom :: forall a x. Maybe a -> Rep (Maybe a) x
from :: forall x. Maybe a -> Rep (Maybe a) x
$cto :: forall a x. Rep (Maybe a) x -> Maybe a
to :: forall x. Rep (Maybe a) x -> Maybe a
Generic, (forall a. Maybe a -> Rep1 Maybe a)
-> (forall a. Rep1 Maybe a -> Maybe a) -> Generic1 Maybe
forall a. Rep1 Maybe a -> Maybe a
forall a. Maybe a -> Rep1 Maybe a
forall k (f :: k -> *).
(forall (a :: k). f a -> Rep1 f a)
-> (forall (a :: k). Rep1 f a -> f a) -> Generic1 f
$cfrom1 :: forall a. Maybe a -> Rep1 Maybe a
from1 :: forall a. Maybe a -> Rep1 Maybe a
$cto1 :: forall a. Rep1 Maybe a -> Maybe a
to1 :: forall a. Rep1 Maybe a -> Maybe a
Generic1)

toStrict :: L.Maybe a -> Maybe a
toStrict :: forall a. Maybe a -> Maybe a
toStrict Maybe a
L.Nothing  = Maybe a
forall a. Maybe a
Nothing
toStrict (L.Just a
x) = a -> Maybe a
forall a. a -> Maybe a
Just a
x

toLazy :: Maybe a -> L.Maybe a
toLazy :: forall a. Maybe a -> Maybe a
toLazy Maybe a
Nothing  = Maybe a
forall a. Maybe a
L.Nothing
toLazy (Just a
x) = a -> Maybe a
forall a. a -> Maybe a
L.Just a
x

-- | Yields 'True' iff the argument is of the form @Just _@.
isJust :: Maybe a -> Bool
isJust :: forall a. Maybe a -> Bool
isJust Maybe a
Nothing = Bool
False
isJust Maybe a
_       = Bool
True

-- | Yields 'True' iff the argument is 'Nothing'.
isNothing :: Maybe a -> Bool
isNothing :: forall a. Maybe a -> Bool
isNothing Maybe a
Nothing = Bool
True
isNothing Maybe a
_       = Bool
False

-- | Extracts the element out of a 'Just' and throws an error if the argument
-- is 'Nothing'.
fromJust :: Maybe a -> a
fromJust :: forall a. Maybe a -> a
fromJust Maybe a
Nothing  = String -> a
forall a. HasCallStack => String -> a
error String
"Data.Strict.Maybe.fromJust: Nothing"
fromJust (Just a
x) = a
x

-- | Given a default value and a 'Maybe', yield the default value if the
-- 'Maybe' argument is 'Nothing' and extract the value out of the 'Just'
-- otherwise.
fromMaybe :: a -> Maybe a -> a
fromMaybe :: forall a. a -> Maybe a -> a
fromMaybe a
x Maybe a
Nothing  = a
x
fromMaybe a
_ (Just a
y) = a
y

-- | Given a default value, a function and a 'Maybe' value, yields the default
-- value if the 'Maybe' value is 'Nothing' and applies the function to the
-- value stored in the 'Just' otherwise.
maybe :: b -> (a -> b) -> Maybe a -> b
maybe :: forall b a. b -> (a -> b) -> Maybe a -> b
maybe b
x a -> b
_ Maybe a
Nothing  = b
x
maybe b
_ a -> b
f (Just a
y) = a -> b
f a
y

-- | Analogous to 'L.listToMaybe' in "Data.Maybe".
listToMaybe :: [a] -> Maybe a
listToMaybe :: forall a. [a] -> Maybe a
listToMaybe []        =  Maybe a
forall a. Maybe a
Nothing
listToMaybe (a
a:[a]
_)     =  a -> Maybe a
forall a. a -> Maybe a
Just a
a

-- | Analogous to 'L.maybeToList' in "Data.Maybe".
maybeToList :: Maybe a -> [a]
maybeToList :: forall a. Maybe a -> [a]
maybeToList  Maybe a
Nothing   = []
maybeToList  (Just a
x)  = [a
x]

-- | Analogous to 'L.catMaybes' in "Data.Maybe".
catMaybes :: [Maybe a] -> [a]
catMaybes :: forall a. [Maybe a] -> [a]
catMaybes [Maybe a]
ls = [a
x | Just a
x <- [Maybe a]
ls]

-- | Analogous to 'L.mapMaybe' in "Data.Maybe".
mapMaybe :: (a -> Maybe b) -> [a] -> [b]
mapMaybe :: forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe a -> Maybe b
_ []     = []
mapMaybe a -> Maybe b
f (a
x:[a]
xs) = case a -> Maybe b
f a
x of
    Maybe b
Nothing -> [b]
rs
    Just b
r  -> b
rb -> [b] -> [b]
forall a. a -> [a] -> [a]
:[b]
rs
  where
    rs :: [b]
rs = (a -> Maybe b) -> [a] -> [b]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe a -> Maybe b
f [a]
xs

-- Instances
------------

instance Semigroup a => Semigroup (Maybe a) where
  Maybe a
Nothing <> :: Maybe a -> Maybe a -> Maybe a
<> Maybe a
m       = Maybe a
m
  Maybe a
m       <> Maybe a
Nothing = Maybe a
m
  Just a
x1 <> Just a
x2 = a -> Maybe a
forall a. a -> Maybe a
Just (a
x1 a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
x2)

instance Semigroup a => Monoid (Maybe a) where
  mempty :: Maybe a
mempty = Maybe a
forall a. Maybe a
Nothing

instance Functor Maybe where
  fmap :: forall a b. (a -> b) -> Maybe a -> Maybe b
fmap a -> b
_ Maybe a
Nothing  = Maybe b
forall a. Maybe a
Nothing
  fmap a -> b
f (Just a
x) = b -> Maybe b
forall a. a -> Maybe a
Just (a -> b
f a
x)

instance Foldable Maybe where
    foldMap :: forall m a. Monoid m => (a -> m) -> Maybe a -> m
foldMap a -> m
_ Maybe a
Nothing  = m
forall a. Monoid a => a
mempty
    foldMap a -> m
f (Just a
x) = a -> m
f a
x

instance Traversable Maybe where
    traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Maybe a -> f (Maybe b)
traverse a -> f b
_ Maybe a
Nothing  = Maybe b -> f (Maybe b)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe b
forall a. Maybe a
Nothing
    traverse a -> f b
f (Just a
x) = b -> Maybe b
forall a. a -> Maybe a
Just (b -> Maybe b) -> f b -> f (Maybe b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
x

-- deepseq
instance NFData a => NFData (Maybe a) where
  rnf :: Maybe a -> ()
rnf = Maybe a -> ()
forall a. NFData a => a -> ()
rnf (Maybe a -> ()) -> (Maybe a -> Maybe a) -> Maybe a -> ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe a -> Maybe a
forall a. Maybe a -> Maybe a
toLazy

instance NFData1 Maybe where
  liftRnf :: forall a. (a -> ()) -> Maybe a -> ()
liftRnf a -> ()
rnfA = (a -> ()) -> Maybe a -> ()
forall a. (a -> ()) -> Maybe a -> ()
forall (f :: * -> *) a. NFData1 f => (a -> ()) -> f a -> ()
liftRnf a -> ()
rnfA (Maybe a -> ()) -> (Maybe a -> Maybe a) -> Maybe a -> ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe a -> Maybe a
forall a. Maybe a -> Maybe a
toLazy

-- binary
instance Binary a => Binary (Maybe a) where
  put :: Maybe a -> Put
put = Maybe a -> Put
forall t. Binary t => t -> Put
put (Maybe a -> Put) -> (Maybe a -> Maybe a) -> Maybe a -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe a -> Maybe a
forall a. Maybe a -> Maybe a
toLazy
  get :: Get (Maybe a)
get = Maybe a -> Maybe a
forall a. Maybe a -> Maybe a
toStrict (Maybe a -> Maybe a) -> Get (Maybe a) -> Get (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get (Maybe a)
forall t. Binary t => Get t
get

-- hashable
instance Hashable a => Hashable (Maybe a) where
  hashWithSalt :: Int -> Maybe a -> Int
hashWithSalt Int
salt = Int -> Maybe a -> Int
forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
salt (Maybe a -> Int) -> (Maybe a -> Maybe a) -> Maybe a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe a -> Maybe a
forall a. Maybe a -> Maybe a
toLazy

instance Hashable1 Maybe where
  liftHashWithSalt :: forall a. (Int -> a -> Int) -> Int -> Maybe a -> Int
liftHashWithSalt Int -> a -> Int
hashA Int
salt = (Int -> a -> Int) -> Int -> Maybe a -> Int
forall a. (Int -> a -> Int) -> Int -> Maybe a -> Int
forall (t :: * -> *) a.
Hashable1 t =>
(Int -> a -> Int) -> Int -> t a -> Int
liftHashWithSalt Int -> a -> Int
hashA Int
salt (Maybe a -> Int) -> (Maybe a -> Maybe a) -> Maybe a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe a -> Maybe a
forall a. Maybe a -> Maybe a
toLazy

-- Data.Functor.Classes
instance Eq1 Maybe where
  liftEq :: forall a b. (a -> b -> Bool) -> Maybe a -> Maybe b -> Bool
liftEq a -> b -> Bool
f (Just a
a) (Just b
a') = a -> b -> Bool
f a
a b
a'
  liftEq a -> b -> Bool
_ Maybe a
Nothing  Maybe b
Nothing   = Bool
True
  liftEq a -> b -> Bool
_ Maybe a
_        Maybe b
_         = Bool
False

instance Ord1 Maybe where
  liftCompare :: forall a b. (a -> b -> Ordering) -> Maybe a -> Maybe b -> Ordering
liftCompare a -> b -> Ordering
_ Maybe a
Nothing  Maybe b
Nothing   = Ordering
EQ
  liftCompare a -> b -> Ordering
_ Maybe a
Nothing  (Just b
_)  = Ordering
LT
  liftCompare a -> b -> Ordering
_ (Just a
_) Maybe b
Nothing   = Ordering
GT
  liftCompare a -> b -> Ordering
f (Just a
a) (Just b
a') = a -> b -> Ordering
f a
a b
a'

instance Show1 Maybe where
  liftShowsPrec :: forall a.
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> Maybe a -> ShowS
liftShowsPrec Int -> a -> ShowS
_  [a] -> ShowS
_ Int
_ Maybe a
Nothing = String -> ShowS
showString String
"Nothing"
  liftShowsPrec Int -> a -> ShowS
sa [a] -> ShowS
_ Int
d (Just a
a) = Bool -> ShowS -> ShowS
showParen (Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
10)
    (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ String -> ShowS
showString String
"Just "
    ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> a -> ShowS
sa Int
11 a
a

instance Read1 Maybe where
  liftReadsPrec :: forall a. (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (Maybe a)
liftReadsPrec Int -> ReadS a
ra ReadS [a]
_ Int
d = Bool -> ReadS (Maybe a) -> ReadS (Maybe a)
forall a. Bool -> ReadS a -> ReadS a
readParen (Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
10) ReadS (Maybe a)
cons where
    cons :: ReadS (Maybe a)
cons String
s0 = do
      (String
ident, String
s1) <- ReadS String
lex String
s0
      case String
ident of
        String
"Nothing" -> (Maybe a, String) -> [(Maybe a, String)]
forall a. a -> [a]
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe a
forall a. Maybe a
Nothing, String
s1)
        String
"Just"    -> do
          (a
a, String
s2) <- Int -> ReadS a
ra Int
11 String
s1
          (Maybe a, String) -> [(Maybe a, String)]
forall a. a -> [a]
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
a, String
s2)
        String
_         -> []