-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsrfi-57-1.7.html
1353 lines (1095 loc) · 51.9 KB
/
srfi-57-1.7.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 2005/01/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 - 2005/01/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.
We also introduce a syntax for declaring record type schemes, representing
families of record types that share a set of field labels.
A polymorphic predicate and
polymorphic field accessors and modifiers may be specified for each
record type scheme.
A syntax is provided for constructing records by
field label, for in-place and for functional record
update, and for composing records.
<H1>Rationale</H1>
We extend the existing SRFI-9 [1] with the following features,
each listed with a brief justification. Expanded rationales appear
in the specification section below.
<ul>
<li>A syntax for constructing record values by field label. Such a
feature is common in the prior art [2]. Indeed, the ability to
address fields by labels instead of position is one of the main
reasons for introducing the concept of records in the first
place.
Taking advantage of this feature can ease coding and increase readability and
robustness of code.
</li><li>
A mechanism for defining and incrementally extending
polymorphic functions on families
of record types sharing a set of field labels. This automates
a common programming pattern which would otherwise be laborious to
code explicitly.
</li><li>Facilities for record composition, functional record update,
including polymorphic
update, and linear in-place update.
These facilitate functional style programming with records.
</li><li>A declaration syntax that allows shorter variations and
optional elements, to save labor and reduce namespace
pollution.
</li></ul>
<H1>Specification</H1>
<h2>Declaration</h2>
<pre> <command or definition>
-> <record type definition>
-> <record scheme 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>)
<record scheme definition> -> (define-record-scheme <scheme clause>
<deconstructor clause>
<predicate clause>
<field clause> ...)
-> (define-record-scheme <scheme clause>
<deconstructor clause>)
-> (define-record-scheme <scheme clause>)
<type clause> -> <type name>
-> (<type name> <scheme name> ...)
<scheme clause> -> <scheme name>
-> (<scheme name> <parent scheme name> ...)
<constructor clause> -> (<constructor name> <field label> ...)
-> <constructor name>
-> #f
<deconstructor clause> -> (<deconstructor name> <field label> ...)
-> <deconstructor name>
-> #f
<predicate clause> -> <predicate name>
-> #f
<field clause> -> (<field label> <accessor clause> <modifier clause>)
-> (<field label> <accessor clause>)
-> (<field label>)
<accessor clause> -> <accessor name>
-> #f
<modifier clause> -> <modifier name>
-> #f
<field label> -> <identifier>
<... name> -> <identifier>
</pre>
<h3>Record types</h3>
An instance of <code>define-record-type</code> is equivalent to the following:
<ul>
<li> A list of field labels is associated with the record type <code><type name></code>,
obtained by appending from left to right the lists of field labels
of any record
type schemes (see below) appearing in the <code><type clause></code>,
followed by the list of labels in the
<code><constructor clause></code>, followed by the labels
in order of appearance in the <code><field clause></code>s.
Duplicates are removed from the resulting list according
to the semantics of <code>delete-duplicates</code> of SRFI-1.
<p>
</p></li><li>For each <code><scheme name></code> in <code><type clause></code>, the record type
<code><type name></code> is said to be an <i>instance</i> of, or to
<i>conform</i> to the corresponding
record type scheme <code><scheme name></code> and to all
parent type schemes (see below) of <code><scheme name></code>.
<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. It may also be registered, as specified in a
future SRFI, for performing pattern matching on record values of
type <code><type name></code>.
<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.
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 defined above.
</p><p>
<code><constructor name></code> may be
registered, in a way to be described in a future SRFI, for performing a
positional pattern match of the fields <code><field label> ...</code>
of record
values of type <code><type name></code> in the first case,
or of all fields
associated with <code><scheme name></code> in the default
order defined above in the second case.
</p></li><li> <code><predicate name></code>, is bound to a predicate procedure
that returns <code>#t</code> when given a record value that has been constructed using
the macro <code><type name></code> or the procedure <code><constructor name></code>,
and <code>#f</code> for any other
value. Values on which <code><predicate name></code>, if applied, would return
<code>#t</code>, are said to be <i>of type</i> <code><type name></code>.
<p>
</p></li><li> Field labels inherited from a <code><type scheme></code> or
introduced in the <code><constructor clause></code> do not have to be
repeated in the
<code><field clause></code>s.
Where present, <code><field
clause></code>s may provide additional information on such fields, or may
declare additional fields.
<p></p><p>
Field labels may be reused as the name of accessors or modifiers (a practice known
as punning).
</p><p>
</p><ul>
<p></p><li> Each <code><accessor name></code> is bound to
a procedure that takes a
value of type <code><type name></code>,
and returns the current value of the corresponding
field. It is an error to pass an accessor a value not of type
<code><type name></code>.
<p></p></li><li> Each <code><modifier name></code> is bound to
a procedure that takes a value of type <code><type name></code>
and a value which becomes the new value of the corresponding field.
It is an error to pass a modifier a first argument that is not of type
<code><type name></code>.
The return value of <code><modifier name></code> is unspecified.
<p>
</p></li></ul></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.
<h3>Record type schemes</h3>
An instance of <code>define-record-scheme</code> is equivalent to the following:
<ul>
<li> A list of field labels is associated with the type scheme <code><scheme name></code>,
obtained by appending from left to right the lists of field labels
of any parent
type schemes appearing in the <code><scheme clause></code>,
followed by the list of labels in the
<code><deconstructor clause></code>, followed by the labels
in order of appearance in the <code><field clause></code>s.
Duplicates are removed from the resulting list according
to the semantics of <code>delete-duplicates</code> of SRFI-1.
<p>
</p></li><li>A record type scheme is called a <i>parent scheme</i> of
<code><scheme name></code> if it appears in the
<code><scheme clause></code>, or if it is a parent scheme of
one of the <code><parent scheme name></code>'s appearing in the
<code><scheme clause></code>.
The type scheme
<code><scheme name></code> is said to
<i>extend</i> its parent type schemes. It is an error to extend a type scheme
that has not yet been defined.
<p></p></li><li> <code><scheme name></code> may be bound to a macro or otherwise
registered, in a way to be
described in a future
SRFI,
for performing pattern matching on record
values conforming to <code><scheme name></code>.
<p>
</p></li><li>If <code><deconstructor clause></code> is
of the form <code>(<deconstructor name> <field label> ...)</code>, then
<code><deconstructor name></code> may be bound to a macro or otherwise
registered, in a way to be described in a future SRFI, for performing a
positional pattern match of the fields <code><field label> ...</code>
on record
values conforming to <code><scheme name></code>.
If <code><deconstructor clause></code> is of the form <code><deconstructor name></code>,
the positional match will be on all fields
associated with <code><scheme name></code>, in the default order 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 of any record type conforming
to <code><scheme name></code>,
and <code>#f</code> for any other
value. Values on which <code><predicate name></code>, if applied, would return
<code>#t</code>, are themselves said to <i>conform</i> to
<code><scheme name></code>.
<p>
</p></li><li> Field labels inherited from a <code><parent type scheme></code> or
introduced in the <code><deconstructor clause></code> do not have to be
repeated in the
<code><field clause></code>s.
Where present, <code><field
clause></code>s may provide additional information on such fields, or may
declare additional fields.
<p></p><p>
Field labels may be reused as the name of accessors or modifiers (a practice known
as punning).
</p><p>
</p><ul>
<p></p><li> Each <code><accessor name></code> is bound to
a procedure that takes a
value conforming to <code><scheme name></code>,
and returns the current value of the corresponding
field. It is an error to pass an accessor a value not conforming to
<code><scheme name></code>.
<p></p></li><li> Each <code><modifier name></code> is bound to
a procedure that takes a value conforming to <code><scheme name></code>
and a value which becomes the new value of the corresponding field.
It is an error to pass a modifier a first argument that does not conform to
<code><scheme name></code>.
The return value of <code><modifier name></code> is unspecified.
<p>
</p></li></ul></li></ul>
</p><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))
(get-y p) ==> 3
(point? p) ==> #t
</pre>
<h4>Record type schemes:</h4>
Let's declare a couple of record schemes. Record schemes do not have constructors.
They introduce polymorphic predicates and accessors.
<pre> (define-record-scheme <point #f <point?
(x <point.x)
(y <point.y))
(define-record-scheme <color #f <color?
(hue <color.hue))
</pre>
We now declare concrete instances of the above schemes.
Constructors may be introduced.
Predicates and accessors for concrete record types, when declared, are monomorphic.
<pre> (define-record-type (point <point) make-point point?
(x point.x)
(y point.y))
(define-record-type (color <color) make-color)
(define-record-type (color-point <color <point)
(make-color-point x y hue) color-point?
(info color-point.info))
(define cp (make-color-point 1 2 'blue))
(<point? cp) ==> #t
(<color? cp) ==> #t
(<point.y cp) ==> 2
(<color.hue cp) ==> blue
(point? cp) ==> #f
(point.x cp) ==> error
(color-point? cp) ==> #t
(color-point.info cp) ==> <undefined>
</pre>
<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 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)
(right right))
</pre>
Here the constructor <code>make-node</code> has the default argument order and no predicate
is bound. Also note that field labels are
punned.
<p></p><h4>A note on repeated fields and redefinitions</h4>
In the following example, two record type schemes define different accessors for the same field:
<pre> (define-record-scheme foo #f #f (x foo-x))
(define-record-scheme bar #f #f (x bar-x))
(define-record-type (foo-bar foo bar))
</pre>
Since any value <code>fb</code> of type <code>foo-bar</code> conforms to both
<code>foo</code> and <code>bar</code>, both <code>foo-x</code> and <code>bar-x</code>
can be applied to <code>fb</code>, returning the value of the <code>x</code> field.
<p>
In the following example, two declarations introduce
the same accessor:
</p><pre> (define-record-scheme foo #f #f (x foo-x))
(define-record-type (bar foo) #f #f (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 be bound to the monomorphic accessor applicable only to values
of type <code>bar</code>, replacing its binding to the polymorphic accessor procedure
introduced in the <code>foo</code> declaration.
<h2>Labeled record expressions</h2>
The following syntax allows one to construct a record value by labels. The result
is a record value of type <code><type name></code> with each field
<code><field label></code> populated with the value of the corresponding
<code><expression></code>. The order of evaluation of the expressions
<code><expression> ...</code> is undefined. 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. The
runtime efficiency of a labeled record expression is required to be at least that of
the equivalent positional constructor.
<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 inherits fields from a type scheme.
Field labels are checked for validity
and the macro may be 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> <scheme name> (<field label> <expression>) ...)
-> (record-update <record> <type name> (<field label> <expression>) ...)
-> (record-update! <record> <type name> (<field label> <expression>) ...)
-> (record-update! <record> <scheme 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 conforms to
<code><scheme 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 scheme <code><scheme 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 of type
<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 and fourth alternatives are used for linear, in-place record update. The expression
<code><record></code> must evaluate to a record value of type
<code><type name></code> or conforming to scheme <code><scheme name></code> . The result will be the original record value
with the given fields
mutated 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> or scheme <code><scheme name></code>.
If this condition is not satisfied, an expansion time error must be signaled.
</p><p>
In these forms, the order of evaluation of the expressions
<code><expression> ...</code> is undefined.
</p><h4>Rationale</h4>
A mechanism for functional update facilitates and encourages functional-style programming
with records.
Note that 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><p>The 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>
operation may be replaced by <code>update!</code> for efficiency.
See SRFI-1 for a good discussion of the rationale behind linear update procedures.
Note, however, that in contrast with the linear procedures in SRFI-1, <code>update!</code> here is <i>required</i>
to mutate the original record.
</p><h4>Examples</h4>
Monomorphic update:
<pre>
(define p (point (x 1) (y 2)))
(record-update p point (x 7)) ==> (point (x 7) (y 2))
p ==> (point (x 1) (y 2)) - original unaffected
</pre>
Polymorphic update:
<pre> (define cp (color-point (hue 'blue) (x 1) (y 2)))
(record-update cp <point (x 7)) ==> (color-point (info <undefined>) (hue blue) (x 7) (y 2))
cp ==> (color-point (info <undefined>) (hue blue) (x 1) (y 2))
</pre>
In-place update:
<pre> (record-update! cp <point (x 7))) ==> (color-point (info <undefined>) (hue blue) (x 7) (y 2))
cp ==> (color-point (info <undefined>) (hue blue) (x 7) (y 2))
</pre>
<h2>Record composition</h2>
The following syntax provides a shorthand for composing record values:
<pre> <expression> -> (record-compose (<import name> <record>)
...
(<export-type name> (<field label> <expression>) ...))
<import name> -> <type name>
-> <scheme name>
</pre>
Here each expression <code><record></code> must evaluate to a record value of type
<code><type name></code> or conforming to type scheme <code><scheme name></code>. The expression
evaluates to a new record value of type <code><export-type name></code>
whose fields are
populated as follows: For each field label belonging to <code><import name></code>
that is also a field label of the type
<export-type name>, the corresponding field of <code><record></code>
is copied into the result. This is done for all imports from left to
right, dropping any repeated fields. The additional fields <code><field label></code>
are then populated with the value of the
corresponding <code><expression></code>, overwriting
any fields with the same labels already imported. Any remaining fields are undefined.
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 order of evaluation of the expressions <code><record> ...</code>
<code><expression> ...</code> is undefined. All these
expressions will be evaluated, even if their values might not be used in
the result.
</p><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.<p>
Monomorphic record update is a special case of <code>record-compose</code>. The latter
may be used to express more general updates polymorphic in the
argument but monomorphic in the result type.
<p>
</p><h4>Examples</h4>
Use <code>record-compose</code> for updates polymorphic in the argument but
monomorphic in the result type:
<pre> (define cp (make-color-point 1 2 'green))
(record-compose (<point cp) (point (x 8))) ==> (point (x 8) (y 2))
</pre>
A more general composition example:
<pre> (define cp (make-color-point 1 2 'green))
(define c (make-color 'blue))
(record-compose (<point cp) ; polymorphic import - only fields x and y of cp taken
(color c) ; monomorphic import
(color-point (x 8) ; overrides imported field
(info 'hi)))
==> (color-point (info hi) (hue blue) (x 8) (y 2))
</pre>
Small module-functor example:
<pre>
(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))
(define integer-monoid (monoid (mult *)
(one 1)))
(define integer-group (abelian-group (add +)
(zero 0)
(sub -)))
(define (make-ring g m) ; simple functor a la ML
(record-compose (monoid m)
(abelian-group g)
(ring)))
(define integer-ring (make-ring integer-group
integer-monoid))
((ring.add integer-ring) 1
2) ==> 3
</pre>
<H1>Implementation</H1>
<p>
The reference implementation uses the macro mechanism of
R5RS. It assumes an existing implementation of SRFI-9, here denoted
srfi-9:define-record-type. It also has a trivial use of case-lambda from SRFI-16.
</p><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 as used in Chez Scheme. It is
also true 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>
Only the names mentioned in the specification should be visible to
the user. Other
names should be hidden by a module system or naming convention.
</p><p>
The last section contains a few examples and (non-exhaustive) tests.
</p>
<h2>Reference implementation </h2>
<pre>;============================================================================================
; IMPLEMENTATION:
;
; Andre van Tonder, 2004.
;
;============================================================================================
(define-syntax define-record-type
(syntax-rules ()
((define-record-type . body)
(parse-declaration #f . body))))
(define-syntax define-record-scheme
(syntax-rules ()
((define-record-scheme . body)
(parse-declaration #t . body))))
(define-syntax parse-declaration
(syntax-rules ()
((parse-declaration is-scheme? (name super ...) constructor-clause predicate field-clause ...)
(build-record 0 constructor-clause (super ...) (field-clause ...) name predicate is-scheme?))
((parse-declaration is-scheme? (name super ...) constructor-clause)
(parse-declaration is-scheme? (name super ...) constructor-clause #f))
((parse-declaration is-scheme? (name super ...))
(parse-declaration is-scheme? (name super ...) #f #f))
((parse-declaration is-scheme? name . rest)
(parse-declaration is-scheme? (name) . rest))))
(define-syntax record-update!
(syntax-rules ()
((record-update! record name (label exp) ...)
(meta
`(let ((r record))
((meta ,(name ("setter") label)) r exp)
...
r)))))
(define-syntax record-update
(syntax-rules ()
((record-update record name (label exp) ...)
(name ("is-scheme?")
(meta
`(let ((new ((meta ,(name ("copier"))) record)))
(record-update! new name (label exp) ...)))
(record-compose (name record) (name (label exp) ...))))))
(define-syntax record-compose
(syntax-rules ()
((record-compose (export-name (label exp) ...))
(export-name (label exp) ...))
((record-compose (import-name record) ... (export-name (label exp) ...))
(help-compose 1 (import-name record) ... (export-name (label exp) ...)))))
(define-syntax help-compose
(syntax-rules ()
((help-compose 1 (import-name record) import ... (export-name (label exp) ...))
(meta
`(help-compose 2
(meta ,(intersection
(meta ,(export-name ("labels")))
(meta ,(remove-from (meta ,(import-name ("labels")))
(label ...)
if-free=))
if-free=))
(import-name record)
import ...
(export-name (label exp) ...))))
((help-compose 2 (copy-label ...) (import-name record) import ... (export-name . bindings))
(meta
`(let ((r record))
(record-compose import ...
(export-name (copy-label ((meta ,(import-name ("getter") copy-label)) r))
...
. bindings)))))))
(define-syntax build-record
(syntax-rules ()
((build-record 0 (constructor . pos-labels) . rest) ; extract positional labels from constructor clause
(build-record 1 (constructor . pos-labels) pos-labels . rest)) ;
((build-record 0 constructor . rest) ;
(build-record 1 (constructor . #f) () . rest)) ;
((build-record 1 constructor-clause (pos-label ...) (super ...)
((label . accessors) ...) . rest)
(meta
`(build-record 2
constructor-clause
(meta ,(union (meta ,(super ("labels"))) ; compute union of labels from supers,
... ; constructor clause and field clauses
(pos-label ...)
(label ...)
top:if-free=))
((label . accessors) ...)
(meta ,(union (meta ,(super ("supers"))) ; compute transitive union of supers
...
top:if-free=))
. rest)))
((build-record 2 (constructor . pos-labels) labels . rest) ; insert default constructor labels if not given
(syntax-if pos-labels
(build-record 3 (constructor . pos-labels) labels . rest)
(build-record 3 (constructor . labels) labels . rest)))
((build-record 3 constructor-clause labels ((label . accessors) ...) . rest)
(meta
`(build-record 4
(meta ,(remove-from labels ; separate the labels that do not appear in a
(label ...) ; field clause for next step
top:if-free=))
((label . accessors) ...)
constructor-clause
labels
. rest)))
((build-record 4
(undeclared-label ...)
(field-clause ...)
(constructor . pos-labels)
labels
supers
name
predicate
is-scheme?)
(meta
`(build-record 5 ; generate identifiers for constructor, predicate
is-scheme? ; getters and setters as needed
name
supers
supers
labels
(meta ,(to-identifier constructor))
(meta ,(add-temporaries pos-labels)) ; needed for constructor below
(meta ,(to-identifier predicate))
(meta ,(augment-field field-clause))
...
(undeclared-label (meta ,(generate-identifier))
(meta ,(generate-identifier)))
...)))
((build-record 5
is-scheme?
name
(super ...)
supers
(label ...)
constructor
((pos-label pos-temp) ...)
predicate
(field-label getter setter)
...)
(begin
(syntax-if is-scheme?
(begin
(define-generic (predicate x) (lambda (x) #f))
(define-generic (getter x))
...
(define-generic (setter x v))
...
(define-generic (copy x)))
(begin
(srfi-9:define-record-type internal-name
(maker field-label ...)
predicate
(field-label getter setter) ...)
(define constructor
(lambda (pos-temp ...)
(populate 1 maker (field-label ...) (pos-label pos-temp) ...)))
(extend-predicates supers predicate)
(extend-accessors supers field-label predicate getter setter)
...
(define (copy x)
(maker (getter x) ...))
(extend-copiers supers copy predicate)
(define-method (show (r predicate))
(list 'name
(list 'field-label (getter r))
...))))
(define-syntax name
(syntax-rules (field-label ...)
((name ("is-scheme?") sk fk) (syntax-if is-scheme? sk fk))
((name ("predicate") k) (syntax-apply k predicate))
((name ("supers") k) (syntax-apply k (super ... name)))
((name ("labels") k) (syntax-apply k (label ...)))
((name ("pos-labels") k) (syntax-apply k (pos-label ...)))
((name ("getter") field-label k) (syntax-apply k getter))
...
((name ("getter") other k) (syntax-apply k #f))
((name ("setter") field-label k) (syntax-apply k setter))
...
((name ("setter") other k) (syntax-apply k #f))
((name ("copier") k) (syntax-apply k copy))
((name . bindings) (populate 1 maker (field-label ...) . bindings))))))))
(define-syntax to-identifier
(syntax-rules ()
((to-identifier #f k) (syntax-apply k generated-identifier))
((to-identifier id k) (syntax-apply k id))))
(define-syntax augment-field
(syntax-rules ()
((augment-field (label) k) (syntax-apply k (label generated-getter generated-setter)))
((augment-field (label getter) k) (meta `(label (meta ,(to-identifier getter)) generated-setter) k))
((augment-field (label getter setter) k) (meta `(label (meta ,(to-identifier getter))
(meta ,(to-identifier setter))) k))))
(define-syntax extend-predicates
(syntax-rules ()
((extend-predicates (super ...) predicate)
(begin
(meta
`(define-method (meta ,(super ("predicate")))
(predicate)
(x)
any?))
...))))
(define-syntax extend-copiers
(syntax-rules ()
((extend-copiers (super ...) copy predicate)
(begin
(meta
`(define-method (meta ,(super ("copier")))
(predicate)
(x)
copy))
...))))
(define-syntax extend-accessors
(syntax-rules ()
((extend-accessors (super ...) label predicate selector modifier)
(meta
`(begin
(syntax-if (meta ,(super ("getter") label))
(define-method (meta ,(super ("getter") label))
(predicate)
(x)
selector)
(begin))
...
(syntax-if (meta ,(super ("setter") label))
(define-method (meta ,(super ("setter") label))
(predicate any?)
(x v)
modifier)
(begin))
...)))))
(define-syntax populate
(syntax-rules ()
((populate 1 maker labels . bindings)
(meta
`(populate 2 maker
(meta ,(order labels bindings ('<undefined>))))))
((populate 2 maker ((label exp) ...))
(maker exp ...))))
(define-syntax order
(syntax-rules ()
((order (label ...) ((label* . binding) ...) default k)
(meta
`(if-empty? (meta ,(remove-from (label* ...)
(label ...)
if-free=))
(order "emit" (label ...) ((label* . binding) ...) default k)
(syntax-error "Illegal labels in" ((label* . binding) ...)
"Legal labels are" (label ...)))))
((order "emit" (label ...) bindings default k)
(meta
`((label . (meta ,(syntax-lookup label
bindings
if-free=
default)))
...)
k))))
;============================================================================================
; Simple generic functions:
(define-syntax define-generic
(syntax-rules ()
((define-generic (name arg ...))
(define-generic (name arg ...)
(lambda (arg ...) (error "Inapplicable method:" 'name
"Arguments:" (show arg) ... ))))
((define-generic (name arg ...) proc)
(define name (make-generic (arg ...) proc)))))
(define-syntax define-method
(syntax-rules ()
((define-method (generic (arg pred?) ...) . body)
(define-method generic (pred? ...) (arg ...) (lambda (arg ...) . body)))
((define-method generic (pred? ...) (arg ...) procedure)
(let ((next ((generic) 'get-proc))
(proc procedure))
(((generic) 'set-proc)
(lambda (arg ...)
(if (and (pred? arg) ...)
(proc arg ...)
(next arg ...))))))))
(define-syntax make-generic
(syntax-rules ()
((make-generic (arg arg+ ...) default-proc)
(let ((proc default-proc))
(case-lambda
((arg arg+ ...)
(proc arg arg+ ...))
(()
(lambda (msg)
(case msg
((get-proc) proc)
((set-proc) (lambda (new)
(set! proc new)))))))))))