## User login## Navigation |
## Type inference for PythonThe subject of type inference for dynamically-checked languages came up in the Buried Treasure thread. A question was raised in that thread having to do with why static type inference in these languages is difficult. Since there's a nascent body of literature which addresses that question, here are a few links to articles and papers about type inference for Python. A nice overview can be found in Localized Type Inference of Atomic Types in Python, a Master's thesis by Brett Cannon. The whole thesis is relevant, but for an overview of the issues, see Chapter 3, "Challenges of Inferring Types in Python". Chapter 4 summarizes previous attempts involving static inference in Python, including Psyco (previously on LtU) and Starkiller. The limitations of these attempts are briefly addressed. Type inference solutions for Python invariably involve restrictions to make the problem tractable. The above paper focuses on "inferring atomic types in the local namespace". Another approach is described in Aggressive Type Inference, by John Aycock. Aycock makes an important observation:
The article offers a type inference approach which exploits this observation. (If the meaning of the above quote isn't clear, I recommend reviewing our mammoth three-part thread on the subject, "Why type systems are interesting", part I, part II, and part III.) The PyPy implementation of Python in Python (previously on LtU) uses a restricted subset of Python, called RPython, to implement parts of the language. RPython is sufficiently static to be able to support full-program type inference. It is not a "soft" inference approach, and is not designed to be used with ordinary Python programs. The paper Compiling dynamic language implementations covers the approach used for static analysis of RPython. The PyPy Coding Guide, starting at section 1.4 may also be useful. (It may be interesting to note that the PyPy approach is very similar to that used previously for Scheme 48. The core of Scheme 48 is implemented in PreScheme, a subset of Scheme that supports full-program type inference.) Finally, Guido van Rossum has a number of blog entries on the subject of adding optional static typing to Python: - Adding Optional Static Typing to Python
- Adding Optional Static Typing to Python -- Part II
- Optional Static Typing -- Stop the Flames!
- Python Optional Typechecking Redux
If anyone knows of any other good treatments of type inference in Python or similar languages, please post links here. |
## Browse archives## Active forum topics |

## Recent comments

1 hour 6 min ago

3 hours 53 min ago

5 hours 11 min ago

7 hours 4 min ago

7 hours 11 min ago

7 hours 24 min ago

8 hours 4 min ago

9 hours 21 min ago

12 hours 9 min ago

12 hours 47 min ago