Agda-2.5.2: A dependently typed functional programming language and proof assistant

Safe Haskell | Safe
Language | Haskell2010

Agda.Utils.Maybe

Contents

- Collection operations.
- Conditionals and loops.
- Monads and Maybe.

Description

Extend `Maybe` by common operations for the `Maybe` type.

Note: since this module is usually imported unqualified, we do not use short names, but all names contain `Maybe`, `Just`, or 'Nothing.

Synopsis

- unionMaybeWith :: (a -> a -> a) -> Maybe a -> Maybe a -> Maybe a
- unzipMaybe :: Maybe (a, b) -> (Maybe a, Maybe b)
- filterMaybe :: (a -> Bool) -> a -> Maybe a
- forMaybe :: [a] -> (a -> Maybe b) -> [b]
- caseMaybe :: Maybe a -> b -> (a -> b) -> b
- maybeM :: Monad m => m b -> (a -> m b) -> m (Maybe a) -> m b
- fromMaybeM :: Monad m => m a -> m (Maybe a) -> m a
- caseMaybeM :: Monad m => m (Maybe a) -> m b -> (a -> m b) -> m b
- ifJustM :: Monad m => m (Maybe a) -> (a -> m b) -> m b -> m b
- whenJust :: Monad m => Maybe a -> (a -> m ()) -> m ()
- whenNothing :: Monad m => Maybe a -> m () -> m ()
- whenJustM :: Monad m => m (Maybe a) -> (a -> m ()) -> m ()
- whenNothingM :: Monad m => m (Maybe a) -> m () -> m ()
- allJustM :: Monad m => [m (Maybe a)] -> m (Maybe [a])
- allJustsOrNothings :: [Maybe a] -> Maybe (Maybe [a])
- module Data.Maybe

# Collection operations.

unionMaybeWith :: (a -> a -> a) -> Maybe a -> Maybe a -> Maybe a #

`unionWith` for collections of size <= 1.

unzipMaybe :: Maybe (a, b) -> (Maybe a, Maybe b) #

Unzipping a list of length <= 1.

filterMaybe :: (a -> Bool) -> a -> Maybe a #

Filtering a singleton list.

    filterMaybe p a = listToMaybe (filter p [a])

# Conditionals and loops.

forMaybe :: [a] -> (a -> Maybe b) -> [b] #

Version of `mapMaybe` with different argument ordering.

caseMaybe :: Maybe a -> b -> (a -> b) -> b #

Version of `maybe` with different argument ordering. Often, we want to case on a `Maybe`, do something interesting in the `Just` case, but only a default action in the `Nothing` case. Then, the argument ordering of `caseMaybe` is preferable.

    caseMaybe m d f = flip (maybe d) m f

# Monads and Maybe.

maybeM :: Monad m => m b -> (a -> m b) -> m (Maybe a) -> m b #

Monadic version of `maybe`.

fromMaybeM :: Monad m => m a -> m (Maybe a) -> m a #

Monadic version of `fromMaybe`.

caseMaybeM :: Monad m => m (Maybe a) -> m b -> (a -> m b) -> m b #

Monadic version of `caseMaybe`. That is, `maybeM` with a different argument ordering.

ifJustM :: Monad m => m (Maybe a) -> (a -> m b) -> m b -> m b #

`caseMaybeM` with flipped branches.

whenJust :: Monad m => Maybe a -> (a -> m ()) -> m () #

A more telling name for `forM_` for the `Maybe` collection type. Or: `caseMaybe` without the `Nothing` case.

whenNothing :: Monad m => Maybe a -> m () -> m () #

`caseMaybe` without the `Just` case.

whenJustM :: Monad m => m (Maybe a) -> (a -> m ()) -> m () #

`caseMaybeM` without the `Nothing` case.

whenNothingM :: Monad m => m (Maybe a) -> m () -> m () #

`caseMaybeM` without the `Just` case.

allJustM :: Monad m => [m (Maybe a)] -> m (Maybe [a]) #

Lazy version of `allJust . sequence`. (`allJust = mapM` for the `Maybe` monad.) Only executes monadic effect while `isJust`.

allJustsOrNothings :: [Maybe a] -> Maybe (Maybe [a]) #

Precondition: list not empty. `allJustsOrNothings [Nothing, Nothing] = Just Nothing allJustsOrNothings [Just 0, Just 1] = Just $ Just [0,1] allJustsOrNothings [Just 0, Nothing] = Nothing`

module Data.Maybe