[plt-scheme] Some syntax issues

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Thu Aug 22 22:24:43 EDT 2002

At Sun, 18 Aug 2002 00:49:05 +0300, Lauri Alanko wrote:
> Here's one idea. We need a new construct: a traditional nested scoping
> block akin to C++'s namespaces or ML's modules. It is in essence an
> inner module declaration in which the definitions outside it are also
> visible (as opposed to current PLT modules which do not see top-level
> bindings). Nested modules would be useful for other things too, but here
> we need just a special kind: an _anonymous_ one.

Sounds like you're talking about Chez's `module' construct. I agree
that it would be useful.

> In addition, we need set-syntax!. This shouldn't be a very big deal,
> since syntactic bindings are already mutable, as demonstrated by
> fluid-let-syntax.

`set-syntax!' is a big deal, actually --- not difficult to implement,
but it has nasty consequences for partial expansion.

FWIW, `fluid-let-syntax' is not implemented by mutating bindings. (For
one thing, it would create an unmanagable mess when using threads.)

> So now we can define a macro that refers to a unique variable simply as:
> 
> (define-syntax my-macro #f)
> (anonymous-inner-module
>    (define my-variable (whatever))
>    (set-syntax! my-macro (lambda (stx) #'(do-something-with my-variable))))
> 
> Since each anonymous-inner-module has its own scope, it's no big deal
> that we use the same identifier for my-variable multiple times.

This is a good idea. No need for `set-syntax!', though. If we're going
to add a new form, we might as well make it declarative, something like
this:

 (import-from-anonymous-inner-module ; a new definition form
        (my-macro) ; bindings to be imported
   (define my-variable (whatever))
   (define-syntax my-macro (lambda (stx) #'(do-something-with my-variable))))

My concern is that the programmer is still taking responsibility for
hiding names, and this sort of hiding is usually done automatically.

For example, suppose I define

  (define-syntax (define-box stx)
    (syntax-case stx ()
       [(_ name) #'(begin
                    (define hidden 5)
                    (define name
                      (case-lambda
                        [() hidden]
                        [(v) (set! hidden v)])))]))

If I use `define-box' in an internal-definition position, then `hidden'
really is hidden.

I'd like to change the macro system so that `hidden' is hidden when
`define-box' is used in a top-level position, too. I'm not sure of
certain details, though.

Matthew




Posted on the users mailing list.