module Agda.Utils.Maybe
( module Agda.Utils.Maybe
, module Data.Maybe
) where
import Control.Applicative
import Control.Monad.Trans.Maybe
import Data.Maybe
boolToMaybe :: Bool -> a -> Maybe a
boolToMaybe :: forall a. Bool -> a -> Maybe a
boolToMaybe Bool
b a
x = if Bool
b then forall a. a -> Maybe a
Just a
x else forall a. Maybe a
Nothing
unionMaybeWith :: (a -> a -> a) -> Maybe a -> Maybe a -> Maybe a
unionMaybeWith :: forall a. (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) = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ a -> a -> a
f a
a a
b
unionsMaybeWith :: (a -> a -> a) -> [Maybe a] -> Maybe a
unionsMaybeWith :: forall a. (a -> a -> a) -> [Maybe a] -> Maybe a
unionsMaybeWith a -> a -> a
f [Maybe a]
ms = case forall a. [Maybe a] -> [a]
catMaybes [Maybe a]
ms of
[] -> forall a. Maybe a
Nothing
[a]
as -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldl1 a -> a -> a
f [a]
as
unzipMaybe :: Maybe (a,b) -> (Maybe a, Maybe b)
unzipMaybe :: forall a b. Maybe (a, b) -> (Maybe a, Maybe b)
unzipMaybe Maybe (a, b)
Nothing = (forall a. Maybe a
Nothing, forall a. Maybe a
Nothing)
unzipMaybe (Just (a
a,b
b)) = (forall a. a -> Maybe a
Just a
a, forall a. a -> Maybe a
Just b
b)
filterMaybe :: (a -> Bool) -> a -> Maybe a
filterMaybe :: forall a. (a -> Bool) -> a -> Maybe a
filterMaybe a -> Bool
p a
a
| a -> Bool
p a
a = forall a. a -> Maybe a
Just a
a
| Bool
otherwise = forall a. Maybe a
Nothing
forMaybe :: [a] -> (a -> Maybe b) -> [b]
forMaybe :: forall a b. [a] -> (a -> Maybe b) -> [b]
forMaybe = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe
caseMaybe :: Maybe a -> b -> (a -> b) -> b
caseMaybe :: forall a b. Maybe a -> b -> (a -> b) -> b
caseMaybe Maybe a
m b
d a -> b
f = forall b a. b -> (a -> b) -> Maybe a -> b
maybe b
d a -> b
f Maybe a
m
ifJust :: Maybe a -> (a -> b) -> b -> b
ifJust :: forall a b. Maybe a -> (a -> b) -> b -> b
ifJust Maybe a
m a -> b
f b
d = forall b a. b -> (a -> b) -> Maybe a -> b
maybe b
d a -> b
f Maybe a
m
maybeM :: Monad m => m b -> (a -> m b) -> m (Maybe a) -> m b
maybeM :: forall (m :: * -> *) b a.
Monad m =>
m b -> (a -> m b) -> m (Maybe a) -> m b
maybeM m b
n a -> m b
j m (Maybe a)
mm = forall b a. b -> (a -> b) -> Maybe a -> b
maybe m b
n a -> m b
j 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 :: forall (m :: * -> *) a. Monad m => m a -> m (Maybe a) -> m a
fromMaybeM m a
m m (Maybe a)
mm = forall (m :: * -> *) b a.
Monad m =>
m b -> (a -> m b) -> m (Maybe a) -> m b
maybeM m a
m 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 :: forall (m :: * -> *) a b.
Monad m =>
m (Maybe a) -> m b -> (a -> m b) -> m b
caseMaybeM m (Maybe a)
mm m b
d a -> m b
f = forall (m :: * -> *) b a.
Monad m =>
m b -> (a -> m b) -> m (Maybe a) -> m b
maybeM m b
d a -> m b
f m (Maybe a)
mm
ifJustM :: Monad m => m (Maybe a) -> (a -> m b) -> m b -> m b
ifJustM :: forall (m :: * -> *) a b.
Monad m =>
m (Maybe a) -> (a -> m b) -> m b -> m b
ifJustM m (Maybe a)
mm = forall a b c. (a -> b -> c) -> b -> a -> c
flip (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 :: forall (m :: * -> *) a. Monad m => Maybe a -> (a -> m ()) -> m ()
whenJust Maybe a
m a -> m ()
k = forall a b. Maybe a -> b -> (a -> b) -> b
caseMaybe Maybe a
m (forall (m :: * -> *) a. Monad m => a -> m a
return ()) a -> m ()
k
whenNothing :: Monoid m => Maybe a -> m -> m
whenNothing :: forall m a. Monoid m => Maybe a -> m -> m
whenNothing Maybe a
m m
d = forall a b. Maybe a -> b -> (a -> b) -> b
caseMaybe Maybe a
m m
d (\a
_ -> forall a. Monoid a => a
mempty)
whenJustM :: Monad m => m (Maybe a) -> (a -> m ()) -> m ()
whenJustM :: forall (m :: * -> *) a.
Monad m =>
m (Maybe a) -> (a -> m ()) -> m ()
whenJustM m (Maybe a)
c a -> m ()
m = m (Maybe a)
c forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (forall (m :: * -> *) a. Monad m => Maybe a -> (a -> m ()) -> m ()
`whenJust` a -> m ()
m)
whenNothingM :: Monad m => m (Maybe a) -> m () -> m ()
whenNothingM :: forall (m :: * -> *) a. Monad m => m (Maybe a) -> m () -> m ()
whenNothingM m (Maybe a)
mm m ()
d = forall b a. b -> (a -> b) -> Maybe a -> b
maybe m ()
d (\a
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return ()) forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< m (Maybe a)
mm
allJustM :: Monad m => [m (Maybe a)] -> m (Maybe [a])
allJustM :: forall (m :: * -> *) a. Monad m => [m (Maybe a)] -> m (Maybe [a])
allJustM = forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT
liftMaybe :: Alternative f => Maybe a -> f a
liftMaybe :: forall (f :: * -> *) a. Alternative f => Maybe a -> f a
liftMaybe = forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall (f :: * -> *) a. Alternative f => f a
empty forall (f :: * -> *) a. Applicative f => a -> f a
pure