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
(incl. VAT)
Buy Linux Magazine
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.
News
-
So Long Neofetch and Thanks for the Info
Today is a day that every Linux user who enjoys bragging about their system(s) will mourn, as Neofetch has come to an end.
-
Ubuntu 24.04 Comes with a “Flaw"
If you're thinking you might want to upgrade from your current Ubuntu release to the latest, there's something you might want to consider before doing so.
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.