21

Looking at most (if not all) dynamic languages (e.g. Python, PHP, Perl and Ruby), they are all interpreted. Correct me if I'm wrong. Is there any example of dynamic language that goes through compilation phase? Is dynamic language identical with interpreted language?

11
  • 4
    Define dynamic language, is it dynamically typed?
    – BenjaminB
    Jul 1, 2011 at 1:10
  • 3
    Objective-C exhibits many "dynamic" properties. Jul 1, 2011 at 2:29
  • 4
    @Job, one could do it with Lisp for decades. And it is both compiled and dynamically typed. So, there had never been an exact borderline between compilation and interpretation.
    – SK-logic
    Jul 1, 2011 at 6:28
  • 2
    @Darien You can compile that at run time as well and execute code afterwards. Strictly speaking, it's not interpretation.
    – mmx
    Jul 1, 2011 at 6:46
  • 4
    @Darien Nothing prevents a compiler from storing a symbol table information in the compiled binary and generating code to access that at run time. It's true that some languages lend themselves to interpretation more than compilation, but the whole point is it's possible to have a compiler for that language. Another important thing to note is that some people assume a compiler has to generate some sort of machine code. In practice, there are compilers that simply perform a source level transformation across two languages (or even the same language, like some Javascript minifiers).
    – mmx
    Jul 3, 2011 at 2:04

11 Answers 11

35

Looking at most (if not all) dynamic languages [i.e Python, PHP, Perl and Ruby], they are all interpreted.

Not true. You can compile Python source. That's one existential proof.

There are interpreters for statically-typed languages, and compilers for dynamically-typed languages. The two concepts are orthogonal.

Side note: In general, a language is just that: a language, with a set of syntactic constructs to express semantics. If you write Python on a whiteboard, it's still called Python! It's the implementation that can be an interpreter or a compiler. Being statically-typed or dynamically-typed (of kind of a hybrid of both) is a property of the language, while executing a program by interpreting or compilation is a property of the implementation.

15
  • 21
    To what precision must indents match on a whiteboard for the Python to be syntactically valid? ;) Jul 1, 2011 at 7:55
  • 1
    You can not compile Python. PYC accelerate only the load of a module. And py2exe simply embed the interpretor into the exe with the source file.
    – BenjaminB
    Jul 1, 2011 at 13:11
  • 8
    @Ubiquité: .pyc files are bytecode. Python source code was parsed, optimized and compiled to create them. The bytecode instructions are relatively high-level and the most popular implementation of it is a plain interpreter (for contrast, look at PyPy which JIT-compiles bytecode to very clever machine code at runtime) but Python isn't any less compiled than Java or C#. Python is only "not compiled" if "compilation" was restricted to native ahead-of-time compilation, but nobody said anything about that and generally it can refer to any lanugage-to-language transformation.
    – user7043
    Jul 1, 2011 at 13:26
  • 4
    @Ubiquité: Yes, that is correct, but that stands in no relation with your claim that "You can not compile Python" or whether it is possible to compile Python. First and foremost, you are mixing Pythonand CPython, whereas the latter is an implementation of the former, so is PyPy.
    – phant0m
    Jul 1, 2011 at 16:23
  • 2
    @ClemC ALL properties of a language are built into a compiler or interpreter, otherwise the interpreter or compiler is something for another language.
    – Pieter B
    Aug 29, 2017 at 12:47
15

Common Lisp is dynamically (and strongly) typed and usually compiled.

Since this dynamic-ness is achieved at runtime, there are some directives you can use in source code to assure the compiler that a symbol will hold only a certain kind of value, so that the compiler can optimize the generated code and boost performance.

14

C# 4.0 supports dynamic types (late-binding) and it is compiled.

4

node.js is based on Google's V8 javascript engine. V8 does runtime compilation. V8 is blindingly fast given that fact. Just check out http://shootout.alioth.debian.org and compare V8 vs. any of the above interpreted languages.

4

No - it's certainly possible to compile dynamic languages.

There are even some dynamic languages that are always compiled by design (e.g. Clojure).

The question however touches on an important related point: although dynamic languages can be compiled, it is often the case that dynamic langauges cannot be compiled down to code that is as efficient as a statically typed language. This is because there are some inherent features in dynamic languages that require runtime checks that would be unnecessary in a statically compiled langauge.

An example of this: languages that allow runtime patching of objects (e.g. Ruby) often require the object to be inspected (with a hashtable lookup or similar) whenever you invoke a method on the object. Even if this is compiled, the compiler will have to generate code to do the method lookup at runtime. To some extent this method lookup is not dissimilar to what an interpreter would have to do.

This adds a significant overhead when compared to a method call in a language like Java, where the correct method can be statically determined by the compiler from the class definition and reduced to a simple function call in native code.

I believe it is this effect more than anything else that results in dynamic languages having slower performance on average than their statically compiled counterparts. As you can see from the flawed benchmarks, it's the statically typed languages (C, Java, Fortran etc.) that tend to be fastest with the dynamic languages (Perl, Python, Ruby, PHP etc.) at the bottom of the ranking.

2

Once upon a time, BASIC was interpreted. And some variants of BASIC had dynamic typing. And you could get compilers for them as well.

(This was back in the days of 100K floppy drives, when dinosaurs still roamed the earth and ate unsuspecting s/w developers for breakfast.)

2
  • ...but only when they used GOTOs. (Which was, of course, quite common if they were developing in BASIC. AHA! That explains it!) Jul 1, 2011 at 2:07
  • BASIC at its design time was a compiled language. Jul 1, 2011 at 5:15
2

Different Smalltalk implementations handle this differently, but several of them compile to bytecodes that run on a high-performance VM.

2

In fact most of the so called "interpreted" languages go through / allow a just-in-time compilation to make it run faster. And some of them has to be compiled to byte code before you can run them.

In fact dynamic and interpreted are totally 2 different ideas, though there is an correlation. The reason being who ever feels the dynamic typing makes their job easier and faster, they wouldn't mind the code to be run a bit slower but portable.

1

Chrome, IE9 and Firefox 3.1+ all compile JavaScript to native binaries, and JavaScript is dynamically typed.

I think the reason that dynamic languages historically tend to be interpreted is because dynamic typing and interpreting (or more specifically, the lack of compiling) both tend to be features useful to scripting languages and scripting tasks in general.

Performance also isn't (wasn't) as much of a concern for the kinds of programs that were written in these languages, so again, the overhead of dynamic typing and interpreting wasn't as big an issue as it would be in languages that value performance.

1

Python is, typically, compiled. Admittedly compiled to byte code that is then interpreted.

Perl works in a similar fashion.

Common Lisp will, typically, compile to one of native or byte code. This differs between implementations (and, to some degree, within an implementation, depending on various optimization settings).

-5

Yes. All dynamic languages are interpreted language (but an interpreted language could be non-dynamic).

The reason is simple : if it is dynamic, it needs an interpreter to perform the dynamism at the level of the binary compilation.

ex. : when we put a data in a PHP variable, then later another one of a different type, our program could not compile into binary code since each type has its own binary representation format ; the interpreter manages the shifts at the binary level in a dynamic way

14
  • 2
    Wrong. Dynamic languages can be compiled (and sometimes very efficiently, e.g. using JIT and adaptative compilation techniques) Aug 29, 2017 at 11:53
  • "Roughly, JIT compilation combines the speed of compiled code with the flexibility of interpretation, with the overhead of an interpreter..." en.wikipedia.org/wiki/Just-in-time_compilation your program does not compile : it is compiled by the interpreter for you
    – ClearMind
    Aug 29, 2017 at 11:56
  • Read papers related to SELF Aug 29, 2017 at 12:06
  • Sure. Your link mentions : "One feature of Self is that it is based on the same sort of virtual machine system that earlier Smalltalk systems used. That is, programs are not stand-alone entities as they are in languages such as C, but need their entire memory environment in order to run." not stand-alone = not binary compiled the virtual machine is needed to perform the binary compilation
    – ClearMind
    Aug 29, 2017 at 12:19
  • 1
    Your definition of compiler is too restrictive. Not every compiler produces a binary executable file. For a recent counterexample, study the implementation of SBCL. Read the latest Dragon Book and Lisp In Small Pieces Aug 29, 2017 at 12:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.