; Author: Juergen Lorenz ; ju (at) jugilo (dot) de ; ; Copyright (c) 2018, 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 dispasser. ; ; Redistributions in binary form must reproduce the above copyright ; notice, this list of conditions and the following dispasser 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. #|[ messages as special, i.e. sometimes parametrized, generics of arity one. protocols are then simply lists of messages. To check, if an object is adopted by a protocol, one can inspect the method-trees of the messages' generics. ]|# (require-library generics simple-cells) (module messages ;* (messages define-message message? message-arity message-variadic? define-protocol protocol-adopts? protocol-docs pseudo-length project) (import scheme (only chicken case-lambda error print fx+ fx> fixnum?) (only generics generic? define-generic define-method) (only simple-cells cell cell?) (only data-structures compress list-of?)) (reexport (only generics define-method)) ;;; (messages sym ..) ;;; ----------------- ;;; documentation procedure (define messages (let ((alst '( (messages procedure: (messages sym ..) "documentation procedure") (define-protocol macro: (define-protocol NAME Msg ....) "where Msg is either a parametrized message of the form" "(Name arg ....) or a parameterless message Name." "defines a protocol, NAME, consisting of a list of messages") (protocol-adopts? procedure: (protocol-adopts? NAME obj) "can each message of protocol NAME operate on obj?") (protocol-docs procdeure: (protocol-docs NAME) "returns a readable list of the messages defined in protocol NAME") (define-message macro: (define-message ((Msg arg ....) obj)) (define-message (Msg obj)) "defines the message Msg, possibly with arguments arg ...." "as a generic function of arity one") (define-method macro: (define-method (Name (x x??)) body ....) "inserts an anonymous method of arity one" "into the method tree of generic Name constructed" "from argument x, selector x?? and body ....," "the latter being a lambda expression of arguments arg ...." "in case of a parametrized generic." "This is a reexport of the generics library.") (message? procedure: (message? xpr) "evaluates xpr to a message?") (message-arity procedure: (message-arity msg) "number of fixed arguments of message msg") (message-variadic? procedure: (message-variadic? msg) "has message msg a variable number of arguments?") (pseudo-length procedure: (pseudo-length x) "returns the length of a pseudo-list x") (project k procedure: (project k) "returns kth item of an argument list") ))) (case-lambda (() (map car alst)) ((sym) (let ((lst (assq sym alst))) (if lst (for-each print (cdr lst)) (error 'messages "not exported" sym))))))) ;;; (pseudo-length x) ;;; ----------------- ;;; length of a pseudo-list (define (pseudo-length x) (let loop ((x x) (len 0)) (if (pair? x) (loop (cdr x) (fx+ 1 len)) len))) ;;; (project k) ;;; ----------- ;;; get kth item of an argument list (define (project k) (lambda args (list-ref args k))) (define-syntax parameters->args (syntax-rules () ((_ ()) '()) ((_ (x . xs)) (cons x (parameters->args xs))) ((_ x) x))) ;;; (define-message ((Msg arg ....) obj) ;;; (define-message (Msg obj) ;;; ------------------------------------ ;;; defines a new message, Msg, which might be ;;; parametrized by arg ....) (define-syntax define-message (syntax-rules () ((_ ((Name arg . args) obj)) ;; message with arguments (define Name (let ((tree (cell (list (cons any?? (lambda (x) (lambda (arg . args) (error 'Name "no method found"))))) method-tree?))) (case-lambda (() (values tree #f ; generic-variadic? (pseudo-length '(arg . args)) (not (list? '(arg . args))))) ; message-variadic ((arg . args) (lambda (obj) (let ((proc (method-tree-dispatch (tree) obj))) (apply (proc obj) (parameters->args (arg . args)))))))))) ((_ (Name obj)) ;; argumentless message (define Name (let ((tree (cell (list (cons any?? (lambda (x) (error 'Name "no method found")))) method-tree?))) (case-lambda (() (values tree #f 0 #f)) ((obj) (let ((proc (method-tree-dispatch (tree) obj))) (proc obj))))))) )) ;;; (message? xpr) ;;; -------------- ;;; type predicate (define (message? xpr) (and (generic? xpr) (boolean? (call-with-values xpr (project 1))) (fixnum? (call-with-values xpr (project 2))) (boolean? (call-with-values xpr (project 3))))) ;;; (message-arity Msg) ;;; ------------------- ;;; returns the number of fixed parameters of message Msg (define (message-arity Msg) (call-with-values Msg (project 2))) ;;; (message-variadic? Msg) ;;; ----------------------- ;;; has message Msg additional variadic parameters? (define (message-variadic? Msg) (call-with-values Msg (project 3))) ;;; (define-protocol NAME Msg|(Msg args ....) ....) ;;; ----------------------------------------------- ;;; defines a protocol, NAME, from messages Msg .... (define-syntax define-protocol (er-macro-transformer (lambda (form rename compare?) (let ((NAME (cadr form)) (messages (cddr form))) (let ((message-names (map (lambda (msg) (if (pair? msg) (car msg) msg)) messages)) (%obj (rename 'obj)) (%list (rename 'list)) (%begin (rename 'begin)) (%define (rename 'define)) (%values (rename 'values)) (%define-message (rename 'define-message))) `(,%begin ,@(map (lambda (msg) `(,%define-message (,msg ,%obj))) messages) (,%define (,NAME) (,%values (,%list ,@message-names) ',messages)))))))) ;(define-syntax define-protocol ; (syntax-rules () ; ((_ NAME msg0 msg ...) ; (begin ; (define-message (msg0 obj)) ; (define-message (msg obj)) ; ... ; (define (NAME) ; (values ; '(msg0 msg ...))))))) ;;; (protocol-docs NAME) ;;; -------------------- ;;; returns a readable list of the messages defined in protocol NAME (define (protocol-docs NAME) (call-with-values NAME (project 1))) ;;; (protocol-adopts? NAME obj) ;;; --------------------------- ;;; can each message of protocol Name operate on obj? (define (protocol-adopts? NAME obj) (let ((message-protocols (NAME))) (let ((trees (map (lambda (m) ((m))) message-protocols))) (let ((checks (map (lambda (tree) (map (lambda (t) ((car t) obj)) tree)) trees))) (let ((lens (map (lambda (cs) (length (memq #t cs))) checks))) ((list-of? (lambda (l) (fx> l 1))) lens)))))) ) ; module messages