Why Is Dynamic Typing So Often Associated with Interpreted Languages?

Interesting question. BTW, I’m the author/maintainer of phc (compiler for PHP), and am doing my PhD on compilers for dynamic languages, so I hope I can offer some insights.

I think there is a mistaken assumption here. The authors of PHP, Perl, Python, Ruby, Lua, etc didn’t design “interpreted languages”, they designed dynamic languages, and implemented them using interpreters. They did this because interpreters are much much easier to write than compilers.

Java’s first implementation was interpreted, and it is a statically typed language. Interpreters do exist for static languages: Haskell and OCaml both have interpreters, and there used to be a popular interpreter for C, but that was a long time ago. They are popular because they allow a REPL, which can make development easier.

That said, there is an aversion to static typing in the dynamic language community, as you’d expect. They believe that the static type systems provided by C, C++ and Java are verbose, and not worth the effort. I think I agree with this to a certain extent. Programming in Python is far more fun than C++.

To address the points of others:

  • dlamblin says: “I never strongly felt that there was anything special about compilation vs interpretation that suggested dynamic over static typing.” Well, you’re very wrong there. Compilation of dynamic languages is very difficult. There is mostly the eval statement to consider, which is used extensively in Javascript and Ruby. phc compiles PHP ahead-of-time, but we still need a run-time interpreter to handle evals. eval also can’t be analysed statically in an optimizing compiler, though there is a cool technique if you don’t need soundness.

  • To damblin’s response to Andrew Hare: you could of course perform static analysis in an interpreter, and find errors before run-time, which is exactly what Haskell’s ghci does. I expect that the style of interpreter used in functional languages requires this. dlamblin is of course right to say that the analysis is not part of interpretation.

  • Andrew Hare’s answer is predicated on the questioners wrong assumption, and similarly has things the wrong way around. However, he raises an interesting question: “how hard is static analysis of dynamic languages?”. Very very hard. Basically, you’ll get a PhD for describing how it works, which is exactly what I’m doing. Also see the previous point.

  • The most correct answer so far is that of Ivo Wetzel. However, the points he describes can be handled at run-time in a compiler, and many compilers exist for Lisp and Scheme that have this type of dynamic binding. But, yes, its tricky.

Leave a Comment