Fast and reliable programs with OCaml

Type Checking

When trying the new ask function,

let () =
  match ask "Automatic?" with
  | `yes -> automatic ()
  | `no -> manual ()

OCaml immediately spots a potential problem:

Error: This pattern matches values of type
[< `no | `yes ]
but a pattern was expected which matches values of type
[> `abort | `no | `yes ]
The first variant type does not allow tag(s) `abort

I haven't manually specified any types, but OCaml has inferred a type for ask automatically: It may return `abort, `no, or `yes. If you try to match on the result, OCaml complains that you don't handle one of the possible responses. Instead of adding the missing third case to the match block, I'll look at another useful feature (Listing 2).

Listing 2

yes_no Function

 

The yes_no function asks for a response using ask. If the response is `yes or `no, it is returned, but `abort exits the program. OCaml automatically infers that yes_no may return only `yes or `no. This ability to handle subsets (and supersets) of variant types, verified statically at compile time, can be very useful.

Running OCaml Code

OCaml comes with an interpreter, which can be used to run code directly and provides an interactive top level, as well as compilers for bytecode and native code. To run an example in this article, save it to a file (e.g., prog.ml) and run:

ocaml prog.ml

The easiest way to compile OCaml code is to use the ocamlbuild command by placing the source file (e.g., prog.ml) in a new directory and running:

ocamlbuild prog.native

to build a native binary. It's also a good idea to enable all warnings, which you can do by adding a file called _tags (note the leading underscore) containing:

true: warn(A), strict_sequence

The true means that this applies to all files in the project; warn(A) turns on all compiler warnings, and strict_sequence requires that the result of every expression (unless unit) be used.

Each .ml file you create is a separate OCaml module, and you can refer to something in another module using the notation Module.function. Note that module names are always capitalized in the source, even though the file name is lower case!

If your main file depends on other modules in the source directory, ocamlbuild will (re)build them automatically as needed.

As your program gets larger, OCaml's support for packages, classes, modules, immutable structures, and abstract types will help keep your code maintainable.

Packages

OCaml packages are most easily installed using OPAM [2]. For example, to get the lablgtk GTK bindings, enter:

$ opam install lablgtk

and edit your _tags file to make your project depend on it by adding

package(lablgtk2, lablgtk2.auto-init)

to the list of tags on the first line.

When using packages, you will need to build with the following:

ocamlbuild -use-ocamlfind

because, for historical reasons, support for packages is turned off by default in ocamlbuild.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Batsh

    Batsh kills two birds with one stone: Programs written in this language can be compiled both as Linux Bash scripts and Windows batch files.

  • Helma

    The powerful Helma application server brings new powers to the JavaScript language. We'll show you how to use Helma to build a simple RSS reader.

  • File Comparison

    With support for more than 60 file formats, diffoscope extends the power of diff beyond the plain text or HTML file.

  • Highlight Learns F#

    The new version of Highlight, the software that highlights numerous program codes in color, supports amongst other languages, F#.

  • Let's Go!

    Released back in 2012, Go flew under the radar for a long time until showcase projects such as Docker pushed its popularity. Today, Go has become the language of choice of many system programmers.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News