Skip to content

Instantly share code, notes, and snippets.

@leque
Last active October 6, 2020 09:06
Show Gist options
  • Save leque/cbb5ecdbc972e92390eb743c4a4dc9e6 to your computer and use it in GitHub Desktop.
Save leque/cbb5ecdbc972e92390eb743c4a4dc9e6 to your computer and use it in GitHub Desktop.
#!r6rs
(library (binding)
(export bound?)
(import
(for (only (rnrs base) eq? list not quote lambda eqv? unquote quasiquote) expand)
(for (only (rnrs base) define-syntax) run)
(for (only (rnrs lists) exists) expand)
(for (only (rnrs syntax-case)
quasisyntax unsyntax
syntax-case syntax free-identifier=? syntax->datum datum->syntax)
expand)
(for (only (rnrs eval) eval environment) expand))
(define-syntax bound?
(lambda(ctx)
(syntax-case ctx ()
((_ id)
(exists (lambda(x)(free-identifier=? #'id x))
(list #'syntax #'quote #'syntax->datum #'datum->syntax))
;; id: bound as syntax, quote, syntax->datum, datum->syntax, or aliases to them
#t)
((_ id)
(free-identifier=?
(eval `(datum->syntax #'k ',(syntax->datum #'id))
(environment '(only (rnrs)
datum->syntax
syntax->datum
syntax
quote
)))
#'id)
;; id: unbound (except for datum->syntax, syntax, syntax, and quote)
#f)
((_ id)
(exists (lambda (x) (eq? (syntax->datum #'id) x))
'(datum->syntax syntax->datum syntax quote))
;; id: unbound datum->syntax, syntax->datum, syntax, or quote
#f)
((_ id)
;; otherwise, id should be bound
#t)
)))
)
#!r6rs
(import (except (rnrs) syntax datum->syntax)
(rename (only (rnrs) syntax)
(syntax stx))
(binding))
(define x 0)
(define-syntax test-bound?
(syntax-rules ()
((_ ids ...)
(begin
(begin (display `(bound? ,'ids))
(display "\t;; => ")
(display (bound? ids))
(newline))
...))))
(test-bound? stx syntax define quote datum->syntax t x)
;; output of chez --program m.scm
(bound? stx) ;; => #t
(bound? syntax) ;; => #f
(bound? define) ;; => #t
(bound? quote) ;; => #t
(bound? datum->syntax) ;; => #f
(bound? t) ;; => #f
(bound? x) ;; => #t
;; output of plt-r6rs m.scm
;; setup: plt-r6rs --install binding.scm
{bound? stx} ;; => #t
{bound? syntax} ;; => #t
{bound? define} ;; => #t
{bound? quote} ;; => #f
{bound? datum->syntax} ;; => #t
{bound? t} ;; => #f
{bound? x} ;; => #t
;; output of sagittarius -L. m.scm
(bound? stx) ;; => #t
(bound? syntax) ;; => #t
(bound? define) ;; => #t
(bound? quote) ;; => #t
(bound? datum->syntax) ;; => #f
(bound? t) ;; => #f
(bound? x) ;; => #t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment