guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Macro id-memv??, workings of tripple dot


From: Zelphir Kaltstahl
Subject: Re: Macro id-memv??, workings of tripple dot
Date: Fri, 15 Mar 2024 10:05:48 +0000

Hello Jean!

On 3/15/24 09:03, Jean Abou Samra wrote:
My question is: Do the ... in the case

((test id _kt _kf) _kt) ...

produce one case for each identifier in the list?

Yes, they do.


I am guessing that this is what they do. However, they are mentioned as 
literals in the inner syntax-rules,

No, they aren't. The (id ...) form is expanded by the outer syntax-rules
form, as part of expanding the id-memv?? macro. They don't remain in the
expanded result. For example, if you call

(id-memv?? foo (foo bar baz) kt kf)

the expansion will look like

(let-syntax ((test
               (syntax-rules (foo bar baz)
                 ((test foo _kt _kf) _kf)
                 ((test bar _kt _kf) _kf)
                 ((test baz _kt _kf) _kf)
                 ((test otherwise _kt _kf) _kf))))
   (test foo kt kf))
Aha! Of course! The inner syntax-rules is nothing special from the perspective of the outer syntax-rules! Somehow I saw it as something special and "evaluated from the inside out" in my head instead of from the "outside in", as I should have been doing. That makes a lot more sense now.
so I was thinking the expansion will simply put literally three dots there,
instead of understanding the three dots to mean "for each of the ids".

And also I still am unsure about whether the three dots work like this at all.

They do.


When one puts the ... after a compound expression, that contains the thing, that
the ... were after in the matching -- in this case they were after id, and id is
contained in the compound expression (test id _kt _kf) _kt) -- does that make
the compound expression be generated for each thing matched?

Yes, see:

(syntax->datum
  (with-syntax ((simple #'a)
                ((compound ...) #'(b c d))
                (((nested-compound ...) ...) #'((e f g) (h i j))))
    #'(((simple compound nested-compound) ...) ...)))

⇒ (((a b e) (a c f) (a d g)) ((a b h) (a c i) (a d j)))
That example is quite difficult to unpack for me, but I think I get it. Sort of. It is showing the reverse thing, not matching, but telling Guile what the match is. And then using that in an expression.
But if this is the case, then I might be misunderstanding the Guile docs at
https://www.gnu.org/software/guile/manual/html_node/Syntax-Rules.html:

"Instances of a pattern variable in the template must be followed by an 
ellipsis."
Note that this is talking about the patterns, not the syntax forms. But it
is slightly misleading: also in patterns it is perfectly possible to do
something like

(syntax->datum
  (with-syntax ((((a b) ...) #'((1 2) (3 4) (5 6))))
    #'((a ...) . (b ...))))

⇒ ((1 3 5) 2 4 6)
OK, this is easier to understand. It tells Guile what values A and B will be and then uses A and B in another way. But still quite cool, that this is possible. Not sure where to use it yet.
Note the pattern

((a b) ...)

An ellipsized pattern is recognized by the ellipsis, but it doesn't
need to follow a simple pattern variable, it can follow a nested
pattern.
What do you mean by "follow a nested pattern"?
Best,
Jean
As before, thanks for the revelations!
Zelphir

--
repositories:https://notabug.org/ZelphirKaltstahl


reply via email to

[Prev in Thread] Current Thread [Next in Thread]