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
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.
-
New KDE Slimbook Plasma Available for Preorder
Powered by an AMD Ryzen CPU, the latest KDE Slimbook laptop is powerful enough for local AI tasks.
-
Rhino Linux Announces Latest "Quick Update"
If you prefer your Linux distribution to be of the rolling type, Rhino Linux delivers a beautiful and reliable experience.
-
Plasma Desktop Will Soon Ask for Donations
The next iteration of Plasma has reached the soft feature freeze for the 6.2 version and includes a feature that could be divisive.
-
Linux Market Share Hits New High
For the first time, the Linux market share has reached a new high for desktops, and the trend looks like it will continue.
-
LibreOffice 24.8 Delivers New Features
LibreOffice is often considered the de facto standard office suite for the Linux operating system.
-
Deepin 23 Offers Wayland Support and New AI Tool
Deepin has been considered one of the most beautiful desktop operating systems for a long time and the arrival of version 23 has bolstered that reputation.
-
CachyOS Adds Support for System76's COSMIC Desktop
The August 2024 release of CachyOS includes support for the COSMIC desktop as well as some important bits for video.