-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsrfi-57-1.1.html
1596 lines (1350 loc) · 61.8 KB
/
srfi-57-1.1.html
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><title>SRFI 57: Records</title>
</HEAD>
<BODY>
<H1>Title</H1>
Records
<H1>Author</H1>
Andre van Tonder
<H1>Status</H1>
This SRFI is currently in ``draft'' status. To see an explanation of each
status that a SRFI can hold, see <A
href="http://srfi.schemers.org/srfi-process.html">here</A>. It will remain
in draft status until 2004/11/08, or as amended. To provide input on this
SRFI, please <CODE><A
See <A
href="../../srfi-list-subscribe.html">instructions
here</A> to subscribe to the list. You can access previous messages via <A
href="http://srfi.schemers.org/srfi-57/mail-archive/maillist.html">the
archive of the mailing list</A>. <P> <UL>
<LI>Received: 2004/09/08
<LI>Draft: 2004/09/08 - 2004/11/08
</LI>
</UL>
<H1>Abstract</H1>
We describe a syntax for defining record types. A predicate, constructor,
and field accessors and modifiers may be specified for each record type.
Records may be constructed positionally or by field labels, and may be
deconstructed by pattern matching. A mechanism for record subtyping is
specified. A simple syntax is provided for both destructive and functional
update, and for composing records.
<H1>Rationale</H1>
The existing SRFI-9 provides a facility for defining record types, along with
a simple set of record operations. While having the advantage of simplicity,
it does not (nor was it meant to) support various common operations and idioms
particularly well, or at all. Such support was left to future extensions.
The current SRFI provides the following extensions:
<ul>
<li>The facility
is provided to construct record values by field labels for
more robust and readable code.
<li>Various mechanisms for record subtyping are described
in the literature and implemented in various languages.
This SRFI provides a simple mechanism for programmer-specified (nominal, as
opposed to structural) record subtyping. A record may have multiple
supertypes.
<li>Functional programming idioms are not supported particularly well by various
current record implementations in Scheme. To remedy this, in addition
to the standard destructive update operations, we
specify a syntax for functional record update (including polymorphic
update, a common awkward case).
<li>We specify a very simple syntax for record composition.
<li>A syntax is specified for deconstructing record values by pattern matching
positionally or by label.
<li>The declaration syntax allows less wordy variations:
In cases where they are not needed, predicates, constructors, field accessors
and modifiers do not have to be declared. In addition,
specifiying the argument order for positional constructors is optional.
</ul>
Further extensions, such as introspection facilities, reading and printing,
are left to future SRFIs.
<h1>Specification</h1>
<h2>Declaration</h2>
The syntax of a record type defintion is as follows (optional elements
enclosed in square brackets):
<pre>
<command or definition>
-> <record type definition> ; addition to 7.1.6 in R5RS
<record type definition> -> (define-record <type clause>
[<constuctor clause>]
(<field spec> ...)
[<predicate name>])
<type clause> -> <type name>
-> (<type name> <supertype name> ...)
<constructor clause> -> <constructor name>
-> (<constructor name> <field label> ...)
<field spec> -> <field label> ; immutable field
-> (<field label> !) ; mutable field
-> (<field label> <accessor name>) ; immutable field with accessor
-> (<field label> <accessor name> <modifier name>) ; mutable field with accessor and modifier
<field label> -> <identifier>
<... name> -> <identifier>
</pre>
An instance of <code>define-record</code> is equivalent to the following:
<ul>
<li> <code><type name></code> is bound to a macro that can be used to construct record
values by label. It is also declares a pattern syntax for
deconstructing record values by label (see below).
<li> If <code><constructor clause></code> is present and
of the form <code>(<constructor name> <field label> ...)</code>,
<code><constructor name></code> is bound to a procedure that takes as many arguments as
there are <code><field label></code>s following it
and returns a new <code><type name></code> record.
Fields whose labels are listed with <code><type name></code> have the corresponding
argument as their initial value. The initial values of all other fields are unspecified.
<br>
If <code><constructor clause></code> is of the form <code><constructor name></code>,
a default (*)
argument arity and order is taken for the procedure <code><constructor name></code>.
<br>
In addition, <code><constructor clause></code> declares a pattern syntax for
deconstructing record values positionally (see below).
<li> <code><predicate name></code>, if present, is bound to a predicate procedure
that returns <code>#t</code> when given a record value that belongs to any subtype of
<code><type name></code>,
and <code>#f</code> for any other value.
<li> Each <code><accessor name></code>, if present, is bound to a procedure that takes a
record belonging to any subtype of <code><type name></code>,
and returns the current value of the corresponding
field. It is an error to pass an accessor a value which is not a record of the appropriate type.
<li> Each <code><modifier name></code>, if present, is bound to
a procedure that takes a record belonging to any subtype of
<code><type name></code>,
and a value which becomes the new value of the corresponding field. The updated record
is returned (note that this differs from tradition, in that a useful value is returned).
It is an error to pass a modifier a first argument which is not a record
of the appropriate type.
</ul>
(*) The default field ordering when no supertypes are present
consists of all fields included in order of declaration.
When supertypes are present, the default field ordering is
obtained by appending the default field ordering of each supertype sequentially
from left to right, followed by the fields of <code><type name></code> in order of
declaration, at
each step dropping any repeated fields.
<p>
Fields already existing in supertypes may be redeclared in the <code><field spec></code> of a
subtype along with
additional accessors or modifiers as desired. It is an error to redeclare an immutable field
as mutable in a subtype. In addition, it is an error if more than one supertype
declares the same field label with different mutability attributes.
<p>
<code>Define-record</code> is generative: each use creates a new record type that is distinct
from all existing types, including
other record types and Scheme's predefined types. Record-type definitions may only occur at top-level.
<h4>Examples</h4>
A simple record:
<pre>
(define-record point
(make-point x y)
((x get-x set-x!)
(y get-y set-y!))
point?)
(define p (make-point 1 2))
(get-y p) ==> 2
(set-y! p 3) ==> (point (x 1) (y 3))
(point? p) ==> #t
</pre>
Subtyping:
<pre>
(define-record color make-color ; default argument order is declaration order
((hue hue set-hue!)) ; notice punning
color?)
(define-record (color-point color point)
(make-color-point x y hue) ; differs from default ordering and arity
((info info)) ; additional field left undefined by constructor
color-point?)
(define cp (make-color-point 1 2 'green))
(color-point? cp) ==> #t
(point? cp) ==> #t
(color? cp) ==> #t
(get-x cp) ==> 1
(hue cp) ==> green
(info cp) ==> <undefined>
</pre>
<h2>Labeled record expressions</h2>
The following syntax allows one to construct a record by label:
<pre>
<expression> -> (<type name> (<field label> <expression>) ...)
</pre>
<h4>Example</h4>
<pre>
(color-point (info 'hi) (x 1) (y 2))
==> (color-point (hue <undefined>) (x 1) (y 2) (info hi))
</pre>
<h2>Record update</h2>
The following syntax allows different forms of record update:
<pre>
<expression> -> (update <type name> <record> (<field label> <expression>) ...)
-> (update* <type name> <record> (<field label> <expression>) ...)
-> (update! <type name> <record> (<field label> <expression>) ...)
</pre>
The first alternative is used for polymorphic functional record update. The expression
<code><record></code> must evaluate to a record value that belongs to a subtype of
<code><type name></code>. The result will be a new record value of the same type as
the original <code><record></code>, with the given fields updated. The original
record value is unaffected.
<p>
The second alternative is used for monomorphic functional record update. The expression
<code><record></code> must evaluate to a record value that belongs to a subtype of
<code><type name></code>. The result will be a new record value of type
<code><type name></code>, with the given fields updated. The original
record value is unaffected.
<p>
The third alternative is used for in-place record update. The expression
<code><record></code> must evaluate to a record value that belongs to a subtype of
<code><type name></code>. The result will be the original record value
<code><type name></code>, with the given fields, which must be mutable,
updated in place. It is an error to attempt to <code>update!</code> immutable fields.
Note that a useful value is returned.
<h4>Examples</h4>
<pre>
(define-record point2 ((x !) (y !)))
(define-record (point3 point2) ((x !) (y !) (z !)))
(define p (point3 (x 1) (y 1) (z 3)))
(update point2 p (y 5))) ==> (point3 (x 1) (y 5) (z 3)) -- polymorphic update
p ==> (point3 (x 1) (y 1) (z 3)) -- original unaffected
(update* point2 p (y 5))) ==> (point2 (x 1) (y 5)) -- monomorphic update
p ==> (point3 (x 1) (y 1) (z 3)) -- original unaffected
(update! point2 p (y 5))) ==> (point3 (x 1) (y 5) (z 3)) -- destructive update
p ==> (point3 (x 1) (y 5) (z 3)) -- original updated
</pre>
<h2>Record extension</h2>
The following syntax provides a shorthand for composing record values:
<pre>
<expression> -> (extend ((<import-type name> <record>) ...)
(<export-type name> (<field label> <expression>) ...)
</pre>
Here each expression <code><record></code> must evaluate to a record value belonging to
a subtype of
<code><import-type name></code>. The exported record type
<code><export-type name></code> must be a subtype of all the import types. The expression
evaluates to a record value of type <code><export-type name></code> whose fields are
populated as follows: All fields of the imported record values are imported from left to
right, dropping repeated fields. The additional fields <code><field label></code>
are then populated with the corresponding <code><expression></code>.
<h4>Example</h4>
<pre>
(define c (color (hue 'green)))
(define p (point (x 1) (y 2)))
(extend ((point p)
(color c))
(color-point (x 5)
(info 'hi))) ==> (color-point (hue green) (x 5) (y 2) (info hi))
</pre>
<h2>Pattern matching</h2>
This section specifies the following basic syntax for deconstructing records via pattern matching.
<pre>
<expression> -> (match <expression> <clause> ...)
-> (match-let ((<pattern> <expression>) ...)
<body>)
-> (match-let* ((<pattern> <expression>) ...)
<body>)
<clause> -> (<pattern> <body>)
-> (<pattern> (=> <identifier>) <body>)
<pattern> -> <identifier> ; anything, binding <identifier>
-> _ ; anything
-> <literal> ; an equal? expression (literals include quoted s-expressions)
-> (<type name> (<field label> <pattern>) ...) ; labeled record pattern
-> (<constructor name> <pattern> ...) ; positional record pattern
</pre>
The pattern <code>(<type name> (<field label> <pattern>) ...)</code>
will successfully match a value belonging to any subtype of <code><type name></code>
as long as the value of each field indexed by <code><field label></code> matches
the corresponding <code><pattern></code>.
<p>
The pattern <code>(<constructor name> <pattern> ...)</code>
will successfully match a value belonging to any subtype of the corresponding record type
as long as the value of each field, in the order and arity they were declared for the
constructor, matches the corresponding <code><pattern></code>. It is an
error if the number of patterns is different from the arity of the constructor.
<p>
The variation <code>(<pattern> (=> <identifier>) <body>)</code> of
<code><clause></code> will bind <code><identifier></code> to a 0-arity procedure
in the region <code><body></code> that can be invoked to continue the matching starting at the next
branch.
<h4>Examples</h4>
<pre>
(match (make-point 1 2)
((make-point x y) (list x y))) ==> (1 2)
(match-let (((make-point x y) (make-point 1 2)))
(list x y)) ==> (1 2)
(match (point (x 4))
((point (x a) (y b)) (list a b))) ==> (4 <undefined>)
(match (make-color-point 1 2 'green)
((make-point x y)
(list x y))) ==> (1 2)
(match (color-point (x 1) (y 2) (info 'hi))
((point (x a) (y b))
(list a b))) ==> (1 2)
(define-record plus make-plus (left right))
(define-record num make-num (value))
(define (evaluate expr)
(match expr
((make-num v) v)
((make-plus l r) (+ (evaluate l)
(evaluate r)))))
(evaluate (make-plus (make-num 3)
(make-plus (make-num 4)
(make-num 5)))) ==> 12
</pre>
<p>
While it is outside the scope of the current SRFI to specify a full-featured pattern matching
facility, the reference implementation provides the following additional patterns and
is easily extensible. The overall philosophy in the choice of syntax here and above
was that patterns should be mirror images of the
correponding constructors as far as possible.
<pre>
<pattern> -> (cons <pattern1> <pattern2>) ; a pair
-> (list <pattern1> ... <patternk>) ; a list of k elements
-> (list* <pattern1> ... <patternk> <pattern*>) ; a list of k or more elements, matching <pattern*> against the tail
-> (vector <pattern1> ... <patternk>) ; a vector of k elements
-> (and <pattern> ...) ; if all patterns match
-> (or <pattern> ...) ; if any of the patterns match
-> (= <procedure> <pattern>) ; if (<procedure> <expression>) matches <pattern>
-> (? <predicate?> <pattern> ...) ; if (<predicate?> <expression>) is not #f and all <pattern>s match
-> `<quasipattern> ; a quasipattern
<quasipattern> -> <self-evaluating> ; an equal? expression
-> <identifier> ; an equal? symbol
-> () ; the empty list
-> (<quasipattern> . <quasipattern>) ; a pair
-> #(<quasipattern1> ... <quasipatternk>) ; a vector of k elements
-> ,<pattern> ; a pattern
</pre>
<h4>Further examples</h4>
<pre>
(match 1
(1 (=> next) (next))
(_ 2)) ==> 2
(match (cons 1 2)
((cons a b) (list a b))) ==> (1 2)
(match `(if #f 0 1)
(`(if #t ,a ,b) a)
(`(if #f ,a ,b) b)) ==> 1
(match '(1 2 3 4)
((list* a b (cons c d))
(values a b c d))) ==> 1 2 3 (4)
(match (cons 1 2)
((and (cons x y) z) (values x y z))) ==> 1 2 (1 . 2)
(match (cons 1 2)
((= car x) x)) ==> 1
(match (list (make-point 1 2) (make-point 3 4))
((list (make-point a b) (make-point c d)) (list a b c d))) ==> (1 2 3 4)
(match `(,(make-point 1 2) ,(make-point 3 4))
(`(,(make-point a b) ,(make-point c d)) (list a b c d))) ==> (1 2 3 4)
(match 1
((? symbol? x) (list 'symbol x))
((? number? x) (list 'number x))) ==> (number 1)
(define (map f lst)
(match lst
('() '())
((cons h t) (cons (f h) (map f t)))))
</pre>
<h2>Reflection</h2>
It is outside the scope of the current SRFI to specify an exhaustive reflection
mechanism. We limit ourselves to providing the following procedure:
<pre>
(record->sexp <record>)
</pre>
This procedure takes as argument any record value and returns an s-expression
of the shape
<pre>
`(<type name> (<field label> ,<value>) ...)
</pre>
where the fields are listed in the default order.
<h4>Example</h4>
<pre>
(record->sexp (make-point 1 2)) ==> (point (x 1) (y 2))
</pre>
<H1>Implementation</H1>
The reference implementation uses the macro mechanism of
R5RS. It does not use any other SRFI or any library.
<p>
The SRFI specification was designed with the constraint that
all record expressions containing field labels be translatable into positional
expressions at macro-expansion time. For example, labeled record expressions
and patterns should be just as efficient as positional constructors and
patterns. This is true for the reference implementation.
<p>
To stay within the constraints of portability, the reference implementation
deviates slightly from the specification in
that the positional constructor is defined as a macro instead
of a first-order procedure.
If you have syntax-case, uncomment the indicated lines in <code>emit-record</code>
to make the constructor behave as if it were declared as a
first-order procedure.
<p>
In another deviation from the specification,
record types defined by the reference implementation are not disjoint from other
types. Since various Schemes have their own ways of defining unique tags/types,
it is left to implementors to choose the best way of achieving generativity.
<p>
Only the names in the exports section should be visible to the user. The other
name should be hidden by a suitable module system.
<p>
The last section contains a few examples and (non-exhaustive) tests.
<p>
A undocumented facility called <code>define-view</code> is provided for extending the
pattern matcher. See the included examples of use in the code.
<H2>Reference implementation </H2>
<PRE>
;==============================================================================
; IMPLEMENTATION:
;
; Andre van Tonder 2004.
;
; Records are implemented as closures that inject their fields into
; a continuation (see build-maker). This gives positional pattern matching
; essentially for free. Constructors and matchers by labels are
; compiled into positional constructors and matchers at expansion time.
; Mutable fields are represented by boxed values.
;==============================================================================
; Exports:
(define-syntax define-record
(syntax-rules ()
((define-record (name super ...) (make-name label ...) (field ...) name?)
(build-record name (super ...) (make-name label ...) name? (field ...)))
((define-record (name super ...) (make-name label ...) (field ...))
(define-record (name super ...) (make-name label ...) (field ...) "placeholder"))
((define-record (name super ...) (field ...))
(define-record (name super ...) ("placeholder" #f) (field ...) "placeholder"))
((define-record (name super ...) (field ...) name?)
(define-record (name super ...) ("placeholder" #f) (field ...) name?))
((define-record (name super ...) make-name . rest)
(define-record (name super ...) (make-name #f) . rest))
((define-record name . rest)
(define-record (name) . rest))))
(define (record? x)
(and (pair? x)
(eq? (car x) <record>)))
(define (record->sexp r)
((cadddr r)))
(define-syntax update
(syntax-rules ()
((update name record (label binding) ...)
(name ("labels")
(update-help record (name (label binding) ...))))))
(define-syntax update*
(syntax-rules ()
((update* name val (label exp) ...)
(name ("labels")
(update*-help name val ((label . exp) ...))))))
(define-syntax update!
(syntax-rules ()
((update! name val (label exp) ...)
(add-temporaries ((label exp) ...)
(update!-help val name)))))
(define-syntax extend
(syntax-rules ()
((extend () (export-name (label exp) ...))
(export-name (label exp) ...))
((extend ((name val) . imports) export)
(name ("labels")
(extend-help (export name val imports))))))
(define-syntax match
(syntax-rules ()
((match exp . rest)
(if-symbol? exp
(match-var exp . rest)
(let ((var exp))
(match-var var . rest))))))
(define-syntax match-let
(syntax-rules ()
((match-let bindings . body)
(add-temporaries bindings
(match-let-emit body)))))
(define-syntax match-let*
(syntax-rules ()
((match-let* () . body)
(begin . body))
((match-let* (binding . bindings) . body)
(match-let (binding)
(match-let* bindings . body)))))
(define-syntax define-view
(syntax-rules (match)
((define-view name (lit ...)
(match var sk fk
((name* . pat) template)
...)
. rest)
(define-syntax name
(syntax-rules (lit ...)
((name ("match") var pat sk fk) template)
...
. rest)))))
;====================================================================
; Internal syntax utilities:
(define-syntax syntax-error (syntax-rules ()))
(define-syntax syntax-apply
(syntax-rules ()
((syntax-apply (f . args) exp ...)
(f exp ... . args))))
(define-syntax if-free=
(syntax-rules ()
((if-free= x y kt kf)
(let-syntax
((test (syntax-rules (x)
((test x kt* kf*) kt*)
((test z kt* kf*) kf*))))
(test y kt kf)))))
(define-syntax if-symbol?
(syntax-rules ()
((if-symbol? (x . y) sk fk) fk)
((if-symbol? #(x ...)) fk)
((if-symbol? x sk fk)
(let-syntax ((test (syntax-rules ()
((test x sk* fk*) sk*)
((test non-x sk* fk*) fk*))))
(test foo sk fk)))))
(define-syntax syntax-cons
(syntax-rules ()
((syntax-cons x rest k) (syntax-apply k (x . rest)))))
(define-syntax syntax-cons-after
(syntax-rules ()
((syntax-cons-after rest x k) (syntax-apply k (x . rest)))))
(define-syntax syntax-car
(syntax-rules ()
((syntax-car (h . t) k) (syntax-apply k h))))
(define-syntax syntax-foldl
(syntax-rules ()
((syntax-foldl accum (f arg ...) () k)
(syntax-apply k accum))
((syntax-foldl accum (f arg ...) (h . t) k)
(f accum h arg ...
(syntax-foldl (f arg ...) t k)))))
(define-syntax syntax-foldr
(syntax-rules ()
((syntax-foldr accum (f arg ...) () k)
(syntax-apply k accum))
((syntax-foldr accum (f arg ...) (h . t) k)
(syntax-foldr accum (f arg ...) t
(f h arg ... k)))))
(define-syntax syntax-append
(syntax-rules ()
((syntax-append () y k) (syntax-apply k y))
((syntax-append (h . t) y k) (syntax-append t y
(syntax-cons-after h k)))))
(define-syntax syntax-map
(syntax-rules ()
((syntax-map () (f arg ...) k) (syntax-apply k ()))
((syntax-map (h . t) (f arg ...) k) (syntax-map t (f arg ...)
(syntax-map (f arg ...) h k)))
((syntax-map done (f arg ...) h k) (f h arg ...
(syntax-cons done k)))))
(define-syntax syntax-filter
(syntax-rules ()
((syntax-filter () (if-p? arg ...) k)
(syntax-apply k ()))
((syntax-filter (h . t) (if-p? arg ...) k)
(if-p? h arg ...
(syntax-filter t (if-p? arg ...) (syntax-cons-after h k))
(syntax-filter t (if-p? arg ...) k)))))
(define-syntax syntax-reverse
(syntax-rules ()
((syntax-reverse lst k) (syntax-reverse lst () k))
((syntax-reverse () acc k) (syntax-apply k acc))
((syntax-reverse (h . t) acc k) (syntax-reverse t (h . acc) k))))
(define-syntax syntax-zip
(syntax-rules ()
((syntax-zip lst lst* sk fk) (syntax-zip () lst lst* sk fk))
((syntax-zip accum () () sk fk) (syntax-reverse accum sk))
((syntax-zip accum () lst* sk fk) fk)
((syntax-zip accum lst () sk fk) fk)
((syntax-zip accum (h . t) (h* . t*) sk fk) (syntax-zip ((h . h*) . accum) t t* sk fk))))
(define-syntax add-temporaries
(syntax-rules ()
((add-temporaries lst k) (add-temporaries lst () k))
((add-temporaries () temps k) (syntax-reverse temps k))
((add-temporaries (h . t) temps k) (add-temporaries t ((h temp) . temps) k))))
(define-syntax push-result
(syntax-rules ()
((push-result x stack k) (syntax-apply k (x . stack)))))
;=============================================================================
; Internal match utilities:
(define-syntax match-var
(syntax-rules (_ => quote cons vector list list* and or = ? quasiquote unquote)
((match-var var)
(error "No match for" (if (record? var)
(record->sexp var)
var)))
((match-var var (pattern (=> fail) . body) . rest)
(let ((fail (lambda ()
(match-var var . rest))))
(match-var var (pattern . body) . rest)))
((match-var var (_ . body) . rest)
(begin . body))
((match-var var ((quote x) . body) . rest)
(if (equal? (quote x) var)
(begin . body)
(match-var var . rest)))
((match-var var ((quasiquote ()) . body) . rest)
(if (null? var)
(begin . body)
(match-var var . rest)))
((match-var var ((quasiquote #(pat ...)) . body) . rest)
(if (vector? var)
(match-var (vector->list var)
((list (quasiquote pat) ...) . body) . rest)
(match-var var . rest)))
((match-var var ((quasiquote (unquote pat)) . body) . rest)
(match-var var
(pat . body)
(_ (match-var var . rest))))
((match-var var ((quasiquote (pat . pats)) . body) . rest)
(let ((fail (lambda () (match-var var . rest))))
(if (pair? var)
(match (car var)
((quasiquote pat) (match (cdr var)
((quasiquote pats) . body)
(_ (fail))))
(_ (fail)))
(fail))))
((match-var var ((quasiquote pat) . body) . rest)
(match-var var
((quote pat) . body)
(_ (match-var var . rest))))
((match-var var ((cons pat1 pat2) . body) . rest)
(let ((fail (lambda () (match-var var . rest))))
(if (pair? var)
(match (car var)
(pat1 (match (cdr var)
(pat2 . body)
(_ (fail))))
(_ (fail)))
(fail))))
((match-var var ((vector pat ...) . body) . rest)
(if (vector? var)
(match-var (vector->list var)
((list pat ...) . body) . rest)
(match-var var . rest)))
((match-var var ((list) . body) . rest)
(if (null? var)
(begin . body)
(match-var var . rest)))
((match-var var ((list pat . pats) . body) . rest)
(let ((fail (lambda () (match-var var . rest))))
(if (pair? var)
(match (car var)
(pat (match (cdr var)
((list . pats) . body)
(_ (fail))))
(_ (fail)))
(fail))))
((match-var var ((list* pat) . body) . rest)
(match var
(pat . body)
(_ (match-var var . rest))))
((match-var var ((list* pat . pats) . body) . rest)
(let ((fail (lambda () (match-var var . rest))))
(if (pair? var)
(match (car var)
(pat (match (cdr var)
((list* . pats) . body)
(_ (fail))))
(_ (fail)))
(fail))))
((match-var var ((and) . body) . rest)
(begin . body))
((match-var var ((and pat . pats) . body) . rest)
(let ((fail (lambda () (match-var var . rest))))
(match-var var
(pat (match-var var
((and . pats) . body)
(_ (fail))))
(_ (fail)))))
((match-var var ((or) . body) . rest)
(match-var var . rest))
((match-var var ((or pat . pats) . body) . rest)
(match-var var
(pat . body)
(_ (match-var var
((or . pats) . body)
(_ (match-var var . rest))))))
((match-var var ((= f pat) . body) . rest)
(match (f var)
(pat . body)
(_ (match-var var . rest))))
((match-var var ((? pred? pat ...) . body) . rest)
(let ((fail (lambda () (match-var var . rest))))
(if (pred? var)
(match-var var
((and pat ...) . body)
(_ (fail)))
(fail))))
((match-var var ((name . pattern) . body) . rest)
(name ("match") var pattern (begin . body) (match-var var . rest)))
((match-var var (x . body) . rest)
(if-symbol? x
(let ((x var)) . body)
(if (eqv? x var)
(begin . body)
(match-var var . rest))))))
(define-syntax match-let-emit
(syntax-rules ()
((match-let-emit (((pat exp) temp) ...) body)
(let ((temp exp) ...)
(match-let-vars ((pat temp) ...) . body)))))
(define-syntax match-let-vars
(syntax-rules ()
((match-let-vars () . body)
(begin . body))
((match-let-vars ((pat var) . bindings) . body)
(match-var var
(pat (match-let-vars bindings . body))
(_ (error "Match-let pattern" 'pat
"does not match value" (if (record? var)
(record->sexp var)
var)))))))
;==================================================================================
; Internal record utilities:
(define <record> '<record>)
(define <undefined> '<undefined>)
(define-syntax build-record
(syntax-rules ()
((build-record name supers constructor name? fields)
(get-immutable () fields ; -> (immutables . stack) where stack = ()
(get-mutable fields ; -> (mutables immutables . stack)
(get-interfaces supers ; -> (interfaces mutables immutables . stack)
(union-mutables ; -> (mut-labels interfaces mutables immutables . stack)
(extract-labels fields ; -> (labels mut-labels interfaces mutables immutables . stack)
(union-labels ; -> (labels mut-labels interfaces mutables immutables . stack)
(emit-record name constructor name?))))))))))
(define-syntax emit-record
(syntax-rules ()
((emit-record (labels . stack) name (make-name #f) name?)
(emit-record (labels . stack) name (make-name . labels) name?))
((emit-record (labels . stack) name (make-name pos-label ...) name?)
(add-temporaries labels
(emit-record name (make-name pos-label ...) name? stack)))
((emit-record ((label temp) ...) name (make-name pos-label ...) name? stack)
(add-temporaries (pos-label ...)
(emit-record ((label temp) ...) (label ...) (temp ...) name make-name name? stack)))
((emit-record ((pos-label pos-temp) ...) ((label temp) ...) labels temps
name make-name name? ((mut-label ...)
((super super-label ...) ...)
((label** get-label** set-label!**) ...)
((label* get-label*) ...) . stack))
(begin
(define-syntax name
(syntax-rules (name label ... super ...)
((name ("info") k)
(syntax-apply k (make-name ((super super-label ...) ...) label ...)))
((name ("labels") k) (syntax-apply k labels))
((name ("pos-labels") k) (syntax-apply k (pos-label ...)))
((name ("has") label sk fk) sk)
...
((name ("has") other sk fk) fk)
((name ("project") temps label k) (syntax-apply k temp))
...
((name ("project") temps other k) (syntax-error "Attempt to look up illegal label"
other "in record type" name))
((name ("project*") temps label) temp)
...
((name ("project*") temps other) (syntax-error "Attempt to look up illegal label"
other "in record type" name))
((name ("mutable?") mut-label sk fk) sk)
...
((name ("mutable?") other sk fk) fk)
((name ("=") name sk fk) sk)
((name ("=") other sk fk) fk)
((name ("match") val bindings sk fk)
(match-record val (name . bindings) labels (pos-label ...) sk fk))
((name ("make"))
(build-maker name (label ...) (mut-label ...) ((super super-label ...) ...)))
((name ("construct"))
(lambda (pos-temp ...) (name (pos-label pos-temp) ...)))
((name ("?"))
(lambda (val)
(and (record? val)
((cadr val) 'name
(lambda ignore #t)
(lambda () #f)))))
((name ("select") mut-label) ; mutable fields shadow alternative below
(build-mut-selector name labels mut-label))
...
((name ("select") label)
(lambda (record)
((cadr record) 'name
(lambda labels label)
(lambda () (error "Record" (record->sexp record)
"does not support selector for"
'(name label))))))
...
((name ("set!") mut-label)
(build-mutator name labels mut-label))
...
((name ("set!") other)
(syntax-error "Field" other "is not mutable in record type" name))
((name ("pos") . exps) (make-record name make-name labels ("pos") . exps))
((name binding . bindings) (make-record name make-name labels binding . bindings))
((name) (make-record name make-name labels))))
(define-syntax-present make-name
(syntax-rules ()
((make-name ("match") val patterns sk fk)
(name ("match") val (("pos") . patterns) sk fk))
((make-name . args)
((name ("construct")) . args))))
; The above definition deviates from the specification in
; that the positional constructor is defined as a macro instead
; of a first-order procedure.
; If you have syntax-case, uncomment the following to make
; the constructor behave as if it were declared as a
; first-order procedure:
; (define-syntax-present make-name
; (lambda (stx)
; (syntax-case stx ()
; ((make-name ("match") val patterns sk fk)
; (syntax (name ("match") val (("pos") . patterns) sk fk)))
; ((make-name . args)
; (syntax ((name ("construct")) . args)))
; (make-name
; (syntax (name ("construct")))))))
(define-present name? (name ("?")))
(define get-label* (name ("select") label*))
...
(define-present get-label** (name ("select") label**))
...
(define-present set-label!** (name ("set!") label**))
...
(newline)
(display "Record: ") (display 'name) (newline)
(display "Labels: ") (display 'labels) (newline)
(display "Constr: ") (display '(pos-label ...)) (newline)
(display "Supers: ")
(for-each (lambda (int) (display int) (newline)
(display " "))
(reverse '((super super-label ...) ...)))
(newline)))))
; In the following, we cannot simply use (label ...), (mut-label ...)
; and (super-label ...) as bound variables because equivalent labels in the
; three sets may have different colors. So generate some temporaries and
; project the required positions out of these.
(define-syntax build-maker
(syntax-rules ()
((build-maker name labels mut-labels interfaces)
(add-temporaries labels
(build-maker name labels mut-labels interfaces)))
((build-maker ((label temp) ...) name labels mut-labels interfaces)
(syntax-map mut-labels (project name (temp ...))
(build-maker (temp ...) (label ...) ((label temp) ...)
name labels mut-labels interfaces)))
((build-maker (mut-temp ...) temps (label ...) labels-temps
name labels mut-labels interfaces)
(build-maker (mut-temp ...) (mut-temp ...) temps (label ...) labels-temps
name labels mut-labels interfaces))
((build-maker mut-temps (mut-temp ...) temps (label ...) ((label* temp*) ...)
name labels mut-labels ((super super-label ...) ...))
(letrec ((make-name
(lambda temps
(let ((mut-temp (box mut-temp))
...)
(list <record>
(lambda (interface sk fk)
(case interface
((name) (sk (name ("project*") temps label) ...))
((super) (sk (name ("project*") temps super-label) ...))
...
(else (fk))))
(lambda (super-tag fields-k) ; for polymorphic update
(case super-tag
((name) (fields-k (lambda labels
(make-name . labels))))