From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Jean Abou Samra Newsgroups: gmane.lisp.guile.user Subject: Re: defining macros within eval Date: Sun, 16 Oct 2022 16:07:59 +0200 Message-ID: <966a9085-bc84-c53f-56ac-812ddf9d60b5@abou-samra.fr> References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="32817"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.3.1 To: Paul Jarc , guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sun Oct 16 16:08:30 2022 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ok4Ja-0008NP-5A for guile-user@m.gmane-mx.org; Sun, 16 Oct 2022 16:08:30 +0200 Original-Received: from localhost ([::1]:38052 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ok4JY-0002fv-O4 for guile-user@m.gmane-mx.org; Sun, 16 Oct 2022 10:08:28 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:60932) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ok4JN-0002fZ-5E for guile-user@gnu.org; Sun, 16 Oct 2022 10:08:17 -0400 Original-Received: from mout.kundenserver.de ([212.227.126.134]:41207) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ok4JL-0006tV-H4 for guile-user@gnu.org; Sun, 16 Oct 2022 10:08:16 -0400 Original-Received: from [10.188.149.104] ([46.193.67.184]) by mrelayeu.kundenserver.de (mreue010 [212.227.15.168]) with ESMTPSA (Nemesis) id 1MJEMt-1oRAuf24Lr-00Kg3R; Sun, 16 Oct 2022 16:08:02 +0200 Content-Language: en-US In-Reply-To: X-Provags-ID: V03:K1:qerCWU+wTdugv1bc3whRQxMrvGwlXsIIto6yRFM+Uos4doHesEj T2CLvoatW7NyyC/yxUd572Y8XFzmFx6zJLotognt7+nzu4wUrqG+AfjiHztzfiu+hEg5GoS YVZ5+7Zn4BoSaG1EhP6gEFri+St99HntdMRKipPsVE0BvHI39NAfVGZfcncN7Q3k8OueHmM +wnTJvNAf5U1tNXmv8/Zw== X-UI-Out-Filterresults: notjunk:1;V03:K0:ifkCQ8oZMyE=:FqKCegyQl0+LNpzKX9lQTb db48hnE5DDz8EZKQZ5SBu/0F9aowZzGW/tiwl8CSGHmevOQqISJJGRaYPVELDat5U1fN0rEQu Dd6Ar5XZlwtL4X4KKJUwqYyi40/sXjIWFwix8RCdt+q1VUEKpkOveiluZEuqEokraJnyw2mjt RuyYTiq5m1y+UZ9L2CqzZsRjjhDmyVDziXow8zoyLekW1mFwlGsFmlQrYqiS/G6njV7yHbpSh dUhEPafI5bKE2jiLabaI80eZn7Yy3fXOD+iv6/Pg3IJ1RFm6M4vXh2YaMyOHJHjit7tW8slMB ZsUdQKAfibgUVCXEyuJxJIES8u/7npixjSYvqcFC/dNyHzA+d1hZXwUh1XL7QPRYfYKWQjare YuGfpeMOmGLLQmbIeBKUze9KGzbFZlzHV8F05kbx95ao3v9NdDXSDQdsYmldwH9O1CsqJd0vO 7483x129BYWhqCr4AKViQ1u9RiVd1LbgRj1lBz69q1O/mfMZ1MXwIxcGl0zS5zYPB2+LSaeS3 pmm2/sCcQMUUPGOoPoZPDr2xxnKcF1oy7xRhgjIpt8kNsIfz92db+mC9UjuSQ1+WAhBOoh8zZ ytO0QA+87z9vyl7N589fZNnWPxc5a2v1kdoWw96UuBs8W7/Df0hFYJ+yA3/rPiKYNVYbQrokl /nR1S50TvjwvzjxZZZlZgdFkIVqZcn8gbs57HcWz9C3E8c4yAf4q58dt36Aqw6qOlrtSlpxbY Z6fVtOzh9vKaelYdLwBqJyFfwOENAjBaLK9iFi+24jy6YI5CES9Kwgx5i1KCYq1Ck8Wjz65q Received-SPF: none client-ip=212.227.126.134; envelope-from=jean@abou-samra.fr; helo=mout.kundenserver.de X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, NICE_REPLY_A=-0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.io gmane.lisp.guile.user:18657 Archived-At: 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: # > > 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