#|[ Author: Juergen Lorenz ju (at) jugilo (dot) de 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. ]|# ; This is a variant of the datatype egg, written by Felix Winkelmann, ; which in turn is patterned after the datatype facility described in the ; classic "Essentials of programming languages" (EOPL) by Friedman, Wand ; and Haynes. It simplyfies Felix' code by using the facilities of my ; bindings egg, changes the names and syntax a little and adds abstract ; types. Concrete types are variant records as EOPL's datatypes, but ; constructor arguments can accept zero or more predicates, and abstract ; types hide the constructors of its implementing concrete types like the ; corresponding language construct of ML. (require-library bindings) (module datatypes (define-concrete-type define-abstract-type (concrete-case invoke) datatypes) (import scheme chicken bindings (only data-structures conjoin disjoin o)) (import-for-syntax (only bindings macro-rules bindable? bind-case)) (define-macro define-concrete-type (macro-rules () ((_ TYPE type? . variants) (where ((list-of? list?) variants) (not (null? variants)) ((list-of? symbol?) (map car variants)) ((list-of? list?) (map cdr variants))) `(begin (define (,type? form) (##sys#structure? form ',TYPE)) ,@(map (lambda (variant) (bind-case variant ((variantname . checkedargs) (let ((variantargs (map car checkedargs)) (variantpreds (map cdr checkedargs))) `(define (,variantname ,@variantargs) (##sys#make-structure ',TYPE ',variantname ,@(map (lambda (name pred) `(if (##core#check (,pred ,name)) ,name (##sys#signal-hook #:type-error "bad argument type to variant constructor" ,name ',variantname ',name))) variantargs (map (lambda (preds) `(conjoin ,@preds)) variantpreds)))))))) variants))))) (define-macro concrete-case (macro-rules (else) ((_ TYPE xpr . clauses) (where ((conjoin (list-of? list?) (disjoin (bindable? ((Constr . args) . xprs)) (bindable? (else . xprs)))) clauses) (not (null? clauses))) `(let ((tmp ,xpr)) (if (##core#check (##sys#structure? tmp ',TYPE)) (let ((tag (##sys#slot tmp 1))) (cond ,@(map (lambda (clause) (bind-case clause (((variantname . variantargs) xpr . xprs) `((eq? tag ',variantname) (invoke ',variantname tmp ,(length variantargs) (lambda ,variantargs ,xpr ,@xprs)))) ((else xpr . xprs) `(else (let () ,xpr ,@xprs))) )) clauses))) (##sys#signal-hook #:type-error "bad argument type to `concrete-case'" tmp ',TYPE)))))) (define (invoke name block count proc) (apply proc (let ((limit (fx- (##sys#size block) 2))) (let rec ((i 0)) (cond ((fx>= i count) '()) ((fx>= i limit) (error 'concrete-case "too many record fields accessed" name block)) (else (cons (##sys#slot block (fx+ i 2)) (rec (fx+ i 1))))))))) (define-macro define-abstract-type (macro-rules (with printer reader) ((_ TYPE type? (variant . variants) (with routine . routines) (printer . printers) (reader . readers)) (where ((conjoin (list-of? list?) (o not null?)) variants)) (let ((variants (cons variant variants)) (routines (cons routine routines))) (let ((names (map caar routines)) (args (map cdar routines)) (bodies (map cdr routines))) `(define-values (,type? ,@names) (letrec ,(map (lambda (n) `(,n #f)) names) (define-concrete-type ,TYPE ,type? ,variants) ,@(map (lambda (n a b) `(set! ,n (lambda ,a ,@b))) names args bodies) ,(if (null? printers) #f `(define-record-printer ,TYPE ,(car printers))) ,(if (null? readers) #f `(define-reader-ctor ',TYPE ,(car readers))) (values ,type? ,@names)))))) ((_ TYPE type? (variant . variants) (with . routines) (printer . printers)) `(define-abstract-type ,TYPE ,type? (,variant ,@variants) (with ,@routines) (printer ,@printers) (reader))) ((_ TYPE type? (variant . variants) (with . routines)) `(define-abstract-type ,TYPE ,type? (,variant ,@variants) (with ,@routines) (printer) (reader))))) ;; documentation procedure (define datatypes (let ( (signatures '( (define-concrete-type TYPE type? (Constructor (arg arg? ...) ...) ....) (concrete-case TYPE obj ((Constructor arg ...) xpr . xprs) .... (else xpr . xprs) ..) (define-abstract-type TYPE type? (Constructor (arg arg? ...) ...) .... (with ((proc . args) xpr . xprs) ....) (printer (lambda (obj out) xpr . xprs)) .. (reader proc) ..) )) ) (case-lambda (() (map car signatures)) ((sym) (assq sym signatures))))) ) ; module datatypes