Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 40 ยท Getting Started

What is OCaml?

Meet OCaml: a fast, statically typed functional language with type inference, pattern matching and a powerful module system. Learn where it comes from and why people use it.

OCaml (pronounced "oh-CAM-ul") is a general-purpose programming language that mixes functional, imperative and object-oriented styles, with the functional style at the center. It is statically typed: the compiler checks your whole program before it runs, and it rejects whole classes of mistakes, such as adding a number to a string or forgetting to handle a case. Yet you rarely write type annotations, because the compiler works out the types for you.

This tutorial teaches modern OCaml (version 5.x) from the first line to complete projects. You do not need to know any functional programming already. If you have written code in any language, you can follow along.

OCaml is not installed on this website. The OCaml code blocks in this tutorial are read-only, and the outputs shown are what the real compiler or toplevel prints. Where a live demo helps, we add a small, clearly labelled JavaScript stand-in that you can run and edit.

A short history

OCaml belongs to the ML family of languages (ML stands for "meta language"). The ML idea started in the 1970s, in a theorem-proving system, and gave the world type inference and algebraic data types. The Caml language was developed at INRIA, the French national research institute, and gained an object system in 1996, which led to the name Objective Caml. In 2011 the name was shortened to OCaml. Since OCaml 5.0 (2022) the runtime supports shared-memory parallelism through "domains" and, later, effect handlers.

What makes OCaml different

Why people choose OCaml

Static types Many bugs die at compile time
Type inference Types without the typing
Pattern matching Take data apart safely
Fast native code Compiles to machine code

Four ideas you will meet in every lesson.

1 / 2

Static typing with inference

Every expression in OCaml has a type, and the compiler knows it. You can write a function without saying anything about types, and the compiler still works them out:

OCAML
let double x = x * 2
(* val double : int -> int = <fun> *)

Because * works on integers, the compiler concludes that x is an int and that the result is an int. You will study this in Type inference.

Functional first

Functions are ordinary values. You can pass them to other functions, return them, and store them in lists. Values are immutable by default: let x = 5 binds the name x to 5 and never changes it. Loops exist, but most OCaml code uses recursion and functions such as List.map instead.

Pattern matching and variants

You describe your own data with variant types and take it apart with match. The compiler warns you if you forget a case. Variants and matching get their own chapter later in this tutorial.

Modules

A module is a named collection of types and values. Modules can have interfaces that hide details, and functors are functions from modules to modules. This system is what lets large OCaml code bases stay tidy.

Speed and safety

The ocamlopt compiler produces fast native code, usually in the same league as Java or Go for ordinary programs, and it has a quick compile step. The runtime has a garbage collector, so you never free memory by hand, and out-of-bounds array access raises an exception instead of corrupting memory.

Where OCaml is used

OCaml is popular wherever correctness matters: compilers and language tools (the Rocq prover, the Flow type checker, the original Rust compiler was written in it), static analysis, finance (Jane Street builds its trading systems in OCaml), and developer infrastructure. It is also a favorite teaching language in universities for the same reason: it makes you think clearly about data and functions.

A first look at the syntax

Here is a tiny complete program. Do not worry about the details yet:

OCAML
let greet name = "Hello, " ^ name ^ "!"

let () = print_endline (greet "OCaml")

The first line defines a function greet; ^ joins strings. The second line runs the program: let () = ... is the usual way to say "do this when the file runs". It prints Hello, OCaml!.

A JavaScript stand-in: strict number types

OCaml refuses to mix int and float. The + operator is for integers and +. is for floats. This stand-in mimics that strictness so you can feel what the compiler is protecting you from:

JS Stand-in: strict int and float operators

Press Run (or Ctrl + Enter) to see the console output here.

No output.

In real OCaml the error appears at compile time, before your program starts. Here we can only simulate it at run time.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which description fits OCaml best?

    OCaml checks types at compile time and infers most of them, with functions as first-class values.
  2. 2. Which family of languages does OCaml belong to?

    OCaml is a member of the ML family, which introduced Hindley-Milner type inference and algebraic data types.
  3. 3. What happens when you write let x = 5 in OCaml?

    A let binding gives a name to a value. Values are immutable by default.
  4. 4. Who frees memory in an OCaml program?

    OCaml has an automatic garbage collector, so you never free memory by hand.

Summary

  • OCaml is a fast, statically typed, functional-first language from the ML family, developed at INRIA.
  • Types are inferred, so code stays short but is still checked at compile time.
  • Immutability, pattern matching, variants and modules are the core ideas of the language.
  • OCaml 5 adds multicore parallelism and effect handlers.

Next: Installing OCaml