| 1 |
;;;; Conditional syntax |
| 2 |
|
| 3 |
(define-syntax raises-error-object |
| 4 |
(syntax-rules () |
| 5 |
((_ expr) |
| 6 |
(guard (obj ((error-object? obj) #t) |
| 7 |
(else #f)) |
| 8 |
expr)))) |
| 9 |
|
| 10 |
(define (check-syntax) |
| 11 |
(print-header "Testing syntax...") |
| 12 |
|
| 13 |
(check (maybe-if (just #t) #t #f) => #t) |
| 14 |
(check (maybe-if (nothing) #t #f) => #f) |
| 15 |
(check (raises-error-object (maybe-if #t #t #f)) => #t) |
| 16 |
|
| 17 |
;;; maybe-and, -or, -let*, and -let*-values |
| 18 |
|
| 19 |
(check (just? (maybe-and)) => #t) |
| 20 |
(check (just-of-z? (maybe-and (just 'z))) => #t) |
| 21 |
(check (just-of-z? (maybe-and (just #t) (just 'z))) => #t) |
| 22 |
(check (nothing? (maybe-and (just #t) (nothing) (just 'z))) => #t) |
| 23 |
;; and / bind identities |
| 24 |
(check (maybe= eqv? |
| 25 |
(maybe-bind (just #f) (constantly (just #t))) |
| 26 |
(maybe-and (just #f) (just #t)) |
| 27 |
(just #t)) |
| 28 |
=> #t) |
| 29 |
(check (maybe= eqv? |
| 30 |
(maybe-bind (nothing) (constantly (just #t))) |
| 31 |
(maybe-and (nothing) (just #t)) |
| 32 |
(nothing)) |
| 33 |
=> #t) |
| 34 |
(check (raises-error-object (maybe-and #t (just #t))) => #t) |
| 35 |
|
| 36 |
(check (nothing? (maybe-or)) => #t) |
| 37 |
(check (just-of-z? (maybe-or (just 'z))) => #t) |
| 38 |
(check (just-of-z? (maybe-or (nothing) (just 'z))) => #t) |
| 39 |
(check (nothing? (maybe-or (nothing) (nothing) (nothing))) => #t) |
| 40 |
(check (raises-error-object (maybe-or (nothing) #t)) => #t) |
| 41 |
|
| 42 |
(check (just-of-z? |
| 43 |
(maybe-let* (((maybe-bind (just #t) just))) |
| 44 |
'z)) => #t) |
| 45 |
(check (nothing? |
| 46 |
(maybe-let* ((x (just #t)) |
| 47 |
(y (nothing))) |
| 48 |
x)) |
| 49 |
=> #t) |
| 50 |
(check (maybe= eqv? |
| 51 |
(maybe-let* ((x (just 2)) |
| 52 |
(y (just 3))) |
| 53 |
(* x y)) |
| 54 |
(just 6)) |
| 55 |
=> #t) |
| 56 |
(check (nothing? |
| 57 |
(maybe-let* ((x (just 2)) |
| 58 |
((maybe-bind (just 'z) (constantly (nothing))))) |
| 59 |
x)) |
| 60 |
=> #t) |
| 61 |
(check (maybe= eqv? |
| 62 |
(maybe-let* ((b (just #t)) ((truth->maybe b))) |
| 63 |
b) |
| 64 |
(just #t)) |
| 65 |
=> #t) |
| 66 |
;; Behavior of bound-variable claws. |
| 67 |
(let ((just-of-z (just 'z)) (zilch (nothing))) |
| 68 |
(check (just-of-z? (maybe-let* (just-of-z) 'z)) => #t) |
| 69 |
(check (maybe= eqv? |
| 70 |
(maybe-let* ((x (just 2)) just-of-z (y (just 3))) |
| 71 |
(* x y)) |
| 72 |
(just 6)) |
| 73 |
=> #t) |
| 74 |
(check (just-of-z? (maybe-let* (just-of-z ((just 'x))) |
| 75 |
'z)) |
| 76 |
=> #t) |
| 77 |
(check (nothing? (maybe-let* ((x (just 2)) zilch (y (just 3))) |
| 78 |
(* x y))) |
| 79 |
=> #t)) |
| 80 |
;; let* / bind identities. |
| 81 |
(let ((just-neg (lambda (b) (just (not b))))) |
| 82 |
(check (maybe= eqv? |
| 83 |
(maybe-bind (just #t) just-neg) |
| 84 |
(maybe-let* ((b (just #t))) (not b)) |
| 85 |
(just #f)) |
| 86 |
=> #t) |
| 87 |
(check (maybe= eqv? |
| 88 |
(maybe-bind (nothing) just-neg) |
| 89 |
(maybe-let* ((b (nothing))) (not b)) |
| 90 |
(nothing)) |
| 91 |
=> #t)) |
| 92 |
(check (raises-error-object (maybe-let* ((b #t)) 'z)) => #t) |
| 93 |
(check (raises-error-object |
| 94 |
(maybe-let* ((b (just #t)) ('nothing)) #t)) |
| 95 |
=> #t) |
| 96 |
|
| 97 |
(check (just-of-z? |
| 98 |
(maybe-let*-values (((maybe-bind (just #t) just))) |
| 99 |
'z)) => #t) |
| 100 |
(check (nothing? |
| 101 |
(maybe-let*-values (((x) (just #t)) |
| 102 |
(y (nothing))) |
| 103 |
x)) |
| 104 |
=> #t) |
| 105 |
(check (maybe= eqv? |
| 106 |
(maybe-let*-values (((x y) (just 2 3))) |
| 107 |
(* x y)) |
| 108 |
(just 6)) |
| 109 |
=> #t) |
| 110 |
(check (nothing? |
| 111 |
(maybe-let*-values (((x) (just 2)) |
| 112 |
((maybe-bind (just 'z) (constantly (nothing))))) |
| 113 |
x)) |
| 114 |
=> #t) |
| 115 |
(check (maybe= eqv? |
| 116 |
(maybe-let*-values (((b) (just #t)) ((truth->maybe b))) |
| 117 |
b) |
| 118 |
(just #t)) |
| 119 |
=> #t) |
| 120 |
(check (just-of-z? (maybe-let*-values ((vals (just 'z #t))) |
| 121 |
(car vals))) |
| 122 |
=> #t) |
| 123 |
(check (just-of-z? (maybe-let*-values (((x . _) (just 'z #t))) |
| 124 |
x)) |
| 125 |
=> #t) |
| 126 |
(check (maybe= eqv? |
| 127 |
(maybe-let*-values ((vals (just 'z #t)) |
| 128 |
((b c . _) (just #t 'y #f))) |
| 129 |
(values (car vals) b)) |
| 130 |
(just 'z #t)) |
| 131 |
=> #t) |
| 132 |
;; Behavior of bound-variable claws. |
| 133 |
(let ((just-of-z (just 'z)) (zilch (nothing))) |
| 134 |
(check (just-of-z? (maybe-let*-values (just-of-z) 'z)) => #t) |
| 135 |
(check (maybe= eqv? |
| 136 |
(maybe-let*-values (((x) (just 2)) |
| 137 |
just-of-z |
| 138 |
((y) (just 3))) |
| 139 |
(* x y)) |
| 140 |
(just 6)) |
| 141 |
=> #t) |
| 142 |
(check (just-of-z? (maybe-let*-values (just-of-z ((just 'x))) |
| 143 |
'z)) |
| 144 |
=> #t) |
| 145 |
(check (nothing? (maybe-let*-values (((x) (just 2)) |
| 146 |
zilch |
| 147 |
((y) (just 3))) |
| 148 |
(* x y))) |
| 149 |
=> #t)) |
| 150 |
;; let*-values / bind identities. |
| 151 |
(let* ((neg-both (lambda (b c) (values (not b) (not c)))) |
| 152 |
(just-neg-both (lambda (b c) |
| 153 |
(call-with-values (lambda () (neg-both b c)) |
| 154 |
just)))) |
| 155 |
(check (maybe= eqv? |
| 156 |
(maybe-bind (just #t #t) just-neg-both) |
| 157 |
(maybe-let*-values (((b c) (just #t #t))) |
| 158 |
(neg-both b c)) |
| 159 |
(just #f #f)) |
| 160 |
=> #t) |
| 161 |
(check (maybe= eqv? |
| 162 |
(maybe-bind (nothing) just-neg-both) |
| 163 |
(maybe-let*-values (((b c) (nothing))) (neg-both b c)) |
| 164 |
(nothing)) |
| 165 |
=> #t)) |
| 166 |
(check (raises-error-object (maybe-let*-values (((b) #t)) #t)) => #t) |
| 167 |
(check (raises-error-object |
| 168 |
(maybe-let*-values (((b) (just #t)) ('nothing)) #t)) |
| 169 |
=> #t) |
| 170 |
|
| 171 |
;;; either-and, -or, and -let* |
| 172 |
|
| 173 |
(check (right? (either-and)) => #t) |
| 174 |
(check (right-of-z? (either-and (right 'z))) => #t) |
| 175 |
(check (right-of-z? (either-and (right #t) (right 'z))) => #t) |
| 176 |
(check (left-of-z? (either-and (right) (left 'z) (right))) => #t) |
| 177 |
;; and / bind identities |
| 178 |
(check (either= eqv? |
| 179 |
(either-bind (right #f) (constantly (right #t))) |
| 180 |
(either-and (right #f) (right #t)) |
| 181 |
(right #t)) |
| 182 |
=> #t) |
| 183 |
(check (either= eqv? |
| 184 |
(either-bind (left #f) (constantly (right #t))) |
| 185 |
(either-and (left #f) (right #t)) |
| 186 |
(left #f)) |
| 187 |
=> #t) |
| 188 |
(check (raises-error-object (either-and #t (right #t))) => #t) |
| 189 |
|
| 190 |
(check (left? (either-or)) => #t) |
| 191 |
(check (right-of-z? (either-or (right 'z))) => #t) |
| 192 |
(check (right-of-z? (either-or (left) (right 'z))) => #t) |
| 193 |
(check (left-of-z? (either-or (left) (left) (left 'z))) => #t) |
| 194 |
(check (raises-error-object (either-or (left #f) #t)) => #t) |
| 195 |
|
| 196 |
(check (right-of-z? |
| 197 |
(either-let* (((either-bind (right #t) right))) |
| 198 |
'z)) |
| 199 |
=> #t) |
| 200 |
(check (left-of-z? (either-let* ((x (right #t)) (y (left 'z))) |
| 201 |
x)) |
| 202 |
=> #t) |
| 203 |
(check (either= eqv? |
| 204 |
(either-let* ((x (right 2)) (y (right 3))) |
| 205 |
(* x y)) |
| 206 |
(right 6)) |
| 207 |
=> #t) |
| 208 |
(check (left-of-z? |
| 209 |
(either-let* ((x (right 2)) |
| 210 |
((either-swap (right 'z)))) |
| 211 |
x)) |
| 212 |
=> #t) |
| 213 |
(check (either= eqv? |
| 214 |
(either-let* ((b (right #t)) ((truth->either b))) |
| 215 |
b) |
| 216 |
(right #t)) |
| 217 |
=> #t) |
| 218 |
;; Behavior of bound-variable claws. |
| 219 |
(let ((right-of-z (right 'z)) (left-of-z (left 'z))) |
| 220 |
(check (right-of-z? (either-let* (right-of-z) 'z)) => #t) |
| 221 |
(check (either= eqv? |
| 222 |
(either-let* ((x (right 2)) |
| 223 |
right-of-z |
| 224 |
(y (right 3))) |
| 225 |
(* x y)) |
| 226 |
(right 6)) |
| 227 |
=> #t) |
| 228 |
(check (right-of-z? |
| 229 |
(either-let* (right-of-z ((right 'x))) |
| 230 |
'z)) |
| 231 |
=> #t) |
| 232 |
(check (left-of-z? |
| 233 |
(either-let* ((x (right 2)) left-of-z (y (right 3))) |
| 234 |
(* x y))) |
| 235 |
=> #t)) |
| 236 |
;; let* / bind identities. |
| 237 |
(let ((right-neg (lambda (b) (right (not b))))) |
| 238 |
(check (either= eqv? |
| 239 |
(either-bind (right #t) right-neg) |
| 240 |
(either-let* ((b (right #t))) (not b)) |
| 241 |
(right #f)) |
| 242 |
=> #t) |
| 243 |
(check (either= eqv? |
| 244 |
(either-bind (left #t) right-neg) |
| 245 |
(either-let* ((b (left #t))) (not b)) |
| 246 |
(left #t)) |
| 247 |
=> #t)) |
| 248 |
(check (raises-error-object (either-let* ((b #t)) 'z)) => #t) |
| 249 |
(check (raises-error-object |
| 250 |
(either-let* ((b (right #t)) ('left)) #t)) |
| 251 |
=> #t) |
| 252 |
|
| 253 |
(check (right-of-z? |
| 254 |
(either-let*-values (((either-bind (right #t) right))) |
| 255 |
'z)) |
| 256 |
=> #t) |
| 257 |
(check (left-of-z? |
| 258 |
(either-let*-values (((x) (right #t)) |
| 259 |
(y (left 'z))) |
| 260 |
x)) |
| 261 |
=> #t) |
| 262 |
(check (either= eqv? |
| 263 |
(either-let*-values (((x y) (right 2 3))) |
| 264 |
(* x y)) |
| 265 |
(right 6)) |
| 266 |
=> #t) |
| 267 |
(check (left-of-z? |
| 268 |
(either-let*-values (((x) (right 2)) |
| 269 |
((either-swap (right 'z)))) |
| 270 |
x)) |
| 271 |
=> #t) |
| 272 |
(check (either= eqv? |
| 273 |
(either-let*-values (((b) (right #t)) ((truth->either b))) |
| 274 |
b) |
| 275 |
(right #t)) |
| 276 |
=> #t) |
| 277 |
(check (right-of-z? (either-let*-values ((vals (right 'z #t))) |
| 278 |
(car vals))) |
| 279 |
=> #t) |
| 280 |
(check (right-of-z? (either-let*-values (((x . _) (right 'z #t))) |
| 281 |
x)) |
| 282 |
=> #t) |
| 283 |
(check (either= eqv? |
| 284 |
(either-let*-values ((vals (right 'z #t)) |
| 285 |
((b c . _) (right #t 'y #f))) |
| 286 |
(values (car vals) b)) |
| 287 |
(right 'z #t)) |
| 288 |
=> #t) |
| 289 |
;; Behavior of bound-variable claws. |
| 290 |
(let ((right-of-z (right 'z)) (left-of-z (left 'z))) |
| 291 |
(check (right-of-z? (either-let*-values (right-of-z) |
| 292 |
'z)) |
| 293 |
=> #t) |
| 294 |
(check (either= eqv? |
| 295 |
(either-let*-values (((x) (right 2)) |
| 296 |
right-of-z |
| 297 |
((y) (right 3))) |
| 298 |
(* x y)) |
| 299 |
(right 6)) |
| 300 |
=> #t) |
| 301 |
(check (right-of-z? (either-let*-values (right-of-z ((right 'x))) |
| 302 |
'z)) |
| 303 |
=> #t) |
| 304 |
(check (left-of-z? (either-let*-values (((x) (right 2)) |
| 305 |
left-of-z |
| 306 |
((y) (right 3))) |
| 307 |
(* x y))) |
| 308 |
=> #t)) |
| 309 |
;; let*-values / bind identities. |
| 310 |
(let* ((neg-both (lambda (b c) (values (not b) (not c)))) |
| 311 |
(right-neg-both (lambda (b c) |
| 312 |
(call-with-values (lambda () (neg-both b c)) |
| 313 |
right)))) |
| 314 |
(check (either= eqv? |
| 315 |
(either-bind (right #t #t) right-neg-both) |
| 316 |
(either-let*-values (((b c) (right #t #t))) |
| 317 |
(neg-both b c)) |
| 318 |
(right #f #f)) |
| 319 |
=> #t) |
| 320 |
(check (either= eqv? |
| 321 |
(either-bind (left #t #t) right-neg-both) |
| 322 |
(either-let*-values (((b c) (left #t #t))) |
| 323 |
(neg-both b c)) |
| 324 |
(left #t #t)) |
| 325 |
=> #t)) |
| 326 |
(check (raises-error-object |
| 327 |
(either-let*-values (((b) #t)) 'z)) |
| 328 |
=> #t) |
| 329 |
(check (raises-error-object |
| 330 |
(either-let*-values (((b) (right #t)) ('left)) 'z)) |
| 331 |
=> #t) |
| 332 |
|
| 333 |
(check (left-of-z? (either-guard symbol? (raise 'z))) => #t) |
| 334 |
(check (right-of-z? (either-guard symbol? 'z)) => #t) |
| 335 |
(check (guard (obj ((symbol? obj) obj)) |
| 336 |
(either-guard number? (raise-continuable 'z))) |
| 337 |
=> 'z) |
| 338 |
(check (either= eqv? |
| 339 |
(with-exception-handler |
| 340 |
not |
| 341 |
(lambda () |
| 342 |
(either-guard string? (not (raise-continuable #t))))) |
| 343 |
(right #t)) |
| 344 |
=> #t)) |