Help: Module system in scheme48/scsh 
Author Message
 Help: Module system in scheme48/scsh

I'm trying to use the module system in scsh (which is a variant of
scheme48). I have read Jonathan Rees article "Another Module System
for Scheme", but I can't get simple examples to work. For instance
(taken almost verbatim from Rees' article):

  501 sandra $ scsh
  Scsh 0.5
  > ,config (define-structure foo (export)
               (open module-system built-in-structures more-structures))
  > ,in foo
  foo> (define-structure x (export a b)
         (open scheme)
         (begin (define a 17)))

  Error: undefined variable
         .make-reflective-tower.
         (package foo)
  1 foo>

I don't know if I'm doing something wrong, or if the module system is
somehow broken in scsh. A small working example would be appreciated.

/Niels



Tue, 13 Feb 2001 03:00:00 GMT  
 Help: Module system in scheme48/scsh

Quote:

> I'm trying to use the module system in scsh (which is a variant of
> scheme48). I have read Jonathan Rees article "Another Module System
> for Scheme", but I can't get simple examples to work. For instance
> (taken almost verbatim from Rees' article):

>   501 sandra $ scsh
>   Scsh 0.5
>   > ,config (define-structure foo (export)
>           (open module-system built-in-structures more-structures))
>   > ,in foo
>   foo> (define-structure x (export a b)

Shouldn't it be

foo> ,config (define-structure x (export a b)

here?

Quote:
>     (open scheme)
>     (begin (define a 17)))

>   Error: undefined variable
>     .make-reflective-tower.
>     (package foo)
>   1 foo>

In this way this works in scheme48:

Quote:
> ,config (define-structure foo (export)

               (open module-system built-in-structures more-structures))
Quote:
> ,in foo

foo> ,config (define-structure x (export a b)
         (open scheme)
         (begin (define a 17)))
foo> ,open x
Load structure x (y/n)? y
[x
Warning: Structure has undefined exports
         #{Structure 143 x}
         (b)
]
foo> a
17
foo>

-----------
Dmitrii.



Tue, 13 Feb 2001 03:00:00 GMT  
 Help: Module system in scheme48/scsh

Quote:

> I'm trying to use the module system in scsh (which is a variant of
> scheme48). I have read Jonathan Rees article "Another Module System
> for Scheme", but I can't get simple examples to work. For instance
> (taken almost verbatim from Rees' article):

>   501 sandra $ scsh
>   Scsh 0.5
>   > ,config (define-structure foo (export)
>           (open module-system built-in-structures more-structures))
>   > ,in foo
>   foo> (define-structure x (export a b)
>     (open scheme)
>     (begin (define a 17)))

>   Error: undefined variable
>     .make-reflective-tower.
>     (package foo)
>   1 foo>

> I don't know if I'm doing something wrong, or if the module system is
> somehow broken in scsh. A small working example would be appreciated.

> /Niels

I think your problem is that you are typing forms in the configuration
language ((define-structure ...)) at the scheme interpreter, which
doesn't understand that language.  You need to do something like

,config (define-structure ... )

whenever you define a structure.  My understanding is that structures
are "top-level"; that is, there's only onle place to define them.  So
using ",in" to get to another package and trying to define a structure
"in" that package isn't going to work.

If you look at the scheme48 scheme sources, in particular at files
such as interfaces.scm and packages.scm, you will find some good
examples.  One thing to notice is that the code of the package is
really never included in the definition of the structure.  Instead, a
"files" directive is used to indicate the location of the code.  It's
a little strange at first, but pretty fun once you get the hang of
it.  

--

joe



Tue, 13 Feb 2001 03:00:00 GMT  
 Help: Module system in scheme48/scsh

Quote:

> >   foo> (define-structure x (export a b)
> >        (open scheme)
> >        (begin (define a 17)))

> >   Error: undefined variable
> >        .make-reflective-tower.
> >        (package foo)
> >   1 foo>
> I think your problem is that you are typing forms in the configuration
> language ((define-structure ...)) at the scheme interpreter, which
> doesn't understand that language.  You need to do something like

> ,config (define-structure ... )

> whenever you define a structure.  My understanding is that structures
> are "top-level"; that is, there's only onle place to define them.  So
> using ",in" to get to another package and trying to define a structure
> "in" that package isn't going to work.

I was thinking that the config package was no different from any other
package, except that the interactive environment uses it for looking
up module names in some cases (yes, my understanding is still rather
vague...).

So that it should be posible to do (define-structure ...) in other
packages than the configuration package. One may have to first open
module-system in that package, to get access to the define-structure
syntax.

Is that incorrect?

/Niels



Wed, 14 Feb 2001 03:00:00 GMT  
 Help: Module system in scheme48/scsh
Hi,

Here is a reprint of an article Olin Shivers posted about the module
system. I'm sure you will find it useful...


Newsgroups: comp.lang.scheme.scsh
Subject: Scheme 48 module system
Date: 09 May 1998 23:08:27 -0400
Organization: Artificial Intelligence Lab, MIT
Lines: 149

NNTP-Posting-Host: lambda.ai.mit.edu


        Tue, 21 Apr 1998 19:46:37 -0700 (PDT))
X-Newsreader: Gnus v5.3/Emacs 19.34
Xref: news.northwest.com comp.lang.scheme.scsh:47


   The define structure part I understand, I figured that out pretty
   early on.  What's got me stuck is how you actually use the structure
   you've created.  I keep wanting to do something like (yes you can tell
   I'm a python guy) :

     (import structure)

     (use things which were exported from structure, maybe with some type
   of qualifier...)

   So after some muddling around I ended up with :

     #! /usr/local/bin/scsh \
     -lm module-def.scm -o module -s
     !#

     (code which uses things in module... )

Brian, you are on the right track here. I'll describe things in more detail
below.

   So other than setting up the script in this manner I really don't
   understand how you "import" things from the structures which you've
   defined.  I have the paper which talks all about the module system and
   it still it eludes me.

   I'm thinking that maybe the "code which uses..." part should actually
   be in the define-structure, but then how would I run it ?  Just
   loading the module definition doesn't work, e.g.

     scsh -s module-def.scm

   results in :

     Error: undefined variable
            module
            (package user)

That's right -- it doesn't work. Because module-def.scm doesn't contain Scheme.
It contains expressions from the module-definition language -- expressions
of the form

    (define-structure int-funs (exports factorial ack fib)
      (open scheme)
      (begin (define (factorial n) ...)
             (define (ack n) ...)
             (define (fib n) ...)))

If you look in the R4RS spec, you won't see the DEFINE-STRUCTURE form
defined -- that's what I mean by "it's not Scheme."

The problem is that if you just say
    scsh -s module-def.scm
then scsh is going to load module-def.scm into the user package. Well,
the user package opens (or "imports", if you like) bindings for all the
standard Scheme things (variables like CAR, DISPLAY, and syntax keywords
like LAMBDA, QUOTE, DEFINE), plus all the scsh stuff like FORK and EXEC.
But the user package *doesn't* have any bindings for DEFINE-STRUCTURE.
So you'll lose -- the (define-structure int-funs ...) expression won't
make sense.

However, there's another package, the config package. This package is
very different -- it *doesn't* have bindings for CAR, CDR, APPEND, LAMBDA,
etc., but it *does* have DEFINE-STRUCTURE available. You *can* load an
expression written in the module language into this package. That's what
    scsh -lm module-defs.scm
does. It's also what happens if you say
    ,config ,load module-defs.scm
at the scsh prompt. If module-defs.scm contains the structure
definition I sketched out above, then after you do this, your scsh has
a new package available. This package is bound to the variable int-funs
in the config package. Here, let me say that again. In the user package,
you still just have CAR, CDR, APPEND, LAMBDA, COND and friends. In the
config package, you have the keyword DEFINE-STRUCTURE, plus a new
variable binding -- the variable INT-FUNS is bound to your new package.
The config package is a package that contains other packages. For example,
it contains a variable PP, which is bound to the pretty-printer package,
which itself exports the variable P, which is the pretty-printing procedure.
In the config package, you will also find packages like SCSH and SCHEME.

OK, so far so good. We have our new package loaded. Notice that our
package definition
    (define-structure int-funs (exports factorial ack fib)
      (open scheme)
      (begin (define (factorial n) ...)
             (define (ack n) ...)
             (define (fib n) ...)))
opened the SCHEME package. What does this mean? Well, it means that
the package system looks in the config package for a variable named
SCHEME. This guy's value is itself a package -- the one defining and
exporting all the standard Scheme bindings. The package system grabs
all these bindings and makes them available to int-funs -- so the
forms in int-fun's (BEGIN ...) clause get to use things like LAMBDA,
IF, +, etc. If int-fun had contained a
    (FILES foo.scm bar.scm)
clause, the source in these files would have been able to reference these
Scheme bindings, too.

Now that we have our int-fun package defined, we'd like to use the functions
it defines. There are a couple of ways we could do this.
- From the scsh prompt, we could say
    ,in int-funs
  This would "move" the scsh interpreter into the package int-funs
  (which it would find by looking in the config package, where all the
  packages are defined). Now we can see all the stuff in this package.
  Not just the three bindings we exported (factorial, ack & fib), but
  all the unexported internals, as well. We can redefine stuff, try
  things out, and so forth, all in our package.

- From the scsh prompt, we could say
    ,open int-funs
  This would import int-funs' exported bindings (factorial, ack & fib)
  into our current package (which is probably the user package). Now we
  can reference these three bindings from the user package.

- If we are doing a script, we could say something like this:

    #!/usr/local/bin/scsh \
    -lm foo-module.scm -o int-funs -s
    !#

    (display (factorial (string->number (argv 1))))
    (write-char #\newline)

 The command line says: (1) load foo-module.scm into the config package
 (so it better contain source written in the module language), (2) open
 the package int-funs in the current package (which is by default
 the user package, though we could have changed it with the -m switch),
 (3) load our script and then exit. Voila -- your script got access to your
 little package of integer functions.

I think S48's module system is an incredibly cool mechanism. It's more
than just allowing you to define modules of Scheme code. You get to
define complete sets of language bindings, including all the syntax
keywords like LAMBDA and IF and so on. If your package opens the SCHEME
package, your package can be defined in terms of Scheme; no sweat. But
if you *don't* open the SCHEME package, you don't get any of it! You can
instead open up some other *completely different* set of keywords and base
function bindings.

Can't do that in C.
    -Olin

P.S. It's an undocumented feature of S48's module system, but you aren't
even restricted to Scheme's s-expression syntax when you use Kelsey and
Rees' module system. There's an extra clause in the DEFINE-STRUCTURE
form that lets you specify the parser function used to parse the files
named in the FILES clause. Now *that's* a general module system!

--

Brian Denheyer



Thu, 15 Feb 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. scheme48 module system

2. accessing queue-front (scheme48/scsh)

3. Systems programming in scsh

4. Help Linking Assembly Modules with C Modules

5. Scheme48 and Call-backs

6. Scheme48

7. Accessing environments in scheme48

8. SLIB and scheme48

9. Building Scheme48 on SunOS 4.1.3

10. regexp problem with scheme48

11. scheme48 and slib on windows NT

12. using "error" in Scheme48

 

 
Powered by phpBB® Forum Software