
OCamlScript – a JavaScript back end for OCaml - frutiger
https://github.com/bloomberg/ocamlscript
======
jordwalke
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.

~~~
Drup
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 ? :/

~~~
hongboz
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/](http://bloomberg.github.io/ocamlscript/)

~~~
cannam
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?

~~~
jordwalke
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.

~~~
cannam
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?

------
hongboz
Hi, thanks for your interest in it.

This project has been released to exchange some ideas and collect feedback
from the OCaml and JavaScript communities. It is still in an very early stage
and more like research projects though we already had some good results.

Note that ocamlscript is inspired from typescript and js_of_ocaml, we like the
readability of typescript generated js code, while don't want to loose the
expressivity of a powerful langauge, ocaml, here in particular.

The main difference from js_of_ocaml is that js_of_ocaml maps the whole
bytecode program into a js file while ocamlscript maps an ocaml module to es6
module.

Indeed, there is some difference in the runtime representation between
js_of_ocaml and ocaml, for example, ocamlscript uses js string and array for
ocaml string and arrray(we do this since we want an optimal js output at the
price of a bit runtime incompability), this may make interaction between
js_of_ocaml and ocaml a bit harder, but that's exactly why we made an early
release to hear what other people may think. js_of_ocaml is an excellent
project and we learned a lot from it and will seek some collaborations to
share some workload.

~~~
jordwalke
Question for hongboz and Drup about F-lambda:

OCamlScript does some cross-module optimization it seems. That's really cool,
and js_of_ocaml does several optimizations as well, but for both approaches,
it seems we'll not be able to take advantage of all really great f-lambda
optimizations to come because they happen at a compiler stage after
OCamlScript and js_of_ocaml have already intercepted their intermediate
representations. Do you (or Drup) have thoughts on that?

------
nv-vn
Not to be confused with the other OCamlScript [1]. Also, other than the naming
overlap, is there any reason why this hasn't been made available as an OPAM
package? Having to manually patch, compile, and install is a bit of an
inconvenience when the `opam switch` infrastructure already exists. Is it just
a matter of time before it's added to OPAM or are there simply no intentions
of adding it to the repositories? [1] -
[http://mjambon.com/ocamlscript.html](http://mjambon.com/ocamlscript.html)

~~~
jordwalke
I made github issues to discuss the name collision as well as allowing the
compiler patches to be applied via an OPAM switch.

------
ski91
Interesting to see many type safe functional languages compiling to
javascript. Elm is another such project being a haskell-like type safe
language which compiles to JS/HTML/CSS.

~~~
aylmao
PureScript is another one worth a look

~~~
cannam
Other compilers for ML-family languages to Javascript:

SMLtoJS (whose site at
[http://www.smlserver.org/smltojs/](http://www.smlserver.org/smltojs/) perhaps
ironically seems to be down at the moment), which compiles the full Standard
ML language (though not the full Basis library)

Yetiscript
([https://www.npmjs.com/package/yetiscript](https://www.npmjs.com/package/yetiscript))
which compiles (a very close approximation of) the Yeti language
([http://mth.github.io/yeti/](http://mth.github.io/yeti/))

Looks like there are some F# options as well, though I haven't tried any and I
don't know what the relationship between e.g. Websharper and Funscript is.

Any others?

------
e_d_g_a_r
This is great, idea of mapping a module to es6 module makes sense.

------
ante_annum
what is it about js_of_ocaml that made this worth doing?

~~~
djur
The design goals section of the README makes that pretty clear. js_of_ocaml
produces pretty opaque JS (in my experience) that doesn't make much use of
modern features like modules.

------
aylmao
Yes please.

