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
tuple-type-make-implementation
make-tuple-field-accessor
make-default-tuple-properties
19.1 Tuple Type Definitions
define-tuple-type
7.3

19 Tuple Types

 (require rebellion/tuple-type) 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 tuple-type-make-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 tuple-type-make-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 tuple-type-make-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 tuple-type-make-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

(tuple-type-make-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-type/descriptor. 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
    (tuple-type-make-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 tuple-type-make-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 tuple-type-make-implementation when no prop-maker argument is supplied.

19.1 Tuple Type Definitions

 (require rebellion/tuple-type-definition)
  package: rebellion

syntax

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

 
option = #:constructor constructor-id
  | #:predicate 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