unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Mark H Weaver <mhw@netris.org>
To: "pelzflorian \(Florian Pelz\)" <pelzflorian@pelzflorian.de>
Cc: guile-user@gnu.org
Subject: Re: How to use-modules within macro?
Date: Wed, 04 Sep 2019 17:55:58 -0400	[thread overview]
Message-ID: <87imq7afp2.fsf@netris.org> (raw)
In-Reply-To: <87sgpbalbz.fsf@netris.org> (Mark H. Weaver's message of "Wed, 04 Sep 2019 15:54:13 -0400")

Hello again,

I wrote earlier:
> So, instead of using 'match' on the result of 'syntax->datum', you
> should instead use 'syntax-case' on the syntax object itself, like this
> (untested):
>
>   (let loop ((e #'exp))
>     (syntax-case e ()
>       (num
>        (number? (syntax->datum #'num))
>        #'(1+ num))
>       ((x ...)
>        (map loop #'(x ...)))
>       (y
>        #'y)))

I should mention that the use of 'map' directly on a syntax object is
only allowable in certain cases.  Here, we assume that the syntax object
produced by #'(x ...) is a normal Scheme list.  That is _not_ the case
for arbitrary syntax objects that correspond to a list.  For example, it
would _not_ be safe to pass 'e' as the list argument to 'map', although
'e' is in some sense equivalent to #(x ...) in the second clause above.

The reason is that syntax objects contain information about the
associated lexical environment which starts at the top of the expression
tree, and is *lazily* pushed down into the subexpressions as the tree is
taken apart using 'syntax-case' and put back together using 'syntax'
(a.k.a. "#'").

As a result, there are only a few cases when you can safely assume that
the top structure of a syntax object is a normal list or pair, and they
are spelled out in the documentation for 'syntax' in the R6RS Standard
Libraries specification:

  http://www.r6rs.org/final/html/r6rs-lib/r6rs-lib-Z-H-13.html#node_sec_12.4

Here's the relevant excerpt:

  The output produced by syntax is wrapped or unwrapped according to the
  following rules.

  * the copy of (<t1> . <t2>) is a pair if <t1> or <t2> contain any
    pattern variables,

  * the copy of (<t> <ellipsis>) is a list if <t> contains any pattern
    variables,

  * the copy of #(<t1> ... <tn>) is a vector if any of <t1>, ..., <tn>
    contain any pattern variables, and

  * the copy of any portion of <t> not containing any pattern variables
    is a wrapped syntax object.

A "wrapped syntax object" is one where the lexical environment
information has not yet been pushed down into the subexpressions.  It is
a special kind of object that you can only take apart using
'syntax-case'.

So, in the clause above where 'map' is used, 'e' might be a "wrapped
syntax object", but when the elements are extracted from it using the
'syntax-case' pattern (x ...) and then put back together using
#'(x ...), you can then assume that the resulting syntax object is
a normal Scheme list of syntax objects, and therefore it is safe to
use 'map' on it.

       Mark



  reply	other threads:[~2019-09-04 21:55 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-29 14:41 How to use-modules within macro? pelzflorian (Florian Pelz)
2019-08-29 23:04 ` Mark H Weaver
2019-08-30  6:47   ` pelzflorian (Florian Pelz)
2019-09-04 11:24   ` pelzflorian (Florian Pelz)
2019-09-04 19:54     ` Mark H Weaver
2019-09-04 21:55       ` Mark H Weaver [this message]
2019-09-04 22:58       ` pelzflorian (Florian Pelz)

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=87imq7afp2.fsf@netris.org \
    --to=mhw@netris.org \
    --cc=guile-user@gnu.org \
    --cc=pelzflorian@pelzflorian.de \
    /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).