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

#if MIN_VERSION_base(4,9,0)
#define LIFTED_FUNCTOR_CLASSES 1
#else
#if MIN_VERSION_transformers(0,5,0)
#define LIFTED_FUNCTOR_CLASSES 1
#else
#if MIN_VERSION_transformers_compat(0,5,0) && !MIN_VERSION_transformers(0,4,0)
#define LIFTED_FUNCTOR_CLASSES 1
#endif
#endif
#endif

-----------------------------------------------------------------------------
-- |
--
-- 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 (..))
import           Data.Binary         (Binary (..))
import           Data.Hashable       (Hashable(..))
import           Data.Hashable.Lifted (Hashable1 (..))
import           GHC.Generics        (Generic)
import           Data.Data           (Data (..), Typeable)


#if __GLASGOW_HASKELL__ >= 706
import           GHC.Generics        (Generic1)
#endif

#if MIN_VERSION_deepseq(1,4,3)
import Control.DeepSeq (NFData1 (..))
#endif

#ifdef LIFTED_FUNCTOR_CLASSES
import Data.Functor.Classes (Eq1 (..), Ord1 (..), Read1 (..), Show1 (..))
#else
import Data.Functor.Classes (Eq1 (..), Ord1 (..), Read1 (..), Show1 (..))
#endif

-- | 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
/= :: Maybe a -> Maybe a -> Bool
$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
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
min :: Maybe a -> Maybe a -> Maybe a
$cmin :: forall a. Ord a => Maybe a -> Maybe a -> Maybe a
max :: Maybe a -> Maybe a -> Maybe a
$cmax :: forall a. Ord a => Maybe a -> Maybe a -> Maybe a
>= :: 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
$c< :: forall a. Ord a => Maybe a -> Maybe a -> Bool
compare :: Maybe a -> Maybe a -> Ordering
$ccompare :: forall a. Ord a => Maybe a -> Maybe a -> Ordering
$cp1Ord :: forall a. Ord a => Eq (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
readListPrec :: ReadPrec [Maybe a]
$creadListPrec :: forall a. Read a => ReadPrec [Maybe a]
readPrec :: ReadPrec (Maybe a)
$creadPrec :: forall a. Read a => ReadPrec (Maybe a)
readList :: ReadS [Maybe a]
$creadList :: forall a. Read a => ReadS [Maybe a]
readsPrec :: Int -> ReadS (Maybe a)
$creadsPrec :: forall a. Read a => Int -> ReadS (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
showList :: [Maybe a] -> ShowS
$cshowList :: forall a. Show a => [Maybe a] -> ShowS
show :: Maybe a -> String
$cshow :: forall a. Show a => Maybe a -> String
showsPrec :: Int -> Maybe a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> Maybe a -> ShowS
Show, Typeable, Typeable (Maybe a)
DataType
Constr
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 -> DataType
Maybe a -> Constr
(forall d. Data d => c (t d)) -> Maybe (c (Maybe a))
(forall b. Data b => b -> b) -> Maybe a -> Maybe a
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Maybe a -> c (Maybe a)
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Maybe a)
forall a. Data a => Typeable (Maybe a)
forall a. Data a => Maybe a -> DataType
forall a. Data a => Maybe a -> Constr
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))
$cJust :: Constr
$cNothing :: Constr
$tMaybe :: DataType
gmapMo :: (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)
gmapMp :: (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)
gmapM :: (forall d. Data d => d -> m d) -> Maybe a -> m (Maybe a)
$cgmapM :: forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d) -> Maybe a -> m (Maybe a)
gmapQi :: Int -> (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
gmapQ :: (forall d. Data d => d -> u) -> Maybe a -> [u]
$cgmapQ :: forall a u.
Data a =>
(forall d. Data d => d -> u) -> Maybe a -> [u]
gmapQr :: (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
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Maybe a -> r
$cgmapQl :: forall a r r'.
Data a =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Maybe a -> r
gmapT :: (forall b. Data b => b -> b) -> Maybe a -> Maybe a
$cgmapT :: forall a.
Data a =>
(forall b. Data b => b -> b) -> Maybe a -> Maybe a
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e)) -> 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))
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c (Maybe a))
$cdataCast1 :: forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Maybe a))
dataTypeOf :: Maybe a -> DataType
$cdataTypeOf :: forall a. Data a => Maybe a -> DataType
toConstr :: Maybe a -> Constr
$ctoConstr :: forall a. Data a => Maybe a -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> 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)
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Maybe a -> 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)
$cp1Data :: forall a. Data a => Typeable (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
$cto :: forall a x. Rep (Maybe a) x -> Maybe a
$cfrom :: forall a x. Maybe a -> Rep (Maybe a) x
Generic
#if __GLASGOW_HASKELL__ >= 706
    , (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
$cto1 :: forall a. Rep1 Maybe a -> Maybe a
$cfrom1 :: forall a. Maybe a -> Rep1 Maybe a
Generic1
#endif
    )

toStrict :: L.Maybe a -> Maybe a
toStrict :: 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 :: 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 :: 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 :: 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 :: 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 :: 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 :: 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 :: [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 :: 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 :: [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 :: (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)

#if MIN_VERSION_base(4,11,0)
instance Semigroup a => Monoid (Maybe a) where
  mempty :: Maybe a
mempty = Maybe a
forall a. Maybe a
Nothing
#else
instance Monoid a => Monoid (Maybe a) where
  mempty = Nothing

  Nothing `mappend` m       = m
  m       `mappend` Nothing = m
  Just x1 `mappend` Just x2 = Just (x1 `mappend` x2)
#endif

instance Functor Maybe where
  fmap :: (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 :: (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 :: (a -> f b) -> Maybe a -> f (Maybe b)
traverse a -> f b
_ Maybe a
Nothing  = Maybe b -> f (Maybe b)
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

#if MIN_VERSION_deepseq(1,4,3)
instance NFData1 Maybe where
  liftRnf :: (a -> ()) -> Maybe a -> ()
liftRnf a -> ()
rnfA = (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
#endif

-- 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 :: (Int -> a -> Int) -> Int -> Maybe a -> Int
liftHashWithSalt Int -> a -> Int
hashA Int
salt = (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
#ifdef LIFTED_FUNCTOR_CLASSES

instance Eq1 Maybe where
  liftEq :: (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 :: (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 :: (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 :: (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 (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 (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
a, String
s2)
        String
_         -> []

#else
instance Eq1   Maybe where eq1        = (==)
instance Ord1  Maybe where compare1   = compare
instance Show1 Maybe where showsPrec1 = showsPrec
instance Read1 Maybe where readsPrec1 = readsPrec
#endif