forked from filonenko-mikhail/cltl2-doc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
backq.tex
588 lines (571 loc) · 23.5 KB
/
backq.tex
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
\clearpage\def\pagestatus{ROUGH PAGES}
\begingroup
\makeatletter
\def\@listi{\leftmargin\leftmargini \labelsep\leftmargin
\parsep 3pt\relax
\topsep 4pt plus 10pt\relax
\itemsep\topsep}
\makeatother
\chapter{Backquote}
\label{BACKQUOTE-SIMULATOR}
\begin{new}
Here is the code for an implementation of backquote syntax
(see section~\ref{BACKQUOTE}) that I have found quite useful
in explaining to myself the behavior of nested backquotes.
It implements the formal rules for backquote processing
and optionally applies a code simplifier to the result.
One must be very careful in choosing the simplification rules;
the rules given here work, but some Common Lisp implementations
have run into trouble at one time or another by using a
simplification rule that does not work in all cases.
Code transformations that are plausible when single forms
are involved are likely to fail in the presence of splicing.
At the end of this appendix are some samples of
nested backquote syntax with commentary.
\begin{lisp}
;;; Common Lisp backquote implementation, written in Common Lisp. \\*
;;; Author: Guy L. Steele Jr.~~~~~Date: 27 December 1985 \\*
;;; Tested under Symbolics Common Lisp and Lucid Common Lisp. \\*
;;; This software is in the public domain.
\end{lisp}
\begin{lisp}
;;; \$ is pseudo-backquote and \% is pseudo-comma.~~This makes it \\*
;;; possible to test this code without interfering with normal \\*
;;; Common Lisp syntax.
\end{lisp}
\begin{lisp}
;;; The following are unique tokens used during processing. \\*
;;; They need not be symbols; they need not even be atoms.
\end{lisp}
\begin{lisp}
(defvar *comma* (make-symbol "COMMA")) \\*
(defvar *comma-atsign* (make-symbol "COMMA-ATSIGN")) \\
(defvar *comma-dot* (make-symbol "COMMA-DOT")) \\
(defvar *bq-list* (make-symbol "BQ-LIST")) \\
(defvar *bq-append* (make-symbol "BQ-APPEND")) \\
(defvar *bq-list** (make-symbol "BQ-LIST*")) \\
(defvar *bq-nconc* (make-symbol "BQ-NCONC")) \\
(defvar *bq-clobberable* (make-symbol "BQ-CLOBBERABLE")) \\
(defvar *bq-quote* (make-symbol "BQ-QUOTE")) \\*
(defvar *bq-quote-nil* (list *bq-quote* nil))
\end{lisp}
\begin{lisp}
;;; Reader macro characters: \\*
;;;~~~~\$foo is read in as (BACKQUOTE foo) \\*
;;;~~~~\%foo is read in as (\#:COMMA foo) \\*
;;;~~~~\%{\Xatsign}foo is read in as (\#:COMMA-ATSIGN foo) \\*
;;;~~~~\%.foo is read in as (\#:COMMA-DOT foo) \\*
;;; where \#:COMMA is the value of the variable *COMMA*, etc.
\end{lisp}
\begin{lisp}
;;; BACKQUOTE is an ordinary macro (not a read-macro) that \\*
;;; processes the expression foo, looking for occurrences of \\*
;;; \#:COMMA, \#:COMMA-ATSIGN, and \#:COMMA-DOT.~~It constructs code \\*
;;; in strict accordance with the rules on pages 349-350 of \\*
;;; the first edition (pages 528-529 of this second edition). \\*
;;; It then optionally applies a code simplifier.
\end{lisp}
\begin{lisp}
(set-macro-character \#{\Xbackslash}\$ \\*
~~\#'(lambda (stream char) \\*
~~~~~~(declare (ignore char)) \\*
~~~~~~(list 'backquote (read stream t nil t))))
\end{lisp}
\begin{lisp}
(set-macro-character \#{\Xbackslash}\% \\*
~~\#'(lambda (stream char) \\*
~~~~~~(declare (ignore char)) \\*
~~~~~~~~(case (peek-char nil stream t nil t) \\*
~~~~~~~~~~(\#{\Xbackslash}{\Xatsign} (read-char stream t nil t) \\*
~~~~~~~~~~~~~~~(list *comma-atsign* (read stream t nil t))) \\
~~~~~~~~~~(\#{\Xbackslash}. (read-char stream t nil t) \\*
~~~~~~~~~~~~~~~(list *comma-dot* (read stream t nil t))) \\*
~~~~~~~~~~(otherwise (list *comma* (read stream t nil t))))))
\end{lisp}
\begin{lisp}
\\*
;;; If the value of *BQ-SIMPLIFY* is non-NIL, then BACKQUOTE \\*
;;; processing applies the code simplifier.~~If the value is NIL, \\*
;;; then the code resulting from BACKQUOTE is exactly that \\*
;;; specified by the official rules.
\end{lisp}
\begin{lisp}
(defparameter *bq-simplify* t)
\end{lisp}
\begin{lisp}
(defmacro backquote (x) \\*
~~(bq-completely-process x))
\end{lisp}
\begin{lisp}
;;; Backquote processing proceeds in three stages: \\*
;;; \\*
;;; (1) BQ-PROCESS applies the rules to remove occurrences of \\*
;;; \#:COMMA, \#:COMMA-ATSIGN, and \#:COMMA-DOT corresponding to \\*
;;; this level of BACKQUOTE.~~(It also causes embedded calls to \\
;;; BACKQUOTE to be expanded so that nesting is properly handled.) \\
;;; Code is produced that is expressed in terms of functions \\
;;; \#:BQ-LIST, \#:BQ-APPEND, and \#:BQ-CLOBBERABLE.~~This is done \\
;;; so that the simplifier will simplify only list construction \\
;;; functions actually generated by BACKQUOTE and will not involve \\
;;; any user code in the simplification.~~\#:BQ-LIST means LIST, \\
;;; \#:BQ-APPEND means APPEND, and \#:BQ-CLOBBERABLE means IDENTITY \\
;;; but indicates places where "\%." was used and where NCONC may \\*
;;; therefore be introduced by the simplifier for efficiency. \\*
;;; \\*
;;; (2) BQ-SIMPLIFY, if used, rewrites the code produced by \\*
;;; BQ-PROCESS to produce equivalent but faster code.~~The \\
;;; additional functions \#:BQ-LIST* and \#:BQ-NCONC may be \\*
;;; introduced into the code. \\*
;;; \\*
;;; (3) BQ-REMOVE-TOKENS goes through the code and replaces \\*
;;; \#:BQ-LIST with LIST, \#:BQ-APPEND with APPEND, and so on. \\
;;; \#:BQ-CLOBBERABLE is simply eliminated (a call to it being \\
;;; replaced by its argument).~~\#:BQ-LIST* is replaced by either \\
;;; LIST* or CONS (the latter is used in the two-argument case, \\*
;;; purely to make the resulting code a tad more readable).
\end{lisp}
\begin{lisp}
(defun bq-completely-process (x) \\*
~~(let ((raw-result (bq-process x))) \\*
~~~~(bq-remove-tokens (if *bq-simplify* \\*
~~~~~~~~~~~~~~~~~~~~~~~~~~(bq-simplify raw-result) \\*
~~~~~~~~~~~~~~~~~~~~~~~~~~raw-result))))
\end{lisp}
\begin{lisp}
(defun bq-process (x) \\*
~~(cond ((atom x) \\*
~~~~~~~~~(list *bq-quote* x)) \\
~~~~~~~~((eq (car x) 'backquote) \\*
~~~~~~~~~(bq-process (bq-completely-process (cadr x)))) \\
~~~~~~~~((eq (car x) *comma*) (cadr x)) \\
~~~~~~~~((eq (car x) *comma-atsign*) \\*
~~~~~~~~~(error ",{\Xatsign}{\Xtilde}S after {\Xbq}" (cadr x))) \\
~~~~~~~~((eq (car x) *comma-dot*) \\*
~~~~~~~~~(error ",.{\Xtilde}S after {\Xbq}" (cadr x))) \\
~~~~~~~~(t (do ((p x (cdr p)) \\*
~~~~~~~~~~~~~~~~(q '() (cons (bracket (car p)) q))) \\*
~~~~~~~~~~~~~~~((atom p) \\*
~~~~~~~~~~~~~~~~(cons *bq-append* \\*
~~~~~~~~~~~~~~~~~~~~~~(nreconc q (list (list *bq-quote* p))))) \\
~~~~~~~~~~~~~(when (eq (car p) *comma*) \\*
~~~~~~~~~~~~~~~(unless (null (cddr p)) (error "Malformed ,{\Xtilde}S" p)) \\*
~~~~~~~~~~~~~~~(return (cons *bq-append* \\*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(nreconc q (list (cadr p)))))) \\
~~~~~~~~~~~~~(when (eq (car p) *comma-atsign*) \\*
~~~~~~~~~~~~~~~(error "Dotted ,{\Xatsign}{\Xtilde}S" p)) \\
~~~~~~~~~~~~~(when (eq (car p) *comma-dot*) \\*
~~~~~~~~~~~~~~~(error "Dotted ,.{\Xtilde}S" p))))))
\end{lisp}
\begin{lisp}
;;; This implements the bracket operator of the formal rules.
\end{lisp}
\begin{lisp}
(defun bracket (x) \\*
~~(cond ((atom x) \\*
~~~~~~~~~(list *bq-list* (bq-process x))) \\
~~~~~~~~((eq (car x) *comma*) \\*
~~~~~~~~~(list *bq-list* (cadr x))) \\
~~~~~~~~((eq (car x) *comma-atsign*) \\*
~~~~~~~~~(cadr x)) \\
~~~~~~~~((eq (car x) *comma-dot*) \\*
~~~~~~~~~(list *bq-clobberable* (cadr x))) \\*
~~~~~~~~(t (list *bq-list* (bq-process x)))))
\end{lisp}
\begin{lisp}
;;; This auxiliary function is like MAPCAR but has two extra \\*
;;; purposes: (1) it handles dotted lists; (2) it tries to make \\*
;;; the result share with the argument x as much as possible.
\end{lisp}
\begin{lisp}
(defun maptree (fn x) \\*
~~(if (atom x) \\*
~~~~~~(funcall fn x) \\*
~~~~~~(let ((a (funcall fn (car x))) \\*
~~~~~~~~~~~~(d (maptree fn (cdr x)))) \\
~~~~~~~~(if (and (eql a (car x)) (eql d (cdr x))) \\*
~~~~~~~~~~~~x \\*
~~~~~~~~~~~~(cons a d)))))
\end{lisp}
\begin{lisp}
;;; This predicate is true of a form that when read looked \\*
;;; like \%{\Xatsign}foo or \%.foo.
\end{lisp}
\begin{lisp}
(defun bq-splicing-frob (x) \\*
~~(and (consp x) \\*
~~~~~~~(or (eq (car x) *comma-atsign*) \\*
~~~~~~~~~~~(eq (car x) *comma-dot*))))
\end{lisp}
\begin{lisp}
\\*
;;; This predicate is true of a form that when read \\*
;;; looked like \%{\Xatsign}foo or \%.foo or just plain \%foo.
\end{lisp}
\begin{lisp}
(defun bq-frob (x) \\*
~~(and (consp x) \\*
~~~~~~~(or (eq (car x) *comma*) \\*
~~~~~~~~~~~(eq (car x) *comma-atsign*) \\*
~~~~~~~~~~~(eq (car x) *comma-dot*))))
\end{lisp}
\begin{lisp}
;;; The simplifier essentially looks for calls to \#:BQ-APPEND and \\*
;;; tries to simplify them.~~The arguments to \#:BQ-APPEND are \\*
;;; processed from right to left, building up a replacement form. \\*
;;; At each step a number of special cases are handled that, \\*
;;; loosely speaking, look like this: \\*
;;; \\
;;;~~(APPEND (LIST a b c) foo) => (LIST* a b c foo) \\*
;;;~~~~~~~provided a, b, c are not splicing frobs \\*
;;;~~(APPEND (LIST* a b c) foo) => (LIST* a b (APPEND c foo)) \\*
;;;~~~~~~~provided a, b, c are not splicing frobs \\*
;;;~~(APPEND (QUOTE (x)) foo) => (LIST* (QUOTE x) foo) \\*
;;;~~(APPEND (CLOBBERABLE x) foo) => (NCONC x foo)
\end{lisp}
\begin{lisp}
(defun bq-simplify (x) \\*
~~(if (atom x) \\*
~~~~~~x \\*
~~~~~~(let ((x (if (eq (car x) *bq-quote*) \\*
~~~~~~~~~~~~~~~~~~~x \\*
~~~~~~~~~~~~~~~~~~~(maptree \#'bq-simplify x)))) \\
~~~~~~~~(if (not (eq (car x) *bq-append*)) \\*
~~~~~~~~~~~~x \\*
~~~~~~~~~~~~(bq-simplify-args x)))))
\end{lisp}
\begin{lisp}
(defun bq-simplify-args (x) \\*
~~(do ((args (reverse (cdr x)) (cdr args)) \\*
~~~~~~~(result \\*
~~~~~~~~~nil \\*
~~~~~~~~~(cond ((atom (car args)) \\*
~~~~~~~~~~~~~~~~(bq-attach-append *bq-append* (car args) result)) \\
~~~~~~~~~~~~~~~((and (eq (caar args) *bq-list*) \\*
~~~~~~~~~~~~~~~~~~~~~(notany \#'bq-splicing-frob (cdar args))) \\*
~~~~~~~~~~~~~~~~(bq-attach-conses (cdar args) result)) \\
~~~~~~~~~~~~~~~((and (eq (caar args) *bq-list**) \\*
~~~~~~~~~~~~~~~~~~~~~(notany \#'bq-splicing-frob (cdar args))) \\
~~~~~~~~~~~~~~~~(bq-attach-conses \\*
~~~~~~~~~~~~~~~~~~(reverse (cdr (reverse (cdar args)))) \\*
~~~~~~~~~~~~~~~~~~(bq-attach-append *bq-append* \\*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(car (last (car args))) \\*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~result))) \\
~~~~~~~~~~~~~~~((and (eq (caar args) *bq-quote*) \\*
~~~~~~~~~~~~~~~~~~~~~(consp (cadar args)) \\*
~~~~~~~~~~~~~~~~~~~~~(not (bq-frob (cadar args))) \\*
~~~~~~~~~~~~~~~~~~~~~(null (cddar args))) \\
~~~~~~~~~~~~~~~~(bq-attach-conses (list (list *bq-quote* \\*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(caadar args))) \\*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~result)) \\
~~~~~~~~~~~~~~~((eq (caar args) *bq-clobberable*) \\*
~~~~~~~~~~~~~~~~(bq-attach-append *bq-nconc* (cadar args) result)) \\
~~~~~~~~~~~~~~~(t (bq-attach-append *bq-append* \\*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(car args) \\*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~result))))) \\*
~~~~~~((null args) result)))
\end{lisp}
\begin{lisp}
(defun null-or-quoted (x) \\*
~~(or (null x) (and (consp x) (eq (car x) *bq-quote*))))
\end{lisp}
\begin{lisp}
;;; When BQ-ATTACH-APPEND is called, the OP should be \#:BQ-APPEND \\*
;;; or \#:BQ-NCONC.~~This produces a form (op item result) but \\*
;;; some simplifications are done on the fly: \\*
;;; \\
;;;~~(op '(a b c) '(d e f g)) => '(a b c d e f g) \\*
;;;~~(op item 'nil) => item, provided item is not a splicable frob \\*
;;;~~(op item 'nil) => (op item), if item is a splicable frob \\*
;;;~~(op item (op a b c)) => (op item a b c)
\end{lisp}
\begin{lisp}
(defun bq-attach-append (op item result) \\*
~~(cond ((and (null-or-quoted item) (null-or-quoted result)) \\*
~~~~~~~~~(list *bq-quote* (append (cadr item) (cadr result)))) \\
~~~~~~~~((or (null result) (equal result *bq-quote-nil*)) \\*
~~~~~~~~~(if (bq-splicing-frob item) (list op item) item)) \\
~~~~~~~~((and (consp result) (eq (car result) op)) \\*
~~~~~~~~~(list* (car result) item (cdr result))) \\*
~~~~~~~~(t (list op item result))))
\end{lisp}
\begin{lisp}
;;; The effect of BQ-ATTACH-CONSES is to produce a form as if by \\*
;;; {\Xbq}(LIST* ,{\Xatsign}items ,result) but some simplifications are done \\*
;;; on the fly. \\*
;;; \\
;;;~~(LIST* 'a 'b 'c 'd) => '(a b c . d) \\*
;;;~~(LIST* a b c 'nil) => (LIST a b c) \\*
;;;~~(LIST* a b c (LIST* d e f g)) => (LIST* a b c d e f g) \\*
;;;~~(LIST* a b c (LIST d e f g)) => (LIST a b c d e f g)
\end{lisp}
\begin{lisp}
(defun bq-attach-conses (items result) \\*
~~(cond ((and (every \#'null-or-quoted items) \\*
~~~~~~~~~~~~~~(null-or-quoted result)) \\
~~~~~~~~~(list *bq-quote* \\*
~~~~~~~~~~~~~~~(append (mapcar \#'cadr items) (cadr result)))) \\
~~~~~~~~((or (null result) (equal result *bq-quote-nil*)) \\*
~~~~~~~~~(cons *bq-list* items)) \\
~~~~~~~~((and (consp result) \\*
~~~~~~~~~~~~~~(or (eq (car result) *bq-list*) \\*
~~~~~~~~~~~~~~~~~~(eq (car result) *bq-list**))) \\*
~~~~~~~~~(cons (car result) (append items (cdr result)))) \\*
~~~~~~~~(t (cons *bq-list** (append items (list result))))))
\end{lisp}
\begin{lisp}
;;; Removes funny tokens and changes (\#:BQ-LIST* a b) into \\*
;;; (CONS a b) instead of (LIST* a b), purely for readability.
\end{lisp}
\begin{lisp}
(defun bq-remove-tokens (x) \\*
~~(cond ((eq x *bq-list*) 'list) \\*
~~~~~~~~((eq x *bq-append*) 'append) \\
~~~~~~~~((eq x *bq-nconc*) 'nconc) \\
~~~~~~~~((eq x *bq-list**) 'list*) \\
~~~~~~~~((eq x *bq-quote*) 'quote) \\
~~~~~~~~((atom x) x) \\
~~~~~~~~((eq (car x) *bq-clobberable*) \\*
~~~~~~~~~(bq-remove-tokens (cadr x))) \\
~~~~~~~~((and (eq (car x) *bq-list**) \\*
~~~~~~~~~~~~~~(consp (cddr x)) \\*
~~~~~~~~~~~~~~(null (cdddr x))) \\*
~~~~~~~~~(cons 'cons (maptree \#'bq-remove-tokens (cdr x)))) \\
~~~~~~~~(t (maptree \#'bq-remove-tokens x))))
\end{lisp}
Suppose that we first make the following definitions:
\begin{lisp}
(setq q '(r s)) \\*
(defun r (x) (reduce \#'* x)) \\*
(setq r '(3 5)) \\*
(setq s '(4 6))
\end{lisp}
Without simplification, the notation
\cd{\$\$(\%\%q)} (which stands for \cd{{\Xbq}{\Xbq}(,,q)})
is read as the expression
\begin{lisp}
(APPEND (LIST 'APPEND) (LIST (APPEND (LIST 'LIST) (LIST Q))))
\end{lisp}
The value of this expression is
\begin{lisp}
(APPEND (LIST (R S)))
\end{lisp}
and the value of this value is \cd{(24)}. We conclude
that the net effect
of twice-evaluating \cd{{\Xbq}{\Xbq}(,,q)} is to take
the value \cd{24} of the value \cd{(r~s)} of \cdf{q}
and plug it into the template \cd{(~)} to produce \cd{(24)}.
With simplification, the notation
\cd{\$\$(\%\%q)}
is read as the expression
\begin{lisp}
(LIST 'LIST Q)
\end{lisp}
The value of this expression is
\begin{lisp}
(LIST (R S))
\end{lisp}
and the value of this value is \cd{(24)}.
Thus the two ways of reading \cd{\$\$(\%\%q)} do not produce the
same expression---this we expected---but the values of the two ways are
different as well. Only the values of the values are the same.
In general, Common Lisp guarantees the result of
an expression with backquotes nested to depth {\it k} only after
{\it k} successive evaluations have been performed; the results after
fewer than {\it k} evaluations are implementation-dependent.
(Note that in the expression \cd{`(foo ,(process `(bar ,x)))}
the backquotes are {\it not} doubly nested. The inner backquoted
expression occurs within the textual scope of a comma belonging
to the outer backquote. The correct way to determine the backquote
nesting level of any subexpression is to start a count at zero and
proceed up the S-expression tree, adding one for each backquote
and subtracting one for each comma. This is similar to the rule
for determining nesting level with respect to parentheses by scanning
a character string linearly, adding or subtracting one as parentheses
are passed.)
It is convenient to extend the ``\EQ'' notation to handle multiple evaluation:
{\it x}~\EQ\EQ~{\it y} means that the expressions {\it x} and {\it y} may have
different results but they have the same results when twice evaluated.
Similarly, {\it x}~\EQ\EQ\EQ~{\it y} means that the values of the values of the
values of {\it x} and {\it y} are the same, and so on.
We can illustrate the differences between non-splicing and splicing
backquote inclusions quite concisely:
\begin{lisp}
\$\$(\%\%q)~~\EQ \\*
~~(APPEND (LIST 'APPEND) (LIST (APPEND (LIST 'LIST) (LIST Q)))) \\*
~~\EQ\EQ\ (LIST 'LIST Q) \EV\ (LIST (R S)) \EV\ (24)
\end{lisp}
\begin{lisp}
\$\$(\%{\Xatsign}\%q) \EQ \\*
~~(APPEND (LIST 'APPEND) (LIST Q)) \\*
~~\EQ\EQ\ Q \EV\ (R S) \EV\ 24
\end{lisp}
\begin{lisp}
\$\$(\%\%{\Xatsign}q) \EQ \\*
~~(APPEND (LIST 'APPEND) (LIST (APPEND (LIST 'LIST) Q))) \\*
~~\EQ\EQ\ (CONS 'LIST Q) \EV\ (LIST R S) \EV\ ((3 5) (4 6))
\end{lisp}
\begin{lisp}
\$\$(\%{\Xatsign}\%{\Xatsign}q) \EQ \\*
~~(APPEND (LIST 'APPEND) Q) \\*
~~\EQ\EQ\ (CONS 'APPEND Q) \EV\ (APPEND R S) \EV\ (3 5 4 6)
\end{lisp}
In each case I have shown both the unsimplified and simplified forms
and then traced the intermediate evaluations of the simplified form.
(Actually, the unsimplified forms do contain one simplification
without which they would be unreadable:
the \cdf{nil} that terminates each list has been systematically suppressed,
so that one sees \cd{(append~{\it x}~{\it y})} rather than
\cd{(append~{\it x}~{\it y}~'nil)}.)
The following driver function is useful for tracing the behavior
of nested backquote syntax through multiple evaluations.
The argument \cdf{ls} is a list of strings; each string
will be processed by the reader (\cdf{read-from-string}).
The argument \cdf{n} is the number of evaluations desired.
\begin{lisp}
(defun try (ls \&optional (n 0)) \\*
~~(dolist (x ls) \\*
~~~~(format t "{\Xtilde}\&{\Xtilde}A" \\*
~~~~~~~~~~~~(substitute \#{\Xbackslash}{\Xbq} \#{\Xbackslash}\$ (substitute \#{\Xbackslash}, \#{\Xbackslash}\% x))) \\
~~~~(do ((form (macroexpand (read-from-string x)) (eval form)) \\*
~~~~~~~~~(str " = " "{\Xtilde}\% => ") \\*
~~~~~~~~~(j 0 (+ j 1))) \\
~~~~~~~~((>= j n) \\*
~~~~~~~~~(format t str) \\*
~~~~~~~~~(write form :pretty t)) \\
~~~~~~(format t str) \\*
~~~~~~(write form :pretty t))) \\*
~~(format t "{\Xtilde}\&"))
\end{lisp}
This driver routine makes it easdy to explore a large number of cases
systematically. Here is a list of examples that illustrate not only
the differences between \cd{,} and \cd{,{\Xatsign}} but also their
interaction with \cd{'}.
\begin{lisp}
(setq fools2 '( \\*
"\$\$(foo \%\%p)" \\*
"\$\$(foo \%\%{\Xatsign}q)" \\*
"\$\$(foo \%'\%r)" \\
"\$\$(foo \%'\%{\Xatsign}s)" \\
"\$\$(foo \%{\Xatsign}\%p)" \\
"\$\$(foo \%{\Xatsign}\%{\Xatsign}q)" \\
"\$\$(foo \%{\Xatsign}'\%r)" \\*
"\$\$(foo \%{\Xatsign}'\%{\Xatsign}s)" \\*
))
\end{lisp}
Consider this set of sample values:
\begin{lisp}
(setq p '(union x y)) \\*
(setq q '((union x y) (list 'sqrt 9))) \\*
(setq r '(union x y)) \\*
(setq s '((union x y)))
\end{lisp}
Here is what happened when I executed \cd{(try fools2 2)} with
a non-\cdf{nil} value for the variable \cd{*bq-simplify*} (to see
simplified forms). I have interpolated some remarks.
\begin{lisp}
{\Xbq}{\Xbq}(foo ,,p) = (LIST 'LIST ''FOO P) \\*
=> (LIST 'FOO (UNION X Y)) \\*
=> (FOO (A B C))
\end{lisp}
So \cd{,,p} means ``the value of \cdf{p} is a form;
use the value of the value of \cdf{p}.''
\begin{lisp}
{\Xbq}{\Xbq}(foo ,,{\Xatsign}q) = (LIST* 'LIST ''FOO Q) \\*
=> (LIST 'FOO (UNION X Y) (LIST 'SQRT 9)) \\*
=> (FOO (A B C) (SQRT 9))
\end{lisp}
So \cd{,,{\Xatsign}q} means ``the value of \cdf{q} is a list of forms;
splice the list of values of the elements of the value of \cdf{q}.''
\begin{lisp}
{\Xbq}{\Xbq}(foo ,',r) = (LIST 'LIST ''FOO (LIST 'QUOTE R)) \\*
=> (LIST 'FOO '(UNION X Y)) \\*
=> (FOO (UNION X Y))
\end{lisp}
So \cd{,',r} means ``the value of \cdf{r} may be any object;
use the value of \cdf{r}
that is available at the time of first evaluation,
that is, when the outer backquote is evaluated.''
(To use the value of \cdf{r} that is available at the time of second evaluation,
that is, when the inner backquote is evaluated,
just use \cd{,r}.)
\begin{lisp}
{\Xbq}{\Xbq}(foo ,',{\Xatsign}s) = (LIST 'LIST ''FOO (CONS 'QUOTE S)) \\*
=> (LIST 'FOO '(UNION X Y)) \\*
=> (FOO (UNION X Y))
\end{lisp}
So \cd{,',{\Xatsign}s} means ``the value of \cdf{s} must be a singleton list of any object;
use the element of the value of \cdf{s}
that is available at the time of first evaluation,
that is, when the outer backquote is evaluated.''
Note that \cdf{s} must be a singleton list because it will be spliced
into a form \cd{(quote~)}, and the \cdf{quote} special operator requires exactly
one subform to appear; this is generally true of the sequence \cd{',{\Xatsign}}.
(To use the value of \cdf{s} that is available at the time of second evaluation,
that is, when the inner backquote is evaluated,
just use \cd{,{\Xatsign}s},in which case the list \cdf{s} is not restricted to be singleton,
or \cd{,(car~s)}.)
\begin{lisp}
{\Xbq}{\Xbq}(foo ,{\Xatsign},p) = (LIST 'CONS ''FOO P) \\*
=> (CONS 'FOO (UNION X Y)) \\*
=> (FOO A B C)
\end{lisp}
So \cd{,{\Xatsign},p} means ``the value of \cdf{p} is a form;
splice in the value of the value of \cdf{p}.''
\begin{lisp}
{\Xbq}{\Xbq}(foo ,{\Xatsign},{\Xatsign}q) = (LIST 'CONS ''FOO (CONS 'APPEND Q)) \\*
=> (CONS 'FOO (APPEND (UNION X Y) (LIST 'SQRT 9))) \\*
=> (FOO A B C SQRT 9)
\end{lisp}
So \cd{,{\Xatsign},{\Xatsign}q} means ``the value of \cdf{q} is a list of forms;
splice each of the values of the elements of the value of \cdf{q},
so that many splicings occur.''
\begin{lisp}
{\Xbq}{\Xbq}(foo ,{\Xatsign}',r) = (LIST 'CONS ''FOO (LIST 'QUOTE R)) \\*
=> (CONS 'FOO '(UNION X Y)) \\*
=> (FOO UNION X Y)
\end{lisp}
So \cd{,{\Xatsign}',r} means ``the value of \cdf{r} must be a list;
splice in the value of \cdf{r}
that is available at the time of first evaluation,
that is, when the outer backquote is evaluated.''
(To splice the value of \cdf{r} that is available at the time of second evaluation,
that is, when the inner backquote is evaluated,
just use \cd{,{\Xatsign}r}.)
\begin{lisp}
{\Xbq}{\Xbq}(foo ,{\Xatsign}',{\Xatsign}s) = (LIST 'CONS ''FOO (CONS 'QUOTE S)) \\*
=> (CONS 'FOO '(UNION X Y)) \\*
=> (FOO UNION X Y)
\end{lisp}
So \cd{,{\Xatsign}',{\Xatsign}s} means ``the value of \cdf{s} must be a singleton list whose
element is a list;
splice in the list that is the element of the value of \cdf{s}
that is available at the time of first evaluation,
that is, when the outer backquote is evaluated.''
(To splice the element of the value of \cdf{s} that is available at the time of second evaluation,
that is, when the inner backquote is evaluated,
just use \cd{,{\Xatsign}(car~s)}.)
I leave it to the reader to explore the possibilities of triply nested backquotes.
\begin{lisp}
(setq fools3 '( \\*
"\$\$\$(foo \%\%\%p)" ~~~~"\$\$\$(foo \%\%\%{\Xatsign}q)" \\*
"\$\$\$(foo \%\%'\%r)" ~~~"\$\$\$(foo \%\%'\%{\Xatsign}s)" \\
"\$\$\$(foo \%\%{\Xatsign}\%p)" ~~~"\$\$\$(foo \%\%{\Xatsign}\%{\Xatsign}q)" \\
"\$\$\$(foo \%\%{\Xatsign}'\%r)" ~~"\$\$\$(foo \%\%{\Xatsign}'\%{\Xatsign}s)" \\
"\$\$\$(foo \%'\%\%p)" ~~~"\$\$\$(foo \%'\%\%{\Xatsign}q)" \\
"\$\$\$(foo \%'\%'\%r)" ~~"\$\$\$(foo \%'\%'\%{\Xatsign}s)" \\
"\$\$\$(foo \%'\%{\Xatsign}\%p)" ~~"\$\$\$(foo \%'\%{\Xatsign}\%{\Xatsign}q)" \\
"\$\$\$(foo \%'\%{\Xatsign}'\%r)" ~"\$\$\$(foo \%'\%{\Xatsign}'\%{\Xatsign}s)" \\
"\$\$\$(foo \%{\Xatsign}\%\%p)" ~~~"\$\$\$(foo \%{\Xatsign}\%\%{\Xatsign}q)" \\
"\$\$\$(foo \%{\Xatsign}\%'\%r)" ~~"\$\$\$(foo \%{\Xatsign}\%'\%{\Xatsign}s)" \\
"\$\$\$(foo \%{\Xatsign}\%{\Xatsign}\%p)" ~~"\$\$\$(foo \%{\Xatsign}\%{\Xatsign}\%{\Xatsign}q)" \\
"\$\$\$(foo \%{\Xatsign}\%{\Xatsign}'\%r)" ~"\$\$\$(foo \%{\Xatsign}\%{\Xatsign}'\%{\Xatsign}s)" \\
"\$\$\$(foo \%{\Xatsign}'\%\%p)" ~~"\$\$\$(foo \%{\Xatsign}'\%\%{\Xatsign}q)" \\
"\$\$\$(foo \%{\Xatsign}'\%'\%r)" ~"\$\$\$(foo \%{\Xatsign}'\%'\%{\Xatsign}s)" \\
"\$\$\$(foo \%{\Xatsign}'\%{\Xatsign}\%p)" ~"\$\$\$(foo \%{\Xatsign}'\%{\Xatsign}\%{\Xatsign}q)" \\*
"\$\$\$(foo \%{\Xatsign}'\%{\Xatsign}'\%r)" "\$\$\$(foo \%{\Xatsign}'\%{\Xatsign}'\%{\Xatsign}s)" \\*
))
\end{lisp}
It is a pleasant exercise to construct values for \cdf{p}, \cdf{q}, \cdf{r}, and \cdf{s}
that will allow execution of \cd{(try~fools3~3)} without error.
\end{new}
\endgroup