How is Julia written in Julia?

Hey all, I am broadly trying to understand how Julia as a language is written in Julia itself. Is the compiler what is enabling this or is there a different reason?

4 Likes

Julia is not written in Julia.
The Julia standard libraries, including Base, are implemented in Julia.

Julia is implemented mostly in C,
with a little bit of C++ for LLVM code-gen,
and FemtoLisp for the lowering and the parser.
And a little bit of Julia for the optimizer.

I assume the optimizer is compiled by Julia without optimization running. Or maybe it is compiled twice.

16 Likes

Note that it is possible for a language to be implemented entirely in itself: this called self-hosting (Wikipedia).

However, Julia is not self-hosting and there is not much interest in working on this. See e.g. this discussion, this discussion, and this article on why an emphasis on self-hosting can be detrimental to language design.

21 Likes

@stevengj

(Disclaimer: this is probably a very stupid question, but I have no background on compilers and things like this)

If Julia can create executables, then, theoretically, Julia can be written entirely in Julia, right? Since you said that Julia devs are not interested in writing Julia entirely in Julia, does this means that probably Julia will not be able to generate executables without relying on C/C++ (like we do today)?

1 Like

Yes. (Any programming language can implement any programming language, in theory. You could write a Cobol interpreter or compiler in PowerShell if you wanted to, in theory.)

Yes, Julia will need C and C++ compilers for the foreseeable future. (This poses no practical difficulties.)

8 Likes

Whilst there is a big bit of C, C++, and Scheme, a lot of Julia is written in Julia itself; and it’s not just the standard libs. My limited understanding is that “bootstrap” starts with the part of Julia the C/C++/Scheme provides, which is very limited, then bit by bit the more complicated parts of Julia are implemented. During this bootstrap the Core part of Julia is made, see: https://github.com/JuliaLang/julia/blob/master/base/boot.jl. Once Core is in place the bootstrap continues to build up Base here https://github.com/JuliaLang/julia/blob/master/base/Base.jl. That bootstrap process is, I think, why there is the odd duplication of functionality between Core and Base.

As you can probably tell, I’m a bit out of my depth here. So maybe someone can fill in some details and/or correct me.

Edit: one way to experience this bootstrap process is when contributing something to Base which is needed fairly early during bootstrap. Then you have to program the feature using only what is already available during that time in bootstrap, which can be tricky to do (if you’re not used to it).

8 Likes

Something I find really cool about Julia is the ability to see how things are implemented. When someone says “well, Julia’s just calling C libraries for all the math and stuff, right?”, I can do @edit sin(1.0) and show them that, no, that math function is written in Julia, here’s the polynomial used for the approximation and here’s what it does for different argument types. Or I can load the SpecialFunctions package and go down the rabbit hole for a Bessel function, and after a few clicks see that it isn’t written in Julia, but is in Fortran.

Oh, and the source buttons you find in the manual when looking at the doc for a function that take you straight to the source for that function. So cool!

11 Likes

though Julia is not a self-host language, in practice, especially in scientific research, a pitch I often give is: all the parts you would ever care about is in Julia and you can read, understand, and even tweak at will. I’m referring to the fact that base and stdlib are in Julia and packages are more often pure Julia (at least for the part that does heavy lifting).

Unlike, for example, Numpy, where one hits C code super quickly once start digging (even function as simple as mean is written in C).

6 Likes

I’ve definitely solved a few problems unrelated to Julia by looking at a method in stdlib to see what the most efficient solution to a linear algebra problem was.

8 Likes

I know people who “copied” the searchsorted algorithm from Julia and Numba JITed it for their own utility package, works like a charm. The reason is of course the implementation in Numpy it’s “not accessible” :wink:

1 Like