Boolean-0.2.4: Generalized booleans and numbers

Copyright | (c) Conal Elliott 2009-2012
License | BSD3
Maintainer | conal@conal.net
Stability | experimental
Safe Haskell | Safe-Inferred
Language | Haskell98

Data.Boolean

Description

Some classes for generalized boolean operations.

In this design, for if-then-else, equality and inequality tests, the boolean type depends on the value type.

I also tried using a unary type constructor class. The class doesn't work for regular booleans, so generality is lost. Also, we'd probably have to wire class constraints in like: `(==*) :: Eq a => f Bool -> f a -> f a -> f a`, which disallows situations needing additional constraints, e.g., Show.

Starting with 0.1.0, this package uses type families. Up to version 0.0.2, it used MPTCs with functional dependencies. My thanks to Andy Gill for suggesting & helping with the change.

Synopsis

- class Boolean b where
  - true :: b
  - false :: b
  - notB :: b -> b
  - (&&*) :: b -> b -> b
  - (||*) :: b -> b -> b
- type family BooleanOf a
- class Boolean (BooleanOf a) => IfB a where
  - ifB :: bool ~ BooleanOf a => bool -> a -> a -> a
- boolean :: (IfB a, bool ~ BooleanOf a) => a -> a -> bool -> a
- cond :: (Applicative f, IfB a, bool ~ BooleanOf a) => f bool -> f a -> f a -> f a
- crop :: (Applicative f, Monoid (f a), IfB a, bool ~ BooleanOf a) => f bool -> f a -> f a
- class Boolean (BooleanOf a) => EqB a where
  - (==*) :: bool ~ BooleanOf a => a -> a -> bool
  - (/=*) :: bool ~ BooleanOf a => a -> a -> bool
- class Boolean (BooleanOf a) => OrdB a where
  - (<*) :: bool ~ BooleanOf a => a -> a -> bool
  - (<=*) :: bool ~ BooleanOf a => a -> a -> bool
  - (>*) :: bool ~ BooleanOf a => a -> a -> bool
  - (>=*) :: bool ~ BooleanOf a => a -> a -> bool
- minB :: (IfB a, OrdB a) => a -> a -> a
- maxB :: (IfB a, OrdB a) => a -> a -> a
- sort2B :: (IfB a, OrdB a) => (a, a) -> (a, a)
- guardedB :: (IfB b, bool ~ BooleanOf b) => bool -> [(bool, b)] -> b -> b
- caseB :: (IfB b, bool ~ BooleanOf b) => a -> [(a -> bool, b)] -> b -> b

# Documentation

class Boolean b where Source #

Generalized boolean class

Methods

true :: b Source #

false :: b Source #

notB :: b -> b Source #

(&&*) :: b -> b -> b infixr 3 Source #

(||*) :: b -> b -> b infixr 2 Source #

#### Instances

Instances details

Boolean Bool Source # |
Instance details Defined in Data.Boolean Methods true :: Bool Source # false :: Bool Source # notB :: Bool -> Bool Source # (&&*) :: Bool -> Bool -> Bool Source # (||*) :: Bool -> Bool -> Bool Source #
Boolean bool => Boolean (z -> bool) Source # |
Instance details Defined in Data.Boolean Methods true :: z -> bool Source # false :: z -> bool Source # notB :: (z -> bool) -> z -> bool Source # (&&*) :: (z -> bool) -> (z -> bool) -> z -> bool Source # (||*) :: (z -> bool) -> (z -> bool) -> z -> bool Source #

type family BooleanOf a Source #

`BooleanOf` computed the boolean analog of a specific type.

#### Instances

Instances details

type BooleanOf Integer Source # |
Instance details Defined in Data.Boolean type BooleanOf Integer = Bool
type BooleanOf Bool Source # |
Instance details Defined in Data.Boolean type BooleanOf Bool = Bool
type BooleanOf Char Source # |
Instance details Defined in Data.Boolean type BooleanOf Char = Bool
type BooleanOf Double Source # |
Instance details Defined in Data.Boolean type BooleanOf Double = Bool
type BooleanOf Float Source # |
Instance details Defined in Data.Boolean type BooleanOf Float = Bool
type BooleanOf Int Source # |
Instance details Defined in Data.Boolean type BooleanOf Int = Bool
type BooleanOf [a] Source # |
Instance details Defined in Data.Boolean type BooleanOf [a] = BooleanOf a
type BooleanOf (a, b) Source # |
Instance details Defined in Data.Boolean type BooleanOf (a, b) = BooleanOf a
type BooleanOf (z -> a) Source # |
Instance details Defined in Data.Boolean type BooleanOf (z -> a) = z -> BooleanOf a
type BooleanOf (a, b, c) Source # |
Instance details Defined in Data.Boolean type BooleanOf (a, b, c) = BooleanOf a
type BooleanOf (a, b, c, d) Source # |
Instance details Defined in Data.Boolean type BooleanOf (a, b, c, d) = BooleanOf a

class Boolean (BooleanOf a) => IfB a where Source #

Types with conditionals

Methods

ifB :: bool ~ BooleanOf a => bool -> a -> a -> a Source #

#### Instances

Instances details

IfB Integer Source # |
Instance details Defined in Data.Boolean Methods ifB :: bool ~ BooleanOf Integer => bool -> Integer -> Integer -> Integer Source #
IfB Bool Source # |
Instance details Defined in Data.Boolean Methods ifB :: bool ~ BooleanOf Bool => bool -> Bool -> Bool -> Bool Source #
IfB Char Source # |
Instance details Defined in Data.Boolean Methods ifB :: bool ~ BooleanOf Char => bool -> Char -> Char -> Char Source #
IfB Double Source # |
Instance details Defined in Data.Boolean Methods ifB :: bool ~ BooleanOf Double => bool -> Double -> Double -> Double Source #
IfB Float Source # |
Instance details Defined in Data.Boolean Methods ifB :: bool ~ BooleanOf Float => bool -> Float -> Float -> Float Source #
IfB Int Source # |
Instance details Defined in Data.Boolean Methods ifB :: bool ~ BooleanOf Int => bool -> Int -> Int -> Int Source #
(Boolean (BooleanOf a), BooleanOf a ~ Bool) => IfB [a] Source # |
Instance details Defined in Data.Boolean Methods ifB :: bool ~ BooleanOf [a] => bool -> [a] -> [a] -> [a] Source #
(bool ~ BooleanOf p, bool ~ BooleanOf q, IfB p, IfB q) => IfB (p, q) Source # |
Instance details Defined in Data.Boolean Methods ifB :: bool0 ~ BooleanOf (p, q) => bool0 -> (p, q) -> (p, q) -> (p, q) Source #
IfB a => IfB (z -> a) Source # |
Instance details Defined in Data.Boolean Methods ifB :: bool ~ BooleanOf (z -> a) => bool -> (z -> a) -> (z -> a) -> z -> a Source #
(bool ~ BooleanOf p, bool ~ BooleanOf q, bool ~ BooleanOf r, IfB p, IfB q, IfB r) => IfB (p, q, r) Source # |
Instance details Defined in Data.Boolean Methods ifB :: bool0 ~ BooleanOf (p, q, r) => bool0 -> (p, q, r) -> (p, q, r) -> (p, q, r) Source #
(bool ~ BooleanOf p, bool ~ BooleanOf q, bool ~ BooleanOf r, bool ~ BooleanOf s, IfB p, IfB q, IfB r, IfB s) => IfB (p, q, r, s) Source # |
Instance details Defined in Data.Boolean Methods ifB :: bool0 ~ BooleanOf (p, q, r, s) => bool0 -> (p, q, r, s) -> (p, q, r, s) -> (p, q, r, s) Source #

boolean :: (IfB a, bool ~ BooleanOf a) => a -> a -> bool -> a Source #

Expression-lifted conditional with condition last

cond :: (Applicative f, IfB a, bool ~ BooleanOf a) => f bool -> f a -> f a -> f a Source #

Point-wise conditional

crop :: (Applicative f, Monoid (f a), IfB a, bool ~ BooleanOf a) => f bool -> f a -> f a Source #

Generalized cropping, filling in `mempty` where the test yields false.

class Boolean (BooleanOf a) => EqB a where Source #

Types with equality. Minimum definition: `(==*)`.

Minimal complete definition

(==*)

Methods

(==*) :: bool ~ BooleanOf a => a -> a -> bool infix 4 Source #

(/=*) :: bool ~ BooleanOf a => a -> a -> bool infix 4 Source #

#### Instances

Instances details

EqB Integer Source # |
Instance details Defined in Data.Boolean Methods (==*) :: bool ~ BooleanOf Integer => Integer -> Integer -> bool Source # (/=*) :: bool ~ BooleanOf Integer => Integer -> Integer -> bool Source #
EqB Bool Source # |
Instance details Defined in Data.Boolean Methods (==*) :: bool ~ BooleanOf Bool => Bool -> Bool -> bool Source # (/=*) :: bool ~ BooleanOf Bool => Bool -> Bool -> bool Source #
EqB Char Source # |
Instance details Defined in Data.Boolean Methods (==*) :: bool ~ BooleanOf Char => Char -> Char -> bool Source # (/=*) :: bool ~ BooleanOf Char => Char -> Char -> bool Source #
EqB Double Source # |
Instance details Defined in Data.Boolean Methods (==*) :: bool ~ BooleanOf Double => Double -> Double -> bool Source # (/=*) :: bool ~ BooleanOf Double => Double -> Double -> bool Source #
EqB Float Source # |
Instance details Defined in Data.Boolean Methods (==*) :: bool ~ BooleanOf Float => Float -> Float -> bool Source # (/=*) :: bool ~ BooleanOf Float => Float -> Float -> bool Source #
EqB Int Source # |
Instance details Defined in Data.Boolean Methods (==*) :: bool ~ BooleanOf Int => Int -> Int -> bool Source # (/=*) :: bool ~ BooleanOf Int => Int -> Int -> bool Source #
EqB a => EqB (z -> a) Source # |
Instance details Defined in Data.Boolean Methods (==*) :: bool ~ BooleanOf (z -> a) => (z -> a) -> (z -> a) -> bool Source # (/=*) :: bool ~ BooleanOf (z -> a) => (z -> a) -> (z -> a) -> bool Source #

class Boolean (BooleanOf a) => OrdB a where Source #

Types with inequality. Minimum definition: `(<*)`.

Minimal complete definition

(<*)

Methods

(<*) :: bool ~ BooleanOf a => a -> a -> bool infix 4 Source #

(<=*) :: bool ~ BooleanOf a => a -> a -> bool infix 4 Source #

(>*) :: bool ~ BooleanOf a => a -> a -> bool infix 4 Source #

(>=*) :: bool ~ BooleanOf a => a -> a -> bool infix 4 Source #

#### Instances

Instances details

OrdB Integer Source # |
Instance details Defined in Data.Boolean Methods (<*) :: bool ~ BooleanOf Integer => Integer -> Integer -> bool Source # (<=*) :: bool ~ BooleanOf Integer => Integer -> Integer -> bool Source # (>*) :: bool ~ BooleanOf Integer => Integer -> Integer -> bool Source # (>=*) :: bool ~ BooleanOf Integer => Integer -> Integer -> bool Source #
OrdB Bool Source # |
Instance details Defined in Data.Boolean Methods (<*) :: bool ~ BooleanOf Bool => Bool -> Bool -> bool Source # (<=*) :: bool ~ BooleanOf Bool => Bool -> Bool -> bool Source # (>*) :: bool ~ BooleanOf Bool => Bool -> Bool -> bool Source # (>=*) :: bool ~ BooleanOf Bool => Bool -> Bool -> bool Source #
OrdB Char Source # |
Instance details Defined in Data.Boolean Methods (<*) :: bool ~ BooleanOf Char => Char -> Char -> bool Source # (<=*) :: bool ~ BooleanOf Char => Char -> Char -> bool Source # (>*) :: bool ~ BooleanOf Char => Char -> Char -> bool Source # (>=*) :: bool ~ BooleanOf Char => Char -> Char -> bool Source #
OrdB Double Source # |
Instance details Defined in Data.Boolean Methods (<*) :: bool ~ BooleanOf Double => Double -> Double -> bool Source # (<=*) :: bool ~ BooleanOf Double => Double -> Double -> bool Source # (>*) :: bool ~ BooleanOf Double => Double -> Double -> bool Source # (>=*) :: bool ~ BooleanOf Double => Double -> Double -> bool Source #
OrdB Float Source # |
Instance details Defined in Data.Boolean Methods (<*) :: bool ~ BooleanOf Float => Float -> Float -> bool Source # (<=*) :: bool ~ BooleanOf Float => Float -> Float -> bool Source # (>*) :: bool ~ BooleanOf Float => Float -> Float -> bool Source # (>=*) :: bool ~ BooleanOf Float => Float -> Float -> bool Source #
OrdB Int Source # |
Instance details Defined in Data.Boolean Methods (<*) :: bool ~ BooleanOf Int => Int -> Int -> bool Source # (<=*) :: bool ~ BooleanOf Int => Int -> Int -> bool Source # (>*) :: bool ~ BooleanOf Int => Int -> Int -> bool Source # (>=*) :: bool ~ BooleanOf Int => Int -> Int -> bool Source #
OrdB a => OrdB (z -> a) Source # |
Instance details Defined in Data.Boolean Methods (<*) :: bool ~ BooleanOf (z -> a) => (z -> a) -> (z -> a) -> bool Source # (<=*) :: bool ~ BooleanOf (z -> a) => (z -> a) -> (z -> a) -> bool Source # (>*) :: bool ~ BooleanOf (z -> a) => (z -> a) -> (z -> a) -> bool Source # (>=*) :: bool ~ BooleanOf (z -> a) => (z -> a) -> (z -> a) -> bool Source #

minB :: (IfB a, OrdB a) => a -> a -> a Source #

Variant of `min` using `ifB` and `(<=*)`

maxB :: (IfB a, OrdB a) => a -> a -> a Source #

Variant of `max` using `ifB` and `(>=*)`

sort2B :: (IfB a, OrdB a) => (a, a) -> (a, a) Source #

Variant of `min` and `max` using `ifB` and `(<=*)`

guardedB :: (IfB b, bool ~ BooleanOf b) => bool -> [(bool, b)] -> b -> b Source #

A generalized replacement for guards and chained ifs.

caseB :: (IfB b, bool ~ BooleanOf b) => a -> [(a -> bool, b)] -> b -> b Source #

A generalized version of a case like control structure.