Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
This module replaces the monomorphic boolean operators from Prelude with a set of polymorphic operators.
Synopsis
- class Boolean a where
- class Boolean a => BooleanMonoid a where
- newtype ApplicativeBoolean f bool = ApplicativeBoolean (f bool)
- or :: (Container c, BooleanMonoid (Element c)) => c -> Element c
- and :: (Container c, BooleanMonoid (Element c)) => c -> Element c
- or1 :: Boolean a => NonEmpty a -> a
- and1 :: Boolean a => NonEmpty a -> a
- newtype Any a = Any {
- getAny :: a
- newtype All a = All {
- getAll :: a
- any :: (Container c, BooleanMonoid b) => (Element c -> b) -> c -> b
- all :: (Container c, BooleanMonoid b) => (Element c -> b) -> c -> b
- any1 :: Boolean b => (a -> b) -> NonEmpty a -> b
- all1 :: Boolean b => (a -> b) -> NonEmpty a -> b
Documentation
class Boolean a where Source #
Generalized boolean operators.
This is useful for defining things that behave like booleans, e.g. predicates, or EDSL for predicates.
>>>
Yay && Nay
Nay>>>
and1 $ Yay :| replicate 9 Yay
Yay
There are also instances for these types lifted into IO
and (->) a
:
>>>
(const Yay) && (const Nay) $ ()
Nay>>>
(const Yay) || (const Nay) $ ()
Yay
Instances
Boolean Bool Source # | |
Boolean bool => Boolean (IO bool) Source # | |
Boolean bool => Boolean (a -> bool) Source # | |
(Applicative f, Boolean bool) => Boolean (ApplicativeBoolean f bool) Source # | |
Defined in Morley.Prelude.Boolean (&&) :: ApplicativeBoolean f bool -> ApplicativeBoolean f bool -> ApplicativeBoolean f bool Source # (||) :: ApplicativeBoolean f bool -> ApplicativeBoolean f bool -> ApplicativeBoolean f bool Source # not :: ApplicativeBoolean f bool -> ApplicativeBoolean f bool Source # |
class Boolean a => BooleanMonoid a where Source #
This is useful to complete the isomorphism between regular and generalized booleans. It's a separate class because not all boolean-like things form a monoid.
>>>
or $ replicate 10 Nay
Nay
Instances
BooleanMonoid Bool Source # | |
BooleanMonoid bool => BooleanMonoid (IO bool) Source # | |
BooleanMonoid bool => BooleanMonoid (a -> bool) Source # | |
(Applicative f, BooleanMonoid bool) => BooleanMonoid (ApplicativeBoolean f bool) Source # | |
Defined in Morley.Prelude.Boolean false :: ApplicativeBoolean f bool Source # true :: ApplicativeBoolean f bool Source # |
newtype ApplicativeBoolean f bool Source #
A newtype for deriving a Boolean
instance for any Applicative
type
constructor using DerivingVia
.
ApplicativeBoolean (f bool) |
Instances
or :: (Container c, BooleanMonoid (Element c)) => c -> Element c Source #
Generalized version of or
.
>>>
or $ replicate 10 Nay
Nay>>>
or $ Yay : replicate 10 Nay
Yay
and :: (Container c, BooleanMonoid (Element c)) => c -> Element c Source #
Generalized version of and
.
>>>
and $ replicate 10 Yay
Yay>>>
and $ Nay : replicate 10 Yay
Nay
or1 :: Boolean a => NonEmpty a -> a Source #
A version of or
that works on NonEmpty
, thus doesn't require
BooleanMonoid
instance.
>>>
or1 $ Yay :| [Nay]
Yay
and1 :: Boolean a => NonEmpty a -> a Source #
A version of and
that works on NonEmpty
, thus doesn't require
BooleanMonoid
instance.
>>>
and1 $ Yay :| [Nay]
Nay
A generalized version of Any
monoid wrapper.
>>>
Any Nay <> Any Nay
Any {getAny = Nay}>>>
Any Yay <> Any Nay
Any {getAny = Yay}>>>
Any Yay <> Any Yay
Any {getAny = Yay}
Instances
Data a => Data (Any a) Source # | |
Defined in Morley.Prelude.Boolean gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Any a -> c (Any a) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Any a) # dataTypeOf :: Any a -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Any a)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Any a)) # gmapT :: (forall b. Data b => b -> b) -> Any a -> Any a # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Any a -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Any a -> r # gmapQ :: (forall d. Data d => d -> u) -> Any a -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Any a -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Any a -> m (Any a) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Any a -> m (Any a) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Any a -> m (Any a) # | |
BooleanMonoid a => Monoid (Any a) Source # | |
Boolean a => Semigroup (Any a) Source # | |
Bounded a => Bounded (Any a) Source # | |
Enum a => Enum (Any a) Source # | |
Defined in Morley.Prelude.Boolean | |
Generic (Any a) Source # | |
Read a => Read (Any a) Source # | |
Show a => Show (Any a) Source # | |
Eq a => Eq (Any a) Source # | |
Ord a => Ord (Any a) Source # | |
type Rep (Any a) Source # | |
Defined in Morley.Prelude.Boolean |
A generalized version of All
monoid wrapper.
>>>
All Nay <> All Nay
All {getAll = Nay}>>>
All Yay <> All Nay
All {getAll = Nay}>>>
All Yay <> All Yay
All {getAll = Yay}
Instances
Data a => Data (All a) Source # | |
Defined in Morley.Prelude.Boolean gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> All a -> c (All a) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (All a) # dataTypeOf :: All a -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (All a)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (All a)) # gmapT :: (forall b. Data b => b -> b) -> All a -> All a # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> All a -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> All a -> r # gmapQ :: (forall d. Data d => d -> u) -> All a -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> All a -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> All a -> m (All a) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> All a -> m (All a) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> All a -> m (All a) # | |
BooleanMonoid a => Monoid (All a) Source # | |
Boolean a => Semigroup (All a) Source # | |
Bounded a => Bounded (All a) Source # | |
Enum a => Enum (All a) Source # | |
Defined in Morley.Prelude.Boolean | |
Generic (All a) Source # | |
Read a => Read (All a) Source # | |
Show a => Show (All a) Source # | |
Eq a => Eq (All a) Source # | |
Ord a => Ord (All a) Source # | |
type Rep (All a) Source # | |
Defined in Morley.Prelude.Boolean |
any :: (Container c, BooleanMonoid b) => (Element c -> b) -> c -> b Source #
Generalized any
.
>>>
any (\x -> if x > 50 then Yay else Nay) [1..100]
Yay
all :: (Container c, BooleanMonoid b) => (Element c -> b) -> c -> b Source #
Generalized all
.
>>>
all (\x -> if x > 50 then Yay else Nay) [1..100]
Nay
any1 :: Boolean b => (a -> b) -> NonEmpty a -> b Source #
A version of any
that works on NonEmpty
, thus doesn't require
BooleanMonoid
instance.
>>>
any1 (\x -> if x > 50 then Yay else Nay) $ 50 :| replicate 10 0
Nay
all1 :: Boolean b => (a -> b) -> NonEmpty a -> b Source #
A version of all
that works on NonEmpty
, thus doesn't require
BooleanMonoid
instance.
>>>
all1 (\x -> if x > 50 then Yay else Nay) $ 100 :| replicate 10 51
Yay
Example definitions
>>>
:{
data Vote = Yay | Nay deriving Show -- instance Boolean Vote where Yay && Yay = Yay _ && _ = Nay Nay || Nay = Nay _ || _ = Yay not Yay = Nay not Nay = Yay -- instance BooleanMonoid Vote where true = Yay false = Nay :}