Debian Codegraph @

%3a224/224.scm

raw symbols SCIP
1 ;;; SPDX-FileCopyrightText: 2021 Wolfgang Corcoran-Mathe <[email protected]>
2 ;;;
3 ;;; SPDX-License-Identifier: MIT
4
5 ;;;; Utility
6
7 (define (plist-fold proc nil ps)
8 (let loop ((b nil) (ps ps))
9 (pmatch ps
10 (() b)
11 ((,k ,v . ,ps*)
12 (loop (proc k v b) ps*))
13 (else (error 'plist-fold "invalid plist")))))
14
15 (define (first-arg _k x _y) x)
16 (define (second-arg _k _x y) y)
17
18 (define (constantly x)
19 (lambda (_) x))
20
21 ;;;; Type
22
23 (define-record-type <fxmapping>
24 (raw-fxmapping trie)
25 fxmapping?
26 (trie fxmapping-trie))
27
28 ;;;; Constructors
29
30 (define (fxmapping . args)
31 (raw-fxmapping
32 (plist-fold (lambda (k v trie) (trie-adjoin trie k v))
33 the-empty-trie
34 args)))
35
36 (define (pair-or-null? x)
37 (or (pair? x) (null? x)))
38
39 (define (alist->fxmapping/combinator comb as)
40 (assume (procedure? comb))
41 (assume (pair-or-null? as))
42 (raw-fxmapping
43 (fold (lambda (p trie)
44 (assume (pair? p) "alist->fxmapping/combinator: not a pair")
45 (trie-insert/combine trie (car p) (cdr p) comb))
46 the-empty-trie
47 as)))
48
49 (define (alist->fxmapping as)
50 (alist->fxmapping/combinator second-arg as))
51
52 (define fxmapping-unfold
53 (case-lambda
54 ((stop? mapper successor seed) ; fast path
55 (assume (procedure? stop?))
56 (assume (procedure? mapper))
57 (assume (procedure? successor))
58 (let lp ((trie the-empty-trie) (seed seed))
59 (if (stop? seed)
60 (raw-fxmapping trie)
61 (let-values (((k v) (mapper seed)))
62 (assume (valid-integer? k))
63 (lp (trie-adjoin trie k v) (successor seed))))))
64 ((stop? mapper successor . seeds) ; variadic path
65 (assume (procedure? stop?))
66 (assume (procedure? mapper))
67 (assume (procedure? successor))
68 (assume (pair? seeds))
69 (let lp ((trie the-empty-trie) (seeds seeds))
70 (if (apply stop? seeds)
71 (raw-fxmapping trie)
72 (let-values (((k v) (apply mapper seeds))
73 (seeds* (apply successor seeds)))
74 (assume (valid-integer? k))
75 (lp (trie-adjoin trie k v) seeds*)))))))
76
77 (define fxmapping-accumulate
78 (case-lambda
79 ((proc seed) ; fast path
80 (assume (procedure? proc))
81 (call-with-current-continuation
82 (lambda (k)
83 (let lp ((trie the-empty-trie) (seed seed))
84 (let-values (((k v seed*)
85 (proc (lambda xs (apply k (raw-fxmapping trie) xs))
86 seed)))
87 (lp (trie-adjoin trie k v) seed*))))))
88 ((proc . seeds) ; variadic path
89 (assume (procedure? proc))
90 (assume (pair? seeds))
91 (call-with-current-continuation
92 (lambda (k)
93 (let lp ((trie the-empty-trie) (seeds seeds))
94 (let-values (((k v . seeds*)
95 (apply proc
96 (lambda xs (apply k (raw-fxmapping trie) xs))
97 seeds)))
98 (lp (trie-adjoin trie k v) seeds*))))))))
99
100 ;;;; Predicates
101
102 (define (fxmapping-contains? fxmap n)
103 (assume (fxmapping? fxmap))
104 (assume (valid-integer? n))
105 (trie-contains? (fxmapping-trie fxmap) n))
106
107 (define (fxmapping-empty? fxmap)
108 (assume (fxmapping? fxmap))
109 (eqv? (fxmapping-trie fxmap) the-empty-trie))
110
111 (define (fxmapping-disjoint? fxmap1 fxmap2)
112 (assume (fxmapping? fxmap1))
113 (assume (fxmapping? fxmap2))
114 (trie-disjoint? (fxmapping-trie fxmap1) (fxmapping-trie fxmap2)))
115
116 ;;;; Accessors
117
118 (define fxmapping-ref
119 (case-lambda
120 ((fxmap key)
121 (fxmapping-ref fxmap
122 key
123 (lambda () (error 'fxmapping-ref
124 "key not found"
125 key
126 fxmap))
127 values))
128 ((fxmap key failure)
129 (fxmapping-ref fxmap key failure values))
130 ((fxmap key failure success)
131 (assume (fxmapping? fxmap))
132 (assume (valid-integer? key))
133 (assume (procedure? failure))
134 (assume (procedure? success))
135 (trie-assoc (fxmapping-trie fxmap) key failure success))))
136
137 (define (fxmapping-ref/default fxmap key default)
138 (assume (fxmapping? fxmap))
139 (assume (valid-integer? key))
140 (trie-assoc/default (fxmapping-trie fxmap) key default))
141
142 (define (fxmapping-min fxmap)
143 (assume (not (fxmapping-empty? fxmap)))
144 (trie-min (fxmapping-trie fxmap)))
145
146 (define (fxmapping-max fxmap)
147 (assume (not (fxmapping-empty? fxmap)))
148 (trie-max (fxmapping-trie fxmap)))
149
150 ;;;; Updaters
151
152 (define fxmapping-adjoin/combinator
153 (case-lambda
154 ((fxmap combine key value) ; one-assoc fast path
155 (raw-fxmapping
156 (trie-insert/combine (fxmapping-trie fxmap) key value combine)))
157 ((fxmap combine . ps)
158 (raw-fxmapping
159 (plist-fold (lambda (k v t)
160 (trie-insert/combine t k v combine))
161 (fxmapping-trie fxmap)
162 ps)))))
163
164 ;; Preserve existing associations for keys.
165 (define fxmapping-adjoin
166 (case-lambda
167 ((fxmap key value) ; one-assoc fast path
168 (raw-fxmapping
169 (trie-adjoin (fxmapping-trie fxmap) key value)))
170 ((fxmap . ps)
171 (raw-fxmapping
172 (plist-fold (lambda (k v t) (trie-adjoin t k v))
173 (fxmapping-trie fxmap)
174 ps)))))
175
176 ;; Replace existing associations for keys.
177 (define fxmapping-set
178 (case-lambda
179 ((fxmap key value) ; one-assoc fast path
180 (raw-fxmapping
181 (trie-insert (fxmapping-trie fxmap) key value)))
182 ((fxmap . ps)
183 (raw-fxmapping
184 (plist-fold (lambda (k v t) (trie-insert t k v))
185 (fxmapping-trie fxmap)
186 ps)))))
187
188 (define (fxmapping-adjust fxmap key proc)
189 (assume (fxmapping? fxmap))
190 (assume (valid-integer? key))
191 (assume (procedure? proc))
192 (raw-fxmapping (trie-adjust (fxmapping-trie fxmap) key proc)))
193
194 (define fxmapping-delete
195 (case-lambda
196 ((fxmap key) ; fast path
197 (assume (fxmapping? fxmap))
198 (assume (valid-integer? key))
199 (raw-fxmapping (trie-delete (fxmapping-trie fxmap) key)))
200 ((fxmap . keys)
201 (fxmapping-delete-all fxmap keys))))
202
203 (define (fxmapping-delete-all fxmap keys)
204 (assume (or (pair? keys) (null? keys)))
205 (let ((key-map (fxmapping-trie
206 (fxmapping-unfold null?
207 (lambda (ks) (values (car ks) #t))
208 cdr
209 keys))))
210 (fxmapping-remove (lambda (k _) (trie-contains? key-map k))
211 fxmap)))
212
213 (define fxmapping-update
214 (case-lambda
215 ((fxmap key success)
216 (fxmapping-update fxmap
217 key
218 success
219 (lambda ()
220 (error 'fxmapping-update "key not found" key fxmap))))
221 ((fxmap key success failure)
222 (assume (fxmapping? fxmap))
223 (assume (valid-integer? key))
224 (assume (procedure? success))
225 (assume (procedure? failure))
226 (trie-update (fxmapping-trie fxmap) key success failure raw-fxmapping))))
227
228 (define (fxmapping-alter fxmap key failure success)
229 (assume (fxmapping? fxmap))
230 (assume (valid-integer? key))
231 (assume (procedure? failure))
232 (assume (procedure? success))
233 (trie-alter (fxmapping-trie fxmap) key failure success raw-fxmapping))
234
235 (define (fxmapping-delete-min fxmap)
236 (fxmapping-update-min fxmap
237 (lambda (_k _v _rep delete)
238 (delete))))
239
240 (define (fxmapping-update-min fxmap success)
241 (assume (fxmapping? fxmap))
242 (assume (not (fxmapping-empty? fxmap)))
243 (assume (procedure? success))
244 (trie-update-min (fxmapping-trie fxmap) success raw-fxmapping))
245
246 (define (fxmapping-pop-min fxmap)
247 (assume (fxmapping? fxmap))
248 (assume (not (fxmapping-empty? fxmap)))
249 (let-values (((k v trie) (trie-pop-min (fxmapping-trie fxmap))))
250 (values k v (raw-fxmapping trie))))
251
252 (define (fxmapping-delete-max fxmap)
253 (fxmapping-update-max fxmap
254 (lambda (_k _v _rep delete)
255 (delete))))
256
257 (define (fxmapping-update-max fxmap success)
258 (assume (fxmapping? fxmap))
259 (assume (not (fxmapping-empty? fxmap)))
260 (assume (procedure? success))
261 (trie-update-max (fxmapping-trie fxmap) success raw-fxmapping))
262
263 (define (fxmapping-pop-max fxmap)
264 (assume (fxmapping? fxmap))
265 (assume (not (fxmapping-empty? fxmap)))
266 (let-values (((k v trie) (trie-pop-max (fxmapping-trie fxmap))))
267 (values k v (raw-fxmapping trie))))
268
269 ;;;; The whole fxmapping
270
271 (define (fxmapping-size fxmap)
272 (assume (fxmapping? fxmap))
273 (trie-size (fxmapping-trie fxmap)))
274
275 (define fxmapping-find
276 (case-lambda
277 ((pred fxmap failure)
278 (fxmapping-find pred fxmap failure values))
279 ((pred fxmap failure success)
280 (assume (procedure? pred))
281 (assume (fxmapping? fxmap))
282 (assume (procedure? failure))
283 (assume (procedure? success))
284 (trie-find pred (fxmapping-trie fxmap) failure success))))
285
286 (define (fxmapping-count pred fxmap)
287 (assume (procedure? pred))
288 (fxmapping-fold (lambda (k v acc)
289 (if (pred k v) (+ 1 acc) acc))
290 0
291 fxmap))
292
293 (define (fxmapping-any? pred fxmap)
294 (assume (procedure? pred))
295 (call-with-current-continuation
296 (lambda (return)
297 (fxmapping-fold (lambda (k v _)
298 (and (pred k v) (return #t)))
299 #f
300 fxmap))))
301
302 (define (fxmapping-every? pred fxmap)
303 (assume (procedure? pred))
304 (call-with-current-continuation
305 (lambda (return)
306 (fxmapping-fold (lambda (k v _)
307 (or (pred k v) (return #f)))
308 #t
309 fxmap))))
310
311 ;;;; Mapping and folding
312
313 ;; Map proc over the assocs. of fxmap, inserting result values under
314 ;; the same key.
315 ;; This is *not* the same as SRFI 146's mapping-map.
316 (define (fxmapping-map proc fxmap)
317 (assume (procedure? proc))
318 (assume (fxmapping? fxmap))
319 (raw-fxmapping (trie-map proc (fxmapping-trie fxmap))))
320
321 (define (unspecified)
322 (if #f #f))
323
324 (define (fxmapping-for-each proc fxmap)
325 (assume (procedure? proc))
326 (fxmapping-fold (lambda (k v _)
327 (proc k v)
328 (unspecified))
329 (unspecified)
330 fxmap))
331
332 (define (fxmapping-fold proc nil fxmap)
333 (assume (procedure? proc))
334 (assume (fxmapping? fxmap))
335 (let ((trie (fxmapping-trie fxmap)))
336 (tmatch trie
337 ((branch ? ,m ,l ,r) (guard (negative? m))
338 (trie-fold-left proc (trie-fold-left proc nil r) l))
339 ((branch ? ? ,l ,r)
340 (trie-fold-left proc (trie-fold-left proc nil l) r))
341 (else (trie-fold-left proc nil trie)))))
342
343 (define (fxmapping-fold-right proc nil fxmap)
344 (assume (procedure? proc))
345 (assume (fxmapping? fxmap))
346 (let ((trie (fxmapping-trie fxmap)))
347 (tmatch trie
348 ((branch ? ,m ,l ,r) (guard (negative? m))
349 (trie-fold-right proc (trie-fold-right proc nil l) r))
350 ((branch ? ? ,l ,r)
351 (trie-fold-right proc (trie-fold-right proc nil r) l))
352 (else (trie-fold-right proc nil trie)))))
353
354 (define (fxmapping-map->list proc fxmap)
355 (assume (procedure? proc))
356 (fxmapping-fold-right (lambda (k v us)
357 (cons (proc k v) us))
358 '()
359 fxmap))
360
361 (define (fxmapping-filter pred fxmap)
362 (assume (procedure? pred))
363 (assume (fxmapping? fxmap))
364 (raw-fxmapping (trie-filter pred (fxmapping-trie fxmap))))
365
366 (define (fxmapping-remove pred fxmap)
367 (fxmapping-filter (lambda (k v) (not (pred k v))) fxmap))
368
369 (define (fxmapping-partition pred fxmap)
370 (assume (procedure? pred))
371 (assume (fxmapping? fxmap))
372 (let-values (((tin tout)
373 (trie-partition pred (fxmapping-trie fxmap))))
374 (values (raw-fxmapping tin) (raw-fxmapping tout))))
375
376 ;;;; Conversion
377
378 (define (fxmapping->alist fxmap)
379 (fxmapping-fold-right (lambda (k v as) (cons (cons k v) as))
380 '()
381 fxmap))
382
383 (define (fxmapping->decreasing-alist fxmap)
384 (fxmapping-fold (lambda (k v as) (cons (cons k v) as))
385 '()
386 fxmap))
387
388 (define (fxmapping-keys fxmap)
389 (fxmapping-fold-right (lambda (k _ ks) (cons k ks)) '() fxmap))
390
391 (define (fxmapping-values fxmap)
392 (fxmapping-fold-right (lambda (_ v vs) (cons v vs)) '() fxmap))
393
394 (define (fxmapping->generator fxmap)
395 (assume (fxmapping? fxmap))
396 (make-coroutine-generator
397 (lambda (yield)
398 (fxmapping-fold (lambda (k v _) (yield (cons k v)))
399 #f
400 fxmap))))
401
402 (define (fxmapping->decreasing-generator fxmap)
403 (assume (fxmapping? fxmap))
404 (make-coroutine-generator
405 (lambda (yield)
406 (fxmapping-fold-right (lambda (k v _) (yield (cons k v)))
407 #f
408 fxmap))))
409
410 ;;;; Comparison
411
412 (define (fxmapping=? comp fxmap1 fxmap2 . imaps)
413 (assume (comparator? comp))
414 (assume (fxmapping? fxmap1))
415 (let ((fxmap-eq1 (lambda (fxmap)
416 (assume (fxmapping? fxmap))
417 (or (eqv? fxmap1 fxmap)
418 (trie=? comp
419 (fxmapping-trie fxmap1)
420 (fxmapping-trie fxmap))))))
421 (and (fxmap-eq1 fxmap2)
422 (or (null? imaps)
423 (every fxmap-eq1 imaps)))))
424
425 (define (fxmapping<? comp fxmap1 fxmap2 . imaps)
426 (assume (comparator? comp))
427 (assume (fxmapping? fxmap1))
428 (assume (fxmapping? fxmap2))
429 (let lp ((t1 (fxmapping-trie fxmap1))
430 (t2 (fxmapping-trie fxmap2))
431 (imaps imaps))
432 (and (trie-proper-subset? comp t1 t2)
433 (pmatch imaps
434 (() #t)
435 ((,m . ,imaps*) (lp t2 (fxmapping-trie m) imaps*))))))
436
437 (define (fxmapping>? comp fxmap1 fxmap2 . imaps)
438 (assume (comparator? comp))
439 (assume (fxmapping? fxmap1))
440 (assume (fxmapping? fxmap2))
441 (let lp ((t1 (fxmapping-trie fxmap1))
442 (t2 (fxmapping-trie fxmap2))
443 (imaps imaps))
444 (and (trie-proper-subset? comp t2 t1)
445 (pmatch imaps
446 (() #t)
447 ((,m . ,imaps*) (lp t2 (fxmapping-trie m) imaps*))))))
448
449 (define (fxmapping<=? comp fxmap1 fxmap2 . imaps)
450 (assume (comparator? comp))
451 (assume (fxmapping? fxmap1))
452 (assume (fxmapping? fxmap2))
453 (let lp ((t1 (fxmapping-trie fxmap1))
454 (t2 (fxmapping-trie fxmap2))
455 (imaps imaps))
456 (and (memv (trie-subset-compare comp t1 t2) '(less equal))
457 (pmatch imaps
458 (() #t)
459 ((,m . ,imaps*) (lp t2 (fxmapping-trie m) imaps*))))))
460
461 (define (fxmapping>=? comp fxmap1 fxmap2 . imaps)
462 (assume (comparator? comp))
463 (assume (fxmapping? fxmap1))
464 (assume (fxmapping? fxmap2))
465 (let lp ((t1 (fxmapping-trie fxmap1))
466 (t2 (fxmapping-trie fxmap2))
467 (imaps imaps))
468 (and (memv (trie-subset-compare comp t2 t1) '(less equal))
469 (pmatch imaps
470 (() #t)
471 ((,m . ,imaps*) (lp t2 (fxmapping-trie m) imaps*))))))
472
473 ;;;; Set theory operations
474
475 (define (fxmapping-union . args)
476 (apply fxmapping-union/combinator first-arg args))
477
478 (define (fxmapping-intersection . args)
479 (apply fxmapping-intersection/combinator first-arg args))
480
481 (define fxmapping-difference
482 (case-lambda
483 ((fxmap1 fxmap2)
484 (assume (fxmapping? fxmap1))
485 (assume (fxmapping? fxmap2))
486 (raw-fxmapping
487 (trie-difference (fxmapping-trie fxmap1)
488 (fxmapping-trie fxmap2))))
489 ((fxmap . rest)
490 (assume (fxmapping? fxmap))
491 (assume (pair? rest))
492 (raw-fxmapping
493 (trie-difference (fxmapping-trie fxmap)
494 (fxmapping-trie
495 (apply fxmapping-union rest)))))))
496
497 (define (fxmapping-xor fxmap1 fxmap2)
498 (assume (fxmapping? fxmap1))
499 (assume (fxmapping? fxmap2))
500 (raw-fxmapping
501 (trie-xor (fxmapping-trie fxmap1) (fxmapping-trie fxmap2))))
502
503 (define (fxmapping-union/combinator proc fxmap . rest)
504 (assume (procedure? proc))
505 (assume (fxmapping? fxmap))
506 (assume (pair? rest))
507 (raw-fxmapping
508 (fold (lambda (im t)
509 (assume (fxmapping? im))
510 (trie-merge proc t (fxmapping-trie im)))
511 (fxmapping-trie fxmap)
512 rest)))
513
514 (define (fxmapping-intersection/combinator proc fxmap . rest)
515 (assume (procedure? proc))
516 (assume (fxmapping? fxmap))
517 (assume (pair? rest))
518 (raw-fxmapping
519 (fold (lambda (im t)
520 (assume (fxmapping? im))
521 (trie-intersection proc t (fxmapping-trie im)))
522 (fxmapping-trie fxmap)
523 rest)))
524
525 ;;;; Subsets
526
527 (define (fxsubmapping= fxmap key)
528 (fxmapping-ref fxmap
529 key
530 fxmapping
531 (lambda (v) (fxmapping key v))))
532
533 (define (fxmapping-open-interval fxmap low high)
534 (assume (fxmapping? fxmap))
535 (assume (valid-integer? low))
536 (assume (valid-integer? high))
537 (assume (fx>=? high low))
538 (raw-fxmapping
539 (subtrie-interval (fxmapping-trie fxmap) low high #f #f)))
540
541 (define (fxmapping-closed-interval fxmap low high)
542 (assume (fxmapping? fxmap))
543 (assume (valid-integer? low))
544 (assume (valid-integer? high))
545 (assume (fx>=? high low))
546 (raw-fxmapping
547 (subtrie-interval (fxmapping-trie fxmap) low high #t #t)))
548
549 (define (fxmapping-open-closed-interval fxmap low high)
550 (assume (fxmapping? fxmap))
551 (assume (valid-integer? low))
552 (assume (valid-integer? high))
553 (assume (fx>=? high low))
554 (raw-fxmapping
555 (subtrie-interval (fxmapping-trie fxmap) low high #f #t)))
556
557 (define (fxmapping-closed-open-interval fxmap low high)
558 (assume (fxmapping? fxmap))
559 (assume (valid-integer? low))
560 (assume (valid-integer? high))
561 (assume (fx>=? high low))
562 (raw-fxmapping
563 (subtrie-interval (fxmapping-trie fxmap) low high #t #f)))
564
565 (define (fxsubmapping< fxmap key)
566 (assume (fxmapping? fxmap))
567 (assume (valid-integer? key))
568 (raw-fxmapping (subtrie< (fxmapping-trie fxmap) key #f)))
569
570 (define (fxsubmapping<= fxmap key)
571 (assume (fxmapping? fxmap))
572 (assume (valid-integer? key))
573 (raw-fxmapping (subtrie< (fxmapping-trie fxmap) key #t)))
574
575 (define (fxsubmapping> fxmap key)
576 (assume (fxmapping? fxmap))
577 (assume (valid-integer? key))
578 (raw-fxmapping (subtrie> (fxmapping-trie fxmap) key #f)))
579
580 (define (fxsubmapping>= fxmap key)
581 (assume (fxmapping? fxmap))
582 (assume (valid-integer? key))
583 (raw-fxmapping (subtrie> (fxmapping-trie fxmap) key #t)))
584
585 (define (fxmapping-split fxmap k)
586 (assume (fxmapping? fxmap))
587 (assume (integer? k))
588 (let-values (((trie-low trie-high)
589 (trie-split (fxmapping-trie fxmap) k)))
590 (values (raw-fxmapping trie-low) (raw-fxmapping trie-high))))
591
592 ;;;; fxmappings as relations
593
594 (define (fxmapping-relation-map proc fxmap)
595 (assume (procedure? proc))
596 (assume (fxmapping? fxmap))
597 (raw-fxmapping (trie-relation-map proc (fxmapping-trie fxmap))))