Beginner’s OCaml setup on Mac

FP is beautifil. Don’t ask why, just take as given that in my text it is so. Otherwise, why in the world would I write about it?

Haskell is beautiful as well. It’s pure FP, strong and it is a Ph.D.’s language. After almost 20 years of knowing it I still learn it. Good read.

The only bad thing about FP and Haskell is myself. I have a brain that’s quite of an engineer kind. I love doing things rather than reason about heavy math underneath.

That’s mainly why back in mid-2000s when I had to write something able to work on a low-budget hardware I chose OCaml. Not only because it had a moderate memory footprint and good native-code performance. It also was a reasonably writable language for me (the language can both forgive you lack of Ph.D. degree and teach you rather good functional style). The docs, the runtime, the stdlib and third-party libs API and tools were so good that I could start writing a production software after a week of reading and excercising.

Enough OCaml praising for now. It is praising itself good enough without my poor propaganda. I’ve written a thing in it and that thing made me some money for some years. Then the money chose Ruby and webdev so I followed that direction.

Recently, I decided to return to my FP musings and remembered that good old programming language. Of course I still love Haskell but… Haskell holds me in a friendzone because I’m still not a Ph.D. So I bought “Real World OCaml” (the book’s free web version is also googlable) and launched a console. Well, looks like things in OCaml world got fancier since my mid-2000s experiments. Now we have opam for managing the dependencies, utop the toplevel (quite a hipster thing) and better Emacs support. These things we’ll use to setup our OCaml student environment.

Here’s my short note of how to setup the beginner’s OCaml programming environment on a mac. Mine is mid-2012 MacBook Pro with Sierra onboard.

Setting up the tools

First, let’s make a dir where we will experiment. I have that old Unix admin’s superstition that it’s better to do everything standing in the right place. BTW I use fish shell, so beware.

➜  work mkdir ocaml-tutorial; cd ocaml-tutorial
➜ ocaml-tutorial

Now, let’s install the language and it’s package manager, the opam.

➜  ocaml-tutorial brew install ocaml opam

NB: this command will also install ocamlbuild tool. We’ll meet this name later. Upon installing brew will give some usage hints, of which these are most useful:

OPAM uses ~/.opam by default for its package database, so you need to
initialize it first by running (as a normal user):
$  opam init
Run the following to initialize your environment variables:
$  eval `opam config env`

Ok, nailed it.

What a hipster thingy :) I like those little camels. Truely it’s as friendly as those famous “Don’t panic” thing. Here I found the bad thing: somehow the fish scripts shipped with opam are breaking fish bootup so if you use fish you better just go to your ~/.config/fish/config.fish, delete “OPAM section” and paste there the contents of ~/.opam/opam-init/variables.fish. Also, if you’ve got fish 2.3.1 you’d better remove all quotes and semicolons from that text. Finally these worked for me:

# OPAM configuration
set -gx PATH /Users/paul/.opam/system/bin $PATH
set -gx OCAML_TOPLEVEL_PATH /Users/paul/.opam/system/lib/toplevel
set -gx PERL5LIB /Users/paul/.opam/system/lib/perl5:$PERL5LIB
set -gx MANPATH $MANPATH /Users/paul/.opam/system/man
set -gx OPAMUTF8MSGS 1
set -gx CAML_LD_LIBRARY_PATH /Users/paul/.opam/system/lib/stublibs:/usr/local/lib/ocaml/stublibs

Here I restart my console to set opam environment vars.

Now, let’s install utop, the OCaml’s REPL. It’ll serve a toplevel for “let’s write a tiny program” experiments. Also, the book uses it to test the examples.

Also, it’s wise to opam install core because it’s The Good Thing to continue with OCaml these days. Back in 2000s I didn’t have it and that was a problem ;)

Now we can start it and experiment already! “Real World OCaml” is the thing better to use from now on. There you find code snippets and play with them in here:

Kinda cyberhipsterish GUI but well, it’s amusing.

Emacs setup

Now, and last thing for today, we add some setup Emacs to use our ocaml/opam/utop setup in a good old editor (at least I think so).

First, install tuareg and utop packages. The first will handle OCaml mechanics, the second will run for you utop in separate Emacs buffer. Then, add these two lines in Emacs’ local config:

(autoload 'utop-minor-mode "utop" "Minor mode for utop" t)
(add-hook 'tuareg-mode-hook 'utop-minor-mode)

For further info on tuareg and utop Emacs packages please google up their docs on Github. As for me, I use Bozhidar Batsov’s prelude package (google up bbatsov/prelude) — it sets up the whole thing including ocaml packages very comfortably.

Now, we start Emacs, open an *.ml file and doC-c C-s note this thing at the bottom:

Here I show what you should type there. There’s a setting to nail it down; it’s described in utop package’s docs on Github.

Finally we have something like this:

Wrapping up

Well, we’ve just:

  • returned back to the wonderful world of FP with OCaml
  • found a book to read about how to do things here
  • installed OCaml and its very basic tools
  • set up Emacs to help us in further experiments

Happy OCamling.