[plt-scheme] namespace-attach-module and sharing modules between evals

From: Danny Yoo (dyoo at hkn.eecs.berkeley.edu)
Date: Tue Jun 6 14:43:47 EDT 2006

> You are using the current module name resolver to turn "brick.ss"
> into a module name (symbol). I believe that in doing so you are
> triggering the loading of the module into the current namespace (ns),
> and so you aren't able to attach it from the original namespace
> (main-ns).
> Try moving the call to module-name->symbol outside of the
> parameterize expression.

Hi Ryan,

Thank you so much!  That was exactly it.  Good grief, that was subtle.

Here's the working example I have now:

(module brick mzscheme
   (define-struct brick (a b))
   (provide (struct brick (a b)))
   (printf "brick.ss is invoked~n"))

(module evaluation mzscheme

   (define (module-path->symbol module-path)
     ((current-module-name-resolver) module-path #f #f))

   (define (make-evaluation-namespace)
     (let ([ns (make-namespace)]
           [main-ns (current-namespace)]
           [path (module-path->symbol "brick.ss")])
       (parameterize ([current-namespace ns])
         (namespace-attach-module main-ns path)

   (provide evaluate)
   (define (evaluate module-stx)
     (syntax-case module-stx ()
       [(module module-name language body ...)
        (parameterize ([current-namespace (make-evaluation-namespace)])
          (eval-syntax module-stx)
          (eval-syntax #`(require module-name))
          (namespace-variable-value 'result))])))

(module test-evaluation mzscheme
   (require "evaluation.ss"
   (define mybrick
     (evaluate #'(module a mzscheme
                   (require "brick.ss")
                   (provide result)
                   (define result (make-brick 3 4)))))
   (printf "The brick I made is: (~a ~a)~n"
           (brick-a mybrick) (brick-b mybrick)))

I'll add this to the Schematics Cookbook later tonight; it seems like 
something that comes in handy.

Posted on the users mailing list.