1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

{ 
Type classes (and instances) for things that are like Booleans.
The names of methods in 'Boolean' clash with the standard Prelude,
so you probably want to inport the Prelude hiding these three
names (since the class methods do the same thing, but with more
general type signatures).
Please note the following points:
* This module imports "Control.Monad.Instances", which brings
several new 'P.Monad' instances into scope.
* Among other things, a monad instance for functions is brought
into scope. This, combined with the 'Boolean' instance for
monads, causes any function that returns a 'Boolean' to become
a 'Boolean' itself. This allows you to write constructions such
as @(> 5) && (< 9)@, which has the obvious meaning.
* Another interesting consequence of the 'Boolean' instance for
monads is that 'P.Maybe' 'P.Bool' is a 'Boolean'. You can use
this to implement 3value logic (\"true\", \"false\" and
\"other\"), with 'P.Nothing' implementing \"other\". Any logical
operations yield 'P.Nothing' unless all arguments are 'P.Just'
something. (This is usually the behaviour you want.)
}
{# LANGUAGE FlexibleInstances #}
module Data.Boolean where
import qualified Prelude as P
import qualified Control.Monad as M
import Control.Monad.Instances  For the Monad ((>) r) instance.
{ 
Typeclass for things that have true and false values.
Instances:
* Normal 'P.Bool' values (obviously).
* Any function that yields a 'BoolValue' as its result.
(@'true' = 'P.const' 'P.True'@, @'false' = 'P.const' 'P.False'@)
This instance arrises due to the monad instance for functions.
* Any monadic action that yields a 'BoolValue' as its result.
(@'true' = 'P.return' 'P.True'@, @'false' = 'P.return' 'P.False'@)
}
class BoolValue b where
true :: b
false :: b
instance BoolValue P.Bool where
true = P.True
false = P.False
instance (P.Monad m, BoolValue b) => BoolValue (m b) where
true = P.return true
false = P.return false
  Convert a 'P.Bool' value to the appropriate 'BoolValue'.
lift_bool :: (BoolValue b) => P.Bool > b
lift_bool b = if b then true else false
{ 
Typeclass for things that support Boolean operators.
Instances:
* Normal 'P.Bool' values (obviously).
* Any function that returns a 'Boolean'.
This instance arrises due to the monad instance for functions.
* Any monadic action that returns a 'Boolean'.
The left action is performed before the right action (which may
be significant, depending on the monad).
}
class Boolean b where
  LogicalAND of two values.
(&&) :: b > b > b
  LogicalOR of two values. (InclusiveOR.)
() :: b > b > b
  LogicalNOT of two values. (Logical inverse.)
not :: b > b
{ 
ExclusiveOR (XOR). There is a default implementation, but you
can override it for efficiency if desired.
}
xor :: b > b > b
x `xor` y = (x  y) && (not (x && y))
instance Boolean P.Bool where
(&&) = (P.&&)
() = (P.)
not = P.not
xor = (P.==)
instance (P.Monad m, Boolean b) => Boolean (m b) where
(&&) = M.liftM2 (&&)
() = M.liftM2 ()
not = M.liftM not
xor = M.liftM2 xor
