#+TITLE: OCaml Guide: Getting Started
#+AUTHOR: Taylor Venable
#+OPTIONS: H:6 toc:2
* Install an OCaml System
** Linux Distribution
*** Arch Linux
Arch has packages for OCaml, and many supporting libraries are in the AUR.
** Using GODI
* The Toplevel
OCaml has three modes of operation: interpreting code using the toplevel, byte compiling code, and
compiling code to native binaries. The toplevel is like the REPL in Lisp or Scheme, or like
~ipython~ or ~irb~ for Python and Ruby. It lets you load code and evaluate expressions in an
interactive way. Lots of our experimentation will be done using the toplevel, because it's quick and
easy to test things out without getting bogged down in the difficult task of compilation. You can
start the toplevel with the ~ocaml~ command.
Objective Caml version 3.12.1
The octothorpe (~#~) is the OCaml prompt. We'll show it in our examples to indicate what you type
versus what the toplevel will print. Some special directives start with ~#~, so don't confuse those
with the prompt. Let's get a feel for what we can do.
# 1 + 2 ;;
- : int = 3
# print_string "hello, ocaml\n" ;;
- : unit = ()
# let f = fun (name) -> "What's hot, " ^ name ^ "?" ;;
val f : string -> string = <fun>
# f "DJ Roomba" ;;
- : string = "What's hot, DJ Roomba?"
- : string -> unit = <fun>
- : string array = [|"/usr/bin/ocaml"|]
- : ('a, Format.formatter, unit) format -> 'a = <fun>
Expressions end in double semi-colons (say what you want about their aesthetic value, they make it
very obvious when statements end). Whenever we end an expression and hit enter, the toplevel prints
out a description of the value of the expression. The toplevel prints the variable's name, followed
by a colon and then the variable's type, and finally the value the variable currently holds. If we
don't give a name for the variable then the toplevel prints a minus sign, which is commonly read as
"it." However, in some cases we make an assignment using a ~let~ expression to a lexical variable,
the name of which the toplevel prints.
After the variable's type, the toplevel prints an equals sign and the value. If the value is
something complicated, like a function, then a highly simplified version is printed. Functions, for
example, aren't printed out like you would enter them in. But other things, such as numbers,
strings, lists, and arrays, are printed exactly like you would type them in source code.
Note that since functions are values just like everything else in OCaml, if you want to know the
type of any function that is already defined in the toplevel, you can just enter it. In the example
above, typing in the name of the ~print_string~ function shows you that it is a function from string
to unit. This may not mean much now, but later when we talk about types, it will become very useful
to be able to find the type of an existing function.
# 1 + 2.5 ;;
Error: This expression has type float but an expression was expected of type
Here's an example of an error you'll see a lot. When the toplevel finds an error in something you've
typed in, it tries to indicate where the error occurred. In the terminal, you will probably see the
bad part underlined, but we'll depict it with carets. The error message is telling you that "~2.5~"
is a float, but the system was expecting an int.
Now let's do something useful with the toplevel. If you have ~findlib~ installed, you can do this:
# #use "topfind" ;;
- : unit = ()
Findlib has been successfully loaded. Additional directives:
#require "package";; to load a package
#list;; to list the available packages
#camlp4o;; to load camlp4 (standard syntax)
#camlp4r;; to load camlp4 (revised syntax)
#predicates "p,q,...";; to set these predicates
Topfind.reset();; to force that packages will be reloaded
#thread;; to enable threads
- : unit = ()
# #list ;;
bigarray (version: [distributed with Ocaml])
camlp4 (version: [distributed with Ocaml])
dbm (version: [distributed with Ocaml])
dynlink (version: [distributed with Ocaml])
graphics (version: [distributed with Ocaml])
labltk (version: [distributed with Ocaml])
num (version: [distributed with Ocaml])
num.core (version: [internal])
ocamlbuild (version: [distributed with Ocaml])
stdlib (version: [distributed with Ocaml])
str (version: [distributed with Ocaml])
threads (version: [distributed with Ocaml])
threads.posix (version: [internal])
threads.vm (version: [internal])
unix (version: [distributed with Ocaml])
This loads ~findlib~ and enables some additional directives in the toplevel. The ~#list~ directive
shows which packages are available. Your output will undoubtedly have many more packages listed, as
I've removed most of the output.