Title

define-lambda-object

Author

Joo ChurlSoo

Status

This SRFI is currently in final status. Here is an explanation of each status that a SRFI can hold. To provide input on this SRFI, please send email to srfi-100@nospamsrfi.schemers.org. To subscribe to the list, follow these instructions. You can access previous messages via the mailing list archive.

Abstract

This SRFI introduces a macro, DEFINE-LAMBDA-OBJECT which defines a set of procedures, that is, a group, two constructors, and a predicate. The constructors also make a group of procedures, namely lambda objects. The macro extends DEFINE-RECORD-TYPE (SRFI 9) in being more general but much less general than DEFCLASS (CLOS). The macro has no explicit field accessors and mutators but parent groups, required fields, optional fields, automatic fields, read-write fields, read-only fields, inaccessible hidden fields, immutable virtual fields, and common sharing fields.

Rationale

An object created by a constructor procedure is a procedure whose first argument is a symbolized field name that is used to identify fields. The lambda object plays the role of the accessor and mutator of each field. Though the average time required to access a randomly chosen field is more for the lambda object than for the accessors and mutators of most other record-defining macros that use field indices to indentify fields, the lambda object makes the troublesome explicit or implicit accessors and mutators unnecessary. In addition, this makes the accesors and mutators to be automatically `nongenerative' and reduces the role of the predicate procedure. Although DEFINE-RECORD-TYPE of R6RS can also have implicit accessors and mutators, they should know their own record name. Further more, when there are parents, they should know both their own record name and their parents' record names, which could make users confused, though there is an advantage that a record can have another field with the same name.

This macro works not only as DEFINE-RECORD-TYPE with required fields but also as DEFSTRUCT of Common Lisp with optional fields. The automatic field can be used as a procedure that modifies or handles the values of the other fields.

When a group has multiple parent groups, all the fields of parent groups must exist in the field spec of the child group in contrary with DEFINE-RECORD-TYPE of R6RS. This is too much trouble in case parent groups have several tens of fields. But it also has the advantage of reconfirming the existence and properties of each field, and making the constructors to be able to be defined irrespectively the order of the parents' fields. From a practical point of view, inheritance may be superfluous in this macro as the lambda object itself has data and methods as well as their accessors and mutators.

Specification

(define-lambda-object <group spec> <field spec>)

<group spec> --> <group> | (<group> <parent group>*)

<parent group> --> <group>                       ;unamendable group
                | (<group>)                       ;amendable group

<field spec> --> <required field>* <optional field>* <automatic field>*

<required field> --> <field>                     ;read-only field
                   | (<field>)                    ;read-write field

<optional field> --> (<field>  <default>)  ;read-only field
                   | ((<field>) <default>)  ;read-write field
                   | ('<field> <default>)   ;inaccessible hidden field

<automatic field> --> (,<field>  <default>)        ;read-only field
                    | ((,<field>) <default>)        ;read-write field
                    | (',<field>  <default>)        ;inaccessible hidden field
                    | (`,<field>  <default>)        ;immutable virtual field
                    | (,,<field> <default>) ;common read-only field
                    | ((,,<field>) <default>)       ;common read-write field

The name of <constructor> is generated by prefixing `make-' to the group name, or by prefixing `make-' and postfixing `-by-name' to the group name. The name of <predicate> is generated by adding a question mark (`?') to the end of the group name.

The <group> and <field> must be identifiers.

Each <default> is an <expression> that is evaluated in an environment that the values of all the previous <field>s are visible. There is one exception to this rule. The <default>s of <automatic common field>s are evaluated in the outer environment of the define-lambda-object form, and their values are visible as the <default>s of the other fields are evaluated.

The define-lambda-object form is a definition and can appear anywhere any other <definition> can appear. Each time define-lambda-object form is evaluated, a new group is created with distinct <group>, <constructor>, and <predicate> procedures.

The <group> is bound to a procedure of one argument. Like a gene, it has information on its <parent group>s, <constructor>s, <predicate>, and the number and properties of <field>s. And they are checked out whenever define-lambda-object form is evaluated. In case of inheritance, all the <field>s of <parent group>s must exist in the <field spec> of the child group, irrespectively of the order. Otherwise an error is signaled. In addition, the properties (mutability, sort of field, and default expression) of <field>s of unamendable groups must be preserved in contrast with those of amendable groups. Otherwise an error is signaled.

The <constructor> is bound to a procedure that takes at least as many arguments as the number of <required field>s. Whenever it is called, it returns an object of the <group>, namely a procedure, which has information on its own group and all that goes with it. Its first argument must be a symbol of the same name as <field>. Otherwise an error is signaled. The object becomes an accessor procedure of each <field> in case of one argument and a mutator procedure of each <field> in case of two arguments where the second argument is a new field value.

The names of <field>s are used to access the <field>s as symbols of the same names. So they must be distinct. Otherwise an error is signaled. The read-write fields can be modified, whereas any attempt to modify the values of the read-only fields via mutators signals an error. Note: The read-only fields are not immutable. Their values, for instance, can be modified by other fields whose values work like their mutators.

The <required field> is initialized to the first one of the remaining arguments. If there are no more remaining arguments, an error is signaled.

The initialization of the <optional field>s is done by two types of <constructor>s:

  1. <make-`group-name'> constructor The initialization method of <optional field>s is the same as that of <required field>s except that the field is bound to the <default> instead of signaling an error if there are no more remaining arguments.
  2. <make-`group-name'-by-name> constructor The name used at a call site for the corresponding <optional field> is a symbol of the same name as the <field>. The remaining arguments are sequentially interpreted as a series of pairs, where the first member of each pair is a field name and the second is the corresponding value. If there is no element for a particular field name, the field is initialized to the <default>.

The <automatic common field>s are initialized to each corresponding <default> that is evaluated at the time the define-lambda-object form is evaluated, and the values are shared with all the lambda objects that are maded by the constructors of the define-lambda-object form. The other <automatic field>s except <automatic virtual field>s are initialized to each corresponding <default> that is evaluated at the time the lambda object is made by a constructor. The <hidden field> is an externally nonexistent field, that is, the field is invisible outside of the define-lambda-object form but visible inside of it. On the contrary, the <virtual field> is an internally nonexistent field whose <default> is evaluated each time when the field is accessed.

The <predicate> is a predicate procedure that returns #t for objects constructed by <constructor> or <constructor>s for child groups and #f for everything else.

Examples

;; The `x' is a read-write field.
;; The `y' is a read-only field.
(define-lambda-object ppoint (x) y)

(define pp  (make-ppoint 10 20))
(pp 'x)                                 => 10
(pp 'y)                                 => 20
(pp 'x 11) (pp 'x)                      => 11
(pp 'y 22)                              => error: read-only field y

;; The parent group `ppoint' is an unamendable group.
(define-lambda-object (cpoint ppoint) x y color)
                              => error: incompatible read-write field ppoint x

;; The 'color-init' and 'area-init' are automatic fields.
;; The 'color' and 'area' are virtual fields.
(define color 'black)
(define-lambda-object (cpoint ppoint)
  (x) y
  (,color-init color) (,area-init (* x y))
  (`,color color) (`,area (* x y)))

(define ap (make-cpoint 3 33 'black))           => error: expects 2 arguments
(define ap (make-cpoint 10 20))
(map ap '(x y color-init color area-init area)) => (10 20 black black 200 200)
(ap 'x 30)
(map ap '(x y color-init color area-init area)) => (30 20 black black 200 600)
(set! color 'white)
(map ap '(x y color-init color area-init area)) => (30 20 black white 200 600)

;; The 'color' is an automatic common field.
(define-lambda-object (cpoint ppoint)
  (x) y
  ((,,color) color)
  (`,area (* x y))
  (,set/add (lambda (i j) (set! x (+ i x)) (set! y (+ j y)))))

(define tp (make-cpoint 10 15))
(map tp '(x y color area))              => (10 15 white 150)
(define cp (make-cpoint 15 20))
(map cp '(x y color area))              => (15 20 white 300)
(cp 'color 'brown)
((cp 'set/add) 5 10)
(map cp '(x y color area))              => (20 30 brown 600)
(map tp '(x y color area))              => (10 15 brown 150)
(cpoint? ap)                            => #f
(cpoint? tp)                            => #t
(cpoint? cp)                            => #t
(ppoint? cp)                            => #t

;; The parent group `ppoint' is an amendable group.
;; The 'stack' is an optional hidden field.
;; The 'pop' is a virtual field.
;; The 'push' is an automatic field.
(define-lambda-object (spoint (ppoint))
  (x 0) (y x) (z x) ('stack '())
  (`,pop (if (null? stack)
             (error 'spoint "null stack" stack)
             (let ((s (car stack))) (set! stack (cdr stack)) s)))
  (,push (lambda (s) (set! stack (cons s stack)))))

(define sp (make-spoint))
(map sp '(x y z))                       => (0 0 0)
(define sp (make-spoint 5 55))
(map sp '(x y z))                       => (5 55 5)
(define sp (make-spoint-by-name 'z 100 'stack (list 'sunflower)))
(map sp '(x y z))                       => (0 0 100)
((sp 'push) 'rose) ((sp 'push) 'lily)
(sp 'pop)                               => lily
(sp 'pop)                               => rose
(sp 'pop)                               => sunflower
(sp 'pop)                               => error: null stack ()
(sp 'stack)                             => error: absent field stack

;; The 'stack' is an automatic hidden field.
;; The `set/add' is the same automatic field as that of `cpoint' group,
;; but it has a different default which simulates polymorphism and overloading.
(define-lambda-object (epoint (spoint) (cpoint))
  ((x) 5) ((y) 10) ((z) 15) ((planet) "earth")
  (,,color "brown")
  (',stack '())
  (`,area (* x y))
  (`,volume (* x y z))
  (`,pop (if (null? stack)
             (error 'spoint "null stack" stack)
             (let ((s (car stack))) (set! stack (cdr stack)) s)))
  (,push (lambda (s) (set! stack (cons s stack))))
  (,adbmal (lambda (f) (f x y z color planet (* x y) (* x y z))))
  (,set/add
   (case-lambda
    ((i j) (cond
            ((and (string? i) (string? j)) (set! color i) (set! planet j))
            ((and (number? i) (number? j)) (set! x (+ i x)) (set! y (+ j y)))
            (else (error 'epoint "set/add: wrong data type" i j))))
    ((i j k) (set! x (+ i x)) (set! y (+ j y)) (set! z (+ k z))))))

(define ep (make-epoint-by-name 'planet "jupiter"))
((ep 'adbmal) vector)                   => #(5 10 15 "brown" "jupiter" 50 750)
(define tp (make-epoint 10 15 20))
((tp 'adbmal) vector)                   => #(10 15 20 "brown" "earth" 150 3000)
(map (lambda (o) (o 'x)) (list pp ap cp sp ep))
                                        => (11 30 20 0 5)
(map (lambda (p) (p ep)) (list ppoint? cpoint? spoint? epoint?))
                                        => (#t #t #t #t)
((ep 'set/add) "red" "mars")
((ep 'adbmal) list)                     => (5 10 15 "red" "mars" 50 750)
((tp 'adbmal) list)                     => (10 15 20 "red" "earth" 150 3000)
((ep 'set/add) 5 10)
((ep 'adbmal) list)                     => (10 20 15 "red" "mars" 200 3000)
((ep 'set/add) 10 30 50)
(map ep '(x y z area volume))           => (20 50 65 1000 65000)
(map cp '(x y area))                    => (20 30 600)
((cp 'set/add) 20 50)
(map cp '(x y area))                    => (40 80 3200)
((cp 'set/add) 10 100 1000)             => error: expects 2 arguments

epoint                          => #<procedure:epoint>
(epoint 'parent)                => (#<procedure:spoint> #<procedure:cpoint>)
(epoint 'constructor)           => (#<procedure:make-epoint> #<procedure:make-epoint-by-name>)
(epoint 'predicate)             => #<procedure:epoint?>
(epoint 'read-write-field)      => (x y z planet)
(epoint 'read-only-field)       => (color area volume pop push adbmal set/add)
(epoint 'required-field)        => ()
(epoint 'optional-field)        => ((x 5) (y 10) (z 15) (planet "earth"))
(epoint 'common-field)          => ((color "brown"))
(epoint 'hidden-field)          => ((stack '()))
(epoint 'virtual-field)         => ((area (* x y))
                                    (volume (* x y z))
                                    (pop (if (null? stack)
                                             (error 'spoint "null stack" stack)
                                             (let ((s (car stack)))
                                               (set! stack (cdr stack)) s))))
(epoint 'automatic-field)
=>((color "brown")
   (area (* x y))
   (volume (* x y z))
   (pop
    (if (null? stack)
        (error 'spoint "null stack" stack)
        (let ((s (car stack))) (set! stack (cdr stack)) s)))
   (stack '())
   (push (lambda (s) (set! stack (cons s stack))))
   (adbmal (lambda (f) (f x y z color planet (* x y) (* x y z))))
   (set/add
    (case-lambda
     ((i j)
      (cond
       ((and (string? i) (string? j)) (set! color i) (set! planet j))
       ((and (number? i) (number? j)) (set! x (+ i x)) (set! y (+ j y)))
       (else (error 'epoint "set/add: wrong data type" i j))))
     ((i j k) (set! x (+ i x)) (set! y (+ j y)) (set! z (+ k z))))))

Implementation

The implementation is written in R6RS hygienic macro and define-macro.

The predicate procedure is implementation dependant. For instance, a procedure such as procedure-name or object-name, which returns the name of procedure or object, must be available to distinguish objects created by all the constructors from the others.

References

[R6RS]      Michael Sperber, R. Kent Dybvig, Matthew Flatt, and
            Anton von Straaten:
            Revised(6) Report on the Algorithmic Language Scheme
            http://www.r6rs.org
[SRFI 9]    Richard Kelsey: Defining Record Type
            https://srfi.schemers.org/srfi-9
[On Lisp]   Paul Graham:
            http://www.paulgraham.com/onlisp.html

Copyright

Copyright (c) 2009 Joo ChurlSoo.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ``Software''), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Editor: Mike Sperber