Debian Codegraph @

lib/match.ss

raw
1 ;;; match.ss
2
3 ;;; This program was originally designed and implemented by Dan
4 ;;; Friedman. It was redesigned and implemented by Erik Hilsdale;
5 ;;; some improvements were suggested by Steve Ganz. Additional
6 ;;; modifications were made by Kent Dybvig.
7
8 ;; (Jan 2008)
9 ;; Aziz added match/lexical-context trace-match/lexical-context
10
11 ;; (Nov 2007)
12 ;; Aziz Ghuloum added it to ikarus.
13
14 ;; (Apr 2007)
15 ;; Aziz Ghuloum ported it to r6rs.
16
17 ;; (13 March 2002)
18 ;; rkd added following change by Friedman and Ganz to the main source
19 ;; code thread and fixed a couple of minor problems.
20
21 ;; (9 March 2002)
22 ;; Dan Friedman and Steve Ganz added the ability to use identical pattern
23 ;; variables. The patterns represented by the variables are compared
24 ;; using the value of the parameter match-equality-test, which defaults
25 ;; to equal?.
26 ;;
27 ;; > (match '(1 2 1 2 1)
28 ;; ((,a ,b ,a ,b ,a) (guard (number? a) (number? b)) (+ a b)))
29 ;; 3
30 ;; ;;
31 ;; > (match '((1 2 3) 5 (1 2 3))
32 ;; (((,a ...) ,b (,a ...)) `(,a ... ,b)))
33 ;; (1 2 3 5)
34 ;; ;;
35 ;; > (parameterize ((match-equality-test (lambda (x y) (equal? x (reverse y)))))
36 ;; (match '((1 2 3) (3 2 1))
37 ;; ((,a ,a) 'yes)
38 ;; (,oops 'no)))
39 ;; yes
40
41 ;; (10 Jan 2002)
42 ;; eh fixed bug that caused (match '((1 2 3 4)) (((,a ... ,d) . ,x) a)) to
43 ;; blow up. The bug was caused by a bug in the sexp-dispatch procedure
44 ;; where a base value empty list was passed to an accumulator from inside
45 ;; the recursion, instead of passing the old value of the accumulator.
46
47 ;; (14 Jan 2001)
48 ;; rkd added syntax checks to unquote pattern parsing to weed out invalid
49 ;; patterns like ,#(a) and ,((vector-ref d 1)).
50
51 ;; (14 Jan 2001)
52 ;; rkd added ,(Cata -> Id* ...) to allow specification of recursion
53 ;; function. ,(Id* ...) recurs to match; ,(Cata -> Id* ...) recurs
54 ;; to Cata.
55
56 ;; (14 Jan 2001)
57 ;; rkd tightened up checks for ellipses and nested quasiquote; was comparing
58 ;; symbolic names, which, as had been noted in the source, is a possible
59 ;; hygiene bug. Replaced error call in guard-body with syntax-error to
60 ;; allow error to include source line/character information.
61
62 ;; (13 Jan 2001)
63 ;; rkd fixed match patterns of the form (stuff* ,(x) ... stuff+), which
64 ;; had been recurring on subforms of each item rather than on the items
65 ;; themselves.
66
67 ;; Previous changelog listings at end of file.
68
69 ;;; ============================================================
70
71 ;; Exp ::= (match Exp Clause)
72 ;; || (trace-match Exp Clause)
73 ;; || (match+ (Id*) Exp Clause*)
74 ;; || (trace-match+ (Id*) Exp Clause*)
75 ;; || OtherSchemeExp
76
77 ;; Clause ::= (Pat Exp+) || (Pat (guard Exp*) Exp+)
78
79 ;; Pat ::= (Pat ... . Pat)
80 ;; || (Pat . Pat)
81 ;; || ()
82 ;; || #(Pat* Pat ... Pat*)
83 ;; || #(Pat*)
84 ;; || ,Id
85 ;; || ,(Id*)
86 ;; || ,(Cata -> Id*)
87 ;; || Id
88
89 ;; Cata ::= Exp
90
91 ;; YOU'RE NOT ALLOWED TO REFER TO CATA VARS IN GUARDS. (reasonable!)
92
93
94
95
96 (library (match)
97 (export match trace-match
98 match+ trace-match+
99 match/lexical-context trace-match/lexical-context
100 match-equality-test
101 guard ... quasiquote unquote unquote-splicing)
102 (import (ikarus))
103
104 (define-syntax rec
105 (syntax-rules ()
106 [(_ name val)
107 (letrec ([name val]) name)]))
108
109 (define match-equality-test
110 (make-parameter
111 equal?
112 (lambda (x)
113 (unless (procedure? x)
114 (error 'match-equality-test "not a procedure" x))
115 x)))
116
117 (define-syntax match+
118 (lambda (x)
119 (syntax-case x ()
120 ((ctxt (ThreadedId ...) Exp Clause ...)
121 #'(let f ((ThreadedId ThreadedId) ... (x Exp))
122 (match-help ctxt f x (ThreadedId ...) Clause ...))))))
123
124
125 (define-syntax match/lexical-context
126 (lambda (x)
127 (syntax-case x ()
128 ((_ ctxt Exp Clause ...)
129 #'(let f ((x Exp))
130 (match-help ctxt f x () Clause ...))))))
131
132 (define-syntax match
133 (lambda (x)
134 (syntax-case x ()
135 ((ctxt Exp Clause ...)
136 #'(match/lexical-context ctxt Exp Clause ...)))))
137
138 (define-syntax trace-match+
139 (lambda (x)
140 (syntax-case x ()
141 ((ctxt (ThreadedId ...) Name Exp Clause ...)
142 #'(letrec ((f (trace-lambda Name (ThreadedId ... x)
143 (match-help ctxt f x (ThreadedId ...) Clause ...))))
144 (f ThreadedId ... Exp))))))
145
146 (define-syntax trace-match/lexical-context
147 (lambda (x)
148 (syntax-case x ()
149 ((_ ctxt Name Exp Clause ...)
150 #'(letrec ((f (trace-lambda Name (x)
151 (match-help ctxt f x () Clause ...))))
152 (f Exp))))))
153
154 (define-syntax trace-match
155 (lambda (x)
156 (syntax-case x ()
157 ((ctxt Name Exp Clause ...)
158 #'(trace-match/lexical-context ctxt Name Exp Clause ...)))))
159
160 ;;; ------------------------------
161
162 (define-syntax let-values**
163 (syntax-rules ()
164 ((_ () B0 B ...) (begin B0 B ...))
165 ((_ ((Formals Exp) Rest ...) B0 B ...)
166 (let-values** (Rest ...)
167 (call-with-values (lambda () Exp)
168 (lambda Formals B0 B ...))))))
169
170 (define-syntax match-help
171 (lambda (x)
172 (syntax-case x ()
173 ((_ Template Cata Obj ThreadedIds)
174 #'(error 'match "Unmatched datum" Obj))
175 ((_ Template Cata Obj ThreadedIds (Pat B0 B ...) Rest ...)
176 #'(convert-pat Pat
177 (match-help1 Template Cata Obj ThreadedIds
178 (B0 B ...)
179 Rest ...))))))
180
181 (define-syntax match-help1
182 (lambda (x)
183 (syntax-case x (guard)
184 ((_ PatLit Vars () Cdecls Template Cata Obj ThreadedIds
185 ((guard) B0 B ...) Rest ...)
186 #'(let ((ls/false (sexp-dispatch Obj PatLit)))
187 (if ls/false
188 (apply (lambda Vars
189 (clause-body Cata Cdecls ThreadedIds
190 (extend-backquote Template B0 B ...)))
191 ls/false)
192 (match-help Template Cata Obj ThreadedIds Rest ...))))
193 ((_ PatLit Vars (PG ...) Cdecls Template Cata Obj ThreadedIds
194 ((guard G ...) B0 B ...) Rest ...)
195 #'(let ((ls/false (sexp-dispatch Obj PatLit)))
196 (if (and ls/false (apply (lambda Vars
197 (guard-body Cdecls
198 (extend-backquote Template
199 (and PG ... G ...))))
200 ls/false))
201 (apply (lambda Vars
202 (clause-body Cata Cdecls ThreadedIds
203 (extend-backquote Template B0 B ...)))
204 ls/false)
205 (match-help Template Cata Obj ThreadedIds Rest ...))))
206 ((_ PatLit Vars (PG ...) Cdecls Template Cata Obj ThreadedIds
207 (B0 B ...) Rest ...)
208 #'(match-help1 PatLit Vars (PG ...) Cdecls Template Cata Obj ThreadedIds
209 ((guard) B0 B ...) Rest ...)))))
210
211 (define-syntax clause-body
212 (lambda (x)
213 (define build-mapper
214 (lambda (vars depth cata tIds)
215 (if (zero? depth)
216 cata
217 (with-syntax ((rest (build-mapper vars (- depth 1) cata tIds))
218 (vars vars)
219 (tIds tIds))
220 #'(mapper rest vars tIds)))))
221 (syntax-case x ()
222 ((_ Cata ((CVar CDepth CMyCata CFormal ...) ...) (ThreadedId ...) B)
223 (with-syntax (((Mapper ...)
224 (map (lambda (mycata formals depth)
225 (build-mapper formals
226 (syntax->datum depth)
227 (syntax-case mycata ()
228 (#f #'Cata)
229 (exp #'exp))
230 #'(ThreadedId ...)))
231 #'(CMyCata ...)
232 #'((CFormal ...) ...)
233 #'(CDepth ...))))
234 #'(let-values** (((ThreadedId ... CFormal ...)
235 (Mapper ThreadedId ... CVar))
236 ...)
237 B))))))
238
239 (define-syntax guard-body
240 (lambda (x)
241 (syntax-case x ()
242 ((_ ((Cvar Cdepth MyCata Cformal ...) ...) B)
243 (with-syntax (((CF ...) (apply append #'((Cformal ...) ...))))
244 #'(let-syntax
245 ((CF
246 (lambda (x)
247 (syntax-case x ()
248 (Name
249 (syntax-error #'Name
250 "guard cannot refer to return-value variable")))))
251 ...)
252 B))))))
253
254 (define-syntax convert-pat
255 ;; returns sexp-pat x vars x guards x cdecls
256 (let ()
257 (define ellipsis?
258 (lambda (x)
259 (and (identifier? x) (free-identifier=? x #'(... ...)))))
260 (define Var?
261 (lambda (x)
262 (syntax-case x (->)
263 (-> #f)
264 (id (identifier? #'id)))))
265 (define fVar
266 (lambda (var vars guards)
267 (let loop ((ls vars))
268 (if (null? ls)
269 (values (cons var vars) guards)
270 (if (bound-identifier=? var (car ls))
271 (with-syntax (((tmp) (generate-temporaries (list var)))
272 (var (car ls)))
273 (values (cons #'tmp vars)
274 (cons #'((match-equality-test) tmp var) guards)))
275 (loop (cdr ls)))))))
276 (define (f syn vars guards cdecls depth)
277 (define (andmap f ls)
278 (cond
279 ((null? ls) #t)
280 ((null? (cdr ls)) (f (car ls)))
281 (else (and (f (car ls)) (andmap f (cdr ls))))))
282 (syntax-case syn (unquote)
283 ((unquote . stuff) ; separate for better error detection
284 (syntax-case syn (unquote ->)
285 ((unquote (MyCata -> Var ...))
286 (andmap Var? #'(Var ...))
287 (with-syntax (((Temp) (generate-temporaries '(x)))
288 (Depth depth))
289 (values #'any
290 (cons #'Temp vars)
291 guards
292 (cons #'(Temp Depth MyCata Var ...) cdecls))))
293 ((unquote (Var ...))
294 (andmap Var? #'(Var ...))
295 (with-syntax (((Temp) (generate-temporaries '(x)))
296 (Depth depth))
297 (values #'any
298 (cons #'Temp vars)
299 guards
300 (cons #'(Temp Depth #f Var ...) cdecls))))
301 ((unquote Var)
302 (Var? #'Var)
303 (let-values* (((vars guards) (fVar #'Var vars guards)))
304 (values #'any vars guards cdecls)))))
305 (((unquote . stuff) Dots)
306 (ellipsis? #'Dots)
307 (syntax-case syn (unquote ->)
308 (((unquote (MyCata -> Var ...)) Dots)
309 (andmap Var? #'(Var ...))
310 (with-syntax (((Temp) (generate-temporaries '(x)))
311 (Depth+1 (add1 depth)))
312 (values #'each-any
313 (cons #'Temp vars)
314 guards
315 (cons #'(Temp Depth+1 MyCata Var ...) cdecls))))
316 (((unquote (Var ...)) Dots)
317 (andmap Var? #'(Var ...))
318 (with-syntax (((Temp) (generate-temporaries '(x)))
319 (Depth+1 (add1 depth)))
320 (values #'each-any
321 (cons #'Temp vars)
322 guards
323 (cons #'(Temp Depth+1 #f Var ...) cdecls))))
324 (((unquote Var) Dots)
325 (Var? #'Var)
326 (let-values* (((vars guards) (fVar #'Var vars guards)))
327 (values #'each-any vars guards cdecls)))
328 ((expr Dots) (syntax-error #'expr "match-pattern unquote syntax"))))
329 ((Pat Dots)
330 (ellipsis? #'Dots)
331 (let-values* (((Dpat Dvars Dguards Dcdecls)
332 (f #'Pat vars guards cdecls (add1 depth))))
333 (with-syntax ((Size (- (length Dvars) (length vars)))
334 (Dpat Dpat))
335 (values #'#(each Dpat Size) Dvars Dguards Dcdecls))))
336 ((Pat Dots . Rest)
337 (ellipsis? #'Dots)
338 (let-values* (((Rpat Rvars Rguards Rcdecls)
339 (f #'Rest vars guards cdecls depth))
340 ((Dpat Dvars Dguards Dcdecls)
341 (f #'(Pat (... ...)) Rvars Rguards Rcdecls depth)))
342 (with-syntax ((Size (- (length Dvars) (length Rvars)))
343 ((RevRestTl . RevRest) (reverseX Rpat '()))
344 (Dpat Dpat))
345 (values #'#(tail-each Dpat Size RevRest RevRestTl)
346 Dvars Dguards Dcdecls))))
347 ((X . Y)
348 (let-values* (((Ypat Yvars Yguards Ycdecls)
349 (f #'Y vars guards cdecls depth))
350 ((Xpat Xvars Xguards Xcdecls)
351 (f #'X Yvars Yguards Ycdecls depth)))
352 (with-syntax ((Xpat Xpat) (Ypat Ypat))
353 (values #'(Xpat . Ypat) Xvars Xguards Xcdecls))))
354 (() (values #'() vars guards cdecls))
355 (#(X ...)
356 (let-values* (((Pat Vars Eqvars Cdecls)
357 (f #'(X ...) vars guards cdecls depth)))
358 (with-syntax ((Pat Pat))
359 (values #'#(vector Pat) Vars Eqvars Cdecls))))
360 (Thing (values #'#(atom Thing) vars guards cdecls))))
361 (define reverseX
362 (lambda (ls acc)
363 (if (pair? ls)
364 (reverseX (cdr ls) (cons (car ls) acc))
365 (cons ls acc))))
366 (define-syntax let-values*
367 (syntax-rules ()
368 ((_ () B0 B ...) (begin B0 B ...))
369 ((_ (((Formal ...) Exp) Decl ...) B0 B ...)
370 (call-with-values (lambda () Exp)
371 (lambda (Formal ...)
372 (let-values* (Decl ...) B0 B ...))))))
373 (define-syntax let-synvalues*
374 (syntax-rules ()
375 ((_ () B0 B ...) (begin B0 B ...))
376 ((_ (((Formal ...) Exp) Decl ...) B0 B ...)
377 (call-with-values (lambda () Exp)
378 (lambda (Formal ...)
379 (with-syntax ((Formal Formal) ...)
380 (let-synvalues* (Decl ...) B0 B ...)))))))
381 (lambda (syn)
382 (syntax-case syn ()
383 ((_ syn (kh . kt))
384 (let-synvalues* (((Pat Vars Guards Cdecls) (f #'syn '() '() '() 0)))
385 #'(kh 'Pat Vars Guards Cdecls . kt)))))))
386
387 (define-syntax mapper
388 (lambda (x)
389 (syntax-case x ()
390 ((_ F (RetId ...) (ThreadId ...))
391 (with-syntax (((t ...) (generate-temporaries #'(RetId ...)))
392 ((ts ...) (generate-temporaries #'(RetId ...)))
393 ((null ...) (map (lambda (x) #''()) #'(RetId ...))))
394 #'(let ((fun F))
395 (rec g
396 (lambda (ThreadId ... ls)
397 (if (null? ls)
398 (values ThreadId ... null ...)
399 (call-with-values
400 (lambda () (g ThreadId ... (cdr ls)))
401 (lambda (ThreadId ... ts ...)
402 (call-with-values
403 (lambda () (fun ThreadId ... (car ls)))
404 (lambda (ThreadId ... t ...)
405 (values ThreadId ... (cons t ts) ...))))))))))))))
406
407 ;;; ------------------------------
408
409 (define-syntax my-backquote
410 (lambda (x)
411 (define ellipsis?
412 (lambda (x)
413 (and (identifier? x) (free-identifier=? x #'(... ...)))))
414 (define-syntax with-values
415 (syntax-rules ()
416 ((_ P C) (call-with-values (lambda () P) C))))
417 (define-syntax syntax-lambda
418 (lambda (x)
419 (syntax-case x ()
420 ((_ (Pat ...) Body0 Body ...)
421 (with-syntax (((X ...) (generate-temporaries #'(Pat ...))))
422 #'(lambda (X ...)
423 (with-syntax ((Pat X) ...)
424 Body0 Body ...)))))))
425 (define-syntax with-temp
426 (syntax-rules ()
427 ((_ V Body0 Body ...)
428 (with-syntax (((V) (generate-temporaries '(x))))
429 Body0 Body ...))))
430 (define-syntax with-temps
431 (syntax-rules ()
432 ((_ (V ...) (Exp ...) Body0 Body ...)
433 (with-syntax (((V ...) (generate-temporaries #'(Exp ...))))
434 Body0 Body ...))))
435 (define destruct
436 (lambda (Orig x depth)
437 (syntax-case x (quasiquote unquote unquote-splicing)
438 ;; inner quasiquote
439 ((quasiquote Exp)
440 (with-values (destruct Orig #'Exp (add1 depth))
441 (syntax-lambda (Builder Vars Exps)
442 (if (null? #'Vars)
443 (values #''(quasiquote Exp) '() '())
444 (values #'(list 'quasiquote Builder) #'Vars #'Exps)))))
445 ;; unquote
446 ((unquote Exp)
447 (zero? depth)
448 (with-temp X
449 (values #'X (list #'X) (list #'Exp))))
450 ((unquote Exp)
451 (with-values (destruct Orig #'Exp (sub1 depth))
452 (syntax-lambda (Builder Vars Exps)
453 (if (null? #'Vars)
454 (values #''(unquote Exp) '() '())
455 (values #'(list 'unquote Builder) #'Vars #'Exps)))))
456 ;; splicing
457 (((unquote-splicing Exp))
458 (zero? depth)
459 (with-temp X
460 (values #'X (list #'X) (list #'Exp))))
461 (((unquote-splicing Exp ...))
462 (zero? depth)
463 (with-temps (X ...) (Exp ...)
464 (values #'(append X ...) #'(X ...) #'(Exp ...))))
465 (((unquote-splicing Exp ...) . Rest)
466 (zero? depth)
467 (with-values (destruct Orig #'Rest depth)
468 (syntax-lambda (Builder Vars Exps)
469 (with-temps (X ...) (Exp ...)
470 (if (null? #'Vars)
471 (values #'(append X ... 'Rest)
472 #'(X ...) #'(Exp ...))
473 (values #'(append X ... Builder)
474 #'(X ... . Vars) #'(Exp ... . Exps)))))))
475 ((unquote-splicing Exp ...)
476 (with-values (destruct Orig #'(Exp ...) (sub1 depth))
477 (syntax-lambda (Builder Vars Exps)
478 (if (null? #'Vars)
479 (values #''(unquote-splicing Exp ...) '() '())
480 (values #'(cons 'unquote-splicing Builder)
481 #'Vars #'Exps)))))
482 ;; dots
483 (((unquote Exp) Dots)
484 (and (zero? depth) (ellipsis? #'Dots))
485 (with-temp X
486 (values #'X (list #'X) (list #'Exp))))
487 (((unquote Exp) Dots . Rest)
488 (and (zero? depth) (ellipsis? #'Dots))
489 (with-values (destruct Orig #'Rest depth)
490 (syntax-lambda (RestBuilder RestVars RestExps)
491 (with-syntax ((TailExp
492 (if (null? #'RestVars)
493 #''Rest
494 #'RestBuilder)))
495 (with-temp X
496 (values #'(append X TailExp)
497 (cons #'X #'RestVars)
498 (cons #'Exp #'RestExps)))))))
499 ((Exp Dots . Rest)
500 (and (zero? depth) (ellipsis? #'Dots))
501 (with-values (destruct Orig #'Exp depth)
502 (syntax-lambda (ExpBuilder (ExpVar ...) (ExpExp ...))
503 (if (null? #'(ExpVar ...))
504 (syntax-error Orig "Bad ellipsis")
505 (with-values (destruct Orig #'Rest depth)
506 (syntax-lambda (RestBuilder RestVars RestExps)
507 (with-syntax ((TailExp
508 (if (null? #'RestVars)
509 #''Rest
510 #'RestBuilder))
511 (Orig Orig))
512 (values #'(let f ((ExpVar ExpVar) ...)
513 (if (and (pair? ExpVar) ...)
514 (cons
515 (let ((ExpVar (car ExpVar)) ...)
516 ExpBuilder)
517 (f (cdr ExpVar) ...))
518 (if (and (null? ExpVar) ...)
519 TailExp
520 (error 'unquote
521 "Mismatched lists"
522 Orig))))
523 (append #'(ExpVar ...) #'RestVars)
524 (append #'(ExpExp ...) #'RestExps)))))))))
525 ;; Vectors
526 (#(X ...)
527 (with-values (destruct Orig #'(X ...) depth)
528 (syntax-lambda (LsBuilder LsVars LsExps)
529 (values #'(list->vector LsBuilder) #'LsVars #'LsExps))))
530 ;; random stuff
531 ((Hd . Tl)
532 (with-values (destruct Orig #'Hd depth)
533 (syntax-lambda (HdBuilder HdVars HdExps)
534 (with-values (destruct Orig #'Tl depth)
535 (syntax-lambda (TlBuilder TlVars TlExps)
536 (with-syntax ((Hd (if (null? #'HdVars)
537 #''Hd
538 #'HdBuilder))
539 (Tl (if (null? #'TlVars)
540 #''Tl
541 #'TlBuilder)))
542 (values #'(cons Hd Tl)
543 (append #'HdVars #'TlVars)
544 (append #'HdExps #'TlExps))))))))
545 (OtherThing
546 (values #''OtherThing '() '())))))
547 ;; macro begins
548 (syntax-case x ()
549 ((_ Datum)
550 (with-values (destruct #'(quasiquote Datum) #'Datum 0)
551 (syntax-lambda (Builder (Var ...) (Exp ...))
552 (if (null? #'(Var ...))
553 #''Datum
554 #'(let ((Var Exp) ...)
555 Builder))))))))
556
557 (define-syntax extend-backquote
558 (lambda (x)
559 (syntax-case x ()
560 ((_ Template Exp ...)
561 (with-syntax ((quasiquote
562 (datum->syntax #'Template 'quasiquote)))
563 #'(let-syntax ((quasiquote
564 (lambda (x)
565 (syntax-case x ()
566 ((_ Foo) #'(my-backquote Foo))))))
567 Exp ...))))))
568
569 ;;; ------------------------------
570
571 (define-syntax with-values
572 (syntax-rules ()
573 ((_ P C) (call-with-values (lambda () P) C))))
574
575 (define-syntax letcc
576 (syntax-rules ()
577 ((_ V B0 B ...) (call/cc (lambda (V) B0 B ...)))))
578
579 (define classify-list
580 (lambda (ls)
581 (cond
582 ((null? ls) 'proper)
583 ((not (pair? ls)) 'improper)
584 (else
585 (let f ((tortoise ls) (hare (cdr ls)))
586 (cond
587 ((eq? tortoise hare) 'infinite)
588 ((null? hare) 'proper)
589 ((not (pair? hare)) 'improper)
590 (else
591 (let ((hare (cdr hare)))
592 (cond
593 ((null? hare) 'proper)
594 ((not (pair? hare)) 'improper)
595 (else (f (cdr ls) (cdr hare))))))))))))
596
597 (define ilist-copy-flat
598 (lambda (ils)
599 (let f ((tortoise ils) (hare (cdr ils)))
600 (if (eq? tortoise hare)
601 (list (car tortoise))
602 (cons (car tortoise) (f (cdr tortoise) (cddr hare)))))))
603
604 (define sexp-dispatch
605 (lambda (obj pat);; #f or list of vars
606 (letcc escape
607 (let ((fail (lambda () (escape #f))))
608 (let f ((pat pat) (obj obj) (vals '()))
609 (cond
610 ((eq? pat 'any)
611 (cons obj vals))
612 ((eq? pat 'each-any)
613 ;; handle infinities
614 (case (classify-list obj)
615 ((proper infinite) (cons obj vals))
616 ((improper) (fail))))
617 ((pair? pat)
618 (if (pair? obj)
619 (f (car pat) (car obj) (f (cdr pat) (cdr obj) vals))
620 (fail)))
621 ((vector? pat)
622 (case (vector-ref pat 0)
623 ((atom)
624 (let ((a (vector-ref pat 1)))
625 (if (eqv? obj a)
626 vals
627 (fail))))
628 ((vector)
629 (if (vector? obj)
630 (let ((vec-pat (vector-ref pat 1)))
631 (f vec-pat (vector->list obj) vals))
632 (fail)))
633 ((each)
634 ;; if infinite, copy the list as flat, then do the matching,
635 ;; then do some set-cdrs.
636 (let ((each-pat (vector-ref pat 1))
637 (each-size (vector-ref pat 2)))
638 (case (classify-list obj)
639 ((improper) (fail))
640 ((infinite)
641 (let ((each-vals (f pat (ilist-copy-flat obj) '())))
642 (for-each (lambda (x) (set-cdr! (last-pair x) x))
643 each-vals)
644 (append each-vals vals)))
645 ((proper)
646 (append
647 (let g ((obj obj))
648 (if (null? obj)
649 (make-list each-size '())
650 (let ((hd-vals (f each-pat (car obj) '()))
651 (tl-vals (g (cdr obj))))
652 (map cons hd-vals tl-vals))))
653 vals)))))
654 ((tail-each)
655 (let ((each-pat (vector-ref pat 1))
656 (each-size (vector-ref pat 2))
657 (revtail-pat (vector-ref pat 3))
658 (revtail-tail-pat (vector-ref pat 4)))
659 (when (eq? (classify-list obj) 'infinite) (fail))
660 (with-values
661 (let g ((obj obj))
662 ;; in-tail?, vals, revtail-left/ls
663 (cond
664 ((pair? obj)
665 (with-values (g (cdr obj))
666 (lambda (in-tail? vals tail-left/ls)
667 (if in-tail?
668 (if (null? tail-left/ls)
669 (values #f vals (list (car obj)))
670 (values #t (f (car tail-left/ls)
671 (car obj)
672 vals)
673 (cdr tail-left/ls)))
674 (values #f vals
675 (cons (car obj) tail-left/ls))))))
676 (else
677 (values #t
678 (f revtail-tail-pat obj vals)
679 revtail-pat))))
680 (lambda (in-tail? vals tail-left/ls)
681 (if in-tail?
682 (if (null? tail-left/ls)
683 (append (make-list each-size '())
684 vals)
685 (fail))
686 (f each-pat tail-left/ls vals))))))))
687 (else
688 (if (eqv? obj pat)
689 vals
690 (fail)))))))))
691 )
692
693
694 ;;; examples of passing along threaded information.
695
696 ;;; Try (collect-symbols '(if (x y 'a 'c zz) 'b 'c))
697 ;;; Note that it commonizes the reference to c.
698
699 ;; (define-syntax with-values
700 ;; (syntax-rules ()
701 ;; ((_ P C) (call-with-values (lambda () P) C))))
702 ;; (define collect-symbols
703 ;; (lambda (exp)
704 ;; (with-values (collect-symbols-help exp)
705 ;; (lambda (symbol-decls exp)
706 ;; (match symbol-decls
707 ;; (((,symbol-name . ,symbol-var) ...)
708 ;; `(let ((,symbol-var (quote ,symbol-name)) ...) ,exp)))))))
709 ;; (define collect-symbols-help
710 ;; (lambda (exp)
711 ;; (let ((symbol-env '()))
712 ;; (match+ (symbol-env) exp
713 ;; (,x
714 ;; (guard (symbol? x))
715 ;; (values symbol-env x))
716 ;; ((quote ,x)
717 ;; (guard (symbol? x))
718 ;; (let ((pair/false (assq x symbol-env)))
719 ;; (if pair/false
720 ;; (values symbol-env (cdr pair/false))
721 ;; (let ((v (gensym)))
722 ;; (values (cons (cons x v) symbol-env)
723 ;; v)))))
724 ;; ((quote ,x)
725 ;; (values symbol-env `(quote ,x)))
726 ;; ((if ,(t) ,(c) ,(a))
727 ;; (values symbol-env `(if ,t ,c ,a)))
728 ;; ((,(op) ,(arg) ...)
729 ;; (values symbol-env `(,op ,arg ...)))))))
730 ;;
731 ;; ;;; the grammar for this one is just if-exprs and everything else
732 ;;
733 ;; (define collect-leaves
734 ;; (lambda (exp acc)
735 ;; (match+ (acc) exp
736 ;; ((if ,() ,() ,())
737 ;; acc)
738 ;; ((,() ,() ...)
739 ;; acc)
740 ;; (,x
741 ;; (cons x acc)))))
742 ;;
743 ;; ;; here's something that takes apart quoted stuff.
744 ;;
745 ;; (define destruct
746 ;; (lambda (datum)
747 ;; (match datum
748 ;; (() `'())
749 ;; ((,(X) . ,(Y))`(cons ,X ,Y))
750 ;; (#(,(X) ...) `(vector ,X ...))
751 ;; (,thing
752 ;; (guard (symbol? thing))
753 ;; `',thing)
754 ;; (,thing
755 ;; thing))))
756 ;;
757 ;; ;; examples using explicit Catas
758 ;;
759 ;; (define sumsquares
760 ;; (lambda (ls)
761 ;; (define square
762 ;; (lambda (x)
763 ;; (* x x)))
764 ;; (match ls
765 ;; ((,(a*) ...) (apply + a*))
766 ;; (,(square -> n) n))))
767 ;;
768 ;; (define sumsquares
769 ;; (lambda (ls)
770 ;; (define square
771 ;; (lambda (x)
772 ;; (* x x)))
773 ;; (let ((acc 0))
774 ;; (match+ (acc) ls
775 ;; ((,() ...) acc)
776 ;; (,((lambda (acc x) (+ acc (square x))) ->) acc)))))
777 ;;
778 ;; ;;; The following uses explicit Catas to parse programs in the
779 ;; ;;; simple language defined by the grammar below
780 ;;
781 ;; ;;; <Prog> -> (program <Stmt>* <Expr>)
782 ;; ;;; <Stmt> -> (if <Expr> <Stmt> <Stmt>)
783 ;; ;;; | (set! <var> <Expr>)
784 ;; ;;; <Expr> -> <var>
785 ;; ;;; | <integer>
786 ;; ;;; | (if <Expr> <Expr> <Expr>)
787 ;; ;;; | (<Expr> <Expr*>)
788 ;;
789 ;; (define parse
790 ;; (lambda (x)
791 ;; (define Prog
792 ;; (lambda (x)
793 ;; (match x
794 ;; ((program ,(Stmt -> s*) ... ,(Expr -> e))
795 ;; `(begin ,s* ... ,e))
796 ;; (,other (error 'parse "invalid program" other)))))
797 ;; (define Stmt
798 ;; (lambda (x)
799 ;; (match x
800 ;; ((if ,(Expr -> e) ,(Stmt -> s1) ,(Stmt -> s2))
801 ;; `(if ,e ,s1 ,s2))
802 ;; ((set! ,v ,(Expr -> e))
803 ;; (guard (symbol? v))
804 ;; `(set! ,v ,e))
805 ;; (,other (error 'parse "invalid statement" other)))))
806 ;; (define Expr
807 ;; (lambda (x)
808 ;; (match x
809 ;; (,v (guard (symbol? v)) v)
810 ;; (,n (guard (integer? n)) n)
811 ;; ((if ,(e1) ,(e2) ,(e3))
812 ;; `(if ,e1 ,e2 ,e3))
813 ;; ((,(rator) ,(rand*) ...) `(,rator ,rand* ...))
814 ;; (,other (error 'parse "invalid expression" other)))))
815 ;; (Prog x)))
816 ;;; (parse '(program (set! x 3) (+ x 4)))) => (begin (set! x 3) (+ x 4))
817
818 ;; CHANGELOG (most recent changes are logged at the top of this file)
819
820 ;; (29 Feb 2000)
821 ;; Fixed a case sensitivity bug.
822
823 ;; (24 Feb 2000)
824 ;; Matcher now handles vector patterns. Quasiquote also handles
825 ;; vector patterns, but does NOT do the csv6.2 optimization of
826 ;; `#(a 1 ,(+ 3 4) x y) ==> (vector 'a 1 (+ 3 4) 'x 'y).
827 ;; Also fixed bug in (P ... . P) matching code.
828
829 ;; (23 Feb 2000)
830 ;; KSM fixed bug in unquote-splicing inside quasiquote.
831
832 ;; (10 Feb 2000)
833 ;; New forms match+ and trace-match+ thread arguments right-to-left.
834 ;; The pattern (P ... . P) now works the way you might expect.
835 ;; Infinite lists are now properly matched (and not matched).
836 ;; Removed the @ pattern.
837 ;; Internal: No longer converting into syntax-case.
838
839 ;; (6 Feb 2000)
840 ;; Added expansion-time error message for referring to cata variable
841 ;; in a guard.
842
843 ;; (4 Feb 2000)
844 ;; Fixed backquote so it can handle nested backquote (oops).
845 ;; Double-backquoted elipses are neutralized just as double-backquoted
846 ;; unquotes are. So:
847 ;; `(a ,'(1 2 3) ... b) =eval=> (a 1 2 3 b)
848 ;; ``(a ,'(1 2 3) ... b) =eval=> `(a ,'(1 2 3) ... b)
849 ;; ``(a ,(,(1 2 3) ...) b) =eval=> `(a ,(1 2 3) b)
850 ;; Added support for
851 ;; `((unquote-splicing x y z) b) =expand==> (append x y z (list 'b))
852
853 ;; (1 Feb 2000)
854 ;; Fixed a bug involving forgetting to quote stuff in the revised backquote.
855 ;; Recognized unquote-splicing and signalled errors in the appropriate places.
856 ;; Added support for deep elipses in backquote.
857 ;; Rewrote backquote so it does the rebuilding directly instead of
858 ;; expanding into Chez's backquote.
859
860 ;; (31 Jan 2000)
861 ;; Kent Dybvig fixed template bug.
862
863 ;; (31 Jan 2000)
864 ;; Added the trace-match form, and made guards contain
865 ;; an explicit and expression:
866 ;; (guard E ...) ==> (guard (and E ...))
867
868 ;; (26 Jan 2000)
869 ;; Inside the clauses of match expressions, the following
870 ;; transformation is performed inside backquote expressions:
871 ;; ,v ... ==> ,@v
872 ;; (,v ,w) ... ==> ,@(map list v w)
873 ;; etc.
874