3

What I mean is, is there a language or could one be designed, such that all high level programming languages could be compiled into this intermediate language?

This is excluding machine languages.

siyu
  • 147
  • 5
KthProg
  • 2,050
  • 1
  • 24
  • 32

1 Answers1

4

Every general-purpose language that is Turing-complete is a universal programming language.

Two languages (or machines) are considered to be Turing-equivalent if any program for one can be compiled into a program for the other. A language is Turing-complete if it is Turing-equivalent to a Turing machine.

There were several early efforts to formalize the notion of a computation; a Turing machine was one, the lambda calculus another, and the class of general recursive functions a third. Alonzo Church and Alan Turing proved that all three of these formalizations were Turing-equivalent; any program for a Turing machine could be compiled to the lambda calculus, and vice versa, as could any general recursive function be implemented by either the lambda calculus or a Turing machine, and again vice versa.

The Church-Turing thesis hypothesizes that any computation that can be expressed in any formal system can be converted into a program that can run on a Turing machine; or equivalently, can be expressed in the untyped lambda calculus, or is general recursive, based on the equivalence described above.

It is merely a hypothesis and cannot be formally proven, as there is no way to formally characterize the class of computations that are subject to it (without circular reasoning by defining them as the class of computations that can be performed by a Turing machine), but there has never been any proposed model of computation that is not possible to compute with a Turing machine.

Because you can write a simulator of a Turing machine (or implementation of lambda calculus) in almost any general purpose language, and likewise those languages can be compiled to a program running on a Turing machine, pretty much all general purpose languages are Turing complete.

There are, however, some languages which are not Turing complete; regular expressions are an example. They can be simulated by a Turing machine, but they cannot in turn simulate a Turing machine.

Note that none of this addresses efficiency or access to host system resources; merely that the same computation can be expressed, and that it will eventually provide the same answer. There are some languages that are Turing complete in which there are some problems that cannot be computed at the same asymptotic efficiency as in other languages. And some languages provide access to external resources like the filesystem, I/O, networking, etc, while others which just allow computation in memory, but in any language that is Turing complete it would be possible to add an API or method of manipulating memory that allows it to access those external resources, so lack of access to system resources isn't a fundamental limitation, just a limitation of implementation.

As a more practical matter, there are several languages that have been designed to be portable, intermediate languages that are targets of compilation. The LLVM IR is one commonly used example, C-- is another. Also, any bytecode for a language runtime acts this way, the JVM is a compilation target for many languages, the CLR is another. Finally, many languages compile to C, as C compilers are widely available and the code is more portable than machine code.

And more recently, with the advent of the web and JavaScript being a language that is available in every web browser, JavaScript has become a popular target for compilation, both for languages that were designed to compile down to JavaScript like CoffeeScript and Dart, but also existing languages that were originally design to compile to machine code, via projects like Emscripten. Recognizing this usage, there has been effort to specify a subset of JavaScript, with more strict rules, known as asm.js, that makes a better target for compilation, while still allowing the same code to work backwards-compatibly with regular JavaScript engines that don't know anything about asm.js.

Brian Campbell
  • 322,767
  • 57
  • 360
  • 340
  • So you're saying all Turing-complete languages can be compiled into each other? – KthProg May 09 '15 at 14:46
  • @KthProg: Any Turing-equivalent system can simulate any other Turing-equivalent system. – Oliver Charlesworth May 09 '15 at 14:49
  • @OliverCharlesworth All of a sudden you know the answer, too. Even though you likely downvoted and tried to close this question as opinion-based. – KthProg May 09 '15 at 14:50
  • @KthProg: I already knew the answer. I didn't downvote, but I did vote to close (because it was unclear what distinction you were trying to draw with machine languages...) But given that you seem to be saying that this is an acceptable answer to your question, happy to count that as a clarified question, so will withdraw the close-vote! – Oliver Charlesworth May 09 '15 at 14:50
  • @OliverCharlesworth Okay thanks! Anyways, if one can simulate the other, then It seems like compiling is possible. – KthProg May 09 '15 at 14:51
  • @sepp2k Well, you wouldn't turn a sphere inside out, but that also is rigorously possible in mathematics lol. – KthProg May 09 '15 at 14:53
  • @KthProg I'm not sure how that addresses my question. I'd appreciate it if you could be more clear. – sepp2k May 09 '15 at 14:55
  • @KthProg I've expanded my answer to provide some more information, though you can also follow the links to Wikipedia to read more. – Brian Campbell May 09 '15 at 15:02
  • @KthProg Expanded my answer a little more to also include examples of languages that are frequently used as compilation targets; while any language could be used as such in theory, there are several that are designed for the purpose, and several more that are used as compilation targets due to their ubiquity (like C and JavaScript). – Brian Campbell May 09 '15 at 15:35