cond-0.2: Basic conditional and boolean operators with monadic variants.

Control.Conditional

Description

A convenient set of useful conditional operators.

Synopsis

# Simple conditional operators

if' :: Bool -> a -> a -> aSource

A simple conditional function.

(??) :: a -> a -> Bool -> aSource

`if'` with the `Bool` argument at the end (infixr 1).

bool :: a -> a -> Bool -> aSource

A catamorphism (aka fold) for the Bool type. This is analogous to `foldr`, `maybe`, and `either`. The first argument is the false case, the second argument is the true case, and the last argument is the predicate value.

# Lisp-style conditional operators

cond :: [(Bool, a)] -> aSource

Lisp-style conditionals. If no conditions match, then a runtime exception is thrown. Here's a trivial example:

```   signum x = cond [(x > 0     , 1 )
,(x < 0     , -1)
,(otherwise , 0 )]
```

condPlus :: MonadPlus m => [(Bool, a)] -> m aSource

Lisp-style conditionals generalized over `MonadPlus`. If no conditions match, then the result is `mzero`. This is a safer variant of `cond`.

Here's a highly contrived example using `fromMaybe`:

```   signum x = fromMaybe 0 . condPlus \$ [(x > 0, 1 )
,(x < 0, -1)]
```

Alternatively, you could use the `<|` operator from Hoare's ternary conditional choice operator, like so:

```   signum x = 0 <| condPlus [(x > 0, 1 )
,(x < 0, -1)]
```

# Conditional operator on categories

(?.) :: Category cat => Bool -> cat a a -> cat a aSource

Conditional composition. If the predicate is False, `id` is returned instead of the second argument. This function, for example, can be used to conditionally add functions to a composition chain.

# Conditional operator on monoids

(?<>) :: Monoid a => Bool -> a -> aSource

Conditional monoid operator. If the predicate is `False`, the second argument is replaced with `mempty`. The fixity of this operator is one level higher than `<>`.

It can also be used to chain multiple predicates together, like this:

``` even (length ls) ?<> not (null ls) ?<> ls
```

# Conditional operator on functions

select :: (a -> Bool) -> (a -> b) -> (a -> b) -> a -> bSource

Composes a predicate function and 2 functions into a single function. The first function is called when the predicate yields True, the second when the predicate yields False.

Note that after importing Control.Monad.Instances, `select` becomes a special case of `ifM`.

# C-style ternary conditional

(?) :: Bool -> (Bool -> a) -> aSource

An operator that allows you to write C-style ternary conditionals of the form:

``` p ? t ?? f
```

Note that parentheses are required in order to chain sequences of conditionals together. This is probably a good thing.

# Hoare's conditional choice operator

The following operators form a ternary conditional of the form

``` t <| p |> f
```

These operators chain with right associative fixity. This allows chaining of conditions, where the result of the expression is the value to the left of the first predicate that succeeds.

(|>) :: Bool -> a -> Maybe aSource

Right bracket of the conditional choice operator. If the predicate is `False`, returns `Nothing`, otherwise it returns `Just` the right-hand argument.

(<|) :: a -> Maybe a -> aSource

Left bracket of the conditional choice operator. This is equivalent to `fromMaybe`

## Unicode variants

Intended to resemble the notation used in Tony Hoare's Unified Theories of Programming.

(⊳) :: Bool -> a -> Maybe aSource

Unicode rebinding of `<|`.

(⊲) :: a -> Maybe a -> aSource

Unicode rebinding of `|>`.

# Lifted conditional and boolean operators

ifM :: Monad m => m Bool -> m a -> m a -> m aSource

`if'` lifted to `Monad`. Unlike `liftM3` `if'`, this is short-circuiting in the monad, such that only the predicate action and one of the remaining argument actions are executed.

(<||>) :: Monad m => m Bool -> m Bool -> m BoolSource

Lifted boolean or. Unlike `liftM2` (`||`), This function is short-circuiting in the monad. Fixity is the same as `||` (infixr 2).

(<&&>) :: Monad m => m Bool -> m Bool -> m BoolSource

Lifted boolean and. Unlike `liftM2` (`&&`), this function is short-circuiting in the monad. Fixity is the same as `&&` (infxr 3).

notM :: Monad m => m Bool -> m BoolSource

Lifted boolean negation.

condM :: Monad m => [(m Bool, m a)] -> m aSource

`cond` lifted to `Monad`. If no conditions match, a runtime exception is thrown.

condPlusM :: MonadPlus m => [(m Bool, m a)] -> m aSource

`condPlus` lifted to `Monad`. If no conditions match, then `mzero` is returned.

otherwiseM :: Monad m => m BoolSource

A synonym for `return` `True`.

guardM :: MonadPlus m => m Bool -> m ()Source

A variant of `guard` with a monadic predicate.

whenM :: Monad m => m Bool -> m () -> m ()Source

A variant of `when` with a monadic predicate.

unlessM :: Monad m => m Bool -> m () -> m ()Source

A variant of `unless` with a monadic predicate.

selectM :: Monad m => (a -> m Bool) -> (a -> m b) -> (a -> m b) -> a -> m bSource

`select` lifted to `Monad`.