-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsrfi-72-1.2.html
1268 lines (1047 loc) · 40.2 KB
/
srfi-72-1.2.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
<html>
<head>
<title>SRFI 72: Hygienic macros</title>
</head>
<body>
<H1>Title</H1>
Hygienic macros.
<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/08/14, or as amended. To
provide input on this SRFI, please <CODE>
<A HREF="mailto:[email protected]">mailto:[email protected]</A></CODE>.
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-72/mail-archive/maillist.html">the
archive of the mailing list</A>.
<P>
<ul>
<li>Received: 2005/06/05</li>
<li>Draft: <a href="http://srfi.schemers.org/srfi-72/srfi-72-1.1.html">2005/06/14 - 2005/04/14</a></li>
<li>Revised: <a href="http://srfi.schemers.org/srfi-72/srfi-72-1.2.html">2005/07/01</a></li>
</ul>
<h1>Index</h1>
<ul>
<li>
<a href="#abstract">Abstract</a>
</li>
<li>
<a href="#intro">Introduction</a>
</li>
<li>
<a href="#hygiene">Improved hygiene</a>
</li>
<li>
<a href="#breaking">Improved hygiene breaking</a>
</li>
<li>
<a href="#correlation">Source-object correlation</a>
</li>
<li>
<a href="#spec">Specification</a>
</li>
<li>
<a href="#implementation">Implementation</a>
</li>
<li>
<a href="#refs">References</a>
</li>
</ul>
<a name="abstract"></a>
<h1>Abstract</h1>
This SRFI describes a procedural macro proposal for Scheme with the following
features:
<ul>
<li>
<h3>Improved hygiene:</h3>
<p>
Existing hygienic macro algorithms work well for high-level macros, but
do not solve the
unintended variable capture problem in the case of procedural macros.
The proposal described here identifies and solves a class of
capture problems that may occur in procedural macros.
</li>
<p>
<li>
<h3>Composable hygiene-breaking macros:</h3>
<p>
A primitive is provided for writing hygiene-breaking macros
that are composable and referentially transparent, correcting
a known shortcoming of some existing macro systems.
</li>
<p>
<li>
<h3>Minimal:</h3>
<p>
The design provides a small number of core primitives.
More complex features, including pattern matching and substitution facilities
such as <tt>syntax-case</tt> and <tt>quasisyntax</tt>, are specified as library syntax.
</li>
<p>
<li>
<h3>Syntax-case:</h3>
<p>
<tt>Syntax-case</tt> is expressible as a macro in this proposal and specified as library syntax.
The version specified here incorporates a proposal for improved hygiene in procedural macros.
</li>
<p>
<li>
<h3>Fast hygiene algorithm:</h3>
<p>
The reference implementation documents
an imperative hygiene algorithm that is eager,
has linear complexity, and is very fast.
</li>
<p>
<li>
<h3>Source-object correlation:</h3>
<p>
Source correlation information is not tied to syntax
objects, but instead recorded separately by the expander,
allowing not just source location but also
intermediate expansion steps to be tracked.
</li>
<p>
<li>
<h3>Procedural interface:</h3>
<p>
The primitive interface to compound syntax objects is procedural
rather than through special forms. In particular,
the usual and very useful abstractions <tt>car</tt>, <tt>cdr</tt>, <tt>cons</tt>
, <tt>...</tt>, for manipulating syntactic objects in a Lisp are available to the
macro writer.
</li>
<p>
<li>
<h3>Tractable implementation:</h3>
<p>
The reference implementation is small and tractable, written in mostly portable
R5RS Scheme without using R5RS macros. It should run out of the box on
most existing Schemes.
</li>
</ul>
<a name="intro"></a>
<h1>Introduction</h1>
We start with a simple example:
<pre>
(define-syntax (swap! a b)
(quasisyntax
(let ((temp ,a))
(set! ,a ,b)
(set! ,b temp))))
</pre>
This macro builds a syntax object using the <tt>quasisyntax</tt> substitution
form.
Syntax provided as part
of the input expression is inserted in the result using <tt>unquote</tt>
or <tt>unquote-splicing</tt>. Macros written in this way are hygienic
and referentially transparent. For example,
<pre>
(let ((temp 1)
(set! 2))
(swap! set! temp)
(values temp set!)) ==> 2 1
</pre>
The above macro may also be written as
<pre>
(define-syntax swap!
(lambda (form)
(let ((a (cadr form))
(b (caddr form)))
`(,(syntax let) ((,(syntax temp) ,a))
(,(syntax set!) ,a ,b)
(,(syntax set!) ,b ,(syntax temp))))))
</pre>
showing that <tt>quasisyntax</tt> may itself be defined as library syntax
in terms of a primitive <tt>syntax</tt> form [12].
<p>
The example illustrates that we can use
the traditional and very useful abstractions <tt>car</tt>, <tt>cdr</tt>, <tt>...</tt>,
for handling compound syntax objects in a Lisp.
Indeed, the core interface to compound
syntax objects is procedural rather than via special forms.
<p>
For comparing identifiers, primitives <tt>free-identifier=?</tt>,
<tt>bound-identifier=?</tt>
and <tt>literal-identifier=?</tt> are provided.
For example, a simplified <tt>cond</tt> macro can be written as follows:
<pre>
(define-syntax (my-cond clause . clauses)
(or (and (pair? clause)
(pair? (cdr clause)))
(syntax-error))
(cond ((literal-identifier=? (car clause)
(syntax else)) (quasisyntax (begin ,@(cdr clause))))
((null? clauses) (quasisyntax (if ,(car clause) (begin ,@(cdr clause)))))
(else (quasisyntax (if ,(car clause)
(begin ,@(cdr clause))
(my-cond ,@clauses))))))
(my-cond (#f 1) (else 2)) ==> 2
(let ((else #f)) (my-cond (else 2))) ==> unspecified
</pre>
<tt>Syntax-case</tt> is expressible as a macro and
specified as library syntax,
so that we can write, for example.
<pre>
(define-syntax my-cond
(lambda form
(syntax-case form (else)
((_ (else e1 e2 ...)) (syntax (begin e1 e2 ...)))
((_ (e0 e1 e2 ...)) (syntax (if e0 (begin e1 e2 ...))))
((_ (e0 e1 e2 ...) c1 c2 ...) (syntax (if e0
(begin e1 e2 ...)
(my-cond c1 c2 ...)))))))
</pre>
While <tt>syntax-case</tt> combines the concerns of pattern
matching and substitution, these concerns may be separated.
For example, the programmer may also write
<pre>
(define-syntax my-cond
(lambda (form)
(syntax-case form (else)
((_ (else e1 e2 ...)) (quasisyntax (begin ,e1 ,e2 ...)))
((_ (e0 e1 e2 ...)) (quasisyntax (if ,e0 (begin ,e1 ,e2 ...))))
((_ (e0 e1 e2 ...) c1 c2 ...) (quasisyntax (if ,e0
(begin ,e1 ,e2 ...)
(my-cond ,c1 ,c2 ...)))))))
</pre>
One is not limited to using <tt>syntax-case</tt> for matching.
More general pattern matchers are easily integrated with the system
described here [1].
<a name="hygiene"></a><h1>Improved Hygiene</h1>
Existing hygiene algorithms are well suited for
<tt>syntax-rules</tt> macros but still suffer from accidental
variable capture problems in procedural macros.
<p>
In the proposal of this SRFI, <tt>quasisyntax</tt>,
<tt>syntax-case</tt>, and <tt>with-syntax</tt> are specified
so that the following macro will give the
answer <tt>1</tt>
<pre>
(define-syntax (no-capture)
(define (helper value)
(quasisyntax
(let ((temp 2)) ,value)))
(quasisyntax
(let ((temp 1))
,(helper (syntax temp)))))
(no-capture) ==> 1
</pre>
or equivalently
<pre>
(define-syntax (no-capture)
(define (helper value)
(with-syntax ((value value))
(syntax (let ((temp 2)) value))))
(with-syntax ((nested (helper (syntax temp))))
(syntax (let ((temp 1))
nested))))
(no-capture) ==> 1
</pre>
whereas the same macro would give the answer <tt>2</tt>
in existing systems due to a variable capture.
Note that if the helper procedure were written as a macro, one would
expect to obtain the answer <tt>1</tt> due to automatic hygiene. In
the current proposal, the macro
writer is not penalized for using a procedure instead.
<p>
Next consider the following macros for a version of <tt>let</tt>
with left to right evaluation. With the proposal of this SRFI, this
definition is correct:
<pre>
(define-syntax let-in-order
(lambda (form)
(define (let-help form temps vars)
(syntax-case form ()
((_ () . body)
(with-syntax (((var ...) vars)
((temp ...) temps))
(syntax
(let ((var temp) ...) . body))))
((_ ((var exp) binding ...) . body)
(with-syntax
((rest (let-help (syntax (_ (binding ...) . body))
(cons (syntax temp) temps)
(cons (syntax var) vars))))
(syntax
(let ((temp exp)) rest))))))
(let-help form '() '())))
(let-in-order ((x 1)
(y 2))
(+ x y)) ==> 3
</pre>
or equivalently
<pre>
(define-syntax (let-in-order bindings . body)
(define (let-help bindings temps vars)
(cond ((null? bindings) (quasisyntax
(let ,(map list vars temps) ,@body)))
((pair? bindings) (quasisyntax
(let ((temp ,(cadar bindings)))
,(let-help (cdr bindings)
(cons (syntax temp) temps)
(cons (caar bindings) vars)))))))
(let-help bindings '() '()))
(let-in-order ((x 1)
(y 2))
(+ x y)) ==> 3
</pre>
whereas existing systems will give the wrong answer <tt>4</tt> due to
variable capture.
Again, if the helper were
expressed as a macro, one would not expect variable capture. With the
current proposal, this good behaviour is guaranteed also for
procedural helpers.
<p>
To preserve hygiene,
identifiers introduced during a macro invocation are effectively renamed
to prevent accidental capture. In
existing systems only one renaming function is
typically used during the entire dynamic extent of the macro
invocation.
This means that accidental captures may still take
place in
procedural macros
unless the programmer keeps careful track of
all identifiers introduced in different parts of the macro and
all its helper procedures. This burden is reminiscent of the difficulties
one encounters in languages with dynamic scoping of variables.
It limits the modularity and constrains the maintainability
of complex macros.
Especially if code is generated recursively, as in the
<tt>let-in-order</tt> macro, it may be quite hard, in these systems,
to verify whether accidental captures occur.
<p>
The solution proposed here is based on a form
<tt>with-fresh-renaming-scope</tt> that effectively causes a fresh renaming
function to be used in the static region following this keyword.
Identifiers introduced while expanding this region cannot capture or be
captured by identifiers introduced outside this region.
It is important to note that this is a lexical, not a dynamic, construct.
For example, we have
<pre>
(let ((f (lambda () (syntax x))))
(with-fresh-renaming-scope
(bound-identifier=? (syntax x)
(f)))) ==> #f
</pre>
where <tt>bound-identifier=?</tt> non-equivalence means that a binding of
one identifier cannot capture references to the other.
<p>
It should be clear from studying the examples of this section
that the capture
problems occur when splicing together syntax generated in a lexically
separate parts of the program text. We can therefore largely avoid these
capture problems by requiring all <i>substitution forms</i> to
expand to an occurrence of <tt>with-fresh-renaming-scope</tt>.
<p>
In the current proposal, the substitution forms are <tt>quasisyntax</tt>,
<tt>syntax-case</tt> and <tt>with-syntax</tt>, which are indeed defined in this way. With this
specification, the above macros all behave correctly as they stand.
<a name="breaking"></a><h1>Improved hygiene breaking</h1>
We would like to write an unhygienic macro <tt>if-it</tt> that assigns
the value of its condition to the identifier <tt>it</tt> in its consequent
and alternative.
<pre>
(if-it 1 it 42) ==> 1
</pre>
We would also like to be able to freely compose unhygienic macros, something
that is
notoriously difficult to do in existing systems.
In particular, we would like to be able to write macros <tt>when-it</tt>,
<tt>if-flag-it</tt> and <tt>my-or</tt> in terms of <tt>if-it</tt> as follows:
<pre>
(define-syntax (when-it condition consequent)
(quasisyntax
(if-it ,condition
,consequent
(if #f #f))))
(define-syntax (if-flag-it body else)
(quasisyntax
(if-it flag ,body ,else)))
(define-syntax (my-or expr1 expr2)
(quasisyntax
(if-it ,expr1 it ,expr2)))
</pre>
These macros should behave as follows:
<pre>
(when-it 42 it) ==> 42
(define flag 3)
(if-flag-it it 'none) ==> 3
(my-or 2 it) ==> 2
(my-or #f it) ==> #f
</pre>
The macro system described here has a primitive <tt>datum->syntax</tt>
similar to that provided in the Chez <tt>syntax-case</tt> specification.
However,
as far as the author is aware, it is impossible to satisfy these four
conditions using <tt>datum->syntax</tt> without code-walking.
Furthermore, we may wish to impose
referential transparency requirements such as
<pre>
(let ((it 1)) (if-it 42 it #f)) ==> 1
(let ((it 1)) (when-it 42 it)) ==> 1
(let ((it 1)) (my-or 2 it)) ==> 2
(let ((it 1)) (my-or #f it)) ==> 1
</pre>
by analogy with the behaviour of
<pre>
(let ((else #f)) (my-cond (else 2))) ==> void
</pre>
To satisfy these requirements, we provide a new primitive,
<tt>make-capturing-identifier</tt>, that introduces an identifier which, when
bound, will capture all <tt>free-identifier=?</tt> identifiers in its scope.
With this primitive, the following implementation of <tt>if-it</tt> satisfies
all the above requirements:
<pre>
(define-syntax (if-it condition consequent alternative)
(let ((it (make-capturing-identifier (syntax here) 'it)))
(quasisyntax
(let ((,it ,condition))
(if ,it
,consequent
,alternative)))))
</pre>
A similar idea has been proposed by Oleg Kiselyov in [2].
<p>
The meaning of the new identifier is determined by the lexical
binding of the first argument. This allows us fine control
over what we mean by referential transparency. Compare for example the
above with:
<pre>
(define-syntax if-it
(lambda (form)
(let ((it (make-capturing-identifier (car form) 'it)))
(quasisyntax
(let ((,it ,(cadr form)))
(if ,it
,(caddr form)
,(cadddr form)))))))
(let ((it 1)) (if-it 42 it #f)) ==> 42
</pre>
<p>
The primitive <tt>datum->syntax</tt> is still the
appropriate primitive for introducing identifiers that should be captured
by <tt>bound-identifier=?</tt> identifiers in surrounding binding forms.
Comparing with the description of <tt>make-capturing-identifier</tt> above,
we see that the one introduces identifiers that are the <i>subject</i> of capture,
while the other introduces identifiers that should be the <i>object</i> of capture.
<a name="correlation"></a><h1>
Source-object correlation:
</h1>
Source correlation information is not tied to syntax
objects, but instead recorded separately by the expander.
In addition to tracking source location, this also allows intermediate expansion
steps from
the source to the object code to be recorded and made available to
tools.
<a name="spec"></a>
<h1>Specification</h1>
The following primitive forms are provided:
<pre> define-syntax
let-syntax
letrec-syntax
set-syntax!
identifier?
bound-identifier=?
free-identifier=?
literal-identifier=?
syntax
syntax-quote
with-fresh-renaming-scope
make-capturing-identifier
datum->syntax
syntax->datum
expand
syntax-debug
syntax-error
</pre>
The following library forms are provided:
<pre> quasisyntax
syntax-case
with-syntax
syntax-rules
</pre>
<dl>
<dt><pre><b>Syntax objects:</b></pre>
<dd>A syntax object is a graph whose nodes are Scheme pairs or vectors
and whose leaves are constants or identifiers. The following expressions
evaluate to syntax objects:
<pre>
'()
1
#f
'(1 2 3)
(cons (syntax x) (vector 1 2 3 (syntax y)))
(syntax (let ((x 1)) x))
(quasisyntax (let ((x 1)) ,(syntax x)))
</pre>
Symbols may not appear in syntax objects:
<pre>
'(let ((x 1)) x) ==> not a syntax object
</pre>
<dt><pre><b>syntax: (DEFINE-SYNTAX var exp)
(DEFINE-SYNTAX (var . formals) exp1 exp ...)</b></pre>
<dd> <tt>Exp</tt> is expanded and then evaluated in the current top level
environment, <tt>var</tt> is bound to a top level location, and the
resulting value is stored in the location.
<p>
The second variant is equivalent to
<pre>
(DEFINE-SYNTAX var
(let ((transformer (lambda (dummy . formals) exp1 exp ...)))
(lambda (form)
(apply transformer form))))
</pre>
<tt>Exp</tt> may, but does not have to, evaluate to a procedure,
also called a transformer.
<dt>
<pre><b>syntax: (LET[REC]-SYNTAX ((var exp) ...) exp* ...)</b>
</pre>
<dd>
These primitives have the semantics described in R5RS:
<pre>
(let ((x 'outer))
(let-syntax ((m (lambda (form) (syntax x))))
(let ((x 'inner))
(m)))) ==> outer
(let-syntax ((when (lambda (form)
(quasisyntax (if ,(cadr form)
(begin ,@(cddr form)))))))
(let ((if #t))
(when if (set! if 'now))
if)) ==> now
</pre>
The language for expanding further nested macros is incrementally
extended, as the following example shows:
<pre>
(let ((x 1))
(let-syntax ((m (lambda (form) (syntax (syntax x)))))
(let-syntax ((n (lambda (form) (m))))
(n))))
==> 1
</pre>
<dt>
<pre><b>syntax: (SET-SYNTAX! var exp)</b>
</pre>
<dd> <tt>Set-syntax</tt> is to <tt>define-syntax</tt> as <tt>set!</tt> is to <tt>define</tt>.
<pre>
(define-syntax (test) (syntax (syntax 'a)))
(set-syntax! test (lambda (form) (test)))
(test) ==> a
</pre>
<dt>
<pre><b>procedure: (IDENTIFIER? obj)</b>
</pre>
<dd>
Returns <tt>#t</tt> if <tt>obj</tt> is an identifier, <tt>#f</tt> otherwise.
<dt>
<pre>
<b>procedure: (BOUND-IDENTIFIER=? obj1 obj2)
(FREE-IDENTIFIER=? obj1 obj2)
(LITERAL-IDENTIFIER=? obj1 obj2)</b>
</pre>
<dd>
Identifiers are <tt>free-identifier=?</tt> if they refer to the same lexical or
toplevel binding. For this purpose, all identifiers that are not lexically bound are
considered implicitly bound at the toplevel.
<p>
Identifiers are <tt>literal-identifier=?</tt> if they are <tt>free-identifier=?</tt> or
if they both refer to toplevel bindings and have the same symbolic name.
This primitive should be used to reliably identify literals
(such as <tt>else</tt> in <tt>cond</tt>) even if they occur in a different module
from the macro definition.
<p>
Identifiers are <tt>bound-identifier=?</tt> if a binding of one would capture
references to the other in the scope of the binding. Two identifiers
with the same name are <tt>bound-identifier=?</tt> if they were present in the same toplevel
expression in the original program text. Identifiers will also
be <tt>bound-identifier=?</tt> if they were created by applying <tt>syntax</tt> to existing <tt>bound-identifier=?</tt> identifiers
during the same macro-invocation or invocation of
<tt>with-fresh-renaming-scope</tt>. In addition,
<tt>datum->syntax</tt> may create identifiers that are <tt>bound-identifier=?</tt>
to previously introduced identifiers.
<p>
These procedures return #f if either argument is not an identifier.
<dt>
<pre><b>syntax: (SYNTAX datum)</b>
</pre>
<dd>
Creates a new syntax object from <tt>datum</tt>, which
must be a syntax object, as follows:
Constants contained in <tt>datum</tt> are unaffected, while
identifiers are effectively renamed to obtain fresh identifiers
in the sense of <tt>bound-identifier=?</tt>.
These fresh identifiers remain <tt>free-identifier=?</tt> to the original
identifiers.
This means that a fresh identifier will denote the same thing as the original
identifier in <tt>datum</tt> unless the macro application
places an occurrence of it in a binding position.
<p>
During the course of a single macro invocation, <tt>syntax</tt>
ordinarily acts
like a one-to-one mathematical function on identifiers: Two identifiers created by evaluating <tt>syntax</tt> expressions will be
<tt>bound-identifier=?</tt> if and only if the <tt>syntax</tt> expressions were evaluated during
the same macro invocation and the original identifiers in the templates
were <tt>bound-identifier=?</tt>.
<pre>
(bound-identifier=? (syntax x) (syntax x)) ==> #t
</pre>
This behaviour may be modified by the form
<tt>with-fresh-renaming-scope</tt>, which in effect causes a
fresh renaming function to be used for evaluating <tt>syntax</tt>
expressions occurring in the static region following
this keyword. Note that this is a lexical, not dynamic, construct. For example, one has
<pre>
(let ((f (lambda () (syntax x))))
(with-fresh-renaming-scope
(bound-identifier=? (syntax x)
(f)))) ==> #f
</pre>
<p>
Identifiers that are <tt>bound-identifier=?</tt> are required to also
be <tt>free-identifier=?</tt>, denoting the same binding.
Any attempt to break this invariant should cause an error to be
signaled.
<pre>
(cons (syntax x)
(let ((x 1)) (syntax x))) ==> error
</pre>
<p>
<b>Examples:</b>
<p>
In the following example, <tt>(m)</tt> expands to <tt>(syntax x)</tt>, where
<tt>x</tt> denotes the outer binding. Although the expanded
<tt>(syntax x)</tt> occurs in the syntactic environment of the middle binding,
the fresh identifier resulting from evaluating it will denote the same thing as the
identifier <tt>x</tt> in the template. It will therefore also refer to the outer binding.
<pre>
(let ((x 'outer))
(let-syntax ((m (lambda (_) (syntax (syntax x)))))
(let ((x 'middle))
(let-syntax ((n (lambda (_) (m))))
(let ((x 'inner))
(n)))))) ==> outer
</pre>
Note that <tt>syntax</tt> does not unify identifiers previously distinct in the sense
of <tt>bound-identifier=?</tt> occurring
in <tt>template</tt> even if they
have the same symbolic name:
<pre> (let ((x 1))
(let-syntax ((m (lambda (_) (syntax (syntax x)))))
(let ((x 2))
(let-syntax ((n (lambda (_)
(quasisyntax
(let-syntax ((o (lambda (_)
(,(syntax syntax)
(,(syntax list)
,(m)
,(syntax x))))))
(o))))))
(n))))) ==> (1 2)
</pre>
<dt>
<pre><b>syntax: (SYNTAX-QUOTE datum)</b>
</pre>
<dd>
Returns the existing syntax object <tt>datum</tt> embedded in the
program. Unlike
<tt>syntax</tt>, no new syntax object is constructed.
This primitive is useful for defining certain kinds of
macro-generating macros
that have to compose pieces of code preserving <tt>bound-identifier=?</tt>
equivalence where not all the pieces are passed via the same chain of macro calls.
<p>
For example, in the following fragment, <tt>z</tt> is passed to the inner
macro by two paths, one of them via <tt>x</tt> and then <tt>y</tt>, and the other
via only <tt>x</tt>. Using <tt>syntax-quote</tt>, we can "tunnel"
<tt>x</tt> to the inner macro as follows:
<pre>
(let-syntax ((m (lambda (form)
(syntax-case form ()
((_ x)
(syntax
(let-syntax ((n (lambda (form*)
(syntax-case form* ()
((_ y)
(with-syntax ((x (syntax-quote x)))
(syntax (let ((y 1))
x))))))))
(n x))))))))
(m z)) ==> 1
</pre>
<dt>
<pre><b>syntax: (WITH-FRESH-RENAMING-SCOPE exp1 exp ...)</b>
</pre>
<dd>
Causes a fresh syntactic renaming function to be used in
the lexical region <tt>exp1 exp ...</tt> as described in the
specification of <tt>syntax</tt> above.
<p>
The body <tt>exp1 exp ...</tt> is subject to the rules for
a <tt>lambda</tt> body, and may contain internal definitions.
<dt>
<pre>
<b>procedure: (MAKE-CAPTURING-IDENTIFIER context-identifier symbol)</b>
</pre>
<dd>
This procedure returns a fresh identifier with symbolic
name <tt>symbol</tt>, and with denotation that of <tt>symbol</tt> in
the syntactic environment in which <tt>context-identifier</tt> was
introduced. If the resulting
identifier occurs in a binding, it will capture any identifiers
in the scope of the binding that are <tt>free-identifier=?</tt> to it.
The new identifier is not <tt>bound-identifier=?</tt> to any
existing identifiers.
<pre>
(define-syntax (if-it condition consequent alternative)
(let ((it (make-capturing-identifier (syntax here) 'it)))
(quasisyntax
(let ((,it ,condition))
(if ,it
,consequent
,alternative)))))
(if-it 42 it #f) ==> 42
(let ((it 1)) (if-it 42 it #f)) ==> 1
</pre>
The following examples illustrate how the behaviour
of the capturing identifier is affected by the <tt>context-identifier</tt>
argument. Compare in particular the first example below with the one
above.
<pre>
(define-syntax if-it
(lambda (form)
(let ((it (make-capturing-identifier (car form) 'it)))
(quasisyntax
(let ((,it ,(cadr form)))
(if ,it
,(caddr form)
,(cadddr form)))))))
(let ((it 1)) (if-it 42 it #f)) ==> 42
(let ((y 'outer))
(let-syntax ((m (lambda (_) (make-capturing-identifier (syntax here) 'y))))
(let ((y 'inner))
(m)))) ==> outer
(let ((y 'outer))
(let-syntax ((m (lambda (form)
(let ((y (make-capturing-identifier (syntax here) 'y)))
(quasisyntax
(let ((,y 'inner)) ,@(cdr form)))))))
(m y)))
==> inner
(let ((y 'outer))
(let-syntax ((m (lambda (form)
(let ((y (make-capturing-identifier (syntax here) 'y)))
(quasisyntax
(let ((,y 'inner)) ,@(cdr form)))))))
(let ((y 'more))
(m y))))
==> more
</pre>
<dt>
<pre>
<b>procedure: (DATUM->SYNTAX context-identifier obj)</b>
</pre>
<dd>
Transforms <tt>obj</tt>, which must be a Scheme object with symbols
or constants as leaves, to a syntax object as follows: Constants
in <tt>obj</tt> are unaffected, while
symbols appearing in <tt>obj</tt> are converted to identifiers that behave
under <tt>bound-identifier=?</tt> and <tt>free-identifier=?</tt>
exactly the same as an identifier with the same symbolic name would
behave if it had occurred together
with <tt>context-identifier</tt> in the same source toplevel expression
or was produced during the same evaluation of the <tt>syntax</tt>
expression producing <tt>context-identifier</tt>.
<p>
If <tt>template-identifier</tt> is a capturing identifier, the symbols
in <tt>obj</tt> will also be converted to capturing identifiers.
<pre>
(let ((x 'outer))
(let-syntax ((m (lambda (_) (syntax (syntax z)))))
(let ((x 'middle))
(let-syntax ((n (lambda (_) (datum->syntax (m) 'x))))
(let ((x 'inner))
(n)))))) ==> outer
</pre>
<dt>
<pre>
<b>procedure: (SYNTAX->DATUM syntax-object)</b>
</pre>
<dd>
Transforms a syntax object by replacing
contained identifiers by their symbolic names.
<dt>
<pre>
<b>procedure: (EXPAND syntax-object)</b>
</pre>
<dd>
Expands the syntax object fully to obtain a core Scheme expression.
<pre>
(expand (syntax (let ((x 1)) x))) ==> ((lambda (@x5872) @x5872) 1)
</pre>
<dt>
<pre>
<b>procedure: (SYNTAX-DEBUG syntax-object)</b>
</pre>
<dd> Converts its argument to a human-readable format.
<pre>
(syntax-debug (syntax (let ((x 1)) y))) ==> (let ((x#top 1)) y#top)
</pre>
<dd>
<dt>
<pre>
<b>procedure: (SYNTAX-ERROR obj ...)</b>
</pre>
<dd>
Invokes a syntax error. The objects <tt>obj ...</tt> are displayed,
available source-object correlation information is displayed or
provided to debugging tools, and
the expander is stopped.
<dt>
<pre><b>library syntax: (QUASISYNTAX template)</b>
</pre>
<dd>
Constructs a new syntax object from the template, parts
of which may be unquoted using <tt>unquote</tt> or <tt>unquote-splicing</tt>.
<tt>Quasisyntax</tt> is to <tt>syntax</tt> as <tt>quasiquote</tt> is
to <tt>quote</tt>. <tt>Quasisyntax</tt> may be implemented as a macro
in terms of <tt>with-fresh-renaming-scope</tt> and <tt>syntax</tt>.
<p>
To reliably preserve hygiene in the presence of substitions, the
proposal of the section <a href="#hygiene">Improved hygiene</a> is
implemented. Namely, the expansion of <tt>quasisyntax</tt> includes
an enclosing occurrence of <tt>with-fresh-renaming-scope</tt>,
causing a fresh syntactic renaming function to be used in the static region
lexically following the <tt>quasisyntax</tt> keyword.
<p>
For example, no variable capture
occurs in the following macro:
<pre>
(define-syntax (no-capture)
(define (helper value)
(quasisyntax
(let ((temp 2)) ,value)))
(quasisyntax
(let ((temp 1))
,(helper (syntax temp)))))
(no-capture) ==> 1
</pre>
since, for example, the second <tt>quasisyntax</tt> expression
expands to the equivalent of
<pre>
(with-fresh-renaming-scope
`(,(syntax let) ((,(syntax temp) 1))
,(helper (syntax temp))))
</pre>
See section <a href="#hygiene">Improved hygiene</a> for a more practical
example.
<p>
To make nested <tt>unquote-splicing</tt>
behave in a more useful way than R5RS, the R5RS-compatible extension
described in appendix B of Bawden's paper [10] is recommended.
<p>
For convenient use with <tt>syntax-case</tt>,
<tt>quasisyntax</tt> templates may contain ellipses, which
will be expanded exactly like the corresponding <tt>syntax-rules</tt>
template, with the proviso that the inserted values have to be
explicitly unquoted. Ellipsis-bound values belong to a special
data type and are created when binding <tt>syntax-case</tt>