compare.scm 24 KB

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