On this page:
tuple-type?
tuple-type
tuple-type-name
tuple-type-size
tuple-type-predicate-name
tuple-type-constructor-name
tuple-type-accessor-name
tuple-descriptor?
initialized-tuple-descriptor?
uninitialized-tuple-descriptor?
tuple-descriptor-type
tuple-descriptor-predicate
tuple-descriptor-constructor
tuple-descriptor-accessor
make-tuple-implementation
make-tuple-field-accessor
make-default-tuple-properties
make-tuple-equal+  hash
make-tuple-custom-write
define-tuple-type
7.3

2.2 Tuple Types

 (require rebellion/type/tuple) package: rebellion

A tuple type is a data type representing immutable fixed-size sequences of unnamed values, each of which is a tuple instance of the tuple type. All instances of a tuple type are the same size, and that size is accessible from the type using tuple-type-size.

Tuple types are specifications, not implementations. A tuple type does not directly provide a constructor or accessor; it only provides information about the type such as its symbolic name, the number of fields, etc. An actual implementation of a tuple type is obtained via make-tuple-implementation, which generates a new implementation of a tuple type and returns a tuple type descriptor object. Descriptors provide functions that implement the type and a predicate that identifies the implementation’s values. Descriptors are generative; calling make-tuple-implementation with the same tuple type will make multiple distinct implementations of that type.

procedure

(tuple-type? v)  boolean?

  v : any/c
A predicate for tuple types.

procedure

(tuple-type name    
  size    
  [#:predicate-name predicate-name    
  #:constructor-name constructor-name    
  #:accessor-name accessor-name])  tuple-type?
  name : symbol?
  size : natural?
  predicate-name : (or/c symbol? #f) = #f
  constructor-name : (or/c symbol? #f) = #f
  accessor-name : (or/c symbol? #f) = #f
Constructs a tuple type of size size and named name. The optional predicate-name, constructor-name, and accessor-name arguments control the result of object-name on the functions implementing the type. If not provided, predicate-name defaults to name

?

, constructor-name defaults to name, and accessor-name defaults to name

-ref

. Two tuple types constructed with the same arguments are equal?. To make an implementation of a tuple type, see make-tuple-implementation.

procedure

(tuple-type-name type)  symbol?

  type : tuple-type?

procedure

(tuple-type-size type)  natural?

  type : tuple-type?

procedure

(tuple-type-predicate-name type)  symbol?

  type : tuple-type?

procedure

(tuple-type-constructor-name type)  symbol?

  type : tuple-type?

procedure

(tuple-type-accessor-name type)  symbol?

  type : tuple-type?
Accessors for the various fields of a tuple type.

procedure

(tuple-descriptor? v)  boolean?

  v : any/c
A predicate for tuple type descriptors.

procedure

(initialized-tuple-descriptor? v)  boolean?

  v : any/c

procedure

(uninitialized-tuple-descriptor? v)  boolean?

  v : any/c
Predicates for tuple type descriptors that either have or haven’t been initialized yet. The predicate, constructor, and accessor of an uninitialized tuple descriptor must not be called until initialization is complete, as the implementations of those functions won’t exist until then. Initialization of a descriptor completes when make-tuple-implementation returns.

procedure

(tuple-descriptor-type descriptor)  tuple-type?

  descriptor : tuple-descriptor?

procedure

(tuple-descriptor-predicate descriptor)  (-> any/c boolean?)

  descriptor : tuple-descriptor?

procedure

(tuple-descriptor-constructor descriptor)  procedure?

  descriptor : tuple-descriptor?

procedure

(tuple-descriptor-accessor descriptor)

  (-> (tuple-descriptor-predicate descriptor) natural? any/c)
  descriptor : tuple-descriptor?
Accessors for the various fields of a tuple type descriptor.

procedure

(make-tuple-implementation type 
  [#:guard guard 
  #:inspector inspector 
  #:property-maker prop-maker]) 
  initialized-tuple-descriptor?
  type : tuple-type?
  guard : (or/c procedure? #f) = #f
  inspector : inspector? = (current-inspector)
  prop-maker : 
(-> uninitialized-tuple-descriptor?
    (listof (cons/c struct-type-property? any/c)))
   = make-default-tuple-properties
Implements type and returns a tuple type descriptor for the new implementation. The guard and inspector arguments behave the same as the corresponding arguments of make-struct-type, although there are no transparent or prefab tuple types. The prop-maker argument is similar to the corresponding argument of make-struct-implementation. By default, tuple types are created with properties that make them print and compare in a manner similar to transparent structure types — see make-default-tuple-properties for details.

Examples:
> (define point-descriptor
    (make-tuple-implementation (tuple-type 'point 2)))
> (define point (tuple-descriptor-constructor point-descriptor))
> (define point-x (make-tuple-field-accessor point-descriptor 0 'x))
> (define point-y (make-tuple-field-accessor point-descriptor 1 'y))
> (point 42 888)

(point 42 888)

> (point-x (point 42 888))

42

> (point-y (point 42 888))

888

procedure

(make-tuple-field-accessor descriptor 
  pos 
  [name]) 
  (-> (tuple-descriptor-predicate descriptor) any/c)
  descriptor : tuple-descriptor?
  pos : natural?
  name : (or/c symbol? #f)
   = (symbol->string (format "field~a" pos))
Builds a field accessor function that returns the pos field of instances of the tuple type implemented by descriptor. If name is provided, it is used to derive the name of the function for debugging purposes. See make-tuple-implementation for usage examples.

procedure

(make-default-tuple-properties descriptor)

  (listof (cons/c struct-type-property? any/c))
  descriptor : uninitialized-tuple-descriptor?
Returns implementations of prop:equal+hash and prop:custom-write suitable for most tuple types. This function is called by make-tuple-implementation when no prop-maker argument is supplied.

procedure

(make-tuple-equal+hash descriptor)  equal+hash/c

  descriptor : tuple-descriptor?
Builds an equality-checking function, a hashing function, and a secondary hashing function suitable for use with prop:equal+hash, each of which operate on instances of descriptor. All fields in descriptor are compared and hashed by the returned procedures. This causes equal? to behave roughly the same as it does on transparent structure types.

Examples:
> (define-tuple-type point (x y)
    #:property-maker
    (λ (descriptor)
      (list (cons prop:equal+hash (make-tuple-equal+hash descriptor)))))
> (equal? (point 1 2) (point 1 2))

#t

> (equal? (point 1 2) (point 2 1))

#f

procedure

(make-tuple-custom-write descriptor)  custom-write-function/c

  descriptor : tuple-descriptor?
Constructs a custom write implementation that prints instances of the tuple type described by descriptor in a manner similar to the way that make-constructor-style-printer prints values.

Examples:
> (define-tuple-type point (x y)
    #:property-maker
    (λ (descriptor)
      (define custom-write (make-tuple-custom-write descriptor))
      (list (cons prop:custom-write custom-write))))
> (point 1 2)

(point 1 2)

> (parameterize ([pretty-print-columns 10])
    (pretty-print (point 100000000000000 200000000000000)))

(point

 100000000000000

 200000000000000)

syntax

(define-tuple-type id (field-id ...) option ...)

 
option = #:constructor-name constructor-id
  | #:predicate-name predicate-id
  | #:property-maker prop-maker-expr
 
  prop-maker-expr : 
(-> uninitialized-tuple-descriptor?
    (listof (cons/c struct-type-property? any/c)))
Defines a new tuple type.

Examples:
> (define-tuple-type point (x y))
> (point 1 2)

(point 1 2)

> (point-x (point 42 0))

42

> (point-y (point 42 0))

0