-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVBool.hs
More file actions
246 lines (191 loc) · 6.39 KB
/
VBool.hs
File metadata and controls
246 lines (191 loc) · 6.39 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
{-# LANGUAGE FlexibleContexts, UndecidableInstances, TypeSynonymInstances, FlexibleInstances, DataKinds, DeriveGeneric, DeriveAnyClass, DeriveDataTypeable #-}
module VBool where
import Badness
import Test.QuickCheck
import Data.List
import Data.Reflection
import Data.Ord
import GHC.Generics
import Data.Data
infix 4 ==%
infixr 3 &&%, &&+, &&^
infixr 3 ||%, ||+, ||^
infixr 1 ==>%
--------------------------------------------------------------------------------
data VBool = VFalse Inf | VTrue Inf -- x non-negative
deriving (Eq, Data)
instance Ord VBool where
compare = comparing howTrue
instance Show VBool where
show (VFalse v) = "false " ++ show (fromInf v)
show (VTrue v) = "true " ++ show (fromInf v)
instance Arbitrary VBool where
arbitrary = do
x <- arbitrary
val <- elements [VFalse, VTrue]
return (val x)
--------------------------------------------------------------------------------
-- A nonzero real number which can also be infinite.
data Inf = Finite Double | Infinite
deriving (Eq, Ord, Generic, CoArbitrary, Data)
plusInf :: Inf -> Inf -> Inf
plusInf (Finite x) (Finite y) = Finite (x+y)
plusInf _ _ = Infinite
distInf :: Inf -> Inf -> Inf
distInf (Finite x) (Finite y) = Finite (sqrt (x^2+y^2))
distInf _ _ = Infinite
-- parInf x y = 1/(1/x + 1/y)
parInf :: Inf -> Inf -> Inf
parInf (Finite x) (Finite y)
| x /= 0, y /= 0 =
Finite (recip (recip x + recip y))
-- 1/(1/x + 1/y) -> y as x -> 0
parInf (Finite 0) (Finite _) = Finite 0
parInf (Finite _) (Finite 0) = Finite 0
-- 1/infty = 0
parInf (Finite x) Infinite = Finite x
parInf Infinite (Finite y) = Finite y
parInf Infinite Infinite = Infinite
scaleInf :: Real a => Inf -> a -> Inf
scaleInf (Finite x) a = Finite (x*fromRational (toRational a))
scaleInf Infinite _ = Infinite
fromInf :: Inf -> Double
fromInf (Finite x) | x >= 0 = x
fromInf Infinite = 1/0
toInf :: Real a => a -> Inf
toInf = Finite . fromRational . toRational
instance Arbitrary Inf where
arbitrary =
oneof [
return Infinite,
do
NonNegative x <- arbitrary
return (Finite x) ]
--------------------------------------------------------------------------------
false, true :: VBool
false = VFalse Infinite
true = VTrue Infinite
bool :: Bool -> VBool
bool False = false
bool True = true
bad, good :: Real a => a -> VBool
bad x = VFalse (toInf x)
good x = VTrue (toInf x)
howTrue :: VBool -> Double
howTrue (VFalse x) = -1 - fromInf x
howTrue (VTrue x) = 1 + fromInf x
isTrue, isFalse :: VBool -> Bool
isTrue (VFalse _) = False
isTrue (VTrue _) = True
isFalse = not . isTrue
(#) :: Real a => VBool -> a -> VBool
_ # a | a < 0 = error "negative #"
VFalse x # a = VFalse (scaleInf x a)
-- XXX should this use par?
VTrue x # a = VTrue (scaleInf x a)
(#+) :: Real a => VBool -> a -> VBool
_ #+ a | a < 0 = error "negative #+"
VFalse x #+ a = VFalse (plusInf x (toInf a))
-- XXX should this be par?
VTrue x #+ a = VTrue (plusInf x (toInf a))
--------------------------------------------------------------------------------
nt :: VBool -> VBool
nt (VFalse x) = VTrue x
nt (VTrue x) = VFalse x
conjunction :: (Inf -> Inf -> Inf) -> (Inf -> Inf -> Inf) -> VBool -> VBool -> VBool
conjunction f _ (VFalse x) (VFalse y) = VFalse (f x y)
conjunction _ g (VTrue x) (VTrue y) = VTrue (g x y)
conjunction _ _ (VFalse x) (VTrue _) = VFalse x
conjunction _ _ (VTrue _) (VFalse x) = VFalse x
disjunction :: (Inf -> Inf -> Inf) -> (Inf -> Inf -> Inf) -> VBool -> VBool -> VBool
disjunction f g x y = nt (conjunction f g (nt x) (nt y))
(&&%) :: VBool -> VBool -> VBool
VFalse x &&% VFalse y = VFalse (x `max` y)
VTrue x &&% VTrue y = VTrue (x `min` y)
VFalse x &&% VTrue _ = VFalse x
VTrue _ &&% VFalse x = VFalse x
(&&+) :: VBool -> VBool -> VBool
VFalse x &&+ VFalse y = VFalse (plusInf x y)
VTrue x &&+ VTrue y = VTrue (parInf x y)
VTrue _x &&+ VFalse y = VFalse y
VFalse x &&+ VTrue _y = VFalse x
(&&++) :: VBool -> VBool -> VBool
VFalse x &&++ VFalse y = VFalse (plusInf x y)
VTrue x &&++ VTrue y = VTrue (plusInf x y)
VTrue _x &&++ VFalse y = VFalse y
VFalse x &&++ VTrue _y = VFalse x
(&&^) :: VBool -> VBool -> VBool
VFalse x &&^ VFalse y = VFalse (distInf x y)
VTrue x &&^ VTrue y = VTrue (parInf x y)
VTrue _x &&^ VFalse y = VFalse y
VFalse x &&^ VTrue _y = VFalse x
(&&^^) :: VBool -> VBool -> VBool
VFalse x &&^^ VFalse y = VFalse (distInf x y)
VTrue x &&^^ VTrue y = VTrue (distInf x y)
VTrue _x &&^^ VFalse y = VFalse y
VFalse x &&^^ VTrue _y = VFalse x
(||%), (||+) :: VBool -> VBool -> VBool
x ||% y = nt (nt x &&% nt y)
x ||+ y = nt (nt x &&+ nt y)
x ||^ y = nt (nt x &&^ nt y)
x ||++ y = nt (nt x &&++ nt y)
x ||^^ y = nt (nt x &&^^ nt y)
conj :: [VBool] -> VBool
conj = foldl' (&&+) true
conj' :: [VBool] -> VBool
conj' = foldl' (&&%) true
disj :: [VBool] -> VBool
disj = foldl' (||+) false
disj' :: [VBool] -> VBool
disj' = foldl' (||%) false
--------------------------------------------------------------------------------
big :: Double
big = 100000
(==>%) :: VBool -> VBool -> VBool
x ==>% y
= (nt x # 10) ||% y
(==>+) :: VBool -> VBool -> VBool
x ==>+ y
= (nt x # 10) ||+ y
-- | isTrue x = y
-- | otherwise = nt x #+ big
--------------------------------------------------------------------------------
(>%), (>=%), (<=%), (<%) :: Real a => a -> a -> VBool
x >=% y
| x >= y = good (x-y+1)
| otherwise = bad (y-x+1)
x >% y = nt (x <=% y)
x <% y = y >% x
x <=% y = y >=% x
--------------------------------------------------------------------------------
class VEq a where
(==%) :: a -> a -> VBool
instance VEq Double where
x ==% y
-- ??? REALLY??? infinity?
| x == y = true
| otherwise = bad (abs (x-y))
instance VEq Int where
x ==% y
| x == y = true
| otherwise = bad (fromIntegral (abs (x-y)))
instance VEq Bool where
x ==% y = if x == y then true else false
instance VEq VBool where
x ==% y
| isTrue x == isTrue y = good (big / (0.1+d))
| otherwise = bad d
where
d = abs (howTrue x - howTrue y)
instance VEq () where
_ ==% _ = true
instance (VEq a, VEq b) => VEq (a,b) where
(a1,a2) ==% (b1,b2) = a1 ==% b1 &&+ a2 ==% b2
instance VEq a => VEq [a] where
[] ==% [] = true
(a:as) ==% (b:bs) = (a==%b) &&+ (as ==% bs)
as ==% bs = bad (fromIntegral (length (as++bs)))
--------------------------------------------------------------------------------
instance Given Badness => Testable VBool where
--property x = property (isTrue x)
property x = badness given (- howTrue x) (isTrue x)