Skip to content

Console ​

Romain CasatiOctober 17, 2026· in OCaml, Notebook, Console, New modules· 2 min read

OCaml libraries with #require ​

Basthon's OCaml kernel can now load libraries, with the #require directive of the OCaml toplevel, as you would with a library installed by opam:

ocaml
#require "zarith";;
Z.(~$2 ** 200);;

Libraries are only downloaded at the first #require, and are already compiled to JavaScript: they load almost instantly. Here are those available:

LibraryTo…
visuvisualize memory, recursive calls, trees and graphs (Basthon.Visu)
graphvizdraw graphs described in DOT (Basthon.Graphviz)
ocamlgraphgraphs and their algorithms (Graph module)
zarithbig integers and rationals (Z and Q modules)
containers, containers-datapriority queues, deques, dynamic arrays…
yojson, csvread and write JSON and CSV data

The libraries specific to Basthon are put in the Basthon module: after #require "visu", write for instance module Visu = Basthon.Visu. Basthon.help () lists them all. By the way, the Basthon module gains display_svg and display_html to show an SVG image or HTML.

Each one has its example notebook in the gallery, presented in the following posts.

See the gallery

Loading libraries and the help of the Basthon module


Romain CasatiOctober 17, 2026· in OCaml, Notebook, Console, New modules· 1 min read

Priority queues, deques… in OCaml with containers ​

OCaml's standard library has no priority queue, no double-ended queue and no dynamic array. In Basthon, the containers library fills this gap:

ocaml
#require "containers-data";;

module Heap = CCHeap.Make (struct type t = int let leq = ( <= ) end);;
Heap.to_list_sorted (Heap.of_list [ 5; 3; 8; 1 ]);;

let d = CCDeque.of_list [ 2; 3 ];;
CCDeque.push_front d 1;;

It notably provides heaps (CCHeap), deques (CCDeque), persistent queues (CCFQueue), dynamic arrays (CCVector), bit vectors (CCBV), as well as many extensions of the standard library (CCList, CCString…). The example notebook uses them for heap sort, Dijkstra's algorithm, a breadth-first search or the sieve of Eratosthenes.

Play with it!

A deque of containers


Romain CasatiOctober 17, 2026· in OCaml, Notebook, Console, New modules· 1 min read

Reading CSV and JSON data in OCaml ​

To work in OCaml on real data (spreadsheet exports, open data…), Basthon offers two libraries: csv and yojson.

ocaml
#require "csv";;
#require "yojson";;

let rows = Csv.Rows.load ~has_header:true "students.csv";;
List.map (fun r -> Csv.Row.find r "name") rows;;

let json = Yojson.Safe.from_string {|{"name": "Ada", "marks": [15, 18]}|};;
Yojson.Safe.Util.(json |> member "marks" |> to_list);;

A file opened in Basthon (or attached to the notebook) is read directly, and a file produced is downloaded with Basthon.download. The example notebook computes means from a CSV file, writes a ranking, builds and analyses JSON, then converts the CSV into JSON.

Play with it!

Reading a CSV file with headers


Romain CasatiOctober 17, 2026· in OCaml, Notebook, Console· 1 min read

Your own OCaml modules, as with a compiler ​

In Basthon, you can open your own .ml files (Open menu or module= URL parameter) to use them as modules. Loading them is now close to separate compilation:

  • a module is only loaded when first used: stack.ml can use Queue even if it is opened before queue.ml;
  • an interface queue.mli constrains the module, and hides what it does not declare;
  • the errors of a module are shown in the cell that uses it, with the excerpt of the file at fault;
  • a student's module takes precedence over the modules opened by Basthon: a file.ml (the queue of the French course, "file" in French) is no longer hidden by js_of_ocaml's File module;
  • a module opened again after a change is reloaded, and circular dependencies are reported.

Play with it!

User modules, their interface and their errors


Romain CasatiOctober 17, 2026· in OCaml, Notebook, Console, New modules· 1 min read

Big integers and rationals in OCaml with zarith ​

In Basthon, OCaml is compiled to JavaScript and its integers are 32 bits: max_int is 2,147,483,647 and the factorial of 13 already overflows. The zarith library brings integers of arbitrary size (Z module) and exact rationals (Q module):

ocaml
#require "zarith";;

let rec fact n = if n = 0 then Z.one else Z.mul (Z.of_int n) (fact (n - 1));;
fact 100;;

Z.probab_prime Z.(~$2 ** 127 - ~$1) 25;;
Q.(of_ints 1 10 + of_ints 2 10);;

GCD and Bézout's identity, modular inverse and exponentiation, primality tests: everything is there for arithmetic, up to a miniature RSA written in a few lines in the example notebook. Values are shown directly in the toplevel, and computations stay fast thanks to JavaScript's big integers (BigInt).

Play with it!

A miniature RSA with zarith