From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Zelphir Kaltstahl Newsgroups: gmane.lisp.guile.user Subject: Re: Control the order of expansion of syntax-case macros Date: Fri, 20 Dec 2024 20:12:56 +0000 Message-ID: References: <1bfa042ed3719a10006789205e954bb53c83cf45.camel@planete-kraus.eu> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="13876"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Guile User To: Vivien Kraus Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Fri Dec 20 21:13:59 2024 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 1tOjNm-0003SZ-VA for guile-user@m.gmane-mx.org; Fri, 20 Dec 2024 21:13:58 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tOjMz-0005P3-8B; Fri, 20 Dec 2024 15:13:09 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tOjMx-0005OH-AD for guile-user@gnu.org; Fri, 20 Dec 2024 15:13:07 -0500 Original-Received: from mout01.posteo.de ([185.67.36.65]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tOjMu-0002Yo-Je for guile-user@gnu.org; Fri, 20 Dec 2024 15:13:06 -0500 Original-Received: from submission (posteo.de [185.67.36.169]) by mout01.posteo.de (Postfix) with ESMTPS id A4DB7240028 for ; Fri, 20 Dec 2024 21:12:58 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.de; s=2017; t=1734725578; bh=LpRBdwge7GFgSblLDTTDIz1hgSoHslHk9v0LDFpPFwI=; h=Content-Type:Message-ID:Date:MIME-Version:Subject:To:From:Cc: From; b=nf7OCBGPyamAoseajKvhZk9XwYSoStYWCTBChFsj6i4uQxlsxwwKCQYNyEu/AffeT 99b9F99o7EH/vChXHUVi26Q0KLLY0LmF7QvJIaUsoqNgT2YO1Jsg9XUFRngks/1Wax OQobxCmF+owRNUKiO/bbD2/dswofBpd7KWh3TVbJkRWPKVyKcQDmMa3OAVqcIkMaGF 8d+wBH65sZI+jvmHyoSInCTP/XfHvKp2Y4KsoATmEvz/Z/AphtquFdDNdbUtJaiKzM dUhVTzE+yN+D70UDO5mHF/noBLJQiaN7GP+NR4J0UG37JlTUlubM8Fp0i8dEsT7DxN FU5EhwJrxgOrg== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4YFJWF1jFWz6twR; Fri, 20 Dec 2024 21:12:57 +0100 (CET) Content-Language: en-US In-Reply-To: <1bfa042ed3719a10006789205e954bb53c83cf45.camel@planete-kraus.eu> Received-SPF: pass client-ip=185.67.36.65; envelope-from=zelphirkaltstahl@posteo.de; helo=mout01.posteo.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Content-Filtered-By: Mailman/MimeDel 2.1.29 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-bounces+guile-user=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.lisp.guile.user:20021 Archived-At: Hello Vivien On 17.12.24 17:33, Vivien Kraus wrote: > Dear Guile users, > > syntax-case macros can have cool side effects at expansion time. > However, they are still draped in a veil of mystery to me. > > Consider this code: > > (define-syntax hello > (lambda (stx) > (syntax-case stx () > (_ > (begin > (format (current-error-port) "Hello!\n") > (datum->syntax #f "hello")))))) > > (define-syntax world > (lambda (stx) > (syntax-case stx () > (_ > (begin > (format (current-error-port) "World!\n") > (datum->syntax #f "world")))))) > > (define-syntax hello-world > (lambda (stx) > (syntax-case stx () > (_ > #`(string-append #,hello " " #,world))))) > > ,expand hello-world > > > Running it gives me: > > Hello! > World! > $1 = (string-append "hello" " " "world") > > Cool! Now, I suspect there are no clear rules to guess the order of > expansion (whether Hello! is printed before World! or after). I would > very much like to twist that order and reliably get World! and then > Hello!. How can I achieve that? > > (define-syntax hello-world > (lambda (stx) > (syntax-case stx () > (_ > (let* ((w #'world) > (h #'hello)) > #`(string-append #,h " " #,w)))))) > > With this modification, I get the same order: > > Hello! > World! > $1 = (string-append "hello" " " "world") > > So my guess is that syntax objects are expanded lazily. Is there > something I can do to get #'world expanded before #'hello? > > Best regards, > > Vivien I think CK macro style might make it easier/possible, since it is internally managing a stack of expressions and they compose like usual functions. It might also be overkill for this specific macro. But if this is not just an experiment, maybe the actual use case would warrant CK macro style. Check out the amazing https://okmij.org/ftp/Scheme/macros.html#ck-macros and not amazing, but perhaps helpful https://codeberg.org/ZelphirKaltstahl/guile-examples/src/commit/ece4060df673b1a3173856555aeca6d8d8c7fd25/macros/CK-macros/ck-macro.scm explanations as far as I have once understood what goes on. For a usage example you could look at https://codeberg.org/ZelphirKaltstahl/guile-examples/src/commit/ece4060df673b1a3173856555aeca6d8d8c7fd25/macros/contracts. Once I got my usage of the CK macro right, I found such macro way easier to use than normal macro which expands from left to right basically (maybe this is not technically correct to say, I don't know). Best regards, Zelphir -- repositories:https://notabug.org/ZelphirKaltstahl,https://codeberg.org/ZelphirKaltstahl