6.5

### 1Type Reference

 syntax
Any Racket value. All other types are subtypes of Any.

 syntax
Any number of Racket values of any type.

 syntax
The empty type. No values inhabit this type, and any expression of this type will not evaluate to a value.

#### 1.1Base Types

##### 1.1.1Numeric Types

These types represent the hierarchy of numbers of Racket. The diagram below shows the relationships between the types in the hierarchy.

The regions with a solid border are layers of the numeric hierarchy corresponding to sets of numbers such as integers or rationals. Layers contained within another are subtypes of the layer containing them. For example, Exact-Rational is a subtype of Exact-Number.

The Real layer is also divided into positive and negative types (shown with a dotted line). The Integer layer is subdivided into several fixed-width integers types, detailed later in this section.

 syntax
 syntax
Number and Complex are synonyms. This is the most general numeric type, including all Racket numbers, both exact and inexact, including complex numbers.

 syntax
Includes Racket’s exact integers and corresponds to the exact-integer? predicate. This is the most general type that is still valid for indexing and other operations that require integral values.

 syntax
 syntax
Includes Racket’s double-precision (default) floating-point numbers and corresponds to the flonum? predicate. This type excludes single-precision floating-point numbers.

 syntax
Includes Racket’s single-precision floating-point numbers and corresponds to the single-flonum? predicate. This type excludes double-precision floating-point numbers.

 syntax
Includes all of Racket’s floating-point numbers, both single- and double-precision.

 syntax
Includes Racket’s exact rationals, which include fractions and exact integers.

 syntax
Includes all of Racket’s real numbers, which include both exact rationals and all floating-point numbers. This is the most general type for which comparisons (e.g. <) are defined.

 syntax
 syntax
 syntax
 syntax
These types correspond to Racket’s complex numbers.

The above types can be subdivided into more precise types if you want to enforce tighter constraints. Typed Racket provides types for the positive, negative, non-negative and non-positive subsets of the above types (where applicable).

 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
Natural and Exact-Nonnegative-Integer are synonyms. So are the integer and exact-integer types, and the float and flonum types. Zero includes only the integer 0. Real-Zero includes exact 0 and all the floating-point zeroes.

These types are useful when enforcing that values have a specific sign. However, programs using them may require additional dynamic checks when the type-checker cannot guarantee that the sign constraints will be respected.

In addition to being divided by sign, integers are further subdivided into range-bounded types. The relationships between most of the range-bounded types are shown in this diagram:

Like the previous diagram, types nested inside of another in the diagram are subtypes of its containing types.

 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
One includes only the integer 1. Byte includes numbers from 0 to 255. Index is bounded by 0 and by the length of the longest possible Racket vector. Fixnum includes all numbers represented by Racket as machine integers. For the latter two families, the sets of values included in the types are architecture-dependent, but typechecking is architecture-independent.

These types are useful to enforce bounds on numeric values, but given the limited amount of closure properties these types offer, dynamic checks may be needed to check the desired bounds at runtime.

Examples:
 > 7 - : Integer [more precisely: Positive-Byte] 7 > 8.3 - : Flonum [more precisely: Positive-Flonum] 8.3 > (/ 8 3) - : Exact-Rational [more precisely: Positive-Exact-Rational] 8/3 > 0 - : Integer [more precisely: Zero] 0 > -12 - : Integer [more precisely: Negative-Fixnum] -12 > 3+4i - : Exact-Number 3+4i

 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
80-bit extflonum types, for the values operated on by racket/extflonum exports. These are not part of the numeric tower.

##### 1.1.2Other Base Types

 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
 syntax
These types represent primitive Racket data.

Examples:
 > #t - : Boolean [more precisely: True] #t > #f - : False #f > "hello" - : String "hello" > (current-input-port) - : Input-Port # > (current-output-port) - : Output-Port # > (string->path "/") - : Path # > #rx"a*b*" - : Regexp #rx"a*b*" > #px"a*b*" - : PRegexp #px"a*b*" > '#"bytes" - : Bytes #"bytes" > (current-namespace) - : Namespace # > #\b - : Char #\b > (thread (lambda () (add1 7))) - : Thread #

 syntax
The union of the Path and String types. Note that this does not match exactly what the predicate path-string? recognizes. For example, strings that contain the character #\nul have the type Path-String but path-string? returns #f for those strings. For a complete specification of which strings path-string? accepts, see its documentation.

#### 1.2Singleton Types

Some kinds of data are given singleton types by default. In particular, booleans, symbols, and keywords have types which consist only of the particular boolean, symbol, or keyword. These types are subtypes of Boolean, Symbol and Keyword, respectively.

Examples:
 > #t - : Boolean [more precisely: True] #t > '#:foo - : '#:foo '#:foo > 'bar - : Symbol [more precisely: 'bar] 'bar

#### 1.3Containers

The following base types are parametric in their type arguments.

 syntax(Pairof s t)
is the pair containing s as the car and t as the cdr

Examples:
 > (cons 1 2) - : (Pairof One Positive-Byte) '(1 . 2) > (cons 1 "one") - : (Pairof One String) '(1 . "one")

 syntax(Listof t)
Homogenous lists of t
 syntax(List t ...)
is the type of the list with one element, in order, for each type provided to the List type constructor.
 syntax(List t ... trest ... bound)
is the type of a list with one element for each of the ts, plus a sequence of elements corresponding to trest, where bound must be an identifier denoting a type variable bound with ....
 syntax(List* t t1 ... s)
is equivalent to (Pairof t (List* t1 ... s)).

Examples:
> (list 'a 'b 'c)

- : (Listof (U 'a 'b 'c)) [more precisely: (List 'a 'b 'c)]

'(a b c)

 > (plambda: (a ...) ([sym : Symbol] boxes : (Boxof a) ... a) (ann (cons sym boxes) (List Symbol (Boxof a) ... a)))
 - : (All (a ...) (-> Symbol (Boxof a) ... a (Pairof Symbol (List (Boxof a) ... a))))

#<procedure>

> (map symbol->string (list 'a 'b 'c))

- : (Listof String) [more precisely: (Pairof String (Listof String))]

'("a" "b" "c")

 syntax(MListof t)
Homogenous mutable lists of t.
 syntax(MPairof t u)
Mutable pairs of t and u.

 syntax
is the type of a mutable pair with unknown element types and is the supertype of all mutable pair types. This type typically appears in programs via the combination of occurrence typing and mpair?.

Example:
 > (lambda: ([x : Any]) (if (mpair? x) x (error "not an mpair!"))) - : (-> Any MPairTop) #

 syntax(Boxof t)
A box of t

Example:
 > (box "hello world") - : (Boxof String) '#&"hello world"

 syntax
is the type of a box with an unknown element type and is the supertype of all box types. Only read-only box operations (e.g. unbox) are allowed on values of this type. This type typically appears in programs via the combination of occurrence typing and box?.

Example:
 > (lambda: ([x : Any]) (if (box? x) x (error "not a box!"))) - : (-> Any BoxTop) #

 syntax(Vectorof t)
Homogenous vectors of t
 syntax(Vector t ...)
is the type of the list with one element, in order, for each type provided to the Vector type constructor.

Examples:
 > (vector 1 2 3) - : (Vector Integer Integer Integer) '#(1 2 3) > #(a b c) - : (Vector Symbol Symbol Symbol) '#(a b c)

 syntax

Example:
 > (flvector 1.0 2.0 3.0) - : FlVector (flvector 1.0 2.0 3.0)

 syntax

Example:
 > (extflvector 1.0t0 2.0t0 3.0t0) - : ExtFlVector #

 syntax

Example:
 > (fxvector 1 2 3) - : FxVector (fxvector 1 2 3)

 syntax
is the type of a vector with unknown length and element types and is the supertype of all vector types. Only read-only vector operations (e.g. vector-ref) are allowed on values of this type. This type typically appears in programs via the combination of occurrence typing and vector?.

Example:
 > (lambda: ([x : Any]) (if (vector? x) x (error "not a vector!"))) - : (-> Any VectorTop) #

 syntax(HashTable k v)
is the type of a hash table with key type k and value type v.

Example:
 > #hash((a . 1) (b . 2)) - : (HashTable Symbol Integer) '#hash((a . 1) (b . 2))

 syntax
is the type of a hash table with unknown key and value types and is the supertype of all hash table types. Only read-only hash table operations (e.g. hash-ref) are allowed on values of this type. This type typically appears in programs via the combination of occurrence typing and hash?.

Example:
 > (lambda: ([x : Any]) (if (hash? x) x (error "not a hash table!"))) - : (-> Any HashTableTop) #

 syntax(Setof t)
is the type of a hash set of t. This includes custom hash sets, but not mutable hash set or sets that are implemented using gen:set.

Example:
 > (set 0 1 2 3) - : (Setof Byte) (set 1 3 0 2)

Example:
 > (seteq 0 1 2 3) - : (Setof Byte) (seteq 0 1 2 3)

 syntax(Channelof t)
A channel on which only ts can be sent.

Example:
 > (ann (make-channel) (Channelof Symbol)) - : (Channelof Symbol) #

 syntax
is the type of a channel with unknown message type and is the supertype of all channel types. This type typically appears in programs via the combination of occurrence typing and channel?.

Example:
 > (lambda: ([x : Any]) (if (channel? x) x (error "not a channel!"))) - : (-> Any ChannelTop) #

 syntax
An asynchronous channel on which only ts can be sent.

Examples:
> (require typed/racket/async-channel)
> (ann (make-async-channel) (Async-Channelof Symbol))

- : (Async-Channelof Symbol)

#<async-channel>

Added in version 1.1 of package typed-racket-lib.

 syntax
is the type of an asynchronous channel with unknown message type and is the supertype of all asynchronous channel types. This type typically appears in programs via the combination of occurrence typing and async-channel?.

Examples:
> (require typed/racket/async-channel)
> (lambda: ([x : Any]) (if (async-channel? x) x (error "not an async-channel!")))

- : (-> Any Async-ChannelTop)

#<procedure>

Added in version 1.1 of package typed-racket-lib.

 syntax(Parameterof t) (Parameterof s t)
A parameter of t. If two type arguments are supplied, the first is the type the parameter accepts, and the second is the type returned.

Examples:
 > current-input-port - : (Parameterof Input-Port) # > current-directory - : (Parameterof Path-String Path) #

 syntax(Promise t)
A promise of t.

Example:
 > (delay 3) - : (Promise Positive-Byte) #

 syntax(Futureof t)
A future which produce a value of type t when touched.

 syntax(Sequenceof t)
A sequence that produces values of type t on each iteration.

 syntax
A custodian box of t.
 syntax
 syntax
is the type of a thread cell with unknown element type and is the supertype of all thread cell types. This type typically appears in programs via the combination of occurrence typing and thread-cell?.

Example:
 > (lambda: ([x : Any]) (if (thread-cell? x) x (error "not a thread cell!"))) - : (-> Any Thread-CellTop) #

 syntax(Weak-Boxof t)
The type for a weak box whose value is of type t.

Examples:
 > (make-weak-box 5) - : (Weak-Boxof Positive-Byte) # > (weak-box-value (make-weak-box 5)) - : (U Positive-Byte False) 5

 syntax
is the type of a weak box with an unknown element type and is the supertype of all weak box types. This type typically appears in programs via the combination of occurrence typing and weak-box?.

Example:
 > (lambda: ([x : Any]) (if (weak-box? x) x (error "not a box!"))) - : (-> Any Weak-BoxTop) #

 syntax(Ephemeronof t)
An ephemeron whose value is of type t.

 syntax(Evtof t)

Examples:
 > always-evt - : (Rec x (Evtof x)) # > (system-idle-evt) - : (Evtof Void) # > (ann (thread (λ () (displayln "hello world"))) (Evtof Thread)) - : (Evtof Thread) #

#### 1.4Syntax Objects

The following types represent syntax objects and their content.

 syntax(Syntaxof t)
A syntax object with content of type t. Applying syntax-e to a value of type (Syntaxof t) produces a value of type t.

 syntax
A syntax object containing a symbol. Equivalent to (Syntaxof Symbol).

 syntax
A syntax object containing only symbols, keywords, strings, characters, booleans, numbers, boxes containing Syntax, vectors of Syntax, or (possibly improper) lists of Syntax. Equivalent to (Syntaxof Syntax-E).

 syntax
The content of syntax objects of type Syntax. Applying syntax-e to a value of type Syntax produces a value of type Syntax-E.

 syntax(Sexpof t)
The recursive union of t with symbols, keywords, strings, characters, booleans, numbers, boxes, vectors, and (possibly improper) lists.

 syntax
Applying syntax->datum to a value of type Syntax produces a value of type Sexp. Equivalent to (Sexpof Nothing).

 syntax
Applying datum->syntax to a value of type Datum produces a value of type Syntax. Equivalent to (Sexpof Syntax).

#### 1.5Control

The following types represent prompt tags and keys for continuation marks for use with delimited continuation functions and continuation mark functions.

 syntax(Prompt-Tagof s t)
A prompt tag to be used in a continuation prompt whose body produces the type s and whose handler has the type t. The type t must be a function type.

The domain of t determines the type of the values that can be aborted, using abort-current-continuation, to a prompt with this prompt tag.

Example:
 > (make-continuation-prompt-tag 'prompt-tag) - : (Prompt-Tagof Any Any) #

 syntax
is the type of a prompt tag with unknown body and handler types and is the supertype of all prompt tag types. This type typically appears in programs via the combination of occurrence typing and continuation-prompt-tag?.

Example:
> (lambda: ([x : Any]) (if (continuation-prompt-tag? x) x (error "not a prompt tag!")))
 hello world - : (-> Any Prompt-TagTop)

#<procedure>

 syntax
A continuation mark key that is used for continuation mark operations such as with-continuation-mark and continuation-mark-set->list. The type t represents the type of the data that is stored in the continuation mark with this key.

Example:
 > (make-continuation-mark-key 'mark-key) - : (Continuation-Mark-Keyof Any) #

 syntax
is the type of a continuation mark key with unknown element type and is the supertype of all continuation mark key types. This type typically appears in programs via the combination of occurrence typing and continuation-mark-key?.

Example:
 > (lambda: ([x : Any]) (if (continuation-mark-key? x) x (error "not a mark key!"))) - : (-> Any Continuation-Mark-KeyTop) #

#### 1.6Other Type Constructors

syntax

(-> dom ... rng optional-filter)

(-> dom ... rest * rng)
(-> dom ... rest ooo bound rng)
(dom ... -> rng optional-filter)
(dom ... rest * -> rng)
(dom ... rest ooo bound -> rng)

ooo = ...

dom = type
| mandatory-kw
| optional-kw

mandatory-kw = keyword type

optional-kw = [keyword type]

optional-filter =
| : type
| : pos-filter neg-filter object

pos-filter =
| #:+ proposition ...

neg-filter =
| #:- proposition ...

object =
| #:object index

proposition = type
| (! type)
| (type @ path-elem ... index)
| (! type @ path-elem ... index)
| (and proposition ...)
| (or proposition ...)
| (implies proposition ...)

path-elem = car
| cdr

index = positive-integer
| (positive-integer positive-integer)
| identifier
The type of functions from the (possibly-empty) sequence dom .... to the rng type.

Examples:
 > (λ: ([x : Number]) x) - : (-> Number Number) # > (λ: () 'hello) - : (-> 'hello) #

The second form specifies a uniform rest argument of type rest, and the third form specifies a non-uniform rest argument of type rest with bound bound. The bound refers to the type variable that is in scope within the rest argument type.

Examples:
> (λ: ([x : Number]    y : String *)  (length y))

- : (-> Number String * Index)

#<procedure>

> ormap
 - : (All (a c b ...) (-> (-> a b ... b c) (Listof a) (Listof b) ... b (U False c)))

#<procedure:ormap>

In the third form, the ... introduced by ooo is literal, and bound must be an identifier denoting a type variable.

The doms can include both mandatory and optional keyword arguments. Mandatory keyword arguments are a pair of keyword and type, while optional arguments are surrounded by a pair of parentheses.

Examples:
> (:print-type file->string)

(-> Path-String [#:mode (U 'binary 'text)] String)

> (: is-zero? : (-> Number #:equality (-> Number Number Any) [#:zero Number] Any))
 > (define (is-zero? n #:equality equality #:zero [zero 0]) (equality n zero))
> (is-zero? 2 #:equality =)

- : Any

#f

> (is-zero? 2 #:equality eq? #:zero 2.0)

- : Any

#f

When optional-filter is provided, it specifies the filter for the function type (for an introduction to filters, see Filters and Predicates). For almost all use cases, only the simplest form of filters, with a single type after a :, are necessary:

Example:
 > string? - : (-> Any Boolean : String) #

The filter specifies that when (string? x) evaluates to a true value for a conditional branch, the variable x in that branch can be assumed to have type String. Likewise, if the expression evaluates to #f in a branch, the variable does not have type String.

In some cases, asymmetric type information is useful in filters. For example, the filter function’s first argument is specified with only a positive filter:

Example:
> filter
 - : (All (a b) (case-> (-> (-> a Any : #:+ b) (Listof a) (Listof b)) (-> (-> a Any) (Listof a) (Listof a))))

#<procedure:filter>

The use of #:+ indicates that when the function applied to a variable evaluates to a true value, the given type can be assumed for the variable. However, the type-checker gains no information in branches in which the result is #f.

Conversely, #:- specifies that a function provides information for the false branch of a conditional.

The other filter proposition cases are rarely needed, but the grammar documents them for completeness. They correspond to logical operations on the propositions.

The type of functions can also be specified with an infix -> which comes immediately before the rng type. The fourth through sixth forms match the first three cases, but with the infix style of arrow.

Examples:
> (: add2 (Number -> Number))
> (define (add2 n) (+ n 2))

syntax

(->* (mandatory-dom ...) optional-doms rest rng)

mandatory-dom = type
| keyword type

optional-doms =
| (optional-dom ...)

optional-dom = type
| keyword type

rest =
| #:rest type
Constructs the type of functions with optional or rest arguments. The first list of mandatory-doms correspond to mandatory argument types. The list optional-doms, if provided, specifies the optional argument types.

Examples:
> (: append-bar (->* (String) (Positive-Integer) String))
 > (define (append-bar str [how-many 1]) (apply string-append str (make-list how-many "bar")))

If provided, the rest expression specifies the type of elements in the rest argument list.

Examples:
> (: +all (->* (Integer) #:rest Integer (Listof Integer)))
 > (define (+all inc . rst) (map (λ: ([x : Integer]) (+ x inc)) rst))
> (+all 20 1 2 3)

- : (Listof Integer)

'(21 22 23)

Both the mandatory and optional argument lists may contain keywords paired with types.

Examples:
> (: kw-f (->* (#:x Integer) (#:y Integer) Integer))
> (define (kw-f #:x x #:y [y 0]) (+ x y))

The syntax for this type constructor matches the syntax of the ->* contract combinator, but with types instead of contracts.

 syntax
 syntax
These are filters that can be used with ->. Top is the filter with no information. Bot is the filter which means the result cannot happen.

 syntax
is the supertype of all function types. The Procedure type corresponds to values that satisfy the procedure? predicate. Because this type encodes only the fact that the value is a procedure, and not its argument types or even arity, the type-checker cannot allow values of this type to be applied.

For the types of functions with known arity and argument types, see the -> type constructor.

Examples:
> (: my-list Procedure)
> (define my-list list)
> (my-list "zwiebelkuchen" "socca")

eval:82:0: Type Checker: cannot apply a function with

unknown arity;

function `my-list' has type Procedure which cannot be

applied

in: "socca"

 syntax(U t ...)
is the union of the types t ....

Example:
 > (λ: ([x : Real])(if (> 0 x) "yes" 'no)) - : (-> Real (U String 'no)) #

 syntax(case-> fun-ty ...)
is a function that behaves like all of the fun-tys, considered in order from first to last. The fun-tys must all be function types constructed with ->.

Example:
 > (: add-map : (case-> [(Listof Integer) -> (Listof Integer)] [(Listof Integer) (Listof Integer) -> (Listof Integer)]))

For the definition of add-map look into case-lambda:.

 syntax(t t1 t2 ...)
is the instantiation of the parametric type t at types t1 t2 ...
 syntax(All (a ...) t) (All (a ... a ooo) t)
is a parameterization of type t, with type variables a .... If t is a function type constructed with infix ->, the outer pair of parentheses around the function type may be omitted.

Examples:
> (: list-length : (All (A) (Listof A) -> Natural))
 > (define (list-length lst) (if (null? lst) 0 (add1 (list-length (cdr lst)))))
> (list-length (list 1 2 3))

- : Integer [more precisely: Nonnegative-Integer]

3

 syntax(Values t ...)
is the type of a sequence of multiple values, with types t .... This can only appear as the return type of a function.

Example:
> (values 1 2 3)

- : (values Integer Integer Integer) [more precisely: (Values One Positive-Byte Positive-Byte)]

 1 2 3

 syntaxv
where v is a number, boolean or string, is the singleton type containing only that value
 syntax(quote val)
where val is a Racket value, is the singleton type containing only that value
 syntaxi
where i is an identifier can be a reference to a type name or a type variable
 syntax(Rec n t)
is a recursive type where n is bound to the recursive type in the body t

Examples:
> (define-type IntList (Rec List (Pair Integer (U List Null))))
> (define-type (List A) (Rec List (Pair A (U List Null))))

 syntax(Struct st)
is a type which is a supertype of all instances of the potentially-polymorphic structure type st. Note that structure accessors for st will not accept (Struct st) as an argument.

 syntax(Struct-Type st)
is a type for the structure type descriptor value for the structure type st. Values of this type are used with reflective operations such as struct-type-info.

Examples:
> struct:arity-at-least

- : (StructType arity-at-least)

#<struct-type:arity-at-least>

> (struct-type-info struct:arity-at-least)
 - : (values Symbol Integer Integer (-> arity-at-least Nonnegative-Integer Any) (-> arity-at-least Nonnegative-Integer Nothing Void) (Listof Nonnegative-Integer) (U False Struct-TypeTop) Boolean) [more precisely: (values Symbol Nonnegative-Integer Nonnegative-Integer (-> arity-at-least Nonnegative-Integer Any) (-> arity-at-least Nonnegative-Integer Nothing Void) (Listof Nonnegative-Integer) (U False Struct-TypeTop) Boolean)]
 'arity-at-least 1 0 # # '(0) #f #f

 syntax
is the supertype of all types for structure type descriptor values. The corresponding structure type is unknown for values of this top type.

Example:
> (struct-info (arity-at-least 0))

- : (values (U False Struct-TypeTop) Boolean)

 # #f

 syntax(Prefab key type ...)
Represents a prefab structure type with the given prefab structure key (such as one returned by prefab-struct-key or accepted by make-prefab-struct) and with the given types for each field.

In the case of prefab structure types with supertypes, the field types of the supertypes come before the field types of the child structure type. The order of types matches the order of arguments to a prefab struct constructor.

Examples:

- : (Prefab salad String String)

 syntax
An alias for ->.
 syntax
An alias for case->.
 syntax
An alias for All.

#### 1.7Other Types

 syntax(Option t)
Either t or #f
 syntax(Opaque t)
A type constructed using the #:opaque clause of require/typed.