Control.Monad.Plus

monadplus-1.4.2: Haskell98 partial maps and filters over MonadPlus.

Copyright | (c) Hans Hoglund 2012
License | BSD-style
Maintainer | hans@hanshoglund.se
Stability | experimental
Portability | non-portable (TF,GNTD)
Safe Haskell | None
Language | Haskell98

Control.Monad.Plus

Contents

- Basics
- Constructing
- Filtering
  - Special filters
- Special maps
- Utility

Description

Partial maps and filters over `MonadPlus` instances. The basic idea here is that the monad interface together with the monoidal structure of `MonadPlus` is enough to implement partial maps and filters (i.e. `mmapMaybe` and `mfilter`).

This is especially useful for sequential structures such as event lists, tracks etc.

Inspired by the following blog post:

- http://conal.net/blog/posts/a-handy-generalized-filter

Synopsis

- module Control.Monad
- msum :: (Foldable t, MonadPlus m) => t (m a) -> m a
- msum' :: (MonadPlus m, Foldable t) => t (m a) -> m a
- mfold :: (MonadPlus m, Foldable t) => t a -> m a
- mfromList :: MonadPlus m => [a] -> m a
- mfromMaybe :: MonadPlus m => Maybe a -> m a
- mreturn :: MonadPlus m => (a -> Maybe b) -> a -> m b
- mpartition :: MonadPlus m => (a -> Bool) -> m a -> (m a, m a)
- mscatter :: MonadPlus m => m [b] -> m b
- mscatter' :: (MonadPlus m, Foldable t) => m (t b) -> m b
- mcatMaybes :: MonadPlus m => m (Maybe a) -> m a
- mlefts :: MonadPlus m => m (Either a b) -> m a
- mrights :: MonadPlus m => m (Either a b) -> m b
- mpartitionEithers :: MonadPlus m => m (Either a b) -> (m a, m b)
- mmapMaybe :: MonadPlus m => (a -> Maybe b) -> m a -> m b
- mconcatMap :: MonadPlus m => (a -> [b]) -> m a -> m b
- mconcatMap' :: (MonadPlus m, Foldable t) => (a -> t b) -> m a -> m b
- newtype Partial a b = Partial {
  - getPartial :: a -> Maybe b
  }
- partial :: (a -> Bool) -> a -> Maybe a
- predicate :: (a -> Maybe a) -> a -> Bool
- always :: (a -> b) -> a -> Maybe b
- never :: a -> Maybe c

# Basics

module Control.Monad

msum :: (Foldable t, MonadPlus m) => t (m a) -> m a #

The sum of a collection of actions, generalizing `concat`. As of base 4.8.0.0, `msum` is just `asum`, specialized to `MonadPlus`.

msum' :: (MonadPlus m, Foldable t) => t (m a) -> m a #

This generalizes the list-based `concat` function.

# Constructing

mfold :: (MonadPlus m, Foldable t) => t a -> m a #

Fold a value into an arbitrary `MonadPlus` type.

This function generalizes the `toList` function.

mfromList :: MonadPlus m => [a] -> m a #

Translate a list to an arbitrary `MonadPlus` type.

This function generalizes the `listToMaybe` function.

mfromMaybe :: MonadPlus m => Maybe a -> m a #

Translate maybe to an arbitrary `MonadPlus` type.

This function generalizes the `maybeToList` function.

mreturn :: MonadPlus m => (a -> Maybe b) -> a -> m b #

Convert a partial function to a function returning an arbitrary `MonadPlus` type.

# Filtering

mpartition :: MonadPlus m => (a -> Bool) -> m a -> (m a, m a) #

The `partition` function takes a predicate a list and returns the pair of lists of elements which do and do not satisfy the predicate, respectively; i.e.,

    partition p xs == (filter p xs, filter (not . p) xs)

This function generalizes the `partition` function.

## Special filters

mscatter :: MonadPlus m => m [b] -> m b #

Join list elements together.

This function generalizes the `catMaybes` function.

mscatter' :: (MonadPlus m, Foldable t) => m (t b) -> m b #

Join foldable elements together.

This function generalizes the `catMaybes` function.

mcatMaybes :: MonadPlus m => m (Maybe a) -> m a #

Pass through `Just` elements.

This function generalizes the `catMaybes` function.

mlefts :: MonadPlus m => m (Either a b) -> m a #

Pass through `Left` elements.

This function generalizes the `lefts` function.

mrights :: MonadPlus m => m (Either a b) -> m b #

Pass through `Right` elements.

This function generalizes the `rights` function.

mpartitionEithers :: MonadPlus m => m (Either a b) -> (m a, m b) #

Separate `Left` and `Right` elements.

This function generalizes the `partitionEithers` function.

# Special maps

mmapMaybe :: MonadPlus m => (a -> Maybe b) -> m a -> m b #

Modify or discard a value.

This function generalizes the `mapMaybe` function.

mconcatMap :: MonadPlus m => (a -> [b]) -> m a -> m b #

Modify, discard or spawn values.

This function generalizes the `concatMap` function.

mconcatMap' :: (MonadPlus m, Foldable t) => (a -> t b) -> m a -> m b #

Modify, discard or spawn values.

This function generalizes the `concatMap` function.

# Utility

newtype Partial a b #

Wrapper for partial functions with `MonadPlus` instance.

Constructors

Partial |
Fields getPartial :: a -> Maybe b

Instances

Monad (Partial r) # |
Methods (>>=) :: Partial r a -> (a -> Partial r b) -> Partial r b # (>>) :: Partial r a -> Partial r b -> Partial r b # return :: a -> Partial r a # fail :: String -> Partial r a #
Functor (Partial r) # |
Methods fmap :: (a -> b) -> Partial r a -> Partial r b # (<$) :: a -> Partial r b -> Partial r a #
Applicative (Partial r) # |
Methods pure :: a -> Partial r a # (<*>) :: Partial r (a -> b) -> Partial r a -> Partial r b # (*>) :: Partial r a -> Partial r b -> Partial r b # (<*) :: Partial r a -> Partial r b -> Partial r a #
Alternative (Partial r) # |
Methods empty :: Partial r a # (<|>) :: Partial r a -> Partial r a -> Partial r a # some :: Partial r a -> Partial r [a] # many :: Partial r a -> Partial r [a] #
MonadPlus (Partial r) # |
Methods mzero :: Partial r a # mplus :: Partial r a -> Partial r a -> Partial r a #
Monoid (Partial a b) # |
Methods mempty :: Partial a b # mappend :: Partial a b -> Partial a b -> Partial a b # mconcat :: [Partial a b] -> Partial a b #

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

Convert a predicate to a partial function.

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

Convert a partial function to a predicate.

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

Convert a total function to a partial function.

never :: a -> Maybe c #

Make a partial function that always rejects its input.