[racket] occurrence typing for case?

From: Alexander D. Knauth (alexander at knauth.org)
Date: Sun May 11 20:54:47 EDT 2014

On May 11, 2014, at 7:41 PM, Eric Dobson <eric.n.dobson at gmail.com> wrote:

> The issue is that there is a temporary variable that has the same
> value as x and TR cannot see that they point to the same location.
> Your implementation of case/type has the same problem. I thought there
> was a bug filed for it but couldn't find it so just filed one.
> http://bugs.racket-lang.org/query/?cmd=view&pr=14502

> Use this:
> #lang typed/racket
> (require typed/rackunit)
> 
> (define-syntax-rule
>  (case/type y x-expr
>    [t1 body ...] ...)
>  (let ([y x-expr])
>    (cond [((make-predicate t1) y) body ...] ...))
>  )
This worked:
#lang typed/racket
(require typed/rackunit)
(require syntax/parse/define)

(define-simple-macro
  (case/type x:id
    [t:expr body:expr ...+] ...)
  (cond [((make-predicate t) x) body ...] ...)
  )

(define (my-length x)
  (case/type x
    [String (string-length x)]
    [(Listof Any) ((inst length Any) x)]
    ;[VectorTop (vector-length x)]
    ))
(check-equal? (my-length "12345") 5)
(check-equal? (my-length '(1 2 3 4 5)) 5)
;(check-equal? (my-length #(1 2 3 4 5)) 5)

Would something like that work for the typed version of case?  

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140511/0b927f1c/attachment.html>

Posted on the users mailing list.