You are looking at historical revision 33027 of this page. It may differ significantly from its current revision.

datatype

Description

An implementation of concrete, abstract and object types. Concrete types are discriminated variant records, as advocated in the classic "Essentials of Programming Languages" by Friedman, Wand and Haynes and ported to Chicken by Felix Winkelmann. Abstract types are based on concrete types, but hide the variant constructors and export constructor and accessor procedures instead. Object types export message handlers as well as the messages understood by that handler, the latter being constructors of concrete types. Note that the arguments of variant constructors can accept zero or multiple predicates. Felix' code is simplyfied by using the facilities of the bindings egg.

Documentation

This documentation uses special ellipses meaning

occurences of the item to its left.

datatypes

[procedure] (datatypes sym ..)

shows the list of available exported symbols of the module when called without argument or the signature of that very argument.

define-concrete-type

[syntax] (define-concrete-type TYPE type? (Constructor (arg arg? ...) ...) ....)

defines a concrete type TYPE withe type predicate type? and with variant constructors Constructor ...., whose arguments arg ... are checked by arg? ... respectively.

concrete-case

[syntax] (concrete-case (obj type?) ((Constructor arg ...) xpr . xprs) .... (else xpr . xprs) ..)

the one macro which replaces in concrete datatypes the many accessor routines by destructuring the Constructors' arguments via pattern matching. The macro checks first, if obj passes the type? test and then tries to match obj against the variant constructors, invoking the body of the first matching one. This body xpr . xprs has access to the constructor's arguments arg ... Note, that the syntax of concrete-case differs from that of cases.

define-abstract-type

[syntax] (define-abstract-type TYPE type? (Constructor (arg arg? ...) ...) .... (with ((proc . args) xpr . xprs) ....) (printer (lambda (obj out) xpr . xprs)) .. (reader proc) ..)

like define-concrete-type, but the variant constructors Constructor .... are hidden and procedures (proc . args) .... are exported instead. Only those procedures have access to the variant constructors. printers and readers are optional, but they need access to the variant constructors, so they must be defined here. They use define-record-printer and define-reader-ctor.

define-object-type

[syntax] (define-object-type CHILD child? make-child ((parent parent?) (z z? ...) ...) (override ((A a ...) . aprs) ...) ((X (x x? ...) ...) . xprs) ....)

exports a constructor, (make-child parent z ...), a predicate, child?, as well as messages (X x ...) ... with body xprs, whose arguments x are checked by preconditions x? ...

The constructor's argunemts are checked by parent? and z? ... respectively.

In the override clause signatures (A a ...) and overridden bodies aprs of messages already exported and argment-checked by a parent are given.

make-base-object

[procedure] (make-base-object)

creates the base object, which exports the messages (Types), (Info), (Invariant) and (Ancestors), which will be automatically overridden in each child.

object?

[procedure] (object? xpr)

the type predicate for all objects.

Types

[procedure] (Types)

passing this message to an object will print its type hierarchy.

Info

[procedure] (Info)

passing this message to an object will print the documentation of all exported messages including its preconditions.

Invariant

[procedure] (Invariant)

passing this message to an object will print its invariant or #f, hence can be used as predicate.

Ancestors

[procedure] (Ancestors)

passing this message to an object will list the ancestors of the object.

Examples


;; immutable lists as a  concrete type
(define-concrete-type LIST List?
  (List-null)
  (List-cons (first) (rest List?)))

(define (Null? obj)
  (concrete-case (obj List?)
    ((List-null) #t)
    (else #f)))

(define (List-first obj)
  (concrete-case (obj List?)
    ((List-null)
     (error 'List-first))
    ((List-cons first rest)
     first)))

(define (List-rest obj)
  (concrete-case (obj List?)
    ((List-null)
     (error 'List-rest))
    ((List-cons first rest)
     rest)))

;; Integers as chains
(define-concrete-type CHAIN chain?
  (Chain-link (item integer? (lambda (x) (>= x 0)))
              (next procedure?)))

(define (integers n) 
  (Chain-link n integers))

(define (chain-item n xpr)
  (concrete-case (xpr chain?)
    ((Chain-link i fn)
     (if (= n 1)
       i
       (chain-item (- n 1) (fn (+ i 1)))))))

;; Points as an abstract type
(define-abstract-type POINT point?
  (Point (x number?) (y number?))
  (with
    ((point x y) (Point x y))
    ((point-x pt)
     (concrete-case (pt point?)
       ((Point x y) x)))
    ((point-y pt)
     (concrete-case (pt point?)
       ((Point x y) y))))
  (printer
    (lambda (pt out)
      (display "#,(POINT " out)
      (display (point-x pt) out)
      (display " " out)
      (display (point-y pt) out)
      (display ")\n" out)))
  (reader Point))

;; Points as objects
(use cells)
(define-object-type POINT point? make-point
  ((parent object?) (x (cell-of? number?)) (y (cell-of? number?)))
   (override)
   ; no overrides except those of base object
  ;; new messages with hanlers
  ((X) (cell-ref x))
  ((Y) (cell-ref y))
  ((X-set! (arg number?))
   (set! (cell-ref x) arg))
  ((Y-set! (arg number?))
   (set! (cell-ref y) arg))
  )
(define-object-type POINT_3D point-3d? make-point-3d
  ((parent point?) (z (cell-of? number?)))
  (override ((X) (* 2 (parent (X))))
            ;; preconditions checked in parent
            ;; hence args without predicates
            ((X-set! arg)
             (parent (X-set! (* 2 arg)))))
  ((Z) (cell-ref z))
  ((Z-set! (arg number?))
   (set! (cell-ref z) arg))
  )
(define obj (make-base-object))
(define p2
  (make-point obj (cell 1) (cell 2)))
(define p3
  (make-point-3d p2 (cell 3)))
;; Now you can access
(p2 (Info))
(p3 (Invariant))
(p2 (X))
(p3 (X)) ;; overridden
(p2 (Y-set! 20))
;; ...
;; Note that Messages are precondition-checked and constructors pass the
;; (Invariant) message only if their argument types are ok.

Requirements

bindings

Last update

June 25, 2015

Author

Juergen Lorenz

License

Copyright (c) 2015, Juergen Lorenz
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
Neither the name of the author nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission. 
  
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Version History

1.2
object types added
1.1
syntax-change in concrete-case
1.0
initial import