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.
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.
