[plt-scheme] Evaluation Across Modules (or Namespaces?)

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Sun Jul 16 20:06:09 EDT 2006

At Sun, 16 Jul 2006 11:49:22 -0700, "Williams, M. Douglas" wrote:
> >> And get this when I run it:
> >> 
> >> (app(t1 2) = 4
> >> (my-f2 2) = 4
> >> . . a.ss::436: reference to undefined identifier: my-f2
> >seems to only work for identifiers (like *) in the mzscheme
> >> namespace.
> >
> >I don't think I understand your whole problem, but you might make
> >progress by using syntax objects instead of plain symbols and pairs:
> >
> >(module a mzscheme
> >  
> >  (provide (all-defined))
> >  
> >  (define-struct s (args body f))
> >  
> >  (define-syntax def
> >    (syntax-rules ()
> >      ((d name args body ...)
> >       (define name (make-s #'args
> >                            #'(body ...)
> >                            #f)))))
> >  
> >  (define (act an-s)
> >    (set-s-f! an-s
> >              (eval #`(lambda #,(s-args an-s)
> >                       #,@(s-body an-s)))))
> >  
> >  (define (app an-s . args)
> >    (apply (s-f an-s) args))
> >  
> >  (define (my-f3 x) (* x x))
> >  
> >  )
> >
> >
> >Matthew
> 
> Why does it also work when the args are not syntax-quoted?

In your example, the arguments have enclosing other binding below the
level of the module-introduced binding, so it appears to work.

Try

  (define (my-f2 x) (* x x))
  (let ([x 13])
    (def t2 (x) (my-f2 x))
    (act t2)
    (printf "(my-f2 2) = ~a~n" (my-f2 2))
    (printf "(app(t2 2) = ~a~n" (app t2 2)))

In this case, the quoted `x' gets the module top-level context, whereas
the body `x' gets a context inside the `let'.

Similarly, this fails:

  (define (my-f2 x) (* x x))
  (def t2 (an-s) (my-f2 an-s))
  (act t2)
  (printf "(my-f2 2) = ~a~n" (my-f2 2))
  (printf "(app(t2 2) = ~a~n" (app t2 2))

because the quoted 'an-s' gets the context from where it is converted
to a syntax object by #', which is inside the macro definition.

Matthew



Posted on the users mailing list.