[racket] Again on bindings visibility in eval

From: Eli Barzilay (eli at barzilay.org)
Date: Thu Jul 14 15:33:37 EDT 2011

30 minutes ago, Markku Rontu wrote:
> Well one problem is that all these syntax objects have introduced a
> new language, with accessors and functions that are completely new
> that must be learnt. And programs using these new functions look
> quite different to standard data structure (i.e., cons cell)
> manipulation. [...]

Ah, but it's not a new language -- just a new type.  It's a good hook
to explain it in a different way -- it's as if you had this:

  (provide syntax? syntax-e syntax->datum)
  (struct syntax (e))
    ;; a wrapper for syntax values, holding a single value which is a
    ;; plain sexpr -- don't give out the `syntax' constructor.
  (define (syntax->datum stx)
    (unless (syntax? stx)
      (raise-type-error 'syntax->datum "syntax" stx))
    (let loop ([d stx])
      (cond [(syntax? d) (loop (syntax-e d))]
            [(pair? d)   (cons (loop (car d) (cdr d)))]
            [(vector? d) ...]
            [else d])))

and then extended it with some opaque lexical context field:

  (provide syntax? syntax-e syntax->datum datum->syntax)
  (struct syntax (e ctx))
  ;; but don't provide `syntax-ctx', instead have a function that
  ;; creates a syntax using the context of another:
  (define (datum->syntax c dtm)
    (syntax (syntax-ctx c) dtm))

and this goes on.

(This is a nice way to explain it, since many explanations take
`syntax-case' as the primitive and the rest is derived from it.)

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                    http://barzilay.org/                   Maze is Life!

Posted on the users mailing list.