<html><head><meta http-equiv="Content-Type" content="text/html charset=windows-1252"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">It seems like case doesn’t do occurrence typing, when it seems like it would be easiest for case, because of singleton types. <div>For example:</div><div><div><font face="Courier New">#lang typed/racket</font></div><div><font face="Courier New"><br></font></div><div><font face="Courier New">(define (f x)</font></div><div><font face="Courier New"> (case x</font></div><div><font face="Courier New"> [(thing) (g x)]</font></div><div><font face="Courier New"> ))</font></div><div><font face="Courier New"><br></font></div><div><font face="Courier New">(: g ('thing -> 'thing))</font></div><div><font face="Courier New">(define (g x) x)</font></div></div><div><br></div><div>Gives me this:</div><div><div><font face="Courier New">. Type Checker: type mismatch</font></div><div><font face="Courier New"> expected: 'thing</font></div><div><font face="Courier New"> given: Any in: x</font></div></div><div><br></div><div>This isn’t much of a problem (I can easily just do a cast), but It just seems like it should be really easy for typed racket to figure out that x has the type ‘thing if it gets into that clause. </div><div><br></div><div>By the way is there something like case/type that would be something like this?:</div><div><font face="Courier New">(case/type x</font></div><div><font face="Courier New"> [Type-1 body …]</font></div><div><font face="Courier New"> [Type-2 body …]</font></div><div><font face="Courier New"> …</font></div><div><font face="Courier New"> )</font></div><div>And would use (make-predicate Type) to do it so that it would do occurrence typing? Maybe something like this:</div><div><font face="Courier New">(define-syntax-rule</font></div><div><font face="Courier New"> (case/type x-expr</font></div><div><font face="Courier New"> [t body …] …)</font></div><div><font face="Courier New"> (let ([x x-expr])</font></div><div><font face="Courier New"> (cond [((make-predicate t) x) body …] …))</font></div><div><font face="Courier New"> )</font></div><div><br></div><div>I tried it, but it didn’t work like I expected it to do with occurrence typing:</div><div><div><font face="Courier New">#lang typed/racket</font></div><div><font face="Courier New">(require typed/rackunit)</font></div><div><font face="Courier New"><br></font></div><div><font face="Courier New">(define-syntax-rule</font></div><div><font face="Courier New"> (case/type x-expr</font></div><div><font face="Courier New"> [t1 body ...] ...)</font></div><div><font face="Courier New"> (let ([x x-expr])</font></div><div><font face="Courier New"> (cond [((make-predicate t1) x) body ...] ...))</font></div><div><font face="Courier New"> )</font></div><div><font face="Courier New"><br></font></div><div><font face="Courier New">(define (my-length x)</font></div><div><font face="Courier New"> (case/type x</font></div><div><font face="Courier New"> [String (string-length x)]</font></div><div><font face="Courier New"> [(Listof Any) ((inst length Any) x)]</font></div><div><font face="Courier New"> [VectorTop (vector-length x)]</font></div><div><font face="Courier New"> ))</font></div><div><font face="Courier New">(check-equal? (my-length "12345") 5)</font></div><div><font face="Courier New">(check-equal? (my-length '(1 2 3 4 5)) 5)</font></div><div><font face="Courier New">(check-equal? (my-length #(1 2 3 4 5)) 5)</font></div></div><div><br></div><div>It gave me these errors:</div><div><div><font face="Courier New">. Type Checker: type mismatch</font></div><div><font face="Courier New"> expected: String</font></div><div><font face="Courier New"> given: Any in: x</font></div><div><font face="Courier New">. Type Checker: type mismatch</font></div><div><font face="Courier New"> expected: (Listof Any)</font></div><div><font face="Courier New"> given: Any in: x</font></div><div><font face="Courier New">. Type Checker: type mismatch</font></div><div><font face="Courier New"> expected: VectorTop</font></div><div><font face="Courier New"> given: Any in: x</font></div></div><div><br></div><div>So I guess it didn’t do occurrence typing for that either, even though it uses make-predicate. Is there anything I can do to help it do occurrence typing for this?</div><div><br></div><div>By the way why is length polymorphic? </div><div><br></div></body></html>