Skip to content

November 2026 ​

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, New modules· 2 min read

Drawing graphs in OCaml: Graphviz and ocamlgraph ​

Two libraries arrive in Basthon's OCaml kernel to work on graphs.

graphviz draws a graph described in the DOT language of Graphviz, directly in the notebook:

ocaml
#require "graphviz";;
module Gv = Basthon.Graphviz;;

Gv.display "digraph { a -> b; b -> c; c -> a }";;
Gv.display ~engine:"circo" "graph { 1 -- 2 -- 3 -- 4 -- 1 }";;

So writing a function that produces the DOT text of a structure is enough to draw a graph given by adjacency lists, a binary search tree or an automaton.

ocamlgraph is OCaml's reference graph library: many graph structures and the classic algorithms (traversals, Dijkstra, connected components, topological sort, spanning trees, flows…). It produces DOT too, which Gv.display draws:

ocaml
#require "ocamlgraph";;
module G = Graph.Pack.Digraph;;

let g = G.Classic.full 4;;
G.dot_output g "g.dot";;
Gv.display_file "g.dot";;

The example notebook shows ocamlgraph's two styles (the ready-to-use Pack module, and functors), with for instance the shortest path in red.

Play with it!

The shortest path found by Dijkstra, drawn by Graphviz


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, New modules· 2 min read

Visualizing memory and recursive calls in OCaml ​

In Python, tools such as lolviz, rcviz or Python Tutor help students picture memory and recursive calls. Basthon's OCaml kernel now has its own tool: the visu library, which draws in the notebook:

  • the trace and the call tree of a recursive function, where redundant computations appear in orange;
  • the memory representation of a value: blocks, pointers, sharing (the classic trap of Array.make to create a matrix!) and cyclic values;
  • trees, graphs and arrays, for instance to follow a sort step by step.
ocaml
#require "visu";;
module Visu = Basthon.Visu;;

let fib fib n = if n < 2 then n else fib (n - 1) + fib (n - 2);;
Visu.arbre_appels ~nom:"fib" string_of_int string_of_int fib 5;;

Visu.memoire ~nom:"m" (Array.make 3 (Array.make 2 0));;

To observe its calls, the function is written in open recursion: its first parameter is the function to use for its recursive calls. The #trace directive of the OCaml toplevel does not work in Basthon, where OCaml is compiled to JavaScript: Visu.trace replaces it.

Play with it!

The call tree of fib 5