Portability | portable |
---|---|

Stability | experimental |

Short-circuit evaluation.

- class HasFalse a where
- false :: a

- class HasTrue a where
- true :: a

- class Shortcircuit a where
- isFalse :: Shortcircuit a => a -> Bool
- if' :: Shortcircuit a => a -> b -> b -> b
- unless' :: Shortcircuit a => a -> b -> b -> b
- (??) :: Shortcircuit a => b -> b -> a -> b
- (||) :: Shortcircuit a => a -> a -> a
- (&&) :: Shortcircuit a => a -> a -> a
- firstTrueOf :: (Shortcircuit a, HasFalse a) => [a] -> a
- lastFalseOf :: (Shortcircuit a, HasTrue a) => [a] -> a
- orM :: (Monad m, Shortcircuit a) => m a -> m a -> m a
- andM :: (Monad m, Shortcircuit a) => m a -> m a -> m a
- firstTrueOfM :: (Monad m, Shortcircuit a, HasFalse a) => [m a] -> m a
- lastFalseOfM :: (Monad m, Shortcircuit a, HasTrue a) => [m a] -> m a

# Type classes

Types with a defined false value.

A false value for this type.
If `Shortcircuit`

`a`

holds, `isTrue false == False`

must hold.

Types with a defined true value.

A true value for this type.
If `Shortcircuit`

`a`

holds, `isTrue true == True`

must hold.

class Shortcircuit a whereSource

Types that support short circuits.

Shortcircuit Bool | |

Shortcircuit (Maybe a) | |

Shortcircuit (Either a b) |

# Short circuit tests

isFalse :: Shortcircuit a => a -> BoolSource

Whether the value is false-like (i.e. not true-like).

if' :: Shortcircuit a => a -> b -> b -> bSource

`if then else`

generalised to `Shortcircuit`

.

unless' :: Shortcircuit a => a -> b -> b -> bSource

The opposite of `if'`

.

(??) :: Shortcircuit a => b -> b -> a -> bSource

Like `if'`

, but with different argument order, allowing infix use.

(||) :: Shortcircuit a => a -> a -> aSource

`Prelude.||`

generalised to `Shortcircuit`

.

(&&) :: Shortcircuit a => a -> a -> aSource

`Prelude.&&`

generalised to `Shortcircuit`

.

firstTrueOf :: (Shortcircuit a, HasFalse a) => [a] -> aSource

Returns the first true-ish value from a list, or `false`

.

lastFalseOf :: (Shortcircuit a, HasTrue a) => [a] -> aSource

Returns the last false-ish value from a list, or `true`

.

# Monadic short circuits

orM :: (Monad m, Shortcircuit a) => m a -> m a -> m aSource

Short-circuit two actions, performing the second only if the first returned a false-ish value.

andM :: (Monad m, Shortcircuit a) => m a -> m a -> m aSource

Short-circuit two actions, performing the second only if the first returned a true-ish value.

firstTrueOfM :: (Monad m, Shortcircuit a, HasFalse a) => [m a] -> m aSource

Short-circuit a list of actions, performing only until a true-ish value is found, or the list exhausted.

lastFalseOfM :: (Monad m, Shortcircuit a, HasTrue a) => [m a] -> m aSource

Short-circuit a list of actions, performing only until a false-ish value is found, or the list exhausted.