DEV Community


Hello World with OCaml and ReasonML

idkjs profile image Alain ・2 min read

A note to self on using OCaml with the ReasonML syntax without Bucklescript.


  1. Install opam
  2. Install reason language options with opam install reason


Create a file:

print_string("Hello world!\n");

Compile by running ocamlc -o hello -pp "refmt -p ml" -impl

Open your terminal and run ./hello.

Your output is:

➜  helloworld ocamlc -o hello -pp "refmt -p ml" -impl
➜  helloworld ./hello
Hello world!



Notes from Discord Channel:

@idkjs the ocaml toolchain read only ocaml syntax, binary ast (which is shared between reason/ocaml). Hopefully the reason package come with a tool that is able to translate both syntax refmt.
the -p option of refmt is print. So refmt -p ml > take the reason syntax in the re file and output in the file. I use -p ml for debugging but -p binary is used by dune because it is quicker. Then you can use OCaml binary ocamlc -o my_target (the -o is for ouput default name is machine dependant). because file end with .ml the compiler know it must build .cmo/.exe, if passed a .mli it will build .cmi, etc. The problem with .re file is it is not .ml nor .mli so we need to say it will be a implementation -impl or a interface -intf. and finally -pp is run this command on this file to have a valid ocaml file (here we use refmt because it output valid OCaml)

-impl <file>  Compile <file> as a .ml file
-intf <file>  Compile <file> as a .mli file
-intf-suffix <string>  Suffix for interface files (default: .mli)
-pp <command>  Pipe sources through preprocessor <command>


Editor guide
citizen428 profile image
Michael Kohl

Have you looked at esy yet? It uses reason-native for Reason projects.

Side note: I think it's great that Reason exposes more developers to the ML language family, but as someone who already knows OCaml and F#, I find the JS-based syntax to be a bit of a turnoff, it's much noisier than normal ML.

idkjs profile image
Alain Author

All about esy brother. Those dudes are ocaml/npm alchemists. The issue was trying to understand how to do it without esy. If you want to see it with esy check out It does the same exact thing using esy. The readme tells you how to do it.

citizen428 profile image
Michael Kohl

Thanks, I know how to do it with esy, I just wasn't sure if you are aware of it.

Thread Thread
idkjs profile image
Alain Author

If you had asked me before yesterday, I would not have been aware of it. Thank you for making the point, brother.

Incidentally, can you recommend some F# sharp starting material? I need to get my feet wet there.