Debian Codegraph @

%3a128/128.body1.scm

raw symbols SCIP
1 ;;; Copyright (C) John Cowan (2015). All Rights Reserved.
2 ;;;
3 ;;; Permission is hereby granted, free of charge, to any person
4 ;;; obtaining a copy of this software and associated documentation
5 ;;; files (the "Software"), to deal in the Software without
6 ;;; restriction, including without limitation the rights to use,
7 ;;; copy, modify, merge, publish, distribute, sublicense, and/or
8 ;;; sell copies of the Software, and to permit persons to whom the
9 ;;; Software is furnished to do so, subject to the following
10 ;;; 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
17 ;;; OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 ;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 ;;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 ;;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 ;;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 ;;; OTHER DEALINGS IN THE SOFTWARE.
23
24 ;;;; Main part of the SRFI 114 reference implementation
25
26 ;;; "There are two ways of constructing a software design: One way is to
27 ;;; make it so simple that there are obviously no deficiencies, and the
28 ;;; other way is to make it so complicated that there are no *obvious*
29 ;;; deficiencies." --Tony Hoare
30
31 ;;; Syntax (because syntax must be defined before it is used, contra Dr. Hardcase)
32
33 ;; Arithmetic if
34 (define-syntax comparator-if<=>
35 (syntax-rules ()
36 ((if<=> a b less equal greater)
37 (comparator-if<=> (make-default-comparator) a b less equal greater))
38 ((comparator-if<=> comparator a b less equal greater)
39 (cond
40 ((=? comparator a b) equal)
41 ((<? comparator a b) less)
42 (else greater)))))
43
44 ;; Upper bound of hash functions is 2^25-1
45 (define-syntax hash-bound
46 (syntax-rules ()
47 ((hash-bound) 33554432)))
48
49 (define %salt% (make-parameter 16064047))
50
51 (define-syntax hash-salt
52 (syntax-rules ()
53 ((hash-salt) (%salt%))))
54
55 (define-syntax with-hash-salt
56 (syntax-rules ()
57 ((with-hash-salt new-salt hash-func obj)
58 (parameterize ((%salt% new-salt)) (hash-func obj)))))
59
60 ;;; Definition of comparator records with accessors and basic comparator
61
62 (define-record-type comparator
63 (make-raw-comparator type-test equality ordering hash ordering? hash?)
64 comparator?
65 (type-test comparator-type-test-predicate)
66 (equality comparator-equality-predicate)
67 (ordering comparator-ordering-predicate)
68 (hash comparator-hash-function)
69 (ordering? comparator-ordered?)
70 (hash? comparator-hashable?))
71
72 ;; Public constructor
73 (define (make-comparator type-test equality ordering hash)
74 (make-raw-comparator
75 (if (eq? type-test #t) (lambda (x) #t) type-test)
76 (if (eq? equality #t) (lambda (x y) (eqv? (ordering x y) 0)) equality)
77 (if ordering ordering (lambda (x y) (error #f "ordering not supported")))
78 (if hash hash (lambda (x y) (error #f "hashing not supported")))
79 (if ordering #t #f)
80 (if hash #t #f)))
81
82 ;;; Invokers
83
84 ;; Invoke the test type
85 (define (comparator-test-type comparator obj)
86 ((comparator-type-test-predicate comparator) obj))
87
88 ;; Invoke the test type and throw an error if it fails
89 (define (comparator-check-type comparator obj)
90 (if (comparator-test-type comparator obj)
91 #t
92 (error #f "comparator type check failed" comparator obj)))
93
94 ;; Invoke the hash function
95 (define (comparator-hash comparator obj)
96 ((comparator-hash-function comparator) obj))
97
98 ;;; Comparison predicates
99
100 ;; Binary versions for internal use
101
102 (define (binary=? comparator a b)
103 ((comparator-equality-predicate comparator) a b))
104
105 (define (binary<? comparator a b)
106 ((comparator-ordering-predicate comparator) a b))
107
108 (define (binary>? comparator a b)
109 (binary<? comparator b a))
110
111 (define (binary<=? comparator a b)
112 (not (binary>? comparator a b)))
113
114 (define (binary>=? comparator a b)
115 (not (binary<? comparator a b)))
116
117 ;; General versions for export
118
119 (define (=? comparator a b . objs)
120 (let loop ((a a) (b b) (objs objs))
121 (and (binary=? comparator a b)
122 (if (null? objs) #t (loop b (car objs) (cdr objs))))))
123
124 (define (<? comparator a b . objs)
125 (let loop ((a a) (b b) (objs objs))
126 (and (binary<? comparator a b)
127 (if (null? objs) #t (loop b (car objs) (cdr objs))))))
128
129 (define (>? comparator a b . objs)
130 (let loop ((a a) (b b) (objs objs))
131 (and (binary>? comparator a b)
132 (if (null? objs) #t (loop b (car objs) (cdr objs))))))
133
134 (define (<=? comparator a b . objs)
135 (let loop ((a a) (b b) (objs objs))
136 (and (binary<=? comparator a b)
137 (if (null? objs) #t (loop b (car objs) (cdr objs))))))
138
139 (define (>=? comparator a b . objs)
140 (let loop ((a a) (b b) (objs objs))
141 (and (binary>=? comparator a b)
142 (if (null? objs) #t (loop b (car objs) (cdr objs))))))
143
144
145 ;;; Simple ordering and hash functions
146
147 (define (boolean<? a b)
148 ;; #f < #t but not otherwise
149 (and (not a) b))
150
151
152 (define (boolean-hash obj)
153 (if obj (%salt%) 0))
154
155 (define (char-hash obj)
156 (modulo (* (%salt%) (char->integer obj)) (hash-bound)))
157
158 (define (char-ci-hash obj)
159 (modulo (* (%salt%) (char->integer (char-foldcase obj))) (hash-bound)))
160
161 (define (number-hash obj)
162 (cond
163 ((nan? obj) (%salt%))
164 ((and (infinite? obj) (positive? obj)) (* 2 (%salt%)))
165 ((infinite? obj) (* (%salt%) 3))
166 ((real? obj) (abs (exact (round obj))))
167 (else (+ (number-hash (real-part obj)) (number-hash (imag-part obj))))))
168
169 ;; Lexicographic ordering of complex numbers
170 (define (complex<? a b)
171 (if (= (real-part a) (real-part b))
172 (< (imag-part a) (imag-part b))
173 (< (real-part a) (real-part b))))
174
175 ;; already defined in (rnrs hashtables)
176 #;(define (string-ci-hash obj)
177 (string-hash (string-foldcase obj)))
178
179 (define (symbol<? a b) (string<? (symbol->string a) (symbol->string b)))
180
181 ;; already defined in (rnrs hashtables)
182 #;(define (symbol-hash obj)
183 (string-hash (symbol->string obj)))
184
185 ;;; Wrapped equality predicates
186 ;;; These comparators don't have ordering functions.
187
188 (define (make-eq-comparator)
189 (make-comparator #t eq? #f default-hash))
190
191 (define (make-eqv-comparator)
192 (make-comparator #t eqv? #f default-hash))
193
194 (define (make-equal-comparator)
195 (make-comparator #t equal? #f default-hash))
196
197 ;;; Sequence ordering and hash functions
198 ;; The hash functions are based on djb2, but
199 ;; modulo 2^25 instead of 2^32 in hopes of sticking to fixnums.
200
201 (define (make-hasher)
202 (let ((result (%salt%)))
203 (case-lambda
204 (() result)
205 ((n) (set! result (+ (modulo (* result 33) (hash-bound)) n))
206 result))))
207
208 ;;; Pair comparator
209 (define (make-pair-comparator car-comparator cdr-comparator)
210 (make-comparator
211 (make-pair-type-test car-comparator cdr-comparator)
212 (make-pair=? car-comparator cdr-comparator)
213 (make-pair<? car-comparator cdr-comparator)
214 (make-pair-hash car-comparator cdr-comparator)))
215
216 (define (make-pair-type-test car-comparator cdr-comparator)
217 (lambda (obj)
218 (and (pair? obj)
219 (comparator-test-type car-comparator (car obj))
220 (comparator-test-type cdr-comparator (cdr obj)))))
221
222 (define (make-pair=? car-comparator cdr-comparator)
223 (lambda (a b)
224 (and ((comparator-equality-predicate car-comparator) (car a) (car b))
225 ((comparator-equality-predicate cdr-comparator) (cdr a) (cdr b)))))
226
227 (define (make-pair<? car-comparator cdr-comparator)
228 (lambda (a b)
229 (if (=? car-comparator (car a) (car b))
230 (<? cdr-comparator (cdr a) (cdr b))
231 (<? car-comparator (car a) (car b)))))
232
233 (define (make-pair-hash car-comparator cdr-comparator)
234 (lambda (obj)
235 (let ((acc (make-hasher)))
236 (acc (comparator-hash car-comparator (car obj)))
237 (acc (comparator-hash cdr-comparator (cdr obj)))
238 (acc))))
239
240 ;;; List comparator
241
242 ;; Cheap test for listness
243 (define (norp? obj) (or (null? obj) (pair? obj)))
244
245 (define (make-list-comparator element-comparator type-test empty? head tail)
246 (make-comparator
247 (make-list-type-test element-comparator type-test empty? head tail)
248 (make-list=? element-comparator type-test empty? head tail)
249 (make-list<? element-comparator type-test empty? head tail)
250 (make-list-hash element-comparator type-test empty? head tail)))
251
252
253 (define (make-list-type-test element-comparator type-test empty? head tail)
254 (lambda (obj)
255 (and
256 (type-test obj)
257 (let ((elem-type-test (comparator-type-test-predicate element-comparator)))
258 (let loop ((obj obj))
259 (cond
260 ((empty? obj) #t)
261 ((not (elem-type-test (head obj))) #f)
262 (else (loop (tail obj)))))))))
263
264 (define (make-list=? element-comparator type-test empty? head tail)
265 (lambda (a b)
266 (let ((elem=? (comparator-equality-predicate element-comparator)))
267 (let loop ((a a) (b b))
268 (cond
269 ((and (empty? a) (empty? b) #t))
270 ((empty? a) #f)
271 ((empty? b) #f)
272 ((elem=? (head a) (head b)) (loop (tail a) (tail b)))
273 (else #f))))))
274
275 (define (make-list<? element-comparator type-test empty? head tail)
276 (lambda (a b)
277 (let ((elem=? (comparator-equality-predicate element-comparator))
278 (elem<? (comparator-ordering-predicate element-comparator)))
279 (let loop ((a a) (b b))
280 (cond
281 ((and (empty? a) (empty? b) #f))
282 ((empty? a) #t)
283 ((empty? b) #f)
284 ((elem=? (head a) (head b)) (loop (tail a) (tail b)))
285 ((elem<? (head a) (head b)) #t)
286 (else #f))))))
287
288 (define (make-list-hash element-comparator type-test empty? head tail)
289 (lambda (obj)
290 (let ((elem-hash (comparator-hash-function element-comparator))
291 (acc (make-hasher)))
292 (let loop ((obj obj))
293 (cond
294 ((empty? obj) (acc))
295 (else (acc (elem-hash (head obj))) (loop (tail obj))))))))
296
297
298 ;;; Vector comparator
299
300 (define (make-vector-comparator element-comparator type-test length ref)
301 (make-comparator
302 (make-vector-type-test element-comparator type-test length ref)
303 (make-vector=? element-comparator type-test length ref)
304 (make-vector<? element-comparator type-test length ref)
305 (make-vector-hash element-comparator type-test length ref)))
306
307 (define (make-vector-type-test element-comparator type-test length ref)
308 (lambda (obj)
309 (and
310 (type-test obj)
311 (let ((elem-type-test (comparator-type-test-predicate element-comparator))
312 (len (length obj)))
313 (let loop ((n 0))
314 (cond
315 ((= n len) #t)
316 ((not (elem-type-test (ref obj n))) #f)
317 (else (loop (+ n 1)))))))))
318
319 (define (make-vector=? element-comparator type-test length ref)
320 (lambda (a b)
321 (and
322 (= (length a) (length b))
323 (let ((elem=? (comparator-equality-predicate element-comparator))
324 (len (length b)))
325 (let loop ((n 0))
326 (cond
327 ((= n len) #t)
328 ((elem=? (ref a n) (ref b n)) (loop (+ n 1)))
329 (else #f)))))))
330
331 (define (make-vector<? element-comparator type-test length ref)
332 (lambda (a b)
333 (cond
334 ((< (length a) (length b)) #t)
335 ((> (length a) (length b)) #f)
336 (else
337 (let ((elem=? (comparator-equality-predicate element-comparator))
338 (elem<? (comparator-ordering-predicate element-comparator))
339 (len (length a)))
340 (let loop ((n 0))
341 (cond
342 ((= n len) #f)
343 ((elem=? (ref a n) (ref b n)) (loop (+ n 1)))
344 ((elem<? (ref a n) (ref b n)) #t)
345 (else #f))))))))
346
347 (define (make-vector-hash element-comparator type-test length ref)
348 (lambda (obj)
349 (let ((elem-hash (comparator-hash-function element-comparator))
350 (acc (make-hasher))
351 (len (length obj)))
352 (let loop ((n 0))
353 (cond
354 ((= n len) (acc))
355 (else (acc (elem-hash (ref obj n))) (loop (+ n 1))))))))
356
357 ;; already defined in (rnrs hashtables)
358 #;(define (string-hash obj)
359 (let ((acc (make-hasher))
360 (len (string-length obj)))
361 (let loop ((n 0))
362 (cond
363 ((= n len) (acc))
364 (else (acc (char->integer (string-ref obj n))) (loop (+ n 1)))))))
365