unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Zelphir Kaltstahl <zelphirkaltstahl@posteo.de>
To: Maxime Devos <maximedevos@telenet.be>,
	Jean Abou Samra <jean@abou-samra.fr>
Cc: Guile User <guile-user@gnu.org>
Subject: Re: Understanding `symbol??` macro from okmij.org
Date: Thu, 14 Mar 2024 13:46:31 +0000	[thread overview]
Message-ID: <6b132982-1585-4fc3-b2b4-98ef84b6af42@posteo.de> (raw)
In-Reply-To: <20240314020307.yR322B00F319MNK01R36Mg@andre.telenet-ops.be>

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


  reply	other threads:[~2024-03-14 13:46 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

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=6b132982-1585-4fc3-b2b4-98ef84b6af42@posteo.de \
    --to=zelphirkaltstahl@posteo.de \
    --cc=guile-user@gnu.org \
    --cc=jean@abou-samra.fr \
    --cc=maximedevos@telenet.be \
    /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).