{-# 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
module Data.Strict.Maybe (
Maybe(..)
, isJust
, isNothing
, fromJust
, fromMaybe
, maybe
, listToMaybe
, maybeToList
, catMaybes
, mapMaybe
) where
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 (..))
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
data Maybe a = Nothing | Just !a
deriving (Maybe a -> Maybe a -> Bool
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, Maybe a -> Maybe a -> Bool
Maybe a -> Maybe a -> Ordering
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
Ord, ReadPrec [Maybe a]
ReadPrec (Maybe a)
ReadS [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
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, Maybe a -> DataType
Maybe a -> Constr
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 (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))
gmapMo :: 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)
gmapMp :: forall (m :: * -> *).
MonadPlus 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)
gmapM :: forall (m :: * -> *).
Monad m =>
(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 :: forall u. 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 u. (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 :: 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
gmapQl :: forall r r'.
(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 (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(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 (t :: * -> *) (c :: * -> *).
Typeable t =>
(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 (c :: * -> *).
(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 (c :: * -> *).
(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)
Data, 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. 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 :: forall a. Maybe a -> Maybe a
toStrict Maybe a
L.Nothing = forall a. Maybe a
Nothing
toStrict (L.Just a
x) = 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 = forall a. Maybe a
L.Nothing
toLazy (Just a
x) = forall a. a -> Maybe a
L.Just a
x
isJust :: Maybe a -> Bool
isJust :: forall a. Maybe a -> Bool
isJust Maybe a
Nothing = Bool
False
isJust Maybe a
_ = Bool
True
isNothing :: Maybe a -> Bool
isNothing :: forall a. Maybe a -> Bool
isNothing Maybe a
Nothing = Bool
True
isNothing Maybe a
_ = Bool
False
fromJust :: Maybe a -> a
fromJust :: forall a. Maybe a -> a
fromJust Maybe a
Nothing = forall a. HasCallStack => String -> a
error String
"Data.Strict.Maybe.fromJust: Nothing"
fromJust (Just a
x) = a
x
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
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
listToMaybe :: [a] -> Maybe a
listToMaybe :: forall a. [a] -> Maybe a
listToMaybe [] = forall a. Maybe a
Nothing
listToMaybe (a
a:[a]
_) = forall a. a -> Maybe a
Just a
a
maybeToList :: Maybe a -> [a]
maybeToList :: forall a. Maybe a -> [a]
maybeToList Maybe a
Nothing = []
maybeToList (Just a
x) = [a
x]
catMaybes :: [Maybe a] -> [a]
catMaybes :: forall a. [Maybe a] -> [a]
catMaybes [Maybe a]
ls = [a
x | Just a
x <- [Maybe a]
ls]
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
rforall a. a -> [a] -> [a]
:[b]
rs
where
rs :: [b]
rs = forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe a -> Maybe b
f [a]
xs
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 = forall a. a -> Maybe a
Just (a
x1 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 = 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 :: forall a b. (a -> b) -> Maybe a -> Maybe b
fmap a -> b
_ Maybe a
Nothing = forall a. Maybe a
Nothing
fmap a -> b
f (Just a
x) = 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 = 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 = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing
traverse a -> f b
f (Just a
x) = forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
x
instance NFData a => NFData (Maybe a) where
rnf :: Maybe a -> ()
rnf = forall a. NFData a => a -> ()
rnf forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Maybe a -> Maybe a
toLazy
#if MIN_VERSION_deepseq(1,4,3)
instance NFData1 Maybe where
liftRnf :: forall a. (a -> ()) -> Maybe a -> ()
liftRnf a -> ()
rnfA = forall (f :: * -> *) a. NFData1 f => (a -> ()) -> f a -> ()
liftRnf a -> ()
rnfA forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Maybe a -> Maybe a
toLazy
#endif
instance Binary a => Binary (Maybe a) where
put :: Maybe a -> Put
put = forall t. Binary t => t -> Put
put forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Maybe a -> Maybe a
toLazy
get :: Get (Maybe a)
get = forall a. Maybe a -> Maybe a
toStrict forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall t. Binary t => Get t
get
instance Hashable a => Hashable (Maybe a) where
hashWithSalt :: Int -> Maybe a -> Int
hashWithSalt Int
salt = forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
salt forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 = forall (t :: * -> *) a.
Hashable1 t =>
(Int -> a -> Int) -> Int -> t a -> Int
liftHashWithSalt Int -> a -> Int
hashA Int
salt forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Maybe a -> Maybe a
toLazy
#ifdef LIFTED_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 forall a. Ord a => a -> a -> Bool
> Int
10)
forall a b. (a -> b) -> a -> b
$ String -> ShowS
showString String
"Just "
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 = forall a. Bool -> ReadS a -> ReadS a
readParen (Int
d forall a. Ord a => a -> a -> Bool
> Int
10) String -> [(Maybe a, String)]
cons where
cons :: String -> [(Maybe a, String)]
cons String
s0 = do
(String
ident, String
s1) <- ReadS String
lex String
s0
case String
ident of
String
"Nothing" -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Maybe a
Nothing, String
s1)
String
"Just" -> do
(a
a, String
s2) <- Int -> ReadS a
ra Int
11 String
s1
forall (m :: * -> *) a. Monad m => a -> m a
return (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