chickadee » messages

Outdated egg!

This is an egg for CHICKEN 4, the unsupported old release. You're almost certainly looking for the CHICKEN 5 version of this egg, if it exists.

If it does not exist, there may be equivalent functionality provided by another egg; have a look at the egg index. Otherwise, please consider porting this egg to the current version of CHICKEN.

Messages

This library implements messages as special, sometimes parametrized, generics of arity one as well as protocols. For the sake of simplicity, all method-trees, which are in fact lists, have a sentinel with selector any?? which branches to the default behaviour, calling the error routine.

Protocols are simply lists of messages. To check, if a protocol adopts an object, one can inspect the selectors of all method-trees: besides any?? there must be another selector which accepts the object.

The three fundamental macros are

 (define-message ((Msg arg . args) obj))
 (define-message (Msg obj))

for parametrized or simple messages respectively,

 (define-protocol NAME Msg|(Msg arg . args) ....)

and

 (define-method (Name (x x??)) body ....)

the latter being reexported from the generics library. Note, that body must be a lambda expression with arguments arg . args in case of a parametrized message.

The module messages

messages

messages sym ..procedure

documentation procedure: returns the list of exported symbols when called as thunk or the documentation of its argument sym.

define-message

<macro>(define-message ((Msg arg . args) obj))</macros> <macro>(define-message (Msg obj))</macro>

defines a message named Msg, which might be parametrized. Messages are generic functions of arity one, which, when parametrized, are called differently than generics.

define-method

(define-method (Msg (x x??)) body ....)syntax

inserts an anonymous method constructed from argument x, selector x?? and body .... into the method tree of the generic function Msg at the position determined by selector's parents. The body must be a lambda expression in case of a message with parameters. Reexported from generics.

message?

message? xprprocedure

type predicate

message-variadic?

message-variadic? Msgprocedure

is the message function Msg variadic?

message-arity

message-arity Msgprocedure

returns the arity of the message Msg

define-protocol

(define-protocol NAME Msg ....)syntax

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?

protocol-adopts? NAME objprocedure

can each message of protocol NAME operate on obj?

protocol-docs

protocol-docs NAMEprocedure

returns a readable list of the messages defined in protocol NAME

Requirements

generics simple-cells

Usage

(use messages generics simple-cells)

Examples

;;; Sequences
;;; ---------
(define-protocol SEQ (Drop k) (Take k) (At k))
(define-method (Drop (seq string??))
               (lambda (k) (substring seq k)))
(define-method (Drop (seq vector??))
               (lambda (k) (subvector seq k)))
(define-method (Drop (seq list??))
               (lambda (k) (list-tail seq k)))
(define-method (Take (seq string??))
               (lambda (k) (substring seq 0 k)))
(define-method (Take (seq vector??))
               (lambda (k) (subvector seq 0 k)))
(define-method (Take (seq list??))
               (lambda (k)
                 (let loop ((n 0) (tail seq) (head '()))
                   (if (fx= k n)
                     (reverse head)
                     (loop (1+ n)
                           (cdr tail)
                           (cons (car tail) head))))))
(define-method (At (seq string??))
               (lambda (k) (string-ref seq k)))
(define-method (At (seq vector??))
               (lambda (k) (vector-ref seq k)))
(define-method (At (seq list??))
               (lambda (k) (list-ref seq k)))

((At 2) '(0 1 2 3 4)) ; -> 2
((Drop 1) "01234") ; -> "1234"
((Take 3) #(0 1 2 3 4)) ; -> #(0 1 2)

;;; Stacks
;;; -------
(define-protocol STACK (Push! arg) Pop! Top)
(define-selector stack?? list?? cell?)
(define-method (Push! (obj stack??))
               (lambda (arg)
                 (let ((old (obj)))
                   (obj (cons arg old))
                   old)))
(define-method (Top (obj stack??))
               (if (null? (obj))
                 (gensym 'stack-empty)
                 (car (obj))))
(define-method (Pop! (obj stack??))
               (if (null? (obj))
                 (gensym 'stack-empty)
                 (let ((old (obj)))
                   (obj (cdr old))
                   old)))
(define stk (cell '() list?))
((Push! 0) stk) ; -> '()
((Push! 1) stk) ; -> '(0)
(Top stk) ; -> 1
((Push! 2) stk) ; -> '(1 0)
(Top stk) ; -> 2
(Pop! stk) ; -> '(2 1 0)
(Top stk) ; -> 1

Last update

Mar 15, 2018

Author

Juergen Lorenz

License

Copyright (c) 2018, Juergen Lorenz (ju (at) jugilo (dot) de)
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

0.1
initial import

Contents »