-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Basic conditional and boolean operators with monadic variants. -- -- This library provides: -- -- -- -- Feel free to send ideas and suggestions for new conditional operators -- to the maintainer. -- -- Monadic looping constructs are not included as part of this package, -- since the monad-loops package has a fairly complete collection of them -- already. @package cond @version 0.3 module Data.Algebra.Boolean -- | A class for boolean algebras. Instances of this class should obey all -- the axioms of boolean algebra. -- -- Minimal complete definition: true, false, not or -- <-->, || or &&. class Boolean b where not = (<--> false) x && y = not (x || y) x || y = not (x && y) x xor y = (x || y) && (not (x && y)) x --> y = not x || y x <--> y = (x && y) || not (x || y) true :: Boolean b => b false :: Boolean b => b not :: Boolean b => b -> b (&&) :: Boolean b => b -> b -> b (||) :: Boolean b => b -> b -> b xor :: Boolean b => b -> b -> b (-->) :: Boolean b => b -> b -> b (<-->) :: Boolean b => b -> b -> b -- | Injection from Bool into a boolean algebra. fromBool :: Boolean b => Bool -> b instance Boolean (Endo Bool) instance Boolean (Dual Bool) instance Boolean All instance Boolean Any instance Boolean Bool -- | A convenient set of useful conditional operators. module Control.Conditional -- | 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
--   
class ToBool b toBool :: ToBool b => b -> Bool -- | A simple conditional operator if' :: ToBool b => b -> a -> a -> a -- | if' with the Bool argument at the end (infixr 1). (??) :: ToBool b => a -> a -> b -> a -- | 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. bool :: ToBool b => a -> a -> b -> a -- | 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. ifM :: (ToBool b, Monad m) => m b -> m a -> m a -> m a -- | 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 b -- | Lifted conjunction. Unlike liftM2 (&&), this -- function is short-circuiting in the monad. Fixity is the same as -- && (infxr 3). (<&&>) :: (ToBool b, Boolean b, Monad m) => m b -> m b -> m b -- | Lifted boolean negation. notM :: (Boolean b, Monad m) => m b -> m b -- | Lifted boolean exclusive disjunction. xorM :: (Boolean b, Monad m) => m b -> m b -> m b -- | 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 )]
--   
cond :: ToBool b => [(b, a)] -> a -- | 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)]
--   
condPlus :: (ToBool b, MonadPlus m) => [(b, a)] -> m a -- | cond lifted to Monad. If no conditions match, a runtime -- exception is thrown. condM :: (ToBool b, Monad m) => [(m b, m a)] -> m a -- | condPlus lifted to Monad. If no conditions match, then -- mzero is returned. condPlusM :: (ToBool b, MonadPlus m) => [(m b, m a)] -> m a -- | A synonym for return true. otherwiseM :: (Boolean b, Monad m) => m b -- | 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. (?.) :: (ToBool b, Category cat) => b -> cat a a -> cat a a -- | 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
--   
(?<>) :: (ToBool b, Monoid a) => b -> a -> a -- | 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. select :: ToBool p => (a -> p) -> (a -> b) -> (a -> b) -> (a -> b) -- | select lifted to Monad. selectM :: (ToBool p, Monad m) => (a -> m p) -> (a -> m b) -> (a -> m b) -> (a -> m b) -- | 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. (?) :: b -> (b -> a) -> a -- | Right bracket of the conditional choice operator. If the predicate is -- False, returns Nothing, otherwise it returns Just -- the right-hand argument. (|>) :: ToBool b => b -> a -> Maybe a -- | Left bracket of the conditional choice operator. This is equivalent to -- fromMaybe (<|) :: a -> Maybe a -> a -- | A monadic variant of |>. (|>>) :: (ToBool b, Monad m) => m b -> m a -> m (Maybe a) -- | A monadic variant of <|. (<<|) :: Monad m => m a -> m (Maybe a) -> m a -- | Unicode rebinding of |>. (⊳) :: ToBool b => b -> a -> Maybe a -- | Unicode rebinding of <|. (⊲) :: a -> Maybe a -> a -- | Generalization of guard guard :: (ToBool b, MonadPlus m) => b -> m () -- | A variant of guard with a monadic predicate. guardM :: (ToBool b, MonadPlus m) => m b -> m () -- | Generalization of when when :: (ToBool b, MonadPlus m) => b -> m () -> m () -- | A variant of when with a monadic predicate. whenM :: (ToBool b, Monad m) => m b -> m () -> m () -- | Generalization of unless unless :: (Boolean b, ToBool b, MonadPlus m) => b -> m () -> m () -- | A variant of unless with a monadic predicate. unlessM :: (ToBool b, Boolean b, Monad m) => m b -> m () -> m () instance ToBool (Dual Bool) instance ToBool All instance ToBool Any instance ToBool Bool