Enumerated and finite types facility

_Enumerated and finite types facility_

David Van Horn

This an implementation of the finite-types structure from Scheme 48.

> (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.

For example, 

  (define-enumerated-type colour :colour 
    (black white purple maroon))

  (colour-name (vector-ref colours 0))  ==> black
  (colour-name (colour white))          ==> white
  (colour-index (colour purple))        ==> 2

> (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 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 notlisted in the first field tag
list must be assigned later.

For example, 

  (define-finite-type colour :colour 
    (red green blue) 
    (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))          ==> black
  (colour-name (vector-ref colours 1))  ==> white
  (colour-index (colour purple))        ==> 2
  (colour-red (colour maroon))          ==> 176