Optional Static Typing

Ville Vainio ville at spammers.com
Mon Dec 27 21:49:28 CET 2004


>>>>> "Alex" == Alex Martelli <aleaxit at yahoo.com> writes:

    Alex> I've always liked the (theoretical) idea that assertions
    Alex> (including of course contracts) could be used as axioms used
    Alex> to optimize generated code, rather than (necessarily) as a
    Alex> runtime burden.  E.g. (and I don't know of any
    Alex> implementation of this concept, mind you), inferring (e.g.
    Alex> from such assertions) that x>=0, and that y<0, the compiler
    Alex> could simply cut away a branch of dead code guarded by "if
    Alex> x<y:" (maybe with a warning, in this case;-)...

I realize that your example is theoretical, but efficiency impact of
such optimizations would be dwarfed (speedwise) by optimization on
type, so that "if x < y" yields native object code for integer
comparison. Also, to get real benefits the functions called would need
to be inline-expanded so that the implications of x < y are taken in
account deeper in the call chain; a programmer that does

def f(x,y):
  assert x > y
  if x < y:
    blah(y,x)

needs a slap on the wrists anyway. Doing "if x < y" in blah() would
make sense, but then there would need to be a special version of
blah()...
  
I could think of one worthwhile example as well:

def foo(l):
  assert issorted(l)
  if "hi" in l:   # in does a bsearch because the list is sorted
    blah()

but things like this probably belong to languages like Lisp where the
user gets to expand and mess with the compiler.

-- 
Ville Vainio   http://tinyurl.com/2prnb



More information about the Python-list mailing list