[plt-scheme] Baby steps in Typed Scheme

From: Todd O'Bryan (toddobryan at gmail.com)
Date: Thu Apr 9 17:50:12 EDT 2009

I'm trying to figure out how to implement the earliest parts of  HtDP
in Typed Scheme, just to understand it for myself.

So, we write something like

;; A list-of-number is:
;; empty, or
;; (cons number list-of-number)

So, I tried this parameterized version in Typed Scheme

#lang typed-scheme
(require scheme/list)

(define-type-alias (ListOf X) (U Empty (Cons X)))
(define-struct: Empty ())
(define-struct: (X) Cons ((first : X) (rest : (ListOf X))))

(: sum ((ListOf Number) -> Number))
(define (sum alon)
  (cond
    [(Empty? alon) 0]
    [(Cons? alon) (+ (Cons-first alon)
                     (sum (Cons-rest alon)))]))

(make-Cons 5 (make-Cons 3 (make-Cons 2 (make-Empty))))

I get an error on the cond saying that it expected a Number, but got a
Void. (I'm assuming it doesn't think I'm exhausting my type
possibilities.)

When I replace (Cons? alon) with else, it runs, but when I try to take
the sum of the example, it says

typecheck: Wrong function argument type, expected (U Empty (Cons
Number)), got (Cons Integer) for argument 1 in: (make-Cons 5
(make-Cons 3 (make-Cons 2 (make-Empty))))

Can anybody shed some light?
Todd


Posted on the users mailing list.