3

Often when translating between languages (whether with program translation or compiling) it's a one-way, destructive translation. The functionality of the "port" isn't lost, but some of the intent and expression is.

For instance, porting a program from Java to C is possible, but you lose the notion of classes and methods. If that program were to be ported back to Java you'd need to infer the intent of what would be considered an object to get back to the original state. And that's something that a computer isn't good at doing. As such you'd end up with a Java program that looked more like a C program with data structures and a big collection of static functions.

Also consider a compiler. Once a language has been compiled down to assembler or even to say CIL or JVM, concepts such as if statements are lost as they are turned into branches.

On a higher level, source to source translators exist, but some languages have features (i.e. delegates in C#) which don't exist in other languages (like Java not having delegates). The translation can happen, but is mildly destructive (i.e. Java would need a wrapper class to simulate delegates).

So, having said all that, is there a language who's goal is to be structurally compatible with all other languages by supporting all language features of all languages?

I'm not so concerned with being able to fully translate between languages as I am with being able to express each language's code in the common language. So the goal would be translating to the common language and back again to the source language without losing anything. Like: Java->Common->Java or LISP->Common->LISP

  • 2
    @gnat I don't see how this relates to Turing-completeness. I'm not interested in the translation process, just the one common language. – Sam Washburn May 16 '15 at 19:46
  • Okay, maybe you could create a common intermediate language for few of them most popular languages. But where do you stop? At Haskell? At all of its extensions? At APL? At INTRECAL? At Malbolge? And would you keep updating your common intermediate language forever, as new languages are developed (and old ones updated)? – svick May 16 '15 at 21:05
  • 3
    Also, you didn't answer the most important question: Why? What would be the point of such language existing, if the only thing it could do well would be to get back the code you already had? – svick May 16 '15 at 21:07
  • @svick I think at some point you'll hit a critical mass where all language features are supported. The common language doesn't have to support all language syntax, just features. Enough that you could trivially rebuild the syntax for the source language. – Sam Washburn May 16 '15 at 21:47
8

Once a language has been compiled down to assembler or even to say CIL or JVM, concepts such as if statements are lost as they are turned into branches.

Not particularly in practice. If you look at a tool like Reflector, it will happily turn CIL back into pretty accurate C# code, ifs and all.

So, having said all that, is there a language who's goal is to be structurally compatible with all other languages by supporting all language features of all languages?

No, because at that point we wouldn't need compilers or source translators since we have the One True Language. Sarcasm aside, this is effectively asking why we have different languages in the first place. Language features are not things that live in isolation. Providing one often hinders (or breaks) others. If there was a language that could provide the features of others without loss, why would we be using the others?

  • 6
    If you look at a tool like Reflector, it will happily turn CIL back into pretty accurate C# code, ifs and all. -- That's because CIL includes a substantial amount of metadata that helps reconstruct the original C#, including identifiers. This is not generally true of languages like C, whose compile process is lossy. – Robert Harvey May 16 '15 at 20:13
  • @RobertHarvey - sure, but even "substantial amount of metadata" isn't "100% of the information contained in the source language". – Telastyn May 16 '15 at 20:26
  • Which is why Reflector isn't 100% accurate at reproducing the original source code. – Robert Harvey May 16 '15 at 20:27
  • That's not quite the same question because there are some features we clearly don't want, like COMEFROM. – DeadMG May 16 '15 at 20:32
  • 1
    @DeadMG: That's a trivial case; most of the time, language design is far more involved than that, and deals not with misfeatures but with features that are not quite good enough, or that have some wart that has a particularly unfortunate reaction with some other minor feature in the language, or the like. Look up, say, Eric Lippert's writings for a good idea of all the tradeoffs. Or Jonathan Shapiro's, if you want a less mainstream approach. – Nathan Tuggy May 16 '15 at 21:34
4

Some intermediate languages have been successfully used as target for a lot of various languages, e.g. LLVM (or Ramsey & Jones' C--, which might be a dead project in 2015)

Notice that an intermediate language does not carry all the information provided in the source language (e.g. you are losing information when compiling from C to LLVM).

However, I am not sure that your wish makes sense. There are some programming language features which are not compositional, or are a whole program thing. Eg continuations (you can't implement call/cc without some support from your intermediate language), exceptions, or even garbage collection or parallelism or homoiconicity.

Notice that for your question programming language semantics matter much more than syntax.

BTW, some people might believe that x86-64 machine code or JavaScript is actually what you want....

  • IIRC C-- is targeted by Haskell – AK_ May 17 '15 at 22:42
  • AFAIK C-- was targeted by Haskell. – Basile Starynkevitch May 19 '15 at 19:54
2

The only way for a conversion from language X to some intermediate language IL and back to be lossless is for IL to be a superset of X. If you want to have multiple languages as X, then IL has to be a superset of all of them.

Therefore, I would suggest that IL has the following structure:

delimiter: ---------SOMERANDOMSTRING----------
---------SOMERANDOMSTRING----------
language: C
#include <stdio.h>
int main() { printf("hello world\n"); return 0; }
---------SOMERANDOMSTRING----------
language: Java
public class Main { public static void main(String[] args) {
  System.stdout.println("hello world");
}}
---------SOMERANDOMSTRING----------

In other words, MIME-like embedding of the actual source code into the language. The "compiler" then has to parse apart the pieces and pass them to a proper processor for that language.

This obviously isn't useful.

  • I mean lossless from a semantic standpoint. The syntax can vary so long as the original language can be reconstructed in a "pretty print" fashion. Do you still think this is the only solution? – Sam Washburn May 20 '15 at 5:09
  • @SamWashburn You'd have to have a language with every feature from every language, or at least features so powerful that they can represent every feature, along with annotations containing hint about what the original looked like. Such a language would be completely unusable for humans because of the learning curve involved, and the compiler would be incredibly massive. My solution is much easier to implement, even though it wasn't meant quite seriously. – Sebastian Redl May 20 '15 at 8:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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