Safe Haskell | Safe-Infered |
---|

A convenient set of useful conditional operators.

- if' :: Bool -> a -> a -> a
- (??) :: a -> a -> Bool -> a
- bool :: a -> a -> Bool -> a
- cond :: [(Bool, a)] -> a
- condPlus :: MonadPlus m => [(Bool, a)] -> m a
- (?.) :: Category cat => Bool -> cat a a -> cat a a
- (?<>) :: Monoid a => Bool -> a -> a
- select :: (a -> Bool) -> (a -> b) -> (a -> b) -> a -> b
- (?) :: Bool -> (Bool -> a) -> a
- (|>) :: Bool -> a -> Maybe a
- (<|) :: a -> Maybe a -> a
- (⊳) :: Bool -> a -> Maybe a
- (⊲) :: a -> Maybe a -> a
- ifM :: Monad m => m Bool -> m a -> m a -> m a
- (<||>) :: Monad m => m Bool -> m Bool -> m Bool
- (<&&>) :: Monad m => m Bool -> m Bool -> m Bool
- notM :: Monad m => m Bool -> m Bool
- condM :: Monad m => [(m Bool, m a)] -> m a
- condPlusM :: MonadPlus m => [(m Bool, m a)] -> m a
- otherwiseM :: Monad m => m Bool
- guardM :: MonadPlus m => m Bool -> m ()
- whenM :: Monad m => m Bool -> m () -> m ()
- unlessM :: Monad m => m Bool -> m () -> m ()
- selectM :: Monad m => (a -> m Bool) -> (a -> m b) -> (a -> m b) -> a -> m b

# Simple conditional operators

# 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

# 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.

For more information see http://zenzike.com/posts/2011-08-01-the-conditional-choice-operator

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