Caml
Power

Text Editors

You can develop OCaml programs using any text editor. We recommend Visual Studio Code (VSCode), but you may use Emacs, Atom, Vim, or whatever.

To get automatic syntax highlighting in Emacs, Atom, or Vim, you will want to use Merlin, which is an OCaml syntax-highlight tool for use in editors like that. (VSCode uses Merlin under the hood too.)

VS Code

A link to instructions for installing VS Code can be found on the Resources page

Emacs

There are several different implementations of Emacs.

  • On a Mac, you can use Aquamacs because it is well integrated with the Mac windowing system. To open files from the terminal, you can type open -a Aquamacs filename. You can create an alias or a script (as described here) to abbreviate that so you just need to type amacs filename, which is convenient.
  • Alternatively, on a Mac, if you have homebrew installed, follow the instructions here under the heading "Homebrew".
  • On Ubuntu Linux run the following command from a terminal (for other distributions substitute their appropriate package manager):
    $ sudo apt-get install emacs24
    

Tuareg Mode

Tuareg mode is a special mode for editing OCaml files in Emacs. You'll want to use Tuareg mode whether or not you choose to install Merlin; Tuareg mode provides syntax highlighting, indentation, etc., while Merlin helps with programming logic. Tuareg is now available via OPAM. You will want to install tuareg mode, merlin, ocp-indent and user-setup. Run the following command. If you have already run a similar command and installed some of these packages, that is ok. The package manager will tell you that they have already been installed. No harm done. That is what package managers are for.
opam install -y tuareg merlin ocp-indent user-setup

Once the install completes, opam tells you what to do next. Follow opam's instructions. It should tell you to run the following command to initialize your editors:

opam user-setup install

That will copy a few lines into your .emacs file, which configures emacs.

You can also find the Tuareg software and installation instructions on the Tuareg github, but it is typically easier to use opam and hopefully you just did that so you don't need to do any of the following. The key files for manual installation are tuareg.el, tuareg_indent.el, ocaml_debug.el, and tuareg-site-file.el. Place these files in an accessible directory, and add the following line to your .emacs configuration file, substituting that directory for $DIR:

(add-to-list 'load-path "$DIR")
(load "tuareg-site-file")

Once you have installed OCaml, Emacs, and Tuareg open any file with a .ml or .mli extension. This should start up Tuareg mode and you should see OCaml key-word, comment color highlighting and other features. To start the OCaml top-level interpreter in Emacs, on Windows at least, make sure the OCaml bin directory is in your path. At this point, you should be able to type C-c C-s in Emacs and the OCaml top-level interpreter will start. Alternatively, you can start OCaml from the Tuareg pull-down menu by choosing "Interactive Mode" and then "Run Caml Toplevel."

Merlin Mode

To install Merlin, type (once again, if you've been doing things in order, you already have Merlin installed; but attempting to install merlin with opam again does no harm):
opam install merlin user-setup
and then
opam user-setup install

Once the install completes, opam will provide some instructions about what to do next. That may be to run the following, if you didn't run it before with Merlin installed. (Running this command multiple times does no harm. You want to run it at least once after you have emacs installed and after you have downloaded tuareg, merlin and ocp-indent via opam.)

opam user-setup install

Merlin supports several nice features within Emacs. For example:

  • M-x merlin-mode: begin using Merlin mode (also starts whenever you load a .ml file)
  • C-c C-x: jump to the next Merlin flagged error.
  • C-c C-t: type check the expression your cursor is on
  • C-c C-l: when your cursor is on an identifier, jumps to the code that binds that identifier
There will be various ways to attempt auto-completion for identifiers depending on the environment. In some builds of emacs with Merlin the command is C-c [tab] others have just M-[tab].

Check out this link on Emacs from scratch for more information on using Merlin with Emacs.

Vim

To install Merlin, type:
opam install merlin
and then
opam user-setup install

Check out this link on Vim from scratch for more instructions.

Eclipse

Many students have developed Java programs using Eclipse. However, nowadays it's probably better to use VS Code. Nevertheless, in the past, several OCaml plugins for Eclipse have been available in various states of development, as well as a stand-alone OCaml IDE from Brown called Camelia.

Of these options, people have had the best luck with the Eclipse plugin OcaIDE. In the past students have gotten it configured appropriately simply by following the online instructions.