-
Notifications
You must be signed in to change notification settings - Fork 0
/
chap-22.texi
5549 lines (4184 loc) · 193 KB
/
chap-22.texi
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
@node Printer
@chapter Printer
@menu
* The Lisp Printer::
* The Lisp Pretty Printer::
* Formatted Output::
* Printer Dictionary::
@end menu
@node The Lisp Printer
@section The Lisp Printer
@menu
* Overview of The Lisp Printer::
* Printer Dispatching::
* Default Print-Object Methods::
* Examples of Printer Behavior::
@end menu
@node Overview of The Lisp Printer
@subsection Overview of The Lisp Printer
@clisp{}@spc{}provides a representation of most @term{objects} in the form
of printed text called the printed representation.
Functions such as @symbolref{print, SYM} take an @term{object}
and send the characters of its printed representation to a @term{stream}.
The collection of routines that does this is known as the (@clisp{}) printer.
Reading a printed representation
typically
produces an @term{object} that is @symbolref{equal, SYM} to the
originally printed @term{object}.
@node Multiple Possible Textual Representations
@subsubsection Multiple Possible Textual Representations
Most @term{objects} have more than one possible textual representation.
For example, the positive @term{integer} with a magnitude of twenty-seven
can be textually expressed in any of these ways:
@lisp
27 27. #o33 #x1B #b11011 #.(* 3 3 3) 81/3
@end lisp
A list containing the two symbols @f{A} and @f{B} can also be textually
expressed in a variety of ways:
@lisp
(A B) (a b) ( a b ) (@bsl{}A |B|)
(|@bsl{}A|
B
)
@end lisp
In general,
from the point of view of the @term{Lisp reader},
wherever @term{whitespace} is permissible in a textual representation,
any number of @term{spaces} and @term{newlines} can appear in @term{standard syntax}.
When a function such as @symbolref{print, SYM} produces a printed representation,
it must choose
from among many possible textual representations.
In most cases, it chooses a
program readable representation,
but in certain cases it might use a more compact notation that is not
program-readable.
A number of option variables, called
@cindex printer control variable
@dfn{printer control variables},
are provided to permit control of individual aspects of the
printed representation of @term{objects}.
@Thenextfigure{}@spc{}shows the @term{standardized} @term{printer control variables};
there might also be @term{implementation-defined} @term{printer control variables}.
@float Figure,StdPrinterControlVars
@cartouche
@multitable{*print-circle*}{*print-miser-width*}{*print-pprint-dispatch*}
@item *print-array* @tab *print-gensym* @tab *print-pprint-dispatch*
@item *print-base* @tab *print-length* @tab *print-pretty*
@item *print-case* @tab *print-level* @tab *print-radix*
@item *print-circle* @tab *print-lines* @tab *print-readably*
@item *print-escape* @tab *print-miser-width* @tab *print-right-margin*
@end multitable
@end cartouche
@caption{Standardized Printer Control Variables}
@end float
In addition to the @term{printer control variables},
the following additional @term{defined names}
relate to or affect the behavior of the @term{Lisp printer}:
@float Figure,fig22.2
@cartouche
@multitable{*read-default-float-format*}{*read-eval*}{readtable-case}
@item *package* @tab *read-eval* @tab readtable-case
@item *read-default-float-format* @tab *readtable* @tab
@end multitable
@end cartouche
@caption{Additional Influences on the Lisp printer.}
@end float
@node Printer Escaping
@subsubsection Printer Escaping
@Thevariable{*print-escape*} controls whether the @term{Lisp printer}
tries to produce notations such as escape characters and package prefixes.
@Thevariable{*print-readably*} can be used to override
many of the individual aspects controlled by the other
@term{printer control variables} when program-readable output
is especially important.
One of the many effects of making @thevalueof{*print-readably*} be @term{true}
is that the @term{Lisp printer} behaves as if @symbolref{*print-escape*, SYM} were also @term{true}.
For notational convenience, we say that
if the value of either @symbolref{*print-readably*, SYM} or @symbolref{*print-escape*, SYM} is @term{true},
then @newterm{printer escaping} is ``enabled'';
and we say that
if the values of both @symbolref{*print-readably*, SYM} and @symbolref{*print-escape*, SYM} are @term{false},
then @term{printer escaping} is ``disabled''.
@node Printer Dispatching
@subsection Printer Dispatching
The @term{Lisp printer} makes its determination of how to print an
@term{object} as follows:
If @thevalueof{*print-pretty*} is @term{true},
printing is controlled by the @term{current pprint dispatch table};
see @ref{Pretty Print Dispatch Tables}.
Otherwise (if @thevalueof{*print-pretty*} is @term{false}),
the object's @symbolref{print-object, SYM} method is used;
see @ref{Default Print-Object Methods}.
@node Default Print-Object Methods
@subsection Default Print-Object Methods
This section describes the default behavior of
@symbolref{print-object, SYM} methods for the @term{standardized} @term{types}.
@node Printing Numbers
@subsubsection Printing Numbers
@node Printing Integers
@subsubsection Printing Integers
@term{Integers} are printed in the radix specified by the @term{current output base}
in positional notation, most significant digit first.
If appropriate, a radix specifier can be printed; see @symbolref{*print-radix*, SYM}.
If an @term{integer} is negative, a minus sign is printed and then the
absolute value of the @term{integer} is printed.
The @term{integer} zero is represented
by the single digit @f{0} and never has a sign.
A decimal point might be printed,
depending on @thevalueof{*print-radix*}.
For related information about the syntax of an @term{integer},
see @ref{Syntax of an Integer}.
@node Printing Ratios
@subsubsection Printing Ratios
@cindex ratio
@term{Ratios} are printed as follows:
the absolute value of the numerator is printed, as for an @term{integer};
then a @f{/}; then the denominator. The numerator and denominator are
both printed in the radix specified by the @term{current output base};
they are obtained as if by
@symbolref{numerator, SYM} and @symbolref{denominator, SYM}, and so @term{ratios}
are printed in reduced form (lowest terms).
If appropriate, a radix specifier can be printed; see
@symbolref{*print-radix*, SYM}.
If the ratio is negative, a minus sign is printed before the numerator.
For related information about the syntax of a @term{ratio},
see @ref{Syntax of a Ratio}.
@node Printing Floats
@subsubsection Printing Floats
@cindex float
If the magnitude of the @term{float} is either zero or between @mat{10^-3} (inclusive)
and @mat{10^7} (exclusive), it is printed as the integer part of the number,
then a decimal point,
followed by the fractional part of the number;
there is always at least one
digit on each side of the decimal point.
If the sign of the number
(as determined by @symbolref{float-sign, SYM})
is negative, then a minus sign is printed before the number.
If the format of the number
does not match that specified by
@symbolref{*read-default-float-format*, SYM}, then the @term{exponent marker} for
that format and the digit @f{0} are also printed.
For example, the base of the natural logarithms as a @term{short float}
might be printed as @f{2.71828S0}.
For non-zero magnitudes outside of the range @mat{10^-3} to @mat{10^7},
a @term{float} is printed in computerized scientific notation.
The representation of the number is scaled to be between
1 (inclusive) and 10 (exclusive) and then printed, with one digit
before the decimal point and at least one digit after the decimal point.
Next the @term{exponent marker} for the format is printed,
except that
if the format of the number matches that specified by
@symbolref{*read-default-float-format*, SYM}, then the @term{exponent marker} @f{E}
is used.
Finally, the power of ten by which the fraction must be multiplied
to equal the original number is printed as a decimal integer.
For example, Avogadro's number as a @term{short float}
is printed as @f{6.02S23}.
For related information about the syntax of a @term{float},
see @ref{Syntax of a Float}.
@node Printing Complexes
@subsubsection Printing Complexes
@cindex complex
A @term{complex} is printed as @f{#C}, an open parenthesis,
the printed representation of its real part, a space,
the printed representation of its imaginary part, and finally
a close parenthesis.
For related information about the syntax of a @term{complex},
see @ref{Syntax of a Complex}@spc{}and @ref{Sharpsign C}.
@node Note about Printing Numbers
@subsubsection Note about Printing Numbers
The printed representation of a number must not contain @term{escape} @term{characters};
see @ref{Escape Characters and Potential Numbers}.
@node Printing Characters
@subsubsection Printing Characters
When @term{printer escaping} is disabled,
a @term{character} prints as itself;
it is sent directly to the output @term{stream}.
When @term{printer escaping} is enabled,
then @f{#@bsl{}} syntax is used.
When the printer types out the name of a @term{character},
it uses the same table as the @f{#@bsl{}} @term{reader macro} would use;
therefore any @term{character} name that is typed out
is acceptable as input (in that @term{implementation}).
If a @term{non-graphic} @term{character} has a @term{standardized} @term{name}@sub{5},
that @term{name} is preferred over non-standard @term{names}
for printing in @f{#@bsl{}} notation.
For the @term{graphic} @term{standard characters},
the @term{character} itself is always used
for printing in @f{#@bsl{}} notation---even if
the @term{character} also has a @term{name}@sub{5}.
For details about the @f{#@bsl{}} @term{reader macro}, see @ref{Sharpsign Backslash}.
@node Printing Symbols
@subsubsection Printing Symbols
When @term{printer escaping} is disabled,
only the characters of the @term{symbol}'s @term{name} are output
(but the case in which to print characters in the @term{name} is
controlled by @symbolref{*print-case*, SYM};
see @ref{Effect of Readtable Case on the Lisp Printer}).
The remainder of this section @spc{}applies only
when @term{printer escaping} is enabled.
When printing a @term{symbol}, the printer inserts enough
@term{single escape} and/or @term{multiple escape}
characters (@term{backslashes} and/or @term{vertical-bars}) so that if
@symbolref{read, SYM} were called with the same @symbolref{*readtable*, SYM} and
with @symbolref{*read-base*, SYM} bound to the @term{current output base}, it
would return the same @term{symbol} (if it is not
@term{apparently uninterned}) or an @term{uninterned} @term{symbol}
with the same @term{print name} (otherwise).
For example, if @thevalueof{*print-base*} were @f{16}
when printing the symbol @f{face}, it would have to be printed as
@f{@bsl{}FACE} or @f{@bsl{}Face} or @f{|FACE|},
because the token @f{face} would be read as a hexadecimal
number (decimal value 64206) if @thevalueof{*read-base*} were @f{16}.
For additional restrictions concerning characters with nonstandard
@term{syntax types} in the @term{current readtable}, see the @term{variable} @ref{*print-readably*}
For information about how the @term{Lisp reader} parses @term{symbols},
see @ref{Sharpsign Colon}.
@nil{}@spc{}might be printed as @f{()}
when @symbolref{*print-pretty*, SYM} is @term{true}
and @term{printer escaping} is enabled.
@node Package Prefixes for Symbols
@subsubsection Package Prefixes for Symbols
@term{Package prefixes} are printed if necessary.
The rules for @term{package prefixes} are as follows.
When the @term{symbol} is printed, if it is in the @code{KEYWORD} @term{package},
then it is printed with a preceding @term{colon}; otherwise, if
it is @term{accessible} in the @term{current package}, it is printed without any
@term{package prefix}; otherwise, it is printed with a @term{package prefix}.
A @term{symbol} that is @term{apparently uninterned} is printed
preceded by ``@f{#:}''
if @symbolref{*print-gensym*, SYM} is @term{true} and @term{printer escaping} is enabled;
if @symbolref{*print-gensym*, SYM} is @term{false} or @term{printer escaping} is disabled,
then the @term{symbol} is printed without a prefix,
as if it were in the @term{current package}.
Because the @f{#:} syntax does not intern the
following symbol, it is necessary to use circular-list syntax
if @symbolref{*print-circle*, SYM} is @term{true} and
the same uninterned symbol appears several times in an expression
to be printed. For example, the result of
@lisp
(let ((x (make-symbol "FOO"))) (list x x))
@end lisp
would be printed as @f{(#:foo #:foo)} if @symbolref{*print-circle*, SYM}
were @term{false}, but as @f{(#1=#:foo #1#)} if @symbolref{*print-circle*, SYM}
were @term{true}.
A summary of the preceding package prefix rules follows:
@table @asis
@item @id{@f{foo:bar}}
@f{foo:bar} is printed when @term{symbol} @f{bar}
is external in its @term{home package} @f{foo}
and is not @term{accessible} in the @term{current package}.
@item @id{@f{foo::bar}}
@f{foo::bar} is printed when @f{bar} is internal in its @term{home package}
@f{foo} and is not @term{accessible} in the @term{current package}.
@item @id{@f{:bar}}
@f{:bar} is printed when the home package of @f{bar} is the @code{KEYWORD} @term{package}.
@item @id{@tt{#:bar}}
@f{#:bar} is printed when @f{bar} is @term{apparently uninterned},
even in the pathological case that @f{bar}
has no @term{home package} but is nevertheless somehow @term{accessible}
in the @term{current package}.
@end table
@node Effect of Readtable Case on the Lisp Printer
@subsubsection Effect of Readtable Case on the Lisp Printer
When
@term{printer escaping} is disabled,
or the characters under consideration are not already
quoted specifically by @term{single escape} or @term{multiple escape}
syntax,
the @term{readtable case} of the @term{current readtable}
affects the way the @term{Lisp printer} writes @term{symbols}
in the following ways:
@table @asis
@item @id{@kwd{upcase}}
When the @term{readtable case} is @kwd{upcase},
@term{uppercase} @term{characters}
are printed in the case specified by @symbolref{*print-case*, SYM}, and
@term{lowercase} @term{characters} are printed in their own case.
@item @id{@kwd{downcase}}
When the @term{readtable case} is @kwd{downcase},
@term{uppercase} @term{characters} are printed in their own case, and
@term{lowercase} @term{characters}
are printed in the case specified by @symbolref{*print-case*, SYM}.
@item @id{@kwd{preserve}}
When the @term{readtable case} is @kwd{preserve},
all @term{alphabetic} @term{characters} are printed in their own case.
@item @id{@kwd{invert}}
When the @term{readtable case} is @kwd{invert},
the case of all @term{alphabetic} @term{characters}
in single case symbol names is inverted.
Mixed-case symbol names are printed as is.
@end table
The rules for escaping @term{alphabetic} @term{characters} in symbol names are affected by
the @symbolref{readtable-case, SYM}
if @term{printer escaping} is enabled.
@term{Alphabetic} @term{characters} are escaped as follows:
@table @asis
@item @id{@kwd{upcase}}
When the @term{readtable case} is @kwd{upcase},
all @term{lowercase} @term{characters} must be escaped.
@item @id{@kwd{downcase}}
When the @term{readtable case} is @kwd{downcase},
all @term{uppercase} @term{characters} must be escaped.
@item @id{@kwd{preserve}}
When the @term{readtable case} is @kwd{preserve},
no @term{alphabetic} @term{characters} need be escaped.
@item @id{@kwd{invert}}
When the @term{readtable case} is @kwd{invert},
no @term{alphabetic} @term{characters} need be escaped.
@end table
@node Examples of Effect of Readtable Case on the Lisp Printer
@subsubsection Examples of Effect of Readtable Case on the Lisp Printer
@lisp
(defun test-readtable-case-printing ()
(let ((*readtable* (copy-readtable nil))
(*print-case* *print-case*))
(format t "READTABLE-CASE *PRINT-CASE* Symbol-name Output~
~%--------------------------------------------------~
~%")
(dolist (readtable-case '(:upcase :downcase :preserve :invert))
(setf (readtable-case *readtable*) readtable-case)
(dolist (print-case '(:upcase :downcase :capitalize))
(dolist (symbol '(|ZEBRA| |Zebra| |zebra|))
(setq *print-case* print-case)
(format t "~&:~A~15T:~A~29T~A~42T~A"
(string-upcase readtable-case)
(string-upcase print-case)
(symbol-name symbol)
(prin1-to-string symbol)))))))
@end lisp
The output from @f{(test-readtable-case-printing)} should be as follows:
@lisp
READTABLE-CASE *PRINT-CASE* Symbol-name Output
--------------------------------------------------
:UPCASE :UPCASE ZEBRA ZEBRA
:UPCASE :UPCASE Zebra |Zebra|
:UPCASE :UPCASE zebra |zebra|
:UPCASE :DOWNCASE ZEBRA zebra
:UPCASE :DOWNCASE Zebra |Zebra|
:UPCASE :DOWNCASE zebra |zebra|
:UPCASE :CAPITALIZE ZEBRA Zebra
:UPCASE :CAPITALIZE Zebra |Zebra|
:UPCASE :CAPITALIZE zebra |zebra|
:DOWNCASE :UPCASE ZEBRA |ZEBRA|
:DOWNCASE :UPCASE Zebra |Zebra|
:DOWNCASE :UPCASE zebra ZEBRA
:DOWNCASE :DOWNCASE ZEBRA |ZEBRA|
:DOWNCASE :DOWNCASE Zebra |Zebra|
:DOWNCASE :DOWNCASE zebra zebra
:DOWNCASE :CAPITALIZE ZEBRA |ZEBRA|
:DOWNCASE :CAPITALIZE Zebra |Zebra|
:DOWNCASE :CAPITALIZE zebra Zebra
:PRESERVE :UPCASE ZEBRA ZEBRA
:PRESERVE :UPCASE Zebra Zebra
:PRESERVE :UPCASE zebra zebra
:PRESERVE :DOWNCASE ZEBRA ZEBRA
:PRESERVE :DOWNCASE Zebra Zebra
:PRESERVE :DOWNCASE zebra zebra
:PRESERVE :CAPITALIZE ZEBRA ZEBRA
:PRESERVE :CAPITALIZE Zebra Zebra
:PRESERVE :CAPITALIZE zebra zebra
:INVERT :UPCASE ZEBRA zebra
:INVERT :UPCASE Zebra Zebra
:INVERT :UPCASE zebra ZEBRA
:INVERT :DOWNCASE ZEBRA zebra
:INVERT :DOWNCASE Zebra Zebra
:INVERT :DOWNCASE zebra ZEBRA
:INVERT :CAPITALIZE ZEBRA zebra
:INVERT :CAPITALIZE Zebra Zebra
:INVERT :CAPITALIZE zebra ZEBRA
@end lisp
@node Printing Strings
@subsubsection Printing Strings
The characters of the @term{string} are output in order.
If @term{printer escaping} is enabled,
a @term{double-quote} is output before and after, and all
@term{double-quotes} and @term{single escapes} are preceded by @term{backslash}.
The printing of @term{strings} is not affected by @symbolref{*print-array*, SYM}.
Only the @term{active} @term{elements} of the @term{string} are printed.
For information on how the @term{Lisp reader} parses @term{strings},
see @ref{Double-Quote}.
@node Printing Lists and Conses
@subsubsection Printing Lists and Conses
Wherever possible, list notation is preferred over dot notation.
Therefore the following algorithm is used to print a @term{cons} @mat{x}:
@goodbreak{}
@enumerate 1
@item A @term{left-parenthesis} is printed.
@medbreak{}
@item The @term{car} of @mat{x} is printed.
@medbreak{}
@item If the @term{cdr} of @mat{x} is itself a @term{cons},
it is made to be the current @term{cons}
(@ie{} @mat{x} becomes that @term{cons}),
a @term{space}
is printed,
and step 2 is re-entered.
@medbreak{}
@item If the @term{cdr} of @mat{x} is not @term{null},
a @term{space},
a @term{dot},
a @term{space},
and the @term{cdr} of @mat{x} are printed.
@medbreak{}
@item A @term{right-parenthesis} is printed.
@end enumerate
Actually, the above algorithm is only used when @symbolref{*print-pretty*, SYM}
is @term{false}. When @symbolref{*print-pretty*, SYM} is @term{true} (or
when @symbolref{pprint, SYM} is used),
additional @term{whitespace}@sub{1}
may replace the use of a single @term{space},
and a more elaborate algorithm with similar goals but more presentational
flexibility is used; see @ref{Printer Dispatching}.
Although the two expressions below are equivalent,
and the reader accepts
either one and
produces
the same @term{cons}, the printer
always prints such a @term{cons} in the second form.
@lisp
(a . (b . ((c . (d . nil)) . (e . nil))))
(a b (c d) e)
@end lisp
The printing of @term{conses} is affected by @symbolref{*print-level*, SYM},
@symbolref{*print-length*, SYM}, and @symbolref{*print-circle*, SYM}.
@goodbreak{}
Following are examples of printed representations of @term{lists}:
@lisp
(a . b) ;A dotted pair of a and b
(a.b) ;A list of one element, the symbol named a.b
(a. b) ;A list of two elements a. and b
(a .b) ;A list of two elements a and .b
(a b . c) ;A dotted list of a and b with c at the end; two conses
.iot ;The symbol whose name is .iot
(. b) ;Invalid -- an error is signaled if an attempt is made to read
;this syntax.
(a .) ;Invalid -- an error is signaled.
(a .. b) ;Invalid -- an error is signaled.
(a . . b) ;Invalid -- an error is signaled.
(a b c ...) ;Invalid -- an error is signaled.
(a @bsl{}. b) ;A list of three elements a, ., and b
(a |.| b) ;A list of three elements a, ., and b
(a @bsl{}... b) ;A list of three elements a, ..., and b
(a |...| b) ;A list of three elements a, ..., and b
@end lisp
For information on how the @term{Lisp reader} parses @term{lists} and @term{conses},
see @ref{Left-Parenthesis}.
@node Printing Bit Vectors
@subsubsection Printing Bit Vectors
A @term{bit vector} is printed as @f{#*} followed by the bits of the @term{bit vector}
in order. If @symbolref{*print-array*, SYM} is @term{false}, then the @term{bit vector} is
printed in a format (using @f{#<}) that is concise but not readable.
Only the @term{active} @term{elements} of the @term{bit vector} are printed.
@reviewer{Barrett: Need to provide for @f{#5*0} as an alternate
notation for @f{#*00000}.}
For information on @term{Lisp reader} parsing of @term{bit vectors},
see @ref{Sharpsign Asterisk}.
@node Printing Other Vectors
@subsubsection Printing Other Vectors
If @symbolref{*print-array*, SYM} is @term{true}
and @symbolref{*print-readably*, SYM} is @term{false},
any
@term{vector}
other than a @term{string} or @term{bit vector} is printed using
general-vector syntax; this means that information
about specialized vector representations does not appear.
The printed representation of a zero-length @term{vector} is @f{#()}.
The printed representation of a non-zero-length @term{vector} begins with @f{#(}.
Following that, the first element of the @term{vector} is printed.
If there are any other elements, they are printed in turn, with
each such additional element preceded by
a @term{space} if @symbolref{*print-pretty*, SYM} is @term{false},
or @term{whitespace}@sub{1} if @symbolref{*print-pretty*, SYM} is @term{true}.
A @term{right-parenthesis} after the last element
terminates the printed representation of the @term{vector}.
The printing of @term{vectors}
is affected by @symbolref{*print-level*, SYM} and @symbolref{*print-length*, SYM}.
If the @term{vector} has a @term{fill pointer},
then only those elements below
the @term{fill pointer} are printed.
If both @symbolref{*print-array*, SYM} and @symbolref{*print-readably*, SYM} are @term{false},
the @term{vector} is not printed as described above,
but in a format (using @f{#<}) that is concise but not readable.
If @symbolref{*print-readably*, SYM} is @term{true},
the @term{vector} prints in an @term{implementation-defined} manner;
see the @term{variable} @ref{*print-readably*}.
For information on how the @term{Lisp reader} parses these ``other @term{vectors},''
see @ref{Sharpsign Left-Parenthesis}.
@node Printing Other Arrays
@subsubsection Printing Other Arrays
If @symbolref{*print-array*, SYM} is @term{true}
and @symbolref{*print-readably*, SYM} is @term{false},
any
@term{array} other than a @term{vector} is printed
using @f{#}@f{n}@f{A} format.
Let @f{n} be the @term{rank} of the @term{array}.
Then @f{#} is printed, then @f{n} as a decimal integer,
then @f{A}, then @f{n} open parentheses.
Next the @term{elements} are scanned in row-major order,
using @symbolref{write, SYM} on each @term{element},
and separating @term{elements} from each other with @term{whitespace}@sub{1}.
The array's dimensions are numbered 0 to @f{n}-1 from left to right,
and are enumerated with the rightmost index changing fastest.
Every time the index for dimension @f{j} is incremented,
the following actions are taken:
@itemize @bullet{}
@item
If @f{j} < @f{n}-1, then a close parenthesis is printed.
@item
If incrementing the index for dimension @f{j} caused it to equal
dimension @f{j}, that index is reset to zero and the
index for dimension @f{j}-1 is incremented (thereby performing these three steps recursively),
unless @f{j}=0, in which case the entire algorithm is terminated.
If incrementing the index for dimension @f{j} did not cause it to
equal dimension @f{j}, then a space is printed.
@item
If @f{j} < @f{n}-1, then an open parenthesis is printed.
@end itemize
This causes the contents to be printed in a format suitable for
@kwd{initial-contents} to @symbolref{make-array, SYM}.
The lists effectively printed by this procedure are subject to
truncation by @symbolref{*print-level*, SYM} and @symbolref{*print-length*, SYM}.
If the @term{array}
is of a specialized @term{type}, containing bits or characters,
then the innermost lists generated by the algorithm given above can instead
be printed using bit-vector or string syntax, provided that these innermost
lists would not be subject to truncation by @symbolref{*print-length*, SYM}.
If both @symbolref{*print-array*, SYM} and @symbolref{*print-readably*, SYM} are @term{false},
then the @term{array} is printed
in a format (using @f{#<}) that is concise but not readable.
If @symbolref{*print-readably*, SYM} is @term{true},
the @term{array} prints in an @term{implementation-defined} manner;
see the @term{variable} @ref{*print-readably*}.
In particular,
this may be important for arrays having some dimension @f{0}.
For information on how the @term{Lisp reader} parses these ``other @term{arrays},''
see @ref{Sharpsign A}.
@node Examples of Printing Arrays
@subsubsection Examples of Printing Arrays
@lisp
(let ((a (make-array '(3 3)))
(*print-pretty* t)
(*print-array* t))
(dotimes (i 3) (dotimes (j 3) (setf (aref a i j) (format nil "<~D,~D>" i j))))
(print a)
(print (make-array 9 :displaced-to a)))
@OUT{} #2A(("<0,0>" "<0,1>" "<0,2>")
@OUT{} ("<1,0>" "<1,1>" "<1,2>")
@OUT{} ("<2,0>" "<2,1>" "<2,2>"))
@OUT{} #("<0,0>" "<0,1>" "<0,2>" "<1,0>" "<1,1>" "<1,2>" "<2,0>" "<2,1>" "<2,2>")
@EV{} #<ARRAY 9 indirect 36363476>
@end lisp
@node Printing Random States
@subsubsection Printing Random States
A specific syntax for printing @term{objects} @oftype{random-state} is
not specified. However, every @term{implementation}
must arrange to print a @term{random state} @term{object} in such a way that,
within the same implementation, @symbolref{read, SYM}
can construct from the printed representation a copy of the
@term{random state}
object as if the copy had been made by @symbolref{make-random-state, SYM}.
If the type @term{random state} is effectively implemented
by using the machinery for @symbolref{defstruct, SYM},
the usual structure syntax can then be used for printing
@term{random state}
objects; one might look something like
@lisp
#S(RANDOM-STATE :DATA #(14 49 98436589 786345 8734658324 ... ))
@end lisp
where the components are @term{implementation-dependent}.
@node Printing Pathnames
@subsubsection Printing Pathnames
When @term{printer escaping} is enabled,
the syntax @f{#P"..."} is how a
@term{pathname} is printed by @symbolref{write, SYM} and the other functions herein described.
The @f{"..."} is the namestring representation of the pathname.
When @term{printer escaping} is disabled,
@symbolref{write, SYM} writes a @term{pathname} @i{P}
by writing @f{(namestring @i{P})} instead.
For information on how the @term{Lisp reader} parses @term{pathnames},
see @ref{Sharpsign P}.
@node Printing Structures
@subsubsection Printing Structures
By default, a @term{structure} of type @mat{S} is printed using @f{#S} syntax.
This behavior can be customized by specifying a @kwd{print-function}
or @kwd{print-object} option to the @symbolref{defstruct, SYM} @term{form} that defines @mat{S},
or by writing a @symbolref{print-object, SYM} @term{method}
that is @term{specialized} for @term{objects} of type @mat{S}.
Different structures might print out in different ways;
the default notation for structures is:
@lisp
#S(@param{structure-name} @star{@curly{@param{slot-key} @param{slot-value}}})
@end lisp
where @f{#S} indicates structure syntax,
@param{structure-name} is a @term{structure name},
each @param{slot-key} is an initialization argument @term{name}
for a @term{slot} in the @term{structure},
and each corresponding @param{slot-value} is a representation
of the @term{object} in that @term{slot}.
For information on how the @term{Lisp reader} parses @term{structures},
see @ref{Sharpsign S}.
@node Printing Other Objects
@subsubsection Printing Other Objects
Other @term{objects} are printed in an @term{implementation-dependent} manner.
It is not required that an @term{implementation} print those @term{objects}
@term{readably}.
For example, @term{hash tables},
@term{readtables},
@term{packages},
@term{streams},
and @term{functions}
might not print @term{readably}.
A common notation to use in this circumstance is @f{#<...>}.
Since @f{#<} is not readable by the @term{Lisp reader},
the precise format of the text which follows is not important,
but a common format to use is that provided by @themacro{print-unreadable-object}.
For information on how the @term{Lisp reader} treats this notation,
see @ref{Sharpsign Less-Than-Sign}.
For information on how to notate @term{objects} that cannot be printed @term{readably},
see @ref{Sharpsign Dot}.
@node Examples of Printer Behavior
@subsection Examples of Printer Behavior
@lisp
(let ((*print-escape* t)) (fresh-line) (write #@bsl{}a))
@OUT{} #@bsl{}a
@EV{} #@bsl{}a
(let ((*print-escape* nil) (*print-readably* nil))
(fresh-line)
(write #@bsl{}a))
@OUT{} a
@EV{} #@bsl{}a
(progn (fresh-line) (prin1 #@bsl{}a))
@OUT{} #@bsl{}a
@EV{} #@bsl{}a
(progn (fresh-line) (print #@bsl{}a))
@OUT{}
@OUT{} #@bsl{}a
@EV{} #@bsl{}a
(progn (fresh-line) (princ #@bsl{}a))
@OUT{} a
@EV{} #@bsl{}a
@medbreak{}
(dolist (val '(t nil))
(let ((*print-escape* val) (*print-readably* val))
(print '#@bsl{}a)
(prin1 #@bsl{}a) (write-char #@bsl{}Space)
(princ #@bsl{}a) (write-char #@bsl{}Space)
(write #@bsl{}a)))
@OUT{} #@bsl{}a #@bsl{}a a #@bsl{}a
@OUT{} #@bsl{}a #@bsl{}a a a
@EV{} NIL
@medbreak{}
(progn (fresh-line) (write '(let ((a 1) (b 2)) (+ a b))))
@OUT{} (LET ((A 1) (B 2)) (+ A B))
@EV{} (LET ((A 1) (B 2)) (+ A B))
@medbreak{}
(progn (fresh-line) (pprint '(let ((a 1) (b 2)) (+ a b))))
@OUT{} (LET ((A 1)
@OUT{} (B 2))
@OUT{} (+ A B))
@EV{} (LET ((A 1) (B 2)) (+ A B))
@medbreak{}
(progn (fresh-line)
(write '(let ((a 1) (b 2)) (+ a b)) :pretty t))
@OUT{} (LET ((A 1)
@OUT{} (B 2))
@OUT{} (+ A B))
@EV{} (LET ((A 1) (B 2)) (+ A B))
@medbreak{}
(with-output-to-string (s)
(write 'write :stream s)
(prin1 'prin1 s))
@EV{} "WRITEPRIN1"
@end lisp
@node The Lisp Pretty Printer
@section The Lisp Pretty Printer
@menu
* Pretty Printer Concepts::
* Examples of using the Pretty Printer::
* Notes about the Pretty Printer's Background::
@end menu
@node Pretty Printer Concepts
@subsection Pretty Printer Concepts
The facilities provided by the @newterm{pretty printer} permit
@term{programs} to redefine the way in which @term{code} is displayed,
and allow the full power of @term{pretty printing} to be applied
to complex combinations of data structures.
Whether any given style of output is in fact ``pretty'' is inherently a
somewhat subjective issue. However, since the effect of the
@term{pretty printer} can be customized by @term{conforming programs},
the necessary flexibility is provided for individual @term{programs}
to achieve an arbitrary degree of aesthetic control.
By providing direct access to the mechanisms within the pretty printer
that make dynamic decisions about layout, the macros and functions
@symbolref{pprint-logical-block, SYM}, @symbolref{pprint-newline, SYM}, and
@symbolref{pprint-indent, SYM} make it possible to specify pretty printing
layout rules as a part of any function that produces output. They also
make it very easy for the detection of circularity and sharing, and
abbreviation based on length and nesting depth to be supported by the
function.
The @term{pretty printer} is driven entirely by dispatch based on
@thevalueof{*print-pprint-dispatch*}.
@Thefunction{set-pprint-dispatch} makes it possible
for @term{conforming programs} to associate new pretty printing
functions with a @term{type}.
@node Dynamic Control of the Arrangement of Output
@subsubsection Dynamic Control of the Arrangement of Output
The actions of the @term{pretty printer} when a piece of output is too
large to fit in the space available can be precisely controlled.
Three concepts underlie
the way these operations work---@newterm{logical blocks},
@newterm{conditional newlines},
and @newterm{sections}.
Before proceeding further, it is important to define these terms.
The first line of @thenextfigure{}@spc{}shows a schematic piece of output. Each of
the characters in the output is represented by ``@f{-}''. The positions of
conditional newlines are indicated by digits. The beginnings and ends of
logical blocks are indicated by ``@f{<}'' and ``@f{>}'' respectively.
The output as a whole is a logical block and the outermost section. This
section is indicated by the @f{0}'s on the second line of Figure 1. Logical
blocks nested within the output are specified by the macro
@symbolref{pprint-logical-block, SYM}. Conditional newline positions are specified
by calls to @symbolref{pprint-newline, SYM}. Each conditional newline defines
two sections (one before it and one after it) and is associated with a
third (the section immediately containing it).
The section after a conditional newline consists of: all the output up to,
but not including, (a) the next conditional newline immediately contained
in the same logical block; or if (a) is not applicable, (b) the next
newline that is at a lesser level of nesting in logical blocks; or if (b)
is not applicable, (c) the end of the output.
The section before a conditional newline consists of: all the output back
to, but not including, (a) the previous conditional newline that is
immediately contained in the same logical block; or if (a) is not
applicable, (b) the beginning of the immediately containing logical block.
The last four lines in Figure 1 indicate the sections before and after the
four conditional newlines.
The section immediately containing a conditional newline is the shortest
section that contains the conditional newline in question. In @thenextfigure{},
the first conditional newline is immediately contained in the section
marked with @f{0}'s, the second and third conditional newlines are immediately
contained in the section before the fourth conditional newline, and the
fourth conditional newline is immediately contained in the section after
the first conditional newline.
@float Figure,fig22.3
@lisp
<-1---<--<--2---3->--4-->->
000000000000000000000000000
11 111111111111111111111111
22 222
333 3333
44444444444444 44444
@end lisp
@caption{Example of Logical Blocks, Conditional Newlines, and Sections}
@end float