That is brilliant. And basically exactly what I am looking for. Thank you John!<div> -Patrick<br><br><div class="gmail_quote">On Tue, Jan 3, 2012 at 7:52 PM, John Clements <span dir="ltr"><<a href="mailto:clements@brinckerhoff.org">clements@brinckerhoff.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im"><br>
On Jan 2, 2012, at 6:40 PM, Patrick Li wrote:<br>
<br>
> Hello everyone,<br>
><br>
> I am trying to accomplish the following task, and was wondering if anyone knows of a fitting paper that I can read. I want to write a static analysis tool for a small scheme-like language to catch simple typing errors. The following is a simple example of the type of errors that I would like to detect:<br>
><br>
> 1) let b = (or (pair? x) (string? x)) ;; compute whether x is a pair or a string<br>
> 2) assert b ;; early exit the program if b is false.<br>
> 3) result = x * 2 ;; <--- This is a type error that can be found using static analysis.<br>
><br>
> In words, the pseudocode fragment says:<br>
> 1) Let b be a boolean, it indicates whether x is a pair or string.<br>
> 2) Assert that b must be true.<br>
> 3) Store x times 2 into result. This must be a type error because, for program execution to reach this point, x must either be a pair or a string, otherwise the assertion would have failed. Therefore, since x is either a pair or a string, it is definitely not an integer.<br>
<br>
</div>It looks to me like this is exactly the problem that "occurrence typing" solves, and that typed racket implements. To wit:<br>
<br>
#lang typed/racket<br>
<br>
(: my-fun (Any -> Any))<br>
(define (my-fun x)<br>
(cond [(not (or (string? x) (pair? x)))<br>
(error 'my-fun "oh dear, I wanted a string or a pair")]<br>
[else (* x 2)]))<br>
<br>
=><br>
<br>
Type Checker: Expected Complex, but got (U String (Pairof Any Any)) in: x<br>
<font color="#888888"><br>
<br>
John<br>
<br>
</font></blockquote></div><br></div>