Domain-shims: an implementation of `Domain` for OCaml 4 (beta version)

Hi everyone,

I just wrote a small package, domain-shims, that provides a non-multicore implementation of the new Domain module of the OCaml 5 standard library:

repository: https://gitlab.com/gasche/domain-shims
the one source file: https://gitlab.com/gasche/domain-shims/-/blob/trunk/lib/domain.ml

This is a fun hack, but it may also be useful for people who write some code using Domain to test it under OCaml 5, but still want to be able to build and run their project using their normal OCaml 4 setup.

I haven’t put a version number and made a formal release yet.

The main limitation of the library is that it does not implement the Effect module providing effect handlers, so it is not true that all OCaml 5 code will be compatible. I discuss this in more details in the README # no effect support, quoted below:

OCaml 5 also provides effect handlers as a new Effect module of the standard library. We do not provide an implementation of Effect, which is much harder to emulate using regular OCaml 4.

Someone could experiment with an implementation using the delimcc library for delimited control. (Please do this and report! It is probably fun.) We expect that the result would be much slower than the OCaml 5 implementation, probably by an order of magnitude. This would be okay for toy experiments. But it would not be good enough to seriously run libraries relying on effect handlers for performance-sensitive operations, such as Domainslib or Eio.

The other option would be to implement shim versions of Effect-using libraries. We are considering providing a shim for Domainslib in particular, which is very easy to do without emulating effects at all. This general approach sounds scary (no limit to the number of programs you need to provide shims for), but remember that OCaml 5 users are not supposed to use effect handlers too much yet, it was only made available for lightweight concurrent schedulers.

Happy hacking :slight_smile:

10 Likes

To really be useful I think this should be a noop in OCaml >= 5.0.
You can see an example of how to do this in the seq library:

3 Likes

Hmmm. I agree that I need to ensure the package (once released) can be added as a dependency for a project, install itself on OCaml 4 and “do nothing” on OCaml 5. But the solution you propose looks painful: a fair amount of Dune boilerplate, and I would be working with source files with a different name and extension, breaking my Merlin setup etc. I’m sure there are many solutions to configuring/packaging/releasing shims, and you @kit-ty-kate know them better than I do, so eventually I will have to go for it anyway.

(I sent you an invitation to co-maintain the package in case you think it would be useful for you. No pressure, we all have our plates full already.)

The amount of dune rule looks very manageable and merlin just works fine, I’ve tried. No need to change the (final) extension:

1 Like