[R6RS] Enumerations proposal pre-draft

William D Clinger will at ccs.neu.edu
Thu Apr 20 17:16:00 EDT 2006


This is what Mike's enumerations proposal might look like
if it used symbols to represent the enumerated values.
I took the liberty of adding a few features, mainly to
show why I think it is better to add new functions than
new types.

Enumeration Types
=================

Enumeration types are subtypes of Scheme's standard symbol
types.  Each enumeration type represents a fixed set of
symbols, which are specified when the enumeration type is
defined.

The syntax for defining an enumerated type is:

(define-enum-type <type-name>
  <predicate-name>
  <index-accessor>
  (<symbol> ...))

This defines three procedures: <type-name>, <predicate-name>,
and <index-accessor>.  <type-name> will be a procedure of no
arguments that returns symbols that comprise the enumeration
type, in the same order as they were specified.  <predicate-name>
will take an arbitrary object, returning true if and only if
the object is one of the symbols returned by <type-name>.
<index-accessor> takes an arbitrary object, returning false
if the object is not one of the symbols returned by <type-name>;
if the object is one of the symbols returned by <type-name>,
then <index-accessor> returns its 0-origin index within the list
returned by <type-name>.

The <type-name> procedure also serves as the canonical
representation of the enumerated type.

Example:

(define-enum-type color
  color?
  color-index
  (black white purple maroon))

(color)                            => (black white purple maroon)
(color? 'black)                    => #t
(color? 'gold)                     => #f
(color-index (color purple))       => 2

Enumeration-Set Types
=====================

Enumeration-set types work naturally with the enumeration types.
The syntax for defining such a type is:

(define-enum-set-type <enum-type> <predicate> <constructor>)

where <enum-type> is the canonical representation of the
enumeration type that serves as universe for the
enumeration-set type, <predicate> will be a predicate that
returns true if an only if its argument is an enumeration
set defined by the <constructor>, and <constructor> will
be a procedure that takes any number of symbols that belong
to the <enum-type> and returns an enumeration set defined
with respect to the universe <enum-type>.

Given the enumerated type `color', for example, we can define
sets of color:

(define-enum-set-type color
                      color-set?
                      color-set)

(color-set? (color-set 'black 'white))  =>  #t
(color-set? '(black white))             =>  #f


(enum-set-universe enum-set)
(enum-set->list enum-set)
(enum-set-member? symbol enum-set)
(enum-set-subset? enum-set enum-set)
(enum-set=? enum-set enum-set) => boolean
(enum-set-union enum-set enum-set) => enum-set
(enum-set-intersection enum-set enum-set => enum-set
(enum-set-difference enum-set enum-set)
(enum-set-complement enum-set) => enum-set

enum-set-universe returns the canonical representation
of the enumeration type with respect to which its argument
was defined.  (Note: the canonical representation of an
enumeration type is a procedure of no arguments that
returns the ordered list of symbols for that enumeration
type.)

enum-set->list returns a list of the symbols that belong
to an enumeration set.

enum-set-member? returns true if and only if its first
argument is an element of its second argument.

enum-set-subset? returns true if and only if the universe
of its first argument is a subset of the universe of its
second argument (considered as sets of symbols) and every
element of its first argument is a member of its second.

enum-set=? returns true if and only if its first argument
is a subset of its second and vice versa, as determined
by the enum-set-subset? procedure.

enum-set-union takes two enumeration sets that share the
same enumeration type as universe, and returns their union.

enum-set-intersection takes two enumeration sets that share
the same enumeration type as universe, and returns their
intersection.

enum-set-difference takes two enumeration sets that share
the same enumeration type as universe, and returns their
difference.

enum-set-complement takes an enumeration set and returns
its complement with respect to its universe.


((enum-set-universe (color-set)))  =>  (black white purple maroon)

(enum-set->list (color-set))       =>  ()

(enum-set-member? 'white (color-set 'white))  =>  #t

(enum-set-subset? (color-set 'white)
                  (enum-set-complement (color-set 'black)))  =>  #t

(enum-set=? (color-set 'black 'maroon)
            (enum-set-complement
             (color-set 'white 'purple)))  =>  #t


The representation of an enumeration set is not specified
by this proposal.

Will



More information about the R6RS mailing list