-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsrfi-57-1.3.html
1964 lines (1641 loc) · 74.7 KB
/
srfi-57-1.3.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>
André 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.
A mechanism for record subtyping is
specified. A syntax is provided for both destructive and functional
update, and for composing records.
<H1>Rationale</H1>
The existing SRFI-9 [1] provides a facility for defining record types.
It is simple and clean, but does
not support various common record operations and
programming idioms
particularly well. Such support was left to extensions such as
the current SRFI, which provides
<ul>
<li>The ability to construct record values by field labels, useful for
writing robust and readable code.
</li><li>A mechanism for record subtyping. Record types may have multiple supertypes.
</li><li>Facilities for functional record update, including polymorphic
update, as well as a linear version of destructive update. These
make functional programming with records easier.
</li><li>A mechanism for composing records.
</li><li>A declaration syntax that allows less wordy variations and
optional elements.
</li></ul>
For efficiency, the SRFI specification was designed with the constraint that
all record expressions containing field labels be translatable into positional
expressions at macro-expansion time.
<h1>Specification</h1>
<h2>Declaration</h2>
The syntax of a record type definition is as follows:
<pre>
<command or definition>
-> <record type definition> ; addition to 7.1.6 in R5RS
<record type definition> -> (define-record-type <type clause>
<constructor clause>
<predicate clause>
<field clause> ...)
-> (define-record-type <type clause>
<constructor clause>)
-> (define-record-type <type clause>)
<type clause> -> <type name>
-> (<type name> <supertype name> ...)
<constructor clause> -> (<constructor name> <field label> ...)
-> <constructor name>
-> #f
<predicate clause> -> <predicate name>
-> #f
<field clause> -> (<field label> <accessor clause> <modifier clause>)
-> (<field label> <accessor clause>)
<accessor clause> -> <accessor name>
-> #f
<modifier clause> -> <modifier name>
-> #f
<field label> -> <identifier>
<... name> -> <identifier>
</pre>
An instance of <code>define-record-type</code> is equivalent to the following:
<ul>
<li> A default order of field labels is associated with the record type <code><type name></code>,
obtained by appending the default order of each declared supertype
from left to right, followed by all field labels in order of first appearance in either the
<code><constructor clause></code> or the <code><field clause></code>s of
the <code><record type definition></code>, while dropping any repeated labels.
<p>
</p></li><li> <code><type name></code> is bound to a macro, described below, that can be used to construct record
values by label.
<p>
</p></li><li> If <code><constructor clause></code> is
of the form <code>(<constructor name> <field label> ...)</code>, then
<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.
<p>
If <code><constructor clause></code> is of the form <code><constructor name></code>,
the procedure
<code><constructor name></code> takes as many arguments as there are field labels
associated with <code><type name></code>, in the default order as defined above.
</p><p>
</p></li><li> <code><predicate name></code>, 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.
<p>
</p></li><li> Fields appearing in the constructor do not have to be repeated in the
<code><field clause></code>s. Fields that are not further specified in
a <code><field clause></code> of the record type or a
supertype are taken as immutable. Where present, <code><field
clause></code>s may provide additional information
on fields already listed in the constructor, or may
declare additional fields not appearing in the constructor.
<p>
A field clause of the form
<code>(<field label> <accessor clause> <modifier
clause>)</code>
declares a mutable field, while a field clause of the form
<code>(<field label> <accessor clause>)</code>
declares an immutable field.
</p><p>
</p><p>
A <code><field label></code> already declared in a supertype may
be listed again in a <code><field clause></code> of a
subtype, along with additional accessors or modifiers as desired. It
is an error for a subtype declaration to modify the mutability
attribute of an
inherited field. In addition, it is an error if more than one supertype
of a record type declares the same field label with different mutability attributes.
</p><p>
Field labels may be reused as the name of accessors or modifiers (a practice known
as punning).
</p><p>
</p></li><li> Each <code><accessor name></code> 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.
<p>
</p></li><li> Each <code><modifier name></code> 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 SRFI-9, in that we
require that a useful value be returned).
It is an error to pass a modifier a first argument which is not a record
of the appropriate type.
<p>
</p></li></ul>
<p>
<code>define-record-type</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.
</p><h4>A note on subtyping and redefinition</h4>
In the following example, two supertypes define different accessors for the same field:
<pre> (define-record-type foo make-foo foo? (x foo-x))
(define-record-type bar make-bar bar? (x bar-x))
(define-record-type (foo-bar foo bar) (x foo-bar-x))
</pre>
Since any instance <code>fb</code> of <code>foo-bar</code> is an instance of both
<code>foo</code> and <code>bar</code>, both <code>foo-x</code> and <code>bar-x</code>
will work on <code>fb</code>, returning the <code>x</code> field.
The accessor <code>foo-bar-x</code> will return the <code>x</code>
field of any instances of <code>foo-bar</code> or further subtypes of <code>foo-bar</code>
<p>
In the following example, two record types define
the same accessor:
</p><pre> (define-record-type foo make-foo foo? (x foo-x))
(define-record-type (bar foo) make-bar bar? (x foo-x))
</pre>
As in any <code>define-...</code> form, later bindings replace earlier bindings.
After the second declaration is executed,
<code>foo-x</code> will now work on instances of <code>bar</code> and not any
longer on instances of the supertype <code>foo</code> that are not also instances of <code>bar</code>.
<h3>Examples</h3>
<h4>A simple record:</h4>
<pre> (define-record-type point (make-point x y) point?
(x get-x set-x!)
(y get-y set-y!))
(define p (make-point 1 2))
(get-y p) ==> 2
(set-y! p 3)) ==> (point (x 1) (y 3))
(point? p) ==> #t
</pre>
Note that the setter returns the updated record.
<h4>Optional elements:</h4>
Elements may be left out if not desired, as the following examples illustrate:
<pre>
(define-record-type node (make-node left right))
(define-record-type leaf (make-leaf value))
</pre>
In these declarations, no predicates are bound. Also note that field labels listed in the
constructor do not have to be repeated in the field clause list unless we want them to be
mutable or we want to bind getters or setters.
<pre>
(define-record-type monday)
(define-record-type tuesday #f tuesday?)
</pre>
Here <code>monday</code> has no declared constructor or predicate, while <code>tuesday</code>
has a predicate but no constructor.
<pre> (define-record-type node make-node #f
(left left #f)
(right right #f))
</pre>
Here the constructor <code>make-node</code> has the default argument order and no predicate
is bound. Fields are mutable, but no setters are bound. Also note that field labels are
punned.
<h4>Subtyping:</h4>
<pre> (define-record-type color make-color color? ; make-color takes default argument order
(hue hue set-hue!)) ; punning of field labels is allowed
(define-record-type (color-point color point) ; more than one supertype
(make-color-point x y hue)
color-point?
(info info)) ; field left undefined by constructor
(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 labels. All the
<code><field label></code>s have to belong to the record type <code><type name></code>.
If this condition is not satisfied, an expansion time error must be signaled.
<pre> <expression> -> (<type name> (<field label> <expression>) ...)
</pre>
<h4>Rationale</h4>
The traditional practice of instantiating record values with a positional constructor procedure
can lead to code that is hard to read and fragile under common operations such as
adding, removing, or rearranging field declarations. The ability to populate record values
by labels provides a more robust and readable alternative, especially useful when a record has
more than two or three fields, or if it has supertypes. Field labels are checked for validity
and the macro is compiled to a positional constructor at expansion time,
thus eliminating a large class of potential programmer errors at no cost in efficiency.
<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> -> (record-update <record> <type name> (<field label> <expression>) ...)
-> (record-update* <record> <type name> (<field label> <expression>) ...)
-> (record-update! <record> <type name> (<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. All the
<code><field label></code>s have to belong to the record type <code><type name></code>.
If this condition is not satisfied, an expansion time error must be signaled.
<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. All the
<code><field label></code>s have to belong to the record type <code><type name></code>.
If this condition is not satisfied, an expansion time error must be signaled.
</p><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.
Note that a useful value is returned. All the
<code><field label></code>s have to belong to the record type <code><type name></code>
and, in addition, be mutable.
If this condition is not satisfied, an expansion time error must be signaled.
</p><h4>Rationale</h4>
In the presence of subtyping, polymorphic record update is not reducible
to the other operations we have listed and therefore has to be
provided as a built-in primitive [2].
<p>
Apart from this, a mechanism for functional update facilitates and encourages functional-style programming
with records. It is particularly useful when we are updating only a
few fields of a large record.
<p> A rationale for the naming convention is as follows: the shortest name is
given to the more general polymorphic <code>update</code> form, which is the safest in that it
can be used in most
instances where the monomorphic <code>update*</code> would suffice, and the
former can indeed be replaced by the latter for efficiency when the programmer knows
that the situation requires only monomorphic update. A destructive linear version
<code>update!</code> is provided especially for cases where the programmer
knows that no other references to a value exist, to produce what is, observationally, a
pure-functional result. In these cases, an <code>update</code> or
<code>update*</code> operation may be replaced by <code>update!</code> for efficiency.
See SRFI-1 for a good discussion of linear update procedures.
</p><h4>Examples</h4>
<pre> (define-record-type point2 #f #f (x) (y))
(define-record-type (point3 point2) #f #f (x) (y) (z))
(define p (point3 (x 1) (y 1) (z 3)))
(record-update p point2 (y 5)) ==> (point3 (x 1) (y 5) (z 3)) -- polymorphic update
p ==> (point3 (x 1) (y 1) (z 3)) -- original unaffected
(record-update* p point2 (y 5)) ==> (point2 (x 1) (y 5)) -- monomorphic update
p ==> (point3 (x 1) (y 1) (z 3)) -- original unaffected
(record-update! p point2 (y 5)) ==> (point3 (x 1) (y 5) (z 3)) -- destructive update
p ==> (point3 (x 1) (y 5) (z 3)) -- original updated
</pre>
<h2>Record composition</h2>
The following syntax provides a shorthand for composing record values:
<pre> <expression> -> (record-compose ((<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 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 that also belong to the type
<code><export type name></code> by comparing labels are imported from left to
right, dropping any repeated fields. The additional fields <code><field label></code>
are then populated with the corresponding <code><expression></code>, overwriting
any fields with the same labels already imported.
All the
<code><field label></code>s have to belong to the record type <code><export type name></code>.
If this condition is not satisfied, an expansion time error must be signaled.
<p>
The exported record type
<code><export-type name></code> does not have to be a subtype of all the import types.
<h4>Rationale</h4>
Calculi for composing record values, such as the above scheme,
may be used, for example, as "units" are used in
PLT Scheme, or for writing what amounts to modules and functors in the sense of ML.
<h4>Examples</h4>
<pre> (define c (color (hue 'green)))
(define p (point (x 1) (y 2)))
(record-compose ((point p)
(color c))
(color-point (x 5)
(info 'hi))) ==> (color-point (hue green) (x 5) (y 2) (info hi))
; Another record composition example:
(define-record-type monoid #f #f
(mult monoid.mult)
(one monoid.one))
(define-record-type abelian-group #f #f
(add group.add)
(zero group.zero)
(sub group.sub))
(define-record-type ring #f #f
(mult ring.mult)
(one ring.one)
(add ring.add)
(zero ring.zero)
(sub ring.sub))
; A simple functor
(define (make-ring g m)
(record-compose ((monoid m)
(abelian-group g))
(ring)))
(define integer-monoid (monoid (mult *)
(one 1)))
(define integer-group (abelian-group (add +)
(zero 0)
(sub -)))
(define integer-ring (make-ring integer-group
integer-monoid))
((ring.add integer-ring) 1
2) ;==> 3
</pre>
<H1>Implementation</H1>
<p>
The reference implementations use the macro mechanism of
R5RS. They do not use any other SRFI or any library.
<p>
Two reference implementations are given. One represents records as
closures, while the other represents records as vectors. Depending
on the application,
the implementation based on closures may in fact perform better.
<p>
This version depends on <code>define</code> being treated as a binding form by <code>syntax-rules</code>.
This is true for recent versions of portable syntax-case, for PLT, for Scheme48, and possibly others.
</p><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><p>
In a deviation from the specification,
record types defined by the reference implementation are not generative, and not disjoint from other
types. Since various Schemes have their own ways of defining unique tags or types,
it is left to implementors to choose the best way of achieving generativity.
A simple mechanism for achieving generativity is implemented in SRFI-9. However, since this
mechanism is not foolproof and slows down certain key primitives, it was not adopted here.
</p><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 or naming convention.
</p><p>
Stub implementations are given for pattern matching, hopefully to be specified in a
future SRFI.
This is done because in the absence of static analysis,
providing pattern matching as a primitive is much more efficient
for certain common programming patterns.
For example, writing
<pre>
(match r
((r1 : f1 ... fn) (.... (f1 r) ....
.... (f2 r) ....
...
.... (fn r) ....)))
</pre>
for the common pattern
<pre>
(cond
((r1? r) (.... (r1.field1 r) ....
.... (r1.field2 r) ....
...
.... (r1.fieldn r) ....)))
</pre>
will save at least n type tests, and up to kn type tests in the
presence of subtyping with depth k.
</p><p>
The last section contains a few examples and (non-exhaustive) tests.
</p>
<H2>Reference implementation </H2>
</p><h3>Records as vectors</h3>
<pre>;==============================================================================
; Andre van Tonder, 2004.
; Records are implemented as vectors, with the attending virtual tables
; for polymorphism included in a dispatcher procedure.
;==============================================================================
; Exports:
(define-syntax define-record-type
(syntax-rules ()
((define-record-type (name super ...) constructor-clause name? field ...)
(build-record name (super ...) constructor-clause name? (field ...)))
((define-record-type (name super ...) constructor-clause)
(define-record-type (name super ...) constructor-clause #f))
((define-record-type (name super ...))
(define-record-type (name super ...) #f #f))
((define-record-type name . rest)
(define-record-type (name) . rest))))
(define-syntax record-update
(syntax-rules ()
((record-update record name (label exp) ...)
(update name record ((label exp) ...)))))
(define-syntax record-update*
(syntax-rules ()
((record-update* record name (label exp) ...)
(record.labels name
(update* name record ((label exp) ...))))))
(define-syntax record-update!
(syntax-rules ()
((record-update! record name (label exp) ...)
(update! name record ((label exp) ...)))))
(define-syntax record-compose
(syntax-rules ()
((record-compose () (export-name (label exp) ...))
(export-name (label exp) ...))
((record-compose ((name val) . imports) (export-name (label exp) ...))
(syntax-map (name export-name) (record.labels)
(compose 1 ((name val) . imports) (export-name (label exp) ...))))))
;==================================================================================
; Internal record utilities:
(define <record> '<record>)
(define <undefined> '<undefined>)
(define-syntax build-record
(syntax-rules ()
((build-record name supers (make-name pos-label ...) name? fields)
(build-record name supers (make-name pos-label ...) (pos-label ...) name? fields))
((build-record name supers make-name name? fields)
(build-record name supers make-name () name? fields))
((build-record name supers constructor (pos-label ...) 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 (pos-label ...) name ; -> (labels mut-labels interfaces mutables immutables . stack)
(union-labels ; -> (labels interfaces mut-labels mutables immutables . stack)
(recolor-identifiers
(emit-record 1 name constructor name?)))))))))))
(define-syntax emit-record
(syntax-rules ()
((emit-record (labels interfaces . stack) 1 name (make-name . pos-labels) name?)
(syntax-map (labels pos-labels interfaces) (add-temporaries)
(emit-record 2 labels name make-name name? . stack)))
((emit-record (labels . stack) 1 name make-name name?)
(emit-record (labels . stack) 1 name (make-name . labels) name?))
((emit-record (((label index) ...) ((pos-label pos-temp) ...) (((super super-label ...) vtable) ...)) 2
labels name make-name name? (mut-label ...)
((label** get-label** set-label!**) ...)
((label* get-label*) ...))
(begin
(define n 3)
(begin
(define index n)
(set! n (+ n 1)))
...
(define-syntax name
(syntax-rules (label ...)
((name ("index") label) index)
...))
(define maker
(let ((dispatcher
(let ((vtable (vector #f #f #f (record.index name super-label) ...))
...)
(lambda (interface sk fk)
(case interface
((name) (sk (lambda (n) n)))
((super) (sk (lambda (n) (vector-ref vtable n))))
...
(else (fk))))))
(printer
(lambda (record)
(list 'name
(list 'label
(vector-ref record
(record.index name label)))
...))))
(lambda labels
(make-record dispatcher
printer
label
...))))
(define (constructor pos-temp ...)
(populate maker 1 labels (pos-label pos-temp) ...))
(define-if make-name constructor)
(define-if name?
(lambda (val)
(and (record? val)
((record.dispatcher val) 'name
(lambda ignore #t)
(lambda () #f)))))
(define-if get-label*
(lambda (record)
((record.dispatcher record) 'name
(lambda (idx)
(vector-ref record
(idx (record.index name label*))))
(lambda () (error "Invalid argument" (show record)
"for" 'get-label*)))))
...
(define-if get-label**
(lambda (record)
((record.dispatcher record) 'name
(lambda (idx)
(vector-ref record
(idx (record.index name label**))))
(lambda () (error "Invalid argument" (show record)
"for" 'get-label**)))))
...
(define-if set-label!**
(lambda (record val)
((record.dispatcher record) 'name
(lambda (idx)
(vector-set! record
(idx (record.index name label**))
val)
record)
(lambda () (error "Invalid argument" (show record)
"for" 'set-label**)))))
...
(define-syntax name
(syntax-rules (: name label ... super ...)
((name ("labels") k) (syntax-apply k labels))
((name ("mutables") k) (syntax-apply k (mut-label ...)))
((name ("interfaces") k) (syntax-apply k ((super super-label ...) ... (name label ...))))
((name ("match") val (: . pats) sk fk) (syntax-zip (pos-label ...) pats
(match-labels name labels val sk fk)
(syntax-error "Wrong number of patterns in match"
(name : pats)
"Positional fields are"
(pos-label ...))))
((name ("match") val bindings sk fk) (match-labels bindings name labels val sk fk))
((name ("mutable?") mut-label sk fk) sk)
...
((name ("mutable?") other sk fk) fk)
((name ("index") label) index)
...
((name . bindings) (populate maker 1 labels . bindings))))
(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)))))
(define-syntax make-record
(syntax-rules ()
((make-record dispatcher printer field ...)
(vector <record> dispatcher printer field ...))))
(define (record? x)
(and (vector? x)
(> (vector-length x) 2)
(eq? (vector-ref x 0) <record>)))
(define (record.dispatcher r) (vector-ref r 1))
(define (record.printer r) (vector-ref r 2))
(define-syntax record.labels
(syntax-rules ()
((record.labels name k)
(name ("labels") k))))
(define-syntax record.mutables
(syntax-rules ()
((record.mutables name k)
(name ("mutables") k))))
(define-syntax record.if-mutable?
(syntax-rules ()
((record.if-mutable? label name sk fk)
(name ("mutable?") label sk fk))))
(define-syntax record.interfaces
(syntax-rules ()
((record.interfaces name k)
(name ("interfaces") k))))
(define-syntax record.index
(syntax-rules ()
((record.index name label)
(name ("index") label))))
(define (record-copy record)
(let* ((ln (vector-length record))
(new (make-vector ln)))
(let recur ((i 0))
(if (= i ln)
new
(begin (vector-set! new i (vector-ref record i))
(recur (+ i 1)))))))
(define-syntax define-if
(syntax-rules ()
((define-if #f binding) (begin))
((define-if name binding) (define name binding))))
(define (record->list r)
((record.printer r) r))
(define (show x)
(if (record? x)
(record->list x)
x))
(define-syntax extract-labels
(syntax-rules ()
((extract-labels stack fields pos-labels name k)
(syntax-map fields (syntax-car)
(syntax-append-after pos-labels
(remove-duplicates top:if-free=
(push-result stack k)))))))
(define-syntax get-immutable
(syntax-rules ()
((get-immutable stack fields k)
(syntax-filter fields (if-immutable?)
(push-result stack k)))))
(define-syntax if-immutable?
(syntax-rules ()
((if-immutable? (a b) sk fk) sk)
((if-immutable? other sk fk) fk)))
(define-syntax get-mutable
(syntax-rules ()
((get-mutable stack fields k)
(syntax-filter fields (if-mutable?)
(push-result stack k)))))
(define-syntax if-mutable?
(syntax-rules ()
((if-mutable? (a b c) sk fk) sk)
((if-mutable? other sk fk) fk)))
(define-syntax get-interfaces
(syntax-rules ()
((get-interfaces stack supers k)
(syntax-map supers (record.interfaces)
(syntax-append-all
(remove-duplicates interface=
(push-result stack k)))))))
(define-syntax interface=
(syntax-rules ()
((interface= (name . stuff) (name* . stuff*) sk fk)
(top:if-free= name name* sk fk))))
(define-syntax union-mutables
(syntax-rules ()
((union-mutables (interfaces ((mutable . stuff) ...) . stack) k)
(syntax-map interfaces (syntax-car)
(syntax-map (record.mutables)
(syntax-cons-after (mutable ...)
(syntax-append-all
(remove-duplicates top:if-free=
(push-result (interfaces ((mutable . stuff) ...) . stack) k)))))))))
(define-syntax union-labels
(syntax-rules ()
((union-labels (labels mut-labels interfaces . stack) k)
(syntax-map interfaces (syntax-cdr)
(syntax-append (labels)
(syntax-append-all
(remove-duplicates top:if-free=
(push-result (interfaces mut-labels . stack) k))))))))
(define-syntax recolor-identifiers
(syntax-rules ()
((recolor-identifiers (labels . rest) k)
(recolor top:if-free= labels rest
(syntax-cons-after labels k)))))
(define-syntax populate
(syntax-rules ()
((populate maker 1 labels (label exp) ...)
(order labels ((label . exp) ...) <undefined>
(populate 2 maker)))
((populate ((label . exp) ...) 2 maker)
(maker exp ...))))
(define-syntax order
(syntax-rules ()
((order ordering alist default k)
(order ordering alist alist () default k))
((order () () () accum default k)
(syntax-apply k accum))
((order (label* . labels*) bindings () (binding* ...) default k)
(order labels* bindings bindings (binding* ... (label* . default)) default k))
((order () ((label . value) . rest) countdown bindings* default k)
(syntax-error "Illegal label in" (label value)
"Legal bindings are" bindings*))
((order (label* . labels*)
((label . value) binding ...)
(countdown . countdowns)
(binding* ...)
default
k)
(if-free= label label*
(order labels*
(binding ...)
(binding ...)
(binding* ... (label . value))
default
k)
(order (label* . labels*)
(binding ... (label . value))
countdowns
(binding* ...)
default
k)))))
(define-syntax update
(syntax-rules ()
((update name record ((label exp) ...))
(let* ((val record)
(new (record-copy val)))
((record.dispatcher val) 'name
(lambda (idx)
(vector-set! new
(idx (record.index name label))
exp)
...
new)
(lambda () (error "Value" (show val)
"is not of record type" 'name)))))))
(define-syntax update!
(syntax-rules ()
((update! name record ((label exp) ...))
(syntax-andmap (label ...) (record.if-mutable? name)
(let ((val record))
((record.dispatcher val) 'name
(lambda (idx) (vector-set! val
(idx (record.index name label))
exp)
...
val)
(lambda () (error "Value" (show val)
"is not of record type" 'name))))
(syntax-error "Attempt to update! immutable field:"
(update! name record ((label exp) ...)))))))
(define-syntax update*
(syntax-rules ()
((update* (label ...) name record ((label* exp) ...))
(let ((val record))
((record.dispatcher val) 'name
(lambda (idx)
(let ((new (name (label (vector-ref val
(idx (record.index name label))))
...)))
(vector-set! new
(idx (record.index name label*))
exp)
...
new))
(lambda () (error "Value" (show val)
"is not of record type" 'name)))))))
(define-syntax compose
(syntax-rules ()
((compose (labels export-labels) 1 ((name record) . imports) (export-name . bindings))
(syntax-filter labels (if-member? export-labels if-free=)
(syntax-filter (if-not-member? bindings)
(compose 2 ((name record) . imports) (export-name . bindings)))))
((compose (label ...) 2 ((name record) . imports) (export-name . bindings))
(let ((val record))
((record.dispatcher val) 'name
(lambda (idx)
(record-compose imports
(export-name (label (vector-ref val
(idx (record.index name label))))
...
. bindings)))
(lambda () (error "Value" (show val)
"is not of record type" 'name)))))))
(define-syntax if-not-member?
(syntax-rules ()
((if-not-member? label () sk fk) sk)
((if-not-member? label ((label* . stuff*) . bindings) sk fk)
(if-free= label label*
fk
(if-not-member? label bindings sk fk)))))
(define-syntax record-match
(syntax-rules (_)
((record-match exp)
(error "Match failure for" exp))
((record-match (f . args) clause ...)
(let ((val (f . args)))
(record-match val clause ...)))
((record-match val ((name . pattern) . template) clause ...)
(name ("match") val pattern
(begin . template)
(record-match val clause ...)))
((record-match val (_ . template) clause ...)
(begin . template))
((record-match val (x . template) clause ...)
(let ((x val)) . template))))
(define-syntax match-labels
(syntax-rules ()
((match-labels ((label pat) ...) name labels val sk fk)
(syntax-andmap (label ...) (if-member? labels if-free=)
(let ((fail (lambda () fk)))
(if (record? val)
((record.dispatcher val) 'name
(lambda (idx)
(match-each ((pat (vector-ref val
(idx (record.index name label))))
...)
sk
fail))
fail)
(fail)))
(syntax-error "Attempt to match illegal label in match expression"
(name (label pat) ...)
"Legal labels are" labels)))))
(define-syntax match-each
(syntax-rules ()
((match-each () sk fail)
sk)
((match-each ((pat var) . bindings) sk fail)
(record-match var
(pat (match-each bindings sk fail))