r/Clojure • • Apr 21 '20

ClojureRS - Clojure, implemented atop Rust

https://github.com/Tko1/ClojureRS
109 Upvotes

29 comments sorted by

9

u/slifin Apr 21 '20

Seems like a cool idea

5

u/lughaidhdev Apr 21 '20

I was thinking of doing something similar recently, what references do you use to implement Clojure if you don't mind?

Do you start from specs, from docs in Clojure?

5

u/---cameron Apr 22 '20 edited Apr 22 '20

That's strange, I never saw this comment.

It depends on what you mean; if we're talking about, say, implementing a persistent list, then I just implement a persistent list. If we're talking about the exact finer details on something (ie, questions like 'what happens if you intern a symbol without a namespace, versus one with a namespace' or, to use an example I talked about recently, 'how exactly is concat defined' [which involves lazy sequences and a lot more nuance than just 'put these collections together']), I consult the Clojure source for every part, always. If we're talking overall architecture, I consult the Clojure source for every part, and then sometimes make decisions based from what abstractions are more natural to me, and what seem more natural to rust (meaning, for instance, I'm not going to remove the idea of a namespace -- that's central to Clojure. But I did add a struct representing an 'environment', which is how eval gets access to, say, closures and namespaces and just generally figuring out what a value means 'right now'. This may one day merge into some version of Clojure's RT class).

Speaking of the latter, another example of a difference (but this time having to do with the fact I'm using Rust and not Java) is in Java, an IFn -- ie, to my knowledge, something function-like that you can invoke like a function (a function, a map, a keyword; something where you can do

({:name "Name"} :name) => "Name"

(:name {:name "Name"}) => "Name"

(+ 1 2) => 3;

-- this generic invoke function, defined in the IFn interface, is defined as

public Object invoke(Object arg1 ... )

And you can implement dynamic dispatch this way, dispatching on type eventually by doing something like this at runtime

public Object invoke(Object a, Object b) {`
 if(a instanceof Integer && b instanceof Integer) {` 
    ... use function definition defined for integers `
 }
}

But with Rust, my closest equivalent seems to be dealing with trait objects, pointers afaik to a vtable and some data. I ran into major issues implementing dynamic typing that way, and so I opted for a Value adt (enum in Rust) representing any potential Value -- something like

enum Value { 
  I32(i32),
  String(..),
  ...
}

This appears to not be unusual; Ketos, another more-scheme-like lisp implemented in Rust appears to implement generic values this way too (going as far as to also name them values). I do still use some trait objects (such as for the body of a function / macro), and the book isn't shut on this for me-- and it will especially be reopened when I start digging into performance and how this specific decision works under the hood (in a very up close way), but regardless, there's another example of an architectural change, in this case due to the language.

2

u/lughaidhdev Apr 22 '20

Wow very interesting, thank you very much!

Yes my question was more like overall architecture and having a references for how Clojure should behave in specific scenario. Because reading the source seems like not the most optimal way, if there is a bug in the source you are gonna take that bug with you in ClojureRS (if you implement it exactly the same way). And working from specs is also nice in the way that you might do Rust code that is more idiomatic than if you read Clojure sources I guess.

I'm super new to Rust so I'm a bit lost with the Trait vs Value point you make but that's a good starting point to know more about them, thank you.

Another question, do you plan (or is it already possible) to do interop with Rust crates in a very easy way? And what about embedding a ClojureRS lib into another program written in another langage like Rust is able to do?

3

u/---cameron Apr 22 '20

Because reading the source seems like not the most optimal way, if there is a bug in the source you are gonna take that bug with you in ClojureRS

Hehe well for starters I would be ok taking a bug with me -- I don't consider Clojure particularly buggy, and simply acting as Clojure does (for better or worse) is a good enough standard for me. I think its a viable strategy to be reactive, in place of proactive, in this case -- there's a lot of eyes already on the Clojure source code I can rely on.

But no, I'm not talking about some line per line, indiscriminate copying anyways. While I generally drop into the code on on a line by line basis to see what's happening, the ideas I'm trying to glean from it are much higher level than that -- except in the Clojure-written-in-Clojure source, where my intent often is exactly to be able to copy and paste in the original right there, line per line.

I have more to write on this but I have to go for about 12 hours, I will try to write as soon as I'm back!

2

u/arichiardi Apr 22 '20

If nothing else this could be an awesome stating point for a better universal-ctags Clojure parser instead of:

https://github.com/universal-ctags/ctags/blob/master/parsers/clojure.c

Keep it up ๐Ÿ‘๐Ÿ‘๐Ÿ‘

1

u/therealdivs1210 Apr 22 '20

Very interesting! It even has a REPL!

Is it interpreted? Is it compiling on the fly?

4

u/---cameron Apr 22 '20

Its interpreted right now. I don't know what it would become; I'm looking at JIT compilation to native code, as well as just implementing a plain VM / bytecode, but I'd rather iterate on some other things and do more research before making a decision there

1

u/therealdivs1210 Apr 22 '20

All the best! Will check it out - might be a good way to get into Rust!

1

u/lost3d Apr 27 '20

Are you implementing the data structures yourself or using Bodil Stokke's implementations? https://docs.rs/im/14.3.0/im/ ... If you're not using them, are there any problems with them? I'm just learning Rust ATM, and was thinking of trying them out ...

1

u/FormerAct Jul 02 '20

Would it give a performance boost? Just to know...

1

u/---cameron Oct 08 '20

I don't know, but I certainly hope I could work that in; it something I really wanted to explore as well, and was one of the other main reasons for using something like Rust

1

u/buth3r Apr 21 '20

I need clojure in Go. Anyone? :)

7

u/SimonGray Apr 21 '20

-1

u/buth3r Apr 21 '20

Its not proper impl right? More like a toy project not production ready.

1

u/SimonGray Apr 21 '20

Haven't tried it, but I think it's definitely just a hobby project.

1

u/[deleted] Apr 22 '20

I have heard of it being used for automation at a really huge company, in an important place. So ready or not, I think that counts as production.

1

u/didibus Apr 22 '20

It's production ready for what it is, which is an interpreted Clojure written in Go without interop. I use it for scripting mostly.

1

u/buth3r Apr 22 '20

I found interop to be crucial for Clojure to succeed. Its hard to get into Clojurescript for me for that reason as JS ecosystem is wild and using JVM on lambdas is a no go. I'd be so happy for Golang impl with interop :)

3

u/didibus Apr 22 '20

Ya, Joker isn't going for the same use case. It tries to be a scripting language only, like a replacement for bash. It isn't trying to be a hosted language, just a normal language implemented in Go.

Currently, the idea with it is to Shell out when you need to interact more heavily with the OS. Kind of like what you would do with bash, powershell and all.

For that, it works pretty well, though I'd say that babashka is probably getting to be more feature-full at that use case then joker is.

Related to AWS Lambda, did you hear about provisioned concurrency? https://aws.amazon.com/blogs/compute/new-for-aws-lambda-predictable-start-up-times-with-provisioned-concurrency/

It can address cold start problems with Clojure, is worth it if your workloads are somewhat predictable.

P.S.: I also suspect that GraalVM native compilation will get better faster than a mature Clojure on Go port will happen. Once all quirks and limitations of GraalVM native are addressed, I don't think there'd be a point to a Go hosted Clojure really.

2

u/goldenfolding Apr 22 '20

It's almost an understatement to say the JS ecosystem is wild lol. I really like JS but the churn and breakage is unbelievable. Why do you say that makes it hard to get into ClojureScript though? That's exactly why I'm using it!

I just started a react native project in cljs and while the interop (not just between JS/CLJS, but also React/Reagent) was rough at first, once I got the hang of it I was really enjoying myself.

2

u/buth3r Apr 22 '20

I think its just my personal preference. I'm doing backend apps and I think I'm just scared to leave rock solid Go ecosystem for backend JS.

→ More replies (1)

2

u/arichiardi Apr 22 '20

It would probably work well inside a Custom AWS runtime. https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html

I collaborated to something similar but using lumo: https://github.com/grav/aws-lumo-cljs-runtime

It actually worked very well, check the video there, you can achieve a pretty slick workflow.

0

u/[deleted] Apr 21 '20

[deleted]

2

u/cbleslie Apr 21 '20 edited Sep 12 '25

continue rainstorm six narrow steer quaint instinctive oil soup punch

This post was mass deleted and anonymized with Redact

1

u/didibus Apr 22 '20

I think Go wouldn't be able to support Clojure fully. I think it end up looking very similar to the subset of Clojure that GraalVM supports, and maybe even less.

From what I know, Go is quite static, and has very limited reflection capabilities and reification. But I might be wrong.

-1

u/CryptoNaiah Apr 22 '20

Loved to have this work, but without funding it will remain a novelty.

Sad because it would be awesome to have it maintained, and fully functional!

0

u/[deleted] Apr 21 '20

[deleted]

18

u/pl0nk Apr 21 '20

GC probably the single biggest challenge. Persistent data structures like clojureโ€™s rely heavily on them, and the JVM has seen enormous attention and tuning in this regard.

0

u/[deleted] Apr 21 '20

[deleted]

22

u/---cameron Apr 21 '20 edited Apr 21 '20

Oh hey guys, author here. Excited to see my project posted somewhere already, I was waiting a bit before posting it.

Right now I indeed plan to continue writing the majority of Clojure here in Clojure itself. I am only resorting to rust hardcoding at the moment for, say, primitives and for what is usually accounted for with interop