Translate Julia into another language

417 views
Skip to first unread message

francoi...@gmail.com

unread,
May 11, 2014, 10:43:14 AM5/11/14
to julia...@googlegroups.com
Hi Julia users,

As a newcomer, let me first introduce myself. I have some experience in numeric code, written mostly in Fortran 2008, C++, C#, Delphi and Mathematica. As a consultant, I have to write some numerical codes but those need to be written in languages that are different from clients to clients. Most of the time, the code can't be linked to any open source library for licensing (or management) reasons. Therefore, I need to rewrite some basic codes in a lot of different languages. Bug fixed in one language are a pain to fix in another one.

Therefore, it would be nice to have a unified language for prototyping. Julia seems to be an amazing candidate for different reasons :
- Execution speed in the same order as compiled languages
- I love the type system and the multiple dispatch idea
- The language is quite close to all the above languages when it comes to number crunching
- Its reflection capabilities

As Julia code can be manipulated by Julia, I am wondering if translating Julia code to any of those language could be mostly automated. With Mathematica, it is quite easy to generate code from mathematical expressions, which is an extremely powerful way of preprocessing symbolic expressions before the big number crunching.

I am thinking of a Julia function that takes as input :
- A Julia function that use only function whose concrete output types can be inferred from their concrete input types
- Some concrete types
- A language : Fortran, C, C#, Delphi
and output some code in that language.

Even if this process can't be completely automated, it there a way to make have a code "helping" at the translation ?

Best regards to all of you,
François

Mike Innes

unread,
May 11, 2014, 2:12:07 PM5/11/14
to julia...@googlegroups.com
This is certainly possible, but I imagine it's not going to happen except as an outside project.

Making a compiler like this isn't exactly easy, either – you'd probably have to do a lot of reaching into Julia's C internals to get to the type-inferred code.

You're welcome to try this, and I'm sure that you'd be able to find help on the mailing list for the trickier parts, but it might be best to wait until we have static compilation and link against Julia code as a library – you can avoid the run time that way.

– Mike

Stefan Karpinski

unread,
May 11, 2014, 3:37:15 PM5/11/14
to julia...@googlegroups.com
Since Julia uses LLVM and LLVM had an (unmaintained) C backend, it would be possible to generate C code form Julia that way. You could do something similar for some other languages as well but you'd have to build the backend yourself. Not an easy undertaking. It would also produce largely unintelligible code. If you can call Julia code as a dynamic library, that might work. On the whole I think that just sucking it up and using the different languages clients require is on the whole the least trouble.

Toivo Henningsson

unread,
May 11, 2014, 4:02:34 PM5/11/14
to julia...@googlegroups.com
It's an interesting idea. There would surely have to be some work to expose type inference and the like before it would any idea to try. I imagine that generating code to a language that can easily call into C would be a lot easier, since it's hard to avoid relying on a at least a few functions that rely on ccalls. If the starting point would be the surface syntax AST then I think that the result could be quite intelligible.
Getting somewhere might not be that hard, but it's very hard to know until you have tried it. It's certainly a high-risk project :)
There will be limitations of course. For starters, any call to eval that isn't executed as initialization before translating should be out of the picture.

francoi...@gmail.com

unread,
May 11, 2014, 4:17:16 PM5/11/14
to julia...@googlegroups.com
> On the whole I think that just sucking it up and using the different languages clients require is on the whole the least trouble

Unfortunately, that's less time spending with Julia :-(. Still, it will be an amazing tool for prototyping, and it might be worth it to translate the code manually afterwards, which is often quite easy for number crunching.

You guys do an amazing job. I'll try to spend more time with it, and I hope I will find some time to contribute.

François

Stefan Karpinski

unread,
May 11, 2014, 4:57:34 PM5/11/14
to julia...@googlegroups.com
I appreciate the sentiment :-). Somewhat unfortunately for your use case, I've found that while it's easy to translate, e.g. Matlab to Julia, but translating idiomatic Julia to Matlab is kind of impossible. It's the rampant use of custom types and multiple dispatch with lots of small methods that kills you – it just doesn't translate well at all. The easiest target language coming from Julia is probably Mathematica – term rewriting can often simulate multiple dispatch pretty well.

Jeff Bezanson

unread,
May 11, 2014, 5:48:09 PM5/11/14
to julia...@googlegroups.com

Thank you Francois.
I will point out that "translation" among languages can require subtle decisions. For example, we emit clever instruction sequences for things like comparison and rounding. It's debatable whether these should be reproduced exactly in c++, or mapped to idiomatic constructs that may be faster and/or unsafe.

Reply all
Reply to author
Forward
0 new messages