OCaml for Aliens: Development Environment

Part of OCaml for Aliens.

Package Manager

OPAM is the package manager used for OCaml packages built from source. Install this and the OCaml toolchain with it the first thing you do.

Toplevel

OCaml comes with a toplevel or REPL (read eval print loop). A REPL is quite useful for experimentation. Install and use UTop instead of the default toplevel as it has command history and working control keys. Things mostly works as expected when it comes to input.

Build System

Most new software seems to be built using Dune. Unfortunately Dune is not well documented (in my opinion). It does not explain the central concepts well enough before moving on to details. A way around this is to clone the dune-universe repo and figure out how other projects use certain Dune configuration to accomplish what you need. It's unfortunate to have to do it this way but for me it has worked better than trying to understand the Dune documentation.

Even though my criticism of the Dune documentation it seems to do its job well enough in actual use of building software. It also automatically generates .merlin configuration files for the editor integration so that code navigation and completion works properly.

Editor and Integration

I use GNU Emacs and fortunately for me the Merlin editor plugin works really well for OCaml development. Merlin also support Vim but I have never tried it.

Install tools:

opam install merlin tuareg ocp-indent

You should probably then add something similar to what follows to ~/.emacs. Configuration examples are also provided when installing the tools.

;; tuareg
(load "~/.opam/4.08.0/share/emacs/site-lisp/tuareg-site-file")

;; ocp-indent
(add-to-list 'load-path "~/.opam/4.08.0/share/emacs/site-lisp")
(require 'ocp-indent)

;; merlin
(let ((opam-share (ignore-errors (car (process-lines "opam" "config" "var" "share")))))
  (when (and opam-share (file-directory-p opam-share))
    (add-to-list 'load-path (expand-file-name "emacs/site-lisp" opam-share))
    (autoload 'merlin-mode "merlin" nil t nil)
    (add-hook 'tuareg-mode-hook 'merlin-mode t)
    (add-hook 'caml-mode-hook 'merlin-mode t)
    (setq merlin-command 'opam)))

Standard What?

There is some fragmentation in the OCaml ecosystem when it comes to things like what standard library to use. There are a few alternatives. Initially I would advise to stick to the original standard library. This distraction is really not needed when the core language is unfamiliar.

Published: 2019-07-10