Note: This is taken from the Chicken Wiki, where a more recent version could be available.

[[tags: egg]

syntax-case

Portable syntax-case macro and module system

Usage

(require-extension syntax-case)

Alternatively you can pass -require-extension syntax-case at the command-line to chicken (or -R syntax-case for csc).

Introduction

This is a port of the ``portable syntax-case'' macro expander by Dybvig, Waddel, Hieb and Bruggeman. This facility provides fully compliant R5RS syntax-rules and the much more powerful syntax-case form.

A comprehensive explanation is best found in the Chez Scheme Users Guide, specifically Section 9.3 - Modules.

What follows is an informal introduction to that module system.

(This introduction to modules is courtesy of Scott G. Miller)

Modules provide an additional level of scoping control, allowing symbolic and syntactic bindings to be bundled in a named or anonymous package. The package can then be imported into any scope, making the bindings contained in the module visible in only that scope.

Overview

The basic unit of modularization is a module. A typical module definition has this appearance:

(module foo
    (bar baz)
  (import boo1)
  (import boo2)
  (include "file.scm")
  (define (bar x) ...)
  (define-syntax baz ...)
  (define (something-else ...) ...)
  (do-something)
  (do-something-else))

A module definition consists of a name (foo), a list of exports (bar and baz) and a body. Expressions which can appear in the body of a module are the same as those which can appear in a lambda body. The import form imports bindings from a named module (in this case boo1 and boo2) into the current lexical scope. The include form performs a textual inclusion of the source code found in the named file (file.scm). In other words, it works as if the contents of the file had appeared literally in place of the include statement.

All identifiers appearing in the export list must be defined or define-syntaxed in the body of the module, or imported from another module.

It is recommended to clearly separate modularization from actual code. The best way to accomplish this is to

There are several reasons for this. First, it makes refactoring easier, as one can move relevant code from module to module merely by rewriting the module definitions, leaving the implementation code unchanged. Second, it makes debugging easier, as one can load the implementation code directly into the Scheme system to have access to all bindings, or load the module definition to view the finished, encapsulated exports. Finally, it stylistically separates interface (the modules) from implementation (the included Scheme source).

Modularizing Existing Code

Since module bodies are treated like the bodies of lambdas, the R5RS rules of how internal definitions are treated apply to all the definitions in the module body (both ordinary and syntax), including all code included from files. This is often a source of errors when moving code from the top-level into a module because:

This often necessitates re-arranging the code and the introduction of set! expressions. Here is an example of a sequence of top-level definitions/expressions and how they need to be rewritten so that they may appear in a module body:

(define (foo) 1)
(define bar (foo))
(do-some-stuff)
(define (baz) (bar))
==>
(define (foo) 1)
(define bar)
(define (baz) (bar))
(set! bar (foo))
(do-some-stuff)

The general strategy is to go through the list of expressions/definitions from top to bottom and build two lists - one of definitions and one of expressions - as follows:

The concatenation of the resulting definition list with the expression list makes a suitable module body.

Evaluation

Modules are lexically scoped. It is possible to define modules inside lambdas and inside other modules and to export modules from modules. Example:

(define (f c)
  (module foo
      (bar)
    (module bar
        (baz)
      (define (baz x y) (- x y))
      (display "defining baz\n")))
  (if (> c 0)
      (let ((a 1))
         (import foo)
         (let loop ((b c))
            (import bar)
            (if (> b 0) (loop (baz b a)) (f (- c 1)))))))

The expressions in a module body get executed at the time and in the context of module definition. So, in the above example, the body of bar containing the display statement is executed once for every call to f rather than once for every iteration of the inner loop containing the import of the bar module.

There are quite a few more things you can do with modules. For instance one can define anonymous modules, which are a short cut for defining a named module and then importing it, import selected bindings from a module and renaming them rather then importing all bindings as is etc etc. For more details again refer to the Chez Scheme user manual.

Notes

The following procedures and syntactic forms are supported:

The alternative form:

(define-syntax (keyword var) 
  (syntax-case var (...) ...) )

is allowed for define-syntax.

(import* MODULE IMP ...) allows selective imports. Only the identifiers IMP ... will be imported from MODULE. IMP may be a list of the form (NEW OLD) where the exported identifier OLD will be imported under the name NEW.

When import, import* or import-only is used with an argument that names a module that is currently not defined, then the current include-path (and the repository-path as well) is searched for a source-file of the same name (possibly with the extension .scm), which (if found) will be ``visited'' (it's module- and syntax-definitions are processed) using the procedure visit.

define-syntax can not be used inside an eval-when form.

No builtin modules are provided.

The run-time-macros declaration (and compiler option) has no effect with syntax-case macros

All non-standard CHICKEN syntax is supported, including define-macro

define-method is only valid for generic functions that have been previously defined with define-generic

When used during compilation, the non-standard syntax for interfacing to foreign code is loaded automatically

define-constant and define-inline are treated specially.

To export undecorated module bindings from a compilation unit, use the export-toplevel form:

(module (foo)
  (define (foo) 'ok) 
  (export-toplevel foo) )  ; global variable `foo' is now visible from outside

((export-toplevel also accepts (VARIABLE EXPORTEDVARIABLE) which exports a variable under a different name)

The idea is to allow using modules in separate compilation units while interfacing with code that doesn't use the syntax-case macro (and module) system.

Note that export-toplevel does not work for syntax.

Example

Here is a small example that demonstrates separate compilation:

Let's say we have one file foo.scm that defines a module:

(module foo (pr (bar baz))
  (define pr print)
  (define baz 99)
  (define-syntax bar
    (syntax-rules ()
      ((_ x) (list baz 'x)) ) ) )

and another that uses it (use-foo.scm):

(load "foo.so")   ; (require 'foo) would also work

(import foo)
(pr (bar hello))

Compiling the files like this will lead to one dynamically loadable library and a plain executable:

$ csc -s -R syntax-case foo.scm
$ csc -R syntax-case use-foo.scm
$ use-foo
(99 hello)

Additional procedures

<procedure>(visit FILENAME)</procedure>

Reads all toplevel expressions from the given file and expands all syntax, extracting module- and syntax-information to be subsequently used during the current compilation or interpretation of modules.

<procedure>(debug-expand EXP)</procedure>

Macro-expands the expression EXP and shows each expansion step, giving a choice of expanding the expression completely, advance to the next expansion step or aborting the expansion. This might be a useful tool for debugging complex macros.

The implementation of cond is SRFI-61 compliant.

Authors

R. Kent Dybvig, Oscar Waddell, Bob Hieb, Carl Bruggeman

License

Copyright © 1992-2002 Cadence Research Systems Permission to copy this software, in whole or in part, to use this software for any lawful purpose, and to redistribute this software is granted subject to the restriction that all copies made of this software must include this copyright notice in full. This software is provided AS IS, with NO WARRANTY, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES OF ANY NATURE WHATSOEVER.

Requirements

None

Version History