A0: Intro. to OCaml

Due: Tuesday, 1/19

OCaml is a functional programming language in the ML family. The purpose of this assignment is to set you up with a working OCaml environment (either on your own machine or in one of the labs in Stocker) and to introduce the fundamentals of programming in OCaml.

In general, functional programming languages like OCaml are characterized by their support for

  • higher-order functions: functions can be passed to, and returned from, other functions just like other values (ints, bools, etc.)
  • algebraic data types and pattern-matching — the programmer can define and compute on his or her own new data types in a type-safe way
  • strong module systems that encapsulate program components via interfaces
These features together make OCaml an excellent medium for writing compilers, as I hope to convince you in this course.

Part 1: Install OCaml

For consistency in the code we distribute for assignments and when grading, we'll be using a specific version of OCaml in this course, version 4.02.1. You have three options here:
  1. Use VirtualBox together with the CS4100 VM, which comes with all necessary software pre-installed;
  2. Install OCaml (and the associated libraries) on your own machine following the "Laptop Instructions" below; or
  3. Complete this and later assignments on the lab machines in 107 and 307, by following the "Lab Machine Instructions."

1. VirtualBox

First, install the VirtualBox client appropriate for your machine.

Then, download the CS4100 VM (about 2.5Gb).

(UPDATE 1/18/16: At least one student had problems with the VM above. If you fall into this category, you may have better luck with the Xfce version of the CS4100 VM.)

Now fire up VirtualBox and import the VM by doing "File->Import Appliance". The OS should log in automatically. If you are ever prompted for username or password, use "cs4100" (username) and "cs4100" (password).

Once you've completed the steps above, make sure everything works by jumping to Making Sure Everything Works below.

2. Laptop Instructions

The easiest way to install OCaml on your own machine is to first install OPAM, the OCaml package manager, and then use OPAM to "switch" to the right version of OCaml. Install OPAM by following the directions here: http://opam.ocaml.org/doc/Install.html.

If you're running Windows, follow the instructions here: http://protz.github.io/ocaml-installer/.

Open a terminal and do

  opam init
If you're prompted ("Do you want OPAM to modify ~/.profile..."), type "y" and then hit enter.

Now type

  eval `opam config env`

Then do

  opam switch 4.02.1
and follow the prompts to switch to OCaml version 4.02.1

We'll be using a number of libraries to develop the compiler later on in this course. To install these, do:

  opam install ocamlbuild
  opam install ocamlfind
  opam install batteries
  opam install menhir
If you're prompted, just hit "Y" then enter or just enter.

To test that all this worked, follow the insructions in Making Sure Everything Works below.

3. Lab Machine Instructions

After logging in to one of the Solaris machines in either 107 or 307, open a terminal and type

  opam init
Now wait...and keep waiting for quite a while. For some reason related to how the networked file system is set up in the labs, this command takes forever!

At the prompt ("Do you want OPAM to modify ~/.profile..."), type "y" and then hit enter.

Now type

  eval `opam config env`
After you've initialized and configured OPAM, you need to install Batteries, an enhanced standard library for OCaml. To do so, type
  opam install batteries
Hit "Y" if prompted. The final library you'll need (for assignments 3 and beyond) is Menhir, a parser generator for OCaml. To install it, type
  opam install menhir

To test that all this worked, follow the insructions in Making Sure Everything Works below.

Making Sure Everything Works

To test that the above steps were successful, try typing at a terminal:
  $ ocamlfind batteries/ocaml
If everything worked, you'll be greeted with a screen that looks like:
        OCaml version 4.02.1

      _____________________________
    [| +   | |   Batteries 2.4  - |
     |_____|_|____________________|
      _____________________________
     | -  Type '#help;;'    | | + |]
     |______________________|_|___|

# 
This is the OCaml toplevel — for interactively executing OCaml code — loaded with the OCaml Batteries Included standard library. At the prompt, try typing something like:
# 1+1;;
You should be greeted with a response like:
- : int = 2
To exit the toplevel, type Ctl^D.

Part 2: Complete the Exercises in intro.ml