News for hackers. Stuff that matters.
Re:OCamlScript – a JavaScript back end for OCaml by jordwalke on Thursday January 07, 2016 @06:39PM (#10861704)
This looks like it could be very useful. I've really enjoyed using js_of_ocaml, which supported source maps very nicely, but the value printing was often obscured because types and record field names were erased. This looks like it has much deeper integration with the host JS environment and ecosystem. Great work and I look forward to seeing where this goes.
Re:OCamlScript – a JavaScript back end for OCaml by Drup on Thursday January 07, 2016 @06:52PM (#10861786)
I'm still wondering why js_of_ocaml couldn't be improved. You yourself did some work towards that, mostly out of the compiler itself.

We have multiple standard libraries, two concurrency library. It has damaged and divided the community significantly. And now two compilers to js ? :/

Re:OCamlScript – a JavaScript back end for OCaml by jordwalke on Thursday January 07, 2016 @07:09PM (#10861894)
Small correction: I did not improve js_of_ocaml besides giving feedback (which included feedback about the need to not mangle names).

Before Babel for JS came out, there were tons of different solutions to compiling ES6 features into ES5, and the decision about which toolchain to use would fatigue newcomers to the community. But it was the fact that the community was relatively accepting of new spinoffs that Babel was able to gain momentum, and increase its quality to the point where it is the de factor tool that everyone uses - and it's really good.

As a consumer of the compiler, I don't necessarily consider multiple options a bad thing as long as something of really high quality emerges. (Although I can see how for something like an Async library (that introduces incompatibilities between dependencies) that would be particularly frustrating to have too many options)

It looks like OCamlScript used some good ideas from js_of_ocaml. Maybe js_of_ocaml can end up sharing some of the good ideas from OCamlScript so that it can also have very readable/debugable code and ecosystem integration?

Re:OCamlScript – a JavaScript back end for OCaml by jordwalke on Thursday January 07, 2016 @07:41PM (#10862067)
Drup, Ah, yes - CommonML does indeed have built in support for js_of_ocaml.

And I think you have valid points. I'm curious though, since both js_of_ocaml have seamless interop with JS (seamless in terms of memory model), couldn't the two have seamless interop between each other by going through JS bindings?

I'm curious, do you think it's possible or practical to create an OCaml to JS compiler that has the strengths of both approaches?

Re:OCamlScript – a JavaScript back end for OCaml by Drup on Thursday January 07, 2016 @08:17PM (#10862229)
It doesn't seem that ocamlscript and js_of_ocaml are using the same memory representation for OCaml values, so that's a first issue.

At least for the integration between the js world and the ocaml world, I do believe it is possible. yes. For readability of the js produced, I really can't tell. It's probably pretty easy to improve js_of_ocaml on that though, given the current state. :)

Re:OCamlScript – a JavaScript back end for OCaml by jordwalke on Friday January 08, 2016 @02:34AM (#10863493)
Drup:

Also, if it wasn't abundantly clear (to you or others) - I believe js_of_ocaml to be perhaps the best systems language compile-to-js toolchain that I've ever discovered (and I've looked extensively for others) - so I hope you can fully appreciate just how valuable it continues to be, to me personally and many others.

I have a question about your comment: "It's probably pretty easy to improve js_of_ocaml on that though, given the current state."

Would this also require patching the compiler to preserve record field names and avoid name mangling in general?

And I have a question about your comment on reddit which implies that maintaining a backend would be difficult to sustain: Do you believe that a seamlessly debuggable JavaScript backend, could generate the necessary amount of attention required to sustain such a backend, given how much more widely accessible end-end OCaml development would become as a result of its existence?

Re:OCamlScript – a JavaScript back end for OCaml by jordwalke on Thursday January 07, 2016 @08:39PM (#10862310)
Drup, By the way, I really have appreciated the absolute simplicity of js_of_ocaml - it's pretty great how you can just take any byte code file and run it in any JS engine with great performance - it's amazing really. I really love how js_of_ocaml does dead code elimination, and inlining among other features.

It seems like the primary difference is that the goals of js_of_ocaml gravitate more towards taking an app/library that had already been fully tested/debugged in OCaml and then running that thing in the browser with some nice debugging facilities that let you see the source code and kind of step through in the more rare event that something goes wrong.

I can't speak for this new approach here, but it seems to be more focused on using the JS target as the core development workflow for rapid iteration. I can see why that's appealing. Everyone knows how to use the browser debugger, and JS is one of most ubiquitous byte code formats ever. Running your OCaml app in the browser also gives you all kinds of really great timing and memory profiling tools that help you detect leaks (that even tells you something about the nature of your program when compiling OCaml to machine code).

Obviously, I wish there was a single toolchain that could actually do both though - a toolchain that you could use to get very JS-friendly code generation so the debugging/value printing/console inspection works just like it does in JS, but then would also let you do a js_of_ocaml style compilation based purely on an OCaml byte code object file (edit: or some kind of representation of the whole program) which would be free to remove and obscure record field names if it so chose.

(BTW: The difference in object representation (record field names are not obscured) seems to be the very thing that makes the values inspectable in the browser. Maybe there are some of OCamlScript's compiler patches that could be accepted in upstream ocaml so that the same can be achieved with js_of_ocaml?).

Re:OCamlScript – a JavaScript back end for OCaml by Drup on Thursday January 07, 2016 @07:28PM (#10861993)
I was actually talking about your work for CommonML[1], which I count as improvements to the "js_of_ocaml ecosystem". ;)

My issue here is that both are strictly incompatible and that you can't write FFI to a library for both.

[1]: https://github.com/jordwalke/CommonML

Re:OCamlScript – a JavaScript back end for OCaml by hongboz on Thursday January 07, 2016 @06:58PM (#10861834)
Author here, just created a new account : ). I think ocamlscript is complementary to js_of_ocaml, actually it is inspired from js_of_ocaml. For those who like to write ocaml only application, js_of_ocaml is perfect, ocamlscript is designed to help easier integration with existing javascript system.

Design goals here: http://bloomberg.github.io/ocamlscript/

Re:OCamlScript – a JavaScript back end for OCaml by cannam on Friday January 08, 2016 @04:28AM (#10863825)
In the (nice) immutable map example on that page, a reference cell is used & updated each time something is added to the map. How would the JS output differ if you built the map using a fold or similar, without any refs?
Re:OCamlScript – a JavaScript back end for OCaml by jordwalke on Friday January 08, 2016 @05:27AM (#10864003)
Before that, I'd ask if the implementation of OCaml's map is at all similar to the implementation of Immutable.js's Map. Different internal data structures/algorithms are probably the most likely explanation for any difference. Still, it's nice to see a benchmark that shows how OCaml compiled to JS is competitive in performance, even when compared to a very popular library that is known for performing well.
Re:OCamlScript – a JavaScript back end for OCaml by cannam on Friday January 08, 2016 @06:35AM (#10864181)
Also a fair point, and I take your implication that the effect of a change to the outer loop syntax would probably be lost in the noise. It just struck me that using ref cells and loops feels like a concession to writing Javascript-style code in the first place (I grant in this particular case it makes the code more obvious) and I wondered how much difference it actually makes. Is the compiler able to flatten a fold into a similar loop? Or do you get a readable fold call in the JS code and, if so, is it much slower? Or neither?
Re:OCamlScript – a JavaScript back end for OCaml by hongboz on Friday January 08, 2016 @01:48PM (#10867187)
List.fold_left is compiled into a while loop in ocamlscript. The main reason that I used for loop is that the generated code is the same as hand written js, so the comparison is more reasonable

Edit: I am a practical programmer, I am in favor of both for loop and recursion as long as it is readable

Re:OCamlScript – a JavaScript back end for OCaml by Drup on Thursday January 07, 2016 @07:08PM (#10861879)
That is how you justify the project, and it makes sense, when you look at it with a microscope.

But my remark still stand: we now have two compilers to js, their API/FFI is not the same, so you can't factorize work, and the integration work could be made on js_of_ocaml too (see alain frisch's project).

Re:OCamlScript – a JavaScript back end for OCaml by jordwalke on Thursday January 07, 2016 @07:26PM (#10861975)
Hi Drup,

I'd be curious to hear about the work that Alain is doing. Do you have a link?

Re:OCamlScript – a JavaScript back end for OCaml by Drup on Thursday January 07, 2016 @07:33PM (#10862018)
Re:OCamlScript – a JavaScript back end for OCaml by hongboz on Thursday January 07, 2016 @08:21PM (#10862249)
actually we have great relationship with Lexifi, and we did exchange some ideas about ocamlscript before, I think mostly we are on the same page, there is enough room for two compilers :-)
Re:OCamlScript – a JavaScript back end for OCaml by truncate on Thursday January 07, 2016 @10:14PM (#10862691)
Its good to see ocaml->js back to life. Have you considered supporting TCO or arbitrarily deep stack? I'm just curious how often could we reuse ocaml libraries with just tail call to loop conversion.
Re:OCamlScript – a JavaScript back end for OCaml by hongboz on Friday January 08, 2016 @10:48AM (#10865661)
self tail call is supported. mutual recursive tailcall can be done partially, but there is not a strong guarantee
Re:OCamlScript – a JavaScript back end for OCaml by vitriol83 on Friday January 08, 2016 @05:42AM (#10864036)
I would say the main problem with the OCaml community is it's too small. More projects are welcome imo, even if it inevitably means some duplication of effort. After all js_of_ocaml is free to take some of the more desirable features.