OCaml Resources


OCaml WWW Site

The OCaml WWW site (www.ocaml.org) contains the OCaml distribution, including pre-compiled binaries for some platforms. It also contains documentation about OCaml, and various other resources.

See the OCaml Manual for definitive information about the OCaml language and libraries.

Running the OCaml Interative Top-level

To invoke the OCaml interactive top-level, run the ocaml command from the shell. Ocaml's prompt is #.

To get ocaml to process your input, you must type ";;" (two semicolons).

To exit ocaml type

  #quit;;
or simply type CTRL-d. To load a file file into ocaml, as if you had typed the file's contents directly, type
  #use "file";;

OCaml Mode for Emacs

The best way to edit OCaml programs, and to interact with the OCaml compiler, is by using an OCaml Mode for the Emacs text editor. Although one such mode comes with the OCaml distribution, a nicer OCaml Mode, called Tuareg, is also available from the OCaml WWW site.

If you want to use the Tuareg OCaml mode, put the following code in your .emacs file:

  (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)

  (require 'font-lock)

This file should be located in your home directory; if it doesn't already exist, create it.

To run ocaml as a child process of Emacs, one types

  META-x tuareg-run-caml RET

Editing a source file whose name ends with .ml (or .mli) causes Emacs to enter the major mode Tuareg, at which point typing

  CTRL-h m

(or META-x describe-mode RET) will cause a summary of the mode to be printed. One can send the contents of the source file to ocaml by typing

  CTRL-c CTRL-b

If ocaml reports that the source file contains errors, then typing

  CTRL-c `

will cause the cursor of the ocaml buffer to be positioned at the first error message, and cause the cursor of the source file to be placed at the location of the error itself. The next error can then be examined by typing CTRL-c ` again, and so on.

If you don't like Tuareg's automatic formatting, you can put the following text in your .emacs file:

  (defun my-tuareg-mode-hook ()
    (fset 'indent-for-tab-command 'tab-to-tab-stop) ;; turn off automatic
                                                    ;; indentation
    (setq tuareg-use-abbrev-mode nil)
    (setq tuareg-electric-indent nil)
    (setq tuareg-electric-close-vector nil))
  
  (add-hook 'tuareg-mode-hook 'my-tuareg-mode-hook)

OCaml on the CIS Linux System

OCaml is already installed on the CIS Linux system. Furthermore, the Tuareg OCaml Emacs mode is also already available on that system.


Alley Stoughton (stough@cis.ksu.edu)
Valid XHTML 1.0!   Valid CSS!