{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Agda.Utils.Maybe.Strict
( module Data.Strict.Maybe
, module Agda.Utils.Maybe.Strict
) where
import Prelude hiding (Maybe (..), maybe, null)
import qualified Prelude as Lazy
import Control.DeepSeq (NFData (..))
import Data.Binary (Binary (..))
import Data.Data (Data (..))
import Data.Semigroup (Semigroup, (<>))
import Data.Strict.Maybe (Maybe (Nothing, Just), fromJust,
fromMaybe, isJust, isNothing, maybe)
import GHC.Generics (Generic (..))
import Agda.Utils.Null
toStrict :: Lazy.Maybe a -> Maybe a
toStrict :: Maybe a -> Maybe a
toStrict Maybe a
Lazy.Nothing = Maybe a
forall a. Maybe a
Nothing
toStrict (Lazy.Just a
x) = a -> Maybe a
forall a. a -> Maybe a
Just a
x
toLazy :: Maybe a -> Lazy.Maybe a
toLazy :: Maybe a -> Maybe a
toLazy Maybe a
Nothing = Maybe a
forall a. Maybe a
Lazy.Nothing
toLazy (Just a
x) = a -> Maybe a
forall a. a -> Maybe a
Lazy.Just a
x
deriving instance Data a => Data (Maybe a)
deriving instance Generic (Maybe a)
instance Null (Maybe a) where
empty :: Maybe a
empty = Maybe a
forall a. Maybe a
Nothing
null :: Maybe a -> Bool
null = Maybe a -> Bool
forall a. Maybe a -> Bool
isNothing
instance Semigroup a => Semigroup (Maybe a) where
<> :: Maybe a -> Maybe a -> Maybe a
(<>) = (a -> a -> a) -> Maybe a -> Maybe a -> Maybe a
forall a. (a -> a -> a) -> Maybe a -> Maybe a -> Maybe a
unionMaybeWith a -> a -> a
forall a. Semigroup a => a -> a -> a
(<>)
instance Semigroup a => Monoid (Maybe a) where
mempty :: Maybe a
mempty = Maybe a
forall a. Maybe a
Nothing
mappend :: Maybe a -> Maybe a -> Maybe a
mappend = Maybe a -> Maybe a -> Maybe a
forall a. Semigroup a => a -> a -> a
(<>)
instance Applicative Maybe where
pure :: a -> Maybe a
pure = a -> Maybe a
forall a. a -> Maybe a
Just
Just a -> b
f <*> :: Maybe (a -> b) -> Maybe a -> Maybe b
<*> Just a
x = b -> Maybe b
forall a. a -> Maybe a
Just (b -> Maybe b) -> b -> Maybe b
forall a b. (a -> b) -> a -> b
$ a -> b
f a
x
Maybe (a -> b)
_ <*> Maybe a
_ = Maybe b
forall a. Maybe a
Nothing
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
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 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
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
maybeToList :: Maybe a -> [a]
maybeToList :: Maybe a -> [a]
maybeToList Maybe a
Nothing = []
maybeToList (Just a
x) = [a
x]
catMaybes :: [Maybe a] -> [a]
catMaybes :: [Maybe a] -> [a]
catMaybes [Maybe a]
ls = [a
x | Just a
x <- [Maybe a]
ls]
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
unionMaybeWith :: (a -> a -> a) -> Maybe a -> Maybe a -> Maybe a
unionMaybeWith :: (a -> a -> a) -> Maybe a -> Maybe a -> Maybe a
unionMaybeWith a -> a -> a
f Maybe a
Nothing Maybe a
mb = Maybe a
mb
unionMaybeWith a -> a -> a
f Maybe a
ma Maybe a
Nothing = Maybe a
ma
unionMaybeWith a -> a -> a
f (Just a
a) (Just a
b) = a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$ a -> a -> a
f a
a a
b
unzipMaybe :: Maybe (a,b) -> (Maybe a, Maybe b)
unzipMaybe :: Maybe (a, b) -> (Maybe a, Maybe b)
unzipMaybe Maybe (a, b)
Nothing = (Maybe a
forall a. Maybe a
Nothing, Maybe b
forall a. Maybe a
Nothing)
unzipMaybe (Just (a
a,b
b)) = (a -> Maybe a
forall a. a -> Maybe a
Just a
a, b -> Maybe b
forall a. a -> Maybe a
Just b
b)
filterMaybe :: (a -> Bool) -> a -> Maybe a
filterMaybe :: (a -> Bool) -> a -> Maybe a
filterMaybe a -> Bool
p a
a
| a -> Bool
p a
a = a -> Maybe a
forall a. a -> Maybe a
Just a
a
| Bool
otherwise = Maybe a
forall a. Maybe a
Nothing
forMaybe :: [a] -> (a -> Maybe b) -> [b]
forMaybe :: [a] -> (a -> Maybe b) -> [b]
forMaybe = ((a -> Maybe b) -> [a] -> [b]) -> [a] -> (a -> Maybe b) -> [b]
forall a b c. (a -> b -> c) -> b -> a -> c
flip (a -> Maybe b) -> [a] -> [b]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe
caseMaybe :: Maybe a -> b -> (a -> b) -> b
caseMaybe :: Maybe a -> b -> (a -> b) -> b
caseMaybe Maybe a
m b
err a -> b
f = b -> (a -> b) -> Maybe a -> b
forall b a. b -> (a -> b) -> Maybe a -> b
maybe b
err a -> b
f Maybe a
m
maybeM :: Monad m => m b -> (a -> m b) -> m (Maybe a) -> m b
maybeM :: m b -> (a -> m b) -> m (Maybe a) -> m b
maybeM m b
n a -> m b
j m (Maybe a)
mm = m b -> (a -> m b) -> Maybe a -> m b
forall b a. b -> (a -> b) -> Maybe a -> b
maybe m b
n a -> m b
j (Maybe a -> m b) -> m (Maybe a) -> m b
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< m (Maybe a)
mm
fromMaybeM :: Monad m => m a -> m (Maybe a) -> m a
fromMaybeM :: m a -> m (Maybe a) -> m a
fromMaybeM m a
m m (Maybe a)
mm = m a -> (a -> m a) -> m (Maybe a) -> m a
forall (m :: * -> *) b a.
Monad m =>
m b -> (a -> m b) -> m (Maybe a) -> m b
maybeM m a
m a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return m (Maybe a)
mm
caseMaybeM :: Monad m => m (Maybe a) -> m b -> (a -> m b) -> m b
caseMaybeM :: m (Maybe a) -> m b -> (a -> m b) -> m b
caseMaybeM m (Maybe a)
mm m b
err a -> m b
f = m b -> (a -> m b) -> m (Maybe a) -> m b
forall (m :: * -> *) b a.
Monad m =>
m b -> (a -> m b) -> m (Maybe a) -> m b
maybeM m b
err a -> m b
f m (Maybe a)
mm
ifJustM :: Monad m => m (Maybe a) -> (a -> m b) -> m b -> m b
ifJustM :: m (Maybe a) -> (a -> m b) -> m b -> m b
ifJustM m (Maybe a)
mm = (m b -> (a -> m b) -> m b) -> (a -> m b) -> m b -> m b
forall a b c. (a -> b -> c) -> b -> a -> c
flip (m (Maybe a) -> m b -> (a -> m b) -> m b
forall (m :: * -> *) a b.
Monad m =>
m (Maybe a) -> m b -> (a -> m b) -> m b
caseMaybeM m (Maybe a)
mm)
whenJust :: Monad m => Maybe a -> (a -> m ()) -> m ()
whenJust :: Maybe a -> (a -> m ()) -> m ()
whenJust Maybe a
m a -> m ()
k = Maybe a -> m () -> (a -> m ()) -> m ()
forall a b. Maybe a -> b -> (a -> b) -> b
caseMaybe Maybe a
m (() -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()) a -> m ()
k
whenJustM :: Monad m => m (Maybe a) -> (a -> m ()) -> m ()
whenJustM :: m (Maybe a) -> (a -> m ()) -> m ()
whenJustM m (Maybe a)
c a -> m ()
m = m (Maybe a)
c m (Maybe a) -> (Maybe a -> m ()) -> m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Maybe a -> (a -> m ()) -> m ()
forall (m :: * -> *) a. Monad m => Maybe a -> (a -> m ()) -> m ()
`whenJust` a -> m ()
m)