[racket] Weird typed racket error

From: Neil Toronto (neil.toronto at gmail.com)
Date: Sat Apr 19 22:44:48 EDT 2014

That is weird. It works in the nightly build without annotating anything 
but `sphere3d`, and likely in the next release as well. Keyword 
arguments haven't been all that well-supported in the past, so I'd try 
making the arguments to sphere3d positional instead.

FWIW, the easiest way to destructure a vector is

     (match-define (vector ctr.x ctr.y ctr.z) ctr.posn)

Also FWIW, in Typed Racket, vectors are annoying function arguments 
because their types are invariant; i.e. because vectors are mutable, a 
(Vectorof Integer) isn't also a (Vectorof Real). To use the `sphere3d` 
function, I had to explicitly instantiate the #:posn argument's type:

     (plot3d (sphere3d #:posn ((inst vector Real) 0 0 0) #:r 2))

When it was (vector 0 0 0), TR made it a (Vectorof Integer) and raised a 
type error.

It would be great if we had types and functions for operating on 
immutable vectors. Until then, it's best to use lists or your own 
immutable structures.

Neil ⊥

On 04/19/2014 07:05 PM, Alexander D. Knauth wrote:
>
> Here’s the program:
> #lang typed/racket
>
> (require plot/typed plot/typed/utils)
>
> (: vector-x ((Vectorof Real) -> Real))
> (: vector-y ((Vectorof Real) -> Real))
> (: vector-z ((Vectorof Real) -> Real))
> (define (vector-x v) (vector-ref v 0))
> (define (vector-y v) (vector-ref v 1))
> (define (vector-z v) (vector-ref v 2))
>
> (: sphere3d (#:posn (Vectorof Real) #:r Nonnegative-Real [#:color
> Plot-Color] -> renderer3d))
> (define (sphere3d #:posn #{ctr.posn : (Vectorof Real)} #:r #{r :
> Nonnegative-Real} #:color [#{color : Plot-Color} "black"])
>    (let: ([ctr.x : Real (vector-x ctr.posn)]
>           [ctr.y : Real (vector-y ctr.posn)]
>           [ctr.z : Real (vector-z ctr.posn)])
>      (isosurface3d (λ: ([x : Real] [y : Real] [z : Real])
>                      (let: ([posn : (Vectorof Real) (vector x y z)])
>                        (vmag^2 (v- posn ctr.posn))))
>                    (ann (sqr r) Real)
>                    (ann (- ctr.x r) (U Real False)) (ann (+ ctr.x r) (U
> Real False))
>                    (ann (- ctr.y r) (U Real False)) (ann (+ ctr.y r) (U
> Real False))
>                    (ann (- ctr.z r) (U Real False)) (ann (+ ctr.z r) (U
> Real False))
>                    #:line-style 'transparent
>                    #:color color)))
>
> And it returns these errors:
> . Type Checker: Expected (Vectorof Real), but got Plot-Color in: (define
> (sphere3d …) …)
> . Type Checker: Expected Nonnegative-Real, but got True in: (define
> (sphere3d …) …)
> . Type Checker: Expected Plot-Color, but got (Vectorof Real) in: (define
> (sphere3d …) …)
> . Type Checker: Expected (Vectorof Real), but got False in: (define
> (sphere3d …) …)
> . Type Checker: Expected Nonnegative-Real, but got False in: (define
> (sphere3d …) …)
> . Type Checker: Expected Plot-Color, but got (Vectorof Real) in: (define
> (sphere3d …) …)
>
> Why am I getting these?  Is it somehow mixing up the arguments?
>
>
>
>
> ____________________
>    Racket Users list:
>    http://lists.racket-lang.org/users
>


Posted on the users mailing list.