Stream: contributing

Topic: Embedding roc


view this post on Zulip Murariu Fabian (Oct 28 2023 at 11:56):

I've been looking for a small typed language that I could embed in rust, I'm aware gluon exists but I'd like it to be compiled and interop easily with rust. Could that be possible with roc? And if so where would I start looking to make it happen?

view this post on Zulip Brian Carroll (Oct 28 2023 at 12:54):

Yes! We have a "platforms and applications" model where Roc is the application and Rust can implement the platform. In fact it's the most common choice.
Have a look in the examples directory in the main Roc repo. And the basic-cli repo. Both under roc-lang on GitHub.

view this post on Zulip Richard Feldman (Oct 28 2023 at 13:18):

yeah roc is designed to be easy to call from other languages, and actually Rust is currently the best-supported one :smiley:

view this post on Zulip Richard Feldman (Oct 28 2023 at 13:19):

we don't yet have good documentation on how to do it, though - but happy to answer questions here and help you get started!

view this post on Zulip Murariu Fabian (Oct 28 2023 at 14:26):

I am a bit confused about the starting point, I'd like to start from rust and generate at runtime a roc program that I can then compile and execute. I'm aware roc has great interop and the notion of platform which fits perfectly with my needs. But I don't see how I could create a roc compiler at runtime from rust then get something like a function that I could call

view this post on Zulip Richard Feldman (Oct 28 2023 at 14:31):

can you say more about what you're building? I'd love to learn more about where the roc program is being generated from!

view this post on Zulip Murariu Fabian (Oct 28 2023 at 14:38):

I'd like to drive query execution from roc, where I'd convert something like Cypher or SQL into roc code. I'd like to avoid having to go as low as LLVM. Admittedly I don't know if there are any advantages vs just vectorised execution. I suppose with roc I'd go via a hybrid approach where roc would drive the underlying rust calls to vectorised functions

view this post on Zulip Richard Feldman (Oct 28 2023 at 15:28):

I'm not familiar with Cypher, but in the case of SQL - would the Rust program be converting SQL into Roc code at runtime and then executing the Roc code on a database?

view this post on Zulip Murariu Fabian (Oct 28 2023 at 15:39):

That's the idea

view this post on Zulip Richard Feldman (Oct 28 2023 at 15:53):

interesting! So what would be the advantage of having generated Roc running on the database instead of SQL? Is the idea that you'd mix handwritten Roc code with Roc code generated from SQL, and run both on the same database?

view this post on Zulip Murariu Fabian (Oct 28 2023 at 17:07):

I think you misunderstood, I'm building a database from scratch and there are 2 ways for query execution (that I care about) one is building an AST from the physical operators and just executing it by tree walking. The other way is compile the physical operators into code that represents what the query needs doing, like this or like this

view this post on Zulip Brendan Hansknecht (Oct 28 2023 at 17:36):

Interesting, this definitely could be done with roc, but I am not sure how smoothly it would work currently. Essentially, you would generate the roc code (It would always references the same platform primitives, which is your database). Then you would compile the code to a shared library and load it. Finally, you would just call into/run it.

view this post on Zulip Brendan Hansknecht (Oct 28 2023 at 17:37):

This could be done with the dev backend to avoid llvm, though not sure it would be faster than a tree walking interpretter.

view this post on Zulip Brendan Hansknecht (Oct 28 2023 at 17:37):

Otherwise, you could run llvm fully. That would get you perf, but compilation time may be too long in many cases.

view this post on Zulip Murariu Fabian (Oct 28 2023 at 17:53):

I'm looking at large datasets some queries could take minutes, the reason I'm looking at roc is because I don't want to build the entire thing from scratch just with LLVM

view this post on Zulip Brendan Hansknecht (Oct 28 2023 at 18:05):

Ah, if these queries are super long, then yeah, compilation time shouldn't matter much. Definitely seems doable.

view this post on Zulip Murariu Fabian (Oct 28 2023 at 18:24):

cool, where about would I look to get this going?

view this post on Zulip Murariu Fabian (Oct 28 2023 at 18:25):

as in the roc codebase

view this post on Zulip Brian Carroll (Oct 28 2023 at 18:41):

I think the only thing we have that generates Roc source code is the formatter. It uses the AST which you would need too.

view this post on Zulip Brian Carroll (Oct 28 2023 at 18:41):

I'll try to find some links for you

view this post on Zulip Brian Carroll (Oct 28 2023 at 18:42):

https://github.com/roc-lang/roc/blob/main/crates/cli/src/format.rs

view this post on Zulip Brian Carroll (Oct 28 2023 at 18:46):

https://github.com/roc-lang/roc/blob/main/crates/compiler/fmt/src/lib.rs

view this post on Zulip Brian Carroll (Oct 28 2023 at 18:48):

https://github.com/roc-lang/roc/blob/main/crates/compiler/parse/src/lib.rs

view this post on Zulip Brendan Hansknecht (Oct 28 2023 at 19:05):

Oh, I was thinking he would just textually generate roc source code in rust, but I guess it could also be done in a more structured way

view this post on Zulip Brendan Hansknecht (Oct 28 2023 at 19:06):

As for the other stuff, I think the closest we have to an example currently is in tests. In some tests we compile to a shared library and loading in rust to text the roc compiler is working.

view this post on Zulip Brendan Hansknecht (Oct 28 2023 at 19:07):

But fundamentally you would be compiling roc with --lib and directly using the shared library

view this post on Zulip Brendan Hansknecht (Oct 28 2023 at 19:20):

In general, I would advise first making sure you know how to use libloading and executing code from a shared library in rust.

view this post on Zulip Brendan Hansknecht (Oct 28 2023 at 19:20):

Then switch to using a roc shared library

view this post on Zulip Brendan Hansknecht (Oct 28 2023 at 19:20):

then add in the roc codegen part.

view this post on Zulip Murariu Fabian (Oct 28 2023 at 19:38):

thanks, I'll have a look at the AST, I was hoping to find some tests that use the AST, libloading is still a mystery to me


Last updated: Jul 06 2025 at 12:14 UTC