Struct
_Struct_
_struct_
This collection provides two files:
_datatype.ss_: algebraic datatype definitions
_hierarchy.ss_: hierarchical structure definitions
This library provides syntaxes for defining algebraic datatypes and hierarchical
datatypes and attaching contracts to the field of these datatypes. All the
datatypes implemented in this library are implemented as structs in order to be
compatible with the pattern-matching libraries _match.ss_ and _plt-match.ss_.
======================================================================
datatype.ss
-----------
> (define-datatype type [variant (field ...)] ...)
Declares a new algebraic datatype named `type'. Each variant is given
a name `variant' and a list of zero or more fields, each named `field'.
Analogous to define-struct, `define-datatype' has the side effect of
defining constructors, selectors, and mutators for each variant, with
similar naming conventions.
> (provide-datatype type)
Exports all constructors, selectors, and mutators for the given
algebraic datatype from the current module.
> (provide-datatype/contract type [variant (contract ...)] ...)
Exports all constructors, selectors, and mutators for the given
algebraic datatype from the current module, with contracts attached
to each field. The contracts are associated positionally with the
fields in the order in which the fields were declared (as well as the
order in which they must be provided to the constructors).
hierarchy.ss
------------
> (define-hierarchy (name (field ...)
(child (ch-field ...) etc ...)
...)) :: syntax
Defines a structure type hierarchy. The nesting of structure type inheritance
follows the nesting of the syntax tree.
> (provide-hierarchy name) :: syntax
Provides all structure definitions that were given in a _define-hierarchy_ form,
without attaching contracts to any of the fields.
> (provide-hierarchy/contract (name (contract ...)
(child (ch-contract ...) etc ...)
...)) :: syntax
Provides all structure definitions that were given in a _define-hierarchy_ form
with contracts attached to all of the fields. The structure of the syntax tree
must match the structure of the corresponding _define-hierarchy_ form exactly.
> (define-hierarchy/provide/contract (name ([field contract] ...)
(child ([ch-field ch-contract] ...) etc ...)
...)) :: syntax
Defines and provides a structure type hierarchy with contracts attached to each
of the fields.
> (without-hierarchy-contracts exp)
Convenience form for disregarding any contract information associated with
structure type hierarchies defined in the body of `exp' by forms from this
library. Since contracts tend to be large and slow to compile, this can be
useful for speeding up compilation (i.e., macro-expansion) times.
EXAMPLES -------------------------------------------------------------
;; Algebraic datatypes:
(define-datatype Term
[Var (name)]
[Abs (var body)]
[App (rator rand)])
(provide-datatype/contract Term
[Var (symbol?)]
[Abs (symbol? Term?)]
[App (Term? Term?)])
(define Delta
(make-Abs 'x
(make-App (make-Var 'x)
(make-Var 'x))))
(define Omega
(make-App Delta Delta))
;; Type hierarchies:
(define-hierarchy/provide/contract
(error ([message string?])
(error:contract ([label symbol?] [guilty-party string?])
(error:contract:arity ([expected integer?] [actual integer?])))
(error:io ()
(error:io:eof ()))))