-
Notifications
You must be signed in to change notification settings - Fork 1
/
srfi-9.html
426 lines (371 loc) · 14.5 KB
/
srfi-9.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
<!DOCTYPE html PUBLIC "-//IETF//DTD HTML 3.2//EN" "http://www.w3.org/TR/HTML32.dtd">
<html lang="en">
<head>
<title>SRFI 9: Defining Record Types</title>
<meta charset="utf-8" />
<meta content="width=device-width, initial-scale=1" name="viewport" />
<link href="https://srfi.schemers.org/srfi.css" rel="stylesheet" type="text/css" />
<link href="/favicon.png" rel="icon" sizes="192x192" type="image/png" />
</head>
<body>
<h1><a href="https://srfi.schemers.org/"><img alt="SRFI surfboard logo" class="srfi-logo" src="https://srfi.schemers.org/srfi-logo.svg" /></a>9: Defining Record Types</h1>
<p>by Richard Kelsey</p>
<h2>Status</h2>
<p>This SRFI is currently in <em>final</em> status. Here is <a href="https://srfi.schemers.org/srfi-process.html">an explanation</a> of each status that a SRFI can hold. To provide input on this SRFI, please send email to <code><a href="mailto:srfi+minus+9+at+srfi+dotschemers+dot+org">srfi-9@<span class="antispam">nospam</span>srfi.schemers.org</a></code>. To subscribe to the list, follow <a href="https://srfi.schemers.org/srfi-list-subscribe.html">these instructions</a>. You can access previous messages via the mailing list <a href="https://srfi-email.schemers.org/srfi-9">archive</a>.</p>
<ul>
<li>Received: 1999-07-01</li>
<li>Revised: 1999-08-25</li>
<li>Draft: 1999-07-07--1999-09-06</li>
<li>Final: 1999-09-09</li>
</ul>
<h2>Abstract</h2>
<p>
This SRFI describes syntax for creating new data types, called record types.
A predicate, constructor, and field accessors and modifiers are defined for
each record type. Each new record type is distinct from all existing types,
including other record types and Scheme's predefined types.
</p>
<h2>Rationale</h2>
<p>
Many Scheme implementations provide means for creating new types,
usually called either records or structures.
The <code>DEFINE-RECORD-TYPE</code> syntax described here is a slight
simplification of one written for Scheme 48 by Jonathan Rees.
Unlike many record-defining macros or special forms, it
does not create any new identifiers.
Instead, the names of the
record type, predicate, constructor, and so on are all listed explicitly
in the source.
This has the following advantages:
</p>
<ul>
<li>
It can be defined using a simple <code>SYNTAX-RULES</code> macro
in Scheme implementations that provide a procedural interface
for creating record types.
</li>
<li>
It does not restrict users to a particular naming convention.
</li>
<li>
Tools like <code>grep</code> and GNU Emacs's tag facility will see the
defining occurrence of each identifier.
</li>
</ul>
<h2 id="define-record-type">Specification</h2>
<p>
The syntax of a record-type definition is:
</p>
<pre>
<command or definition>
-> <record type definition> ; addition to 8.1.6 in R5RS
<record type definition>
-> (define-record-type <type name>
(<constructor name> <field tag> ...)
<predicate name>
<field spec> ...)
<field spec> -> (<field tag> <accessor name>)
-> (<field tag> <accessor name> <modifier name>)
<field tag> -> <identifier>
<... name> -> <identifier>
</pre>
<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.
</p>
<p>
Record-type definitions may only occur at top-level (there are two
possible semantics for `internal' record-type definitions, generative
and nongenerative, and no consensus as to which is better).
</p>
<p>
An instance of <code>DEFINE-RECORD-TYPE</code> is equivalent to the following
definitions:
</p>
<ul>
<li>
<code><type name></code> is bound to a representation of the record
type itself. Operations on record types, such as defining print
methods, reflection, etc. are left to other SRFIs.
</li>
<li>
<code><constructor name></code> is bound to a procedure that takes
as many arguments as there are <code><field tag></code>s in the
<code>(<constructor name> ...)</code>
subform and returns a new <code><type name></code> record.
Fields whose tags are listed with <code><constructor name></code>
have the corresponding argument as their
initial value. The initial values of all other fields are unspecified.
</li>
<li>
<code><predicate name></code> is a predicate that returns #T when
given a value returned by <code><constructor name></code> and #F
for everything else.
</li>
<li>
Each <code><accessor name></code> is a procedure that takes a record
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 which is not a record of
the appropriate type.
</li>
<li>
Each <code><modifier name></code> is a procedure that takes a record
of type <code><type name></code> and a value which becomes the new
value of the corresponding field; an unspecified value is returned.
It is an error to pass a modifier a first
argument which is not a record of the appropriate type.
</li>
</ul>
<p>
Records are disjoint from the types listed in Section 4.2 of R5RS.
</p>
<p>
<code>Set!</code>ing the value of any of these identifiers has no
effect on the behavior of any of their original values.
</p>
<p>
The following
</p>
<pre>
(define-record-type :pare
(kons x y)
pare?
(x kar set-kar!)
(y kdr))
</pre>
<p>
defines <code>KONS</code> to be a constructor, <code>KAR</code> and
<code>KDR</code> to be accessors, <code>SET-KAR!</code> to be a modifier,
and <code>PARE?</code> to be a predicate for <code>:PARE</code>s.
</p>
<pre>
(pare? (kons 1 2)) --> #t
(pare? (cons 1 2)) --> #f
(kar (kons 1 2)) --> 1
(kdr (kons 1 2)) --> 2
(let ((k (kons 1 2)))
(set-kar! k 3)
(kar k)) --> 3
</pre>
<h2>Implementation</h2>
<p>
This code is divided into three layers. In top-down order these are:
</p>
<ol>
<li>
Syntax definitions for <code>DEFINE-RECORD-TYPE</code> and an auxillary macro.
</li>
<li>
An implementation of record types with a procedural interface.
Some Scheme implementations already have something close to this.
</li>
<li>
Vector-like records implemented in R5RS. This redefines some standard
Scheme procedures and therefor must be loaded before any other code, including
part 2 above. Note that these procedures can be used to break the
record-type abstraction (for example, <code>RECORD-SET!</code> can be used
to modify the type of a record). Access to these procedures should be
restricted.
</li>
</ol>
<h3>Syntax definitions</h3>
<pre>
; Definition of DEFINE-RECORD-TYPE
(define-syntax define-record-type
(syntax-rules ()
((define-record-type type
(constructor constructor-tag ...)
predicate
(field-tag accessor . more) ...)
(begin
(define type
(make-record-type 'type '(field-tag ...)))
(define constructor
(record-constructor type '(constructor-tag ...)))
(define predicate
(record-predicate type))
(define-record-field type field-tag accessor . more)
...))))
; An auxilliary macro for define field accessors and modifiers.
; This is needed only because modifiers are optional.
(define-syntax define-record-field
(syntax-rules ()
((define-record-field type field-tag accessor)
(define accessor (record-accessor type 'field-tag)))
((define-record-field type field-tag accessor modifier)
(begin
(define accessor (record-accessor type 'field-tag))
(define modifier (record-modifier type 'field-tag))))))
</pre>
<h3>Record types</h3>
<pre>
; We define the following procedures:
;
; (make-record-type <type-name <field-names>) -> <record-type>
; (record-constructor <record-type<field-names>) -> <constructor>
; (record-predicate <record-type>) -> <predicate>
; (record-accessor <record-type <field-name>) -> <accessor>
; (record-modifier <record-type <field-name>) -> <modifier>
; where
; (<constructor> <initial-value> ...) -> <record>
; (<predicate> <value>) -> <boolean>
; (<accessor> <record>) -> <value>
; (<modifier> <record> <value>) -> <unspecific>
; Record types are implemented using vector-like records. The first
; slot of each record contains the record's type, which is itself a
; record.
(define (record-type record)
(record-ref record 0))
;----------------
; Record types are themselves records, so we first define the type for
; them. Except for problems with circularities, this could be defined as:
; (define-record-type :record-type
; (make-record-type name field-tags)
; record-type?
; (name record-type-name)
; (field-tags record-type-field-tags))
; As it is, we need to define everything by hand.
(define :record-type (make-record 3))
(record-set! :record-type 0 :record-type) ; Its type is itself.
(record-set! :record-type 1 ':record-type)
(record-set! :record-type 2 '(name field-tags))
; Now that :record-type exists we can define a procedure for making more
; record types.
(define (make-record-type name field-tags)
(let ((new (make-record 3)))
(record-set! new 0 :record-type)
(record-set! new 1 name)
(record-set! new 2 field-tags)
new))
; Accessors for record types.
(define (record-type-name record-type)
(record-ref record-type 1))
(define (record-type-field-tags record-type)
(record-ref record-type 2))
;----------------
; A utility for getting the offset of a field within a record.
(define (field-index type tag)
(let loop ((i 1) (tags (record-type-field-tags type)))
(cond ((null? tags)
(error "record type has no such field" type tag))
((eq? tag (car tags))
i)
(else
(loop (+ i 1) (cdr tags))))))
;----------------
; Now we are ready to define RECORD-CONSTRUCTOR and the rest of the
; procedures used by the macro expansion of DEFINE-RECORD-TYPE.
(define (record-constructor type tags)
(let ((size (length (record-type-field-tags type)))
(arg-count (length tags))
(indexes (map (lambda (tag)
(field-index type tag))
tags)))
(lambda args
(if (= (length args)
arg-count)
(let ((new (make-record (+ size 1))))
(record-set! new 0 type)
(for-each (lambda (arg i)
(record-set! new i arg))
args
indexes)
new)
(error "wrong number of arguments to constructor" type args)))))
(define (record-predicate type)
(lambda (thing)
(and (record? thing)
(eq? (record-type thing)
type))))
(define (record-accessor type tag)
(let ((index (field-index type tag)))
(lambda (thing)
(if (and (record? thing)
(eq? (record-type thing)
type))
(record-ref thing index)
(error "accessor applied to bad value" type tag thing)))))
(define (record-modifier type tag)
(let ((index (field-index type tag)))
(lambda (thing value)
(if (and (record? thing)
(eq? (record-type thing)
type))
(record-set! thing index value)
(error "modifier applied to bad value" type tag thing)))))
</pre>
<h3>Records</h3>
<pre>
; This implements a record abstraction that is identical to vectors,
; except that they are not vectors (VECTOR? returns false when given a
; record and RECORD? returns false when given a vector). The following
; procedures are provided:
; (record? <value>) -> <boolean>
; (make-record <size>) -> <record>
; (record-ref <record> <index>) -> <value>
; (record-set! <record> <index> <value>) -> <unspecific>
;
; These can implemented in R5RS Scheme as vectors with a distinguishing
; value at index zero, providing VECTOR? is redefined to be a procedure
; that returns false if its argument contains the distinguishing record
; value. EVAL is also redefined to use the new value of VECTOR?.
; Define the marker and redefine VECTOR? and EVAL.
(define record-marker (list 'record-marker))
(define real-vector? vector?)
(define (vector? x)
(and (real-vector? x)
(or (= 0 (vector-length x))
(not (eq? (vector-ref x 0)
record-marker)))))
; This won't work if ENV is the interaction environment and someone has
; redefined LAMBDA there.
(define eval
(let ((real-eval eval))
(lambda (exp env)
((real-eval `(lambda (vector?) ,exp))
vector?))))
; Definitions of the record procedures.
(define (record? x)
(and (real-vector? x)
(< 0 (vector-length x))
(eq? (vector-ref x 0)
record-marker)))
(define (make-record size)
(let ((new (make-vector (+ size 1))))
(vector-set! new 0 record-marker)
new))
(define (record-ref record index)
(vector-ref record (+ index 1)))
(define (record-set! record index value)
(vector-set! record (+ index 1) value))
</pre>
<h2>Copyright</h2>
<p>Copyright (C) Richard Kelsey (1999). All Rights Reserved. </p>
<p>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
</p>
<p>
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
</p>
<p>
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
</p>
<hr />
<address>Editor: <a href="mailto:srfi-editors%20at%20srfi%20dot%20schemers%20dot%20org">Mike Sperber</a></address>
</body>
</html>