From: Mark S McWhinney
Subject: Getting a function name
Date: 
Message-ID: <1992Jun26.034114.234662@cs.cmu.edu>
I am new Lisp user and I am trying to find out how to get the name of
the currently executing function. So far I have found *package* but
nothing for the function name. I need it to be able to write an
assertion function or macro that prints the name of a function that
has failed an assertion.


I also want to be able to print the name of a variable and its value
with a single function or macro call. For example,

(setf foo '(a b c))
(bar foo)

where bar is a function or macro that prints the symbol name "foo" and
its value

"Foo = (a b c)"


I suspect that these two things are trivial in Lisp, but I have not
been able to figure them out despite hours of pooring over Steele's
book.

Thanks in advance.

From: Barry Margolin
Subject: Re: Getting a function name
Date: 
Message-ID: <12ebhfINN7o0@early-bird.think.com>
In article <·······················@cs.cmu.edu> ···@mse.cmu.edu (Mark S McWhinney) writes:
>I am new Lisp user and I am trying to find out how to get the name of
>the currently executing function. So far I have found *package* but
>nothing for the function name. I need it to be able to write an
>assertion function or macro that prints the name of a function that
>has failed an assertion.

There's no built-in mechanism for this.  Common Lisp implementations aren't
even required to remember the name of a function (they have to provide
name->function mappings, but not the other way around).

The simplest solution to your original problem is to require that the name
of the function be passed as an argument to the assertion function or
macro.

You can get what you specifically asked for by writing your own
function-defining macro, e.g.

(defvar *current-function* nil)

(defmacro defun-bind-name (name arglist &body body)
  (let ((appendage `((*current-function* ',name))))
    (unless (member '&aux arglist)
      (push '&aux appendage))
    (setq arglist (append arglist appendage))
    `(defun ,name ,arglist ,@body)))

Then use DEFUN-BIND-NAME instead of DEFUN to define your functions.  Your
assertion function, or the expansion of your assertion macro, can access
the variable *CURRENT-FUNCTION* to get the current function name.

>I also want to be able to print the name of a variable and its value
>with a single function or macro call. For example,
>
>(setf foo '(a b c))
>(bar foo)
>
>where bar is a function or macro that prints the symbol name "foo" and
>its value
>
>"Foo = (a b c)"

(defmacro print-expression (expression &optional (stream '*standard-output*))
  `(let ((value ,expression)
	 (stream ,stream))
     (format stream "~&~S = ~S~%" ',expression value)))
-- 
Barry Margolin
System Manager, Thinking Machines Corp.

······@think.com          {uunet,harvard}!think!barmar
From: Gregor Kiczales
Subject: Re: Getting a function name
Date: 
Message-ID: <GREGOR.92Jul1145657@tracer-bullet.parc.xerox.com>
    From: ······@think.com (Barry Margolin)
    Date: 26 Jun 1992 05:55:59 GMT

    >Mark S McWhinney writes:
    >I am new Lisp user and I am trying to find out how to get the name of
    >the currently executing function. So far I have found *package* but
    >nothing for the function name. I need it to be able to write an
    >assertion function or macro that prints the name of a function that
    >has failed an assertion.

    There's no built-in mechanism for this.  Common Lisp implementations aren't
    even required to remember the name of a function (they have to provide
    name->function mappings, but not the other way around).


I can't resist the temptation to suggest that extending the metaobject
protocol, beyond CLOS and out into the Lisp, would be a nice way to
address these sorts of problems.  The (meta)object in question already
exists, since Common Lisp has first class functions.  All that remains
to be exposed is a way to get a hold of the current function and
accessors like FUNCTION-NAME, FUNCTION-ARGLIST etc.

Access to the currently running function could be done as in 3-Lisp,
with a special form like CURRENT-FUNCTION.  So, the code in question
would look something like:

  (let ((name (function-name (current-function))))
    ...)

(Of course function names in this sense would be like generic function
names.  They have no real ``semantics'' they are just documentation.)

Of course if you were adding an introspective protocol, there might be
other things that would be nice to have, in order to build the the
different kinds of things an introspective protocol should allow:
debuggers, program analysis tools, new user interfaces etc.  For
example, this might be the appropriate framework within which to reify
environments and provide accessors for them.  It might also be nice to
have a way to trace back up calls and out lexical scopes.  An additional
question is whether the bodies of functions should be represented with
lists and symbols, or whether they should be ``objectified'' as well.

This isn't {\em just} idle speculation.  At PARC, we are working on
several metaobject protocols for Lisp itself (Scheme actually), not just
the OO extension.  We are doing an interpreter and two ``compilers'',
each of which is being used to get at different aspects of what a MOP
for Scheme might provide.  The interpreter, which is being done by Amin
Vahdat, provides much of the functionality mentioned above.  It also
provides intercessory functionality, thereby allowing the user to easily
create variants of the Scheme language.  One can, for example, add
partial closures (Lee&Friedman, Indiana University), or "active
variables" that have demons associated with them that run whenever the
variable is read or set.

Gregor