Package base v0.12.0
Base is a standard library for OCaml. It provides a standard set of general-purpose modules that are well tested, performant, and fully portable across any environment that can run OCaml code.
Unlike other standard library projects, Base is meant to be used as a wholesale replacement of the standard library distributed with the OCaml compiler. In particular, it makes different choices and doesn't re-export features that are not fully portable such as I/O, which are left to other libraries.
The full API is browsable here.
Note that an API for OCaml's channel-based I/O can be found in the Stdio
library.
This package also ships an extra library: base.md5.
Relationship to Core_kernel and Core
Base is the smallest, most self-contained version of Jane Street's family of three standard library replacements. It is extended by Core_kernel
, which is in turn extended by Core
.
In sum:
- Base: Minimal stdlib replacement. Portable and lightweight and intended to be highly stable.
- Core_kernel: Extension of Base. More fully featured, with more code and dependencies, and APIs that evolve more quickly. Portable, and works on Javascript.
- Core: Core_kernel extended with UNIX APIs.
Using the OCaml standard library with Base
Base is intended as a full stdlib replacement. As a result, after an open Base
, all the modules, values, types, etc., coming from the OCaml standard library that one normally gets in the default environment are deprecated.
In order to access these values, one must use the Caml
library, which re-exports them all through the toplevel name Caml
: Caml.String
, Caml.print_string
, ...
The new modules and values made available by Base are documented here.
Differences between Base and the OCaml standard library
Programmers who are used to the OCaml standard library should read through this section to understand major differences between the two libraries that one should be aware of when switching to Base.
Comparison operators
The comparison operators exposed by the OCaml standard library are polymorphic:
val compare : 'a -> 'a -> int
val ( <= ) : 'a -> 'a -> bool
(* ... *)
What they implement is structural comparison of the runtime representation of values. Since these are often error-prone, i.e., they don't correspond to what the user expects, they are not exposed directly by Base.
To use polymorphic comparison with Base, one should use the Poly
module. The default comparison operators exposed by Base are the integer ones, just like the default arithmetic operators are the integer ones.
The recommended way to compare arbitrary complex data structures is to use the specific compare
functions. For instance:
List.compare String.compare x y
The ppx_compare
rewriter offers an alternative way to write this:
[%compare: string list] x y
Roadmap
Base is still under active development and there are several missing feature that are yet to be added. Consult the roadmap to see what is happening.