-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxm11.scm
1663 lines (1445 loc) · 59.9 KB
/
xm11.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; SPDX-FileCopyrightText: 2024 Sergei Egorov
;;; SPDX-License-Identifier: MIT
;========================================================================================
;
; Simple extendable pattern matcher with backtracking (c) 2024 Sergei Egorov
;
;========================================================================================
; Note: this file is made for loading intro a REPL with (scheme base) and (scheme write)
; libraries already imported (the latter one is only needed for tests).
; The system as implememted here is quite light on optimization and error checking.
; Support for #& read syntax and boxes is commented-out, but, if uncommented, works
; on systems supporting both features (e.g. Chez Scheme).
;
; 1) helper macros, used throughout all parts of the implementation
;
; (classify x if-ellipsis if-underscore if-var if-other)
; returns one of four expressions based on type of x, in 4 steps
; Should work on R7RS, R6RS, and earlier syntax-rules expanders
(define-syntax classify
(syntax-rules ()
((_ (x . y) ke ku kv kf) kf)
((_ #(x ...) ke ku kv kf) kf)
;((_ #&x ke ku kv kf) kf)
((_ atom ke ku kv kf) ; ellipsis test after Taylor Campbell
(let-syntax ((e? (syntax-rules () ((_ (x atom) t f) t) ((_ x t f) f))))
(e? (1 2 3) ke (classify-nonellipsis-atom atom ku kv kf))))))
(define-syntax classify-nonellipsis-atom
(syntax-rules ()
((_ atom ku kv kf) ; symbol test after Oleg Kiselyof
(let-syntax ((s? (syntax-rules () ((_ atom t f) t) ((_ x t f) f))))
(s? abracadabra (classify-nonellipsis-symbol atom ku kv) kf)))))
(define-syntax classify-nonellipsis-symbol
(syntax-rules ()
((_ symbol ku kv) ; see if symbol acts like a variable
(let-syntax ((b (syntax-rules () ((_ symbol k t f) (k symbol t f))))
(k (syntax-rules () ((_ () t f) t) ((_ x t f) f))))
(b () k (classify-nonellipsis-var symbol ku kv) ku)))))
(define-syntax classify-nonellipsis-var
(syntax-rules ()
((_ var ku kv) ; needed for R4RS/R5RS systems where _ is a regular var
(let-syntax ((u? (syntax-rules (var) ((_ var t f) t) ((_ x t f) f))))
(u? _ ku kv)))))
; check if v is not among vars in (a ...) list, using free-id= rules for comparison
; NB: neither v, nor any of a-s should be ellipsis (...) or underscore (_) identifiers
(define-syntax if-new-var
(syntax-rules ()
((_ v (a ...) kt kf)
(let-syntax ((ck (syntax-rules (a ...) ((_ a) kf) ... ((_ x) kt)))) (ck v)))))
; calculate set minus on two set of vars, using free-id= rules for comparison
; NB: neither set should contain ellipsis (...) and underscore (_) identifiers
(define-syntax varset-minus
(syntax-rules () ; (_ v* mv* k () . a*) ==> (k v-mv* . a*)
((_ () mv* k rv* . a*) (k rv* . a*))
((_ (v . v*) mv* k rv* . a*)
(if-new-var v mv*
(varset-minus v* mv* k (v . rv*) . a*)
(varset-minus v* mv* k rv* . a*)))))
; (replace-specials ell und sexp . k) invokes k with reassembled sexp,
; replacing ... with ell and _ with und (_ and ... are recognized according to the free-id= rules)
(define-syntax replace-specials
(syntax-rules ()
((_ ell und sexp . cont)
(letrec-syntax
((subx
(syntax-rules ()
((_ ev uv () k . a*) (k () . a*))
((_ ev uv (x . y) k . a*) (subx ev uv x subcdr ev uv y k . a*))
;((_ ev uv #&x k . a*) (subx ev uv x rebox k . a*))
((_ ev uv #(x (... ...)) k . a*) (subx ev uv (x (... ...)) revec k . a*))
((_ ev uv x k . a*) (classify x (k ev . a*) (k uv . a*) (k x . a*) (syntax-error "r-s?")))))
(subcdr
(syntax-rules ()
((_ sx ev uv y k . a*) (subx ev uv y repair sx k . a*))))
(repair (syntax-rules () ((_ sy sx k . a*) (k (sx . sy) . a*))))
;(rebox (syntax-rules () ((_ sx k . a*) (k #&sx . a*))))
(revec (syntax-rules () ((_ (sx (... ...)) k . a*) (k #(sx (... ...)) . a*)))))
(subx ell und sexp . cont)))))
; Petrofsky's extraction (after Al* Petrofsky via Oleg Kiselyof)
(define-syntax extract
(syntax-rules ()
((_ _id _x _c)
(letrec-syntax
((tr (syntax-rules (_id)
((_ x _id tail (k il . a*)) (k (x . il) . a*))
((_ d (x . y) tail c) (tr x x (y . tail) c))
((_ d1 d2 () (k il . a*)) (k (_id . il) . a*))
((_ d1 d2 (x . y) c) (tr x x y c)))))
(tr _x _x () _c)))))
(define-syntax extract*
(syntax-rules ()
((_ id* x c) (extract* () id* x c))
((_ il () x (k () . a*)) (k il . a*))
((_ il (id . id*) x c)
(extract id x (extract* il id* x c)))))
;
; 2) base matcher macro framework/protocol
;
(define-syntax match
(syntax-rules ()
((_ exp clause ...)
(let ((xv exp))
(match-var xv clause ...)))))
; internal macro to jump-start var collection and code generation;
; submatch below accepts only vars as first argument
(define-syntax match-var
(syntax-rules (=>)
((_ xv) (if #f #f))
((_ xv (pat (=> nv) exp ...) . clauses)
(let* ((nv (lambda () (match-var xv . clauses))) (b nv))
(let-syntax ((n (syntax-rules () ((_ k . a*) (k () . a*)))))
(submatch xv pat (b n) (let () exp ...) (nv)))))
((_ xv (pat (=> nv bv) exp ...) . clauses)
(let* ((nv (lambda () (match-var xv . clauses))) (bv nv))
(let-syntax ((n (syntax-rules () ((_ k . a*) (k () . a*)))))
(submatch xv pat (bv n) (let () exp ...) (nv)))))
((_ xv (pat exp ...) . clauses)
(let* ((next (lambda () (match-var xv . clauses))) (b next))
(let-syntax ((n (syntax-rules () ((_ k . a*) (k () . a*)))))
(submatch xv pat (b n) (let () exp ...) (next)))))))
; main var scanner / code generator
; Submatch operates in two modes: as pattern variable scanner and as code generator
; xv is a variable let-bound to the current expression being matched by the pattern
; c is 'context' (as described below, context format depends on mode of operation)
; p is a pattern (either primitive or a macro name followed by args / subpatterns)
; kt is the right-hand-side expr, usually complex, so it shouldn't be duplicated
; kf is the failure expression, usually just a call of a variable, can be duplicated
; - in scanner mode, it is invoked with () for xv and kf parameters and (n) context,
; where n is a macro thunk, returning a list of pairs (var . tmpid), with vars unique
; in free-id=? sense; this list is grown by syntactically rebinding n to a new list
; and expanding kt in its context. The main work is done by submatch, but external
; matchers have to cooperate by calling submatch with their sub-pattern arguments
; - in codegen mode, it is invoked with an id for xv and (b n) context, where n
; is as stated above (except that it assembles a list of vars, with no tempids),
; and b is the name of the lexically closest backtracking point (also a thunk).
; In this mode, submatch'es task is to build the matching code; it does that on
; its own for primitive patterns, but needs cooperation from external matchers for
; all non-primitive patterns
(define-syntax submatch
(syntax-rules (quote quasiquote)
; scan for vars
((_ () () (n) kt ()) kt)
((_ () (quote lit) (n) kt ()) kt)
; generate code
((_ xv () c kt kf)
(if (null? xv) kt kf))
((_ xv (quote lit) c kt kf)
(if (equal? xv (quote lit)) kt kf))
((_ xv (quasiquote qq) c kt kf)
(submatch-qq xv qq c kt kf))
((_ xv (h . t) c kt kf)
(h xv t c kt kf))
; scan atom for vars
((_ () a (n) kt ())
(classify a
(syntax-error "... used as a variable name")
kt ; _ is not a var
(let-syntax
((k (syntax-rules ()
((_ v* t*)
(if-new-var a v*
; rebind n to a syntax 'thunk' returning extended name-temp alist
(let-syntax ((n (syntax-rules () ((_ k . args) (k (a . v*) (t . t*) . args)))))
kt)
kt)))))
(n k))
kt))
; generate atom code
((_ xv a (b n) kt kf)
(classify a
(syntax-error "... used as a variable name")
kt ; _ matches anything
(let-syntax
((k (syntax-rules ()
((_ v*)
(if-new-var a v*
(let ((a xv))
; rebind n to a syntax 'thunk' returning extended name list
(let-syntax ((n (syntax-rules () ((_ k . args) (k (a . v*) . args)))))
kt))
(if (equal? xv a) kt kf)))))) ; non-linear check ror repeated vars
(n k))
(if (equal? xv (quote a)) kt kf)))))
; quasiquote matcher expands into a combination of specific matchers defined below;
; multiple splicing patterns at the same level do greedy matching, maximizing lengths
; of left/upstream segments; other 'solutions' can be obtained via backtracking
(define-syntax submatch-qq
(syntax-rules (unquote unquote-splicing)
((_ xv ,p c kt kf)
(submatch xv p c kt kf))
((_ xv (,@lp) c kt kf)
(submatch xv lp c kt kf))
((_ xv (,@lp . dp) c kt kf)
(submatch xv (~append lp `dp) c kt kf))
((_ xv (ap . dp) c kt kf)
(submatch xv (~cons `ap `dp) c kt kf))
((_ xv #(p ...) c kt kf)
(submatch xv (~list->vector `(p ...)) c kt kf))
;((_ xv #&p c kt kf)
; (submatch xv (~box `p) c kt kf))
((_ xv lit c kt kf)
(submatch xv (quote lit) c kt kf))))
; pattern var scanner -- uses var extraction protocol (submatch) and Petrofsky's extractor;
; var extraction protocol returns arbitrarily colored vars, distinct w.r.t free-id=?;
; Petrofsky's extractor locates their pattern-colored versions suitable for binding.
; It wouldn't be necessary if not for ~or and ~etc matchers that need to bind all vars
; NB: The extractor can't accidentally hit on a non-pattern identifier because we explicitly
; prohibit use of pattern variables in non-pattern subelements of patterns
(define-syntax extract-pattern-vars
(syntax-rules () ; (_ p (rk . a*)) ==> (rk v* t* . a*)
((_ p (rk . a*))
(let-syntax ((n (syntax-rules () ((_ k . args) (k () () . args)))))
(submatch () p (n) ; scan for vars protocol
(let-syntax
((k (syntax-rules ()
((_ v* t*) (extract-pattern-vars p v* t* (rk . a*))))))
(n k))
())))
((_ p v* t* (rk . a*)) ; rescan to get colors right (in bound-id=? sense)
(extract* v* p (rk () t* . a*)))))
(define-syntax extract-new-pattern-vars
(syntax-rules () ; (_ p n (rk . a*)) ==> (rk () nv* . a*)
((_ pat names cont)
(letrec-syntax
((step1 (syntax-rules () ((_ mv* p c) (extract-pattern-vars p (step2 mv* c)))))
(step2 (syntax-rules () ((_ v* t* mv* c) (varset-minus v* mv* step3 () c))))
(step3 (syntax-rules () ((_ v-mv* (rk . a*)) (rk () v-mv* . a*)))))
(names step1 pat cont)))))
;
; 3) matchers used by quasiquote notation
;
(define-syntax ~cons
(syntax-rules ()
((_ () (ap dp) (n) kt ()) ; scan ap, dp for vars
(submatch () ap (n) (submatch () dp (n) kt ()) ()))
((_ xv (ap dp) c kt kf)
(if (pair? xv)
(let ((axv (car xv)) (dxv (cdr xv)))
(submatch axv ap c (submatch dxv dp c kt kf) kf))
kf))))
(define-syntax ~list
(syntax-rules ()
((_ xv () c kt kf)
(submatch xv '() c kt kf))
((_ xv (p . p*) c kt kf)
(submatch xv (~cons p (~list . p*)) c kt kf))))
(define-syntax ~list->vector
(syntax-rules ()
((_ () (p) (n) kt ()) ; scan for vars
(submatch () p (n) kt ()))
((_ xv (p) c kt kf)
(if (vector? xv)
(let ((yv (vector->list xv))) (submatch yv p c kt kf))
kf))))
(define (match:append-greedy-start xv try) ; internal
(let loop ((txv xv) (rxv '()))
(if (pair? txv) (loop (cdr txv) (cons (car txv) rxv)) (try rxv txv))))
(define-syntax ~append
(syntax-rules ()
((_ () p* (n) kt ()) ; scan for vars
(submatch () (~and . p*) (n) kt ())) ; union
((_ xv () c kt kf)
(submatch xv '() c kt kf))
((_ xv (p) c kt kf)
(submatch xv p c kt kf))
((_ xv (hp tp) (b n) kt kf)
(let ((f (lambda () kf))) ; in scope of 'parent' b
(define (try rxv txv)
(define (b) (if (pair? rxv) (try (cdr rxv) (cons (car rxv) txv)) (f)))
(let ((hxv (reverse rxv))) ; match head first
(submatch hxv hp (b n) (submatch txv tp (b n) kt (b)) (b))))
(match:append-greedy-start xv try)))
((_ xv (p . p*) c kt kf)
(submatch xv (~append p (~append . p*)) c kt kf))))
;(define-syntax ~box
; (syntax-rules ()
; ((_ () (p) (n) kt ()) ; scan for vars
; (submatch () p (n) kt ()))
; ((_ xv (p) c kt kf)
; (if (box? xv)
; (let ((yv (unbox xv))) (submatch yv p c kt kf))
; kf))))
;
; 4) additional useful matchers, going beyond common core specified by SRFI-200
;
; 'value' matcher compares xv with the value of runtime-calculated expression via equal?
(define-syntax ~value
(syntax-rules ()
((_ () (e) (n) kt ()) kt) ; scan for vars
((_ xv (e) c kt kf) (if (equal? xv e) kt kf))))
; non-greedy matcher for (possibly improper) lists
(define-syntax ~append/ng
(syntax-rules ()
((_ () p* (n) kt ()) ; scan for vars
(submatch () (~and . p*) (n) kt ())) ; union
((_ xv () c kt kf)
(submatch xv '() c kt kf))
((_ xv (p) c kt kf)
(submatch xv p c kt kf))
((_ xv (hp tp) (b n) kt kf)
(let ((f (lambda () kf))) ; in scope of 'parent' b
(let loop ((hxv '()) (txv xv)) ; match head first
(define (b) (if (pair? txv) (loop (append hxv (list (car txv))) (cdr txv)) (f)))
(submatch hxv hp (b n) (submatch txv tp (b n) kt (b)) (b)))))
((_ xv (p . p*) c kt kf)
(submatch xv (~append/ng p (~append/ng . p*)) c kt kf))))
; non-iterative matcher for append with fixed-length rhs list (first arg, may be improper)
; may be used to implement single-ellipsis append patterns as popularized by syntax-rules
(define (match:append-tail-length l)
(let loop ((l l) (n 0))
(if (pair? l) (loop (cdr l) (+ n 1)) n)))
(define (match:append-tail-start xv n fail try) ;=> (try head tail) or (fail)
(let loop ((l xv) (n n))
(if (zero? n)
(let loop ((lag xv) (lead l) (head '()))
(if (pair? lead)
(loop (cdr lag) (cdr lead) (cons (car lag) head))
(try (reverse head) lag)))
(if (pair? l) (loop (cdr l) (- n 1)) (fail)))))
(define-syntax ~append/t
(syntax-rules ()
((_ () (t hp tp) (n) kt ()) ; scan for vars
(submatch () (~and hp tp) (n) kt ())) ; union
((_ xv (t hp tp) c kt kf)
(let ((f (lambda () kf)))
(match:append-tail-start xv (if (integer? 't) 't (match:append-tail-length 't)) f
(lambda (hxv txv) (submatch hxv hp c (submatch txv tp c kt (f)) (f))))))))
; general-purpose parameterized backtracking matcher
; (~iterate start head tail v* p) builds a backtracking matcher
; that produces a stream of 'solutions' to be matched against p;
; v* is a list of state variables (names mostly used for exposition)
; - start is invoked with original value and two continuation procedures:
; first one accepts 'seed' values for state variables if start succeeds,
; the second one accepts no values and is called if start fails
; - head accepts current values of state variables and returns a
; single value to be matched against pattern p
; - tail accepts the same two continuations as start, followed by
; the current values of state variables; it should either call its
; first continuation to continue with new values of state vars,
; or the second one to signal that there are no more 'solutions'
; note: start head tail can be procedures add/or macros
; note: both try and f should be called in tail positions!
(define-syntax ~iterate
(syntax-rules ()
; scan for vars
((_ () (start head tail v* p) (n) kt ())
(submatch () p (n) kt ()))
; generate code
((_ xv (start head tail v* p) (b n) kt kf)
(let ((f (lambda () kf))) ; in scope of 'parent' b
(define (try . v*)
(define (b) (tail try f . v*))
(let ((mxv (head . v*)))
(submatch mxv p (b n) kt (b))))
(start xv try f)))))
; 'popular' parameterized matchers for vector-like sequences
(define-syntax ~seq-append
(syntax-rules ()
((_ () (x? x-length subx nullx . p*) (n) kt ()) ; scan for vars
(submatch () (~and . p*) (n) kt ()))
((_ xv (x? x-length subx nullx) c kt kf)
(submatch xv nullx c kt kf))
((_ xv (x? x-length subx nullx p) c kt kf)
(submatch xv p c kt kf))
((_ xv (x? x-length subx nullx hp tp) (b n) kt kf)
(let ((l (x-length xv)) (f (lambda () kf)))
(let loop ((i l))
(define (b) (if (> i 0) (loop (- i 1)) (f)))
(let ((hxv (subx xv 0 i))) ; match head first
(submatch hxv hp (b n)
(let ((txv (subx xv i l)))
(submatch txv tp (b n) kt (b))) (b))))))
((_ xv (x? x-length subx nullx p . p*) c kt kf)
(submatch xv (~seq-append x? x-length subx nullx p
(~seq-append x? x-length subx nullx . p*)) c kt kf))))
(define-syntax ~seq-append/ng
(syntax-rules ()
((_ () (x? x-length subx nullx . p*) (n) kt ()) ; scan for vars
(submatch () (~and . p*) (n) kt ()))
((_ xv (x? x-length subx nullx) c kt kf)
(submatch xv nullx c kt kf))
((_ xv (x? x-length subx nullx p) c kt kf)
(submatch xv p c kt kf))
((_ xv (x? x-length subx nullx hp tp) (b n) kt kf)
(let ((l (x-length xv)) (f (lambda () kf)))
(let loop ((i 0))
(define (b) (if (< i l) (loop (+ i 1)) (f)))
(let ((hxv (subx xv 0 i))) ; match head first
(submatch hxv hp (b n)
(let ((txv (subx xv i l)))
(submatch txv tp (b n) kt (b))) (b))))))
((_ xv (x? x-length subx nullx p . p*) c kt kf)
(submatch xv (~seq-append/ng x? x-length subx nullx p
(~seq-append/ng x? x-length subx nullx . p*)) c kt kf))))
;
; 5) boolean matchers
;
; 'and'-matcher (all pattern vars are bound in the rest of the patterns)
(define-syntax ~and
(syntax-rules ()
((_ xv () c kt kf)
kt)
((_ xv (p) c kt kf)
(submatch xv p c kt kf))
((_ xv (p . p*) c kt kf)
(submatch xv p c (submatch xv (~and . p*) c kt kf) kf))))
; 'or'-matcher (on success, all pattern vars are bound to #f except for those in the matching case)
(define-syntax ~or
(syntax-rules ()
((_ () p* (n) kt ()) ; scan for vars
(submatch () (~and . p*) (n) kt ())) ; union
((_ xv () c kt kf)
kf)
((_ xv (p) c kt kf)
(submatch xv p c kt kf))
((_ xv p* (b n) kt kf)
(extract-new-pattern-vars (~and . p*) n (~or xv p* (b n) kt kf)))
((_ () (v ...) xv p* c kt kf)
(let ((v #f) ... (lkt (lambda (v ...) kt)))
(submatch xv (match:or-aux . p*) c (lkt v ...) kf)))))
(define-syntax match:or-aux ; helper for ~or
(syntax-rules ()
((_ xv () c kt kf)
kf)
((_ xv (p) c kt kf)
(submatch xv p c kt kf))
((_ xv (p . p*) (b n) kt kf) ; kt can be duplicated
(let ((b (lambda () (submatch xv (match:or-aux . p*) (b n) kt kf)))) ; in scope of 'parent' b
(submatch xv p (b n) kt (b))))))
; 'not'-matcher (no additional pattern vars, if present, are bound in the rest of the patterns)
(define-syntax ~not
(syntax-rules ()
((_ () p* (n) kt ()) kt) ; scan for vars: no vars escape
((_ xv (p) c kt kf)
(let ((t (lambda () kt)) (f (lambda () kf)))
(submatch xv p c (f) (t))))
((_ xv (p ...) c kt kf)
(submatch xv (~and (~not p) ...) c kt kf))))
;
; 6) ~etc: ... - like list matcher
;
; Nonlinear part of ~etc works as follows: code for p is generated in
; empty 'n' (i.e. we skip nonlinear checks against vars on the left);
; when all p's own vars are bound to full lists, a precondition code
; is built that compares their values with the existing values of
; common variables of p and patterns upstream and triggers a failure
; if they are different; then the 'n' lists are merged for the patterns
; downstream (this part is easy -- just scan p in the usual manner)
(define-syntax match:etc-nl-test
(syntax-rules ()
((_ ov* () () e*) (and . e*))
((_ ov* (iv . iv*) (it . it*) e*)
(if-new-var iv ov*
(match:etc-nl-test ov* iv* it* e*)
(match:etc-nl-test ov* iv* it*
((equal? iv (reverse it)) . e*))))))
(define-syntax ~etc
(syntax-rules ()
((_ () (p) (n) kt ()) ; scan for vars
(submatch () p (n) kt ()))
((_ xv (p) c kt kf)
(extract-pattern-vars p (~etc xv p c kt kf)))
((_ (v ...) (t ...) xv p (b n) kt kf)
(let loop ((lxv xv) (t '()) ...)
(cond ((null? lxv)
(if (n match:etc-nl-test (v ...) (t ...) ())
(let ((v (reverse t)) ...) kt)
kf))
((pair? lxv)
(let ((axv (car lxv)) (dxv (cdr lxv)))
(let-syntax ((n (syntax-rules () ((_ k . a*) (k () . a*)))))
(submatch axv p (b n) (loop dxv (cons v t) ...) kf))))
(else kf))))))
;
; 7) general-purpose matchers with a function/predicate parameter
;
; 'property access' matcher: when matching x,
; (~prop f => p ...) matches the result(s) of (f x) to p ...
; (~prop f (arg ...) => p ...) matches the result(s) of (f x arg ...) to p ...
(define-syntax ~prop
(syntax-rules (=>)
; scan for vars
((_ () (x->y => . p*) (n) kt ()) (submatch () (~and . p*) (n) kt ()))
((_ () (x->y (a ...) => . p*) (n) kt ()) (submatch () (~and . p*) (n) kt ()))
; generate code
((_ xv (x->y => p) c kt kf)
(let ((yv (x->y xv)))
(submatch yv p c kt kf)))
((_ xv (x->y => . p*) c kt kf)
(let ((yv (call-with-values (lambda () (x->y xv)) list)))
(submatch yv (~list . p*) c kt kf)))
((_ xv (x->y (a ...) => p) c kt kf)
(let ((yv (x->y xv a ...)))
(submatch yv p c kt kf)))
((_ xv (x->y (a ...) => . p*) c kt kf)
(let ((yv (call-with-values (lambda () (x->y xv a ...)) list)))
(submatch yv (~list . p*) c kt kf)))))
; 'predicate test' matcher: when matching x,
; (~test f) fails if (f x) returns #f
; (~test f (arg ...)) fails if (f x arg ...) returns #f
; (~test f => p) fails if (f x) returns #f, otherwise matches result to p
; (~test f (arg ...) => p) fails if (f x) returns #f, otherwise matches result to p
(define-syntax ~test
(syntax-rules (=>)
; scan for vars
((_ () (x?) (n) kt ()) kt)
((_ () (x? (a ...)) (n) kt ()) kt)
((_ () (x? => p) (n) kt ()) (submatch () p (n) kt ()))
((_ () (x? (a ...) => p) (n) kt ()) (submatch () p (n) kt ()))
; generate code
((_ xv (x?) c kt kf)
(if (x? xv) kt kf))
((_ xv (x? (a ...)) c kt kf)
(if (x? xv a ...) kt kf))
((_ xv (x? => p) c kt kf)
(let ((v (x? xv))) (if v (submatch v p c kt kf) kf)))
((_ xv (x? (a ...) => p) c kt kf)
(let ((v (x? xv a ...))) (if v (submatch v p c kt kf) kf)))))
;
; 8) special matchers serving as building blocks for custom matchers
;
; 'literal check' matcher : (~if-id-member a (l ...) pt pf)
; uses pt if a is in (l ...), pf otherwise; uses free-id=? rules
(define-syntax ~if-id-member
(syntax-rules ()
((_ xv (a (l ...) pt pf) c kt kf)
(classify a
(syntax-error "... used as a variable name")
(syntax-error "_ used as a variable name")
(if-new-var a (l ...) (submatch xv pf c kt kf) (submatch xv pt c kt kf))
(submatch yv pt c kt kf)))))
; (~replace-specials new-ellipsis new-underscore p) matches against p after replacing
; ... in p with new-ellipsis and _ with new-underscore
(define-syntax ~replace-specials
(syntax-rules ()
((_ xv (ne nu p) c kt kf)
(replace-specials ne nu p ~replace-specials #f xv c kt kf))
((_ newp #f xv c kt kf)
(submatch xv newp c kt kf))))
; 'cut' matcher (does not allow backtracking into p; experimental)
(define-syntax ~!
(syntax-rules ()
((_ () (p) (n) kt ()) ; scan for vars
(submatch () p (n) kt ()))
((_ xv (p) (b n) kt kf)
(let ((b! b)) (submatch xv p (b n) (let ((b b!)) kt) kf)))))
;
; 9) extending the matcher
;
(define-syntax define-match-pattern
(syntax-rules ()
((_ ~dm (l ...) ((* . args) p) ...)
(define-syntax ~dm
(syntax-rules (l ...)
((_ xv args c kt kf)
(submatch xv p c kt kf)) ...)))))
; NB: all new matchers below are defined via define-match-pattern (no more submatch/hand-coding)
;
; 10) convenience matchers for popular scheme data types
;
; these two matchers are already defined in section 3) :
; (define-match-pattern ~box () ((_ p) (~and (~test box?) (~prop unbox => p))))
; (define-match-pattern ~list->vector () ((_ p) (~and (~test vector?) (~prop vector->list => p))))
(define-match-pattern ~null? () ((_ p ...) (~and (~test null?) p ...)))
(define-match-pattern ~pair? () ((_ p ...) (~and (~test pair?) p ...)))
(define-match-pattern ~list? () ((_ p ...) (~and (~test list?) p ...)))
(define-match-pattern ~boolean? () ((_ p ...) (~and (~test boolean?) p ...)))
(define-match-pattern ~number? () ((_ p ...) (~and (~test number?) p ...)))
(define-match-pattern ~integer? () ((_ p ...) (~and (~test integer?) p ...)))
(define-match-pattern ~vector? () ((_ p ...) (~and (~test vector?) p ...)))
(define-match-pattern ~string? () ((_ p ...) (~and (~test string?) p ...)))
(define-match-pattern ~symbol? () ((_ p ...) (~and (~test symbol?) p ...)))
(define-match-pattern ~char? () ((_ p ...) (~and (~test char?) p ...)))
;(define-match-pattern ~box? () ((_ p ...) (~and (~test box?) p ...)))
(define-match-pattern ~vector->list () ((_ p) (~and (~test list?) (~prop list->vector => p))))
(define-match-pattern ~string->list () ((_ p) (~and (~test list?) (~prop list->string => p))))
(define-match-pattern ~list->string () ((_ p) (~and (~test string?) (~prop string->list => p))))
(define-match-pattern ~string->symbol () ((_ p) (~and (~test symbol?) (~prop symbol->string => p))))
(define-match-pattern ~symbol->string () ((_ p) (~and (~test string?) (~prop string->symbol => p))))
(define-match-pattern ~vector () ((_ p ...) (~and (~test vector?) (~prop vector->list => (~list p ...)))))
(define-match-pattern ~string () ((_ p ...) (~and (~test string?) (~prop string->list => (~list p ...)))))
(define-match-pattern ~string-append () ((_ p ...) (~seq-append string? string-length substring "" p ...)))
(define-match-pattern ~string-append/ng () ((_ p ...) (~seq-append/ng string? string-length substring "" p ...)))
(define-match-pattern ~vector-append () ((_ p ...) (~seq-append vector? vector-length vector-copy '#() p ...)))
(define-match-pattern ~vector-append/ng () ((_ p ...) (~seq-append/ng vector? vector-length vector-copy '#() p ...)))
(define-match-pattern ~number->string () ((_ p) (~and (~test string?) (~prop string->number => p)))
((_ p rx) (~and (~test string?) (~prop string->number (rx) => p))))
(define-match-pattern ~string->number () ((_ p) (~and (~test number?) (~prop number->string => p)))
((_ p rx) (~and (~test number?) (~prop number->string (rx) => p))))
;
; 11) additional convenience/compatibility matchers
;
; WCS-like = property matcher
(define-match-pattern ~= ()
((~= f p) (~prop f => p)))
; WCS-like ? predicate matcher
(define-match-pattern ~? ()
((~? f p ...) (~and (~test f) p ...)))
; Racket-like list* (a.k.a. cons*)
(define-match-pattern ~list* ()
((~list* p) p)
((~list* p . p*) (~cons p (~list* . p*))))
; Racket-like list-no-order
(define-syntax match:cno-start
(syntax-rules () ((_ xv try f) (if (pair? xv) (try '() xv) (f)))))
(define-syntax match:cno-head
(syntax-rules () ((_ h t) (cons (car t) (append h (cdr t))))))
(define-syntax match:cno-tail
(syntax-rules () ((_ try f h t) (if (pair? (cdr t)) (try (cons (car t) h) (cdr t)) (f)))))
(define-match-pattern ~cons-no-order ()
((~cons-no-order pe pr)
(~iterate match:cno-start match:cno-head match:cno-tail (h t) (~cons pe pr))))
(define-match-pattern ~list-no-order ()
((~list-no-order) '())
((~list-no-order p) (~list p))
((~list-no-order p . p*) (~cons-no-order p (~list-no-order . p*))))
(define-match-pattern ~list-no-order* ()
((~list-no-order* p) p)
((~list-no-order* p . p*) (~cons-no-order p (~list-no-order* . p*))))
;========================================================================================
;
; Complementary pieces of templating
;
;========================================================================================
; By construction, patterns resemble corresponding Scheme constructors, and can be
; combined in the same way, so regular base Scheme already supplies cons, list, and
; scores of other functions that re-construct what similar-looking patterns de-construct:
;
; pattern: (~list (~cons a b)) template (regular Scheme): (list (cons a b))
;
; This proposal's quasiquote patterns also mirror regular Scheme's quasiquote templates:
;
; pattern: `(,(~cons a b)) template (regular Scheme): `(,(cons a b))
; pattern: `(,@a ,@b) template (regular Scheme): `(,@a ,@b)
;
; This correspondence is not designed to be 1-to-1, because needs of patterns and Scheme
; 'templates', i.e. constructor expressions are different. Still, there are pieces of the
; pattern language that call for equally expressive template forms. In particular, one
; may find it convenient to have a templating counterpart to ~etc patterns.
;
; Unlike patterns though, the choice of regular scheme expressions for templating does
; not allow us to collect what would act as template variables easily -- procedures
; won't cooperate. What we can do is to limit subforms of 'etc' templates, using a
; restricted grammar that makes this possible:
;
; <template expression> -> <value expression> | <etc expression> | <expression>
;
; <value expression> -> (value <expression>)
;
; <etc expression> -> (etc <template>)
;
; <template> ->
; <template var> ; symbol
; | <constant> ; char, string, number, ...
; | (quote <datum>)
; | (value <expression>)
; | (<name> <template> ...)
;
; NB: <name> should be restricted to names of forms that have expressions as subforms;
; this disqualify forms like lambda, let, do, forms with internal definitions etc.
;
; This restricted form allows scanning for template vars due to its limited nature.
; Scanning of <etc expression> should produce one or more template vars, which are
; expected to be lists of the same length at runtime, so the following transformation
; can produce the actual output expression when 'etc' form is macroexpanded:
;
; (etc (list x* y* (value foo))) ==> (map (lambda (x* y*) (list x* y* foo)) x* y*)
;
; Note that (value x) parts, not scanned for template vars, are just replicated.
; In all other respects, value is just the identity macro.
(define-syntax value ; need to be imported together with etc
(syntax-rules () ((_ x) x)))
; scanning for template vars
(define-syntax extract-template-vars
(syntax-rules () ; (_ t rk . a*) => (rk v* . a*)
((_ tpl . cont)
(letrec-syntax
((err
(syntax-rules ()
((_ t) (syntax-error "etc: invalid template" t))))
(ext
(syntax-rules (quote value) ; quasiquote
((_ (quote x) k . a*) (k () . a*))
((_ (value x) k . a*) (k () . a*))
((_ () k . a*) (err ()))
((_ (n) k . a*) (k () . a*))
((_ (n t) k . a*) (ext t k . a*))
((_ (n t . t*) k . a*) (ext t extcdr (n . t*) k . a*))
;((_ #&x k . a*) (err #&x))
((_ #(x (... ...)) k . a*) (err #(x (... ...))))
((_ x k . a*)
(classify x (err x) (err x) (k (x) . a*) (k () . a*)))))
(extcdr
(syntax-rules ()
((_ v* t k . a*) (ext t merge v* k . a*))))
(merge
(syntax-rules ()
((_ () rv* k . a*) (k rv* . a*))
((_ (v . v*) rv* k . a*)
(if-new-var v rv* (merge v* (v . rv*) k . a*) (merge v* rv* k . a*))))))
(ext tpl . cont)))))
; expander for (etc <template>)
(define-syntax etc
(syntax-rules ()
((_ t)
(classify t
(syntax-error "etc: ... used as a template variable")
(syntax-error "etc: _ used as a template variable")
t ; optimization: for template var t, (etc t) ==> t
(extract-template-vars t etc t)))
((_ () t) (syntax-error "etc: no template variables" t))
((_ (v ...) t) (map (lambda (v ...) t) v ...))))
;========================================================================================
;
; Tests (borrowed from many sources)
;
;========================================================================================
; simple test machinery (avp)
(define *errors* 0)
(define (test-matcher matcher t*)
(let ((count 0))
(for-each (lambda (l&r)
(let* ((in (car l&r))
(ex (cadr l&r))
(rv (matcher in)))
(if (equal? rv ex) (begin (display "ok : ") (write in))
(begin (set! count (+ 1 count))
(display "ERROR on ") (write in)
(display ", returned ") (write rv)
(display ", expected ") (write ex)))
(newline)))
t*)
(cond
((zero? count) (display "all tests passed\n"))
(else (set! *errors* (+ *errors* count))
(display (number->string count))
(display " ERRORS\n")))))
(define (test-restart matcher in out)
(let ((v '()))
(matcher in (lambda (x) (display "K: ") (write x) (newline) (set! v (cons x v))))
(set! v (reverse v))
(cond
((equal? out v) (display "ok : ") (write in) (newline))
(else (set! *errors* (+ *errors* 1))
(display "ERROR : ") (write in) (newline)
(display " result ") (write v) (newline)
(display " expect ") (write out) (newline)))))
; tests
; simple matches
(display "\nsimple matcher-1\n")
(define (matcher-1 x)
(match x
(1 'number-one)
('a 'symbol-a)
('(a b) 'list-a-b)
(`(,v q) `(list ,v q))
(`((,x ,y) (,z ,x)) `(head&tail ,x ,y ,z))
(`(+ 0 ,a ,a) `(* 2 ,a))
(`(+ (,f ,@a) (,g ,@a)) `((+ ,f ,g) ,@a))
(`(** ,(~number? a) ,(~number? b)) (expt a b))
(w `(generic ,w))))
(test-matcher matcher-1
'((1 number-one)
(a symbol-a)
(((x y) q) (list (x y) q))
(((a 2) (b a)) (head&tail a 2 b))
((+ 0 (+ y z) (+ y z)) (* 2 (+ y z)))
((+ (sin a b) (cos a b)) ((+ sin cos) a b))
((** 2 4) 16)
((** 2 a) (generic (** 2 a)))))
; rollback to the next rule
(display "\nmatcher-2\n")
(define (matcher-2 x k)
(match x
(`(,@a ,(~symbol? b) ,@c) (=> next) (k `(p1 ,a ,b ,c)) (next))
(`(,@a ,@c ,x) (=> next) (k `(p2 ,a ,c ,x)) (next))
(x (k `(p3 ,x)))))
(test-restart matcher-2
'(1 2 3 a 4 5)
'((p1 (1 2 3) a (4 5))
(p2 (1 2 3 a 4) () 5)
(p3 (1 2 3 a 4 5))))
; rollback to the next match
(display "\nmatcher-3\n")
(define (matcher-3 x k)
(match x
(`(,@a ,b ,@c) (=> next back) (k `(fst ,a ,b ,c)) (back))
(`(,@a ,@c) (=> next back) (k `(snd ,a ,c)) (back))
(`,x (k `(final ,x)))))
(test-restart matcher-3
'(1 2 3 4 5)
'((fst (1 2 3 4) 5 ())
(fst (1 2 3) 4 (5))
(fst (1 2) 3 (4 5))
(fst (1) 2 (3 4 5))
(fst () 1 (2 3 4 5))
(snd (1 2 3 4 5) ())
(snd (1 2 3 4) (5))
(snd (1 2 3) (4 5))
(snd (1 2) (3 4 5))
(snd (1) (2 3 4 5))
(snd () (1 2 3 4 5))
(final (1 2 3 4 5))))
; rollback to the next match, constructor syntax
(display "\nmatcher-4\n")
(define (matcher-4 x k)
(match x
((~append a (~list b) c) (=> next back) (k `(fst ,a ,b ,c)) (back))
((~append a c) (=> next back) (k `(snd ,a ,c)) (back))
(x (k `(final ,x)))))
(test-restart matcher-4
'(1 2 3 4 5)
'((fst (1 2 3 4) 5 ())
(fst (1 2 3) 4 (5))
(fst (1 2) 3 (4 5))
(fst (1) 2 (3 4 5))
(fst () 1 (2 3 4 5))
(snd (1 2 3 4 5) ())
(snd (1 2 3 4) (5))
(snd (1 2 3) (4 5))
(snd (1 2) (3 4 5))
(snd (1) (2 3 4 5))
(snd () (1 2 3 4 5))
(final (1 2 3 4 5))))
; same, but with strings
(display "\nmatcher-5\n")
(define (matcher-5 x k)
(match x
((~string-append a (~string b) c) (=> next back) (k `(fst ,a ,b ,c)) (back))
((~string-append a c) (=> next back) (k `(snd ,a ,c)) (back))
(x (k `(final ,x)))))
(test-restart matcher-5
"12345"
'((fst "1234" #\5 "")
(fst "123" #\4 "5")
(fst "12" #\3 "45")
(fst "1" #\2 "345")
(fst "" #\1 "2345")
(snd "12345" "")
(snd "1234" "5")
(snd "123" "45")
(snd "12" "345")
(snd "1" "2345")
(snd "" "12345")
(final "12345")))
(define (matcher-5ng x k)
(match x
((~string-append/ng a (~string b) c) (=> next back) (k `(fst ,a ,b ,c)) (back))
((~string-append/ng a c) (=> next back) (k `(snd ,a ,c)) (back))
(x (k `(final ,x)))))
(test-restart matcher-5ng
"12345"
'((fst "" #\1 "2345")
(fst "1" #\2 "345")
(fst "12" #\3 "45")
(fst "123" #\4 "5")
(fst "1234" #\5 "")
(snd "" "12345")
(snd "1" "2345")
(snd "12" "345")
(snd "123" "45")
(snd "1234" "5")
(snd "12345" "")
(final "12345")))
(display "\nnonlinear matcher-6\n")