On this page:
define-tuple-type
2.3.1 Tuple Type Information
tuple-type?
tuple-type
tuple-type-name
tuple-type-size
tuple-type-predicate-name
tuple-type-constructor-name
tuple-type-accessor-name
2.3.2 Tuple Type Descriptors
tuple-descriptor?
initialized-tuple-descriptor?
uninitialized-tuple-descriptor?
tuple-descriptor-type
tuple-descriptor-predicate
tuple-descriptor-constructor
tuple-descriptor-accessor
2.3.3 Dynamically Implementing Tuple Types
make-tuple-implementation
make-tuple-field-accessor
default-tuple-properties
default-tuple-equal+  hash
default-tuple-custom-write
2.3.4 Tuple Chaperones and Impersonators
tuple-impersonate
7.7

2.3 Tuple Types

 (require rebellion/type/tuple) package: rebellion

A tuple type is a kind of data type for composite values that contain an ordered list of fields. The definition of each tuple type declares how many fields it has and what their names are, although those names are only observable at compile time. Constructing an instance of a tuple type requires passing a positional argument for each field to the type’s constructor. Tuple types are useful when a fixed number of different pieces of data together represent a single logical thing, and there is an obvious order to those pieces.

Examples:
(define-tuple-type point (x y))
 
(define/contract (point-distance start end)
  (-> point? point? real?)
  (match-define (point x1 y1) start)
  (match-define (point x2 y2) end)
  (define dx (- x2 x1))
  (define dy (- y2 y1))
  (sqrt (+ (sqr dx) (sqr dy))))

 

> (point-distance (point 0 0) (point 3 4))

5

> (point-distance (point 0 0) (list 3 4))

point-distance: contract violation

  expected: point?

  given: '(3 4)

  in: the 2nd argument of

      (-> point? point? real?)

  contract from: (function point-distance)

  blaming: top-level

   (assuming the contract is correct)

  at: eval:3.0

syntax

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

 
option = #:omit-root-binding
  | #:constructor-name constructor-id
  | #:predicate-name predicate-id
  | #:pattern-name pattern-id
  | #:property-maker prop-maker-expr
 
  prop-maker-expr : 
(-> uninitialized-tuple-descriptor?
    (listof (cons/c struct-type-property? any/c)))
Creates a new tuple type named id and binds the following identifiers:

Additionally, unless #:omit-root-binding is specified, the original id is bound to pattern-id when used in match patterns and to constructor-id when used as an expression. Use #:omit-root-binding when you want control over what id is bound to, such as when creating a smart constructor.

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

(point 1 2)

> (point? (point 1 2))

#t

> (point-x (point 42 0))

42

> (point-y (point 42 0))

0

> (match-define (point (? positive? x) (? negative? y)) (point 3 -3))

2.3.1 Tuple Type Information

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 : interned-symbol?
  size : natural?
  predicate-name : (or/c interned-symbol? #f) = #f
  constructor-name : (or/c interned-symbol? #f) = #f
  accessor-name : (or/c interned-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)  interned-symbol?

  type : tuple-type?

procedure

(tuple-type-size type)  natural?

  type : tuple-type?

procedure

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

  type : tuple-type?

procedure

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

  type : tuple-type?

procedure

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

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

2.3.2 Tuple Type Descriptors

procedure

(tuple-descriptor? v)  boolean?

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

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.

2.3.3 Dynamically Implementing Tuple Types

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)))
   = default-tuple-properties
Implements type and returns a 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 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 interned-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.

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

(default-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 (default-tuple-equal+hash descriptor)))))
> (equal? (point 1 2) (point 1 2))

#t

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

#f

procedure

(default-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 (default-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)

2.3.4 Tuple Chaperones and Impersonators

procedure

(tuple-impersonate instance 
  descriptor 
  [#:properties properties 
  #:chaperone? chaperone?]) 
  (tuple-descriptor-predicate descriptor)
  instance : (tuple-descriptor-predicate descriptor)
  descriptor : initialized-tuple-descriptor?
  properties : (hash/c impersonator-property? any/c #:immutable #t)
   = empty-hash
  chaperone? : boolean? = #t
Returns an impersonator of instance with each impersonator property in properties attached to it. If chaperone? is true (the default), the returned impersonator is a chaperone.