2 Beginning Typed Racket
Recall the typed module from Quick Start:
#lang typed/racket (struct: pt ([x : Real] [y : Real])) (: distance (> pt pt Real)) (define (distance p1 p2) (sqrt (+ (sqr ( (ptx p2) (ptx p1))) (sqr ( (pty p2) (pty p1))))))
Let us consider each element of this program in turn.
This specifies that the module is written in the typed/racket language, which is a typed version of the racket language. Typed versions of other languages are provided as well; for example, the typed/racket/base language corresponds to racket/base.
Many forms in Typed Racket have the same name as the untyped forms, with a : suffix.
This declares that distance has the type (> pt pt Real).
The type (> pt pt Real) is a function type, that is, the type of a procedure. The input type, or domain, is two arguments of type pt, which refers to an instance of the pt structure. The > indicates that this is a function type. The range type, or output type, is the last element in the function type, in this case Real.
If you are familiar with contracts, the notation for function types is similar to function contract combinators.
(define (distance p1 p2) (sqrt (+ (sqr ( (ptx p2) (ptx p1))) (sqr ( (pty p2) (pty p1))))))
This definition is unchanged from the untyped version of the code. The goal of Typed Racket is to allow almost all definitions to be typechecked without change. The typechecker verifies that the body of the function has the type Real, under the assumption that p1 and p2 have type pt, taking these types from the earlier type declaration. Since the body does have this type, the program is accepted.
In the Typed Racket REPL, calling distance will show the result as usual and will also print the result’s type:
> (distance (pt 0 0) (pt 3.1415 2.7172))  : Real
4.153576541969583
Just evaluating the function name will print the function value and its type, which can be useful for discovering the types that Typed Racket ascribes to Racket functions. Alternatively, the :printtype command will just print the type:
> distance  : (> pt pt Real)
#<procedure:distance>
> stringlength  : (> String Index)
#<procedure:stringlength>
> (:printtype stringref) (> String Integer Char)
2.1 Datatypes and Unions
Many data structures involve multiple variants. In Typed Racket, we represent these using union types, written (U t1 t2 ...).
#lang typed/racket (definetype Tree (U leaf node)) (struct: leaf ([val : Number])) (struct: node ([left : Tree] [right : Tree])) (: treeheight (> Tree Integer)) (define (treeheight t) (cond [(leaf? t) 1] [else (max (+ 1 (treeheight (nodeleft t))) (+ 1 (treeheight (noderight t))))])) (: treesum (> Tree Number)) (define (treesum t) (cond [(leaf? t) (leafval t)] [else (+ (treesum (nodeleft t)) (treesum (noderight t)))]))
In this module, we have defined two new datatypes: leaf and node. We’ve also defined the type name Tree to be (U node leaf), which represents a binary tree of numbers. In essence, we are saying that the treeheight function accepts a Tree, which is either a node or a leaf, and produces a number.
In order to calculate interesting facts about trees, we have to take them apart and get at their contents. But since accessors such as nodeleft require a node as input, not a Tree, we have to determine which kind of input we were passed.
For this purpose, we use the predicates that come with each defined structure. For example, the leaf? predicate distinguishes leafs from all other Typed Racket values. Therefore, in the first branch of the cond clause in treesum, we know that t is a leaf, and therefore we can get its value with the leafval function.
In the else clauses of both functions, we know that t is not a leaf, and since the type of t was Tree by process of elimination we can determine that t must be a node. Therefore, we can use accessors such as nodeleft and noderight with t as input.
The process by which Typed Racket typechecks the bodies of the cond clauses, using information from the predicate checks, is called occurrence typing and is described in detail in Occurrence Typing.
2.2 Type Errors
When Typed Racket detects a type error in the module, it raises an error before running the program.
Example:  
