Power
Text Editors
You can develop OCaml programs using any text editor. If you already are comfortable with Emacs, Vim, or VS Code, proceed to the instructions regarding your editor of choice. Your profs usually use Emacs with Tuareg mode and Merlin. VS code, which has a nice extension for OCaml, is also very popular these days.
Merlin also has a front-end for Vim. Eclipse offers some other OCaml plugins. Many students have had success with Sublime, which handles syntax highlighting natively but does not offer support for in-editor evaluation nor Merlin. Atom is perhaps an even better choice, as students last year indicated it had better plugins for built-in support of Merlin and other tools than does Sublime.
VS Code
Go to VS Code and download the editor for your platform (Windows, Mac, Linux).
Open VS Code. Go to the extensions component of the editor. (On a Mac, you can use Command-shift-X to get there, or you can go to Extensions under the View menu to get there, or you can click on the 5th icon, which kinda looks like a bit of a crossword puzzle, in the upper-left column of the VS Code window, to get there.) Search for "OCaml and Reason IDE" in the search box. Install it. Try opening a .ml file, like the one you get from assignment one. You should see some syntax highlighting.
If you are using VS Code on a Windows machine and have installed the Windows Subsystem for Linux, you might want to take a look at this blog post
Emacs
There are several different implementations of Emacs.
- On a Mac, one of your profs uses 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-setupand 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
Check out this link on Emacs from scratch for more information on using Merlin with Emacs.
Vim
To install Merlin, type:opam install merlinand then
opam user-setup install
Check out this link on Vim from scratch for more instructions.
Eclipse
Many students develop Java programs using Eclipse. However, I'd probably suggest that you use VS Code (see above). It seems to be the thing people who want this kind of editor are using these days. 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.
Acknowledgement
These OCaml set-up notes come in part from Dexter Kozen, Nate Foster and Arjun Guha, as well as the Cornell Cos 3110 page found at https://www.cs.cornell.edu/courses/cs3110/2020sp/install.html.