Hello,
this might be a stupid question, but can a function be written that
prints its symbol name?
E.g.
(defun foo ()
(princ my-symbol-name))
should print
foo
and the question is how to determine my-symbol-name...
Frank Roland <·········@frank-roland.de> writes:
> Hello,
>
> this might be a stupid question, but can a function be written that
> prints its symbol name?
>
> E.g.
> (defun foo ()
> (princ my-symbol-name))
>
> should print
>
> foo
>
> and the question is how to determine my-symbol-name...
You have some confusions here, but the main one is this:
A function is an entity unrelated to any naming.
#'(lambda () (princ my-symbol-name))
would be the anonymous function equivalent of what you did. It would
have no name.
Your DEFUN really doesn't do a lot more than
(setf (symbol-function 'foo)
#'(lambda () (princ my-symbol-name)))
which is the same as
(let ((my-function #'(lambda () (princ my-symbol-name))))
(setf (symbol-function 'foo) my-function))
So think about what would happen if you did:
(let ((my-function #'(lambda () (princ my-symbol-name))))
(setf (symbol-function 'foo) my-function)
(setf (symbol-function 'bar) my-function))
In this case, the identical function would be held by two different
functions, highlighting the fact that the mapping from functions to
names is, potentially at least, one to many.
Some systems remember the name that you originally called a function,
for debugging purposes (such as TRACE), but other systems do not.
It's not required by the language. Importantly, though, there are no
"good style" programs that you, if you are a Lisp novice, should
probably be writing that make use of such facilities.
Why do you want this?
Kent M Pitman wrote:
> Some systems remember the name that you originally called a function,
> for debugging purposes (such as TRACE), but other systems do not.
> It's not required by the language. Importantly, though, there are no
> "good style" programs that you, if you are a Lisp novice, should
> probably be writing that make use of such facilities.
>
> Why do you want this?
I always wish I had it because I hate typing, and I debug with print
statements like:
(trc "md-slot-value > entry" self slot-name)
If the TRC macro could get at the function name MD-SLOT-VALUE i could
type less.
I suppose I could write a macro to help here:
(trcdefun md-slot-value (self slot-name) etc etc
hmmm....the neat thing is I could detect recursion and indent printed
output appropriately. as it is I have a special with-trc macro I have to
jam into the code a lot more laboriously when the normal absence of
recursion hinting becomes a problem (not often, but if I can get it for
free...).
I just need to make sure TRC lexically captures the function name. If I
use a special and forget to change defun to trcdefun, the output will be
a little inscrutable. :)
--
kenny tilton
clinisys, inc
http://www.tilton-technology.com/
---------------------------------------------------------------
"Cells let us walk, talk, think, make love and realize
the bath water is cold." -- Lorraine Lee Cudmore
Kent M Pitman wrote:
> Frank Roland <·········@frank-roland.de> writes:
>
>
>>Hello,
>>
>>this might be a stupid question, but can a function be written that
>>prints its symbol name?
>>
>>E.g.
>>(defun foo ()
>> (princ my-symbol-name))
>>
>>should print
>>
>> foo
>>
>>and the question is how to determine my-symbol-name...
>
>
> You have some confusions here, but the main one is this:
>
> A function is an entity unrelated to any naming.
> #'(lambda () (princ my-symbol-name))
> would be the anonymous function equivalent of what you did. It would
> have no name.
>
> Your DEFUN really doesn't do a lot more than
> (setf (symbol-function 'foo)
> #'(lambda () (princ my-symbol-name)))
>
> which is the same as
>
> (let ((my-function #'(lambda () (princ my-symbol-name))))
> (setf (symbol-function 'foo) my-function))
>
> So think about what would happen if you did:
>
>
> (let ((my-function #'(lambda () (princ my-symbol-name))))
> (setf (symbol-function 'foo) my-function)
> (setf (symbol-function 'bar) my-function))
>
> In this case, the identical function would be held by two different
> functions, highlighting the fact that the mapping from functions to
> names is, potentially at least, one to many.
Please apologise if I have some problems regarding corret wording.
What I want, in your example above, is that the function my-function
prints foo for symbol-function foo and bar for symbol-function bar.
I think I could expand my question to (I may use wrong wording again):
Can a function check wheter it was called by use of a symbol and return
the name of the symbol?
I guess it can't.
>
> Some systems remember the name that you originally called a function,
> for debugging purposes (such as TRACE), but other systems do not.
> It's not required by the language. Importantly, though, there are no
> "good style" programs that you, if you are a Lisp novice, should
> probably be writing that make use of such facilities.
>
> Why do you want this?
I try to find a way to automatically generate log-files. The content of
the file should contain entry and exit of functions (maybe with
parameters and return values).
I could use trace, but then I would have to apply (again wrong wording?)
trace to each function and send its output to the file. (Okay, the might
be a way to redirect standard output or whatever trace uses, but I think
this is even worse). Furthermore I want to enable and disable the debug
output by simply setting a variable to t or nil.
One last thing. I have also some function from which I want to know
wheter their sambol-function has been rebound (e.g. by memoization).
Why? Simply because I plan to have some functions checking wheter a key
is valid. And I do not want the user to bypass my checking function :-)
(Although I might be still possible, but a bit harder).
Frank Roland <·········@frank-roland.de> writes:
> Can a function check wheter it was called by use of a symbol
> and return the name of the symbol?
No. This is what I tried to explain. A function is not a symbol.
Global functions (though not local functions) are stored in symbols
but at the time the function is called, it has no idea what function
name was used to access it. There might be, in some implementations,
a way to recover this due to incidental aspects of the implementation,
but you should not rely on such means in any portable program. If you
want to do this in non-portable fashion (and I REALLY don't recommend
you do this), you should contact your vendor and find out what
vendor-specific means might be available.
> I try to find a way to automatically generate log-files. The content
> of the file should contain entry and exit of functions (maybe with
> parameters and return values).
Use the function TRACE for this, possibly redirecting the stream in
*TRACE-OUTPUT* to a file. Do not try to do this in the way you are
presently trying.
> I could use trace, but then I would have to apply (again wrong
> wording?) trace to each function and send its output to the
> file. (Okay, the might be a way to redirect standard output or
> whatever trace uses, but I think this is even worse).
You can redirect *trace-output*.
(with-open-file (*trace-output* "my-traced-output.text" :direction :output
:if-exists :supersede)
(do-some-stuff-with-functions-you-have-traced))
> Furthermore I
> want to enable and disable the debug output by simply setting a
> variable to t or nil.
Why not by calling a function.
(defvar *to-trace*)
(defun enable-tracing ()
(dolist (fn *to-trace*)
(eval `(trace ,fn))))
(defun disable-tracing ()
(untrace))
> One last thing. I have also some function from which I want to know
> wheter their sambol-function has been rebound (e.g. by
> memoization).
You need to give an example. This is too vague and could mean any of
several things. The answer would be different for each such thing, and
I'm not going to answer for all of them.
> Why? Simply because I plan to have some functions
> checking wheter a key is valid. And I do not want the user to bypass
> my checking function :-)
> (Although I might be still possible, but a bit harder).
You can, of course, also just name the function yourself.
e.g.,
(defun *debug* nil)
(defun debug-data (format-string &rest format-args)
(when *debug*
(apply #'format *trace-output* format-string format-args)))
(defun foo (x)
(debug-data "~&Entering FOO x=~S~%" x)
...)
Kent M Pitman wrote:
> Frank Roland <·········@frank-roland.de> writes:
>
>
>> Can a function check wheter it was called by use of a symbol
>>and return the name of the symbol?
>
>
> No. This is what I tried to explain. A function is not a symbol.
> Global functions (though not local functions) are stored in symbols
> but at the time the function is called, it has no idea what function
> name was used to access it. There might be, in some implementations,
> a way to recover this due to incidental aspects of the implementation,
> but you should not rely on such means in any portable program. If you
> want to do this in non-portable fashion (and I REALLY don't recommend
> you do this), you should contact your vendor and find out what
> vendor-specific means might be available.
>
>
>>I try to find a way to automatically generate log-files. The content
>>of the file should contain entry and exit of functions (maybe with
>>parameters and return values).
>
>
> Use the function TRACE for this, possibly redirecting the stream in
> *TRACE-OUTPUT* to a file. Do not try to do this in the way you are
> presently trying.
>
>
>>I could use trace, but then I would have to apply (again wrong
>>wording?) trace to each function and send its output to the
>>file. (Okay, the might be a way to redirect standard output or
>>whatever trace uses, but I think this is even worse).
>
>
> You can redirect *trace-output*.
>
> (with-open-file (*trace-output* "my-traced-output.text" :direction :output
> :if-exists :supersede)
> (do-some-stuff-with-functions-you-have-traced))
>
>
>>Furthermore I
>>want to enable and disable the debug output by simply setting a
>>variable to t or nil.
>
>
> Why not by calling a function.
>
> (defvar *to-trace*)
> (defun enable-tracing ()
> (dolist (fn *to-trace*)
> (eval `(trace ,fn))))
> (defun disable-tracing ()
> (untrace))
>
Thanks. That idea is good. I think I will use that.
>
>>One last thing. I have also some function from which I want to know
>>wheter their sambol-function has been rebound (e.g. by
>>memoization).
>
>
> You need to give an example. This is too vague and could mean any of
> several things. The answer would be different for each such thing, and
> I'm not going to answer for all of them.
>
>
>>Why? Simply because I plan to have some functions
>>checking wheter a key is valid. And I do not want the user to bypass
>>my checking function :-)
>>(Although I might be still possible, but a bit harder).
>
>
> You can, of course, also just name the function yourself.
> e.g.,
>
> (defun *debug* nil)
>
> (defun debug-data (format-string &rest format-args)
> (when *debug*
> (apply #'format *trace-output* format-string format-args)))
>
>
> (defun foo (x)
> (debug-data "~&Entering FOO x=~S~%" x)
> ...)
>
The last thing is what I currently do.
On Mon, 10 Mar 2003 18:55:32 +0100, Frank Roland
<·········@frank-roland.de> wrote:
>Hello,
>
>this might be a stupid question, but can a function be written that
>prints its symbol name?
>
>E.g.
>(defun foo ()
> (princ my-symbol-name))
>
>should print
>
> foo
>
>and the question is how to determine my-symbol-name...
Just for fun, here is a macro which creates an alternative to DEFUN
which does what you might want:
(defmacro my-defun (name &rest lambda-and-body)
`(let ((my-symbol-name ',name))
(declare (ignorable my-symbol-name))
(defun ,name ,@lambda-and-body)))
(my-defun foo ()
(princ my-symbol-name))
or
(my-defun foo ()
(princ my-symbol-name)(values))
On Mon, 10 Mar 2003 20:34:26 GMT, ·····@corman.net (Roger Corman)
wrote:
>Just for fun, here is a macro which creates an alternative to DEFUN
>which does what you might want:
>
>(defmacro my-defun (name &rest lambda-and-body)
> `(let ((my-symbol-name ',name))
> (declare (ignorable my-symbol-name))
> (defun ,name ,@lambda-and-body)))
>
And for even more fun you can effectively redefine DEFUN so you
don't have to go changing all your DEFUN's to MY-DEFUN's:
(in-package :xyz :use '(:LISP))
#<PACKAGE "XYZ">
(shadow '("DEFUN") :xyz)
T
(defmacro xyz::defun (name &rest lambda-and-body)
`(let ((__function-name__ ',name))
(declare (ignorable __function-name__))
(lisp::defun ,name ,@lambda-and-body)))
DEFUN
DEFUN
(defun foo () (print __function-name__) (terpri) (+ 3 7))
(foo)
FOO
10
······@alum.mit.edu (JP Massar) writes:
> And for even more fun you can effectively redefine DEFUN so you
> don't have to go changing all your DEFUN's to MY-DEFUN's:
And in case anyone things 'why would anyone use this in real code',
Screamer [1] shadows DEFUN so it can analyse the code to see if it
uses any non-deterministic functions and re-arrange the code
accordingly.
[1] http://radio.weblogs.com/0102385/2003/03/12.html#a309
(Contains a description and examples of Screamer, links to it, and
to Screamer+)
Chris.
--
http://www.double.co.nz/cl