chickadee » typeclass

typeclass

Description

Macros for programming in type-class-style in Scheme.

Author

Andre Van Tonder. Ported to Chicken by Ivan Raikov.

Requirements

Documentation

(define-class NAME FIELD-FORM ...) syntax

Defines a type class named NAME, where FIELD-FORM ... defines one or more fields for instances of this class. FIELD-FORM should be of the form:

 FIELD-FORM = FIELD-LABEL | (SUPERCLASS-NAME FIELD-LABEL)

SUPERCLASS-NAME is the name of a class from which the field is inherited.

The result of this macro is the creation of a constructor with name of the form MAKE-CLASSNAME, which in turn takes an instance constructor procedure and applies to the given field values.

(define=> (<PROCEDURE-NAME> <CLASS-FORM> ...) . BODY) syntax

Defines a procedure such that all fields of the given class(es) are visible in the scope of the procedure body.

CLASS-FORM is of the following form:

CLASS-FORM = CLASS-NAME | (CLASS-NAME PREFIX-SYMBOL)
(lambda=> (<CLASS-FORM> ...) . BODY) syntax

Anonymous procedure such that all fields in the superclasses of the given class are visible in the scope of the procedure body.

(with-instance (<INSTANCE-FORM> ...) . BODY) syntax

An expression such that all fields of the given class(es) are visible in the scope of BODY.

INSTANCE-FORM is of the following form:

 INSTANCE-FORM = (CLASS-NAME INSTANCE-EXPR)
                | (CLASS-NAME INSTANCE-EXPR PREFIX-SYMBOL)
(import-instance <INSTANCE-FORM> ...) syntax

Imports all fields of the given classes as identifiers in the current scope.

Example

;=======================================================
; Equality example:

; class (Eq a) where
;   egal?     : a a -> boolean
;   not-egal? : a a -> boolean

(define-class <Eq> egal?
                   not-egal?)

(define (default-Eq egal?)
  (make-<Eq> egal?
             (lambda (x y) (not (egal? x y)))))

(define num-Eq (default-Eq =))
(define eqv-Eq (default-Eq eqv?))
(define chr-Eq (default-Eq char=?))

Changelog

License

Copyright (c) 2004-2010, Andre van Tonder, Ivan Raikov.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

Contents »