r/Clojure • • Aug 08 '17

Ferret is a restricted subset of Clojure that compiles to ISO C++11

http://ferret-lang.org
71 Upvotes

30 comments sorted by

4

u/madmax9186 Aug 08 '17

This project doesn't seem to have a run-time eval function...

Granted, this would be hard to hack in because then you need a compiler at runtime. But this functionality is key to being a Lisp.

Other cool embedded Lisp projects include uLisp, and this actually supports eval.

1

u/halgari Aug 08 '17

Agreed, calling this a Clojure or even a Lisp is a bit of a stretch.

4

u/yogthos Aug 08 '17

In practical terms, people get a language with Clojure style syntax that they can embed in their C++ projects. Whether it qualifies as a Lisp is purely of academic interest here. Also, as /u/Venthorn points out, ClojureScript didn't support macros originally either, but it was still very useful.

3

u/halgari Aug 09 '17

It's a lisp, but what is Clojure without immutable hashmaps, vectors, etc? It may look like clojure, but aside from the syntax I don't see the similarities. And that doesn't even get into the question of where the REPL is, even CLJS had that at the start (as well as JVM macros, half of CLJS is macros after all).

2

u/yogthos Aug 09 '17

Again, considering the environment this targets, it seems quite reasonable to me. I certainly would prefer using Clojure style syntax to express business logic to C++. Given that the target appears to be low resource environments, such as embedded systems, stuff like immutable data structures might not even be desirable as the default.

That said, I don't see anything fundamentally preventing adding things like immutable data structures, the REPL, and macros if Ferret actually started getting some use. As a proof of concept it's quite interesting and has clear use cases.

I've been using CLJS from the start. Getting the REPL to work has always been an adventure, and it's still an arcane experience I might add. Meanwhile, eval wasn't even supported until recently, and now only works with unminified CLJS limiting its usefulness quite a bit.

Finally, you could use JVM Clojure to expand Ferret macros the same way it's done with CLJS.

3

u/halgari Aug 09 '17

I've done a fair amount of work with writing Clojure-esque language on several platforms, and the tricky bit is solving shared structure memory management. Reference counting doesn't work in a multithreaded environment, and GCs are somewhat adverse to the goal of Ferret compiling to "normal" C++. Considering how critical persistent hash maps and vectors are to a proper Clojure install, I'd love to hear from the author how they plan on solving these hard problems.

So no, it's not macros, eval, or any of that "easy" stuff that makes me say "Ferret isn't a Clojure", but the total lack of the guts of what makes Clojure unique: immutable collections.

4

u/yogthos Aug 09 '17

I completely agree that persistent data structures are a big part of Clojure. However, my point is that even just having Clojure style DSL available in C++ environment would provide a lot of value. Embedding Scheme has been a popular approach for years, and Ferret addresses same problems. Embedded DSL languages aren't typically used to write the types of large applications that Clojure is used for. We already have regular Clojure for that.

Frankly, I have a hard time understanding your negativity here. This is a specialized tool for solving a specific problem of having a nice embeddable DSL for C++ projects. It's clearly not meant as a standalone language, or as an alternative to Clojure.

5

u/halgari Aug 09 '17

You're right, I'm turned off by the title of the project "Ferret is a free software Clojure implementation for real time embedded control systems."

It's not a Clojure implementation, and I think it's misleading to potential users (they loose time investigating the tech just to find out that it's not what it says on the tin). I would be fine if they changed the language to: "Ferret is a free software DSL for real time embedded control systems, inspired by Clojure."

5

u/halgari Aug 09 '17

I'll try to curb my negativity :).

I think what bugs me is that the problems someone would have to solve to get a "Clojure in C++" are really hard. And as someone who's spent a fair amount of time trying to solve these problems it rustles my feathers when someone leads people to think that these problems have been solved, when in reality they haven't.

But I get your point, I'll try to be less negative.

2

u/yogthos Aug 09 '17

OK yeah that's fair. I did title it as a restricted subset, but could've been more explicit about it not really being Clojure.

1

u/j0hnGa1t Aug 13 '17

Reference counting doesn't work in a multithreaded environment...

Isn't that just std::shared_ptr ?

1

u/halgari Aug 13 '17 edited Aug 13 '17

Which do not support mulithreading. Look up the ABA problem or hazard pointers for more info. https://en.m.wikipedia.org/wiki/Hazard_pointer

1

u/j0hnGa1t Aug 13 '17

cppreference.com: std::shared_ptr

std::shared_ptr is a smart pointer that retains shared ownership of an object through a pointer. Several shared_ptr objects may own the same object. The object is destroyed and its memory deallocated when either of the following happens: (1) the last remaining shared_ptr owning the object is destroyed; (2) the last remaining shared_ptr owning the object is assigned another pointer via operator= or reset().

...

All member functions (including copy constructor and copy assignment) can be called by multiple threads on different instances of shared_ptr without additional synchronization even if these instances are copies and share ownership of the same object. If multiple threads of execution access the same shared_ptr without synchronization and any of those accesses uses a non-const member function of shared_ptr then a data race will occur; the shared_ptr overloads of atomic functions can be used to prevent the data race.

1

u/halgari Aug 13 '17

I read that too, but I don't think it means what we (myself included) think it means. Firstly, if it was this easy to solve I don't think hazard pointers would have needed to been invented. Secondly, it doesn't line up with the reason the ABA problem occurs (it's not a simple thing to solve). Thirdly, I tried this code, and it segfaults pretty quickly with "object being modified after being freed". Compiled with Clang and C++14.

#include <iostream>
#include <thread>

class Object
{

public:
    int val;
    Object(int val) : val(val) {};
};

std::shared_ptr<Object> ptr;

void mutate() {
    for (int x = 0; x < 10000000000; x++) {
        ptr = std::make_shared<Object>(42);

    }
}

int main() {

    std::thread t1(mutate);
    std::thread t2(mutate);

   t1.join();
   t2.join();

   return 0;
}
→ More replies (0)

1

u/j0hnGa1t Aug 13 '17

Considering how critical persistent hash maps and vectors are to a proper Clojure install, I'd love to hear from the author how they plan on solving these hard problems.

Phil Nash gave a recent presentation at ACCU 2017 about his persistent hash implementation in C++.

https://github.com/philsquared/hash_trie

2

u/jbiserkov Aug 08 '17

I believe this project is targeted at embedded systems with limited RAM. It even can be configured to run without calling malloc, so the memory usage can be known at compile time. So "no eval" is a feature in such a context.

6

u/aliem Aug 08 '17

Nice! Finally it does have his own home :)

I'm tempted to write a firmware for my Atreus

5

u/[deleted] Aug 09 '17

I'd be surprised if you could get it to fit in 2.5kb of RAM. You'd have better luck with MicroScheme: https://atreus.technomancy.us/firmware

1

u/aliem Aug 09 '17

Thank you for the insight, I didn't really take into account anything except the small idea.

BTW, I'm loving the keyb :)

2

u/ProfessorSexyTime Aug 08 '17

Oh shit now I can do lower level stuff with Clojure? Fuck yea!

1

u/[deleted] Aug 10 '17

I tried it and it is jolly good.

0

u/[deleted] Aug 08 '17

[deleted]

11

u/halgari Aug 08 '17

More than you would expect, not as much as you would like. That sounds really vague bit it's pretty accurate. A lot of the standard library and standard features aren't there, it's mostly a port of some of the ideas of Clojure to C++.

From what I can tell, some things like immutable hashmaps aren't implemented yet (and are super hard to write in a non GC language if you want multithreading support). But it's hard to tell for sure because the sourcecode isn't in a version control repo. So none of that makes me want to dig much more into it.

0

u/[deleted] Aug 08 '17

[deleted]

2

u/halgari Aug 09 '17

I'm not sure that's correct, I see hash-map referenced several times in that doc, but no implementation that I can find.