1
0
Fork 0
cl-sites/HyperSpec-7-0/HyperSpec/Issues/iss180_w.htm
2024-04-01 10:24:07 +02:00

245 lines
18 KiB
HTML

<!-- Common Lisp HyperSpec (TM), version 7.0 generated by Kent M. Pitman on Mon, 11-Apr-2005 2:31am EDT -->
<HTML>
<HEAD>
<TITLE>CLHS: Issue GENERALIZE-PRETTY-PRINTER Writeup</TITLE>
<LINK HREF="../Data/clhs.css" REL="stylesheet" TYPE="text/css" />
<META HTTP-EQUIV="Author" CONTENT="Kent M. Pitman">
<META HTTP-EQUIV="Organization" CONTENT="LispWorks Ltd.">
<LINK REL=TOP HREF="../Front/index.htm">
<LINK REL=COPYRIGHT HREF="../Front/Help.htm#Legal">
<LINK REL=DISCLAIMER HREF="../Front/Help.htm#Disclaimer">
<LINK REL=PREV HREF="../Issues/iss175_w.htm">
<LINK REL=UP HREF="../Issues/iss180.htm">
<LINK REL=NEXT HREF="../Issues/iss181_w.htm">
</HEAD>
<BODY>
<H1><A REV=MADE HREF="http://www.lispworks.com/"><IMG WIDTH=80 HEIGHT=65 ALT="[LISPWORKS]" SRC="../Graphics/LWSmall.gif" ALIGN=Bottom></A><A REL=TOP HREF="../Front/index.htm"><IMG WIDTH=237 HEIGHT=65 ALT="[Common Lisp HyperSpec (TM)]" SRC="../Graphics/CLHS_Sm.gif" ALIGN=Bottom></A> <A REL=PREV HREF="../Issues/iss175_w.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="../Issues/iss180.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="../Issues/iss181_w.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
<HR>
<H2>Issue GENERALIZE-PRETTY-PRINTER Writeup</H2>
<PRE><B>Forum:</B> Public Review<P>
<B>Issue:</B> <A HREF="iss180.htm">GENERALIZE-PRETTY-PRINTER</A><P>
<B>References:</B> Loosemore's public review comment #18 (X3J13/92-1318)<P>
Section 22.2.1 (Pretty Printer Concepts), p 22-13<P>
~/name/ <A REL=DEFINITION HREF="../Body/f_format.htm#format"><B>FORMAT</B></A> directive, p 22-39..22-40<P>
<A REL=DEFINITION HREF="../Body/f_ppr_di.htm#pprint-dispatch"><B>PPRINT-DISPATCH</B></A>, p 22-45..22-46<P>
<A REL=DEFINITION HREF="../Body/m_ppr_ex.htm#pprint-exit-if-list-exhausted"><B>PPRINT-EXIT-IF-LIST-EXHAUSTED</B></A>, p 22-46<P>
<A REL=DEFINITION HREF="../Body/f_ppr_fi.htm#pprint-fill"><B>PPRINT-FILL</B></A> (etc), p 22-47<P>
<A REL=DEFINITION HREF="../Body/f_ppr_in.htm#pprint-indent"><B>PPRINT-INDENT</B></A>, p 22-48<P>
<A REL=DEFINITION HREF="../Body/m_ppr_lo.htm#pprint-logical-block"><B>PPRINT-LOGICAL-BLOCK</B></A>, p 22-49..22-51<P>
<A REL=DEFINITION HREF="../Body/f_ppr_nl.htm#pprint-newline"><B>PPRINT-NEWLINE</B></A>, p 22-51..22-52<P>
<A REL=DEFINITION HREF="../Body/m_ppr_po.htm#pprint-pop"><B>PPRINT-POP</B></A>, p 22-52..22-54<P>
<A REL=DEFINITION HREF="../Body/f_ppr_ta.htm#pprint-tab"><B>PPRINT-TAB</B></A>, p 22-54<P>
<A REL=DEFINITION HREF="../Body/f_pr_obj.htm#print-object"><B>PRINT-OBJECT</B></A>, p 22-55..22-56<P>
<A REL=DEFINITION HREF="../Body/f_set_pp.htm#set-pprint-dispatch"><B>SET-PPRINT-DISPATCH</B></A>, p 22-57..22-58<P>
<B>Category:</B> CHANGE<P>
<B>Edit history:</B> V1, 18 Sept 1992, Sandra Loosemore<P>
V2, 18 Sept 1992, Sandra Loosemore<P>
(comments from Dick Waters)<P>
V3, 04 Feb 1993, Sandra Loosemore<P>
(fix references)<P>
<B>Status:</B> Proposal UNIFY passed 8-3 on letter ballot 93-302.<P>
<P>
<P>
<B>Problem description:<P>
</B><P>
The pretty-printing mechanisms are poorly integrated with the rest<P>
of the printer.<P>
<P>
Specifically,<P>
<P>
(1) The requirement that objects for which there is no pretty-print<P>
function defined in <A REL=DEFINITION HREF="../Body/v_pr_ppr.htm#STprint-pprint-dispatchST"><B>*PRINT-PPRINT-DISPATCH*</B></A> print using the <A REL=DEFINITION HREF="../Body/07_ffb.htm#standard"><B>standard</B></A><P>
mechanisms but with <A REL=DEFINITION HREF="../Body/v_pr_pre.htm#STprint-prettyST"><B>*PRINT-PRETTY*</B></A> bound to <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A> (p. 22-15 and 22-45)<P>
makes it impossible for <A REL=DEFINITION HREF="../Body/f_pr_obj.htm#print-object"><B>PRINT-OBJECT</B></A> methods or <A REL=DEFINITION HREF="../Body/m_defstr.htm#defstruct"><B>DEFSTRUCT</B></A> <P>
:PRINT-FUNCTIONS to handle <A REL=DEFINITION HREF="../Body/v_pr_pre.htm#STprint-prettyST"><B>*PRINT-PRETTY*</B></A> themselves. This can lead <P>
to unnecessary duplication of code, as it requires programmers to write <P>
two different functions to print the same kind of object.<P>
<P>
(2) The behavior of <A REL=DEFINITION HREF="../Body/f_ppr_fi.htm#pprint-fill"><B>PPRINT-FILL</B></A>, <A REL=DEFINITION HREF="../Body/f_ppr_fi.htm#pprint-linear"><B>PPRINT-LINEAR</B></A>, <A REL=DEFINITION HREF="../Body/f_ppr_fi.htm#pprint-tabular"><B>PPRINT-TABULAR</B></A>,<P>
<A REL=DEFINITION HREF="../Body/f_ppr_in.htm#pprint-indent"><B>PPRINT-INDENT</B></A>, <A REL=DEFINITION HREF="../Body/m_ppr_lo.htm#pprint-logical-block"><B>PPRINT-LOGICAL-BLOCK</B></A>, <A REL=DEFINITION HREF="../Body/f_ppr_nl.htm#pprint-newline"><B>PPRINT-NEWLINE</B></A>, and <A REL=DEFINITION HREF="../Body/f_ppr_ta.htm#pprint-tab"><B>PPRINT-TAB</B></A> when<P>
<A REL=DEFINITION HREF="../Body/v_pr_pre.htm#STprint-prettyST"><B>*PRINT-PRETTY*</B></A> is not true is unspecified, although the names imply<P>
that they are only useful when pretty-printing or that they may bind<P>
<A REL=DEFINITION HREF="../Body/v_pr_pre.htm#STprint-prettyST"><B>*PRINT-PRETTY*</B></A>. Again, to avoid pointless duplication of code, these<P>
mechanisms should do something reasonable regardless of whether<P>
<A REL=DEFINITION HREF="../Body/v_pr_pre.htm#STprint-prettyST"><B>*PRINT-PRETTY*</B></A> is true.<P>
<P>
(3) The order of the &quot;stream&quot; and &quot;object&quot; arguments to <P>
<A REL=DEFINITION HREF="../Body/f_ppr_fi.htm#pprint-fill"><B>PPRINT-FILL</B></A>, <A REL=DEFINITION HREF="../Body/f_ppr_fi.htm#pprint-linear"><B>PPRINT-LINEAR</B></A>, <A REL=DEFINITION HREF="../Body/f_ppr_fi.htm#pprint-tabular"><B>PPRINT-TABULAR</B></A>, functions in <A REL=DEFINITION HREF="../Body/f_wr_pr.htm#pprint"><B>pprint</B></A><P>
dispatch tables, and functions used with the ~/name/ format directive<P>
is backwards with respect to that of other printing functions.<P>
<P>
There are three non-mutually-exclusive proposals here. Proposal<P>
UNIFY addresses problems (1) and (2). Proposal REORDER addresses<P>
problem (3). Proposal RENAME changes the names of most of the<P>
affected functions to reflect the more general utility under proposal<P>
UNIFY.<P>
<P>
<P>
<B>Proposal (GENERALIZE-PRETTY-PRINTER:UNIFY):<P>
</B><P>
(1) Change the behavior stated in section 22.2.1.4 and in the<P>
description of <A REL=DEFINITION HREF="../Body/f_ppr_di.htm#pprint-dispatch"><B>PPRINT-DISPATCH</B></A> to say that if there is no specific<P>
found in the <A REL=DEFINITION HREF="../Body/f_wr_pr.htm#pprint"><B>pprint</B></A> dispatch table, a function which invokes the<P>
appropriate <A REL=DEFINITION HREF="../Body/f_pr_obj.htm#print-object"><B>PRINT-OBJECT</B></A> <A REL=DEFINITION HREF="../Body/t_method.htm#method"><B>method</B></A> is used. Remove the requirement that<P>
<A REL=DEFINITION HREF="../Body/v_pr_pre.htm#STprint-prettyST"><B>*PRINT-PRETTY*</B></A> be rebound to <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A>.<P>
<P>
(2) Clarify that <A REL=DEFINITION HREF="../Body/m_ppr_lo.htm#pprint-logical-block"><B>PPRINT-LOGICAL-BLOCK</B></A> may be used even when<P>
<A REL=DEFINITION HREF="../Body/v_pr_pre.htm#STprint-prettyST"><B>*PRINT-PRETTY*</B></A> is not true. Remove the requirement that the stream<P>
cease to behave as a pretty-printing stream if <A REL=DEFINITION HREF="../Body/v_pr_pre.htm#STprint-prettyST"><B>*PRINT-PRETTY*</B></A> becomes<P>
bound to <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A>. Clarify that <A REL=DEFINITION HREF="../Body/m_ppr_lo.htm#pprint-logical-block"><B>PPRINT-LOGICAL-BLOCK</B></A> does not locally<P>
rebind <A REL=DEFINITION HREF="../Body/v_pr_pre.htm#STprint-prettyST"><B>*PRINT-PRETTY*</B></A>.<P>
<P>
(3) Specify that if <A REL=DEFINITION HREF="../Body/v_pr_pre.htm#STprint-prettyST"><B>*PRINT-PRETTY*</B></A> is false, the functions <A REL=DEFINITION HREF="../Body/f_ppr_fi.htm#pprint-fill"><B>PPRINT-FILL</B></A>,<P>
<A REL=DEFINITION HREF="../Body/f_ppr_fi.htm#pprint-linear"><B>PPRINT-LINEAR</B></A>, and <A REL=DEFINITION HREF="../Body/f_ppr_fi.htm#pprint-tabular"><B>PPRINT-TABULAR</B></A> print list arguments with a minimum<P>
of whitespace (i.e., as described in section 22.1.3.8). Clarify that<P>
these functions do not locally rebind <A REL=DEFINITION HREF="../Body/v_pr_pre.htm#STprint-prettyST"><B>*PRINT-PRETTY*</B></A>.<P>
<P>
(4) The draft currently says that <A REL=DEFINITION HREF="../Body/f_ppr_nl.htm#pprint-newline"><B>PPRINT-NEWLINE</B></A>, <A REL=DEFINITION HREF="../Body/f_ppr_ta.htm#pprint-tab"><B>PPRINT-TAB</B></A>, and<P>
<A REL=DEFINITION HREF="../Body/f_ppr_in.htm#pprint-indent"><B>PPRINT-INDENT</B></A> do nothing if the stream is not a pretty-printing stream. <P>
Change this to say they also do nothing if <A REL=DEFINITION HREF="../Body/v_pr_pre.htm#STprint-prettyST"><B>*PRINT-PRETTY*</B></A> is false.<P>
<P>
(5) Change the description of <A REL=DEFINITION HREF="../Body/f_pr_obj.htm#print-object"><B>PRINT-OBJECT</B></A> to mention the use of<P>
functions such as <A REL=DEFINITION HREF="../Body/f_ppr_fi.htm#pprint-fill"><B>PPRINT-FILL</B></A> to handle <A REL=DEFINITION HREF="../Body/v_pr_pre.htm#STprint-prettyST"><B>*PRINT-PRETTY*</B></A>, and<P>
<A REL=DEFINITION HREF="../Body/m_ppr_lo.htm#pprint-logical-block"><B>PPRINT-LOGICAL-BLOCK</B></A> to handle <A REL=DEFINITION HREF="../Body/v_pr_lev.htm#STprint-lengthST"><B>*PRINT-LENGTH*</B></A> truncation, instead<P>
of discouraging people from handling these variables properly.<P>
<P>
<P>
Rationale:<P>
<P>
This allows you to write a single function that handles both pretty<P>
and non-pretty printing that can use a single block of code inside<P>
<A REL=DEFINITION HREF="../Body/m_ppr_lo.htm#pprint-logical-block"><B>PPRINT-LOGICAL-BLOCK</B></A> to print structured objects regardless of whether<P>
<A REL=DEFINITION HREF="../Body/v_pr_pre.htm#STprint-prettyST"><B>*PRINT-PRETTY*</B></A> is true. It also allows you to call the other<P>
functions without having to make an explicit check to be sure that <P>
<A REL=DEFINITION HREF="../Body/v_pr_pre.htm#STprint-prettyST"><B>*PRINT-PRETTY*</B></A> is true, and do something else if it isn't.<P>
<P>
<P>
<P>
<B>Proposal (GENERALIZE-PRETTY-PRINTER:REORDER):<P>
</B><P>
(1) Reverse the order of the &quot;object&quot; and &quot;stream&quot; arguments to<P>
functions in <A REL=DEFINITION HREF="../Body/f_wr_pr.htm#pprint"><B>pprint</B></A> dispatch tables.<P>
<P>
(2) Explicitly state that <A REL=DEFINITION HREF="../Body/f_ppr_di.htm#pprint-dispatch"><B>PPRINT-DISPATCH</B></A> returns the generic function<P>
<A REL=DEFINITION HREF="../Body/f_pr_obj.htm#print-object"><B>PRINT-OBJECT</B></A> if there is no matching type specifier in the <A REL=DEFINITION HREF="../Body/f_wr_pr.htm#pprint"><B>pprint</B></A><P>
dispatch table.<P>
<P>
(3) Reverse the order of the &quot;object&quot; and &quot;stream&quot; arguments to<P>
<A REL=DEFINITION HREF="../Body/f_ppr_fi.htm#pprint-fill"><B>PPRINT-FILL</B></A>, <A REL=DEFINITION HREF="../Body/f_ppr_fi.htm#pprint-linear"><B>PPRINT-LINEAR</B></A>, and <A REL=DEFINITION HREF="../Body/f_ppr_fi.htm#pprint-tabular"><B>PPRINT-TABULAR</B></A>. Make the &quot;stream&quot;<P>
argument optional and default in the usual way for an output stream<P>
designator, so that the argument list is <P>
(object &amp;optional stream colon-p at-sign-p).<P>
<P>
(4) Reverse the order of the &quot;object&quot; and &quot;stream&quot; arguments passed<P>
to functions invoked with the ~/name/ format directive.<P>
<P>
<P>
Rationale:<P>
<P>
This removes a pointless inconsistency in the language design and<P>
makes it a little simpler.<P>
<P>
<P>
<P>
<B>Proposal (GENERALIZE-PRETTY-PRINTER:RENAME):<P>
</B><P>
Rename <A REL=DEFINITION HREF="../Body/m_ppr_ex.htm#pprint-exit-if-list-exhausted"><B>PPRINT-EXIT-IF-LIST-EXHAUSTED</B></A>, <A REL=DEFINITION HREF="../Body/f_ppr_fi.htm#pprint-fill"><B>PPRINT-FILL</B></A>, <A REL=DEFINITION HREF="../Body/f_ppr_fi.htm#pprint-linear"><B>PPRINT-LINEAR</B></A>,<P>
<A REL=DEFINITION HREF="../Body/f_ppr_fi.htm#pprint-tabular"><B>PPRINT-TABULAR</B></A>, <A REL=DEFINITION HREF="../Body/f_ppr_in.htm#pprint-indent"><B>PPRINT-INDENT</B></A>, <A REL=DEFINITION HREF="../Body/m_ppr_lo.htm#pprint-logical-block"><B>PPRINT-LOGICAL-BLOCK</B></A>, <A REL=DEFINITION HREF="../Body/f_ppr_nl.htm#pprint-newline"><B>PPRINT-NEWLINE</B></A>,<P>
<A REL=DEFINITION HREF="../Body/m_ppr_po.htm#pprint-pop"><B>PPRINT-POP</B></A>, and <A REL=DEFINITION HREF="../Body/f_ppr_ta.htm#pprint-tab"><B>PPRINT-TAB</B></A> to PRINT-*.<P>
<P>
Rationale: <P>
<P>
Changing the names makes it more apparent that these functions are<P>
useful even when <A REL=DEFINITION HREF="../Body/v_pr_pre.htm#STprint-prettyST"><B>*PRINT-PRETTY*</B></A> is false and that they do not<P>
rebind <A REL=DEFINITION HREF="../Body/v_pr_pre.htm#STprint-prettyST"><B>*PRINT-PRETTY*</B></A> to true as <A REL=DEFINITION HREF="../Body/f_wr_pr.htm#pprint"><B>PPRINT</B></A> does. (Note that this list <P>
does not include <A REL=DEFINITION HREF="../Body/v_pr_ppr.htm#STprint-pprint-dispatchST"><B>*PRINT-PPRINT-DISPATCH*</B></A>, <A REL=DEFINITION HREF="../Body/f_ppr_di.htm#pprint-dispatch"><B>PPRINT-DISPATCH</B></A>, and<P>
<A REL=DEFINITION HREF="../Body/f_set_pp.htm#set-pprint-dispatch"><B>SET-PPRINT-DISPATCH</B></A>, since these are only relevant when <A REL=DEFINITION HREF="../Body/v_pr_pre.htm#STprint-prettyST"><B>*PRINT-PRETTY*</B></A><P>
is true.)<P>
<P>
Since proposals UNIFY and REORDER make incompatible changes in the<P>
behavior of these functions, changing their names may be helpful in<P>
detecting obsolete calls.<P>
<P>
<P>
<B>Current practice:<P>
</B><P>
XP doesn't implement any of these changes.<P>
<P>
Loosemore made most of these changes when rewriting XP into a<P>
Scheme-like dialect; the Yale Haskell system uses it extensively.<P>
<P>
<P>
<B>Cost to implementors:<P>
</B><P>
Medium. Making the changes to the printer implementation is<P>
straightforward, but there are the usual support costs associated<P>
with making an incompatible change.<P>
<P>
<P>
<B>Cost to users:<P>
</B><P>
This is an incompatible change for people who now use XP, but it's<P>
easy to fix. Adoption of proposal RENAME may make it easier to detect<P>
obsolete uses.<P>
<P>
<P>
<B>Cost of non-adoption:<P>
</B><P>
More work for people writing new code. Confusion about why<P>
<A REL=DEFINITION HREF="../Body/f_pr_obj.htm#print-object"><B>PRINT-OBJECT</B></A> methods can't detect when <A REL=DEFINITION HREF="../Body/v_pr_pre.htm#STprint-prettyST"><B>*PRINT-PRETTY*</B></A> is true. Risk<P>
of function for printing when <A REL=DEFINITION HREF="../Body/v_pr_pre.htm#STprint-prettyST"><B>*PRINT-PRETTY*</B></A> is true getting out of<P>
sync with function for printing the same object when <A REL=DEFINITION HREF="../Body/v_pr_pre.htm#STprint-prettyST"><B>*PRINT-PRETTY*</B></A> is<P>
false. Risk of people making stupid programming mistakes because they<P>
can't remember which functions accept arguments in which order.<P>
<P>
<P>
<B>Benefits:<P>
</B><P>
The cost of non-adoption is avoided.<P>
<P>
<P>
<B>Aesthetics:<P>
</B><P>
Making the language simpler and more uniform is generally a good<P>
thing.<P>
<P>
<P>
<B>Editorial impact:<P>
</B><P>
It would probably take two or three days to make the necessary changes,<P>
including updating examples. Loosemore is willing to undertake the<P>
editorial work.<P>
<P>
<P>
<B>Discussion:<P>
</B><P>
This issue was first raised on the X3J13 mailing list in January 1992<P>
with the subject &quot;apparent shortcomings with <A REL=DEFINITION HREF="../Body/f_wr_pr.htm#pprint"><B>PPRINT</B></A> interface&quot;.<P>
<P>
Loosemore says: I discovered these problems when trying to implement<P>
the new pretty-printer functionality for the first time for use by the<P>
Yale Haskell system. We have several dozen different kinds of structures<P>
representing AST nodes and wanted to be able to print them reasonably<P>
for debugging and error messages. Pushing the detection for<P>
<A REL=DEFINITION HREF="../Body/v_pr_pre.htm#STprint-prettyST"><B>*PRINT-PRETTY*</B></A> into the support functions resulted in *major*<P>
consolidations and simplifications of this code.<P>
<P>
Waters says: I support your proposal strongly. Please feel free to<P>
add me to the proposal as a formal supporter.<P>
<P>
John Burger also says he was bitten by the inability to handle<P>
<A REL=DEFINITION HREF="../Body/v_pr_pre.htm#STprint-prettyST"><B>*PRINT-PRETTY*</B></A> in <A REL=DEFINITION HREF="../Body/f_pr_obj.htm#print-object"><B>PRINT-OBJECT</B></A> methods, and that he supports this <P>
proposal.<P>
</PRE>
<HR>
<A REL=NAVIGATOR HREF="../Front/StartPts.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Starting Points]" SRC="../Graphics/StartPts.gif" ALIGN=Bottom></A><A REL=TOC HREF="../Front/Contents.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Contents]" SRC="../Graphics/Contents.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Master.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Index]" SRC="../Graphics/Index.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Symbol.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Symbols]" SRC="../Graphics/Symbols.gif" ALIGN=Bottom></A><A REL=GLOSSARY HREF="../Body/26_a.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Glossary]" SRC="../Graphics/Glossary.gif" ALIGN=Bottom></A><A HREF="../Front/X3J13Iss.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Issues]" SRC="../Graphics/Issues.gif" ALIGN=Bottom></A><BR>
<A REL=COPYRIGHT HREF="../Front/Help.htm#Legal"><I>Copyright 1996-2005, LispWorks Ltd. All rights reserved.</I></A><P>
</BODY>
</HTML>