racket / typed-racket

Typed Racket
Other
527 stars 104 forks source link

Cast doesn't work on types with type variables #380

Open AlexKnauth opened 8 years ago

AlexKnauth commented 8 years ago

In racket version 6.5.0.5, if I run this program:

#lang typed/racket
(: f : (All (X) (-> X X)))
(define (f x)
  (cast x Any)
  x)

I expect it to succeed and create an identity function, but instead I get this compile-time error

. . ../../Applications/Racket/2016-06-16/Racket v6.5.0.5/share/pkgs/typed-racket-lib/typed-racket/private/type-contract.rkt:337:4: 
type->static-contract: Recursive value lookup failed. #hash() X

This error comes from line 446 of type-contract.rkt, where there's a TODO: https://github.com/racket/typed-racket/blob/master/typed-racket-lib/typed-racket/private/type-contract.rkt#L446

Instead of raising this error on types with type variables, should this generate an any-wrap/c contract? That would be much better for backwards compatibility.

AlexKnauth commented 8 years ago

An easy workaround for these types of examples would be using (cast (ann x Any) ...). However, that's too restrictive to do in general because it disallows higher order uses of casted values. The contract does need to take advantage of the original type, but it should be able to fall back to any-wrap/c for things like type variables.

Is that the right thing to do?