[Types-sig] Sample declarations

Daniel Wang danwang@CS.Princeton.EDU
13 Mar 2001 14:40:03 -0500


Paul Prescod <paulp@activestate.com> writes:
{stuff deleted}
> > Someone suggested that one could infer a type by examining the "check"
> > method of an object. This actually is not as crazy as it sounds. If people
> > are willing to program in a small subset of Python for implementing checks,
> > these "behavioral" type checking isn't so crazy an idea. The subset would
> > have to be a terminating side-effect free subset of python.
> 
> I like the idea. I'll leave it to a PhD student to define that subset.

Acutally, if all you really care about is "earlier errors" for debugging
purposes, and if you can convince a PhD student to do some work for
you. There is a  "simple" and completely automatic way to get earlier errors.

given a method or function body
 def foo (....):
   random code...

automatically compute a "type assertion" function that basically simulates
the behavior of the random code with respect to type errors  
i.e.

  if (b) then 
    primop1    (* doesn't care what type x is *)
    primop2(x) (* may fail if x is not an int *)
  else 
    primop2(x) (* may fail if x is not an float *)
    foo(x)     (* call the function foo *)

get's converted to..

   if(debug) then
     if b then (checkInt(x))
     else checkFloat(x)
          checkFoo(x) (* call foo's check function *)
   (* original code *)

or to put it another way, do some simple program analysis and "hoist" type
errors to the earliest possible point in the computation. This can be
completely automatic, and in the worst case you leave your program alone if
it isn't smart enough to figure out how to hoist errors earlier then they
already occur. Also, if the assertions are really accurate, an optimizing
python compiler can use that fact to generate good code.

Also, this doesn't require any language extensions, just some fancy compiler
hacking and program analysis. Even something very dumb might do the
trick. The first hack, would be to hoist type assertions to the beginning of
every basic block. Then the next step would be to merge and hoist type
assertions based on control flow information. 

(This idea should work for Java and Scheme too... I'd be surprised if
someone hasn't tried this before.... but you never know... most people don't
think program analysis is easy....)