Skip to content

OCaml ​

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


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


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

Interactive and animated OCaml canvases ​

In Basthon's OCaml kernel, a Graphics_js canvas once shown stays alive after the cell has run. It reacts to the mouse and the keyboard with loop, mouse_pos or read_key, and can be animated with Lwt.

It works in the console as in the notebook, including with isolated kernels.

Play with it!

An OCaml canvas that reacts to clicks and to the keyboard