From 8466d8cfa486fb30d1755c4261b781135083787b Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Mon, 20 Feb 2017 00:05:29 -0800 Subject: Import Upstream version 3a1 --- slib.texi | 7882 ++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 4394 insertions(+), 3488 deletions(-) (limited to 'slib.texi') diff --git a/slib.texi b/slib.texi index 3acaa14..0453584 100644 --- a/slib.texi +++ b/slib.texi @@ -27,7 +27,7 @@ This file documents SLIB, the portable Scheme library. Copyright (C) 1993 Todd R. Eigenschink@* -Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000 Aubrey Jaffer +Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002 Aubrey Jaffer Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice @@ -117,6 +117,7 @@ implementation, user, or directory. @menu * The Library System:: How to use and customize. +* Universal SLIB Procedures:: Provided for all implementations. * Scheme Syntax Extension Packages:: * Textual Conversion Packages:: * Mathematical Packages:: @@ -126,31 +127,32 @@ implementation, user, or directory. * Index:: @end menu -@node The Library System, Scheme Syntax Extension Packages, Top, Top +@node The Library System, Universal SLIB Procedures, Top, Top @chapter The Library System @menu * Feature:: SLIB names. -* Requesting Features:: +* Require:: * Library Catalogs:: -* Catalog Compilation:: -* Built-in Support:: -* About this manual:: +* Catalog Creation:: +* Catalog Vicinities:: +* Compiling Scheme:: @end menu -@node Feature, Requesting Features, The Library System, The Library System +@node Feature, Require, The Library System, The Library System @section Feature @noindent @cindex feature SLIB denotes @dfn{features} by symbols. SLIB maintains a list of -features supported by the Scheme @dfn{session}. The set of features +features supported by a Scheme @dfn{session}. The set of features @cindex session -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. +provided by a session may change during that session. Some features +are properties of the Scheme implementation being used. The following +@cindex intrinsic feature +@dfn{intrinsic feature}s detail what sort of numbers are available +from an implementation: @itemize @bullet @item @@ -166,17 +168,46 @@ implementation. @end itemize @noindent -Other features correspond to the presence of sets of Scheme procedures -or syntax (macros). +SLIB initialization (in @file{require.scm}) tests and @dfn{provide}s +any of these numeric features which are appropriate. + +@noindent +Other features correspond to the presence of packages of Scheme +procedures or syntax (macros). @defun provided? feature -Returns @code{#t} if @var{feature} is supported by the current Scheme -session. +Returns @code{#t} if @var{feature} is present in the current Scheme +session; otherwise @code{#f}. More specifically, @code{provided?} +returns @code{#t} if the symbol @var{feature} is the +@code{software-type} or if @var{feature} has been provided by a module +already loaded; and @code{#f} otherwise. + +In some implementations @code{provided?} tests whether a module has +been @code{require}d by any module or in any thread; other +implementations will have @code{provided?} reflect only the modules +@code{require}d by that particular session or thread. + +To work portably in both scenarios, use @code{provided?} only to test +whether intrinsic properties (like those above) are present. + +The @var{feature} argument can also be an expression calling +@code{and}, @code{or}, and @code{not} of features. The boolean result +of the logical question asked by @var{feature} is returned. +@end defun + +@noindent +The generalization of @code{provided?} for arbitrary features and catalog +is @code{feature-eval}: + +@defun feature-eval expression provided? +Evaluates @code{and}, @code{or}, and @code{not} forms in +@var{expression}, using the values returned by calling @var{provided?} +on the leaf symbols. @code{feature-eval} returns the boolean result +of the logical combinations. @end defun -@deffn Procedure provide feature -Informs SLIB that @var{feature} is supported. Henceforth -@code{(provided? @var{feature})} will return @code{#t}. +@deffn {Procedure} provide feature +Informs SLIB that @var{feature} is supported in this session. @end deffn @example @@ -185,15 +216,35 @@ Informs SLIB that @var{feature} is supported. Henceforth (provided? 'foo) @result{} #t @end example +@c @defvar *features* +@c Is a list of symbols denoting features present in this implementation. +@c @var{*features*} can grow as modules are @code{require}d. +@c @footnote{The variables @var{*modules*} and @var{*features*} were +@c originally modeled on variables of the same names in common-lisp. But +@c the distinction between features native to an implementation versus +@c those provided by loading files was not useful. The symbols in +@c @var{*features*} now indicate the presence of a capability regardless +@c of how it was provided.} +@c @end defvar + -@node Requesting Features, Library Catalogs, Feature, The Library System -@section Requesting Features +@node Require, Library Catalogs, Feature, The Library System +@section Require @noindent @cindex catalog SLIB creates and maintains a @dfn{catalog} mapping features to locations of files introducing procedures and syntax denoted by those features. +@defvar *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, @code{source}, @code{compiled}, or one of the other cases +described in @ref{Library Catalogs}. The cdr of the pathname should +be either a string or a list. +@end defvar + @noindent At the beginning of each section of this manual, there is a line like @code{(require '@var{feature})}. @@ -205,16 +256,16 @@ names map to the corresponding files. SLIB provides a form, @code{require}, which loads the files providing the requested feature. -@deffn Procedure require feature +@deffn {Procedure} require feature @itemize @bullet @item If @code{(provided? @var{feature})} is true, -then @code{require} just returns an unspecified value. +then @code{require} just returns. @item Otherwise, if @var{feature} is found in the catalog, then the -corresponding files will be loaded and an unspecified value returned. - -Subsequently @code{(provided? @var{feature})} will return @code{#t}. +corresponding files will be loaded and @code{(provided? +@var{feature})} will henceforth return @code{#t}. That @var{feature} +is thereafter @code{provided}. @item Otherwise (@var{feature} not found in the catalog), an error is signaled. @@ -222,47 +273,58 @@ signaled. @end deffn @noindent -The catalog can also be queried using @code{require:feature->path}. +There is a related form @code{require-if}, used primarily for enabling +compilers to statically include modules which would be dynamically +loaded by interpreters. -@defun require:feature->path feature -@itemize @bullet -@item -If @var{feature} is already provided, then returns @code{#t}. -@item -Otherwise, if @var{feature} is in the catalog, the path or list of paths -associated with @var{feature} is returned. -@item -Otherwise, returns @code{#f}. -@end itemize -@end defun +@deffn {Procedure} require-if condition feature +Requires @var{feature} if @var{condition} is true. +@end deffn -@node Library Catalogs, Catalog Compilation, Requesting Features, The Library System -@section Library Catalogs +@noindent +The @code{random} module uses @code{require-if} to flag +@code{object->string} as a (dynamic) required module. + +@example +(require 'byte) +(require 'logical) +(require-if 'compiling 'object->string) +@end example @noindent -At the start of a session no catalog is present, but is created with the -first catalog inquiry (such as @code{(require 'random)}). Several -sources of catalog information are combined to produce the catalog: +The @code{batch} module uses @code{require-if} to flag +@code{posix-time} as a module to load if the implementation supports +large precision exact integers. -@itemize @bullet -@item -standard SLIB packages. -@item -additional packages of interest to this site. -@item -packages specifically for the variety of Scheme which this -session is running. -@item -packages this user wants to always have available. This catalog is the -file @file{homecat} in the user's @dfn{HOME} directory. -@cindex HOME -@item -packages germane to working in this (current working) directory. This -catalog is the file @file{usercat} in the directory to which it applies. -One would typically @code{cd} to this directory before starting the -Scheme session. -@end itemize +@example +(require-if '(and bignum compiling) 'posix-time) +@end example + +@noindent +The @code{commutative-ring} module uses @code{require-if} to ensure +that it has an exponentiation routine, regardless of whether the +implementation supports inexact numbers: + +@example +(require-if '(not inexact) 'logical) ;for integer-expt +(define number^ (if (provided? 'inexact) expt integer-expt)) +@end example + +@noindent +The catalog can also be queried using @code{slib:in-catalog?}. + +@defun slib:in-catalog? feature +Returns a @code{CDR} of the catalog entry if one was found for the +symbol @var{feature} in the alist @code{*catalog*} (and transitively +through any symbol aliases encountered). Otherwise, returns +@code{#f}. The format of catalog entries is explained in @ref{Library +Catalogs}. +@end defun + + +@node Library Catalogs, Catalog Creation, Require, The Library System +@section Library Catalogs @noindent Catalog files consist of one or more @dfn{association list}s. @@ -277,9 +339,14 @@ Redirects to the feature named @i{}. @item (@var{feature} . "@i{}") Loads file @i{}. @item (@var{feature} source "@i{"}) +@cindex source @code{slib:load}s the Scheme source file @i{}. @item (@var{feature} compiled "@i{"} @dots{}) +@cindex compiled @code{slib:load-compiled}s the files @i{} @dots{}. +@item (@var{feature} aggregate @i{} @dots{}) +@cindex aggregate +@code{slib:require}s the features @i{} @dots{}. @end table @noindent @@ -289,40 +356,58 @@ appropriate for the implementation. @table @code @item (@var{feature} defmacro "@i{"}) +@cindex defmacro @code{defmacro:load}s the Scheme source file @i{}. @item (@var{feature} macro-by-example "@i{"}) +@cindex macro-by-example @code{defmacro:load}s the Scheme source file @i{}. @end table @table @code @item (@var{feature} macro "@i{"}) +@cindex macro @code{macro:load}s the Scheme source file @i{}. @item (@var{feature} macros-that-work "@i{"}) +@cindex macros-that-work @code{macro:load}s the Scheme source file @i{}. @item (@var{feature} syntax-case "@i{"}) +@cindex syntax-case @code{macro:load}s the Scheme source file @i{}. @item (@var{feature} syntactic-closures "@i{"}) +@cindex syntactic-closures @code{macro:load}s the Scheme source file @i{}. @end table -@noindent -Here is an example of a @file{usercat} catalog. A Program in this -directory can invoke the @samp{run} feature with @code{(require 'run)}. - -@example -;;; "usercat": SLIB catalog additions for SIMSYNCH. -*-scheme-*- - -( - (simsynch . "../synch/simsynch.scm") - (run . "../synch/run.scm") - (schlep . "schlep.scm") -) -@end example +@node Catalog Creation, Catalog Vicinities, Library Catalogs, The Library System +@section Catalog Creation -@node Catalog Compilation, Built-in Support, Library Catalogs, The Library System -@section Catalog Compilation +@noindent +At the start of an interactive session no catalog is present, but is +created with the first catalog inquiry (such as @code{(require +'random)}). Several sources of catalog information are combined to +produce the catalog: +@itemize @bullet +@item +standard SLIB packages. +@item +additional packages of interest to this site. +@item +packages specifically for the variety of Scheme which this +session is running. +@item +packages this user wants to always have available. This catalog is the +file @file{homecat} in the user's @dfn{HOME} directory. +@cindex HOME +@item +packages germane to working in this (current working) directory. This +catalog is the file @file{usercat} in the directory to which it applies. +One would typically @code{cd} to this directory before starting the +Scheme session. +@item +packages which are part of an application program. +@end itemize @noindent SLIB combines the catalog information which doesn't vary per user into @@ -333,34 +418,39 @@ installation to installation, SLIB builds a separate catalog for each implementation it is used with. @noindent -The definition of @code{*SLIB-VERSION*} in SLIB file @file{require.scm} -is checked against the catalog association of @code{*SLIB-VERSION*} to -ascertain when versions have changed. I recommend that the definition -of @code{*SLIB-VERSION*} be changed whenever the library is changed. If -multiple implementations of Scheme use SLIB, remember that recompiling -one @file{slibcat} will fix only that implementation's catalog. +The definition of @code{*SLIB-VERSION*} in SLIB file +@file{require.scm} is checked against the catalog association of +@code{*SLIB-VERSION*} to ascertain when versions have changed. It is +a reasonable practice to change the definition of +@code{*SLIB-VERSION*} whenever the library is changed. If multiple +implementations of Scheme use SLIB, remember that recompiling one +@file{slibcat} will update only that implementation's catalog. @noindent The compilation scripts of Scheme implementations which work with SLIB can automatically trigger catalog compilation by deleting -@file{slibcat} or by invoking a special form of @code{require}: +@file{slibcat} or by invoking @code{require} of a special feature: -@deffn Procedure require @r{'new-catalog} +@deffn {Procedure} require @r{'new-catalog} @cindex new-catalog This will load @file{mklibcat}, which compiles and writes a new @file{slibcat}. @end deffn @noindent -Another special form of @code{require} erases SLIB's catalog, forcing it -to be reloaded the next time the catalog is queried. +Another special feature of @code{require} erases SLIB's catalog, +forcing it to be reloaded the next time the catalog is queried. -@deffn Procedure require @r{#f} +@deffn {Procedure} require @r{#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. @end deffn + +@node Catalog Vicinities, Compiling Scheme, Catalog Creation, The Library System +@section Catalog Vicinities + @noindent Each file in the table below is descibed in terms of its file-system independent @dfn{vicinity} (@pxref{Vicinity}). The entries @@ -404,113 +494,213 @@ This file contains the associations specific to an SLIB user. @item @code{user-vicinity} @file{usercat} @cindex usercat -This file contains associations effecting only those sessions whose +This file contains associations affecting only those sessions whose @dfn{working directory} is @code{user-vicinity}. @end table -@node Built-in Support, About this manual, Catalog Compilation, The Library System -@section Built-in Support +@noindent +Here is an example of a @file{usercat} catalog. A program in this +directory can invoke the @samp{run} feature with @code{(require 'run)}. + +@example +;;; "usercat": SLIB catalog additions for SIMSYNCH. -*-scheme-*- +( + (simsynch . "../synch/simsynch.scm") + (run . "../synch/run.scm") + (schlep . "schlep.scm") +) +@end example @noindent -The procedures described in these sections are supported by all -implementations as part of the @samp{*.init} files or by -@file{require.scm}. +Copying @file{usercat} to many directories is inconvenient. +Application programs which aren't always run in specially prepared +directories can nonetheless register their features during +initialization. -@menu -* Require:: Module Management -* Vicinity:: Pathname Management -* Configuration:: Characteristics of Scheme Implementation -* Input/Output:: Things not provided by the Scheme specs. -* Legacy:: -* System:: LOADing, EVALing, ERRORing, and EXITing -@end menu +@deffn {Procedure} catalog:read vicinity catalog +Reads file named by string @var{catalog} in @var{vicinity}, resolving +all paths relative to @var{vicinity}, and adds those feature +associations to @var{*catalog*}. +@code{catalog:read} would typically be used by an application program +having dynamically loadable modules. For instance, to register +factoring and other modules in @var{*catalog*}, JACAL does: -@node Require, Vicinity, Built-in Support, Built-in Support -@subsection Require +@example +(catalog:read (program-vicinity) "jacalcat") +@end example -@defvar *features* -Is a list of symbols denoting features supported in this implementation. -@var{*features*} can grow as modules are @code{require}d. -@var{*features*} must be defined by all implementations -(@pxref{Porting}). +@end deffn -Here are features which SLIB (@file{require.scm}) adds to -@var{*features*} when appropriate. +@noindent +For an application program there are three appropriate venues for +registering its catalog associations: @itemize @bullet @item -'inexact +in a @file{usercat} file in the directory where the program runs; or @item -'rational +in an @file{implcat} file in the @code{implementation-vicinity}; or @item -'real +in an application program directory; loaded by calling +@code{catalog:read}. +@end itemize + + +@node Compiling Scheme, , Catalog Vicinities, The Library System +@section Compiling Scheme + +To use Scheme compilers effectively with SLIB the compiler needs to +know which SLIB modules are to be compiled and which symbols are +exported from those modules. + +The procedures in this section automate the extraction of this +information from SLIB modules. They are guaranteed to work on SLIB +modules; to use them on other sources, those sources should follow +SLIB conventions. + +@menu +* Module Conventions:: +* Module Manifests:: +* Module Semantics:: +* Top-level Variable References:: +* Module Analysis:: +@end menu + +@node Module Conventions, Module Manifests, Compiling Scheme, Compiling Scheme +@subsection Module Conventions + +@itemize @bullet @item -'complex +All the top-level @code{require} commands have one quoted argument and +are positioned before other Scheme definitions and expressions in the +file. @item -'bignum +Any conditionally @code{require}d SLIB modules +@footnote{There are some functions with internal @code{require} calls +to delay loading modules until they are needed. While this reduces +startup latency for interpreters, it can produce headaches for +compilers.} +also appear at the beginning of their files conditioned on the feature +@cindex compiling +@code{compiling} using @code{require-if} +(@pxref{Require, require-if}). + +@example +(require 'logical) +(require 'multiarg/and-) +(require-if 'compiling 'sort) +(require-if 'compiling 'ciexyz) +@end example + +@item +Schmooz-style comments preceding a definition, identify that +definition as an exported identifier (@pxref{Schmooz}). For +non-schmooz files, putting @samp{;@@} at the beginning of the line +immediately preceding the definition (@code{define}, +@code{define-syntax}, or @code{defmacro}) suffices. + +@example +;@@ +(define (make-vicinity ) ) +@end example + +@item +Syntax (macro) definitions are grouped at the end of a module file. + +@item +Modules defining macros do not invoke those macros. SLIB macro +implementations are exempt from this rule. + +An example of how to expand macro invocations is: + +@example +(require 'macros-that-work) +(require 'yasos) +(require 'pprint-file) +(pprint-filter-file "collect.scm" macwork:expand) +@end example + @end itemize -For each item, @code{(provided? '@var{feature})} will return @code{#t} -if that feature is available, and @code{#f} if not. -@end defvar -@defvar *modules* -Is a list of pathnames denoting files which have been loaded. -@end defvar +@node Module Manifests, Module Semantics, Module Conventions, Compiling Scheme +@subsection Module Manifests -@defvar *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, @code{source}, or @code{compiled}. The cdr of the pathname -should be either a string or a list. -@end defvar +@include manifest.txi -@noindent -In the following functions if the argument @var{feature} is not a symbol -it is assumed to be a pathname. -@defun provided? feature -Returns @code{#t} if @var{feature} is a member of @code{*features*} or -@code{*modules*} or if @var{feature} is supported by a file already -loaded and @code{#f} otherwise. -@end defun - -@deffn Procedure require feature -@var{feature} is a symbol. If @code{(provided? @var{feature})} is true -@code{require} returns. Otherwise, if @code{(assq @var{feature} -*catalog*)} is not @code{#f}, the associated files will be loaded and -@code{(provided? @var{feature})} will henceforth return @code{#t}. An -unspecified value is returned. If @var{feature} is not found in -@code{*catalog*}, then an error is signaled. - -@deffnx Procedure require pathname -@var{pathname} is a string. If @var{pathname} has not already been -given as an argument to @code{require}, @var{pathname} is loaded. An -unspecified value is returned. -@end deffn +@node Module Semantics, Top-level Variable References, Module Manifests, Compiling Scheme +@subsection Module Semantics -@deffn Procedure provide feature -Assures that @var{feature} is contained in @code{*features*} if -@var{feature} is a symbol and @code{*modules*} otherwise. -@end deffn +For the purpose of compiling Scheme code, each top-level +@code{require} makes the identifiers exported by its feature's module +@code{defined} (or defmacroed or defined-syntaxed) within the file +(being compiled) headed with those requires. + +Top-level occurrences of @code{require-if} make defined the exports +from the module named by the second argument @emph{if} the +@var{feature-expression} first argument is true in the target +environment. The target feature @code{compiling} should be provided +during this phase of compilation. + +Non-top-level SLIB occurences of @code{require} and @code{require-if} +of quoted features can be ignored by compilers. The SLIB modules will +all have top-level constructs for those features. + +@cindex aggregate +Note that aggregate catalog entries import more than one module. +Implementations of @code{require} may or may @emph{not} be transitive; +code which uses module exports without requiring the providing module +is in error. + +In the SLIB modules @code{modular}, @code{batch}, @code{hash}, +@code{common-lisp-time}, @code{commutative-ring}, @code{charplot}, +@code{logical}, @code{common-list-functions}, @code{coerce} and +@code{break} there is code conditional on features being +@code{provided?}. Most are testing for the presence of features which +are intrinsic to implementations (inexacts, bignums, ...). + +In all cases these @code{provided?} tests can be evaluated at +compile-time using @code{feature-eval} +(@pxref{Feature, feature-eval}). The simplest way to compile these +constructs may be to treat @code{provided?} as a macro. + + +@node Top-level Variable References, Module Analysis, Module Semantics, Compiling Scheme +@subsection Top-level Variable References + +@include top-refs.txi -@defun require:feature->path feature -Returns @code{#t} if @var{feature} is a member of @code{*features*} or -@code{*modules*} or if @var{feature} is supported by a file already -loaded. Returns a path if one was found in @code{*catalog*} under the -feature name, and @code{#f} otherwise. The path can either be a string -suitable as an argument to load or a pair as described above for -*catalog*. -@end defun +@node Module Analysis, , Top-level Variable References, Compiling Scheme +@subsection Module Analysis + +@include vet.txi + + + +@node Universal SLIB Procedures, Scheme Syntax Extension Packages, The Library System, Top +@chapter Universal SLIB Procedures + +@noindent +The procedures described in these sections are supported by all +implementations as part of the @samp{*.init} files or by +@file{require.scm}. + +@menu +* Vicinity:: Pathname Management +* Configuration:: Characteristics of Scheme Implementation +* Input/Output:: Things not provided by the Scheme specs. +* System:: LOADing, EVALing, ERRORing, and EXITing +* Miscellany:: +@end menu -@node Vicinity, Configuration, Require, Built-in Support -@subsection Vicinity +@node Vicinity, Configuration, Universal SLIB Procedures, Universal SLIB Procedures +@section Vicinity @noindent A vicinity is a descriptor for a place in the file system. Vicinities @@ -525,8 +715,17 @@ these procedures are file system dependent. @noindent These procedures are provided by all implementations. -@defun make-vicinity path -Returns the vicinity of @var{path} for use by @code{in-vicinity}. +@defun make-vicinity dirpath +Returns @var{dirpath} as a vicinity for use as first argument to +@code{in-vicinity}. +@end defun + +@defun pathname->vicinity path +Returns the vicinity containing @var{path}. +@example +(pathname->vicinity "/usr/local/lib/scm/Link.scm") + @result{} "/usr/local/lib/scm/" +@end example @end defun @defun program-vicinity @@ -567,6 +766,12 @@ returns @code{#f}. @c systems this is @samp{.scm}. @c @end defun +@defun vicinity:suffix? chr +Returns the @samp{#t} if @var{chr} is a vicinity suffix character; and +@code{#f} otherwise. Typical vicinity suffixes are @samp{/}, +@samp{:}, and @samp{\}, +@end defun + @defun in-vicinity vicinity filename Returns a filename suitable for use by @code{slib:load}, @code{slib:load-source}, @code{slib:load-compiled}, @@ -590,8 +795,8 @@ return a pathname of the subdirectory @var{name} of -@node Configuration, Input/Output, Vicinity, Built-in Support -@subsection Configuration +@node Configuration, Input/Output, Vicinity, Universal SLIB Procedures +@section Configuration @noindent These constants and procedures describe characteristics of the Scheme @@ -652,8 +857,8 @@ Writes the report to file @file{filename}. (slib:report) @result{} slib "@value{SLIBVERSION}" on scm "5b1" on unix -(implementation-vicinity) is "/home/jaffer/scm/" -(library-vicinity) is "/home/jaffer/slib/" +(implementation-vicinity) is "/usr/local/lib/scm/" +(library-vicinity) is "/usr/local/lib/slib/" (scheme-file-suffix) is ".scm" loaded *features* : trace alist qp sort @@ -663,7 +868,7 @@ implementation *features* : bignum complex real rational inexact vicinity ed getenv tmpnam abort transcript with-file - ieee-p1178 rev4-report rev4-optional-procedures hash + ieee-p1178 r4rs rev4-optional-procedures hash object-hash delay eval dynamic-wind multiarg-apply multiarg/and- logical defmacro string-port source current-time record @@ -671,127 +876,134 @@ implementation *features* : array dump char-ready? full-continuation system implementation *catalog* : - (i/o-extensions compiled "/home/jaffer/scm/ioext.so") + (i/o-extensions compiled "/usr/local/lib/scm/ioext.so") ... @end example @end defun -@node Input/Output, Legacy, Configuration, Built-in Support -@subsection Input/Output +@node Input/Output, System, Configuration, Universal SLIB Procedures +@section Input/Output @noindent These procedures are provided by all implementations. -@deffn Procedure file-exists? filename +@defun file-exists? filename Returns @code{#t} if the specified file exists. Otherwise, returns @code{#f}. If the underlying implementation does not support this feature then @code{#f} is always returned. -@end deffn +@end defun -@deffn Procedure delete-file filename +@defun delete-file filename Deletes the file specified by @var{filename}. If @var{filename} can not be deleted, @code{#f} is returned. Otherwise, @code{#t} is returned. +@end defun + +@defun open-file filename modes +@var{filename} should be a string naming a file. @code{open-file} +returns a port depending on the symbol @var{modes}: + +@table @r +@item r +an input port capable of delivering characters from the file. +@item rb +a @emph{binary} input port capable of delivering characters from the file. +@item w +an output port capable of writing characters to a new file by that name. +@item wb +a @emph{binary} output port capable of writing characters to a new file +by that name. +@end table + +If an implementation does not distinguish between binary and non-binary +files, then it must treat @r{rb} as @r{r} and @r{wb} as @r{w}. + +If the file cannot be opened, either #f is returned or an error is +signalled. For output, if a file with the given name already exists, +the effect is unspecified. +@end defun + +@defun port? obj +Returns @t{#t} if @var{obj} is an input or output port, otherwise +returns @t{#f}. +@end defun + +@deffn {Procedure} close-port port +Closes the file associated with @var{port}, rendering the @var{port} +incapable of delivering or accepting characters. + +@code{close-file} has no effect if the file has already been closed. +The value returned is unspecified. @end deffn -@deffn Procedure tmpnam +@defun call-with-open-ports proc ports @dots{} +@defunx call-with-open-ports ports @dots{} proc +@var{Proc} should be a procedure that accepts as many arguments as there +are @var{ports} passed to @code{call-with-open-ports}. +@code{call-with-open-ports} calls @var{proc} with @var{ports} @dots{}. +If @var{proc} returns, then the ports are closed automatically and the +value yielded by the @var{proc} is returned. If @var{proc} does not +return, then the ports will not be closed automatically unless it is +possible to prove that the ports will never again be used for a read or +write operation. +@end defun + +@defun tmpnam Returns a pathname for a file which will likely not be used by any other process. Successive calls to @code{(tmpnam)} will return different pathnames. -@end deffn +@end defun -@deffn Procedure current-error-port +@defun current-error-port Returns the current port to which diagnostic and error output is directed. -@end deffn +@end defun -@deffn Procedure force-output -@deffnx Procedure force-output port +@deffn {Procedure} force-output +@deffnx {Procedure} force-output port Forces any pending output on @var{port} to be delivered to the output device and returns an unspecified value. The @var{port} argument may be omitted, in which case it defaults to the value returned by @code{(current-output-port)}. @end deffn -@deffn Procedure output-port-width -@deffnx Procedure output-port-width port +@defun output-port-width +@defunx output-port-width port Returns the width of @var{port}, which defaults to @code{(current-output-port)} if absent. If the width cannot be determined 79 is returned. -@end deffn +@end defun -@deffn Procedure output-port-height -@deffnx Procedure output-port-height port +@defun output-port-height +@defunx output-port-height port Returns the height of @var{port}, which defaults to @code{(current-output-port)} if absent. If the height cannot be determined 24 is returned. -@end deffn - -@node Legacy, System, Input/Output, Built-in Support -@subsection Legacy - -These procedures are provided by all implementations. +@end defun -@defun identity x -@var{identity} returns its argument. -Example: -@lisp -(identity 3) - @result{} 3 -(identity '(foo bar)) - @result{} (foo bar) -(map identity @var{lst}) - @equiv{} (copy-list @var{lst}) -@end lisp -@end defun +@node System, Miscellany, Input/Output, Universal SLIB Procedures +@section System @noindent -The following procedures were present in Scheme until R4RS -(@pxref{Notes, , Language changes ,r4rs, Revised(4) Scheme}). -They are provided by all SLIB implementations. +These procedures are provided by all implementations. -@defvr Constant t -Derfined as @code{#t}. -@end defvr +@deffn {Procedure} slib:load-source name +Loads a file of Scheme source code from @var{name} with the default +filename extension used in SLIB. For instance if the filename extension +used in SLIB is @file{.scm} then @code{(slib:load-source "foo")} will +load from file @file{foo.scm}. +@end deffn -@defvr Constant nil -Defined as @code{#f}. -@end defvr +@deffn {Procedure} slib:load-compiled name +On implementations which support separtely loadable compiled modules, +loads a file of compiled code from @var{name} with the implementation's +filename extension for compiled code appended. +@end deffn -@defun last-pair l -Returns the last pair in the list @var{l}. Example: -@lisp -(last-pair (cons 1 2)) - @result{} (1 . 2) -(last-pair '(1 2)) - @result{} (2) - @equiv{} (cons 2 '()) -@end lisp -@end defun - -@node System, , Legacy, Built-in Support -@subsection System - -@noindent -These procedures are provided by all implementations. - -@deffn Procedure slib:load-source name -Loads a file of Scheme source code from @var{name} with the default -filename extension used in SLIB. For instance if the filename extension -used in SLIB is @file{.scm} then @code{(slib:load-source "foo")} will -load from file @file{foo.scm}. -@end deffn - -@deffn Procedure slib:load-compiled name -On implementations which support separtely loadable compiled modules, -loads a file of compiled code from @var{name} with the implementation's -filename extension for compiled code appended. -@end deffn - -@deffn Procedure slib:load name +@deffn {Procedure} slib:load name Loads a file of Scheme source or compiled code from @var{name} with the appropriate suffixes appended. If both source and compiled code are present with the appropriate names then the implementation will load @@ -802,13 +1014,13 @@ If an implementation does not support compiled code then @code{slib:load} will be identical to @code{slib:load-source}. @end deffn -@deffn Procedure slib:eval obj +@deffn {Procedure} slib:eval obj @code{eval} returns the value of @var{obj} evaluated in the current top level environment. @ref{Eval} provides a more general evaluation facility. @end deffn -@deffn Procedure slib:eval-load filename eval +@deffn {Procedure} slib:eval-load filename eval @var{filename} should be a string. If filename names an existing file, the Scheme source code expressions and definitions are read from the file and @var{eval} called with them sequentially. The @@ -816,19 +1028,19 @@ file and @var{eval} called with them sequentially. The @code{current-input-port} and @code{current-output-port}. @end deffn -@deffn Procedure slib:warn arg1 arg2 @dots{} +@deffn {Procedure} slib:warn arg1 arg2 @dots{} Outputs a warning message containing the arguments. @end deffn -@deffn Procedure slib:error arg1 arg2 @dots{} +@deffn {Procedure} slib:error arg1 arg2 @dots{} 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. @end deffn -@deffn Procedure slib:exit n -@deffnx Procedure slib:exit +@deffn {Procedure} slib:exit n +@deffnx {Procedure} slib:exit Exits from the Scheme session returning status @var{n} to the system. If @var{n} is omitted or @code{#t}, a success status is returned to the system (if possible). If @var{n} is @code{#f} a failure is returned to @@ -837,29 +1049,108 @@ returned to the system (if possible). If the Scheme session cannot exit an unspecified value is returned from @code{slib:exit}. @end deffn +@defun browse-url url +Web browsers have become so ubiquitous that programming languagues +should support a uniform interface to them. -@node About this manual, , Built-in Support, The Library System -@section About this manual +If a @samp{netscape} browser is running, @code{browse-url} causes the +browser to display the page specified by string @var{url} and returns +#t. -@itemize @bullet -@item -Entries that are labeled as Functions are called for their return -values. Entries that are labeled as Procedures are called primarily for -their side effects. +If the browser is not running, @code{browse-url} starts a browser +displaying the argument @var{url}. If the browser starts as a +background job, @code{browse-url} returns #t immediately; if the +browser starts as a foreground job, then @code{browse-url} returns #t +when the browser exits; otherwise it returns #f. +@end defun -@item -Examples in this text were produced using the @code{scm} Scheme -implementation. -@item -At the beginning of each section, there is a line that looks like -@ftindex feature -@code{(require 'feature)}. Include this line in your code prior to -using the package. -@end itemize +@node Miscellany, , System, Universal SLIB Procedures +@section Miscellany + +These procedures are provided by all implementations. + +@defun identity x +@var{identity} returns its argument. + +Example: +@lisp +(identity 3) + @result{} 3 +(identity '(foo bar)) + @result{} (foo bar) +(map identity @var{lst}) + @equiv{} (copy-list @var{lst}) +@end lisp +@end defun + +@subsection Mutual Exclusion + +@noindent +An @dfn{exchanger} is a procedure of one argument regulating mutually +@cindex exchanger +exclusive access to a resource. When a exchanger is called, its current +content is returned, while being replaced by its argument in an atomic +operation. + +@defun make-exchanger obj + +Returns a new exchanger with the argument @var{obj} as its initial +content. + +@example +(define queue (make-exchanger (list a))) +@end example + +A queue implemented as an exchanger holding a list can be protected from +reentrant execution thus: + +@example +(define (pop queue) + (let ((lst #f)) + (dynamic-wind + (lambda () (set! lst (queue #f))) + (lambda () (and lst (not (null? lst)) + (let ((ret (car lst))) + (set! lst (cdr lst)) + ret))) + (lambda () (and lst (queue lst)))))) + +(pop queue) @result{} a + +(pop queue) @result{} #f +@end example +@end defun + + +@subsection Legacy + +@noindent +The following procedures were present in Scheme until R4RS +(@pxref{Notes, , Language changes ,r4rs, Revised(4) Scheme}). +They are provided by all SLIB implementations. + +@defvr Constant t +Derfined as @code{#t}. +@end defvr + +@defvr Constant nil +Defined as @code{#f}. +@end defvr + +@defun last-pair l +Returns the last pair in the list @var{l}. Example: +@lisp +(last-pair (cons 1 2)) + @result{} (1 . 2) +(last-pair '(1 2)) + @result{} (2) + @equiv{} (cons 2 '()) +@end lisp +@end defun -@node Scheme Syntax Extension Packages, Textual Conversion Packages, The Library System, Top +@node Scheme Syntax Extension Packages, Textual Conversion Packages, Universal SLIB Procedures, Top @chapter Scheme Syntax Extension Packages @menu @@ -962,7 +1253,7 @@ Takes an R4RS expression, macro-expands it, evals the result of the macro expansion, and returns the result of the evaluation. @end defun -@deffn Procedure macro:load filename +@deffn {Procedure} macro:load filename @var{filename} should be a string. If filename names an existing file, the @code{macro:load} procedure reads Scheme source code expressions and definitions from the file and evaluates them sequentially. These source @@ -1076,8 +1367,8 @@ Side effects of @var{expression} will affect the top level environment. @end defun -@deffn Procedure macro:load filename -@deffnx Procedure macwork:load filename +@deffn {Procedure} macro:load filename +@deffnx {Procedure} macwork:load filename @var{filename} should be a string. If filename names an existing file, the @code{macro:load} procedure reads Scheme source code expressions and definitions from the file and evaluates them sequentially. These source @@ -1266,8 +1557,8 @@ Side effects of @var{expression} will affect the top level environment. @end defun -@deffn Procedure macro:load filename -@deffnx Procedure synclo:load filename +@deffn {Procedure} macro:load filename +@deffnx {Procedure} synclo:load filename @var{filename} should be a string. If filename names an existing file, the @code{macro:load} procedure reads Scheme source code expressions and definitions from the file and evaluates them sequentially. These @@ -1290,7 +1581,8 @@ report. The syntactic closures facility extends the BNF rule for @var{transformer spec} to allow a new keyword that introduces a -low-level macro transformer:@refill +low-level macro transformer: + @example @var{transformer spec} := (transformer @var{expression}) @end example @@ -1320,7 +1612,8 @@ closures facility. recursively constructed. A form is any expression, any definition, any syntactic keyword, or any syntactic closure. The variable name that appears in a @code{set!} special form is also a form. Examples of -forms:@refill +forms: + @lisp 17 #t @@ -1387,7 +1680,8 @@ the transformer, the @dfn{output form}, is automatically closed in the which the @code{transformer} expression occurred. For example, here is a definition of a push macro using -@code{syntax-rules}:@refill +@code{syntax-rules}: + @lisp (define-syntax push (syntax-rules () @@ -1416,7 +1710,8 @@ Some macros may be non-hygienic by design. For example, the following defines a loop macro that implicitly binds @code{exit} to an escape procedure. The binding of @code{exit} is intended to capture free references to @code{exit} in the body of the loop, so @code{exit} must -be left free when the body is closed:@refill +be left free when the body is closed: + @lisp (define-syntax loop (transformer @@ -1483,7 +1778,8 @@ transformed, call @var{procedure} on the current syntactic environment. in that same syntactic environment, in place of the form. An example will make this clear. Suppose we wanted to define a simple -@code{loop-until} keyword equivalent to@refill +@code{loop-until} keyword equivalent to + @lisp (define-syntax loop-until (syntax-rules () @@ -1530,7 +1826,8 @@ exposed to is the one just outside the @code{lambda} expression: before the user's identifier is added to the syntactic environment, but after the identifier loop has been added. @code{capture-syntactic-environment} captures exactly that environment -as follows:@refill +as follows: + @lisp (define-syntax loop-until (transformer @@ -1562,7 +1859,8 @@ and the @code{loop} and use them in the body of the @code{lambda}. A common use of @code{capture-syntactic-environment} is to get the -transformer environment of a macro transformer:@refill +transformer environment of a macro transformer: + @lisp (transformer (lambda (exp env) @@ -1582,7 +1880,8 @@ high-level @code{syntax-rules} facility. As discussed earlier, an identifier is either a symbol or an @dfn{alias}. An alias is implemented as a syntactic closure whose -@dfn{form} is an identifier:@refill +@dfn{form} is an identifier: + @lisp (make-syntactic-closure env '() 'a) @result{} an @dfn{alias} @@ -1607,7 +1906,8 @@ know the syntactic roles of the substituted input subforms. @defun identifier? object Returns @code{#t} if @var{object} is an identifier, otherwise returns -@code{#f}. Examples:@refill +@code{#f}. Examples: + @lisp (identifier? 'a) @result{} #t @@ -1647,7 +1947,7 @@ environments, and @var{identifier1} and @var{identifier2} must be identifiers. @code{identifier=?} returns @code{#t} if the meaning of @var{identifier1} in @var{environment1} is the same as that of @var{identifier2} in @var{environment2}, otherwise it returns @code{#f}. -Examples:@refill +Examples: @lisp (let-syntax @@ -1711,8 +2011,8 @@ Side effects of @var{expression} will affect the top level environment. @end defun -@deffn Procedure macro:load filename -@deffnx Procedure syncase:load filename +@deffn {Procedure} macro:load filename +@deffnx {Procedure} syncase:load filename @var{filename} should be a string. If filename names an existing file, the @code{macro:load} procedure reads Scheme source code expressions and definitions from the file and evaluates them sequentially. These @@ -1773,6 +2073,7 @@ To check operation of syntax-case get @lisp (require 'syntax-case) @ftindex syntax-case +@findex syncase:sanity-check (syncase:sanity-check) @end lisp @@ -1810,11 +2111,16 @@ 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). -@subsection Note from maintainer +@subsection Note from SLIB maintainer + +@code{(require 'structure)} +@findex define-structure Included with the @code{syntax-case} files was @file{structure.scm} -which defines a macro @code{define-structure}. There is no -documentation for this macro and it is not used by any code in SLIB. +which defines a macro @code{define-structure}. I have no +documentation for this macro; it is not used by any other code in +SLIB. + @node Fluid-Let, Yasos, Syntax-Case Macros, Scheme Syntax Extension Packages @section Fluid-Let @@ -1952,7 +2258,7 @@ Used in an operation definition (of @var{self}) to invoke the identity. Also known as ``send-to-super''. @end deffn -@deffn Procedure print obj port +@deffn {Procedure} print obj port A default @code{print} operation is provided which is just @code{(format @var{port} @var{obj})} (@pxref{Format}) for non-instances and prints @var{obj} preceded by @samp{#} for instances. @@ -2014,14 +2320,14 @@ foo @result{} "foo" @end example @end deffn -@deffn Procedure add-setter getter setter +@deffn {Procedure} add-setter getter setter Add procedures @var{getter} and @var{setter} to the (inaccessible) list of valid setter/getter pairs. @var{setter} implements the store operation corresponding to the @var{getter} access operation for the relevant state. The return value is unspecified. @end deffn -@deffn Procedure remove-setter-for getter +@deffn {Procedure} remove-setter-for getter Removes the setter corresponding to the specified @var{getter} from the list of valid setters. The return value is unspecified. @end deffn @@ -2058,7 +2364,7 @@ value is unspecified. ((string? obj) (string-length obj)) ((char? obj) 1) (else - (error "Operation not supported: size" obj)))) + (slib:error "Operation not supported: size" obj)))) (define-predicate cell?) (define-operation (fetch obj)) @@ -2144,13 +2450,14 @@ value is unspecified. * Format:: Common-Lisp Format * Standard Formatted I/O:: Posix printf and scanf * Programs and Arguments:: -* HTML:: +* HTML:: Generating * HTML Tables:: Databases meet HTML * HTTP and CGI:: Serve WWW sites +* Parsing HTML:: 'html-for-each * URI:: Uniform Resource Identifier * Printing Scheme:: Nicely * Time and Date:: -* Vector Graphics:: +* NCBI-DNA:: DNA and protein sequences * Schmooz:: Documentation markup for Scheme programs @end menu @@ -2179,13 +2486,14 @@ procedures for higher level specification of rulesets. @menu * Precedence Parsing Overview:: +* Rule Types:: * Ruleset Definition and Use:: * Token definition:: * Nud and Led Definition:: * Grammar Rule Definition:: @end menu -@node Precedence Parsing Overview, Ruleset Definition and Use, Precedence Parsing, Precedence Parsing +@node Precedence Parsing Overview, Rule Types, Precedence Parsing, Precedence Parsing @subsection Precedence Parsing Overview @noindent @@ -2209,6 +2517,35 @@ parsed when the error occured; The symbol @code{?} is substituted for missing input. @end itemize +@noindent +@cindex binding power +The notion of @dfn{binding power} may be unfamiliar to those +accustomed to BNF grammars. + +@noindent +When two consecutive objects are parsed, the first might be the prefix +to the second, or the second might be a suffix of the first. +Comparing the left and right binding powers of the two objects decides +which way to interpret them. + +@noindent +Objects at each level of syntactic grouping have binding powers. + +@noindent +@cindex syntax tree +A syntax tree is not built unless the rules explicitly do so. The +call graph of grammar rules effectively instantiate the sytnax tree. + +@noindent +The JACAL symbolic math system +(@url{http://swissnet.ai.mit.edu/~jaffer/JACAL.html}) uses +@t{precedence-parse}. Its grammar definitions in the file +@file{jacal/English.scm} can serve as examples of use. + + +@node Rule Types, Ruleset Definition and Use, Precedence Parsing Overview, Precedence Parsing +@subsection Rule Types + @noindent Here are the higher-level syntax types and an example of each. Precedence considerations are omitted for clarity. See @ref{Grammar @@ -2279,7 +2616,7 @@ delimits the extent of the restfix operator @code{set}. @end deftp -@node Ruleset Definition and Use, Token definition, Precedence Parsing Overview, Precedence Parsing +@node Ruleset Definition and Use, Token definition, Rule Types, Precedence Parsing @subsection Ruleset Definition and Use @defvar *syn-defs* @@ -2405,6 +2742,16 @@ Is the string consisting of all characters between 0 and 255 for which @code{char-whitespace?} returns true. @end defvr +@noindent +For the purpose of reporting problems in error messages, this package +keeps track of the @dfn{current column}. When the column does not +simply track input characters, @code{tok:bump-column} can be used to +adjust the current-column. + +@defun tok:bump-column pos port +Adds @var{pos} to the current-column for input-port @var{port}. +@end defun + @node Nud and Led Definition, Grammar Rule Definition, Token definition, Precedence Parsing @subsection Nud and Led Definition @@ -2689,10 +3036,17 @@ The ruleset in effect before @var{tk} was parsed is restored; @node Format, Standard Formatted I/O, Precedence Parsing, Textual Conversion Packages @section Format (version 3.0) -@code{(require 'format)} +@ifset html + +@end ifset +@c @code{(require 'format)} @ftindex format -@include fmtdoc.txi +@c @include fmtdoc.txi +The @file{format.scm} package was removed because it was not +reentrant. @url{http://swissnet.ai.mit.edu/~jaffer/SLIB.FAQ} explains +more about FORMAT's woes. + @node Standard Formatted I/O, Programs and Arguments, Format, Textual Conversion Packages @section Standard Formatted I/O @@ -2724,14 +3078,17 @@ Defined to be @code{(current-error-port)}. @node Standard Formatted Output, Standard Formatted Input, Standard Formatted I/O, Standard Formatted I/O @subsection Standard Formatted Output +@ifset html + +@end ifset @code{(require 'printf)} @ftindex printf -@deffn Procedure printf format arg1 @dots{} -@deffnx Procedure fprintf port format arg1 @dots{} -@deffnx Procedure sprintf str format arg1 @dots{} -@deffnx Procedure sprintf #f format arg1 @dots{} -@deffnx Procedure sprintf k format arg1 @dots{} +@deffn {Procedure} printf format arg1 @dots{} +@deffnx {Procedure} fprintf port format arg1 @dots{} +@deffnx {Procedure} sprintf str format arg1 @dots{} +@deffnx {Procedure} sprintf #f format arg1 @dots{} +@deffnx {Procedure} sprintf k format arg1 @dots{} Each function converts, formats, and outputs its @var{arg1} @dots{} arguments according to the control string @var{format} argument and @@ -3148,32 +3505,51 @@ This routine implements Posix command line argument parsing. Notice that returning values through global variables means that @code{getopt} is @emph{not} reentrant. +Obedience to Posix format for the @code{getopt} calls sows confusion. +Passing @var{argc} and @var{argv} as arguments while referencing +@var{optind} as a global variable leads to strange behavior, +especially when the calls to @code{getopt} are buried in other +procedures. + +Even in C, @var{argc} can be derived from @var{argv}; what purpose +does it serve beyond providing an opportunity for +@var{argv}/@var{argc} mismatch? Just such a mismatch existed for +years in a SLIB @code{getopt--} example. + +I have removed the @var{argc} and @var{argv} arguments to getopt +procedures; and replaced them with a global variable: + +@defvar *argv* +Define @var{*argv*} with a list of arguments before calling getopt +procedures. If you don't want the first (0th) element to be ignored, +set @var{*optind*} to 0 (after requiring getopt). +@end defvar + @defvar *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, @var{*opting*} must be reset. +one, @var{*optind*} must be reset. @end defvar @defvar *optarg* Is set by getopt to the (string) option-argument of the current option. @end defvar -@deffn Procedure getopt argc argv optstring -Returns the next option letter in @var{argv} (starting from +@defun getopt optstring +Returns the next option letter in @var{*argv*} (starting from @code{(vector-ref argv *optind*)}) that matches a letter in -@var{optstring}. @var{argv} is a vector or list of strings, the 0th of -which getopt usually ignores. @var{argc} is the argument count, usually -the length of @var{argv}. @var{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 @var{argv}. - -@var{*optind*} is the index of the next element of the @var{argv} vector +@var{optstring}. @var{*argv*} is a vector or list of strings, the 0th +of which getopt usually ignores. @var{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 @var{*argv*}. + +@var{*optind*} is the index of the next element of the @var{*argv*} vector to be processed. It is initialized to 1 by @file{getopt.scm}, and @code{getopt} updates it when it finishes with each element of -@var{argv}. +@var{*argv*}. -@code{getopt} returns the next option character from @var{argv} that +@code{getopt} returns the next option character from @var{*argv*} that matches a character in @var{optstring}, if there is one that matches. If the option takes an argument, @code{getopt} sets the variable @var{*optarg*} to the option-argument as follows: @@ -3181,15 +3557,15 @@ If the option takes an argument, @code{getopt} sets the variable @itemize @bullet @item If the option was the last character in the string pointed to by an -element of @var{argv}, then @var{*optarg*} contains the next element of -@var{argv}, and @var{*optind*} is incremented by 2. If the resulting -value of @var{*optind*} is greater than or equal to @var{argc}, this -indicates a missing option argument, and @code{getopt} returns an error -indication. +element of @var{*argv*}, then @var{*optarg*} contains the next element +of @var{*argv*}, and @var{*optind*} is incremented by 2. If the +resulting value of @var{*optind*} is greater than or equal to +@code{(length @var{*argv*})}, this indicates a missing option +argument, and @code{getopt} returns an error indication. @item Otherwise, @var{*optarg*} is set to the string following the option -character in that element of @var{argv}, and @var{*optind*} is +character in that element of @var{*argv*}, and @var{*optind*} is incremented by 1. @end itemize @@ -3251,11 +3627,11 @@ Example: (slib:exit) @end lisp -@end deffn +@end defun -@subsection Getopt-- +@subsection Getopt--- -@defun getopt-- argc argv optstring +@defun @code{getopt--} optstring The procedure @code{getopt--} is an extended version of @code{getopt} which parses @dfn{long option names} of the form @samp{--hold-the-onions} and @samp{--verbosity-level=extreme}. @@ -3269,22 +3645,21 @@ not returned as part of the option string. No information is passed to @code{getopt--} concerning which long options should be accepted or whether such options can take arguments. -If a long option did not have an argument, @code{*optarg} will be set to -@code{#f}. The caller is responsible for detecting and reporting +If a long option did not have an argument, @code{*optarg*} will be set +to @code{#f}. The caller is responsible for detecting and reporting errors. @example (define opts ":-:b:") -(define argc 5) -(define argv '("foo" "-b9" "--f1" "--2=" "--g3=35234.342" "--")) +(define *argv* '("foo" "-b9" "--f1" "--2=" "--g3=35234.342" "--")) (define *optind* 1) (define *optarg* #f) (require 'qp) @ftindex qp (do ((i 5 (+ -1 i))) ((zero? i)) - (define opt (getopt-- argc argv opts)) - (print *optind* opt *optarg*))) + (let ((opt (getopt-- opts))) + (print *optind* opt *optarg*))) @print{} 2 #\b "9" 3 "f1" #f @@ -3297,81 +3672,7 @@ errors. @node Command Line, Parameter lists, Getopt, Programs and Arguments @subsection Command Line -@code{(require 'read-command)} -@ftindex read-command - -@defun read-command port -@defunx read-command -@code{read-command} converts a @dfn{command line} into a list of strings -@cindex command line -suitable for parsing by @code{getopt}. The syntax of command lines -supported resembles that of popular @dfn{shell}s. @code{read-command} -updates @var{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 @var{port} argument may be omitted, in which case it defaults to the -value returned by @code{current-input-port}. - -The fields into which the command line is split are delimited by -whitespace as defined by @code{char-whitespace?}. The end of a command -is delimited by end-of-file or unescaped semicolon (@key{;}) or -@key{newline}. Any character can be literally included in a field by -escaping it with a backslach (@key{\}). - -The initial character and types of fields recognized are: -@table @asis -@item @samp{\} -The next character has is taken literally and not interpreted as a field -delimiter. If @key{\} is the last character before a @key{newline}, -that @key{newline} is just ignored. Processing continues from the -characters after the @key{newline} as though the backslash and -@key{newline} were not there. -@item @samp{"} -The characters up to the next unescaped @key{"} are taken literally, -according to [R4RS] rules for literal strings (@pxref{Strings, , ,r4rs, -Revised(4) Scheme}). -@item @samp{(}, @samp{%'} -One scheme expression is @code{read} starting with this character. The -@code{read} expression is evaluated, converted to a string -(using @code{display}), and replaces the expression in the returned -field. -@item @samp{;} -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. -@end table - -@noindent -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. @key{#} can be part of fields if these conditions are -not met. For instance, @code{ab#c} is just the field ab#c. - -@table @samp -@item # -Introduces a comment. The comment continues to the end of the line on -which the semicolon appears. Comments are treated as whitespace by -@code{read-dommand-line} and backslashes before @key{newline}s in -comments are also ignored. -@end table -@end defun - -@defun read-options-file filename -@code{read-options-file} converts an @dfn{options file} into a list of -@cindex options file -strings suitable for parsing by @code{getopt}. The syntax of options -files is the same as the syntax for command -lines, except that @key{newline}s do not terminate reading (only @key{;} -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. -@end defun - +@include comparse.txi @node Parameter lists, Getopt Parameter lists, Command Line, Programs and Arguments @@ -3418,11 +3719,11 @@ If there are more than one @var{parameter-name} parameters, an error is signaled. @end deffn -@deffn Procedure adjoin-parameters! parameter-list parameter1 @dots{} +@deffn {Procedure} adjoin-parameters! parameter-list parameter1 @dots{} Returns @var{parameter-list} with @var{parameter1} @dots{} merged in. @end deffn -@deffn Procedure parameter-list-expand expanders parameter-list +@deffn {Procedure} parameter-list-expand expanders parameter-list @var{expanders} is a list of procedures whose order matches the order of the @var{parameter-name}s in the call to @code{make-parameter-list} which created @var{parameter-list}. For each non-false element of @@ -3487,171 +3788,13 @@ should appear. @node Getopt Parameter lists, Filenames, Parameter lists, Programs and Arguments @subsection Getopt Parameter lists -@code{(require 'getopt-parameters)} - -@deffn Function getopt->parameter-list argc argv optnames arities types aliases desc @dots{} -Returns @var{argv} converted to a parameter-list. @var{optnames} are -the parameter-names. @var{arities} and @var{types} are lists of symbols -corresponding to @var{optnames}. - -@var{aliases} is a list of lists of strings or integers paired with -elements of @var{optnames}. Each one-character string will be treated -as a single @samp{-} option by @code{getopt}. Longer strings will be -treated as long-named options (@pxref{Getopt, getopt--}). - -If the @var{aliases} association list has only strings as its -@code{car}s, then all the option-arguments after an option (and before -the next option) are adjoined to that option. - -If the @var{aliases} association list has integers, then each (string) -option will take at most one option-argument. Unoptioned arguments are -collected in a list. A @samp{-1} alias will take the last argument in -this list; @samp{+1} will take the first argument in the list. The -aliases -2 then +2; -3 then +3; @dots{} are tried so long as a positive -or negative consecutive alias is found and arguments remain in the list. -Finally a @samp{0} alias, if found, absorbs any remaining arguments. - -In all cases, if unclaimed arguments remain after processing, a warning -is signaled and #f is returned. -@end deffn - -@deffn Function getopt->arglist argc argv optnames positions arities types defaulters checks aliases desc @dots{} -Like @code{getopt->parameter-list}, but converts @var{argv} to an -argument-list as specified by @var{optnames}, @var{positions}, -@var{arities}, @var{types}, @var{defaulters}, @var{checks}, and -@var{aliases}. If the options supplied violate the @var{arities} or -@var{checks} constraints, then a warning is signaled and #f is returned. -@end deffn - -@noindent -These @code{getopt} functions can be used with SLIB relational -databases. For an example, @xref{Database Utilities, -make-command-server}. - -@noindent -If errors are encountered while processing options, directions for using -the options (and argument strings @var{desc} @dots{}) are printed to -@code{current-error-port}. - -@example -(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)))) -@print{} -Usage: cmd [OPTION ARGUMENT ...] ... - - -f, --flag - -o, --optional= - -n, --nary= ... - -N, --nary1= ... - -s, --single= - --Flag - -B - -a ... - --Abs= ... - -ERROR: getopt->parameter-list "unrecognized option" "-?" -@end example +@include getparam.txi @node Filenames, Batch, Getopt Parameter lists, Programs and Arguments @subsection Filenames -@code{(require 'filename)} or @code{(require 'glob)} - -@defun filename:match?? pattern -@defunx filename:match-ci?? pattern -Returns a predicate which returns a non-false value if its string argument -matches (the string) @var{pattern}, false otherwise. Filename matching -is like -@cindex glob -@dfn{glob} expansion described the bash manpage, except that names -beginning with @samp{.} are matched and @samp{/} characters are not -treated specially. - -These functions interpret the following characters specially in -@var{pattern} strings: -@table @samp -@item * -Matches any string, including the null string. -@item ? -Matches any single character. -@item [@dots{}] -Matches any one of the enclosed characters. A pair of characters -separated by a minus sign (-) denotes a range; any character lexically -between those two characters, inclusive, is matched. If the first -character following the @samp{[} is a @samp{!} or a @samp{^} then any -character not enclosed is matched. A @samp{-} or @samp{]} may be -matched by including it as the first or last character in the set. -@end table - -@example -@end example -@end defun - -@defun filename:substitute?? pattern template -@defunx filename:substitute-ci?? pattern template -Returns a function transforming a single string argument according to -glob patterns @var{pattern} and @var{template}. @var{pattern} and -@var{template} must have the same number of wildcard specifications, -which need not be identical. @var{pattern} and @var{template} may have -a different number of literal sections. If an argument to the function -matches @var{pattern} in the sense of @code{filename:match??} then it -returns a copy of @var{template} in which each wildcard specification is -replaced by the part of the argument matched by the corresponding -wildcard specification in @var{pattern}. A @code{*} wildcard matches -the longest leftmost string possible. If the argument does not match -@var{pattern} then false is returned. - -@var{template} may be a function accepting the same number of string -arguments as there are wildcard specifications in @var{pattern}. In -the case of a match the result of applying @var{template} to a list -of the substrings matched by wildcard specifications will be returned, -otherwise @var{template} will not be called and @code{#f} will be returned. - -@example -((filename:substitute?? "scm_[0-9]*.html" "scm5c4_??.htm") - "scm_10.html") -@result{} "scm5c4_10.htm" -((filename:substitute?? "??" "beg?mid?end") "AZ") -@result{} "begAmidZend" -((filename:substitute?? "*na*" "?NA?") "banana") -@result{} "banaNA" -((filename:substitute?? "?*?" (lambda (s1 s2 s3) (string-append s3 s1))) "ABZ") -@result{} "ZA" -@end example -@end defun - -@defun replace-suffix str old new -@var{str} can be a string or a list of strings. Returns a new string -(or strings) similar to @code{str} but with the suffix string @var{old} -removed and the suffix string @var{new} appended. If the end of -@var{str} does not match @var{old}, an error is signaled. - -@example -(replace-suffix "/usr/local/lib/slib/batch.scm" ".scm" ".c") -@result{} "/usr/local/lib/slib/batch.c" -@end example -@end defun +@include glob.txi @node Batch, , Filenames, Programs and Arguments @@ -3680,7 +3823,7 @@ dos @item vms @item -amigados +amigaos @item system @item @@ -3689,8 +3832,8 @@ system @end table @noindent -@file{batch.scm} uses 2 enhanced relational tables (@pxref{Database -Utilities}) to store information linking the names of +@file{batch.scm} uses 2 enhanced relational tables +(@pxref{Using Databases}) to store information linking the names of @code{operating-system}s to @code{batch-dialect}es. @defun batch:initialize! database @@ -3699,9 +3842,9 @@ the domain @code{operating-system} to the enhanced relational database @var{database}. @end defun -@defvar batch:platform +@defvar *operating-system* Is batch's best guess as to which operating-system it is running under. -@code{batch:platform} is set to @code{(software-type)} +@code{*operating-system*} is set to @code{(software-type)} (@pxref{Configuration}) unless @code{(software-type)} is @code{unix}, in which case finer distinctions are made. @end defvar @@ -3835,8 +3978,8 @@ tables added to @var{database} by @code{batch:initialize!}. Here is an example of the use of most of batch's procedures: @example -(require 'database-utilities) -@ftindex database-utilities +(require 'databases) +@ftindex databases (require 'parameters) @ftindex parameters (require 'batch) @@ -3848,8 +3991,8 @@ Here is an example of the use of most of batch's procedures: (batch:initialize! batch) (define my-parameters - (list (list 'batch-dialect (os->batch-dialect batch:platform)) - (list 'platform batch:platform) + (list (list 'batch-dialect (os->batch-dialect *operating-system*)) + (list 'operating-system *operating-system*) (list 'batch-port (current-output-port)))) ;gets filled in later (batch:call-with-output-script @@ -3883,7 +4026,7 @@ Here is an example of the use of most of batch's procedures: Produces the file @file{my-batch}: @example -#!/bin/sh +#! /bin/sh # "my-batch" script created by SLIB/batch Sun Oct 31 18:24:10 1999 # ================ Write file with C program. mv -f hello.c hello.c~ @@ -3925,13 +4068,19 @@ hello world @include db2html.txi -@node HTTP and CGI, URI, HTML Tables, Textual Conversion Packages +@node HTTP and CGI, Parsing HTML, HTML Tables, Textual Conversion Packages @section HTTP and CGI @include http-cgi.txi -@node URI, Printing Scheme, HTTP and CGI, Textual Conversion Packages +@node Parsing HTML, URI, HTTP and CGI, Textual Conversion Packages +@section Parsing HTML + +@include html4each.txi + + +@node URI, Printing Scheme, Parsing HTML, Textual Conversion Packages @section URI @include uri.txi @@ -3960,7 +4109,7 @@ 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. -@deffn Procedure generic-write obj display? width output +@deffn {Procedure} generic-write obj display? width output @table @var @item obj Scheme data value to transform. @@ -4012,8 +4161,8 @@ where @code{(require 'pretty-print)} @ftindex pretty-print -@deffn Procedure pretty-print obj -@deffnx Procedure pretty-print obj port +@deffn {Procedure} pretty-print obj +@deffnx {Procedure} pretty-print obj port @code{pretty-print}s @var{obj} on @var{port}. If @var{port} is not specified, @code{current-output-port} is used. @@ -4032,8 +4181,8 @@ Example: @end example @end deffn -@deffn Procedure pretty-print->string obj -@deffnx Procedure pretty-print->string obj width +@deffn {Procedure} pretty-print->string obj +@deffnx {Procedure} pretty-print->string obj width Returns the string of @var{obj} @code{pretty-print}ed in @var{width} columns. If @var{width} is not specified, @code{(output-port-width)} is @@ -4083,8 +4232,8 @@ Example: @code{(require 'pprint-file)} @ftindex pprint-file -@deffn Procedure pprint-file infile -@deffnx Procedure pprint-file infile outfile +@deffn {Procedure} pprint-file infile +@deffnx {Procedure} pprint-file infile outfile Pretty-prints all the code in @var{infile}. If @var{outfile} is specified, the output goes to @var{outfile}, otherwise it goes to @code{(current-output-port)}. @@ -4119,7 +4268,7 @@ thus can reduce loading time. The following will write into (pprint-filter-file "code.scm" defmacro:expand* "exp-code.scm") @end lisp -@node Time and Date, Vector Graphics, Printing Scheme, Textual Conversion Packages +@node Time and Date, NCBI-DNA, Printing Scheme, Textual Conversion Packages @section Time and Date @menu @@ -4244,6 +4393,12 @@ made of any timezone at any calendar time. @end defun +@defun tz:std-offset tz +@var{tz} is a time-zone object. @code{tz:std-offset} returns the +number of seconds west of the Prime Meridian timezone @var{tz} is. + +@end defun + @noindent The rest of these procedures and variables are provided for POSIX compatability. Because of shared state they are not thread-safe. @@ -4422,87 +4577,19 @@ match the arguments to @code{encode-universal-time}. @end defun -@node Vector Graphics, Schmooz, Time and Date, Textual Conversion Packages -@section Vector Graphics - -@menu -* Tektronix Graphics Support:: -@end menu - -@node Tektronix Graphics Support, , Vector Graphics, Vector Graphics -@subsection Tektronix Graphics Support - -@emph{Note:} The Tektronix graphics support files need more work, and -are not complete. - -@subsubsection 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. - -@deffn Procedure tek40:init -@end deffn - -@deffn Procedure tek40:graphics -@end deffn - -@deffn Procedure tek40:text -@end deffn - -@deffn Procedure tek40:linetype linetype -@end deffn - -@deffn Procedure tek40:move x y -@end deffn - -@deffn Procedure tek40:draw x y -@end deffn - -@deffn Procedure tek40:put-text x y str -@end deffn - -@deffn Procedure tek40:reset -@end deffn - - -@subsubsection 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. - -@deffn Procedure tek41:init -@end deffn - -@deffn Procedure tek41:reset -@end deffn - -@deffn Procedure tek41:graphics -@end deffn - -@deffn Procedure tek41:move x y -@end deffn - -@deffn Procedure tek41:draw x y -@end deffn - -@deffn Procedure tek41:point x y number -@end deffn +@node NCBI-DNA, Schmooz, Time and Date, Textual Conversion Packages +@section NCBI-DNA -@deffn Procedure tek41:encode-x-y x y -@end deffn +@include ncbi-dna.txi -@deffn Procedure tek41:encode-int number -@end deffn -@node Schmooz, , Vector Graphics, Textual Conversion Packages +@node Schmooz, , NCBI-DNA, Textual Conversion Packages @section Schmooz @include schmooz.texi + + @node Mathematical Packages, Database Packages, Textual Conversion Packages, Top @chapter Mathematical Packages @@ -4512,12 +4599,14 @@ be mixed with regular text and ANSI or other terminal control sequences. * Prime Numbers:: 'factor * Random Numbers:: 'random * Fast Fourier Transform:: 'fft -* Cyclic Checksum:: 'make-crc -* Plotting:: 'charplot +* Cyclic Checksum:: 'crc +* Graphing:: +* Solid Modeling:: VRML97 +* Color:: * Root Finding:: 'root * Minimizing:: 'minimize * Commutative Rings:: 'commutative-ring -* Determinant:: 'determinant +* Matrix Algebra:: 'determinant @end menu @@ -4527,6 +4616,7 @@ be mixed with regular text and ANSI or other terminal control sequences. @code{(require 'logical)} @ftindex logical +@noindent The bit-twiddling functions are made available through the use of the @code{logical} package. @code{logical} is loaded by inserting @code{(require 'logical)} before the code that uses these @@ -4534,7 +4624,7 @@ The bit-twiddling functions are made available through the use of the functions. These functions behave as though operating on integers in two's-complement representation. -@subheading Bitwise Operations +@subsection Bitwise Operations @defun logand n1 n1 Returns the integer which is the bit-wise AND of the two integer @@ -4615,7 +4705,7 @@ Example: @end defun -@subheading Bit Within Word +@subsection Bit Within Word @defun logbit? index j @example @@ -4641,17 +4731,17 @@ Example: @end example @end defun -@subheading Fields of Bits +@subsection Fields of Bits + +@defun logical:ones n +Returns the smallest non-negative integer having @var{n} binary ones. +@end defun @defun bit-field n start end -@findex bit-extract Returns the integer composed of the @var{start} (inclusive) through @var{end} (exclusive) bits of @var{n}. The @var{start}th bit becomes the 0-th bit in the result. -This function was called @code{bit-extract} in previous versions of SLIB. -@refill - Example: @lisp (number->string (bit-field #b1101101010 0 4) 2) @@ -4676,9 +4766,9 @@ Example: @end example @end defun -@defun ash int count +@defun ash n count Returns an integer equivalent to -@code{(inexact->exact (floor (* @var{int} (expt 2 @var{count}))))}. +@code{(inexact->exact (floor (* @var{n} (expt 2 @var{count}))))}. Example: @lisp @@ -4715,89 +4805,121 @@ Example: @end lisp @end defun -@node Modular Arithmetic, Prime Numbers, Bit-Twiddling, Mathematical Packages -@section Modular Arithmetic +@subsection Bit order and Lamination -@code{(require 'modular)} -@ftindex modular +@defun logical:rotate k count len +Returns the low-order @var{len} bits of @var{k} cyclically permuted +@var{count} bits towards high-order. -@defun extended-euclid n1 n2 -Returns a list of 3 integers @code{(d x y)} such that d = gcd(@var{n1}, -@var{n2}) = @var{n1} * x + @var{n2} * y. +Example: +@lisp +(number->string (logical:rotate #b0100 3 4) 2) +@result{} "10" +(number->string (logical:rotate #b0100 -1 4) 2) +@result{} "10" +@end lisp @end defun -@defun symmetric:modulus n -Returns @code{(quotient (+ -1 n) -2)} for positive odd integer @var{n}. -@end defun +@defun bit-reverse k n +Returns the low-order @var{k} bits of @var{n} with the bit order +reversed. The low-order bit of @var{n} is the high order bit of the +returned value. -@defun modulus->integer modulus -Returns the non-negative integer characteristic of the ring formed when -@var{modulus} is used with @code{modular:} procedures. +@example +(number->string (bit-reverse 8 #xa7) 16) + @result{} "e5" +@end example @end defun -@defun modular:normalize modulus n -Returns the integer @code{(modulo @var{n} (modulus->integer -@var{modulus}))} in the representation specified by @var{modulus}. -@end defun +@defun integer->list k len +@defunx integer->list k +@code{integer->list} returns a list of @var{len} booleans corresponding +to each bit of the given integer. #t is coded for each 1; #f for 0. +The @var{len} argument defaults to @code{(integer-length @var{k})}. -@noindent -The rest of these functions assume normalized arguments; That is, the -arguments are constrained by the following table: +@defunx list->integer list +@code{list->integer} returns an integer formed from the booleans in the +list @var{list}, which must be a list of booleans. A 1 bit is coded for +each #t; a 0 bit for #f. -@noindent -For all of these functions, if the first argument (@var{modulus}) is: -@table @code -@item positive? -Work as before. The result is between 0 and @var{modulus}. +@code{integer->list} and @code{list->integer} are inverses so far as +@code{equal?} is concerned. +@end defun -@item zero? -The arguments are treated as integers. An integer is returned. +@defun booleans->integer bool1 @dots{} +Returns the integer coded by the @var{bool1} @dots{} arguments. +@end defun -@item negative? -The arguments and result are treated as members of the integers modulo -@code{(+ 1 (* -2 @var{modulus}))}, but with @dfn{symmetric} -representation; i.e. @code{(<= (- @var{modulus}) @var{n} -@var{modulus})}. -@end table +@defun bitwise:laminate k1 @dots{} +Returns an integer composed of the bits of @var{k1} @dots{} interlaced +in argument order. Given @var{k1}, @dots{} @var{kn}, the n low-order +bits of the returned value will be the lowest-order bit of each +argument. -@noindent -If all the arguments are fixnums the computation will use only fixnums. +@defunx bitwise:delaminate count k +Returns a list of @var{count} integers comprised of every @var{count}h +bit of the integer @var{k}. -@defun modular:invertable? modulus k -Returns @code{#t} if there exists an integer n such that @var{k} * n -@equiv{} 1 mod @var{modulus}, and @code{#f} otherwise. +For any non-negative integers @var{k} and @var{count}: +@example +(eqv? k (bitwise:laminate (bitwise:delaminate count k))) +@end example @end defun -@defun modular:invert modulus k2 -Returns an integer n such that 1 = (n * @var{k2}) mod @var{modulus}. If -@var{k2} has no inverse mod @var{modulus} an error is signaled. -@end defun -@defun modular:negate modulus k2 -Returns (@minus{}@var{k2}) mod @var{modulus}. -@end defun +@subsection Gray code -@defun modular:+ modulus k2 k3 -Returns (@var{k2} + @var{k3}) mod @var{modulus}. -@end defun +@cindex Gray code +@noindent +A @dfn{Gray code} is an ordering of non-negative integers in which +exactly one bit differs between each pair of successive elements. There +are multiple Gray codings. An n-bit Gray code corresponds to a +Hamiltonian cycle on an n-dimensional hypercube. -@defun modular:@minus{} modulus k2 k3 -Returns (@var{k2} @minus{} @var{k3}) mod @var{modulus}. -@end defun +@noindent +Gray codes find use communicating incrementally changing values between +asynchronous agents. De-laminated Gray codes comprise the coordinates +of Peano-Hilbert space-filling curves. -@defun modular:* modulus k2 k3 -Returns (@var{k2} * @var{k3}) mod @var{modulus}. -The Scheme code for @code{modular:*} with negative @var{modulus} is not -completed for fixnum-only implementations. +@defun integer->gray-code k +Converts @var{k} to a Gray code of the same @code{integer-length} as +@var{k}. + +@defunx gray-code->integer k +Converts the Gray code @var{k} to an integer of the same +@code{integer-length} as @var{k}. + +For any non-negative integer @var{k}, +@example +(eqv? k (gray-code->integer (integer->gray-code k))) +@end example @end defun -@defun modular:expt modulus k2 k3 -Returns (@var{k2} ^ @var{k3}) mod @var{modulus}. +@defun = k1 k2 +@defunx gray-code? k1 k2 +@defunx gray-code<=? k1 k2 +@defunx gray-code>=? k1 k2 +These procedures return #t if their Gray code arguments are +(respectively): equal, monotonically increasing, monotonically +decreasing, monotonically nondecreasing, or monotonically nonincreasing. + +For any non-negative integers @var{k1} and @var{k2}, the Gray code +predicate of @code{(integer->gray-code k1)} and +@code{(integer->gray-code k2)} will return the same value as the +corresponding predicate of @var{k1} and @var{k2}. @end defun + +@node Modular Arithmetic, Prime Numbers, Bit-Twiddling, Mathematical Packages +@section Modular Arithmetic + +@include modular.txi + + @node Prime Numbers, Random Numbers, Modular Arithmetic, Mathematical Packages @section Prime Numbers @@ -4811,9 +4933,6 @@ Returns (@var{k2} ^ @var{k3}) mod @var{modulus}. @node Random Numbers, Fast Fourier Transform, Prime Numbers, Mathematical Packages @section Random Numbers -@code{(require 'random)} -@ftindex random - @cindex RNG @cindex PRNG A pseudo-random number generator is only as good as the tests it passes. @@ -4822,18 +4941,25 @@ tests named @dfn{DIEHARD} (@url{http://stat.fsu.edu/~geo/diehard.html}). @file{diehard.c} has a bug which the patch @url{http://swissnet.ai.mit.edu/ftpdir/users/jaffer/diehard.c.pat} corrects. -SLIB's new PRNG generates 8 bits at a time. With the degenerate seed -@samp{0}, the numbers generated pass DIEHARD; but when bits are combined -from sequential bytes, tests fail. With the seed -@samp{http://swissnet.ai.mit.edu/~jaffer/SLIB.html}, all of those tests -pass. +SLIB's PRNG generates 8 bits at a time. With the degenerate seed +@samp{0}, the numbers generated pass DIEHARD; but when bits are +combined from sequential bytes, tests fail. With the seed +@samp{http://swissnet.ai.mit.edu/~jaffer/SLIB.html}, all of those +tests pass. + +@menu +* Exact Random Numbers:: 'random +* Inexact Random Numbers:: 'random-inexact +@end menu + +@node Exact Random Numbers, Inexact Random Numbers, Random Numbers, Random Numbers +@subsection Exact Random Numbers @include random.txi -If inexact numbers are supported by the Scheme implementation, -@file{randinex.scm} will be loaded as well. @file{randinex.scm} -contains procedures for generating inexact distributions. +@node Inexact Random Numbers, , Exact Random Numbers, Random Numbers +@subsection Inexact Random Numbers @include randinex.txi @@ -4841,2088 +4967,3162 @@ contains procedures for generating inexact distributions. @node Fast Fourier Transform, Cyclic Checksum, Random Numbers, Mathematical Packages @section Fast Fourier Transform -@code{(require 'fft)} -@ftindex fft +@include fft.txi -@defun fft array -@var{array} is an array of @code{(expt 2 n)} numbers. @code{fft} -returns an array of complex numbers comprising the @dfn{Discrete Fourier -Transform} of @var{array}. -@defunx fft-1 array -@code{fft-1} returns an array of complex numbers comprising the inverse -Discrete Fourier Transform of @var{array}. -@end defun +@node Cyclic Checksum, Graphing, Fast Fourier Transform, Mathematical Packages +@section Cyclic Checksum -@code{(fft-1 (fft @var{array}))} will return an array of values close to -@var{array}. +@code{(require 'crc)} +@ftindex crc +@noindent +Cyclic Redundancy Checks using Galois field GF(2) polynomial +arithmetic are used for error detection in many data transmission +and storage applications. -@example -(fft '#(1 0+i -1 0-i 1 0+i -1 0-i)) @result{} +@noindent +The generator polynomials for various CRC protocols are availble +from many sources. But the polynomial is just one of many +parameters which must match in order for a CRC implementation to +interoperate with existing systems: -#(0.0 0.0 0.0+628.0783185208527e-18i 0.0 - 0.0 0.0 8.0-628.0783185208527e-18i 0.0) +@itemize @bullet -(fft-1 '#(0 0 0 0 0 0 8 0)) @result{} +@item +the byte-order and bit-order of the data stream; -#(1.0 -61.23031769111886e-18+1.0i -1.0 61.23031769111886e-18-1.0i - 1.0 -61.23031769111886e-18+1.0i -1.0 61.23031769111886e-18-1.0i) -@end example +@item +whether the CRC or its inverse is being calculated; +@item +the initial CRC value; and -@node Cyclic Checksum, Plotting, Fast Fourier Transform, Mathematical Packages -@section Cyclic Checksum +@item +whether and where the CRC value is appended (inverted +or non-inverted) to the data stream. + +@end itemize + +@noindent +There is even some controversy over the polynomials themselves. + +@defvr Constant crc-32-polynomial +For CRC-32, http://www2.sis.pitt.edu/~jkabara/tele-2100/lect08.html +gives x^32+x^26+x^23+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x^1+1. + +But +http://www.cs.ncl.ac.uk/people/harry.whitfield/home.formal/CRCs.html, +http://duchon.umuc.edu/Web_Pages/duchon/99_f_cm435/ShiftRegister.htm, +http://spinroot.com/spin/Doc/Book91_PDF/ch3.pdf, +http://www.erg.abdn.ac.uk/users/gorry/course/dl-pages/crc.html, +http://www.rad.com/networks/1994/err_con/crc_most.htm, and +http://www.gpfn.sk.ca/~rhg/csc8550s02/crc.html, +http://www.nobugconsulting.ro/crc.php give +x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1. + +SLIB @code{crc-32-polynomial} uses the latter definition. +@end defvr + +@defvr Constant crc-ccitt-polynomial + +http://www.math.grin.edu/~rebelsky/Courses/CS364/2000S/Outlines/outline.12.html, +http://duchon.umuc.edu/Web_Pages/duchon/99_f_cm435/ShiftRegister.htm, +http://www.cs.ncl.ac.uk/people/harry.whitfield/home.formal/CRCs.html, +http://www2.sis.pitt.edu/~jkabara/tele-2100/lect08.html, and +http://www.gpfn.sk.ca/~rhg/csc8550s02/crc.html give +CRC-CCITT: x^16+x^12+x^5+1. +@end defvr + +@defvr Constant crc-16-polynomial + +http://www.math.grin.edu/~rebelsky/Courses/CS364/2000S/Outlines/outline.12.html, +http://duchon.umuc.edu/Web_Pages/duchon/99_f_cm435/ShiftRegister.htm, +http://www.cs.ncl.ac.uk/people/harry.whitfield/home.formal/CRCs.html, +http://www.gpfn.sk.ca/~rhg/csc8550s02/crc.html, and +http://www.usb.org/developers/data/crcdes.pdf give +CRC-16: x^16+x^15+x^2+1. +@end defvr + +@defvr Constant crc-12-polynomial + +http://www.math.grin.edu/~rebelsky/Courses/CS364/2000S/Outlines/outline.12.html, +http://www.cs.ncl.ac.uk/people/harry.whitfield/home.formal/CRCs.html, +http://www.it.iitb.ac.in/it605/lectures/link/node4.html, and +http://spinroot.com/spin/Doc/Book91_PDF/ch3.pdf give +CRC-12: x^12+x^11+x^3+x^2+1. + +But +http://www.ffldusoe.edu/Faculty/Denenberg/Topics/Networks/Error_Detection_Correction/crc.html, +http://duchon.umuc.edu/Web_Pages/duchon/99_f_cm435/ShiftRegister.htm, +http://www.eng.uwi.tt/depts/elec/staff/kimal/errorcc.html, +http://www.ee.uwa.edu.au/~roberto/teach/itc314/java/CRC/, +http://www.gpfn.sk.ca/~rhg/csc8550s02/crc.html, and +http://www.efg2.com/Lab/Mathematics/CRC.htm give +CRC-12: x^12+x^11+x^3+x^2+x+1. + +These differ in bit 1 and calculations using them return different +values. With citations near evenly split, it is hard to know which is +correct. +@end defvr + +@defvr Constant crc-10-polynomial + +http://www.math.grin.edu/~rebelsky/Courses/CS364/2000S/Outlines/outline.12.html gives +CRC-10: x^10+x^9+x^5+x^4+1; +but +http://cell-relay.indiana.edu/cell-relay/publications/software/CRC/crc10.html, +http://www.it.iitb.ac.in/it605/lectures/link/node4.html, +http://www.gpfn.sk.ca/~rhg/csc8550s02/crc.html, +http://www.techfest.com/networking/atm/atm.htm, +http://www.protocols.com/pbook/atmcell2.htm, and +http://www.nobugconsulting.ro/crc.php give +CRC-10: x^10+x^9+x^5+x^4+x+1. +@end defvr + +@defvr Constant crc-08-polynomial + +http://www.math.grin.edu/~rebelsky/Courses/CS364/2000S/Outlines/outline.12.html, +http://www.cs.ncl.ac.uk/people/harry.whitfield/home.formal/CRCs.html, +http://www.it.iitb.ac.in/it605/lectures/link/node4.html, and +http://www.nobugconsulting.ro/crc.php give +CRC-8: x^8+x^2+x^1+1 +@end defvr + +@defvr Constant atm-hec-polynomial + +http://cell-relay.indiana.edu/cell-relay/publications/software/CRC/32bitCRC.tutorial.html and +http://www.gpfn.sk.ca/~rhg/csc8550s02/crc.html give +ATM HEC: x^8+x^2+x+1. +@end defvr -@code{(require 'make-crc)} -@ftindex make-crc +@defvr Constant dowcrc-polynomial -@defun make-port-crc -@defunx make-port-crc degree -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. +http://www.cs.ncl.ac.uk/people/harry.whitfield/home.formal/CRCs.html gives +DOWCRC: x^8+x^5+x^4+1. +@end defvr -The integer @var{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. +@defvr Constant usb-token-polynomial -@defunx make-port-crc generator +http://www.usb.org/developers/data/crcdes.pdf and +http://www.nobugconsulting.ro/crc.php give +USB-token: x^5+x^2+1. +@end defvr -The integer @var{generator} specifies the polynomial being computed. -The power of 2 generating each 1 bit is the exponent of a term of the -polynomial. The value of @var{generator} must be larger than 127. +@noindent +Each of these polynomial constants is a string of @samp{1}s and +@samp{0}s, the exponent of each power of @var{x} in descending order. -@defunx make-port-crc degree generator +@defun crc:make-table poly -The integer @var{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 @var{degree} is implicit and should not -be part of @var{generator}. This allows systems with numbers limited to -32 bits to calculate 32 bit checksums. The default value of -@var{generator} when @var{degree} is 32 (its default) is: +@var{poly} must be string of @samp{1}s and @samp{0}s beginning with +@samp{1} and having length greater than 8. @code{crc:make-table} +returns a vector of 256 integers, such that: @example -(make-port-crc 32 #b00000100110000010001110110110111) +(set! @var{crc} + (logxor (ash (logand (+ -1 (ash 1 (- @var{deg} 8))) @var{crc}) 8) + (vector-ref @var{crc-table} + (logxor (ash @var{crc} (- 8 @var{deg})) @var{byte})))) @end example -Creates a procedure to calculate the P1003.2/D11.2 (POSIX.2) 32-bit -checksum from the polynomial: +will compute the @var{crc} with the 8 additional bits in @var{byte}; +where @var{crc} is the previous accumulated CRC value, @var{deg} is +the degree of @var{poly}, and @var{crc-table} is the vector returned +by @code{crc:make-table}. -@example - 32 26 23 22 16 12 11 - ( x + x + x + x + x + x + x + +If the implementation does not support @var{deg}-bit integers, then +@code{crc:make-table} returns #f. - 10 8 7 5 4 2 1 - x + x + x + x + x + x + x + 1 ) mod 2 -@end example @end defun -@example -(require 'make-crc) -@ftindex 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")) +@defun cksum file + +Computes the P1003.2/D11.2 (POSIX.2) 32-bit checksum of @var{file}. + +@example +(require 'crc) +@ftindex crc +(cksum (in-vicinity (library-vicinity) "ratize.scm")) @result{} 157103930 @end example -@node Plotting, Root Finding, Cyclic Checksum, Mathematical Packages -@section Plotting on Character Devices +@defunx cksum port +Computes the checksum of the bytes read from @var{port} until the +end-of-file. -@code{(require 'charplot)} -@ftindex charplot +@end defun + +@noindent +@cindex cksum-string +@code{cksum-string}, which returns the P1003.2/D11.2 (POSIX.2) 32-bit +checksum of the bytes in @var{str}, can be defined as follows: + +@example +(require 'string-port) +(define (cksum-string str) (call-with-input-string str cksum)) +@end example + +@defun crc16 file + +Computes the USB data-packet (16-bit) CRC of @var{file}. + +@defunx crc16 port +Computes the USB data-packet (16-bit) CRC of the bytes read from +@var{port} until the end-of-file. + +@code{crc16} calculates the same values as the crc16.pl program given +in http://www.usb.org/developers/data/crcdes.pdf. + +@end defun + +@defun crc5 file + +Computes the USB token (5-bit) CRC of @var{file}. + +@defunx crc5 port +Computes the USB token (5-bit) CRC of the bytes read from +@var{port} until the end-of-file. + +@code{crc5} calculates the same values as the crc5.pl program given +in http://www.usb.org/developers/data/crcdes.pdf. + +@end defun + +@node Graphing, Solid Modeling, Cyclic Checksum, Mathematical Packages +@section Graphing + +@menu +* Character Plotting:: +* PostScript Graphing:: +@end menu + +@node Character Plotting, PostScript Graphing, Graphing, Graphing +@subsection Character Plotting -The plotting procedure is made available through the use of the -@code{charplot} package. @code{charplot} is loaded by inserting -@code{(require 'charplot)} before the code that uses this procedure. +@code{(require 'charplot)} @ftindex charplot -@defvar charplot:height -The number of rows to make the plot vertically. -@end defvar +@defvar charplot:dimensions +A list of the maximum height (number of lines) and maximum width (number +of columns) for the graph, its scales, and labels. -@defvar charplot:width -The number of columns to make the plot horizontally. +The default value for @var{charplot:dimensions} is the +@code{output-port-height} and @code{output-port-width} of +@code{current-output-port}. @end defvar -@deffn Procedure plot! coords x-label y-label -@var{coords} is a list of pairs of x and y coordinates. @var{x-label} -and @var{y-label} are strings with which to label the x and y -axes. +@deffn {Procedure} plot coords x-label y-label +@var{coords} is a list or vector of coordinates, lists of x and y +coordinates. @var{x-label} and @var{y-label} are strings with which to +label the x and y axes. Example: @example (require 'charplot) @ftindex charplot -(set! charplot:height 19) -(set! charplot:width 45) +(set! charplot:dimensions '(20 55)) (define (make-points n) (if (zero? n) '() - (cons (cons (/ n 6) (sin (/ n 6))) (make-points (1- n))))) + (cons (list (/ n 6) (sin (/ n 6))) (make-points (1- n))))) -(plot! (make-points 37) "x" "Sin(x)") +(plot (make-points 40) "x" "Sin(x)") @print{} @group - Sin(x) ______________________________________________ - 1.25|- | - | | - 1|- **** | - | ** ** | - 0.75|- * * | - | * * | - 0.5|- * * | - | * | - 0.25|- * | - | * * | - 0|-------------------*--------------------------| - | * | - -0.25|- * * | - | * * | - -0.5|- * | - | * * | - -0.75|- * * | - | ** ** | - -1|- **** | - |____________:_____._____:_____._____:_________| - x 2 4 6 + Sin(x) _________________________________________ + 1|- **** | + | ** ** | + 0.75|- * * | + | * * | + 0.5|- * * | + | * *| + 0.25|- * * | + | * * | + 0|-------------------*------------------*--| + | * | + -0.25|- * * | + | * * | + -0.5|- * | + | * * | + -0.75|- * * | + | ** ** | + -1|- **** | + |:_____._____:_____._____:_____._____:____| + x 2 4 6 @end group @end example @end deffn -@deffn Procedure plot-function! func x1 x2 -@deffnx Procedure plot-function! func x1 x2 npts +@deffn {Procedure} plot func x1 x2 +@deffnx {Procedure} plot func x1 x2 npts Plots the function of one argument @var{func} over the range @var{x1} to @var{x2}. If the optional integer argument @var{npts} is supplied, it specifies the number of points to evaluate @var{func} at. + +@example +(plot sin 0 (* 2 pi)) +@print{} +@group + _________________________________________ + 1|-: **** | + | : ** ** | + 0.75|-: * * | + | : * * | + 0.5|-: ** ** | + | : * * | + 0.25|-:** ** | + | :* * | + 0|-*------------------*--------------------| + | : * * | + -0.25|-: ** ** | + | : * * | + -0.5|-: * ** | + | : * * | + -0.75|-: * ** | + | : ** ** | + -1|-: **** | + |_:_____._____:_____._____:_____._____:___| + 0 2 4 6 +@end group +@end example @end deffn +@deffn {Procedure} histograph data label +Creates and displays a histogram of the numerical values contained in +vector or list @var{data} -@node Root Finding, Minimizing, Plotting, Mathematical Packages -@section Root Finding +@example +(require 'random-inexact) +(histograph (do ((idx 99 (+ -1 idx)) + (lst '() (cons (* .02 (random:normal)) lst))) + ((negative? idx) lst)) + "normal") +@print{} +@group + _________________________________________ + 8|- : I | + | : I | + 7|- I I : I | + | I I : I | + 6|- III I :I I | + | III I :I I | + 5|- IIIIIIIIII I | + | IIIIIIIIII I | + 4|- IIIIIIIIIIII | + | IIIIIIIIIIII | + 3|-I I I IIIIIIIIIIII II I | + | I I I IIIIIIIIIIII II I | + 2|-I I I IIIIIIIIIIIIIIIII I | + | I I I IIIIIIIIIIIIIIIII I | + 1|-II I I IIIIIIIIIIIIIIIIIIIII I I I | + | II I I IIIIIIIIIIIIIIIIIIIII I I I | + 0|-IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII----| + |__.____:____.____:____.____:____.____:___| + normal -0.025 0 0.025 0.05 +@end group +@end example +@end deffn -@code{(require 'root)} -@ftindex root -@defun newtown:find-integer-root f df/dx x0 -Given integer valued procedure @var{f}, its derivative (with respect to -its argument) @var{df/dx}, and initial integer value @var{x0} for which -@var{df/dx}(@var{x0}) is non-zero, returns an integer @var{x} for which -@var{f}(@var{x}) is closer to zero than either of the integers adjacent -to @var{x}; or returns @code{#f} if such an integer can't be found. +@node PostScript Graphing, , Character Plotting, Graphing +@subsection PostScript Graphing -To find the closest integer to a given integers square root: +@include grapheps.txi -@example -(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) @result{} 4 -@end example -@end defun +@node Solid Modeling, Color, Graphing, Mathematical Packages +@section Solid Modeling -@defun integer-sqrt y -Given a non-negative integer @var{y}, returns the rounded square-root of -@var{y}. -@end defun +@include solid.txi -@defun newton:find-root f df/dx x0 prec -Given real valued procedures @var{f}, @var{df/dx} of one (real) -argument, initial real value @var{x0} for which @var{df/dx}(@var{x0}) is -non-zero, and positive real number @var{prec}, returns a real @var{x} -for which @code{abs}(@var{f}(@var{x})) is less than @var{prec}; or -returns @code{#f} if such a real can't be found. -If @var{prec} is instead a negative integer, @code{newton:find-root} -returns the result of -@var{prec} iterations. -@end defun +@node Color, Root Finding, Solid Modeling, Mathematical Packages +@section Color + +@ifset html + +@end ifset + +@uref{http://swissnet.ai.mit.edu/~jaffer/Color} @noindent -H. J. Orchard, @cite{The Laguerre Method for Finding the Zeros of -Polynomials}, IEEE Transactions on Circuits and Systems, Vol. 36, -No. 11, November 1989, pp 1377-1381. +The goals of this package are to provide methods to specify, compute, +and transform colors in a core set of additive color spaces. The color +spaces supported should be sufficient for working with the color data +encountered in practice and the literature. -@quotation -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. -@end quotation +@menu +* Color Data-Type:: 'color +* Color Spaces:: XYZ, L*a*b*, L*u*v*, L*C*h, RGB709, sRGB +* Spectra:: Color Temperatures and CIEXYZ(1931) +* Color Difference Metrics:: Society of Dyers and Colorists +* Color Conversions:: Low-level +* Color Names:: in relational databases +* Daylight:: Sunlight and sky colors +@end menu +@node Color Data-Type, Color Spaces, Color, Color +@subsection Color Data-Type -@defun laguerre:find-root f df/dz ddf/dz^2 z0 prec -Given complex valued procedure @var{f} of one (complex) argument, its -derivative (with respect to its argument) @var{df/dx}, its second -derivative @var{ddf/dz^2}, initial complex value @var{z0}, and positive -real number @var{prec}, returns a complex number @var{z} for which -@code{magnitude}(@var{f}(@var{z})) is less than @var{prec}; or returns -@code{#f} if such a number can't be found. +@ifset html + +@end ifset +@code{(require 'color)} -If @var{prec} is instead a negative integer, @code{laguerre:find-root} -returns the result of -@var{prec} iterations. -@end defun +@defun color? obj +Returns #t if @var{obj} is a color. -@defun laguerre:find-polynomial-root deg f df/dz ddf/dz^2 z0 prec -Given polynomial procedure @var{f} of integer degree @var{deg} of one -argument, its derivative (with respect to its argument) @var{df/dx}, its -second derivative @var{ddf/dz^2}, initial complex value @var{z0}, and -positive real number @var{prec}, returns a complex number @var{z} for -which @code{magnitude}(@var{f}(@var{z})) is less than @var{prec}; or -returns @code{#f} if such a number can't be found. +@defunx color? obj typ +Returns #t if @var{obj} is a color of color-space @var{typ}. The symbol +@var{typ} must be one of: -If @var{prec} is instead a negative integer, -@code{laguerre:find-polynomial-root} returns the result of -@var{prec} -iterations. +@itemize @bullet +@item +CIEXYZ +@item +RGB709 +@item +L*a*b* +@item +L*u*v* +@item +sRGB +@item +e-sRGB +@item +L*C*h +@end itemize @end defun -@defun secant:find-root f x0 x1 prec -@defunx secant:find-bracketed-root f x0 x1 prec -Given a real valued procedure @var{f} and two real valued starting -points @var{x0} and @var{x1}, returns a real @var{x} for which -@code{(abs (f x))} is less than @var{prec}; or returns -@code{#f} if such a real can't be found. - -If @var{x0} and @var{x1} are chosen such that they bracket a root, that is -@example -(or (< (f x0) 0 (f x1)) - (< (f x1) 0 (f x0))) -@end example -then the root returned will be between @var{x0} and @var{x1}, and -@var{f} will not be passed an argument outside of that interval. +@defun make-color space arg @dots{} +Returns a color of type @var{space}. +@end defun -@code{secant:find-bracketed-root} will return @code{#f} unless @var{x0} -and @var{x1} bracket a root. +@defun color-space color +Returns the symbol for the color-space in which @var{color} is embedded. +@end defun -The secant method is used until a bracketing interval is found, at which point -a modified @i{regula falsi} method is used. +@defun color-precision color +For colors in digital color-spaces, @code{color-precision} returns the +number of bits used for each of the R, G, and B channels of the +encoding. Otherwise, @code{color-precision} returns #f +@end defun -If @var{prec} is instead a negative integer, @code{secant:find-root} -returns the result of -@var{prec} iterations. +@defun color-white-point color +Returns the white-point of @var{color} in all color-spaces except CIEXYZ. +@end defun -If @var{prec} is a procedure it should accept 5 arguments: @var{x0} -@var{f0} @var{x1} @var{f1} and @var{count}, where @var{f0} will be -@code{(f x0)}, @var{f1} @code{(f x1)}, and @var{count} the number of -iterations performed so far. @var{prec} should return non-false -if the iteration should be stopped. +@defun convert-color color space white-point +@defunx convert-color color space +@defunx convert-color color e-sRGB precision +Converts @var{color} into @var{space} at optional @var{white-point}. @end defun -@node Minimizing, Commutative Rings, Root Finding, Mathematical Packages -@section Minimizing +@subsubsection External Representation -@code{(require 'minimize)} -@ftindex minimize -@cindex minimize +@noindent +Each color encoding has an external, case-insensitive representation. +To ensure portability, the white-point for all color strings is D65. +@footnote{Readers may recognize these color string formats from Xlib. +X11's color management system was doomed by its fiction that CRT +monitors' (and X11 default) color-spaces were linear RGBi. Unable to +shed this legacy, the only practical way to view pictures on X is to +ignore its color management system and use an sRGB monitor. In this +implementation the device-independent RGB709 and sRGB spaces replace the +device-dependent RGBi and RGB spaces of Xlib.} -@include minimize.txi +@multitable @columnfractions .33 .66 +@item Color Space +@tab External Representation +@item CIEXYZ +@tab CIEXYZ:@i{}/@i{}/@i{} +@item RGB709 +@tab RGBi:@i{}/@i{}/@i{} +@item L*a*b* +@tab CIELAB:@i{}/@i{}/@i{} +@item L*u*v* +@tab CIELuv:@i{}/@i{}/@i{} +@item L*C*h +@tab CIELCh:@i{}/@i{}/@i{} +@end multitable -@node Commutative Rings, Determinant, Minimizing, Mathematical Packages -@section Commutative Rings +@noindent +The @var{X}, @var{Y}, @var{Z}, @var{L}, @var{a}, @var{b}, @var{u}, +@var{v}, @var{C}, @var{h}, @var{R}, @var{G}, and @var{B} fields are +(Scheme) real numbers within the appropriate ranges. -Scheme provides a consistent and capable set of numeric functions. -Inexacts implement a field; integers a commutative ring (and Euclidean -domain). This package allows one to use basic Scheme numeric functions -with symbols and non-numeric elements of commutative rings. +@multitable @columnfractions .33 .66 +@item Color Space +@tab External Representation +@item sRGB +@tab sRGB:@i{}/@i{}/@i{} +@item e-sRGB10 +@tab e-sRGB10:@i{}/@i{}/@i{} +@item e-sRGB12 +@tab e-sRGB12:@i{}/@i{}/@i{} +@item e-sRGB16 +@tab e-sRGB16:@i{}/@i{}/@i{} +@end multitable -@code{(require 'commutative-ring)} -@ftindex commutative-ring -@cindex ring, commutative +@noindent +The @var{R}, @var{G}, and @var{B}, fields are non-negative exact decimal +integers within the appropriate ranges. -The @dfn{commutative-ring} package makes the procedures @code{+}, -@code{-}, @code{*}, @code{/}, and @code{^} @dfn{careful} in the sense -@cindex careful -that any non-numeric arguments they do not 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). -@cindex self-set +@noindent +Several additional syntaxes are supported by @code{string->color}: -@example -(define a 'a) -@dots{} -(define z 'z) -@end example +@multitable @columnfractions .33 .66 +@item Color Space +@tab External Representation +@item sRGB +@tab sRGB:@i{} +@item sRGB +@tab #@i{} +@item sRGB +@tab 0x@i{} +@item sRGB +@tab #x@i{} +@end multitable -Or just @code{(require 'self-set)}. Now try some sample expressions: -@ftindex self-set +Where @var{RRGGBB} is a non-negative six-digit hexadecimal number. -@example -(+ (+ a b) (- a b)) @result{} (* a 2) -(* (+ a b) (+ a b)) @result{} (^ (+ a b) 2) -(* (+ a b) (- a b)) @result{} (* (+ a b) (- a b)) -(* (- a b) (- a b)) @result{} (^ (- a b) 2) -(* (- a b) (+ a b)) @result{} (* (+ a b) (- a b)) -(/ (+ a b) (+ c d)) @result{} (/ (+ a b) (+ c d)) -(^ (+ a b) 3) @result{} (^ (+ a b) 3) -(^ (+ a 2) 3) @result{} (^ (+ 2 a) 3) -@end example +@defun color->string color +Returns a string representation of @var{color}. +@end defun -Associative rules have been applied and repeated addition and -multiplication converted to multiplication and exponentiation. +@defun string->color string +Returns the color represented by @var{string}. If @var{string} is not a +syntactically valid notation for a color, then @code{string->color} +returns #f. +@end defun -We can enable distributive rules, thus expanding to sum of products -form: -@example -(set! *ruleset* (combined-rulesets distribute* distribute/)) +@subsubsection White -(* (+ a b) (+ a b)) @result{} (+ (* 2 a b) (^ a 2) (^ b 2)) -(* (+ a b) (- a b)) @result{} (- (^ a 2) (^ b 2)) -(* (- a b) (- a b)) @result{} (- (+ (^ a 2) (^ b 2)) (* 2 a b)) -(* (- a b) (+ a b)) @result{} (- (^ a 2) (^ b 2)) -(/ (+ a b) (+ c d)) @result{} (+ (/ a (+ c d)) (/ b (+ c d))) -(/ (+ a b) (- c d)) @result{} (+ (/ a (- c d)) (/ b (- c d))) -(/ (- a b) (- c d)) @result{} (- (/ a (- c d)) (/ b (- c d))) -(/ (- a b) (+ c d)) @result{} (- (/ a (+ c d)) (/ b (+ c d))) -(^ (+ a b) 3) @result{} (+ (* 3 a (^ b 2)) (* 3 b (^ a 2)) (^ a 3) (^ b 3)) -(^ (+ a 2) 3) @result{} (+ 8 (* a 12) (* (^ a 2) 6) (^ a 3)) +@noindent +We experience color relative to the illumination around us. +CIEXYZ coordinates, although subject to uniform scaling, are +objective. Thus other color spaces are specified relative to a +@cindex white point +@dfn{white point} in CIEXYZ coordinates. +@cindex white point + +@noindent +The white point for digital color spaces is set to D65. For the other +spaces a @var{white-point} argument can be specified. The default if +none is specified is the white-point with which the color was created +or last converted; and D65 if none has been specified. + +@defvr Constant D65 +Is the color of 6500.K (blackbody) illumination. D65 is close +to the average color of daylight. +@end defvr + +@defvr Constant D50 +Is the color of 5000.K (blackbody) illumination. D50 is the color of +indoor lighting by incandescent bulbs, whose filaments have +temperatures around 5000.K. +@end defvr + + +@node Color Spaces, Spectra, Color Data-Type, Color +@subsection Color Spaces + +@ifset html + +@end ifset +@include color.txi + + + +@node Spectra, Color Difference Metrics, Color Spaces, Color +@subsection Spectra + +@ifset html + +@end ifset +@noindent +The following functions compute colors from spectra, scale color +luminance, and extract chromaticity. XYZ is used in the names of +procedures for unnormalized colors; the coordinates of CIEXYZ colors are +constrained as described in @ref{Color Spaces}. + +@code{(require 'color-space)} + +@noindent +A spectrum may be represented as: + +@itemize @bullet +@item +A procedure of one argument accepting real numbers from 380e-9 to +780e-9, the wavelength in meters; or +@item +A vector of real numbers representing intensity samples evenly spaced +over some range of wavelengths overlapping the range 380e-9 to 780e-9. +@end itemize + +@noindent +CIEXYZ values are calculated as dot-product with the X, Y (Luminance), +and Z @dfn{Spectral Tristimulus Values}. The files @file{cie1931.xyz} +and @file{cie1964.xyz} in the distribution contain these CIE-defined +values. +@cindex Spectral Tristimulus Values + +@deftp {Feature} cie1964 +@ftindex cie1964 +Loads the Spectral Tristimulus Values defining @cite{CIE 1964 +Supplementary Standard Colorimetric Observer}. +@deftpx {Feature} cie1931 +@ftindex cie1931 +Loads the Spectral Tristimulus Values defining @cite{CIE 1931 +Supplementary Standard Colorimetric Observer}. +@deftpx {Feature} ciexyz +@ftindex ciexyz +Requires Spectral Tristimulus Values, defaulting to cie1931. +@end deftp + +@noindent +@code{(require 'cie1964)} or @code{(require 'cie1931)} will +@findex load-ciexyz +@code{load-ciexyz} specific values used by the following spectrum +conversion procedures. The spectrum conversion procedures +@code{(require 'ciexyz)} to assure that a set is loaded. + +@defun spectrum->XYZ proc +@var{proc} must be a function of one argument. @code{spectrum->XYZ} +computes the CIEXYZ(1931) values for the spectrum returned by @var{proc} +when called with arguments from 380e-9 to 780e-9, the wavelength in +meters. + +@defunx spectrum->XYZ spectrum x1 x2 +@var{x1} and @var{x2} must be positive real numbers specifying the +wavelengths (in meters) corresponding to the zeroth and last elements of +vector or list @var{spectrum}. @code{spectrum->XYZ} returns the +CIEXYZ(1931) values for a light source with spectral values proportional +to the elements of @var{spectrum} at evenly spaced wavelengths between +@var{x1} and @var{x2}. + +Compute the colors of 6500.K and 5000.K blackbody radiation: + +@example +(require 'color-space) +(define xyz (spectrum->XYZ (blackbody-spectrum 6500))) +(define y_n (cadr xyz)) +(map (lambda (x) (/ x y_n)) xyz) + @result{} (0.9687111145512467 1.0 1.1210875945303613) + +(define xyz (spectrum->XYZ (blackbody-spectrum 5000))) +(map (lambda (x) (/ x y_n)) xyz) + @result{} (0.2933441826889158 0.2988931825387761 0.25783646831201573) @end example -Use of this package is not restricted to simple arithmetic expressions: +@defunx spectrum->CIEXYZ proc +@defunx spectrum->CIEXYZ spectrum x1 x2 +@code{spectrum->CIEXYZ} computes the CIEXYZ(1931) values for the +spectrum, scaled so their sum is 1. +@end defun + +@defun spectrum->chromaticity proc +@defunx spectrum->chromaticity spectrum x1 x2 +Computes the chromaticity for the given spectrum. +@end defun + +@defun wavelength->XYZ w +@defunx wavelength->chromaticity w +@defunx wavelength->CIEXYZ w +@var{w} must be a number between 380e-9 to 780e-9. +@code{wavelength->XYZ} returns (unnormalized) XYZ values for a +monochromatic light source with wavelength @var{w}. +@code{wavelength->chromaticity} returns the chromaticity for a +monochromatic light source with wavelength @var{w}. +@code{wavelength->CIEXYZ} returns XYZ values for the saturated color +having chromaticity of a monochromatic light source with wavelength +@var{w}. +@end defun + +@defun blackbody-spectrum temp +@defunx blackbody-spectrum temp span +Returns a procedure of one argument (wavelength in meters), which +returns the radiance of a black body at @var{temp}. + +The optional argument @var{span} is the wavelength analog of bandwidth. +With the default @var{span} of 1.nm (1e-9.m), the values returned by the +procedure correspond to the power of the photons with wavelengths +@var{w} to @var{w}+1e-9. +@end defun + +@defun temperature->XYZ x +The positive number @var{x} is a temperature in degrees kelvin. +@code{temperature->XYZ} computes the CIEXYZ(1931) values for the +spectrum of a black body at temperature @var{x}. + +Compute the chromaticities of 6500.K and 5000.K blackbody radiation: @example -(require 'determinant) +(require 'color-space) +(XYZ->chromaticity (temperature->XYZ 6500)) + @result{} (0.3135191660557008 0.3236456786200268) -(determinant '((a b c) (d e f) (g h i))) @result{} -(- (+ (* a e i) (* b f g) (* c d h)) (* a f h) (* b d i) (* c e g)) +(XYZ->chromaticity (temperature->XYZ 5000)) + @result{} (0.34508082841161052 0.3516084965163377) @end example +@end defun -Currently, only @code{+}, @code{-}, @code{*}, @code{/}, and @code{^} -support non-numeric elements. Expressions with @code{-} are converted -to equivalent expressions without @code{-}, so behavior for @code{-} is -not defined separately. @code{/} expressions are handled similarly. +@defun temperature->CIEXYZ x +The positive number @var{x} is a temperature in degrees kelvin. +@code{temperature->CIEXYZ} computes the CIEXYZ(1931) values for the +spectrum of a black body at temperature @var{x}, scaled to be just +inside the RGB709 gamut. +@end defun -This list might be extended to include @code{quotient}, @code{modulo}, -@code{remainder}, @code{lcm}, and @code{gcd}; but these work only for -the more restrictive Euclidean (Unique Factorization) Domain. -@cindex Unique Factorization -@cindex Euclidean Domain +@defun temperature->chromaticity x +@end defun -@heading Rules and Rulesets +@defun XYZ:normalize xyz +@var{xyz} is a list of three non-negative real numbers. +@code{XYZ:normalize} returns a list of numbers proportional to +@var{xyz}; scaled so their sum is 1. +@end defun -The @dfn{commutative-ring} package allows control of ring properties -through the use of @dfn{rulesets}. +@defun XYZ:normalize-colors colors @dots{} +@var{colors} is a list of XYZ triples. @code{XYZ:normalize-colors} +scales all the triples by a common factor such that the maximum sum of +numbers in a scaled triple is 1. +@end defun -@defvar *ruleset* -Contains the set of rules currently in effect. Rules defined by -@code{cring:define-rule} are stored within the value of *ruleset* at the -time @code{cring:define-rule} is called. If @var{*ruleset*} is -@code{#f}, then no rules apply. -@end defvar +@defun XYZ->chromaticity xyz +Returns a two element list: the x and y components of @var{xyz} +normalized to 1 (= @var{x} + @var{y} + @var{z}). +@end defun -@defun make-ruleset rule1 @dots{} -@defunx make-ruleset name rule1 @dots{} -Returns a new ruleset containing the rules formed by applying -@code{cring:define-rule} to each 4-element list argument @var{rule}. If -the first argument to @code{make-ruleset} is a symbol, then the database -table created for the new ruleset will be named @var{name}. Calling -@code{make-ruleset} with no rule arguments creates an empty ruleset. +@defun chromaticity->CIEXYZ x y +Returns the list of @var{x}, and @var{y}, 1 - @var{y} - @var{x}. @end defun -@defun combined-rulesets ruleset1 @dots{} -@defunx combined-rulesets name ruleset1 @dots{} -Returns a new ruleset containing the rules contained in each ruleset -argument @var{ruleset}. If the first argument to -@code{combined-ruleset} is a symbol, then the database table created for -the new ruleset will be named @var{name}. Calling -@code{combined-ruleset} with no ruleset arguments creates an empty -ruleset. +@defun chromaticity->whitepoint x y +Returns the CIEXYZ(1931) values having luminosity 1 and chromaticity +@var{x} and @var{y}. @end defun -Two rulesets are defined by this package. +@cindex xyY +@noindent +Many color datasets are expressed in @dfn{xyY} format; chromaticity with +CIE luminance (Y). But xyY is not a CIE standard like CIEXYZ, CIELAB, +and CIELUV. Although chrominance is well defined, the luminance +component is sometimes scaled to 1, sometimes to 100, but usually has no +obvious range. With no given whitepoint, the only reasonable course is +to ascertain the luminance range of a dataset and normalize the values +to lie from 0 to 1. -@defvr Constant distribute* -Contain the ruleset to distribute multiplication over addition and -subtraction. -@defvrx Constant distribute/ -Contain the ruleset to distribute division over addition and -subtraction. +@defun XYZ->xyY xyz +Returns a three element list: the @var{x} and @var{y} components of +@var{XYZ} normalized to 1, and CIE luminance @var{Y}. +@end defun -Take care when using both @var{distribute*} and @var{distribute/} -simultaneously. It is possible to put @code{/} into an infinite loop. -@end defvr +@defun xyY->XYZ xyY +@end defun -You can specify how sum and product expressions containing non-numeric -elements simplify by specifying the rules for @code{+} or @code{*} for -cases where expressions involving objects reduce to numbers or to -expressions involving different non-numeric elements. +@defun xyY:normalize-colors colors +@var{colors} is a list of xyY triples. @code{xyY:normalize-colors} +scales each chromaticity so it sums to 1 or less; and divides the +@var{Y} values by the maximum @var{Y} in the dataset, so all lie between +0 and 1. -@defun cring:define-rule op sub-op1 sub-op2 reduction -Defines a rule for the case when the operation represented by symbol -@var{op} is applied to lists whose @code{car}s are @var{sub-op1} and -@var{sub-op2}, respectively. The argument @var{reduction} is a -procedure accepting 2 arguments which will be lists whose @code{car}s -are @var{sub-op1} and @var{sub-op2}. +@defunx xyY:normalize-colors colors n +If @var{n} is positive real, then @code{xyY:normalize-colors} divides +the @var{Y} values by @var{n} times the maximum @var{Y} in the dataset. -@defunx cring:define-rule op sub-op1 'identity reduction -Defines a rule for the case when the operation represented by symbol -@var{op} is applied to a list whose @code{car} is @var{sub-op1}, and -some other argument. @var{Reduction} will be called with the list whose -@code{car} is @var{sub-op1} and some other argument. +If @var{n} is an exact non-positive integer, then +@code{xyY:normalize-colors} divides the @var{Y} values by the maximum of +the @var{Y}s in the dataset excepting the -@var{n} largest @var{Y} +values. -If @var{reduction} returns @code{#f}, the reduction has failed and other -reductions will be tried. If @var{reduction} returns a non-false value, -that value will replace the two arguments in arithmetic (@code{+}, -@code{-}, and @code{*}) calculations involving non-numeric elements. +In all cases, returned @var{Y} values are limited to lie from 0 to 1. +@end defun -The operations @code{+} and @code{*} are assumed commutative; hence both -orders of arguments to @var{reduction} will be tried if necessary. +@noindent +Why would one want to normalize to other than 1? If the sun or its +reflection is the brightest object in a scene, then normalizing to its +luminance will tend to make the rest of the scene very dark. As with +photographs, limiting the specular highlights looks better than +darkening everything else. -The following rule is the definition for distributing @code{*} over -@code{+}. +@noindent +The results of measurements being what they are, +@code{xyY:normalize-colors} is extremely tolerant. Negative numbers are +replaced with zero, and chromaticities with sums greater than one are +scaled to sum to one. -@example -(cring:define-rule - '* '+ 'identity - (lambda (exp1 exp2) - (apply + (map (lambda (trm) (* trm exp2)) (cdr exp1)))))) -@end example -@end defun -@heading How to Create a Commutative Ring +@node Color Difference Metrics, Color Conversions, Spectra, Color +@subsection Color Difference Metrics -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. +@ifset html + +@end ifset -@example -(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)))) +@code{(require 'color-space)} -(define (s x y) (n x y)) +The low-level metric functions operate on lists of 3 numbers, lab1, +lab2, lch1, or lch2. -(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)))) -@end example +@code{(require 'color)} -Define a procedure to multiply 2 non-numeric elements of the ring. -Other multiplicatons are handled automatically. Objects for which rules -have @emph{not} been defined are not changed. +The wrapped functions operate on objects of type color, color1 and +color2 in the function entries. -@example -(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)))) -@end example +@defun L*a*b*:DE* lab1 lab2 +Returns the Euclidean distance between @var{lab1} and @var{lab2}. -Test the procedures to see if they work. +@defunx CIE:DE* color1 color2 white-point +@defunx CIE:DE* color1 color2 +Returns the Euclidean distance in L*a*b* space between @var{color1} and +@var{color2}. +@end defun -@example -;;; 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) - (stringstring 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)) @result{} (m a b) -@end example +@defun L*C*h:DE*94 lch1 lch2 parametric-factors +@defunx L*C*h:DE*94 lch1 lch2 -Then register the rule for multiplying type N objects by type N objects. +@defunx CIE:DE*94 color1 color2 parametric-factors +@defunx CIE:DE*94 color1 color2 -@example -(cring:define-rule '* 'N 'N N*N)) -@end example +Measures distance in the L*C*h cylindrical color-space. +The three axes are individually scaled (depending on C*) in their +contributions to the total distance. -Now we are ready to compute! +The CIE has defined reference conditions under which the metric with +default parameters can be expected to perform well. These are: -@example -(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)) -@print{} -(- (+ (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))) -@end example - -@node Determinant, , Commutative Rings, Mathematical Packages -@section Determinant - -@defun determinant square-matrix -Returns the determinant of @var{square-matrix}. +@itemize @bullet +@item +The specimens are homogeneous in colour. +@item +The colour difference (CIELAB) is <= 5 units. +@item +They are placed in direct edge contact. +@item +Each specimen subtends an angle of >4 degrees to the assessor, whose +colour vision is normal. +@item +They are illuminated at 1000 lux, and viewed against a background of +uniform grey, with L* of 50, under illumination simulating D65. +@end itemize -@example -(require 'determinant) -(determinant '((1 2) (3 4))) @result{} -2 -(determinant '((1 2 3) (4 5 6) (7 8 9))) @result{} 0 -(determinant '((1 2 3 4) (5 6 7 8) (9 10 11 12))) @result{} 0 -@end example +The @var{parametric-factors} argument is a list of 3 quantities kL, kC +and kH. @var{parametric-factors} independently adjust each +colour-difference term to account for any deviations from the reference +viewing conditions. Under the reference conditions explained above, the +default is kL = kC = kH = 1. @end defun -@node Database Packages, Other Packages, Mathematical Packages, Top -@chapter Database Packages - -@menu -* Base Table:: -* Relational Database:: 'relational-database -* Weight-Balanced Trees:: 'wt-tree -@end menu +@noindent +The Color Measurement Committee of The Society of Dyers and Colorists in +Great Britain created a more sophisticated color-distance function for +use in judging the consistency of dye lots. With CMC:DE* it is possible +to use a single value pass/fail tolerance for all shades. -@node Base Table, Relational Database, Database Packages, Database Packages -@section Base Table +@defun CMC-DE lch1 lch2 parametric-factors +@defunx CMC-DE lch1 lch2 l c +@defunx CMC-DE lch1 lch2 l +@defunx CMC-DE lch1 lch2 -A base table implementation using Scheme association lists is available -as the value of the identifier @code{alist-table} after doing: +@defunx CMC:DE* color1 color2 l c +@defunx CMC:DE* color1 color2 -@code{(require 'alist-table)} -@ftindex alist-table +@code{CMC:DE} is a L*C*h metric. The @var{parametric-factors} +argument is a list of 2 numbers @var{l} and @var{c}. @var{l} and +@var{c} parameterize this metric. 1 and 1 are recommended for +perceptibility; the default, 2 and 1, for acceptability. +@end defun -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 @ref{Relational Database} package -constructs a Relational system. It will be of interest primarily to -those wishing to port or write new base-table implementations. +@node Color Conversions, Color Names, Color Difference Metrics, Color +@subsection Color Conversions -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 @code{#f} -otherwise. For example: +@ifset html + +@end ifset -@example -@group -(require 'alist-table) -@ftindex alist-table -(define open-base (alist-table 'make-base)) -make-base @result{} *a procedure* -(define foo (alist-table 'foo)) -foo @result{} #f -@end group -@end example +@noindent +This package contains the low-level color conversion and color metric +routines operating on lists of 3 numbers. There is no type or range +checking. -@defun make-base filename key-dimension column-types -Returns a new, open, low-level database (collection of tables) -associated with @var{filename}. This returned database has an empty -table associated with @var{catalog-id}. The positive integer -@var{key-dimension} is the number of keys composed to make a -@var{primary-key} for the catalog table. The list of symbols -@var{column-types} describes the types of each column for that table. -If the database cannot be created as specified, @code{#f} is returned. +@code{(require 'color-space)} -Calling the @code{close-base} method on this database and possibly other -operations will cause @var{filename} to be written to. If -@var{filename} is @code{#f} a temporary, non-disk based database will be -created if such can be supported by the base table implelentation. -@end defun +@defvr Constant CIEXYZ:D65 +Is the color of 6500.K (blackbody) illumination. D65 is close to the +average color of daylight. +@end defvr -@defun open-base filename mutable -Returns an open low-level database associated with @var{filename}. If -@var{mutable?} is @code{#t}, this database will have methods capable of -effecting change to the database. If @var{mutable?} is @code{#f}, only -methods for inquiring the database will be available. If the database -cannot be opened as specified @code{#f} is returned. +@defvr Constant CIEXYZ:D50 +Is the color of 5000.K (blackbody) illumination. D50 is the color of +indoor lighting by incandescent bulbs. +@end defvr -Calling the @code{close-base} (and possibly other) method on a -@var{mutable?} database will cause @var{filename} to be written to. -@end defun +@defvr Constant CIEXYZ:A +@defvrx Constant CIEXYZ:B +@defvrx Constant CIEXYZ:C +@defvrx Constant CIEXYZ:E +CIE 1931 illuminants normalized to 1 = y. +@end defvr -@defun write-base lldb filename -Causes the low-level database @var{lldb} to be written to -@var{filename}. If the write is successful, also causes @var{lldb} to -henceforth be associated with @var{filename}. Calling the -@code{close-database} (and possibly other) method on @var{lldb} may -cause @var{filename} to be written to. If @var{filename} is @code{#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, @code{#t} is returned. -Otherwise, @code{#f} is returned. +@defun color:linear-transform matrix row @end defun -@defun sync-base lldb -Causes the file associated with the low-level database @var{lldb} to be -updated to reflect its current state. If the associated filename is -@code{#f}, no action is taken and @code{#f} is returned. If this -operation completes successfully, @code{#t} is returned. Otherwise, -@code{#f} is returned. +@defun CIEXYZ->RGB709 xyz +@defunx RGB709->CIEXYZ srgb @end defun -@defun close-base lldb -Causes the low-level database @var{lldb} to be written to its associated -file (if any). If the write is successful, subsequent operations to -@var{lldb} will signal an error. If the operations complete -successfully, @code{#t} is returned. Otherwise, @code{#f} is returned. +@defun CIEXYZ->L*u*v* xyz white-point +@defunx CIEXYZ->L*u*v* xyz +@defunx L*u*v*->CIEXYZ L*u*v* white-point +@defunx L*u*v*->CIEXYZ L*u*v* +The @var{white-point} defaults to CIEXYZ:D65. @end defun -@defun make-table lldb key-dimension column-types -Returns the @var{base-id} for a new base table, otherwise returns -@code{#f}. The base table can then be opened using @code{(open-table -@var{lldb} @var{base-id})}. The positive integer @var{key-dimension} is -the number of keys composed to make a @var{primary-key} for this table. -The list of symbols @var{column-types} describes the types of each -column. +@defun CIEXYZ->L*a*b* xyz white-point +@defunx CIEXYZ->L*a*b* xyz +@defunx L*a*b*->CIEXYZ L*a*b* white-point +@defunx L*a*b*->CIEXYZ L*a*b* +The XYZ @var{white-point} defaults to CIEXYZ:D65. @end defun -@defvr Constant catalog-id -A constant @var{base-id} suitable for passing as a parameter to -@code{open-table}. @var{catalog-id} will be used as the base table for -the system catalog. -@end defvr - -@defun open-table lldb base-id key-dimension column-types -Returns a @var{handle} for an existing base table in the low-level -database @var{lldb} if that table exists and can be opened in the mode -indicated by @var{mutable?}, otherwise returns @code{#f}. - -As with @code{make-table}, the positive integer @var{key-dimension} is -the number of keys composed to make a @var{primary-key} for this table. -The list of symbols @var{column-types} describes the types of each -column. +@defun L*a*b*->L*C*h L*a*b* +@defunx L*C*h->L*a*b* L*C*h @end defun -@defun kill-table lldb base-id key-dimension column-types -Returns @code{#t} if the base table associated with @var{base-id} was -removed from the low level database @var{lldb}, and @code{#f} otherwise. +@defun CIEXYZ->sRGB xyz +@defunx sRGB->CIEXYZ srgb @end defun -@defun make-keyifier-1 type -Returns a procedure which accepts a single argument which must be of -type @var{type}. This returned procedure returns an object suitable for -being a @var{key} argument in the functions whose descriptions follow. - -Any 2 arguments of the supported type passed to the returned function -which are not @code{equal?} must result in returned values which are not -@code{equal?}. +@defun CIEXYZ->xRGB xyz +@defunx xRGB->CIEXYZ srgb @end defun -@defun make-list-keyifier key-dimension types -The list of symbols @var{types} must have at least @var{key-dimension} -elements. Returns a procedure which accepts a list of length -@var{key-dimension} and whose types must corresopond to the types named -by @var{types}. This returned procedure combines the elements of its -list argument into an object suitable for being a @var{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 -@code{equal?} must result in returned values which are not -@code{equal?}. +@defun sRGB->xRGB xyz +@defunx xRGB->sRGB srgb @end defun -@defun make-key-extractor key-dimension types column-number -Returns a procedure which accepts objects produced by application of the -result of @code{(make-list-keyifier @var{key-dimension} @var{types})}. -This procedure returns a @var{key} which is @code{equal?} to the -@var{column-number}th element of the list which was passed to create -@var{combined-key}. The list @var{types} must have at least -@var{key-dimension} elements. +@defun CIEXYZ->e-sRGB n xyz +@defunx e-sRGB->CIEXYZ n srgb @end defun -@defun make-key->list key-dimension types -Returns a procedure which accepts objects produced by application of the -result of @code{(make-list-keyifier @var{key-dimension} @var{types})}. -This procedure returns a list of @var{key}s which are elementwise -@code{equal?} to the list which was passed to create @var{combined-key}. +@defun sRGB->e-sRGB n srgb +@defunx e-sRGB->sRGB n srgb +The integer @var{n} must be 10, 12, or 16. Because sRGB and e-sRGB use +the same RGB709 chromaticities, conversion between them is simpler than +conversion through CIEXYZ. @end defun @noindent -In the following functions, the @var{key} argument can always be assumed -to be the value returned by a call to a @emph{keyify} routine. - -@noindent -@cindex match-keys -@cindex match -@cindex wild-card -In contrast, a @var{match-keys} argument is a list of length equal to -the number of primary keys. The @var{match-keys} restrict the actions -of the table command to those records whose primary keys all satisfy the -corresponding element of the @var{match-keys} list. The elements and -their actions are: +Do not convert e-sRGB precision through @code{e-sRGB->sRGB} then +@code{sRGB->e-sRGB} -- values would be truncated to 8-bits! -@quotation -@table @asis -@item @code{#f} -The false value matches any key in the corresponding position. -@item 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 @code{#f} is not. -@item other values -Any other value matches only those keys @code{equal?} to it. -@end table -@end quotation +@defun e-sRGB->e-sRGB n1 srgb n2 +The integers @var{n1} and @var{n2} must be 10, 12, or 16. +@code{e-sRGB->e-sRGB} converts @var{srgb} to e-sRGB of precision +@var{n2}. +@end defun -@noindent -The @var{key-dimension} and @var{column-types} arguments are needed to -decode the combined-keys for matching with @var{match-keys}. -@defun for-each-key handle procedure key-dimension column-types match-keys -Calls @var{procedure} once with each @var{key} in the table opened in -@var{handle} which satisfy @var{match-keys} in an unspecified order. -An unspecified value is returned. -@end defun -@defun map-key handle procedure key-dimension column-types match-keys -Returns a list of the values returned by calling @var{procedure} once -with each @var{key} in the table opened in @var{handle} which satisfy -@var{match-keys} in an unspecified order. -@end defun +@node Color Names, Daylight, Color Conversions, Color +@subsection Color Names -@defun ordered-for-each-key handle procedure key-dimension column-types match-keys -Calls @var{procedure} once with each @var{key} in the table opened in -@var{handle} which satisfy @var{match-keys} in the natural order for -the types of the primary key fields of that table. An unspecified value -is returned. -@end defun +@ifset html + +@end ifset +@include colornam.txi -@defun delete* handle key-dimension column-types match-keys -Removes all rows which satisfy @var{match-keys} from the table opened in -@var{handle}. An unspecified value is returned. -@end defun +@include mkclrnam.txi -@defun present? handle key -Returns a non-@code{#f} value if there is a row associated with -@var{key} in the table opened in @var{handle} and @code{#f} otherwise. -@end defun +@subsubheading The Short List -@defun delete handle key -Removes the row associated with @var{key} from the table opened in -@var{handle}. An unspecified value is returned. -@end defun +@code{(require 'saturate)} +@ftindex saturate -@defun make-getter key-dimension types -Returns a procedure which takes arguments @var{handle} and @var{key}. -This procedure returns a list of the non-primary values of the relation -(in the base table opened in @var{handle}) whose primary key is -@var{key} if it exists, and @code{#f} otherwise. -@end defun +@defun saturate name +Looks for @var{name} among the 19 saturated colors from +@cite{Approximate Colors on CIE Chromaticity Diagram}: -@defun make-putter key-dimension types -Returns a procedure which takes arguments @var{handle} and @var{key} and -@var{value-list}. This procedure associates the primary key @var{key} -with the values in @var{value-list} (in the base table opened in -@var{handle}) and returns an unspecified value. -@end defun +@multitable @columnfractions .25 .25 .25 .25 +@item reddish orange @tab orange @tab yellowish orange @tab yellow +@item greenish yellow @tab yellow green @tab yellowish green @tab green +@item bluish green @tab blue green @tab greenish blue @tab blue +@item purplish blue @tab bluish purple @tab purple @tab reddish purple +@item red purple @tab purplish red @tab red +@end multitable -@defun supported-type? symbol -Returns @code{#t} if @var{symbol} names a type allowed as a column value -by the implementation, and @code{#f} otherwise. At a minimum, an -implementation must support the types @code{integer}, @code{symbol}, -@code{string}, @code{boolean}, and @code{base-id}. +(@url{http://swissnet.ai.mit.edu/~jaffer/Color/saturate.pdf}). If +@var{name} is found, the corresponding color is returned. Otherwise #f +is returned. Use saturate only for light source colors. @end defun -@defun supported-key-type? symbol -Returns @code{#t} if @var{symbol} names a type allowed as a key value by -the implementation, and @code{#f} otherwise. At a minimum, an -implementation must support the types @code{integer}, and @code{symbol}. -@end defun -@table @code -@item integer -Scheme exact integer. -@item symbol -Scheme symbol. -@item boolean -@code{#t} or @code{#f}. -@item base-id -Objects suitable for passing as the @var{base-id} parameter to -@code{open-table}. The value of @var{catalog-id} must be an acceptable -@code{base-id}. -@end table +@noindent +Resene Paints Limited, New Zealand's largest privately-owned and +operated paint manufacturing company, has generously made their +@cite{Resene RGB Values List} available. -@node Relational Database, Weight-Balanced Trees, Base Table, Database Packages -@section Relational Database +@code{(require 'resene)} +@ftindex resene -@code{(require 'relational-database)} -@ftindex relational-database +@defun resene name +Looks for @var{name} among the 1300 entries in the Resene color-name +dictionary (@url{http://swissnet.ai.mit.edu/~jaffer/Color/resene.pdf}). +If @var{name} is found, the corresponding color is returned. Otherwise +#f is returned. The @cite{Resene RGB Values List} is an excellent +source for surface colors. +@end defun -This package implements a database system inspired by the Relational -Model (@cite{E. F. Codd, A Relational Model of Data for Large Shared -Data Banks}). An SLIB relational database implementation can be created -from any @ref{Base Table} implementation. +@noindent +If you include the @dfn{Resene RGB Values List} in binary form in a +program, then you must include its license with your program: -@menu -* Motivations:: Database Manifesto -* Creating and Opening Relational Databases:: -* Relational Database Operations:: -* Table Operations:: -* Catalog Representation:: -* Unresolved Issues:: -* Database Utilities:: 'database-utilities -* Database Reports:: -* Database Browser:: 'database-browse -@end menu +@quotation +Resene RGB Values List@* +For further information refer to http://www.resene.co.nz@* +Copyright Resene Paints Ltd 2001 -@node Motivations, Creating and Opening Relational Databases, Relational Database, Relational Database -@subsection 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 conscientious 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 -@emph{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 @dfn{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 -@dfn{expression}s. This type allows expressions as defined by the -Scheme standards to be stored in the database. Using @code{slib:eval} -retrieved expressions can be evaluated (in the top-level environment). -Scheme's @code{lambda} facilitates closure of environments, modularity, -etc. so that procedures (which could not be stored directly most -databases) can still be effectively retrieved. Since @code{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. - -@noindent -Such standardization to a relational-like model brings many benefits: +Permission to copy this dictionary, to modify it, to redistribute it, +to distribute modified versions, and to use it for any purpose is +granted, subject to the following restrictions and understandings. -@itemize @bullet -@item -Tables, fields, domains, and types can be dealt with by name in -programs. -@item -The underlying database implementation can be changed (for -performance or other reasons) by changing a single line of code. -@item -The formats of tables can be easily extended or changed without -altering code. -@item -Consistency checks are specified as part of the table descriptions. -Changes in checks need only occur in one place. +@enumerate @item -All the configuration information which the developer wishes to group -together is easily grouped, without needing to change programs aware of -only some of these tables. +Any text copy made of this dictionary must include this copyright +notice in full. + @item -Generalized report generators, interactive entry programs, and other -database utilities can be part of a shared library. The burden of -adding configurability to a program is greatly reduced. +Any redistribution in binary form must reproduce this copyright +notice in the documentation or other materials provided with the +distribution. + @item -Scheme is the "comprehensive language" for these databases. Scripting -for configuration no longer needs to be in a separate language with -additional documentation. +Resene Paints Ltd makes no warranty or representation that this +dictionary is error-free, and is under no obligation to provide any +services, by way of maintenance, update, or otherwise. + @item -Scheme's latent types mesh well with the strict typing and logical -requirements of the relational model. +There shall be no use of the name of Resene or Resene Paints Ltd +in any advertising, promotional, or sales literature without prior +written consent in each case. + @item -Portable formats allow easy interchange of data. The included table -descriptions help prevent misinterpretation of format. -@end itemize +These RGB colour formulations may not be used to the detriment of +Resene Paints Ltd. +@end enumerate +@end quotation -@node Creating and Opening Relational Databases, Relational Database Operations, Motivations, Relational Database -@subsection Creating and Opening Relational Databases -@defun make-relational-system base-table-implementation +@node Daylight, , Color Names, Color +@subsection Daylight -Returns a procedure implementing a relational database using the -@var{base-table-implementation}. +@ifset html + +@end ifset +@include daylight.txi -All of the operations of a base table implementation are accessed -through a procedure defined by @code{require}ing that implementation. -Similarly, all of the operations of the relational database -implementation are accessed through the procedure returned by -@code{make-relational-system}. For instance, a new relational database -could be created from the procedure returned by -@code{make-relational-system} by: - -@example -(require 'alist-table) -@ftindex 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")) -@end example -@end defun - -@noindent -What follows are the descriptions of the methods available from -relational system returned by a call to @code{make-relational-system}. - -@defun create-database filename -Returns an open, nearly empty relational database associated with -@var{filename}. The only tables defined are the system catalog and -domain table. Calling the @code{close-database} method on this database -and possibly other operations will cause @var{filename} to be written -to. If @var{filename} is @code{#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 -@code{#f} is returned. For the fields and layout of descriptor tables, -@ref{Catalog Representation} -@end defun -@defun open-database filename mutable? +@node Root Finding, Minimizing, Color, Mathematical Packages +@section Root Finding -Returns an open relational database associated with @var{filename}. If -@var{mutable?} is @code{#t}, this database will have methods capable of -effecting change to the database. If @var{mutable?} is @code{#f}, only -methods for inquiring the database will be available. Calling the -@code{close-database} (and possibly other) method on a @var{mutable?} -database will cause @var{filename} to be written to. If the database -cannot be opened as specified @code{#f} is returned. -@end defun +@code{(require 'root)} +@ftindex root -@node Relational Database Operations, Table Operations, Creating and Opening Relational Databases, Relational Database -@subsection Relational Database Operations +@defun newton:find-integer-root f df/dx x0 +Given integer valued procedure @var{f}, its derivative (with respect to +its argument) @var{df/dx}, and initial integer value @var{x0} for which +@var{df/dx}(@var{x0}) is non-zero, returns an integer @var{x} for which +@var{f}(@var{x}) is closer to zero than either of the integers adjacent +to @var{x}; or returns @code{#f} if such an integer can't be found. -@noindent -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: +To find the closest integer to a given integers square root: @example -(define my-database - (create-alist-database "mydata.db")) -(define telephone-table-desc - ((my-database 'create-table) 'telephone-table-desc)) -@end example - -@defun 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, @code{#t} is returned. Otherwise, @code{#f} is returned. -@end defun +(define (integer-sqrt y) + (newton:find-integer-root + (lambda (x) (- (* x x) y)) + (lambda (x) (* 2 x)) + (ash 1 (quotient (integer-length y) 2)))) -@defun write-database filename -Causes the relational database to be written to @var{filename}. If the -write is successful, also causes the database to henceforth be -associated with @var{filename}. Calling the @code{close-database} (and -possibly other) method on this database will cause @var{filename} to be -written to. If @var{filename} is @code{#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, @code{#t} is returned. Otherwise, @code{#f} is -returned. +(integer-sqrt 15) @result{} 4 +@end example @end defun -@defun sync-database -Causes any pending updates to the database file to be written out. If -the operations completed successfully, @code{#t} is returned. -Otherwise, @code{#f} is returned. +@defun integer-sqrt y +Given a non-negative integer @var{y}, returns the rounded square-root of +@var{y}. @end defun -@defun table-exists? table-name -Returns @code{#t} if @var{table-name} exists in the system catalog, -otherwise returns @code{#f}. -@end defun +@defun newton:find-root f df/dx x0 prec +Given real valued procedures @var{f}, @var{df/dx} of one (real) +argument, initial real value @var{x0} for which @var{df/dx}(@var{x0}) is +non-zero, and positive real number @var{prec}, returns a real @var{x} +for which @code{abs}(@var{f}(@var{x})) is less than @var{prec}; or +returns @code{#f} if such a real can't be found. -@defun open-table table-name mutable? -Returns a @dfn{methods} procedure for an existing relational table in -this database if it exists and can be opened in the mode indicated by -@var{mutable?}, otherwise returns @code{#f}. +If @var{prec} is instead a negative integer, @code{newton:find-root} +returns the result of -@var{prec} iterations. @end defun @noindent -These methods will be present only in databases which are -@var{mutable?}. +H. J. Orchard, @cite{The Laguerre Method for Finding the Zeros of +Polynomials}, IEEE Transactions on Circuits and Systems, Vol. 36, +No. 11, November 1989, pp 1377-1381. -@defun delete-table table-name -Removes and returns the @var{table-name} row from the system catalog if -the table or view associated with @var{table-name} gets removed from the -database, and @code{#f} otherwise. -@end defun +@quotation +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. +@end quotation -@defun 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 @code{#f}. For the fields and layout of descriptor tables, -@xref{Catalog Representation}. -@defunx create-table table-name table-desc-name -Returns a methods procedure for a new (open) relational table with -columns as described by @var{table-desc-name}, otherwise returns -@code{#f}. -@end defun +@defun laguerre:find-root f df/dz ddf/dz^2 z0 prec +Given complex valued procedure @var{f} of one (complex) argument, its +derivative (with respect to its argument) @var{df/dx}, its second +derivative @var{ddf/dz^2}, initial complex value @var{z0}, and positive +real number @var{prec}, returns a complex number @var{z} for which +@code{magnitude}(@var{f}(@var{z})) is less than @var{prec}; or returns +@code{#f} if such a number can't be found. -@defun create-view ?? -@defunx project-table ?? -@defunx restrict-table ?? -@defunx cart-prod-tables ?? -Not yet implemented. +If @var{prec} is instead a negative integer, @code{laguerre:find-root} +returns the result of -@var{prec} iterations. @end defun -@node Table Operations, Catalog Representation, Relational Database Operations, Relational Database -@subsection Table Operations +@defun laguerre:find-polynomial-root deg f df/dz ddf/dz^2 z0 prec +Given polynomial procedure @var{f} of integer degree @var{deg} of one +argument, its derivative (with respect to its argument) @var{df/dx}, its +second derivative @var{ddf/dz^2}, initial complex value @var{z0}, and +positive real number @var{prec}, returns a complex number @var{z} for +which @code{magnitude}(@var{f}(@var{z})) is less than @var{prec}; or +returns @code{#f} if such a number can't be found. -@noindent -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: +If @var{prec} is instead a negative integer, +@code{laguerre:find-polynomial-root} returns the result of -@var{prec} +iterations. +@end defun + +@defun secant:find-root f x0 x1 prec +@defunx secant:find-bracketed-root f x0 x1 prec +Given a real valued procedure @var{f} and two real valued starting +points @var{x0} and @var{x1}, returns a real @var{x} for which +@code{(abs (f x))} is less than @var{prec}; or returns +@code{#f} if such a real can't be found. +If @var{x0} and @var{x1} are chosen such that they bracket a root, that is @example -@group -(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)) -@end group +(or (< (f x0) 0 (f x1)) + (< (f x1) 0 (f x0))) @end example +then the root returned will be between @var{x0} and @var{x1}, and +@var{f} will not be passed an argument outside of that interval. -@noindent -Some operations described below require primary key arguments. Primary -keys arguments are denoted @var{key1} @var{key2} @dots{}. 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. +@code{secant:find-bracketed-root} will return @code{#f} unless @var{x0} +and @var{x1} bracket a root. -@noindent -The term @dfn{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 @code{#f}. Primary keys must not -be missing. +The secant method is used until a bracketing interval is found, at which point +a modified @i{regula falsi} method is used. -@defun get column-name -Returns a procedure of arguments @var{key1} @var{key2} @dots{} which -returns the value for the @var{column-name} column of the row associated -with primary keys @var{key1}, @var{key2} @dots{} if that row exists in -the table, or @code{#f} otherwise. +If @var{prec} is instead a negative integer, @code{secant:find-root} +returns the result of -@var{prec} iterations. -@example -((plat 'get 'processor) 'djgpp) @result{} i386 -((plat 'get 'processor) 'be-os) @result{} #f -@end example +If @var{prec} is a procedure it should accept 5 arguments: @var{x0} +@var{f0} @var{x1} @var{f1} and @var{count}, where @var{f0} will be +@code{(f x0)}, @var{f1} @code{(f x1)}, and @var{count} the number of +iterations performed so far. @var{prec} should return non-false +if the iteration should be stopped. +@end defun -@defunx get* column-name -Returns a procedure of optional arguments @var{match-key1} @dots{} which -returns a list of the values for the specified column for all rows in -this table. The optional @var{match-key1} @dots{} arguments restrict -actions to a subset of the table. See the match-key description below -for details. +@node Minimizing, Commutative Rings, Root Finding, Mathematical Packages +@section Minimizing -@example -((plat 'get* 'processor)) @result{} -(i386 8086 i386 8086 i386 i386 8086 m68000 - m68000 m68000 m68000 m68000 powerpc) +@code{(require 'minimize)} +@ftindex minimize +@cindex minimize -((plat 'get* 'processor) #f) @result{} -(i386 8086 i386 8086 i386 i386 8086 m68000 - m68000 m68000 m68000 m68000 powerpc) +@include minimize.txi -(define (a-key? key) - (char=? #\a (string-ref (symbol->string key) 0))) +@node Commutative Rings, Matrix Algebra, Minimizing, Mathematical Packages +@section Commutative Rings -((plat 'get* 'processor) a-key?) @result{} -(m68000 m68000 m68000 m68000 m68000 powerpc) +Scheme provides a consistent and capable set of numeric functions. +Inexacts implement a field; integers a commutative ring (and Euclidean +domain). This package allows one to use basic Scheme numeric functions +with symbols and non-numeric elements of commutative rings. -((plat 'get* 'name) a-key?) @result{} -(atari-st-turbo-c atari-st-gcc amiga-sas/c-5.10 - amiga-aztec amiga-dice-c aix) -@end example -@end defun +@code{(require 'commutative-ring)} +@ftindex commutative-ring +@cindex ring, commutative -@defun row:retrieve -Returns a procedure of arguments @var{key1} @var{key2} @dots{} which -returns the row associated with primary keys @var{key1}, @var{key2} -@dots{} if it exists, or @code{#f} otherwise. +The @dfn{commutative-ring} package makes the procedures @code{+}, +@code{-}, @code{*}, @code{/}, and @code{^} @dfn{careful} in the sense +@cindex careful +that any non-numeric arguments they do not 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). +@cindex self-set @example -((plat 'row:retrieve) 'linux) @result{} (linux i386 linux gcc) -((plat 'row:retrieve) 'multics) @result{} #f +(define a 'a) +@dots{} +(define z 'z) @end example -@defunx row:retrieve* -Returns a procedure of optional arguments @var{match-key1} @dots{} which -returns a list of all rows in this table. The optional @var{match-key1} -@dots{} arguments restrict actions to a subset of the table. See the -match-key description below for details. -@end defun +Or just @code{(require 'self-set)}. Now try some sample expressions: +@ftindex self-set @example -((plat 'row:retrieve*) a-key?) @result{} -((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 -)) +(+ (+ a b) (- a b)) @result{} (* a 2) +(* (+ a b) (+ a b)) @result{} (^ (+ a b) 2) +(* (+ a b) (- a b)) @result{} (* (+ a b) (- a b)) +(* (- a b) (- a b)) @result{} (^ (- a b) 2) +(* (- a b) (+ a b)) @result{} (* (+ a b) (- a b)) +(/ (+ a b) (+ c d)) @result{} (/ (+ a b) (+ c d)) +(^ (+ a b) 3) @result{} (^ (+ a b) 3) +(^ (+ a 2) 3) @result{} (^ (+ 2 a) 3) @end example -@defun row:remove -Returns a procedure of arguments @var{key1} @var{key2} @dots{} which -removes and returns the row associated with primary keys @var{key1}, -@var{key2} @dots{} if it exists, or @code{#f} otherwise. - -@defunx row:remove* -Returns a procedure of optional arguments @var{match-key1} @dots{} which -removes and returns a list of all rows in this table. The optional -@var{match-key1} @dots{} arguments restrict actions to a subset of the -table. See the match-key description below for details. -@end defun +Associative rules have been applied and repeated addition and +multiplication converted to multiplication and exponentiation. -@defun row:delete -Returns a procedure of arguments @var{key1} @var{key2} @dots{} which -deletes the row associated with primary keys @var{key1}, @var{key2} -@dots{} if it exists. The value returned is unspecified. +We can enable distributive rules, thus expanding to sum of products +form: +@example +(set! *ruleset* (combined-rulesets distribute* distribute/)) -@defunx row:delete* -Returns a procedure of optional arguments @var{match-key1} @dots{} which -Deletes all rows from this table. The optional @var{match-key1} @dots{} -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. -@end defun +(* (+ a b) (+ a b)) @result{} (+ (* 2 a b) (^ a 2) (^ b 2)) +(* (+ a b) (- a b)) @result{} (- (^ a 2) (^ b 2)) +(* (- a b) (- a b)) @result{} (- (+ (^ a 2) (^ b 2)) (* 2 a b)) +(* (- a b) (+ a b)) @result{} (- (^ a 2) (^ b 2)) +(/ (+ a b) (+ c d)) @result{} (+ (/ a (+ c d)) (/ b (+ c d))) +(/ (+ a b) (- c d)) @result{} (+ (/ a (- c d)) (/ b (- c d))) +(/ (- a b) (- c d)) @result{} (- (/ a (- c d)) (/ b (- c d))) +(/ (- a b) (+ c d)) @result{} (- (/ a (+ c d)) (/ b (+ c d))) +(^ (+ a b) 3) @result{} (+ (* 3 a (^ b 2)) (* 3 b (^ a 2)) (^ a 3) (^ b 3)) +(^ (+ a 2) 3) @result{} (+ 8 (* a 12) (* (^ a 2) 6) (^ a 3)) +@end example -@defun row:update -Returns a procedure of one argument, @var{row}, which adds the row, -@var{row}, to this table. If a row for the primary key(s) specified by -@var{row} already exists in this table, it will be overwritten. The -value returned is unspecified. +Use of this package is not restricted to simple arithmetic expressions: -@defunx row:update* -Returns a procedure of one argument, @var{rows}, which adds each row in -the list of rows, @var{rows}, to this table. If a row for the primary -key specified by an element of @var{rows} already exists in this table, -it will be overwritten. The value returned is unspecified. -@end defun +@example +(require 'determinant) -@defun row:insert -Adds the row @var{row} to this table. If a row for the primary key(s) -specified by @var{row} already exists in this table an error is -signaled. The value returned is unspecified. +(determinant '((a b c) (d e f) (g h i))) @result{} +(- (+ (* a e i) (* b f g) (* c d h)) (* a f h) (* b d i) (* c e g)) +@end example -@defunx row:insert* -Returns a procedure of one argument, @var{rows}, which adds each row in -the list of rows, @var{rows}, to this table. If a row for the primary -key specified by an element of @var{rows} already exists in this table, -an error is signaled. The value returned is unspecified. -@end defun +Currently, only @code{+}, @code{-}, @code{*}, @code{/}, and @code{^} +support non-numeric elements. Expressions with @code{-} are converted +to equivalent expressions without @code{-}, so behavior for @code{-} is +not defined separately. @code{/} expressions are handled similarly. -@defun for-each-row -Returns a procedure of arguments @var{proc} @var{match-key1} @dots{} -which calls @var{proc} with each @var{row} in this table in the -(implementation-dependent) natural ordering for rows. The optional -@var{match-key1} @dots{} arguments restrict actions to a subset of the -table. See the match-key description below for details. +This list might be extended to include @code{quotient}, @code{modulo}, +@code{remainder}, @code{lcm}, and @code{gcd}; but these work only for +the more restrictive Euclidean (Unique Factorization) Domain. +@cindex Unique Factorization +@cindex Euclidean Domain -@emph{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. -@end defun +@section Rules and Rulesets -@noindent -@cindex match-keys -The (optional) @var{match-key1} @dots{} 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 @var{match-key} arguments -default to @code{#f}. +The @dfn{commutative-ring} package allows control of ring properties +through the use of @dfn{rulesets}. -@noindent -The @var{match-key1} @dots{} restrict the actions of the table command -to those records whose primary keys each satisfy the corresponding -@var{match-key} argument. The arguments and their actions are: +@defvar *ruleset* +Contains the set of rules currently in effect. Rules defined by +@code{cring:define-rule} are stored within the value of *ruleset* at the +time @code{cring:define-rule} is called. If @var{*ruleset*} is +@code{#f}, then no rules apply. +@end defvar -@quotation -@table @asis -@item @code{#f} -The false value matches any key in the corresponding position. -@item 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 @code{#f} is not. -@item other values -Any other value matches only those keys @code{equal?} to it. -@end table -@end quotation +@defun make-ruleset rule1 @dots{} +@defunx make-ruleset name rule1 @dots{} +Returns a new ruleset containing the rules formed by applying +@code{cring:define-rule} to each 4-element list argument @var{rule}. If +the first argument to @code{make-ruleset} is a symbol, then the database +table created for the new ruleset will be named @var{name}. Calling +@code{make-ruleset} with no rule arguments creates an empty ruleset. +@end defun -@defun close-table -Subsequent operations to this table will signal an error. +@defun combined-rulesets ruleset1 @dots{} +@defunx combined-rulesets name ruleset1 @dots{} +Returns a new ruleset containing the rules contained in each ruleset +argument @var{ruleset}. If the first argument to +@code{combined-ruleset} is a symbol, then the database table created for +the new ruleset will be named @var{name}. Calling +@code{combined-ruleset} with no ruleset arguments creates an empty +ruleset. @end defun -@defvr Constant column-names -@defvrx Constant column-foreigns -@defvrx Constant column-domains -@defvrx 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. +Two rulesets are defined by this package. -@defvrx Constant primary-limit -Returns the number of primary keys fields in the relations in this -table. +@defvr Constant distribute* +Contains the ruleset to distribute multiplication over addition and +subtraction. @end defvr -@node Catalog Representation, Unresolved Issues, Table Operations, Relational Database -@subsection Catalog Representation - -@noindent -Each database (in an implementation) has a @dfn{system catalog} which -describes all the user accessible tables in that database (including -itself). +@defvr Constant distribute/ +Contains the ruleset to distribute division over addition and +subtraction. -@noindent -The system catalog base table has the following fields. @code{PRI} -indicates a primary key for that table. +Take care when using both @var{distribute*} and @var{distribute/} +simultaneously. It is possible to put @code{/} into an infinite loop. +@end defvr -@example -@group -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. -@end group -@end example +You can specify how sum and product expressions containing non-numeric +elements simplify by specifying the rules for @code{+} or @code{*} for +cases where expressions involving objects reduce to numbers or to +expressions involving different non-numeric elements. -@noindent -Descriptors for base tables (not views) are tables (pointed to by -system catalog). Descriptor (base) tables have the fields: +@defun cring:define-rule op sub-op1 sub-op2 reduction +Defines a rule for the case when the operation represented by symbol +@var{op} is applied to lists whose @code{car}s are @var{sub-op1} and +@var{sub-op2}, respectively. The argument @var{reduction} is a +procedure accepting 2 arguments which will be lists whose @code{car}s +are @var{sub-op1} and @var{sub-op2}. -@example -@group -PRI column-number sequential integers from 1 - primary-key? boolean TRUE for primary key components - column-name - column-integrity-rule - domain-name -@end group -@end example +@defunx cring:define-rule op sub-op1 'identity reduction +Defines a rule for the case when the operation represented by symbol +@var{op} is applied to a list whose @code{car} is @var{sub-op1}, and +some other argument. @var{Reduction} will be called with the list whose +@code{car} is @var{sub-op1} and some other argument. -@noindent -A @dfn{primary key} is any column marked as @code{primary-key?} in the -corresponding descriptor table. All the @code{primary-key?} columns -must have lower column numbers than any non-@code{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. +If @var{reduction} returns @code{#f}, the reduction has failed and other +reductions will be tried. If @var{reduction} returns a non-false value, +that value will replace the two arguments in arithmetic (@code{+}, +@code{-}, and @code{*}) calculations involving non-numeric elements. -@noindent -This package currently supports tables having from 1 to 4 primary keys -if there are non-primary columns, and any (natural) number if @emph{all} -columns are primary keys. If you need more than 4 primary keys, I would -like to hear what you are doing! +The operations @code{+} and @code{*} are assumed commutative; hence both +orders of arguments to @var{reduction} will be tried if necessary. -@noindent -A @dfn{domain} is a category describing the allowable values to occur in -a column. It is described by a (base) table with the fields: +The following rule is the definition for distributing @code{*} over +@code{+}. @example -@group -PRI domain-name - foreign-table - domain-integrity-rule - type-id - type-param -@end group +(cring:define-rule + '* '+ 'identity + (lambda (exp1 exp2) + (apply + (map (lambda (trm) (* trm exp2)) (cdr exp1)))))) @end example +@end defun -@noindent -The @dfn{type-id} field value is a symbol. This symbol may be used by -the underlying base table implementation in storing that field. +@section How to Create a Commutative Ring -@noindent -If the @code{foreign-table} field is non-@code{#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 @var{type-param} (or -@code{#f}, if allowed). This package currently does not support -composite foreign-keys. +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. -@noindent -The types for which support is planned are: @example -@group - atom - symbol - string [] - number [] - money - date-time - boolean +(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)))) - foreign-key - expression - virtual -@end group +(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)))) @end example -@node Unresolved Issues, Database Utilities, Catalog Representation, Relational Database -@subsection Unresolved Issues +Define a procedure to multiply 2 non-numeric elements of the ring. +Other multiplicatons are handled automatically. Objects for which rules +have @emph{not} been defined are not changed. -Although @file{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. +@example +(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)))) +@end example -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. +Test the procedures to see if they work. -The system catalog has a field called @code{view-procedure}. This -should allow a purely functional implementation of views. This will -work but is unsatisfying for views resulting from a @dfn{select}ion -(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. +@example +;;; 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) + (stringstring 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 (slib: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 (slib:error 'cyclicsplice list1 list2)))) -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. +(N*N (S a b) (S a b)) @result{} (m a b) +@end example -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. +Then register the rule for multiplying type N objects by type N objects. -@noindent -There are 2 scope issues that must be resolved for multiprocess -transaction boundaries: +@example +(cring:define-rule '* 'N 'N N*N)) +@end example -@table @asis -@item 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, @code{dynamic-wind} would -provide a workable hook into process switching for many implementations. -@item Shared utilities with state -Some shared utilities have state which should @emph{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. -@end table +Now we are ready to compute! -@node Database Utilities, Database Reports, Unresolved Issues, Relational Database -@subsection Database Utilities +@example +(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)) +@print{} +(- (+ (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))) +@end example -@code{(require 'database-utilities)} -@ftindex database-utilities -@noindent -This enhancement wraps a utility layer on @code{relational-database} -which provides: -@itemize @bullet -@item -Automatic loading of the appropriate base-table package when opening a -database. -@item -Automatic execution of initialization commands stored in database. -@item -Transparent execution of database commands stored in @code{*commands*} -table in database. -@end itemize +@node Matrix Algebra, , Commutative Rings, Mathematical Packages +@section Matrix Algebra -@noindent -Also included are utilities which provide: -@itemize @bullet -@item -Data definition from Scheme lists and -@item -Report generation -@end itemize -@noindent -for any SLIB relational database. +@include determ.txi -@defun create-database filename base-table-type -Returns an open, nearly empty enhanced (with @code{*commands*} table) -relational database (with base-table type @var{base-table-type}) -associated with @var{filename}. -@end defun -@defun open-database filename -@defunx open-database filename base-table-type -Returns an open enchanced relational database associated with -@var{filename}. The database will be opened with base-table type -@var{base-table-type}) if supplied. If @var{base-table-type} is not -supplied, @code{open-database} will attempt to deduce the correct -base-table-type. If the database can not be opened or if it lacks the -@code{*commands*} table, @code{#f} is returned. +@node Database Packages, Other Packages, Mathematical Packages, Top +@chapter Database Packages + +@menu +* Relational Database:: 'relational-database +* Relational Infrastructure:: +* Weight-Balanced Trees:: 'wt-tree +@end menu + +@node Relational Database, Relational Infrastructure, Database Packages, Database Packages +@section Relational Database + +@code{(require 'relational-database)} +@ftindex relational-database + +This package implements a database system inspired by the Relational +Model (@cite{E. F. Codd, A Relational Model of Data for Large Shared +Data Banks}). An SLIB relational database implementation can be created +from any @ref{Base Table} implementation. + +Why relational database? For motivations and design issues see@* +@uref{http://swissnet.ai.mit.edu/~jaffer/DBManifesto.html}. + +@menu +* Using Databases:: 'databases +* Table Operations:: +* Database Interpolation:: 'database-interpolate +* Embedded Commands:: 'database-commands +* Database Macros:: 'within-database +* Database Browser:: 'database-browse +@end menu + +@node Using Databases, Table Operations, Relational Database, Relational Database +@subsection Using Databases + +@include dbutil.txi + -@defunx open-database! filename -@defunx open-database! filename base-table-type -Returns @emph{mutable} open enchanced relational database @dots{} -@end defun + +@node Table Operations, Database Interpolation, Using Databases, Relational Database +@subsection Table Operations @noindent -The table @code{*commands*} in an @dfn{enhanced} relational-database has -the fields (with domains): +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: + @example -@group -PRI name symbol - parameters parameter-list - procedure expression - documentation string -@end group +((plat 'get 'processor) 'djgpp) @result{} i386 @end example -The @code{parameters} field is a foreign key (domain -@code{parameter-list}) of the @code{*catalog-data*} table and should -have the value of a table described by @code{*parameter-columns*}. This -@code{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 -@code{parameter-list} table has the following fields: +@noindent +Some operations described below require primary key arguments. Primary +keys arguments are denoted @var{key1} @var{key2} @dots{}. 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. + +@defop {Operation} {relational-table} get column-name +Returns a procedure of arguments @var{key1} @var{key2} @dots{} which +returns the value for the @var{column-name} column of the row associated +with primary keys @var{key1}, @var{key2} @dots{} if that row exists in +the table, or @code{#f} otherwise. + @example -@group -PRI index uint - name symbol - arity parameter-arity - domain domain - defaulter expression - expander expression - documentation string -@end group +((plat 'get 'processor) 'djgpp) @result{} i386 +((plat 'get 'processor) 'be-os) @result{} #f @end example +@end defop -The @code{arity} field can take the values: - -@table @code -@item single -Requires a single parameter of the specified domain. -@item optional -A single parameter of the specified domain or zero parameters is -acceptable. -@item boolean -A single boolean parameter or zero parameters (in which case @code{#f} -is substituted) is acceptable. -@item 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. -@item 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. -@end table +@menu +* Single Row Operations:: +* Match-Keys:: +* Multi-Row Operations:: +* Indexed Sequential Access Methods:: +* Sequential Index Operations:: +* Table Administration:: +@end menu -The @code{domain} field specifies the domain which a parameter or -parameters in the @code{index}th field must satisfy. -The @code{defaulter} field is an expression whose value is either -@code{#f} or a procedure of one argument (the parameter-list) which -returns a @emph{list} of the default value or values as appropriate. -Note that since the @code{defaulter} procedure is called every time a -default parameter is needed for this column, @dfn{sticky} defaults can -be implemented using shared state with the domain-integrity-rule. +@node Single Row Operations, Match-Keys, Table Operations, Table Operations +@subsubsection Single Row Operations -@subsubheading Invoking Commands +@noindent +The term @dfn{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 @code{#f}. Primary keys must not +be missing. -When an enhanced relational-database is called with a symbol which -matches a @var{name} in the @code{*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. +@defop {Operation} {relational-table} row:insert +Adds the row @var{row} to this table. If a row for the primary key(s) +specified by @var{row} already exists in this table an error is +signaled. The value returned is unspecified. +@end defop -The command @code{*initialize*} is special. If present in the +@example +@group +(define telephone-table-desc + ((my-database 'create-table) 'telephone-table-desc)) +(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)) +@end group +@end example + +@defop {Operation} {relational-table} row:update +Returns a procedure of one argument, @var{row}, which adds the row, +@var{row}, to this table. If a row for the primary key(s) specified by +@var{row} already exists in this table, it will be overwritten. The +value returned is unspecified. +@end defop + +@defop {Operation} {relational-table} row:retrieve +Returns a procedure of arguments @var{key1} @var{key2} @dots{} which +returns the row associated with primary keys @var{key1}, @var{key2} +@dots{} if it exists, or @code{#f} otherwise. +@end defop + +@example +((plat 'row:retrieve) 'linux) @result{} (linux i386 linux gcc) +((plat 'row:retrieve) 'multics) @result{} #f +@end example + +@defop {Operation} {relational-table} row:remove +Returns a procedure of arguments @var{key1} @var{key2} @dots{} which +removes and returns the row associated with primary keys @var{key1}, +@var{key2} @dots{} if it exists, or @code{#f} otherwise. +@end defop + +@defop {Operation} {relational-table} row:delete +Returns a procedure of arguments @var{key1} @var{key2} @dots{} which +deletes the row associated with primary keys @var{key1}, @var{key2} +@dots{} if it exists. The value returned is unspecified. +@end defop + + +@node Match-Keys, Multi-Row Operations, Single Row Operations, Table Operations +@subsubsection Match-Keys + +@noindent +@cindex match-keys +The (optional) @var{match-key1} @dots{} 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 @var{match-key} arguments +default to @code{#f}. + +@noindent +The @var{match-key1} @dots{} restrict the actions of the table command +to those records whose primary keys each satisfy the corresponding +@var{match-key} argument. The arguments and their actions are: + +@quotation +@table @asis +@item @code{#f} +The false value matches any key in the corresponding position. +@item 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 @code{#f} is not. +@item other values +Any other value matches only those keys @code{equal?} to it. +@end table +@end quotation + +@defop {Operation} {relational-table} get* column-name +Returns a procedure of optional arguments @var{match-key1} @dots{} which +returns a list of the values for the specified column for all rows in +this table. The optional @var{match-key1} @dots{} arguments restrict +actions to a subset of the table. + +@example +((plat 'get* 'processor)) @result{} +(i386 i8086 i386 i8086 i386 i386 i8086 m68000 + m68000 m68000 m68000 m68000 powerpc) + +((plat 'get* 'processor) #f) @result{} +(i386 i8086 i386 i8086 i386 i386 i8086 m68000 + m68000 m68000 m68000 m68000 powerpc) + +(define (a-key? key) + (char=? #\a (string-ref (symbol->string key) 0))) + +((plat 'get* 'processor) a-key?) @result{} +(m68000 m68000 m68000 m68000 m68000 powerpc) + +((plat 'get* 'name) a-key?) @result{} +(atari-st-turbo-c atari-st-gcc amiga-sas/c-5.10 + amiga-aztec amiga-dice-c aix) +@end example +@end defop + + +@node Multi-Row Operations, Indexed Sequential Access Methods, Match-Keys, Table Operations +@subsubsection Multi-Row Operations + +@defop {Operation} {relational-table} row:retrieve* +Returns a procedure of optional arguments @var{match-key1} @dots{} +which returns a list of all rows in this table. The optional +@var{match-key1} @dots{} arguments restrict actions to a subset of the +table. For details see @xref{Match-Keys}. +@end defop + +@example +((plat 'row:retrieve*) a-key?) @result{} +((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 -)) +@end example + +@defop {Operation} {relational-table} row:remove* +Returns a procedure of optional arguments @var{match-key1} @dots{} which +removes and returns a list of all rows in this table. The optional +@var{match-key1} @dots{} arguments restrict actions to a subset of the +table. +@end defop + +@defop {Operation} {relational-table} row:delete* +Returns a procedure of optional arguments @var{match-key1} @dots{} +which Deletes all rows from this table. The optional @var{match-key1} +@dots{} arguments restrict deletions to a subset of the table. The +value returned is unspecified. The descriptor table and catalog entry +for this table are not affected. +@end defop + +@defop {Operation} {relational-table} for-each-row +Returns a procedure of arguments @var{proc} @var{match-key1} @dots{} +which calls @var{proc} with each @var{row} in this table. The +optional @var{match-key1} @dots{} arguments restrict actions to a +subset of the table. For details see @xref{Match-Keys}. +@end defop + +@noindent +Note that @code{row:insert*} and @code{row:update*} do @emph{not} use +match-keys. + +@defop {Operation} {relational-table} row:insert* +Returns a procedure of one argument, @var{rows}, which adds each row in +the list of rows, @var{rows}, to this table. If a row for the primary +key specified by an element of @var{rows} already exists in this table, +an error is signaled. The value returned is unspecified. +@end defop + +@defop {Operation} {relational-table} row:update* +Returns a procedure of one argument, @var{rows}, which adds each row in +the list of rows, @var{rows}, to this table. If a row for the primary +key specified by an element of @var{rows} already exists in this table, +it will be overwritten. The value returned is unspecified. +@end defop + + +@node Indexed Sequential Access Methods, Sequential Index Operations, Multi-Row Operations, Table Operations +@subsubsection Indexed Sequential Access Methods + +@noindent +@cindex ISAM +@dfn{Indexed Sequential Access Methods} are a way of arranging +database information so that records can be accessed both by key and +by key sequence (ordering). @dfn{ISAM} is not part of Codd's +relational model. Hardcore relational programmers might use some +least-upper-bound join for every row to get them into an order. + +@noindent +Associative memory in B-Trees is an example of a database +implementation which can support a native key ordering. SLIB's +@code{alist-table} implementation uses @code{sort} to implement +@code{for-each-row-in-order}, but does not support @code{isam-next} +and @code{isam-prev}. + +@noindent +The multi-primary-key ordering employed by these operations is the +lexicographic collation of those primary-key fields in their given +order. For example: + +@example +(12 a 34) < (12 a 36) < (12 b 1) < (13 a 0) +@end example + + +@node Sequential Index Operations, Table Administration, Indexed Sequential Access Methods, Table Operations +@subsubsection Sequential Index Operations + +@noindent +The following procedures are individually optional depending on the +base-table implememtation. If an operation is @emph{not} supported, +then calling the table with that operation symbol will return false. + +@defop {Operation} {relational-table} for-each-row-in-order +Returns a procedure of arguments @var{proc} @var{match-key1} @dots{} +which calls @var{proc} with each @var{row} in this table in the +(implementation-dependent) natural, repeatable ordering for rows. The +optional @var{match-key1} @dots{} arguments restrict actions to a +subset of the table. For details see @xref{Match-Keys}. +@end defop + +@defop {Operation} {relational-table} isam-next +Returns a procedure of arguments @var{key1} @var{key2} @dots{} which +returns the key-list identifying the lowest record higher than +@var{key1} @var{key2} @dots{} which is stored in the relational-table; +or false if no higher record is present. + +@defopx {Operation} {relational-table} isam-next column-name +The symbol @var{column-name} names a key field. In the list returned +by @code{isam-next}, that field, or a field to its left, will be +changed. This allows one to skip over less significant key fields. +@end defop + +@defop {Operation} {relational-table} isam-prev +Returns a procedure of arguments @var{key1} @var{key2} @dots{} which +returns the key-list identifying the highest record less than +@var{key1} @var{key2} @dots{} which is stored in the relational-table; +or false if no lower record is present. + +@defopx {Operation} {relational-table} isam-prev index +The symbol @var{column-name} names a key field. In the list returned +by @code{isam-next}, that field, or a field to its left, will be +changed. This allows one to skip over less significant key fields. +@end defop + +For example, if a table has key fields: +@example +(col1 col2) +(9 5) +(9 6) +(9 7) +(9 8) +(12 5) +(12 6) +(12 7) +@end example + +Then: +@example +((table 'isam-next) '(9 5)) @result{} (9 6) +((table 'isam-next 'col2) '(9 5)) @result{} (9 6) +((table 'isam-next 'col1) '(9 5)) @result{} (12 5) +((table 'isam-prev) '(12 7)) @result{} (12 6) +((table 'isam-prev 'col2) '(12 7)) @result{} (12 6) +((table 'isam-prev 'col1) '(12 7)) @result{} (9 8) +@end example + + +@node Table Administration, , Sequential Index Operations, Table Operations +@subsubsection Table Administration + +@defop {Operation} {relational-table} column-names +@defopx {Operation} {relational-table} column-foreigns +@defopx {Operation} {relational-table} column-domains +@defopx {Operation} {relational-table} 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. + +@defopx {Operation} {relational-table} primary-limit +Returns the number of primary keys fields in the relations in this +table. +@end defop + +@defop {Operation} {relational-table} close-table +Subsequent operations to this table will signal an error. +@end defop + +@node Database Interpolation, Embedded Commands, Table Operations, Relational Database +@subsection Database Interpolation + +@code{(require 'database-interpolate)} + +@noindent +Indexed sequential access methods allow finding the keys (having +associations) closest to a given value. This facilitates the +interpolation of associations between those in the table. + +@defun interpolate-from-table table column +@var{Table} should be a relational table with one numeric primary key +field which supports the @code{isam-prev} and @code{isam-next} +operations. @var{column} should be a symbol or exact positive integer +designating a numerically valued column of @var{table}. + +@code{interpolate-from-table} calculates and returns a value +proportionally intermediate between its values in the next and +previous key records contained in @var{table}. For keys larger than +all the stored keys the value associated with the largest stored key +is used. For keys smaller than all the stored keys the value +associated with the smallest stored key is used. +@end defun + + + +@node Embedded Commands, Database Macros, Database Interpolation, Relational Database +@subsection Embedded Commands + +@code{(require 'database-commands)} + +@noindent +This enhancement wraps a utility layer on @code{relational-database} +which provides: + +@itemize @bullet +@item +Automatic execution of initialization commands stored in database. +@item +Transparent execution of database commands stored in @code{*commands*} +table in database. +@end itemize + +When an enhanced relational-database is called with a symbol which +matches a @var{name} in the @code{*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 @code{*initialize*} is special. If present in the @code{*commands*} table, @code{open-database} or @code{open-database!} will return the value of the @code{*initialize*} command. Notice that arbitrary code can be run when the @code{*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 @ref{Relational Database -Operations}, this can be done by a dispatch in the closure returned by -the @code{*initialize*} expression rather than by entries in the +relational-database methods described in @ref{Database Operations}, this +can be done by a dispatch in the closure returned by the +@code{*initialize*} expression rather than by entries in the @code{*commands*} table if it is desired that the underlying methods remain accessible to code in the @code{*commands*} table. -@defun 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 -@var{command} in table @var{table-name} and calls the call-back -procedure with arguments: -@table @var -@item command -The @var{command} -@item command-value -The result of evaluating the expression in the @var{procedure} field of -@var{table-name} and calling it with @var{rdb}. -@item parameter-name -A list of the @dfn{official} name of each parameter. Corresponds to the -@code{name} field of the @var{command}'s parameter-table. -@item positions -A list of the positive integer index of each parameter. Corresponds to -the @code{index} field of the @var{command}'s parameter-table. -@item arities -A list of the arities of each parameter. Corresponds to the -@code{arity} field of the @var{command}'s parameter-table. For a -description of @code{arity} see table above. -@item types -A list of the type name of each parameter. Correspnds to the -@code{type-id} field of the contents of the @code{domain} of the -@var{command}'s parameter-table. -@item defaulters -A list of the defaulters for each parameter. Corresponds to -the @code{defaulters} field of the @var{command}'s parameter-table. -@item 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 @code{nary} arity -parameters. -@item aliases -A list of lists of @code{(@r{alias} @r{parameter-name})}. There can be -more than one alias per @var{parameter-name}. -@end table + +@menu +* Database Extension:: +* Command Intrinsics:: +* Define-tables Example:: +* The *commands* Table:: +* Command Service:: +* Command Example:: +@end menu + + +@node Database Extension, Command Intrinsics, Embedded Commands, Embedded Commands +@subsubsection Database Extension + +@defun wrap-command-interface rdb +Returns relational database @var{rdb} wrapped with additional commands +defined in its *commands* table. @end defun -For information about parameters, @xref{Parameter lists}. Here is an -example of setting up a command with arguments and parsing those -arguments from a @code{getopt} style argument list (@pxref{Getopt}). +@defun add-command-tables rdb +The relational database @var{rdb} must be mutable. +@var{add-command-tables} adds a *command* table to @var{rdb}; then +returns @code{(wrap-command-interface @var{rdb})}. +@end defun + +@defun define-*commands* rdb spec-0 @dots{} + +Adds commands to the @code{*commands*} table as specified in +@var{spec-0} @dots{} to the open relational-database @var{rdb}. Each +@var{spec} has the form: + +@lisp +((@r{} @r{}) @r{"comment"} @r{} @r{} @dots{}) +@end lisp +or +@lisp +((@r{} @r{}) @r{} @r{} @dots{}) +@end lisp + +where @r{} is the command name, @r{} is a formal passed the +calling relational database, @r{"comment"} describes the +command, and @r{}, @r{}, @dots{} are the +body of the procedure. + +@code{define-*commands*} adds to the @code{*commands*} table a command +@r{}: + +@lisp +(lambda (@r{} @r{}) @r{} @r{} @dots{}) +@end lisp + +@end defun + + +@defun open-command-database filename +@defunx open-command-database filename base-table-type +Returns an open enhanced relational database associated with +@var{filename}. The database will be opened with base-table type +@var{base-table-type}) if supplied. If @var{base-table-type} is not +supplied, @code{open-command-database} will attempt to deduce the correct +base-table-type. If the database can not be opened or if it lacks the +@code{*commands*} table, @code{#f} is returned. + +@defunx open-command-database! filename +@defunx open-command-database! filename base-table-type +Returns @emph{mutable} open enhanced relational database @dots{} + +@defunx open-command-database database +Returns @var{database} if it is an immutable relational database; #f +otherwise. + +@defunx open-command-database! database +Returns @var{database} if it is a mutable relational database; #f +otherwise. +@end defun + + +@node Command Intrinsics, Define-tables Example, Database Extension, Embedded Commands +@subsubsection Command Intrinsics + +Some commands are defined in all extended relational-databases. The are +called just like @ref{Database Operations}. + +@defop {Operation} {relational-database} add-domain domain-row +Adds @var{domain-row} to the @dfn{domains} table if there is no row in +the domains table associated with key @code{(car @var{domain-row})} and +returns @code{#t}. Otherwise returns @code{#f}. + +For the fields and layout of the domain table, @xref{Catalog +Representation}. Currently, these fields are +@itemize @bullet +@item +domain-name +@item +foreign-table +@item +domain-integrity-rule +@item +type-id +@item +type-param +@end itemize + +The following example adds 3 domains to the @samp{build} database. +@samp{Optstring} is either a string or @code{#f}. @code{filename} is a +string and @code{build-whats} is a symbol. + +@example +(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))) +@end example +@end defop + +@defop {Operation} {relational-database} delete-domain domain-name +Removes and returns the @var{domain-name} row from the @dfn{domains} +table. +@end defop + +@defop {Operation} {relational-database} domain-checker domain +Returns a procedure to check an argument for conformance to domain +@var{domain}. +@end defop + + +@node Define-tables Example, The *commands* Table, Command Intrinsics, Embedded Commands +@subsubsection Define-tables Example + +@noindent +The following example shows a new database with the name of +@file{foo.db} being created with tables describing processor families +and processor/os/compiler combinations. The database is then +solidified; saved and changed to immutable. + +@example +(require 'databases) +@ftindex databases +(define my-rdb (create-database "foo.db" 'alist-table)) +(define-tables my-rdb + '(processor-family + ((family atom)) + ((also-ran processor-family)) + ((m68000 #f) + (m68030 m68000) + (i386 i8086) + (i8086 #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 i8086 ms-dos borland-c) + (djgpp i386 ms-dos gcc) + (linux i386 linux gcc) + (microsoft-c i8086 ms-dos microsoft-c) + (os/2-emx i386 os/2 gcc) + (turbo-c-2 i8086 ms-dos turbo-c) + (watcom-9.0 i386 ms-dos watcom)))) + +(solidify-database my-rdb) +@end example + + +@node The *commands* Table, Command Service, Define-tables Example, Embedded Commands +@subsubsection The *commands* Table + +@noindent +The table @code{*commands*} in an @dfn{enhanced} relational-database has +the fields (with domains): +@example +@group +PRI name symbol + parameters parameter-list + procedure expression + documentation string +@end group +@end example + +The @code{parameters} field is a foreign key (domain +@code{parameter-list}) of the @code{*catalog-data*} table and should +have the value of a table described by @code{*parameter-columns*}. This +@code{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 +@code{parameter-list} table has the following fields: +@example +@group +PRI index ordinal + name symbol + arity parameter-arity + domain domain + defaulter expression + expander expression + documentation string +@end group +@end example + +The @code{arity} field can take the values: + +@table @code +@item single +Requires a single parameter of the specified domain. +@item optional +A single parameter of the specified domain or zero parameters is +acceptable. +@item boolean +A single boolean parameter or zero parameters (in which case @code{#f} +is substituted) is acceptable. +@item 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. +@item 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. +@end table + +The @code{domain} field specifies the domain which a parameter or +parameters in the @code{index}th field must satisfy. + +The @code{defaulter} field is an expression whose value is either +@code{#f} or a procedure of one argument (the parameter-list) which +returns a @emph{list} of the default value or values as appropriate. +Note that since the @code{defaulter} procedure is called every time a +default parameter is needed for this column, @dfn{sticky} defaults can +be implemented using shared state with the domain-integrity-rule. + + +@node Command Service, Command Example, The *commands* Table, Embedded Commands +@subsubsection Command Service + +@defun 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 +@var{command} in table @var{table-name} and calls the call-back +procedure with arguments: +@table @var +@item command +The @var{command} +@item command-value +The result of evaluating the expression in the @var{procedure} field of +@var{table-name} and calling it with @var{rdb}. +@item parameter-name +A list of the @dfn{official} name of each parameter. Corresponds to the +@code{name} field of the @var{command}'s parameter-table. +@item positions +A list of the positive integer index of each parameter. Corresponds to +the @code{index} field of the @var{command}'s parameter-table. +@item arities +A list of the arities of each parameter. Corresponds to the +@code{arity} field of the @var{command}'s parameter-table. For a +description of @code{arity} see table above. +@item types +A list of the type name of each parameter. Correspnds to the +@code{type-id} field of the contents of the @code{domain} of the +@var{command}'s parameter-table. +@item defaulters +A list of the defaulters for each parameter. Corresponds to +the @code{defaulters} field of the @var{command}'s parameter-table. +@item 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 @code{nary} arity +parameters. +@item aliases +A list of lists of @code{(@r{alias} @r{parameter-name})}. There can be +more than one alias per @var{parameter-name}. +@end table +@end defun + +For information about parameters, @xref{Parameter lists}. + + +@node Command Example, , Command Service, Embedded Commands +@subsubsection Command Example + +Here is an example of setting up a command with arguments and parsing +those arguments from a @code{getopt} style argument list +(@pxref{Getopt}). + +@example +(require 'database-commands) +@ftindex database-commands +(require 'databases) +@ftindex databases +(require 'getopt-parameters) +@ftindex getopt-parameters +(require 'parameters) +@ftindex parameters +(require 'getopt) +@ftindex getopt +(require 'fluid-let) +(require 'printf) + +(define my-rdb (add-command-tables (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 ordinal + (lambda (pl) '()) #f "zero or one number") + (5 flag boolean boolean + (lambda (pl) '(#f)) #f "a boolean flag"))) + '(foo-pnames + ((name string)) + ((parameter-index ordinal)) + (("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 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 options positions + arities types defaulters dirs aliases))))) + +(define (cmd . opts) + (fluid-let ((*optind* 1)) + (printf "%-34s @result{} " + (call-with-output-string + (lambda (pt) (write (cons 'cmd opts) pt)))) + (set! opts (cons "cmd" opts)) + (force-output) + (dbutil:serve-command-line + my-rdb 'my-commands 'foo (length opts) opts))) + +(cmd) @result{} ("str" () (symb) () #f) +(cmd "-f") @result{} ("str" () (symb) () #t) +(cmd "--flag") @result{} ("str" () (symb) () #t) +(cmd "-o177") @result{} ("str" () (symb) (177) #f) +(cmd "-o" "177") @result{} ("str" () (symb) (177) #f) +(cmd "--optional" "621") @result{} ("str" () (symb) (621) #f) +(cmd "--optional=621") @result{} ("str" () (symb) (621) #f) +(cmd "-s" "speciality") @result{} ("speciality" () (symb) () #f) +(cmd "-sspeciality") @result{} ("speciality" () (symb) () #f) +(cmd "--single" "serendipity") @result{} ("serendipity" () (symb) () #f) +(cmd "--single=serendipity") @result{} ("serendipity" () (symb) () #f) +(cmd "-n" "gravity" "piety") @result{} ("str" () (piety gravity) () #f) +(cmd "-ngravity" "piety") @result{} ("str" () (piety gravity) () #f) +(cmd "--nary" "chastity") @result{} ("str" () (chastity) () #f) +(cmd "--nary=chastity" "") @result{} ("str" () ( chastity) () #f) +(cmd "-N" "calamity") @result{} ("str" () (calamity) () #f) +(cmd "-Ncalamity") @result{} ("str" () (calamity) () #f) +(cmd "--nary1" "surety") @result{} ("str" () (surety) () #f) +(cmd "--nary1=surety") @result{} ("str" () (surety) () #f) +(cmd "-N" "levity" "fealty") @result{} ("str" () (fealty levity) () #f) +(cmd "-Nlevity" "fealty") @result{} ("str" () (fealty levity) () #f) +(cmd "--nary1" "surety" "brevity") @result{} ("str" () (brevity surety) () #f) +(cmd "--nary1=surety" "brevity") @result{} ("str" () (brevity surety) () #f) +(cmd "-?") +@print{} +Usage: cmd [OPTION ARGUMENT ...] ... + + -f, --flag + -o, --optional[=] + -n, --nary[=] ... + -N, --nary1[=] ... + -s, --single[=] + +ERROR: getopt->parameter-list "unrecognized option" "-?" +@end example + + + +@node Database Macros, Database Browser, Embedded Commands, Relational Database +@subsection Database Macros + +@code{(require 'within-database)} + +The object-oriented programming interface to SLIB relational databases +has failed to support clear, understandable, and modular code-writing +for database applications. + +This seems to be a failure of the object-oriented paradigm where the +type of an object is not manifest (or even traceable) in source code. + +@code{within-database}, along with the @samp{databases} package, +reorganizes high-level database functions toward a more declarative +style. Using this package, one can tag database table and command +declarations for emacs: + +@example +etags -lscheme -r'/ *(define-\(command\|table\) (\([^; \t]+\)/\2/' \ + source1.scm ... +@end example + +@menu +* Within-database Example:: +@end menu + +@defun within-database database statement-1 @dots{} + +@code{within-database} creates a lexical scope in which the commands +@code{define-table} and @code{define-command} create tables and +@code{*commands*}-table entries respectively in open relational +database @var{database}. + +@code{within-database} Returns @var{database}. +@end defun + +@deffn Syntax define-command (@r{} @r{}) @r{"comment"} @r{} @r{} @dots{} +@deffnx Syntax define-command (@r{} @r{}) @r{} @r{} @dots{} + +Adds to the @code{*commands*} table a command +@r{}: + +@lisp +(lambda (@r{} @r{}) @r{} @r{} @dots{}) +@end lisp + +@end deffn + +@deffn Syntax define-table @r{} @r{} @r{} @r{} +@deffnx Syntax define-table @r{} @r{} @r{} @r{} + +where @r{} is the table name, @r{} is the symbol +name of a descriptor table, @r{} and +@r{} describe the primary keys and other fields +respectively, and @r{} is a list of data rows to be added to the +table. + +@r{} and @r{} are lists of field +descriptors of the form: + +@lisp +(@r{} @r{}) +@end lisp +or +@lisp +(@r{} @r{} @r{}) +@end lisp + +where @r{} is the column name, @r{} is the domain +of the column, and @r{} is an expression whose +value is a procedure of one argument (which returns @code{#f} to signal +an error). + +If @r{} is not a defined domain name and it matches the name of +this table or an already defined (in one of @var{spec-0} @dots{}) single +key field table, a foreign-key domain will be created for it. + +@end deffn + + +@node Within-database Example, , Database Macros, Database Macros +@subsubsection Within-database Example + +@noindent +Here is an example of @code{within-database} macros: + +@example +(require 'within-database) + +(define my-rdb + (add-command-tables + (create-database "foo.db" 'alist-table))) + +(within-database my-rdb + (define-command (*initialize* rdb) + "Print Welcome" + (display "Welcome") + (newline) + rdb) + (define-command (without-documentation rdb) + (display "without-documentation called") + (newline)) + (define-table (processor-family + ((family atom)) + ((also-ran processor-family))) + (m68000 #f) + (m68030 m68000) + (i386 i8086) + (i8086 #f) + (powerpc #f)) + (define-table (platform + ((name symbol)) + ((processor processor-family) + (os symbol) + (compiler symbol))) + (aix powerpc aix -) + ;; ... + (amiga-aztec m68000 amiga aztec) + (amiga-sas/c-5.10 m68000 amiga sas/c) + (atari-st-gcc m68000 atari gcc) + ;; ... + (watcom-9.0 i386 ms-dos watcom)) + (define-command (get-processor rdb) + "Get processor for given platform." + (((rdb 'open-table) 'platform #f) 'get 'processor))) + +(close-database my-rdb) + +(set! my-rdb (open-command-database! "foo.db")) +@print{} +Welcome + +(my-rdb 'without-documentation) +@print{} +without-documentation called + +((my-rdb 'get-processor) 'amiga-sas/c-5.10) +@result{} m68000 + +(close-database my-rdb) +@end example + + + +@node Database Browser, , Database Macros, Relational Database +@subsection Database Browser + +(require 'database-browse) + +@deffn {Procedure} browse database + +Prints the names of all the tables in @var{database} and sets browse's +default to @var{database}. + +@deffnx {Procedure} browse + +Prints the names of all the tables in the default database. + +@deffnx {Procedure} browse table-name + +For each record of the table named by the symbol @var{table-name}, +prints a line composed of all the field values. + +@deffnx {Procedure} browse pathname + +Opens the database named by the string @var{pathname}, prints the names +of all its tables, and sets browse's default to the database. + +@deffnx {Procedure} browse database table-name + +Sets browse's default to @var{database} and prints the records of the +table named by the symbol @var{table-name}. + +@deffnx {Procedure} browse pathname table-name + +Opens the database named by the string @var{pathname} and sets browse's +default to it; @code{browse} prints the records of the table named by +the symbol @var{table-name}. + +@end deffn + + +@node Relational Infrastructure, Weight-Balanced Trees, Relational Database, Database Packages +@section Relational Infrastructure + + +@menu +* Base Table:: +* Catalog Representation:: +* Relational Database Objects:: +* Database Operations:: +@end menu + + +@node Base Table, Catalog Representation, Relational Infrastructure, Relational Infrastructure +@subsection Base Table + +@cindex base-table +A @dfn{base-table} is the primitive database layer upon which SLIB +relational databases are built. At the minimum, it must support the +types integer, symbol, string, and boolean. The base-table may restrict +the size of integers, symbols, and strings it supports. + +A base table implementation is available as the value of the identifier +naming it (eg. @var{alist-table}) after requiring the symbol of that +name. + +@deftp {Feature} alist-table +@code{(require 'alist-table)} +@ftindex alist-table + +Association-list base tables support all Scheme types and are suitable +for small databases. In order to be retrieved after being written to a +file, the data stored should include only objects which are readable and +writeable in the Scheme implementation. + +The @dfn{alist-table} base-table implementation is included in the +SLIB distribution. +@end deftp + +@dfn{WB} is a B-tree database package with SCM interfaces. Being +disk-based, WB databases readily store and access hundreds of +megabytes of data. WB comes with two base-table embeddings. + +@deftp {Feature} wb-table +@code{(require 'wb-table)} +@ftindex wb-table + +@cindex WB +@code{wb-table} supports scheme expressions for keys and values whose +text representations are less than 255 characters in length. +@xref{wb-table, , , wb, WB}. +@end deftp + + +@deftp {Feature} rwb-isam +@code{(require 'rwb-isam)} +@ftindex rwb-isam + +@dfn{rwb-isam} is a sophisticated base-table implementation built on +WB and SCM which uses binary numerical formats for key and non-key +fields. It supports IEEE floating-point and fixed-precision integer +keys with the correct numerical collation order. +@end deftp + +This rest of this section documents the interface for a base table +implementation from which the @ref{Relational Database} package +constructs a Relational system. It will be of interest primarily to +those wishing to port or write new base-table implementations. + +@defvar *base-table-implementations* +To support automatic dispatch for @code{open-database}, each base-table +module adds an association to @var{*base-table-implementations*} when +loaded. This association is the list of the base-table symbol and the +value returned by @code{(make-relational-system @var{base-table})}. +@end defvar + +@menu +* The Base:: +* Base Tables:: +* Base Field Types:: +* Composite Keys:: +* Base Record Operations:: +* Match Keys:: +* Aggregate Base Operations:: +* Base ISAM Operations:: +@end menu + +@node The Base, Base Tables, Base Table, Base Table +@subsubsection The Base + +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 @code{#f} +otherwise. For example: + +@example +@group +(require 'alist-table) +@ftindex alist-table +@findex alist-table +(define my-base (alist-table 'make-base)) +my-base @result{} *a procedure* +(define foo (alist-table 'foo)) +foo @result{} #f +@end group +@end example + +@defop {Operation} {base-table} make-base filename key-dimension column-types +Returns a new, open, low-level database (collection of tables) +associated with @var{filename}. This returned database has an empty +table associated with @var{catalog-id}. The positive integer +@var{key-dimension} is the number of keys composed to make a +@var{primary-key} for the catalog table. The list of symbols +@var{column-types} describes the types of each column for that table. +If the database cannot be created as specified, @code{#f} is returned. + +Calling the @code{close-base} method on this database and possibly other +operations will cause @var{filename} to be written to. If +@var{filename} is @code{#f} a temporary, non-disk based database will be +created if such can be supported by the base table implelentation. +@end defop + +@defop {Operation} {base-table} open-base filename mutable +Returns an open low-level database associated with @var{filename}. If +@var{mutable} is @code{#t}, this database will have methods capable of +effecting change to the database. If @var{mutable} is @code{#f}, only +methods for inquiring the database will be available. If the database +cannot be opened as specified @code{#f} is returned. + +Calling the @code{close-base} (and possibly other) method on a +@var{mutable} database will cause @var{filename} to be written to. +@end defop + +@defop {Operation} {base-table} write-base lldb filename +Causes the low-level database @var{lldb} to be written to +@var{filename}. If the write is successful, also causes @var{lldb} to +henceforth be associated with @var{filename}. Calling the +@code{close-database} (and possibly other) method on @var{lldb} may +cause @var{filename} to be written to. If @var{filename} is @code{#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, @code{#t} is returned. +Otherwise, @code{#f} is returned. +@end defop + +@defop {Operation} {base-table} sync-base lldb +Causes the file associated with the low-level database @var{lldb} to be +updated to reflect its current state. If the associated filename is +@code{#f}, no action is taken and @code{#f} is returned. If this +operation completes successfully, @code{#t} is returned. Otherwise, +@code{#f} is returned. +@end defop + +@defop {Operation} {base-table} close-base lldb +Causes the low-level database @var{lldb} to be written to its associated +file (if any). If the write is successful, subsequent operations to +@var{lldb} will signal an error. If the operations complete +successfully, @code{#t} is returned. Otherwise, @code{#f} is returned. +@end defop + + +@node Base Tables, Base Field Types, The Base, Base Table +@subsubsection Base Tables + +@defop {Operation} {base-table} make-table lldb key-dimension column-types +Returns the ordinal @var{base-id} for a new base table, otherwise +returns @code{#f}. The base table can then be opened using +@code{(open-table @var{lldb} @var{base-id})}. The positive integer +@var{key-dimension} is the number of keys composed to make a +@var{primary-key} for this table. The list of symbols +@var{column-types} describes the types of each column. +@end defop + +@defop {Operation} {base-table} open-table lldb base-id key-dimension column-types +Returns a @var{handle} for an existing base table in the low-level +database @var{lldb} if that table exists and can be opened in the mode +indicated by @var{mutable}, otherwise returns @code{#f}. + +As with @code{make-table}, the positive integer @var{key-dimension} is +the number of keys composed to make a @var{primary-key} for this table. +The list of symbols @var{column-types} describes the types of each +column. +@end defop + +@defop {Operation} {base-table} kill-table lldb base-id key-dimension column-types +Returns @code{#t} if the base table associated with @var{base-id} was +removed from the low level database @var{lldb}, and @code{#f} otherwise. +@end defop + +@defop {Operation} {base-table} catalog-id +A constant @var{base-id} ordinal suitable for passing as a parameter to +@code{open-table}. @var{catalog-id} will be used as the base table for +the system catalog. +@end defop + + +@node Base Field Types, Composite Keys, Base Tables, Base Table +@subsubsection Base Field Types + +@defop {Operation} {base-table} supported-type? symbol +Returns @code{#t} if @var{symbol} names a type allowed as a column +value by the implementation, and @code{#f} otherwise. At a minimum, +an implementation must support the types @code{integer}, +@code{ordinal}, @code{symbol}, @code{string}, and @code{boolean}. +@end defop + +@defop {Operation} {base-table} supported-key-type? symbol +Returns @code{#t} if @var{symbol} names a type allowed as a key value +by the implementation, and @code{#f} otherwise. At a minimum, an +implementation must support the types @code{ordinal}, and +@code{symbol}. +@end defop + +@noindent +An @dfn{ordinal} is an exact positive integer. The other types are +standard Scheme. + + +@node Composite Keys, Base Record Operations, Base Field Types, Base Table +@subsubsection Composite Keys + +@defop {Operation} {base-table} make-keyifier-1 type +Returns a procedure which accepts a single argument which must be of +type @var{type}. This returned procedure returns an object suitable for +being a @var{key} argument in the functions whose descriptions follow. + +Any 2 arguments of the supported type passed to the returned function +which are not @code{equal?} must result in returned values which are not +@code{equal?}. +@end defop + +@defop {Operation} {base-table} make-list-keyifier key-dimension types +The list of symbols @var{types} must have at least @var{key-dimension} +elements. Returns a procedure which accepts a list of length +@var{key-dimension} and whose types must corresopond to the types named +by @var{types}. This returned procedure combines the elements of its +list argument into an object suitable for being a @var{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 +@code{equal?} must result in returned values which are not +@code{equal?}. +@end defop + +@defop {Operation} {base-table} make-key-extractor key-dimension types column-number +Returns a procedure which accepts objects produced by application of the +result of @code{(make-list-keyifier @var{key-dimension} @var{types})}. +This procedure returns a @var{key} which is @code{equal?} to the +@var{column-number}th element of the list which was passed to create +@var{composite-key}. The list @var{types} must have at least +@var{key-dimension} elements. +@end defop + +@defop {Operation} {base-table} make-key->list key-dimension types +Returns a procedure which accepts objects produced by application of +the result of @code{(make-list-keyifier @var{key-dimension} +@var{types})}. This procedure returns a list of @var{key}s which are +elementwise @code{equal?} to the list which was passed to create +@var{composite-key}. +@end defop + + +@node Base Record Operations, Match Keys, Composite Keys, Base Table +@subsubsection Base Record Operations + +@noindent +In the following functions, the @var{key} argument can always be assumed +to be the value returned by a call to a @emph{keyify} routine. -@example -(require 'database-utilities) -@ftindex database-utilities -(require 'fluid-let) -@ftindex fluid-let -(require 'parameters) -@ftindex parameters -(require 'getopt) -@ftindex getopt +@defop {Operation} {base-table} present? handle key +Returns a non-@code{#f} value if there is a row associated with +@var{key} in the table opened in @var{handle} and @code{#f} otherwise. +@end defop -(define my-rdb (create-database #f 'alist-table)) +@defop {Operation} {base-table} make-getter key-dimension types +Returns a procedure which takes arguments @var{handle} and @var{key}. +This procedure returns a list of the non-primary values of the relation +(in the base table opened in @var{handle}) whose primary key is +@var{key} if it exists, and @code{#f} otherwise. +@end defop -(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")))) +@noindent +@code{make-getter-1} is a new operation. The relational-database +module works with older base-table implementations by using +@code{make-getter}. -(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))))) +@defop {Operation} {base-table} make-getter-1 key-dimension types index +Returns a procedure which takes arguments @var{handle} and @var{key}. +This procedure returns the value of the @var{index}th field (in the +base table opened in @var{handle}) whose primary key is @var{key} if +it exists, and @code{#f} otherwise. -(define (cmd . opts) - (fluid-let ((*optind* 1)) - (printf "%-34s @result{} " - (call-with-output-string - (lambda (pt) (write (cons 'cmd opts) pt)))) - (set! opts (cons "cmd" opts)) - (force-output) - (dbutil:serve-command-line - my-rdb 'my-commands 'foo (length opts) opts))) +@var{index} must be larger than @var{key-dimension}. +@end defop -(cmd) @result{} ("str" () (symb) () #f) -(cmd "-f") @result{} ("str" () (symb) () #t) -(cmd "--flag") @result{} ("str" () (symb) () #t) -(cmd "-o177") @result{} ("str" () (symb) (177) #f) -(cmd "-o" "177") @result{} ("str" () (symb) (177) #f) -(cmd "--optional" "621") @result{} ("str" () (symb) (621) #f) -(cmd "--optional=621") @result{} ("str" () (symb) (621) #f) -(cmd "-s" "speciality") @result{} ("speciality" () (symb) () #f) -(cmd "-sspeciality") @result{} ("speciality" () (symb) () #f) -(cmd "--single" "serendipity") @result{} ("serendipity" () (symb) () #f) -(cmd "--single=serendipity") @result{} ("serendipity" () (symb) () #f) -(cmd "-n" "gravity" "piety") @result{} ("str" () (piety gravity) () #f) -(cmd "-ngravity" "piety") @result{} ("str" () (piety gravity) () #f) -(cmd "--nary" "chastity") @result{} ("str" () (chastity) () #f) -(cmd "--nary=chastity" "") @result{} ("str" () ( chastity) () #f) -(cmd "-N" "calamity") @result{} ("str" () (calamity) () #f) -(cmd "-Ncalamity") @result{} ("str" () (calamity) () #f) -(cmd "--nary1" "surety") @result{} ("str" () (surety) () #f) -(cmd "--nary1=surety") @result{} ("str" () (surety) () #f) -(cmd "-N" "levity" "fealty") @result{} ("str" () (fealty levity) () #f) -(cmd "-Nlevity" "fealty") @result{} ("str" () (fealty levity) () #f) -(cmd "--nary1" "surety" "brevity") @result{} ("str" () (brevity surety) () #f) -(cmd "--nary1=surety" "brevity") @result{} ("str" () (brevity surety) () #f) -(cmd "-?") -@print{} -Usage: cmd [OPTION ARGUMENT ...] ... +@defop {Operation} {base-table} make-putter key-dimension types +Returns a procedure which takes arguments @var{handle} and @var{key} and +@var{value-list}. This procedure associates the primary key @var{key} +with the values in @var{value-list} (in the base table opened in +@var{handle}) and returns an unspecified value. +@end defop - -f, --flag - -o, --optional[=] - -n, --nary[=] ... - -N, --nary1[=] ... - -s, --single[=] +@defop {Operation} {base-table} delete handle key +Removes the row associated with @var{key} from the table opened in +@var{handle}. An unspecified value is returned. +@end defop -ERROR: getopt->parameter-list "unrecognized option" "-?" -@end example -Some commands are defined in all extended relational-databases. The are -called just like @ref{Relational Database Operations}. +@node Match Keys, Aggregate Base Operations, Base Record Operations, Base Table +@subsubsection Match Keys -@defun add-domain domain-row -Adds @var{domain-row} to the @dfn{domains} table if there is no row in -the domains table associated with key @code{(car @var{domain-row})} and -returns @code{#t}. Otherwise returns @code{#f}. +@noindent +@cindex match-keys +@cindex match +@cindex wild-card +A @var{match-keys} argument is a list of length equal to +the number of primary keys. The @var{match-keys} restrict the actions +of the table command to those records whose primary keys all satisfy the +corresponding element of the @var{match-keys} list. The elements and +their actions are: -For the fields and layout of the domain table, @xref{Catalog -Representation}. Currently, these fields are -@itemize @bullet -@item -domain-name -@item -foreign-table -@item -domain-integrity-rule -@item -type-id -@item -type-param -@end itemize +@quotation +@table @asis +@item @code{#f} +The false value matches any key in the corresponding position. +@item 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 @code{#f} is not. +@item other values +Any other value matches only those keys @code{equal?} to it. +@end table +@end quotation -The following example adds 3 domains to the @samp{build} database. -@samp{Optstring} is either a string or @code{#f}. @code{filename} is a -string and @code{build-whats} is a symbol. -@example -(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))) -@end example -@end defun +@node Aggregate Base Operations, Base ISAM Operations, Match Keys, Base Table +@subsubsection Aggregate Base Operations -@defun delete-domain domain-name -Removes and returns the @var{domain-name} row from the @dfn{domains} -table. -@end defun +@noindent +The @var{key-dimension} and @var{column-types} arguments are needed to +decode the composite-keys for matching with @var{match-keys}. -@defun domain-checker domain -Returns a procedure to check an argument for conformance to domain -@var{domain}. -@end defun +@defop {Operation} {base-table} delete* handle key-dimension column-types match-keys +Removes all rows which satisfy @var{match-keys} from the table opened in +@var{handle}. An unspecified value is returned. +@end defop -@subsubheading Defining Tables +@defop {Operation} {base-table} for-each-key handle procedure key-dimension column-types match-keys +Calls @var{procedure} once with each @var{key} in the table opened in +@var{handle} which satisfy @var{match-keys} in an unspecified order. +An unspecified value is returned. +@end defop -@deffn Procedure define-tables rdb spec-0 @dots{} -Adds tables as specified in @var{spec-0} @dots{} to the open -relational-database @var{rdb}. Each @var{spec} has the form: +@defop {Operation} {base-table} map-key handle procedure key-dimension column-types match-keys +Returns a list of the values returned by calling @var{procedure} once +with each @var{key} in the table opened in @var{handle} which satisfy +@var{match-keys} in an unspecified order. +@end defop -@lisp -(@r{} @r{} @r{} @r{}) -@end lisp -or -@lisp -(@r{} @r{} @r{} @r{}) -@end lisp -where @r{} is the table name, @r{} is the symbol -name of a descriptor table, @r{} and -@r{} describe the primary keys and other fields -respectively, and @r{} is a list of data rows to be added to the -table. +@node Base ISAM Operations, , Aggregate Base Operations, Base Table +@subsubsection Base ISAM Operations -@r{} and @r{} are lists of field -descriptors of the form: +@noindent +These operations are optional for a Base-Table implementation. -@lisp -(@r{} @r{}) -@end lisp -or -@lisp -(@r{} @r{} @r{}) -@end lisp +@defop {Operation} {base-table} ordered-for-each-key handle procedure key-dimension column-types match-keys +Calls @var{procedure} once with each @var{key} in the table opened in +@var{handle} which satisfy @var{match-keys} in the natural order for +the types of the primary key fields of that table. An unspecified value +is returned. +@end defop -where @r{} is the column name, @r{} is the domain -of the column, and @r{} is an expression whose -value is a procedure of one argument (which returns @code{#f} to signal -an error). +@defop {Operation} {base-table} make-nexter handle key-dimension column-types index +Returns a procedure of arguments @var{key1} @var{key2} @dots{} which +returns the key-list identifying the lowest record higher than +@var{key1} @var{key2} @dots{} which is stored in the base-table and +which differs in column @var{index} or a lower indexed key; or false +if no higher record is present. +@end defop -If @r{} is not a defined domain name and it matches the name of -this table or an already defined (in one of @var{spec-0} @dots{}) single -key field table, a foriegn-key domain will be created for it. -@end deffn +@defop {Operation} {base-table} make-prever handle key-dimension column-types index +Returns a procedure of arguments @var{key1} @var{key2} @dots{} which +returns the key-list identifying the highest record less than +@var{key1} @var{key2} @dots{} which is stored in the base-table and +which differs in column @var{index} or a lower indexed key; or false +if no higher record is present. +@end defop + +@node Catalog Representation, Relational Database Objects, Base Table, Relational Infrastructure +@subsection Catalog Representation + +@noindent +Each database (in an implementation) has a @dfn{system catalog} which +describes all the user accessible tables in that database (including +itself). + +@noindent +The system catalog base table has the following fields. @code{PRI} +indicates a primary key for that table. + +@example +@group +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. +@end group +@end example + +@noindent +Descriptors for base tables (not views) are tables (pointed to by +system catalog). Descriptor (base) tables have the fields: + +@example +@group +PRI column-number sequential integers from 1 + primary-key? boolean TRUE for primary key components + column-name + column-integrity-rule + domain-name +@end group +@end example + +@noindent +A @dfn{primary key} is any column marked as @code{primary-key?} in the +corresponding descriptor table. All the @code{primary-key?} columns +must have lower column numbers than any non-@code{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. + +@noindent +A @dfn{domain} is a category describing the allowable values to occur in +a column. It is described by a (base) table with the fields: +@example +@group +PRI domain-name + foreign-table + domain-integrity-rule + type-id + type-param +@end group +@end example @noindent -The following example shows a new database with the name of -@file{foo.db} being created with tables describing processor families -and processor/os/compiler combinations. +The @dfn{type-id} field value is a symbol. This symbol may be used by +the underlying base table implementation in storing that field. @noindent -The database command @code{define-tables} is defined to call -@code{define-tables} with its arguments. The database is also -configured to print @samp{Welcome} when the database is opened. The -database is then closed and reopened. +If the @code{foreign-table} field is non-@code{#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 @var{type-param} (or +@code{#f}, if allowed). This package currently does not support +composite foreign-keys. +@noindent +The types for which support is planned are: @example -(require 'database-utilities) -@ftindex database-utilities -(define my-rdb (create-database "foo.db" 'alist-table)) +@group + atom + symbol + string [] + number [] + money + date-time + boolean -(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)) -@print{} -Welcome + foreign-key + expression + virtual +@end group @end example -@subsubheading Listing Tables - -@deffn Procedure list-table-definition rdb table-name -If symbol @var{table-name} exists in the open relational-database -@var{rdb}, then returns a list of the table-name, its primary key names -and domains, its other key names and domains, and the table's records -(as lists). Otherwise, returns #f. - -The list returned by @code{list-table-definition}, when passed as an -argument to @code{define-tables}, will recreate the table. -@end deffn - -@node Database Reports, Database Browser, Database Utilities, Relational Database -@subsection Database Reports +@node Relational Database Objects, Database Operations, Catalog Representation, Relational Infrastructure +@subsection Relational Database Objects @noindent -Code for generating database reports is in @file{report.scm}. After -writing it using @code{format}, I discovered that Common-Lisp -@code{format} is not useable for this application because there is no -mechanismm for truncating fields. @file{report.scm} needs to be -rewritten using @code{printf}. +This object-oriented interface is deprecated for typical database +applications; @ref{Using Databases} provides an application programmer +interface which is easier to understand and use. -@deffn Procedure create-report rdb destination report-name table -@deffnx Procedure create-report rdb destination report-name -The symbol @var{report-name} must be primary key in the table named -@code{*reports*} in the relational database @var{rdb}. -@var{destination} is a port, string, or symbol. If @var{destination} is -a: +@defun make-relational-system base-table-implementation -@table @asis -@item port -The table is created as ascii text and written to that port. -@item string -The table is created as ascii text and written to the file named by -@var{destination}. -@item symbol -@var{destination} is the primary key for a row in the table named *printers*. -@end table +Returns a procedure implementing a relational database using the +@var{base-table-implementation}. -The report is prepared as follows: +All of the operations of a base table implementation are accessed +through a procedure defined by @code{require}ing that implementation. +Similarly, all of the operations of the relational database +implementation are accessed through the procedure returned by +@code{make-relational-system}. For instance, a new relational database +could be created from the procedure returned by +@code{make-relational-system} by: -@itemize @bullet -@item -@code{Format} (@pxref{Format}) is called with the @code{header} field -and the (list of) @code{column-names} of the table. -@item -@code{Format} is called with the @code{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 -@code{minimum-break} left on this page and the number of lines for this -row is larger or equal to twice @code{minimum-break}. -@item -@code{Format} is called with the @code{footer} field and the (list of) -@code{column-names} of the table. The footer field should not output a -newline. -@item -A new page is output. -@item -This entire process repeats until all the rows are output. -@end itemize -@end deffn +@example +(require 'alist-table) +@ftindex 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")) +@end example +@end defun -Each row in the table *reports* has the fields: +@noindent +What follows are the descriptions of the methods available from +relational system returned by a call to @code{make-relational-system}. -@table @asis -@item name -The report name. -@item default-table -The table to report on if none is specified. -@item header, footer -A @code{format} string. At the beginning and end of each page -respectively, @code{format} is called with this string and the (list of) -column-names of this table. -@item reporter -A @code{format} string. For each row in the table, @code{format} is -called with this string and the row. -@item minimum-break -The minimum number of lines into which the report lines for a row can be -broken. Use @code{0} if a row's lines should not be broken over page -boundaries. -@end table +@defop {Operation} {relational-system} create-database filename -Each row in the table *printers* has the fields: +Returns an open, nearly empty relational database associated with +@var{filename}. The only tables defined are the system catalog and +domain table. Calling the @code{close-database} method on this database +and possibly other operations will cause @var{filename} to be written +to. If @var{filename} is @code{#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 +@code{#f} is returned. For the fields and layout of descriptor tables, +@ref{Catalog Representation} +@end defop -@table @asis -@item name -The printer name. -@item print-procedure -The procedure to call to actually print. -@end table +@defop {Operation} {relational-system} open-database filename mutable? +Returns an open relational database associated with @var{filename}. If +@var{mutable?} is @code{#t}, this database will have methods capable of +effecting change to the database. If @var{mutable?} is @code{#f}, only +methods for inquiring the database will be available. Calling the +@code{close-database} (and possibly other) method on a @var{mutable?} +database will cause @var{filename} to be written to. If the database +cannot be opened as specified @code{#f} is returned. +@end defop -@node Database Browser, , Database Reports, Relational Database -@subsection Database Browser +@node Database Operations, , Relational Database Objects, Relational Infrastructure +@subsection Database Operations -(require 'database-browse) +@noindent +This object-oriented interface is deprecated for typical database +applications; @ref{Using Databases} provides an application programmer +interface which is easier to understand and use. -@deffn Procedure browse database +@noindent +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: -Prints the names of all the tables in @var{database} and sets browse's -default to @var{database}. +@example +(define my-database + (create-alist-database "mydata.db")) +(define telephone-table-desc + ((my-database 'create-table) 'telephone-table-desc)) +@end example -@deffnx Procedure browse +@defop {Operation} {relational-database} 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, @code{#t} is returned. Otherwise, @code{#f} is returned. +@end defop -Prints the names of all the tables in the default database. +@defop {Operation} {relational-database} write-database filename +Causes the relational database to be written to @var{filename}. If the +write is successful, also causes the database to henceforth be +associated with @var{filename}. Calling the @code{close-database} (and +possibly other) method on this database will cause @var{filename} to be +written to. If @var{filename} is @code{#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, @code{#t} is returned. Otherwise, @code{#f} is +returned. +@end defop -@deffnx Procedure browse table-name +@defop {Operation} {relational-database} sync-database +Causes any pending updates to the database file to be written out. If +the operations completed successfully, @code{#t} is returned. +Otherwise, @code{#f} is returned. +@end defop -For each record of the table named by the symbol @var{table-name}, -prints a line composed of all the field values. +@defop {Operation} {relational-database} solidify-database +Causes any pending updates to the database file to be written out. If +the writes completed successfully, then the database is changed to be +immutable and @code{#t} is returned. Otherwise, @code{#f} is returned. +@end defop -@deffnx Procedure browse pathname +@defop {Operation} {relational-database} table-exists? table-name +Returns @code{#t} if @var{table-name} exists in the system catalog, +otherwise returns @code{#f}. +@end defop -Opens the database named by the string @var{pathname}, prints the names -of all its tables, and sets browse's default to the database. +@defop {Operation} {relational-database} open-table table-name mutable? +Returns a @dfn{methods} procedure for an existing relational table in +this database if it exists and can be opened in the mode indicated by +@var{mutable?}, otherwise returns @code{#f}. +@end defop -@deffnx Procedure browse database table-name +@noindent +These methods will be present only in mutable databases. -Sets browse's default to @var{database} and prints the records of the -table named by the symbol @var{table-name}. +@defop {Operation} {relational-database} delete-table table-name +Removes and returns the @var{table-name} row from the system catalog if +the table or view associated with @var{table-name} gets removed from the +database, and @code{#f} otherwise. +@end defop -@deffnx Procedure browse pathname table-name +@defop {Operation} {relational-database} 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 @code{#f}. For the fields and layout of descriptor tables, +@xref{Catalog Representation}. -Opens the database named by the string @var{pathname} and sets browse's -default to it; @code{browse} prints the records of the table named by -the symbol @var{table-name}. +@defopx {Operation} {relational-database} create-table table-name table-desc-name +Returns a methods procedure for a new (open) relational table with +columns as described by @var{table-desc-name}, otherwise returns +@code{#f}. +@end defop + +@defop {Operation} {relational-database} create-view ?? +@defopx {Operation} {relational-database} project-table ?? +@defopx {Operation} {relational-database} restrict-table ?? +@defopx {Operation} {relational-database} cart-prod-tables ?? +Not yet implemented. +@end defop -@end deffn -@node Weight-Balanced Trees, , Relational Database, Database Packages +@node Weight-Balanced Trees, , Relational Infrastructure, Database Packages @section Weight-Balanced Trees @code{(require 'wt-tree)} @@ -7013,7 +8213,7 @@ To use weight balanced trees, execute @example (load-option 'wt-tree) @end example -@findex load-option +@ftindex load-option @noindent once before calling any of the procedures defined here. @@ -7126,10 +8326,10 @@ trees. These operations are the usual tree operations for insertion, deletion and lookup, some predicates and a procedure for determining the number of associations in a tree. -@deffn {procedure+} wt-tree? object -Returns @code{#t} if @var{object} is a weight-balanced tree, otherwise -returns @code{#f}. -@end deffn +@c @deffn {procedure+} wt-tree? object +@c Returns @code{#t} if @var{object} is a weight-balanced tree, otherwise +@c returns @code{#f}. +@c @end deffn @deffn {procedure+} wt-tree/empty? wt-tree Returns @code{#t} if @var{wt-tree} contains no associations, otherwise @@ -7366,15 +8566,13 @@ number of associations in the tree. Indexing can be used to find the median and maximum keys in the tree as follows: +@end deffn @example -median: (wt-tree/index @var{wt-tree} - (quotient (wt-tree/size @var{wt-tree}) 2)) +median: (wt-tree/index @var{wt-tree} (quotient (wt-tree/size @var{wt-tree}) 2)) -maximum: (wt-tree/index @var{wt-tree} - (-1+ (wt-tree/size @var{wt-tree}))) +maximum: (wt-tree/index @var{wt-tree} (-1+ (wt-tree/size @var{wt-tree}))) @end example -@end deffn @deffn {procedure+} wt-tree/rank wt-tree key Determines the 0-based position of @var{key} in the sorted sequence of @@ -7432,314 +8630,92 @@ operation is equivalent to @end deffn -@node Other Packages, About SLIB, Database Packages, Top -@chapter Other Packages - -@menu -* Data Structures:: Various data structures. -* Sorting and Searching:: -* Procedures:: Miscellaneous utility procedures. -* Standards Support:: Support for Scheme Standards. -* Session Support:: REPL and Debugging. -* Extra-SLIB Packages:: -@end menu - - -@node Data Structures, Sorting and Searching, Other Packages, Other Packages -@section Data Structures - - - -@menu -* Arrays:: 'array -* Array Mapping:: 'array-for-each -* Association Lists:: 'alist -* Byte:: 'byte -* Portable Image Files:: 'pnm -* Collections:: 'collect -* Dynamic Data Type:: 'dynamic -* Hash Tables:: 'hash-table -* Hashing:: 'hash, 'sierpinski, 'soundex -* Object:: 'object -* Priority Queues:: 'priority-queue -* Queues:: 'queue -* Records:: 'record -@end menu - - - - -@node Arrays, Array Mapping, Data Structures, Data Structures -@subsection Arrays - -@include array.txi - - -@node Array Mapping, Association Lists, Arrays, Data Structures -@subsection Array Mapping - -@code{(require 'array-for-each)} -@ftindex array-for-each - -@defun array-map! array0 proc array1 @dots{} -@var{array1}, @dots{} must have the same number of dimensions as -@var{array0} and have a range for each index which includes the range -for the corresponding index in @var{array0}. @var{proc} is applied to -each tuple of elements of @var{array1} @dots{} and the result is stored -as the corresponding element in @var{array0}. The value returned is -unspecified. The order of application is unspecified. -@end defun - -@defun array-for-each @var{proc} @var{array0} @dots{} -@var{proc} is applied to each tuple of elements of @var{array0} @dots{} -in row-major order. The value returned is unspecified. -@end defun - -@defun array-indexes @var{array} -Returns an array of lists of indexes for @var{array} such that, if -@var{li} is a list of indexes for which @var{array} is defined, (equal? -@var{li} (apply array-ref (array-indexes @var{array}) @var{li})). -@end defun - -@defun array-index-map! array proc -applies @var{proc} to the indices of each element of @var{array} in -turn, storing the result in the corresponding element. The value -returned and the order of application are unspecified. - -One can implement @var{array-indexes} as -@example -(define (array-indexes array) - (let ((ra (apply make-array #f (array-shape array)))) - (array-index-map! ra (lambda x x)) - ra)) -@end example -Another example: -@example -(define (apl:index-generator n) - (let ((v (make-uniform-vector n 1))) - (array-index-map! v (lambda (i) i)) - v)) -@end example -@end defun - -@defun array-copy! source destination -Copies every element from vector or array @var{source} to the -corresponding element of @var{destination}. @var{destination} must have -the same rank as @var{source}, and be at least as large in each -dimension. The order of copying is unspecified. -@end defun - - -@node Association Lists, Byte, Array Mapping, Data Structures -@subsection Association Lists - -@code{(require 'alist)} -@ftindex alist - -Alist functions provide utilities for treating a list of key-value pairs -as an associative database. These functions take an equality predicate, -@var{pred}, as an argument. This predicate should be repeatable, -symmetric, and transitive. - -Alist functions can be used with a secondary index method such as hash -tables for improved performance. - -@defun predicate->asso pred -Returns an @dfn{association function} (like @code{assq}, @code{assv}, or -@code{assoc}) corresponding to @var{pred}. The returned function -returns a key-value pair whose key is @code{pred}-equal to its first -argument or @code{#f} if no key in the alist is @var{pred}-equal to the -first argument. -@end defun - -@defun alist-inquirer pred -Returns a procedure of 2 arguments, @var{alist} and @var{key}, which -returns the value associated with @var{key} in @var{alist} or @code{#f} if -@var{key} does not appear in @var{alist}. -@end defun - -@defun alist-associator pred -Returns a procedure of 3 arguments, @var{alist}, @var{key}, and -@var{value}, which returns an alist with @var{key} and @var{value} -associated. Any previous value associated with @var{key} will be -lost. This returned procedure may or may not have side effects on its -@var{alist} argument. An example of correct usage is:@refill -@lisp -(define put (alist-associator string-ci=?)) -(define alist '()) -(set! alist (put alist "Foo" 9)) -@end lisp -@end defun - -@defun alist-remover pred -Returns a procedure of 2 arguments, @var{alist} and @var{key}, which -returns an alist with an association whose @var{key} is key removed. -This returned procedure may or may not have side effects on its -@var{alist} argument. An example of correct usage is:@refill -@lisp -(define rem (alist-remover string-ci=?)) -(set! alist (rem alist "foo")) -@end lisp -@end defun - -@defun alist-map proc alist -Returns a new association list formed by mapping @var{proc} over the -keys and values of @var{alist}. @var{proc} must be a function of 2 -arguments which returns the new value part. -@end defun - -@defun alist-for-each proc alist -Applies @var{proc} to each pair of keys and values of @var{alist}. -@var{proc} must be a function of 2 arguments. The returned value is -unspecified. -@end defun - -@node Byte, Portable Image Files, Association Lists, Data Structures -@subsection Byte - -@code{(require 'byte)} - -Some algorithms are expressed in terms of arrays of small integers. -Using Scheme strings to implement these arrays is not portable vis-a-vis -the correspondence between integers and characters and non-ascii -character sets. These functions abstract the notion of a @dfn{byte}. -@cindex byte - -@deffn Function byte-ref bytes k -@var{k} must be a valid index of @var{bytes}. @code{byte-ref} returns -byte @var{k} of @var{bytes} using zero-origin indexing. -@findex byte-ref -@end deffn - -@deffn Procedure byte-set! bytes k byte -@var{k} must be a valid index of @var{bytes}%, and @var{byte} must be a -small integer. @code{Byte-set!} stores @var{byte} in element @var{k} -of @var{bytes} -@findex byte-set! -and returns an unspecified value. @c +@node Other Packages, About SLIB, Database Packages, Top +@chapter Other Packages -@end deffn +@menu +* Data Structures:: Various data structures. +* Sorting and Searching:: +* Procedures:: Miscellaneous utility procedures. +* Standards Support:: Support for Scheme Standards. +* Session Support:: REPL and Debugging. +* System Interface:: 'system, 'getenv, and other programs. +* Extra-SLIB Packages:: Outside the envelope. +@end menu -@deffn Function make-bytes k -@deffnx Function make-bytes k byte -@code{Make-bytes} returns a newly allocated byte-array of -@findex make-bytes -length @var{k}. If @var{byte} is given, then all elements of the -byte-array are initialized to @var{byte}, otherwise the contents of the -byte-array are unspecified. +@node Data Structures, Sorting and Searching, Other Packages, Other Packages +@section Data Structures -@end deffn -@deffn Function bytes-length bytes -@code{bytes-length} returns length of byte-array @var{bytes}. -@findex bytes-length +@menu +* Arrays:: 'array +* Subarrays:: 'subarray +* Array Mapping:: 'array-for-each +* Association Lists:: 'alist +* Byte:: 'byte +* Byte/Number Conversions:: 'byte-number +* MAT-File Format:: 'matfile +* Portable Image Files:: 'pnm +* Collections:: 'collect +* Dynamic Data Type:: 'dynamic +* Hash Tables:: 'hash-table +* Object:: 'object +* Priority Queues:: 'priority-queue +* Queues:: 'queue +* Records:: 'record +@end menu -@end deffn -@deffn Function write-byte byte -@deffnx Function write-byte byte port -Writes the byte @var{byte} (not an external representation of the -byte) to the given @var{port} and returns an unspecified value. The -@var{port} argument may be omitted, in which case it defaults to the value -returned by @code{current-output-port}. -@findex current-output-port -@end deffn +@node Arrays, Subarrays, Data Structures, Data Structures +@subsection Arrays -@deffn Function read-byte -@deffnx Function read-byte port +@include array.txi -Returns the next byte available from the input @var{port}, updating -the @var{port} to point to the following byte. If no more bytes -are available, an end of file object is returned. @var{Port} may be -omitted, in which case it defaults to the value returned by -@code{current-input-port}. -@findex current-input-port -@end deffn +@node Subarrays, Array Mapping, Arrays, Data Structures +@subsection Subarrays -@deffn Function bytes byte @dots{} +@include subarray.txi -Returns a newly allocated byte-array composed of the arguments. -@end deffn +@node Array Mapping, Association Lists, Subarrays, Data Structures +@subsection Array Mapping -@deffn Function bytes->list bytes -@deffnx Function list->bytes bytes +@include arraymap.txi -@code{Bytes->list} returns a newly allocated list of the -@findex bytes->list -bytes that make up the given byte-array. @code{List->bytes} -@findex list->bytes -returns a newly allocated byte-array formed from the small integers in -the list @var{bytes}. @code{Bytes->list} and @code{list->bytes} are -@findex list->bytes -@findex bytes->list -inverses so far as @code{equal?} is concerned. -@findex equal? -@end deffn +@node Association Lists, Byte, Array Mapping, Data Structures +@subsection Association Lists -@node Portable Image Files, Collections, Byte, Data Structures -@subsection Portable Image Files +@include alist.txi -@code{(require 'pnm)} -@deffn Function pnm:type-dimensions path -The string @var{path} must name a @dfn{portable bitmap graphics} file. -@code{pnm:type-dimensions} returns a list of 4 items: -@enumerate -@item -A symbol describing the type of the file named by @var{path}. -@item -The image width in pixels. -@item -The image height in pixels. -@item -The maximum value of pixels assume in the file. -@end enumerate +@node Byte, Byte/Number Conversions, Association Lists, Data Structures +@subsection Byte -The current set of file-type symbols is: -@table @asis -@item pbm -@itemx pbm-raw -Black-and-White image; pixel values are 0 or 1. -@item pgm -@itemx pgm-raw -Gray (monochrome) image; pixel values are from 0 to @var{maxval} -specified in file header. -@item ppm -@itemx ppm-raw -RGB (full color) image; red, green, and blue interleaved pixel values -are from 0 to @var{maxval} -@end table +@include byte.txi -@end deffn -@deffn Function pnm:image-file->array path array +@node Byte/Number Conversions, MAT-File Format, Byte, Data Structures +@subsection Byte/Number Conversions -Reads the @dfn{portable bitmap graphics} file named by @var{path} into -@var{array}. @var{array} must be the correct size and type for -@var{path}. @var{array} is returned. +@include bytenumb.txi -@deffnx Function pnm:image-file->array path -@code{pnm:image-file->array} creates and returns an array with the -@dfn{portable bitmap graphics} file named by @var{path} read into it. +@node MAT-File Format, Portable Image Files, Byte/Number Conversions, Data Structures +@subsection MAT-File Format -@end deffn +@include matfile.txi -@deffn Procedure pnm:array-write type array maxval path -Writes the contents of @var{array} to a @var{type} image file named -@var{path}. The file will have pixel values between 0 and @var{maxval}, -which must be compatible with @var{type}. For @samp{pbm} files, -@var{maxval} must be @samp{1}. +@node Portable Image Files, Collections, MAT-File Format, Data Structures +@subsection Portable Image Files -@end deffn +@include pnm.txi @node Collections, Dynamic Data Type, Portable Image Files, Data Structures @@ -7752,14 +8728,17 @@ which must be compatible with @var{type}. For @samp{pbm} files, @code{(require 'collect)} @ftindex collect +@noindent Routines for managing collections. Collections are aggregate data structures supporting iteration over their elements, similar to the Dylan(TM) language, but with a different interface. They have @dfn{elements} indexed by corresponding @dfn{keys}, although the keys may be implicit (as with lists). +@noindent New types of collections may be defined as YASOS objects (@pxref{Yasos}). They must support the following operations: + @itemize @bullet @item @code{(collection? @var{self})} (always returns @code{#t}); @@ -7770,28 +8749,33 @@ They must support the following operations: @item @code{(print @var{self} @var{port})} is a specialized print operation for the collection which prints a suitable representation on the given -@var{port} or returns it as a string if @var{port} is @code{#t};@refill +@var{port} or returns it as a string if @var{port} is @code{#t}; @item +@findex gen-elts @code{(gen-elts @var{self})} returns a thunk which on successive invocations yields elements of @var{self} in order or gives an error if -it is invoked more than @code{(size @var{self})} times;@refill +it is invoked more than @code{(size @var{self})} times; @item +@findex gen-keys @code{(gen-keys @var{self})} is like @code{gen-elts}, but yields the collection's keys in order. - @end itemize + +@noindent They might support specialized @code{for-each-key} and @code{for-each-elt} operations. + + @defun collection? obj A predicate, true initially of lists, vectors and strings. New sorts of collections must answer @code{#t} to @code{collection?}. @end defun -@deffn Procedure map-elts proc collection1 @dots{} -@deffnx Procedure do-elts proc collection1 @dots{} +@deffn {Procedure} map-elts proc collection1 @dots{} +@deffnx {Procedure} do-elts proc collection1 @dots{} @var{proc} is a procedure taking as many arguments as there are @var{collections} (at least one). The @var{collections} are iterated over in their natural order and @var{proc} is applied to the elements @@ -7809,8 +8793,8 @@ Example: @end lisp @end deffn -@deffn Procedure map-keys proc collection1 @dots{} -@deffnx Procedure do-keys proc collection1 @dots{} +@deffn {Procedure} map-keys proc collection1 @dots{} +@deffnx {Procedure} do-keys proc collection1 @dots{} These are analogous to @code{map-elts} and @code{do-elts}, but each iteration is over the @var{collections}' @emph{keys} rather than their elements. @@ -7822,14 +8806,14 @@ Example: @end lisp @end deffn -@deffn Procedure for-each-key collection proc -@deffnx Procedure for-each-elt collection proc +@deffn {Procedure} for-each-key collection proc +@deffnx {Procedure} for-each-elt collection proc These are like @code{do-keys} and @code{do-elts} but only for a single collection; they are potentially more efficient. @end deffn @defun reduce proc seed collection1 @dots{} -A generalization of the list-based @code{comlist:reduce-init} +A generalization of the list-based @code{reduce-init} (@pxref{Lists as sequences}) to collections which will shadow the list-based version if @code{(require 'collect)} follows @ftindex collect @@ -7858,8 +8842,8 @@ Example: @end defun @defun every? pred collection1 @dots{} -A generalization of the list-based @code{every} (@pxref{Lists as -sequences}) to collections. +A generalization of the list-based @code{every} +(@pxref{Lists as sequences}) to collections. Example: @lisp @@ -7934,354 +8918,87 @@ Here is a sample collection: @code{simple-table} which is also a (loop (cdr last) (cdr this))) ) ) ) )) - ;; collection behaviors - ((COLLECTION? self) #t) - ((GEN-KEYS self) (collect:list-gen-elts (map car table))) - ((GEN-ELTS self) (collect:list-gen-elts (map cdr table))) - ((FOR-EACH-KEY self proc) - (for-each (lambda (bucket) (proc (car bucket))) table) - ) - ((FOR-EACH-ELT self proc) - (for-each (lambda (bucket) (proc (cdr bucket))) table) - ) - ) ) ) -@end lisp - - - - - -@node Dynamic Data Type, Hash Tables, Collections, Data Structures -@subsection Dynamic Data Type - -@code{(require 'dynamic)} -@ftindex dynamic - -@defun make-dynamic obj -Create and returns a new @dfn{dynamic} whose global value is @var{obj}. -@end defun - -@defun dynamic? obj -Returns true if and only if @var{obj} is a dynamic. No object -satisfying @code{dynamic?} satisfies any of the other standard type -predicates. -@end defun - -@defun dynamic-ref dyn -Return the value of the given dynamic in the current dynamic -environment. -@end defun - -@deffn Procedure dynamic-set! dyn obj -Change the value of the given dynamic to @var{obj} in the current -dynamic environment. The returned value is unspecified. -@end deffn - -@defun call-with-dynamic-binding dyn obj thunk -Invoke and return the value of the given thunk in a new, nested dynamic -environment in which the given dynamic has been bound to a new location -whose initial contents are the value @var{obj}. This dynamic -environment has precisely the same extent as the invocation of the thunk -and is thus captured by continuations created within that invocation and -re-established by those continuations when they are invoked. -@end defun - -The @code{dynamic-bind} macro is not implemented. - - - - -@node Hash Tables, Hashing, Dynamic Data Type, Data Structures -@subsection Hash Tables - -@code{(require 'hash-table)} -@ftindex hash-table - -@defun predicate->hash pred -Returns a hash function (like @code{hashq}, @code{hashv}, or -@code{hash}) corresponding to the equality predicate @var{pred}. -@var{pred} should be @code{eq?}, @code{eqv?}, @code{equal?}, @code{=}, -@code{char=?}, @code{char-ci=?}, @code{string=?}, or -@code{string-ci=?}. -@end defun - -A hash table is a vector of association lists. - -@defun make-hash-table k -Returns a vector of @var{k} empty (association) lists. -@end defun - -Hash table functions provide utilities for an associative database. -These functions take an equality predicate, @var{pred}, as an argument. -@var{pred} should be @code{eq?}, @code{eqv?}, @code{equal?}, @code{=}, -@code{char=?}, @code{char-ci=?}, @code{string=?}, or -@code{string-ci=?}. - -@defun predicate->hash-asso pred -Returns a hash association function of 2 arguments, @var{key} and -@var{hashtab}, corresponding to @var{pred}. The returned function -returns a key-value pair whose key is @var{pred}-equal to its first -argument or @code{#f} if no key in @var{hashtab} is @var{pred}-equal to -the first argument. -@end defun - -@defun hash-inquirer pred -Returns a procedure of 2 arguments, @var{hashtab} and @var{key}, which -returns the value associated with @var{key} in @var{hashtab} or -@code{#f} if @var{key} does not appear in @var{hashtab}. -@end defun - -@defun hash-associator pred -Returns a procedure of 3 arguments, @var{hashtab}, @var{key}, and -@var{value}, which modifies @var{hashtab} so that @var{key} and -@var{value} associated. Any previous value associated with @var{key} -will be lost. -@end defun - -@defun hash-remover pred -Returns a procedure of 2 arguments, @var{hashtab} and @var{key}, which -modifies @var{hashtab} so that the association whose key is @var{key} is -removed. -@end defun - -@defun hash-map proc hash-table -Returns a new hash table formed by mapping @var{proc} over the -keys and values of @var{hash-table}. @var{proc} must be a function of 2 -arguments which returns the new value part. -@end defun - -@defun hash-for-each proc hash-table -Applies @var{proc} to each pair of keys and values of @var{hash-table}. -@var{proc} must be a function of 2 arguments. The returned value is -unspecified. -@end defun - - - - - -@node Hashing, Object, Hash Tables, Data Structures -@subsection Hashing - -@code{(require 'hash)} -@ftindex hash - -These hashing functions are for use in quickly classifying objects. -Hash tables use these functions. - -@defun hashq obj k -@defunx hashv obj k -@defunx hash obj k -Returns an exact non-negative integer less than @var{k}. For each -non-negative integer less than @var{k} there are arguments @var{obj} for -which the hashing functions applied to @var{obj} and @var{k} returns -that integer. - -For @code{hashq}, @code{(eq? obj1 obj2)} implies @code{(= (hashq obj1 k) -(hashq obj2))}. - -For @code{hashv}, @code{(eqv? obj1 obj2)} implies @code{(= (hashv obj1 k) -(hashv obj2))}. - -For @code{hash}, @code{(equal? obj1 obj2)} implies @code{(= (hash obj1 k) -(hash obj2))}. - -@code{hash}, @code{hashv}, and @code{hashq} return in time bounded by a -constant. Notice that items having the same @code{hash} implies the -items have the same @code{hashv} implies the items have the same -@code{hashq}. -@end defun - - -@code{(require 'sierpinski)} -@ftindex sierpinski - -@defun make-sierpinski-indexer max-coordinate -Returns a procedure (eg hash-function) of 2 numeric arguments which -preserves @emph{nearness} in its mapping from NxN to N. - -@var{max-coordinate} is the maximum coordinate (a positive integer) of a -population of points. The returned procedures is a function that takes -the x and y coordinates of a point, (non-negative integers) and returns -an integer corresponding to the relative position of that point along a -Sierpinski curve. (You can think of this as computing a (pseudo-) -inverse of the Sierpinski spacefilling curve.) - -Example use: Make an indexer (hash-function) for integer points lying in -square of integer grid points [0,99]x[0,99]: -@example -(define space-key (make-sierpinski-indexer 100)) -@end example -Now let's compute the index of some points: -@example -(space-key 24 78) @result{} 9206 -(space-key 23 80) @result{} 9172 -@end example - -Note that locations (24, 78) and (23, 80) are near in index and -therefore, because the Sierpinski spacefilling curve is continuous, we -know they must also be near in the plane. Nearness in the plane does -not, however, necessarily correspond to nearness in index, although it -@emph{tends} to be so. - -Example applications: -@itemize @bullet - -@item -Sort points by Sierpinski index to get heuristic solution to -@emph{travelling salesman problem}. For details of performance, -see L. Platzman and J. Bartholdi, "Spacefilling curves and the -Euclidean travelling salesman problem", JACM 36(4):719--737 -(October 1989) and references therein. - -@item -Use Sierpinski index as key by which to store 2-dimensional data -in a 1-dimensional data structure (such as a table). Then -locations that are near each other in 2-d space will tend to -be near each other in 1-d data structure; and locations that -are near in 1-d data structure will be near in 2-d space. This -can significantly speed retrieval from secondary storage because -contiguous regions in the plane will tend to correspond to -contiguous regions in secondary storage. (This is a standard -technique for managing CAD/CAM or geographic data.) - -@end itemize -@end defun - - - -@code{(require 'soundex)} -@ftindex soundex - -@defun soundex name -Computes the @emph{soundex} hash of @var{name}. Returns a string of an -initial letter and up to three digits between 0 and 6. Soundex -supposedly has the property that names that sound similar in normal -English pronunciation tend to map to the same key. - -Soundex was a classic algorithm used for manual filing of personal -records before the advent of computers. It performs adequately for -English names but has trouble with other languages. - -See Knuth, Vol. 3 @cite{Sorting and searching}, pp 391--2 - -To manage unusual inputs, @code{soundex} omits all non-alphabetic -characters. Consequently, in this implementation: - -@example -(soundex ) @result{} "" -(soundex "") @result{} "" -@end example +@group + ;; collection behaviors + ((COLLECTION? self) #t) + ((GEN-KEYS self) (collect:list-gen-elts (map car table))) + ((GEN-ELTS self) (collect:list-gen-elts (map cdr table))) + ((FOR-EACH-KEY self proc) + (for-each (lambda (bucket) (proc (car bucket))) table) + ) + ((FOR-EACH-ELT self proc) + (for-each (lambda (bucket) (proc (cdr bucket))) table) + ) ) ) ) +@end group +@end lisp -Examples from Knuth: -@example -(map soundex '("Euler" "Gauss" "Hilbert" "Knuth" - "Lloyd" "Lukasiewicz")) - @result{} ("E460" "G200" "H416" "K530" "L300" "L222") -(map soundex '("Ellery" "Ghosh" "Heilbronn" "Kant" - "Ladd" "Lissajous")) - @result{} ("E460" "G200" "H416" "K530" "L300" "L222") -@end example -Some cases in which the algorithm fails (Knuth): -@example -(map soundex '("Rogers" "Rodgers")) @result{} ("R262" "R326") +@node Dynamic Data Type, Hash Tables, Collections, Data Structures +@subsection Dynamic Data Type -(map soundex '("Sinclair" "St. Clair")) @result{} ("S524" "S324") +@code{(require 'dynamic)} +@ftindex dynamic -(map soundex '("Tchebysheff" "Chebyshev")) @result{} ("T212" "C121") -@end example +@defun make-dynamic obj +Create and returns a new @dfn{dynamic} whose global value is @var{obj}. @end defun - -@node Object, Priority Queues, Hashing, Data Structures -@subsection Macroless Object System -@include objdoc.txi - - -@node Priority Queues, Queues, Object, Data Structures -@subsection Priority Queues - -@code{(require 'priority-queue)} -@ftindex priority-queue - -@defun make-heap pred '(2 3) '(1 4)) +(some > '(1 3) '(2 4)) @result{} #f @end lisp @end defun @@ -8973,7 +9709,7 @@ given identical arugments. These procedures may mutate the list they operate on, but any such mutation is undefined. -@deffn Procedure nconc args +@deffn {Procedure} nconc args @code{nconc} destructively concatenates its arguments. (Compare this with @code{append}, which copies arguments rather than destroying them.) Sometimes called @code{append!} (@pxref{Rev2 Procedures}). @@ -8984,8 +9720,8 @@ Example: You want to find the subsets of a set. Here's the obvious way: (define (subsets set) (if (null? set) '(()) - (append (mapcar (lambda (sub) (cons (car set) sub)) - (subsets (cdr set))) + (append (map (lambda (sub) (cons (car set) sub)) + (subsets (cdr set))) (subsets (cdr set))))) @end lisp But that does way more consing than you need. Instead, you could @@ -9005,7 +9741,7 @@ x @code{nconc} is the same as @code{append!} in @file{sc2.scm}. @end deffn -@deffn Procedure nreverse lst +@deffn {Procedure} nreverse lst @code{nreverse} reverses the order of elements in @var{lst} by mutating @code{cdr}s of the list. Sometimes called @code{reverse!}. @@ -9020,7 +9756,8 @@ foo Some people have been confused about how to use @code{nreverse}, thinking that it doesn't return a value. It needs to be pointed out -that@refill +that + @lisp (set! lst (nreverse lst)) @end lisp @@ -9032,21 +9769,21 @@ is the proper usage, not The example should suffice to show why this is the case. @end deffn -@deffn Procedure delete elt lst -@deffnx Procedure delete-if pred lst -@deffnx Procedure delete-if-not pred lst +@deffn {Procedure} delete elt lst +@deffnx {Procedure} delete-if pred lst +@deffnx {Procedure} delete-if-not pred lst Destructive versions of @code{remove} @code{remove-if}, and @code{remove-if-not}. Example: @lisp -(define lst '(foo bar baz bang)) +(define lst (list 'foo 'bar 'baz 'bang)) (delete 'foo lst) @result{} (bar baz bang) lst @result{} (foo bar baz bang) -(define lst '(1 2 3 4 5 6 7 8 9)) +(define lst (list 1 2 3 4 5 6 7 8 9)) (delete-if odd? lst) @result{} (2 4 6 8) lst @@ -9054,8 +9791,9 @@ lst @end lisp Some people have been confused about how to use @code{delete}, -@code{delete-if}, and @code{delete-if}, thinking that they dont' return -a value. It needs to be pointed out that@refill +@code{delete-if}, and @code{delete-if}, thinking that they don't return +a value. It needs to be pointed out that + @lisp (set! lst (delete el lst)) @end lisp @@ -9076,7 +9814,7 @@ The examples should suffice to show why this is the case. @code{and?} checks to see if all its arguments are true. If they are, @code{and?} returns @code{#t}, otherwise, @code{#f}. (In contrast to @code{and}, this is a function, so all arguments are always evaluated -and in an unspecified order.)@refill +and in an unspecified order.) Example: @lisp @@ -9090,7 +9828,7 @@ Example: @defun or? arg1 @dots{} @code{or?} checks to see if any of its arguments are true. If any is true, @code{or?} returns @code{#t}, and @code{#f} otherwise. (To -@code{or} as @code{and?} is to @code{and}.)@refill +@code{or} as @code{and?} is to @code{and}.) Example: @lisp @@ -9118,96 +9856,13 @@ pair. (Called @code{atom} in Common LISP.) @node Tree Operations, Chapter Ordering, Common List Functions, Sorting and Searching @subsection Tree operations -@code{(require 'tree)} -@ftindex tree - -These are operations that treat lists a representations of trees. - -@defun subst new old tree -@defunx subst new old tree equ? -@defunx substq new old tree -@defunx substv new old tree -@code{subst} makes a copy of @var{tree}, substituting @var{new} for -every subtree or leaf of @var{tree} which is @code{equal?} to @var{old} -and returns a modified tree. The original @var{tree} is unchanged, but -may share parts with the result. - -@code{substq} and @code{substv} are similar, but test against @var{old} -using @code{eq?} and @code{eqv?} respectively. If @code{subst} is -called with a fourth argument, @var{equ?} is the equality predicate. - -Examples: -@lisp -(substq 'tempest 'hurricane '(shakespeare wrote (the hurricane))) - @result{} (shakespeare wrote (the tempest)) -(substq 'foo '() '(shakespeare wrote (twelfth night))) - @result{} (shakespeare wrote (twelfth night . foo) . foo) -(subst '(a . cons) '(old . pair) - '((old . spice) ((old . shoes) old . pair) (old . pair))) - @result{} ((old . spice) ((old . shoes) a . cons) (a . cons)) -@end lisp -@end defun - -@defun copy-tree tree -Makes a copy of the nested list structure @var{tree} using new pairs and -returns it. All levels are copied, so that none of the pairs in the -tree are @code{eq?} to the original ones -- only the leaves are. - -Example: -@lisp -(define bar '(bar)) -(copy-tree (list bar 'foo)) - @result{} ((bar) foo) -(eq? bar (car (copy-tree (list bar 'foo)))) - @result{} #f -@end lisp -@end defun +@include tree.txi @node Chapter Ordering, Sorting, Tree Operations, Sorting and Searching @subsection Chapter Ordering -@code{(require 'chapter-order)} -@ftindex chapter-order - -The @samp{chap:} functions deal with strings which are ordered like -chapter numbers (or letters) in a book. Each section of the string -consists of consecutive numeric or consecutive aphabetic characters of -like case. - -@defun chap:string? string1 string2 -@defunx chap:string<=? string1 string2 -@defunx chap:string>=? string1 string2 -Implement the corresponding chapter-order predicates. -@end defun - -@defun chap:next-string string -Returns the next string in the @emph{chapter order}. If @var{string} -has no alphabetic or numeric characters, -@code{(string-append @var{string} "0")} is returnd. The argument to -chap:next-string will always be @code{chap:string}, @code{char?}, +@code{char-ci?}, @code{string?}, +@code{string-ci?} are suitable for use as +comparison functions. Think of @code{(less? x y)} as saying when +@code{x} must @emph{not} precede @code{y}. + +@defun sorted? sequence less? +Returns @code{#t} when the sequence argument is in non-decreasing order +according to @var{less?} (that is, there is no adjacent pair @code{@dots{} x +y @dots{}} for which @code{(less? y x)}). + +Returns @code{#f} when the sequence contains at least one out-of-order +pair. It is an error if the sequence is not a list, vector, or +string. +@end defun + +@defun merge list1 list2 less? +This merges two lists, producing a completely new list as result. I +gave serious consideration to producing a Common-LISP-compatible +version. However, Common LISP's @code{sort} is our @code{sort!} (well, +in fact Common LISP's @code{stable-sort} is our @code{sort!}, merge sort +is @emph{fast} as well as stable!) so adapting CL code to Scheme takes a +bit of work anyway. I did, however, appeal to CL to determine the +@emph{order} of the arguments. +@end defun + +@deffn {Procedure} merge! list1 list2 less? +Merges two lists, re-using the pairs of @var{list1} and @var{list2} to +build the result. If the code is compiled, and @var{less?} constructs +no new pairs, no pairs at all will be allocated. The first pair of the +result will be either the first pair of @var{list1} or the first pair of +@var{list2}, but you can't predict which. + +The code of @code{merge} and @code{merge!} could have been quite a bit +simpler, but they have been coded to reduce the amount of work done per +iteration. (For example, we only have one @code{null?} test per +iteration.) + +@end deffn + +@defun sort sequence less? +Accepts either a list, vector, or string; and returns a new sequence +which is sorted. The new sequence is the same type as the input. +Always @code{(sorted? (sort sequence less?) less?)}. The original +sequence is not altered in any way. The new sequence shares its +@emph{elements} with the old one; no elements are copied. +@end defun + +@deffn {Procedure} sort! sequence less? +Returns its sorted result in the original boxes. If the original +sequence is a list, no new storage is allocated at all. If the +original sequence is a vector or string, the sorted elements are put +back in the same vector or string. + +Some people have been confused about how to use @code{sort!}, thinking +that it doesn't return a value. It needs to be pointed out that +@lisp +(set! slist (sort! slist <)) +@end lisp +@noindent +is the proper usage, not +@lisp +(sort! slist <) +@end lisp +@end deffn + +Note that these functions do @emph{not} accept a CL-style @samp{:key} +argument. A simple device for obtaining the same expressiveness is to +define + +@lisp +(define (keyed less? key) + (lambda (x y) (less? (key x) (key y)))) +@end lisp +@noindent +and then, when you would have written +@lisp +(sort a-sequence #'my-less :key #'my-key) +@end lisp +@noindent +in Common LISP, just write +@lisp +(sort! a-sequence (keyed my-less? my-key)) +@end lisp +@noindent +in Scheme. + +@node Topological Sort, Hashing, Sorting, Sorting and Searching +@subsection Topological Sort + +@include tsort.txi + + +@node Hashing, Space-Filling Curves, Topological Sort, Sorting and Searching +@subsection Hashing + +@code{(require 'hash)} +@ftindex hash + +These hashing functions are for use in quickly classifying objects. +Hash tables use these functions. + +@defun hashq obj k +@defunx hashv obj k +@defunx hash obj k +Returns an exact non-negative integer less than @var{k}. For each +non-negative integer less than @var{k} there are arguments @var{obj} for +which the hashing functions applied to @var{obj} and @var{k} returns +that integer. + +For @code{hashq}, @code{(eq? obj1 obj2)} implies @code{(= (hashq obj1 k) +(hashq obj2))}. + +For @code{hashv}, @code{(eqv? obj1 obj2)} implies @code{(= (hashv obj1 k) +(hashv obj2))}. + +For @code{hash}, @code{(equal? obj1 obj2)} implies @code{(= (hash obj1 k) +(hash obj2))}. + +@code{hash}, @code{hashv}, and @code{hashq} return in time bounded by a +constant. Notice that items having the same @code{hash} implies the +items have the same @code{hashv} implies the items have the same +@code{hashq}. +@end defun + + +@node Space-Filling Curves, Soundex, Hashing, Sorting and Searching +@subsection Space-Filling Curves + +@menu +* Peano-Hilbert Space-Filling Curve:: +* Sierpinski Curve:: +@end menu + +@node Peano-Hilbert Space-Filling Curve, Sierpinski Curve, Space-Filling Curves, Space-Filling Curves +@subsubsection Peano-Hilbert Space-Filling Curve + +@include phil-spc.txi + + +@node Sierpinski Curve, , Peano-Hilbert Space-Filling Curve, Space-Filling Curves +@subsubsection Sierpinski Curve + +@code{(require 'sierpinski)} +@ftindex sierpinski + +@defun make-sierpinski-indexer max-coordinate +Returns a procedure (eg hash-function) of 2 numeric arguments which +preserves @emph{nearness} in its mapping from NxN to N. + +@var{max-coordinate} is the maximum coordinate (a positive integer) of a +population of points. The returned procedures is a function that takes +the x and y coordinates of a point, (non-negative integers) and returns +an integer corresponding to the relative position of that point along a +Sierpinski curve. (You can think of this as computing a (pseudo-) +inverse of the Sierpinski spacefilling curve.) + +Example use: Make an indexer (hash-function) for integer points lying in +square of integer grid points [0,99]x[0,99]: +@example +(define space-key (make-sierpinski-indexer 100)) +@end example +Now let's compute the index of some points: +@example +(space-key 24 78) @result{} 9206 +(space-key 23 80) @result{} 9172 +@end example -@lisp -(not (f x x)) -(and (f x y) (f y z)) @equiv{} (f x z) -@end lisp +Note that locations (24, 78) and (23, 80) are near in index and +therefore, because the Sierpinski spacefilling curve is continuous, we +know they must also be near in the plane. Nearness in the plane does +not, however, necessarily correspond to nearness in index, although it +@emph{tends} to be so. -The standard functions @code{<}, @code{>}, @code{char?}, -@code{char-ci?}, @code{string?}, -@code{string-ci?} are suitable for use as -comparison functions. Think of @code{(less? x y)} as saying when -@code{x} must @emph{not} precede @code{y}. +Example applications: +@itemize @bullet -@defun sorted? sequence less? -Returns @code{#t} when the sequence argument is in non-decreasing order -according to @var{less?} (that is, there is no adjacent pair @code{@dots{} x -y @dots{}} for which @code{(less? y x)}). +@item +Sort points by Sierpinski index to get heuristic solution to +@emph{travelling salesman problem}. For details of performance, +see L. Platzman and J. Bartholdi, "Spacefilling curves and the +Euclidean travelling salesman problem", JACM 36(4):719--737 +(October 1989) and references therein. -Returns @code{#f} when the sequence contains at least one out-of-order -pair. It is an error if the sequence is neither a list nor a vector. -@end defun +@item +Use Sierpinski index as key by which to store 2-dimensional data +in a 1-dimensional data structure (such as a table). Then +locations that are near each other in 2-d space will tend to +be near each other in 1-d data structure; and locations that +are near in 1-d data structure will be near in 2-d space. This +can significantly speed retrieval from secondary storage because +contiguous regions in the plane will tend to correspond to +contiguous regions in secondary storage. (This is a standard +technique for managing CAD/CAM or geographic data.) -@defun merge list1 list2 less? -This merges two lists, producing a completely new list as result. I -gave serious consideration to producing a Common-LISP-compatible -version. However, Common LISP's @code{sort} is our @code{sort!} (well, -in fact Common LISP's @code{stable-sort} is our @code{sort!}, merge sort -is @emph{fast} as well as stable!) so adapting CL code to Scheme takes a -bit of work anyway. I did, however, appeal to CL to determine the -@emph{order} of the arguments. +@end itemize @end defun -@deffn Procedure merge! list1 list2 less? -Merges two lists, re-using the pairs of @var{list1} and @var{list2} to -build the result. If the code is compiled, and @var{less?} constructs -no new pairs, no pairs at all will be allocated. The first pair of the -result will be either the first pair of @var{list1} or the first pair of -@var{list2}, but you can't predict which. - -The code of @code{merge} and @code{merge!} could have been quite a bit -simpler, but they have been coded to reduce the amount of work done per -iteration. (For example, we only have one @code{null?} test per -iteration.)@refill -@end deffn -@defun sort sequence less? -Accepts either a list or a vector, and returns a new sequence which is -sorted. The new sequence is the same type as the input. Always -@code{(sorted? (sort sequence less?) less?)}. The original sequence is -not altered in any way. The new sequence shares its @emph{elements} -with the old one; no elements are copied. -@end defun +@node Soundex, String Search, Space-Filling Curves, Sorting and Searching +@subsection Soundex -@deffn Procedure sort! sequence less? -Returns its sorted result in the original boxes. If the original -sequence is a list, no new storage is allocated at all. If the original -sequence is a vector, the sorted elements are put back in the same -vector. +@code{(require 'soundex)} +@ftindex soundex -Some people have been confused about how to use @code{sort!}, thinking -that it doesn't return a value. It needs to be pointed out that -@lisp -(set! slist (sort! slist <)) -@end lisp -@noindent -is the proper usage, not -@lisp -(sort! slist <) -@end lisp -@end deffn +@defun soundex name +Computes the @emph{soundex} hash of @var{name}. Returns a string of an +initial letter and up to three digits between 0 and 6. Soundex +supposedly has the property that names that sound similar in normal +English pronunciation tend to map to the same key. -Note that these functions do @emph{not} accept a CL-style @samp{:key} -argument. A simple device for obtaining the same expressiveness is to -define@refill -@lisp -(define (keyed less? key) - (lambda (x y) (less? (key x) (key y)))) -@end lisp -@noindent -and then, when you would have written -@lisp -(sort a-sequence #'my-less :key #'my-key) -@end lisp -@noindent -in Common LISP, just write -@lisp -(sort! a-sequence (keyed my-less? my-key)) -@end lisp -@noindent -in Scheme. +Soundex was a classic algorithm used for manual filing of personal +records before the advent of computers. It performs adequately for +English names but has trouble with other languages. -@node Topological Sort, String Search, Sorting, Sorting and Searching -@subsection Topological Sort +See Knuth, Vol. 3 @cite{Sorting and searching}, pp 391--2 -@code{(require 'topological-sort)} or @code{(require 'tsort)} -@ftindex topological-sort -@ftindex tsort +To manage unusual inputs, @code{soundex} omits all non-alphabetic +characters. Consequently, in this implementation: -@noindent -The algorithm is inspired by Cormen, Leiserson and Rivest (1990) -@cite{Introduction to Algorithms}, chapter 23. +@example +(soundex ) @result{} "" +(soundex "") @result{} "" +@end example -@defun tsort dag pred -@defunx topological-sort dag pred -where -@table @var -@item dag -is a list of sublists. The car of each sublist is a vertex. The cdr is -the adjacency list of that vertex, i.e. a list of all vertices to which -there exists an edge from the car vertex. -@item pred -is one of @code{eq?}, @code{eqv?}, @code{equal?}, @code{=}, -@code{char=?}, @code{char-ci=?}, @code{string=?}, or @code{string-ci=?}. -@end table +Examples from Knuth: -Sort the directed acyclic graph @var{dag} so that for every edge from -vertex @var{u} to @var{v}, @var{u} will come before @var{v} in the -resulting list of vertices. +@example +(map soundex '("Euler" "Gauss" "Hilbert" "Knuth" + "Lloyd" "Lukasiewicz")) + @result{} ("E460" "G200" "H416" "K530" "L300" "L222") -Time complexity: O (|V| + |E|) +(map soundex '("Ellery" "Ghosh" "Heilbronn" "Kant" + "Ladd" "Lissajous")) + @result{} ("E460" "G200" "H416" "K530" "L300" "L222") +@end example -Example (from Cormen): -@quotation -Prof. Bumstead topologically sorts his clothing when getting -dressed. The first argument to `tsort' describes which -garments he needs to put on before others. (For example, -Prof Bumstead needs to put on his shirt before he puts on his -tie or his belt.) `tsort' gives the correct order of dressing: -@end quotation +Some cases in which the algorithm fails (Knuth): @example -(require 'tsort) -@ftindex tsort -(tsort '((shirt tie belt) - (tie jacket) - (belt jacket) - (watch) - (pants shoes belt) - (undershorts pants shoes) - (socks shoes)) - eq?) -@result{} -(socks undershorts pants shoes watch shirt belt tie jacket) +(map soundex '("Rogers" "Rodgers")) @result{} ("R262" "R326") + +(map soundex '("Sinclair" "St. Clair")) @result{} ("S524" "S324") + +(map soundex '("Tchebysheff" "Chebyshev")) @result{} ("T212" "C121") @end example @end defun -@node String Search, Sequence Comparison, Topological Sort, Sorting and Searching +@node String Search, Sequence Comparison, Soundex, Sorting and Searching @subsection String Search @code{(require 'string-search)} @ftindex string-search -@deffn Procedure string-index string char -@deffnx Procedure string-index-ci string char +@deffn {Procedure} string-index string char +@deffnx {Procedure} string-index-ci string char Returns the index of the first occurence of @var{char} within @var{string}, or @code{#f} if the @var{string} does not contain a character @var{char}. @end deffn -@deffn Procedure string-reverse-index string char -@deffnx Procedure string-reverse-index-ci string char +@deffn {Procedure} string-reverse-index string char +@deffnx {Procedure} string-reverse-index-ci string char Returns the index of the last occurence of @var{char} within @var{string}, or @code{#f} if the @var{string} does not contain a character @var{char}. @end deffn -@deffn procedure substring? pattern string -@deffnx procedure substring-ci? pattern string +@deffn {Procedure} substring? pattern string +@deffnx {Procedure} substring-ci? pattern string Searches @var{string} to see if some substring of @var{string} is equal to @var{pattern}. @code{substring?} returns the index of the first character of the first substring of @var{string} that is equal to @@ -9480,19 +10244,19 @@ character of the first substring of @var{string} that is equal to @end example @end deffn -@deffn Procedure find-string-from-port? str in-port max-no-chars +@deffn {Procedure} find-string-from-port? str in-port max-no-chars Looks for a string @var{str} within the first @var{max-no-chars} chars of the input port @var{in-port}. -@deffnx Procedure find-string-from-port? str in-port +@deffnx {Procedure} find-string-from-port? str in-port When called with two arguments, the search span is limited by the end of the input stream. -@deffnx Procedure find-string-from-port? str in-port char +@deffnx {Procedure} find-string-from-port? str in-port char Searches up to the first occurrence of character @var{char} in @var{str}. -@deffnx Procedure find-string-from-port? str in-port proc +@deffnx {Procedure} find-string-from-port? str in-port proc Searches up to the first occurrence of the procedure @var{proc} returning non-false when called with a character (from @var{in-port}) argument. @@ -9510,8 +10274,13 @@ open to a pipe or other communication channel. @defun string-subst txt old1 new1 @dots{} Returns a copy of string @var{txt} with all occurrences of string -@var{old1} in @var{txt} replaced with @var{new1}, @var{old2} replaced -with @var{new2} @dots{}. +@var{old1} in @var{txt} replaced with @var{new1}; then @var{old2} +replaced with @var{new2} @dots{}. Matches are found from the left. +Matches do not overlap. +@end defun + +@defun count-newlines str +Returns the number of @samp{#\newline} characters in string @var{str}. @end defun @@ -9555,15 +10324,15 @@ up here. @code{(require 'string-case)} @ftindex string-case -@deffn Procedure string-upcase str -@deffnx Procedure string-downcase str -@deffnx Procedure string-capitalize str +@deffn {Procedure} string-upcase str +@deffnx {Procedure} string-downcase str +@deffnx {Procedure} string-capitalize str The obvious string conversion routines. These are non-destructive. @end deffn @defun string-upcase! str @defunx string-downcase! str -@defunx string-captialize! str +@defunx string-capitalize! str The destructive versions of the functions above. @end defun @@ -9579,6 +10348,29 @@ symbol case; the case of symbol characters is not changed. #f is converted to the empty string (symbol). @end defun +@defun StudlyCapsExpand str delimiter +@defunx StudlyCapsExpand str +@var{delimiter} must be a string or character. If absent, +@var{delimiter} defaults to @samp{-}. @code{StudlyCapsExpand} returns a +copy of @var{str} where @var{delimiter} is inserted between each +lower-case character immediately followed by an upper-case character; +and between two upper-case characters immediately followed by a +lower-case character. + +@example +(StudlyCapsExpand "aX" " ") @result{} "a X" +(StudlyCapsExpand "aX" "..") @result{} "a..X" +(StudlyCapsExpand "AX") @result{} "AX" +(StudlyCapsExpand "Ax") @result{} "Ax" +(StudlyCapsExpand "AXLE") @result{} "AXLE" +(StudlyCapsExpand "aAXACz") @result{} "a-AXA-Cz" +(StudlyCapsExpand "AaXACz") @result{} "Aa-XA-Cz" +(StudlyCapsExpand "AAaXACz") @result{} "A-Aa-XA-Cz" +(StudlyCapsExpand "AAaXAC") @result{} "A-Aa-XAC" +@end example + +@end defun + @node String Ports, Line I/O, String-Case, Procedures @@ -9587,14 +10379,14 @@ converted to the empty string (symbol). @code{(require 'string-port)} @ftindex string-port -@deffn Procedure call-with-output-string proc +@deffn {Procedure} call-with-output-string proc @var{proc} must be a procedure of one argument. This procedure calls @var{proc} with one argument: a (newly created) output port. When the function returns, the string composed of the characters written into the port is returned. @end deffn -@deffn Procedure call-with-input-string string proc +@deffn {Procedure} call-with-input-string string proc @var{proc} must be a procedure of one argument. This procedure calls @var{proc} with one argument: an (newly created) input port from which @var{string}'s contents may be read. When @var{proc} returns, the port @@ -9621,25 +10413,25 @@ returned. This module implements asynchronous (non-polled) time-sliced multi-processing in the SCM Scheme implementation using procedures @code{alarm} and @code{alarm-interrupt}. -@findex alarm -@findex alarm-interrupt +@cindex alarm +@cindex alarm-interrupt Until this is ported to another implementation, consider it an example of writing schedulers in Scheme. -@deffn Procedure add-process! proc +@deffn {Procedure} add-process! proc Adds proc, which must be a procedure (or continuation) capable of accepting accepting one argument, to the @code{process:queue}. The value returned is unspecified. The argument to @var{proc} should be ignored. If @var{proc} returns, the process is killed. @end deffn -@deffn Procedure process:schedule! +@deffn {Procedure} process:schedule! Saves the current process on @code{process:queue} and runs the next process from @code{process:queue}. The value returned is unspecified. @end deffn -@deffn Procedure kill-process! +@deffn {Procedure} kill-process! Kills the current process and runs the next process from @code{process:queue}. If there are no more processes on @code{process:queue}, @code{(slib:exit)} is called (@pxref{System}). @@ -9724,7 +10516,7 @@ A unit exponent follows the unit, separated by a CIRCUMFLEX (@samp{^}). Exponents may be positive or negative. Fractional exponents must be parenthesized. -@subsubheading SI Prefixes +@subsubsection SI Prefixes @example Factor Name Symbol | Factor Name Symbol ====== ==== ====== | ====== ==== ====== @@ -9740,7 +10532,7 @@ parenthesized. 1e1 deka da | 1e-24 yocto y @end example -@subsubheading Binary Prefixes +@subsubsection Binary Prefixes These binary prefixes are valid only with the units B (byte) and bit. However, decimal prefixes can also be used with bit; and decimal @@ -9757,7 +10549,7 @@ multiple (not submultiple) prefixes can also be used with B (byte). 1.024e3 (2^10) kibi Ki @end example -@subsubheading Unit Symbols +@subsubsection Unit Symbols @example Type of Quantity Name Symbol Equivalent @@ -9861,6 +10653,7 @@ if linear conversion (by a factor) is not possible. @menu +* RnRS:: Revised Reports on Scheme * With-File:: 'with-file * Transcripts:: 'transcript * Rev2 Procedures:: 'rev2-procedures @@ -9868,14 +10661,54 @@ if linear conversion (by a factor) is not possible. * Multi-argument / and -:: 'multiarg/and- * Multi-argument Apply:: 'multiarg-apply * Rationalize:: 'rationalize -* Promises:: 'promise +* Promises:: 'delay * Dynamic-Wind:: 'dynamic-wind * Eval:: 'eval * Values:: 'values * SRFI:: 'http://srfi.schemers.org/srfi-0/srfi-0.html @end menu -@node With-File, Transcripts, Standards Support, Standards Support +@node RnRS, With-File, Standards Support, Standards Support +@subsection RnRS + +@noindent +The @code{r2rs}, @code{r3rs}, @code{r4rs}, and @code{r5rs} features +attempt to provide procedures and macros to bring a Scheme +implementation to the desired version of Scheme. + +@deftp {Feature} r2rs +@ftindex r2rs +Requires features implementing procedures and optional procedures +specified by @cite{Revised^2 Report on the Algorithmic Language Scheme}; +namely @code{rev3-procedures} and @code{rev2-procedures}. +@end deftp + +@deftp {Feature} r3rs +@ftindex r3rs +Requires features implementing procedures and optional procedures +specified by @cite{Revised^3 Report on the Algorithmic Language Scheme}; +namely @code{rev3-procedures}. + +@emph{Note:} SLIB already mandates the @code{r3rs} procedures which can +be portably implemented in @code{r4rs} implementations. +@end deftp + +@deftp {Feature} r4rs +@ftindex r4rs +Requires features implementing procedures and optional procedures +specified by @cite{Revised^4 Report on the Algorithmic Language Scheme}; +namely @code{rev4-optional-procedures}. +@end deftp + +@deftp {Feature} r5rs +@ftindex r5rs +Requires features implementing procedures and optional procedures +specified by @cite{Revised^5 Report on the Algorithmic Language Scheme}; +namely @code{values}, @code{macro}, and @code{eval}. +@end deftp + + +@node With-File, Transcripts, RnRS, Standards Support @subsection With-File @code{(require 'with-file)} @@ -9910,13 +10743,13 @@ Redefines @code{read-char}, @code{read}, @code{write-char}, The procedures below were specified in the @cite{Revised^2 Report on Scheme}. @strong{N.B.}: The symbols @code{1+} and @code{-1+} are not -@cite{R4RS} syntax. Scheme->C, for instance, barfs on this +@cite{R4RS} syntax. Scheme->C, for instance, chokes on this module. -@deffn Procedure substring-move-left! string1 start1 end1 string2 start2 -@deffnx Procedure substring-move-right! string1 start1 end1 string2 start2 +@deffn {Procedure} substring-move-left! string1 start1 end1 string2 start2 +@deffnx {Procedure} substring-move-right! string1 start1 end1 string2 start2 @var{string1} and @var{string2} must be a strings, and @var{start1}, -@var{start2} and @var{end1} must be exact integers satisfying@refill +@var{start2} and @var{end1} must be exact integers satisfying @display 0 <= @var{start1} <= @var{end1} <= (string-length @var{string1}) @@ -9933,7 +10766,7 @@ increasing indices. @code{substring-move-right!} stores characters in time order of increasing indeces. @end deffn -@deffn Procedure substring-fill! string start end char +@deffn {Procedure} substring-fill! string start end char Fills the elements @var{start}--@var{end} of @var{string} with the character @var{char}. @end deffn @@ -9942,7 +10775,7 @@ character @var{char}. @equiv{} @code{(= 0 (string-length @var{str}))} @end defun -@deffn Procedure append! pair1 @dots{} +@deffn {Procedure} append! pair1 @dots{} Destructively appends its arguments. Equivalent to @code{nconc}. @end deffn @@ -9986,7 +10819,7 @@ For the specification of these optional procedures, @defun string-copy @end defun -@deffn Procedure string-fill! s obj +@deffn {Procedure} string-fill! s obj @end deffn @defun list->vector l @@ -9995,7 +10828,7 @@ For the specification of these optional procedures, @defun vector->list s @end defun -@deffn Procedure vector-fill! s obj +@deffn {Procedure} vector-fill! s obj @end deffn @@ -10005,25 +10838,15 @@ For the specification of these optional procedures, @node Multi-argument / and -, Multi-argument Apply, Rev4 Optional Procedures, Standards Support @subsection Multi-argument / and - -@code{(require 'mutliarg/and-)} -@ftindex mutliarg +@code{(require 'multiarg/and-)} +@ftindex multiarg For the specification of these optional forms, @xref{Numerical -operations, , ,r4rs, Revised(4) Scheme}. The @code{two-arg:}* forms are -only defined if the implementation does not support the many-argument -forms. - -@defun two-arg:/ n1 n2 -The original two-argument version of @code{/}. -@end defun +operations, , ,r4rs, Revised(4) Scheme}. @defun / dividend divisor1 @dots{} @end defun -@defun two-arg:- n1 n2 -The original two-argument version of @code{-}. -@end defun - @defun - minuend subtrahend1 @dots{} @end defun @@ -10041,61 +10864,16 @@ The original two-argument version of @code{-}. For the specification of this optional form, @xref{Control features, , ,r4rs, Revised(4) Scheme}. -@defun two-arg:apply proc l -The implementation's native @code{apply}. Only defined for -implementations which don't support the many-argument version. -@end defun - @defun apply proc arg1 @dots{} @end defun - - @node Rationalize, Promises, Multi-argument Apply, Standards Support @subsection Rationalize -@code{(require 'rationalize)} -@ftindex rationalize - -The procedure @dfn{rationalize} is interesting because most programming -languages do not provide anything analogous to it. Thanks to Alan -Bawden for contributing this algorithm. - -@defun rationalize x y -Computes the correct result for exact arguments (provided the -implementation supports exact rational numbers of unlimited precision); -and produces a reasonable answer for inexact arguments when inexact -arithmetic is implemented using floating-point. -@end defun - -@code{Rationalize} has limited use in implementations lacking exact -(non-integer) rational numbers. The following procedures return a list -of the numerator and denominator. - -@defun find-ratio x y -@code{find-ratio} returns the list of the @emph{simplest} -numerator and denominator whose quotient differs from @var{x} by no more -than @var{y}. - -@format -@t{(find-ratio 3/97 .0001) @result{} (3 97) -(find-ratio 3/97 .001) @result{} (1 32) -} -@end format -@end defun - -@defun find-ratio-between x y -@code{find-ratio-between} returns the list of the @emph{simplest} -numerator and denominator between @var{x} and @var{y}. +@include ratize.txi -@format -@t{(find-ratio-between 2/7 3/5) @result{} (1 2) -(find-ratio-between -3/5 -2/7) @result{} (-1 2) -} -@end format -@end defun @node Promises, Dynamic-Wind, Rationalize, Standards Support @@ -10107,13 +10885,21 @@ numerator and denominator between @var{x} and @var{y}. @defun make-promise proc @end defun +@defun force promise +@end defun + +@code{(require 'delay)} provides @code{force} and @code{delay}: + +@defmac delay obj Change occurrences of @code{(delay @var{expression})} to -@code{(make-promise (lambda () @var{expression}))} and @code{(define -force promise:force)} to implement promises if your implementation -doesn't support them -(@pxref{Control features, , ,r4rs, Revised(4) Scheme}). +@example +(make-promise (lambda () @var{expression})) +@end example + +@end defmac +(@pxref{Control features, , ,r4rs, Revised(4) Scheme}). @node Dynamic-Wind, Eval, Promises, Standards Support @@ -10126,7 +10912,7 @@ This facility is a generalization of Common LISP @code{unwind-protect}, designed to take into account the fact that continuations produced by @code{call-with-current-continuation} may be reentered. -@deffn Procedure dynamic-wind thunk1 thunk2 thunk3 +@deffn {Procedure} dynamic-wind thunk1 thunk2 thunk3 The arguments @var{thunk1}, @var{thunk2}, and @var{thunk3} must all be procedures of no arguments (thunks). @@ -10150,6 +10936,7 @@ the time of the error or interrupt. @subsection Eval @code{(require 'eval)} +@ftindex eval @defun eval expression environment-specifier @@ -10270,15 +11057,49 @@ unspecified. @menu * SRFI-1:: list-processing +* SRFI-2:: guarded LET* special form +* SRFI-8:: Binding to multiple values +* SRFI-9:: Defining Record Types @end menu -@node SRFI-1, , SRFI, SRFI +@node SRFI-1, SRFI-2, SRFI, SRFI @subsubsection SRFI-1 @include srfi-1.txi +@node SRFI-2, SRFI-8, SRFI-1, SRFI +@subsubsection SRFI-2 -@node Session Support, Extra-SLIB Packages, Standards Support, Other Packages +@include srfi-2.txi + +@node SRFI-8, SRFI-9, SRFI-2, SRFI +@subsubsection SRFI-8 + +@include srfi-8.txi + +@node SRFI-9, , SRFI-8, SRFI +@subsubsection SRFI-9 + +@code{(require 'srfi-9)} +@ftindex srfi-9 + +@url{http://srfi.schemers.org/srfi-9/srfi-9.html} + +@defspec define-record-type ( ...) ... + +Where +@lisp + @equiv{} ( ) + @equiv{} ( ) + +@end lisp + +@code{define-record-type} is a syntax wrapper for the SLIB +@code{record} module. +@end defspec + + +@node Session Support, System Interface, Standards Support, Other Packages @section Session Support @menu @@ -10287,7 +11108,6 @@ unspecified. * Debug:: To err is human ... * Breakpoints:: Pause execution * Trace:: 'trace -* System Interface:: 'system, 'getenv, and 'net-clients @end menu @@ -10299,7 +11119,7 @@ unspecified. Here is a read-eval-print-loop which, given an eval, evaluates forms. -@deffn Procedure repl:top-level repl:eval +@deffn {Procedure} repl:top-level repl:eval @code{read}s, @code{repl:eval}s and @code{write}s expressions from @code{(current-input-port)} to @code{(current-output-port)} until an end-of-file is encountered. @code{load}, @code{slib:eval}, @@ -10307,7 +11127,7 @@ end-of-file is encountered. @code{load}, @code{slib:eval}, @code{repl:top-level}. @end deffn -@deffn Procedure repl:quit +@deffn {Procedure} repl:quit Exits from the invocation of @code{repl:top-level}. @end deffn @@ -10350,9 +11170,9 @@ Common-Lisp's @ref{Format} from consideration; even when variables bit-vectors are @emph{not} limited. @end quotation -@deffn Procedure qp arg1 @dots{} -@deffnx Procedure qpn arg1 @dots{} -@deffnx Procedure qpr arg1 @dots{} +@deffn {Procedure} qp arg1 @dots{} +@deffnx {Procedure} qpn arg1 @dots{} +@deffnx {Procedure} qpr arg1 @dots{} @code{qp} writes its arguments, separated by spaces, to @code{(current-output-port)}. @code{qp} compresses printing by substituting @samp{...} for substructure it does not have sufficient @@ -10362,8 +11182,11 @@ its last argument. @end deffn @defvar *qp-width* -@code{*qp-width*} is the largest number of characters that @code{qp} -should use. +@var{*qp-width*} is the largest number of characters that @code{qp} +should use. If @var{*qp-width*} is #f, then all items will be +@code{write}n. If @var{*qp-width*} is 0, then all items except +procedures will be @code{write}n; procedures will be indicated by +@samp{#[proc]}. @end defvar @node Debug, Breakpoints, Quick Print, Session Support @@ -10387,18 +11210,20 @@ printer for @code{qp}. This example shows how to do this: @ftindex debug @end example -@deffn Procedure trace-all file @dots{} +@deffn {Procedure} trace-all file @dots{} Traces (@pxref{Trace}) all procedures @code{define}d at top-level in @file{file} @dots{}. -@deffnx Procedure track-all file @dots{} + +@deffnx {Procedure} track-all file @dots{} Tracks (@pxref{Trace}) all procedures @code{define}d at top-level in @file{file} @dots{}. -@deffnx Procedure stack-all file @dots{} + +@deffnx {Procedure} stack-all file @dots{} Stacks (@pxref{Trace}) all procedures @code{define}d at top-level in @file{file} @dots{}. @end deffn -@deffn Procedure break-all file @dots{} +@deffn {Procedure} break-all file @dots{} Breakpoints (@pxref{Breakpoints}) all procedures @code{define}d at top-level in @file{file} @dots{}. @end deffn @@ -10483,7 +11308,7 @@ To unbreak, type @end lisp @end defun -@node Trace, System Interface, Breakpoints, Session Support +@node Trace, , Breakpoints, Session Support @subsection Tracing @code{(require 'trace)} @@ -10567,6 +11392,10 @@ supported by your implementation, these might be more convenient to use. @defun tracef proc @defunx tracef proc name +@defunx trackf proc +@defunx trackf proc name +@defunx stackf proc +@defunx stackf proc name To trace, type @lisp (set! @var{symbol} (tracef @var{symbol})) @@ -10597,8 +11426,8 @@ To untrace, type @end defun -@node System Interface, , Trace, Session Support -@subsection System Interface +@node System Interface, Extra-SLIB Packages, Session Support, Other Packages +@section System Interface @noindent If @code{(provided? 'getenv)}: @@ -10616,18 +11445,43 @@ Executes the @var{command-string} on the computer and returns the integer status code. @end defun + +@menu +* Directories:: +* Transactions:: +* CVS:: +@end menu + +@node Directories, Transactions, System Interface, System Interface +@subsection Directories + +@include dirs.txi + + +@node Transactions, CVS, Directories, System Interface +@subsection Transactions + @noindent If @code{system} is provided by the Scheme implementation, the -@dfn{net-clients} package provides interfaces to common network client -programs like FTP, mail, and Netscape. +@dfn{transact} package provides functions for file-locking and +file-replacement transactions. + +@code{(require 'transact)} +@ftindex transact -@code{(require 'net-clients)} -@ftindex net-clients +@include transact.txi -@include nclients.txi +@node CVS, , Transactions, System Interface +@subsection CVS -@node Extra-SLIB Packages, , Session Support, Other Packages +@code{(require 'cvs)} +@ftindex cvs + +@include cvs.txi + + +@node Extra-SLIB Packages, , System Interface, Other Packages @section Extra-SLIB Packages Several Scheme packages have been written using SLIB. There are several @@ -10690,18 +11544,19 @@ http://www.cs.tut.fi/staff/pk/scheme/psd/article/article.html @sp 1 @item SCHELOG -is an embedding of Prolog in Scheme. +is an embedding of Prolog in Scheme.@* @ifset html - + @end ifset -http://www.cs.rice.edu/CS/PLT/packages/schelog/ +http://www.ccs.neu.edu/~dorai/schelog/schelog.html @ifset html @end ifset @sp 1 @item JFILTER -is a Scheme program which converts text among the JIS, EUC, and Shift-JIS Japanese character sets. +is a Scheme program which converts text among the JIS, EUC, and +Shift-JIS Japanese character sets.@* @ifset html @end ifset @@ -10733,13 +11588,13 @@ Aubrey Jaffer @* * Porting:: SLIB to new platforms. * Coding Guidelines:: How to write modules for SLIB. * Copyrights:: Intellectual propery issues. +* About this manual:: @end menu @node Installation, Porting, About SLIB, About SLIB @section Installation - @ifset html @end ifset @@ -10747,6 +11602,8 @@ Aubrey Jaffer @* @end ifset +@cindex install +@cindex installation There are four parts to installation: @itemize @bullet @@ -10818,6 +11675,15 @@ SLIB), then: (require 'new-catalog) @end example +The catalog also supports color-name dictionaries. With an +SLIB-installed scheme implementation, type: +@example +(require 'color-names) +(make-slib-color-name-db) +(require 'new-catalog) +(slib:exit) +@end example + @subsection Implementation-specific Instructions Multiple implementations of Scheme can all use the same SLIB directory. @@ -10885,7 +11751,7 @@ initialization file. To use SLIB in MzScheme, set the @var{SCHEME_LIBRARY_PATH} environment variable to the installed SLIB location; then invoke MzScheme thus: -@code{mzscheme -L init.ss slibinit} +@code{mzscheme -f $@{SCHEME_LIBRARY_PATH@}DrScheme.init} @end deftp @deftp Implementation {MIT Scheme} @@ -10903,10 +11769,15 @@ variable to the installed SLIB location; then invoke MzScheme thus: If there is no initialization file for your Scheme implementation, you will have to create one. Your Scheme implementation must be largely -compliant with @cite{IEEE Std 1178-1990}, @cite{Revised^4 Report on the -Algorithmic Language Scheme}, or @cite{Revised^5 Report on the -Algorithmic Language Scheme} in order to support SLIB. @footnote{If you -are porting a @cite{Revised^3 Report on the Algorithmic Language Scheme} +compliant with +@lisp +@cite{IEEE Std 1178-1990}, +@cite{Revised^4 Report on the Algorithmic Language Scheme}, or +@cite{Revised^5 Report on the Algorithmic Language Scheme} +@end lisp +@noindent +in order to support SLIB. @footnote{If you are porting a +@cite{Revised^3 Report on the Algorithmic Language Scheme} implementation, then you will need to finish writing @file{sc4sc3.scm} and @code{load} it from your initialization file.} @@ -10921,9 +11792,9 @@ Your customized version should then be loaded as part of your scheme implementation's initialization. It will load @file{require.scm} from the library; this will allow the use of @code{provide}, @code{provided?}, and @code{require} along with the @dfn{vicinity} -functions (these functions are documented in the section @ref{Require}). -The rest of the library will then be accessible in a system independent -fashion. +functions (these functions are documented in the sections +@ref{Feature} and @ref{Require}). The rest of the library will then +be accessible in a system independent fashion. Please mail new working configuration files to @code{agj @@ alum.mit.edu} so that they can be included in the SLIB distribution. @@ -10935,28 +11806,39 @@ so that they can be included in the SLIB distribution. All library packages are written in IEEE P1178 Scheme and assume that a configuration file and @file{require.scm} package have already been loaded. Other versions of Scheme can be supported in library packages -as well by using, for example, @code{(provided? 'rev3-report)} or -@code{(require 'rev3-report)} (@pxref{Require}). -@ftindex rev3-report +as well by using, for example, @code{(provided? 'r3rs)} or +@code{(require 'r3rs)} (@pxref{Require}). +@ftindex r3rs + +If a procedure defined in a module is called by other procedures in +that module, then those procedures should instead call an alias +defined in that module: + +@lisp +(define module-name:foo foo) +@end lisp + +The module name and @samp{:} should prefix that symbol for the +internal name. Do not export internal aliases. -The module name and @samp{:} should prefix each symbol defined in the -package. Definitions for external use should then be exported by having -@code{(define foo module-name:foo)}. +A procedure is exported from a module by putting Schmooz-style +comments (@pxref{Schmooz}) or @samp{;@@} at the beginning of the line +immediately preceding the definition (@code{define}, +@code{define-syntax}, or @code{defmacro}). Modules, exports and other +relevant issues are discussed in @ref{Compiling Scheme}. -Code submitted for inclusion in SLIB should not duplicate routines -already in SLIB files. Use @code{require} to force those library -routines to be used by your package. Care should be taken that there -are no circularities in the @code{require}s and @code{load}s between the -library packages. +Code submitted for inclusion in SLIB should not duplicate (more than +one) routines already in SLIB files. Use @code{require} to force +those library routines to be used by your package. Documentation should be provided in Emacs Texinfo format if possible, -But documentation must be provided. +but documentation must be provided. Your package will be released sooner with SLIB if you send me a file which tests your code. Please run this test @emph{before} you send me the code! -@subheading Modifications +@subsection Modifications Please document your changes. A line or two for @file{ChangeLog} is sufficient for simple fixes or extensions. Look at the format of @@ -10975,7 +11857,7 @@ Please @emph{do not} reformat the source code with your favorite beautifier, make 10 fixes, and send me the resulting source code. I do not have the time to fish through 10000 diffs to find your 10 real fixes. -@node Copyrights, , Coding Guidelines, About SLIB +@node Copyrights, About this manual, Coding Guidelines, About SLIB @section Copyrights @ifset html @@ -10986,6 +11868,7 @@ not have the time to fish through 10000 diffs to find your 10 real fixes. @end ifset This section has instructions for SLIB authors regarding copyrights. +@cindex copyright Each package in SLIB must either be in the public domain, or come with a statement of terms permitting users to copy, redistribute and modify it. @@ -10995,88 +11878,111 @@ The comments at the beginning of @file{require.scm} and If your code or changes amount to less than about 10 lines, you do not need to add your copyright or send a disclaimer. -@subheading Putting code into the Public Domain +@subsection Putting code into the Public Domain In order to put code in the public domain you should sign a copyright disclaimer and send it to the SLIB maintainer. Contact agj @@ alum.mit.edu for the address to mail the disclaimer to. +@need 1000 @quotation -I, @var{name}, hereby affirm that I have placed the software package -@var{name} in the public domain. +I, @var{}, hereby affirm that I have placed the software +package @var{} in the public domain. I affirm that I am the sole author and sole copyright holder for the software package, that I have the right to place this software package in the public domain, and that I will do nothing to undermine this status in the future. - @flushright @var{signature and date} @end flushright @end quotation This wording assumes that you are the sole author. If you are not the -sole author, the wording needs to be different. If you don't want to be -bothered with sending a letter every time you release or modify a +sole author, the wording needs to be different. If you don't want to +be bothered with sending a letter every time you release or modify a module, make your letter say that it also applies to your future revisions of that module. -Make sure no employer has any claim to the copyright on the work you are -submitting. If there is any doubt, create a copyright disclaimer and -have your employer sign it. Mail the signed disclaimer to the SLIB -maintainer. Contact agj @@ alum.mit.edu for the address to mail the -disclaimer to. An example disclaimer follows. +Make sure no employer has any claim to the copyright on the work you +are submitting. If there is any doubt, create a copyright disclaimer +and have your employer sign it. Mail the signed disclaimer to the +SLIB maintainer. Contact agj @@ alum.mit.edu for the address to mail +the disclaimer to. An example disclaimer follows. -@subheading Explicit copying terms +@subsection Explicit copying terms @noindent -If you submit more than about 10 lines of code which you are not placing -into the Public Domain (by sending me a disclaimer) you need to: +If you submit more than about 10 lines of code which you are not +placing into the Public Domain (by sending me a disclaimer) you need +to: @itemize @bullet @item Arrange that your name appears in a copyright line for the appropriate -year. Multiple copyright lines are acceptable. +year. Multiple copyright lines are acceptable. @item -With your copyright line, specify any terms you require to be different -from those already in the file. +With your copyright line, specify any terms you require to be +different from those already in the file. @item -Make sure no employer has any claim to the copyright on the work you are -submitting. If there is any doubt, create a copyright disclaimer and -have your employer sign it. Mail the signed disclaim to the SLIB +Make sure no employer has any claim to the copyright on the work you +are submitting. If there is any doubt, create a copyright disclaimer +and have your employer sign it. Mail the signed disclaim to the SLIB maintainer. Contact agj @@ alum.mit.edu for the address to mail the disclaimer to. @end itemize -@subheading Example: Company Copyright Disclaimer +@subsection Example: Company Copyright Disclaimer -This disclaimer should be signed by a vice president or general manager -of the company. If you can't get at them, anyone else authorized to -license out software produced there will do. Here is a sample wording: +This disclaimer should be signed by a vice president or general +manager of the company. If you can't get at them, anyone else +authorized to license out software produced there will do. Here is a +sample wording: @quotation -@var{employer} Corporation hereby disclaims all copyright -interest in the program @var{program} written by @var{name}. +@var{} Corporation hereby disclaims all copyright +interest in the program @var{} written by @var{}. -@var{employer} Corporation affirms that it has no other intellectual -property interest that would undermine this release, and will do nothing -to undermine it in the future. +@var{} Corporation affirms that it has no other intellectual +property interest that would undermine this release, and will do +nothing to undermine it in the future. @flushleft -@var{signature and date}, -@var{name}, @var{title}, @var{employer} Corporation +@var{}, +@var{}, @var{}, @var{<employer>} Corporation @end flushleft @end quotation +@node About this manual, , Copyrights, About SLIB +@section About this manual + +@itemize @bullet +@item +Entries that are labeled as Functions are called for their return +values. Entries that are labeled as Procedures are called primarily for +their side effects. + +@item +Examples in this text were produced using the @code{scm} Scheme +implementation. + +@item +At the beginning of each section, there is a line that looks like +@ftindex feature +@code{(require 'feature)}. Include this line in your code prior to +using the package. +@end itemize + + @node Index, , About SLIB, Top -@c @node Procedure and Macro Index, Variable Index, About SLIB, Top +@c @node Procedure and Macro Index, Operator Index, About SLIB, Top @unnumbered Procedure and Macro Index This is an alphabetical list of all the procedures and macros in SLIB. @printindex fn -@c @node Variable Index, Concept Index, Procedure and Macro Index, Top +@c @node Variable Index, Concept Index, Operator Index, Top @unnumbered Variable Index This is an alphabetical list of all the global variables in SLIB. -- cgit v1.2.3