67.upstream.scm 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. ; Copyright (c) 2005 Sebastian Egner and Jens Axel S{\o}gaard.
  2. ;
  3. ; Permission is hereby granted, free of charge, to any person obtaining
  4. ; a 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
  12. ; included in all copies or substantial portions of the Software.
  13. ;
  14. ; THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
  15. ; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. ; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. ; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18. ; LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19. ; OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20. ; WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. ;
  22. ; -----------------------------------------------------------------------
  23. ;
  24. ; Compare procedures SRFI (reference implementation)
  25. ; Sebastian.Egner@philips.com, Jensaxel@soegaard.net
  26. ; history of this file:
  27. ; SE, 14-Oct-2004: first version
  28. ; SE, 18-Oct-2004: 1st redesign: axioms for 'compare function'
  29. ; SE, 29-Oct-2004: 2nd redesign: higher order reverse/map/refine/unite
  30. ; SE, 2-Nov-2004: 3rd redesign: macros cond/refine-compare replace h.o.f's
  31. ; SE, 10-Nov-2004: (im,re) replaced by (re,im) in complex-compare
  32. ; SE, 11-Nov-2004: case-compare by case (not by cond); select-compare added
  33. ; SE, 12-Jan-2005: pair-compare-cdr
  34. ; SE, 15-Feb-2005: stricter typing for compare-<type>; pairwise-not=?
  35. ; SE, 16-Feb-2005: case-compare -> if-compare -> if3; <? </<? chain<? etc.
  36. ; JS, 24-Feb-2005: selection-compare added
  37. ; SE, 25-Feb-2005: selection-compare -> kth-largest modified; if<? etc.
  38. ; JS, 28-Feb-2005: kth-largest modified - is "stable" now
  39. ; SE, 28-Feb-2005: simplified pairwise-not=?/kth-largest; min/max debugged
  40. ; SE, 07-Apr-2005: compare-based type checks made explicit
  41. ; SE, 18-Apr-2005: added (rel? compare) and eq?-test
  42. ; SE, 16-May-2005: naming convention changed; compare-by< etc. optional x y
  43. ; =============================================================================
  44. ; Reference Implementation
  45. ; ========================
  46. ;
  47. ; in R5RS (including hygienic macros)
  48. ; + SRFI-16 (case-lambda)
  49. ; + SRFI-23 (error)
  50. ; + SRFI-27 (random-integer)
  51. ; Implementation remarks:
  52. ; * In general, the emphasis of this implementation is on correctness
  53. ; and portability, not on efficiency.
  54. ; * Variable arity procedures are expressed in terms of case-lambda
  55. ; in the hope that this will produce efficient code for the case
  56. ; where the arity is statically known at the call site.
  57. ; * In procedures that are required to type-check their arguments,
  58. ; we use (compare x x) for executing extra checks. This relies on
  59. ; the assumption that eq? is used to catch this case quickly.
  60. ; * Care has been taken to reference comparison procedures of R5RS
  61. ; only at the time the operations here are being defined. This
  62. ; makes it possible to redefine these operations, if need be.
  63. ; * For the sake of efficiency, some inlining has been done by hand.
  64. ; This is mainly expressed by macros producing defines.
  65. ; * Identifiers of the form compare:<something> are private.
  66. ;
  67. ; Hints for low-level implementation:
  68. ; * The basis of this SRFI are the atomic compare procedures,
  69. ; i.e. boolean-compare, char-compare, etc. and the conditionals
  70. ; if3, if=?, if<? etc., and default-compare. These should make
  71. ; optimal use of the available type information.
  72. ; * For the sake of speed, the reference implementation does not
  73. ; use a LET to save the comparison value c for the ERROR call.
  74. ; This can be fixed in a low-level implementation at no cost.
  75. ; * Type-checks based on (compare x x) are made explicit by the
  76. ; expression (compare:check result compare x ...).
  77. ; * Eq? should can used to speed up built-in compare procedures,
  78. ; but it can only be used after type-checking at least one of
  79. ; the arguments.
  80. (define (compare:checked result compare . args)
  81. (for-each (lambda (x) (compare x x)) args)
  82. result)
  83. ; 3-sided conditional
  84. (define-syntax if3
  85. (syntax-rules ()
  86. ((if3 c less equal greater)
  87. (case c
  88. ((-1) less)
  89. (( 0) equal)
  90. (( 1) greater)
  91. (else (error "comparison value not in {-1,0,1}"))))))
  92. ; 2-sided conditionals for comparisons
  93. (define-syntax compare:if-rel?
  94. (syntax-rules ()
  95. ((compare:if-rel? c-cases a-cases c consequence)
  96. (compare:if-rel? c-cases a-cases c consequence (if #f #f)))
  97. ((compare:if-rel? c-cases a-cases c consequence alternate)
  98. (case c
  99. (c-cases consequence)
  100. (a-cases alternate)
  101. (else (error "comparison value not in {-1,0,1}"))))))
  102. (define-syntax if=?
  103. (syntax-rules ()
  104. ((if=? arg ...)
  105. (compare:if-rel? (0) (-1 1) arg ...))))
  106. (define-syntax if<?
  107. (syntax-rules ()
  108. ((if<? arg ...)
  109. (compare:if-rel? (-1) (0 1) arg ...))))
  110. (define-syntax if>?
  111. (syntax-rules ()
  112. ((if>? arg ...)
  113. (compare:if-rel? (1) (-1 0) arg ...))))
  114. (define-syntax if<=?
  115. (syntax-rules ()
  116. ((if<=? arg ...)
  117. (compare:if-rel? (-1 0) (1) arg ...))))
  118. (define-syntax if>=?
  119. (syntax-rules ()
  120. ((if>=? arg ...)
  121. (compare:if-rel? (0 1) (-1) arg ...))))
  122. (define-syntax if-not=?
  123. (syntax-rules ()
  124. ((if-not=? arg ...)
  125. (compare:if-rel? (-1 1) (0) arg ...))))
  126. ; predicates from compare procedures
  127. (define-syntax compare:define-rel?
  128. (syntax-rules ()
  129. ((compare:define-rel? rel? if-rel?)
  130. (define rel?
  131. (case-lambda
  132. (() (lambda (x y) (if-rel? (default-compare x y) #t #f)))
  133. ((compare) (lambda (x y) (if-rel? (compare x y) #t #f)))
  134. ((x y) (if-rel? (default-compare x y) #t #f))
  135. ((compare x y)
  136. (if (procedure? compare)
  137. (if-rel? (compare x y) #t #f)
  138. (error "not a procedure (Did you mean rel/rel??): " compare))))))))
  139. (compare:define-rel? =? if=?)
  140. (compare:define-rel? <? if<?)
  141. (compare:define-rel? >? if>?)
  142. (compare:define-rel? <=? if<=?)
  143. (compare:define-rel? >=? if>=?)
  144. (compare:define-rel? not=? if-not=?)
  145. ; chains of length 3
  146. (define-syntax compare:define-rel1/rel2?
  147. (syntax-rules ()
  148. ((compare:define-rel1/rel2? rel1/rel2? if-rel1? if-rel2?)
  149. (define rel1/rel2?
  150. (case-lambda
  151. (()
  152. (lambda (x y z)
  153. (if-rel1? (default-compare x y)
  154. (if-rel2? (default-compare y z) #t #f)
  155. (compare:checked #f default-compare z))))
  156. ((compare)
  157. (lambda (x y z)
  158. (if-rel1? (compare x y)
  159. (if-rel2? (compare y z) #t #f)
  160. (compare:checked #f compare z))))
  161. ((x y z)
  162. (if-rel1? (default-compare x y)
  163. (if-rel2? (default-compare y z) #t #f)
  164. (compare:checked #f default-compare z)))
  165. ((compare x y z)
  166. (if-rel1? (compare x y)
  167. (if-rel2? (compare y z) #t #f)
  168. (compare:checked #f compare z))))))))
  169. (compare:define-rel1/rel2? </<? if<? if<?)
  170. (compare:define-rel1/rel2? </<=? if<? if<=?)
  171. (compare:define-rel1/rel2? <=/<? if<=? if<?)
  172. (compare:define-rel1/rel2? <=/<=? if<=? if<=?)
  173. (compare:define-rel1/rel2? >/>? if>? if>?)
  174. (compare:define-rel1/rel2? >/>=? if>? if>=?)
  175. (compare:define-rel1/rel2? >=/>? if>=? if>?)
  176. (compare:define-rel1/rel2? >=/>=? if>=? if>=?)
  177. ; chains of arbitrary length
  178. (define-syntax compare:define-chain-rel?
  179. (syntax-rules ()
  180. ((compare:define-chain-rel? chain-rel? if-rel?)
  181. (define chain-rel?
  182. (case-lambda
  183. ((compare)
  184. #t)
  185. ((compare x1)
  186. (compare:checked #t compare x1))
  187. ((compare x1 x2)
  188. (if-rel? (compare x1 x2) #t #f))
  189. ((compare x1 x2 x3)
  190. (if-rel? (compare x1 x2)
  191. (if-rel? (compare x2 x3) #t #f)
  192. (compare:checked #f compare x3)))
  193. ((compare x1 x2 . x3+)
  194. (if-rel? (compare x1 x2)
  195. (let chain? ((head x2) (tail x3+))
  196. (if (null? tail)
  197. #t
  198. (if-rel? (compare head (car tail))
  199. (chain? (car tail) (cdr tail))
  200. (apply compare:checked #f
  201. compare (cdr tail)))))
  202. (apply compare:checked #f compare x3+))))))))
  203. (compare:define-chain-rel? chain=? if=?)
  204. (compare:define-chain-rel? chain<? if<?)
  205. (compare:define-chain-rel? chain>? if>?)
  206. (compare:define-chain-rel? chain<=? if<=?)
  207. (compare:define-chain-rel? chain>=? if>=?)
  208. ; pairwise inequality
  209. (define pairwise-not=?
  210. (let ((= =) (<= <=))
  211. (case-lambda
  212. ((compare)
  213. #t)
  214. ((compare x1)
  215. (compare:checked #t compare x1))
  216. ((compare x1 x2)
  217. (if-not=? (compare x1 x2) #t #f))
  218. ((compare x1 x2 x3)
  219. (if-not=? (compare x1 x2)
  220. (if-not=? (compare x2 x3)
  221. (if-not=? (compare x1 x3) #t #f)
  222. #f)
  223. (compare:checked #f compare x3)))
  224. ((compare . x1+)
  225. (let unequal? ((x x1+) (n (length x1+)) (unchecked? #t))
  226. (if (< n 2)
  227. (if (and unchecked? (= n 1))
  228. (compare:checked #t compare (car x))
  229. #t)
  230. (let* ((i-pivot (random-integer n))
  231. (x-pivot (list-ref x i-pivot)))
  232. (let split ((i 0) (x x) (x< '()) (x> '()))
  233. (if (null? x)
  234. (and (unequal? x< (length x<) #f)
  235. (unequal? x> (length x>) #f))
  236. (if (= i i-pivot)
  237. (split (+ i 1) (cdr x) x< x>)
  238. (if3 (compare (car x) x-pivot)
  239. (split (+ i 1) (cdr x) (cons (car x) x<) x>)
  240. (if unchecked?
  241. (apply compare:checked #f compare (cdr x))
  242. #f)
  243. (split (+ i 1) (cdr x) x< (cons (car x) x>)))))))))))))
  244. ; min/max
  245. (define min-compare
  246. (case-lambda
  247. ((compare x1)
  248. (compare:checked x1 compare x1))
  249. ((compare x1 x2)
  250. (if<=? (compare x1 x2) x1 x2))
  251. ((compare x1 x2 x3)
  252. (if<=? (compare x1 x2)
  253. (if<=? (compare x1 x3) x1 x3)
  254. (if<=? (compare x2 x3) x2 x3)))
  255. ((compare x1 x2 x3 x4)
  256. (if<=? (compare x1 x2)
  257. (if<=? (compare x1 x3)
  258. (if<=? (compare x1 x4) x1 x4)
  259. (if<=? (compare x3 x4) x3 x4))
  260. (if<=? (compare x2 x3)
  261. (if<=? (compare x2 x4) x2 x4)
  262. (if<=? (compare x3 x4) x3 x4))))
  263. ((compare x1 x2 . x3+)
  264. (let min ((xmin (if<=? (compare x1 x2) x1 x2)) (xs x3+))
  265. (if (null? xs)
  266. xmin
  267. (min (if<=? (compare xmin (car xs)) xmin (car xs))
  268. (cdr xs)))))))
  269. (define max-compare
  270. (case-lambda
  271. ((compare x1)
  272. (compare:checked x1 compare x1))
  273. ((compare x1 x2)
  274. (if>=? (compare x1 x2) x1 x2))
  275. ((compare x1 x2 x3)
  276. (if>=? (compare x1 x2)
  277. (if>=? (compare x1 x3) x1 x3)
  278. (if>=? (compare x2 x3) x2 x3)))
  279. ((compare x1 x2 x3 x4)
  280. (if>=? (compare x1 x2)
  281. (if>=? (compare x1 x3)
  282. (if>=? (compare x1 x4) x1 x4)
  283. (if>=? (compare x3 x4) x3 x4))
  284. (if>=? (compare x2 x3)
  285. (if>=? (compare x2 x4) x2 x4)
  286. (if>=? (compare x3 x4) x3 x4))))
  287. ((compare x1 x2 . x3+)
  288. (let max ((xmax (if>=? (compare x1 x2) x1 x2)) (xs x3+))
  289. (if (null? xs)
  290. xmax
  291. (max (if>=? (compare xmax (car xs)) xmax (car xs))
  292. (cdr xs)))))))
  293. ; kth-largest
  294. (define kth-largest
  295. (let ((= =) (< <))
  296. (case-lambda
  297. ((compare k x0)
  298. (case (modulo k 1)
  299. ((0) (compare:checked x0 compare x0))
  300. (else (error "bad index" k))))
  301. ((compare k x0 x1)
  302. (case (modulo k 2)
  303. ((0) (if<=? (compare x0 x1) x0 x1))
  304. ((1) (if<=? (compare x0 x1) x1 x0))
  305. (else (error "bad index" k))))
  306. ((compare k x0 x1 x2)
  307. (case (modulo k 3)
  308. ((0) (if<=? (compare x0 x1)
  309. (if<=? (compare x0 x2) x0 x2)
  310. (if<=? (compare x1 x2) x1 x2)))
  311. ((1) (if3 (compare x0 x1)
  312. (if<=? (compare x1 x2)
  313. x1
  314. (if<=? (compare x0 x2) x2 x0))
  315. (if<=? (compare x0 x2) x1 x0)
  316. (if<=? (compare x0 x2)
  317. x0
  318. (if<=? (compare x1 x2) x2 x1))))
  319. ((2) (if<=? (compare x0 x1)
  320. (if<=? (compare x1 x2) x2 x1)
  321. (if<=? (compare x0 x2) x2 x0)))
  322. (else (error "bad index" k))))
  323. ((compare k x0 . x1+) ; |x1+| >= 1
  324. (if (not (and (integer? k) (exact? k)))
  325. (error "bad index" k))
  326. (let ((n (+ 1 (length x1+))))
  327. (let kth ((k (modulo k n))
  328. (n n) ; = |x|
  329. (rev #t) ; are x<, x=, x> reversed?
  330. (x (cons x0 x1+)))
  331. (let ((pivot (list-ref x (random-integer n))))
  332. (let split ((x x) (x< '()) (n< 0) (x= '()) (n= 0) (x> '()) (n> 0))
  333. (if (null? x)
  334. (cond
  335. ((< k n<)
  336. (kth k n< (not rev) x<))
  337. ((< k (+ n< n=))
  338. (if rev
  339. (list-ref x= (- (- n= 1) (- k n<)))
  340. (list-ref x= (- k n<))))
  341. (else
  342. (kth (- k (+ n< n=)) n> (not rev) x>)))
  343. (if3 (compare (car x) pivot)
  344. (split (cdr x) (cons (car x) x<) (+ n< 1) x= n= x> n>)
  345. (split (cdr x) x< n< (cons (car x) x=) (+ n= 1) x> n>)
  346. (split (cdr x) x< n< x= n= (cons (car x) x>) (+ n> 1))))))))))))
  347. ; compare functions from predicates
  348. (define compare-by<
  349. (case-lambda
  350. ((lt) (lambda (x y) (if (lt x y) -1 (if (lt y x) 1 0))))
  351. ((lt x y) (if (lt x y) -1 (if (lt y x) 1 0)))))
  352. (define compare-by>
  353. (case-lambda
  354. ((gt) (lambda (x y) (if (gt x y) 1 (if (gt y x) -1 0))))
  355. ((gt x y) (if (gt x y) 1 (if (gt y x) -1 0)))))
  356. (define compare-by<=
  357. (case-lambda
  358. ((le) (lambda (x y) (if (le x y) (if (le y x) 0 -1) 1)))
  359. ((le x y) (if (le x y) (if (le y x) 0 -1) 1))))
  360. (define compare-by>=
  361. (case-lambda
  362. ((ge) (lambda (x y) (if (ge x y) (if (ge y x) 0 1) -1)))
  363. ((ge x y) (if (ge x y) (if (ge y x) 0 1) -1))))
  364. (define compare-by=/<
  365. (case-lambda
  366. ((eq lt) (lambda (x y) (if (eq x y) 0 (if (lt x y) -1 1))))
  367. ((eq lt x y) (if (eq x y) 0 (if (lt x y) -1 1)))))
  368. (define compare-by=/>
  369. (case-lambda
  370. ((eq gt) (lambda (x y) (if (eq x y) 0 (if (gt x y) 1 -1))))
  371. ((eq gt x y) (if (eq x y) 0 (if (gt x y) 1 -1)))))
  372. ; refine and extend construction
  373. (define-syntax refine-compare
  374. (syntax-rules ()
  375. ((refine-compare)
  376. 0)
  377. ((refine-compare c1)
  378. c1)
  379. ((refine-compare c1 c2 cs ...)
  380. (if3 c1 -1 (refine-compare c2 cs ...) 1))))
  381. (define-syntax select-compare
  382. (syntax-rules (else)
  383. ((select-compare x y clause ...)
  384. (let ((x-val x) (y-val y))
  385. (select-compare (x-val y-val clause ...))))
  386. ; used internally: (select-compare (x y clause ...))
  387. ((select-compare (x y))
  388. 0)
  389. ((select-compare (x y (else c ...)))
  390. (refine-compare c ...))
  391. ((select-compare (x y (t? c ...) clause ...))
  392. (let ((t?-val t?))
  393. (let ((tx (t?-val x)) (ty (t?-val y)))
  394. (if tx
  395. (if ty (refine-compare c ...) -1)
  396. (if ty 1 (select-compare (x y clause ...)))))))))
  397. (define-syntax cond-compare
  398. (syntax-rules (else)
  399. ((cond-compare)
  400. 0)
  401. ((cond-compare (else cs ...))
  402. (refine-compare cs ...))
  403. ((cond-compare ((tx ty) cs ...) clause ...)
  404. (let ((tx-val tx) (ty-val ty))
  405. (if tx-val
  406. (if ty-val (refine-compare cs ...) -1)
  407. (if ty-val 1 (cond-compare clause ...)))))))
  408. ; R5RS atomic types
  409. (define-syntax compare:type-check
  410. (syntax-rules ()
  411. ((compare:type-check type? type-name x)
  412. (if (not (type? x))
  413. (error (string-append "not " type-name ":") x)))
  414. ((compare:type-check type? type-name x y)
  415. (begin (compare:type-check type? type-name x)
  416. (compare:type-check type? type-name y)))))
  417. (define-syntax compare:define-by=/<
  418. (syntax-rules ()
  419. ((compare:define-by=/< compare = < type? type-name)
  420. (define compare
  421. (let ((= =) (< <))
  422. (lambda (x y)
  423. (if (type? x)
  424. (if (eq? x y)
  425. 0
  426. (if (type? y)
  427. (if (= x y) 0 (if (< x y) -1 1))
  428. (error (string-append "not " type-name ":") y)))
  429. (error (string-append "not " type-name ":") x))))))))
  430. (define (boolean-compare x y)
  431. (compare:type-check boolean? "boolean" x y)
  432. (if x (if y 0 1) (if y -1 0)))
  433. (compare:define-by=/< char-compare char=? char<? char? "char")
  434. (compare:define-by=/< char-compare-ci char-ci=? char-ci<? char? "char")
  435. (compare:define-by=/< string-compare string=? string<? string? "string")
  436. (compare:define-by=/< string-compare-ci string-ci=? string-ci<? string? "string")
  437. (define (symbol-compare x y)
  438. (compare:type-check symbol? "symbol" x y)
  439. (string-compare (symbol->string x) (symbol->string y)))
  440. (compare:define-by=/< integer-compare = < integer? "integer")
  441. (compare:define-by=/< rational-compare = < rational? "rational")
  442. (compare:define-by=/< real-compare = < real? "real")
  443. (define (complex-compare x y)
  444. (compare:type-check complex? "complex" x y)
  445. (if (and (real? x) (real? y))
  446. (real-compare x y)
  447. (refine-compare (real-compare (real-part x) (real-part y))
  448. (real-compare (imag-part x) (imag-part y)))))
  449. (define (number-compare x y)
  450. (compare:type-check number? "number" x y)
  451. (complex-compare x y))
  452. ; R5RS compound data structures: dotted pair, list, vector
  453. (define (pair-compare-car compare)
  454. (lambda (x y)
  455. (compare (car x) (car y))))
  456. (define (pair-compare-cdr compare)
  457. (lambda (x y)
  458. (compare (cdr x) (cdr y))))
  459. (define pair-compare
  460. (case-lambda
  461. ; dotted pair
  462. ((pair-compare-car pair-compare-cdr x y)
  463. (refine-compare (pair-compare-car (car x) (car y))
  464. (pair-compare-cdr (cdr x) (cdr y))))
  465. ; possibly improper lists
  466. ((compare x y)
  467. (cond-compare
  468. (((null? x) (null? y)) 0)
  469. (((pair? x) (pair? y)) (compare (car x) (car y))
  470. (pair-compare compare (cdr x) (cdr y)))
  471. (else (compare x y))))
  472. ; for convenience
  473. ((x y)
  474. (pair-compare default-compare x y))))
  475. (define list-compare
  476. (case-lambda
  477. ((compare x y empty? head tail)
  478. (cond-compare
  479. (((empty? x) (empty? y)) 0)
  480. (else (compare (head x) (head y))
  481. (list-compare compare (tail x) (tail y) empty? head tail))))
  482. ; for convenience
  483. (( x y empty? head tail)
  484. (list-compare default-compare x y empty? head tail))
  485. ((compare x y )
  486. (list-compare compare x y null? car cdr))
  487. (( x y )
  488. (list-compare default-compare x y null? car cdr))))
  489. (define list-compare-as-vector
  490. (case-lambda
  491. ((compare x y empty? head tail)
  492. (refine-compare
  493. (let compare-length ((x x) (y y))
  494. (cond-compare
  495. (((empty? x) (empty? y)) 0)
  496. (else (compare-length (tail x) (tail y)))))
  497. (list-compare compare x y empty? head tail)))
  498. ; for convenience
  499. (( x y empty? head tail)
  500. (list-compare-as-vector default-compare x y empty? head tail))
  501. ((compare x y )
  502. (list-compare-as-vector compare x y null? car cdr))
  503. (( x y )
  504. (list-compare-as-vector default-compare x y null? car cdr))))
  505. (define vector-compare
  506. (let ((= =))
  507. (case-lambda
  508. ((compare x y size ref)
  509. (let ((n (size x)) (m (size y)))
  510. (refine-compare
  511. (integer-compare n m)
  512. (let compare-rest ((i 0)) ; compare x[i..n-1] y[i..n-1]
  513. (if (= i n)
  514. 0
  515. (refine-compare (compare (ref x i) (ref y i))
  516. (compare-rest (+ i 1))))))))
  517. ; for convenience
  518. (( x y size ref)
  519. (vector-compare default-compare x y size ref))
  520. ((compare x y )
  521. (vector-compare compare x y vector-length vector-ref))
  522. (( x y )
  523. (vector-compare default-compare x y vector-length vector-ref)))))
  524. (define vector-compare-as-list
  525. (let ((= =))
  526. (case-lambda
  527. ((compare x y size ref)
  528. (let ((nx (size x)) (ny (size y)))
  529. (let ((n (min nx ny)))
  530. (let compare-rest ((i 0)) ; compare x[i..n-1] y[i..n-1]
  531. (if (= i n)
  532. (integer-compare nx ny)
  533. (refine-compare (compare (ref x i) (ref y i))
  534. (compare-rest (+ i 1))))))))
  535. ; for convenience
  536. (( x y size ref)
  537. (vector-compare-as-list default-compare x y size ref))
  538. ((compare x y )
  539. (vector-compare-as-list compare x y vector-length vector-ref))
  540. (( x y )
  541. (vector-compare-as-list default-compare x y vector-length vector-ref)))))
  542. ; default compare
  543. (define (default-compare x y)
  544. (select-compare
  545. x y
  546. (null? 0)
  547. (pair? (default-compare (car x) (car y))
  548. (default-compare (cdr x) (cdr y)))
  549. (boolean? (boolean-compare x y))
  550. (char? (char-compare x y))
  551. (string? (string-compare x y))
  552. (symbol? (symbol-compare x y))
  553. (number? (number-compare x y))
  554. (vector? (vector-compare default-compare x y))
  555. (else (error "unrecognized type in default-compare" x y))))
  556. ; Note that we pass default-compare to compare-{pair,vector} explictly.
  557. ; This makes sure recursion proceeds with this default-compare, which
  558. ; need not be the one in the lexical scope of compare-{pair,vector}.
  559. ; debug compare
  560. (define (debug-compare c)
  561. (define (checked-value c x y)
  562. (let ((c-xy (c x y)))
  563. (if (or (eqv? c-xy -1) (eqv? c-xy 0) (eqv? c-xy 1))
  564. c-xy
  565. (error "compare value not in {-1,0,1}" c-xy (list c x y)))))
  566. (define (random-boolean)
  567. (zero? (random-integer 2)))
  568. (define q ; (u v w) such that u <= v, v <= w, and not u <= w
  569. '#(
  570. ;x < y x = y x > y [x < z]
  571. 0 0 0 ; y < z
  572. 0 (z y x) (z y x) ; y = z
  573. 0 (z y x) (z y x) ; y > z
  574. ;x < y x = y x > y [x = z]
  575. (y z x) (z x y) 0 ; y < z
  576. (y z x) 0 (x z y) ; y = z
  577. 0 (y x z) (x z y) ; y > z
  578. ;x < y x = y x > y [x > z]
  579. (x y z) (x y z) 0 ; y < z
  580. (x y z) (x y z) 0 ; y = z
  581. 0 0 0 ; y > z
  582. ))
  583. (let ((z? #f) (z #f)) ; stored element from previous call
  584. (lambda (x y)
  585. (let ((c-xx (checked-value c x x))
  586. (c-yy (checked-value c y y))
  587. (c-xy (checked-value c x y))
  588. (c-yx (checked-value c y x)))
  589. (if (not (zero? c-xx))
  590. (error "compare error: not reflexive" c x))
  591. (if (not (zero? c-yy))
  592. (error "compare error: not reflexive" c y))
  593. (if (not (zero? (+ c-xy c-yx)))
  594. (error "compare error: not anti-symmetric" c x y))
  595. (if z?
  596. (let ((c-xz (checked-value c x z))
  597. (c-zx (checked-value c z x))
  598. (c-yz (checked-value c y z))
  599. (c-zy (checked-value c z y)))
  600. (if (not (zero? (+ c-xz c-zx)))
  601. (error "compare error: not anti-symmetric" c x z))
  602. (if (not (zero? (+ c-yz c-zy)))
  603. (error "compare error: not anti-symmetric" c y z))
  604. (let ((ijk (vector-ref q (+ c-xy (* 3 c-yz) (* 9 c-xz) 13))))
  605. (if (list? ijk)
  606. (apply error
  607. "compare error: not transitive"
  608. c
  609. (map (lambda (i) (case i ((x) x) ((y) y) ((z) z)))
  610. ijk)))))
  611. (set! z? #t))
  612. (set! z (if (random-boolean) x y)) ; randomized testing
  613. c-xy))))