Rationale [see 1]
Declaring and generating fixed sized data type disjoint from all other types, called AGGREGATES.
General other record or object features may be build on top of these aggregates.
Datatype and associated functions [see 3]
(make-datatype designation fieldname ...)
Returns a new datatype. fieldname must be symbols. This is not a functional call in order to avoid potential collisions between similar aggregates but of different kind.
If <my-datatype> is of datatype kind, then returns its designation.
If <my-datatype> is of datatype kind, then returns corresponding list of fields.
Aggregates ( constructor / accessor ) constructor
If <datatype> is of datatype kind, then returns corresponding constructor and switch function (see below). <datatype> ensures that the call to create-aggregate-functions is functional, the same aggregate functions are generated if applied to the same <datatype>.
create-aggregate-functions returns 3 functions as values:
make-<aggregate> is a function taking a fixed number of arguments as specified by the <datatype> and returning a new aggregate containing these arguments as aggregate components.
<aggregate>-switch is explained below.
<aggregate>-set! is a setter function which takes 3 arguments:
(<aggregate>-set! <fieldname> <obj> <val>) sets field <fieldname> of <obj> with <val>, if <obj> is not of <aggregate> corresponding datatype kind then an error is signaled.
The data created by make-<aggregate> and selected by <aggregate>-switch are required to conform to <my-datatype> properties (currently only arity).
With modules it is possible not to export the datatype value nor the generic aggregate functions but only functions defined on top of them. This way one could have finer control of aggregate usage. The idea is to allow flexibility by exposing only interface functions via modules exports and that it is impossible to rebuild aggregate functions if corresponding datatype (with unique designation) is not exported.
Accessing aggregate components
- Unsafe access procedures must be invoked after a predicate checking data type
- Or safe access procedure, then a check is performed before accessing and an error is signalled if the data type is not what is expected.
However data are aggregated in order to retrieve many part of it and not only one. Solution 2 requires to perform redundant check for each accessed field and moreover the error in general is fatal to the program execution. Solution 1 alone is unsatisfactory as if an unsafe access procedure is applied to not of the correct kind data then random and unwanted behaviors may appear.
Another solution is to group together data type checking with accessing in a case analysis function (per aggregate types) :
(<aggregate>-switch <aggregate-case> <else-case>) = (lambda (<obj>) ...)
Two cases are possible: if the data <obj> is of <aggregate> corresponding datatype kind then <aggregate-case> function is called with the components of the <obj> data, else <else-case> is called with <obj>.
The idea behind my-datatype-switch function is to open an environment with bindings for corresponding aggregate components.
A data is a variant if it is one kind of a list of aggregates.
For instance, one could view the following classic types as variant:
- List as variant of NULL or CONS
- Tree as variant of LEAF or NODE
Convenience macro for variants may be provided as in [see 2]:(define-syntax variant-case
(error "variant-case: all case exhausted " <obj>))
(else <body> ...))
(begin <body> ...))
(<aggregate-switch> (<var> ...) <body> ...)
(lambda (<var> ...) <body> ...)
(lambda (<obj>) (variant-case <obj> rest ...)))
SRFI-9 records interface(define-syntax define-record-type
(constructor constructor-tag ...)
(field-tag accessor . more) ...)
(define type (make-datatype typename field-tag ...))
(define-values (type-constructor type-switch type-set!)
(lambda (constructor-tag ...)
(type-constructor field-tag ...)))
(type-switch (lambda (field-tag ...) #t)
(lambda (obj) #f)))
;; Macro call for accessors and optional modifiers
(define-record-field type-switch (field-tag ...) field-tag accessor . more)
((define-record-field type-switch field-list field-tag accessor)
(type-switch (lambda field-list field-tag)
(lambda (obj) (error "Invalid type: " obj))))))
((define-record-field type-switch field-list field-tag accessor modifier)
(type-switch (lambda field-list field-tag)
(lambda (obj) (error "Invalid type: " obj))))
(define (modifier obj val)
(type-set! field-tag obj val))))))
Various examples(define-values (make-null null-switch null-set!)
(create-aggregate-functions (make-datatype "NULL")))
(define-values (make-pair pair-switch pair-set!)
(create-aggregate-functions (make-datatype "PAIR" 'first 'second)))
;; With my-car, my-cdr for instance:
(define (my-car obj)
((pair-switch (lambda (first second) first) error)
(define (my-cdr obj)
((pair-switch (lambda (first second) second) error)
;; MAYBE data kind maker: either empty or containing some data.
(define-values (make-empty maybe-switch maybe-set!)
(create-aggregate-functions (make-datatype "Empty")))
(maybe-switch <empty-case> <not-empty-case>)
;; 3d point example
(define-values (make-point3d point3d-switch point3d-set!)
(create-aggregate-functions (make-datatype "3d point" 'X 'Y 'Z)))
(define (point3d-length x y z)
(sqrt (+ (* x x) (* y y) (* z z))))
(define (point3d-scale alpha)
(lambda (x y z)
(make-point3d (* alpha x) (* alpha y) (* alpha z))))
(define p3d (make-point3d 3 4 5))
((point3d-switch point3d-length error) p3d) ;; 7.07...
((point3d-switch (point3d-scale -2) error) p3d) ;; [-6 -8 -10]
;; Binary tree example
(define-values (make-bin-leaf bin-leaf-switch bin-leaf-set!)
(create-aggregate-functions (make-datatype "Binary tree leaf" 'Data)))
(define-values (make-bin-node bin-node-switch bin-node-set!)
(create-aggregate-functions (make-datatype "Binary tree node" 'Data 'Left 'Right)))
; variant-case example
(define (map-tree fun bin-tree)
(bin-node-switch (data left right)
(make-bin-node (fun data)
(map-tree fun left)
(map-tree fun right)))
(make-bin-leaf (fun data)))
(else (error "Not a bin-tree: " bin-tree))))
;; aggregate copy
(<my-aggregate-switch> <make-my-aggregate> error))
;; aggregate to association list converter
(lambda l (map list (datatype->fields <datatype>) l))
;; Unforgeable aggregate with built-in assertion checking.
((create-aggregate-with-assertion (<datatype-designation> '<field> ...) <assertion>)
(let-values ((maker switch setter)
(create-aggregate-functions (make-datatype (list <datatype-designation>) '<field> ...)))
(values (lambda (<field> ...)
(if (<assertion> <field> ...)
(maker <field> ...)
(error "Assertion failed: "
(list <datatype-designation> <field> ...))))
- Maybe it is better to have instead of create-aggregate-functions a syntactic construct to bind the created functions name ?
- An orthogonal mechanism is foreseen to allow securing data (like a lock mechanism with capabilities) but this is not the subject of this page.
Disjointness issue raised and proposal:
 Jonathan A. Rees. "User-defined data types". Lisp Pointers. 'The Scheme of Things' (column). 1993
For variant-case to destructure records:
 Daniel P. Friedman, Mitchell Wand, and Christopher T. Haynes. Essentials of Programming Languages. MIT Press and McGraw-Hill, 1991.
RTD (datatype) functions:
 Jonathan A. Rees, Norman I. Adams IV and James R. Meehan. "The T manual". Yale University Computer Science Department. 1984.