[plt-scheme] Modules, structs and environments

From: Jacob Matthews (jacobm at cs.uchicago.edu)
Date: Mon Nov 19 12:16:24 EST 2007

I agree with Chongkai and with Noel. As an example of what Noel is
saying, you can use a customized namespace to solve your particular
problem like this:

(module run mzscheme
  (require "struct.ss")

  (define test (make-test-struct
                'apple
                'banana))

  (display test)

  (define ns (make-namespace))             ; make a namespace with the
basic set of bindings
  (parameterize ([current-namespace ns])   ; make eval look up names in ns
    (namespace-require "struct.ss")        ; put the names from
"struct.ss" into ns
    (read-eval-print-loop)))

-jacob

On Nov 19, 2007 10:15 AM, Joel J. Adamson <jadamson at partners.org> wrote:
> The following message is a courtesy copy of an article
> that has been posted to comp.lang.scheme as well.
>
> Howdy Schemers,
>
> There's something I'm not getting about require'ing modules,
> environments and how to export struct definitions.  I'm using PLT Scheme
> (MzScheme only) 371.  When I import a struct and then start a new REPL,
> I get undefined identifier from within the REPL for make-`struct'.
>
> Here's some test code:
>
> ;;;;;;;;;;;;;;;;;;;;
> ;; struct.ss
> ;; this is a library that creates the struct and exports
> ;; it.
> (module struct mzscheme
>   (provide (struct test-struct (f1 f2)))
>   (define-struct test-struct (f1 f2)))
>
> ;; run.ss
> ;; This is "main"
> ;; I compile this with `mzc -v --exe run run.ss' and then invoke it from
> ;; the command-line
> (module run mzscheme
>   (require "struct.ss")
>
>   (define test (make-test-struct
>                 'apple
>                 'banana))
>
>   (display test)
>
>   (read-eval-print-loop))
> ;;;;;;;;;;;;;;;;;;;;
>
> This prints "<struct:test-struct>" (i.e., it works) and then enters the
> REPL.  If I `make-test-struct' in the REPL I get "undefined identifier:
> make-test-struct"
>
> The obvious explanation is that the REPL initializes the namespace and
> therefore the struct is not imported into the new namespace.  In that
> case, should I pass the environment to the REPL, and how would I do
> that?  I started defining my own `eval', which seems like either playing
> with fire or a sign of a novice ;)
>
> Also I read in a draft of R6RS that run-time identifiers must be
> imported with some qualification or flag --- do I need to do that in
> mzscheme?  If so, how?  All I've found is require-for-syntax and
> require-for-template: is there a require-for-run?
>
> The overall rationale: my real program is to take an input file
> (currently composed of scheme code) and evaluate it, and print the
> results to an output file or STDOUT.  After I configure the input and
> output ports from command line options, starting an REPL seemed to be
> the easiest way to do this.  If there's a better way, please let me
> know.  I will add custom printers and all that, but that is pretty far
> down the road.
>
> Thanks,
> Joel
> --
> Joel J. Adamson
> Biostatistician
> Pediatric Psychopharmacology Research Unit
> Massachusetts General Hospital
> Boston, MA  02114
> (617) 643-1432
> (303) 880-3109
>
> The information transmitted in this electronic communication is intended only
> for the person or entity to whom it is addressed and may contain confidential
> and/or privileged material. Any review, retransmission, dissemination or other
> use of or taking of any action in reliance upon this information by persons or
> entities other than the intended recipient is prohibited. If you received this
> information in error, please contact the Compliance HelpLine at 800-856-1983 and
> properly dispose of this information.
>
>
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>


Posted on the users mailing list.