We should creating a (simple) pure-OCaml online live coding/programming environment!

Hello there.

I’m a long time fan of OCaml and especially of TryOCaml.
I think that for teaching OCaml and teaching computer science with OCaml, having a web-based in-browser OCaml interpreter like TryOCaml is already a great tool, easing the first contact with the language and its REPL ocaml in a terminal.
But using a REPL has the limitations that we all know, it’s so “static”: you type, hit enter, and see the result, one statement at a time…

There also exists web-based in-browser OCaml editors and interpreters that go one step further, I’m thinking of “Learn OCaml”, <Sketch.sh> or others.
There also exists two “kernels” to use OCaml in IPython or Jupyter notebook, and one is even available to try online, with OCaml code executed in javascript: IOCaml-JS by @andrewray on GitHub (have a look if you don’t know that, it’s quite powerfull!), and also ocaml-jupyter by akabe, which is a more complete kernel, but needs a server under the hood, able to run ocaml.

But it’s still not interactive enough, I have to write the code, hit run, see the result, and iteratively do that over and over as the code progresses. I want more!

So after digging, I just discovered that Reason documentation has this page https://reasonml.github.io/en/try which lets you write OCaml (or Reason) code, and automatically “live” converts it OCaml <-> Reason (on the left), to JavaScript (with BuckleScript), and more importantly it executes your code continuously!!
That’s it, a live coding environment for OCaml (and Reason). It was written and improved in 2017.
But it’s overly complicated for the simpler goal of teaching OCaml, and half the screen space is used by Reason code. It also does not show the - val : ... and type inference that ocaml REPL shows when executing code, both of which are very useful when teaching (with) OCaml.

I also found this playground for Revery by outrunlabs, and Reason React playground by astrada on GitHub.

But apparently nothing exists for basic OCaml !! It’s quite ironic, as all these recents Reason and Revery and others tools are standing on the shoulder of the giant that is OCaml, but no such tools have been proposed for the “base” language.

I think a lot of teachers that teach OCaml at University could benefit to have this kind of live coding or live programming environment, but in a simplified version: just OCaml code on the left, and both result of OCaml toplevel (using js_of_ocaml version of ocaml topleve) and result of execution (just the stdout) on the right (maybe two panels, like in the Reason “try” page).

What do you guys think?

I’ve looked for this in depth on the Internet and here, and couldn’t find anything, not even a mention of “a live coding environment for OCaml”, nothing. See for instance on awesome-ocaml on GitHub: TryOCaml is mentioned, but nothing else in the same spirit.

If indeed you can confirm that nothing exists (yet), then I would like to write this myself, but would need some help… when seeing the code for the Reason playground, I don’t understand much of it.

This is inspired by the great « Bret Victor - Inventing on Principle » video (on YouTube). I’m quite amazed to see how few of these live coding environments exists, even for major languages such as Python. For VSCode, apparently the only serious one is the extension fraser.live-coder.

Thanks for your insights.

6 Likes

Here is what I mean by live programming, using Reason playground:

Basically, I would like to have a simpler version of this, just showing the OCaml input code and the console.

Including the types and - val : ... like sketch.sh does would be truly great.

1 Like

I like the direction

Hackerrank also has a live environment.

I would personally aim towards something like the devcontainers (ide’s in the browser) that some people have been adding to github repos, so that the developer can create a full dune file using whatever libraries that they want.

1 Like

I didn’t know about that. I’m aiming at a simple client-side (server-less, pure javascript) web app that can be downloaded as a zip file and installed on any webserver, not something as complicated (and server-side) as Hackerrank.

1 Like

I’ve just came across Klipse’s plugin for OCaml, which I discovered a few months ago. I forgot that it has continuous evaluation by default.
It works fine but has a big limitation: if the code happens to run infinitely at some time, it breaks…
ReasonML “try” page, on the other hand, displays a simple “[Evaluation timed out!]” if the code runs for more than a few seconds.

2 Likes

I tried Kilpse locally with Jekyll.

It works. cool~

I found the code is not fully like console toplevel. e.g.

let x = 1;;
let y = 2;;
x + y;;
(* it evals to 2 *)
let x = 1;;
let y = 2;;
let z = x + y;;
(* it evals to 3 *)

p.s. I have to hack the codemirror.css a bit to have a similar div box as the blog shows, though it should be caused by my css in Jekyll.