Re: Using SRFI 212 to detect boundness of an identifier in syntax-rules Marc Nieper-Wißkirchen 22 Oct 2022 16:26 UTC

Neat!

At least under an R6RS-like where violations must not be ignored, you
can also use SRFI 213:

(define-syntax bound?
  (lambda (stx)
    (lambda (lookup)
      (syntax-case stx ()
        [(_ id)
         (identifier? #'id)
         (begin
           (guard (exc [(syntax-violation? exc) #'#f])
             (lookup #'id #'*)
             #'#t))]))))

Am Sa., 22. Okt. 2022 um 18:06 Uhr schrieb Daphne Preston-Kendal
<xxxxxx@nonceword.org>:
>
> A neat trick/edge case I just discovered:
>
> (define-syntax bound?
>   (syntax-rules ()
>     ((_ id)
>      (let-syntax
>          ((test
>            (syntax-rules (id)
>              ((_ id id) #t)
>              ((_ _ _) #f))))
>        (alias abracadabra id)
>        (test abracadabra id)))))
>
> > (bound? cons)
> #t
> > (bound? foo)
> #f
>
> This works in Gerbil (where it’s called `define-alias` by default) but not in Chez or Unsyntax for some reason (even adding an extra (let () …) to account for the fact that R6RS let-syntax is splicing) where it always returns #t, and not in Kawa where it always returns #f. Also, it always thinks the identifier ‘abracadabra’ is bound.
>
> Since this falls under ‘If identifier2 is unbound, it is an error’, I think this is not very useful. But I was still pretty pleased when I found it worked on Gerbil.
>
>
> Daphne
>