Outdated egg!

This is an egg for CHICKEN 4, the unsupported old release. You're almost certainly looking for the CHICKEN 5 version of this egg, if it exists.

If it does not exist, there may be equivalent functionality provided by another egg; have a look at the egg index. Otherwise, please consider porting this egg to the current version of CHICKEN.

datatype

  1. Outdated egg!
  2. 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
  3. Last update
  4. Author
  5. License
  6. 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)

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 simple-cells)

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

;; Couple objects
(define-object-type
  COUPLE couple? make-couple (
    (parent obj?)
    (cell-1 cell?)
    (cell-2 cell?))
  (override)
  ((First) (cell-1))
  ((Second) (cell-2))
  ((First-set! (value number?)) (cell-1 value))
  ((Second-set! (value number?)) (cell-2 value)))

(define cpl
  (make-couple obj (cell 1) (cell 2)))
(couple? cpl) ; -> #t
(object? cpl) ; -> #t
(not (couple? First)) ; -> #t
(cpl (Types))
(cpl (Info)) ; -> ((Types) ... (First) ... (First-set! (value number?)) ...
(cpl (Invariant)) ; -> (and (parent (Invariant)) (cell-1 cell?) ...
(cpl (Ancestors))
(= (cpl (First)) 1) ; -> #t
(= (cpl (Second)) 2) ; -> #t
(cpl (First-set! 10)) ; -> 1 (= old value of cell-1)
(cpl (Second-set! 20)) ; -> 2 (= old value of cell-2)
(= (cpl (First)) 10) ; -> #t
(= (cpl (Second)) 20) ; -> #t

;; Triple objects
(define-object-type
  TRIPLE triple? make-triple (
    (parent couple?)
    (cell-3 cell?))
  (override)
  ((Third) (cell-3))
  ((Third-set! (value number?)) (cell-3 value)))

(define trp (make-triple cpl (cell 3)))
(trp (Ancestors))
(trp (Info)) ; -> ((Types) ... (First) ... (Third) (Third-set! (value number?)))
(= (trp (Third)) 3) ; -> #t
(trp (Third-set! 30)) ; -> 3 (= old value of cell-3)
(= (trp (Third)) 30) ; -> #t
(= (trp (First)) 20) ; -> #f
(= (trp (Second)) 20) ; -> #t
(trp (Second-set! 2)) ; -> 20 (= old value of cell-2)
(= (trp (Second)) 2) ; -> #t
(trp (First-set! 25)) ; -> 10 (= old value of cell-1)
(= (trp (First)) 100) ; -> #f
(triple? trp) ; -> #t
(not (triple? cpl)) ; -> #t
(couple? trp) ; -> #t
(object? trp) ; -> #t

;; 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