-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsrfi-100.html
413 lines (394 loc) · 20 KB
/
srfi-100.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
<!DOCTYPE html>
<html lang="en">
<!--
SPDX-FileCopyrightText: 2009 Joo ChurlSoo
SPDX-License-Identifier: MIT
-->
<head>
<meta charset="utf-8">
<title>SRFI 100: define-lambda-object</title>
<meta name="viewport" content=
"width=device-width, initial-scale=1">
<link rel="stylesheet" href="/srfi.css" type="text/css">
<link href="/favicon.png" rel="icon" sizes="192x192" type=
"image/png">
</head>
<body>
<h1>Title</h1>
<p>define-lambda-object</p>
<h1>Author</h1>
<p>Joo ChurlSoo</p>
<h1>Status</h1>
<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+100+at+srfi+dotschemers+dot+org">srfi-100@<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-100">archive</a>.</p>
<ul>
<li>Received: <a href=
"https://srfi.schemers.org/srfi-100/srfi-100-1.1.txt">2009-08-31</a>
</li>
<li>Revised: <a href=
"https://srfi.schemers.org/srfi-100/srfi-100-1.2.txt">2009-09-14</a>
</li>
<li>Revised: <a href=
"https://srfi.schemers.org/srfi-100/srfi-100-1.3.txt">2009-09-22</a>
</li>
<li>Revised: <a href=
"https://srfi.schemers.org/srfi-100/srfi-100-1.4.txt">2009-09-30</a>
</li>
<li>Revised: <a href=
"https://srfi.schemers.org/srfi-100/srfi-100-1.5.txt">2009-10-05</a>
</li>
<li>Revised: <a href=
"https://srfi.schemers.org/srfi-100/srfi-100-1.6.txt">2010-06-14</a>
</li>
<li>Finalized: <a href=
"https://srfi.schemers.org/srfi-100/srfi-100-1.7.txt">2010-06-21</a>
</li>
<li>Draft: 2009-09-09--2009-11-09</li>
<li>Conversion to HTML: 2019-09-18</li>
</ul>
<h1>Abstract</h1>
<p>This SRFI introduces a macro, DEFINE-LAMBDA-OBJECT which
defines a set of procedures, that is, a group, two constructors,
and a predicate. The constructors also make a group of
procedures, namely lambda objects. The macro extends
DEFINE-RECORD-TYPE (SRFI 9) in being more general but much less
general than DEFCLASS (CLOS). The macro has no explicit field
accessors and mutators but parent groups, required fields,
optional fields, automatic fields, read-write fields, read-only
fields, inaccessible hidden fields, immutable virtual fields, and
common sharing fields.</p>
<h1>Rationale</h1>
<p>An object created by a constructor procedure is a procedure
whose first argument is a symbolized field name that is used to
identify fields. The lambda object plays the role of the accessor
and mutator of each field. Though the average time required to
access a randomly chosen field is more for the lambda object than
for the accessors and mutators of most other record-defining
macros that use field indices to indentify fields, the lambda
object makes the troublesome explicit or implicit accessors and
mutators unnecessary. In addition, this makes the accesors and
mutators to be automatically `nongenerative' and reduces the role
of the predicate procedure. Although DEFINE-RECORD-TYPE of R6RS
can also have implicit accessors and mutators, they should know
their own record name. Further more, when there are parents, they
should know both their own record name and their parents' record
names, which could make users confused, though there is an
advantage that a record can have another field with the same
name.</p>
<p>This macro works not only as DEFINE-RECORD-TYPE with required
fields but also as DEFSTRUCT of Common Lisp with optional fields.
The automatic field can be used as a procedure that modifies or
handles the values of the other fields.</p>
<p>When a group has multiple parent groups, all the fields of
parent groups must exist in the field spec of the child group in
contrary with DEFINE-RECORD-TYPE of R6RS. This is too much
trouble in case parent groups have several tens of fields. But it
also has the advantage of reconfirming the existence and
properties of each field, and making the constructors to be able
to be defined irrespectively the order of the parents' fields.
From a practical point of view, inheritance may be superfluous in
this macro as the lambda object itself has data and methods as
well as their accessors and mutators.</p>
<h1>Specification</h1>
<pre>
(define-lambda-object <group spec> <field spec>)
<group spec> --> <group> | (<group> <parent group>*)
<parent group> --> <group> ;unamendable group
| (<group>) ;amendable group
<field spec> --> <required field>* <optional field>* <automatic field>*
<required field> --> <field> ;read-only field
| (<field>) ;read-write field
<optional field> --> (<field> <default>) ;read-only field
| ((<field>) <default>) ;read-write field
| ('<field> <default>) ;inaccessible hidden field
<automatic field> --> (,<field> <default>) ;read-only field
| ((,<field>) <default>) ;read-write field
| (',<field> <default>) ;inaccessible hidden field
| (`,<field> <default>) ;immutable virtual field
| (,,<field> <default>) ;common read-only field
| ((,,<field>) <default>) ;common read-write field
</pre>
<p>The name of <code><constructor></code> is generated by
prefixing `make-' to the group name, or by prefixing `make-' and
postfixing `-by-name' to the group name. The name of
<code><predicate></code> is generated by adding a question
mark (`?') to the end of the group name.</p>
<p>The <code><group></code> and <code><field></code>
must be identifiers.</p>
<p>Each <code><default></code> is an
<code><expression></code> that is evaluated in an
environment that the values of all the previous
<code><field></code>s are visible. There is one exception
to this rule. The <code><default></code>s of
<code><automatic common field></code>s are evaluated in the
outer environment of the define-lambda-object form, and their
values are visible as the <code><default></code>s of the
other fields are evaluated.</p>
<p>The define-lambda-object form is a definition and can appear
anywhere any other <code><definition></code> can appear.
Each time define-lambda-object form is evaluated, a new group is
created with distinct <code><group></code>,
<code><constructor></code>, and
<code><predicate></code> procedures.</p>
<p>The <code><group></code> is bound to a procedure of one
argument. Like a gene, it has information on its <code><parent
group></code>s, <code><constructor></code>s,
<code><predicate></code>, and the number and properties of
<code><field></code>s. And they are checked out whenever
define-lambda-object form is evaluated. In case of inheritance,
all the <code><field></code>s of <code><parent
group></code>s must exist in the <code><field
spec></code> of the child group, irrespectively of the order.
Otherwise an error is signaled. In addition, the properties
(mutability, sort of field, and default expression) of
<code><field></code>s of unamendable groups must be
preserved in contrast with those of amendable groups. Otherwise
an error is signaled.</p>
<p>The <code><constructor></code> is bound to a procedure
that takes at least as many arguments as the number of
<code><required field></code>s. Whenever it is called, it
returns an object of the <code><group></code>, namely a
procedure, which has information on its own group and all that
goes with it. Its first argument must be a symbol of the same
name as <code><field></code>. Otherwise an error is
signaled. The object becomes an accessor procedure of each
<code><field></code> in case of one argument and a mutator
procedure of each <code><field></code> in case of two
arguments where the second argument is a new field value.</p>
<p>The names of <code><field></code>s are used to access
the <code><field></code>s as symbols of the same names. So
they must be distinct. Otherwise an error is signaled. The
read-write fields can be modified, whereas any attempt to modify
the values of the read-only fields via mutators signals an error.
Note: The read-only fields are not immutable. Their values, for
instance, can be modified by other fields whose values work like
their mutators.</p>
<p>The <code><required field></code> is initialized to the
first one of the remaining arguments. If there are no more
remaining arguments, an error is signaled.</p>
<p>The initialization of the <code><optional field></code>s
is done by two types of <code><constructor></code>s:</p>
<ol>
<li><code><make-`group-name'></code> constructor The
initialization method of <code><optional field></code>s
is the same as that of <code><required field></code>s
except that the field is bound to the
<code><default></code> instead of signaling an error if
there are no more remaining arguments.</li>
<li><code><make-`group-name'-by-name></code> constructor
The name used at a call site for the corresponding
<code><optional field></code> is a symbol of the same
name as the <code><field></code>. The remaining arguments
are sequentially interpreted as a series of pairs, where the
first member of each pair is a field name and the second is the
corresponding value. If there is no element for a particular
field name, the field is initialized to the
<code><default></code>.</li>
</ol>
<p>The <code><automatic common field></code>s are
initialized to each corresponding <code><default></code>
that is evaluated at the time the define-lambda-object form is
evaluated, and the values are shared with all the lambda objects
that are maded by the constructors of the define-lambda-object
form. The other <code><automatic field></code>s except
<code><automatic virtual field></code>s are initialized to
each corresponding <code><default></code> that is evaluated
at the time the lambda object is made by a constructor. The
<code><hidden field></code> is an externally nonexistent
field, that is, the field is invisible outside of the
define-lambda-object form but visible inside of it. On the
contrary, the <code><virtual field></code> is an internally
nonexistent field whose <code><default></code> is evaluated
each time when the field is accessed.</p>
<p>The <code><predicate></code> is a predicate procedure
that returns #t for objects constructed by
<code><constructor></code> or
<code><constructor></code>s for child groups and #f for
everything else.</p>
<h1>Examples</h1>
<pre>
;; The `x' is a read-write field.
;; The `y' is a read-only field.
(define-lambda-object ppoint (x) y)
(define pp (make-ppoint 10 20))
(pp 'x) => 10
(pp 'y) => 20
(pp 'x 11) (pp 'x) => 11
(pp 'y 22) => error: read-only field y
;; The parent group `ppoint' is an unamendable group.
(define-lambda-object (cpoint ppoint) x y color)
=> error: incompatible read-write field ppoint x
;; The 'color-init' and 'area-init' are automatic fields.
;; The 'color' and 'area' are virtual fields.
(define color 'black)
(define-lambda-object (cpoint ppoint)
(x) y
(,color-init color) (,area-init (* x y))
(`,color color) (`,area (* x y)))
(define ap (make-cpoint 3 33 'black)) => error: expects 2 arguments
(define ap (make-cpoint 10 20))
(map ap '(x y color-init color area-init area)) => (10 20 black black 200 200)
(ap 'x 30)
(map ap '(x y color-init color area-init area)) => (30 20 black black 200 600)
(set! color 'white)
(map ap '(x y color-init color area-init area)) => (30 20 black white 200 600)
;; The 'color' is an automatic common field.
(define-lambda-object (cpoint ppoint)
(x) y
((,,color) color)
(`,area (* x y))
(,set/add (lambda (i j) (set! x (+ i x)) (set! y (+ j y)))))
(define tp (make-cpoint 10 15))
(map tp '(x y color area)) => (10 15 white 150)
(define cp (make-cpoint 15 20))
(map cp '(x y color area)) => (15 20 white 300)
(cp 'color 'brown)
((cp 'set/add) 5 10)
(map cp '(x y color area)) => (20 30 brown 600)
(map tp '(x y color area)) => (10 15 brown 150)
(cpoint? ap) => #f
(cpoint? tp) => #t
(cpoint? cp) => #t
(ppoint? cp) => #t
;; The parent group `ppoint' is an amendable group.
;; The 'stack' is an optional hidden field.
;; The 'pop' is a virtual field.
;; The 'push' is an automatic field.
(define-lambda-object (spoint (ppoint))
(x 0) (y x) (z x) ('stack '())
(`,pop (if (null? stack)
(error 'spoint "null stack" stack)
(let ((s (car stack))) (set! stack (cdr stack)) s)))
(,push (lambda (s) (set! stack (cons s stack)))))
(define sp (make-spoint))
(map sp '(x y z)) => (0 0 0)
(define sp (make-spoint 5 55))
(map sp '(x y z)) => (5 55 5)
(define sp (make-spoint-by-name 'z 100 'stack (list 'sunflower)))
(map sp '(x y z)) => (0 0 100)
((sp 'push) 'rose) ((sp 'push) 'lily)
(sp 'pop) => lily
(sp 'pop) => rose
(sp 'pop) => sunflower
(sp 'pop) => error: null stack ()
(sp 'stack) => error: absent field stack
;; The 'stack' is an automatic hidden field.
;; The `set/add' is the same automatic field as that of `cpoint' group,
;; but it has a different default which simulates polymorphism and overloading.
(define-lambda-object (epoint (spoint) (cpoint))
((x) 5) ((y) 10) ((z) 15) ((planet) "earth")
(,,color "brown")
(',stack '())
(`,area (* x y))
(`,volume (* x y z))
(`,pop (if (null? stack)
(error 'spoint "null stack" stack)
(let ((s (car stack))) (set! stack (cdr stack)) s)))
(,push (lambda (s) (set! stack (cons s stack))))
(,adbmal (lambda (f) (f x y z color planet (* x y) (* x y z))))
(,set/add
(case-lambda
((i j) (cond
((and (string? i) (string? j)) (set! color i) (set! planet j))
((and (number? i) (number? j)) (set! x (+ i x)) (set! y (+ j y)))
(else (error 'epoint "set/add: wrong data type" i j))))
((i j k) (set! x (+ i x)) (set! y (+ j y)) (set! z (+ k z))))))
(define ep (make-epoint-by-name 'planet "jupiter"))
((ep 'adbmal) vector) => #(5 10 15 "brown" "jupiter" 50 750)
(define tp (make-epoint 10 15 20))
((tp 'adbmal) vector) => #(10 15 20 "brown" "earth" 150 3000)
(map (lambda (o) (o 'x)) (list pp ap cp sp ep))
=> (11 30 20 0 5)
(map (lambda (p) (p ep)) (list ppoint? cpoint? spoint? epoint?))
=> (#t #t #t #t)
((ep 'set/add) "red" "mars")
((ep 'adbmal) list) => (5 10 15 "red" "mars" 50 750)
((tp 'adbmal) list) => (10 15 20 "red" "earth" 150 3000)
((ep 'set/add) 5 10)
((ep 'adbmal) list) => (10 20 15 "red" "mars" 200 3000)
((ep 'set/add) 10 30 50)
(map ep '(x y z area volume)) => (20 50 65 1000 65000)
(map cp '(x y area)) => (20 30 600)
((cp 'set/add) 20 50)
(map cp '(x y area)) => (40 80 3200)
((cp 'set/add) 10 100 1000) => error: expects 2 arguments
epoint => #<procedure:epoint>
(epoint 'parent) => (#<procedure:spoint> #<procedure:cpoint>)
(epoint 'constructor) => (#<procedure:make-epoint> #<procedure:make-epoint-by-name>)
(epoint 'predicate) => #<procedure:epoint?>
(epoint 'read-write-field) => (x y z planet)
(epoint 'read-only-field) => (color area volume pop push adbmal set/add)
(epoint 'required-field) => ()
(epoint 'optional-field) => ((x 5) (y 10) (z 15) (planet "earth"))
(epoint 'common-field) => ((color "brown"))
(epoint 'hidden-field) => ((stack '()))
(epoint 'virtual-field) => ((area (* x y))
(volume (* x y z))
(pop (if (null? stack)
(error 'spoint "null stack" stack)
(let ((s (car stack)))
(set! stack (cdr stack)) s))))
(epoint 'automatic-field)
=>((color "brown")
(area (* x y))
(volume (* x y z))
(pop
(if (null? stack)
(error 'spoint "null stack" stack)
(let ((s (car stack))) (set! stack (cdr stack)) s)))
(stack '())
(push (lambda (s) (set! stack (cons s stack))))
(adbmal (lambda (f) (f x y z color planet (* x y) (* x y z))))
(set/add
(case-lambda
((i j)
(cond
((and (string? i) (string? j)) (set! color i) (set! planet j))
((and (number? i) (number? j)) (set! x (+ i x)) (set! y (+ j y)))
(else (error 'epoint "set/add: wrong data type" i j))))
((i j k) (set! x (+ i x)) (set! y (+ j y)) (set! z (+ k z))))))
</pre>
<h1>Implementation</h1>
<p>The <a href="srfi-100.scm">implementation</a> is written in
R6RS hygienic macro and define-macro.</p>
<p>The predicate procedure is implementation dependant. For
instance, a procedure such as procedure-name or object-name,
which returns the name of procedure or object, must be available
to distinguish objects created by all the constructors from the
others.</p>
<h1>References</h1>
<pre>
[R6RS] Michael Sperber, R. Kent Dybvig, Matthew Flatt, and
Anton von Straaten:
Revised(6) Report on the Algorithmic Language Scheme
http://www.r6rs.org
[SRFI 9] Richard Kelsey: Defining Record Type
https://srfi.schemers.org/srfi-9
[On Lisp] Paul Graham:
http://www.paulgraham.com/onlisp.html
</pre>
<h1>Copyright</h1>
<p>Copyright (c) 2009 Joo ChurlSoo.</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>
<address>
Editor: Mike Sperber
</address>
</body>
</html>