-- 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.5.0 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 (<-->, false), || or &&. class Boolean b -- | Truth value, defined as the top of the bounded lattice true :: Boolean b => b -- | False value, defined as the bottom of the bounded lattice. false :: Boolean b => b -- | Logical negation. not :: Boolean b => b -> b -- | Logical conjunction. (infixr 3) (&&) :: Boolean b => b -> b -> b -- | Logical inclusive disjunction. (infixr 2) (||) :: Boolean b => b -> b -> b -- | Logical exclusive disjunction. (infixr 1) xor :: Boolean b => b -> b -> b -- | Logical implication. (infixr 1) (-->) :: Boolean b => b -> b -> b -- | Logical biconditional. (infixr 1) (<-->) :: Boolean b => b -> b -> b infixr 2 || infixr 3 && infixr 1 `xor` infixr 1 <--> infixr 1 --> -- | 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 -- | The logical conjunction of several values. and :: (Boolean b, Foldable t) => t b -> b -- | The logical disjunction of several values. or :: (Boolean b, Foldable t) => t b -> b -- | The negated logical conjunction of several values. -- --
--   nand = not . and
--   
nand :: (Boolean b, Foldable t) => t b -> b -- | The negated logical disjunction of several values. -- --
--   nor = not . or
--   
nor :: (Boolean b, Foldable t) => t b -> b -- | The logical disjunction of the mapping of a function over several -- values. any :: (Boolean b, Foldable t) => (a -> b) -> t a -> b -- | The logical conjunction of the mapping of a function over several -- values. all :: (Boolean b, Foldable t) => (a -> b) -> t a -> b newtype Opp a Opp :: a -> Opp a [getOpp] :: Opp a -> a -- | A boolean algebra regarded as a monoid under disjunction newtype AnyB b AnyB :: b -> AnyB b [getAnyB] :: AnyB b -> b -- | A boolean algebra regarded as a monoid under conjunction newtype AllB b AllB :: b -> AllB b [getAllB] :: AllB b -> b -- | A boolean algebra regarded as a monoid under exclusive or newtype XorB b XorB :: b -> XorB b [getXorB] :: XorB b -> b -- | A boolean algebra regarded as a monoid under equivalence newtype EquivB b EquivB :: b -> EquivB b [getEquivB] :: EquivB b -> b instance GHC.Show.Show b => GHC.Show.Show (Data.Algebra.Boolean.AnyB b) instance GHC.Classes.Ord b => GHC.Classes.Ord (Data.Algebra.Boolean.AnyB b) instance GHC.Classes.Eq b => GHC.Classes.Eq (Data.Algebra.Boolean.AnyB b) instance GHC.Show.Show b => GHC.Show.Show (Data.Algebra.Boolean.AllB b) instance GHC.Classes.Ord b => GHC.Classes.Ord (Data.Algebra.Boolean.AllB b) instance GHC.Classes.Eq b => GHC.Classes.Eq (Data.Algebra.Boolean.AllB b) instance GHC.Show.Show b => GHC.Show.Show (Data.Algebra.Boolean.XorB b) instance GHC.Classes.Ord b => GHC.Classes.Ord (Data.Algebra.Boolean.XorB b) instance GHC.Classes.Eq b => GHC.Classes.Eq (Data.Algebra.Boolean.XorB b) instance GHC.Show.Show b => GHC.Show.Show (Data.Algebra.Boolean.EquivB b) instance GHC.Classes.Ord b => GHC.Classes.Ord (Data.Algebra.Boolean.EquivB b) instance GHC.Classes.Eq b => GHC.Classes.Eq (Data.Algebra.Boolean.EquivB b) instance GHC.Show.Show a => GHC.Show.Show (Data.Algebra.Boolean.Opp a) instance GHC.Classes.Ord a => GHC.Classes.Ord (Data.Algebra.Boolean.Opp a) instance GHC.Classes.Eq a => GHC.Classes.Eq (Data.Algebra.Boolean.Opp a) instance Text.Printf.PrintfArg a => Text.Printf.PrintfArg (Data.Algebra.Boolean.Bitwise a) instance Foreign.Storable.Storable a => Foreign.Storable.Storable (Data.Algebra.Boolean.Bitwise a) instance GHC.Ix.Ix a => GHC.Ix.Ix (Data.Algebra.Boolean.Bitwise a) instance Data.Data.Data a => Data.Data.Data (Data.Algebra.Boolean.Bitwise a) instance GHC.Real.Integral a => GHC.Real.Integral (Data.Algebra.Boolean.Bitwise a) instance GHC.Real.Real a => GHC.Real.Real (Data.Algebra.Boolean.Bitwise a) instance GHC.Read.Read a => GHC.Read.Read (Data.Algebra.Boolean.Bitwise a) instance GHC.Show.Show a => GHC.Show.Show (Data.Algebra.Boolean.Bitwise a) instance GHC.Enum.Enum a => GHC.Enum.Enum (Data.Algebra.Boolean.Bitwise a) instance GHC.Enum.Bounded a => GHC.Enum.Bounded (Data.Algebra.Boolean.Bitwise a) instance GHC.Classes.Ord a => GHC.Classes.Ord (Data.Algebra.Boolean.Bitwise a) instance GHC.Classes.Eq a => GHC.Classes.Eq (Data.Algebra.Boolean.Bitwise a) instance GHC.Bits.Bits a => GHC.Bits.Bits (Data.Algebra.Boolean.Bitwise a) instance GHC.Num.Num a => GHC.Num.Num (Data.Algebra.Boolean.Bitwise a) instance (GHC.Num.Num a, GHC.Bits.Bits a) => Data.Algebra.Boolean.Boolean (Data.Algebra.Boolean.Bitwise a) instance Data.Algebra.Boolean.Boolean a => Data.Algebra.Boolean.Boolean (Data.Algebra.Boolean.Opp a) instance Data.Algebra.Boolean.Boolean b => GHC.Base.Semigroup (Data.Algebra.Boolean.EquivB b) instance Data.Algebra.Boolean.Boolean b => GHC.Base.Monoid (Data.Algebra.Boolean.EquivB b) instance Data.Algebra.Boolean.Boolean b => GHC.Base.Semigroup (Data.Algebra.Boolean.XorB b) instance Data.Algebra.Boolean.Boolean b => GHC.Base.Monoid (Data.Algebra.Boolean.XorB b) instance Data.Algebra.Boolean.Boolean b => GHC.Base.Semigroup (Data.Algebra.Boolean.AllB b) instance Data.Algebra.Boolean.Boolean b => GHC.Base.Monoid (Data.Algebra.Boolean.AllB b) instance Data.Algebra.Boolean.Boolean b => GHC.Base.Semigroup (Data.Algebra.Boolean.AnyB b) instance Data.Algebra.Boolean.Boolean b => GHC.Base.Monoid (Data.Algebra.Boolean.AnyB b) instance Data.Algebra.Boolean.Boolean GHC.Types.Bool instance Data.Algebra.Boolean.Boolean Data.Semigroup.Internal.Any instance Data.Algebra.Boolean.Boolean Data.Semigroup.Internal.All instance Data.Algebra.Boolean.Boolean (Data.Semigroup.Internal.Dual GHC.Types.Bool) instance Data.Algebra.Boolean.Boolean b => Data.Algebra.Boolean.Boolean (a -> b) instance Data.Algebra.Boolean.Boolean a => Data.Algebra.Boolean.Boolean (Data.Semigroup.Internal.Endo a) instance Data.Algebra.Boolean.Boolean () instance (Data.Algebra.Boolean.Boolean x, Data.Algebra.Boolean.Boolean y) => Data.Algebra.Boolean.Boolean (x, y) instance (Data.Algebra.Boolean.Boolean x, Data.Algebra.Boolean.Boolean y, Data.Algebra.Boolean.Boolean z) => Data.Algebra.Boolean.Boolean (x, y, z) -- | 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 infixr 1 ?? -- | 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 infixr 2 <||> -- | 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 infixr 3 <&&> -- | 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 infixr 9 ?. -- | 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 infixr 7 ?<> -- | 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 infixr 0 ? -- | 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 infixr 0 |> -- | Left bracket of the conditional choice operator. This is equivalent to -- fromMaybe (<|) :: a -> Maybe a -> a infixr 0 <| -- | A monadic variant of |>. (|>>) :: (ToBool bool, Monad m) => m bool -> m a -> m (Maybe a) infixr 0 |>> -- | A monadic variant of <|. (<<|) :: Monad m => m a -> m (Maybe a) -> m a infixr 0 <<| -- | Unicode rebinding of |>. (⊳) :: ToBool bool => bool -> a -> Maybe a infixr 0 ⊳ -- | Unicode rebinding of <|. (⊲) :: a -> Maybe a -> a infixr 0 ⊲ -- | 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, Monad 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, Monad m) => bool -> m () -> m () -- | A variant of unless with a monadic predicate. unlessM :: (ToBool bool, Boolean bool, Monad m) => m bool -> m () -> m () instance Control.Conditional.ToBool GHC.Types.Bool instance Control.Conditional.ToBool Data.Semigroup.Internal.Any instance Control.Conditional.ToBool Data.Semigroup.Internal.All instance Control.Conditional.ToBool (Data.Semigroup.Internal.Dual GHC.Types.Bool)