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

162 lines
13 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 FUNCTION-DEFINITION 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/iss172_w.htm">
<LINK REL=UP HREF="../Issues/iss173.htm">
<LINK REL=NEXT HREF="../Issues/iss174_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/iss172_w.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="../Issues/iss173.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="../Issues/iss174_w.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
<HR>
<H2>Issue FUNCTION-DEFINITION Writeup</H2>
<PRE><B>Issue:</B> <A HREF="iss173.htm">FUNCTION-DEFINITION</A><P>
<B>References:</B> Issue <A HREF="iss175_m.htm">FUNCTION-TYPE</A><P>
<B>Category:</B> ADDITION<P>
<B>Edit history:</B> 23-Jun-88, Version 1 by Pitman<P>
09-Dec-88, Version 2 by GZ (change name, remove REQUIRED<P>
option, specify first value to be a <A REL=DEFINITION HREF="../Body/a_lambda.htm#lambda"><B>lambda</B></A>, add to<P>
discussion and current practice)<P>
10-Feb-89, Version 3, as amended Jan 89 X3J13<P>
<P>
<B>Problem Description:<P>
</B><P>
There are portable ways to turn symbols and lists into functions,<P>
but there are no portable ways to get back the original symbols and<P>
lists given the functions.<P>
<P>
In many cases, it used to be possible to recover the <A REL=DEFINITION HREF="../Body/a_lambda.htm#lambda"><B>lambda</B></A> expression<P>
after a <A REL=DEFINITION HREF="../Body/m_defun.htm#defun"><B>DEFUN</B></A> by using <A REL=DEFINITION HREF="../Body/a_fn.htm#function"><B>FUNCTION</B></A> or <A REL=DEFINITION HREF="../Body/f_symb_1.htm#symbol-function"><B>SYMBOL-FUNCTION</B></A>, but the passage of<P>
<A HREF="iss175_m.htm">FUNCTION-TYPE</A> will make this no longer be the case.<P>
<P>
<B>Proposal (FUNCTION-DEFINITION:JAN89-X3J13):<P>
</B><P>
Introduce a new function called <A REL=DEFINITION HREF="../Body/f_fn_lam.htm#function-lambda-expression"><B>FUNCTION-LAMBDA-EXPRESSION</B></A><P>
which takes as its argument a function and returns three values:<P>
#1: The function's defining <A REL=DEFINITION HREF="../Body/a_lambda.htm#lambda"><B>lambda</B></A> expression, or <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A> if the information<P>
is not available. The <A REL=DEFINITION HREF="../Body/a_lambda.htm#lambda"><B>lambda</B></A> expression may have been pre-processed<P>
in some ways, but it should remain a suitable argument to <A REL=DEFINITION HREF="../Body/f_cmp.htm#compile"><B>COMPILE</B></A><P>
or <A REL=DEFINITION HREF="../Body/a_fn.htm#function"><B>FUNCTION</B></A>.<P>
#2: <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A> if the definition was enclosed in the null lexical<P>
environment or something non-NIL if the definition might<P>
have been enclosed in some non-null lexical environment.<P>
#3: the `name' of this function. The name is intended for debugging<P>
only and may be any lisp object -- not necessarily one that would<P>
be valid for use as a name in <A REL=DEFINITION HREF="../Body/m_defun.htm#defun"><B>DEFUN</B></A> or <A REL=DEFINITION HREF="../Body/a_fn.htm#function"><B>FUNCTION</B></A>, for example.<P>
By convention, <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A> is used to mean that the function had no name.<P>
<P>
Implementations are free to always return <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A> T <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A>, but are encouraged<P>
to return the <A REL=DEFINITION HREF="../Body/a_lambda.htm#lambda"><B>lambda</B></A> expression in the case where the argument was created<P>
by an in-core call to <A REL=DEFINITION HREF="../Body/f_cmp.htm#compile"><B>COMPILE</B></A> or <A REL=DEFINITION HREF="../Body/f_eval.htm#eval"><B>EVAL</B></A> (as opposed to being created by<P>
loading a compiled file).<P>
<P>
<B>Examples:<P>
</B> <P>
The following examples illustrate some possible return values, but<P>
are not intended to be exhaustive:<P>
<P>
#1: (<A REL=DEFINITION HREF="../Body/f_fn_lam.htm#function-lambda-expression"><B>FUNCTION-LAMBDA-EXPRESSION</B></A> #'(<A REL=DEFINITION HREF="../Body/a_lambda.htm#lambda"><B>LAMBDA</B></A> (X) X))<P>
or (<A REL=DEFINITION HREF="../Body/f_fn_lam.htm#function-lambda-expression"><B>FUNCTION-LAMBDA-EXPRESSION</B></A><P>
(<A REL=DEFINITION HREF="../Body/f_funcal.htm#funcall"><B>FUNCALL</B></A> #'(<A REL=DEFINITION HREF="../Body/a_lambda.htm#lambda"><B>LAMBDA</B></A> () #'(<A REL=DEFINITION HREF="../Body/a_lambda.htm#lambda"><B>LAMBDA</B></A> (X) X))))<P>
might return <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A>, <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A>, <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A><P>
or (<A REL=DEFINITION HREF="../Body/a_lambda.htm#lambda"><B>LAMBDA</B></A> (X) X), T, <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A><P>
or (<A REL=DEFINITION HREF="../Body/a_lambda.htm#lambda"><B>LAMBDA</B></A> (X) X), <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A>, <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A><P>
<P>
#2: (<A REL=DEFINITION HREF="../Body/f_fn_lam.htm#function-lambda-expression"><B>FUNCTION-LAMBDA-EXPRESSION</B></A> <P>
(<A REL=DEFINITION HREF="../Body/f_funcal.htm#funcall"><B>FUNCALL</B></A> #'(<A REL=DEFINITION HREF="../Body/a_lambda.htm#lambda"><B>LAMBDA</B></A> (X) #'(<A REL=DEFINITION HREF="../Body/a_lambda.htm#lambda"><B>LAMBDA</B></A> () X)) <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A>))<P>
might return <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A>, T, <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A><P>
or (<A REL=DEFINITION HREF="../Body/a_lambda.htm#lambda"><B>LAMBDA</B></A> () X), T, <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A><P>
but -not- (<A REL=DEFINITION HREF="../Body/a_lambda.htm#lambda"><B>LAMBDA</B></A> () X), <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A>, <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A><P>
<P>
#3: (<A REL=DEFINITION HREF="../Body/m_defun.htm#defun"><B>DEFUN</B></A> FOO (X) X)<P>
(<A REL=DEFINITION HREF="../Body/a_setf.htm#setf"><B>SETF</B></A> (<A REL=DEFINITION HREF="../Body/f_symb_1.htm#symbol-function"><B>SYMBOL-FUNCTION</B></A> 'BAR) #'FOO)<P>
(<A REL=DEFINITION HREF="../Body/f_fn_lam.htm#function-lambda-expression"><B>FUNCTION-LAMBDA-EXPRESSION</B></A> #'BAR)<P>
might return <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A>, <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A>, <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A><P>
or (<A REL=DEFINITION HREF="../Body/a_lambda.htm#lambda"><B>LAMBDA</B></A> (X) (<A REL=DEFINITION HREF="../Body/s_block.htm#block"><B>BLOCK</B></A> FOO X)), T, <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A><P>
or (<A REL=DEFINITION HREF="../Body/a_lambda.htm#lambda"><B>LAMBDA</B></A> (X) (<A REL=DEFINITION HREF="../Body/s_block.htm#block"><B>BLOCK</B></A> FOO X)), <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A>, FOO<P>
or (SI::BLOCK-LAMBDA FOO (X) X), <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A>, FOO<P>
<P>
#4: (<A REL=DEFINITION HREF="../Body/m_defun.htm#defun"><B>DEFUN</B></A> FOO ()<P>
(<A REL=DEFINITION HREF="../Body/s_flet_.htm#flet"><B>FLET</B></A> ((BAR (X) X))<P>
#'BAR))<P>
(<A REL=DEFINITION HREF="../Body/f_fn_lam.htm#function-lambda-expression"><B>FUNCTION-LAMBDA-EXPRESSION</B></A> (FOO))<P>
might return <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A>, T, <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A><P>
or (<A REL=DEFINITION HREF="../Body/a_lambda.htm#lambda"><B>LAMBDA</B></A> (X) (<A REL=DEFINITION HREF="../Body/s_block.htm#block"><B>BLOCK</B></A> BAR X)), T, <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A><P>
or (<A REL=DEFINITION HREF="../Body/a_lambda.htm#lambda"><B>LAMBDA</B></A> (X) (<A REL=DEFINITION HREF="../Body/s_block.htm#block"><B>BLOCK</B></A> BAR X)), T, (:INTERNAL FOO 0 BAR)<P>
or (<A REL=DEFINITION HREF="../Body/a_lambda.htm#lambda"><B>LAMBDA</B></A> (X) (<A REL=DEFINITION HREF="../Body/s_block.htm#block"><B>BLOCK</B></A> BAR X)), <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A>, &quot;BAR in FOO&quot;<P>
<P>
<B>Rationale:<P>
</B> <P>
This functionality would be useful to a pretty printer, debugger,<P>
inspector, and other tools.<P>
<P>
<B>Cost to Implementors:<P>
</B> <P>
The following implementation is technically legitimate, although many<P>
implementations would want to <A REL=DEFINITION HREF="../Body/f_provid.htm#provide"><B>provide</B></A> something more useful:<P>
<P>
(<A REL=DEFINITION HREF="../Body/m_defun.htm#defun"><B>DEFUN</B></A> <A REL=DEFINITION HREF="../Body/f_fn_lam.htm#function-lambda-expression"><B>FUNCTION-LAMBDA-EXPRESSION</B></A> (<A REL=DEFINITION HREF="../Body/a_fn.htm#function"><B>FUNCTION</B></A>)<P>
(<A REL=DEFINITION HREF="../Body/m_check_.htm#check-type"><B>CHECK-TYPE</B></A> <A REL=DEFINITION HREF="../Body/a_fn.htm#function"><B>FUNCTION</B></A> <A REL=DEFINITION HREF="../Body/a_fn.htm#function"><B>FUNCTION</B></A>)<P>
(<A REL=DEFINITION HREF="../Body/a_values.htm#values"><B>VALUES</B></A> <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A> T <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A>))<P>
<P>
<B>Current Practice:<P>
</B><P>
Many implementations record this information, but not all that do<P>
publish an interface to extracting the information. VAXLISP and<P>
Lucid call it SOURCE-CODE. Coral calls it UNCOMPILE-FUNCTION.<P>
The language T has this operation and calls it DISCLOSE. It is the<P>
conceptual inverse of the ENCLOSE which occurs in some Scheme dialects,<P>
and is implemented as what CLOS would call a &quot;generic function&quot;.<P>
<P>
<B>Cost to Users:<P>
</B><P>
None. The change is upward compatible.<P>
<P>
<B>Cost of Non-Adoption:<P>
</B><P>
Certain kinds of portable debugging tools would be harder to write.<P>
<P>
<B>Benefits:<P>
</B><P>
The cost of non-adoption would be avoided.<P>
<P>
<B>Aesthetics:<P>
</B><P>
The phrase ``program is data; data is program'' comes up a lot in discussions<P>
about Lisp. This makes the case for ``program is data'' more interesting.<P>
<P>
<B>Discussion:<P>
</B><P>
The initial name proposed for this function was <A HREF="iss173.htm">FUNCTION-DEFINITION</A>. This<P>
was changed because of technical uses of the term ``definition'' to refer<P>
to the entire defining form (e.g. a <A REL=DEFINITION HREF="../Body/m_defun.htm#defun"><B>DEFUN</B></A> form) rather than the <A REL=DEFINITION HREF="../Body/a_lambda.htm#lambda"><B>lambda</B></A><P>
expression that might be recovered from it.<P>
<P>
The possibility of _requiring_ the <A REL=DEFINITION HREF="../Body/a_lambda.htm#lambda"><B>lambda</B></A> expression to be available for<P>
all functions created by in-core calls to <A REL=DEFINITION HREF="../Body/f_cmp.htm#compile"><B>COMPILE</B></A> or <A REL=DEFINITION HREF="../Body/a_fn.htm#function"><B>FUNCTION</B></A> was considered<P>
but didn't receive much support. Pitman would prefer that option because<P>
it is considerably more useful in practice, but would like to see at least<P>
the current proposal.<P>
<P>
Jan 89 X3J13 amended the proposal of Version 2 to change the name<P>
from FUNCTION-SOURCE to <A REL=DEFINITION HREF="../Body/f_fn_lam.htm#function-lambda-expression"><B>FUNCTION-LAMBDA-EXPRESSION</B></A>.<P>
<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>