| 1 |
;; Copyright (C) 2020 Wolfgang Corcoran-Mathe |
| 2 |
|
| 3 |
;; Permission is hereby granted, free of charge, to any person obtaining a |
| 4 |
;; copy of this software and associated documentation files (the |
| 5 |
;; "Software"), to deal in the Software without restriction, including |
| 6 |
;; without limitation the rights to use, copy, modify, merge, publish, |
| 7 |
;; distribute, sublicense, and/or sell copies of the Software, and to |
| 8 |
;; permit persons to whom the Software is furnished to do so, subject to |
| 9 |
;; the following conditions: |
| 10 |
|
| 11 |
;; The above copyright notice and this permission notice shall be included |
| 12 |
;; in all copies or substantial portions of the Software. |
| 13 |
|
| 14 |
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 |
;; OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 |
;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 17 |
;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 18 |
;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 19 |
;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 20 |
;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 |
|
| 22 |
(import (scheme base)) |
| 23 |
(import (scheme write)) |
| 24 |
(import (srfi 189)) |
| 25 |
|
| 26 |
(cond-expand |
| 27 |
((library (srfi 78)) |
| 28 |
(import (srfi 78))) |
| 29 |
(else |
| 30 |
(begin |
| 31 |
(define-syntax check |
| 32 |
(syntax-rules (=>) |
| 33 |
((check expr) |
| 34 |
(check expr => #t)) |
| 35 |
((check expr => expected) |
| 36 |
(if (equal? expr expected) |
| 37 |
(begin |
| 38 |
(display 'expr) |
| 39 |
(display " => ") |
| 40 |
(display expected) |
| 41 |
(display " ; correct") |
| 42 |
(newline)) |
| 43 |
(begin |
| 44 |
(display "FAILED: for ") |
| 45 |
(display 'expr) |
| 46 |
(display " expected ") |
| 47 |
(display expected) |
| 48 |
(display " but got ") |
| 49 |
(display expr) |
| 50 |
(newline)) |
| 51 |
)))) |
| 52 |
(define (check-report) #t)))) |
| 53 |
|
| 54 |
;;;; Utility |
| 55 |
|
| 56 |
(define (identity x) x) |
| 57 |
|
| 58 |
(define (print-header message) |
| 59 |
(newline) |
| 60 |
(display ";;; ") |
| 61 |
(display message) |
| 62 |
(newline)) |
| 63 |
|
| 64 |
(define-syntax constantly |
| 65 |
(syntax-rules () |
| 66 |
((_ obj) (lambda _ obj)))) |
| 67 |
|
| 68 |
;; Gives the values of expr as a list. |
| 69 |
(define-syntax values->list |
| 70 |
(syntax-rules () |
| 71 |
((_ expr) |
| 72 |
(call-with-values (lambda () expr) list)))) |
| 73 |
|
| 74 |
(define always (constantly #t)) |
| 75 |
(define never (constantly #f)) |
| 76 |
|
| 77 |
;; Verify that a Maybe is a Just of 'z, a dummy object. |
| 78 |
(define (just-of-z? m) |
| 79 |
(and (maybe? m) (maybe= eqv? m (just 'z)))) |
| 80 |
|
| 81 |
;; Verify that an Either is a Right of 'z, a dummy object. |
| 82 |
(define (right-of-z? e) |
| 83 |
(and (either? e) (either= eqv? e (right 'z)))) |
| 84 |
|
| 85 |
;; Verify that an Either is a Left of 'z, a dummy object. |
| 86 |
(define (left-of-z? e) |
| 87 |
(and (either? e) (either= eqv? e (left 'z)))) |
| 88 |
|
| 89 |
;;;; Tests |
| 90 |
|
| 91 |
(define (check-constructors) |
| 92 |
(print-header "Testing constructors...") |
| 93 |
|
| 94 |
;; Uniqueness of the Nothing object. |
| 95 |
(check (eq? (nothing) (nothing)) => #t) |
| 96 |
|
| 97 |
;; list->just and list->right |
| 98 |
(check (maybe= eqv? (just #t #t) (list->just '(#t #t))) => #t) |
| 99 |
(check (either= eqv? (right #t #t) (list->right '(#t #t))) => #t) |
| 100 |
(check (either= eqv? (left #t #t) (list->left '(#t #t))) => #t) |
| 101 |
|
| 102 |
;; maybe->either and either->maybe |
| 103 |
(check (left-of-z? (maybe->either (nothing) 'z)) => #t) |
| 104 |
(check (right-of-z? (maybe->either (just 'z) #f)) => #t) |
| 105 |
(check (either= eqv? (right #t #t) (maybe->either (just #t #t) #f)) => #t) |
| 106 |
(check (nothing? (either->maybe (left #t))) => #t) |
| 107 |
(check (just-of-z? (either->maybe (right 'z))) => #t) |
| 108 |
(check (maybe= eqv? (just #t #t) (either->maybe (right #t #t))) => #t) |
| 109 |
|
| 110 |
;; either-swap |
| 111 |
(check (either= eqv? (right #t #t) (either-swap (left #t #t))) => #t) |
| 112 |
(check (either= eqv? (left #t #t) (either-swap (right #t #t))) => #t)) |
| 113 |
|
| 114 |
;;;; Predicates |
| 115 |
|
| 116 |
(define (check-predicates) |
| 117 |
(print-header "Testing predicates...") |
| 118 |
|
| 119 |
(check (just? (just 'z)) => #t) |
| 120 |
(check (just? (nothing)) => #f) |
| 121 |
(check (nothing? (just 'z)) => #f) |
| 122 |
(check (nothing? (nothing)) => #t) |
| 123 |
(check (maybe? (just 'z)) => #t) |
| 124 |
(check (maybe? (nothing)) => #t) |
| 125 |
|
| 126 |
(check (right? (right 'z)) => #t) |
| 127 |
(check (right? (left 'z)) => #f) |
| 128 |
(check (left? (right 'z)) => #f) |
| 129 |
(check (left? (left 'z)) => #t) |
| 130 |
(check (either? (right 'z)) => #t) |
| 131 |
(check (either? (left 'z)) => #t) |
| 132 |
|
| 133 |
(check (maybe= eqv? (just #t) (just #t)) => #t) |
| 134 |
(check (maybe= eqv? (just #t) (just #f)) => #f) |
| 135 |
(check (maybe= eqv? (nothing) (nothing)) => #t) |
| 136 |
(check (maybe= eqv? (just #t) (nothing)) => #f) |
| 137 |
|
| 138 |
(check (maybe= eqv? (just #t #f) (just #t #f)) => #t) |
| 139 |
(check (maybe= eqv? (just #t #f) (just #t 'z)) => #f) |
| 140 |
(check (maybe= eqv? (just #t #f) (just #t)) => #f) |
| 141 |
|
| 142 |
(check (maybe= eqv? (just #t) (just #t) (just #t)) => #t) |
| 143 |
(check (maybe= eqv? (nothing) (nothing) (nothing)) => #t) |
| 144 |
(check (maybe= eqv? (just #t) (just #t) (nothing)) => #f) |
| 145 |
(check (maybe= eqv? (just #t) (just #t) (just #f)) => #f) |
| 146 |
(check (maybe= eqv? (just #t 'z) (just #t 'z) (just #t 'z)) => #t) |
| 147 |
|
| 148 |
(check (either= eqv? (right #t) (right #t)) => #t) |
| 149 |
(check (either= eqv? (right #t) (right #f)) => #f) |
| 150 |
(check (either= eqv? (left #t) (left #t)) => #t) |
| 151 |
(check (either= eqv? (left #t) (left #f)) => #f) |
| 152 |
(check (either= eqv? (right #t) (left #t)) => #f) |
| 153 |
|
| 154 |
(check (either= eqv? (right #t #f) (right #t #f)) => #t) |
| 155 |
(check (either= eqv? (right #t #f) (right #t 'z)) => #f) |
| 156 |
(check (either= eqv? (right #t #f) (right #t)) => #f) |
| 157 |
(check (either= eqv? (left #t #f) (left #t #f)) => #t) |
| 158 |
(check (either= eqv? (left #t #f) (left #t 'z)) => #f) |
| 159 |
(check (either= eqv? (left #t #f) (left #t)) => #f) |
| 160 |
(check (either= eqv? (left #t #f) (right #t #f)) => #f) |
| 161 |
|
| 162 |
(check (either= eqv? (right #t) (right #t) (right #t)) => #t) |
| 163 |
(check (either= eqv? (left #t) (left #t) (left #t)) => #t) |
| 164 |
(check (either= eqv? (right #t) (right #t) (left #t)) => #f) |
| 165 |
(check (either= eqv? (right #t) (right #t) (right #f)) => #f) |
| 166 |
(check (either= eqv? (right #t 'z) (right #t 'z) (right #t 'z)) => #t)) |
| 167 |
|
| 168 |
;;;; Accessors |
| 169 |
|
| 170 |
(define (check-accessors) |
| 171 |
(print-header "Testing accessors...") |
| 172 |
|
| 173 |
(check (maybe-ref (nothing) (lambda () #f)) => #f) |
| 174 |
(check (maybe-ref (just #t) (lambda () #f) values) => #t) |
| 175 |
(check (maybe-ref (nothing) (lambda () #f) values) => #f) |
| 176 |
|
| 177 |
(check (values->list (maybe-ref (just #t #f) (lambda () #f))) => '(#t #f)) |
| 178 |
(check (maybe-ref (just #t #f) (lambda () #f) list) => '(#t #f)) |
| 179 |
|
| 180 |
(check (either-ref (left #t) (constantly #f)) => #f) |
| 181 |
(check (either-ref (right #t) (constantly #f) values) => #t) |
| 182 |
(check (either-ref (left #t) values (constantly #f)) => #t) |
| 183 |
|
| 184 |
(check (either-ref (right #t #f) (constantly #f) list) => '(#t #f)) |
| 185 |
(check (either-ref (left #t #f) list (constantly #f)) => '(#t #f)) |
| 186 |
|
| 187 |
(check (maybe-ref/default (just #t) #f) => #t) |
| 188 |
(check (maybe-ref/default (nothing) #f) => #f) |
| 189 |
(check (values->list (maybe-ref/default (just #t #t) #f #f)) => '(#t #t)) |
| 190 |
(check (values->list (maybe-ref/default (nothing) #f #f)) => '(#f #f)) |
| 191 |
|
| 192 |
(check (either-ref/default (right #t) #f) => #t) |
| 193 |
(check (either-ref/default (left #t) #f) => #f) |
| 194 |
(check (values->list (either-ref/default (right #t #t) #f #f)) |
| 195 |
=> '(#t #t)) |
| 196 |
(check (values->list (either-ref/default (left #t) #f #f)) |
| 197 |
=> '(#f #f))) |
| 198 |
|
| 199 |
;;;; Join and bind |
| 200 |
|
| 201 |
(define (check-join-and-bind) |
| 202 |
(print-header "Testing join and bind...") |
| 203 |
|
| 204 |
;; maybe-join |
| 205 |
(check (just-of-z? (maybe-join (just (just 'z)))) => #t) |
| 206 |
(check (nothing? (maybe-join (just (nothing)))) => #t) |
| 207 |
(check (nothing? (maybe-join (nothing))) => #t) |
| 208 |
|
| 209 |
;; either-join |
| 210 |
(check (right-of-z? (either-join (right (right 'z)))) => #t) |
| 211 |
(check (left-of-z? (either-join (right (left 'z)))) => #t) |
| 212 |
(check (left-of-z? (either-join (left 'z))) => #t) |
| 213 |
|
| 214 |
;; maybe-bind |
| 215 |
(check (nothing? (maybe-bind (nothing) just)) => #t) |
| 216 |
|
| 217 |
(check (just-of-z? (maybe-bind (just 'z) just)) => #t) |
| 218 |
|
| 219 |
(check (let ((m (just #t #f))) |
| 220 |
(maybe= eqv? m (maybe-bind m just))) |
| 221 |
=> #t) |
| 222 |
|
| 223 |
;; Associativity of bind. |
| 224 |
(let ((k (lambda (n) (just (* n 2)))) |
| 225 |
(h (lambda (n) (just (+ n 5)))) |
| 226 |
(m (just 1))) |
| 227 |
(check (maybe= eqv? |
| 228 |
(maybe-bind m (lambda (n) (maybe-bind (k n) h))) |
| 229 |
(maybe-bind (maybe-bind m k) h)) |
| 230 |
=> #t)) |
| 231 |
|
| 232 |
;; Bind with multiple mprocs. |
| 233 |
(let ((neg (lambda (b) (just (not b))))) |
| 234 |
(check (maybe= eqv? (just #f) (maybe-bind (just #t) neg neg neg)) |
| 235 |
=> #t) |
| 236 |
(check (nothing? (maybe-bind (just #t) neg (constantly (nothing)) neg)) |
| 237 |
=> #t)) |
| 238 |
|
| 239 |
;; maybe-compose |
| 240 |
(check (nothing? ((maybe-compose (constantly (nothing))) 'z)) => #t) |
| 241 |
(check (just-of-z? ((maybe-compose just) 'z)) => #t) |
| 242 |
|
| 243 |
;; Compose with multiple mprocs. |
| 244 |
(let ((neg (lambda (b) (just (not b))))) |
| 245 |
(check (maybe= eqv? (just #t) ((maybe-compose neg neg neg) #f)) |
| 246 |
=> #t)) |
| 247 |
|
| 248 |
;; either-bind |
| 249 |
(check (left? (either-bind (left #f) right)) => #t) |
| 250 |
|
| 251 |
(check (right-of-z? (either-bind (right 'z) right)) => #t) |
| 252 |
|
| 253 |
(check (let ((e (right #t #f))) |
| 254 |
(either= eqv? e (either-bind e right))) |
| 255 |
=> #t) |
| 256 |
|
| 257 |
;; Associativity of bind. |
| 258 |
(let ((k (lambda (n) (right (* n 2)))) |
| 259 |
(h (lambda (n) (right (+ n 5)))) |
| 260 |
(e (right 1))) |
| 261 |
(check |
| 262 |
(either= eqv? (either-bind e (lambda (n) (either-bind (k n) h))) |
| 263 |
(either-bind (either-bind e k) h)) |
| 264 |
=> #t)) |
| 265 |
|
| 266 |
;; Bind with multiple mprocs. |
| 267 |
(let ((neg (lambda (b) (right (not b))))) |
| 268 |
(check (either= eqv? (right #f) (either-bind (right #t) neg neg neg)) |
| 269 |
=> #t) |
| 270 |
(check (either= eqv? (left #f) (either-bind (right #t) neg left neg)) |
| 271 |
=> #t)) |
| 272 |
|
| 273 |
;; either-compose |
| 274 |
(check (left-of-z? ((either-compose left) 'z)) => #t) |
| 275 |
(check (either= eqv? (right #t) ((either-compose right) #t)) => #t) |
| 276 |
|
| 277 |
;; Compose with multiple mprocs. |
| 278 |
(let ((neg (lambda (b) (right (not b))))) |
| 279 |
(check (either= eqv? (right #t) ((either-compose neg neg neg) #f)) |
| 280 |
=> #t))) |
| 281 |
|
| 282 |
;;;; Sequence operations |
| 283 |
|
| 284 |
(define (check-sequence-operations) |
| 285 |
(define (both b c) (and b c)) |
| 286 |
|
| 287 |
(print-header "Testing sequence operations...") |
| 288 |
|
| 289 |
(check (maybe-length (nothing)) => 0) |
| 290 |
(check (maybe-length (just #t)) => 1) |
| 291 |
|
| 292 |
(check (either-length (left #t)) => 0) |
| 293 |
(check (either-length (right #t)) => 1) |
| 294 |
|
| 295 |
;; maybe-filter & maybe-remove |
| 296 |
(check (just-of-z? (maybe-filter always (just 'z))) => #t) |
| 297 |
(check (nothing? (maybe-filter never (just #t))) => #t) |
| 298 |
(check (nothing? (maybe-filter always (nothing))) => #t) |
| 299 |
|
| 300 |
(check (maybe= eqv? (just #t #t) (maybe-filter both (just #t #t))) => #t) |
| 301 |
|
| 302 |
(check (just-of-z? (maybe-remove never (just 'z))) => #t) |
| 303 |
(check (nothing? (maybe-remove always (just #t))) => #t) |
| 304 |
(check (nothing? (maybe-remove always (nothing))) => #t) |
| 305 |
|
| 306 |
(check (maybe= eqv? (just #t #f) (maybe-remove both (just #t #f))) => #t) |
| 307 |
|
| 308 |
;; maybe-sequence |
| 309 |
(check (maybe= equal? (maybe-sequence (map just '(#t #f)) map identity) |
| 310 |
(just '(#t #f))) |
| 311 |
=> #t) |
| 312 |
(check (maybe= equal? (maybe-sequence (list (just 1 #t) (just 2 #f)) |
| 313 |
map |
| 314 |
list) |
| 315 |
(just '((1 #t) (2 #f)))) |
| 316 |
=> #t) |
| 317 |
(check (nothing? (maybe-sequence (list (just #t) (nothing)) map identity)) |
| 318 |
=> #t) |
| 319 |
|
| 320 |
;; either-filter & either-remove |
| 321 |
(check (right-of-z? (either-filter always (right 'z) #f)) => #t) |
| 322 |
(check (left-of-z? (either-filter never (right #t) 'z)) => #t) |
| 323 |
(check (left-of-z? (either-filter always (left #t) 'z)) => #t) |
| 324 |
|
| 325 |
(check (either= eqv? (right #t #t) (either-filter both (right #t #t) #f)) |
| 326 |
=> #t) |
| 327 |
|
| 328 |
(check (right-of-z? (either-remove never (right 'z) #f)) => #t) |
| 329 |
(check (left-of-z? (either-remove always (right #t) 'z)) => #t) |
| 330 |
(check (left-of-z? (either-remove never (left #t) 'z)) => #t) |
| 331 |
|
| 332 |
(check (either= eqv? (right #t #f) (either-remove both (right #t #f) #f)) |
| 333 |
=> #t) |
| 334 |
|
| 335 |
;; either-sequence |
| 336 |
(check (either= equal? (either-sequence (map right (list 1 2)) map identity) |
| 337 |
(right (list 1 2))) |
| 338 |
=> #t) |
| 339 |
(check (left-of-z? (either-sequence (list (right #t) (left 'z)) map identity)) |
| 340 |
=> #t) |
| 341 |
(check (either= equal? (either-sequence (list (right 1 #t) (right 2 #f)) |
| 342 |
map |
| 343 |
list) |
| 344 |
(right '((1 #t) (2 #f)))) |
| 345 |
=> #t)) |
| 346 |
|
| 347 |
;;;; Protocol conversion procedures |
| 348 |
|
| 349 |
(define (check-conversions) |
| 350 |
(print-header "Testing conversions...") |
| 351 |
|
| 352 |
(check (maybe->list (nothing)) => '()) |
| 353 |
(check (maybe->list (just #t #t)) => '(#t #t)) |
| 354 |
(check (either->list (right #t #t)) => '(#t #t)) |
| 355 |
(check (either->list (left #t #t)) => '(#t #t)) |
| 356 |
|
| 357 |
(check (nothing? (list->maybe '())) => #t) |
| 358 |
(check (just-of-z? (list->maybe '(z))) => #t) |
| 359 |
(check (left-of-z? (list->either '() 'z)) => #t) |
| 360 |
(check (right-of-z? (list->either '(z) #f)) => #t) |
| 361 |
|
| 362 |
(check (maybe->truth (nothing)) => #f) |
| 363 |
(check (maybe->truth (just 'z)) => 'z) |
| 364 |
(check (either->truth (left 'z)) => #f) |
| 365 |
(check (either->truth (right 'z)) => 'z) |
| 366 |
|
| 367 |
(check (nothing? (truth->maybe #f)) => #t) |
| 368 |
(check (just-of-z? (truth->maybe 'z)) => #t) |
| 369 |
(check (left-of-z? (truth->either #f 'z)) => #t) |
| 370 |
(check (right-of-z? (truth->either 'z #f)) => #t) |
| 371 |
|
| 372 |
(check (maybe->list-truth (just 'z #t)) => '(z #t)) |
| 373 |
(check (maybe->list-truth (nothing)) => #f) |
| 374 |
(check (either->list-truth (right 'z #t)) => '(z #t)) |
| 375 |
(check (either->list-truth (left 'z)) => #f) |
| 376 |
|
| 377 |
(check (just-of-z? (list-truth->maybe '(z))) => #t) |
| 378 |
(check (nothing? (list-truth->maybe #f)) => #t) |
| 379 |
(check (right-of-z? (list-truth->either '(z))) => #t) |
| 380 |
(check (left-of-z? (list-truth->either #f 'z)) => #t) |
| 381 |
|
| 382 |
(check (eof-object? (maybe->generation (nothing))) => #t) |
| 383 |
(check (maybe->generation (just #t)) => #t) |
| 384 |
(check (nothing? (generation->maybe (eof-object))) => #t) |
| 385 |
(check (just-of-z? (generation->maybe 'z)) => #t) |
| 386 |
|
| 387 |
(check (eof-object? (either->generation (left))) => #t) |
| 388 |
(check (either->generation (right #t)) => #t) |
| 389 |
(check (left-of-z? (generation->either (eof-object) 'z)) => #t) |
| 390 |
(check (right-of-z? (generation->either 'z #f)) => #t) |
| 391 |
|
| 392 |
;; maybe->values and friends |
| 393 |
(check (maybe->values (just #t)) => #t) |
| 394 |
(check (values->list (maybe->values (nothing))) => '()) |
| 395 |
|
| 396 |
(check (values->list (maybe->two-values (nothing))) => '(#f #f)) |
| 397 |
(check (values->list (maybe->two-values (just #t))) => '(#t #t)) |
| 398 |
|
| 399 |
(check (just-of-z? (two-values->maybe (lambda () (values 'z #t)))) => #t) |
| 400 |
(check (nothing? (two-values->maybe (lambda () (values 'z #f)))) => #t) |
| 401 |
|
| 402 |
(check (nothing? (values->maybe (lambda () (values)))) => #t) |
| 403 |
(check (just-of-z? (values->maybe (lambda () 'z))) => #t) |
| 404 |
(check (maybe->values (values->maybe (lambda () #t))) => #t) |
| 405 |
(check (just-of-z? (values->maybe (lambda () |
| 406 |
(maybe->values (just 'z))))) |
| 407 |
=> #t) |
| 408 |
|
| 409 |
;; either->values and friends |
| 410 |
(check (either->values (right #t)) => #t) |
| 411 |
(check (values->list (either->values (left 'z))) => '()) |
| 412 |
|
| 413 |
(check (left-of-z? (values->either (lambda () (values)) 'z)) => #t) |
| 414 |
(check (right-of-z? (values->either (lambda () 'z) #f)) => #t) |
| 415 |
(check (either->values (values->either (lambda () #t) #f)) => #t) |
| 416 |
(check (right-of-z? (values->either (lambda () |
| 417 |
(either->values (right 'z))) |
| 418 |
#f)) |
| 419 |
=> #t) |
| 420 |
|
| 421 |
(check (left-of-z? (exception->either symbol? (lambda () (raise 'z)))) |
| 422 |
=> #t) |
| 423 |
(check (right-of-z? (exception->either symbol? (lambda () 'z))) => #t) |
| 424 |
(check (guard (obj ((symbol? obj) obj)) |
| 425 |
(exception->either number? |
| 426 |
(lambda () (raise-continuable 'z)))) |
| 427 |
=> 'z) |
| 428 |
(check (either= eqv? |
| 429 |
(with-exception-handler |
| 430 |
not |
| 431 |
(lambda () |
| 432 |
(exception->either string? |
| 433 |
(lambda () |
| 434 |
(not (raise-continuable #t)))))) |
| 435 |
(right #t)) |
| 436 |
=> #t)) |
| 437 |
|
| 438 |
;;;; Map, fold, and unfold |
| 439 |
|
| 440 |
(define (check-map-fold-and-unfold) |
| 441 |
(print-header "Testing maps, folds, and unfolds...") |
| 442 |
|
| 443 |
;; maybe-map |
| 444 |
(check (nothing? (maybe-map not (nothing))) => #t) |
| 445 |
(check (maybe= eqv? (just #f) (maybe-map not (just #t))) => #t) |
| 446 |
|
| 447 |
(check (maybe= eqv? (just #t #f) (maybe-map values (just #t #f))) => #t) |
| 448 |
|
| 449 |
;; either-map |
| 450 |
;; Verify that the result is the same Left (in the sense of eqv?). |
| 451 |
(check (let ((e (left #t))) (eqv? e (either-map not e))) => #t) |
| 452 |
(check (either= eqv? (right #f) (either-map not (right #t))) => #t) |
| 453 |
|
| 454 |
(check (let ((e (right #t #f))) |
| 455 |
(either= eqv? e (either-map values e))) |
| 456 |
=> #t) |
| 457 |
|
| 458 |
;; maybe-for-each |
| 459 |
(check (let ((x #f)) |
| 460 |
(maybe-for-each (lambda (y) (set! x y)) (just #t)) |
| 461 |
x) |
| 462 |
=> #t) |
| 463 |
; Given Nothing, ensure the proc argument is not executed. |
| 464 |
(check (let ((x #f)) |
| 465 |
(maybe-for-each (lambda (_) (set! x #t)) (nothing)) |
| 466 |
x) |
| 467 |
=> #f) |
| 468 |
|
| 469 |
;; either-for-each |
| 470 |
(check (let ((x #f)) |
| 471 |
(either-for-each (lambda (y) (set! x y)) (right #t)) |
| 472 |
x) |
| 473 |
=> #t) |
| 474 |
;; Given a Left, ensure the proc argument is not executed. |
| 475 |
(check (let ((x #f)) |
| 476 |
(either-for-each (lambda (_) (set! x #t)) (left 'z)) |
| 477 |
x) |
| 478 |
=> #f) |
| 479 |
|
| 480 |
(check (maybe-fold cons '() (nothing)) => '()) |
| 481 |
(check (maybe-fold cons '() (just #t)) => '(#t)) |
| 482 |
(check (maybe-fold * 2 (just 3 4)) => 24) |
| 483 |
|
| 484 |
(check (either-fold cons '() (left #t)) => '()) |
| 485 |
(check (either-fold cons '() (right #t)) => '(#t)) |
| 486 |
(check (either-fold * 2 (right 3 4)) => 24) |
| 487 |
|
| 488 |
(check (nothing? (maybe-unfold always not always #f)) => #t) |
| 489 |
(check (maybe= eqv? (just #t) (maybe-unfold values not not #f)) => #t) |
| 490 |
(check (maybe= eqv? (just #t 'z) |
| 491 |
(maybe-unfold (lambda (b _) (not b)) |
| 492 |
values |
| 493 |
(lambda (b x) (values (not b) x)) |
| 494 |
#t |
| 495 |
'z)) |
| 496 |
=> #t) |
| 497 |
|
| 498 |
(check (left-of-z? (either-unfold always not always 'z)) => #t) |
| 499 |
(check (either= eqv? (right #t) (either-unfold values not not #f)) => #t) |
| 500 |
(check (either= eqv? (right #t 'z) |
| 501 |
(either-unfold (lambda (b _) (not b)) |
| 502 |
values |
| 503 |
(lambda (b x) (values (not b) x)) |
| 504 |
#t |
| 505 |
'z)) |
| 506 |
=> #t)) |
| 507 |
|
| 508 |
;;(load "test-syntax.scm") |
| 509 |
(include "srfi-189-test-syntax.scm") |
| 510 |
|
| 511 |
;;;; Trivalent logic |
| 512 |
|
| 513 |
(define (check-trivalent) |
| 514 |
(define (tri-true? m) |
| 515 |
(and (just? m) (maybe-ref/default m 'z))) |
| 516 |
|
| 517 |
(define (tri-false? m) |
| 518 |
(and (just? m) (not (maybe-ref/default m 'z)))) |
| 519 |
|
| 520 |
(print-header "Testing trivalent logic...") |
| 521 |
|
| 522 |
(check (tri-true? (tri-not (just #f))) => #t) |
| 523 |
(check (tri-false? (tri-not (just #t))) => #t) |
| 524 |
(check (nothing? (tri-not (nothing))) => #t) |
| 525 |
|
| 526 |
(check (tri-true? (tri=? (just #t) (just 1) (just 'x))) => #t) |
| 527 |
(check (tri-true? (tri=? (just #f) (just #f))) => #t) |
| 528 |
(check (tri-true? (tri=? (just #f) (just #f))) => #t) |
| 529 |
(check (tri-false? (tri=? (just #f) (just #t))) => #t) |
| 530 |
(check (tri-false? (tri=? (just #f) (nothing))) => #t) |
| 531 |
|
| 532 |
(check (tri-true? (tri-and (just #t) (just 1) (just 'x))) => #t) |
| 533 |
(check (nothing? (tri-and (just #t) (nothing))) => #t) |
| 534 |
(check (tri-false? (tri-and (just #f) (just #t))) => #t) |
| 535 |
(check (tri-true? (tri-and)) => #t) |
| 536 |
|
| 537 |
(check (tri-false? (tri-or (just #f) (just #f) (just #f))) => #t) |
| 538 |
(check (nothing? (tri-or (just #f) (nothing))) => #t) |
| 539 |
(let ((m-true (just 'x))) |
| 540 |
(check (maybe= eqv? m-true (tri-or (just #f) m-true)) => #t)) |
| 541 |
(check (tri-false? (tri-or)) => #t) |
| 542 |
|
| 543 |
(check (nothing? (tri-merge (nothing) (nothing) (nothing))) => #t) |
| 544 |
(let ((m-true (just 'x))) |
| 545 |
(check (maybe= eqv? m-true (tri-merge (nothing) m-true)) => #t)) |
| 546 |
(let ((m-false (just #f))) |
| 547 |
(check (maybe= eqv? m-false (tri-merge (nothing) m-false)) => #t)) |
| 548 |
(check (nothing? (tri-merge)) => #t)) |
| 549 |
|
| 550 |
(define (check-all) |
| 551 |
(check-constructors) |
| 552 |
(check-predicates) |
| 553 |
(check-accessors) |
| 554 |
(check-join-and-bind) |
| 555 |
(check-sequence-operations) |
| 556 |
(check-conversions) |
| 557 |
(check-map-fold-and-unfold) |
| 558 |
(check-syntax) |
| 559 |
(check-trivalent) |
| 560 |
|
| 561 |
(check-report)) |
| 562 |
|
| 563 |
(check-all) |