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

146 lines
8.6 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 PRINT-CIRCLE-STRUCTURE 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/iss274_w.htm">
<LINK REL=UP HREF="../Issues/iss275.htm">
<LINK REL=NEXT HREF="../Issues/iss276_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/iss274_w.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="../Issues/iss275.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="../Issues/iss276_w.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
<HR>
<H2>Issue PRINT-CIRCLE-STRUCTURE Writeup</H2>
<PRE><B>Status:</B> Passed, Jan 89 X3J13, as amended<P>
<B>Issue:</B> <A HREF="iss275.htm">PRINT-CIRCLE-STRUCTURE</A><P>
<B>References:</B> pp. 370-371<P>
<B>Category:</B> CLARIFICATION<P>
<B>Edit history:</B> Version 4, Masinter, 17-Mar-89 (as amended)<P>
Version 3, Masinter 10/8/88 (minor cleanup)<P>
Version 2, Chris McConnell 10/05/88<P>
Version 1, Chris McConnell 09/20/88<P>
<P>
<B>Problem description:<P>
</B><P>
When a lisp object is printed that points to a <A REL=DEFINITION HREF="../Body/f_docume.htm#structure"><B>structure</B></A> with a user<P>
defined print-function and there is a pointer back to the containing<P>
object, the printer will recurse infinitely even when *print-circle*<P>
is set to T. This prevents printing circular structures that point to<P>
objects that cannot be printed and prevents the development of new<P>
printed representations that can be read by the reader.<P>
<P>
<B>Proposal PRINT-CIRCLE-STRUCTURE:USER-FUNCTIONS-WORK: <P>
</B><P>
When *print-circle* is T, a user defined print-function can print<P>
objects to the supplied stream using <A REL=DEFINITION HREF="../Body/f_wr_pr.htm#write"><B>WRITE</B></A>, <A REL=DEFINITION HREF="../Body/f_wr_pr.htm#prin1"><B>PRIN1</B></A>, <A REL=DEFINITION HREF="../Body/f_wr_pr.htm#princ"><B>PRINC</B></A>, or <A REL=DEFINITION HREF="../Body/f_format.htm#format"><B>FORMAT</B></A><P>
and expect circularities to be detected and printed using #n# syntax.<P>
If a user defined print-function prints to a stream other than the one<P>
that was supplied, then circularity detection starts over for that<P>
stream. This applies to methods on <A REL=DEFINITION HREF="../Body/f_pr_obj.htm#print-object"><B>PRINT-OBJECT</B></A> in addition to<P>
:PRINT-FUNCTION options.<P>
<P>
<B>Test Cases/Examples:<P>
</B><P>
;;;<P>
;;; Define a <A REL=DEFINITION HREF="../Body/f_docume.htm#structure"><B>structure</B></A> that can be circular and that has a slot with a<P>
;;; value that cannot be printed.<P>
;;;<P>
(<A REL=DEFINITION HREF="../Body/m_defstr.htm#defstruct"><B>defstruct</B></A> (TEST (:print-function print-test))<P>
ptr<P>
(<A REL=DEFINITION HREF="../Body/a_fn.htm#function"><B>function</B></A> #'(<A REL=DEFINITION HREF="../Body/a_lambda.htm#lambda"><B>lambda</B></A> (x) <P>
(<A REL=DEFINITION HREF="../Body/a_error.htm#error"><B>error</B></A> &quot;No <A REL=DEFINITION HREF="../Body/a_fn.htm#function"><B>function</B></A> is defined.&quot;))))<P>
<P>
;;;<P>
;;; This print function generates a machine readable printed<P>
;;; representation for a <A REL=DEFINITION HREF="../Body/f_docume.htm#structure"><B>structure</B></A> with a slot that cannot be printed.<P>
;;;<P>
(<A REL=DEFINITION HREF="../Body/m_defun.htm#defun"><B>defun</B></A> PRINT-TEST (<A REL=DEFINITION HREF="../Body/f_docume.htm#structure"><B>structure</B></A> <A REL=DEFINITION HREF="../Body/t_stream.htm#stream"><B>stream</B></A> depth)<P>
(<A REL=DEFINITION HREF="../Body/f_format.htm#format"><B>format</B></A> <A REL=DEFINITION HREF="../Body/t_stream.htm#stream"><B>stream</B></A> &quot;#S(TEST PTR ~S)&quot; (test-ptr <A REL=DEFINITION HREF="../Body/f_docume.htm#structure"><B>structure</B></A>)))<P>
<P>
;;;<P>
;;; Define two structures that point to each other. If this<P>
;;; expression successfully evaluates at the top level, then the<P>
;;; printed result should look like:<P>
;;; #1=#S(TEST PTR #S(TEST PTR #1#))<P>
;;;<P>
;;; If it does not work then it will generate an infinite printed<P>
;;; representation. <P>
(<A REL=DEFINITION HREF="../Body/a_setf.htm#setf"><B>setf</B></A> <A REL=DEFINITION HREF="../Body/v_pr_cir.htm#STprint-circleST"><B>*print-circle*</B></A> t<P>
*a (make-test)<P>
*b (make-test)<P>
(test-ptr *a) *b<P>
(test-ptr *b) *a)<P>
<P>
<P>
<B>Rationale:<P>
</B><P>
Many structures are circular and point to complex data structures that<P>
may include things like closures that cannot be printed. It should be<P>
possible to define a way to print these data structures such that they<P>
can be read back in. Common LISP provides two mechanisms for these<P>
problems (*print-circle* and the :print-function option to <A REL=DEFINITION HREF="../Body/m_defstr.htm#defstruct"><B>defstruct</B></A>),<P>
but they do not currently work together in most implementations.<P>
<P>
<B>Current practice:<P>
</B><P>
Lucid 3.0 and the Genera do work on the test case. (Previous versions<P>
did not.) KCL, Coral and Franz do not work.<P>
<P>
<B>Cost to Implementors:<P>
</B><P>
Lucid and Symbolics have done it, so they could give an idea of the<P>
difficulty. Possible techniques include passing the printer state<P>
information by dynamic binding rather than by explicit parameters or<P>
by supplying an internal stream to the user print function.<P>
<P>
Cost to Users: None<P>
<P>
<B>Cost of non-adoption: <P>
</B><P>
Currently this problem causes an infinite recursion whenever a user<P>
print-function prints a lisp object that contains the <A REL=DEFINITION HREF="../Body/f_docume.htm#structure"><B>structure</B></A> that<P>
is being printed by the user print function. If nothing is done, this<P>
error will remain and the whole effort to <A REL=DEFINITION HREF="../Body/f_provid.htm#provide"><B>provide</B></A> a portable printed<P>
representation of LISP structures is of minimal usefulness. In almost<P>
any real application, there are circular structures with non printable<P>
objects such as closures and hash tables that need to be printed. In<P>
addition the development of printers for new reader macros becomes<P>
much more of an effort then it should be since it requires<P>
reimplementing the complete circularity detection mechanism.<P>
<P>
<B>Benefits:<P>
</B><P>
If the proposal is adopted, then it becomes easier to define new<P>
printed representations that are compact and that still capture the<P>
information needed to rebuild data <A REL=DEFINITION HREF="../Body/f_docume.htm#structure"><B>structure</B></A> instances. It allows a<P>
printed representation to hide the actual details of how a data<P>
<A REL=DEFINITION HREF="../Body/f_docume.htm#structure"><B>structure</B></A> is implemented in terms of underlying LISP data structures.<P>
<P>
<B>Esthetics: <P>
</B><P>
This proposal increases the uniformity of the language by making<P>
*print-circle* work in all cases including where a user has defined a<P>
new print function.<P>
<P>
<B>Discussion:<P>
</B><P>
At least three members of the cleanup committee read this and think<P>
it looks good.<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>