#lang scribble/doc @(require scribble/manual scribble/eval planet/util (planet cce/scheme:6:3/planet) (planet cce/scheme:6:3/scribble) (for-label (for-label (this-package-in main))) (for-label scheme)) @(define the-eval (let ([the-eval (make-base-eval)]) (the-eval `(require (planet ,(this-package-version-symbol main)))) the-eval)) @title{@bold{Finite Types}: Enumerated and finite types and sets} @author+email["David Van Horn" "dvanhorn@ccs.neu.edu"] This package provides an implementation of the finite-types and enum-sets interfaces from Scheme 48. @link[(format "http://planet.plt-scheme.org/trac/newticket?component=~a%2F~a&planetversion=(~a+~a)" (this-package-version-owner) (this-package-version-name) (this-package-version-maj) (this-package-version-min))]{Report a bug}. @section{Enumerated and finite types} @defmodule[(planet dvanhorn/finite-types/finite-types)] @defform[(define-enumerated-type dispatcher type predicate instance-vector name-accessor index-accessor (instance-name ...))] This defines a new record type, to which type is bound, with as many instances as there are instance-names. Predicate is defined to be the record type's predicate. Instance-vector is defined to be a vector containing the instances of the type in the same order as the instance-name list. Dispatcher is defined to be a macro of the form (dispatcher instance-name ); it evaluates to the instance with the given name, which is resolved at macro-expansion time. Name-accessor and index-accessor are defined to be unary procedures that return the symbolic name and index into the instance vector, respectively, of the new record type instances. @examples[#:eval the-eval (define-enumerated-type colour :colour colour? colours colour-name colour-index (black white purple maroon)) (colour-name (vector-ref colours 0)) (colour-name (colour white)) (colour-index (colour purple))] @defform[(define-finite-type dispatcher type (field-tag ...) predicate instance-vector name-accessor index-accessor (field-tag accessor [modifier]) ... ((instance-name field-value ...) ...))] This is like @scheme[define-enumerated-type], but the instances can also have added fields beyond the name and the accessor. The first list of field tags lists the fields that each instance is constructed with, and each instance is constructed by applying the unnamed constructor to the initial field values listed. Fields not listed in the first field tag list must be assigned later. @examples[#:eval the-eval (define-finite-type colour :colour (red green blue) colour? colours colour-name colour-index (red colour-red) (green colour-green) (blue colour-blue) ((black 0 0 0) (white 255 255 255) (purple 160 32 240) (maroon 176 48 96))) (colour-name (colour black)) (colour-name (vector-ref colours 1)) (colour-index (colour purple)) (colour-red (colour maroon))] @section{Sets over Enumerated types} @defmodule[(planet dvanhorn/finite-types/enum-sets)] @defform[(define-enum-set-type set-syntax type predicate list->x-set element-syntax element-predicate element-vector element-index)]{ This defines @scheme[set-syntax] to be a syntax for constructing sets, @scheme[type] to be an object that represents the type of enumerated sets, @scheme[predicate] to be a predicate for those sets, and @scheme[list->x-set] to be a procedure that converts a list of elements into a set of the new type. The @scheme[element-syntax] must name a macro for constructing set elements from names (akin to the dispatcher argument to the @scheme[define-enumerated-type] & @scheme[define-finite-type] forms). The @scheme[element-predicate] must be a predicate for the element type, @scheme[element-vector] a vector of all values of the element type, and @scheme[element-index] a procedure that returns the index of an element within @scheme[element-vector].} @defproc[(enum-set->list [enum-set enum-set?]) list?]{ Returns a list of elements within @scheme[enum-set].} @defproc[(enum-set-member? [enum-set enum-set?] [element any/c]) boolean?]{ Tests whether element is a member of @scheme[enum-set].} @defproc[(enum-set=? [enum-set-a enum-set?] [enum-set-b enum-set?]) boolean?]{ Tests whether two enumerated sets are equal, i.e. contain all the same elements.} @defproc*[[[(enum-set-union [enum-set-a enum-set?] [enum-set-b enum-set?]) enum-set?] [(enum-set-intersection [enum-set-a enum-set?] [enum-set-b enum-set?]) enum-set?] [(enum-set-negation [enum-set enum-set?]) enum-set?]]]{ Standard set algebra operations on enumerated sets. It is an error to pass an element that does not satisfy enum-setted sets of different types to @scheme[enum-set=?] or the enumerated set algebra operators.} @examples[#:eval the-eval (define-enumerated-type colour :colour colour? colours colour-name colour-index (red blue green)) (define-enum-set-type colour-set :colour-set colour-set? list->colour-set colour colour? colours colour-index) (map colour-name (enum-set->list (colour-set red blue))) (map colour-name (enum-set->list (enum-set-negation (colour-set)))) (enum-set-member? (colour-set red blue) (colour blue))] @smaller{Much of this documentation is derived from Taylor Campbell's "The Nearly Complete Scheme48 1.3 Reference Manual", which is "Copyright (C) 2004, 2005, 2006 Taylor Campbell. All rights reserved" and in turn states the relevant section was "derived from work copyrighted (C) 1993-2005 by Richard Kelsey, Jonathan Rees, and Mike Sperber."}