-- 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.4.1 module Data.Algebra.Boolean -- | A class for boolean algebras. Instances of this class are expected to -- obey all the laws of boolean algebra. -- -- Minimal complete definition: true or false, not -- or <-->, || or &&. class Boolean b where nand = not . and nor = not . or true = not false false = not true 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) and = foldl' (&&) true or = foldl' (||) false all p = foldl' f true where f a b = a && p b any p = foldl' f false where f a b = a || p b 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 and :: (Boolean b, Foldable t) => t b -> b or :: (Boolean b, Foldable t) => t b -> b nand :: (Boolean b, Foldable t) => t b -> b all :: (Boolean b, Foldable t) => (a -> b) -> t a -> b any :: (Boolean b, Foldable t) => (a -> b) -> t a -> b nor :: (Boolean b, Foldable t) => t b -> b -- | Injection from Bool into a boolean algebra. fromBool :: Boolean b => Bool -> b -- | A newtype wrapper that derives a Boolean instance from any type -- that is both a Bits instance and a Num instance, such -- that boolean logic operations on the Bitwise wrapper correspond -- to bitwise logic operations on the inner type. It should be noted that -- false is defined as Bitwise 0 and true is defined -- as not false. -- -- In addition, a number of other classes are automatically derived from -- the inner type. These classes were chosen on the basis that many other -- Bits instances defined in base are also instances of these -- classes. newtype Bitwise a Bitwise :: a -> Bitwise a getBits :: Bitwise a -> a instance Typeable1 Bitwise instance Num a => Num (Bitwise a) instance Bits a => Bits (Bitwise a) instance Eq a => Eq (Bitwise a) instance Ord a => Ord (Bitwise a) instance Bounded a => Bounded (Bitwise a) instance Enum a => Enum (Bitwise a) instance Show a => Show (Bitwise a) instance Read a => Read (Bitwise a) instance Real a => Real (Bitwise a) instance Integral a => Integral (Bitwise a) instance Data a => Data (Bitwise a) instance Ix a => Ix (Bitwise a) instance Storable a => Storable (Bitwise a) instance PrintfArg a => PrintfArg (Bitwise a) instance (Num a, Bits a) => Boolean (Bitwise a) instance (Boolean x, Boolean y) => Boolean (x, y) 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 bool toBool :: ToBool bool => bool -> Bool -- | A simple conditional operator if' :: ToBool bool => bool -> a -> a -> a -- | if' with the Bool argument at the end (infixr 1). (??) :: ToBool bool => a -> a -> bool -> 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 bool => a -> a -> bool -> 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 bool, Monad m) => m bool -> 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 bool, Boolean bool, Monad m) => m bool -> m bool -> m bool -- | Lifted conjunction. Unlike liftM2 (&&), this -- function is short-circuiting in the monad. Fixity is the same as -- && (infxr 3). (<&&>) :: (ToBool bool, Boolean bool, Monad m) => m bool -> m bool -> m bool -- | Lifted boolean negation. notM :: (Boolean bool, Monad m) => m bool -> m bool -- | Lifted boolean exclusive disjunction. xorM :: (Boolean bool, Monad m) => m bool -> m bool -> m bool -- | 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 bool => [(bool, a)] -> a -- | Analogous to the cond function with a default value supplied, -- which will be used when no condition in the list is matched. condDefault :: ToBool bool => a -> [(bool, 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 bool, MonadPlus m) => [(bool, a)] -> m a -- | cond lifted to Monad. If no conditions match, a runtime -- exception is thrown. condM :: (ToBool bool, Monad m) => [(m bool, m a)] -> m a -- | condPlus lifted to Monad. If no conditions match, then -- mzero is returned. condPlusM :: (ToBool bool, MonadPlus m) => [(m bool, m a)] -> m a -- | A synonym for return true. otherwiseM :: (Boolean bool, Monad m) => m bool -- | 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 bool, Category cat) => bool -> 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 bool, Monoid a) => bool -> 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 bool => (a -> bool) -> (a -> b) -> (a -> b) -> (a -> b) -- | select lifted to Monad. selectM :: (ToBool bool, Monad m) => (a -> m bool) -> (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 -- True, returns Nothing, otherwise it returns Just -- the right-hand argument. (|>) :: ToBool bool => bool -> a -> Maybe a -- | Left bracket of the conditional choice operator. This is equivalent to -- fromMaybe (<|) :: a -> Maybe a -> a -- | A monadic variant of |>. (|>>) :: (ToBool bool, Monad m) => m bool -> m a -> m (Maybe a) -- | A monadic variant of <|. (<<|) :: Monad m => m a -> m (Maybe a) -> m a -- | Unicode rebinding of |>. (⊳) :: ToBool bool => bool -> a -> Maybe a -- | Unicode rebinding of <|. (⊲) :: a -> Maybe a -> a -- | Generalization of guard guard :: (ToBool bool, MonadPlus m) => bool -> m () -- | A variant of guard with a monadic predicate. guardM :: (ToBool bool, MonadPlus m) => m bool -> m () -- | Generalization of when when :: (ToBool bool, MonadPlus m) => bool -> m () -> m () -- | A variant of when with a monadic predicate. whenM :: (ToBool bool, Monad m) => m bool -> m () -> m () -- | Generalization of unless unless :: (Boolean bool, ToBool bool, MonadPlus m) => bool -> m () -> m () -- | A variant of unless with a monadic predicate. unlessM :: (ToBool bool, Boolean bool, Monad m) => m bool -> m () -> m () instance ToBool (Dual Bool) instance ToBool All instance ToBool Any instance ToBool Bool