unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Macro id-memv??, workings of tripple dot
@ 2024-03-15  1:19 Zelphir Kaltstahl
  2024-03-15  8:03 ` Jean Abou Samra
  0 siblings, 1 reply; 3+ messages in thread
From: Zelphir Kaltstahl @ 2024-03-15  1:19 UTC (permalink / raw)
  To: Guile User

Hello Guile Users,

I have another macro understanding question and I think I don't yet grasp all 
the usages of the ellipsis/tripple dot/... :

Again it is a macro from https://okmij.org/ftp/Scheme/assert-syntax-rule.txt. 
This time the id-memv macro, which I think I mostly understand, but have 
questions about:

~~~~
;; A macro-expand-time memv function for identifiers
;; id-memv?? FORM (ID ...) KT KF
;; FORM is an arbitrary form or datum, ID is an identifier.
;; The macro expands into KT if FORM is an identifier, which occurs
;; in the list of identifiers supplied by the second argument.
;; All the identifiers in that list must be unique.
;; Otherwise, id-memv?? expands to KF.
;; Two identifiers match if both refer to the same binding occurrence, or
;; (both are undefined and have the same spelling).
(define-syntax id-memv??
   (syntax-rules ()
     ((id-memv?? form (id ...) kt kf)
      (let-syntax ((test
                    ;; Putting the ID matched in the outer syntax-rules
                    ;; form, where it is flexible to stand for any
                    ;; identifier, into the literals list makes it so,
                    ;; that whatever was matched in the outer
                    ;; syntax-rules is now literally matched against in
                    ;; the inner syntax-rules.
                    (syntax-rules (id ...)
                      ;; If indeed FORM is the same identifier as ID,
                      ;; then the result is the continuation for the
                      ;; true case.
                      ((test id _kt _kf) _kt) ...
                      ((test otherwise _kt _kf) _kf))))
        ;; Pass the form to be checked to test.
        (test form kt kf)))))
~~~~

My question is: Do the ... in the case

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

produce one case for each identifier in the list? I am guessing that this is 
what they do. However, they are mentioned as literals in the inner syntax-rules, 
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. 
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?

For example:

(id-memv?? c (a b c) #t #f)

would internally expand into the cases:

((test a _kt _kf) _kt) _kt)
((test b _kt _kf) _kt) _kt)
((test c _kt _kf) _kt) _kt)
((test otherwise _kt _kf) _kf)

?

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."

So maybe I am just trying to see things here.

Best regards,
Zelphir

repositories:https://notabug.org/ZelphirKaltstahl


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Macro id-memv??, workings of tripple dot
  2024-03-15  1:19 Macro id-memv??, workings of tripple dot Zelphir Kaltstahl
@ 2024-03-15  8:03 ` Jean Abou Samra
  2024-03-15 10:05   ` Zelphir Kaltstahl
  0 siblings, 1 reply; 3+ messages in thread
From: Jean Abou Samra @ 2024-03-15  8:03 UTC (permalink / raw)
  To: Zelphir Kaltstahl, Guile User

[-- Attachment #1: Type: text/plain, Size: 2398 bytes --]

> 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))


> 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)))


> 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)

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.

Best,
Jean


[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Macro id-memv??, workings of tripple dot
  2024-03-15  8:03 ` Jean Abou Samra
@ 2024-03-15 10:05   ` Zelphir Kaltstahl
  0 siblings, 0 replies; 3+ messages in thread
From: Zelphir Kaltstahl @ 2024-03-15 10:05 UTC (permalink / raw)
  To: Jean Abou Samra; +Cc: Guile User

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-03-15 10:05 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-03-15  1:19 Macro id-memv??, workings of tripple dot Zelphir Kaltstahl
2024-03-15  8:03 ` Jean Abou Samra
2024-03-15 10:05   ` Zelphir Kaltstahl

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).