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

Control.Conditional

Description

A convenient set of useful conditional operators.

Synopsis

# Conversion typeclass

class ToBool b whereSource

Conversion of values to `Bool`.

Instances of `ToBool` that are also `Boolean` should obey the following laws:

``` p || q = if toBool p then true else q
```
``` p && q = if toBool p then q else false
```

Methods

toBool :: b -> BoolSource

Instances

 ToBool Bool ToBool All ToBool Any ToBool (Dual Bool)

# Basic conditional operators

if' :: ToBool b => b -> a -> a -> aSource

A simple conditional operator

(??) :: ToBool b => a -> a -> b -> aSource

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

bool :: ToBool b => a -> a -> b -> aSource

A catamorphism (aka fold) for booleans. 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.

ifM :: (ToBool b, Monad m) => m b -> 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.

(<||>) :: (ToBool b, Boolean b, Monad m) => m b -> m b -> m bSource

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

(<&&>) :: (ToBool b, Boolean b, Monad m) => m b -> m b -> m bSource

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

notM :: (Boolean b, Monad m) => m b -> m bSource

Lifted boolean negation.

xorM :: (Boolean b, Monad m) => m b -> m b -> m bSource

Lifted boolean exclusive disjunction.

# Lisp-style conditional operators

cond :: ToBool b => [(b, 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 :: (ToBool b, MonadPlus m) => [(b, 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)]
```

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

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

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

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

otherwiseM :: (Boolean b, Monad m) => m bSource

A synonym for `return` `true`.

# Conditional operator on categories

(?.) :: (ToBool b, Category cat) => b -> 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

(?<>) :: (ToBool b, Monoid a) => b -> 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 :: ToBool p => (a -> p) -> (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`.

selectM :: (ToBool p, Monad m) => (a -> m p) -> (a -> m b) -> (a -> m b) -> a -> m bSource

`select` lifted to `Monad`.

# C-style ternary conditional

(?) :: b -> (b -> 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.

(|>) :: ToBool b => b -> 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`

## Lifted conditional choice

In addition, you can write lifted conditionals of the form:

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

(|>>) :: (ToBool b, Monad m) => m b -> m a -> m (Maybe a)Source

A monadic variant of `|>`.

(<<|) :: Monad m => m a -> m (Maybe a) -> m aSource

A monadic variant of `<|`.

## Unicode variants

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

(⊳) :: ToBool b => b -> a -> Maybe aSource

Unicode rebinding of `|>`.

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

Unicode rebinding of `<|`.

guard :: (ToBool b, MonadPlus m) => b -> m ()Source

Generalization of `guard`

guardM :: (ToBool b, MonadPlus m) => m b -> m ()Source

A variant of `guard` with a monadic predicate.

when :: (ToBool b, MonadPlus m) => b -> m () -> m ()Source

Generalization of `when`

whenM :: (ToBool b, Monad m) => m b -> m () -> m ()Source

A variant of `when` with a monadic predicate.

unless :: (Boolean b, ToBool b, MonadPlus m) => b -> m () -> m ()Source

Generalization of `unless`

unlessM :: (ToBool b, Boolean b, Monad m) => m b -> m () -> m ()Source

A variant of `unless` with a monadic predicate.