chickadee » tuples

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.

tuples

Tuples are a datatype which has much in common with vectors. Namely they store a finite number of items with random access and make them available via (tuple-ref tup n). But contrary to vectors, the items can only by accessed but not assigned to. If you want to change a particular item, you must package it into a box or a single, which is a special tuple storing exactly one item, which can be changed via single-set! without changing the identity of the single object itself.

singles are the only tuples, which can assign to their content. Other special tuples are couples and triples. They are all packaged in their own modules, and all of them use another module, %tuples, which contains the basic implementations without any checks. The unchecked routines are all prefixed with a %.

Note, that couples can be used instead of pairs to implement immutable lists.

Documentation

In this implementation tuples and their relatives (except %tuples) are implemented in the Design by Contract style, i.e. using the dbc egg. A corollary of this is, that the documentation is included in each of these modules in form of a procedure with the module's name. Called without any argument, the list of exported symbols is shown, with such a symbol as argument, the symbol's documentation is shown, and with a string argument, some wiki-like documentation is printed to a file, which you can inspect below.

module tuples

list->tuple

list->tuple lstprocedure

function (result)

(list->tuple lst)

requires (list? lst)

ensures  (and (%tuple? result) (= (length lst) (%tuple-length result)))

tuple

tuple #!rest argsprocedure

function (result)

(tuple . args)

requires #t

ensures  (%tuple? result)

tuple->list

tuple->list tupprocedure

function (result)

(tuple->list tup)

requires (%tuple? tup)

ensures  (list? result)

tuple->vector

tuple->vector tupprocedure

function (result)

(tuple->vector tup)

requires (%tuple? tup)

ensures  (vector? result)

tuple-append

tuple-append #!rest tupsprocedure

function (result)

(tuple-append . tups)

requires ((list-of? %tuple?) tups)

ensures  (and (%tuple? result)
              (= (tuple-length result)
                 (apply + (map tuple-length tups))))

tuple-butleft

tuple-butleft tupprocedure

function (result)

(tuple-butleft tup)

requires (and (%tuple? tup)
              (positive? (%tuple-length tup)))

ensures  (and (%tuple result)
              (= (%tuple-length result)
                 (- (%tuple-length tup) 1)))

tuple-butright

tuple-butright tupprocedure

function (result)

(tuple-butright tup)

requires (and (%tuple? tup) (positive? (%tuple-length tup)))

ensures  (and (%tuple result)
              (= (%tuple-length result)
                 (- (%tuple-length tup) 1)))

tuple-cons-left

tuple-cons-left arg tupprocedure

function (result)

(tuple-cons-left arg tup)

requires (%tuple? tup)

ensures  (and (%tuple result)
              (= (%tuple-length result)
                 (+ (%tuple-length tup) 1)))

tuple-cons-right

tuple-cons-right arg tupprocedure

function (result)

(tuple-cons-right arg tup)

requires (%tuple? tup)

ensures  (and (%tuple result)
              (= (%tuple-length result)
                 (+ (%tuple-length tup) 1)))

tuple-copy

tuple-copy tupprocedure

function (result)

(tuple-copy tup)

requires (%tuple tup)

ensures  (%tuple result)

tuple-empty?

tuple-empty? xprprocedure

function (result)

(tuple-empty? xpr)

requires #t

ensures  (boolean? result)

tuple-eq?

tuple-eq? tup0 tup1procedure

function (result)

(tuple-eq? tup0 tup1)

requires (and (%tuple? tup0) (%tuple? tup1))

ensures  (boolean? result)

tuple-eql?

tuple-eql? eql? tup0 tup1procedure

function (result)

(tuple-eql? eql? tup0 tup1)

requires (and (procedure? eql?)
              (%tuple? tup0) (%tuple? tup1))

ensures  (boolean? result)

tuple-equal?

tuple-equal? tup0 tup1procedure

function (result)

(tuple-equal? tup0 tup1)

requires (and (%tuple? tup0) (%tuple? tup1))

ensures  (boolean? result)

tuple-eqv?

tuple-eqv? tup0 tup1procedure

function (result)

(tuple-eqv? tup0 tup1)

requires (and (%tuple? tup0) (%tuple? tup1))

ensures  (boolean? result)

tuple-find

tuple-find ok? tupprocedure

function (result)

(tuple-find ok? tup)

requires (and (procedure? ok?) (%tuple? tup))

ensures  (or (not result)
             (and (cardinal? result)
                  (< result (%tuple-length tup))
                  "index of found item"))

tuple-for-each

tuple-for-each proc tup #!rest tupsprocedure

command ((old new (lambda (proc tup . tups) #t)))

(tuple-for-each proc tup . tups)

requires (and (procedure? proc)
              (%tuple tup)
              ((list-of? %tuple?) tups)
              (apply = (%tuple-length tup) (map %tuple-length tups)))

ensures  "proc applied to each item"

tuple-from-upto

tuple-from-upto tup #!rest intervalprocedure

function (result)

(tuple-from-upto tup . interval)

requires (and (%tuple tup)
              ((list-of? (lambda (x)
                           (and (cardinal? x)
                           (<= x (%tuple-length tup))))) interval)
              (<= (length interval) 2) (apply <= 0 interval))

ensures  (and (%tuple? result)
              (= (%tuple-length result)
                 (case (length interval)
									 ((0) (%tuple-length tup))
									 ((1) (- (%tuple-length tup) (car interval)))
									 ((2) (- (cadr interval) (car interval))))))

tuple-head

tuple-head tup nprocedure

function (result)

(tuple-head tup n)

requires (and (%tuple? tup)
              (<= n (%tuple-length tup)))

ensures  (and (%tuple result) (= (%tuple-length result) n))

tuple-left

tuple-left tupprocedure

function (result)

(tuple-left tup)

requires (and (%tuple? tup) (positive? (%tuple-length tup)))

ensures  "tup's leftmost item"

tuple-length

tuple-length tupprocedure

function (result)

(tuple-length tup)

requires (%tuple? tup)

ensures  (cardinal? result)

tuple-map

tuple-map fn tup #!rest tupsprocedure

function (result)

(tuple-map fn tup . tups)

requires (and (procedure? fn)
              (%tuple? tup)
              ((list-of? %tuple?) tups)
              (apply = (%tuple-length tup)
                       (map %tuple-length tups)))

ensures  (and (%tuple? result)
              (= (%tuple-length tup)
                 (%tuple-length result)))

tuple-of?

tuple-of? ok?procedure

function (result)

(tuple-of? ok?)

requires (procedure? ok?)

ensures  (procedure? result)

tuple-ref

tuple-ref tup nprocedure

function (result)

(tuple-ref tup n)

requires (and (%tuple? tup)
              (cardinal? n)
              (< -1 n (%tuple-length tup)))

ensures  "tup's nth item"

tuple-reverse

tuple-reverse tupprocedure

function (result)

(tuple-reverse tup)

requires (%tuple? tup)

ensures  (and (%tuple result)
              (= (%tuple-length result)
                 (%tuple-length tup)))

tuple-right

tuple-right tupprocedure

function (result)

(tuple-right tup)

requires (and (%tuple? tup) (positive? (%tuple-length tup)))

ensures  "tup's rightmost item"

tuple-split

tuple-split tup atprocedure

function (head tail)

(tuple-split tup at)

requires (and (%tuple? tup)
          (cardinal? at) (< at (%tuple-length tup)))

ensures (and (%tuple? head) (%tuple? tail)
          (%tuple-eql? equal? tup (%tuple-append head tail)))))

tuple-tail

tuple-tail tup nprocedure

function (result)

(tuple-tail tup n)

requires (and (%tuple? tup) (<= n (%tuple-length tup)))

ensures  (and (%tuple result)
              (= (%tuple-length result)
                 (- (%tuple-length tup) n)))

tuple?

tuple? xprprocedure

function (result)

(tuple? xpr)

requires #t

ensures  (boolean? result)

vector->tuple

vector->tuple vecprocedure

function (result)

(vector->tuple vec)

requires (vector? vec)

ensures  (and (%tuple? result)
              (= (%tuple-length result)
                 (vector-length vec)))

Module triples

triple

triple x y zprocedure

function (result)

(triple x y z)

requires #t

ensures  (%triple? result)

triple->tuple

triple->tuple trpprocedure

function (result)

(triple->tuple trp)

requires (%triple? trp)

ensures  (and (%tuple? result)
              (= (%tuple-length result) 3))

triple-eq?

triple-eq? trp0 trp1procedure

function (result)

(triple-eq? trp0 trp1)

requires (and (%triple? trp0) (%triple? trp1))

ensures  (boolean? result)

triple-eql?

triple-eql? eql? trp0 trp1procedure

function (result)

(triple-eql? eql? trp0 trp1)

requires (and (procedure? eql?)
              (%triple? trp0)
              (%triple? trp1))

ensures  (boolean? result)

triple-equal?

triple-equal? trp0 trp1procedure

function (result)

(triple-equal? trp0 trp1)

requires (and (%triple? trp0) (%triple? trp1))

ensures  (boolean? result)

triple-eqv?

triple-eqv? trp0 trp1procedure

function (result)

(triple-eqv? trp0 trp1)

requires (and (%triple? trp0) (%triple? trp1))

ensures  (boolean? result)

triple-left

triple-left trpprocedure

function (result)

(triple-left trp)

requires (%triple? trp)

ensures  "leftmost item"

triple-middle

triple-middle trpprocedure

function (result)

(triple-middle trp)

requires (%triple? trp)

ensures  "item in the middle"

triple-right

triple-right trpprocedure

function (result)

(triple-right trp)

requires (%triple? trp)

ensures  "rightmost item"

triple?

triple? xprocedure

function (result)

(triple? x)

requires #t

ensures  (boolean? result)

Module couples

couple

couple x yprocedure

function (result)

(couple x y)

requires #t

ensures  (%couple? result)

couple->tuple

couple->tuple cplprocedure

function (result)

(couple->tuple cpl)

requires (%couple? cpl)

ensures  (and (%tuple? result)
              (= (%tuple-length result) 2))

couple-eq?

couple-eq? cpl0 cpl1procedure

function (result)

(couple-eq? cpl0 cpl1)

requires (and (%couple? cpl0) (%couple? cpl1))

ensures  (boolean? result)

couple-eql?

couple-eql? eql? cpl0 cpl1procedure

function (result)

(couple-eql? eql? cpl0 cpl1)

requires (and (procedure? eql?)
              (%couple? cpl0) (%couple? cpl1))

ensures  (boolean? result)

couple-equal?

couple-equal? cpl0 cpl1procedure

function (result)

(couple-equal? cpl0 cpl1)

requires (and (%couple? cpl0) (%couple? cpl1))

ensures  (boolean? result)

couple-eqv?

couple-eqv? cpl0 cpl1procedure

function (result)

(couple-eqv? cpl0 cpl1)

requires (and (%couple? cpl0) (%couple? cpl1))

ensures  (boolean? result)

couple-left

couple-left cplprocedure

function (result)

(couple-left cpl)

requires (%couple? cpl)

ensures  "leftmost item"

couple-right

couple-right cplprocedure

function (result)

(couple-right cpl)

requires (%couple? cpl)

ensures  "rightmost item"

couple?

couple? xprocedure

function (result)

(couple? x)

requires #t

ensures  (boolean? result)

Module singles

single

single xprocedure

function (result)

(single x)

requires #t

ensures  (%single? result)

single-eq?

single-eq? sgl0 sgl1procedure

function (result)

(single-eq? sgl0 sgl1)

requires (and (%single? sgl0) (%single? sgl1))

ensures  (boolean? result)

single-eql?

single-eql? eql? sgl0 sgl1procedure

function (result)

(single-eql? eql? sgl0 sgl1)

requires (and (procedure? eql?)
              (%single? sgl0) (%single? sgl1))

ensures  (boolean? result)

single-equal?

single-equal? sgl0 sgl1procedure

function (result)

(single-equal? sgl0 sgl1)

requires (and (%single? sgl0) (%single? sgl1))

ensures  (boolean? result)

single-eqv?

single-eqv? sgl0 sgl1procedure

function (result)

(single-eqv? sgl0 sgl1)

requires (and (%single? sgl0) (%single? sgl1))

ensures  (boolean? result)

single-ref

single-ref sgprocedure

function (result)

(single-ref sg)

requires (%single? sg)

ensures  "sg's stored item"

single-set!

single-set! sg xprocedure

command ((old new (lambda (sg x) (%single-ref sg))))

(single-set! sg x)

requires (%single? sg)

ensures  (equal? new x)

single?

single? xprocedure

function (result)

(single? x)

requires #t

ensures  (boolean? result)

Requirements

dbc

Last update

Feb 11, 2014

Author

Juergen Lorenz

License

Copyright (c) 2012-2014, 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

1.2
tests with define-test instead of simple-test
1.1
implementation-module renamed, tests with simple-tests, tuple-split added
1.0
dependency on contracts changed to dbc, modules further split, addition routines
0.8
code split into two modules
0.7
single-state and single-state! renamed to single-ref and single-set!
0.6
contracts rewritten
0.5
code maintenance
0.4
added copyright notice
0.3
updated for using contracts 1.0
0.2
added tuple-copy, tuple-left, tuple-right
0.1
initial import

Contents »