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.

  1. Outdated egg!
  2. tuples
  3. Documentation
    1. module tuples
      1. list->tuple
      2. tuple
      3. tuple->list
      4. tuple->vector
      5. tuple-append
      6. tuple-butleft
      7. tuple-butright
      8. tuple-cons-left
      9. tuple-cons-right
      10. tuple-copy
      11. tuple-empty?
      12. tuple-eq?
      13. tuple-eql?
      14. tuple-equal?
      15. tuple-eqv?
      16. tuple-find
      17. tuple-for-each
      18. tuple-from-upto
      19. tuple-head
      20. tuple-left
      21. tuple-length
      22. tuple-map
      23. tuple-of?
      24. tuple-ref
      25. tuple-reverse
      26. tuple-right
      27. tuple-split
      28. tuple-tail
      29. tuple?
      30. vector->tuple
    2. Module triples
      1. triple
      2. triple->tuple
      3. triple-eq?
      4. triple-eql?
      5. triple-equal?
      6. triple-eqv?
      7. triple-left
      8. triple-middle
      9. triple-right
      10. triple?
    3. Module couples
      1. couple
      2. couple->tuple
      3. couple-eq?
      4. couple-eql?
      5. couple-equal?
      6. couple-eqv?
      7. couple-left
      8. couple-right
      9. couple?
    4. Module singles
      1. single
      2. single-eq?
      3. single-eql?
      4. single-equal?
      5. single-eqv?
      6. single-ref
      7. single-set!
      8. single?
  4. Requirements
  5. Last update
  6. Author
  7. License
  8. Version History

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

[procedure] (list->tuple lst)

function (result)


(list->tuple lst)

requires (list? lst)

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

tuple

[procedure] (tuple . args)

function (result)


(tuple . args)

requires #t

ensures  (%tuple? result)

tuple->list

[procedure] (tuple->list tup)

function (result)


(tuple->list tup)

requires (%tuple? tup)

ensures  (list? result)

tuple->vector

[procedure] (tuple->vector tup)

function (result)


(tuple->vector tup)

requires (%tuple? tup)

ensures  (vector? result)

tuple-append

[procedure] (tuple-append . tups)

function (result)


(tuple-append . tups)

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

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

tuple-butleft

[procedure] (tuple-butleft tup)

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

[procedure] (tuple-butright tup)

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

[procedure] (tuple-cons-left arg tup)

function (result)


(tuple-cons-left arg tup)

requires (%tuple? tup)

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

tuple-cons-right

[procedure] (tuple-cons-right arg tup)

function (result)


(tuple-cons-right arg tup)

requires (%tuple? tup)

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

tuple-copy

[procedure] (tuple-copy tup)

function (result)


(tuple-copy tup)

requires (%tuple tup)

ensures  (%tuple result)

tuple-empty?

[procedure] (tuple-empty? xpr)

function (result)


(tuple-empty? xpr)

requires #t

ensures  (boolean? result)

tuple-eq?

[procedure] (tuple-eq? tup0 tup1)

function (result)


(tuple-eq? tup0 tup1)

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

ensures  (boolean? result)

tuple-eql?

[procedure] (tuple-eql? eql? tup0 tup1)

function (result)


(tuple-eql? eql? tup0 tup1)

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

ensures  (boolean? result)

tuple-equal?

[procedure] (tuple-equal? tup0 tup1)

function (result)


(tuple-equal? tup0 tup1)

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

ensures  (boolean? result)

tuple-eqv?

[procedure] (tuple-eqv? tup0 tup1)

function (result)


(tuple-eqv? tup0 tup1)

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

ensures  (boolean? result)

tuple-find

[procedure] (tuple-find ok? tup)

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

[procedure] (tuple-for-each proc tup . tups)

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

[procedure] (tuple-from-upto tup . interval)

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

[procedure] (tuple-head tup n)

function (result)


(tuple-head tup n)

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

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

tuple-left

[procedure] (tuple-left tup)

function (result)


(tuple-left tup)

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

ensures  "tup's leftmost item"

tuple-length

[procedure] (tuple-length tup)

function (result)


(tuple-length tup)

requires (%tuple? tup)

ensures  (cardinal? result)

tuple-map

[procedure] (tuple-map fn tup . tups)

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?

[procedure] (tuple-of? ok?)

function (result)


(tuple-of? ok?)

requires (procedure? ok?)

ensures  (procedure? result)

tuple-ref

[procedure] (tuple-ref tup n)

function (result)


(tuple-ref tup n)

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

ensures  "tup's nth item"

tuple-reverse

[procedure] (tuple-reverse tup)

function (result)


(tuple-reverse tup)

requires (%tuple? tup)

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

tuple-right

[procedure] (tuple-right tup)

function (result)


(tuple-right tup)

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

ensures  "tup's rightmost item"

tuple-split

[procedure] (tuple-split tup at)

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

[procedure] (tuple-tail tup n)

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?

[procedure] (tuple? xpr)

function (result)


(tuple? xpr)

requires #t

ensures  (boolean? result)

vector->tuple

[procedure] (vector->tuple vec)

function (result)


(vector->tuple vec)

requires (vector? vec)

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

Module triples

triple

[procedure] (triple x y z)

function (result)


(triple x y z)

requires #t

ensures  (%triple? result)

triple->tuple

[procedure] (triple->tuple trp)

function (result)


(triple->tuple trp)

requires (%triple? trp)

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

triple-eq?

[procedure] (triple-eq? trp0 trp1)

function (result)


(triple-eq? trp0 trp1)

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

ensures  (boolean? result)

triple-eql?

[procedure] (triple-eql? eql? trp0 trp1)

function (result)


(triple-eql? eql? trp0 trp1)

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

ensures  (boolean? result)

triple-equal?

[procedure] (triple-equal? trp0 trp1)

function (result)


(triple-equal? trp0 trp1)

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

ensures  (boolean? result)

triple-eqv?

[procedure] (triple-eqv? trp0 trp1)

function (result)


(triple-eqv? trp0 trp1)

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

ensures  (boolean? result)

triple-left

[procedure] (triple-left trp)

function (result)


(triple-left trp)

requires (%triple? trp)

ensures  "leftmost item"

triple-middle

[procedure] (triple-middle trp)

function (result)


(triple-middle trp)

requires (%triple? trp)

ensures  "item in the middle"

triple-right

[procedure] (triple-right trp)

function (result)


(triple-right trp)

requires (%triple? trp)

ensures  "rightmost item"

triple?

[procedure] (triple? x)

function (result)


(triple? x)

requires #t

ensures  (boolean? result)

Module couples

couple

[procedure] (couple x y)

function (result)


(couple x y)

requires #t

ensures  (%couple? result)

couple->tuple

[procedure] (couple->tuple cpl)

function (result)


(couple->tuple cpl)

requires (%couple? cpl)

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

couple-eq?

[procedure] (couple-eq? cpl0 cpl1)

function (result)


(couple-eq? cpl0 cpl1)

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

ensures  (boolean? result)

couple-eql?

[procedure] (couple-eql? eql? cpl0 cpl1)

function (result)


(couple-eql? eql? cpl0 cpl1)

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

ensures  (boolean? result)

couple-equal?

[procedure] (couple-equal? cpl0 cpl1)

function (result)


(couple-equal? cpl0 cpl1)

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

ensures  (boolean? result)

couple-eqv?

[procedure] (couple-eqv? cpl0 cpl1)

function (result)


(couple-eqv? cpl0 cpl1)

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

ensures  (boolean? result)

couple-left

[procedure] (couple-left cpl)

function (result)


(couple-left cpl)

requires (%couple? cpl)

ensures  "leftmost item"

couple-right

[procedure] (couple-right cpl)

function (result)


(couple-right cpl)

requires (%couple? cpl)

ensures  "rightmost item"

couple?

[procedure] (couple? x)

function (result)


(couple? x)

requires #t

ensures  (boolean? result)

Module singles

single

[procedure] (single x)

function (result)


(single x)

requires #t

ensures  (%single? result)

single-eq?

[procedure] (single-eq? sgl0 sgl1)

function (result)


(single-eq? sgl0 sgl1)

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

ensures  (boolean? result)

single-eql?

[procedure] (single-eql? eql? sgl0 sgl1)

function (result)


(single-eql? eql? sgl0 sgl1)

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

ensures  (boolean? result)

single-equal?

[procedure] (single-equal? sgl0 sgl1)

function (result)


(single-equal? sgl0 sgl1)

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

ensures  (boolean? result)

single-eqv?

[procedure] (single-eqv? sgl0 sgl1)

function (result)


(single-eqv? sgl0 sgl1)

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

ensures  (boolean? result)

single-ref

[procedure] (single-ref sg)

function (result)


(single-ref sg)

requires (%single? sg)

ensures  "sg's stored item"

single-set!

[procedure] (single-set! sg x)

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


(single-set! sg x)

requires (%single? sg)

ensures  (equal? new x)

single?

[procedure] (single? x)

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