From: pjb@informatimago.com (Pascal J. Bourguignon)
To: help-gnu-emacs@gnu.org
Subject: Re: defun vs lambda
Date: Fri, 31 Jul 2009 14:56:01 +0200 [thread overview]
Message-ID: <7c8wi5nh9a.fsf@pbourguignon.anevia.com> (raw)
In-Reply-To: 787f39a2-b016-4d10-9b28-12bb0bca17da@e27g2000yqm.googlegroups.com
weber <hugows@gmail.com> writes:
> I did wanted to generate code at run-time, similar to the way like
> defstruct generates helper functions.
"Times" are a little complicated matter in lisp.
There is a read-time, a macro-expansion time, a compilation-time, a
load-time and a run-time. In emacs lisp, things are a little
simplier, since there's no customizable reader macros, there's no way
to have user code executing at read-time. Nonetheless, there are
means to have code evaluated during each of four other times.
The problem is that more than "times" they are "phases", and they can
occur intermixed one with the other.
When at run-time, (eg in the scratch buffer you run the
eval-last-expression (C-x C-e) command to evaluate the form you just
typed), this form is first read; during this read-time, a lisp object
is built according the syntax of the characters that are "read". Then
the read object (which must be a form), is evaluated; during this
run-time, it may be noted that we must interpret a macro. We will
call corresponding macro-function, and we get the body of the macro
executed during this macro expansion time. The macro may build a new
function and call byte-compile to include a compiled function in the
returned form, so we will have a compilation-time, which further may
include macro-expansion times, etc. At compilation-time it is possible
to set things up to be evaluated at load-time, only when you use load
on the .elc file (see the load-time-value macro in (require 'cl)).
If you compile the file (byte-compile-file), then you go to the
compilation-time, it reads the source (read-time), then macroexpands
the macros (macro-expansion time), then generate the byte code. You
will later load it (load-time), and then you may execute it
(run-time). If your program generate *at run-time* new functions, it
may compile them (compilation-time, macro-expansion-time), and then
execute them (back at run-time). etc...
defstruct is a macro. You may use it in different circumstances,
let's consider the three following cases:
- You directly evaluate (defstruct point x y).
- You put (defstruct point x y) in a source file,
and then evaluate (byte-compile-file "point.el"),
- You put it in a function, such as:
(defun example ()
(defstruct point x y))
and you evaluate this form (which defines the function example),
or you have this function in a file, and you byte-compile-file it.
Then you load the file and you call the function (example).
Let's consider the second case. The compiler will read the defstruct
form, will notice that the symbol defstruct is defined as a macro, and
the will call the corresponding macro-function. Now at macroexpansion
time, defstruct can work, and generate the accessor functions needed
from the arguments (a list of fields). Notice that the macro
generates the functions at macro-expansion time, not at run-time. It
returns a progn form containing defun forms amongst others. The
compiler then compile that form, including the function definitions,
and generate the byte-code of these functions in a file, along with
the byte-code needed to _define_ these functions, that is, to
associate the compiled function byte code to the symbol that name the
function.
Later you will load this byte-code file, and will have the function
definition loaded in your emacs lisp image. No run-time has occured,
yet your functions are already defined.
In the third case, when compiling the defun form, the compiler will
macroexpand the defstruct form, and will generate the body of the
example function, which contain only the instructions needed to
_define_ these function, that is, to associate the compiled function
byte code to the symbol that name the function.
Then you call (example), and at run-time, the accessors of the
structure (which have been generated at macro-expansion time during
compilation of the function example), will be _defined_, that is
associated to the symbol that name the accessor function. Still, no
code has been generated at run-time.
In the first case, you might have the impression that defstruct
generate code at run-time but as the above explications, and
particularly the third case, may lead you to understand, when
evaluating the defstruct form, there's a macroexpansion time during
which the functions are generated, and then what is executed at
run-time, is only the binding of these functions to their name (when
executing the defun forms). Even without a compilation time, when
interpreting code, the function generation done by macros is not
exactly done at run-time.
The reason why it is not the case, and why it is not needed to
generate the code at run-time, is that all the information needed to
generate these defstruct accessor functions is known before run-time:
none of the arguments of the defstruct macro are evaluated (at
run-time). They're taken literally, and known at compilation-time.
The macro just returns the accessor function definition, it doesn't
need to return forms that would evaluate the arguments (at run-time)
and generate the functions (at run-time), since it has all the
information to do it itself at macro-expansion time.
Therefore since your sentence is contradictory, I still don't know
when you want to generate your functions...
--
__Pascal Bourguignon__
prev parent reply other threads:[~2009-07-31 12:56 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-07-24 20:30 defun vs lambda weber
2009-07-24 21:23 ` Anselm Helbig
2009-07-24 21:46 ` Pascal J. Bourguignon
2009-07-31 11:41 ` weber
2009-07-31 12:56 ` Pascal J. Bourguignon [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=7c8wi5nh9a.fsf@pbourguignon.anevia.com \
--to=pjb@informatimago.com \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).