[racket] typed/racket, promises, and type dispatch

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Thu Jan 22 20:26:58 EST 2015


TR is a library-language of R with types and type checking hooked in after macro-expansion is done (the part of the compiler that is relevant). When type checking is done, types disappear so that the middle end of Racket can kick in as before. The macro system of Racket is unaware of types so it cannot dispatch in response to Racket. -- Matthias



On Jan 22, 2015, at 11:39 AM, Luke Whittlesey wrote:

> Hello,
> 
> I've been learning TR, but I seem to have gotten myself into a corner that I haven't been able to find a workaround for. I'm looking for some help on how to get TR to perform occurrence typing with promises. Basically I'm trying to dispatch based on type, which works without promises, but fails with promises.
> 
> For background, this code works::
> ---- begin working code ----
> 
> #lang typed/racket
> 
> (define-type Wire Symbol)
> (define Wire? (make-predicate Wire))
> (define-type WireVec (Listof Wire))
> (define WireVec? (make-predicate WireVec))
> 
> ;; dispatch based on type
> (: Not (case-> (Wire -> Wire)
>                (WireVec -> WireVec)))
> (define (Not wire*)
>   (if (Wire? wire*)
>       (Not-wire wire*)
>       (Not-wirevec wire*)))
> 
> (: Not-wire (Wire -> Wire))
> (define (Not-wire w)
>   (displayln "received wire")
>   w)
> 
> (: Not-wirevec (WireVec -> WireVec))
> (define (Not-wirevec w)
>   (displayln "received wirevec")
>   w)
> 
> ;; test
> (Not 'my-wire)
> (Not (list 'a 'b 'c))
> 
> ;;; printed results are
> ;received wire
> ;'my-wire
> ;received wirevec
> ;'(a b c)
> 
> ---- end working code ----
> 
> When I use the same code as above, but add promises to the basic types, I can no longer create a predicate, so I also can't do occurrence typing. Is there a workaround to be able to perform compile time dispatch based on promise types?
> 
> ;; define types with promises as well
> (define-type Wire (U (Promise Wire) Symbol))
> (define Wire? (make-predicate Wire))       ; <-- error
> (define-type WireVec (U (Promise WireVec) (Listof Wire)))
> (define WireVec? (make-predicate WireVec)) ; <-- error
> 
> I understand that a contract can't be generated for something like (Promise Wire), because at runtime a promise can't identify it's payload without forcing it, but it seems like the (Promise Wire) type is available at compile time, so there might be a way to make a compile time dispatch.
> 
> Thanks,
> Luke
> 
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users



Posted on the users mailing list.