-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinding.html
714 lines (516 loc) · 28 KB
/
binding.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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"><!-- DO NOT EDIT THIS FILE-->
<!-- Edit the .tex version instead-->
<html>
<head>
<title>Binding Forms</title>
<link href="csug.css" rel="stylesheet" type="text/css">
</head>
<body>
<a name="g36"></a>
<a name="./binding:h0"></a>
<h1>Chapter 5. Binding Forms<a name="CHPTBINDING"></a></h1>
<p>
This chapter describes <i>Chez Scheme</i> extensions to the set of Revised<sup>6</sup>
Report binding forms.
See Chapter <a href="http://scheme.com/tspl4/./binding.html#g88">4</a> of <i>The Scheme Programming Language, 4th Edition</i> or the Revised<sup>6</sup> Report
for a description of standard binding forms.
<p>
<h3><a name="g37"></a><a name="./binding:h1"></a>Section 5.1. Definitions<a name="SECTDEFINITIONS"></a></h3>
<p>
A <a name="./binding:s0"></a>definition in Revised<sup>6</sup> Report Scheme is a
<a name="./binding:s1"></a><a name="./binding:s2"></a>variable definition,
<a name="./binding:s3"></a><a name="./binding:s4"></a>keyword
definition, or derived definition, i.e., a syntactic extension that
expands into a definition.
In addition, the forms within a
<a name="./binding:s5"></a><tt>begin</tt>
expression appearing after a sequence
of definitions is spliced onto the end of the sequence of definitions
so that definitions at the front of the <tt>begin</tt> expression
are treated as if they were part of the outer sequence of definitions.
A <a name="./binding:s6"></a><tt>let-syntax</tt> or
<a name="./binding:s7"></a><tt>letrec-syntax</tt> form
is treated similarly, so that definitions at the front of the body
are treated as if they were part of the outer sequence of definitions,
albeit scoped where the bindings of the <tt>let-syntax</tt> or
<tt>letrec-syntax</tt> form are visible.
<p>
<i>Chez Scheme</i> extends the set of definitions to include
<a name="./binding:s8"></a><a name="./binding:s9"></a><tt>module</tt> forms,
<a name="./binding:s10"></a><tt>import</tt> forms,
<a name="./binding:s11"></a><tt>import-only</tt> forms,
<a name="./binding:s12"></a><tt>meta</tt> definitions, and
<a name="./binding:s13"></a><tt>alias</tt> forms, although the
<tt>module</tt>, <tt>import</tt>, <tt>import-only</tt>,
<tt>meta</tt>, and <tt>alias</tt> keywords are not available
in a library or RNRS top-level program unless the
<tt>scheme</tt> library is included in the library or
top-level programs imports.
These forms are described in Chatper <a href="./syntax.html#g95">11</a>.
<p>
In Revised<sup>6</sup> Report Scheme, definitions can appear at the front of
a <tt>lambda</tt> or similar body (e.g., a <tt>let</tt> or <tt>letrec</tt>
body), at the front of a library body, or intermixed with expressions
within an RNRS top-level program body.
In <i>Chez Scheme</i>, definitions may also be used in the
interactive top-level, i.e., they can be intermixed with expressions in
the REPL or in program text to be loaded from a file
via <a name="./binding:s14"></a><tt>load</tt> (Section <a href="./system.html#g111">12.4</a>).
The Revised<sup>6</sup> Report does not mandate the existence nor specify the
semantics of an interactive top-level, nor of a <tt>load</tt>
procedure.
<p>
The macro expander uses the same two-pass algorithm for expanding
top-level <tt>begin</tt> expressions as it uses for a <tt>lambda</tt>,
<tt>library</tt>, or top-level program body.
(This algorithm is described in Section <a href="./syntax.html#g96">11.1</a> of <i>The Scheme Programming Language, 4th Edition</i>.)
As a result,
<p>
<p><tt>(begin<br>
(define-syntax a (identifier-syntax 3))<br>
(define x a))</tt>
<p>and
<p>
<p><tt>(begin<br>
(define x a)<br>
(define-syntax a (identifier-syntax 3)))</tt>
<p>both result in the giving <tt>x</tt> the value 3,
even though an unbound variable reference to <tt>a</tt> would result if
the two forms within the latter <tt>begin</tt> expression where run
independently at top level.
<p>
Similarly, the <tt>begin</tt> form produced by a use of
<p>
<p><tt>(define-syntax define-constant<br>
(syntax-rules ()<br>
[(_ x e)<br>
(begin<br>
(define t e)<br>
(define-syntax x (identifier-syntax t)))]))</tt>
<p>and the <tt>begin</tt> form produced by a use of
<p>
<p><tt>(define-syntax define-constant<br>
(syntax-rules ()<br>
[(_ x e)<br>
(begin<br>
(define-syntax x (identifier-syntax t))<br>
(define t e))]))</tt>
<p>are equivalent.
<p>
The Revised<sup>6</sup> Report specifies that internal variable definitions be
treated like <tt>letrec*</tt>, while earlier reports required internal
variable definitions to be treated like <tt>letrec</tt>.
By default, <i>Chez Scheme</i> implements the Revised<sup>6</sup> Report semantics for
internal variable definitions, as for all other things, but this behavior
may be overridden via the <tt>internal-defines-as-letrec*</tt> parameter.
<p>
<a name="./binding:s15"></a><span class=formdef><b>thread parameter</b>: <tt>internal-defines-as-letrec*</tt></span>
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>When this parameter is set to <tt>#t</tt> (the default), internal variable
definitions are evaluated using <tt>letrec*</tt> semantics.
It may be set to <tt>#f</tt> to revert to the <tt>letrec</tt> semantics
for internal variable definitions, for backward compatibility.
<p>
<h3><a name="g38"></a><a name="./binding:h2"></a>Section 5.2. Multiple-value Definitions</h3>
<p>
<a name="./binding:s16"></a><span class=formdef><b>syntax</b>: <tt>(define-values formals <i>expr</i>)</tt></span>
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>A <tt>define-values</tt> form is a definition and can appear anywhere
other definitions can appear.
It is like a <tt>define</tt> form but permits an arbitrary formals list
(like <tt>lambda</tt>) on the left-hand side.
It evaluates <tt><i>expr</i></tt> and binds the variables appearing in <tt><i>formals</i></tt>
to the resulting values, in the same manner as the formal parameters of a
procedure are bound to its arguments.
<p>
<p><tt>(let ()<br>
(define-values (x y) (values 1 2))<br>
(list x y)) <img src="math/csug/0.gif" alt="<graphic>"> (1 2)<br>
(let ()<br>
(define-values (x y . z) (values 1 2 3 4))<br>
(list x y z)) <img src="math/csug/0.gif" alt="<graphic>"> (1 2 (3 4))</tt>
<p>A <tt>define-values</tt> form expands into a sequence of definitions, the
first for a hidden temporary bound to a data structure holding the values
returned by <tt><i>expr</i></tt> and the remainder binding each of the formals to
the corresponding value or list of values, extracted from the data
structure via a reference to the temporary.
Because the temporary must be defined before the other variables are
defined, this works for internal <tt>define-values</tt> forms only if
<tt>internal-defines-as-letrec*</tt> is set to the default value
<tt>#t</tt>.
<p>
<h3><a name="g39"></a><a name="./binding:h3"></a>Section 5.3. Recursive Bindings</h3>
<p>
<a name="./binding:s17"></a><span class=formdef><b>syntax</b>: <tt>(rec <i>var</i> <i>expr</i>)</tt></span>
<br>
<b>returns: </b>value of <tt><i>expr</i></tt>
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>The syntactic form <tt>rec</tt> creates a <a name="./binding:s18"></a>recursive object from <tt><i>expr</i></tt> by
establishing a binding of <tt><i>var</i></tt> within <tt><i>expr</i></tt> to the value of <tt><i>expr</i></tt>.
In essence, it is a special case of <tt>letrec</tt> for self-recursive objects.
<p>
This form is useful for creating recursive objects (especially procedures)
that do not depend on external variables for the recursion, which are
sometimes undesirable because the external bindings can change.
For example, a recursive procedure defined at top level depends on the value
of the top-level variable given as its name.
If the value of this variable should change, the meaning of the procedure
itself would change.
If the procedure is defined instead with <tt>rec</tt>, its meaning is independent
of the variable to which it is bound.
<p>
<p><tt>(map (rec sum<br>
(lambda (x)<br>
(if (= x 0)<br>
0<br>
(+ x (sum (- x 1))))))<br>
'(0 1 2 3 4 5)) <img src="math/csug/0.gif" alt="<graphic>"> (0 1 3 6 10 15)
<br>
<br>
(define cycle<br>
(rec self<br>
(list (lambda () self))))
<br>
<br>
(eq? ((car cycle)) cycle) <img src="math/csug/0.gif" alt="<graphic>"> #t</tt>
<p>The definition below expands <tt>rec</tt> in terms of <tt>letrec</tt>.
<p>
<p><tt>(define-syntax rec<br>
(syntax-rules ()<br>
[(_ x e) (letrec ((x e)) x)]))</tt>
<p>
<h3><a name="g40"></a><a name="./binding:h4"></a>Section 5.4. Fluid Bindings</h3>
<p>
<a name="./binding:s19"></a><span class=formdef><b>syntax</b>: <tt>(fluid-let ((<i>var</i> <i>expr</i>) ...) <i>body<sub>1</sub></i> <i>body<sub>2</sub></i> ...)</tt></span>
<br>
<b>returns: </b>the values of the body <tt><i>body<sub>1</sub></i> <i>body<sub>2</sub></i> ...</tt>
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p><a name="./binding:s20"></a><a name="./binding:s21"></a>The syntactic form <tt>fluid-let</tt>
provides a way to temporarily assign values to a set of variables.
The new values are in effect only during the evaluation of the
body of the <tt>fluid-let</tt> expression.
The scopes of the variables are not determined by <tt>fluid-let</tt>; as with
<tt>set!</tt>, the variables must be bound at top level or by an enclosing
<tt>lambda</tt> or other binding form.
It is possible, therefore, to control the scope of a variable with
<tt>lambda</tt> or <tt>let</tt> while establishing a temporary
value with <tt>fluid-let</tt>.
<p>
Although it is similar in appearance to <tt>let</tt>, its operation is more
like that of <tt>set!</tt>.
Each <tt><i>var</i></tt> is assigned, as with <tt>set!</tt>, to the value of the
corresponding <tt><i>expr</i></tt> within the body <tt><i>body<sub>1</sub></i> <i>body<sub>2</sub></i> ...</tt>.
Should the body
exit normally or by invoking a continuation made outside of the body
(see <tt>call/cc</tt>), the values in effect before the bindings were changed
are restored.
Should control return back to the body by the invocation of a continuation
created within the body, the bindings are changed once again to the values
in effect when the body last exited.
<p>
Fluid bindings are most useful for
maintaining variables that must be shared by a group of procedures.
Upon entry to the group of procedures, the shared variables are fluidly
bound to a new set of initial values so that on exit the original values
are restored automatically.
In this way, the group of procedures itself can be reentrant; it may call
itself directly or indirectly without affecting the values of its shared
variables.
<p>
<a name="./binding:s22"></a>Fluid bindings are similar to
<i>special</i> bindings in Common Lisp [<a class=citation href="./bibliography.html#g175">29</a>], except that
(1) there is a single namespace for both lexical and fluid bindings, and
(2) the scope of a fluidly bound variable is not necessarily global.
<p>
<p><tt>(let ([x 3])<br>
(+ (fluid-let ([x 5])<br>
x)<br>
x)) <img src="math/csug/0.gif" alt="<graphic>"> 8
<br>
<br>
(let ([x 'a])<br>
(letrec ([f (lambda (y) (cons x y))])<br>
(fluid-let ([x 'b])<br>
(f 'c)))) <img src="math/csug/0.gif" alt="<graphic>"> (b . c)
<br>
<br>
(let ([x 'a])<br>
(call/cc<br>
(lambda (k)<br>
(fluid-let ([x 'b])<br>
(letrec ([f (lambda (y) (k '*))])<br>
(f '*)))))<br>
x) <img src="math/csug/0.gif" alt="<graphic>"> a</tt>
<p><tt>fluid-let</tt> may be defined in terms of <tt>dynamic-wind</tt> as follows.
<p>
<p><tt>(define-syntax fluid-let<a name="defn:fluid-let"></a><br>
(lambda (x)<br>
(syntax-case x ()<br>
[(_ () b1 b2 ...) #'(let () b1 b2 ...)]<br>
[(_ ((x e) ...) b1 b2 ...)<br>
(andmap identifier? #'(x ...))<br>
(with-syntax ([(y ...) (generate-temporaries #'(x ...))])<br>
#'(let ([y e] ...)<br>
(let ([swap (lambda ()<br>
(let ([t x]) (set! x y) (set! y t))<br>
...)])<br>
(dynamic-wind swap (lambda () b1 b2 ...) swap))))])))</tt>
<p>
<h3><a name="g41"></a><a name="./binding:h5"></a>Section 5.5. Top-Level Bindings<a name="SECTBINDINGTOPLEVEL"></a></h3>
<p>
The procedures described in this section allow the direct manipulation
of <a name="./binding:s23"></a>top-level bindings for variables
and keywords.
They are intended primarily to support the definition of interpreters
or compilers for Scheme in Scheme but may be used to access or alter
top-level bindings anywhere within a program whether at top level or not.
<p>
<a name="./binding:s24"></a><span class=formdef><b>procedure</b>: <tt>(define-top-level-value <i>symbol</i> <i>obj</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(define-top-level-value <i>symbol</i> <i>obj</i> <i>env</i>)</tt></span>
<br>
<b>returns: </b>unspecified
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p><tt>define-top-level-value</tt> is used to establish a binding
for the variable named by <tt><i>symbol</i></tt> to the value <tt><i>obj</i></tt>
in the environment <tt><i>env</i></tt>.
If <tt><i>env</i></tt> is not provided, it defaults to the
value of <tt>interaction-environment</tt>, i.e., the
top-level evaluation environment
(Section <a href="./system.html#g110">12.3</a>).
<p>
An exception is raised with condition type <tt>&assertion</tt> if
<tt><i>env</i></tt> is not mutable.
<p>
A call to <tt>define-top-level-value</tt> is similar to a top-level
<a name="./binding:s25"></a><tt>define</tt> form, except that a call to
<tt>define-top-level-value</tt> need not occur at top-level and
the variable for which the binding is to be established can be
determined at run time, as can the environment.
<p>
<p><tt>(begin<br>
(define-top-level-value 'xyz "hi")<br>
xyz) <img src="math/csug/0.gif" alt="<graphic>"> "hi"
<br>
<br>
(let ([var 'xyz])<br>
(define-top-level-value var "mom")<br>
(list var xyz)) <img src="math/csug/0.gif" alt="<graphic>"> (xyz "mom")</tt>
<p><a name="./binding:s26"></a><span class=formdef><b>procedure</b>: <tt>(set-top-level-value! <i>symbol</i> <i>obj</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(set-top-level-value! <i>symbol</i> <i>obj</i> <i>env</i>)</tt></span>
<br>
<b>returns: </b>unspecified
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p><a name="./binding:s27"></a><tt>set-top-level-value!</tt> assigns
the variable named by <tt><i>symbol</i></tt> to the value <tt><i>obj</i></tt>
in the environment <tt><i>env</i></tt>.
If <tt><i>env</i></tt> is not provided, it defaults to the
value of <tt>interaction-environment</tt>, i.e., the
top-level evaluation environment
(Section <a href="./system.html#g110">12.3</a>).
<p>
An exception is raised with condition type <tt>&assertion</tt> if the
identifier named by <tt><i>symbol</i></tt> is not defined as a variable in <tt><i>env</i></tt>
or if the variable or environment is not mutable.
<p>
<tt>set-top-level-value!</tt> is similar to
<a name="./binding:s28"></a><tt>set!</tt> when <tt>set!</tt>
is used on top-level variables except that the variable to be assigned
can be determined at run time, as can the environment.
<p>
<p><tt>(let ([v (let ([cons list])<br>
(set-top-level-value! 'cons +)<br>
(cons 3 4))])<br>
(list v (cons 3 4))) <img src="math/csug/0.gif" alt="<graphic>"> ((3 4) 7)</tt>
<p><a name="./binding:s29"></a><span class=formdef><b>procedure</b>: <tt>(top-level-value <i>symbol</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(top-level-value <i>symbol</i> <i>env</i>)</tt></span>
<br>
<b>returns: </b>the top-level value of the variable named by <tt><i>symbol</i></tt> in <tt><i>env</i></tt>
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>If <tt><i>env</i></tt> is not provided, it defaults to the
value of <tt>interaction-environment</tt>, i.e., the
top-level evaluation environment
(Section <a href="./system.html#g110">12.3</a>).
<p>
An exception is raised with condition type <tt>&assertion</tt> if the
identifier named by <tt><i>symbol</i></tt> is not defined as a variable in <tt><i>env</i></tt>.
<p>
<tt>top-level-value</tt> is similar to a top-level variable reference
except that the variable to be referenced can be determined at run time,
as can the environment.
<p>
<p><tt>(let ([cons +])<br>
(list (cons 3 4)<br>
((top-level-value 'cons) 3 4))) <img src="math/csug/0.gif" alt="<graphic>"> (7 (3 . 4))
<br>
<br>
(define e (copy-environment (scheme-environment)))<br>
(define-top-level-value 'pi 3.14 e)<br>
(top-level-value 'pi e) <img src="math/csug/0.gif" alt="<graphic>"> 3.14<br>
(set-top-level-value! 'pi 3.1416 e)<br>
(top-level-value 'pi e) <img src="math/csug/0.gif" alt="<graphic>"> 3.1416</tt>
<p><a name="./binding:s30"></a><span class=formdef><b>procedure</b>: <tt>(top-level-bound? <i>symbol</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(top-level-bound? <i>symbol</i> <i>env</i>)</tt></span>
<br>
<b>returns: </b><tt>#t</tt> if <tt><i>symbol</i></tt> is defined as a variable in <tt><i>env</i></tt>, <tt>#f</tt> otherwise
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>If <tt><i>env</i></tt> is not provided, it defaults to the
value of <tt>interaction-environment</tt>, i.e., the
top-level evaluation environment
(Section <a href="./system.html#g110">12.3</a>).
<p>
This predicate is useful in an interpreter to check for the existence of
a top-level binding before requesting the value with
<tt>top-level-value</tt>.
<p>
<p><tt>(top-level-bound? 'xyz) <img src="math/csug/0.gif" alt="<graphic>"> #f
<br>
<br>
(begin<br>
(define-top-level-value 'xyz 3)<br>
(top-level-bound? 'xyz)) <img src="math/csug/0.gif" alt="<graphic>"> #t
<br>
<br>
(define e (copy-environment (interaction-environment)))<br>
(define-top-level-value 'pi 3.14 e)<br>
(top-level-bound? 'pi) <img src="math/csug/0.gif" alt="<graphic>"> #f<br>
(top-level-bound? 'pi e) <img src="math/csug/0.gif" alt="<graphic>"> #t</tt>
<p><a name="./binding:s31"></a><span class=formdef><b>procedure</b>: <tt>(top-level-mutable? <i>symbol</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(top-level-mutable? <i>symbol</i> <i>env</i>)</tt></span>
<br>
<b>returns: </b><tt>#t</tt> if <tt><i>symbol</i></tt> is mutable in <tt><i>env</i></tt>, <tt>#f</tt> otherwise
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>If <tt><i>env</i></tt> is not provided, it defaults to the
value of <tt>interaction-environment</tt>, i.e., the
top-level evaluation environment
(Section <a href="./system.html#g110">12.3</a>).
<p>
This predicate is useful in an interpreter to check whether a variable
can be assigned before assigning it with
<tt>set-top-level-value!</tt>.
<p>
<p><tt>(define xyz 3)<br>
(top-level-mutable? 'xyz) <img src="math/csug/0.gif" alt="<graphic>"> #t<br>
(set-top-level-value! 'xyz 4)<br>
(top-level-value 'xyz) <img src="math/csug/0.gif" alt="<graphic>"> 4
<br>
<br>
(define e (copy-environment (interaction-environment) #f))<br>
(top-level-mutable? 'xyz e) <img src="math/csug/0.gif" alt="<graphic>"> #f<br>
(set-top-level-value! 'xyz e) <img src="math/csug/0.gif" alt="<graphic>"> <i>exception: xyz is immutable</i></tt>
<p><a name="./binding:s32"></a><span class=formdef><b>procedure</b>: <tt>(define-top-level-syntax <i>symbol</i> <i>obj</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(define-top-level-syntax <i>symbol</i> <i>obj</i> <i>env</i>)</tt></span>
<br>
<b>returns: </b>unspecified
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p><tt>define-top-level-syntax</tt> is used to establish a top-level binding
for the identifier named by <tt><i>symbol</i></tt> to the value of <tt><i>obj</i></tt>
in the environment <tt><i>env</i></tt>.
The value must be a procedure, the result of a call to
<tt>make-variable-transformer</tt>, or the result of a call to
<tt>top-level-syntax</tt>.
If <tt><i>env</i></tt> is not provided, it defaults to the
value of <tt>interaction-environment</tt>, i.e., the
top-level evaluation environment
(Section <a href="./system.html#g110">12.3</a>).
<p>
An exception is raised with condition type <tt>&assertion</tt> if
<tt><i>env</i></tt> is not mutable.
<p>
A call to <tt>define-top-level-syntax</tt> is similar to a top-level
<a name="./binding:s33"></a><tt>define-syntax</tt> form, except that a call to
<tt>define-top-level-syntax</tt> need not occur at top-level and
the identifier for which the binding is to be established can be
determined at run time, as can the environment.
<p>
<p><tt>(define-top-level-syntax 'let1<br>
(syntax-rules ()<br>
[(_ x e b1 b2 ...) (let ([x e]) b1 b2 ...)]))<br>
(let1 a 3 (+ a 1)) <img src="math/csug/0.gif" alt="<graphic>"> 4</tt>
<p><tt>define-top-level-syntax</tt> can also be used to attach
to an identifier arbitrary compile-time bindings obtained
via <tt>top-level-syntax</tt>.
<p>
<a name="./binding:s34"></a><span class=formdef><b>procedure</b>: <tt>(top-level-syntax <i>symbol</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(top-level-syntax <i>symbol</i> <i>env</i>)</tt></span>
<br>
<b>returns: </b>unspecified
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p><tt>top-level-syntax</tt> is used to retrieve the transformer, compile-time
value, or other compile-time binding to which
the identifier named by <tt><i>symbol</i></tt> is bound in the environment <tt><i>env</i></tt>.
If <tt><i>env</i></tt> is not provided, it defaults to the
value of <tt>interaction-environment</tt>, i.e., the
top-level evaluation environment
(Section <a href="./system.html#g110">12.3</a>).
All identifiers bound in an environment have compile-time bindings, including
variables.
<p>
An exception is raised with condition type <tt>&assertion</tt> if the
identifier named by <tt><i>symbol</i></tt> is not defined as a keyword in <tt><i>env</i></tt>.
<p>
<p><tt>(define-top-level-syntax 'also-let (top-level-syntax 'let))<br>
(also-let ([x 3] [y 4]) (+ x y)) <img src="math/csug/0.gif" alt="<graphic>"> 7
<br>
<br>
(define foo 17)<br>
(define-top-level-syntax 'also-foo (top-level-syntax 'foo))<br>
also-foo <img src="math/csug/0.gif" alt="<graphic>"> 17<br>
(set! also-foo 23)<br>
also-foo <img src="math/csug/0.gif" alt="<graphic>"> 23<br>
foo <img src="math/csug/0.gif" alt="<graphic>"> 23</tt>
<p>The effect of the last example can be had more clearly with <tt>alias</tt>:
<p>
<p><tt>(define foo 17)<br>
(alias also-foo foo)<br>
also-foo <img src="math/csug/0.gif" alt="<graphic>"> 17<br>
(set! also-foo 23)<br>
also-foo <img src="math/csug/0.gif" alt="<graphic>"> 23<br>
foo <img src="math/csug/0.gif" alt="<graphic>"> 23</tt>
<p><a name="./binding:s35"></a><span class=formdef><b>procedure</b>: <tt>(top-level-syntax? <i>symbol</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(top-level-syntax? <i>symbol</i> <i>env</i>)</tt></span>
<br>
<b>returns: </b><tt>#t</tt> if <tt><i>symbol</i></tt> is bound as a keyword in <tt><i>env</i></tt>, <tt>#f</tt> otherwise
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>If <tt><i>env</i></tt> is not provided, it defaults to the
value of <tt>interaction-environment</tt>, i.e., the
top-level evaluation environment
(Section <a href="./system.html#g110">12.3</a>).
<p>
All identifiers bound in an environment have compile-time bindings, including
variables, so this predicate amounts to a bound check, but is more general
than <tt>top-level-bound?</tt>, which returns true only for bound variables.
<p>
<p><tt>(define xyz 'hello)<br>
(top-level-syntax? 'cons) <img src="math/csug/0.gif" alt="<graphic>"> #t<br>
(top-level-syntax? 'lambda) <img src="math/csug/0.gif" alt="<graphic>"> #t<br>
(top-level-syntax? 'hello) <img src="math/csug/0.gif" alt="<graphic>"> #t
<br>
<br>
(top-level-syntax? 'cons (scheme-environment)) <img src="math/csug/0.gif" alt="<graphic>"> #t<br>
(top-level-syntax? 'lambda (scheme-environment)) <img src="math/csug/0.gif" alt="<graphic>"> #t<br>
(top-level-syntax? 'hello (scheme-environment)) <img src="math/csug/0.gif" alt="<graphic>"> #f</tt>
<p>
<hr class=copyright align=left>
<p>
<a class=plain href="index.html">Chez Scheme Version 9 User's Guide</a><br>
Copyright © 2016 Cisco Systems, Inc.<br>
Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License Version 2.0</a>
(<a class=plain href="canned/copyright.html">full copyright notice.</a>).</br>
Revised June 2016 for Chez Scheme Version 9.4<br>
<a class=plain href="canned/about.html">about this book</a>
</tr></table>
</body>
</html>