[racket] Typed Racket Quick Start example doesn't typecheck

From: Pekka Karjalainen (pekkakarj at gmail.com)
Date: Fri Aug 24 14:07:34 EDT 2012

I'm using Racket 5.3 that I downloaded via the usual route. I tried
experimenting with Typed Racket starting with the first example
program in the Quick Start section of the Typed Racket Guide, and I
got a type error right away. I then made sure I had exactly the same
code in the file as in the Guide and executed the following command. I
didn't expect the program to do anything, of course, since it only has
definitions. Instead of doing nothing, it was rejected by the type
checker.

Why does it give a type error? Is the Guide out of date with regard to
the current state of Typed Racket?

$ racket typed.rkt
typed.rkt:7:2: Type Checker: Expected Real, but got Number
  in: (sqrt (+ (sqr (- (pt-x p2) (pt-x p1))) (sqr (- (pt-y p2) (pt-y p1)))))
  context...:
[...]

That's the example with the point structure and distance function. It
looks to be the same in my local documentation and the current on-line
version, but there's a copy at the end of this message to make sure
you have the same code.

(I found out that it worked after I changed the type Real to Number in
the type signature for the distance function, by the way. I'm not sure
that's a good thing to do because Number is such a general type, but
it was something I tried.)

I haven't filed an issue at Bug Reports yet. I'd like to ask first if
the type checker is right and the docs inaccurate, or the opposite. Or
am I missing something obvious?

Pekka K.

#lang typed/racket
(struct: pt ([x : Real] [y : Real]))

(: distance (pt pt -> Real))
(define (distance p1 p2)
  (sqrt (+ (sqr (- (pt-x p2) (pt-x p1)))
           (sqr (- (pt-y p2) (pt-y p1))))))

Posted on the users mailing list.