New modules
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.maketo create a matrix!) and cyclic values; - trees, graphs and arrays, for instance to follow a sort step by step.
#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.

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):
#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).

pytest tests with ipytest
The ipytest module is available in Basthon. It lets you write pytest tests directly in the console or in a cell of the notebook:
import ipytest
ipytest.autoconfig()
def mean(marks):
return sum(marks) / len(marks)
def test_mean():
assert mean([10, 12, 14]) == 12
def test_empty_list():
assert mean([]) == 0
ipytest.run()asserts are detailed as with pytest: you see straight away which test fails and why. The %%ipytest magic command does not exist in Basthon: call ipytest.run().
drawsvg 2
Version 2 of the drawsvg module is available in Basthon's Python kernel, next to the old drawSvg version. Shapes, texts, gradients, animations… drawings are shown directly and downloaded as SVG with d.download().

icecream
Tired of print to debug? The icecream module is available in Basthon's Python kernel: ic(expression) shows both the expression and its value.
For it to work, Basthon now keeps the source code of the cells run: inspect.getsource also works on the functions defined in the notebook or the console.
