In this post I’d like to introduce
load-module, a portable Scheme module system.
Why did I write a module system?
- Simplicity: A single-file module system in about 200 lines of code
- Understandability: The implementation avoids wizardry and should be accessible to anyone who knows the language
- Portability: One system that can be used across multiple implementations
The way it works is this:
- You have a file (say,
utils.scm) with Scheme code in it that implements stuff that you want to live in the same module.
- You create another file (
utils.mod, but that extension is easy to change) which lists the procedures and syntax you want to export.
utils.scm, rewriting unexported procedure names such that only the procedures you want exported show up at the top-level. Everything else gets rewritten during load-time as an ignorable “gensym” of the form
%--gensym-utils-random-integer-8190504171, where “utils” is the module name, and “random-integer” is the procedure internal to your module.
The module file format is very simple:
(define-module utils (exports random-integer atom? take))
The module system exports one procedure:
load-module. Run it like so to get the procedures from the aforementioned hypothetical
utils package into your environment:
> (load "load-module.scm") > (load-module 'utils) #t > (random-integer 199) 76 > (atom? 199) #t
If you care, there’s more information about over at the project README.