((section 2 "Outdated egg!" (p "This is an egg for CHICKEN 4, the unsupported old release.  You're almost certainly looking for " (int-link "/eggref/5/datatypes" "the CHICKEN 5 version of this egg") ", if it exists.") (p "If it does not exist, there may be equivalent functionality provided by another egg; have a look at the " (link "https://wiki.call-cc.org/chicken-projects/egg-index-5.html" "egg index") ". Otherwise, please consider porting this egg to the current version of CHICKEN.") (tags "egg")) (section 2 "datatype" (toc) (section 3 "Description" (p "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.")) (section 3 "Documentation" (p "This documentation uses special ellipses meaning") (ul (li "two dots: zero or one") (li "three dots: zero or many") (li "four dots: one or many occurences of the item to its left.")) (section 4 "datatypes" (def (sig (procedure "(datatypes sym ..)" (id datatypes))) (p "shows the list of available exported symbols of the module when called without argument or the signature of that very argument."))) (section 4 "define-concrete-type" (def (sig (syntax "(define-concrete-type TYPE type? (Constructor (arg arg? ...) ...) ....)" (id define-concrete-type))) (p "defines a concrete type TYPE withe type predicate type? and with variant constructors Constructor ...., whose arguments arg ... are checked by arg? ...  respectively."))) (section 4 "concrete-case" (def (sig (syntax "(concrete-case (obj type?) ((Constructor arg ...) xpr ....) ....  (else xpr ....) ..)" (id concrete-case))) (p "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."))) (section 4 "define-abstract-type" (def (sig (syntax "(define-abstract-type TYPE type? (Constructor (arg arg? ...) ...) ....  (with ((proc arg ...) xpr ....) ....) (printer (lambda (obj out) xpr ....)) ..  (reader proc) ..)" (id define-abstract-type))) (p "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."))) (section 4 "define-object-type" (def (sig (syntax "(define-object-type CHILD child? make-child ((parent parent?) (z z? ...) ...) (override  ((A a ...) apr ...) ...) ((X (x x? ...) ...) xpr ...) ....)" (id define-object-type))) (p "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? ...") (p "The constructor's argunemts are checked by parent? and z? ... respectively.") (p "In the override clause signatures (A a ...) and overridden bodies apr apr ...  of messages already exported and argment-checked by a parent are given."))) (section 4 "make-base-object" (def (sig (procedure "(make-base-object)" (id make-base-object))) (p "creates the base object, which exports the messages (Types), (Info), (Invariant) and (Ancestors), which will be automatically overridden in each child."))) (section 4 "object?" (def (sig (procedure "(object? xpr)" (id object?))) (p "the type predicate for all objects."))) (section 4 "Types" (def (sig (procedure "(Types)" (id Types))) (p "passing this message to an object will print its type hierarchy."))) (section 4 "Info" (def (sig (procedure "(Info)" (id Info))) (p "passing this message to an object will print the documentation of all exported messages including its preconditions."))) (section 4 "Invariant" (def (sig (procedure "(Invariant)" (id Invariant))) (p "passing this message to an object will print its invariant or #f, hence can be used as predicate."))) (section 4 "Ancestors" (def (sig (procedure "(Ancestors)" (id Ancestors))) (p "passing this message to an object will list the ancestors of the object."))) (section 4 "Examples" (highlight scheme ";; immutable lists as a  concrete type\n(define-concrete-type LIST List?\n  (List-null)\n  (List-cons (first) (rest List?)))\n\n(define (Null? obj)\n  (concrete-case (obj List?)\n    ((List-null) #t)\n    (else #f)))\n\n(define (List-first obj)\n  (concrete-case (obj List?)\n    ((List-null)\n     (error 'List-first))\n    ((List-cons first rest)\n     first)))\n\n(define (List-rest obj)\n  (concrete-case (obj List?)\n    ((List-null)\n     (error 'List-rest))\n    ((List-cons first rest)\n     rest)))\n\n;; Integers as chains\n(define-concrete-type CHAIN chain?\n  (Chain-link (item integer? (lambda (x) (>= x 0)))\n              (next procedure?)))\n\n(define (integers n) \n  (Chain-link n integers))\n\n(define (chain-item n xpr)\n  (concrete-case (xpr chain?)\n    ((Chain-link i fn)\n     (if (= n 1)\n       i\n       (chain-item (- n 1) (fn (+ i 1)))))))\n\n;; Points as an abstract type\n(define-abstract-type POINT point?\n  (Point (x number?) (y number?))\n  (with\n    ((make-point x y) (Point x y))\n    ((point-x pt)\n     (concrete-case (pt point?)\n       ((Point x y) x)))\n    ((point-y pt)\n     (concrete-case (pt point?)\n       ((Point x y) y))))\n  (printer\n    (lambda (pt out)\n      (display \"#,(POINT \" out)\n      (display (point-x pt) out)\n      (display \" \" out)\n      (display (point-y pt) out)\n      (display \")\\n\" out)))\n  (reader Point))\n\n(use simple-cells)\n\n;; Base object\n(define obj (make-base-object))\n\n;; Couple objects\n(define-object-type\n  COUPLE couple? make-couple (\n    (parent obj?)\n    (cell-1 cell?)\n    (cell-2 cell?))\n  (override)\n  ((First) (cell-1))\n  ((Second) (cell-2))\n  ((First-set! (value number?)) (cell-1 value))\n  ((Second-set! (value number?)) (cell-2 value)))\n\n(define cpl\n  (make-couple obj (cell 1) (cell 2)))\n(couple? cpl) ; -> #t\n(object? cpl) ; -> #t\n(not (couple? First)) ; -> #t\n(cpl (Types))\n(cpl (Info)) ; -> ((Types) ... (First) ... (First-set! (value number?)) ...\n(cpl (Invariant)) ; -> (and (parent (Invariant)) (cell-1 cell?) ...\n(cpl (Ancestors))\n(= (cpl (First)) 1) ; -> #t\n(= (cpl (Second)) 2) ; -> #t\n(cpl (First-set! 10)) ; -> 1 (= old value of cell-1)\n(cpl (Second-set! 20)) ; -> 2 (= old value of cell-2)\n(= (cpl (First)) 10) ; -> #t\n(= (cpl (Second)) 20) ; -> #t\n\n;; Triple objects\n(define-object-type\n  TRIPLE triple? make-triple (\n    (parent couple?)\n    (cell-3 cell?))\n  (override)\n  ((Third) (cell-3))\n  ((Third-set! (value number?)) (cell-3 value)))\n\n(define trp (make-triple cpl (cell 3)))\n(trp (Ancestors))\n(trp (Info)) ; -> ((Types) ... (First) ... (Third) (Third-set! (value number?)))\n(= (trp (Third)) 3) ; -> #t\n(trp (Third-set! 30)) ; -> 3 (= old value of cell-3)\n(= (trp (Third)) 30) ; -> #t\n(= (trp (First)) 20) ; -> #f\n(= (trp (Second)) 20) ; -> #t\n(trp (Second-set! 2)) ; -> 20 (= old value of cell-2)\n(= (trp (Second)) 2) ; -> #t\n(trp (First-set! 25)) ; -> 10 (= old value of cell-1)\n(= (trp (First)) 100) ; -> #f\n(triple? trp) ; -> #t\n(not (triple? cpl)) ; -> #t\n(couple? trp) ; -> #t\n(object? trp) ; -> #t\n\n;; Note that Messages are precondition-checked and constructors pass the\n;; (Invariant) message only if their argument types are ok.\n"))) (section 3 "Requirements" (p "none"))) (section 2 "Last update" (p "Jan 06, 2016")) (section 2 "Author" (p (int-link "/users/juergen-lorenz" "Juergen Lorenz"))) (section 2 "License" (pre "Copyright (c) 2015-2016, Juergen Lorenz\nAll rights reserved.") (pre "Redistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are\nmet:\n\nRedistributions of source code must retain the above copyright\nnotice, this list of conditions and the following disclaimer.\n\nRedistributions in binary form must reproduce the above copyright\nnotice, this list of conditions and the following disclaimer in the\ndocumentation and/or other materials provided with the distribution.\nNeither the name of the author nor the names of its contributors may be\nused to endorse or promote products derived from this software without\nspecific prior written permission. \n  \nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS\nIS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED\nTO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A\nPARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\nHOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\nSPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED\nTO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\nPROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\nLIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\nNEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.")) (section 2 "Version History" (dl (dt "1.3") (dd "dependencies removed, bug in abstract types fixed") (dt "1.2") (dd "object types added") (dt "1.1") (dd "syntax-change in concrete-case") (dt "1.0") (dd "initial import"))))