[racket] typed/racket, promises, and type dispatch
I had thought TR would use types to prune unreachable paths. So something
like:
(: test-fn (Symbol -> Symbol))
(define (test-fn y)
(: only-symbol (case-> (Symbol -> Symbol)
(String -> String)))
(define (only-symbol x)
(if (symbol? x)
x
(error "can't ever get here!")))
(only-symbol y))
(test-fn 'a)
would get pruned/rewritten before being handed off to the middle end of
Racket. Now that I'm writing this I realize that type checking, rewriting,
and inlining would have to occur, so I'm not sure why I thought that was
the case. Thanks for setting me straight.
-Luke
On Thu, Jan 22, 2015 at 8:26 PM, Matthias Felleisen <matthias at ccs.neu.edu>
wrote:
>
>
> 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20150123/4a5fc7e8/attachment.html>