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

datatype

Description

An implementation of concrete and abstract types, the former being discriminated variant records, as advocated in the classic "Essentials of Programming Languages" by Friedman, Wand and Haynes and ported to Chicken by Felix Winkelmann. The latter are based on the former, but hide the variant constructors and export constructor and accessor procedures instead. 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: -- two dots means zero or one -- three dots zero or many -- four dots one or many appearances 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 TYPE obj ((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. Note, that the syntax of concrete-case differs from that of cases.

define-abstract-type

<macro>(define-abstract-type TYPE type? (Constructor (arg arg? ...) ...) ....

       (with ((proc . args) xpr . xprs) ....)
       (printer (lambda (obj out) xpr . xprs)) ..
       (reader proc) ..)</macro>

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.

Examples


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

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

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

(define (List-rest obj)
  (concrete-case LIST obj
    ((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 CHAIN xpr
    ((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 POINT pt
       ((Point x y) x)))
    ((point-y pt)
     (concrete-case POINT pt
       ((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))

Requirements

bindings

Last update

May 31, 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.0
initial import