Department
Princeton University

Computer Science 510
Programming Languages
Andrew Appel

Spring 2023


If you have already installed the Coq Platform via opam, then you have already done the steps below up to Ocaml. If you have installed the Coq Platform via compiled binaries, you'll need to perform all these steps.

If you'd rather use Emacs or some other text editor instead of VS Code, see Text Editors note below. In this course you will be writing very little OCaml code (just this week's homework), so you may not need a fancy IDE.

If you'd rather not install OCaml at all, then you can use OCaml on the CS-department "cycles" machines. Just log into cycles.cs.princeton.edu, and /usr/bin/ocaml and /usr/bin/ocamlc should be in your searchpath.

Installing OPAM and OCaml

Follow these instructions in Michael Clarkson's textbook, OCaml Programming: Correct + Efficient + Beautiful. Clarkson gives instructions for installing:

  1. a Unix development environment (which could be native Linux, native MacOS, or (on Windows 10/11) a WSL2 virtual machine running Linux.
  2. The opam package manager running under Unix.
  3. The OCaml compiler installed through opam.
  4. The VS Code editor, running native. (That means, under Windows your VS Code will not run inside WSL2.)

Text Editors

If you'd rather not use VS Code, just follow the instructions above up to step 3. Then you can use Emacs or some other text editor.

Emacs

If you are an Emacs user, you can run OCaml inside an Emacs buffer and paste code in there to experiment.

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, indendation, etc., while Merlin helps with programming logic. Tuareg is now available via OPAM:
$ opam install tuareg

Once the install completes, there are instructions indicating the lines of code you should copy in to your .emacs file. Cut and paste those lines in to your .emacs file, adjusting the placeholder variables to be the correct path, as stated in the instructions given by OPAM.

You can also find the Tuareg software and installation instructions on the Tuareg github. 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."