datatype

  1. datatype
    1. Description
    2. Documentation
      1. datatypes
      2. define-concrete-type
      3. concrete-case
      4. define-abstract-type
      5. define-object-type
      6. make-base-object
      7. object?
      8. Types
      9. Info
      10. Invariant
      11. Ancestors
      12. Examples
    3. Requirements
  2. Last update
  3. Author
  4. License
  5. Version History

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.

Documentation

This documentation uses special ellipses meaning

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 ....) .... (else xpr ....) ..)

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 arg ...) xpr ....) ....) (printer (lambda (obj out) xpr ....)) .. (reader proc) ..)

like define-concrete-type, but the variant constructors Constructor .... are hidden and procedures (proc arg ...) .... 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 ...) apr ...) ...) ((X (x x? ...) ...) xpr ...) ....)

exports a constructor, (make-child parent z ...), a predicate, child?, as well as messages (X x ...) ... with body xpr ..., 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 apr apr ... of messages already exported and argment-checked by a parent are given.

make-base-object

</procedure>(make-base-object)</procedure>

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
    ((make-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))

(use cells)

;; Base object
(define obj (make-base-object))

;; Couple objects
(define cpl
  (make-couple obj (cell 1) (cell 2)))
(couple? cpl)
(object? cpl)
(not (couple? First))
(cpl (Types))
(cpl (Info))
(cpl (Invariant))
(cpl (Ancestors))
(= (cpl (First)) 1)
(= (cpl (Second)) 2)
(cpl (First-set! 10))
(cpl (Second-set! 20))
(= (cpl (First)) 10)
(= (cpl (Second)) 20)
    
;; Triple objects
(define trp (make-triple cpl (cell 3)))
(trp (Ancestors))
(trp (Info))
(= (trp (Third)) 3)
(trp (Third-set! 30))
(= (trp (Third)) 30)
(= (trp (First)) 20)
(= (trp (Second)) 20)
(trp (Second-set! 2))
(= (trp (Second)) 2)
(trp (First-set! 25))
(= (trp (First)) 100)
(triple? trp)
(not (triple? cpl))
(couple? trp)
(object? trp)

;; Note that Messages are precondition-checked and constructors pass the
(Invariant) message only if their argument types are ok.

Requirements

none

Last update

Jan 06, 2016

Author

Juergen Lorenz

License

Copyright (c) 2015-2016, 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.3
dependencies removed, bug in abstract types fixed
1.2
object types added
1.1
syntax-change in concrete-case
1.0
initial import