[R6RS] modules

Matthew Flatt mflatt
Mon Aug 23 23:33:04 EDT 2004

At Mon, 23 Aug 2004 09:49:36 -0500 (EST), "R. Kent Dybvig" wrote:
> >   (module M (x get-x)
> >     (require M)
> >     (define x 5)
> >     (define (get-x) #'x))
> Is the (require M) above a mistake or does it do something special that
> is somehow germaine to your question?  I'll assume you meant
>   (module M (x get-x)
>     (define x 5)
>     (define (get-x) #'x))

Yes - the `(require M)' was a mistake.

> def-f-fun should fail because it produces a run-time reference to a
> variable---M's x---that is available only at expansion time.
> I don't see any reason why def-f-mac should fail.  M's x is used only
> for syntax, as required.

Then I'm still on track --- thanks for bearing with me. I'll take a
couple of steps this time, so that I can finally get to the point.

>  * Related to the above, should module expressions and commands be
>    evaluated once for each "level" of require/require for-syntax or
>    just once the first time needed?  (I prefer once; I'm not sure
>    about Matthew.)

My question is about what "just once the first time needed" means.

I've added a third module to the example:

  (module M (get-x)
    (define x 5)
    (define (get-x) #'x))

  (module N (def-f-fun def-f-mac)
    (require only-for-syntax M)
    (define-syntax def-f-mac 
      (lambda (stx) 
        (with-syntax ((x (get-x)))
          #'(define-syntax f (lambda (stx) x)))))
    (define-syntax def-f-fun
      (lambda (stx) 
        (with-syntax ((x (get-x)))
          #'(define f (lambda (stx) x))))))

  (module P (def-g-fun def-g-mac)
    (require M)
    (define-syntax def-g-mac 
      (lambda (stx) 
        (with-syntax ((x (get-x)))
          #'(define-syntax g (lambda (stx) x)))))
    (define-syntax def-g-fun
      (lambda (stx) 
        (with-syntax ((x (get-x)))
          #'(define g (lambda (stx) x))))))

As you can see, P is just like N, except that it imports M without
"only-for-syntax". As a result, I expect that P's `def-g-fun' works,
and P's `def-g-mac' doesn't.

How many times is the body of M evaluated? Is it just once, so that all
of the calls to `get-x' in N and P call the same function? If so, what
mechanism allows the result to be treated differently in N and P?

> Incidentally, I hope we agree that no one can use def-f-mac's product
> in any case, since f is an introduced identifier with no references.



More information about the R6RS mailing list