* defining macros within eval @ 2022-10-16 9:39 Paul Jarc 2022-10-16 14:07 ` Jean Abou Samra ` (2 more replies) 0 siblings, 3 replies; 7+ messages in thread From: Paul Jarc @ 2022-10-16 9:39 UTC (permalink / raw) To: guile-user 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. It happens when eval is within begin or let, but not at the top level. 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. ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: defining macros within eval 2022-10-16 9:39 defining macros within eval Paul Jarc @ 2022-10-16 14:07 ` Jean Abou Samra 2022-10-16 16:21 ` Maxime Devos 2022-10-16 16:13 ` Maxime Devos 2022-10-16 16:17 ` Maxime Devos 2 siblings, 1 reply; 7+ messages in thread From: Jean Abou Samra @ 2022-10-16 14:07 UTC (permalink / raw) To: Paul Jarc, guile-user 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 ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: defining macros within eval 2022-10-16 14:07 ` Jean Abou Samra @ 2022-10-16 16:21 ` Maxime Devos 0 siblings, 0 replies; 7+ messages in thread From: Maxime Devos @ 2022-10-16 16:21 UTC (permalink / raw) To: Jean Abou Samra, Paul Jarc, guile-user [-- Attachment #1.1.1: Type: text/plain, Size: 758 bytes --] On 16-10-2022 16:07, Jean Abou Samra wrote: > >> 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. [...] Possibly in Paul Jarc's case, while they the macros might be computed, they might also be the same between runs of "guile -l do-something.scm". If that's the case (i.e., the expression read from the file remains unchanged), byte compiling is possible, just use eval-when, see my reply. Greetings, Maxime. [-- Attachment #1.1.2: OpenPGP public key --] [-- Type: application/pgp-keys, Size: 929 bytes --] [-- Attachment #2: OpenPGP digital signature --] [-- Type: application/pgp-signature, Size: 236 bytes --] ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: defining macros within eval 2022-10-16 9:39 defining macros within eval Paul Jarc 2022-10-16 14:07 ` Jean Abou Samra @ 2022-10-16 16:13 ` Maxime Devos 2022-10-16 16:17 ` Maxime Devos 2 siblings, 0 replies; 7+ messages in thread From: Maxime Devos @ 2022-10-16 16:13 UTC (permalink / raw) To: Paul Jarc, guile-user [-- Attachment #1.1.1: Type: text/plain, Size: 1216 bytes --] On 16-10-2022 11:39, Paul Jarc wrote: > 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. It happens when eval is within begin or let, but not at > the top level. 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. Surround (eval ...) by (eval-when (expand) ...). Section '(guile)Eval-when' explains the 'why'. Depending on where you are using 'rule' and what the real 'rule' is, you might need the other 'load' and 'eval' as well. IIUC, the previous 'lazy macros' (?) system of 1.8 (which didn't need the eval-when thing (?)) was rather impractical to do optimisation with, hence the more conventional 'eval-when' as found in other Schemes. Greetings, Maxime. [-- Attachment #1.1.2: OpenPGP public key --] [-- Type: application/pgp-keys, Size: 929 bytes --] [-- Attachment #2: OpenPGP digital signature --] [-- Type: application/pgp-signature, Size: 236 bytes --] ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: defining macros within eval 2022-10-16 9:39 defining macros within eval Paul Jarc 2022-10-16 14:07 ` Jean Abou Samra 2022-10-16 16:13 ` Maxime Devos @ 2022-10-16 16:17 ` Maxime Devos 2022-10-19 8:42 ` Paul Jarc 2 siblings, 1 reply; 7+ messages in thread From: Maxime Devos @ 2022-10-16 16:17 UTC (permalink / raw) To: Paul Jarc, guile-user [-- Attachment #1.1.1: Type: text/plain, Size: 851 bytes --] On 16-10-2022 11:39, Paul Jarc wrote: > 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> [...] See my previous reply, and also are you sure that 'eval' is appropriate here? Would datum->syntax + read tricks work instead? For an example in the wild, see e.g. <https://git.gnunet.org/gnunet-scheme.git/tree/gnu/gnunet/message/protocols.scm>. (the (include-from-path "gnu/.../protocols.scmgen" is not relevant here, you could inline protocols.scmgen in that example -- I just found separating it in a separate file a nicer structure). Greetings, Maxime. [-- Attachment #1.1.2: OpenPGP public key --] [-- Type: application/pgp-keys, Size: 929 bytes --] [-- Attachment #2: OpenPGP digital signature --] [-- Type: application/pgp-signature, Size: 236 bytes --] ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: defining macros within eval 2022-10-16 16:17 ` Maxime Devos @ 2022-10-19 8:42 ` Paul Jarc 2022-10-19 17:36 ` Maxime Devos 0 siblings, 1 reply; 7+ messages in thread From: Paul Jarc @ 2022-10-19 8:42 UTC (permalink / raw) To: guile-user Maxime Devos <maximedevos@telenet.be> wrote: > For an example in the wild, see > e.g. <https://git.gnunet.org/gnunet-scheme.git/tree/gnu/gnunet/message/protocols.scm>. Thanks (to Jean as well) for all the suggestions. Can you point me to an example of where include/sexp is used? To take a step back, my ultimate goal is to have a separate module system, where code libraries are identified by their full pathname rather than an abstract name applied to %load-path. It's similar to include in that way, but the loaded code goes into its own module and only certain bindings are imported, as with :select from use-modules. Each file is loaded only once per run, and typically doesn't change between runs, so somehow it ought to work with compilation enabled. I'll try playing around with include, eval-when, and datum->syntax to see if I can get something working, but if the extra context brings anything else to mind, more suggestions are welcome too. This feels like it would be trivial if the module system had a little more of its guts exposed. ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: defining macros within eval 2022-10-19 8:42 ` Paul Jarc @ 2022-10-19 17:36 ` Maxime Devos 0 siblings, 0 replies; 7+ messages in thread From: Maxime Devos @ 2022-10-19 17:36 UTC (permalink / raw) To: Paul Jarc, guile-user [-- Attachment #1.1.1: Type: text/plain, Size: 868 bytes --] On 19-10-2022 10:42, Paul Jarc wrote: > Maxime Devos <maximedevos@telenet.be> wrote: >> For an example in the wild, see >> e.g. <https://git.gnunet.org/gnunet-scheme.git/tree/gnu/gnunet/message/protocols.scm>. > > Thanks (to Jean as well) for all the suggestions. Can you point me to > an example of where include/sexp is used? It is used by protocols.scmgen (which is included by protocols.scm) -- protocols.scm and protocols.scmgen form a pair, you'll have to read both of them. > To take a step back, my ultimate goal is to have a separate module > system, where code libraries are identified by their full pathname > rather than an abstract name applied to %load-path. [...] 'load-compiled' (if you compile things separately) or 'load' otherwise may be useful, they accept file names instead of module names. Greetings, Maxime. [-- Attachment #1.1.2: OpenPGP public key --] [-- Type: application/pgp-keys, Size: 929 bytes --] [-- Attachment #2: OpenPGP digital signature --] [-- Type: application/pgp-signature, Size: 236 bytes --] ^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2022-10-19 17:36 UTC | newest] Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2022-10-16 9:39 defining macros within eval Paul Jarc 2022-10-16 14:07 ` Jean Abou Samra 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
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).