1 Starting with Typed Scheme
1.1 A First Function
1.2 Adding more complexity
1.3 Defining New Datatypes
1.4 Recursive Datatypes and Unions
2 Polymorphism
2.1 Polymorphic Data Structures
2.2 Polymorphic Functions
3 Variable-Arity Functions: Programming with Rest Arguments
3.1 Uniform Variable-Arity Functions
3.2 Non-Uniform Variable-Arity Functions
Version: 4.2.1

Typed Scheme: Scheme with Static Types

Sam Tobin-Hochstadt

Typed Scheme is a Scheme-like language, with a type system that supports common Scheme programming idioms. Explicit type declarations are required – that is, there is no type inference. The language supports a number of features from previous work on type systems that make it easier to type Scheme programs, as well as a novel idea dubbed occurrence typing for case discrimination.

Typed Scheme is also designed to integrate with the rest of your PLT Scheme system. It is possible to convert a single module to Typed Scheme, while leaving the rest of the program unchanged. The typed module is protected from the untyped code base via automatically-synthesized contracts.

Further information on Typed Scheme is available from the homepage.

1 Starting with Typed Scheme

If you already know PLT Scheme, or even some other Scheme, it should be easy to start using Typed Scheme.

1.1 A First Function

The following program defines the Fibonacci function in PLT Scheme:

  #lang scheme
  (define (fib n)
    (cond [(= 0 n) 1]
          [(= 1 n) 1]
          [else (+ (fib (- n 1)) (fib (- n 2)))]))

This program defines the same program using Typed Scheme.

  #lang typed-scheme
  (: fib (Number -> Number))
  (define (fib n)
    (cond [(= 0 n) 1]
          [(= 1 n) 1]
          [else (+ (fib (- n 1)) (fib (- n 2)))]))

There are two differences between these programs:

In general, these are most of the changes that have to be made to a PLT Scheme program to transform it into a Typed Scheme program.

Changes to uses of require may also be necessary - these are described later.

1.2 Adding more complexity

Other typed binding forms are also available. For example, we could have rewritten our fibonacci program as follows:

  #lang typed-scheme
  (: fib (Number -> Number))
  (define (fib n)
    (let ([base? (or (= 0 n) (= 1 n))])
      (if base?
          1
          (+ (fib (- n 1)) (fib (- n 2))))))

This program uses the let binding form, but no new type annotations are required. Typed Scheme infers the type of base?.

We can also define mutually-recursive functions:

  #lang typed-scheme
  (: my-odd? (Number -> Boolean))
  (define (my-odd? n)
    (if (= 0 n) #f
        (my-even? (- n 1))))
  
  (: my-even? (Number -> Boolean))
  (define (my-even? n)
    (if (= 0 n) #t
        (my-odd? (- n 1))))
  
  (my-even? 12)

As expected, this program prints #t.

1.3 Defining New Datatypes

If our program requires anything more than atomic data, we must define new datatypes. In Typed Scheme, structures can be defined, similarly to PLT Scheme structures. The following program defines a date structure and a function that formats a date as a string, using PLT Scheme’s built-in format function.

  #lang typed-scheme
  (define-struct: Date ([day : Number] [month : String] [year : Number]))
  
  (: format-date (Date -> String))
  (define (format-date d)
    (format "Today is day ~a of ~a in the year ~a"
            (Date-day d) (Date-month d) (Date-year d)))
  
  (format-date (make-Date 28 "November" 2006))

Here we see the built-in type String as well as a definition of the new user-defined type Date. To define Date, we provide all the information usually found in a define-struct, but added type annotations to the fields using the define-struct: form. Then we can use the functions that this declaration creates, just as we would have with define-struct.

1.4 Recursive Datatypes and Unions

Many data structures involve multiple variants. In Typed Scheme, we represent these using union types, written (U t1 t2 ...).

  #lang typed-scheme
  (define-type-alias Tree (U leaf node))
  (define-struct: leaf ([val : Number]))
  (define-struct: node ([left : Tree] [right : Tree]))
  
  (: tree-height (Tree -> Number))
  (define (tree-height t)
    (cond [(leaf? t) 1]
          [else (max (tree-height (node-left t))
                     (tree-height (node-right t)))]))
  
  (: tree-sum (Tree -> Number))
  (define (tree-sum t)
    (cond [(leaf? t) (leaf-val t)]
          [else (+ (tree-sum (node-left t))
                   (tree-sum (node-right t)))]))

In this module, we have defined two new datatypes: leaf and node. We’ve also defined the type alias Tree to be (U node leaf), which represents a binary tree of numbers. In essence, we are saying that the tree-height 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 node-left 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 Scheme values. Therefore, in the first branch of the cond clause in tree-sum, we know that t is a leaf, and therefore we can get its value with the leaf-val 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 node-left and node-right with t as input.

2 Polymorphism

Typed Scheme offers abstraction over types as well as values.

2.1 Polymorphic Data Structures

Virtually every Scheme program uses lists and sexpressions. Fortunately, Typed Scheme can handle these as well. A simple list processing program can be written like this:

  #lang typed-scheme
  (: sum-list ((Listof Number) -> Number))
  (define (sum-list l)
    (cond [(null? l) 0]
          [else (+ (car l) (sum-list (cdr l)))]))

This looks similar to our earlier programs – except for the type of l, which looks like a function application. In fact, it’s a use of the type constructor Listof, which takes another type as its input, here Number. We can use Listof to construct the type of any kind of list we might want.

We can define our own type constructors as well. For example, here is an analog of the Maybe type constructor from Haskell:

  #lang typed-scheme
  (define-struct: Nothing ())
  (define-struct: (a) Just ([v : a]))
  
  (define-type-alias (Maybe a) (U Nothing (Just a)))
  
  (: find (Number (Listof Number) -> (Maybe Number)))
  (define (find v l)
    (cond [(null? l) (make-Nothing)]
          [(= v (car l)) (make-Just v)]
          [else (find v (cdr l))]))

The first define-struct: defines Nothing to be a structure with no contents.

The second definition

  (define-struct: (a) Just ([v : a]))

creates a parameterized type, Just, which is a structure with one element, whose type is that of the type argument to Just. Here the type parameters (only one, a, in this case) are written before the type name, and can be referred to in the types of the fields.

The type alias definiton
  (define-type-alias (Maybe a) (U Nothing (Just a)))
creates a parameterized alias – Maybe is a potential container for whatever type is supplied.

The find function takes a number v and list, and produces (make-Just v) when the number is found in the list, and (make-Nothing) otherwise. Therefore, it produces a (Maybe Number), just as the annotation specified.

2.2 Polymorphic Functions

Sometimes functions over polymorphic data structures only concern themselves with the form of the structure. For example, one might write a function that takes the length of a list of numbers:

  #lang typed-scheme
  (: list-number-length ((Listof Number) -> Integer))
  (define (list-number-length l)
    (if (null? l)
        0
        (add1 (list-number-length (cdr l)))))

and also a function that takes the length of a list of strings:

  #lang typed-scheme
  (: list-string-length ((Listof String) -> Integer))
  (define (list-string-length l)
    (if (null? l)
        0
        (add1 (list-string-length (cdr l)))))

Notice that both of these functions have almost exactly the same definition; the only difference is the name of the function. This is because neither function uses the type of the elements in the definition.

We can abstract over the type of the element as follows:

  #lang typed-scheme
  (: list-length (All (A) ((Listof A) -> Integer)))
  (define (list-length l)
    (if (null? l)
        0
        (add1 (list-length (cdr l)))))

The new type constructor All takes a list of type variables and a body type. The type variables are allowed to appear free in the body of the All form.

3 Variable-Arity Functions: Programming with Rest Arguments

Typed Scheme can handle some uses of rest arguments.

3.1 Uniform Variable-Arity Functions

In Scheme, one can write a function that takes an arbitrary number of arguments as follows:

  #lang scheme
  (define (sum . xs)
    (if (null? xs)
        0
        (+ (car xs) (apply sum (cdr xs)))))
  
  (sum)
  (sum 1 2 3 4)
  (sum 1 3)

The arguments to the function that are in excess to the non-rest arguments are converted to a list which is assigned to the rest parameter. So the examples above evaluate to 0, 10, and 4.

We can define such functions in Typed Scheme as well:

  #lang typed-scheme
  (: sum (Number * -> Number))
  (define (sum . xs)
    (if (null? xs)
        0
        (+ (car xs) (apply sum (cdr xs)))))

This type can be assigned to the function when each element of the rest parameter is used at the same type.

3.2 Non-Uniform Variable-Arity Functions

However, the rest argument may be used as a heterogeneous list. Take this (simplified) definition of the Scheme function map:

  #lang scheme
  (define (map f as . bss)
    (if (or (null? as)
            (ormap null? bss))
        null
        (cons (apply f (car as) (map car bss))
              (apply map f (cdr as) (map cdr bss)))))
  
  (map add1 (list 1 2 3 4))
  (map cons (list 1 2 3) (list (list 4) (list 5) (list 6)))
  (map + (list 1 2 3) (list 2 3 4) (list 3 4 5) (list 4 5 6))

Here the different lists that make up the rest argument bss can be of different types, but the type of each list in bss corresponds to the type of the corresponding argument of f. We also know that, in order to avoid arity errors, the length of bss must be one less than the arity of f (as as corresponds to the first argument of f).

The example uses of map evaluate to (list 2 3 4 5), (list (list 1 4) (list 2 5) (list 3 6)), and (list 10 14 18).

In Typed Scheme, we can define map as follows:

  #lang typed-scheme
  (: map
     (All (C A B ...)
          ((A B ... B -> C) (Listof A) (Listof B) ... B
           ->
           (Listof C))))
  (define (map f as . bss)
    (if (or (null? as)
            (ormap null? bss))
        null
        (cons (apply f (car as) (map car bss))
              (apply map f (cdr as) (map cdr bss)))))

Note that the type variable B is followed by an ellipsis. This denotes that B is a dotted type variable which corresponds to a list of types, much as a rest argument corresponds to a list of values. When the type of map is instantiated at a list of types, then each type t which is bound by B (notated by the dotted pre-type t ... B) is expanded to a number of copies of t equal to the length of the sequence assigned to B. Then B in each copy is replaced with the corresponding type from the sequence.

So the type of (inst map Integer Boolean String Number) is

((Boolean String Number -> Integer) (Listof Boolean) (Listof String) (Listof Number) -> (Listof Integer)).