Some recent discussions around the 'net have been tossing around the ideas about static typing in python, briding static and dynamic typing in C++-like languages, and similar concepts of making static-typing more dynamic or dynamic languages more optimized in static-typing ways. Particularly, I was sparked by Michael Feather's "Set of Tests" article. There are different ways we might look into bringing those concepts to Python, and I rolled a few of them around in my head. My final mental landing was "Can we utilize the assert statement to inform the compiler about these tests that are absolute?". Of course, you probably can see how this is a lot like what assert does now, with the only difference being between run-time and compile-time being the target of the rules. This leads us to looking for where an assert could be compile-time verified and then used to optimize code. The most basic compile-time assert I can think of us "assert builtin is builtin", which would be a contract that the name 'builtin' will continue to be bound to the default builtin object, and won't be changed. This means we can do "assert isinstance is isinstance" and the compiler can make assumptions it could not before: that when it sees the name isinstance, it knows exactly what it is before runtime. This opens up other expressions that use these known names and promise other things to the compiler. We could do things like "assert isinstance(l, sequence)" or "assert len(l)==3", which would create a pair of contracts that l was some kind of 3 element sequence, and the compiler could make it a tuple for optimization.
I am just so excited about this. CARDIAC. The Cardboard Computer. How cool is that? This piece of history is amazing and better than that: it is extremely accessible. This fantastic design was built in 1969 by David Hagelbarger at Bell Labs to explain what computers were to those who would otherwise have no exposure to them. Miraculously, the CARDIAC (CARDboard Interactive Aid to Computation) was able to actually function as a slow and rudimentary computer. One of the most fascinating aspects of this gem is that at the time of its publication the scope it was able to demonstrate was actually useful in explaining what a computer was. Could you imagine trying to explain computers today with anything close to the CARDIAC? It had 100 memory locations and only ten instructions. The memory held signed 3-digit numbers (-999 through 999) and instructions could be encoded such that the first digit was the instruction and the second two digits were the address of memory to operat...
Comments