SLIB

The Portable Scheme Library

Version 2c0

by Aubrey Jaffer


Table of Contents


The Library System

Introduction

SLIB is a portable library for the programming language Scheme. It provides a platform independent framework for using packages of Scheme procedures and syntax. As distributed, SLIB contains useful packages for all implementations. Its catalog can be transparently extended to accomodate packages specific to a site, implementation, user, or directory.

Aubrey Jaffer <jaffer@ai.mit.edu>
Hyperactive Software -- The Maniac Inside!
http://www-swiss.ai.mit.edu/~jaffer/SLIB.html

Feature

SLIB denotes features by symbols. SLIB maintains a list of features supported by the Scheme session. The set of features provided by a session may change over time. Some features are properties of the Scheme implementation being used. The following features detail what sort of numbers are available from an implementation.

Other features correspond to the presence of sets of Scheme procedures or syntax (macros).

Function: provided? feature
Returns #t if feature is supported by the current Scheme session.

Procedure: provide feature
Informs SLIB that feature is supported. Henceforth (provided? feature) will return #t.

(provided? 'foo)    => #f
(provide 'foo)
(provided? 'foo)    => #t

Requesting Features

SLIB creates and maintains a catalog mapping features to locations of files introducing procedures and syntax denoted by those features.

At the beginning of each section of this manual, there is a line like (require 'feature). The Scheme files comprising SLIB are cataloged so that these feature names map to the corresponding files.

SLIB provides a form, require, which loads the files providing the requested feature.

Procedure: require feature

The catalog can also be queried using require:feature->path.

Function: require:feature->path feature

Library Catalogs

At the start of a session no catalog is present, but is created with the first catalog inquiry (such as (require 'random)). Several sources of catalog information are combined to produce the catalog:

Catalog files consist of one or more association lists. In the circumstance where a feature symbol appears in more than one list, the latter list's association is retrieved. Here are the supported formats for elements of catalog lists:

(feature . <symbol>)
Redirects to the feature named <symbol>.
(feature . "<path>")
Loads file <path>.
(feature source "<path>")
slib:loads the Scheme source file <path>.
(feature compiled "<path>" ...)
slib:load-compileds the files <path> ....

The various macro styles first require the named macro package, then just load <path> or load-and-macro-expand <path> as appropriate for the implementation.

(feature defmacro "<path>")
defmacro:loads the Scheme source file <path>.
(feature macro-by-example "<path>")
defmacro:loads the Scheme source file <path>.
(feature macro "<path>")
macro:loads the Scheme source file <path>.
(feature macros-that-work "<path>")
macro:loads the Scheme source file <path>.
(feature syntax-case "<path>")
macro:loads the Scheme source file <path>.
(feature syntactic-closures "<path>")
macro:loads the Scheme source file <path>.

Here is an example of a `usercat' catalog. A Program in this directory can invoke the `run' feature with (require 'run).

;;; "usercat": SLIB catalog additions for SIMSYNCH.	-*-scheme-*-

(
 (simsynch	. "../synch/simsynch.scm")
 (run		. "../synch/run.scm")
 (schlep	. "schlep.scm")
)

Catalog Compilation

SLIB combines the catalog information which doesn't vary per user into the file `slibcat' in the implementation-vicinity. Therefore `slibcat' needs change only when new software is installed or compiled. Because the actual pathnames of files can differ from installation to installation, SLIB builds a separate catalog for each implementation it is used with.

The definition of *SLIB-VERSION* in SLIB file `require.scm' is checked against the catalog association of *SLIB-VERSION* to ascertain when versions have changed. I recommend that the definition of *SLIB-VERSION* be changed whenever the library is changed. If multiple implementations of Scheme use SLIB, remember that recompiling one `slibcat' will fix only that implementation's catalog.

The compilation scripts of Scheme implementations which work with SLIB can automatically trigger catalog compilation by deleting `slibcat' or by invoking a special form of require:

Procedure: require 'new-catalog
This will load `mklibcat', which compiles and writes a new `slibcat'.

Another special form of require erases SLIB's catalog, forcing it to be reloaded the next time the catalog is queried.

Procedure: require #f
Removes SLIB's catalog information. This should be done before saving an executable image so that, when restored, its catalog will be loaded afresh.

Each file in the table below is descibed in terms of its file-system independent vicinity (see section Vicinity). The entries of a catalog in the table override those of catalogs above it in the table.

implementation-vicinity `slibcat'
This file contains the associations for the packages comprising SLIB, the `implcat' and the `sitecat's. The associations in the other catalogs override those of the standard catalog.
library-vicinity `mklibcat.scm'
creates `slibcat'.
library-vicinity `sitecat'
This file contains the associations specific to an SLIB installation.
implementation-vicinity `implcat'
This file contains the associations specific to an implementation of Scheme. Different implementations of Scheme should have different implementation-vicinity.
implementation-vicinity `mkimpcat.scm'
if present, creates `implcat'.
implementation-vicinity `sitecat'
This file contains the associations specific to a Scheme implementation installation.
home-vicinity `homecat'
This file contains the associations specific to an SLIB user.
user-vicinity `usercat'
This file contains associations effecting only those sessions whose working directory is user-vicinity.

Built-in Support

The procedures described in these sections are supported by all implementations as part of the `*.init' files or by `require.scm'.

Require

Variable: *features*
Is a list of symbols denoting features supported in this implementation. *features* can grow as modules are required. *features* must be defined by all implementations (see section Porting).

Here are features which SLIB (`require.scm') adds to *features* when appropriate.

For each item, (provided? 'feature) will return #t if that feature is available, and #f if not.

Variable: *modules*
Is a list of pathnames denoting files which have been loaded.

Variable: *catalog*
Is an association list of features (symbols) and pathnames which will supply those features. The pathname can be either a string or a pair. If pathname is a pair then the first element should be a macro feature symbol, source, or compiled. The cdr of the pathname should be either a string or a list.

In the following functions if the argument feature is not a symbol it is assumed to be a pathname.

Function: provided? feature
Returns #t if feature is a member of *features* or *modules* or if feature is supported by a file already loaded and #f otherwise.

Procedure: require feature
feature is a symbol. If (provided? feature) is true require returns. Otherwise, if (assq feature *catalog*) is not #f, the associated files will be loaded and (provided? feature) will henceforth return #t. An unspecified value is returned. If feature is not found in *catalog*, then an error is signaled.

Procedure: require pathname
pathname is a string. If pathname has not already been given as an argument to require, pathname is loaded. An unspecified value is returned.

Procedure: provide feature
Assures that feature is contained in *features* if feature is a symbol and *modules* otherwise.

Function: require:feature->path feature
Returns #t if feature is a member of *features* or *modules* or if feature is supported by a file already loaded. Returns a path if one was found in *catalog* under the feature name, and #f otherwise. The path can either be a string suitable as an argument to load or a pair as described above for *catalog*.

Vicinity

A vicinity is a descriptor for a place in the file system. Vicinities hide from the programmer the concepts of host, volume, directory, and version. Vicinities express only the concept of a file environment where a file name can be resolved to a file in a system independent manner. Vicinities can even be used on flat file systems (which have no directory structure) by having the vicinity express constraints on the file name. On most systems a vicinity would be a string. All of these procedures are file system dependent.

These procedures are provided by all implementations.

Function: make-vicinity filename
Returns the vicinity of filename for use by in-vicinity.

Function: program-vicinity
Returns the vicinity of the currently loading Scheme code. For an interpreter this would be the directory containing source code. For a compiled system (with multiple files) this would be the directory where the object or executable files are. If no file is currently loading it the result is undefined. Warning: program-vicinity can return incorrect values if your program escapes back into a load.

Function: library-vicinity
Returns the vicinity of the shared Scheme library.

Function: implementation-vicinity
Returns the vicinity of the underlying Scheme implementation. This vicinity will likely contain startup code and messages and a compiler.

Function: user-vicinity
Returns the vicinity of the current directory of the user. On most systems this is `""' (the empty string).

Function: home-vicinity
Returns the vicinity of the user's HOME directory, the directory which typically contains files which customize a computer environment for a user. If scheme is running without a user (eg. a daemon) or if this concept is meaningless for the platform, then home-vicinity returns #f.

Function: in-vicinity vicinity filename
Returns a filename suitable for use by slib:load, slib:load-source, slib:load-compiled, open-input-file, open-output-file, etc. The returned filename is filename in vicinity. in-vicinity should allow filename to override vicinity when filename is an absolute pathname and vicinity is equal to the value of (user-vicinity). The behavior of in-vicinity when filename is absolute and vicinity is not equal to the value of (user-vicinity) is unspecified. For most systems in-vicinity can be string-append.

Function: sub-vicinity vicinity name
Returns the vicinity of vicinity restricted to name. This is used for large systems where names of files in subsystems could conflict. On systems with directory structure sub-vicinity will return a pathname of the subdirectory name of vicinity.

Configuration

These constants and procedures describe characteristics of the Scheme and underlying operating system. They are provided by all implementations.

Constant: char-code-limit
An integer 1 larger that the largest value which can be returned by char->integer.

Constant: most-positive-fixnum
The immediate integer closest to positive infinity.

Constant: slib:tab
The tab character.

Constant: slib:form-feed
The form-feed character.

Function: software-type
Returns a symbol denoting the generic operating system type. For instance, unix, vms, macos, amiga, or ms-dos.

Function: slib:report-version
Displays the versions of SLIB and the underlying Scheme implementation and the name of the operating system. An unspecified value is returned.

(slib:report-version) => slib "2c0" on scm "5b1" on unix 

Function: slib:report
Displays the information of (slib:report-version) followed by almost all the information neccessary for submitting a problem report. An unspecified value is returned.

Function: slib:report #t
provides a more verbose listing.

Function: slib:report filename
Writes the report to file `filename'.

(slib:report)
=>
slib "2c0" on scm "5b1" on unix 
(implementation-vicinity) is "/home/jaffer/scm/" 
(library-vicinity) is "/home/jaffer/slib/" 
(scheme-file-suffix) is ".scm" 
loaded *features* : 
        trace alist qp sort
        common-list-functions macro values getopt
        compiled
implementation *features* : 
        bignum complex real rational
        inexact vicinity ed getenv
        tmpnam abort transcript with-file
        ieee-p1178 rev4-report rev4-optional-procedures hash
        object-hash delay eval dynamic-wind
        multiarg-apply multiarg/and- logical defmacro
        string-port source current-time record
        rev3-procedures rev2-procedures sun-dl string-case
        array dump char-ready? full-continuation
        system
implementation *catalog* : 
        (i/o-extensions compiled "/home/jaffer/scm/ioext.so") 
        ... 

Input/Output

These procedures are provided by all implementations.

Procedure: file-exists? filename
Returns #t if the specified file exists. Otherwise, returns #f. If the underlying implementation does not support this feature then #f is always returned.

Procedure: delete-file filename
Deletes the file specified by filename. If filename can not be deleted, #f is returned. Otherwise, #t is returned.

Procedure: tmpnam
Returns a pathname for a file which will likely not be used by any other process. Successive calls to (tmpnam) will return different pathnames.

Procedure: current-error-port
Returns the current port to which diagnostic and error output is directed.

Procedure: force-output
Procedure: force-output port
Forces any pending output on port to be delivered to the output device and returns an unspecified value. The port argument may be omitted, in which case it defaults to the value returned by (current-output-port).

Procedure: output-port-width
Procedure: output-port-width port

Returns the width of port, which defaults to (current-output-port) if absent. If the width cannot be determined 79 is returned.

Procedure: output-port-height
Procedure: output-port-height port

Returns the height of port, which defaults to (current-output-port) if absent. If the height cannot be determined 24 is returned.

Legacy

These procedures are provided by all implementations.

Function: identity x
identity returns its argument.

Example:

(identity 3)
   => 3
(identity '(foo bar))
   => (foo bar)
(map identity lst)
   == (copy-list lst)

The following procedures were present in Scheme until R4RS (see section `Language changes' in Revised(4) Scheme). They are provided by all SLIB implementations.

Constant: t
Derfined as #t.

Constant: nil
Defined as #f.

Function: last-pair l
Returns the last pair in the list l. Example:
(last-pair (cons 1 2))
   => (1 . 2)
(last-pair '(1 2))
   => (2)
    == (cons 2 '())

System

These procedures are provided by all implementations.

Procedure: slib:load-source name
Loads a file of Scheme source code from name with the default filename extension used in SLIB. For instance if the filename extension used in SLIB is `.scm' then (slib:load-source "foo") will load from file `foo.scm'.

Procedure: slib:load-compiled name
On implementations which support separtely loadable compiled modules, loads a file of compiled code from name with the implementation's filename extension for compiled code appended.

Procedure: slib:load name
Loads a file of Scheme source or compiled code from name with the appropriate suffixes appended. If both source and compiled code are present with the appropriate names then the implementation will load just one. It is up to the implementation to choose which one will be loaded.

If an implementation does not support compiled code then slib:load will be identical to slib:load-source.

Procedure: slib:eval obj
eval returns the value of obj evaluated in the current top level environment.

Procedure: slib:eval-load filename eval
filename should be a string. If filename names an existing file, the Scheme source code expressions and definitions are read from the file and eval called with them sequentially. The slib:eval-load procedure does not affect the values returned by current-input-port and current-output-port.

Procedure: slib:warn arg1 arg2 ...
Outputs a warning message containing the arguments.

Procedure: slib:error arg1 arg2 ...
Outputs an error message containing the arguments, aborts evaluation of the current form and responds in a system dependent way to the error. Typical responses are to abort the program or to enter a read-eval-print loop.

Procedure: slib:exit n
Procedure: slib:exit
Exits from the Scheme session returning status n to the system. If n is omitted or #t, a success status is returned to the system (if possible). If n is #f a failure is returned to the system (if possible). If n is an integer, then n is returned to the system (if possible). If the Scheme session cannot exit an unspecified value is returned from slib:exit.

About this manual

Scheme Syntax Extension Packages

Defmacro

Defmacros are supported by all implementations.

Function: gentemp
Returns a new (interned) symbol each time it is called. The symbol names are implementation-dependent
(gentemp) => scm:G0
(gentemp) => scm:G1

Function: defmacro:eval e
Returns the slib:eval of expanding all defmacros in scheme expression e.

Function: defmacro:load filename
filename should be a string. If filename names an existing file, the defmacro:load procedure reads Scheme source code expressions and definitions from the file and evaluates them sequentially. These source code expressions and definitions may contain defmacro definitions. The macro:load procedure does not affect the values returned by current-input-port and current-output-port.

Function: defmacro? sym
Returns #t if sym has been defined by defmacro, #f otherwise.

Function: macroexpand-1 form
Function: macroexpand form
If form is a macro call, macroexpand-1 will expand the macro call once and return it. A form is considered to be a macro call only if it is a cons whose car is a symbol for which a defmacr has been defined.

macroexpand is similar to macroexpand-1, but repeatedly expands form until it is no longer a macro call.

Macro: defmacro name lambda-list form ...
When encountered by defmacro:eval, defmacro:macroexpand*, or defmacro:load defines a new macro which will henceforth be expanded when encountered by defmacro:eval, defmacro:macroexpand*, or defmacro:load.

Defmacroexpand

(require 'defmacroexpand)

Function: defmacro:expand* e
Returns the result of expanding all defmacros in scheme expression e.

R4RS Macros

(require 'macro) is the appropriate call if you want R4RS high-level macros but don't care about the low level implementation. If an SLIB R4RS macro implementation is already loaded it will be used. Otherwise, one of the R4RS macros implemetations is loaded.

The SLIB R4RS macro implementations support the following uniform interface:

Function: macro:expand sexpression
Takes an R4RS expression, macro-expands it, and returns the result of the macro expansion.

Function: macro:eval sexpression
Takes an R4RS expression, macro-expands it, evals the result of the macro expansion, and returns the result of the evaluation.

Procedure: macro:load filename
filename should be a string. If filename names an existing file, the macro:load procedure reads Scheme source code expressions and definitions from the file and evaluates them sequentially. These source code expressions and definitions may contain macro definitions. The macro:load procedure does not affect the values returned by current-input-port and current-output-port.

Macro by Example

(require 'macro-by-example)

A vanilla implementation of Macro by Example (Eugene Kohlbecker, R4RS) by Dorai Sitaram, (dorai@cs.rice.edu) using defmacro.

Caveat

These macros are not referentially transparent (see section `Macros' in Revised(4) Scheme). Lexically scoped macros (i.e., let-syntax and letrec-syntax) are not supported. In any case, the problem of referential transparency gains poignancy only when let-syntax and letrec-syntax are used. So you will not be courting large-scale disaster unless you're using system-function names as local variables with unintuitive bindings that the macro can't use. However, if you must have the full r4rs macro functionality, look to the more featureful (but also more expensive) versions of syntax-rules available in slib section Macros That Work, section Syntactic Closures, and section Syntax-Case Macros.

Macro: define-syntax keyword transformer-spec
The keyword is an identifier, and the transformer-spec should be an instance of syntax-rules.

The top-level syntactic environment is extended by binding the keyword to the specified transformer.

(define-syntax let*
  (syntax-rules ()
    ((let* () body1 body2 ...)
     (let () body1 body2 ...))
    ((let* ((name1 val1) (name2 val2) ...)
       body1 body2 ...)
     (let ((name1 val1))
       (let* (( name2 val2) ...)
         body1 body2 ...)))))

Macro: syntax-rules literals syntax-rule ...
literals is a list of identifiers, and each syntax-rule should be of the form

(pattern template)

where the pattern and template are as in the grammar above.

An instance of syntax-rules produces a new macro transformer by specifying a sequence of hygienic rewrite rules. A use of a macro whose keyword is associated with a transformer specified by syntax-rules is matched against the patterns contained in the syntax-rules, beginning with the leftmost syntax-rule. When a match is found, the macro use is trancribed hygienically according to the template.

Each pattern begins with the keyword for the macro. This keyword is not involved in the matching and is not considered a pattern variable or literal identifier.

Macros That Work

(require 'macros-that-work)

Macros That Work differs from the other R4RS macro implementations in that it does not expand derived expression types to primitive expression types.

Function: macro:expand expression
Function: macwork:expand expression
Takes an R4RS expression, macro-expands it, and returns the result of the macro expansion.

Function: macro:eval expression
Function: macwork:eval expression
macro:eval returns the value of expression in the current top level environment. expression can contain macro definitions. Side effects of expression will affect the top level environment.

Procedure: macro:load filename
Procedure: macwork:load filename
filename should be a string. If filename names an existing file, the macro:load procedure reads Scheme source code expressions and definitions from the file and evaluates them sequentially. These source code expressions and definitions may contain macro definitions. The macro:load procedure does not affect the values returned by current-input-port and current-output-port.

References:

The Revised^4 Report on the Algorithmic Language Scheme Clinger and Rees [editors]. To appear in LISP Pointers. Also available as a technical report from the University of Oregon, MIT AI Lab, and Cornell.

Macros That Work. Clinger and Rees. POPL '91.

The supported syntax differs from the R4RS in that vectors are allowed as patterns and as templates and are not allowed as pattern or template data.

transformer spec  ==>  (syntax-rules literals rules)

rules  ==>  ()
         |  (rule . rules)

rule  ==>  (pattern template)

pattern  ==>  pattern_var      ; a symbol not in literals
           |  symbol           ; a symbol in literals
           |  ()
           |  (pattern . pattern)
           |  (ellipsis_pattern)
           |  #(pattern*)                     ; extends R4RS
           |  #(pattern* ellipsis_pattern)    ; extends R4RS
           |  pattern_datum

template  ==>  pattern_var
            |  symbol
            |  ()
            |  (template2 . template2)
            |  #(template*)                   ; extends R4RS
            |  pattern_datum

template2  ==>  template
             |  ellipsis_template

pattern_datum  ==>  string                    ; no vector
                 |  character
                 |  boolean
                 |  number

ellipsis_pattern  ==> pattern ...

ellipsis_template  ==>  template ...

pattern_var  ==>  symbol   ; not in literals

literals  ==>  ()
            |  (symbol . literals)

Definitions

Scope of an ellipsis
Within a pattern or template, the scope of an ellipsis (...) is the pattern or template that appears to its left.
Rank of a pattern variable
The rank of a pattern variable is the number of ellipses within whose scope it appears in the pattern.
Rank of a subtemplate
The rank of a subtemplate is the number of ellipses within whose scope it appears in the template.
Template rank of an occurrence of a pattern variable
The template rank of an occurrence of a pattern variable within a template is the rank of that occurrence, viewed as a subtemplate.
Variables bound by a pattern
The variables bound by a pattern are the pattern variables that appear within it.
Referenced variables of a subtemplate
The referenced variables of a subtemplate are the pattern variables that appear within it.
Variables opened by an ellipsis template
The variables opened by an ellipsis template are the referenced pattern variables whose rank is greater than the rank of the ellipsis template.

Restrictions

No pattern variable appears more than once within a pattern.

For every occurrence of a pattern variable within a template, the template rank of the occurrence must be greater than or equal to the pattern variable's rank.

Every ellipsis template must open at least one variable.

For every ellipsis template, the variables opened by an ellipsis template must all be bound to sequences of the same length.

The compiled form of a rule is

rule  ==>  (pattern template inserted)

pattern  ==>  pattern_var
           |  symbol
           |  ()
           |  (pattern . pattern)
           |  ellipsis_pattern
           |  #(pattern)
           |  pattern_datum

template  ==>  pattern_var
            |  symbol
            |  ()
            |  (template2 . template2)
            |  #(pattern)
            |  pattern_datum

template2  ==>  template
             |  ellipsis_template

pattern_datum  ==>  string
                 |  character
                 |  boolean
                 |  number

pattern_var  ==>  #(V symbol rank)

ellipsis_pattern  ==>  #(E pattern pattern_vars)

ellipsis_template  ==>  #(E template pattern_vars)

inserted  ==>  ()
            |  (symbol . inserted)

pattern_vars  ==>  ()
                |  (pattern_var . pattern_vars)

rank  ==>  exact non-negative integer

where V and E are unforgeable values.

The pattern variables associated with an ellipsis pattern are the variables bound by the pattern, and the pattern variables associated with an ellipsis template are the variables opened by the ellipsis template.

If the template contains a big chunk that contains no pattern variables or inserted identifiers, then the big chunk will be copied unnecessarily. That shouldn't matter very often.

Syntactic Closures

(require 'syntactic-closures)

Function: macro:expand expression
Function: synclo:expand expression
Returns scheme code with the macros and derived expression types of expression expanded to primitive expression types.

Function: macro:eval expression
Function: synclo:eval expression
macro:eval returns the value of expression in the current top level environment. expression can contain macro definitions. Side effects of expression will affect the top level environment.

Procedure: macro:load filename
Procedure: synclo:load filename
filename should be a string. If filename names an existing file, the macro:load procedure reads Scheme source code expressions and definitions from the file and evaluates them sequentially. These source code expressions and definitions may contain macro definitions. The macro:load procedure does not affect the values returned by current-input-port and current-output-port.

Syntactic Closure Macro Facility

A Syntactic Closures Macro Facility by Chris Hanson 9 November 1991

This document describes syntactic closures, a low-level macro facility for the Scheme programming language. The facility is an alternative to the low-level macro facility described in the Revised^4 Report on Scheme. This document is an addendum to that report.

The syntactic closures facility extends the BNF rule for transformer spec to allow a new keyword that introduces a low-level macro transformer:

transformer spec := (transformer expression)

Additionally, the following procedures are added:

make-syntactic-closure
capture-syntactic-environment
identifier?
identifier=?

The description of the facility is divided into three parts. The first part defines basic terminology. The second part describes how macro transformers are defined. The third part describes the use of identifiers, which extend the syntactic closure mechanism to be compatible with syntax-rules.

Terminology

This section defines the concepts and data types used by the syntactic closures facility.

Transformer Definition

This section describes the transformer special form and the procedures make-syntactic-closure and capture-syntactic-environment.

Syntax: transformer expression

Syntax: It is an error if this syntax occurs except as a transformer spec.

Semantics: The expression is evaluated in the standard transformer environment to yield a macro transformer as described below. This macro transformer is bound to a macro keyword by the special form in which the transformer expression appears (for example, let-syntax).

A macro transformer is a procedure that takes two arguments, a form and a syntactic environment, and returns a new form. The first argument, the input form, is the form in which the macro keyword occurred. The second argument, the usage environment, is the syntactic environment in which the input form occurred. The result of the transformer, the output form, is automatically closed in the transformer environment, which is the syntactic environment in which the transformer expression occurred.

For example, here is a definition of a push macro using syntax-rules:

(define-syntax  push
  (syntax-rules ()
    ((push item list)
     (set! list (cons item list)))))

Here is an equivalent definition using transformer:

(define-syntax push
  (transformer
   (lambda (exp env)
     (let ((item
            (make-syntactic-closure env '() (cadr exp)))
           (list
            (make-syntactic-closure env '() (caddr exp))))
       `(set! ,list (cons ,item ,list))))))

In this example, the identifiers set! and cons are closed in the transformer environment, and thus will not be affected by the meanings of those identifiers in the usage environment env.

Some macros may be non-hygienic by design. For example, the following defines a loop macro that implicitly binds exit to an escape procedure. The binding of exit is intended to capture free references to exit in the body of the loop, so exit must be left free when the body is closed:

(define-syntax loop
  (transformer
   (lambda (exp env)
     (let ((body (cdr exp)))
       `(call-with-current-continuation
         (lambda (exit)
           (let f ()
             ,@(map (lambda  (exp)
                       (make-syntactic-closure env '(exit)
                                               exp))
                     body)
             (f))))))))

To assign meanings to the identifiers in a form, use make-syntactic-closure to close the form in a syntactic environment.

Function: make-syntactic-closure environment free-names form

environment must be a syntactic environment, free-names must be a list of identifiers, and form must be a form. make-syntactic-closure constructs and returns a syntactic closure of form in environment, which can be used anywhere that form could have been used. All the identifiers used in form, except those explicitly excepted by free-names, obtain their meanings from environment.

Here is an example where free-names is something other than the empty list. It is instructive to compare the use of free-names in this example with its use in the loop example above: the examples are similar except for the source of the identifier being left free.

(define-syntax let1
  (transformer
   (lambda (exp env)
     (let ((id (cadr exp))
           (init (caddr exp))
           (exp (cadddr exp)))
       `((lambda (,id)
           ,(make-syntactic-closure env (list id) exp))
         ,(make-syntactic-closure env '() init))))))

let1 is a simplified version of let that only binds a single identifier, and whose body consists of a single expression. When the body expression is syntactically closed in its original syntactic environment, the identifier that is to be bound by let1 must be left free, so that it can be properly captured by the lambda in the output form.

To obtain a syntactic environment other than the usage environment, use capture-syntactic-environment.

Function: capture-syntactic-environment procedure

capture-syntactic-environment returns a form that will, when transformed, call procedure on the current syntactic environment. procedure should compute and return a new form to be transformed, in that same syntactic environment, in place of the form.

An example will make this clear. Suppose we wanted to define a simple loop-until keyword equivalent to

(define-syntax loop-until
  (syntax-rules ()
    ((loop-until id init test return step)
     (letrec ((loop
               (lambda (id)
                 (if test return (loop step)))))
       (loop init)))))

The following attempt at defining loop-until has a subtle bug:

(define-syntax loop-until
  (transformer
   (lambda (exp env)
     (let ((id (cadr exp))
           (init (caddr exp))
           (test (cadddr exp))
           (return (cadddr (cdr exp)))
           (step (cadddr (cddr exp)))
           (close
            (lambda (exp free)
              (make-syntactic-closure env free exp))))
       `(letrec ((loop
                  (lambda (,id)
                    (if ,(close test (list id))
                        ,(close return (list id))
                        (loop ,(close step (list id)))))))
          (loop ,(close init '())))))))

This definition appears to take all of the proper precautions to prevent unintended captures. It carefully closes the subexpressions in their original syntactic environment and it leaves the id identifier free in the test, return, and step expressions, so that it will be captured by the binding introduced by the lambda expression. Unfortunately it uses the identifiers if and loop within that lambda expression, so if the user of loop-until just happens to use, say, if for the identifier, it will be inadvertently captured.

The syntactic environment that if and loop want to be exposed to is the one just outside the lambda expression: before the user's identifier is added to the syntactic environment, but after the identifier loop has been added. capture-syntactic-environment captures exactly that environment as follows:

(define-syntax loop-until
  (transformer
   (lambda (exp env)
     (let ((id (cadr exp))
           (init (caddr exp))
           (test (cadddr exp))
           (return (cadddr (cdr exp)))
           (step (cadddr (cddr exp)))
           (close
            (lambda (exp free)
              (make-syntactic-closure env free exp))))
       `(letrec ((loop
                  ,(capture-syntactic-environment
                    (lambda (env)
                      `(lambda (,id)
                         (,(make-syntactic-closure env '() `if)
                          ,(close test (list id))
                          ,(close return (list id))
                          (,(make-syntactic-closure env '()
                                                    `loop)
                           ,(close step (list id)))))))))
          (loop ,(close init '())))))))

In this case, having captured the desired syntactic environment, it is convenient to construct syntactic closures of the identifiers if and the loop and use them in the body of the lambda.

A common use of capture-syntactic-environment is to get the transformer environment of a macro transformer:

(transformer
 (lambda (exp env)
   (capture-syntactic-environment
    (lambda (transformer-env)
      ...))))

Identifiers

This section describes the procedures that create and manipulate identifiers. Previous syntactic closure proposals did not have an identifier data type -- they just used symbols. The identifier data type extends the syntactic closures facility to be compatible with the high-level syntax-rules facility.

As discussed earlier, an identifier is either a symbol or an alias. An alias is implemented as a syntactic closure whose form is an identifier:

(make-syntactic-closure env '() 'a)
   => an alias

Aliases are implemented as syntactic closures because they behave just like syntactic closures most of the time. The difference is that an alias may be bound to a new value (for example by lambda or let-syntax); other syntactic closures may not be used this way. If an alias is bound, then within the scope of that binding it is looked up in the syntactic environment just like any other identifier.

Aliases are used in the implementation of the high-level facility syntax-rules. A macro transformer created by syntax-rules uses a template to generate its output form, substituting subforms of the input form into the template. In a syntactic closures implementation, all of the symbols in the template are replaced by aliases closed in the transformer environment, while the output form itself is closed in the usage environment. This guarantees that the macro transformation is hygienic, without requiring the transformer to know the syntactic roles of the substituted input subforms.

Function: identifier? object
Returns #t if object is an identifier, otherwise returns #f. Examples:
(identifier? 'a)
   => #t
(identifier? (make-syntactic-closure env '() 'a))
   => #t
(identifier? "a")
   => #f
(identifier? #\a)
   => #f
(identifier? 97)
   => #f
(identifier? #f)
   => #f
(identifier? '(a))
   => #f
(identifier? '#(a))
   => #f

The predicate eq? is used to determine if two identifers are "the same". Thus eq? can be used to compare identifiers exactly as it would be used to compare symbols. Often, though, it is useful to know whether two identifiers "mean the same thing". For example, the cond macro uses the symbol else to identify the final clause in the conditional. A macro transformer for cond cannot just look for the symbol else, because the cond form might be the output of another macro transformer that replaced the symbol else with an alias. Instead the transformer must look for an identifier that "means the same thing" in the usage environment as the symbol else means in the transformer environment.

Function: identifier=? environment1 identifier1 environment2 identifier2
environment1 and environment2 must be syntactic environments, and identifier1 and identifier2 must be identifiers. identifier=? returns #t if the meaning of identifier1 in environment1 is the same as that of identifier2 in environment2, otherwise it returns #f. Examples:

(let-syntax
    ((foo
      (transformer
       (lambda (form env)
         (capture-syntactic-environment
          (lambda (transformer-env)
            (identifier=? transformer-env 'x env 'x)))))))
  (list (foo)
        (let ((x 3))
          (foo))))
   => (#t #f)
(let-syntax ((bar foo))
  (let-syntax
      ((foo
        (transformer
         (lambda (form env)
           (capture-syntactic-environment
            (lambda (transformer-env)
              (identifier=? transformer-env 'foo
                            env (cadr form))))))))
    (list (foo foo)
          (foobar))))
   => (#f #t)

Acknowledgements

The syntactic closures facility was invented by Alan Bawden and Jonathan Rees. The use of aliases to implement syntax-rules was invented by Alan Bawden (who prefers to call them synthetic names). Much of this proposal is derived from an earlier proposal by Alan Bawden.

Syntax-Case Macros

(require 'syntax-case)

Function: macro:expand expression
Function: syncase:expand expression
Returns scheme code with the macros and derived expression types of expression expanded to primitive expression types.

Function: macro:eval expression
Function: syncase:eval expression
macro:eval returns the value of expression in the current top level environment. expression can contain macro definitions. Side effects of expression will affect the top level environment.

Procedure: macro:load filename
Procedure: syncase:load filename
filename should be a string. If filename names an existing file, the macro:load procedure reads Scheme source code expressions and definitions from the file and evaluates them sequentially. These source code expressions and definitions may contain macro definitions. The macro:load procedure does not affect the values returned by current-input-port and current-output-port.

This is version 2.1 of syntax-case, the low-level macro facility proposed and implemented by Robert Hieb and R. Kent Dybvig.

This version is further adapted by Harald Hanche-Olsen <hanche@imf.unit.no> to make it compatible with, and easily usable with, SLIB. Mainly, these adaptations consisted of:

If you wish, you can see exactly what changes were done by reading the shell script in the file `syncase.sh'.

The two PostScript files were omitted in order to not burden the SLIB distribution with them. If you do intend to use syntax-case, however, you should get these files and print them out on a PostScript printer. They are available with the original syntax-case distribution by anonymous FTP in `cs.indiana.edu:/pub/scheme/syntax-case'.

In order to use syntax-case from an interactive top level, execute:

(require 'syntax-case)
(require 'repl)
(repl:top-level macro:eval)

See the section Repl (See section Repl) for more information.

To check operation of syntax-case get `cs.indiana.edu:/pub/scheme/syntax-case', and type

(require 'syntax-case)
(syncase:sanity-check)

Beware that syntax-case takes a long time to load -- about 20s on a SPARCstation SLC (with SCM) and about 90s on a Macintosh SE/30 (with Gambit).

Notes

All R4RS syntactic forms are defined, including delay. Along with delay are simple definitions for make-promise (into which delay expressions expand) and force.

syntax-rules and with-syntax (described in TR356) are defined.

syntax-case is actually defined as a macro that expands into calls to the procedure syntax-dispatch and the core form syntax-lambda; do not redefine these names.

Several other top-level bindings not documented in TR356 are created:

The syntax of define has been extended to allow (define id), which assigns id to some unspecified value.

We have attempted to maintain R4RS compatibility where possible. The incompatibilities should be confined to `hooks.ss'. Please let us know if there is some incompatibility that is not flagged as such.

Send bug reports, comments, suggestions, and questions to Kent Dybvig (dyb@iuvax.cs.indiana.edu).

Note from maintainer

Included with the syntax-case files was `structure.scm' which defines a macro define-structure. There is no documentation for this macro and it is not used by any code in SLIB.

Fluid-Let

(require 'fluid-let)

Syntax: fluid-let (bindings ...) forms...
(fluid-let ((variable init) ...)
   expression expression ...)

The inits are evaluated in the current environment (in some unspecified order), the current values of the variables are saved, the results are assigned to the variables, the expressions are evaluated sequentially in the current environment, the variables are restored to their original values, and the value of the last expression is returned.

The syntax of this special form is similar to that of let, but fluid-let temporarily rebinds existing variables. Unlike let, fluid-let creates no new bindings; instead it assigns the values of each init to the binding (determined by the rules of lexical scoping) of its corresponding variable.

Yasos

(require 'oop) or (require 'yasos)

`Yet Another Scheme Object System' is a simple object system for Scheme based on the paper by Norman Adams and Jonathan Rees: Object Oriented Programming in Scheme, Proceedings of the 1988 ACM Conference on LISP and Functional Programming, July 1988 [ACM #552880].

Another reference is:

Ken Dickey. Scheming with Objects AI Expert Volume 7, Number 10 (October 1992), pp. 24-33.

Terms

Object
Any Scheme data object.
Instance
An instance of the OO system; an object.
Operation
A method.
Notes:
The object system supports multiple inheritance. An instance can inherit from 0 or more ancestors. In the case of multiple inherited operations with the same identity, the operation used is that from the first ancestor which contains it (in the ancestor let). An operation may be applied to any Scheme data object--not just instances. As code which creates instances is just code, there are no classes and no meta-anything. Method dispatch is by a procedure call a la CLOS rather than by send syntax a la Smalltalk.
Disclaimer:
There are a number of optimizations which can be made. This implementation is expository (although performance should be quite reasonable). See the L&FP paper for some suggestions.

Interface

Syntax: define-operation (opname self arg ...) default-body
Defines a default behavior for data objects which don't handle the operation opname. The default default behavior (for an empty default-body) is to generate an error.

Syntax: define-predicate opname?
Defines a predicate opname?, usually used for determining the type of an object, such that (opname? object) returns #t if object has an operation opname? and #f otherwise.

Syntax: object ((name self arg ...) body) ...
Returns an object (an instance of the object system) with operations. Invoking (name object arg ... executes the body of the object with self bound to object and with argument(s) arg....

Syntax: object-with-ancestors ((ancestor1 init1) ...) operation ...
A let-like form of object for multiple inheritance. It returns an object inheriting the behaviour of ancestor1 etc. An operation will be invoked in an ancestor if the object itself does not provide such a method. In the case of multiple inherited operations with the same identity, the operation used is the one found in the first ancestor in the ancestor list.

Syntax: operate-as component operation self arg ...
Used in an operation definition (of self) to invoke the operation in an ancestor component but maintain the object's identity. Also known as "send-to-super".

Procedure: print obj port
A default print operation is provided which is just (format port obj) (See section Format (version 3.0)) for non-instances and prints obj preceded by `#<INSTANCE>' for instances.

Function: size obj
The default method returns the number of elements in obj if it is a vector, string or list, 2 for a pair, 1 for a character and by default id an error otherwise. Objects such as collections (See section Collections) may override the default in an obvious way.

Setters

Setters implement generalized locations for objects associated with some sort of mutable state. A getter operation retrieves a value from a generalized location and the corresponding setter operation stores a value into the location. Only the getter is named -- the setter is specified by a procedure call as below. (Dylan uses special syntax.) Typically, but not necessarily, getters are access operations to extract values from Yasos objects (See section Yasos). Several setters are predefined, corresponding to getters car, cdr, string-ref and vector-ref e.g., (setter car) is equivalent to set-car!.

This implementation of setters is similar to that in Dylan(TM) (Dylan: An object-oriented dynamic language, Apple Computer Eastern Research and Technology). Common LISP provides similar facilities through setf.

Function: setter getter
Returns the setter for the procedure getter. E.g., since string-ref is the getter corresponding to a setter which is actually string-set!:
(define foo "foo")
((setter string-ref) foo 0 #\F) ; set element 0 of foo
foo => "Foo"

Syntax: set place new-value
If place is a variable name, set is equivalent to set!. Otherwise, place must have the form of a procedure call, where the procedure name refers to a getter and the call indicates an accessible generalized location, i.e., the call would return a value. The return value of set is usually unspecified unless used with a setter whose definition guarantees to return a useful value.
(set (string-ref foo 2) #\O)  ; generalized location with getter
foo => "FoO"
(set foo "foo")               ; like set!
foo => "foo"

Procedure: add-setter getter setter
Add procedures getter and setter to the (inaccessible) list of valid setter/getter pairs. setter implements the store operation corresponding to the getter access operation for the relevant state. The return value is unspecified.

Procedure: remove-setter-for getter
Removes the setter corresponding to the specified getter from the list of valid setters. The return value is unspecified.

Syntax: define-access-operation getter-name
Shorthand for a Yasos define-operation defining an operation getter-name that objects may support to return the value of some mutable state. The default operation is to signal an error. The return value is unspecified.

Examples

;;; These definitions for PRINT and SIZE are already supplied by
(require 'yasos)

(define-operation (print obj port)
  (format port
          (if (instance? obj) "#<instance>" "~s")
          obj))

(define-operation (size obj)
  (cond
   ((vector? obj) (vector-length obj))
   ((list?   obj) (length obj))
   ((pair?   obj) 2)
   ((string? obj) (string-length obj))
   ((char?   obj) 1)
   (else
    (error "Operation not supported: size" obj))))

(define-predicate cell?)
(define-operation (fetch obj))
(define-operation (store! obj newValue))

(define (make-cell value)
  (object
   ((cell? self) #t)
   ((fetch self) value)
   ((store! self newValue)
    (set! value newValue)
    newValue)
   ((size self) 1)
   ((print self port)
    (format port "#<Cell: ~s>" (fetch self)))))

(define-operation (discard obj value)
  (format #t "Discarding ~s~%" value))

(define (make-filtered-cell value filter)
  (object-with-ancestors ((cell (make-cell value)))
                         ((store! self newValue)
                          (if (filter newValue)
                              (store! cell newValue)
                              (discard self newValue)))))

(define-predicate array?)
(define-operation (array-ref array index))
(define-operation (array-set! array index value))

(define (make-array num-slots)
  (let ((anArray (make-vector num-slots)))
    (object
     ((array? self) #t)
     ((size self) num-slots)
     ((array-ref self index)           (vector-ref  anArray index))
     ((array-set! self index newValue) (vector-set! anArray index newValue))
     ((print self port) (format port "#<Array ~s>" (size self))))))

(define-operation (position obj))
(define-operation (discarded-value obj))

(define (make-cell-with-history value filter size)
  (let ((pos 0) (most-recent-discard #f))
    (object-with-ancestors
     ((cell (make-filtered-call value filter))
      (sequence (make-array size)))
     ((array? self) #f)
     ((position self) pos)
     ((store! self newValue)
      (operate-as cell store! self newValue)
      (array-set! self pos newValue)
      (set! pos (+ pos 1)))
     ((discard self value)
      (set! most-recent-discard value))
     ((discarded-value self) most-recent-discard)
     ((print self port)
      (format port "#<Cell-with-history ~s>" (fetch self))))))

(define-access-operation fetch)
(add-setter fetch store!)
(define foo (make-cell 1))
(print foo #f)
=> "#<Cell: 1>"
(set (fetch foo) 2)
=>
(print foo #f)
=> "#<Cell: 2>"
(fetch foo)
=> 2

Textual Conversion Packages

Precedence Parsing

(require 'precedence-parse) or (require 'parse)

This package implements:

Precedence Parsing Overview

This package offers improvements over previous parsers.

Here are the higher-level syntax types and an example of each. Precedence considerations are omitted for clarity. See section Grammar Rule Definition for full details.

Grammar: nofix bye exit
bye

calls the function exit with no arguments.

Grammar: prefix - negate
- 42

Calls the function negate with the argument 42.

Grammar: infix - difference
x - y

Calls the function difference with arguments x and y.

Grammar: nary + sum
x + y + z

Calls the function sum with arguments x, y, and y.

Grammar: postfix ! factorial
5 !

Calls the function factorial with the argument 5.

Grammar: prestfix set set!
set foo bar

Calls the function set! with the arguments foo and bar.

Grammar: commentfix /* */
/* almost any text here */

Ignores the comment delimited by /* and */.

Grammar: matchfix { list }
{0, 1, 2}

Calls the function list with the arguments 0, 1, and 2.

Grammar: inmatchfix ( funcall )
f(x, y)

Calls the function funcall with the arguments f, x, and y.

Grammar: delim ;
set foo bar;

delimits the extent of the restfix operator set.

Ruleset Definition and Use

Variable: *syn-defs*
A grammar is built by one or more calls to prec:define-grammar. The rules are appended to *syn-defs*. The value of *syn-defs* is the grammar suitable for passing as an argument to prec:parse.

Constant: *syn-ignore-whitespace*
Is a nearly empty grammar with whitespace characters set to group 0, which means they will not be made into tokens. Most rulesets will want to start with *syn-ignore-whitespace*

In order to start defining a grammar, either

(set! *syn-defs* '())

or

(set! *syn-defs* *syn-ignore-whitespace*)

Function: prec:define-grammar rule1 ...
Appends rule1 ... to *syn-defs*. prec:define-grammar is used to define both the character classes and rules for tokens.

Once your grammar is defined, save the value of *syn-defs* in a variable (for use when calling prec:parse).

(define my-ruleset *syn-defs*)

Function: prec:parse ruleset delim
Function: prec:parse ruleset delim port
The ruleset argument must be a list of rules as constructed by prec:define-grammar and extracted from *syn-defs*.

The token delim may be a character, symbol, or string. A character delim argument will match only a character token; i.e. a character for which no token-group is assigned. A symbols or string will match only a token string; i.e. a token resulting from a token group.

prec:parse reads a ruleset grammar expression delimited by delim from the given input port. prec:parse returns the next object parsable from the given input port, updating port to point to the first character past the end of the external representation of the object.

If an end of file is encountered in the input before any characters are found that can begin an object, then an end of file object is returned. If a delimiter (such as delim) is found before any characters are found that can begin an object, then #f is returned.

The port argument may be omitted, in which case it defaults to the value returned by current-input-port. It is an error to parse from a closed port.

Token definition

Function: tok:char-group group chars chars-proc
The argument chars may be a single character, a list of characters, or a string. Each character in chars is treated as though tok:char-group was called with that character alone.

The argument chars-proc must be a procedure of one argument, a list of characters. After tokenize has finished accumulating the characters for a token, it calls chars-proc with the list of characters. The value returned is the token which tokenize returns.

The argument group may be an exact integer or a procedure of one character argument. The following discussion concerns the treatment which the tokenizing routine, tokenize, will accord to characters on the basis of their groups.

When group is a non-zero integer, characters whose group number is equal to or exactly one less than group will continue to accumulate. Any other character causes the accumulation to stop (until a new token is to be read).

The group of zero is special. These characters are ignored when parsed pending a token, and stop the accumulation of token characters when the accumulation has already begun. Whitespace characters are usually put in group 0.

If group is a procedure, then, when triggerd by the occurence of an initial (no accumulation) chars character, this procedure will be repeatedly called with each successive character from the input stream until the group procedure returns a non-false value.

The following convenient constants are provided for use with tok:char-group.

Constant: tok:decimal-digits
Is the string "0123456789".
Constant: tok:upper-case
Is the string consisting of all upper-case letters ("ABCDEFGHIJKLMNOPQRSTUVWXYZ").
Constant: tok:lower-case
Is the string consisting of all lower-case letters ("abcdefghijklmnopqrstuvwxyz").
Constant: tok:whitespaces
Is the string consisting of all characters between 0 and 255 for which char-whitespace? returns true.

Nud and Led Definition

This section describes advanced features. You can skip this section on first reading.

The Null Denotation (or nud) of a token is the procedure and arguments applying for that token when Left, an unclaimed parsed expression is not extant.

The Left Denotation (or led) of a token is the procedure, arguments, and lbp applying for that token when there is a Left, an unclaimed parsed expression.

In his paper,

Pratt, V. R. Top Down Operator Precendence. SIGACT/SIGPLAN Symposium on Principles of Programming Languages, Boston, 1973, pages 41-51

the left binding power (or lbp) was an independent property of tokens. I think this was done in order to allow tokens with NUDs but not LEDs to also be used as delimiters, which was a problem for statically defined syntaxes. It turns out that dynamically binding NUDs and LEDs allows them independence.

For the rule-defining procedures that follow, the variable tk may be a character, string, or symbol, or a list composed of characters, strings, and symbols. Each element of tk is treated as though the procedure were called for each element.

Character tk arguments will match only character tokens; i.e. characters for which no token-group is assigned. Symbols and strings will both match token strings; i.e. tokens resulting from token groups.

Function: prec:make-nud tk sop arg1 ...
Returns a rule specifying that sop be called when tk is parsed. If sop is a procedure, it is called with tk and arg1 ... as its arguments; the resulting value is incorporated into the expression being built. Otherwise, (list sop arg1 ...) is incorporated.

If no NUD has been defined for a token; then if that token is a string, it is converted to a symbol and returned; if not a string, the token is returned.

Function: prec:make-led tk sop arg1 ...
Returns a rule specifying that sop be called when tk is parsed and left has an unclaimed parsed expression. If sop is a procedure, it is called with left, tk, and arg1 ... as its arguments; the resulting value is incorporated into the expression being built. Otherwise, left is incorporated.

If no LED has been defined for a token, and left is set, the parser issues a warning.

Grammar Rule Definition

Here are procedures for defining rules for the syntax types introduced in section Precedence Parsing Overview.

For the rule-defining procedures that follow, the variable tk may be a character, string, or symbol, or a list composed of characters, strings, and symbols. Each element of tk is treated as though the procedure were called for each element.

For procedures prec:delim, ..., prec:prestfix, if the sop argument is #f, then the token which triggered this rule is converted to a symbol and returned. A false sop argument to the procedures prec:commentfix, prec:matchfix, or prec:inmatchfix has a different meaning.

Character tk arguments will match only character tokens; i.e. characters for which no token-group is assigned. Symbols and strings will both match token strings; i.e. tokens resulting from token groups.

Function: prec:delim tk
Returns a rule specifying that tk should not be returned from parsing; i.e. tk's function is purely syntactic. The end-of-file is always treated as a delimiter.

Function: prec:nofix tk sop
Returns a rule specifying the following actions take place when tk is parsed:

Function: prec:prefix tk sop bp rule1 ...
Returns a rule specifying the following actions take place when tk is parsed:

Function: prec:infix tk sop lbp bp rule1 ...
Returns a rule declaring the left-binding-precedence of the token tk is lbp and specifying the following actions take place when tk is parsed:

Function: prec:nary tk sop bp
Returns a rule declaring the left-binding-precedence of the token tk is bp and specifying the following actions take place when tk is parsed:

Function: prec:postfix tk sop lbp
Returns a rule declaring the left-binding-precedence of the token tk is lbp and specifying the following actions take place when tk is parsed:

Function: prec:prestfix tk sop bp rule1 ...
Returns a rule specifying the following actions take place when tk is parsed:

Function: prec:commentfix tk stp match rule1 ...
Returns rules specifying the following actions take place when tk is parsed:

Parsing of commentfix syntax differs from the others in several ways. It reads directly from input without tokenizing; It calls stp but does not return its value; nay any value. I added the stp argument so that comment text could be echoed.

Function: prec:matchfix tk sop sep match rule1 ...
Returns a rule specifying the following actions take place when tk is parsed:

Function: prec:inmatchfix tk sop sep match lbp rule1 ...
Returns a rule declaring the left-binding-precedence of the token tk is lbp and specifying the following actions take place when tk is parsed:

Format (version 3.0)

(require 'format)

Format Interface

Function: format destination format-string . arguments
An almost complete implementation of Common LISP format description according to the CL reference book Common LISP from Guy L. Steele, Digital Press. Backward compatible to most of the available Scheme format implementations.

Returns #t, #f or a string; has side effect of printing according to format-string. If destination is #t, the output is to the current output port and #t is returned. If destination is #f, a formatted string is returned as the result of the call. NEW: If destination is a string, destination is regarded as the format string; format-string is then the first argument and the output is returned as a string. If destination is a number, the output is to the current error port if available by the implementation. Otherwise destination must be an output port and #t is returned.

format-string must be a string. In case of a formatting error format returns #f and prints a message on the current output or error port. Characters are output as if the string were output by the display function with the exception of those prefixed by a tilde (~). For a detailed description of the format-string syntax please consult a Common LISP format reference manual. For a test suite to verify this format implementation load `formatst.scm'. Please send bug reports to lutzeb@cs.tu-berlin.de.

Note: format is not reentrant, i.e. only one format-call may be executed at a time.

Format Specification (Format version 3.0)

Please consult a Common LISP format reference manual for a detailed description of the format string syntax. For a demonstration of the implemented directives see `formatst.scm'.

This implementation supports directive parameters and modifiers (: and @ characters). Multiple parameters must be separated by a comma (,). Parameters can be numerical parameters (positive or negative), character parameters (prefixed by a quote character ('), variable parameters (v), number of rest arguments parameter (#), empty and default parameters. Directive characters are case independent. The general form of a directive is:

directive ::= ~{directive-parameter,}[:][@]directive-character

directive-parameter ::= [ [-|+]{0-9}+ | 'character | v | # ]

Implemented CL Format Control Directives

Documentation syntax: Uppercase characters represent the corresponding control directive characters. Lowercase characters represent control directive parameter descriptions.

~A
Any (print as display does).
~@A
left pad.
~mincol,colinc,minpad,padcharA
full padding.
~S
S-expression (print as write does).
~@S
left pad.
~mincol,colinc,minpad,padcharS
full padding.
~D
Decimal.
~@D
print number sign always.
~:D
print comma separated.
~mincol,padchar,commacharD
padding.
~X
Hexadecimal.
~@X
print number sign always.
~:X
print comma separated.
~mincol,padchar,commacharX
padding.
~O
Octal.
~@O
print number sign always.
~:O
print comma separated.
~mincol,padchar,commacharO
padding.
~B
Binary.
~@B
print number sign always.
~:B
print comma separated.
~mincol,padchar,commacharB
padding.
~nR
Radix n.
~n,mincol,padchar,commacharR
padding.
~@R
print a number as a Roman numeral.
~:R
print a number as an ordinal English number.
~:@R
print a number as a cardinal English number.
~P
Plural.
~@P
prints y and ies.
~:P
as ~P but jumps 1 argument backward.
~:@P
as ~@P but jumps 1 argument backward.
~C
Character.
~@C
prints a character as the reader can understand it (i.e. #\ prefixing).
~:C
prints a character as emacs does (eg. ^C for ASCII 03).
~F
Fixed-format floating-point (prints a flonum like mmm.nnn).
~width,digits,scale,overflowchar,padcharF
~@F
If the number is positive a plus sign is printed.
~E
Exponential floating-point (prints a flonum like mmm.nnnEee).
~width,digits,exponentdigits,scale,overflowchar,padchar,exponentcharE
~@E
If the number is positive a plus sign is printed.
~G
General floating-point (prints a flonum either fixed or exponential).
~width,digits,exponentdigits,scale,overflowchar,padchar,exponentcharG
~@G
If the number is positive a plus sign is printed.
~$
Dollars floating-point (prints a flonum in fixed with signs separated).
~digits,scale,width,padchar$
~@$
If the number is positive a plus sign is printed.
~:@$
A sign is always printed and appears before the padding.
~:$
The sign appears before the padding.
~%
Newline.
~n%
print n newlines.
~&
print newline if not at the beginning of the output line.
~n&
prints ~& and then n-1 newlines.
~|
Page Separator.
~n|
print n page separators.
~~
Tilde.
~n~
print n tildes.
~<newline>
Continuation Line.
~:<newline>
newline is ignored, white space left.
~@<newline>
newline is left, white space ignored.
~T
Tabulation.
~@T
relative tabulation.
~colnum,colincT
full tabulation.
~?
Indirection (expects indirect arguments as a list).
~@?
extracts indirect arguments from format arguments.
~(str~)
Case conversion (converts by string-downcase).
~:(str~)
converts by string-capitalize.
~@(str~)
converts by string-capitalize-first.
~:@(str~)
converts by string-upcase.
~*
Argument Jumping (jumps 1 argument forward).
~n*
jumps n arguments forward.
~:*
jumps 1 argument backward.
~n:*
jumps n arguments backward.
~@*
jumps to the 0th argument.
~n@*
jumps to the nth argument (beginning from 0)
~[str0~;str1~;...~;strn~]
Conditional Expression (numerical clause conditional).
~n[
take argument from n.
~@[
true test conditional.
~:[
if-else-then conditional.
~;
clause separator.
~:;
default clause follows.
~{str~}
Iteration (args come from the next argument (a list)).
~n{
at most n iterations.
~:{
args from next arg (a list of lists).
~@{
args from the rest of arguments.
~:@{
args from the rest args (lists).
~^
Up and out.
~n^
aborts if n = 0
~n,m^
aborts if n = m
~n,m,k^
aborts if n <= m <= k

Not Implemented CL Format Control Directives

~:A
print #f as an empty list (see below).
~:S
print #f as an empty list (see below).
~<~>
Justification.
~:^
(sorry I don't understand its semantics completely)

Extended, Replaced and Additional Control Directives

~mincol,padchar,commachar,commawidthD
~mincol,padchar,commachar,commawidthX
~mincol,padchar,commachar,commawidthO
~mincol,padchar,commachar,commawidthB
~n,mincol,padchar,commachar,commawidthR
commawidth is the number of characters between two comma characters.
~I
print a R4RS complex number as ~F~@Fi with passed parameters for ~F.
~Y
Pretty print formatting of an argument for scheme code lists.
~K
Same as ~?.
~!
Flushes the output if format destination is a port.
~_
Print a #\space character
~n_
print n #\space characters.
~/
Print a #\tab character
~n/
print n #\tab characters.
~nC
Takes n as an integer representation for a character. No arguments are consumed. n is converted to a character by integer->char. n must be a positive decimal number.
~:S
Print out readproof. Prints out internal objects represented as #<...> as strings "#<...>" so that the format output can always be processed by read.
~:A
Print out readproof. Prints out internal objects represented as #<...> as strings "#<...>" so that the format output can always be processed by read.
~Q
Prints information and a copyright notice on the format implementation.
~:Q
prints format version.
~F, ~E, ~G, ~$
may also print number strings, i.e. passing a number as a string and format it accordingly.

Configuration Variables

Format has some configuration variables at the beginning of `format.scm' to suit the systems and users needs. There should be no modification necessary for the configuration that comes with SLIB. If modification is desired the variable should be set after the format code is loaded. Format detects automatically if the running scheme system implements floating point numbers and complex numbers.

format:symbol-case-conv
Symbols are converted by symbol->string so the case type of the printed symbols is implementation dependent. format:symbol-case-conv is a one arg closure which is either #f (no conversion), string-upcase, string-downcase or string-capitalize. (default #f)
format:iobj-case-conv
As format:symbol-case-conv but applies for the representation of implementation internal objects. (default #f)
format:expch
The character prefixing the exponent value in ~E printing. (default #\E)

Compatibility With Other Format Implementations

SLIB format 2.x:
See `format.doc'.
SLIB format 1.4:
Downward compatible except for padding support and ~A, ~S, ~P, ~X uppercase printing. SLIB format 1.4 uses C-style printf padding support which is completely replaced by the CL format padding style.
MIT C-Scheme 7.1:
Downward compatible except for ~, which is not documented (ignores all characters inside the format string up to a newline character). (7.1 implements ~a, ~s, ~newline, ~~, ~%, numerical and variable parameters and :/@ modifiers in the CL sense).
Elk 1.5/2.0:
Downward compatible except for ~A and ~S which print in uppercase. (Elk implements ~a, ~s, ~~, and ~% (no directive parameters or modifiers)).
Scheme->C 01nov91:
Downward compatible except for an optional destination parameter: S2C accepts a format call without a destination which returns a formatted string. This is equivalent to a #f destination in S2C. (S2C implements ~a, ~s, ~c, ~%, and ~~ (no directive parameters or modifiers)).

This implementation of format is solely useful in the SLIB context because it requires other components provided by SLIB.

Standard Formatted I/O

stdio

(require 'stdio)

requires printf and scanf and additionally defines the symbols:

Variable: stdin
Defined to be (current-input-port).
Variable: stdout
Defined to be (current-output-port).
Variable: stderr
Defined to be (current-error-port).

Standard Formatted Output

(require 'printf)

Procedure: printf format arg1 ...
Procedure: fprintf port format arg1 ...
Procedure: sprintf str format arg1 ...

Each function converts, formats, and outputs its arg1 ... arguments according to the control string format argument and returns the number of characters output.

printf sends its output to the port (current-output-port). fprintf sends its output to the port port. sprintf string-set!s locations of the non-constant string argument str to the output characters.

Note: sprintf should be changed to a macro so a substring expression could be used for the str argument.

The string format contains plain characters which are copied to the output stream, and conversion specifications, each of which results in fetching zero or more of the arguments arg1 .... The results are undefined if there are an insufficient number of arguments for the format. If format is exhausted while some of the arg1 ... arguments remain unused, the excess arg1 ... arguments are ignored.

The conversion specifications in a format string have the form:

% [ flags ] [ width ] [ . precision ] [ type ] conversion

An output conversion specifications consist of an initial `%' character followed in sequence by:

Exact Conversions

`d', `i'
Print an integer as a signed decimal number. `%d' and `%i' are synonymous for output, but are different when used with scanf for input (see section Standard Formatted Input).
`o'
Print an integer as an unsigned octal number.
`u'
Print an integer as an unsigned decimal number.
`x', `X'
Print an integer as an unsigned hexadecimal number. `%x' prints using the digits `0123456789abcdef'. `%X' prints using the digits `0123456789ABCDEF'.

Inexact Conversions

Note: Inexact conversions are not supported yet.

`f'
Print a floating-point number in fixed-point notation.
`e', `E'
Print a floating-point number in exponential notation. `%e' prints `e' between mantissa and exponont. `%E' prints `E' between mantissa and exponont.
`g', `G'
Print a floating-point number in either normal or exponential notation, whichever is more appropriate for its magnitude. `%g' prints `e' between mantissa and exponont. `%G' prints `E' between mantissa and exponont.

Other Conversions

`c'
Print a single character. The `-' flag is the only one which can be specified. It is an error to specify a precision.
`s'
Print a string. The `-' flag is the only one which can be specified. A precision specifies the maximum number of characters to output; otherwise all characters in the string are output.
`a', `A'
Print a scheme expression. The `-' flag left-justifies the output. The `#' flag specifies that strings and characters should be quoted as by write (which can be read using read); otherwise, output is as display prints. A precision specifies the maximum number of characters to output; otherwise as many characters as needed are output. Note: `%a' and `%A' are SLIB extensions.
`%'
Print a literal `%' character. No argument is consumed. It is an error to specifiy flags, field width, precision, or type modifiers with `%%'.

Standard Formatted Input

(require 'scanf)

Function: scanf-read-list format
Function: scanf-read-list format port
Function: scanf-read-list format string

Macro: scanf format arg1 ...
Macro: fscanf port format arg1 ...
Macro: sscanf str format arg1 ...

Each function reads characters, interpreting them according to the control string format argument.

scanf-read-list returns a list of the items specified as far as the input matches format. scanf, fscanf, and sscanf return the number of items successfully matched and stored. scanf, fscanf, and sscanf also set the location corresponding to arg1 ... using the methods:

symbol
set!
car expression
set-car!
cdr expression
set-cdr!
vector-ref expression
vector-set!
substring expression
substring-move-left!

The argument to a substring expression in arg1 ... must be a non-constant string. Characters will be stored starting at the position specified by the second argument to substring. The number of characters stored will be limited by either the position specified by the third argument to substring or the length of the matched string, whichever is less.

The control string, format, contains conversion specifications and other characters used to direct interpretation of input sequences. The control string contains:

Unless the specification contains the `n' conversion character (described below), a conversion specification directs the conversion of the next input field. The result of a conversion specification is returned in the position of the corresponding argument points, unless `*' indicates assignment suppression. Assignment suppression provides a way to describe an input field to be skipped. An input field is defined as a string of characters; it extends to the next inappropriate character or until the field width, if specified, is exhausted.

Note: This specification of format strings differs from the ANSI C and POSIX specifications. In SLIB, white space before an input field is not skipped unless white space appears before the conversion specification in the format string. In order to write format strings which work identically with ANSI C and SLIB, prepend whitespace to all conversion specifications except `[' and `c'.

The conversion code indicates the interpretation of the input field; For a suppressed field, no value is returned. The following conversion codes are legal:

`%'
A single % is expected in the input at this point; no value is returned.
`d', `D'
A decimal integer is expected.
`u', `U'
An unsigned decimal integer is expected.
`o', `O'
An octal integer is expected.
`x', `X'
A hexadecimal integer is expected.
`i'
An integer is expected. Returns the value of the next input item, interpreted according to C conventions; a leading `0' implies octal, a leading `0x' implies hexadecimal; otherwise, decimal is assumed.
`n'
Returns the total number of bytes (including white space) read by scanf. No input is consumed by %n.
`f', `F', `e', `E', `g', `G'
A floating-point number is expected. The input format for floating-point numbers is an optionally signed string of digits, possibly containing a radix character `.', followed by an optional exponent field consisting of an `E' or an `e', followed by an optional `+', `-', or space, followed by an integer.
`c', `C'
Width characters are expected. The normal skip-over-white-space is suppressed in this case; to read the next non-space character, use `%1s'. If a field width is given, a string is returned; up to the indicated number of characters is read.
`s', `S'
A character string is expected The input field is terminated by a white-space character. scanf cannot read a null string.
`['
Indicates string data and the normal skip-over-leading-white-space is suppressed. The left bracket is followed by a set of characters, called the scanset, and a right bracket; the input field is the maximal sequence of input characters consisting entirely of characters in the scanset. `^', when it appears as the first character in the scanset, serves as a complement operator and redefines the scanset as the set of all characters not contained in the remainder of the scanset string. Construction of the scanset follows certain conventions. A range of characters may be represented by the construct first-last, enabling `[0123456789]' to be expressed `[0-9]'. Using this convention, first must be lexically less than or equal to last; otherwise, the dash stands for itself. The dash also stands for itself when it is the first or the last character in the scanset. To include the right square bracket as an element of the scanset, it must appear as the first character (possibly preceded by a `^') of the scanset, in which case it will not be interpreted syntactically as the closing bracket. At least one character must match for this conversion to succeed.

The scanf functions terminate their conversions at end-of-file, at the end of the control string, or when an input character conflicts with the control string. In the latter case, the offending character is left unread in the input stream.

Program Arguments

Getopt

(require 'getopt)

This routine implements Posix command line argument parsing. Notice that returning values through global variables means that getopt is not reentrant.

Variable: *optind*
Is the index of the current element of the command line. It is initially one. In order to parse a new command line or reparse an old one, *opting* must be reset.

Variable: *optarg*
Is set by getopt to the (string) option-argument of the current option.

Procedure: getopt argc argv optstring
Returns the next option letter in argv (starting from (vector-ref argv *optind*)) that matches a letter in optstring. argv is a vector or list of strings, the 0th of which getopt usually ignores. argc is the argument count, usually the length of argv. optstring is a string of recognized option characters; if a character is followed by a colon, the option takes an argument which may be immediately following it in the string or in the next element of argv.

*optind* is the index of the next element of the argv vector to be processed. It is initialized to 1 by `getopt.scm', and getopt updates it when it finishes with each element of argv.

getopt returns the next option character from argv that matches a character in optstring, if there is one that matches. If the option takes an argument, getopt sets the variable *optarg* to the option-argument as follows:

If, when getopt is called, the string (vector-ref argv *optind*) either does not begin with the character #\- or is just "-", getopt returns #f without changing *optind*. If (vector-ref argv *optind*) is the string "--", getopt returns #f after incrementing *optind*.

If getopt encounters an option character that is not contained in optstring, it returns the question-mark #\? character. If it detects a missing option argument, it returns the colon character #\: if the first character of optstring was a colon, or a question-mark character otherwise. In either case, getopt sets the variable getopt:opt to the option character that caused the error.

The special option "--" can be used to delimit the end of the options; #f is returned, and "--" is skipped.

RETURN VALUE

getopt returns the next option character specified on the command line. A colon #\: is returned if getopt detects a missing argument and the first character of optstring was a colon #\:.

A question-mark #\? is returned if getopt encounters an option character not in optstring or detects a missing argument and the first character of optstring was not a colon #\:.

Otherwise, getopt returns #f when all command line options have been parsed.

Example:

#! /usr/local/bin/scm
;;;This code is SCM specific.
(define argv (program-arguments))
(require 'getopt)

(define opts ":a:b:cd")
(let loop ((opt (getopt (length argv) argv opts)))
  (case opt
    ((#\a) (print "option a: " *optarg*))
    ((#\b) (print "option b: " *optarg*))
    ((#\c) (print "option c"))
    ((#\d) (print "option d"))
    ((#\?) (print "error" getopt:opt))
    ((#\:) (print "missing arg" getopt:opt))
    ((#f) (if (< *optind* (length argv))
              (print "argv[" *optind* "]="
                     (list-ref argv *optind*)))
          (set! *optind* (+ *optind* 1))))
  (if (< *optind* (length argv))
      (loop (getopt (length argv) argv opts))))

(slib:exit)

Getopt--

Function: getopt-- argc argv optstring
The procedure getopt-- is an extended version of getopt which parses long option names of the form `--hold-the-onions' and `--verbosity-level=extreme'. Getopt-- behaves as getopt except for non-empty options beginning with `--'.

Options beginning with `--' are returned as strings rather than characters. If a value is assigned (using `=') to a long option, *optarg* is set to the value. The `=' and value are not returned as part of the option string.

No information is passed to getopt-- concerning which long options should be accepted or whether such options can take arguments. If a long option did not have an argument, *optarg will be set to #f. The caller is responsible for detecting and reporting errors.

(define opts ":-:b:")
(define argc 5)
(define argv '("foo" "-b9" "--f1" "--2=" "--g3=35234.342" "--"))
(define *optind* 1)
(define *optarg* #f)
(require 'qp)
(do ((i 5 (+ -1 i)))
    ((zero? i))
  (define opt (getopt-- argc argv opts))
  (print *optind* opt *optarg*)))
-|
2 #\b "9" 
3 "f1" #f 
4 "2" "" 
5 "g3" "35234.342" 
5 #f "35234.342" 

Command Line

(require 'read-command)

Function: read-command port
Function: read-command
read-command converts a command line into a list of strings suitable for parsing by getopt. The syntax of command lines supported resembles that of popular shells. read-command updates port to point to the first character past the command delimiter.

If an end of file is encountered in the input before any characters are found that can begin an object or comment, then an end of file object is returned.

The port argument may be omitted, in which case it defaults to the value returned by current-input-port.

The fields into which the command line is split are delimited by whitespace as defined by char-whitespace?. The end of a command is delimited by end-of-file or unescaped semicolon (;) or newline. Any character can be literally included in a field by escaping it with a backslach (\).

The initial character and types of fields recognized are:

`\'
The next character has is taken literally and not interpreted as a field delimiter. If \ is the last character before a newline, that newline is just ignored. Processing continues from the characters after the newline as though the backslash and newline were not there.
`"'
The characters up to the next unescaped " are taken literally, according to [R4RS] rules for literal strings (see section `Strings' in Revised(4) Scheme).
`(', `%''
One scheme expression is read starting with this character. The read expression is evaluated, converted to a string (using display), and replaces the expression in the returned field.
`;'
Semicolon delimits a command. Using semicolons more than one command can appear on a line. Escaped semicolons and semicolons inside strings do not delimit commands.

The comment field differs from the previous fields in that it must be the first character of a command or appear after whitespace in order to be recognized. # can be part of fields if these conditions are not met. For instance, ab#c is just the field ab#c.

`#'
Introduces a comment. The comment continues to the end of the line on which the semicolon appears. Comments are treated as whitespace by read-dommand-line and backslashes before newlines in comments are also ignored.

Function: read-options-file filename
read-options-file converts an options file into a list of strings suitable for parsing by getopt. The syntax of options files is the same as the syntax for command lines, except that newlines do not terminate reading (only ; or end of file).

If an end of file is encountered before any characters are found that can begin an object or comment, then an end of file object is returned.

Parameter lists

(require 'parameters)

Arguments to procedures in scheme are distinguished from each other by their position in the procedure call. This can be confusing when a procedure takes many arguments, many of which are not often used.

A parameter-list is a way of passing named information to a procedure. Procedures are also defined to set unused parameters to default values, check parameters, and combine parameter lists.

A parameter has the form (parameter-name value1 ...). This format allows for more than one value per parameter-name.

A parameter-list is a list of parameters, each with a different parameter-name.

Function: make-parameter-list parameter-names
Returns an empty parameter-list with slots for parameter-names.

Function: parameter-list-ref parameter-list parameter-name
parameter-name must name a valid slot of parameter-list. parameter-list-ref returns the value of parameter parameter-name of parameter-list.

Procedure: adjoin-parameters! parameter-list parameter1 ...
Returns parameter-list with parameter1 ... merged in.

Procedure: parameter-list-expand expanders parameter-list
expanders is a list of procedures whose order matches the order of the parameter-names in the call to make-parameter-list which created parameter-list. For each non-false element of expanders that procedure is mapped over the corresponding parameter value and the returned parameter lists are merged into parameter-list.

This process is repeated until parameter-list stops growing. The value returned from parameter-list-expand is unspecified.

Function: fill-empty-parameters defaulters parameter-list
defaulters is a list of procedures whose order matches the order of the parameter-names in the call to make-parameter-list which created parameter-list. fill-empty-parameters returns a new parameter-list with each empty parameter replaced with the list returned by calling the corresponding defaulter with parameter-list as its argument.

Function: check-parameters checks parameter-list
checks is a list of procedures whose order matches the order of the parameter-names in the call to make-parameter-list which created parameter-list.

check-parameters returns parameter-list if each check of the corresponding parameter-list returns non-false. If some check returns #f an error is signaled.

In the following procedures arities is a list of symbols. The elements of arities can be:

single
Requires a single parameter.
optional
A single parameter or no parameter is acceptable.
boolean
A single boolean parameter or zero parameters is acceptable.
nary
Any number of parameters are acceptable.
nary1
One or more of parameters are acceptable.

Function: parameter-list->arglist positions arities types parameter-list
Returns parameter-list converted to an argument list. Parameters of arity type single and boolean are converted to the single value associated with them. The other arity types are converted to lists of the value(s) of type types.

positions is a list of positive integers whose order matches the order of the parameter-names in the call to make-parameter-list which created parameter-list. The integers specify in which argument position the corresponding parameter should appear.

Function: getopt->parameter-list argc argv optnames arities types aliases
Returns argv converted to a parameter-list. optnames are the parameter-names. aliases is a list of lists of strings and elements of optnames. Each of these strings which have length of 1 will be treated as a single - option by getopt. Longer strings will be treated as long-named options (see section Getopt).

Function: getopt->arglist argc argv optnames positions arities types defaulters checks aliases
Like getopt->parameter-list, but converts argv to an argument-list as specified by optnames, positions, arities, types, defaulters, checks, and aliases.

These getopt functions can be used with SLIB relational databases. For an example, See section Database Utilities.

If errors are encountered while processing options, directions for using the options are printed to current-error-port.

(begin
  (set! *optind* 1)
  (getopt->parameter-list
   2
   '("cmd" "-?")
   '(flag number symbols symbols string flag2 flag3 num2 num3)
   '(boolean optional nary1 nary single boolean boolean nary nary)
   '(boolean integer symbol symbol string boolean boolean integer integer)
   '(("flag" flag)
     ("f" flag)
     ("Flag" flag2)
     ("B" flag3)
     ("optional" number)
     ("o" number)
     ("nary1" symbols)
     ("N" symbols)
     ("nary" symbols)
     ("n" symbols)
     ("single" string)
     ("s" string)
     ("a" num2)
     ("Abs" num3))))
-|
Usage: cmd [OPTION ARGUMENT ...] ...

  -f, --flag 
  -o, --optional=<number> 
  -n, --nary=<symbols> ...
  -N, --nary1=<symbols> ...
  -s, --single=<string> 
      --Flag 
  -B        
  -a        <num2> ...
      --Abs=<num3> ...

ERROR: getopt->parameter-list "unrecognized option" "-?"

Batch

(require 'batch)

The batch procedures provide a way to write and execute portable scripts for a variety of operating systems. Each batch: procedure takes as its first argument a parameter-list (see section Parameter lists). This parameter-list argument parms contains named associations. Batch currently uses 2 of these:

batch-port
The port on which to write lines of the batch file.
batch-dialect
The syntax of batch file to generate. Currently supported are:

`batch.scm' uses 2 enhanced relational tables (see section Database Utilities) to store information linking the names of operating-systems to batch-dialectes.

Function: batch:initialize! database
Defines operating-system and batch-dialect tables and adds the domain operating-system to the enhanced relational database database.

Variable: batch:platform
Is batch's best guess as to which operating-system it is running under. batch:platform is set to (software-type) (see section Configuration) unless (software-type) is unix, in which case finer distinctions are made.

Function: batch:call-with-output-script parms file proc
proc should be a procedure of one argument. If file is an output-port, batch:call-with-output-script writes an appropriate header to file and then calls proc with file as the only argument. If file is a string, batch:call-with-output-script opens a output-file of name file, writes an appropriate header to file, and then calls proc with the newly opened port as the only argument. Otherwise, batch:call-with-output-script acts as if it was called with the result of (current-output-port) as its third argument.

Function: batch:apply-chop-to-fit proc arg1 arg2 ... list
The procedure proc must accept at least one argument and return #t if successful, #f if not. batch:apply-chop-to-fit calls proc with arg1, arg2, ..., and chunk, where chunk is a subset of list. batch:apply-chop-to-fit tries proc with successively smaller subsets of list until either proc returns non-false, or the chunks become empty.

The rest of the batch: procedures write (or execute if batch-dialect is system) commands to the batch port which has been added to parms or (copy-tree parms) by the code:

(adjoin-parameters! parms (list 'batch-port port))

Function: batch:system parms string1 string2 ...
Calls batch:try-system (below) with arguments, but signals an error if batch:try-system returns #f.

These functions return a non-false value if the command was successfully translated into the batch dialect and #f if not. In the case of the system dialect, the value is non-false if the operation suceeded.

Function: batch:try-system parms string1 string2 ...
Writes a command to the batch-port in parms which executes the program named string1 with arguments string2 ....

Function: batch:run-script parms string1 string2 ...
Writes a command to the batch-port in parms which executes the batch script named string1 with arguments string2 ....

Note: batch:run-script and batch:try-system are not the same for some operating systems (VMS).

Function: batch:comment parms line1 ...
Writes comment lines line1 ... to the batch-port in parms.

Function: batch:lines->file parms file line1 ...
Writes commands to the batch-port in parms which create a file named file with contents line1 ....

Function: batch:delete-file parms file
Writes a command to the batch-port in parms which deletes the file named file.

Function: batch:rename-file parms old-name new-name
Writes a command to the batch-port in parms which renames the file old-name to new-name.

In addition, batch provides some small utilities very useful for writing scripts:

Function: truncate-up-to path char
Function: truncate-up-to path string
Function: truncate-up-to path charlist
path can be a string or a list of strings. Returns path sans any prefixes ending with a character of the second argument. This can be used to derive a filename moved locally from elsewhere.

(truncate-up-to "/usr/local/lib/slib/batch.scm" "/")
=> "batch.scm"

Function: replace-suffix str old new
str can be a string or a list of strings. Returns a new string (or strings) similar to str but with the suffix string old removed and the suffix string new appended. If the end of str does not match old, an error is signaled.

(replace-suffix "/usr/local/lib/slib/batch.scm" ".scm" ".c")
=> "/usr/local/lib/slib/batch.c"

Function: string-join joiner string1 ...
Returns a new string consisting of all the strings string1 ... in order appended together with the string joiner between each adjacent pair.

Function: must-be-first list1 list2
Returns a new list consisting of the elements of list2 ordered so that if some elements of list1 are equal? to elements of list2, then those elements will appear first and in the order of list1.

Function: must-be-last list1 list2
Returns a new list consisting of the elements of list1 ordered so that if some elements of list2 are equal? to elements of list1, then those elements will appear last and in the order of list2.

Function: os->batch-dialect osname
Returns its best guess for the batch-dialect to be used for the operating-system named osname. os->batch-dialect uses the tables added to database by batch:initialize!.

Here is an example of the use of most of batch's procedures:

(require 'database-utilities)
(require 'parameters)
(require 'batch)

(define batch (create-database #f 'alist-table))
(batch:initialize! batch)

(define my-parameters
  (list (list 'batch-dialect (os->batch-dialect batch:platform))
        (list 'platform batch:platform)
        (list 'batch-port (current-output-port)))) ;gets filled in later

(batch:call-with-output-script
 my-parameters
 "my-batch"
 (lambda (batch-port)
   (adjoin-parameters! my-parameters (list 'batch-port batch-port))
   (and
    (batch:comment my-parameters
                   "================ Write file with C program.")
    (batch:rename-file my-parameters "hello.c" "hello.c~")
    (batch:lines->file my-parameters "hello.c"
                       "#include <stdio.h>"
                       "int main(int argc, char **argv)"
                       "{"
                       "  printf(\"hello world\\n\");"
                       "  return 0;"
                       "}" )
    (batch:system my-parameters "cc" "-c" "hello.c")
    (batch:system my-parameters "cc" "-o" "hello"
                  (replace-suffix "hello.c" ".c" ".o"))
    (batch:system my-parameters "hello")
    (batch:delete-file my-parameters "hello")
    (batch:delete-file my-parameters "hello.c")
    (batch:delete-file my-parameters "hello.o")
    (batch:delete-file my-parameters "my-batch")
    )))

Produces the file `my-batch':

#!/bin/sh
# "my-batch" build script created Sat Jun 10 21:20:37 1995
# ================ Write file with C program.
mv -f hello.c hello.c~
rm -f hello.c
echo '#include <stdio.h>'>>hello.c
echo 'int main(int argc, char **argv)'>>hello.c
echo '{'>>hello.c
echo '  printf("hello world\n");'>>hello.c
echo '  return 0;'>>hello.c
echo '}'>>hello.c
cc -c hello.c
cc -o hello hello.o
hello
rm -f hello
rm -f hello.c
rm -f hello.o
rm -f my-batch

When run, `my-batch' prints:

bash$ my-batch
mv: hello.c: No such file or directory
hello world

Printing Scheme

Generic-Write

(require 'generic-write)

generic-write is a procedure that transforms a Scheme data value (or Scheme program expression) into its textual representation and prints it. The interface to the procedure is sufficiently general to easily implement other useful formatting procedures such as pretty printing, output to a string and truncated output.

Procedure: generic-write obj display? width output
obj
Scheme data value to transform.
display?
Boolean, controls whether characters and strings are quoted.
width
Extended boolean, selects format:
#f
single line format
integer > 0
pretty-print (value = max nb of chars per line)
output
Procedure of 1 argument of string type, called repeatedly with successive substrings of the textual representation. This procedure can return #f to stop the transformation.

The value returned by generic-write is undefined.

Examples:

(write obj) == (generic-write obj #f #f display-string)
(display obj) == (generic-write obj #t #f display-string)

where

display-string ==
(lambda (s) (for-each write-char (string->list s)) #t)

Object-To-String

(require 'object->string)

Function: object->string obj
Returns the textual representation of obj as a string.

Pretty-Print

(require 'pretty-print)

Procedure: pretty-print obj
Procedure: pretty-print obj port

pretty-prints obj on port. If port is not specified, current-output-port is used.

Example:

(pretty-print '((1 2 3 4 5) (6 7 8 9 10) (11 12 13 14 15)
                (16 17 18 19 20) (21 22 23 24 25)))
   -| ((1 2 3 4 5)
   -|  (6 7 8 9 10)
   -|  (11 12 13 14 15)
   -|  (16 17 18 19 20)
   -|  (21 22 23 24 25))

(require 'pprint-file)

Procedure: pprint-file infile
Procedure: pprint-file infile outfile
Pretty-prints all the code in infile. If outfile is specified, the output goes to outfile, otherwise it goes to (current-output-port).

Function: pprint-filter-file infile proc outfile
Function: pprint-filter-file infile proc
infile is a port or a string naming an existing file. Scheme source code expressions and definitions are read from the port (or file) and proc is applied to them sequentially.

outfile is a port or a string. If no outfile is specified then current-output-port is assumed. These expanded expressions are then pretty-printed to this port.

Whitepsace and comments (introduced by ;) which are not part of scheme expressions are reproduced in the output. This procedure does not affect the values returned by current-input-port and current-output-port.

pprint-filter-file can be used to pre-compile macro-expansion and thus can reduce loading time. The following will write into `exp-code.scm' the result of expanding all defmacros in `code.scm'.

(require 'pprint-file)
(require 'defmacroexpand)
(defmacro:load "my-macros.scm")
(pprint-filter-file "code.scm" defmacro:expand* "exp-code.scm")

Time and Date

Posix Time

(require 'posix-time)

Data Type: Calendar-Time
is a datatype encapsulating time.

Data Type: Coordinated Universal Time
(abbreviated UTC) is a vector of integers representing time:

  1. seconds (0 - 61)
  2. minutes (0 - 59)
  3. hours since midnight (0 - 23)
  4. day of month (1 - 31)
  5. month (0 - 11). Note difference from decode-universal-time.
  6. the number of years since 1900. Note difference from decode-universal-time.
  7. day of week (0 - 6)
  8. day of year (0 - 365)
  9. 1 for daylight savings, 0 for regular time

Function: gmtime caltime
Converts the calendar time caltime to UTC and returns it.

Function: localtime caltime tz
Returns caltime converted to UTC relative to timezone tz.

Function: localtime caltime
converts the calendar time caltime to a vector of integers expressed relative to the user's time zone. localtime sets the variable *timezone* with the difference between Coordinated Universal Time (UTC) and local standard time in seconds (see section Time Zone).

Function: gmktime univtime
Converts a vector of integers in GMT Coordinated Universal Time (UTC) format to a calendar time.

Function: mktime univtime
Converts a vector of integers in local Coordinated Universal Time (UTC) format to a calendar time.

Function: mktime univtime tz
Converts a vector of integers in Coordinated Universal Time (UTC) format (relative to time-zone tz) to calendar time.

Function: asctime univtime
Converts the vector of integers caltime in Coordinated Universal Time (UTC) format into a string of the form "Wed Jun 30 21:49:08 1993".

Function: gtime caltime
Function: ctime caltime
Function: ctime caltime tz
Equivalent to (asctime (gmtime caltime)), (asctime (localtime caltime)), and (asctime (localtime caltime tz)), respectively.

Common-Lisp Time

Function: get-decoded-time
Equivalent to (decode-universal-time (get-universal-time)).

Function: get-universal-time
Returns the current time as Universal Time, number of seconds since 00:00:00 Jan 1, 1900 GMT. Note that the reference time is different from current-time.

Function: decode-universal-time univtime
Converts univtime to Decoded Time format. Nine values are returned:
  1. seconds (0 - 61)
  2. minutes (0 - 59)
  3. hours since midnight
  4. day of month
  5. month (1 - 12). Note difference from gmtime and localtime.
  6. year (A.D.). Note difference from gmtime and localtime.
  7. day of week (0 - 6)
  8. #t for daylight savings, #f otherwise
  9. hours west of GMT (-24 - +24)

Notice that the values returned by decode-universal-time do not match the arguments to encode-universal-time.

Function: encode-universal-time second minute hour date month year
Function: encode-universal-time second minute hour date month year time-zone
Converts the arguments in Decoded Time format to Universal Time format. If time-zone is not specified, the returned time is adjusted for daylight saving time. Otherwise, no adjustment is performed.

Notice that the values returned by decode-universal-time do not match the arguments to encode-universal-time.

Vector Graphics

Tektronix Graphics Support

Note: The Tektronix graphics support files need more work, and are not complete.

Tektronix 4000 Series Graphics

The Tektronix 4000 series graphics protocol gives the user a 1024 by 1024 square drawing area. The origin is in the lower left corner of the screen. Increasing y is up and increasing x is to the right.

The graphics control codes are sent over the current-output-port and can be mixed with regular text and ANSI or other terminal control sequences.

Procedure: tek40:init

Procedure: tek40:graphics

Procedure: tek40:text

Procedure: tek40:linetype linetype

Procedure: tek40:move x y

Procedure: tek40:draw x y

Procedure: tek40:put-text x y str

Procedure: tek40:reset

Tektronix 4100 Series Graphics

The graphics control codes are sent over the current-output-port and can be mixed with regular text and ANSI or other terminal control sequences.

Procedure: tek41:init

Procedure: tek41:reset

Procedure: tek41:graphics

Procedure: tek41:move x y

Procedure: tek41:draw x y

Procedure: tek41:point x y number

Procedure: tek41:encode-x-y x y

Procedure: tek41:encode-int number

Mathematical Packages

Bit-Twiddling

(require 'logical)

The bit-twiddling functions are made available through the use of the logical package. logical is loaded by inserting (require 'logical) before the code that uses these functions.

Function: logand n1 n1
Returns the integer which is the bit-wise AND of the two integer arguments.

Example:

(number->string (logand #b1100 #b1010) 2)
   => "1000"

Function: logior n1 n2
Returns the integer which is the bit-wise OR of the two integer arguments.

Example:

(number->string (logior #b1100 #b1010) 2)
   => "1110"

Function: logxor n1 n2
Returns the integer which is the bit-wise XOR of the two integer arguments.

Example:

(number->string (logxor #b1100 #b1010) 2)
   => "110"

Function: lognot n
Returns the integer which is the 2s-complement of the integer argument.

Example:

(number->string (lognot #b10000000) 2)
   => "-10000001"
(number->string (lognot #b0) 2)
   => "-1"

Function: logtest j k
(logtest j k) == (not (zero? (logand j k)))

(logtest #b0100 #b1011) => #f
(logtest #b0100 #b0111) => #t

Function: logbit? index j
(logbit? index j) == (logtest (integer-expt 2 index) j)

(logbit? 0 #b1101) => #t
(logbit? 1 #b1101) => #f
(logbit? 2 #b1101) => #t
(logbit? 3 #b1101) => #t
(logbit? 4 #b1101) => #f

Function: ash int count
Returns an integer equivalent to (inexact->exact (floor (* int (expt 2 count)))).

Example:

(number->string (ash #b1 3) 2)
   => "1000"
(number->string (ash #b1010 -1) 2)
   => "101"

Function: logcount n
Returns the number of bits in integer n. If integer is positive, the 1-bits in its binary representation are counted. If negative, the 0-bits in its two's-complement binary representation are counted. If 0, 0 is returned.

Example:

(logcount #b10101010)
   => 4
(logcount 0)
   => 0
(logcount -2)
   => 1

Function: integer-length n
Returns the number of bits neccessary to represent n.

Example:

(integer-length #b10101010)
   => 8
(integer-length 0)
   => 0
(integer-length #b1111)
   => 4

Function: integer-expt n k
Returns n raised to the non-negative integer exponent k.

Example:

(integer-expt 2 5)
   => 32
(integer-expt -3 3)
   => -27

Function: bit-extract n start end
Returns the integer composed of the start (inclusive) through end (exclusive) bits of n. The startth bit becomes the 0-th bit in the result.

Example:

(number->string (bit-extract #b1101101010 0 4) 2)
   => "1010"
(number->string (bit-extract #b1101101010 4 9) 2)
   => "10110"

Modular Arithmetic

(require 'modular)

Function: extended-euclid n1 n2
Returns a list of 3 integers (d x y) such that d = gcd(n1, n2) = n1 * x + n2 * y.

Function: symmetric:modulus n
Returns (quotient (+ -1 n) -2) for positive odd integer n.

Function: modulus->integer modulus
Returns the non-negative integer characteristic of the ring formed when modulus is used with modular: procedures.

Function: modular:normalize modulus n
Returns the integer (modulo n (modulus->integer modulus)) in the representation specified by modulus.

The rest of these functions assume normalized arguments; That is, the arguments are constrained by the following table:

For all of these functions, if the first argument (modulus) is:

positive?
Work as before. The result is between 0 and modulus.
zero?
The arguments are treated as integers. An integer is returned.
negative?
The arguments and result are treated as members of the integers modulo (+ 1 (* -2 modulus)), but with symmetric representation; i.e. (<= (- modulus) n modulus).

If all the arguments are fixnums the computation will use only fixnums.

Function: modular:invertable? modulus k
Returns #t if there exists an integer n such that k * n == 1 mod modulus, and #f otherwise.

Function: modular:invert modulus k2
Returns an integer n such that 1 = (n * k2) mod modulus. If k2 has no inverse mod modulus an error is signaled.

Function: modular:negate modulus k2
Returns (-k2) mod modulus.

Function: modular:+ modulus k2 k3
Returns (k2 + k3) mod modulus.

Function: modular:- modulus k2 k3
Returns (k2 - k3) mod modulus.

Function: modular:* modulus k2 k3
Returns (k2 * k3) mod modulus.

The Scheme code for modular:* with negative modulus is not completed for fixnum-only implementations.

Function: modular:expt modulus k2 k3
Returns (k2 ^ k3) mod modulus.

Prime Testing and Generation

(require 'primes)

This package tests and generates prime numbers. The strategy used is as follows:

The Miller-Rabin test is a Monte-Carlo test--in other words, it's fast and it gets the right answer with high probability. For a candidate that is prime, the Miller-Rabin test is certain to report "prime"; it will never report "composite". However, for a candidate that is composite, there is a (small) probability that the Miller-Rabin test will erroneously report "prime". This probability can be made arbitarily small by adjusting the number of iterations of the Miller-Rabin test.

Function: probably-prime? candidate
Function: probably-prime? candidate iter
Returns #t if candidate is probably prime. The optional parameter iter controls the number of iterations of the Miller-Rabin test. The probability of a composite candidate being mistaken for a prime is at most (1/4)^iter. The default value of iter is 15, which makes the probability less than 1 in 10^9.

Function: primes< start count
Function: primes< start count iter
Function: primes> start count
Function: primes> start count iter
Returns a list of the first count odd probable primes less (more) than or equal to start. The optional parameter iter controls the number of iterations of the Miller-Rabin test for each candidate. The probability of a composite candidate being mistaken for a prime is at most (1/4)^iter. The default value of iter is 15, which makes the probability less than 1 in 10^9.

Theory

Rabin and Miller's result can be summarized as follows. Let p (the candidate prime) be any odd integer greater than 2. Let b (the "base") be an integer in the range 2 ... p-1. There is a fairly simple Boolean function--call it C, for "Composite"---with the following properties:

For details of C, and why it fails for at most 1/4 of the potential bases, please consult a book on number theory or cryptography such as "A Course in Number Theory and Cryptography" by Neal Koblitz, published by Springer-Verlag 1994.

There is nothing probablistic about this result. It's true for all p. If we had time to test (1/4)p + 1 different bases, we could definitively determine the primality of p. For large candidates, that would take much too long--much longer than the simple approach of dividing by all numbers up to sqrt(p). This is where probability enters the picture.

Suppose we have some candidate prime p. Pick a random integer b in the range 2 ... p-1. Compute C(p,b). If p is prime, the result will certainly be false. If p is composite, the probability is at most 1/4 that the result will be false (demonstrating that p is a strong pseudoprime to base b). The test can be repeated with other random bases. If p is prime, each test is certain to return false. If p is composite, the probability of C(p,b) returning false is at most 1/4 for each test. Since the b are chosen at random, the tests outcomes are independent. So if p is composite and the test is repeated, say, 15 times, the probability of it returning false all fifteen times is at most (1/4)^15, or about 10^-9. If the test is repeated 30 times, the probability of failure drops to at most 8.3e-25.

Rabin and Miller's result holds for all candidates p. However, if the candidate p is picked at random, the probability of the Miller-Rabin test failing is much less than the computed bound. This is because, for most composite numbers, the fraction of bases that cause the test to fail is much less than 1/4. For example, if you pick a random odd number less than 1000 and apply the Miller-Rabin test with only 3 random bases, the computed failure bound is (1/4)^3, or about 1.6e-2. However, the actual probability of failure is much less--about 7.2e-5. If you accidentally pick 703 to test for primality, the probability of failure is (161/703)^3, or about 1.2e-2, which is almost as high as the computed bound. This is because 703 is a strong pseudoprime to 161 bases. But if you pick at random there is only a small chance of picking 703, and no other number less than 1000 has that high a percentage of pseudoprime bases.

The Miller-Rabin test is sometimes used in a slightly different fashion, where it can, at least in principle, cause problems. The weaker version uses small prime bases instead of random bases. If you are picking candidates at random and testing for primality, this works well since very few composites are strong pseudo-primes to small prime bases. (For example, there is only one composite less than 2.5e10 that is a strong pseudo-prime to the bases 2, 3, 5, and 7.) The problem with this approach is that once a candidate has been picked, the test is deterministic. This distinction is subtle, but real. With the randomized test, for any candidate you pick--even if your candidate-picking procedure is strongly biased towards troublesome numbers, the test will work with high probability. With the deterministic version, for any particular candidate, the test will either work (with probability 1), or fail (with probability 1). It won't fail for very many candidates, but that won't be much consolation if your candidate-picking procedure is somehow biased toward troublesome numbers.

Prime Factorization

(require 'factor)

Function: factor k
Returns a list of the prime factors of k. The order of the factors is unspecified. In order to obtain a sorted list do (sort! (factor k) <).

Note: The rest of these procedures implement the Solovay-Strassen primality test. This test has been superseeded by the faster See section Prime Testing and Generation. However these are left here as they take up little space and may be of use to an implementation without bignums.

See Robert Solovay and Volker Strassen, A Fast Monte-Carlo Test for Primality, SIAM Journal on Computing, 1977, pp 84-85.

Function: jacobi-symbol p q
Returns the value (+1, -1, or 0) of the Jacobi-Symbol of exact non-negative integer p and exact positive odd integer q.

Function: prime? p
Returns #f if p is composite; #t if p is prime. There is a slight chance (expt 2 (- prime:trials)) that a composite will return #t.

Function: prime:trials
Is the maxinum number of iterations of Solovay-Strassen that will be done to test a number for primality.

Random Numbers

(require 'random)

Procedure: random n
Procedure: random n state
Accepts a positive integer or real n and returns a number of the same type between zero (inclusive) and n (exclusive). The values returned have a uniform distribution.

The optional argument state must be of the type produced by (make-random-state). It defaults to the value of the variable *random-state*. This object is used to maintain the state of the pseudo-random-number generator and is altered as a side effect of the random operation.

Variable: *random-state*
Holds a data structure that encodes the internal state of the random-number generator that random uses by default. The nature of this data structure is implementation-dependent. It may be printed out and successfully read back in, but may or may not function correctly as a random-number state object in another implementation.

Procedure: make-random-state
Procedure: make-random-state state
Returns a new object of type suitable for use as the value of the variable *random-state* and as a second argument to random. If argument state is given, a copy of it is returned. Otherwise a copy of *random-state* is returned.

If inexact numbers are support by the Scheme implementation, `randinex.scm' will be loaded as well. `randinex.scm' contains procedures for generating inexact distributions.

Procedure: random:uniform state
Returns an uniformly distributed inexact real random number in the range between 0 and 1.

Procedure: random:solid-sphere! vect
Procedure: random:solid-sphere! vect state
Fills vect with inexact real random numbers the sum of whose squares is less than 1.0. Thinking of vect as coordinates in space of dimension n = (vector-length vect), the coordinates are uniformly distributed within the unit n-shere. The sum of the squares of the numbers is returned.

Procedure: random:hollow-sphere! vect
Procedure: random:hollow-sphere! vect state
Fills vect with inexact real random numbers the sum of whose squares is equal to 1.0. Thinking of vect as coordinates in space of dimension n = (vector-length vect), the coordinates are uniformly distributed over the surface of the unit n-shere.

Procedure: random:normal
Procedure: random:normal state
Returns an inexact real in a normal distribution with mean 0 and standard deviation 1. For a normal distribution with mean m and standard deviation d use (+ m (* d (random:normal))).

Procedure: random:normal-vector! vect
Procedure: random:normal-vector! vect state
Fills vect with inexact real random numbers which are independent and standard normally distributed (i.e., with mean 0 and variance 1).

Procedure: random:exp
Procedure: random:exp state
Returns an inexact real in an exponential distribution with mean 1. For an exponential distribution with mean u use (* u (random:exp)).

Cyclic Checksum

(require 'make-crc)

Function: make-port-crc
Function: make-port-crc degree
Function: make-port-crc degree generator
Returns an expression for a procedure of one argument, a port. This procedure reads characters from the port until the end of file and returns the integer checksum of the bytes read.

The integer degree, if given, specifies the degree of the polynomial being computed -- which is also the number of bits computed in the checksums. The default value is 32.

The integer generator specifies the polynomial being computed. The power of 2 generating each 1 bit is the exponent of a term of the polynomial. The bit at position degree is implicit and should not be part of generator. This allows systems with numbers limited to 32 bits to calculate 32 bit checksums. The default value of generator when degree is 32 (its default) is:

(make-port-crc 32 #b00000100110000010001110110110111)

Creates a procedure to calculate the P1003.2/D11.2 (POSIX.2) 32-bit checksum from the polynomial:

     32    26    23    22    16    12    11
  ( x   + x   + x   + x   + x   + x   + x   +

      10    8    7    5    4    2    1
     x   + x  + x  + x  + x  + x  + x  + 1 )  mod 2
(require 'make-crc)
(define crc32 (slib:eval (make-port-crc)))
(define (file-check-sum file) (call-with-input-file file crc32))
(file-check-sum (in-vicinity (library-vicinity) "ratize.scm"))

=> 3553047446

Plotting on Character Devices

(require 'charplot)

The plotting procedure is made available through the use of the charplot package. charplot is loaded by inserting (require 'charplot) before the code that uses this procedure.

Variable: charplot:height
The number of rows to make the plot vertically.

Variable: charplot:width
The number of columns to make the plot horizontally.

Procedure: plot! coords x-label y-label
coords is a list of pairs of x and y coordinates. x-label and y-label are strings with which to label the x and y axes.

Example:

(require 'charplot)
(set! charplot:height 19)
(set! charplot:width 45)

(define (make-points n)
  (if (zero? n)
      '()
      (cons (cons (/ n 6) (sin (/ n 6))) (make-points (1- n)))))

(plot! (make-points 37) "x" "Sin(x)")
-|
  Sin(x)   ______________________________________________
      1.25|-                                             |
          |                                              |
         1|-       ****                                  |
          |      **    **                                |
  750.0e-3|-    *        *                               |
          |    *          *                              |
  500.0e-3|-  *            *                             |
          |  *                                           |
  250.0e-3|-                *                            |
          | *                *                           |
         0|-------------------*--------------------------|
          |                                     *        |
 -250.0e-3|-                   *               *         |
          |                     *             *          |
 -500.0e-3|-                     *                       |
          |                       *          *           |
 -750.0e-3|-                       *        *            |
          |                         **    **             |
        -1|-                          ****               |
          |____________:_____._____:_____._____:_________|
        x              2           4      

Root Finding

(require 'root)

Function: newtown:find-integer-root f df/dx x0
Given integer valued procedure f, its derivative (with respect to its argument) df/dx, and initial integer value x0 for which df/dx(x0) is non-zero, returns an integer x for which f(x) is closer to zero than either of the integers adjacent to x; or returns #f if such an integer can't be found.

To find the closest integer to a given integers square root:

(define (integer-sqrt y)
  (newton:find-integer-root
   (lambda (x) (- (* x x) y))
   (lambda (x) (* 2 x))
   (ash 1 (quotient (integer-length y) 2))))

(integer-sqrt 15) => 4

Function: integer-sqrt y
Given a non-negative integer y, returns the rounded square-root of y.

Function: newton:find-root f df/dx x0 prec
Given real valued procedures f, df/dx of one (real) argument, initial real value x0 for which df/dx(x0) is non-zero, and positive real number prec, returns a real x for which abs(f(x)) is less than prec; or returns #f if such a real can't be found.

If prec is instead a negative integer, newton:find-root returns the result of -prec iterations.

H. J. Orchard, The Laguerre Method for Finding the Zeros of Polynomials, IEEE Transactions on Circuits and Systems, Vol. 36, No. 11, November 1989, pp 1377-1381.

There are 2 errors in Orchard's Table II. Line k=2 for starting value of 1000+j0 should have Z_k of 1.0475 + j4.1036 and line k=2 for starting value of 0+j1000 should have Z_k of 1.0988 + j4.0833.

Function: laguerre:find-root f df/dz ddf/dz^2 z0 prec
Given complex valued procedure f of one (complex) argument, its derivative (with respect to its argument) df/dx, its second derivative ddf/dz^2, initial complex value z0, and positive real number prec, returns a complex number z for which magnitude(f(z)) is less than prec; or returns #f if such a number can't be found.

If prec is instead a negative integer, laguerre:find-root returns the result of -prec iterations.

Function: laguerre:find-polynomial-root deg f df/dz ddf/dz^2 z0 prec
Given polynomial procedure f of integer degree deg of one argument, its derivative (with respect to its argument) df/dx, its second derivative ddf/dz^2, initial complex value z0, and positive real number prec, returns a complex number z for which magnitude(f(z)) is less than prec; or returns #f if such a number can't be found.

If prec is instead a negative integer, laguerre:find-polynomial-root returns the result of -prec iterations.

Commutative Rings

Scheme provides a consistent and capable set of numeric functions. Inexacts implement a field; integers a commutative ring (and Euclidean domain). This package allows the user to use basic Scheme numeric functions with symbols and non-numeric elements of commutative rings.

(require 'commutative-ring)

The commutative-ring package makes +, -, *, /, and ^ careful in the sense that any non-numeric arguments which it cannot reduce appear in the expression output. In order to see what working with this package is like, self-set all the single letter identifiers (to their corresponding symbols).

(define a 'a)
...
(define z 'z)

Or just (require 'self-set). Now for some sample expressions:

(* (+ a b) (+ a b)) => (+ (* 2 a b) (^ a 2) (^ b 2)) 
(* (+ a b) (- a b)) => (- (^ a 2) (^ b 2)) 
(* (- a b) (- a b)) => (- (+ (^ a 2) (^ b 2)) (* 2 a b)) 
(* (- a b) (+ a b)) => (- (^ a 2) (^ b 2)) 
(/ (+ a b) (+ c d)) => (+ (/ a (+ c d)) (/ b (+ c d))) 
(/ (+ a b) (- c d)) => (+ (/ a (- c d)) (/ b (- c d))) 
(/ (- a b) (- c d)) => (- (/ a (- c d)) (/ b (- c d))) 
(/ (- a b) (+ c d)) => (- (/ a (+ c d)) (/ b (+ c d))) 
(^ (+ a b) 3) => (+ (* 3 a (^ b 2)) (* 3 b (^ a 2)) (^ a 3) (^ b 3)) 
(^ (+ a 2) 3) => (+ 8 (* a 12) (* (^ a 2) 6) (^ a 3))

Use of this package is not restricted to simple arithmetic expressions:

(require 'determinant)

(determinant '((a b c) (d e f) (g h i))) =>
(- (+ (* a e i) (* b f g) (* c d h)) (* a f h) (* b d i) (* c e g))

The commutative-ring package differs from other extension mechanisms in that it automatically, using properties true of all commutative rings, simplifies sum and product expressions containing non-numeric elements. One need only specify behavior for + or * for cases where expressions involving objects reduce to numbers or to expressions involving different non-numeric elements.

Currently, only +, -, *, /, and ^ support non-numeric elements. Expressions with - are converted to equivalent expressions without -, so behavior for - is not defined separately. / expressions are handled similarly.

This list might be extended to include quotient, modulo, remainder, lcm, and gcd; but these work only for the more restrictive Euclidean (Unique Factorization) Domain.

Function: cring:define-rule op sub-op1 sub-op2 reduction
Defines a rule for the case when the operation represented by symbol op is applied to lists whose cars are sub-op1 and sub-op2, respectively. The argument reduction is a procedure accepting 2 arguments which will be lists whose cars are sub-op1 and sub-op2.

Function: cring:define-rule op sub-op1 'identity reduction
Defines a rule for the case when the operation represented by symbol op is applied to a list whose car is sub-op1, and some other argument. Reduction will be called with the list whose car is sub-op1 and some other argument.

If reduction returns #f, the reduction has failed and other reductions will be tried. If reduction returns a non-false value, that value will replace the two arguments in arithmetic (+, -, and *) calculations involving non-numeric elements.

The operations + and * are assumed commutative; hence both orders of arguments to reduction will be tried if necessary.

The following rule is the built-in definition for distributing * over +.

(cring:define-rule
 '* '+ 'identity
 (lambda (exp1 exp2)
   (apply + (map (lambda (trm) (* trm exp2)) (cdr exp1))))))

How to Create a Commutative Ring

The first step in creating your commutative ring is to write procedures to create elements of the ring. A non-numeric element of the ring must be represented as a list whose first element is a symbol or string. This first element identifies the type of the object. A convenient and clear convention is to make the type-identifying element be the same symbol whose top-level value is the procedure to create it.

(define (n . list1)
  (cond ((and (= 2 (length list1))
              (eq? (car list1) (cadr list1)))
         0)
        ((not (term< (first list1) (last1 list1)))
         (apply n (reverse list1)))
        (else (cons 'n list1))))

(define (s x y) (n x y))

(define (m . list1)
  (cond ((neq? (first list1) (term_min list1))
         (apply m (cyclicrotate list1)))
        ((term< (last1 list1) (cadr list1))
         (apply m (reverse (cyclicrotate list1))))
        (else (cons 'm list1))))

Define a procedure to multiply 2 non-numeric elements of the ring. Other multiplicatons are handled automatically. Objects for which rules have not been defined are not changed.

(define (n*n ni nj)
  (let ((list1 (cdr ni)) (list2 (cdr nj)))
    (cond ((null? (intersection list1 list2)) #f)
          ((and (eq? (last1 list1) (first list2))
                (neq? (first list1) (last1 list2)))
           (apply n (splice list1 list2)))
          ((and (eq? (first list1) (first list2))
                (neq? (last1 list1) (last1 list2)))
           (apply n (splice (reverse list1) list2)))
          ((and (eq? (last1 list1) (last1 list2))
                (neq? (first list1) (first list2)))
           (apply n (splice list1 (reverse list2))))
          ((and (eq? (last1 list1) (first list2))
                (eq? (first list1) (last1 list2)))
           (apply m (cyclicsplice list1 list2)))
          ((and (eq? (first list1) (first list2))
                (eq? (last1 list1) (last1 list2)))
           (apply m (cyclicsplice (reverse list1) list2)))
          (else #f))))

Test the procedures to see if they work.

;;; where cyclicrotate(list) is cyclic rotation of the list one step
;;; by putting the first element at the end
(define (cyclicrotate list1)
  (append (rest list1) (list (first list1))))
;;; and where term_min(list) is the element of the list which is
;;; first in the term ordering.
(define (term_min list1)
  (car (sort list1 term<)))
(define (term< sym1 sym2)
  (string<? (symbol->string sym1) (symbol->string sym2)))
(define first car)
(define rest cdr)
(define (last1 list1) (car (last-pair list1)))
(define (neq? obj1 obj2) (not (eq? obj1 obj2)))
;;; where splice is the concatenation of list1 and list2 except that their
;;; common element is not repeated.
(define (splice list1 list2)
  (cond ((eq? (last1 list1) (first list2))
         (append list1 (cdr list2)))
        (else (error 'splice list1 list2))))
;;; where cyclicsplice is the result of leaving off the last element of
;;; splice(list1,list2).
(define (cyclicsplice list1 list2)
  (cond ((and (eq? (last1 list1) (first list2))
              (eq? (first list1) (last1 list2)))
         (butlast (splice list1 list2) 1))
        (else (error 'cyclicsplice list1 list2))))

(N*N (S a b) (S a b)) => (m a b)

Then register the rule for multiplying type N objects by type N objects.

(cring:define-rule '* 'N 'N N*N))

Now we are ready to compute!

(define (t)
  (define detM
    (+ (* (S g b)
          (+ (* (S f d)
                (- (* (S a f) (S d g)) (* (S a g) (S d f))))
             (* (S f f)
                (- (* (S a g) (S d d)) (* (S a d) (S d g))))
             (* (S f g)
                (- (* (S a d) (S d f)) (* (S a f) (S d d))))))
       (* (S g d)
          (+ (* (S f b)
                (- (* (S a g) (S d f)) (* (S a f) (S d g))))
             (* (S f f)
                (- (* (S a b) (S d g)) (* (S a g) (S d b))))
             (* (S f g)
                (- (* (S a f) (S d b)) (* (S a b) (S d f))))))
       (* (S g f)
          (+ (* (S f b)
                (- (* (S a d) (S d g)) (* (S a g) (S d d))))
             (* (S f d)
                (- (* (S a g) (S d b)) (* (S a b) (S d g))))
             (* (S f g)
                (- (* (S a b) (S d d)) (* (S a d) (S d b))))))
       (* (S g g)
          (+ (* (S f b)
                (- (* (S a f) (S d d)) (* (S a d) (S d f))))
             (* (S f d)
                (- (* (S a b) (S d f)) (* (S a f) (S d b))))
             (* (S f f)
                (- (* (S a d) (S d b)) (* (S a b) (S d d))))))))
  (* (S b e) (S c a) (S e c)
     detM
     ))
(pretty-print (t))
-|
(- (+ (m a c e b d f g)
      (m a c e b d g f)
      (m a c e b f d g)
      (m a c e b f g d)
      (m a c e b g d f)
      (m a c e b g f d))
   (* 2 (m a b e c) (m d f g))
   (* (m a c e b d) (m f g))
   (* (m a c e b f) (m d g))
   (* (m a c e b g) (m d f)))

Determinant

(require 'determinant)
(determinant '((1 2) (3 4))) => -2
(determinant '((1 2 3) (4 5 6) (7 8 9))) => 0
(determinant '((1 2 3 4) (5 6 7 8) (9 10 11 12))) => 0

Database Packages

Base Table

A base table implementation using Scheme association lists is available as the value of the identifier alist-table after doing:

(require 'alist-table)

Association list base tables are suitable for small databases and support all Scheme types when temporary and readable/writeable Scheme types when saved. I hope support for other base table implementations will be added in the future.

This rest of this section documents the interface for a base table implementation from which the section Relational Database package constructs a Relational system. It will be of interest primarily to those wishing to port or write new base-table implementations.

All of these functions are accessed through a single procedure by calling that procedure with the symbol name of the operation. A procedure will be returned if that operation is supported and #f otherwise. For example:

(require 'alist-table)
(define open-base (alist-table 'make-base))
make-base       => *a procedure*
(define foo (alist-table 'foo))
foo             => #f

Function: make-base filename key-dimension column-types
Returns a new, open, low-level database (collection of tables) associated with filename. This returned database has an empty table associated with catalog-id. The positive integer key-dimension is the number of keys composed to make a primary-key for the catalog table. The list of symbols column-types describes the types of each column for that table. If the database cannot be created as specified, #f is returned.

Calling the close-base method on this database and possibly other operations will cause filename to be written to. If filename is #f a temporary, non-disk based database will be created if such can be supported by the base table implelentation.

Function: open-base filename mutable
Returns an open low-level database associated with filename. If mutable? is #t, this database will have methods capable of effecting change to the database. If mutable? is #f, only methods for inquiring the database will be available. If the database cannot be opened as specified #f is returned.

Calling the close-base (and possibly other) method on a mutable? database will cause filename to be written to.

Function: write-base lldb filename
Causes the low-level database lldb to be written to filename. If the write is successful, also causes lldb to henceforth be associated with filename. Calling the close-database (and possibly other) method on lldb may cause filename to be written to. If filename is #f this database will be changed to a temporary, non-disk based database if such can be supported by the underlying base table implelentation. If the operations completed successfully, #t is returned. Otherwise, #f is returned.

Function: sync-base lldb
Causes the file associated with the low-level database lldb to be updated to reflect its current state. If the associated filename is #f, no action is taken and #f is returned. If this operation completes successfully, #t is returned. Otherwise, #f is returned.

Function: close-base lldb
Causes the low-level database lldb to be written to its associated file (if any). If the write is successful, subsequent operations to lldb will signal an error. If the operations complete successfully, #t is returned. Otherwise, #f is returned.

Function: make-table lldb key-dimension column-types
Returns the base-id for a new base table, otherwise returns #f. The base table can then be opened using (open-table lldb base-id). The positive integer key-dimension is the number of keys composed to make a primary-key for this table. The list of symbols column-types describes the types of each column.

Constant: catalog-id
A constant base-id suitable for passing as a parameter to open-table. catalog-id will be used as the base table for the system catalog.

Function: open-table lldb base-id key-dimension column-types
Returns a handle for an existing base table in the low-level database lldb if that table exists and can be opened in the mode indicated by mutable?, otherwise returns #f.

As with make-table, the positive integer key-dimension is the number of keys composed to make a primary-key for this table. The list of symbols column-types describes the types of each column.

Function: kill-table lldb base-id key-dimension column-types
Returns #t if the base table associated with base-id was removed from the low level database lldb, and #f otherwise.

Function: make-keyifier-1 type
Returns a procedure which accepts a single argument which must be of type type. This returned procedure returns an object suitable for being a key argument in the functions whose descriptions follow.

Any 2 arguments of the supported type passed to the returned function which are not equal? must result in returned values which are not equal?.

Function: make-list-keyifier key-dimension types
The list of symbols types must have at least key-dimension elements. Returns a procedure which accepts a list of length key-dimension and whose types must corresopond to the types named by types. This returned procedure combines the elements of its list argument into an object suitable for being a key argument in the functions whose descriptions follow.

Any 2 lists of supported types (which must at least include symbols and non-negative integers) passed to the returned function which are not equal? must result in returned values which are not equal?.

Function: make-key-extractor key-dimension types column-number
Returns a procedure which accepts objects produced by application of the result of (make-list-keyifier key-dimension types). This procedure returns a key which is equal? to the column-numberth element of the list which was passed to create combined-key. The list types must have at least key-dimension elements.

Function: make-key->list key-dimension types
Returns a procedure which accepts objects produced by application of the result of (make-list-keyifier key-dimension types). This procedure returns a list of keys which are elementwise equal? to the list which was passed to create combined-key.

In the following functions, the key argument can always be assumed to be the value returned by a call to a keyify routine.

In contrast, a match-key argument is a list of length equal to the number of primary keys. The match-key restricts the actions of the table command to those records whose primary keys all satisfy the corresponding element of the match-key list. The elements and their actions are:

#f
The false value matches any key in the corresponding position.
an object of type procedure
This procedure must take a single argument, the key in the corresponding position. Any key for which the procedure returns a non-false value is a match; Any key for which the procedure returns a #f is not.
other values
Any other value matches only those keys equal? to it.

Function: for-each-key handle procedure match-key
Calls procedure once with each key in the table opened in handle which satisfies match-key in an unspecified order. An unspecified value is returned.

Function: map-key handle procedure match-key
Returns a list of the values returned by calling procedure once with each key in the table opened in handle which satisfies match-key in an unspecified order.

Function: ordered-for-each-key handle procedure match-key
Calls procedure once with each key in the table opened in handle which satisfies match-key in the natural order for the types of the primary key fields of that table. An unspecified value is returned.

Function: delete* handle match-key
Removes all rows which satisfy match-key from the table opened in handle. An unspecified value is returned.

Function: present? handle key
Returns a non-#f value if there is a row associated with key in the table opened in handle and #f otherwise.

Function: delete handle key
Removes the row associated with key from the table opened in handle. An unspecified value is returned.

Function: make-getter key-dimension types
Returns a procedure which takes arguments handle and key. This procedure returns a list of the non-primary values of the relation (in the base table opened in handle) whose primary key is key if it exists, and #f otherwise.

Function: make-putter key-dimension types
Returns a procedure which takes arguments handle and key and value-list. This procedure associates the primary key key with the values in value-list (in the base table opened in handle) and returns an unspecified value.

Function: supported-type? symbol
Returns #t if symbol names a type allowed as a column value by the implementation, and #f otherwise. At a minimum, an implementation must support the types integer, symbol, string, boolean, and base-id.

Function: supported-key-type? symbol
Returns #t if symbol names a type allowed as a key value by the implementation, and #f otherwise. At a minimum, an implementation must support the types integer, and symbol.

integer
Scheme exact integer.
symbol
Scheme symbol.
boolean
#t or #f.
base-id
Objects suitable for passing as the base-id parameter to open-table. The value of catalog-id must be an acceptable base-id.

Relational Database

(require 'relational-database)

This package implements a database system inspired by the Relational Model (E. F. Codd, A Relational Model of Data for Large Shared Data Banks). An SLIB relational database implementation can be created from any section Base Table implementation.

Motivations

Most nontrivial programs contain databases: Makefiles, configure scripts, file backup, calendars, editors, source revision control, CAD systems, display managers, menu GUIs, games, parsers, debuggers, profilers, and even error reporting are all rife with databases. Coding databases is such a common activity in programming that many may not be aware of how often they do it.

A database often starts as a dispatch in a program. The author, perhaps because of the need to make the dispatch configurable, the need for correlating dispatch in other routines, or because of changes or growth, devises a data structure to contain the information, a routine for interpreting that data structure, and perhaps routines for augmenting and modifying the stored data. The dispatch must be converted into this form and tested.

The programmer may need to devise an interactive program for enabling easy examination and modification of the information contained in this database. Often, in an attempt to foster modularity and avoid delays in release, intermediate file formats for the database information are devised. It often turns out that users prefer modifying these intermediate files with a text editor to using the interactive program in order to do operations (such as global changes) not forseen by the program's author.

In order to address this need, the concientous software engineer may even provide a scripting language to allow users to make repetitive database changes. Users will grumble that they need to read a large manual and learn yet another programming language (even if it almost has language "xyz" syntax) in order to do simple configuration.

All of these facilities need to be designed, coded, debugged, documented, and supported; often causing what was very simple in concept to become a major developement project.

This view of databases just outlined is somewhat the reverse of the view of the originators of the Relational Model of database abstraction. The relational model was devised to unify and allow interoperation of large multi-user databases running on diverse platforms. A fairly general purpose "Comprehensive Language" for database manipulations is mandated (but not specified) as part of the relational model for databases.

One aspect of the Relational Model of some importance is that the "Comprehensive Language" must be expressible in some form which can be stored in the database. This frees the programmer from having to make programs data-driven in order to use a database.

This package includes as one of its basic supported types Scheme expressions. This type allows expressions as defined by the Scheme standards to be stored in the database. Using slib:eval retrieved expressions can be evaluated (in the top-level environment). Scheme's lambda facilitates closure of environments, modularity, etc. so that procedures (which could not be stored directly most databases) can still be effectively retrieved. Since slib:eval evaluates expressions in the top-level environment, built-in and user defined procedures can be easily accessed by name.

This package's purpose is to standardize (through a common interface) database creation and usage in Scheme programs. The relational model's provision for inclusion of language expressions as data as well as the description (in tables, of course) of all of its tables assures that relational databases are powerful enough to assume the roles currently played by thousands of ad-hoc routines and data formats.

Such standardization to a relational-like model brings many benefits:

Creating and Opening Relational Databases

Function: make-relational-system base-table-implementation

Returns a procedure implementing a relational database using the base-table-implementation.

All of the operations of a base table implementation are accessed through a procedure defined by requireing that implementation. Similarly, all of the operations of the relational database implementation are accessed through the procedure returned by make-relational-system. For instance, a new relational database could be created from the procedure returned by make-relational-system by:

(require 'alist-table)
(define relational-alist-system
        (make-relational-system alist-table))
(define create-alist-database
        (relational-alist-system 'create-database))
(define my-database
        (create-alist-database "mydata.db"))

What follows are the descriptions of the methods available from relational system returned by a call to make-relational-system.

Function: create-database filename

Returns an open, nearly empty relational database associated with filename. The only tables defined are the system catalog and domain table. Calling the close-database method on this database and possibly other operations will cause filename to be written to. If filename is #f a temporary, non-disk based database will be created if such can be supported by the underlying base table implelentation. If the database cannot be created as specified #f is returned. For the fields and layout of descriptor tables, See section Catalog Representation

Function: open-database filename mutable?

Returns an open relational database associated with filename. If mutable? is #t, this database will have methods capable of effecting change to the database. If mutable? is #f, only methods for inquiring the database will be available. Calling the close-database (and possibly other) method on a mutable? database will cause filename to be written to. If the database cannot be opened as specified #f is returned.

Relational Database Operations

These are the descriptions of the methods available from an open relational database. A method is retrieved from a database by calling the database with the symbol name of the operation. For example:

(define my-database
        (create-alist-database "mydata.db"))
(define telephone-table-desc
        ((my-database 'create-table) 'telephone-table-desc))

Function: close-database
Causes the relational database to be written to its associated file (if any). If the write is successful, subsequent operations to this database will signal an error. If the operations completed successfully, #t is returned. Otherwise, #f is returned.

Function: write-database filename
Causes the relational database to be written to filename. If the write is successful, also causes the database to henceforth be associated with filename. Calling the close-database (and possibly other) method on this database will cause filename to be written to. If filename is #f this database will be changed to a temporary, non-disk based database if such can be supported by the underlying base table implelentation. If the operations completed successfully, #t is returned. Otherwise, #f is returned.

Function: table-exists? table-name
Returns #t if table-name exists in the system catalog, otherwise returns #f.

Function: open-table table-name mutable?
Returns a methods procedure for an existing relational table in this database if it exists and can be opened in the mode indicated by mutable?, otherwise returns #f.

These methods will be present only in databases which are mutable?.

Function: delete-table table-name
Removes and returns the table-name row from the system catalog if the table or view associated with table-name gets removed from the database, and #f otherwise.

Function: create-table table-desc-name
Returns a methods procedure for a new (open) relational table for describing the columns of a new base table in this database, otherwise returns #f. For the fields and layout of descriptor tables, See section Catalog Representation.

Function: create-table table-name table-desc-name
Returns a methods procedure for a new (open) relational table with columns as described by table-desc-name, otherwise returns #f.

Function: create-view ??
Function: project-table ??
Function: restrict-table ??
Function: cart-prod-tables ??
Not yet implemented.

Table Operations

These are the descriptions of the methods available from an open relational table. A method is retrieved from a table by calling the table with the symbol name of the operation. For example:

(define telephone-table-desc
        ((my-database 'create-table) 'telephone-table-desc))
(require 'common-list-functions)
(define ndrp (telephone-table-desc 'row:insert))
(ndrp '(1 #t name #f string))
(ndrp '(2 #f telephone
          (lambda (d)
            (and (string? d) (> (string-length d) 2)
                 (every
                  (lambda (c)
                    (memv c '(#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9
                                  #\+ #\( #\  #\) #\-)))
                  (string->list d))))
          string))

Some operations described below require primary key arguments. Primary keys arguments are denoted key1 key2 .... It is an error to call an operation for a table which takes primary key arguments with the wrong number of primary keys for that table.

The term row used below refers to a Scheme list of values (one for each column) in the order specified in the descriptor (table) for this table. Missing values appear as #f. Primary keys must not be missing.

Function: get column-name
Returns a procedure of arguments key1 key2 ... which returns the value for the column-name column of the row associated with primary keys key1, key2 ... if that row exists in the table, or #f otherwise.

((plat 'get 'processor) 'djgpp) => i386
((plat 'get 'processor) 'be-os) => #f

Function: get* column-name
Returns a procedure of optional arguments match-key1 ... which returns a list of the values for the specified column for all rows in this table. The optional match-key1 ... arguments restrict actions to a subset of the table. See the match-key description below for details.

((plat 'get* 'processor)) =>
(i386 8086 i386 8086 i386 i386 8086 m68000
 m68000 m68000 m68000 m68000 powerpc)

((plat 'get* 'processor) #f) =>
(i386 8086 i386 8086 i386 i386 8086 m68000
 m68000 m68000 m68000 m68000 powerpc)

(define (a-key? key)
   (char=? #\a (string-ref (symbol->string key) 0)))

((plat 'get* 'processor) a-key?) =>
(m68000 m68000 m68000 m68000 m68000 powerpc)

((plat 'get* 'name) a-key?) =>
(atari-st-turbo-c atari-st-gcc amiga-sas/c-5.10
 amiga-aztec amiga-dice-c aix)

Function: row:retrieve
Returns a procedure of arguments key1 key2 ... which returns the row associated with primary keys key1, key2 ... if it exists, or #f otherwise.

((plat 'row:retrieve) 'linux) => (linux i386 linux gcc)
((plat 'row:retrieve) 'multics) => #f

Function: row:retrieve*
Returns a procedure of optional arguments match-key1 ... which returns a list of all rows in this table. The optional match-key1 ... arguments restrict actions to a subset of the table. See the match-key description below for details.

((plat 'row:retrieve*) a-key?) =>
((atari-st-turbo-c m68000 atari turbo-c)
 (atari-st-gcc m68000 atari gcc)
 (amiga-sas/c-5.10 m68000 amiga sas/c)
 (amiga-aztec m68000 amiga aztec)
 (amiga-dice-c m68000 amiga dice-c)
 (aix powerpc aix -))

Function: row:remove
Returns a procedure of arguments key1 key2 ... which removes and returns the row associated with primary keys key1, key2 ... if it exists, or #f otherwise.

Function: row:remove*
Returns a procedure of optional arguments match-key1 ... which removes and returns a list of all rows in this table. The optional match-key1 ... arguments restrict actions to a subset of the table. See the match-key description below for details.

Function: row:delete
Returns a procedure of arguments key1 key2 ... which deletes the row associated with primary keys key1, key2 ... if it exists. The value returned is unspecified.

Function: row:delete*
Returns a procedure of optional arguments match-key1 ... which Deletes all rows from this table. The optional match-key1 ... arguments restrict deletions to a subset of the table. See the match-key description below for details. The value returned is unspecified. The descriptor table and catalog entry for this table are not affected.

Function: row:update
Returns a procedure of one argument, row, which adds the row, row, to this table. If a row for the primary key(s) specified by row already exists in this table, it will be overwritten. The value returned is unspecified.

Function: row:update*
Returns a procedure of one argument, rows, which adds each row in the list of rows, rows, to this table. If a row for the primary key specified by an element of rows already exists in this table, it will be overwritten. The value returned is unspecified.

Function: row:insert
Adds the row row to this table. If a row for the primary key(s) specified by row already exists in this table an error is signaled. The value returned is unspecified.

Function: row:insert*
Returns a procedure of one argument, rows, which adds each row in the list of rows, rows, to this table. If a row for the primary key specified by an element of rows already exists in this table, an error is signaled. The value returned is unspecified.

Function: for-each-row
Returns a procedure of arguments proc match-key1 ... which calls proc with each row in this table in the (implementation-dependent) natural ordering for rows. The optional match-key1 ... arguments restrict actions to a subset of the table. See the match-key description below for details.

Real relational programmers would use some least-upper-bound join for every row to get them in order; But we don't have joins yet.

The (optional) match-key1 ... arguments are used to restrict actions of a whole-table operation to a subset of that table. Those procedures (returned by methods) which accept match-key arguments will accept any number of match-key arguments between zero and the number of primary keys in the table. Any unspecified match-key arguments default to #f.

The match-key1 ... restrict the actions of the table command to those records whose primary keys each satisfy the corresponding match-key argument. The arguments and their actions are:

#f
The false value matches any key in the corresponding position.
an object of type procedure
This procedure must take a single argument, the key in the corresponding position. Any key for which the procedure returns a non-false value is a match; Any key for which the procedure returns a #f is not.
other values
Any other value matches only those keys equal? to it.

Function: close-table
Subsequent operations to this table will signal an error.

Constant: column-names
Constant: column-foreigns
Constant: column-domains
Constant: column-types
Return a list of the column names, foreign-key table names, domain names, or type names respectively for this table. These 4 methods are different from the others in that the list is returned, rather than a procedure to obtain the list.

Constant: primary-limit
Returns the number of primary keys fields in the relations in this table.

Catalog Representation

Each database (in an implementation) has a system catalog which describes all the user accessible tables in that database (including itself).

The system catalog base table has the following fields. PRI indicates a primary key for that table.

PRI table-name
    column-limit            the highest column number
    coltab-name             descriptor table name
    bastab-id               data base table identifier
    user-integrity-rule
    view-procedure          A scheme thunk which, when called,
                            produces a handle for the view.  coltab
                            and bastab are specified if and only if
                            view-procedure is not.

Descriptors for base tables (not views) are tables (pointed to by system catalog). Descriptor (base) tables have the fields:

PRI column-number           sequential integers from 1
    primary-key?            boolean TRUE for primary key components
    column-name
    column-integrity-rule
    domain-name

A primary key is any column marked as primary-key? in the corresponding descriptor table. All the primary-key? columns must have lower column numbers than any non-primary-key? columns. Every table must have at least one primary key. Primary keys must be sufficient to distinguish all rows from each other in the table. All of the system defined tables have a single primary key.

This package currently supports tables having from 1 to 4 primary keys if there are non-primary columns, and any (natural) number if all columns are primary keys. If you need more than 4 primary keys, I would like to hear what you are doing!

A domain is a category describing the allowable values to occur in a column. It is described by a (base) table with the fields:

PRI domain-name
    foreign-table
    domain-integrity-rule
    type-id
    type-param

The type-id field value is a symbol. This symbol may be used by the underlying base table implementation in storing that field.

If the foreign-table field is non-#f then that field names a table from the catalog. The values for that domain must match a primary key of the table referenced by the type-param (or #f, if allowed). This package currently does not support composite foreign-keys.

The types for which support is planned are:

    atom
    symbol
    string                  [<length>]
    number                  [<base>]
    money                   <currency>
    date-time
    boolean

    foreign-key             <table-name>
    expression
    virtual                 <expression>

Unresolved Issues

Although `rdms.scm' is not large, I found it very difficult to write (six rewrites). I am not aware of any other examples of a generalized relational system (although there is little new in CS). I left out several aspects of the Relational model in order to simplify the job. The major features lacking (which might be addressed portably) are views, transaction boundaries, and protection.

Protection needs a model for specifying priveledges. Given how operations are accessed from handles it should not be difficult to restrict table accesses to those allowed for that user.

The system catalog has a field called view-procedure. This should allow a purely functional implementation of views. This will work but is unsatisfying for views resulting from a selection (subset of rows); for whole table operations it will not be possible to reduce the number of keys scanned over when the selection is specified only by an opaque procedure.

Transaction boundaries present the most intriguing area. Transaction boundaries are actually a feature of the "Comprehensive Language" of the Relational database and not of the database. Scheme would seem to provide the opportunity for an extremely clean semantics for transaction boundaries since the builtin procedures with side effects are small in number and easily identified.

These side-effect builtin procedures might all be portably redefined to versions which properly handled transactions. Compiled library routines would need to be recompiled as well. Many system extensions (delete-file, system, etc.) would also need to be redefined.

There are 2 scope issues that must be resolved for multiprocess transaction boundaries:

Process scope
The actions captured by a transaction should be only for the process which invoked the start of transaction. Although standard Scheme does not provide process primitives as such, dynamic-wind would provide a workable hook into process switching for many implementations.
Shared utilities with state
Some shared utilities have state which should not be part of a transaction. An example would be calling a pseudo-random number generator. If the success of a transaction depended on the pseudo-random number and failed, the state of the generator would be set back. Subsequent calls would keep returning the same number and keep failing. Pseudo-random number generators are not reentrant; thus they would require locks in order to operate properly in a multiprocess environment. Are all examples of utilities whose state should not be part of transactions also non-reentrant? If so, perhaps suspending transaction capture for the duration of locks would solve this problem.

Database Utilities

(require 'database-utilities)

This enhancement wraps a utility layer on relational-database which provides:

Also included are utilities which provide:

for any SLIB relational database.

Function: create-database filename base-table-type
Returns an open, nearly empty enhanced (with *commands* table) relational database (with base-table type base-table-type) associated with filename.

Function: open-database filename
Function: open-database filename base-table-type
Returns an open enchanced relational database associated with filename. The database will be opened with base-table type base-table-type) if supplied. If base-table-type is not supplied, open-database will attempt to deduce the correct base-table-type. If the database can not be opened or if it lacks the *commands* table, #f is returned.

Function: open-database! filename
Function: open-database! filename base-table-type
Returns mutable open enchanced relational database ...

The table *commands* in an enhanced relational-database has the fields (with domains):

PRI name        symbol
    parameters  parameter-list
    procedure   expression
    documentation string

The parameters field is a foreign key (domain parameter-list) of the *catalog-data* table and should have the value of a table described by *parameter-columns*. This parameter-list table describes the arguments suitable for passing to the associated command. The intent of this table is to be of a form such that different user-interfaces (for instance, pull-down menus or plain-text queries) can operate from the same table. A parameter-list table has the following fields:

PRI index       uint
    name        symbol
    arity       parameter-arity
    domain      domain
    defaulter   expression
    expander    expression
    documentation string

The arity field can take the values:

single
Requires a single parameter of the specified domain.
optional
A single parameter of the specified domain or zero parameters is acceptable.
boolean
A single boolean parameter or zero parameters (in which case #f is substituted) is acceptable.
nary
Any number of parameters of the specified domain are acceptable. The argument passed to the command function is always a list of the parameters.
nary1
One or more of parameters of the specified domain are acceptable. The argument passed to the command function is always a list of the parameters.

The domain field specifies the domain which a parameter or parameters in the indexth field must satisfy.

The defaulter field is an expression whose value is either #f or a procedure of one argument (the parameter-list) which returns a list of the default value or values as appropriate. Note that since the defaulter procedure is called every time a default parameter is needed for this column, sticky defaults can be implemented using shared state with the domain-integrity-rule.

Invoking Commands

When an enhanced relational-database is called with a symbol which matches a name in the *commands* table, the associated procedure expression is evaluated and applied to the enhanced relational-database. A procedure should then be returned which the user can invoke on (optional) arguments.

The command *initialize* is special. If present in the *commands* table, open-database or open-database! will return the value of the *initialize* command. Notice that arbitrary code can be run when the *initialize* procedure is automatically applied to the enhanced relational-database.

Note also that if you wish to shadow or hide from the user relational-database methods described in section Relational Database Operations, this can be done by a dispatch in the closure returned by the *initialize* expression rather than by entries in the *commands* table if it is desired that the underlying methods remain accessible to code in the *commands* table.

Function: make-command-server rdb table-name
Returns a procedure of 2 arguments, a (symbol) command and a call-back procedure. When this returned procedure is called, it looks up command in table table-name and calls the call-back procedure with arguments:
command
The command
command-value
The result of evaluating the expression in the procedure field of table-name and calling it with rdb.
parameter-name
A list of the official name of each parameter. Corresponds to the name field of the command's parameter-table.
positions
A list of the positive integer index of each parameter. Corresponds to the index field of the command's parameter-table.
arities
A list of the arities of each parameter. Corresponds to the arity field of the command's parameter-table. For a description of arity see table above.
types
A list of the type name of each parameter. Correspnds to the type-id field of the contents of the domain of the command's parameter-table.
defaulters
A list of the defaulters for each parameter. Corresponds to the defaulters field of the command's parameter-table.
domain-integrity-rules
A list of procedures (one for each parameter) which tests whether a value for a parameter is acceptable for that parameter. The procedure should be called with each datum in the list for nary arity parameters.
aliases
A list of lists of (alias parameter-name). There can be more than one alias per parameter-name.

For information about parameters, See section Parameter lists. Here is an example of setting up a command with arguments and parsing those arguments from a getopt style argument list (see section Getopt).

(require 'database-utilities)
(require 'fluid-let)
(require 'parameters)
(require 'getopt)

(define my-rdb (create-database #f 'alist-table))

(define-tables my-rdb
  '(foo-params
    *parameter-columns*
    *parameter-columns*
    ((1 single-string single string
        (lambda (pl) '("str")) #f "single string")
     (2 nary-symbols nary symbol
        (lambda (pl) '()) #f "zero or more symbols")
     (3 nary1-symbols nary1 symbol
        (lambda (pl) '(symb)) #f "one or more symbols")
     (4 optional-number optional uint
        (lambda (pl) '()) #f "zero or one number")
     (5 flag boolean boolean
        (lambda (pl) '(#f)) #f "a boolean flag")))
  '(foo-pnames
    ((name string))
    ((parameter-index uint))
    (("s" 1)
     ("single-string" 1)
     ("n" 2)
     ("nary-symbols" 2)
     ("N" 3)
     ("nary1-symbols" 3)
     ("o" 4)
     ("optional-number" 4)
     ("f" 5)
     ("flag" 5)))
  '(my-commands
    ((name symbol))
    ((parameters parameter-list)
     (parameter-names parameter-name-translation)
     (procedure expression)
     (documentation string))
    ((foo
      foo-params
      foo-pnames
      (lambda (rdb) (lambda args (print args)))
      "test command arguments"))))

(define (dbutil:serve-command-line rdb command-table
                                   command argc argv)
  (set! argv (if (vector? argv) (vector->list argv) argv))
  ((make-command-server rdb command-table)
   command
   (lambda (comname comval options positions
                    arities types defaulters dirs aliases) 
     (apply comval (getopt->arglist
                    argc argv options positions
                    arities types defaulters dirs aliases)))))

(define (cmd . opts)
  (fluid-let ((*optind* 1))
    (printf "%-34s => "
            (call-with-output-string (lambda (pt) (write (cons 'cmd opts) pt)))
            ;;(apply string-append (map (lambda (x) (string-append x " ")) opts))
            )
    (set! opts (cons "cmd" opts))
    (force-output)
    (dbutil:serve-command-line
     my-rdb 'my-commands 'foo (length opts) opts)))

(cmd)                              => ("str" () (symb) () #f) 
(cmd "-f")                         => ("str" () (symb) () #t) 
(cmd "--flag")                     => ("str" () (symb) () #t) 
(cmd "-o177")                      => ("str" () (symb) (177) #f) 
(cmd "-o" "177")                   => ("str" () (symb) (177) #f) 
(cmd "--optional" "621")           => ("str" () (symb) (621) #f) 
(cmd "--optional=621")             => ("str" () (symb) (621) #f) 
(cmd "-s" "speciality")            => ("speciality" () (symb) () #f) 
(cmd "-sspeciality")               => ("speciality" () (symb) () #f) 
(cmd "--single" "serendipity")     => ("serendipity" () (symb) () #f) 
(cmd "--single=serendipity")       => ("serendipity" () (symb) () #f) 
(cmd "-n" "gravity" "piety")       => ("str" () (piety gravity) () #f) 
(cmd "-ngravity" "piety")          => ("str" () (piety gravity) () #f) 
(cmd "--nary" "chastity")          => ("str" () (chastity) () #f) 
(cmd "--nary=chastity" "")         => ("str" () ( chastity) () #f) 
(cmd "-N" "calamity")              => ("str" () (calamity) () #f) 
(cmd "-Ncalamity")                 => ("str" () (calamity) () #f) 
(cmd "--nary1" "surety")           => ("str" () (surety) () #f) 
(cmd "--nary1=surety")             => ("str" () (surety) () #f) 
(cmd "-N" "levity" "fealty")       => ("str" () (fealty levity) () #f) 
(cmd "-Nlevity" "fealty")          => ("str" () (fealty levity) () #f) 
(cmd "--nary1" "surety" "brevity") => ("str" () (brevity surety) () #f) 
(cmd "--nary1=surety" "brevity")   => ("str" () (brevity surety) () #f) 
(cmd "-?")
-| 
Usage: cmd [OPTION ARGUMENT ...] ...

  -f, --flag 
  -o, --optional[=]<number> 
  -n, --nary[=]<symbols> ...
  -N, --nary1[=]<symbols> ...
  -s, --single[=]<string> 

ERROR: getopt->parameter-list "unrecognized option" "-?"

Some commands are defined in all extended relational-databases. The are called just like section Relational Database Operations.

Function: add-domain domain-row
Adds domain-row to the domains table if there is no row in the domains table associated with key (car domain-row) and returns #t. Otherwise returns #f.

For the fields and layout of the domain table, See section Catalog Representation. Currently, these fields are

The following example adds 3 domains to the `build' database. `Optstring' is either a string or #f. filename is a string and build-whats is a symbol.

(for-each (build 'add-domain)
          '((optstring #f
                       (lambda (x) (or (not x) (string? x)))
                       string
                       #f)
            (filename #f #f string #f)
            (build-whats #f #f symbol #f)))

Function: delete-domain domain-name
Removes and returns the domain-name row from the domains table.

Function: domain-checker domain
Returns a procedure to check an argument for conformance to domain domain.

Defining Tables

Procedure: define-tables rdb spec-0 ...
Adds tables as specified in spec-0 ... to the open relational-database rdb. Each spec has the form:

(<name> <descriptor-name> <descriptor-name> <rows>)

or

(<name> <primary-key-fields> <other-fields> <rows>)

where <name> is the table name, <descriptor-name> is the symbol name of a descriptor table, <primary-key-fields> and <other-fields> describe the primary keys and other fields respectively, and <rows> is a list of data rows to be added to the table.

<primary-key-fields> and <other-fields> are lists of field descriptors of the form:

(<column-name> <domain>)

or

(<column-name> <domain> <column-integrity-rule>)

where <column-name> is the column name, <domain> is the domain of the column, and <column-integrity-rule> is an expression whose value is a procedure of one argument (which returns #f to signal an error).

If <domain> is not a defined domain name and it matches the name of this table or an already defined (in one of spec-0 ...) single key field table, a foriegn-key domain will be created for it.

Procedure: create-report rdb destination report-name table
Procedure: create-report rdb destination report-name
The symbol report-name must be primary key in the table named *reports* in the relational database rdb. destination is a port, string, or symbol. If destination is a:

port
The table is created as ascii text and written to that port.
string
The table is created as ascii text and written to the file named by destination.
symbol
destination is the primary key for a row in the table named *printers*.

Each row in the table *reports* has the fields:

name
The report name.
default-table
The table to report on if none is specified.
header, footer
A format string. At the beginning and end of each page respectively, format is called with this string and the (list of) column-names of this table.
reporter
A format string. For each row in the table, format is called with this string and the row.
minimum-break
The minimum number of lines into which the report lines for a row can be broken. Use 0 if a row's lines should not be broken over page boundaries.

Each row in the table *printers* has the fields:

name
The printer name.
print-procedure
The procedure to call to actually print.

The report is prepared as follows:

The following example shows a new database with the name of `foo.db' being created with tables describing processor families and processor/os/compiler combinations.

The database command define-tables is defined to call define-tables with its arguments. The database is also configured to print `Welcome' when the database is opened. The database is then closed and reopened.

(require 'database-utilities)
(define my-rdb (create-database "foo.db" 'alist-table))

(define-tables my-rdb
  '(*commands*
    ((name symbol))
    ((parameters parameter-list)
     (procedure expression)
     (documentation string))
    ((define-tables
      no-parameters
      no-parameter-names
      (lambda (rdb) (lambda specs (apply define-tables rdb specs)))
      "Create or Augment tables from list of specs")
     (*initialize*
      no-parameters
      no-parameter-names
      (lambda (rdb) (display "Welcome") (newline) rdb)
      "Print Welcome"))))

((my-rdb 'define-tables)
 '(processor-family
   ((family    atom))
   ((also-ran  processor-family))
   ((m68000           #f)
    (m68030           m68000)
    (i386             8086)
    (8086             #f)
    (powerpc          #f)))

 '(platform
   ((name      symbol))
   ((processor processor-family)
    (os        symbol)
    (compiler  symbol))
   ((aix              powerpc aix     -)
    (amiga-dice-c     m68000  amiga   dice-c)
    (amiga-aztec      m68000  amiga   aztec)
    (amiga-sas/c-5.10 m68000  amiga   sas/c)
    (atari-st-gcc     m68000  atari   gcc)
    (atari-st-turbo-c m68000  atari   turbo-c)
    (borland-c-3.1    8086    ms-dos  borland-c)
    (djgpp            i386    ms-dos  gcc)
    (linux            i386    linux   gcc)
    (microsoft-c      8086    ms-dos  microsoft-c)
    (os/2-emx         i386    os/2    gcc)
    (turbo-c-2        8086    ms-dos  turbo-c)
    (watcom-9.0       i386    ms-dos  watcom))))

((my-rdb 'close-database))

(set! my-rdb (open-database "foo.db" 'alist-table))
-|
Welcome

Weight-Balanced Trees

(require 'wt-tree)

Balanced binary trees are a useful data structure for maintaining large sets of ordered objects or sets of associations whose keys are ordered. MIT Scheme has an comprehensive implementation of weight-balanced binary trees which has several advantages over the other data structures for large aggregates:

These features make weight-balanced trees suitable for a wide range of applications, especially those that require large numbers of sets or discrete maps. Applications that have a few global databases and/or concentrate on element-level operations like insertion and lookup are probably better off using hash-tables or red-black trees.

The size of a tree is the number of associations that it contains. Weight balanced binary trees are balanced to keep the sizes of the subtrees of each node within a constant factor of each other. This ensures logarithmic times for single-path operations (like lookup and insertion). A weight balanced tree takes space that is proportional to the number of associations in the tree. For the current implementation, the constant of proportionality is six words per association.

Weight balanced trees can be used as an implementation for either discrete sets or discrete maps (associations). Sets are implemented by ignoring the datum that is associated with the key. Under this scheme if an associations exists in the tree this indicates that the key of the association is a member of the set. Typically a value such as (), #t or #f is associated with the key.

Many operations can be viewed as computing a result that, depending on whether the tree arguments are to thhe arities of each parameter. Corresponds to the arity field of the command's parameter-table. For a description of arity see table above.

types
A list of the type name of each parameter. Correspnds to the type-id field of the contents of the domain of the command's parameter-table.
defaulters
A list of the defaulters for each parameter. Corresponds to the defaulters field of the command's parameter-table.
domain-integrity-rules
A list of procedures (one for each parameter) which tests whether a value for a parameter is acceptable for that parameter. The procedure should be called with each datum in the list for nary arity parameters.
aliases
A list of lists of (alias parameter-name). There can be more than one alias per parameter-name.

For information about parameters, See section Parameter lists. Here is an example of setting up a command with arguments and parsing those arguments from a getopt style argument list (see section Getopt).

(require 'database-utilities)
(require 'fluid-let)
(require 'parameters)
(require 'getopt)

(define my-rdb (create-database #f 'alist-table))

(define-tables my-rdb
  '(foo-params
    *parameter-columns*
    *parameter-columns*
    ((1 single-string single string
        (lambda (pl) '("str")) #f "single string")
     (2 nary-symbols nary symbol
        (lambda (pl) '()) #f "zero or more symbols")
     (3 nary1-symbols nary1 symbol
        (lambda (pl) '(symb)) #f "one or more symbols")
     (4 optional-number optional uint
        (lambda (pl) '()) #f "zero or one number")
     (5 flag boolean boolean
        (lambda (pl) '(#f)) #f "a boolean flag")))
  '(foo-pnames
    ((name string))
    ((parameter-index uint))
    (("s" 1)
     ("single-string" 1)
     ("n" 2)
     ("nary-symbols" 2)
     ("N" 3)
     ("nary1-symbols" 3)
     ("o" 4)
     ("optional-number" 4)
     ("f" 5)
     ("flag" 5)))
  '(my-commands
    ((name symbol))
    ((parameters parameter-list)
     (parameter-names parameter-name-translation)
     (procedure expression)
     (documentation string))
    ((foo
      foo-params
      foo-pnames
      (lambda (rdb) (lambda args (print args)))
      "test command arguments"))))

(define (dbutil:serve-command-line rdb command-table
                                   command argc argv)
  (set! argv (if (vector? argv) (vector->list argv) argv))
  ((make-command-server rdb command-table)
   command
   (lambda (comname comval options positions
                    arities types defaulters dirs aliases) 
     (apply comval (getopt->arglist
                    argc argv options positions
                    arities types defaulters dirs aliases)))))

(define (cmd . opts)
  (fluid-let ((*optind* 1))
    (printf "%-34s => "
            (call-with-output-string (lambda (pt) (write (cons 'cmd opts) pt)))
            ;;(apply string-append (map (lambda (x) (string-append x " ")) opts))
            )
    (set! opts (cons "cmd" opts))
    (force-output)
    (dbutil:serve-command-line
     my-rdb 'my-commands 'foo (length opts) opts)))

(cmd)                              => ("str" () (symb) () #f) 
(cmd "-f")                         => ("str" () (symb) () #t) 
(cmd "--flag")                     => ("str" () (symb) () #t) 
(cmd "-o177")                      => ("str" () (symb) (177) #f) 
(cmd "-o" "177")                   => ("str" () (symb) (177) #f) 
(cmd "--optional" "621")           => ("str" () (symb) (621) #f) 
(cmd "--optional=621")             => ("str" () (symb) (621) #f) 
(cmd "-s" "speciality")            => ("speciality" () (symb) () #f) 
(cmd "-sspeciality")               => ("speciality" () (symb) () #f) 
(cmd "--single" "serendipity")     => ("serendipity" () (symb) () #f) 
(cmd "--single=serendipity")       => ("serendipity" () (symb) () #f) 
(cmd "-n" "gravity" "piety")       => ("str" () (piety gravity) () #f) 
(cmd "-ngravity" "piety")          => ("str" () (piety gravity) () #f) 
(cmd "--nary" "chastity")          => ("str" () (chastity) () #f) 
(cmd "--nary=chastity" "")         => ("str" () ( chastity) () #f) 
(cmd "-N" "calamity")              => ("str" () (calamity) () #f) 
(cmd "-Ncalamity")                 => ("str" () (calamity) () #f) 
(cmd "--nary1" "surety")           => ("str" () (surety) () #f) 
(cmd "--nary1=surety")             => ("str" () (surety) () #f) 
(cmd "-N" "levity" "fealty")       => ("str" () (fealty levity) () #f) 
(cmd "-Nlevity" "fealty")          => ("str" () (fealty levity) () #f) 
(cmd "--nary1" "surety" "brevity") => ("str" () (brevity surety) () #f) 
(cmd "--nary1=surety" "brevity")   => ("str" () (brevity surety) () #f) 
(cmd "-?")
-| 
Usage: cmd [OPTION ARGUMENT ...] ...

  -f, --flag 
  -o, --optional[=]<number> 
  -n, --nary[=]<symbols> ...
  -N, --nary1[=]<symbols> ...
  -s, --single[=]<string> 

ERROR: getopt->parameter-list "unrecognized option" "-?"

Some commands are defined in all extended relational-databases. The are called just like section Relational Database Operations.

Function: add-domain domain-row
Adds domain-row to the domains table if there is no row in the domains table associated with key (car domain-row) and returns #t. Otherwise returns #f.

For the fields and layout of the domain table, See section Catalog Representation. Currently, these fields are

  • domain-name
  • foreign-table
  • domain-integrity-rule
  • type-id
  • type-param

The following example adds 3 domains to the `build' database. `Optstring' is either a string or #f. filename is a string and build-whats is a symbol.

(for-each (build 'add-domain)
          '((optstring #f
                       (lambda (x) (or (not x) (string? x)))
                       string
                       #f)
            (filename #f #f string #f)
            (build-whats #f #f symbol #f)))

Function: delete-domain domain-name
Removes and returns the domain-name row from the domains table.

Function: domain-checker domain
Returns a procedure to check an argument for conformance to domain domain.

Defining Tables

Procedure: define-tables rdb spec-0 ...
Adds tables as specified in spec-0 ... to the open relational-database rdb. Each spec has the form:

(<name> <descriptor-name> <descriptor-name> <rows>)

or

(<name> <primary-key-fields> <other-fields> <rows>)

where <name> is the table name, <descriptor-name> is the symbol name of a descriptor table, <primary-key-fields> and <other-fields> describe the primary keys and other fields respectively, and <rows> is a list of data rows to be added to the table.

<primary-key-fields> and <other-fields> are lists of field descriptors of the form:

(<column-name> <domain>)

or

(<column-name> <domain> <column-integrity-rule>)

where <column-name> is the column name, <domain> is the domain of the column, and <column-integrity-rule> is an expression whose value is a procedure of one argument (which returns #f to signal an error).

If <domain> is not a defined domain name and it matches the name of this table or an already defined (in one of spec-0 ...) single key field table, a foriegn-key domain will be created for it.

Procedure: create-report rdb destination report-name table
Procedure: create-report rdb destination report-name
The symbol report-name must be primary key in the table named *reports* in the relational database rdb. destination is a port, string, or symbol. If destination is a:

port
The table is created as ascii text and written to that port.
string
The table is created as ascii text and written to the file named by destination.
symbol
destination is the primary key for a row in the table named *printers*.

Each row in the table *reports* has the fields:

name
The report name.
default-table
The table to report on if none is specified.
header, footer
A format string. At the beginning and end of each page respectively, format is called with this string and the (list of) column-names of this table.
reporter
A format string. For each row in the table, format is called with this string and the row.
minimum-break
The minimum number of lines into which the report lines for a row can be broken. Use 0 if a row's lines should not be broken over page boundaries.

Each row in the table *printers* has the fields:

name
The printer name.
print-procedure
The procedure to call to actually print.

The report is prepared as follows:

  • Format (see section Format (version 3.0)) is called with the header field and the (list of) column-names of the table.
  • Format is called with the reporter field and (on successive calls) each record in the natural order for the table. A count is kept of the number of newlines output by format. When the number of newlines to be output exceeds the number of lines per page, the set of lines will be broken if there are more than minimum-break left on this page and the number of lines for this row is larger or equal to twice minimum-break.
  • Format is called with the footer field and the (list of) column-names of the table. The footer field should not output a newline.
  • A new page is output.
  • This entire process repeats until all the rows are output.

The following example shows a new database with the name of `foo.db' being created with tables describing processor families and processor/os/compiler combinations.

The database command define-tables is defined to call define-tables with its arguments. The database is also configured to print `Welcome' when the database is opened. The database is then closed and reopened.

(require 'database-utilities)
(define my-rdb (create-database "foo.db" 'alist-table))

(define-tables my-rdb
  '(*commands*
    ((name symbol))
    ((parameters parameter-list)
     (procedure expression)
     (documentation string))
    ((define-tables
      no-parameters
      no-parameter-names
      (lambda (rdb) (lambda specs (apply define-tables rdb specs)))
      "Create or Augment tables from list of specs")
     (*initialize*
      no-parameters
      no-parameter-names
      (lambda (rdb) (display "Welcome") (newline) rdb)
      "Print Welcome"))))

((my-rdb 'define-tables)
 '(processor-family
   ((family    atom))
   ((also-ran  processor-family))
   ((m68000           #f)
    (m68030           m68000)
    (i386             8086)
    (8086             #f)
    (powerpc          #f)))

 '(platform
   ((name      symbol))
   ((processor processor-family)
    (os        symbol)
    (compiler  symbol))
   ((aix              powerpc aix     -)
    (amiga-dice-c     m68000  amiga   dice-c)
    (amiga-aztec      m68000  amiga   aztec)
    (amiga-sas/c-5.10 m68000  amiga   sas/c)
    (atari-st-gcc     m68000  atari   gcc)
    (atari-st-turbo-c m68000  atari   turbo-c)
    (borland-c-3.1    8086    ms-dos  borland-c)
    (djgpp            i386    ms-dos  gcc)
    (linux            i386    linux   gcc)
    (microsoft-c      8086    ms-dos  microsoft-c)
    (os/2-emx         i386    os/2    gcc)
    (turbo-c-2        8086    ms-dos  turbo-c)
    (watcom-9.0       i386    ms-dos  watcom))))

((my-rdb 'close-database))

(set! my-rdb (open-database "foo.db" 'alist-table))
-|
Welcome

Weight-Balanced Trees

(require 'wt-tree)

Balanced binary trees are a useful data structure for maintaining large sets of ordered objects or sets of associations whose keys are ordered. MIT Scheme has an comprehensive implementation of weight-balanced binary trees which has several advantages over the other data structures for large aggregates:

These features make weight-balanced trees suitable for a wide range of applications, especially those that require large numbers of sets or discrete maps. Applications that have a few global databases and/or concentrate on element-level operations like insertion and lookup are probably better off using hash-tables or red-black trees.

The size of a tree is the number of associations that it contains. Weight balanced binary trees are balanced to keep the sizes of the subtrees of each node within a constant factor of each other. This ensures logarithmic times for single-path operations (like lookup and insertion). A weight balanced tree takes space that is proportional to the number of associations in the tree. For the current implementation, the constant of proportionality is six words per association.

Weight balanced trees can be used as an implementation for either discrete sets or discrete maps (associations). Sets are implemented by ignoring the datum that is associated with the key. Under this scheme if an associations exists in the tree this indicates that the key of the association is a member of the set. Typically a value such as (), #t or #f is associated with the key.

Many operations can be viewed as computing a result that, depending on whether the tree arguments are to thhe arities of each parameter. Corresponds to the arity field of the command's parameter-table. For a description of arity see table above.

types
A list of the type name of each parameter. Correspnds to the type-id field of the contents of the domain of the command's parameter-table.
defaulters
A list of the defaulters for each parameter. Corresponds to the defaulters field of the command's parameter-table.
domain-integrity-rules
A list of procedures (one for each parameter) which tests whether a value for a parameter is acceptable for that parameter. The procedure should be called with each datum in the list for nary arity parameters.
aliases
A list of lists of (alias parameter-name). There can be more than one alias per parameter-name.

For information about parameters, See section Parameter lists. Here is an example of setting up a command with arguments and parsing those arguments from a getopt style argument list (see section Getopt).

(require 'database-utilities)
(require 'fluid-let)
(require 'parameters)
(require 'getopt)

(define my-rdb (create-database #f 'alist-table))

(define-tables my-rdb
  '(foo-params
    *parameter-columns*
    *parameter-columns*
    ((1 single-string single string
        (lambda (pl) '("str")) #f "single string")
     (2 nary-symbols nary symbol
        (lambda (pl) '()) #f "zero or more symbols")
     (3 nary1-symbols nary1 symbol
        (lambda (pl) '(symb)) #f "one or more symbols")
     (4 optional-number optional uint
        (lambda (pl) '()) #f "zero or one number")
     (5 flag boolean boolean
        (lambda (pl) '(#f)) #f "a boolean flag")))
  '(foo-pnames
    ((name string))
    ((parameter-index uint))
    (("s" 1)
     ("single-string" 1)
     ("n" 2)
     ("nary-symbols" 2)
     ("N" 3)
     ("nary1-symbols" 3)
     ("o" 4)
     ("optional-number" 4)
     ("f" 5)
     ("flag" 5)))
  '(my-commands
    ((name symbol))
    ((parameters parameter-list)
     (parameter-names parameter-name-translation)
     (procedure expression)
     (documentation string))
    ((foo
      foo-params
      foo-pnames
      (lambda (rdb) (lambda args (print args)))
      "test command arguments"))))

(define (dbutil:serve-command-line rdb command-table
                                   command argc argv)
  (set! argv (if (vector? argv) (vector->list argv) argv))
  ((make-command-server rdb command-table)
   command
   (lambda (comname comval options positions
                    arities types defaulters dirs aliases) 
     (apply comval (getopt->arglist
                    argc argv options positions
                    arities types defaulters dirs aliases)))))

(define (cmd . opts)
  (fluid-let ((*optind* 1))
    (printf "%-34s => "
            (call-with-output-string (lambda (pt) (write (cons 'cmd opts) pt)))
            ;;(apply string-append (map (lambda (x) (string-append x " ")) opts))
            )
    (set! opts (cons "cmd" opts))
    (force-output)
    (dbutil:serve-command-line
     my-rdb 'my-commands 'foo (length opts) opts)))

(cmd)                              => ("str" () (symb) () #f) 
(cmd "-f")                         => ("str" () (symb) () #t) 
(cmd "--flag")                     => ("str" () (symb) () #t) 
(cmd "-o177")                      => ("str" () (symb) (177) #f) 
(cmd "-o" "177")                   => ("str" () (symb) (177) #f) 
(cmd "--optional" "621")           => ("str" () (symb) (621) #f) 
(cmd "--optional=621")             => ("str" () (symb) (621) #f) 
(cmd "-s" "speciality")            => ("speciality" () (symb) () #f) 
(cmd "-sspeciality")               => ("speciality" () (symb) () #f) 
(cmd "--single" "serendipity")     => ("serendipity" () (symb) () #f) 
(cmd "--single=serendipity")       => ("serendipity" () (symb) () #f) 
(cmd "-n" "gravity" "piety")       => ("str" () (piety gravity) () #f) 
(cmd "-ngravity" "piety")          => ("str" () (piety gravity) () #f) 
(cmd "--nary" "chastity")          => ("str" () (chastity) () #f) 
(cmd "--nary=chastity" "")         => ("str" () ( chastity) () #f) 
(cmd "-N" "calamity")              => ("str" () (calamity) () #f) 
(cmd "-Ncalamity")                 => ("str" () (calamity) () #f) 
(cmd "--nary1" "surety")           => ("str" () (surety) () #f) 
(cmd "--nary1=surety")             => ("str" () (surety) () #f) 
(cmd "-N" "levity" "fealty")       => ("str" () (fealty levity) () #f) 
(cmd "-Nlevity" "fealty")          => ("str" () (fealty levity) () #f) 
(cmd "--nary1" "surety" "brevity") => ("str" () (brevity surety) () #f) 
(cmd "--nary1=surety" "brevity")   => ("str" () (brevity surety) () #f) 
(cmd "-?")
-| 
Usage: cmd [OPTION ARGUMENT ...] ...

  -f, --flag 
  -o, --optional[=]<number> 
  -n, --nary[=]<symbols> ...
  -N, --nary1[=]<symbols> ...
  -s, --single[=]<string> 

ERROR: getopt->parameter-list "unrecognized option" "-?"

Some commands are defined in all extended relational-databases. The are called just like section Relational Database Operations.

Function: add-domain domain-row
Adds domain-row to the domains table if there is no row in the domains table associated with key (car domain-row) and returns #t. Otherwise returns #f.

For the fields and layout of the domain table, See section Catalog Representation. Currently, these fields are

  • domain-name
  • foreign-table
  • domain-integrity-rule
  • type-id
  • type-param

The following example adds 3 domains to the `build' database. `Optstring' is either a string or #f. filename is a string and build-whats is a symbol.

(for-each (build 'add-domain)
          '((optstring #f
                       (lambda (x) (or (not x) (string? x)))
                       string
                       #f)
            (filename #f #f string #f)
            (build-whats #f #f symbol #f)))

Function: delete-domain domain-name
Removes and returns the domain-name row from the domains table.

Function: domain-checker domain
Returns a procedure to check an argument for conformance to domain domain.

Defining Tables

Procedure: define-tables rdb spec-0 ...
Adds tables as specified in spec-0 ... to the open relational-database rdb. Each spec has the form:

(<name> <descriptor-name> <descriptor-name> <rows>)

or

(<name> <primary-key-fields> <other-fields> <rows>)

where <name> is the table name, <descriptor-name> is the symbol name of a descriptor table, <primary-key-fields> and <other-fields> describe the primary keys and other fields respectively, and <rows> is a list of data rows to be added to the table.

<primary-key-fields> and <other-fields> are lists of field descriptors of the form:

(<column-name> <domain>)

or

(<column-name> <domain> <column-integrity-rule>)

where <column-name> is the column name, <domain> is the domain of the column, and <column-integrity-rule> is an expression whose value is a procedure of one argument (which returns #f to signal an error).

If <domain> is not a defined domain name and it matches the name of this table or an already defined (in one of spec-0 ...) single key field table, a foriegn-key domain will be created for it.

Procedure: create-report rdb destination report-name table
Procedure: create-report rdb destination report-name
The symbol report-name must be primary key in the table named *reports* in the relational database rdb. destination is a port, string, or symbol. If destination is a:

port
The table is created as ascii text and written to that port.
string
The table is created as ascii text and written to the file named by destination.
symbol
destination is the primary key for a row in the table named *printers*.

Each row in the table *reports* has the fields:

name
The report name.
default-table
The table to report on if none is specified.
header, footer
A format string. At the beginning and end of each page respectively, format is called with this string and the (list of) column-names of this table.
reporter
A format string. For each row in the table, format is called with this string and the row.
minimum-break
The minimum number of lines into which the report lines for a row can be broken. Use 0 if a row's lines should not be broken over page boundaries.

Each row in the table *printers* has the fields:

name
The printer name.
print-procedure
The procedure to call to actually print.

The report is prepared as follows:

  • Format (see section Format (version 3.0)) is called with the header field and the (list of) column-names of the table.
  • Format is called with the reporter field and (on successive calls) each record in the natural order for the table. A count is kept of the number of newlines output by format. When the number of newlines to be output exceeds the number of lines per page, the set of lines will be broken if there are more than minimum-break left on this page and the number of lines for this row is larger or equal to twice minimum-break.
  • Format is called with the footer field and the (list of) column-names of the table. The footer field should not output a newline.
  • A new page is output.
  • This entire process repeats until all the rows are output.

The following example shows a new database with the name of `foo.db' being created with tables describing processor families and processor/os/compiler combinations.

The database command define-tables is defined to call define-tables with its arguments. The database is also configured to print `Welcome' when the database is opened. The database is then closed and reopened.

(require 'database-utilities)
(define my-rdb (create-database "foo.db" 'alist-table))

(define-tables my-rdb
  '(*commands*
    ((name symbol))
    ((parameters parameter-list)
     (procedure expression)
     (documentation string))
    ((define-tables
      no-parameters
      no-parameter-names
      (lambda (rdb) (lambda specs (apply define-tables rdb specs)))
      "Create or Augment tables from list of specs")
     (*initialize*
      no-parameters
      no-parameter-names
      (lambda (rdb) (display "Welcome") (newline) rdb)
      "Print Welcome"))))

((my-rdb 'define-tables)
 '(processor-family
   ((family    atom))
   ((also-ran  processor-family))
   ((m68000           #f)
    (m68030           m68000)
    (i386             8086)
    (8086             #f)
    (powerpc          #f)))

 '(platform
   ((name      symbol))
   ((processor processor-family)
    (os        symbol)
    (compiler  symbol))
   ((aix              powerpc aix     -)
    (amiga-dice-c     m68000  amiga   dice-c)
    (amiga-aztec      m68000  amiga   aztec)
    (amiga-sas/c-5.10 m68000  amiga   sas/c)
    (atari-st-gcc     m68000  atari   gcc)
    (atari-st-turbo-c m68000  atari   turbo-c)
    (borland-c-3.1    8086    ms-dos  borland-c)
    (djgpp            i386    ms-dos  gcc)
    (linux            i386    linux   gcc)
    (microsoft-c      8086    ms-dos  microsoft-c)
    (os/2-emx         i386    os/2    gcc)
    (turbo-c-2        8086    ms-dos  turbo-c)
    (watcom-9.0       i386    ms-dos  watcom))))

((my-rdb 'close-database))

(set! my-rdb (open-database "foo.db" 'alist-table))
-|
Welcome

Weight-Balanced Trees

(require 'wt-tree)

Balanced binary trees are a useful data structure for maintaining large sets of ordered objects or sets of associations whose keys are ordered. MIT Scheme has an comprehensive implementation of weight-balanced binary trees which has several advantages over the other data structures for large aggregates:

These features make weight-balanced trees suitable for a wide range of applications, especially those that require large numbers of sets or discrete maps. Applications that have a few global databases and/or concentrate on element-level operations like insertion and lookup are probably better off using hash-tables or red-black trees.

The size of a tree is the number of associations that it contains. Weight balanced binary trees are balanced to keep the sizes of the subtrees of each node within a constant factor of each other. This ensures logarithmic times for single-path operations (like lookup and insertion). A weight balanced tree takes space that is proportional to the number of associations in the tree. For the current implementation, the constant of proportionality is six words per association.

Weight balanced trees can be used as an implementation for either discrete sets or discrete maps (associations). Sets are implemented by ignoring the datum that is associated with the key. Under this scheme if an associations exists in the tree this indicates that the key of the association is a member of the set. Typically a value such as (), #t or #f is associated with the key.

Many operations can be viewed as computing a result that, depending on whether the tree arguments are to thhe arities of each parameter. Corresponds to the arity field of the command's parameter-table. For a description of arity see table above.

types
A list of the type name of each parameter. Correspnds to the type-id field of the contents of the domain of the command's parameter-table.
defaulters
A list of the defaulters for each parameter. Corresponds to the defaulters field of the command's parameter-table.
domain-integrity-rules
A list of procedures (one for each parameter) which tests whether a value for a parameter is acceptable for that parameter. The procedure should be called with each datum in the list for nary arity parameters.
aliases
A list of lists of (alias parameter-name). There can be more than one alias per parameter-name.

For information about parameters, See section Parameter lists. Here is an example of setting up a command with arguments and parsing those arguments from a getopt style argument list (see section Getopt).

(require 'database-utilities)
(require 'fluid-let)
(require 'parameters)
(require 'getopt)

(define my-rdb (create-database #f 'alist-table))

(define-tables my-rdb
  '(foo-params
    *parameter-columns*
    *parameter-columns*
    ((1 single-string single string
        (lambda (pl) '("str")) #f "single string")
     (2 nary-symbols nary symbol
        (lambda (pl) '()) #f "zero or more symbols")
     (3 nary1-symbols nary1 symbol
        (lambda (pl) '(symb)) #f "one or more symbols")
     (4 optional-number optional uint
        (lambda (pl) '()) #f "zero or one number")
     (5 flag boolean boolean
        (lambda (pl) '(#f)) #f "a boolean flag")))
  '(foo-pnames
    ((name string))
    ((parameter-index uint))
    (("s" 1)
     ("single-string" 1)
     ("n" 2)
     ("nary-symbols" 2)
     ("N" 3)
     ("nary1-symbols" 3)
     ("o" 4)
     ("optional-number" 4)
     ("f" 5)
     ("flag" 5)))
  '(my-commands
    ((name symbol))
    ((parameters parameter-list)
     (parameter-names parameter-name-translation)
     (procedure expression)
     (documentation string))
    ((foo
      foo-params
      foo-pnames
      (lambda (rdb) (lambda args (print args)))
      "test command arguments"))))

(define (dbutil:serve-command-line rdb command-table
                                   command argc argv)
  (set! argv (if (vector? argv) (vector->list argv) argv))
  ((make-command-server rdb command-table)
   command
   (lambda (comname comval options positions
                    arities types defaulters dirs aliases) 
     (apply comval (getopt->arglist
                    argc argv options positions
                    arities types defaulters dirs aliases)))))

(define (cmd . opts)
  (fluid-let ((*optind* 1))
    (printf "%-34s => "
            (call-with-output-string (lambda (pt) (write (cons 'cmd opts) pt)))
            ;;(apply string-append (map (lambda (x) (string-append x " ")) opts))
            )
    (set! opts (cons "cmd" opts))
    (force-output)
    (dbutil:serve-command-line
     my-rdb 'my-commands 'foo (length opts) opts)))

(cmd)                              => ("str" () (symb) () #f) 
(cmd "-f")                         => ("str" () (symb) () #t) 
(cmd "--flag")                     => ("str" () (symb) () #t) 
(cmd "-o177")                      => ("str" () (symb) (177) #f) 
(cmd "-o" "177")                   => ("str" () (symb) (177) #f) 
(cmd "--optional" "621")           => ("str" () (symb) (621) #f) 
(cmd "--optional=621")             => ("str" () (symb) (621) #f) 
(cmd "-s" "speciality")            => ("speciality" () (symb) () #f) 
(cmd "-sspeciality")               => ("speciality" () (symb) () #f) 
(cmd "--single" "serendipity")     => ("serendipity" () (symb) () #f) 
(cmd "--single=serendipity")       => ("serendipity" () (symb) () #f) 
(cmd "-n" "gravity" "piety")       => ("str" () (piety gravity) () #f) 
(cmd "-ngravity" "piety")          => ("str" () (piety gravity) () #f) 
(cmd "--nary" "chastity")          => ("str" () (chastity) () #f) 
(cmd "--nary=chastity" "")         => ("str" () ( chastity) () #f) 
(cmd "-N" "calamity")              => ("str" () (calamity) () #f) 
(cmd "-Ncalamity")                 => ("str" () (calamity) () #f) 
(cmd "--nary1" "surety")           => ("str" () (surety) () #f) 
(cmd "--nary1=surety")             => ("str" () (surety) () #f) 
(cmd "-N" "levity" "fealty")       => ("str" () (fealty levity) () #f) 
(cmd "-Nlevity" "fealty")          => ("str" () (fealty levity) () #f) 
(cmd "--nary1" "surety" "brevity") => ("str" () (brevity surety) () #f) 
(cmd "--nary1=surety" "brevity")   => ("str" () (brevity surety) () #f) 
(cmd "-?")
-| 
Usage: cmd [OPTION ARGUMENT ...] ...

  -f, --flag 
  -o, --optional[=]<number> 
  -n, --nary[=]<symbols> ...
  -N, --nary1[=]<symbols> ...
  -s, --single[=]<string> 

ERROR: getopt->parameter-list "unrecognized option" "-?"

Some commands are defined in all extended relational-databases. The are called just like section Relational Database Operations.

Function: add-domain domain-row
Adds domain-row to the domains table if there is no row in the domains table associated with key (car domain-row) and returns #t. Otherwise returns #f.

For the fields and layout of the domain table, See section Catalog Representation. Currently, these fields are

  • domain-name
  • foreign-table
  • domain-integrity-rule
  • type-id
  • type-param

The following example adds 3 domains to the `build' database. `Optstring' is either a string or #f. filename is a string and build-whats is a symbol.

(for-each (build 'add-domain)
          '((optstring #f
                       (lambda (x) (or (not x) (string? x)))
                       string
                       #f)
            (filename #f #f string #f)
            (build-whats #f #f symbol #f)))

Function: delete-domain domain-name
Removes and returns the domain-name row from the domains table.

Function: domain-checker domain
Returns a procedure to check an argument for conformance to domain domain.

Defining Tables

Procedure: define-tables rdb spec-0 ...
Adds tables as specified in spec-0 ... to the open relational-database rdb. Each spec has the form:

(<name> <descriptor-name> <descriptor-name> <rows>)

or

(<name> <primary-key-fields> <other-fields> <rows>)

where <name> is the table name, <descriptor-name> is the symbol name of a descriptor table, <primary-key-fields> and <other-fields> describe the primary keys and other fields respectively, and <rows> is a list of data rows to be added to the table.

<primary-key-fields> and <other-fields> are lists of field descriptors of the form:

(<column-name> <domain>)

or

(<column-name> <domain> <column-integrity-rule>)

where <column-name> is the column name, <domain> is the domain of the column, and <column-integrity-rule> is an expression whose value is a procedure of one argument (which returns #f to signal an error).

If <domain> is not a defined domain name and it matches the name of this table or an already defined (in one of spec-0 ...) single key field table, a foriegn-key domain will be created for it.

Procedure: create-report rdb destination report-name table
Procedure: create-report rdb destination report-name
The symbol report-name must be primary key in the table named *reports* in the relational database rdb. destination is a port, string, or symbol. If destination is a:

port
The table is created as ascii text and written to that port.
string
The table is created as ascii text and written to the file named by destination.
symbol
destination is the primary key for a row in the table named *printers*.

Each row in the table *reports* has the fields:

name
The report name.
default-table
The table to report on if none is specified.
header, footer
A format string. At the beginning and end of each page respectively, format is called with this string and the (list of) column-names of this table.
reporter
A format string. For each row in the table, format is called with this string and the row.
minimum-break
The minimum number of lines into which the report lines for a row can be broken. Use 0 if a row's lines should not be broken over page boundaries.

Each row in the table *printers* has the fields:

name
The printer name.
print-procedure
The procedure to call to actually print.

The report is prepared as follows:

  • Format (see section Format (version 3.0)) is called with the header field and the (list of) column-names of the table.
  • Format is called with the reporter field and (on successive calls) each record in the natural order for the table. A count is kept of the number of newlines output by format. When the number of newlines to be output exceeds the number of lines per page, the set of lines will be broken if there are more than minimum-break left on this page and the number of lines for this row is larger or equal to twice minimum-break.
  • Format is called with the footer field and the (list of) column-names of the table. The footer field should not output a newline.
  • A new page is output.
  • This entire process repeats until all the rows are output.

The following example shows a new database with the name of `foo.db' being created with tables describing processor families and processor/os/compiler combinations.

The database command define-tables is defined to call define-tables with its arguments. The database is also configured to print `Welcome' when the database is opened. The database is then closed and reopened.

(require 'database-utilities)
(define my-rdb (create-database "foo.db" 'alist-table))

(define-tables my-rdb
  '(*commands*
    ((name symbol))
    ((parameters parameter-list)
     (procedure expression)
     (documentation string))
    ((define-tables
      no-parameters
      no-parameter-names
      (lambda (rdb) (lambda specs (apply define-tables rdb specs)))
      "Create or Augment tables from list of specs")
     (*initialize*
      no-parameters
      no-parameter-names
      (lambda (rdb) (display "Welcome") (newline) rdb)
      "Print Welcome"))))

((my-rdb 'define-tables)
 '(processor-family
   ((family    atom))
   ((also-ran  processor-family))
   ((m68000           #f)
    (m68030           m68000)
    (i386             8086)
    (8086             #f)
    (powerpc          #f)))

 '(platform
   ((name      symbol))
   ((processor processor-family)
    (os        symbol)
    (compiler  symbol))
   ((aix              powerpc aix     -)
    (amiga-dice-c     m68000  amiga   dice-c)
    (amiga-aztec      m68000  amiga   aztec)
    (amiga-sas/c-5.10 m68000  amiga   sas/c)
    (atari-st-gcc     m68000  atari   gcc)
    (atari-st-turbo-c m68000  atari   turbo-c)
    (borland-c-3.1    8086    ms-dos  borland-c)
    (djgpp            i386    ms-dos  gcc)
    (linux            i386    linux   gcc)
    (microsoft-c      8086    ms-dos  microsoft-c)
    (os/2-emx         i386    os/2    gcc)
    (turbo-c-2        8086    ms-dos  turbo-c)
    (watcom-9.0       i386    ms-dos  watcom))))

((my-rdb 'close-database))

(set! my-rdb (open-database "foo.db" 'alist-table))
-|
Welcome

Weight-Balanced Trees

(require 'wt-tree)

Balanced binary trees are a useful data structure for maintaining large sets of ordered objects or sets of associations whose keys are ordered. MIT Scheme has an comprehensive implementation of weight-balanced binary trees which has several advantages over the other data structures for large aggregates:

These features make weight-balanced trees suitable for a wide range of applications, especially those that require large numbers of sets or discrete maps. Applications that have a few global databases and/or concentrate on element-level operations like insertion and lookup are probably better off using hash-tables or red-black trees.

The size of a tree is the number of associations that it contains. Weight balanced binary trees are balanced to keep the sizes of the subtrees of each node within a constant factor of each other. This ensures logarithmic times for single-path operations (like lookup and insertion). A weight balanced tree takes space that is proportional to the number of associations in the tree. For the current implementation, the constant of proportionality is six words per association.

Weight balanced trees can be used as an implementation for either discrete sets or discrete maps (associations). Sets are implemented by ignoring the datum that is associated with the key. Under this scheme if an associations exists in the tree this indicates that the key of the association is a member of the set. Typically a value such as (), #t or #f is associated with the key.

Many operations can be viewed as computing a result that, depending on whether the tree arguments are to thhe arities of each parameter. Corresponds to the arity field of the command's parameter-table. For a description of arity see table above.

types
A list of the type name of each parameter. Correspnds to the type-id field of the contents of the domain of the command's parameter-table.
defaulters
A list of the defaulters for each parameter. Corresponds to the defaulters field of the command's parameter-table.
domain-integrity-rules
A list of procedures (one for each parameter) which tests whether a value for a parameter is acceptable for that parameter. The procedure should be called with each datum in the list for nary arity parameters.
aliases
A list of lists of (alias parameter-name). There can be more than one alias per parameter-name.

For information about parameters, See section Parameter lists. Here is an example of setting up a command with arguments and parsing those arguments from a getopt style argument list (see section Getopt).

(require 'database-utilities)
(require 'fluid-let)
(require 'parameters)
(require 'getopt)

(define my-rdb (create-database #f 'alist-table))

(define-tables my-rdb
  '(foo-params
    *parameter-columns*
    *parameter-columns*
    ((1 single-string single string
        (lambda (pl) '("str")) #f "single string")
     (2 nary-symbols nary symbol
        (lambda (pl) '()) #f "zero or more symbols")
     (3 nary1-symbols nary1 symbol
        (lambda (pl) '(symb)) #f "one or more symbols")
     (4 optional-number optional uint
        (lambda (pl) '()) #f "zero or one number")
     (5 flag boolean boolean
        (lambda (pl) '(#f)) #f "a boolean flag")))
  '(foo-pnames
    ((name string))
    ((parameter-index uint))
    (("s" 1)
     ("single-string" 1)
     ("n" 2)
     ("nary-symbols" 2)
     ("N" 3)
     ("nary1-symbols" 3)
     ("o" 4)
     ("optional-number" 4)
     ("f" 5)
     ("flag" 5)))
  '(my-commands
    ((name symbol))
    ((parameters parameter-list)
     (parameter-names parameter-name-translation)
     (procedure expression)
     (documentation string))
    ((foo
      foo-params
      foo-pnames
      (lambda (rdb) (lambda args (print args)))
      "test command arguments"))))

(define (dbutil:serve-command-line rdb command-table
                                   command argc argv)
  (set! argv (if (vector? argv) (vector->list argv) argv))
  ((make-command-server rdb command-table)
   command
   (lambda (comname comval options positions
                    arities types defaulters dirs aliases) 
     (apply comval (getopt->arglist
                    argc argv options positions
                    arities types defaulters dirs aliases)))))

(define (cmd . opts)
  (fluid-let ((*optind* 1))
    (printf "%-34s => "
            (call-with-output-string (lambda (pt) (write (cons 'cmd opts) pt)))
            ;;(apply string-append (map (lambda (x) (string-append x " ")) opts))
            )
    (set! opts (cons "cmd" opts))
    (force-output)
    (dbutil:serve-command-line
     my-rdb 'my-commands 'foo (length opts) opts)))

(cmd)                              => ("str" () (symb) () #f) 
(cmd "-f")                         => ("str" () (symb) () #t) 
(cmd "--flag")                     => ("str" () (symb) () #t) 
(cmd "-o177")                      => ("str" () (symb) (177) #f) 
(cmd "-o" "177")                   => ("str" () (symb) (177) #f) 
(cmd "--optional" "621")           => ("str" () (symb) (621) #f) 
(cmd "--optional=621")             => ("str" () (symb) (621) #f) 
(cmd "-s" "speciality")            => ("speciality" () (symb) () #f) 
(cmd "-sspeciality")               => ("speciality" () (symb) () #f) 
(cmd "--single" "serendipity")     => ("serendipity" () (symb) () #f) 
(cmd "--single=serendipity")       => ("serendipity" () (symb) () #f) 
(cmd "-n" "gravity" "piety")       => ("str" () (piety gravity) () #f) 
(cmd "-ngravity" "piety")          => ("str" () (piety gravity) () #f) 
(cmd "--nary" "chastity")          => ("str" () (chastity) () #f) 
(cmd "--nary=chastity" "")         => ("str" () ( chastity) () #f) 
(cmd "-N" "calamity")              => ("str" () (calamity) () #f) 
(cmd "-Ncalamity")                 => ("str" () (calamity) () #f) 
(cmd "--nary1" "surety")           => ("str" () (surety) () #f) 
(cmd "--nary1=surety")             => ("str" () (surety) () #f) 
(cmd "-N" "levity" "fealty")       => ("str" () (fealty levity) () #f) 
(cmd "-Nlevity" "fealty")          => ("str" () (fealty levity) () #f) 
(cmd "--nary1" "surety" "brevity") => ("str" () (brevity surety) () #f) 
(cmd "--nary1=surety" "brevity")   => ("str" () (brevity surety) () #f) 
(cmd "-?")
-| 
Usage: cmd [OPTION ARGUMENT ...] ...

  -f, --flag 
  -o, --optional[=]<number> 
  -n, --nary[=]<symbols> ...
  -N, --nary1[=]<symbols> ...
  -s, --single[=]<string> 

ERROR: getopt->parameter-list "unrecognized option" "-?"

Some commands are defined in all extended relational-databases. The are called just like section Relational Database Operations.

Function: add-domain domain-row
Adds domain-row to the domains table if there is no row in the domains table associated with key (car domain-row) and returns #t. Otherwise returns #f.

For the fields and layout of the domain table, See section Catalog Representation. Currently, these fields are

  • domain-name
  • foreign-table
  • domain-integrity-rule
  • type-id
  • type-param

The following example adds 3 domains to the `build' database. `Optstring' is either a string or #f. filename is a string and build-whats is a symbol.

(for-each (build 'add-domain)
          '((optstring #f
                       (lambda (x) (or (not x) (string? x)))
                       string
                       #f)
            (filename #f #f string #f)
            (build-whats #f #f symbol #f)))

Function: delete-domain domain-name
Removes and returns the domain-name row from the domains table.

Function: domain-checker domain
Returns a procedure to check an argument for conformance to domain domain.

Defining Tables

Procedure: define-tables rdb spec-0 ...
Adds tables as specified in spec-0 ... to the open relational-database rdb. Each spec has the form:

(<name> <descriptor-name> <descriptor-name> <rows>)

or

(<name> <primary-key-fields> <other-fields> <rows>)

where <name> is the table name, <descriptor-name> is the symbol name of a descriptor table, <primary-key-fields> and <other-fields> describe the primary keys and other fields respectively, and <rows> is a list of data rows to be added to the table.

<primary-key-fields> and <other-fields> are lists of field descriptors of the form:

(<column-name> <domain>)

or

(<column-name> <domain> <column-integrity-rule>)

where <column-name> is the column name, <domain> is the domain of the column, and <column-integrity-rule> is an expression whose value is a procedure of one argument (which returns #f to signal an error).

If <domain> is not a defined domain name and it matches the name of this table or an already defined (in one of spec-0 ...) single key field table, a foriegn-key domain will be created for it.

Procedure: create-report rdb destination report-name table
Procedure: create-report rdb destination report-name
The symbol report-name must be primary key in the table named *reports* in the relational database rdb. destination is a port, string, or symbol. If destination is a:

port
The table is created as ascii text and written to that port.
string
The table is created as ascii text and written to the file named by destination.
symbol
destination is the primary key for a row in the table named *printers*.

Each row in the table *reports* has the fields:

name
The report name.
default-table
The table to report on if none is specified.
header, footer
A format string. At the beginning and end of each page respectively, format is called with this string and the (list of) column-names of this table.
reporter
A format string. For each row in the table, format is called with this string and the row.
minimum-break
The minimum number of lines into which the report lines for a row can be broken. Use 0 if a row's lines should not be broken over page boundaries.

Each row in the table *printers* has the fields:

name
The printer name.
print-procedure
The procedure to call to actually print.

The report is prepared as follows:

  • Format (see section Format (version 3.0)) is called with the header field and the (list of) column-names of the table.
  • Format is called with the reporter field and (on successive calls) each record in the natural order for the table. A count is kept of the number of newlines output by format. When the number of newlines to be output exceeds the number of lines per page, the set of lines will be broken if there are more than minimum-break left on this page and the number of lines for this row is larger or equal to twice minimum-break.
  • Format is called with the footer field and the (list of) column-names of the table. The footer field should not output a newline.
  • A new page is output.
  • This entire process repeats until all the rows are output.

The following example shows a new database with the name of `foo.db' being created with tables describing processor families and processor/os/compiler combinations.

The database command define-tables is defined to call define-tables with its arguments. The database is also configured to print `Welcome' when the database is opened. The database is then closed and reopened.

(require 'database-utilities)
(define my-rdb (create-database "foo.db" 'alist-table))

(define-tables my-rdb
  '(*commands*
    ((name symbol))
    ((parameters parameter-list)
     (procedure expression)
     (documentation string))
    ((define-tables
      no-parameters
      no-parameter-names
      (lambda (rdb) (lambda specs (apply define-tables rdb specs)))
      "Create or Augment tables from list of specs")
     (*initialize*
      no-parameters
      no-parameter-names
      (lambda (rdb) (display "Welcome") (newline) rdb)
      "Print Welcome"))))

((my-rdb 'define-tables)
 '(processor-family
   ((family    atom))
   ((also-ran  processor-family))
   ((m68000           #f)
    (m68030           m68000)
    (i386             8086)
    (8086             #f)
    (powerpc          #f)))

 '(platform
   ((name      symbol))
   ((processor processor-family)
    (os        symbol)
    (compiler  symbol))
   ((aix              powerpc aix     -)
    (amiga-dice-c     m68000  amiga   dice-c)
    (amiga-aztec      m68000  amiga   aztec)
    (amiga-sas/c-5.10 m68000  amiga   sas/c)
    (atari-st-gcc     m68000  atari   gcc)
    (atari-st-turbo-c m68000  atari   turbo-c)
    (borland-c-3.1    8086    ms-dos  borland-c)
    (djgpp            i386    ms-dos  gcc)
    (linux            i386    linux   gcc)
    (microsoft-c      8086    ms-dos  microsoft-c)
    (os/2-emx         i386    os/2    gcc)
    (turbo-c-2        8086    ms-dos  turbo-c)
    (watcom-9.0       i386    ms-dos  watcom))))

((my-rdb 'close-database))

(set! my-rdb (open-database "foo.db" 'alist-table))
-|
Welcome

Weight-Balanced Trees

(require 'wt-tree)

Balanced binary trees are a useful data structure for maintaining large sets of ordered objects or sets of associations whose keys are ordered. MIT Scheme has an comprehensive implementation of weight-balanced binary trees which has several advantages over the other data structures for large aggregates:

These features make weight-balanced trees suitable for a wide range of applications, especially those that require large numbers of sets or discrete maps. Applications that have a few global databases and/or concentrate on element-level operations like insertion and lookup are probably better off using hash-tables or red-black trees.

The size of a tree is the number of associations that it contains. Weight balanced binary trees are balanced to keep the sizes of the subtrees of each node within a constant factor of each other. This ensures logarithmic times for single-path operations (like lookup and insertion). A weight balanced tree takes space that is proportional to the number of associations in the tree. For the current implementation, the constant of proportionality is six words per association.

Weight balanced trees can be used as an implementation for either discrete sets or discrete maps (associations). Sets are implemented by ignoring the datum that is associated with the key. Under this scheme if an associations exists in the tree this indicates that the key of the association is a member of the set. Typically a value such as (), #t or #f is associated with the key.

Many operations can be viewed as computing a result that, depending on whether the tree arguments are to thhe arities of each parameter. Corresponds to the arity field of the command's parameter-table. For a description of arity see table above.

types
A list of the type name of each parameter. Correspnds to the type-id field of the contents of the domain of the command's parameter-table.
defaulters
A list of the defaulters for each parameter. Corresponds to the defaulters field of the command's parameter-table.
domain-integrity-rules
A list of procedures (one for each parameter) which tests whether a value for a parameter is acceptable for that parameter. The procedure should be called with each datum in the list for nary arity parameters.
aliases
A list of lists of (alias parameter-name). There can be more than one alias per parameter-name.

For information about parameters, See section Parameter lists. Here is an example of setting up a command with arguments and parsing those arguments from a getopt style argument list (see section Getopt).

(require 'database-utilities)
(require 'fluid-let)
(require 'parameters)
(require 'getopt)

(define my-rdb (create-database #f 'alist-table))

(define-tables my-rdb
  '(foo-params
    *parameter-columns*
    *parameter-columns*
    ((1 single-string single string
        (lambda (pl) '("str")) #f "single string")
     (2 nary-symbols nary symbol
        (lambda (pl) '()) #f "zero or more symbols")
     (3 nary1-symbols nary1 symbol
        (lambda (pl) '(symb)) #f "one or more symbols")
     (4 optional-number optional uint
        (lambda (pl) '()) #f "zero or one number")
     (5 flag boolean boolean
        (lambda (pl) '(#f)) #f "a boolean flag")))
  '(foo-pnames
    ((name string))
    ((parameter-index uint))
    (("s" 1)
     ("single-string" 1)
     ("n" 2)
     ("nary-symbols" 2)
     ("N" 3)
     ("nary1-symbols" 3)
     ("o" 4)
     ("optional-number" 4)
     ("f" 5)
     ("flag" 5)))
  '(my-commands
    ((name symbol))
    ((parameters parameter-list)
     (parameter-names parameter-name-translation)
     (procedure expression)
     (documentation string))
    ((foo
      foo-params
      foo-pnames
      (lambda (rdb) (lambda args (print args)))
      "test command arguments"))))

(define (dbutil:serve-command-line rdb command-table
                                   command argc argv)
  (set! argv (if (vector? argv) (vector->list argv) argv))
  ((make-command-server rdb command-table)
   command
   (lambda (comname comval options positions
                    arities types defaulters dirs aliases) 
     (apply comval (getopt->arglist
                    argc argv options positions
                    arities types defaulters dirs aliases)))))

(define (cmd . opts)
  (fluid-let ((*optind* 1))
    (printf "%-34s => "
            (call-with-output-string (lambda (pt) (write (cons 'cmd opts) pt)))
            ;;(apply string-append (map (lambda (x) (string-append x " ")) opts))
            )
    (set! opts (cons "cmd" opts))
    (force-output)
    (dbutil:serve-command-line
     my-rdb 'my-commands 'foo (length opts) opts)))

(cmd)                              => ("str" () (symb) () #f) 
(cmd "-f")                         => ("str" () (symb) () #t) 
(cmd "--flag")                     => ("str" () (symb) () #t) 
(cmd "-o177")                      => ("str" () (symb) (177) #f) 
(cmd "-o" "177")                   => ("str" () (symb) (177) #f) 
(cmd "--optional" "621")           => ("str" () (symb) (621) #f) 
(cmd "--optional=621")             => ("str" () (symb) (621) #f) 
(cmd "-s" "speciality")            => ("speciality" () (symb) () #f) 
(cmd "-sspeciality")               => ("speciality" () (symb) () #f) 
(cmd "--single" "serendipity")     => ("serendipity" () (symb) () #f) 
(cmd "--single=serendipity")       => ("serendipity" () (symb) () #f) 
(cmd "-n" "gravity" "piety")       => ("str" () (piety gravity) () #f) 
(cmd "-ngravity" "piety")          => ("str" () (piety gravity) () #f) 
(cmd "--nary" "chastity")          => ("str" () (chastity) () #f) 
(cmd "--nary=chastity" "")         => ("str" () ( chastity) () #f) 
(cmd "-N" "calamity")              => ("str" () (calamity) () #f) 
(cmd "-Ncalamity")                 => ("str" () (calamity) () #f) 
(cmd "--nary1" "surety")           => ("str" () (surety) () #f) 
(cmd "--nary1=surety")             => ("str" () (surety) () #f) 
(cmd "-N" "levity" "fealty")       => ("str" () (fealty levity) () #f) 
(cmd "-Nlevity" "fealty")          => ("str" () (fealty levity) () #f) 
(cmd "--nary1" "surety" "brevity") => ("str" () (brevity surety) () #f) 
(cmd "--nary1=surety" "brevity")   => ("str" () (brevity surety) () #f) 
(cmd "-?")
-| 
Usage: cmd [OPTION ARGUMENT ...] ...

  -f, --flag 
  -o, --optional[=]<number> 
  -n, --nary[=]<symbols> ...
  -N, --nary1[=]<symbols> ...
  -s, --single[=]<string> 

ERROR: getopt->parameter-list "unrecognized option" "-?"

Some commands are defined in all extended relational-databases. The are called just like section Relational Database Operations.

Function: add-domain domain-row
Adds domain-row to the domains table if there is no row in the domains table associated with key (car domain-row) and returns #t. Otherwise returns #f.

For the fields and layout of the domain table, See section Catalog Representation. Currently, these fields are

  • domain-name
  • foreign-table
  • domain-integrity-rule
  • type-id
  • type-param

The following example adds 3 domains to the `build' database. `Optstring' is either a string or #f. filename is a string and build-whats is a symbol.

(for-each (build 'add-domain)
          '((optstring #f
                       (lambda (x) (or (not x) (string? x)))
                       string
                       #f)
            (filename #f #f string #f)
            (build-whats #f #f symbol #f)))

Function: delete-domain domain-name
Removes and returns the domain-name row from the domains table.

Function: domain-checker domain
Returns a procedure to check an argument for conformance to domain domain.

Defining Tables

Procedure: define-tables rdb spec-0 ...
Adds tables as specified in spec-0 ... to the open relational-database rdb. Each spec has the form:

(<name> <descriptor-name> <descriptor-name> <rows>)

or

(<name> <primary-key-fields> <other-fields> <rows>)

where <name> is the table name, <descriptor-name> is the symbol name of a descriptor table, <primary-key-fields> and <other-fields> describe the primary keys and other fields respectively, and <rows> is a list of data rows to be added to the table.

<primary-key-fields> and <other-fields> are lists of field descriptors of the form:

(<column-name> <domain>)

or

(<column-name> <domain> <column-integrity-rule>)

where <column-name> is the column name, <domain> is the domain of the column, and <column-integrity-rule> is an expression whose value is a procedure of one argument (which returns #f to signal an error).

If <domain> is not a defined domain name and it matches the name of this table or an already defined (in one of spec-0 ...) single key field table, a foriegn-key domain will be created for it.

Procedure: create-report rdb destination report-name table
Procedure: create-report rdb destination report-name
The symbol report-name must be primary key in the table named *reports* in the relational database rdb. destination is a port, string, or symbol. If destination is a:

port
The table is created as ascii text and written to that port.
string
The table is created as ascii text and written to the file named by destination.
symbol
destination is the primary key for a row in the table named *printers*.

Each row in the table *reports* has the fields:

name
The report name.
default-table
The table to report on if none is specified.
header, footer
A format string. At the beginning and end of each page respectively, format is called with this string and the (list of) column-names of this table.
reporter
A format string. For each row in the table, format is called with this string and the row.
minimum-break
The minimum number of lines into which the report lines for a row can be broken. Use 0 if a row's lines should not be broken over page boundaries.

Each row in the table *printers* has the fields:

name
The printer name.
print-procedure
The procedure to call to actually print.

The report is prepared as follows:

  • Format (see section Format (version 3.0)) is called with the header field and the (list of) column-names of the table.
  • Format is called with the reporter field and (on successive calls) each record in the natural order for the table. A count is kept of the number of newlines output by format. When the number of newlines to be output exceeds the number of lines per page, the set of lines will be broken if there are more than minimum-break left on this page and the number of lines for this row is larger or equal to twice minimum-break.
  • Format is called with the footer field and the (list of) column-names of the table. The footer field should not output a newline.
  • A new page is output.
  • This entire process repeats until all the rows are output.

The following example shows a new database with the name of `foo.db' being created with tables describing processor families and processor/os/compiler combinations.

The database command define-tables is defined to call define-tables with its arguments. The database is also configured to print `Welcome' when the database is opened. The database is then closed and reopened.

(require 'database-utilities)
(define my-rdb (create-database "foo.db" 'alist-table))

(define-tables my-rdb
  '(*commands*
    ((name symbol))
    ((parameters parameter-list)
     (procedure expression)
     (documentation string))
    ((define-tables
      no-parameters
      no-parameter-names
      (lambda (rdb) (lambda specs (apply define-tables rdb specs)))
      "Create or Augment tables from list of specs")
     (*initialize*
      no-parameters
      no-parameter-names
      (lambda (rdb) (display "Welcome") (newline) rdb)
      "Print Welcome"))))

((my-rdb 'define-tables)
 '(processor-family
   ((family    atom))
   ((also-ran  processor-family))
   ((m68000           #f)
    (m68030           m68000)
    (i386             8086)
    (8086             #f)
    (powerpc          #f)))

 '(platform
   ((name      symbol))
   ((processor processor-family)
    (os        symbol)
    (compiler  symbol))
   ((aix              powerpc aix     -)
    (amiga-dice-c     m68000  amiga   dice-c)
    (amiga-aztec      m68000  amiga   aztec)
    (amiga-sas/c-5.10 m68000  amiga   sas/c)
    (atari-st-gcc     m68000  atari   gcc)
    (atari-st-turbo-c m68000  atari   turbo-c)
    (borland-c-3.1    8086    ms-dos  borland-c)
    (djgpp            i386    ms-dos  gcc)
    (linux            i386    linux   gcc)
    (microsoft-c      8086    ms-dos  microsoft-c)
    (os/2-emx         i386    os/2    gcc)
    (turbo-c-2        8086    ms-dos  turbo-c)
    (watcom-9.0       i386    ms-dos  watcom))))

((my-rdb 'close-database))

(set! my-rdb (open-database "foo.db" 'alist-table))
-|
Welcome

Weight-Balanced Trees

(require 'wt-tree)

Balanced binary trees are a useful data structure for maintaining large sets of ordered objects or sets of associations whose keys are ordered. MIT Scheme has an comprehensive implementation of weight-balanced binary trees which has several advantages over the other data structures for large aggregates:

These features make weight-balanced trees suitable for a wide range of applications, especially those that require large numbers of sets or discrete maps. Applications that have a few global databases and/or concentrate on element-level operations like insertion and lookup are probably better off using hash-tables or red-black trees.

The size of a tree is the number of associations that it contains. Weight balanced binary trees are balanced to keep the sizes of the subtrees of each node within a constant factor of each other. This ensures logarithmic times for single-path operations (like lookup and insertion). A weight balanced tree takes space that is proportional to the number of associations in the tree. For the current implementation, the constant of proportionality is six words per association.

Weight balanced trees can be used as an implementation for either discrete sets or discrete maps (associations). Sets are implemented by ignoring the datum that is associated with the key. Under this scheme if an associations exists in the tree this indicates that the key of the association is a member of the set. Typically a value such as (), #t or #f is associated with the key.

Many operations can be viewed as computing a result that, depending on whether the tree arguments are to thhe arities of each parameter. Corresponds to the arity field of the command's parameter-table. For a description of arity see table above.

types
A list of the type name of each parameter. Correspnds to the type-id field of the contents of the domain of the command's parameter-table.
defaulters
A list of the defaulters for each parameter. Corresponds to the defaulters field of the command's parameter-table.
domain-integrity-rules
A list of procedures (one for each parameter) which tests whether a value for a parameter is acceptable for that parameter. The procedure should be called with each datum in the list for nary arity parameters.
aliases
A list of lists of (alias parameter-name). There can be more than one alias per parameter-name.

For information about parameters, See section Parameter lists. Here is an example of setting up a command with arguments and parsing those arguments from a getopt style argument list (see section Getopt).

(require 'database-utilities)
(require 'fluid-let)
(require 'parameters)
(require 'getopt)

(define my-rdb (create-database #f 'alist-table))

(define-tables my-rdb
  '(foo-params
    *parameter-columns*
    *parameter-columns*
    ((1 single-string single string
        (lambda (pl) '("str")) #f "single string")
     (2 nary-symbols nary symbol
        (lambda (pl) '()) #f "zero or more symbols")
     (3 nary1-symbols nary1 symbol
        (lambda (pl) '(symb)) #f "one or more symbols")
     (4 optional-number optional uint
        (lambda (pl) '()) #f "zero or one number")
     (5 flag boolean boolean
        (lambda (pl) '(#f)) #f "a boolean flag")))
  '(foo-pnames
    ((name string))
    ((parameter-index uint))
    (("s" 1)
     ("single-string" 1)
     ("n" 2)
     ("nary-symbols" 2)
     ("N" 3)
     ("nary1-symbols" 3)
     ("o" 4)
     ("optional-number" 4)
     ("f" 5)
     ("flag" 5)))
  '(my-commands
    ((name symbol))
    ((parameters parameter-list)
     (parameter-names parameter-name-translation)
     (procedure expression)
     (documentation string))
    ((foo
      foo-params
      foo-pnames
      (lambda (rdb) (lambda args (print args)))
      "test command arguments"))))

(define (dbutil:serve-command-line rdb command-table
                                   command argc argv)
  (set! argv (if (vector? argv) (vector->list argv) argv))
  ((make-command-server rdb command-table)
   command
   (lambda (comname comval options positions
                    arities types defaulters dirs aliases) 
     (apply comval (getopt->arglist
                    argc argv options positions
                    arities types defaulters dirs aliases)))))

(define (cmd . opts)
  (fluid-let ((*optind* 1))
    (printf "%-34s => "
            (call-with-output-string (lambda (pt) (write (cons 'cmd opts) pt)))
            ;;(apply string-append (map (lambda (x) (string-append x " ")) opts))
            )
    (set! opts (cons "cmd" opts))
    (force-output)
    (dbutil:serve-command-line
     my-rdb 'my-commands 'foo (length opts) opts)))

(cmd)                              => ("str" () (symb) () #f) 
(cmd "-f")                         => ("str" () (symb) () #t) 
(cmd "--flag")                     => ("str" () (symb) () #t) 
(cmd "-o177")                      => ("str" () (symb) (177) #f) 
(cmd "-o" "177")                   => ("str" () (symb) (177) #f) 
(cmd "--optional" "621")           => ("str" () (symb) (621) #f) 
(cmd "--optional=621")             => ("str" () (symb) (621) #f) 
(cmd "-s" "speciality")            => ("speciality" () (symb) () #f) 
(cmd "-sspeciality")               => ("speciality" () (symb) () #f) 
(cmd "--single" "serendipity")     => ("serendipity" () (symb) () #f) 
(cmd "--single=serendipity")       => ("serendipity" () (symb) () #f) 
(cmd "-n" "gravity" "piety")       => ("str" () (piety gravity) () #f) 
(cmd "-ngravity" "piety")          => ("str" () (piety gravity) () #f) 
(cmd "--nary" "chastity")          => ("str" () (chastity) () #f) 
(cmd "--nary=chastity" "")         => ("str" () ( chastity) () #f) 
(cmd "-N" "calamity")              => ("str" () (calamity) () #f) 
(cmd "-Ncalamity")                 => ("str" () (calamity) () #f) 
(cmd "--nary1" "surety")           => ("str" () (surety) () #f) 
(cmd "--nary1=surety")             => ("str" () (surety) () #f) 
(cmd "-N" "levity" "fealty")       => ("str" () (fealty levity) () #f) 
(cmd "-Nlevity" "fealty")          => ("str" () (fealty levity) () #f) 
(cmd "--nary1" "surety" "brevity") => ("str" () (brevity surety) () #f) 
(cmd "--nary1=surety" "brevity")   => ("str" () (brevity surety) () #f) 
(cmd "-?")
-| 
Usage: cmd [OPTION ARGUMENT ...] ...

  -f, --flag 
  -o, --optional[=]<number> 
  -n, --nary[=]<symbols> ...
  -N, --nary1[=]<symbols> ...
  -s, --single[=]<string> 

ERROR: getopt->parameter-list "unrecognized option" "-?"

Some commands are defined in all extended relational-databases. The are called just like section Relational Database Operations.

Function: add-domain domain-row
Adds domain-row to the domains table if there is no row in the domains table associated with key (car domain-row) and returns #t. Otherwise returns #f.

For the fields and layout of the domain table, See section Catalog Representation. Currently, these fields are

  • domain-name
  • foreign-table
  • domain-integrity-rule
  • type-id
  • type-param

The following example adds 3 domains to the `build' database. `Optstring' is either a string or #f. filename is a string and build-whats is a symbol.

(for-each (build 'add-domain)
          '((optstring #f
                       (lambda (x) (or (not x) (string? x)))
                       string
                       #f)
            (filename #f #f string #f)
            (build-whats #f #f symbol #f)))

Function: delete-domain domain-name
Removes and returns the domain-name row from the domains table.

Function: domain-checker domain
Returns a procedure to check an argument for conformance to domain domain.

Defining Tables

Procedure: define-tables rdb spec-0 ...
Adds tables as specified in spec-0 ... to the open relational-database rdb. Each spec has the form:

(<name> <descriptor-name> <descriptor-name> <rows>)

or

(<name> <primary-key-fields> <other-fields> <rows>)

where <name> is the table name, <descriptor-name> is the symbol name of a descriptor table, <primary-key-fields> and <other-fields> describe the primary keys and other fields respectively, and <rows> is a list of data rows to be added to the table.

<primary-key-fields> and <other-fields> are lists of field descriptors of the form:

(<column-name> <domain>)

or

(<column-name> <domain> <column-integrity-rule>)

where <column-name> is the column name, <domain> is the domain of the column, and <column-integrity-rule> is an expression whose value is a procedure of one argument (which returns #f to signal an error).

If <domain> is not a defined domain name and it matches the name of this table or an already defined (in one of spec-0 ...) single key field table, a foriegn-key domain will be created for it.

Procedure: create-report rdb destination report-name table
Procedure: create-report rdb destination report-name
The symbol report-name must be primary key in the table named *reports* in the relational database rdb. destination is a port, string, or symbol. If destination is a:

port
The table is created as ascii text and written to that port.
string
The table is created as ascii text and written to the file named by destination.
symbol
destination is the primary key for a row in the table named *printers*.

Each row in the table *reports* has the fields:

name
The report name.
default-table
The table to report on if none is specified.
header, footer
A format string. At the beginning and end of each page respectively, format is called with this string and the (list of) column-names of this table.
reporter
A format string. For each row in the table, format is called with this string and the row.
minimum-break
The minimum number of lines into which the report lines for a row can be broken. Use 0 if a row's lines should not be broken over page boundaries.

Each row in the table *printers* has the fields:

name
The printer name.
print-procedure
The procedure to call to actually print.

The report is prepared as follows:

  • Format (see section Format (version 3.0)) is called with the header field and the (list of) column-names of the table.
  • Format is called with the reporter field and (on successive calls) each record in the natural order for the table. A count is kept of the number of newlines output by format. When the number of newlines to be output exceeds the number of lines per page, the set of lines will be broken if there are more than minimum-break left on this page and the number of lines for this row is larger or equal to twice minimum-break.
  • Format is called with the footer field and the (list of) column-names of the table. The footer field should not output a newline.
  • A new page is output.
  • This entire process repeats until all the rows are output.

The following example shows a new database with the name of `foo.db' being created with tables describing processor families and processor/os/compiler combinations.

The database command define-tables is defined to call define-tables with its arguments. The database is also configured to print `Welcome' when the database is opened. The database is then closed and reopened.

(require 'database-utilities)
(define my-rdb (create-database "foo.db" 'alist-table))

(define-tables my-rdb
  '(*commands*
    ((name symbol))
    ((parameters parameter-list)
     (procedure expression)
     (documentation string))
    ((define-tables
      no-parameters
      no-parameter-names
      (lambda (rdb) (lambda specs (apply define-tables rdb specs)))
      "Create or Augment tables from list of specs")
     (*initialize*
      no-parameters
      no-parameter-names
      (lambda (rdb) (display "Welcome") (newline) rdb)
      "Print Welcome"))))

((my-rdb 'define-tables)
 '(processor-family
   ((family    atom))
   ((also-ran  processor-family))
   ((m68000           #f)
    (m68030           m68000)
    (i386             8086)
    (8086             #f)
    (powerpc          #f)))

 '(platform
   ((name      symbol))
   ((processor processor-family)
    (os        symbol)
    (compiler  symbol))
   ((aix              powerpc aix     -)
    (amiga-dice-c     m68000  amiga   dice-c)
    (amiga-aztec      m68000  amiga   aztec)
    (amiga-sas/c-5.10 m68000  amiga   sas/c)
    (atari-st-gcc     m68000  atari   gcc)
    (atari-st-turbo-c m68000  atari   turbo-c)
    (borland-c-3.1    8086    ms-dos  borland-c)
    (djgpp            i386    ms-dos  gcc)
    (linux            i386    linux   gcc)
    (microsoft-c      8086    ms-dos  microsoft-c)
    (os/2-emx         i386    os/2    gcc)
    (turbo-c-2        8086    ms-dos  turbo-c)
    (watcom-9.0       i386    ms-dos  watcom))))

((my-rdb 'close-database))

(set! my-rdb (open-database "foo.db" 'alist-table))
-|
Welcome

Weight-Balanced Trees

(require 'wt-tree)

Balanced binary trees are a useful data structure for maintaining large sets of ordered objects or sets of associations whose keys are ordered. MIT Scheme has an comprehensive implementation of weight-balanced binary trees which has several advantages over the other data structures for large aggregates:

These features make weight-balanced trees suitable for a wide range of applications, especially those that require large numbers of sets or discrete maps. Applications that have a few global databases and/or concentrate on element-level operations like insertion and lookup are probably better off using hash-tables or red-black trees.

The size of a tree is the number of associations that it contains. Weight balanced binary trees are balanced to keep the sizes of the subtrees of each node within a constant factor of each other. This ensures logarithmic times for single-path operations (like lookup and insertion). A weight balanced tree takes space that is proportional to the number of associations in the tree. For the current implementation, the constant of proportionality is six words per association.

Weight balanced trees can be used as an implementation for either discrete sets or discrete maps (associations). Sets are implemented by ignoring the datum that is associated with the key. Under this scheme if an associations exists in the tree this indicates that the key of the association is a member of the set. Typically a value such as (), #t or #f is associated with the key.

Many operations can be viewed as computing a result that, depending on whether the tree arguments are to thhe arities of each parameter. Corresponds to the arity field of the command's parameter-table. For a description of arity see table above.

types
A list of the type name of each parameter. Correspnds to the type-id field of the contents of the domain of the command's parameter-table.
defaulters
A list of the defaulters for each parameter. Corresponds to the defaulters field of the command's parameter-table.
domain-integrity-rules
A list of procedures (one for each parameter) which tests whether a value for a parameter is acceptable for that parameter. The procedure should be called with each datum in the list for nary arity parameters.
aliases
A list of lists of (alias parameter-name). There can be more than one alias per parameter-name.

For information about parameters, See section Parameter lists. Here is an example of setting up a command with arguments and parsing those arguments from a getopt style argument list (see section Getopt).

(require 'database-utilities)
(require 'fluid-let)
(require 'parameters)
(require 'getopt)

(define my-rdb (create-database #f 'alist-table))

(define-tables my-rdb
  '(foo-params
    *parameter-columns*
    *parameter-columns*
    ((1 single-string single string
        (lambda (pl) '("str")) #f "single string")
     (2 nary-symbols nary symbol
        (lambda (pl) '()) #f "zero or more symbols")
     (3 nary1-symbols nary1 symbol
        (lambda (pl) '(symb)) #f "one or more symbols")
     (4 optional-number optional uint
        (lambda (pl) '()) #f "zero or one number")
     (5 flag boolean boolean
        (lambda (pl) '(#f)) #f "a boolean flag")))
  '(foo-pnames
    ((name string))
    ((parameter-index uint))
    (("s" 1)
     ("single-string" 1)
     ("n" 2)
     ("nary-symbols" 2)
     ("N" 3)
     ("nary1-symbols" 3)
     ("o" 4)
     ("optional-number" 4)
     ("f" 5)
     ("flag" 5)))
  '(my-commands
    ((name symbol))
    ((parameters parameter-list)
     (parameter-names parameter-name-translation)
     (procedure expression)
     (documentation string))
    ((foo
      foo-params
      foo-pnames
      (lambda (rdb) (lambda args (print args)))
      "test command arguments"))))

(define (dbutil:serve-command-line rdb command-table
                                   command argc argv)
  (set! argv (if (vector? argv) (vector->list argv) argv))
  ((make-command-server rdb command-table)
   command
   (lambda (comname comval options positions
                    arities types defaulters dirs aliases) 
     (apply comval (getopt->arglist
                    argc argv options positions
                    arities types defaulters dirs aliases)))))

(define (cmd . opts)
  (fluid-let ((*optind* 1))
    (printf "%-34s => "
            (call-with-output-string (lambda (pt) (write (cons 'cmd opts) pt)))
            ;;(apply string-append (map (lambda (x) (string-append x " ")) opts))
            )
    (set! opts (cons "cmd" opts))
    (force-output)
    (dbutil:serve-command-line
     my-rdb 'my-commands 'foo (length opts) opts)))

(cmd)                              => ("str" () (symb) () #f) 
(cmd "-f")                         => ("str" () (symb) () #t) 
(cmd "--flag")                     => ("str" () (symb) () #t) 
(cmd "-o177")                      => ("str" () (symb) (177) #f) 
(cmd "-o" "177")                   => ("str" () (symb) (177) #f) 
(cmd "--optional" "621")           => ("str" () (symb) (621) #f) 
(cmd "--optional=621")             => ("str" () (symb) (621) #f) 
(cmd "-s" "speciality")            => ("speciality" () (symb) () #f) 
(cmd "-sspeciality")               => ("speciality" () (symb) () #f) 
(cmd "--single" "serendipity")     => ("serendipity" () (symb) () #f) 
(cmd "--single=serendipity")       => ("serendipity" () (symb) () #f) 
(cmd "-n" "gravity" "piety")       => ("str" () (piety gravity) () #f) 
(cmd "-ngravity" "piety")          => ("str" () (piety gravity) () #f) 
(cmd "--nary" "chastity")          => ("str" () (chastity) () #f) 
(cmd "--nary=chastity" "")         => ("str" () ( chastity) () #f) 
(cmd "-N" "calamity")              => ("str" () (calamity) () #f) 
(cmd "-Ncalamity")                 => ("str" () (calamity) () #f) 
(cmd "--nary1" "surety")           => ("str" () (surety) () #f) 
(cmd "--nary1=surety")             => ("str" () (surety) () #f) 
(cmd "-N" "levity" "fealty")       => ("str" () (fealty levity) () #f) 
(cmd "-Nlevity" "fealty")          => ("str" () (fealty levity) () #f) 
(cmd "--nary1" "surety" "brevity") => ("str" () (brevity surety) () #f) 
(cmd "--nary1=surety" "brevity")   => ("str" () (brevity surety) () #f) 
(cmd "-?")
-| 
Usage: cmd [OPTION ARGUMENT ...] ...

  -f, --flag 
  -o, --optional[=]<number> 
  -n, --nary[=]<symbols> ...
  -N, --nary1[=]<symbols> ...
  -s, --single[=]<string> 

ERROR: getopt->parameter-list "unrecognized option" "-?"

Some commands are defined in all extended relational-databases. The are called just like section Relational Database Operations.

Function: add-domain domain-row
Adds domain-row to the domains table if there is no row in the domains table associated with key (car domain-row) and returns #t. Otherwise returns #f.

For the fields and layout of the domain table, See section Catalog Representation. Currently, these fields are

  • domain-name
  • foreign-table
  • domain-integrity-rule
  • type-id
  • type-param

The following example adds 3 domains to the `build' database. `Optstring' is either a string or #f. filename is a string and build-whats is a symbol.

(for-each (build 'add-domain)
          '((optstring #f
                       (lambda (x) (or (not x) (string? x)))
                       string
                       #f)
            (filename #f #f string #f)
            (build-whats #f #f symbol #f)))

Function: delete-domain domain-name
Removes and returns the domain-name row from the domains table.

Function: domain-checker domain
Returns a procedure to check an argument for conformance to domain domain.

Defining Tables

Procedure: define-tables rdb spec-0 ...
Adds tables as specified in spec-0 ... to the open relational-database rdb. Each spec has the form:

(<name> <descriptor-name> <descriptor-name> <rows>)

or

(<name> <primary-key-fields> <other-fields> <rows>)

where <name> is the table name, <descriptor-name> is the symbol name of a descriptor table, <primary-key-fields> and <other-fields> describe the primary keys and other fields respectively, and <rows> is a list of data rows to be added to the table.

<primary-key-fields> and <other-fields> are lists of field descriptors of the form:

(<column-name> <domain>)

or

(<column-name> <domain> <column-integrity-rule>)

where <column-name> is the column name, <domain> is the domain of the column, and <column-integrity-rule> is an expression whose value is a procedure of one argument (which returns #f to signal an error).

If <domain> is not a defined domain name and it matches the name of this table or an already defined (in one of spec-0 ...) single key field table, a foriegn-key domain will be created for it.

Procedure: create-report rdb destination report-name table
Procedure: create-report rdb destination report-name
The symbol report-name must be primary key in the table named *reports* in the relational database rdb. destination is a port, string, or symbol. If destination is a:

port
The table is created as ascii text and written to that port.
string
The table is created as ascii text and written to the file named by destination.
symbol
destination is the primary key for a row in the table named *printers*.

Each row in the table *reports* has the fields:

name
The report name.
default-table
The table to report on if none is specified.
header, footer
A format string. At the beginning and end of each page respectively, format is called with this string and the (list of) column-names of this table.
reporter
A format string. For each row in the table, format is called with this string and the row.
minimum-break
The minimum number of lines into which the report lines for a row can be broken. Use 0 if a row's lines should not be broken over page boundaries.

Each row in the table *printers* has the fields:

name
The printer name.
print-procedure
The procedure to call to actually print.

The report is prepared as follows:

  • Format (see section Format (version 3.0)) is called with the header field and the (list of) column-names of the table.
  • Format is called with the reporter field and (on successive calls) each record in the natural order for the table. A count is kept of the number of newlines output by format. When the number of newlines to be output exceeds the number of lines per page, the set of lines will be broken if there are more than minimum-break left on this page and the number of lines for this row is larger or equal to twice minimum-break.
  • Format is called with the footer field and the (list of) column-names of the table. The footer field should not output a newline.
  • A new page is output.
  • This entire process repeats until all the rows are output.

The following example shows a new database with the name of `foo.db' being created with tables describing processor families and processor/os/compiler combinations.

The database command define-tables is defined to call define-tables with its arguments. The database is also configured to print `Welcome' when the database is opened. The database is then closed and reopened.

(require 'database-utilities)
(define my-rdb (create-database "foo.db" 'alist-table))

(define-tables my-rdb
  '(*commands*
    ((name symbol))
    ((parameters parameter-list)
     (procedure expression)
     (documentation string))
    ((define-tables
      no-parameters
      no-parameter-names
      (lambda (rdb) (lambda specs (apply define-tables rdb specs)))
      "Create or Augment tables from list of specs")
     (*initialize*
      no-parameters
      no-parameter-names
      (lambda (rdb) (display "Welcome") (newline) rdb)
      "Print Welcome"))))

((my-rdb 'define-tables)
 '(processor-family
   ((family    atom))
   ((also-ran  processor-family))
   ((m68000           #f)
    (m68030           m68000)
    (i386             8086)
    (8086             #f)
    (powerpc          #f)))

 '(platform
   ((name      symbol))
   ((processor processor-family)
    (os        symbol)
    (compiler  symbol))
   ((aix              powerpc aix     -)
    (amiga-dice-c     m68000  amiga   dice-c)
    (amiga-aztec      m68000  amiga   aztec)
    (amiga-sas/c-5.10 m68000  amiga   sas/c)
    (atari-st-gcc     m68000  atari   gcc)
    (atari-st-turbo-c m68000  atari   turbo-c)
    (borland-c-3.1    8086    ms-dos  borland-c)
    (djgpp            i386    ms-dos  gcc)
    (linux            i386    linux   gcc)
    (microsoft-c      8086    ms-dos  microsoft-c)
    (os/2-emx         i386    os/2    gcc)
    (turbo-c-2        8086    ms-dos  turbo-c)
    (watcom-9.0       i386    ms-dos  watcom))))

((my-rdb 'close-database))

(set! my-rdb (open-database "foo.db" 'alist-table))
-|
Welcome

Weight-Balanced Trees

(require 'wt-tree)

Balanced binary trees are a useful data structure for maintaining large sets of ordered objects or sets of associations whose keys are ordered. MIT Scheme has an comprehensive implementation of weight-balanced binary trees which has several advantages over the other data structures for large aggregates:

These features make weight-balanced trees suitable for a wide range of applications, especially those that require large numbers of sets or discrete maps. Applications that have a few global databases and/or concentrate on element-level operations like insertion and lookup are probably better off using hash-tables or red-black trees.

The size of a tree is the number of associations that it contains. Weight balanced binary trees are balanced to keep the sizes of the subtrees of each node within a constant factor of each other. This ensures logarithmic times for single-path operations (like lookup and insertion). A weight balanced tree takes space that is proportional to the number of associations in the tree. For the current implementation, the constant of proportionality is six words per association.

Weight balanced trees can be used as an implementation for either discrete sets or discrete maps (associations). Sets are implemented by ignoring the datum that is associated with the key. Under this scheme if an associations exists in the tree this indicates that the key of the association is a member of the set. Typically a value such as (), #t or #f is associated with the key.

Many operations can be viewed as computing a result that, depending on whether the tree arguments are to thhe arities of each parameter. Corresponds to the arity field of the command's parameter-table. For a description of arity see table above.

types
A list of the type name of each parameter. Correspnds to the type-id field of the contents of the domain of the command's parameter-table.
defaulters
A list of the defaulters for each parameter. Corresponds to the defaulters field of the command's parameter-table.
domain-integrity-rules
A list of procedures (one for each parameter) which tests whether a value for a parameter is acceptable for that parameter. The procedure should be called with each datum in the list for nary arity parameters.
aliases
A list of lists of (alias parameter-name). There can be more than one alias per parameter-name.

For information about parameters, See section Parameter lists. Here is an example of setting up a command with arguments and parsing those arguments from a getopt style argument list (see section Getopt).

(require 'database-utilities)
(require 'fluid-let)
(require 'parameters)
(require 'getopt)

(define my-rdb (create-database #f 'alist-table))

(define-tables my-rdb
  '(foo-params
    *parameter-columns*
    *parameter-columns*
    ((1 single-string single string
        (lambda (pl) '("str")) #f "single string")
     (2 nary-symbols nary symbol
        (lambda (pl) '()) #f "zero or more symbols")
     (3 nary1-symbols nary1 symbol
        (lambda (pl) '(symb)) #f "one or more symbols")
     (4 optional-number optional uint
        (lambda (pl) '()) #f "zero or one number")
     (5 flag boolean boolean
        (lambda (pl) '(#f)) #f "a boolean flag")))
  '(foo-pnames
    ((name string))
    ((parameter-index uint))
    (("s" 1)
     ("single-string" 1)
     ("n" 2)
     ("nary-symbols" 2)
     ("N" 3)
     ("nary1-symbols" 3)
     ("o" 4)
     ("optional-number" 4)
     ("f" 5)
     ("flag" 5)))
  '(my-commands
    ((name symbol))
    ((parameters parameter-list)
     (parameter-names parameter-name-translation)
     (procedure expression)
     (documentation string))
    ((foo
      foo-params
      foo-pnames
      (lambda (rdb) (lambda args (print args)))
      "test command arguments"))))

(define (dbutil:serve-command-line rdb command-table
                                   command argc argv)
  (set! argv (if (vector? argv) (vector->list argv) argv))
  ((make-command-server rdb command-table)
   command
   (lambda (comname comval options positions
                    arities types defaulters dirs aliases) 
     (apply comval (getopt->arglist
                    argc argv options positions
                    arities types defaulters dirs aliases)))))

(define (cmd . opts)
  (fluid-let ((*optind* 1))
    (printf "%-34s => "
            (call-with-output-string (lambda (pt) (write (cons 'cmd opts) pt)))
            ;;(apply string-append (map (lambda (x) (string-append x " ")) opts))
            )
    (set! opts (cons "cmd" opts))
    (force-output)
    (dbutil:serve-command-line
     my-rdb 'my-commands 'foo (length opts) opts)))

(cmd)                              => ("str" () (symb) () #f) 
(cmd "-f")                         => ("str" () (symb) () #t) 
(cmd "--flag")                     => ("str" () (symb) () #t) 
(cmd "-o177")                      => ("str" () (symb) (177) #f) 
(cmd "-o" "177")                   => ("str" () (symb) (177) #f) 
(cmd "--optional" "621")           => ("str" () (symb) (621) #f) 
(cmd "--optional=621")             => ("str" () (symb) (621) #f) 
(cmd "-s" "speciality")            => ("speciality" () (symb) () #f) 
(cmd "-sspeciality")               => ("speciality" () (symb) () #f) 
(cmd "--single" "serendipity")     => ("serendipity" () (symb) () #f) 
(cmd "--single=serendipity")       => ("serendipity" () (symb) () #f) 
(cmd "-n" "gravity" "piety")       => ("str" () (piety gravity) () #f) 
(cmd "-ngravity" "piety")          => ("str" () (piety gravity) () #f) 
(cmd "--nary" "chastity")          => ("str" () (chastity) () #f) 
(cmd "--nary=chastity" "")         => ("str" () ( chastity) () #f) 
(cmd "-N" "calamity")              => ("str" () (calamity) () #f) 
(cmd "-Ncalamity")                 => ("str" () (calamity) () #f) 
(cmd "--nary1" "surety")           => ("str" () (surety) () #f) 
(cmd "--nary1=surety")             => ("str" () (surety) () #f) 
(cmd "-N" "levity" "fealty")       => ("str" () (fealty levity) () #f) 
(cmd "-Nlevity" "fealty")          => ("str" () (fealty levity) () #f) 
(cmd "--nary1" "surety" "brevity") => ("str" () (brevity surety) () #f) 
(cmd "--nary1=surety" "brevity")   => ("str" () (brevity surety) () #f) 
(cmd "-?")
-| 
Usage: cmd [OPTION ARGUMENT ...] ...

  -f, --flag 
  -o, --optional[=]<number> 
  -n, --nary[=]<symbols> ...
  -N, --nary1[=]<symbols> ...
  -s, --single[=]<string> 

ERROR: getopt->parameter-list "unrecognized option" "-?"

Some commands are defined in all extended relational-databases. The are called just like section Relational Database Operations.

Function: add-domain domain-row
Adds domain-row to the domains table if there is no row in the domains table associated with key (car domain-row) and returns #t. Otherwise returns #f.

For the fields and layout of the domain table, See section Catalog Representation. Currently, these fields are

  • domain-name
  • foreign-table
  • domain-integrity-rule
  • type-id
  • type-param

The following example adds 3 domains to the `build' database. `Optstring' is either a string or #f. filename is a string and build-whats is a symbol.

(for-each (build 'add-domain)
          '((optstring #f
                       (lambda (x) (or (not x) (string? x)))
                       string
                       #f)
            (filename #f #f string #f)
            (build-whats #f #f symbol #f)))

Function: delete-domain domain-name
Removes and returns the domain-name row from the domains table.

Function: domain-checker domain
Returns a procedure to check an argument for conformance to domain domain.

Defining Tables

Procedure: define-tables rdb spec-0 ...
Adds tables as specified in spec-0 ... to the open relational-database rdb. Each spec has the form:

(<name> <descriptor-name> <descriptor-name> <rows>)

or

(<name> <primary-key-fields> <other-fields> <rows>)

where <name> is the table name, <descriptor-name> is the symbol name of a descriptor table, <primary-key-fields> and <other-fields> describe the primary keys and other fields respectively, and <rows> is a list of data rows to be added to the table.

<primary-key-fields> and <other-fields> are lists of field descriptors of the form:

(<column-name> <domain>)

or

(<column-name> <domain> <column-integrity-rule>)

where <column-name> is the column name, <domain> is the domain of the column, and <column-integrity-rule> is an expression whose value is a procedure of one argument (which returns #f to signal an error).

If <domain> is not a defined domain name and it matches the name of this table or an already defined (in one of spec-0 ...) single key field table, a foriegn-key domain will be created for it.

Procedure: create-report rdb destination report-name table
Procedure: create-report rdb destination report-name
The symbol report-name must be primary key in the table named *reports* in the relational database rdb. destination is a port, string, or symbol. If destination is a:

port
The table is created as ascii text and written to that port.
string
The table is created as ascii text and written to the file named by destination.
symbol
destination is the primary key for a row in the table named *printers*.

Each row in the table *reports* has the fields:

name
The report name.
default-table
The table to report on if none is specified.
header, footer
A format string. At the beginning and end of each page respectively, format is called with this string and the (list of) column-names of this table.
reporter
A format string. For each row in the table, format is called with this string and the row.
minimum-break
The minimum number of lines into which the report lines for a row can be broken. Use 0 if a row's lines should not be broken over page boundaries.

Each row in the table *printers* has the fields:

name
The printer name.
print-procedure
The procedure to call to actually print.

The report is prepared as follows:

  • Format (see section Format (version 3.0)) is called with the header field and the (list of) column-names of the table.
  • Format is called with the reporter field and (on successive calls) each record in the natural order for the table. A count is kept of the number of newlines output by format. When the number of newlines to be output exceeds the number of lines per page, the set of lines will be broken if there are more than minimum-break left on this page and the number of lines for this row is larger or equal to twice minimum-break.
  • Format is called with the footer field and the (list of) column-names of the table. The footer field should not output a newline.
  • A new page is output.
  • This entire process repeats until all the rows are output.

The following example shows a new database with the name of `foo.db' being created with tables describing processor families and processor/os/compiler combinations.

The database command define-tables is defined to call define-tables with its arguments. The database is also configured to print `Welcome' when the database is opened. The database is then closed and reopened.

(require 'database-utilities)
(define my-rdb (create-database "foo.db" 'alist-table))

(define-tables my-rdb
  '(*commands*
    ((name symbol))
    ((parameters parameter-list)
     (procedure expression)
     (documentation string))
    ((define-tables
      no-parameters
      no-parameter-names
      (lambda (rdb) (lambda specs (apply define-tables rdb specs)))
      "Create or Augment tables from list of specs")
     (*initialize*
      no-parameters
      no-parameter-names
      (lambda (rdb) (display "Welcome") (newline) rdb)
      "Print Welcome"))))

((my-rdb 'define-tables)
 '(processor-family
   ((family    atom))
   ((also-ran  processor-family))
   ((m68000           #f)
    (m68030           m68000)
    (i386             8086)
    (8086             #f)
    (powerpc          #f)))

 '(platform
   ((name      symbol))
   ((processor processor-family)
    (os        symbol)
    (compiler  symbol))
   ((aix              powerpc aix     -)
    (amiga-dice-c     m68000  amiga   dice-c)
    (amiga-aztec      m68000  amiga   aztec)
    (amiga-sas/c-5.10 m68000  amiga   sas/c)
    (atari-st-gcc     m68000  atari   gcc)
    (atari-st-turbo-c m68000  atari   turbo-c)
    (borland-c-3.1    8086    ms-dos  borland-c)
    (djgpp            i386    ms-dos  gcc)
    (linux            i386    linux   gcc)
    (microsoft-c      8086    ms-dos  microsoft-c)
    (os/2-emx         i386    os/2    gcc)
    (turbo-c-2        8086    ms-dos  turbo-c)
    (watcom-9.0       i386    ms-dos  watcom))))

((my-rdb 'close-database))

(set! my-rdb (open-database "foo.db" 'alist-table))
-|
Welcome

Weight-Balanced Trees

(require 'wt-tree)

Balanced binary trees are a useful data structure for maintaining large sets of ordered objects or sets of associations whose keys are ordered. MIT Scheme has an comprehensive implementation of weight-balanced binary trees which has several advantages over the other data structures for large aggregates:

These features make weight-balanced trees suitable for a wide range of applications, especially those that require large numbers of sets or discrete maps. Applications that have a few global databases and/or concentrate on element-level operations like insertion and lookup are probably better off using hash-tables or red-black trees.

The size of a tree is the number of associations that it contains. Weight balanced binary trees are balanced to keep the sizes of the subtrees of each node within a constant factor of each other. This ensures logarithmic times for single-path operations (like lookup and insertion). A weight balanced tree takes space that is proportional to the number of associations in the tree. For the current implementation, the constant of proportionality is six words per association.

Weight balanced trees can be used as an implementation for either discrete sets or discrete maps (associations). Sets are implemented by ignoring the datum that is associated with the key. Under this scheme if an associations exists in the tree this indicates that the key of the association is a member of the set. Typically a value such as (), #t or #f is associated with the key.

Many operations can be viewed as computing a result that, depending on whether the tree arguments are to thhe arities of each parameter. Corresponds to the arity field of the command's parameter-table. For a description of arity see table above.

types
A list of the type name of each parameter. Correspnds to the type-id field of the contents of the domain of the command's parameter-table.
defaulters
A list of the defaulters for each parameter. Corresponds to the defaulters field of the command's parameter-table.
domain-integrity-rules
A list of procedures (one for each parameter) which tests whether a value for a parameter is acceptable for that parameter. The procedure should be called with each datum in the list for nary arity parameters.
aliases
A list of lists of (alias parameter-name). There can be more than one alias per parameter-name.

For information about parameters, See section Parameter lists. Here is an example of setting up a command with arguments and parsing those arguments from a getopt style argument list (see section Getopt).

(require 'database-utilities)
(require 'fluid-let)
(require 'parameters)
(require 'getopt)

(define my-rdb (create-database #f 'alist-table))

(define-tables my-rdb
  '(foo-params
    *parameter-columns*
    *parameter-columns*
    ((1 single-string single string
        (lambda (pl) '("str")) #f "single string")
     (2 nary-symbols nary symbol
        (lambda (pl) '()) #f "zero or more symbols")
     (3 nary1-symbols nary1 symbol
        (lambda (pl) '(symb)) #f "one or more symbols")
     (4 optional-number optional uint
        (lambda (pl) '()) #f "zero or one number")
     (5 flag boolean boolean
        (lambda (pl) '(#f)) #f "a boolean flag")))
  '(foo-pnames
    ((name string))
    ((parameter-index uint))
    (("s" 1)
     ("single-string" 1)
     ("n" 2)
     ("nary-symbols" 2)
     ("N" 3)
     ("nary1-symbols" 3)
     ("o" 4)
     ("optional-number" 4)
     ("f" 5)
     ("flag" 5)))
  '(my-commands
    ((name symbol))
    ((parameters parameter-list)
     (parameter-names parameter-name-translation)
     (procedure expression)
     (documentation string))
    ((foo
      foo-params
      foo-pnames
      (lambda (rdb) (lambda args (print args)))
      "test command arguments"))))

(define (dbutil:serve-command-line rdb command-table
                                   command argc argv)
  (set! argv (if (vector? argv) (vector->list argv) argv))
  ((make-command-server rdb command-table)
   command
   (lambda (comname comval options positions
                    arities types defaulters dirs aliases) 
     (apply comval (getopt->arglist
                    argc argv options positions
                    arities types defaulters dirs aliases)))))

(define (cmd . opts)
  (fluid-let ((*optind* 1))
    (printf "%-34s => "
            (call-with-output-string (lambda (pt) (write (cons 'cmd opts) pt)))
            ;;(apply string-append (map (lambda (x) (string-append x " ")) opts))
            )
    (set! opts (cons "cmd" opts))
    (force-output)
    (dbutil:serve-command-line
     my-rdb 'my-commands 'foo (length opts) opts)))

(cmd)                              => ("str" () (symb) () #f) 
(cmd "-f")                         => ("str" () (symb) () #t) 
(cmd "--flag")                     => ("str" () (symb) () #t) 
(cmd "-o177")                      => ("str" () (symb) (177) #f) 
(cmd "-o" "177")                   => ("str" () (symb) (177) #f) 
(cmd "--optional" "621")           => ("str" () (symb) (621) #f) 
(cmd "--optional=621")             => ("str" () (symb) (621) #f) 
(cmd "-s" "speciality")            => ("speciality" () (symb) () #f) 
(cmd "-sspeciality")               => ("speciality" () (symb) () #f) 
(cmd "--single" "serendipity")     => ("serendipity" () (symb) () #f) 
(cmd "--single=serendipity")       => ("serendipity" () (symb) () #f) 
(cmd "-n" "gravity" "piety")       => ("str" () (piety gravity) () #f) 
(cmd "-ngravity" "piety")          => ("str" () (piety gravity) () #f) 
(cmd "--nary" "chastity")          => ("str" () (chastity) () #f) 
(cmd "--nary=chastity" "")         => ("str" () ( chastity) () #f) 
(cmd "-N" "calamity")              => ("str" () (calamity) () #f) 
(cmd "-Ncalamity")                 => ("str" () (calamity) () #f) 
(cmd "--nary1" "surety")           => ("str" () (surety) () #f) 
(cmd "--nary1=surety")             => ("str" () (surety) () #f) 
(cmd "-N" "levity" "fealty")       => ("str" () (fealty levity) () #f) 
(cmd "-Nlevity" "fealty")          => ("str" () (fealty levity) () #f) 
(cmd "--nary1" "surety" "brevity") => ("str" () (brevity surety) () #f) 
(cmd "--nary1=surety" "brevity")   => ("str" () (brevity surety) () #f) 
(cmd "-?")
-| 
Usage: cmd [OPTION ARGUMENT ...] ...

  -f, --flag 
  -o, --optional[=]<number> 
  -n, --nary[=]<symbols> ...
  -N, --nary1[=]<symbols> ...
  -s, --single[=]<string> 

ERROR: getopt->parameter-list "unrecognized option" "-?"

Some commands are defined in all extended relational-databases. The are called just like section Relational Database Operations.

Function: add-domain domain-row
Adds domain-row to the domains table if there is no row in the domains table associated with key (car domain-row) and returns #t. Otherwise returns #f.

For the fields and layout of the domain table, See section Catalog Representation. Currently, these fields are

  • domain-name
  • foreign-table
  • domain-integrity-rule
  • type-id
  • type-param

The following example adds 3 domains to the `build' database. `Optstring' is either a string or #f. filename is a string and build-whats is a symbol.

(for-each (build 'add-domain)
          '((optstring #f
                       (lambda (x) (or (not x) (string? x)))
                       string
                       #f)
            (filename #f #f string #f)
            (build-whats #f #f symbol #f)))

Function: delete-domain domain-name
Removes and returns the domain-name row from the domains table.

Function: domain-checker domain
Returns a procedure to check an argument for conformance to domain domain.

Defining Tables

Procedure: define-tables rdb spec-0 ...
Adds tables as specified in spec-0 ... to the open relational-database rdb. Each spec has the form:

(<name> <descriptor-name> <descriptor-name> <rows>)

or

(<name> <primary-key-fields> <other-fields> <rows>)

where <name> is the table name, <descriptor-name> is the symbol name of a descriptor table, <primary-key-fields> and <other-fields> describe the primary keys and other fields respectively, and <rows> is a list of data rows to be added to the table.

<primary-key-fields> and <other-fields> are lists of field descriptors of the form:

(<column-name> <domain>)

or

(<column-name> <domain> <column-integrity-rule>)

where <column-name> is the column name, <domain> is the domain of the column, and <column-integrity-rule> is an expression whose value is a procedure of one argument (which returns #f to signal an error).

If <domain> is not a defined domain name and it matches the name of this table or an already defined (in one of spec-0 ...) single key field table, a foriegn-key domain will be created for it.

Procedure: create-report rdb destination report-name table
Procedure: create-report rdb destination report-name
The symbol report-name must be primary key in the table named *reports* in the relational database rdb. destination is a port, string, or symbol. If destination is a:

port
The table is created as ascii text and written to that port.
string
The table is created as ascii text and written to the file named by destination.
symbol
destination is the primary key for a row in the table named *printers*.

Each row in the table *reports* has the fields:

name
The report name.
default-table
The table to report on if none is specified.
header, footer
A format string. At the beginning and end of each page respectively, format is called with this string and the (list of) column-names of this table.
reporter
A format string. For each row in the table, format is called with this string and the row.
minimum-break
The minimum number of lines into which the report lines for a row can be broken. Use 0 if a row's lines should not be broken over page boundaries.

Each row in the table *printers* has the fields:

name
The printer name.
print-procedure
The procedure to call to actually print.

The report is prepared as follows:

  • Format (see section Format (version 3.0)) is called with the header field and the (list of) column-names of the table.
  • Format is called with the reporter field and (on successive calls) each record in the natural order for the table. A count is kept of the number of newlines output by format. When the number of newlines to be output exceeds the number of lines per page, the set of lines will be broken if there are more than minimum-break left on this page and the number of lines for this row is larger or equal to twice minimum-break.
  • Format is called with the footer field and the (list of) column-names of the table. The footer field should not output a newline.
  • A new page is output.
  • This entire process repeats until all the rows are output.

The following example shows a new database with the name of `foo.db' being created with tables describing processor families and processor/os/compiler combinations.

The database command define-tables is defined to call define-tables with its arguments. The database is also configured to print `Welcome' when the database is opened. The database is then closed and reopened.

(require 'database-utilities)
(define my-rdb (create-database "foo.db" 'alist-table))

(define-tables my-rdb
  '(*commands*
    ((name symbol))
    ((parameters parameter-list)
     (procedure expression)
     (documentation string))
    ((define-tables
      no-parameters
      no-parameter-names
      (lambda (rdb) (lambda specs (apply define-tables rdb specs)))
      "Create or Augment tables from list of specs")
     (*initialize*
      no-parameters
      no-parameter-names
      (lambda (rdb) (display "Welcome") (newline) rdb)
      "Print Welcome"))))

((my-rdb 'define-tables)
 '(processor-family
   ((family    atom))
   ((also-ran  processor-family))
   ((m68000           #f)
    (m68030           m68000)
    (i386             8086)
    (8086             #f)
    (powerpc          #f)))

 '(platform
   ((name      symbol))
   ((processor processor-family)
    (os        symbol)
    (compiler  symbol))
   ((aix              powerpc aix     -)
    (amiga-dice-c     m68000  amiga   dice-c)
    (amiga-aztec      m68000  amiga   aztec)
    (amiga-sas/c-5.10 m68000  amiga   sas/c)
    (atari-st-gcc     m68000  atari   gcc)
    (atari-st-turbo-c m68000  atari   turbo-c)
    (borland-c-3.1    8086    ms-dos  borland-c)
    (djgpp            i386    ms-dos  gcc)
    (linux            i386    linux   gcc)
    (microsoft-c      8086    ms-dos  microsoft-c)
    (os/2-emx         i386    os/2    gcc)
    (turbo-c-2        8086    ms-dos  turbo-c)
    (watcom-9.0       i386    ms-dos  watcom))))

((my-rdb 'close-database))

(set! my-rdb (open-database "foo.db" 'alist-table))
-|
Welcome

Weight-Balanced Trees

(require 'wt-tree)

Balanced binary trees are a useful data structure for maintaining large sets of ordered objects or sets of associations whose keys are ordered. MIT Scheme has an comprehensive implementation of weight-balanced binary trees which has several advantages over the other data structures for large aggregates:

These features make weight-balanced trees suitable for a wide range of applications, especially those that require large numbers of sets or discrete maps. Applications that have a few global databases and/or concentrate on element-level operations like insertion and lookup are probably better off using hash-tables or red-black trees.

The size of a tree is the number of associations that it contains. Weight balanced binary trees are balanced to keep the sizes of the subtrees of each node within a constant factor of each other. This ensures logarithmic times for single-path operations (like lookup and insertion). A weight balanced tree takes space that is proportional to the number of associations in the tree. For the current implementation, the constant of proportionality is six words per association.

Weight balanced trees can be used as an implementation for either discrete sets or discrete maps (associations). Sets are implemented by ignoring the datum that is associated with the key. Under this scheme if an associations exists in the tree this indicates that the key of the association is a member of the set. Typically a value such as (), #t or #f is associated with the key.

Many operations can be viewed as computing a result that, depending on whether the tree arguments are to thhe arities of each parameter. Corresponds to the arity field of the command's parameter-table. For a description of arity see table above.

types
A list of the type name of each parameter. Correspnds to the type-id field of the contents of the domain of the command's parameter-table.
defaulters
A list of the defaulters for each parameter. Corresponds to the defaulters field of the command's parameter-table.
domain-integrity-rules
A list of procedures (one for each parameter) which tests whether a value for a parameter is acceptable for that parameter. The procedure should be called with each datum in the list for nary arity parameters.
aliases
A list of lists of (alias parameter-name). There can be more than one alias per parameter-name.

For information about parameters, See section Parameter lists. Here is an example of setting up a command with arguments and parsing those arguments from a getopt style argument list (see section Getopt).

(require 'database-utilities)
(require 'fluid-let)
(require 'parameters)
(require 'getopt)

(define my-rdb (create-database #f 'alist-table))

(define-tables my-rdb
  '(foo-params
    *parameter-columns*
    *parameter-columns*
    ((1 single-string single string
        (lambda (pl) '("str")) #f "single string")
     (2 nary-symbols nary symbol
        (lambda (pl) '()) #f "zero or more symbols")
     (3 nary1-symbols nary1 symbol
        (lambda (pl) '(symb)) #f "one or more symbols")
     (4 optional-number optional uint
        (lambda (pl) '()) #f "zero or one number")
     (5 flag boolean boolean
        (lambda (pl) '(#f)) #f "a boolean flag")))
  '(foo-pnames
    ((name string))
    ((parameter-index uint))
    (("s" 1)
     ("single-string" 1)
     ("n" 2)
     ("nary-symbols" 2)
     ("N" 3)
     ("nary1-symbols" 3)
     ("o" 4)
     ("optional-number" 4)
     ("f" 5)
     ("flag" 5)))
  '(my-commands
    ((name symbol))
    ((parameters parameter-list)
     (parameter-names parameter-name-translation)
     (procedure expression)
     (documentation string))
    ((foo
      foo-params
      foo-pnames
      (lambda (rdb) (lambda args (print args)))
      "test command arguments"))))

(define (dbutil:serve-command-line rdb command-table
                                   command argc argv)
  (set! argv (if (vector? argv) (vector->list argv) argv))
  ((make-command-server rdb command-table)
   command
   (lambda (comname comval options positions
                    arities types defaulters dirs aliases) 
     (apply comval (getopt->arglist
                    argc argv options positions
                    arities types defaulters dirs aliases)))))

(define (cmd . opts)
  (fluid-let ((*optind* 1))
    (printf "%-34s => "
            (call-with-output-string (lambda (pt) (write (cons 'cmd opts) pt)))
            ;;(apply string-append (map (lambda (x) (string-append x " ")) opts))
            )
    (set! opts (cons "cmd" opts))
    (force-output)
    (dbutil:serve-command-line
     my-rdb 'my-commands 'foo (length opts) opts)))

(cmd)                              => ("str" () (symb) () #f) 
(cmd "-f")                         => ("str" () (symb) () #t) 
(cmd "--flag")                     => ("str" () (symb) () #t) 
(cmd "-o177")                      => ("str" () (symb) (177) #f) 
(cmd "-o" "177")                   => ("str" () (symb) (177) #f) 
(cmd "--optional" "621")           => ("str" () (symb) (621) #f) 
(cmd "--optional=621")             => ("str" () (symb) (621) #f) 
(cmd "-s" "speciality")            => ("speciality" () (symb) () #f) 
(cmd "-sspeciality")               => ("speciality" () (symb) () #f) 
(cmd "--single" "serendipity")     => ("serendipity" () (symb) () #f) 
(cmd "--single=serendipity")       => ("serendipity" () (symb) () #f) 
(cmd "-n" "gravity" "piety")       => ("str" () (piety gravity) () #f) 
(cmd "-ngravity" "piety")          => ("str" () (piety gravity) () #f) 
(cmd "--nary" "chastity")          => ("str" () (chastity) () #f) 
(cmd "--nary=chastity" "")         => ("str" () ( chastity) () #f) 
(cmd "-N" "calamity")              => ("str" () (calamity) () #f) 
(cmd "-Ncalamity")                 => ("str" () (calamity) () #f) 
(cmd "--nary1" "surety")           => ("str" () (surety) () #f) 
(cmd "--nary1=surety")             => ("str" () (surety) () #f) 
(cmd "-N" "levity" "fealty")       => ("str" () (fealty levity) () #f) 
(cmd "-Nlevity" "fealty")          => ("str" () (fealty levity) () #f) 
(cmd "--nary1" "surety" "brevity") => ("str" () (brevity surety) () #f) 
(cmd "--nary1=surety" "brevity")   => ("str" () (brevity surety) () #f) 
(cmd "-?")
-| 
Usage: cmd [OPTION ARGUMENT ...] ...

  -f, --flag 
  -o, --optional[=]<number> 
  -n, --nary[=]<symbols> ...
  -N, --nary1[=]<symbols> ...
  -s, --single[=]<string> 

ERROR: getopt->parameter-list "unrecognized option" "-?"

Some commands are defined in all extended relational-databases. The are called just like section Relational Database Operations.

Function: add-domain domain-row
Adds domain-row to the domains table if there is no row in the domains table associated with key (car domain-row) and returns #t. Otherwise returns #f.

For the fields and layout of the domain table, See section Catalog Representation. Currently, these fields are

  • domain-name
  • foreign-table
  • domain-integrity-rule
  • type-id
  • type-param

The following example adds 3 domains to the `build' database. `Optstring' is either a string or #f. filename is a string and build-whats is a symbol.

(for-each (build 'add-domain)
          '((optstring #f
                       (lambda (x) (or (not x) (string? x)))
                       string
                       #f)
            (filename #f #f string #f)
            (build-whats #f #f symbol #f)))

Function: delete-domain domain-name
Removes and returns the domain-name row from the domains table.

Function: domain-checker domain
Returns a procedure to check an argument for conformance to domain domain.

Defining Tables

Procedure: define-tables rdb spec-0 ...
Adds tables as specified in spec-0 ... to the open relational-database rdb. Each spec has the form:

(<name> <descriptor-name> <descriptor-name> <rows>)

or

(<name> <primary-key-fields> <other-fields> <rows>)

where <name> is the table name, <descriptor-name> is the symbol name of a descriptor table, <primary-key-fields> and <other-fields> describe the primary keys and other fields respectively, and <rows> is a list of data rows to be added to the table.

<primary-key-fields> and <other-fields> are lists of field descriptors of the form:

(<column-name> <domain>)

or

(<column-name> <domain> <column-integrity-rule>)

where <column-name> is the column name, <domain> is the domain of the column, and <column-integrity-rule> is an expression whose value is a procedure of one argument (which returns #f to signal an error).

If <domain> is not a defined domain name and it matches the name of this table or an already defined (in one of spec-0 ...) single key field table, a foriegn-key domain will be created for it.

Procedure: create-report rdb destination report-name table
Procedure: create-report rdb destination report-name
The symbol report-name must be primary key in the table named *reports* in the relational database rdb. destination is a port, string, or symbol. If destination is a:

port
The table is created as ascii text and written to that port.
string
The table is created as ascii text and written to the file named by destination.
symbol
destination is the primary key for a row in the table named *printers*.

Each row in the table *reports* has the fields:

name
The report name.
default-table
The table to report on if none is specified.
header, footer
A format string. At the beginning and end of each page respectively, format is called with this string and the (list of) column-names of this table.
reporter
A format string. For each row in the table, format is called with this string and the row.
minimum-break
The minimum number of lines into which the report lines for a row can be broken. Use 0 if a row's lines should not be broken over page boundaries.

Each row in the table *printers* has the fields:

name
The printer name.
print-procedure
The procedure to call to actually print.

The report is prepared as follows:

  • Format (see section Format (version 3.0)) is called with the header field and the (list of) column-names of the table.
  • Format is called with the reporter field and (on successive calls) each record in the natural order for the table. A count is kept of the number of newlines output by format. When the number of newlines to be output exceeds the number of lines per page, the set of lines will be broken if there are more than minimum-break left on this page and the number of lines for this row is larger or equal to twice minimum-break.
  • Format is called with the footer field and the (list of) column-names of the table. The footer field should not output a newline.
  • A new page is output.
  • This entire process repeats until all the rows are output.

The following example shows a new database with the name of `foo.db' being created with tables describing processor families and processor/os/compiler combinations.

The database command define-tables is defined to call define-tables with its arguments. The database is also configured to print `Welcome' when the database is opened. The database is then closed and reopened.

(require 'database-utilities)
(define my-rdb (create-database "foo.db" 'alist-table))

(define-tables my-rdb
  '(*commands*
    ((name symbol))
    ((parameters parameter-list)
     (procedure expression)
     (documentation string))
    ((define-tables
      no-parameters
      no-parameter-names
      (lambda (rdb) (lambda specs (apply define-tables rdb specs)))
      "Create or Augment tables from list of specs")
     (*initialize*
      no-parameters
      no-parameter-names
      (lambda (rdb) (display "Welcome") (newline) rdb)
      "Print Welcome"))))

((my-rdb 'define-tables)
 '(processor-family
   ((family    atom))
   ((also-ran  processor-family))
   ((m68000           #f)
    (m68030           m68000)
    (i386             8086)
    (8086             #f)
    (powerpc          #f)))

 '(platform
   ((name      symbol))
   ((processor processor-family)
    (os        symbol)
    (compiler  symbol))
   ((aix              powerpc aix     -)
    (amiga-dice-c     m68000  amiga   dice-c)
    (amiga-aztec      m68000  amiga   aztec)
    (amiga-sas/c-5.10 m68000  amiga   sas/c)
    (atari-st-gcc     m68000  atari   gcc)
    (atari-st-turbo-c m68000  atari   turbo-c)
    (borland-c-3.1    8086    ms-dos  borland-c)
    (djgpp            i386    ms-dos  gcc)
    (linux            i386    linux   gcc)
    (microsoft-c      8086    ms-dos  microsoft-c)
    (os/2-emx         i386    os/2    gcc)
    (turbo-c-2        8086    ms-dos  turbo-c)
    (watcom-9.0       i386    ms-dos  watcom))))

((my-rdb 'close-database))

(set! my-rdb (open-database "foo.db" 'alist-table))
-|
Welcome

Weight-Balanced Trees

(require 'wt-tree)

Balanced binary trees are a useful data structure for maintaining large sets of ordered objects or sets of associations whose keys are ordered. MIT Scheme has an comprehensive implementation of weight-balanced binary trees which has several advantages over the other data structures for large aggregates:

These features make weight-balanced trees suitable for a wide range of applications, especially those that require large numbers of sets or discrete maps. Applications that have a few global databases and/or concentrate on element-level operations like insertion and lookup are probably better off using hash-tables or red-black trees.

The size of a tree is the number of associations that it contains. Weight balanced binary trees are balanced to keep the sizes of the subtrees of each node within a constant factor of each other. This ensures logarithmic times for single-path operations (like lookup and insertion). A weight balanced tree takes space that is proportional to the number of associations in the tree. For the current implementation, the constant of proportionality is six words per association.

Weight balanced trees can be used as an implementation for either discrete sets or discrete maps (associations). Sets are implemented by ignoring the datum that is associated with the key. Under this scheme if an associations exists in the tree this indicates that the key of the association is a member of the set. Typically a value such as (), #t or #f is associated with the key.

Many operations can be viewed as computing a result that, depending on whether the tree arguments are to thhe arities of each parameter. Corresponds to the arity field of the command's parameter-table. For a description of arity see table above.

types
A list of the type name of each parameter. Correspnds to the type-id field of the contents of the domain of the command's parameter-table.
defaulters
A list of the defaulters for each parameter. Corresponds to the defaulters field of the command's parameter-table.
domain-integrity-rules
A list of procedures (one for each parameter) which tests whether a value for a parameter is acceptable for that parameter. The procedure should be called with each datum in the list for nary arity parameters.
aliases
A list of lists of (alias parameter-name). There can be more than one alias per parameter-name.

For information about parameters, See section Parameter lists. Here is an example of setting up a command with arguments and parsing those arguments from a getopt style argument list (see section Getopt).

(require 'database-utilities)
(require 'fluid-let)
(require 'parameters)
(require 'getopt)

(define my-rdb (create-database #f 'alist-table))

(define-tables my-rdb
  '(foo-params
    *parameter-columns*
    *parameter-columns*
    ((1 single-string single string
        (lambda (pl) '("str")) #f "single string")
     (2 nary-symbols nary symbol
        (lambda (pl) '()) #f "zero or more symbols")
     (3 nary1-symbols nary1 symbol
        (lambda (pl) '(symb)) #f "one or more symbols")
     (4 optional-number optional uint
        (lambda (pl) '()) #f "zero or one number")
     (5 flag boolean boolean
        (lambda (pl) '(#f)) #f "a boolean flag")))
  '(foo-pnames
    ((name string))
    ((parameter-index uint))
    (("s" 1)
     ("single-string" 1)
     ("n" 2)
     ("nary-symbols" 2)
     ("N" 3)
     ("nary1-symbols" 3)
     ("o" 4)
     ("optional-number" 4)
     ("f" 5)
     ("flag" 5)))
  '(my-commands
    ((name symbol))
    ((parameters parameter-list)
     (parameter-names parameter-name-translation)
     (procedure expression)
     (documentation string))
    ((foo
      foo-params
      foo-pnames
      (lambda (rdb) (lambda args (print args)))
      "test command arguments"))))

(define (dbutil:serve-command-line rdb command-table
                                   command argc argv)
  (set! argv (if (vector? argv) (vector->list argv) argv))
  ((make-command-server rdb command-table)
   command
   (lambda (comname comval options positions
                    arities types defaulters dirs aliases) 
     (apply comval (getopt->arglist
                    argc argv options positions
                    arities types defaulters dirs aliases)))))

(define (cmd . opts)
  (fluid-let ((*optind* 1))
    (printf "%-34s => "
            (call-with-output-string (lambda (pt) (write (cons 'cmd opts) pt)))
            ;;(apply string-append (map (lambda (x) (string-append x " ")) opts))
            )
    (set! opts (cons "cmd" opts))
    (force-output)
    (dbutil:serve-command-line
     my-rdb 'my-commands 'foo (length opts) opts)))

(cmd)                              => ("str" () (symb) () #f) 
(cmd "-f")                         => ("str" () (symb) () #t) 
(cmd "--flag")                     => ("str" () (symb) () #t) 
(cmd "-o177")                      => ("str" () (symb) (177) #f) 
(cmd "-o" "177")                   => ("str" () (symb) (177) #f) 
(cmd "--optional" "621")           => ("str" () (symb) (621) #f) 
(cmd "--optional=621")             => ("str" () (symb) (621) #f) 
(cmd "-s" "speciality")            => ("speciality" () (symb) () #f) 
(cmd "-sspeciality")               => ("speciality" () (symb) () #f) 
(cmd "--single" "serendipity")     => ("serendipity" () (symb) () #f) 
(cmd "--single=serendipity")       => ("serendipity" () (symb) () #f) 
(cmd "-n" "gravity" "piety")       => ("str" () (piety gravity) () #f) 
(cmd "-ngravity" "piety")          => ("str" () (piety gravity) () #f) 
(cmd "--nary" "chastity")          => ("str" () (chastity) () #f) 
(cmd "--nary=chastity" "")         => ("str" () ( chastity) () #f) 
(cmd "-N" "calamity")              => ("str" () (calamity) () #f) 
(cmd "-Ncalamity")                 => ("str" () (calamity) () #f) 
(cmd "--nary1" "surety")           => ("str" () (surety) () #f) 
(cmd "--nary1=surety")             => ("str" () (surety) () #f) 
(cmd "-N" "levity" "fealty")       => ("str" () (fealty levity) () #f) 
(cmd "-Nlevity" "fealty")          => ("str" () (fealty levity) () #f) 
(cmd "--nary1" "surety" "brevity") => ("str" () (brevity surety) () #f) 
(cmd "--nary1=surety" "brevity")   => ("str" () (brevity surety) () #f) 
(cmd "-?")
-| 
Usage: cmd [OPTION ARGUMENT ...] ...

  -f, --flag 
  -o, --optional[=]<number> 
  -n, --nary[=]<symbols> ...
  -N, --nary1[=]<symbols> ...
  -s, --single[=]<string> 

ERROR: getopt->parameter-list "unrecognized option" "-?"

Some commands are defined in all extended relational-databases. The are called just like section Relational Database Operations.

Function: add-domain domain-row
Adds domain-row to the domains table if there is no row in the domains table associated with key (car domain-row) and returns #t. Otherwise returns #f.

For the fields and layout of the domain table, See section Catalog Representation. Currently, these fields are

  • domain-name
  • foreign-table
  • domain-integrity-rule
  • type-id
  • type-param

The following example adds 3 domains to the `build' database. `Optstring' is either a string or #f. filename is a string and build-whats is a symbol.

(for-each (build 'add-domain)
          '((optstring #f
                       (lambda (x) (or (not x) (string? x)))
                       string
                       #f)
            (filename #f #f string #f)
            (build-whats #f #f symbol #f)))

Function: delete-domain domain-name
Removes and returns the domain-name row from the domains table.

Function: domain-checker domain
Returns a procedure to check an argument for conformance to domain domain.

Defining Tables

Procedure: define-tables rdb spec-0 ...
Adds tables as specified in spec-0 ... to the open relational-database rdb. Each spec has the form:

(<name> <descriptor-name> <descriptor-name> <rows>)

or

(<name> <primary-key-fields> <other-fields> <rows>)

where <name> is the table name, <descriptor-name> is the symbol name of a descriptor table, <primary-key-fields> and <other-fields> describe the primary keys and other fields respectively, and <rows> is a list of data rows to be added to the table.

<primary-key-fields> and <other-fields> are lists of field descriptors of the form:

(<column-name> <domain>)

or

(<column-name> <domain> <column-integrity-rule>)

where <column-name> is the column name, <domain> is the domain of the column, and <column-integrity-rule> is an expression whose value is a procedure of one argument (which returns #f to signal an error).

If <domain> is not a defined domain name and it matches the name of this table or an already defined (in one of spec-0 ...) single key field table, a foriegn-key domain will be created for it.

Procedure: create-report rdb destination report-name table
Procedure: create-report rdb destination report-name
The symbol report-name must be primary key in the table named *reports* in the relational database rdb. destination is a port, string, or symbol. If destination is a:

port
The table is created as ascii text and written to that port.
string
The table is created as ascii text and written to the file named by destination.
symbol
destination is the primary key for a row in the table named *printers*.

Each row in the table *reports* has the fields:

name
The report name.
default-table
The table to report on if none is specified.
header, footer
A format string. At the beginning and end of each page respectively, format is called with this string and the (list of) column-names of this table.
reporter
A format string. For each row in the table, format is called with this string and the row.
minimum-break
The minimum number of lines into which the report lines for a row can be broken. Use 0 if a row's lines should not be broken over page boundaries.

Each row in the table *printers* has the fields:

name
The printer name.
print-procedure
The procedure to call to actually print.

The report is prepared as follows:

  • Format (see section Format (version 3.0)) is called with the header field and the (list of) column-names of the table.
  • Format is called with the reporter field and (on successive calls) each record in the natural order for the table. A count is kept of the number of newlines output by format. When the number of newlines to be output exceeds the number of lines per page, the set of lines will be broken if there are more than minimum-break left on this page and the number of lines for this row is larger or equal to twice minimum-break.
  • Format is called with the footer field and the (list of) column-names of the table. The footer field should not output a newline.
  • A new page is output.
  • This entire process repeats until all the rows are output.

The following example shows a new database with the name of `foo.db' being created with tables describing processor families and processor/os/compiler combinations.

The database command define-tables is defined to call define-tables with its arguments. The database is also configured to print `Welcome' when the database is opened. The database is then closed and reopened.

(require 'database-utilities)
(define my-rdb (create-database "foo.db" 'alist-table))

(define-tables my-rdb
  '(*commands*
    ((name symbol))
    ((parameters parameter-list)
     (procedure expression)
     (documentation string))
    ((define-tables
      no-parameters
      no-parameter-names
      (lambda (rdb) (lambda specs (apply define-tables rdb specs)))
      "Create or Augment tables from list of specs")
     (*initialize*
      no-parameters
      no-parameter-names
      (lambda (rdb) (display "Welcome") (newline) rdb)
      "Print Welcome"))))

((my-rdb 'define-tables)
 '(processor-family
   ((family    atom))
   ((also-ran  processor-family))
   ((m68000           #f)
    (m68030           m68000)
    (i386             8086)
    (8086             #f)
    (powerpc          #f)))

 '(platform
   ((name      symbol))
   ((processor processor-family)
    (os        symbol)
    (compiler  symbol))
   ((aix              powerpc aix     -)
    (amiga-dice-c     m68000  amiga   dice-c)
    (amiga-aztec      m68000  amiga   aztec)
    (amiga-sas/c-5.10 m68000  amiga   sas/c)
    (atari-st-gcc     m68000  atari   gcc)
    (atari-st-turbo-c m68000  atari   turbo-c)
    (borland-c-3.1    8086    ms-dos  borland-c)
    (djgpp            i386    ms-dos  gcc)
    (linux            i386    linux   gcc)
    (microsoft-c      8086    ms-dos  microsoft-c)
    (os/2-emx         i386    os/2    gcc)
    (turbo-c-2        8086    ms-dos  turbo-c)
    (watcom-9.0       i386    ms-dos  watcom))))

((my-rdb 'close-database))

(set! my-rdb (open-database "foo.db" 'alist-table))
-|
Welcome

Weight-Balanced Trees

(require 'wt-tree)

Balanced binary trees are a useful data structure for maintaining large sets of ordered objects or sets of associations whose keys are ordered. MIT Scheme has an comprehensive implementation of weight-balanced binary trees which has several advantages over the other data structures for large aggregates:

These features make weight-balanced trees suitable for a wide range of applications, especially those that require large numbers of sets or discrete maps. Applications that have a few global databases and/or concentrate on element-level operations like insertion and lookup are probably better off using hash-tables or red-black trees.

The size of a tree is the number of associations that it contains. Weight balanced binary trees are balanced to keep the sizes of the subtrees of each node within a constant factor of each other. This ensures logarithmic times for single-path operations (like lookup and insertion). A weight balanced tree takes space that is proportional to the number of associations in the tree. For the current implementation, the constant of proportionality is six words per association.

Weight balanced trees can be used as an implementation for either discrete sets or discrete maps (associations). Sets are implemented by ignoring the datum that is associated with the key. Under this scheme if an associations exists in the tree this indicates that the key of the association is a member of the set. Typically a value such as (), #t or #f is associated with the key.

Many operations can be viewed as computing a result that, depending on whether the tree arguments are to thhe arities of each parameter. Corresponds to the arity field of the command's parameter-table. For a description of arity see table above.

types
A list of the type name of each parameter. Correspnds to the type-id field of the contents of the domain of the command's parameter-table.
defaulters
A list of the defaulters for each parameter. Corresponds to the defaulters field of the command's parameter-table.
domain-integrity-rules
A list of procedures (one for each parameter) which tests whether a value for a parameter is acceptable for that parameter. The procedure should be called with each datum in the list for nary arity parameters.
aliases
A list of lists of (alias parameter-name). There can be more than one alias per parameter-name.

For information about parameters, See section Parameter lists. Here is an example of setting up a command with arguments and parsing those arguments from a getopt style argument list (see section Getopt).

(require 'database-utilities)
(require 'fluid-let)
(require 'parameters)
(require 'getopt)

(define my-rdb (create-database #f 'alist-table))

(define-tables my-rdb
  '(foo-params
    *parameter-columns*
    *parameter-columns*
    ((1 single-string single string
        (lambda (pl) '("str")) #f "single string")
     (2 nary-symbols nary symbol
        (lambda (pl) '()) #f "zero or more symbols")
     (3 nary1-symbols nary1 symbol
        (lambda (pl) '(symb)) #f "one or more symbols")
     (4 optional-number optional uint
        (lambda (pl) '()) #f "zero or one number")
     (5 flag boolean boolean
        (lambda (pl) '(#f)) #f "a boolean flag")))
  '(foo-pnames
    ((name string))
    ((parameter-index uint))
    (("s" 1)
     ("single-string" 1)
     ("n" 2)
     ("nary-symbols" 2)
     ("N" 3)
     ("nary1-symbols" 3)
     ("o" 4)
     ("optional-number" 4)
     ("f" 5)
     ("flag" 5)))
  '(my-commands
    ((name symbol))
    ((parameters parameter-list)
     (parameter-names parameter-name-translation)
     (procedure expression)
     (documentation string))
    ((foo
      foo-params
      foo-pnames
      (lambda (rdb) (lambda args (print args)))
      "test command arguments"))))

(define (dbutil:serve-command-line rdb command-table
                                   command argc argv)
  (set! argv (if (vector? argv) (vector->list argv) argv))
  ((make-command-server rdb command-table)
   command
   (lambda (comname comval options positions
                    arities types defaulters dirs aliases) 
     (apply comval (getopt->arglist
                    argc argv options positions
                    arities types defaulters dirs aliases)))))

(define (cmd . opts)
  (fluid-let ((*optind* 1))
    (printf "%-34s => "
            (call-with-output-string (lambda (pt) (write (cons 'cmd opts) pt)))
            ;;(apply string-append (map (lambda (x) (string-append x " ")) opts))
            )
    (set! opts (cons "cmd" opts))
    (force-output)
    (dbutil:serve-command-line
     my-rdb 'my-commands 'foo (length opts) opts)))

(cmd)                              => ("str" () (symb) () #f) 
(cmd "-f")                         => ("str" () (symb) () #t) 
(cmd "--flag")                     => ("str" () (symb) () #t) 
(cmd "-o177")                      => ("str" () (symb) (177) #f) 
(cmd "-o" "177")                   => ("str" () (symb) (177) #f) 
(cmd "--optional" "621")           => ("str" () (symb) (621) #f) 
(cmd "--optional=621")             => ("str" () (symb) (621) #f) 
(cmd "-s" "speciality")            => ("speciality" () (symb) () #f) 
(cmd "-sspeciality")               => ("speciality" () (symb) () #f) 
(cmd "--single" "serendipity")     => ("serendipity" () (symb) () #f) 
(cmd "--single=serendipity")       => ("serendipity" () (symb) () #f) 
(cmd "-n" "gravity" "piety")       => ("str" () (piety gravity) () #f) 
(cmd "-ngravity" "piety")          => ("str" () (piety gravity) () #f) 
(cmd "--nary" "chastity")          => ("str" () (chastity) () #f) 
(cmd "--nary=chastity" "")         => ("str" () ( chastity) () #f) 
(cmd "-N" "calamity")              => ("str" () (calamity) () #f) 
(cmd "-Ncalamity")                 => ("str" () (calamity) () #f) 
(cmd "--nary1" "surety")           => ("str" () (surety) () #f) 
(cmd "--nary1=surety")             => ("str" () (surety) () #f) 
(cmd "-N" "levity" "fealty")       => ("str" () (fealty levity) () #f) 
(cmd "-Nlevity" "fealty")          => ("str" () (fealty levity) () #f) 
(cmd "--nary1" "surety" "brevity") => ("str" () (brevity surety) () #f) 
(cmd "--nary1=surety" "brevity")   => ("str" () (brevity surety) () #f) 
(cmd "-?")
-| 
Usage: cmd [OPTION ARGUMENT ...] ...

  -f, --flag 
  -o, --optional[=]<number> 
  -n, --nary[=]<symbols> ...
  -N, --nary1[=]<symbols> ...
  -s, --single[=]<string> 

ERROR: getopt->parameter-list "unrecognized option" "-?"

Some commands are defined in all extended relational-databases. The are called just like section Relational Database Operations.

Function: add-domain domain-row
Adds domain-row to the domains table if there is no row in the domains table associated with key (car domain-row) and returns #t. Otherwise returns #f.

For the fields and layout of the domain table, See section Catalog Representation. Currently, these fields are

  • domain-name
  • foreign-table
  • domain-integrity-rule
  • type-id
  • type-param

The following example adds 3 domains to the `build' database. `Optstring' is either a string or #f. filename is a string and build-whats is a symbol.

(for-each (build 'add-domain)
          '((optstring #f
                       (lambda (x) (or (not x) (string? x)))
                       string
                       #f)
            (filename #f #f string #f)
            (build-whats #f #f symbol #f)))

Function: delete-domain domain-name
Removes and returns the domain-name row from the domains table.

Function: domain-checker domain
Returns a procedure to check an argument for conformance to domain domain.

Defining Tables

Procedure: define-tables rdb spec-0 ...
Adds tables as specified in spec-0 ... to the open relational-database rdb. Each spec has the form:

(<name> <descriptor-name> <descriptor-name> <rows>)

or

(<name> <primary-key-fields> <other-fields> <rows>)

where <name> is the table name, <descriptor-name> is the symbol name of a descriptor table, <primary-key-fields> and <other-fields> describe the primary keys and other fields respectively, and <rows> is a list of data rows to be added to the table.

<primary-key-fields> and <other-fields> are lists of field descriptors of the form:

(<column-name> <domain>)

or

(<column-name> <domain> <column-integrity-rule>)

where <column-name> is the column name, <domain> is the domain of the column, and <column-integrity-rule> is an expression whose value is a procedure of one argument (which returns #f to signal an error).

If <domain> is not a defined domain name and it matches the name of this table or an already defined (in one of spec-0 ...) single key field table, a foriegn-key domain will be created for it.

Procedure: create-report rdb destination report-name table
Procedure: create-report rdb destination report-name
The symbol report-name must be primary key in the table named *reports* in the relational database rdb. destination is a port, string, or symbol. If destination is a:

port
The table is created as ascii text and written to that port.
string
The table is created as ascii text and written to the file named by destination.
symbol
destination is the primary key for a row in the table named *printers*.

Each row in the table *reports* has the fields:

name
The report name.
default-table
The table to report on if none is specified.
header, footer
A format string. At the beginning and end of each page respectively, format is called with this string and the (list of) column-names of this table.
reporter
A format string. For each row in the table, format is called with this string and the row.
minimum-break
The minimum number of lines into which the report lines for a row can be broken. Use 0 if a row's lines should not be broken over page boundaries.

Each row in the table *printers* has the fields:

name
The printer name.
print-procedure
The procedure to call to actually print.

The report is prepared as follows:

  • Format (see section Format (version 3.0)) is called with the header field and the (list of) column-names of the table.
  • Format is called with the reporter field and (on successive calls) each record in the natural order for the table. A count is kept of the number of newlines output by format. When the number of newlines to be output exceeds the number of lines per page, the set of lines will be broken if there are more than minimum-break left on this page and the number of lines for this row is larger or equal to twice minimum-break.
  • Format is called with the footer field and the (list of) column-names of the table. The footer field should not output a newline.
  • A new page is output.
  • This entire process repeats until all the rows are output.

The following example shows a new database with the name of `foo.db' being created with tables describing processor families and processor/os/compiler combinations.

The database command define-tables is defined to call define-tables with its arguments. The database is also configured to print `Welcome' when the database is opened. The database is then closed and reopened.

(require 'database-utilities)
(define my-rdb (create-database "foo.db" 'alist-table))

(define-tables my-rdb
  '(*commands*
    ((name symbol))
    ((parameters parameter-list)
     (procedure expression)
     (documentation string))
    ((define-tables
      no-parameters
      no-parameter-names
      (lambda (rdb) (lambda specs (apply define-tables rdb specs)))
      "Create or Augment tables from list of specs")
     (*initialize*
      no-parameters
      no-parameter-names
      (lambda (rdb) (display "Welcome") (newline) rdb)
      "Print Welcome"))))

((my-rdb 'define-tables)
 '(processor-family
   ((family    atom))
   ((also-ran  processor-family))
   ((m68000           #f)
    (m68030           m68000)
    (i386             8086)
    (8086             #f)
    (powerpc          #f)))

 '(platform
   ((name      symbol))
   ((processor processor-family)
    (os        symbol)
    (compiler  symbol))
   ((aix              powerpc aix     -)
    (amiga-dice-c     m68000  amiga   dice-c)
    (amiga-aztec      m68000  amiga   aztec)
    (amiga-sas/c-5.10 m68000  amiga   sas/c)
    (atari-st-gcc     m68000  atari   gcc)
    (atari-st-turbo-c m68000  atari   turbo-c)
    (borland-c-3.1    8086    ms-dos  borland-c)
    (djgpp            i386    ms-dos  gcc)
    (linux            i386    linux   gcc)
    (microsoft-c      8086    ms-dos  microsoft-c)
    (os/2-emx         i386    os/2    gcc)
    (turbo-c-2        8086    ms-dos  turbo-c)
    (watcom-9.0       i386    ms-dos  watcom))))

((my-rdb 'close-database))

(set! my-rdb (open-database "foo.db" 'alist-table))
-|
Welcome

Weight-Balanced Trees

(require 'wt-tree)

Balanced binary trees are a useful data structure for maintaining large sets of ordered objects or sets of associations whose keys are ordered. MIT Scheme has an comprehensive implementation of weight-balanced binary trees which has several advantages over the other data structures for large aggregates:

These features make weight-balanced trees suitable for a wide range of applications, especially those that require large numbers of sets or discrete maps. Applications that have a few global databases and/or concentrate on element-level operations like insertion and lookup are probably better off using hash-tables or red-black trees.

The size of a tree is the number of associations that it contains. Weight balanced binary trees are balanced to keep the sizes of the subtrees of each node within a constant factor of each other. This ensures logarithmic times for single-path operations (like lookup and insertion). A weight balanced tree takes space that is proportional to the number of associations in the tree. For the current implementation, the constant of proportionality is six words per association.

Weight balanced trees can be used as an implementation for either discrete sets or discrete maps (associations). Sets are implemented by ignoring the datum that is associated with the key. Under this scheme if an associations exists in the tree this indicates that the key of the association is a member of the set. Typically a value such as (), #t or #f is associated with the key.

Many operations can be viewed as computing a result that, depending on whether the tree arguments are to thhe arities of each parameter. Corresponds to the arity field of the command's parameter-table. For a description of arity see table above.

types
A list of the type name of each parameter. Correspnds to the type-id field of the contents of the domain of the command's parameter-table.
defaulters
A list of the defaulters for each parameter. Corresponds to the defaulters field of the command's parameter-table.
domain-integrity-rules
A list of procedures (one for each parameter) which tests whether a value for a parameter is acceptable for that parameter. The procedure should be called with each datum in the list for nary arity parameters.
aliases
A list of lists of (alias parameter-name). There can be more than one alias per parameter-name.

For information about parameters, See section Parameter lists. Here is an example of setting up a command with arguments and parsing those arguments from a getopt style argument list (see section Getopt).

(require 'database-utilities)
(require 'fluid-let)
(require 'parameters)
(require 'getopt)

(define my-rdb (create-database #f 'alist-table))

(define-tables my-rdb
  '(foo-params
    *parameter-columns*
    *parameter-columns*
    ((1 single-string single string
        (lambda (pl) '("str")) #f "single string")
     (2 nary-symbols nary symbol
        (lambda (pl) '()) #f "zero or more symbols")
     (3 nary1-symbols nary1 symbol
        (lambda (pl) '(symb)) #f "one or more symbols")
     (4 optional-number optional uint
        (lambda (pl) '()) #f "zero or one number")
     (5 flag boolean boolean
        (lambda (pl) '(#f)) #f "a boolean flag")))
  '(foo-pnames
    ((name string))
    ((parameter-index uint))
    (("s" 1)
     ("single-string" 1)
     ("n" 2)
     ("nary-symbols" 2)
     ("N" 3)
     ("nary1-symbols" 3)
     ("o" 4)
     ("optional-number" 4)
     ("f" 5)
     ("flag" 5)))
  '(my-commands
    ((name symbol))
    ((parameters parameter-list)
     (parameter-names parameter-name-translation)
     (procedure expression)
     (documentation string))
    ((foo
      foo-params
      foo-pnames
      (lambda (rdb) (lambda args (print args)))
      "test command arguments"))))

(define (dbutil:serve-command-line rdb command-table
                                   command argc argv)
  (set! argv (if (vector? argv) (vector->list argv) argv))
  ((make-command-server rdb command-table)
   command
   (lambda (comname comval options positions
                    arities types defaulters dirs aliases) 
     (apply comval (getopt->arglist
                    argc argv options positions
                    arities types defaulters dirs aliases)))))

(define (cmd . opts)
  (fluid-let ((*optind* 1))
    (printf "%-34s => "
            (call-with-output-string (lambda (pt) (write (cons 'cmd opts) pt)))
            ;;(apply string-append (map (lambda (x) (string-append x " ")) opts))
            )
    (set! opts (cons "cmd" opts))
    (force-output)
    (dbutil:serve-command-line
     my-rdb 'my-commands 'foo (length opts) opts)))

(cmd)                              => ("str" () (symb) () #f) 
(cmd "-f")                         => ("str" () (symb) () #t) 
(cmd "--flag")                     => ("str" () (symb) () #t) 
(cmd "-o177")                      => ("str" () (symb) (177) #f) 
(cmd "-o" "177")                   => ("str" () (symb) (177) #f) 
(cmd "--optional" "621")           => ("str" () (symb) (621) #f) 
(cmd "--optional=621")             => ("str" () (symb) (621) #f) 
(cmd "-s" "speciality")            => ("speciality" () (symb) () #f) 
(cmd "-sspeciality")               => ("speciality" () (symb) () #f) 
(cmd "--single" "serendipity")     => ("serendipity" () (symb) () #f) 
(cmd "--single=serendipity")       => ("serendipity" () (symb) () #f) 
(cmd "-n" "gravity" "piety")       => ("str" () (piety gravity) () #f) 
(cmd "-ngravity" "piety")          => ("str" () (piety gravity) () #f) 
(cmd "--nary" "chastity")          => ("str" () (chastity) () #f) 
(cmd "--nary=chastity" "")         => ("str" () ( chastity) () #f) 
(cmd "-N" "calamity")              => ("str" () (calamity) () #f) 
(cmd "-Ncalamity")                 => ("str" () (calamity) () #f) 
(cmd "--nary1" "surety")           => ("str" () (surety) () #f) 
(cmd "--nary1=surety")             => ("str" () (surety) () #f) 
(cmd "-N" "levity" "fealty")       => ("str" () (fealty levity) () #f) 
(cmd "-Nlevity" "fealty")          => ("str" () (fealty levity) () #f) 
(cmd "--nary1" "surety" "brevity") => ("str" () (brevity surety) () #f) 
(cmd "--nary1=surety" "brevity")   => ("str" () (brevity surety) () #f) 
(cmd "-?")
-| 
Usage: cmd [OPTION ARGUMENT ...] ...

  -f, --flag 
  -o, --optional[=]<number> 
  -n, --nary[=]<symbols> ...
  -N, --nary1[=]<symbols> ...
  -s, --single[=]<string> 

ERROR: getopt->parameter-list "unrecognized option" "-?"

Some commands are defined in all extended relational-databases. The are called just like section Relational Database Operations.

Function: add-domain domain-row
Adds domain-row to the domains table if there is no row in the domains table associated with key (car domain-row) and returns #t. Otherwise returns #f.

For the fields and layout of the domain table, See section Catalog Representation. Currently, these fields are

  • domain-name
  • foreign-table
  • domain-integrity-rule
  • type-id
  • type-param