20

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?

Joshua Partogi
  • 3,845
  • 11
  • 34
  • 43
  • 4
    Define dynamic language, is it dynamically typed? – BenjaminB Jul 01 '11 at 01:10
  • 3
    Objective-C exhibits many "dynamic" properties. – Edward Strange Jul 01 '11 at 02:29
  • 1
    You can already generate and compile .Net code into IL at run-time. .Net 5 hints at creating a "compiler as a service". What is a difference between compiled and interpreted language at that point? – Job Jul 01 '11 at 03:27
  • Define interpreted. bytecode compilers, JITs, these aren't your father's interpreters anymore. – Javier Jul 01 '11 at 04:48
  • 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 01 '11 at 06:28
  • The term "dynamic" is a little tricky. You'll need an interpreter for things that are "eval-like", such as PHP's `$a = "test"; $b = "a"; echo($$b);` – Darien Jul 01 '11 at 06:41
  • 2
    @Darien You can compile that at run time as well and execute code afterwards. Strictly speaking, it's not interpretation. – mmx Jul 01 '11 at 06:46
  • @Mehrdad: No, you can't, because the value of `$b` might be from anywhere, even from runtime user input! You've got to be able to compare the arbitrary value of `$b` to the *original* source-code name of any variable that might be even remotely in scope. – Darien Jul 03 '11 at 01:06
  • 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 03 '11 at 02:04
  • Scheme is compiled. – Xwtek Dec 19 '19 at 13:18
  • @BenjaminB Clearly "dynamic" does not just mean "dynamically typed", at least not for some people, because on https://en.wikipedia.org/wiki/Dynamic_programming_language it says "Most dynamic languages are also dynamically typed, but not all are." It's hard for me to understand what exactly it's saying a "dynamic language" IS, though. – H. H. Aug 28 '22 at 04:30

11 Answers11

34

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.

mmx
  • 719
  • 6
  • 11
  • 20
    To what precision must indents match on a whiteboard for the Python to be syntactically valid? ;) – edA-qa mort-ora-y Jul 01 '11 at 07: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 01 '11 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. –  Jul 01 '11 at 13:26
  • from the documentation : A program doesn’t run any faster when it is read from a .pyc or .pyo file than when it is read from a .py file; the only thing that’s faster about .pyc or .pyo files is the speed with which they are loaded. – BenjaminB Jul 01 '11 at 14:31
  • 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 `Python`and `CPython`, whereas the latter is an implementation of the former, so is `PyPy`. – phant0m Jul 01 '11 at 16:23
  • Ok, I should have said "you can not compile Python with the standard implementation". – BenjaminB Jul 01 '11 at 16:27
  • @Ubiquité still not true. What is true is that with CPython you can not compile into a native binary. But still, .pyo files *are* compiled files. The fact they do not run any faster but only load faster is because the load time is mainly used to compile the source code into python bytecode. You just don't see that usually. – Federico klez Culloca Jul 01 '11 at 22:41
  • Ok, my bad ,I misunderstood. – BenjaminB Jul 01 '11 at 23:12
  • I don't understand your point: "*Being statically-typed or dynamically-typed (of kind of a hybrid of both) is a property **of the language***" when just before you point out yourself that: "*In general, a language is just that: a language, with a set of syntactic constructs to express semantics.*"... Wouldn't you mean instead that being statically-typed or dynamically-typed (of kind of a hybrid of both) is a property **of the compiler/interpreter**? – ClemC Aug 29 '17 at 11:55
  • 1
    @ClemC No, the type system is an integral part of the language. – Hulk Aug 29 '17 at 12:12
  • @Hulk I believe my misunderstanding comes from how I interpreted the word "property". What I meant is what is said [here](https://en.wikipedia.org/wiki/Type_system): "Type systems are often specified as part of programming languages, **and built into the interpreters and compilers for them;**". – ClemC Aug 29 '17 at 12:41
  • 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 '17 at 12:47
  • @ClemC If you have further questions on the topic, please ask a new question instead commenting on such an old answer. – Hulk Aug 29 '17 at 12:50
  • @PieterB If we consider that the *language* is the syntax + compiler/interpreter, OK. But isn't a language just, as said in the answer "*a language, with a set of syntactic constructs to express semantics*" ? So couldn't we have different implementation of this language (with different system types) depending of the interpreter? I'm not sure it's a good example, but look at SQL... In this case, I think the type system would be a compiler/interpreter property... – ClemC Aug 29 '17 at 12:58
  • @ClemC it's just like a natural language: you have the written down rules and you have what people actually speak to eachother. With programming languages it's no different, you can have written down rules, but what actually becomes the language is actually what the compiler expects and accepts. – Pieter B Aug 29 '17 at 13:24
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.

Federico klez Culloca
  • 3,092
  • 24
  • 24
13

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

Matt H
  • 2,133
  • 16
  • 22
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.

mikera
  • 20,617
  • 5
  • 75
  • 80
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.

LLeo
  • 41
  • 1
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.)

quickly_now
  • 14,822
  • 1
  • 35
  • 48
2

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

Randy Coulman
  • 246
  • 1
  • 3
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.

user658991
  • 161
  • 3
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.

Rei Miyasaka
  • 4,541
  • 1
  • 32
  • 36
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).

Vatine
  • 4,251
  • 21
  • 20
-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

  • 2
    Wrong. Dynamic languages can be compiled (and sometimes very efficiently, e.g. using JIT and adaptative compilation techniques) – Basile Starynkevitch Aug 29 '17 at 11:53
  • "Roughly, JIT compilation combines the speed of compiled code with the flexibility of interpretation, with the overhead of an interpreter..." https://en.wikipedia.org/wiki/Just-in-time_compilation your program does not compile : it is compiled by the interpreter for you – ClearMind Aug 29 '17 at 11:56
  • Read papers related to [SELF](https://en.wikipedia.org/wiki/Self_(programming_language)) – Basile Starynkevitch Aug 29 '17 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 '17 at 12:19
  • 1
    Your definition of [compiler](https://en.wikipedia.org/wiki/Compiler) is too restrictive. Not every compiler produces a binary executable file. For a recent counterexample, study the implementation of [SBCL](http://sbcl.org/). Read the latest [Dragon Book](https://en.wikipedia.org/wiki/Compilers:_Principles,_Techniques,_and_Tools#Second_edition) and [Lisp In Small Pieces](https://en.wikipedia.org/wiki/Lisp_in_Small_Pieces) – Basile Starynkevitch Aug 29 '17 at 12:21
  • by definition "compiled" in "compiled language" refers to compilation into binary code, not transformation into some intermediary code. If the language lead to the production of such intermediary code for a program, it will need an additional software to perform the binary compilation from this code : it is then an interpreted language. – ClearMind Aug 29 '17 at 12:29
  • But the compiled code might not be inside an executable file. It could be in memory only. SBCL is *not* an interpreter, for a recent example. And [libgccjit](https://gcc.gnu.org/onlinedocs/jit/) is a compiler *library* (producing GCC compiled code in memory). And 1960s era compilers worked without files! – Basile Starynkevitch Aug 29 '17 at 13:08
  • BTW, even a plain old C program compiled into an [ELF](https://en.wikipedia.org/wiki/Executable_and_Linkable_Format) executable by some [GCC](http://gcc.gnu.org/) compiler on a Linux machine needs in practice *additional* software to be run: the `libc.so`, the Linux kernel, the dynamic linker `ld-linux.so` – Basile Starynkevitch Aug 29 '17 at 13:19
  • a program written with a compiled language needs the OS and no more ; a program written with an interpreted language needs its specific interpreter in addition to the OS, even if it has been processed into some intermediary code....n'est-il pas? – ClearMind Aug 29 '17 at 13:29
  • A program written in C needs the C runtime library. A program written in Ocaml (a compiled language) needs the Ocaml runtime library (which contains a garbage collector). A program written in Go needs the Go runtime. And so on. And many complex programs need many other resources (e.g. my web browser needs fonts etc...). – Basile Starynkevitch Aug 29 '17 at 13:42
  • these are calls to additional resources, not performing the resources supplied by the program itself – ClearMind Aug 29 '17 at 13:58
  • @ClearMind If you're trying to say that "compiling" can't transform to some intermediary code... then you're stating that standard implementations of Java aren't compiled (since they compile to JVM bytecode, which explicitly isn't executable code, it has to go through the JVM) – Delioth Dec 12 '19 at 16:00
  • @ClearMind Chicken Scheme is clearly compiled, Even if you think Ruby and Python is interpreted, Chicken Scheme is compiled to C, which is compiled to native instructions. – Xwtek Dec 19 '19 at 13:22
  • “ [...] your program does not compile : it is compiled by the interpreter for you” makes no sense to me. It’s not relevant why or when the compilation happens. – Dave Newton Sep 19 '20 at 12:06