[racket] Typed Racket and in-vector; Failure to Type Check

From: Daniel King (daniel.zidan.king at gmail.com)
Date: Tue Nov 5 20:22:53 EST 2013

Hi everyone,

I'm implementing an operation in Typed Racket to find the maximum element of a
vector (if this already exists and I missed it, I'd love a pointer to
it!). Unfortunately, it seems that my attempt to save a couple CPU cycles
results in a bunch of nasty type errors.

In the code below, vector-max type checks fine but vector-max2 gives me the
nasty type errors below. Can anyone shed some light on what I'm doing
incorrectly?

Thanks!

  . Type Checker: untyped identifier normalise-inputs imported from module
  <for.rkt> in: (define (vector-max2 vec) (for/fold: : Real ((biggest ((inst
  vector-ref Real) vec 0))) ((element (in-vector vec 1))) (max biggest
  element)))

  . Type Checker: Expected VectorTop, but got Any in: (define (vector-max2 vec)
  (for/fold: : Real ((biggest ((inst vector-ref Real) vec 0))) ((element
  (in-vector vec 1))) (max biggest element)))

  . Type Checker: Expression should produce 4 values, but produces 1 values of
  types Nothing in: (define (vector-max2 vec) (for/fold: : Real ((biggest ((inst
  vector-ref Real) vec 0))) ((element (in-vector vec 1))) (max biggest
  element)))

  . Type Checker: Expected Integer, but got Any in: (for/fold: : Real ((biggest
  ((inst vector-ref Real) vec 0))) ((element (in-vector vec 1))) (max biggest
  element))

  . Type Checker: Expected Integer, but got Any in: (for/fold: : Real ((biggest
  ((inst vector-ref Real) vec 0))) ((element (in-vector vec 1))) (max biggest
  element))

  . Type Checker: Expected Integer, but got Any in: (for/fold: : Real ((biggest
  ((inst vector-ref Real) vec 0))) ((element (in-vector vec 1))) (max biggest
  element))

The code in question:

  #lang typed/racket

  (: vector-max ((Vectorof Real) -> Real))
  (define (vector-max vec)
    (for/fold: : Real ([biggest ((inst vector-ref Real) vec 0)])
                      ([element vec])
      (max biggest element)))


  (: vector-max2 ((Vectorof Real) -> Real))
  (define (vector-max2 vec)
    (for/fold: : Real ([biggest ((inst vector-ref Real) vec 0)])
                      ([element (in-vector vec 1)])
      (max biggest element)))

--
Dan King

Posted on the users mailing list.