Scheme 48 Manual -- Finite record types

Scheme 48 Manual | Contents | In Chapter: Libraries
Previous: Finite record types | Next: Finite record types

## Finite record types

The structure `finite-types` has two macros for defining `finite' record types. These are record types for which there are a fixed number of instances, all of which are created at the same time as the record type itself. The syntax for defining an enumerated type is:

>     (define-enumerated-type tag type-name
>       predicate-name
>       vector-of-instances-name
>       name-accessor
>       index-accessor
>       (instance-name ...))

This defines a new record type, bound to `type-name`, with as many instances as there are `instance-name`'s. `Vector-of-instances-name` is bound to a vector containing the instances of the type in the same order as the `instance-name` list. `Tag` is bound to a macro that when given an `instance-name` expands into an expression that returns corresponding instance. The name lookup is done at macro expansion time. `Predicate-name` is a predicate for the new type. `Name-accessor` and `index-accessor` are accessors for the name and index (in `vector-of-instances`) of instances of the type.

>     (define-enumerated-type color :color
>       color?
>       colors
>       color-name
>       color-index
>       (black white purple maroon))

>     (color-name (vector-ref colors 0)) -> black
>     (color-name (color white))         -> white
>     (color-index (color purple))       -> 2

Finite types are enumerations that allow the user to add additional fields in the type. The syntax for defining a finite type is:

>     (define-finite-type tag type-name
>       (field-tag ...)
>       predicate-name
>       vector-of-instances-name
>       name-accessor
>       index-accessor
>       (field-tag accessor-name [modifier-name])
>       ...
>       ((instance-name field-value ...)
>        ...))

The additional fields are specified exactly as with `define-record-type`. The field arguments to the constructor are listed after the `type-name`; these do not include the name and index fields. The form ends with the names and the initial field values for the instances of the type. The instances are constructed by applying the (unnamed) constructor to these initial field values. The name must be first and the remaining values must match the `field-tag`s in the constructor's argument list.

>     (define-finite-type color :color
>       (red green blue)
>       color?
>       colors
>       color-name
>       color-index
>       (red   color-red)
>       (green color-green)
>       (blue  color-blue)
>       ((black    0   0   0)
>        (white  255 255 255)
>        (purple 160  32 240)
>        (maroon 176  48  96)))

>     (color-name (color black))         -> black
>     (color-name (vector-ref colors 1)) -> white
>     (color-index (color purple))       -> 2
>     (color-red (color maroon))         -> 176

Previous: Finite record types | Next: Finite record types