Compilers

# OCaml Development Environment Setup

Once you have completed the OCaml Setup, you should be ready to configure your development environment. A good development environment is important to efficiency: it will take longer to do the same amount of work if you don’t have good tools to help you. You are free to use whichever software you like, but the instructions in this guide will likely lead to a better OCaml development experience.

Your setup process will depend upon what kind of editor you are using. Your instructor uses and recommends Atom, which is already installed on the department machines, but you are free to use other editors if you like. The instructions below largely concern configuring your editors to use some tools we installed during the OCaml setup, most notably a code analysis tool called Merlin.

(Note for Windows users: some of these instructions may not work for you depending upon how OCaml is installed on your system. Please see the instructor if you need any assistance.)

## Atom

If you are using the department machines, Atom is already installed. Otherwise, please make sure that Atom is installed on your computer before continuing.

Fortunately, existing packages make it fairly easy to use OCaml with Atom; we just need to install them. This can be accomplished using Atom’s own package manager from the command line:

apm install language-ocaml linter ocaml-indent ocaml-merlin


Once you have installed these packages, you can verify they are installed by restarting Atom, pressing Ctrl+, (control comma), and clicking “Packages” on the left. You should see all of the above packages in the “Community Packages” section.

By default, autocompletion is bound to Ctrl+Space.

The above packages do not configure every keystroke we want. We can configure them by editing ~/.atom/keymap.cson file and adding the following text. (You will probably have to create this file from scratch.) Be sure to keep the indentation exactly as it appears here!

'atom-text-editor[data-grammar="source ocaml"]':
'ctrl-shift-t': 'ocaml-merlin:show-type'
'alt-shift-r': 'ocaml-merlin:rename-variable'
'ctrl-alt-f': 'ocaml-indent:file'


You can adjust the keystrokes if you like; these keystrokes are used by your instructor. They have the following effect in OCaml source files:

• Ctrl+Shift+T will show the type of the expression under the cursor (as long as your code is well-formed)
• Alt+Shift+R will rename the variable under the cursor (replacing this occurrence as well as all other occurrences that bind to the same variable; this is not just a search and replace)
• Ctrl-Alt-F will automatically indent your source code

## Vim

Configuration for Vim is relatively easy: it should be enough to run the following command:

opam-user-setup install


Once this is finished, you should be able to test your configuration by:

1. Running vim test.ml
2. Typing “List.m
3. Pressing Ctrl+X followed by Ctrl+O to activate autocompletion

If Merlin setup was successful, you should see a list of the names and types of all of the functions in the List module that begin with the letter “m”.

Documentation on the Vim commands and variables provided by this configuration can be found here.

## Emacs

Configuration for Emacs is relatively easy; in fact, Emacs is a very popular choice among OCaml developers. It should be enough to run the following command:

opam-user-setup install


Once this is finished, you should be able to test your configuration by:

1. Running emacs test.ml
2. Typing “List.m” followed by the tab key

If Merlin setup was successful, you should see a list of the names and types of all of the functions in the List module that begin with the letter “m”.

Documentation on the Emacs commands and variables provided by this configuration can be found here.

## GEdit and Sublime 3

The command

opam-user-setup install


indicates that it has some amount of support for GEdit and Sublime 3. You can try running that command to see what happens. Your instructor has no experience with these environments but will try to assist if you run into trouble.