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

From: Luke Whittlesey (luke.whittlesey at gmail.com)
Date: Thu Jan 22 11:39:10 EST 2015

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20150122/5c213b1b/attachment-0001.html>

Posted on the users mailing list.