1 Type Reference
The Typed Racket Reference
Type Reference
Special Form Reference
Libraries Provided With Typed Racket
Typed Classes
Typed Units
Utilities
Exploring Types
Deep, Shallow, and Optional Semantics
Typed Racket Syntax Without Type Checking
10
Typed Regions
11
Optimization in Typed Racket
12
Unsafe Typed Racket operations
13
Legacy Forms
14
Compatibility Languages
15
Experimental Features
Bibliography
Index
Type Reference
1.1
Base Types
1.2
Singleton Types
1.3
Base Type Constructors and Supertypes
1.4
Syntax Objects
1.5
Control
1.6
Other Type Constructors
1.7
Other Types
On this page:
Any
Any
Values
Nothing
1.1
Base Types
1.1.1
Numeric Types
Number
Complex
Integer
Float
Flonum
Single-
Flonum
Inexact-
Real
Exact-
Rational
Real
Exact-
Number
Float-
Complex
Single-
Flonum-
Complex
Inexact-
Complex
Imaginary
Exact-
Complex
Exact-
Imaginary
Inexact-
Imaginary
Positive-
Integer
Exact-
Positive-
Integer
Nonnegative-
Integer
Exact-
Nonnegative-
Integer
Natural
Negative-
Integer
Nonpositive-
Integer
Zero
Positive-
Float
Positive-
Flonum
Nonnegative-
Float
Nonnegative-
Flonum
Negative-
Float
Negative-
Flonum
Nonpositive-
Float
Nonpositive-
Flonum
Float-
Negative-
Zero
Flonum-
Negative-
Zero
Float-
Positive-
Zero
Flonum-
Positive-
Zero
Float-
Zero
Flonum-
Zero
Float-
Nan
Flonum-
Nan
Positive-
Single-
Flonum
Nonnegative-
Single-
Flonum
Negative-
Single-
Flonum
Nonpositive-
Single-
Flonum
Single-
Flonum-
Negative-
Zero
Single-
Flonum-
Positive-
Zero
Single-
Flonum-
Zero
Single-
Flonum-
Nan
Positive-
Inexact-
Real
Nonnegative-
Inexact-
Real
Negative-
Inexact-
Real
Nonpositive-
Inexact-
Real
Inexact-
Real-
Negative-
Zero
Inexact-
Real-
Positive-
Zero
Inexact-
Real-
Zero
Inexact-
Real-
Nan
Positive-
Exact-
Rational
Nonnegative-
Exact-
Rational
Negative-
Exact-
Rational
Nonpositive-
Exact-
Rational
Positive-
Real
Nonnegative-
Real
Negative-
Real
Nonpositive-
Real
Real-
Zero
One
Byte
Positive-
Byte
Index
Positive-
Index
Fixnum
Positive-
Fixnum
Nonnegative-
Fixnum
Negative-
Fixnum
Nonpositive-
Fixnum
Ext
Flonum
Positive-
Ext
Flonum
Nonnegative-
Ext
Flonum
Negative-
Ext
Flonum
Nonpositive-
Ext
Flonum
Ext
Flonum-
Negative-
Zero
Ext
Flonum-
Positive-
Zero
Ext
Flonum-
Zero
Ext
Flonum-
Nan
1.1.2
Other Base Types
Boolean
True
False
String
Keyword
Symbol
Char
Void
Input-
Port
Output-
Port
Unquoted-
Printing-
String
Port
Path
Path-
For-
Some-
System
Regexp
PRegexp
Byte-
Regexp
Byte-
PRegexp
Bytes
Namespace
Namespace-
Anchor
Variable-
Reference
Null
EOF
Continuation-
Mark-
Set
Undefined
Module-
Path
Module-
Path-
Index
Resolved-
Module-
Path
Compiled-
Module-
Expression
Compiled-
Expression
Internal-
Definition-
Context
Pretty-
Print-
Style-
Table
Special-
Comment
Struct-
Type-
Property
Impersonator-
Property
Read-
Table
Bytes-
Converter
Parameterization
Custodian
Inspector
Security-
Guard
UDP-
Socket
TCP-
Listener
Logger
Log-
Receiver
Log-
Level
Thread
Thread-
Group
Subprocess
Place
Place-
Channel
Semaphore
FSemaphore
Will-
Executor
Pseudo-
Random-
Generator
Environment-
Variables
Path-
String
1.2
Singleton Types
1.3
Base Type Constructors and Supertypes
Pairof
Listof
List
List*
MListof
MPairof
Tree
Listof
MPair
Top
Boxof
Box
Top
Vectorof
Immutable-
Vectorof
Mutable-
Vectorof
Vector
Immutable-
Vector
Mutable-
Vector
Fl
Vector
Ext
Fl
Vector
Fx
Vector
Vector
Top
Mutable-
Vector
Top
Hash
Table
Immutable-
Hash
Table
Mutable-
Hash
Table
Weak-
Hash
Table
Hash
Table
Top
Mutable-
Hash
Table
Top
Weak-
Hash
Table
Top
Setof
Channelof
Channel
Top
Async-
Channelof
Async-
Channel
Top
Parameterof
Promise
Futureof
Sequenceof
Sequence
Top
Custodian-
Boxof
Thread-
Cellof
Thread-
Cell
Top
Weak-
Boxof
Weak-
Box
Top
Ephemeronof
Evtof
1.4
Syntax Objects
Syntaxof
Identifier
Syntax
Syntax-
Sexpof
Sexp
Datum
1.5
Control
Prompt-
Tagof
Prompt-
Tag
Top
Continuation-
Mark-
Keyof
Continuation-
Mark-
Key
Top
1.6
Other Type Constructors
Top
Bot
Procedure
case-
All
Some
Values
Rec
Struct
Struct-
Type
Struct-
Type
Top
Prefab
Prefab
Top
Struct-
Property
Self
Imp
Has-
Struct-
Property
Union
Intersection
→*
case→
1.7
Other Types
Option
Opaque
9.1
Racket
top
contents
← prev
up
next →
Type Reference
type
Any
Any Racket value. All other types are subtypes of
Any
type
AnyValues
Any number of Racket values of any type.
type
Nothing
The empty type. No values inhabit this type, and
any expression of this type will not evaluate to a value.
1.1
Base Types
1.1.1
Numeric 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.
type
Number
type
Complex
Number
and
Complex
are synonyms. This is the most general
numeric type, including all Racket numbers, both exact and inexact, including
complex numbers.
type
Integer
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.
type
Float
type
Flonum
Includes Racket’s double-precision (default) floating-point numbers and
corresponds to the
flonum?
predicate. This type excludes
single-precision floating-point numbers.
type
Single-Flonum
Includes Racket’s single-precision floating-point numbers and corresponds to
the
single-flonum?
predicate. This type excludes double-precision
floating-point numbers.
type
Inexact-Real
Includes all of Racket’s floating-point numbers, both single- and
double-precision.
type
Exact-Rational
Includes Racket’s exact rationals, which include fractions and exact integers.
type
Real
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.
type
Exact-Number
type
Float-Complex
type
Single-Flonum-Complex
type
Inexact-Complex
type
Imaginary
type
Exact-Complex
type
Exact-Imaginary
type
Inexact-Imaginary
These types correspond to Racket’s complex numbers.
Changed in version 1.7 of package
typed-racket-lib
: Added
Imaginary
Inexact-Complex
Exact-Complex
Exact-Imaginary
Inexact-Imaginary
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).
type
Positive-Integer
type
Exact-Positive-Integer
type
Nonnegative-Integer
type
Exact-Nonnegative-Integer
type
Natural
type
Negative-Integer
type
Nonpositive-Integer
type
Zero
type
Positive-Float
type
Positive-Flonum
type
Nonnegative-Float
type
Nonnegative-Flonum
type
Negative-Float
type
Negative-Flonum
type
Nonpositive-Float
type
Nonpositive-Flonum
type
Float-Negative-Zero
type
Flonum-Negative-Zero
type
Float-Positive-Zero
type
Flonum-Positive-Zero
type
Float-Zero
type
Flonum-Zero
type
Float-Nan
type
Flonum-Nan
type
Positive-Single-Flonum
type
Nonnegative-Single-Flonum
type
Negative-Single-Flonum
type
Nonpositive-Single-Flonum
type
Single-Flonum-Negative-Zero
type
Single-Flonum-Positive-Zero
type
Single-Flonum-Zero
type
Single-Flonum-Nan
type
Positive-Inexact-Real
type
Nonnegative-Inexact-Real
type
Negative-Inexact-Real
type
Nonpositive-Inexact-Real
type
Inexact-Real-Negative-Zero
type
Inexact-Real-Positive-Zero
type
Inexact-Real-Zero
type
Inexact-Real-Nan
type
Positive-Exact-Rational
type
Nonnegative-Exact-Rational
type
Negative-Exact-Rational
type
Nonpositive-Exact-Rational
type
Positive-Real
type
Nonnegative-Real
type
Negative-Real
type
Nonpositive-Real
type
Real-Zero
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
Real-Zero
includes exact
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.
type
One
type
Byte
type
Positive-Byte
type
Index
type
Positive-Index
type
Fixnum
type
Positive-Fixnum
type
Nonnegative-Fixnum
type
Negative-Fixnum
type
Nonpositive-Fixnum
One
includes only the integer
Byte
includes
numbers from
to
255
Index
is bounded by
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:
- : Integer [more precisely: Positive-Byte]
8.3
- : Flonum [more precisely: Positive-Float-No-NaN]
8.3
- : Exact-Rational [more precisely: Positive-Exact-Rational]
8/3
- : Integer [more precisely: Zero]
-1
- : Integer [more precisely: Negative-Fixnum]
-12
3+4i
- : Exact-Number
3+4i
type
ExtFlonum
type
Positive-ExtFlonum
type
Nonnegative-ExtFlonum
type
Negative-ExtFlonum
type
Nonpositive-ExtFlonum
type
ExtFlonum-Negative-Zero
type
ExtFlonum-Positive-Zero
type
ExtFlonum-Zero
type
ExtFlonum-Nan
80-bit
extflonum
types, for the values operated on by
racket/extflonum
exports.
These are not part of the numeric tower.
1.1.2
Other Base Types
type
Boolean
type
True
type
False
type
String
type
Keyword
type
Symbol
type
Char
type
Void
type
Input-Port
type
Output-Port
type
Unquoted-Printing-String
type
Port
type
Path
type
Path-For-Some-System
type
Regexp
type
PRegexp
type
Byte-Regexp
type
Byte-PRegexp
type
Bytes
type
Namespace
type
Namespace-Anchor
type
Variable-Reference
type
Null
type
EOF
type
Continuation-Mark-Set
type
Undefined
type
Module-Path
type
Module-Path-Index
type
Resolved-Module-Path
type
Compiled-Module-Expression
type
Compiled-Expression
type
Internal-Definition-Context
type
Pretty-Print-Style-Table
type
Special-Comment
type
Struct-Type-Property
type
Impersonator-Property
type
Read-Table
type
Bytes-Converter
type
Parameterization
type
Custodian
type
Inspector
type
Security-Guard
type
UDP-Socket
type
TCP-Listener
type
Logger
type
Log-Receiver
type
Log-Level
type
Thread
type
Thread-Group
type
Subprocess
type
Place
type
Place-Channel
type
Semaphore
type
FSemaphore
type
Will-Executor
type
Pseudo-Random-Generator
type
Environment-Variables
These types represent primitive Racket data.
Examples:
#t
- : 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
- : Thread
#
type
Path-String
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.2
Singleton 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
- : True
#t
#:foo
- : '#:foo
'#:foo
bar
- : 'bar
'bar
1.3
Base Type Constructors and Supertypes
type constructor
Pairof
Returns a
pair
type containing
as the
car
and
as the
cdr
Examples:
cons
- : (Pairof One Positive-Byte)
'(1 . 2)
cons
"one"
- : (Pairof One String)
'(1 . "one")
type constructor
Listof
Returns the type of a homogeneous
list
of
type constructor
List
...
Returns a list type with one element, in order,
for each type provided to the
List
type constructor.
type constructor
List
...
trest
...
bound
Returns the type of a list with
one element for each of the
s, plus a sequence of elements
corresponding to
trest
, where
bound
must be an identifier denoting a type variable bound with
...
type constructor
List*
t1
...
Is equivalent to
Pairof
List*
t1
...
List*
is equivalent to
itself.
Examples:
list
- : (List 'a 'b 'c)
'(a b c)
plambda:
...
sym
Symbol
boxes
Boxof
...
ann
cons
sym
boxes
List
Symbol
Boxof
...
- : (All (a ...)
(-> Symbol (Boxof a) ... a (Pairof Symbol (List (Boxof a) ... a))))
#
map
symbol->string
list
- : (Pairof String (Listof String))
'("a" "b" "c")
type constructor
MListof
Returns the type of a homogeneous
mutable list
of
type constructor
MPairof
Returns the type of a
Mutable pair
of
and
type constructor
TreeListof
Returns the type of
treelist
of
type
MPairTop
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:
Any
if
mpair?
error
"not an mpair!"
- : (-> Any MPairTop)
#
type constructor
Boxof
Returns the type of a
box
of
Example:
box
"hello world"
- : (Boxof String)
'#&"hello world"
type
BoxTop
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:
Any
if
box?
error
"not a box!"
- : (-> Any BoxTop)
#
type constructor
Vectorof
Returns the type of a homogeneous
vector
list of
(mutable or immutable).
type constructor
Immutable-Vectorof
Returns the type of a homogeneous immutable
vector
of
Added in version 1.9 of package
typed-racket-lib
type constructor
Mutable-Vectorof
Returns the type of a homogeneous mutable
vector
of
Added in version 1.9 of package
typed-racket-lib
type constructor
Vector
...
Returns the type of a mutable or immutable vector with one
element, in order, for each type provided to the
Vector
type constructor.
Example:
ann
vector
Vector
Fixnum
- : (U (Immutable-Vector Fixnum 'A) (Mutable-Vector Fixnum 'A))
'#(1 A)
type constructor
Immutable-Vector
...
Similar to
Vector
...
, but
for immutable vectors.
Example:
vector-immutable
- : (Immutable-Vector One Positive-Byte Positive-Byte)
'#(1 2 3)
Added in version 1.9 of package
typed-racket-lib
type constructor
Mutable-Vector
...
Similar to
Vector
...
, but
for mutable vectors.
Example:
vector
- : (Mutable-Vector Integer Integer Integer)
'#(1 2 3)
Added in version 1.9 of package
typed-racket-lib
type
FlVector
An
flvector
Example:
flvector
1.0
2.0
3.0
- : FlVector
(flvector 1.0 2.0 3.0)
type
ExtFlVector
An
extflvector
Example:
extflvector
1.0t0
2.0t0
3.0t0
- : ExtFlVector
#
type
FxVector
An
fxvector
Example:
fxvector
- : FxVector
(fxvector 1 2 3)
type
VectorTop
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:
Any
if
vector?
error
"not a vector!"
- : (-> Any VectorTop)
#
type
Mutable-VectorTop
Is the type of a mutable
vector
with unknown length and element types.
type constructor
HashTable
Returns the type of a mutable or immutable
hash table
with key type
and value type
Example:
ann
make-hash
HashTable
Symbol
Integer
- : (HashTable Symbol Integer)
'#hash((a . 1) (b . 2))
type constructor
Immutable-HashTable
Returns the type of an immutable
hash table
with key type
and value type
Example:
#hash
- : (Immutable-HashTable Symbol Integer)
'#hash((a . 1) (b . 2))
Added in version 1.8 of package
typed-racket-lib
type constructor
Mutable-HashTable
Returns the type of a mutable
hash table
that holds keys strongly (see
Weak Boxes
with key type
and value type
Example:
make-hash
- : (Mutable-HashTable Symbol Integer)
'#hash((a . 1) (b . 2))
Added in version 1.8 of package
typed-racket-lib
type constructor
Weak-HashTable
Returns the type of a mutable
hash table
that holds keys weakly with key type
and value type
Example:
make-weak-hash
- : (Weak-HashTable Symbol Integer)
'#hash((a . 1) (b . 2))
Added in version 1.8 of package
typed-racket-lib
type
HashTableTop
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:
Any
if
hash?
error
"not a hash table!"
- : (-> Any HashTableTop)
#
type
Mutable-HashTableTop
Is the type of a mutable
hash table
that holds keys strongly with unknown key and value types.
type
Weak-HashTableTop
Is the type of a mutable
hash table
that holds keys weakly with unknown key and value types.
type constructor
Setof
Returns the type of a
hash set
of
. This includes custom hash sets, but not mutable hash set
or sets that are implemented using
gen:set
Example:
set
- : (Setof Byte)
(set 0 1 2 3)
Example:
seteq
- : (Setof Byte)
(seteq 0 1 2 3)
type constructor
Channelof
Returns the type of a
channel
on which only
s can be sent.
Example:
ann
make-channel
Channelof
Symbol
- : (Channelof Symbol)
#
type
ChannelTop
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:
Any
if
channel?
error
"not a channel!"
- : (-> Any ChannelTop)
#
type constructor
Async-Channelof
Returns the type of an
asynchronous channel
on which only
s can be sent.
Examples:
require
typed/racket/async-channel
ann
make-async-channel
Async-Channelof
Symbol
- : (Async-Channelof Symbol)
#
Added in version 1.1 of package
typed-racket-lib
type
Async-ChannelTop
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:
Any
if
async-channel?
error
"not an async-channel!"
- : (-> Any Async-ChannelTop)
#
Added in version 1.1 of package
typed-racket-lib
type constructor
Parameterof
Parameterof
Returns the type of a
parameter
of
. 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)
#
type constructor
Promise
Returns the type of
promise
of
Example:
delay
- : (Promise Positive-Byte)
#
type constructor
Futureof
Returns the type of
future
which produce a value of type
when touched.
type constructor
Sequenceof
...
Returns the type of
sequence
that produces
Values
...
on each iteration. E.g.,
Sequenceof
is a sequence which produces no values,
Sequenceof
String
is a
sequence of strings,
Sequenceof
Number
String
is a sequence which
produces two values—
a number and a string—
on each iteration, etc.
type
SequenceTop
Is the type of a
sequence
with unknown element
type and is the supertype of all sequences. This type typically
appears in programs via the combination of ocurrence typing ang
sequence?
Example:
lambda:
Any
if
sequence?
error
"not a sequence!"
- : (-> Any SequenceTop)
#
Added in version 1.10 of package
typed-racket-lib
type constructor
Custodian-Boxof
Returns the type of
custodian box
of
type constructor
Thread-Cellof
Returns the type of
thread cell
of
type
Thread-CellTop
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:
Any
if
thread-cell?
error
"not a thread cell!"
- : (-> Any Thread-CellTop)
#
type constructor
Weak-Boxof
Returns the type for a
weak box
whose value is of type
Examples:
make-weak-box
- : (Weak-Boxof Integer)
#
weak-box-value
make-weak-box
- : (U False Integer)
type
Weak-BoxTop
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:
Any
if
weak-box?
error
"not a box!"
- : (-> Any Weak-BoxTop)
#
type constructor
Ephemeronof
Returns the type of an
ephemeron
whose value is of type
type constructor
Evtof
synchronizable event
whose
synchronization result
is of type
Examples:
always-evt
- : (Rec x (Evtof x))
#
system-idle-evt
- : (Evtof Void)
#
ann
thread
displayln
"hello world"
Evtof
Thread
- : (Evtof Thread)
hello world
#
1.4
Syntax Objects
The following type constructors and types respectively create and represent
syntax object
s and their content.
type constructor
Syntaxof
Returns the type of syntax object with content of type
Applying
syntax-e
to a value of type
Syntaxof
produces a
value of type
type
Identifier
A syntax object containing a
symbol
. Equivalent
to
Syntaxof
Symbol
type
Syntax
A syntax object containing only
symbol
s,
keyword
s,
string
s,
byte string
s,
character
s,
boolean
s,
number
s,
box
es containing
Syntax
vector
s of
Syntax
, or (possibly improper)
list
s of
Syntax
Equivalent to
Syntaxof
Syntax-E
type
Syntax-E
The content of syntax objects of type
Syntax
Applying
syntax-e
to a value of type
Syntax
produces a value
of type
Syntax-E
type constructor
Sexpof
Returns the recursive union of
with
symbol
s,
keyword
s,
string
s,
byte string
s,
character
s,
boolean
s,
number
s,
box
es,
vector
s, and (possibly improper)
list
s.
type
Sexp
Applying
syntax->datum
to a value of type
Syntax
produces a value of type
Sexp
. Equivalent to
Sexpof
Nothing
type
Datum
Applying
datum->syntax
to a value of type
Datum
produces a value of type
Syntax
. Equivalent to
Sexpof
Syntax
1.5
Control
The following type constructors and type respectively create and represent
prompt tag
s and keys for
continuation mark
s for use with
delimited continuation functions and continuation mark functions.
type constructor
Prompt-Tagof
Returns the type of a prompt tag to be used in a continuation prompt whose
body produces the type
and whose handler has the type
. The type
must be a function type.
The domain of
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)
#
type
Prompt-TagTop
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:
Any
if
continuation-prompt-tag?
error
"not a prompt tag!"
- : (-> Any Prompt-TagTop)
#
type constructor
Continuation-Mark-Keyof
Returns the type of a continuation mark key that is used for continuation mark
operations such as
with-continuation-mark
and
continuation-mark-set->list
. The type
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)
#
type
Continuation-Mark-KeyTop
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:
Any
if
continuation-mark-key?
error
"not a mark key!"
- : (-> Any Continuation-Mark-KeyTop)
#
1.6
Other Type Constructors
type constructor
->
dom
...
rng
opt-proposition
->
dom
...
rest
rng
->
dom
...
rest
ooo
bound
rng
dom
...
->
rng
opt-proposition
dom
...
rest
->
rng
dom
...
rest
ooo
bound
->
rng
ooo
...
dom
type
mandatory-kw
opt-kw
rng
type
Some
...
type
#:+
proposition
Values
type
...
mandatory-kw
keyword
type
opt-kw
keyword
type
opt-proposition
type
pos-proposition
neg-proposition
object
pos-proposition
#:+
proposition
...
neg-proposition
#:-
proposition
...
object
#:object
index
proposition
Top
Bot
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:
Number
- : (-> 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:
Number
String
length
- : (-> Number String * Index)
#
ormap
- : (All (a c b ...)
(-> (-> a b ... b c) (Listof a) (Listof b) ... b (U False c)))
#
In the third form, the
...
introduced by
ooo
is literal,
and
bound
must be an identifier denoting a type variable.
The
dom
s 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?
#:equality
equality
#:zero
zero
equality
zero
is-zero?
#:equality
- : Any
#f
is-zero?
#:equality
eq?
#:zero
2.0
- : Any
#f
When
opt-proposition
is provided, it specifies the
proposition
for the function type (for an introduction to
propositions in Typed Racket, see
Propositions and Predicates
). For almost all use
cases, only the simplest form of propositions, with a single type after a
, are necessary:
Example:
string?
- : (-> Any Boolean : String)
#
The proposition specifies that when
string?
evaluates to a
true value for a conditional branch, the variable
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 the
propositions. For example, the
filter
function’s first
argument is specified with only a positive proposition:
Example:
filter
- : (All (a b)
(case->
(-> (-> a Any : #:+ b) (Listof a) (Listof b))
(-> (-> a Any) (Listof a) (Listof a))))
#
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 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
Currently, because explicit packing operations for existential types are
not supported, existential type results are only used to annotate accessors
for
Struct-Property
Some
...
type
#:+
proposition
for
rng
specifies an
existential type result
, where the type variables
...
may appear
in
type
and
opt-proposition
. Unpacking the existential type
result is done automatically while checking application of the function.
Changed in version 1.12 of package
typed-racket-lib
: Added
existential type results
type constructor
->
mandatory-dom
...
optional-doms
rest
rng
mandatory-dom
type
keyword
type
optional-doms
optional-dom
...
optional-dom
type
keyword
type
rest
#:rest
type
#:rest-star
type
...
Constructs the type of functions with optional or rest arguments. The first
list of
mandatory-dom
s 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
apply
string-append
str
make-list
how-many
"bar"
If provided, the
#:rest
type
specifies the type of
elements in the rest argument list.
Examples:
+all
->
Integer
#:rest
Integer
Listof
Integer
define
+all
inc
rst
map
Integer
inc
rst
+all
20
- : (Listof Integer)
'(21 22 23)
#:rest-star
type
...
specifies the rest list is a sequence
of types which occurs 0 or more times (i.e. the Kleene closure of the
sequence).
Examples:
print-name+ages
->
#:rest-star
String
Natural
Void
define
print-name+ages
names+ages
let
loop
names+ages
Rec
Null
List*
String
Natural
names+ages
when
pair?
names+ages
printf
"~a is ~a years old!\n"
first
names+ages
second
names+ages
loop
cddr
names+ages
printf
"done printing ~a ages"
length
names+ages
print-name+ages
done printing 0 ages
print-name+ages
"Charlotte"
"Harrison"
"Sydney"
Charlotte is 8 years old!
Harrison is 5 years old!
Sydney is 3 years old!
done printing 3 ages
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
#:y
The syntax for this type constructor matches the syntax of the
->
contract combinator, but with types instead of contracts.
type
Top
type
Bot
These are propositions that can be used with
->
Top
is the propositions with no information.
Bot
is the propositions which means the result cannot happen.
type
Procedure
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:91:0: Type Checker: cannot apply a function with
unknown arity;
function `my-list' has type Procedure which cannot be
applied
in: "socca"
type constructor
...
is the union of the types
...
Example:
Real
if
"yes"
no
- : (-> Real (U 'no String))
#
type constructor
...
is the intersection of the types
...
Example:
#:forall
Symbol
foo
- : 'foo
'foo
type constructor
case->
fun-ty
...
is a function that behaves like all of
the
fun-ty
s, considered in order from first to last.
The
fun-ty
s must all be non-dependent function types (i.e. no
preconditions or dependencies between arguments are currently allowed).
Example:
add-map
case->
Listof
Integer
->
Listof
Integer
Listof
Integer
Listof
Integer
->
Listof
Integer
For the definition of
add-map
look into
case-lambda:
type
t1
t2
...
is the instantiation of the parametric type
at types
t1
t2
...
type
All
...
All
...
ooo
is a parameterization of type
, with
type variables
...
. If
is a function type
constructed with infix
->
, the outer pair of parentheses
around the function type may be omitted.
Examples:
list-length
All
Listof
->
Natural
define
list-length
lst
if
null?
lst
add1
list-length
cdr
lst
list-length
list
- : Integer [more precisely: Nonnegative-Integer]
type
Some
...
See
existential type results
Added in version 1.10 of package
typed-racket-lib
type constructor
Values
...
Returns the type of a sequence of multiple values, with
types
...
. This can only appear as the return type of a
function.
Example:
values
- : (values Integer Integer Integer) [more precisely: (Values One Positive-Byte Positive-Byte)]
Note that a type variable cannot be instantiated with a
Values
....
type. For example, the type
All
->
describes a thunk that
returns exactly one value.
type
where
is a number, boolean or string, is the singleton type containing only that value
type
quote
val
where
val
is a Racket value, is the singleton type containing only that value
type
where
is an identifier can be a reference to a type
name or a type variable
type
Rec
is a recursive type where
is bound to the
recursive type in the body
Examples:
define-type
IntList
Rec
List
Pair
Integer
List
Null
define-type
List
Rec
List
Pair
List
Null
type
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.
type
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-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
#
#
'(0)
#f
#f
type
Struct-TypeTop
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
- : (values (U False Struct-TypeTop) Boolean)
#
#f
type constructor
Prefab
key
type
...
Describes a
prefab
structure with the given (implicitly quoted)
prefab
key
key
and specified field types.
Prefabs are more-or-less tagged polymorphic tuples which
can be directly serialized and whose fields can be accessed
by anyone. Subtyping is covariant for immutable fields and
invariant for mutable fields.
When a prefab struct is defined with
struct
the
struct name is bound at the type-level to the
Prefab
type with the corresponding key and field
types and the constructor expects types corresponding to
those declared for each field. The defined predicate,
however, only tests whether a value is a prefab structure
with the same key and number of fields, but does not inspect
the fields’ values.
Examples:
struct
person
name
String
#:prefab
person
- : (-> String person)
#
person?
- : (-> Any Boolean : (Prefab person Any))
#
person-name
- : (All (x) (case-> (-> (Prefab person x) x) (-> (Prefab person Any) Any)))
#
person
"Jim"
- : (Prefab person String)
'#s(person "Jim")
ann
#s
person
"Dwight"
person
- : (Prefab person String)
'#s(person "Dwight")
ann
#s
person
"Pam"
Prefab
person
String
- : person
'#s(person "Pam")
ann
#s
person
"Michael"
Prefab
person
Any
- : (Prefab person Any)
'#s(person "Michael")
person
Toby
eval:112:0: Type Checker: type mismatch
expected: String
given: 'Toby
in: Toby
ann
#s
person
Toby
Prefab
person
String
eval:113:0: Type Checker: type mismatch
expected: person
given: (Prefab person 'Toby)
in: String
ann
#s
person
Toby
Prefab
person
Symbol
- : (Prefab person Symbol)
'#s(person Toby)
person?
#s
person
"Michael"
- : True
#t
person?
#s
person
Toby
- : True
#t
struct
employee
person
schrute-bucks
Natural
#:prefab
employee
"Oscar"
10000
- : (Prefab (employee person 1) String Nonnegative-Integer)
'#s((employee person 1) "Oscar" 10000)
ann
#s
employee
person
"Oscar"
10000
employee
- : (Prefab (employee person 1) String Nonnegative-Integer)
'#s((employee person 1) "Oscar" 10000)
ann
#s
employee
person
"Oscar"
10000
Prefab
employee
person
String
Natural
- : employee
'#s((employee person 1) "Oscar" 10000)
person?
#s
employee
person
"Oscar"
10000
- : True
#t
employee?
#s
employee
person
"Oscar"
10000
- : True
#t
employee
Toby
-1
eval:123:0: Type Checker: type mismatch
expected: String
given: 'Toby
in: -1
ann
#s
employee
person
Toby
-1
Prefab
employee
person
Symbol
Integer
- : (Prefab (employee person 1) Symbol Integer)
'#s((employee person 1) Toby -1)
person?
#s
employee
person
Toby
-1
- : True
#t
employee?
#s
employee
person
Toby
-1
- : True
#t
type
PrefabTop
key
field-count
Describes all
prefab types with the (implicitly quoted) prefab-key
key
and
field-count
many fields.
For immutable prefabs this is equivalent to
Prefab
key
Any
...
with
field-count
many
occurrences of
Any
. For mutable prefabs, this
describes a prefab that can be read from but not written to
(since we do not know at what type other code may have the
fields typed at).
Examples:
struct
point
Number
Number
#:prefab
#:mutable
point
- : (-> Number Number point)
#
point-x
- : (All (a b)
(case->
(-> (Prefab (point #(0 1)) a b) a)
(-> (PrefabTop (point #(0 1)) 2) Any)))
#
point-y
- : (All (a b)
(case->
(-> (Prefab (point #(0 1)) a b) b)
(-> (PrefabTop (point #(0 1)) 2) Any)))
#
point?
- : (-> Any Boolean : (PrefabTop (point #(0 1)) 2))
#
define
maybe-read-x
if
point?
ann
point-x
Any
not-a-point
define
read-some-x-num
if
point?
ann
point-x
Number
-1
eval:133:0: Type Checker: Polymorphic function `point-x'
could not be applied to arguments:
Types: (PrefabTop (point #(0 1)) 2)
-> Any
Arguments: (PrefabTop (point #(0 1)) 2)
Expected result: Number
in: -1
Added in version 1.7 of package
typed-racket-lib
type constructor
Struct-Property
ty
Describes a property that can be attached to a structure type.
The property value must match the type
ty
Example:
:print-type
prop:input-port
(Struct-Property (U Exact-Nonnegative-Integer Input-Port))
Added in version 1.10 of package
typed-racket-lib
type
Self
This type can only appear in a
Struct-Property
type.
A struct property value is attached to an instance of a structure type;
the
Self
type refers to this instance.
Example:
:print-type
prop:custom-write
(Struct-Property (-> Self Output-Port (U Boolean One Zero) AnyValues))
Added in version 1.10 of package
typed-racket-lib
type
Imp
This type can only appear in a
Struct-Property
type.
An
Imp
value may be a
structure subtype
of the
Self
value, or another instance created by the same struct constructor.
Example:
:print-type
prop:equal+hash
(Struct-Property
(List
(-> Self Imp (-> Any Any Boolean) Any)
(-> Self (-> Any Integer) Integer)
(-> Self (-> Any Integer) Integer)))
Added in version 1.10 of package
typed-racket-lib
type
Has-Struct-Property
prop
This type describes an instance of a
structure type associcated with a
Struct-Property
named
prop
type constructor
An alias for
type constructor
Union
An alias for
type constructor
Intersection
An alias for
type constructor
An alias for
->
type constructor
→*
An alias for
->
type constructor
case→
An alias for
case->
type
An alias for
All
1.7
Other Types
type constructor
Option
Either
or
#f
type constructor
Opaque
A type constructed using the
#:opaque
clause of
require/typed
top
contents
← prev
up
next →