chickadee » messages

messages

Description

An implementation of algebraic and abstract types, inspired by ML's datatype and abstype.

Algebraic types are discriminated variant records, as advocated in the classic "Essentials of Programming Languages" by Friedman, Wand and Haynes and named datatype there. They were ported to Chicken by Felix Winkelmann. Here, they are implemented via messages, i.e vectors tagged with a type- and a variant-key, and accessed via case-variant, a small wrapper around bind-case from the bindings module. Type specialized accessor macros, based on case-variant, are automatically generated as well. So, for example, instead of

Note that the arguments of variant constructors can accept zero or multiple predicates.

Abstract types are based on algebraic types, but hide the variant constructors and export constructor and accessor procedures instead.

Both types implement some sort of inheritance via delegation. To avoid different names for different levels of inheritance, the constructors and the exported routines of abstract types are curried and tagged with keywords. For example, if Foo is a type and #:bar is the tag of some variant, the actual constructor is (Foo #:bar). This has the drawback to be heavy on the fingers but the advantage, that different types may have the same symbol discriminator.

This documentation uses special ellipses meaning

messages

messages

messages sym ..procedure

documenatation procedure. shows the list of available exported symbols of the module when called without argument or the signature of that very argument.

make-message

make-message type-key key #!rest argsprocedure

type constructor

message?

message? xprprocedure

type predicate

message-of?

message-of? type-keyprocedure

returns a predicate which checks, if its only argument is a message of the given type-key

message-key

message-key msgprocedure

returns the key of a message

message-type

message-type msgprocedure

returns the type-key of a message

message-data

message-data msgprocedure

returns the data vector of the message.

case-variant

(case-variant Type obj variant ....)syntax

where each variant is either of the form

  • (variant-key (arg ...) body ....)

or as last variant

  • (else body ....)

This one macro replaces in algebraic messages the many accessor routines by destructuring the variants via pattern matching. It destructures obj of Type depending on its variants, i.e. matches the variant-key and the argument-list (variant-key (arg ...)) in sequence and invokes the body .... of the first matching pair. The else clause serves as catch-all.

Note that you can alternatively write

(Type-case obj variant ....)syntax

replacing "Type" with the actual algebraic or abstract type's name.

define-algebraic-type

(define-algebraic-type Child Parent .. variant ....)syntax

where each variant is either of the form

  • (variant-key (a a? ...) ...) or with rest arguments
  • (variant-key (a a? ...) ... as as? ...) defines a selector routine, Child, which, when called with the #:? keyword returns the type predicate, when called with another keyword, returns the corresponding message-constructor. When Child is called with no argument, it returns information on the type. Note that rest arguments, as, as well as their checks are not parenthesized. A specialisation of case-variant, Child-case, is generated as well.

define-abstract-type

(define-abstract-type Child Parent .. variant .... (with clause ....)syntax

defines a hidden algebraic type with variant .... and exports two procedures, the type predicate Child? and a parametrized procedure Child, which, when called with key #:? returns the type predicate, wenn called with another key argument returns the corresponding procedure (Child key) ... , when called without argument returns documentation of all those procedures.

All the procedures are specified in each clause .... either as

  • ((key (x x? ...) ...) body ....)

or

  • ((key (x x? ...) ... xs xs? ...) body ....)

Note that the first list in each clause looks like a variant in the algebraic type.

As in algebraic types, a specialisation of case-variant, Child-case, is generated as well.

Dependencies

bindings

Examples

;; options
(define-algebraic-type Option
  (#:none)
  (#:some (n number?)))

(define (qux opt)
  (Option-case opt
    (#:none () #f)
    (#:some (arg) arg)))

(qux ((Option #:some) 5)) ; -> 5
(qux ((Option #:none))) ; -> #f

;; immutable typed lists
(define (0<= x) (and (number? x) (not (negative? x))))
(define-abstract-type List
  (#:null)
  (#:cons (first number?) (rest (List #:?)))
  (with
    ((#:maker args number?)
     (let loop ((args args))
       (if (null? args)
         ((List #:null))
         ((List #:cons) (car args)
                        (loop (cdr args))))))
    ((#:null? (xs (List #:?)))
     (case-variant List xs
       (#:null () #t)
       (else #f)))
    ((#:ref (xs (List #:?))
            (k 0<=))
     (let loop ((xs xs) (k k))
       (case-variant List xs
         (#:null () (error '(List #:ref)))
         (#:cons (a as) (if (zero? k)
                          a
                          (loop as (- k 1)))))))
    ((#:tail (xs (List #:?))
             (k 0<=))
     (let loop ((xs xs) (k k))
       (case-variant List xs
         (#:null () xs)
         (#:cons (a as) (if (zero? k)
                          xs
                          (loop as (- k 1)))))))
    ))

(define as0123 ((List #:maker) 0 1 2 3)) 

((List #:ref) as0123 2) ; -> 2
((List #:null?) ((List #:tail) as0123 4)) ; -> #t

Last update

Fev 24, 2020

Author

Juergen Lorenz

License

Copyright (c) 2015-2020, 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.

Version History

: 0.6 : functional vectors replaced by normal vectors

0.5
abstract-types improved, object-types removed
0.4
dependency on simple-cells removed and added in test-dependencies
0.3
dependency on simple-cells added
0.2
dependency on symbol-utils added
0.1
initial import

Contents »