#|[ Author: Juergen Lorenz ju (at) jugilo (dot) de 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. ]|# ; 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 as well as object types. Concrete types are variant records ; as EOPL's datatypes, but constructor arguments can accept zero or more ; predicates, abstract types hide the constructors of its implementing ; concrete types like the corresponding language construct of ML, and ; object types are message-handlers, where the messages are defined as ; concrete-types. (module datatypes (define-concrete-type (concrete-case invoke) define-abstract-type define-object-type make-base-object object? Types Info Invariant Ancestors datatypes) (import scheme chicken (only data-structures conjoin list-of?)) (import-for-syntax (only chicken assert)) (define-syntax define-concrete-type (ir-macro-transformer (lambda (form inject compare?) (let ((TYPE (cadr form)) (type? (caddr form)) (variants (cdddr form))) (assert (pair? variants)) (assert ((list-of? pair?) variants)) (assert ((list-of? symbol?) (map car variants))) `(begin (define (,type? form) (##sys#structure? form ',TYPE)) ,@(map (lambda (variant) (let ((variantname (car variant)) (checkedargs (cdr variant))) (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-syntax concrete-case (ir-macro-transformer (lambda (form inject compare?) (let ((pair (cadr form)) (clauses (cddr form))) (assert (pair? clauses)) (assert ((list-of? pair?) clauses)) (assert ((list-of? pair?) (map cdr clauses))) (assert ((list-of? (lambda (x) (or (compare? x 'else) (and (pair? x) (symbol? (car x)))))) (map car clauses))) (let ((xpr (car pair)) (type? (cadr pair))) `(let ((tmp ,xpr)) (if (##core#check (,type? tmp)) (let ((tag (##sys#slot tmp 1))) (cond ,@(map (lambda (clause) (let ((first (car clause)) (xpr (cadr clause)) (xprs (cddr clause))) (if (pair? first) (let ((variantname (car first)) (variantargs (cdr first))) `((eq? tag ',variantname) (invoke ',variantname tmp ,(length variantargs) (lambda ,variantargs ,xpr ,@xprs)))) `(else (let () ,xpr ,@xprs))))) clauses))) (##sys#signal-hook #:type-error "typecheck didn't pass in concrete-case" `(,',type? ,tmp))))))))) (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-syntax define-abstract-type (ir-macro-transformer (lambda (form inject compare?) (let ((TYPE (cadr form)) (type? (caddr form)) (rest (reverse (cdddr form)))) (cond ((compare? (caar rest) 'reader) (define variants (reverse (list-tail rest 3))) (define routines (cdr (list-ref rest 2))) (define printer (cadr (list-ref rest 1))) (define reader (cadr (list-ref rest 0)))) ((compare? (caar rest) 'printer) (define variants (reverse (list-tail rest 2))) (define routines (cdr (list-ref rest 1))) (define printer (cadr (list-ref rest 0))) (define reader #f)) ((compare? (caar rest) 'with) (define variants (reverse (list-tail rest 1))) (define routines (cdr (list-ref rest 0))) (define printer #f) (define reader #f)) (else (error 'define-abstract-type "syntax error"))) (assert (pair? variants)) (assert ((list-of? pair?) variants)) (assert (pair? routines)) (assert ((list-of? pair?) 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 printer `(define-record-printer ,TYPE ,printer)) ,(if reader `(define-reader-ctor ',TYPE ,reader)) (values ,type? ,@names)))))))) ;;; (define-object-type CHILD child? make-child ;;; ((parent parent?) (x x? ...) ...) ; state variables ;;; (override ((A a ...) apr . aprs) ...) ; overridden messages ;;; ((X (x x? ...) ...) xpr . xprs) ; new messages ;;; ....) ;;; ------------------------------------------- (define-syntax define-object-type (ir-macro-transformer (lambda (form inject compare?) (let ((CHILD (list-ref form 1)) (child? (list-ref form 2)) (make-child (list-ref form 3)) (parent-clause (list-ref form 4)) (override-clause (list-ref form 5)) (body (list-tail form 6))) (assert (pair? parent-clause)) (assert (pair? (car parent-clause))) (assert (pair? override-clause)) (assert (compare? (car override-clause) 'override)) (assert (pair? body)) (assert ((list-of? pair?) (map car body))) (assert ((list-of? symbol?) (map caar body))) (assert ((list-of? pair?) (map cdr body))) (let ((parent-pair (car parent-clause)) (xss (cdr parent-clause)) (obody (cdr override-clause))) (pair? xss) ((list-of? pair?) xss) (assert ((list-of? pair?) (map car obody))) (assert ((list-of? symbol?) (map caar obody))) (assert ((list-of? pair?) (map cdr obody))) (let ((parent (car parent-pair)) (parent? (cadr parent-pair)) (xs (map car xss)) (xs? (map (lambda (ps) `(conjoin ,@ps)) (map cdr xss))) (messages (map car body)) (omessages (map car obody)) (extract-args (lambda (as) (if ((list-of? symbol?) as) as (map car as))))) (let ((message-names (append (map car omessages) (map car messages))) (arg-names (append (map extract-args (map cdr omessages)) (map extract-args (map cdr messages)))) (Types (inject 'Types)) (Invariant (inject 'Invariant)) (Info (inject 'Info)) (Ancestors (inject 'Ancestors))) `(define-values (,make-child ,child? ,@message-names) (let ((type (gensym ',CHILD))) ;; note, that the MSG type is distributed over the whole ;; object hierarchy, since the parents, including object, ;; all implement this type locally with the same name. ;; This is the reason, that the else clause in ;; concrete-case branches to the parent. (define-concrete-type MSG msg? ;;; only new messages ,@messages) (values ;; constructor (lambda (,parent ,@xs) (let ((,parent ,parent) ,@(map (lambda (x) `(,x ,x)) xs)) (lambda (msg) (concrete-case (msg msg?) ;; automatic overrides ((,Types) (cons type (,parent (,Types)))) ((,Invariant) (if (and (,parent (,Invariant)) ,@(map (lambda (n p) `(,p ,n)) xs xs?)) '(and (,parent (,Invariant)) ,@xss) #f)) ((,Info) (append (,parent (,Info)) ',messages)) ((,Ancestors) (cons ,parent (,parent (,Ancestors)))) ;; overriden and new messages ,@(map (lambda (n as bs) `((,n ,@as) ,@bs)) message-names arg-names (map cdr (append obody body))) (else (,parent msg)))))) ;; predicate (lambda (xpr) (and (procedure? xpr) (condition-case (if (memq type (xpr (,Types))) #t #f) ((exn) #f)))) ;; messages ,@message-names)))))))))) ;;; base object (define-values (make-base-object object? Types Info Invariant Ancestors) (let ((type (gensym 'OBJECT))) (define-concrete-type MSG msg? (Types) (Info) (Invariant) (Ancestors)) (values ;; constructor (lambda () (lambda (msg) (concrete-case (msg msg?) ((Types) (list type)) ((Info) '((Types) (Info) (Invariant) (Ancestors))) ((Invariant) '()) ((Ancestors) '())))) ;; predicate (lambda (xpr) (and (procedure? xpr) (condition-case (if (memq type (xpr (Types))) #t #f) ((exn) #f)))) ;; messages Types Info Invariant Ancestors))) ;; documentation procedure (define datatypes (let ( (signatures '( (define-concrete-type TYPE type? (Constructor (arg arg? ...) ...) ....) (concrete-case (obj type?) ((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) ..) (define-object-type CHILD child? make-child ((parent parent?) (x x? ...) ...) ; state variables (override ((A a ...) apr . aprs) ...) ; overridden messages ((X (x x? ...) ...) xpr . xprs) ; new messages ....) (make-base-object) (object? xpr) (Types) (Info) (Invariant) (Ancestors) )) ) (case-lambda (() (map car signatures)) ((sym) (assq sym signatures))))) ) ; module datatypes