unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Understanding `symbol??` macro from okmij.org
@ 2024-03-14  0:18 Zelphir Kaltstahl
  2024-03-14  1:01 ` Jean Abou Samra
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Zelphir Kaltstahl @ 2024-03-14  0:18 UTC (permalink / raw)
  To: Guile User

Hello Guile Users!

I am having trouble understanding how the `symbol??` macro from 
https://okmij.org/ftp/Scheme/assert-syntax-rule.txt works.

Here is what I have so far:

~~~~start~~~~
;; (symbol?? FORM KT KF)
;; FORM is an arbitrary form or datum.
;; A symbol? predicate at the macro-expand time.
;; Expands in KT if FORM is a symbol (identifier), Otherwise, expands in KF.
;; KT: Continuation/Case in case of true.
;; KF: Continuation/Case in case of false.
(define-syntax symbol??
   (syntax-rules ()
     ;; The check is done by first pattern matching against some other
     ;; forms, that are not a symbol.
     ((symbol?? (x . y) kt kf) kf) ; It's a pair, not a symbol
     ((symbol?? #(x ...) kt kf) kf) ; It's a vector, not a symbol
     ;; After those things are excluded, the thing might be a symbol.
     ((symbol?? maybe-symbol kt kf)
      (let-syntax ((test
                    (syntax-rules ()
                      ((test maybe-symbol t f) t)
                      ((test x t f) f))))
        (test abracadabra kt kf)))))
~~~~~end~~~~~

So I don't understand why `test` works or how it works to check, whether 
`maybe-symbol` is a symbol or not. And I don't understand how `abracadabra` 
helps with that, or why it helps, to pass something undefined.

Can someone help me understand, what is going on there?

Actually I am trying to understand the whole page, and I got there while looking 
for a good `assert` macro ... rabbit hole and all that.

Best regards,
Zelphir

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


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

* Re: Understanding `symbol??` macro from okmij.org
  2024-03-14  0:18 Understanding `symbol??` macro from okmij.org Zelphir Kaltstahl
@ 2024-03-14  1:01 ` Jean Abou Samra
  2024-03-14  1:03 ` Maxime Devos
  2024-03-14 14:52 ` Olivier Dion
  2 siblings, 0 replies; 8+ messages in thread
From: Jean Abou Samra @ 2024-03-14  1:01 UTC (permalink / raw)
  To: Zelphir Kaltstahl, Guile User

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

Hi!

The explanations are on

https://okmij.org/ftp/Scheme/macros.html#macro-symbol-p

Maybe this version will be easier to understand (we don't really
need continuation-passing style here):

(define-syntax symbol??
  (syntax-rules ()
    ((symbol?? maybe-symbol)
     (let-syntax
         ((test
           (syntax-rules ()
             ((test maybe-symbol) #t)
             ((test _) #f))))
       (test abracadabra)))))

(symbol?? foo) ⇒ #t
(symbol?? (a . b)) ⇒ #f
(symbol?? 5) ⇒ #f
(symbol?? "a") ⇒ #f
(symbol?? #(1 a)) ⇒ #f


Basically: the macro call

(symbol?? <foobar>)

expands to a macro definition of test as

(syntax-rules ()
  ((test <foobar>) #t)
  ((test _) #f))

and a call (test abracadabra). Now, observe that if <foobar>
is a symbol, then it's a catch-all pattern when inserted
in the syntax-rules definition of `test`, so it will match
abracadabra (because it matches anything). On the other hand,
if it's not a symbol, then it won't match abracadabra, by case
analysis: if it's a number it will only match that number; booleans,
strings and characters likewise; if it's a pair it can only match
pairs; if it's a vector it can only match vectors; etc.

I'm not exactly sure why Oleg Kiselyov included special cases for a pair
and a vector, but my guess is that not all Scheme implementations
support vectors in syntax-rules patterns (and the pair check is
necessary because the car or cdr could contain a vector). The Scheme
standards certainly have their opinion on this, and I knew that
stuff by heart at some point (when I implemented a syntax-rules/syntax-case
expander for a university project), but I don't remember, and it's
too late for scouring the standards...

Best,
Jean


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

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

* RE: Understanding `symbol??` macro from okmij.org
  2024-03-14  0:18 Understanding `symbol??` macro from okmij.org Zelphir Kaltstahl
  2024-03-14  1:01 ` Jean Abou Samra
@ 2024-03-14  1:03 ` Maxime Devos
  2024-03-14 13:46   ` Zelphir Kaltstahl
  2024-03-14 14:52 ` Olivier Dion
  2 siblings, 1 reply; 8+ messages in thread
From: Maxime Devos @ 2024-03-14  1:03 UTC (permalink / raw)
  To: Zelphir Kaltstahl, Guile User

(define-syntax symbol??
   (syntax-rules ()
     ;; The check is done by first pattern matching against some other
     ;; forms, that are not a symbol.
     ((symbol?? (x . y) kt kf) kf) ; It's a pair, not a symbol
     ((symbol?? #(x ...) kt kf) kf) ; It's a vector, not a symbol

The first two cases don’t need any explanation I think.

     ;; After those things are excluded, the thing might be a symbol.
     ((symbol?? maybe-symbol kt kf)
      (let-syntax ((test
                    (syntax-rules ()
                      ((test maybe-symbol t f) t)
                      ((test x t f) f))))
        (test abracadabra kt kf)))))

If maybe-symbol is a symbol, then (test abracadabra kt kf) matches (test maybe-symbol t f) (let t=kt, f=kf, maybe-symbol=abracadabra).

If maybe-symbol is not a symbol, for example it is a string “hello”  (please ignore wrong quoting), then
(test maybe-symbol t f) becomes (test “hello” t f).  The string “hello” cannot act as an identifier (because it is a string, not a symbol), so (test abracadabra kt kf) does not match the first case (test maybe-symbol t f). Only the second case (test x t f) remains, and (test abracadabra kt kf) matches this (set x to abracadabra, t to kt, f to kf).

I think the first two cases are superfluous, but perhaps there is a performance advantage.

(I’m wondering if this still works in the case (symbol? ...), because ... is special in syntax-rules)

Best regards,
Maxime Devos.




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

* Re: Understanding `symbol??` macro from okmij.org
  2024-03-14  1:03 ` Maxime Devos
@ 2024-03-14 13:46   ` Zelphir Kaltstahl
  0 siblings, 0 replies; 8+ messages in thread
From: Zelphir Kaltstahl @ 2024-03-14 13:46 UTC (permalink / raw)
  To: Maxime Devos, Jean Abou Samra; +Cc: Guile User

On 3/14/24 02:01, Jean Abou Samra wrote:
> Hi!
>
> The explanations are on
>
> https://okmij.org/ftp/Scheme/macros.html#macro-symbol-p
>
> Maybe this version will be easier to understand (we don't really
> need continuation-passing style here):
>
> (define-syntax symbol??
>    (syntax-rules ()
>      ((symbol?? maybe-symbol)
>       (let-syntax
>           ((test
>             (syntax-rules ()
>               ((test maybe-symbol) #t)
>               ((test _) #f))))
>         (test abracadabra)))))
>
> (symbol?? foo) ⇒ #t
> (symbol?? (a . b)) ⇒ #f
> (symbol?? 5) ⇒ #f
> (symbol?? "a") ⇒ #f
> (symbol?? #(1 a)) ⇒ #f
>
>
> Basically: the macro call
>
> (symbol?? <foobar>)
>
> expands to a macro definition of test as
>
> (syntax-rules ()
>    ((test <foobar>) #t)
>    ((test _) #f))
>
> and a call (test abracadabra). Now, observe that if <foobar>
> is a symbol, then it's a catch-all pattern when inserted
> in the syntax-rules definition of `test`, so it will match
> abracadabra (because it matches anything). On the other hand,
> if it's not a symbol, then it won't match abracadabra, by case
> analysis: if it's a number it will only match that number; booleans,
> strings and characters likewise; if it's a pair it can only match
> pairs; if it's a vector it can only match vectors; etc.
>
> I'm not exactly sure why Oleg Kiselyov included special cases for a pair
> and a vector, but my guess is that not all Scheme implementations
> support vectors in syntax-rules patterns (and the pair check is
> necessary because the car or cdr could contain a vector). The Scheme
> standards certainly have their opinion on this, and I knew that
> stuff by heart at some point (when I implemented a syntax-rules/syntax-case
> expander for a university project), but I don't remember, and it's
> too late for scouring the standards...
>
> Best,
> Jean


On 3/14/24 02:03, Maxime Devos wrote:
>
> (define-syntax symbol??
>
>    (syntax-rules ()
>
>      ;; The check is done by first pattern matching against some other
>
>      ;; forms, that are not a symbol.
>
>      ((symbol?? (x . y) kt kf) kf) ; It's a pair, not a symbol
>
>      ((symbol?? #(x ...) kt kf) kf) ; It's a vector, not a symbol
>
> The first two cases don’t need any explanation I think.
>
>      ;; After those things are excluded, the thing might be a symbol.
>
>      ((symbol?? maybe-symbol kt kf)
>
>       (let-syntax ((test
>
>                     (syntax-rules ()
>
>                       ((test maybe-symbol t f) t)
>
>                       ((test x t f) f))))
>
>         (test abracadabra kt kf)))))
>
> If maybe-symbol is a symbol, then (test abracadabra kt kf) matches (test 
> maybe-symbol t f) (let t=kt, f=kf, maybe-symbol=abracadabra).
>
> If maybe-symbol is not a symbol, for example it is a string “hello”  (please 
> ignore wrong quoting), then
>
> (test maybe-symbol t f) becomes (test “hello” t f).  The string “hello” cannot 
> act as an identifier (because it is a string, not a symbol), so (test 
> abracadabra kt kf) does not match the first case (test maybe-symbol t f). Only 
> the second case (test x t f) remains, and (test abracadabra kt kf) matches 
> this (set x to abracadabra, t to kt, f to kf).
>
> I think the first two cases are superfluous, but perhaps there is a 
> performance advantage.
>
> (I’m wondering if this still works in the case (symbol? ...), because ... is 
> special in syntax-rules)
>
> Best regards,
>
> Maxime Devos.
>
Thank you both!

Mind-blowing stuff. But I think I understand it now. How clever.

Best regards,
Zelphir

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


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

* Re: Understanding `symbol??` macro from okmij.org
  2024-03-14  0:18 Understanding `symbol??` macro from okmij.org Zelphir Kaltstahl
  2024-03-14  1:01 ` Jean Abou Samra
  2024-03-14  1:03 ` Maxime Devos
@ 2024-03-14 14:52 ` Olivier Dion
  2024-03-14 15:57   ` Jean Abou Samra
  2 siblings, 1 reply; 8+ messages in thread
From: Olivier Dion @ 2024-03-14 14:52 UTC (permalink / raw)
  To: Zelphir Kaltstahl, Guile User


Excuse my ignorance, by why is symbol?? not written using syntax-cases
and the `indentifier?' guard?

On Thu, 14 Mar 2024, Zelphir Kaltstahl <zelphirkaltstahl@posteo.de> wrote:
> Hello Guile Users!
>
> I am having trouble understanding how the `symbol??` macro from 
> https://okmij.org/ftp/Scheme/assert-syntax-rule.txt works.
>
> Here is what I have so far:
>
> ~~~~start~~~~
> ;; (symbol?? FORM KT KF)
> ;; FORM is an arbitrary form or datum.
> ;; A symbol? predicate at the macro-expand time.
> ;; Expands in KT if FORM is a symbol (identifier), Otherwise, expands in KF.
> ;; KT: Continuation/Case in case of true.
> ;; KF: Continuation/Case in case of false.
> (define-syntax symbol??
>    (syntax-rules ()
>      ;; The check is done by first pattern matching against some other
>      ;; forms, that are not a symbol.
>      ((symbol?? (x . y) kt kf) kf) ; It's a pair, not a symbol
>      ((symbol?? #(x ...) kt kf) kf) ; It's a vector, not a symbol
>      ;; After those things are excluded, the thing might be a symbol.
>      ((symbol?? maybe-symbol kt kf)
>       (let-syntax ((test
>                     (syntax-rules ()
>                       ((test maybe-symbol t f) t)
>                       ((test x t f) f))))
>         (test abracadabra kt kf)))))
> ~~~~~end~~~~~
>
> So I don't understand why `test` works or how it works to check, whether 
> `maybe-symbol` is a symbol or not. And I don't understand how `abracadabra` 
> helps with that, or why it helps, to pass something undefined.
>
> Can someone help me understand, what is going on there?
>
> Actually I am trying to understand the whole page, and I got there while looking 
> for a good `assert` macro ... rabbit hole and all that.
>
> Best regards,
> Zelphir
>
> -- 
> repositories:https://notabug.org/ZelphirKaltstahl
-- 
Olivier Dion
oldiob.ca



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

* Re: Understanding `symbol??` macro from okmij.org
  2024-03-14 14:52 ` Olivier Dion
@ 2024-03-14 15:57   ` Jean Abou Samra
  2024-03-14 16:39     ` Olivier Dion
  0 siblings, 1 reply; 8+ messages in thread
From: Jean Abou Samra @ 2024-03-14 15:57 UTC (permalink / raw)
  To: Olivier Dion; +Cc: Zelphir Kaltstahl, Guile User


> Excuse my ignorance, by why is symbol?? not written using syntax-cases
> and the `indentifier?' guard?

a) Because it's intellectually interesting to see what the power of syntax-rules is :)
b) Not all Scheme standards and implementations contain syntax-case.





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

* Re: Understanding `symbol??` macro from okmij.org
  2024-03-14 15:57   ` Jean Abou Samra
@ 2024-03-14 16:39     ` Olivier Dion
  2024-03-14 18:01       ` Jean Abou Samra
  0 siblings, 1 reply; 8+ messages in thread
From: Olivier Dion @ 2024-03-14 16:39 UTC (permalink / raw)
  To: Jean Abou Samra; +Cc: Zelphir Kaltstahl, Guile User

On Thu, 14 Mar 2024, Jean Abou Samra <jean@abou-samra.fr> wrote:
>> Excuse my ignorance, by why is symbol?? not written using syntax-cases
>> and the `indentifier?' guard?
>
> a) Because it's intellectually interesting to see what the power of syntax-rules is :)
> b) Not all Scheme standards and implementations contain syntax-case.

Ah, and I though that syntax-rules was always written in term of
syntax-case.  I see.  Thank you!

-- 
Olivier Dion
oldiob.ca



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

* Re: Understanding `symbol??` macro from okmij.org
  2024-03-14 16:39     ` Olivier Dion
@ 2024-03-14 18:01       ` Jean Abou Samra
  0 siblings, 0 replies; 8+ messages in thread
From: Jean Abou Samra @ 2024-03-14 18:01 UTC (permalink / raw)
  To: Olivier Dion; +Cc: Zelphir Kaltstahl, Guile User


> Ah, and I though that syntax-rules was always written in term of
> syntax-case.  I see.  Thank you!


Yeah, it most most often is these days, but historically, syntax-rules was invented before syntax-case. IIRC, the choice to include syntax-case in R6RS was controversial because syntax-rules was expressed in terms of different low-level abstractions in some Scheme implementations ("explicit substitutions" or something like that).



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

end of thread, other threads:[~2024-03-14 18:01 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-14  0:18 Understanding `symbol??` macro from okmij.org Zelphir Kaltstahl
2024-03-14  1:01 ` Jean Abou Samra
2024-03-14  1:03 ` Maxime Devos
2024-03-14 13:46   ` Zelphir Kaltstahl
2024-03-14 14:52 ` Olivier Dion
2024-03-14 15:57   ` Jean Abou Samra
2024-03-14 16:39     ` Olivier Dion
2024-03-14 18:01       ` Jean Abou Samra

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