Hot swapping in OCaml 🥵

Hi, is it possible to do hot swapping in OCaml?
Otherwise, is it possible to add it and how difficult it will be?

1 Like

What exactly do you mean by hot swapping ? I have no knowledge on that topic, it sounds interesting.

I assume you mean hot code reloading. I think it was possible to some degree using Dynlink, as shown in this blog post I found: Hot-reloading OCaml on Web, Desktop, and Android | Jared Forsyth.com. However, it might be no longer possible since 4.08: #1063 which if I understand correctly forbids loading module when a module with the same name is already loaded.

I have been using this tool to live-code in OCaml:

It worked successfully to develop medium-sized programs, though OCaml idioms have to be adapted to be a bit more “live” friendly.

3 Likes

Here is an example of hotcaml editing the slideshow of wall presentation:
out

Steps to reproduce:

opam pin add wall https://github.com/let-def/wall.git
opam pin add hotcaml https://github.com/let-def/hotcaml.git
git clone https://github.com/let-def/wall.git
cd wall/slideshow
make

A window should open displaying the slideshow.
Open another terminal and edit wall/slideshow/presentation.ml, changes should be reflected in the window.

9 Likes

Thanks it’s exactly what I was looking for!

1 Like

Fantastic work @let-def
How feasible would it be to hook this up to a utop style repl?

Speaking of utop, this reminded me of an older experiment I had - It’s not quite the same as HotCaml above, but you can get a Proof-General style interaction mechanism with OCaml with some simple GNU Emacs hackery:

interactive-utop-mode

Which seems like it would be a pretty good fit for live-coding.

Code: Interactive Utop Mode ($2170216) · Snippets · Snippets · GitLab

2 Likes

It can be done, at least from an engineering point of view.
I have given thoughts to something similar, not for hot loading but for debugging: interrupting a running program and getting a toplevel in the current execution context, with builtin stack navigation.

So while it is possible to implement, I am not sure what is the right UI/UX to present that kind of features. (This hotcaml proof-of-concept did not need much work on this front: the edition workflow does not change, it is just the runner that pick up changes when they typecheck).

1 Like