Using Reason libraries from pure OCaml

I stumbled on https://github.com/Schmavery/reprocessing and asked myself: Is it possible to use a library written in Reason syntax in a pure OCaml project?

It should be as Reason is just an alternative syntax. But what would I have to do concretely? Which tools are needed? What would I have to install starting from a plain OCaml opam switch? Do I start by converting the library using refmt? Or is that unneccessary, and if so, will merlin and other tools work nicely when jumping to definitions between ocaml and reasonml files? Will I have to install a language server for Vim? npm?

4 Likes

Jbuilder/Dune has Reason support. From what I understand, if a Reason package is published to OPAM, you can use it just like any other OCaml package if you use that build tool.

Hm. The project I linked seems to rely heavily on the JS package manager npm for installing prerequisites, even though it targets not only JS but also bytecode and native. So this seems potentially incompatible with package installation via opam. I guess the problem there is external dependencies.

For libraries in Reason which do not depend so heavily on JS, it would be cool to have an opam option to install them and convert to OCaml syntax on the fly. This would be a nice feedback from the Reason community on the core language! However I am not aware of significant Reason projects that are independent of JS.

There is, generally speaking, no need to convert code written in Reason’s syntax to OCaml syntax to make it usable from an otherwise OCaml-syntax-based project. Once the code is compiled the original syntax doesn’t matter - you can freely use the library as if it had been written in OCaml syntax, regardless of the build system you use.

As @yawaramin mentioned, jbuilder/dune supports Reason out of the box. This means you can even go as far as mixing .re and .ml code in a single project.

1 Like

I appreciate that the syntax is ‘gone’ after compilation. I mixed up two points. My original question was about the concrete tooling needed to work on a mixed Reason/OCaml project.

My later comment was about something else: refmt does automatic conversion. So, there are two ways to go about it: One, have the user read Reason syntax, which may be better since it ties in with the documentation which will presumably be available only in Reason syntax. The other way would be to shield the user from Reason syntax completely by converting the source including mli files upon install. That way, when asking Merlin to jump to the definition, one would only ever see OCaml syntax.

1 Like

For what it’s worth, ocamlformat can be used to convert from Reason to OCaml with better output than refmt if/when you do have a need for that!

That conversion may be better to handle on the fly in an editor. It would be difficult to know at installation time if the code should be converted or not. That would make jumping to a definition more complex.

3 Likes

Hey @n4323 , I just saw this thread. Sorry for not seeing it before. I’m one of the reprocessing contributors.
The syntax being Reason is not the problem, currently the big issue is that the build system used to compile reprocessing (bsb-native) does not output ocamlfind files (META and .install). I’m currently working on making that work.
Once that works you’ll be able to clone Reprocessing, npm install to get all of the deps, run bsb-native to generate the META and .install file and compile everything to one cma/cmxa library file, and then use a tool like opam-installer which should pick up the files and make the project available through ocamlfind.
I know this is tedious, and I’m very sorry about this. I wish I could find time to publish all of my packages to opam as well, or have a simpler way for you to install this.

An alternative to that is Esy, but @jordwalke can talk in more detail about that.

One last idea could be to use bsb-native to setup your own project. It’s a simple build system, and it allows you to write in OCaml (of course!). I don’t want to turn this answer into a sales pitch so I’ll let you look at it if you want to. Here’s an example project: https://github.com/bsansouci/bsb-native-example

Hi @n4323,

Hope you’ve already find what you need from the previous replies.

For what it’s worth, here are two examples of projects, one of which uses Reason but without dependence on any JS stuff, and another one that mixes up .ml and .re under one source directory.

zest: a store engine implementation on top of irmin, speaking zmq but in RESTy style
lib-ml-databox: client code to store instances that build on top of zest (under /lib)

Both of them using jbuilder/dune as the build tool, and personally I think it provides rather smooth experiences when your codes need to deal with these two different syntaxes. Hope it helps!

4 Likes

thanks! that will certainly be helpful to sort out the effect of JS dependencies vs. that of the Reason syntax alone.

For a non-JS Reason library to be used in an OCaml context, would it be feasible / make sense in principle to just translate all .re files into .ml files with ocamlformat from the start and then forget about Reason altogether?