srfi-67.scm 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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. ; Scheme 48 hack: Using COND + EQ? over CASE speeds things up quite a
  85. ; bit; this isn't portable, however.
  86. (define-syntax if3
  87. (syntax-rules ()
  88. ((if3 ?c ?less ?equal ?greater)
  89. (let ((c ?c))
  90. (cond
  91. ((eq? c -1) ?less)
  92. ((eq? c 0) ?equal)
  93. ((eq? c 1) ?greater)
  94. (else (error "comparison value not in {-1,0,1}")))))))
  95. ; 2-sided conditionals for comparisons
  96. (define-syntax compare:if-rel?
  97. (syntax-rules ()
  98. ((compare:if-rel? c-cases a-cases c consequence)
  99. (compare:if-rel? c-cases a-cases c consequence (if #f #f)))
  100. ((compare:if-rel? c-cases a-cases c consequence alternate)
  101. (case c
  102. (c-cases consequence)
  103. (a-cases alternate)
  104. (else (error "comparison value not in {-1,0,1}"))))))
  105. (define-syntax if=?
  106. (syntax-rules ()
  107. ((if=? arg ...)
  108. (compare:if-rel? (0) (-1 1) arg ...))))
  109. (define-syntax if<?
  110. (syntax-rules ()
  111. ((if<? arg ...)
  112. (compare:if-rel? (-1) (0 1) arg ...))))
  113. (define-syntax if>?
  114. (syntax-rules ()
  115. ((if>? arg ...)
  116. (compare:if-rel? (1) (-1 0) arg ...))))
  117. (define-syntax if<=?
  118. (syntax-rules ()
  119. ((if<=? arg ...)
  120. (compare:if-rel? (-1 0) (1) arg ...))))
  121. (define-syntax if>=?
  122. (syntax-rules ()
  123. ((if>=? arg ...)
  124. (compare:if-rel? (0 1) (-1) arg ...))))
  125. (define-syntax if-not=?
  126. (syntax-rules ()
  127. ((if-not=? arg ...)
  128. (compare:if-rel? (-1 1) (0) arg ...))))
  129. ; predicates from compare procedures
  130. (define-syntax compare:define-rel?
  131. (syntax-rules ()
  132. ((compare:define-rel? rel? if-rel?)
  133. (define rel?
  134. (case-lambda
  135. (() (lambda (x y) (if-rel? (default-compare x y) #t #f)))
  136. ((compare) (lambda (x y) (if-rel? (compare x y) #t #f)))
  137. ((x y) (if-rel? (default-compare x y) #t #f))
  138. ((compare x y)
  139. (if (procedure? compare)
  140. (if-rel? (compare x y) #t #f)
  141. (error "not a procedure (Did you mean rel/rel??): " compare))))))))
  142. (compare:define-rel? =? if=?)
  143. (compare:define-rel? <? if<?)
  144. (compare:define-rel? >? if>?)
  145. (compare:define-rel? <=? if<=?)
  146. (compare:define-rel? >=? if>=?)
  147. (compare:define-rel? not=? if-not=?)
  148. ; chains of length 3
  149. (define-syntax compare:define-rel1/rel2?
  150. (syntax-rules ()
  151. ((compare:define-rel1/rel2? rel1/rel2? if-rel1? if-rel2?)
  152. (define rel1/rel2?
  153. (case-lambda
  154. (()
  155. (lambda (x y z)
  156. (if-rel1? (default-compare x y)
  157. (if-rel2? (default-compare y z) #t #f)
  158. (compare:checked #f default-compare z))))
  159. ((compare)
  160. (lambda (x y z)
  161. (if-rel1? (compare x y)
  162. (if-rel2? (compare y z) #t #f)
  163. (compare:checked #f compare z))))
  164. ((x y z)
  165. (if-rel1? (default-compare x y)
  166. (if-rel2? (default-compare y z) #t #f)
  167. (compare:checked #f default-compare z)))
  168. ((compare x y z)
  169. (if-rel1? (compare x y)
  170. (if-rel2? (compare y z) #t #f)
  171. (compare:checked #f compare z))))))))
  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. (compare:define-rel1/rel2? >/>=? if>? if>=?)
  178. (compare:define-rel1/rel2? >=/>? if>=? if>?)
  179. (compare:define-rel1/rel2? >=/>=? if>=? if>=?)
  180. ; chains of arbitrary length
  181. (define-syntax compare:define-chain-rel?
  182. (syntax-rules ()
  183. ((compare:define-chain-rel? chain-rel? if-rel?)
  184. (define chain-rel?
  185. (case-lambda
  186. ((compare)
  187. #t)
  188. ((compare x1)
  189. (compare:checked #t compare x1))
  190. ((compare x1 x2)
  191. (if-rel? (compare x1 x2) #t #f))
  192. ((compare x1 x2 x3)
  193. (if-rel? (compare x1 x2)
  194. (if-rel? (compare x2 x3) #t #f)
  195. (compare:checked #f compare x3)))
  196. ((compare x1 x2 . x3+)
  197. (if-rel? (compare x1 x2)
  198. (let chain? ((head x2) (tail x3+))
  199. (if (null? tail)
  200. #t
  201. (if-rel? (compare head (car tail))
  202. (chain? (car tail) (cdr tail))
  203. (apply compare:checked #f
  204. compare (cdr tail)))))
  205. (apply compare:checked #f compare x3+))))))))
  206. (compare:define-chain-rel? chain=? if=?)
  207. (compare:define-chain-rel? chain<? if<?)
  208. (compare:define-chain-rel? chain>? if>?)
  209. (compare:define-chain-rel? chain<=? if<=?)
  210. (compare:define-chain-rel? chain>=? if>=?)
  211. ; pairwise inequality
  212. (define pairwise-not=?
  213. (let ((= =) (<= <=))
  214. (case-lambda
  215. ((compare)
  216. #t)
  217. ((compare x1)
  218. (compare:checked #t compare x1))
  219. ((compare x1 x2)
  220. (if-not=? (compare x1 x2) #t #f))
  221. ((compare x1 x2 x3)
  222. (if-not=? (compare x1 x2)
  223. (if-not=? (compare x2 x3)
  224. (if-not=? (compare x1 x3) #t #f)
  225. #f)
  226. (compare:checked #f compare x3)))
  227. ((compare . x1+)
  228. (let unequal? ((x x1+) (n (length x1+)) (unchecked? #t))
  229. (if (< n 2)
  230. (if (and unchecked? (= n 1))
  231. (compare:checked #t compare (car x))
  232. #t)
  233. (let* ((i-pivot (random-integer n))
  234. (x-pivot (list-ref x i-pivot)))
  235. (let split ((i 0) (x x) (x< '()) (x> '()))
  236. (if (null? x)
  237. (and (unequal? x< (length x<) #f)
  238. (unequal? x> (length x>) #f))
  239. (if (= i i-pivot)
  240. (split (+ i 1) (cdr x) x< x>)
  241. (if3 (compare (car x) x-pivot)
  242. (split (+ i 1) (cdr x) (cons (car x) x<) x>)
  243. (if unchecked?
  244. (apply compare:checked #f compare (cdr x))
  245. #f)
  246. (split (+ i 1) (cdr x) x< (cons (car x) x>)))))))))))))
  247. ; min/max
  248. (define min-compare
  249. (case-lambda
  250. ((compare x1)
  251. (compare:checked x1 compare x1))
  252. ((compare x1 x2)
  253. (if<=? (compare x1 x2) x1 x2))
  254. ((compare x1 x2 x3)
  255. (if<=? (compare x1 x2)
  256. (if<=? (compare x1 x3) x1 x3)
  257. (if<=? (compare x2 x3) x2 x3)))
  258. ((compare x1 x2 x3 x4)
  259. (if<=? (compare x1 x2)
  260. (if<=? (compare x1 x3)
  261. (if<=? (compare x1 x4) x1 x4)
  262. (if<=? (compare x3 x4) x3 x4))
  263. (if<=? (compare x2 x3)
  264. (if<=? (compare x2 x4) x2 x4)
  265. (if<=? (compare x3 x4) x3 x4))))
  266. ((compare x1 x2 . x3+)
  267. (let min ((xmin (if<=? (compare x1 x2) x1 x2)) (xs x3+))
  268. (if (null? xs)
  269. xmin
  270. (min (if<=? (compare xmin (car xs)) xmin (car xs))
  271. (cdr xs)))))))
  272. (define max-compare
  273. (case-lambda
  274. ((compare x1)
  275. (compare:checked x1 compare x1))
  276. ((compare x1 x2)
  277. (if>=? (compare x1 x2) x1 x2))
  278. ((compare x1 x2 x3)
  279. (if>=? (compare x1 x2)
  280. (if>=? (compare x1 x3) x1 x3)
  281. (if>=? (compare x2 x3) x2 x3)))
  282. ((compare x1 x2 x3 x4)
  283. (if>=? (compare x1 x2)
  284. (if>=? (compare x1 x3)
  285. (if>=? (compare x1 x4) x1 x4)
  286. (if>=? (compare x3 x4) x3 x4))
  287. (if>=? (compare x2 x3)
  288. (if>=? (compare x2 x4) x2 x4)
  289. (if>=? (compare x3 x4) x3 x4))))
  290. ((compare x1 x2 . x3+)
  291. (let max ((xmax (if>=? (compare x1 x2) x1 x2)) (xs x3+))
  292. (if (null? xs)
  293. xmax
  294. (max (if>=? (compare xmax (car xs)) xmax (car xs))
  295. (cdr xs)))))))
  296. ; kth-largest
  297. (define kth-largest
  298. (let ((= =) (< <))
  299. (case-lambda
  300. ((compare k x0)
  301. (case (modulo k 1)
  302. ((0) (compare:checked x0 compare x0))
  303. (else (error "bad index" k))))
  304. ((compare k x0 x1)
  305. (case (modulo k 2)
  306. ((0) (if<=? (compare x0 x1) x0 x1))
  307. ((1) (if<=? (compare x0 x1) x1 x0))
  308. (else (error "bad index" k))))
  309. ((compare k x0 x1 x2)
  310. (case (modulo k 3)
  311. ((0) (if<=? (compare x0 x1)
  312. (if<=? (compare x0 x2) x0 x2)
  313. (if<=? (compare x1 x2) x1 x2)))
  314. ((1) (if3 (compare x0 x1)
  315. (if<=? (compare x1 x2)
  316. x1
  317. (if<=? (compare x0 x2) x2 x0))
  318. (if<=? (compare x0 x2) x1 x0)
  319. (if<=? (compare x0 x2)
  320. x0
  321. (if<=? (compare x1 x2) x2 x1))))
  322. ((2) (if<=? (compare x0 x1)
  323. (if<=? (compare x1 x2) x2 x1)
  324. (if<=? (compare x0 x2) x2 x0)))
  325. (else (error "bad index" k))))
  326. ((compare k x0 . x1+) ; |x1+| >= 1
  327. (if (not (and (integer? k) (exact? k)))
  328. (error "bad index" k))
  329. (let ((n (+ 1 (length x1+))))
  330. (let kth ((k (modulo k n))
  331. (n n) ; = |x|
  332. (rev #t) ; are x<, x=, x> reversed?
  333. (x (cons x0 x1+)))
  334. (let ((pivot (list-ref x (random-integer n))))
  335. (let split ((x x) (x< '()) (n< 0) (x= '()) (n= 0) (x> '()) (n> 0))
  336. (if (null? x)
  337. (cond
  338. ((< k n<)
  339. (kth k n< (not rev) x<))
  340. ((< k (+ n< n=))
  341. (if rev
  342. (list-ref x= (- (- n= 1) (- k n<)))
  343. (list-ref x= (- k n<))))
  344. (else
  345. (kth (- k (+ n< n=)) n> (not rev) x>)))
  346. (if3 (compare (car x) pivot)
  347. (split (cdr x) (cons (car x) x<) (+ n< 1) x= n= x> n>)
  348. (split (cdr x) x< n< (cons (car x) x=) (+ n= 1) x> n>)
  349. (split (cdr x) x< n< x= n= (cons (car x) x>) (+ n> 1))))))))))))
  350. ; compare functions from predicates
  351. (define compare-by<
  352. (case-lambda
  353. ((lt) (lambda (x y) (if (lt x y) -1 (if (lt y x) 1 0))))
  354. ((lt x y) (if (lt x y) -1 (if (lt y x) 1 0)))))
  355. (define compare-by>
  356. (case-lambda
  357. ((gt) (lambda (x y) (if (gt x y) 1 (if (gt y x) -1 0))))
  358. ((gt x y) (if (gt x y) 1 (if (gt y x) -1 0)))))
  359. (define compare-by<=
  360. (case-lambda
  361. ((le) (lambda (x y) (if (le x y) (if (le y x) 0 -1) 1)))
  362. ((le x y) (if (le x y) (if (le y x) 0 -1) 1))))
  363. (define compare-by>=
  364. (case-lambda
  365. ((ge) (lambda (x y) (if (ge x y) (if (ge y x) 0 1) -1)))
  366. ((ge x y) (if (ge x y) (if (ge y x) 0 1) -1))))
  367. (define compare-by=/<
  368. (case-lambda
  369. ((eq lt) (lambda (x y) (if (eq x y) 0 (if (lt x y) -1 1))))
  370. ((eq lt x y) (if (eq x y) 0 (if (lt x y) -1 1)))))
  371. (define compare-by=/>
  372. (case-lambda
  373. ((eq gt) (lambda (x y) (if (eq x y) 0 (if (gt x y) 1 -1))))
  374. ((eq gt x y) (if (eq x y) 0 (if (gt x y) 1 -1)))))
  375. ; refine and extend construction
  376. (define-syntax refine-compare
  377. (syntax-rules ()
  378. ((refine-compare)
  379. 0)
  380. ((refine-compare c1)
  381. c1)
  382. ((refine-compare c1 c2 cs ...)
  383. (if3 c1 -1 (refine-compare c2 cs ...) 1))))
  384. (define-syntax select-compare
  385. (syntax-rules (else)
  386. ((select-compare x y clause ...)
  387. (let ((x-val x) (y-val y))
  388. (select-compare (x-val y-val clause ...))))
  389. ; used internally: (select-compare (x y clause ...))
  390. ((select-compare (x y))
  391. 0)
  392. ((select-compare (x y (else c ...)))
  393. (refine-compare c ...))
  394. ((select-compare (x y (t? c ...) clause ...))
  395. (let ((t?-val t?))
  396. (let ((tx (t?-val x)) (ty (t?-val y)))
  397. (if tx
  398. (if ty (refine-compare c ...) -1)
  399. (if ty 1 (select-compare (x y clause ...)))))))))
  400. (define-syntax cond-compare
  401. (syntax-rules (else)
  402. ((cond-compare)
  403. 0)
  404. ((cond-compare (else cs ...))
  405. (refine-compare cs ...))
  406. ((cond-compare ((tx ty) cs ...) clause ...)
  407. (let ((tx-val tx) (ty-val ty))
  408. (if tx-val
  409. (if ty-val (refine-compare cs ...) -1)
  410. (if ty-val 1 (cond-compare clause ...)))))))
  411. ; R5RS atomic types
  412. (define-syntax compare:type-check
  413. (syntax-rules ()
  414. ((compare:type-check type? type-name x)
  415. (if (not (type? x))
  416. (error (string-append "not " type-name ":") x)))
  417. ((compare:type-check type? type-name x y)
  418. (begin (compare:type-check type? type-name x)
  419. (compare:type-check type? type-name y)))))
  420. (define-syntax compare:define-by=/<
  421. (syntax-rules ()
  422. ((compare:define-by=/< compare = < type? type-name)
  423. (define compare
  424. (let ((= =) (< <))
  425. (lambda (x y)
  426. (if (type? x)
  427. (if (eq? x y)
  428. 0
  429. (if (type? y)
  430. (if (= x y) 0 (if (< x y) -1 1))
  431. (error (string-append "not " type-name ":") y)))
  432. (error (string-append "not " type-name ":") x))))))))
  433. (define (boolean-compare x y)
  434. (compare:type-check boolean? "boolean" x y)
  435. (if x (if y 0 1) (if y -1 0)))
  436. (compare:define-by=/< char-compare char=? char<? char? "char")
  437. (compare:define-by=/< char-compare-ci char-ci=? char-ci<? char? "char")
  438. (compare:define-by=/< string-compare string=? string<? string? "string")
  439. (compare:define-by=/< string-compare-ci string-ci=? string-ci<? string? "string")
  440. (define (symbol-compare x y)
  441. (compare:type-check symbol? "symbol" x y)
  442. (string-compare (symbol->string x) (symbol->string y)))
  443. (compare:define-by=/< integer-compare = < integer? "integer")
  444. (compare:define-by=/< rational-compare = < rational? "rational")
  445. (compare:define-by=/< real-compare = < real? "real")
  446. (define (complex-compare x y)
  447. (compare:type-check complex? "complex" x y)
  448. (if (and (real? x) (real? y))
  449. (real-compare x y)
  450. (refine-compare (real-compare (real-part x) (real-part y))
  451. (real-compare (imag-part x) (imag-part y)))))
  452. (define (number-compare x y)
  453. (compare:type-check number? "number" x y)
  454. (complex-compare x y))
  455. ; R5RS compound data structures: dotted pair, list, vector
  456. (define (pair-compare-car compare)
  457. (lambda (x y)
  458. (compare (car x) (car y))))
  459. (define (pair-compare-cdr compare)
  460. (lambda (x y)
  461. (compare (cdr x) (cdr y))))
  462. (define pair-compare
  463. (case-lambda
  464. ; dotted pair
  465. ((pair-compare-car pair-compare-cdr x y)
  466. (refine-compare (pair-compare-car (car x) (car y))
  467. (pair-compare-cdr (cdr x) (cdr y))))
  468. ; possibly improper lists
  469. ((compare x y)
  470. (cond-compare
  471. (((null? x) (null? y)) 0)
  472. (((pair? x) (pair? y)) (compare (car x) (car y))
  473. (pair-compare compare (cdr x) (cdr y)))
  474. (else (compare x y))))
  475. ; for convenience
  476. ((x y)
  477. (pair-compare default-compare x y))))
  478. (define list-compare
  479. (case-lambda
  480. ((compare x y empty? head tail)
  481. (cond-compare
  482. (((empty? x) (empty? y)) 0)
  483. (else (compare (head x) (head y))
  484. (list-compare compare (tail x) (tail y) empty? head tail))))
  485. ; for convenience
  486. (( x y empty? head tail)
  487. (list-compare default-compare x y empty? head tail))
  488. ((compare x y )
  489. (list-compare compare x y null? car cdr))
  490. (( x y )
  491. (list-compare default-compare x y null? car cdr))))
  492. (define list-compare-as-vector
  493. (case-lambda
  494. ((compare x y empty? head tail)
  495. (refine-compare
  496. (let compare-length ((x x) (y y))
  497. (cond-compare
  498. (((empty? x) (empty? y)) 0)
  499. (else (compare-length (tail x) (tail y)))))
  500. (list-compare compare x y empty? head tail)))
  501. ; for convenience
  502. (( x y empty? head tail)
  503. (list-compare-as-vector default-compare x y empty? head tail))
  504. ((compare x y )
  505. (list-compare-as-vector compare x y null? car cdr))
  506. (( x y )
  507. (list-compare-as-vector default-compare x y null? car cdr))))
  508. (define vector-compare
  509. (let ((= =))
  510. (case-lambda
  511. ((compare x y size ref)
  512. (let ((n (size x)) (m (size y)))
  513. (refine-compare
  514. (integer-compare n m)
  515. (let compare-rest ((i 0)) ; compare x[i..n-1] y[i..n-1]
  516. (if (= i n)
  517. 0
  518. (refine-compare (compare (ref x i) (ref y i))
  519. (compare-rest (+ i 1))))))))
  520. ; for convenience
  521. (( x y size ref)
  522. (vector-compare default-compare x y size ref))
  523. ((compare x y )
  524. (vector-compare compare x y vector-length vector-ref))
  525. (( x y )
  526. (vector-compare default-compare x y vector-length vector-ref)))))
  527. (define vector-compare-as-list
  528. (let ((= =))
  529. (case-lambda
  530. ((compare x y size ref)
  531. (let ((nx (size x)) (ny (size y)))
  532. (let ((n (min nx ny)))
  533. (let compare-rest ((i 0)) ; compare x[i..n-1] y[i..n-1]
  534. (if (= i n)
  535. (integer-compare nx ny)
  536. (refine-compare (compare (ref x i) (ref y i))
  537. (compare-rest (+ i 1))))))))
  538. ; for convenience
  539. (( x y size ref)
  540. (vector-compare-as-list default-compare x y size ref))
  541. ((compare x y )
  542. (vector-compare-as-list compare x y vector-length vector-ref))
  543. (( x y )
  544. (vector-compare-as-list default-compare x y vector-length vector-ref)))))
  545. ; default compare
  546. (define (default-compare x y)
  547. (select-compare
  548. x y
  549. (null? 0)
  550. (pair? (default-compare (car x) (car y))
  551. (default-compare (cdr x) (cdr y)))
  552. (boolean? (boolean-compare x y))
  553. (char? (char-compare x y))
  554. (string? (string-compare x y))
  555. (symbol? (symbol-compare x y))
  556. (number? (number-compare x y))
  557. (vector? (vector-compare default-compare x y))
  558. (else (error "unrecognized type in default-compare" x y))))
  559. ; Note that we pass default-compare to compare-{pair,vector} explictly.
  560. ; This makes sure recursion proceeds with this default-compare, which
  561. ; need not be the one in the lexical scope of compare-{pair,vector}.
  562. ; debug compare
  563. (define (debug-compare c)
  564. (define (checked-value c x y)
  565. (let ((c-xy (c x y)))
  566. (if (or (eqv? c-xy -1) (eqv? c-xy 0) (eqv? c-xy 1))
  567. c-xy
  568. (error "compare value not in {-1,0,1}" c-xy (list c x y)))))
  569. (define (random-boolean)
  570. (zero? (random-integer 2)))
  571. (define q ; (u v w) such that u <= v, v <= w, and not u <= w
  572. '#(
  573. ;x < y x = y x > y [x < z]
  574. 0 0 0 ; y < z
  575. 0 (z y x) (z y x) ; y = z
  576. 0 (z y x) (z y x) ; y > z
  577. ;x < y x = y x > y [x = z]
  578. (y z x) (z x y) 0 ; y < z
  579. (y z x) 0 (x z y) ; y = z
  580. 0 (y x z) (x z y) ; y > z
  581. ;x < y x = y x > y [x > z]
  582. (x y z) (x y z) 0 ; y < z
  583. (x y z) (x y z) 0 ; y = z
  584. 0 0 0 ; y > z
  585. ))
  586. (let ((z? #f) (z #f)) ; stored element from previous call
  587. (lambda (x y)
  588. (let ((c-xx (checked-value c x x))
  589. (c-yy (checked-value c y y))
  590. (c-xy (checked-value c x y))
  591. (c-yx (checked-value c y x)))
  592. (if (not (zero? c-xx))
  593. (error "compare error: not reflexive" c x))
  594. (if (not (zero? c-yy))
  595. (error "compare error: not reflexive" c y))
  596. (if (not (zero? (+ c-xy c-yx)))
  597. (error "compare error: not anti-symmetric" c x y))
  598. (if z?
  599. (let ((c-xz (checked-value c x z))
  600. (c-zx (checked-value c z x))
  601. (c-yz (checked-value c y z))
  602. (c-zy (checked-value c z y)))
  603. (if (not (zero? (+ c-xz c-zx)))
  604. (error "compare error: not anti-symmetric" c x z))
  605. (if (not (zero? (+ c-yz c-zy)))
  606. (error "compare error: not anti-symmetric" c y z))
  607. (let ((ijk (vector-ref q (+ c-xy (* 3 c-yz) (* 9 c-xz) 13))))
  608. (if (list? ijk)
  609. (apply error
  610. "compare error: not transitive"
  611. c
  612. (map (lambda (i) (case i ((x) x) ((y) y) ((z) z)))
  613. ijk)))))
  614. (set! z? #t))
  615. (set! z (if (random-boolean) x y)) ; randomized testing
  616. c-xy))))