Caml
Power

Using a Virtual Machine Environment

We will provide a virtual machine environment pre-configured for use in the course. If you have a Windows machine, it may be easiest if you use this option. This is is also a fine option for Mac or Linux users.

You can install the VM software from VirtualBox

This course's pre-configured Ubuntu image can be downloaded from the course site. WARNING: It is quite large (2.7GB), so we recommend a wired connection if possible. The image's MD5 hash is 47a2357c73d8f008d0b995785ad67e8e, if you would like to verify the download integrity.

To run the pre-configured Ubuntu image, use VirtualBox's "Import Appliance" command (from the File menu). Choose "Open appliance..." and select COS326VM.ova, which you just downloaded, then click "Import". Importing the image will require approximately 12GB of disk space. Once the image has been imported, you should be able to start the machine. At the login screen, the password for the user cos326 is Princeton.

The remainder of the installation intructions should not be necessary if you are using the VM image, however you may find the discussion about the Emacs keyboard shortcuts to be useful.

Installing OPAM and OCaml

Install the OCaml package manager OPAM. OPAM is the quickest and easiest way to get all kinds of libraries for OCaml programming. To install on Linux via each distribution's package manager or on Mac via Homebrew, follow these instructions. The binary and source installations are also available at the same page, for manual installation or installation in Cygwin.

Install OCaml. Follow the instructions from OCaml.org. It is likely easiest to use OPAM, however there are also instructions available for independently installing on various flavors of Linux, Mac, and Cygwin.

Text Editors

You can develop Ocaml programs using any text editor. If you already are comfortable with Emacs, Vim, or Eclipse, proceed to the instructions regarding your editor of choice. If you are indifferent, I recommend Emacs with Tuareg mode and Merlin.

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.

Emacs

To install Emacs on a Mac, follow the instructions here under the heading "Homebrew". To install Emacs 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, 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."

Merlin Mode

To install Merlin, type:
$ opam install merlin

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.

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]. In our VM image, which does not have Emacs24 auto-completion, M-c [tab] is the command.

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

Vim

To install Merlin, type:
$ opam install merlin

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

Eclipse

Many students develop Java programs using Eclipse. There are several OCaml plugins for Eclipse 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.

Using the OIT clusters

The Friend basement engineering computing clusters have OCaml installed on both the Windows and Mac operating systems.

For Windows, OCaml and Emacs can be found at Start -> All Programs -> OCaml. For Mac, we recommend Aquamacs, which is pre-installed.

The manual Tuareg instructions above will work, though note that you must ensure emacs is in fact opening the correct .emacs file, as there are several scattered among the various mounted local and network drives. You can see which one emacs is loading (and thus, which one you need to edit!) with the emacs command:

M-x describe-variable
When prompted for the variable, enter user-init-file

Using the CS department servers

For CS majors or other students with CS accounts, you can use OCaml version 4.01 on penguins. The manual Tuareg instructions above will work.

Note that merlin is not configured on these systems. Your mileage may vary when trying to install merlin within your user space on either the OIT or CS departmental machines.

Acknowledgement

These OCaml set-up notes come in part from Dexter Kozen, Nate Foster and Arjun Guha.

Real Programmers use Emacs