unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Zelphir Kaltstahl <zelphirkaltstahl@posteo.de>
To: Vivien Kraus <vivien@planete-kraus.eu>
Cc: Guile User <guile-user@gnu.org>
Subject: Re: Control the order of expansion of syntax-case macros
Date: Fri, 20 Dec 2024 20:12:56 +0000	[thread overview]
Message-ID: <baca6c40-d32f-4fc4-88bc-834f7e12c00b@posteo.de> (raw)
In-Reply-To: <1bfa042ed3719a10006789205e954bb53c83cf45.camel@planete-kraus.eu>

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


      parent reply	other threads:[~2024-12-20 20:12 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-17 16:33 Control the order of expansion of syntax-case macros Vivien Kraus
2024-12-20  9:15 ` Maxime Devos via General Guile related discussions
2024-12-20 17:01   ` Vivien Kraus
2024-12-20 10:38 ` Nala Ginrut
2024-12-20 20:12 ` Zelphir Kaltstahl [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/guile/

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

  git send-email \
    --in-reply-to=baca6c40-d32f-4fc4-88bc-834f7e12c00b@posteo.de \
    --to=zelphirkaltstahl@posteo.de \
    --cc=guile-user@gnu.org \
    --cc=vivien@planete-kraus.eu \
    /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).