unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Jean Abou Samra <jean@abou-samra.fr>
To: Paul Jarc <prj@case.edu>, guile-user@gnu.org
Subject: Re: defining macros within eval
Date: Sun, 16 Oct 2022 16:07:59 +0200	[thread overview]
Message-ID: <966a9085-bc84-c53f-56ac-812ddf9d60b5@abou-samra.fr> (raw)
In-Reply-To: <ozkdlepgcdl0.fsf@webapps-d-2.tis.cwru.edu>

Le 16/10/2022 à 11:39, Paul Jarc a écrit :
> Hi.  I'm updating some old code to work with newer versions of Guile.
> This example used to work with 1.8, but gives an error with 2.2 and
> later:
>
> (begin
>    (eval '(define-syntax-rule (rule x) x) (current-module))
>    (display (rule "ok\n")))
>
> ERROR: Wrong type to apply: #<syntax-transformer rule>
>
> The error happens for define-syntax-rule and define-macro, but not
> plain define.



In Guile 2 and 3, the main way to run code is to byte-compile it.
This is what happens by default (Guile will print a note the first
time: "auto-compilation is enabled, ..."). In this mode, Guile will
first compile the .scm file into a .go bytecode file. This requires
doing all the macro expansion. Since the code run by eval is not
necessarily known at compile-time, it can't define macros used by
the compiled code.

What happens here is that the eval call just adds a syntax transformer
in the current module, and it would be used if it had been available
at the time of compilation, but it is too late, and it is just looked
up in the module and applied as a normal function, which fails.



> It happens when eval is within begin or let, but not at
> the top level.


Kind of. Something like

(eval '(define-syntax-rule (rule x) x) (current-module))
(display (rule "ok\n"))

will work in the REPL but not in a script, because in the REPL the
expansion is done step-by-step (since the result for an S-expr is
printed as soon as you enter it), where as in a file, it is done
in batch.


> Is there some way to make this work?  In my real code,
> the expression is read from a file, where it might be a macro
> definition or anything else, and it's evaluated in a different module
> from the current one.


You cannot byte-compile code in advance if it uses macros that
are only known dynamically. What you can do is using the evaluator
to run your code instead of the compiler. For example, if you set
GUILE_AUTO_COMPILE=0 and clear the bytecode cache (for me it's under
~/.cache/guile/ccache), running a script will no longer used compiled
bytecode but go through the evaluator, and in this case it works.

Be aware, though, that debugging evaluated code is a bit of a hell
because you won't get source locations for error messages, and
backtraces won't be in terms of the source code being run but in
terms of the source code of Guile's evaluator, the file
module/ice-9/eval.scm in the Guile source code.

Regards,
Jean




  reply	other threads:[~2022-10-16 14:07 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-16  9:39 defining macros within eval Paul Jarc
2022-10-16 14:07 ` Jean Abou Samra [this message]
2022-10-16 16:21   ` Maxime Devos
2022-10-16 16:13 ` Maxime Devos
2022-10-16 16:17 ` Maxime Devos
2022-10-19  8:42   ` Paul Jarc
2022-10-19 17:36     ` Maxime Devos

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/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=966a9085-bc84-c53f-56ac-812ddf9d60b5@abou-samra.fr \
    --to=jean@abou-samra.fr \
    --cc=guile-user@gnu.org \
    --cc=prj@case.edu \
    /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).