- Courses
- 💻Programming Languages
- 🐫OCaml (WIP)
- Modules, Structure & Tooling
- Module Aliasing
On this page
OCaml Module Aliasing Explained
Published on August 3, 2026
By Enrico Hochkoeppler
Web Developer & TutoriaLibre Founder
module N = ExistingModule creates an alias: a new, often shorter, name that refers to the exact same module. No new structure is created and no code is duplicated — N simply becomes another valid path to reach ExistingModule's existing contents, resolved entirely at compile time.
The Basic Syntax
module L = List
# L.length [1; 2; 3];;
- : int = 3
L.length and List.length refer to the exact same function. Aliasing carries no runtime cost at all: the compiler resolves an alias at compile time, so L compiles down to nothing more than a different name for code that already exists, rather than any kind of wrapper or indirection layer sitting between the alias and the original module.
Shortening Long or Deeply Nested Paths
The most common motivation for aliasing is cutting down repeated references to a long or deeply nested module path:
module Assoc = Base.List.Assoc
let lookup key alist = Assoc.find alist key ~equal:String.equal
Rather than writing Base.List.Assoc.find at every call site, the alias Assoc gives a short, local name to reach it — without needing to open anything. Every reference still requires the Assoc. qualifier, exactly as it would with the full path, so it stays immediately obvious at each call site which module a given function actually came from, even though the path used to reach it is now considerably shorter.
Resolving Naming Collisions
Aliasing is also the standard way to work with two modules that both use a common, generic type or value name. Since most well-designed OCaml modules name their primary type t, having two such modules in scope unqualified would make t ambiguous, or force one to silently shadow the other. Aliasing sidesteps the problem entirely, since neither module ever needs to be opened:
module Cel = Celsius
module Fah = Fahrenheit
let to_fahrenheit (c : Cel.t) : Fah.t = ...
Here, Cel.t and Fah.t are unambiguous at every point they're used. Each type is reached only through its own short, distinct alias, so there's never a moment where it's unclear which module's t is being referred to.
Local Aliases: Scoping the Shorthand
An alias doesn't need to apply to an entire file — it can be scoped to a single function or expression using a local let module, keeping the shorthand contained to exactly where it's useful:
let describe_error e =
let module V = Validation.Errors in
match e with
| V.Empty -> "empty"
| V.Too_long n -> Printf.sprintf "too long (max %d)" n
Outside of describe_error, the name V doesn't exist at all — the alias is entirely local to this one function. This is generally the safer default for short, generic alias names in particular, since a name like V scoped to a whole file runs a real risk of accidentally colliding with something else defined later in that same file, while a local alias can't leak into code it was never meant to affect.
Aliasing Inside a Module Signature
Aliases aren't limited to implementation code — a .mli file can alias a module too, which is useful for re-exporting a module under a different or more convenient name as part of a library's public interface:
(* mylib.mli *)
module Utils = Internal_string_utils
Consumers of Mylib then see Mylib.Utils as the public name for functionality that's actually implemented in a differently, perhaps less publicly meaningful, named module internally — the alias becomes part of the library's intentional, curated public surface, independent of whatever names were most convenient while writing the implementation.
Why Aliasing Is Not the Same as open or include
It's worth being precise about what aliasing does not do, since it's easy to conflate with two related constructs that look similar on the surface but behave quite differently:
- Alias (
module N = M) gives an existing module a new path. Every reference still needs theN.qualifier — nothing aboutM's contents becomes unqualified as a result. - open (
open M) bringsM's contents into scope unqualified, without creating any new name forMitself or changing what module those definitions actually belong to. - include (
include M) copiesM's contents directly into the including module's own definition, meaning they become part of that module's own exported signature too, not merely locally visible while writing it.
Aliasing is the right tool specifically when the goal is a shorter or clearer path to something, while still keeping every reference to it explicitly qualified and easy to trace back to its origin — reaching for open or include instead would solve the verbosity problem differently, at the cost of qualification disappearing entirely rather than merely getting shorter.
Key Takeaways
module N = ExistingModulecreates a zero-cost alias — a new name for the same module, resolved entirely at compile time, with no duplication or runtime overhead.- Aliasing is the standard way to shorten long or deeply nested module paths that get referenced repeatedly, while keeping every reference explicitly qualified.
- It's also the standard fix for naming collisions between modules that share a common type or value name like
t, since it avoids ever needing to open either one. - A local
let modulescopes an alias to a single function or expression, which is the safer default for short, generic alias names. - A
.mlifile can alias a module too, which is useful for re-exporting internal functionality under a curated public name. - Aliasing,
open, andincludeare three distinct operations: aliasing gives a new qualified path,openremoves the need for qualification without changing ownership, andincludecopies a module's contents into the including module's own signature.
Found this helpful? Share the knowledge...
... or support this project to keep it alive!
Previous
Nested Modules
Next
Dune Deep Dive
- The Basic Syntax
- Shortening Long or Deeply Nested Paths
- Resolving Naming Collisions
- Local Aliases: Scoping the Shorthand
- Aliasing Inside a Module Signature
- Why Aliasing Is Not the Same as open or include
- Key Takeaways
