CIS 500: SEAS OCaml Tutorial (Work in Progress)

Prerequisites

This tutorial assumes that you have a working knowledge of Unix.

Getting Started

First you will want to obtain a terminal session on a SEAS Unix system. Some of you may have such a system on your desk, or can find one in one of the SEAS Computer Labs. On one of these computers, there will generally be some menu option that will allow you to open a new terminal window in which to work.

If you do not have immediate access to such a computer, the next option is connect to one of the SEAS Unix servers. The best way to do this is using the Secure Shell Protocol (SSH). A good way to do this from a Windows system is to use PuTTY or SecureCRT. SecureCRT should be installed on SEAS Windows Systems. PuTTY requires no installation, so can be easily downloaded and used from most Windows computers. MacOS X and most Unix systems have a SSH client installed which can be invoked from a local terminal. On MacOS X, the Terminal application can be found in the Utilies folder within the Applications folder. The system you will want to connect to is called eniac.seas.upenn.edu. This is a Solaris cluster. Some of you may also have accounts on halfdome.cis.upenn.edu, a Linux cluster, which also has OCaml installed.

The last option is to compile OCaml on your own computer. Binary packages are available for many common platforms, and if you have any experience with compiling software packages, it generally builds quite easily out-of-the-box. If you can do this, the rest of this tutorial may not be relevant to you.

Making sure OCaml is in your PATH

Once you have a terminal seasion open on an appropriate system, the first thing to check is whether OCaml is already in your executable search path. You can test this by typing ocaml at the command prompt. If you do, you will see something like:

% ocaml
        Objective Caml version 3.06

# 

This is the OCaml interactive toplevel. Otherwise, you will most likely get a message something like:

% ocaml
command not found: ocaml
%

In the latter case you will need to extend your PATH environment variable. If you are using a C-Shell variant (csh or tcsh) as your environment shell you will want to type the following at the command line:

% setenv PATH $PATH:/usr/local/bin

If you are using a Bourne or Korn shell (ash, ash, bash, ksh, sh, zsh, etc.) you will want to type the following:

% export PATH=$PATH:/usr/local/bin

If you do not know what shell you are using, it is generally the first process listed when you run ps at the command line:

% ps
  PID TTY          TIME CMD
29124 pts/7    00:00:00 zsh
29143 pts/7    00:00:00 ps

For example, the user above is running zsh.

Using the OCaml Interactive Toplevel

Once you have reached the point where you can reach the OCaml interactive toplevel, you are ready to do some simple programming. For example you can perform some arithmetic:

# 1 + 1;;
- : int = 2
# 

Or bind values to variables:

# let x = "foo";;
val x : string = "foo"
# 

You can read in the contents of a file containing OCaml code using the following directive:

# #use "myfile.ml";;

Finally, you can exit the toplevel by hitting Control-D or typing the following:

# #quit;;
%

See the OCaml tutorial handed out in class or available online for more information about where to go from here.

Working with OCaml Code in GNU emacs or XEmacs

If you use the GNU emacs or XEmacs editors on a SEAS Unix system, you can add configure it for editing OCaml code by adding the following lines to your .emacs file:

; Add tuareg-mode elisp to your load path
(setq load-path (cons "/home10/c/cis500/elisp" load-path))

; tuareg-mode configuration
(setq auto-mode-alist (cons '("\\.ml\\w?" . tuareg-mode) auto-mode-alist))
(autoload 'tuareg-mode "tuareg" "Major mode for editing Caml code" t)
(autoload 'camldebug "camldebug" "Run the Caml debugger" t)

(if (and (boundp 'window-system) window-system)
    (when (string-match "XEmacs" emacs-version)
        (if (not (and (boundp 'mule-x-win-initted) mule-x-win-initted))
            (require 'sym-lock))
        (require 'font-lock)))

Next time you start GNU emacs or XEmacs and open a file that ends in .ml or .mli it will activate tuareg-mode. This will provide support for syntax hilighting and indentation. Additionally, it will add a new pull-down menu labeled "Tuareg" that will provide a number of useful features. For example, under the "Interactive Mode" submenu, there is an item "Run Caml Top Level" that will create a new window in which to interactive with the OCaml toplevel. This is particularly useful because it provides command history among other features. Once you have a top level running, it also provides menu items to evaluate a buffer of OCaml code or a selected code region.

Working with OCaml Code in vim

If you use the vim editor (installed in /usr/bin on most SEAS Systems), it should come with support for OCaml syntax hilighting by default. This can be enabled by typing :syntax enable. If you are using vim from a terminal emulator that supports color, be sure that you have the environment variable TERM set to something like ansi or xterm-color in order to get color hilighting.

OCaml is also supported by the vim quickfix compilation/error reporting system.