Modules

I noticed there's a new version of Arc. According to the web page, "The most dramatic change is probably the ability to use x.y and x!y as abbreviations for (x y) and (x 'y) respectively."

The x.y syntax reminds me of one of the things I miss in Scheme: a "Pythonic" module system. By "Pythonic" I mean, that you can import modules, and get a module object back, that you can access using x.y syntax. I'm mostly interesting in the namespace issue here, as the convention in Scheme seems to be, to just stick everything in the global namespace.

In other words, I am somewhat uncomfortable that it's not possible to do this:

;; --- foo.scm ---

(define (bar x)
  (+ x 1))

;; --- REPL ---

> (import foo)
> foo
#<module foo>
> (foo.bar 3)
4
> foo.bar
#<procedure (foo.bar x)>

...or something to that effect.

Maybe this is an irrational "need", but I like namespaces, and have gotten used to them over the years, and loading the toplevel namespace with lots and lots of definitions just seems a bit "unsafe" to me.

I found a comparison of Python's and PLT Scheme's module systems, which explains the issue better:

Once we've done '(require (lib "math.ss"))', we have access to the internals of the math library. But there's one surprise: unlike Python, 'math' itself is not a first-class object. By default, the require form has the same semantics as Python's "from [module] import *"!

The article then mentions the following technique to add a prefix to the names imported from a module:

> (require (prefix math. (lib "math.ss" "mzlib")))
> math.e
2.718281828459045

I suppose this would not be too bad as an alternative, although you still cannot do things like inspecting a module, pass it around, etc. Chicken does not seem to support it though, and Schemers generally don't seem to miss the feature. (Or maybe there's a reason why it would be a bad idea in Scheme.) So maybe I should just learn to live without it. Thoughts welcome...

4 Comments »

  1. Jens Axel Søgaard said,

    February 13, 2008 @ 4:51 am

    Hi Hans,

    You can inspect modules, require them dynamically and more in PLT Scheme. For inspection see:

    http://pre.plt-scheme.org/docs/html/reference/Expanding_Top-Level_Forms.html#(part~20modinfo)

    More tricks can be accomplished with the help of module->namespace.

    Search for module in the pre-release version of the documentation. For a more direct answer, don't hessitate to ask the PLT mailing list.

  2. Robby Findler said,

    February 13, 2008 @ 8:36 am

    Well, for one, I would definitely miss modules if they were removed from PLT Scheme. (One thing to keep in mind about the various Schemes out there is that they are really like kissing cousins than actually the same language implemented multiple times.)

  3. Houman Zolfaghari said,

    February 13, 2008 @ 5:28 pm

    Three years ago, I had to choose a scheme implementation for a big project (meaning CPU and data intensive, and designed to evolve over many years). For many different reasons, I chose chicken over PLT scheme and gambit (gambit had almost no libraries). My biggest concern with chicken was precisely namespace/modules.

    Now, after 3 intensive chicken years, I'm surprised that I haven't missed them yet.

    I should say though that I have yet to see a module system in scheme that solves all the problems while keeping the natural simplicity and bare elegance of scheme . IMHO, all the solutions I've seen are either too arbitrary or too elaborate. I'm not a scheme-at-all-cost proponent, and if I was to abandon the simplicity of scheme, I would as well use CL, Ocaml or Haskell.

    I don't write that to enter another boring my-scheme vs your scheme war. I have lots of respect the PLT team's work, as well as each one the scheme implementations. They all have their own strengths and priorities. But this my answer to the question of the necessity of a namespace system.

    Many people are using chicken, and producing lots of libraries, and the need for a more elaborate module system was rarely if ever expressed. Maybe we should ask them directly in their in mailing list why and how they can live without it?

  4. Graham Fawcett said,

    February 15, 2008 @ 10:43 pm

    Hans, I echo Houman's statements. It was also hard for me to accept the lack of namespaces in Chicken, but honestly I cannot say it has been a significant problem.

    For your *own* code, there are lots of options. The portable syntax-case system (provided as an egg) provides a module system; also, search for 'module' in the eggs list for others. I'm writing my own, just for fun: it's about 100 lines of code and does most of what I'd ever want.

RSS feed for comments on this post · TrackBack URI

Leave a Comment