Safe Haskell | Safe-Infered |
---|

A convenient set of useful conditional operators.

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

# Conversion typeclass

# Basic conditional operators

# 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)]

# 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

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

.

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

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`

## Lifted conditional choice

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

t <<| p |>> f

## Unicode variants

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