Skip to content

OCaml: what Basthon adds ​

This page describes the modules and libraries that Basthon adds to OCaml, and the differences with the OCaml toplevel on a computer. To discover the kernel through examples, see OCaml in Basthon.

Module or libraryTo…
Basthondisplay (images, SVG, HTML, canvases), download, wait
Graphics_jsdraw, animate, react to the mouse and the keyboard
#requireload a library
Basthon.Visuvisualize recursive calls, memory, trees, graphs, arrays
Basthon.Graphvizdraw a graph described in DOT

The Basthon module ​

Always available (not to be confused with the basthon Python module). Basthon.help () shows its help and the list of the libraries.

FunctionRole
help : unit -> unitshows the help
version : unit -> stringversion of the module
download : string -> unitdownload path downloads a file of the filesystem
sleep : float -> unitsleep d waits d seconds
display_image : string -> unitdisplay_image path shows a PNG image of the filesystem
display_svg : string -> unitshows an SVG image given as a string
display_html : string -> unitshows HTML given as a string
display : (string * string) list -> unitshows a content according to its MIME type, for instance [("text/html", "<b>hi</b>")]
create_canvas : unit -> canvascreates a canvas, to draw on with Graphics_js
display_canvas : canvas -> unitshows a canvas, which stays up to date and receives the mouse and the keyboard
save_canvas : canvas -> string -> unitsave_canvas c path saves the canvas into a PNG or JPG file (according to the extension)
download_canvas : ?format:string -> canvas -> unitdownloads the canvas as PNG (the default) or JPG (~format:"jpg")

Graphics_js ​

Graphics_js has the interface of OCaml's Graphics module (set_color, fill_circle, moveto, lineto, draw_string…), draws on a canvas of the browser and adds:

FunctionRole
open_canvas : canvas -> unitdraws from now on on this canvas (created by Basthon.create_canvas)
loop : event list -> (status -> unit) -> unitloop events f calls f at each of these events of the canvas; returns at once (the canvas stays active)
mouse_pos : unit -> (int * int) Lwt.tposition of the mouse
button_down : unit -> bool Lwt.tis the mouse button down?
read_key : unit -> char Lwt.twaits for a key press
ocaml
let canvas = Basthon.create_canvas () in
  Graphics_js.open_canvas canvas;
  Graphics_js.resize_window 200 200;
  Graphics_js.loop [ Graphics_js.Button_down ] (fun s ->
      Graphics_js.fill_circle s.Graphics_js.mouse_x s.Graphics_js.mouse_y 5);
  Basthon.display_canvas canvas

The blocking functions of Graphics (wait_next_event…) do not exist: use loop, or Lwt for animations.

Loading a library: #require ​

The #require "name";; directive loads a library, already compiled: it is only downloaded at the first #require.

LibraryModulesTo…
visuBasthon.Visuvisualize recursive calls, memory, trees, graphs
graphvizBasthon.Graphvizdraw a graph described in DOT
ocamlgraphGraphgraph structures and algorithms
zarithZ, Qbig integers and rationals
containersCCList, CCHeap…extensions of the standard library
containers-dataCCDeque, CCFQueue, CCVector, CCBV…data structures (deques, queues, bit vectors…)
yojsonYojsonread and write JSON
csvCsvread and write CSV

graphics, lwt, str and js_of_ocaml are already loaded: a #require of one of them does nothing, so that code written for a usual toplevel works as is.

Basthon.Visu ​

Teaching visualizations, after #require "visu";; (for instance with module Visu = Basthon.Visu;;). The names of its functions are French.

Recursive calls ​

The function observed is written in open recursion: its first parameter is the function to call for the recursive calls.

ocaml
let fib fib n = if n < 2 then n else fib (n - 1) + fib (n - 2);;
Visu.trace ~nom:"fib" string_of_int string_of_int fib 4;;
Visu.arbre_appels ~nom:"fib" string_of_int string_of_int fib 5;;
FunctionRole
trace ?nom montre_arg montre_res f xcomputes f x showing the calls and their results (montre_arg and montre_res turn them into text)
arbre_appels ?nom ?max_appels ?redondants montre_arg montre_res f xcomputes f x and draws the call tree, the calls already computed in orange (unless ~redondants:false), at most max_appels (300) calls

Memory ​

FunctionRole
memoire ?nom ?max_blocs vdraws the memory representation of v: blocks and pointers, in orange those to a block already drawn (sharing, cycle)

Trees ​

FunctionRole
noeud ?detail ?couleur texte filsa node, with its children, a detail written in grey and a background colour
feuille ?detail ?couleur textea node without children
videan empty subtree (drawn as a dot)
arbre adraws the tree a
ocaml
Visu.(arbre (noeud "+" [ feuille "1"; noeud "*" [ feuille "2"; feuille "3" ] ]));;

Graphs and arrays ​

FunctionRole
graphe ?oriente ?noms ?couleurs ?details gdraws the adjacency-list graph g (vertices 0 to n - 1), on a circle; couleurs: list of (vertex, colour)
tableau ?surligne ?titre montre adraws the array a and its indices, the surligne cells in orange
barres ?surligne adraws an array of positive integers as a bar chart

The background colours are Visu.bleu, orange, vert, jaune and gris (blue, orange, green, yellow and grey).

Basthon.Graphviz ​

Draws graphs described in the DOT language of Graphviz, after #require "graphviz";;.

FunctionRole
display ?engine dotdraws the graph described by the dot string
display_file ?engine pathdraws the graph of a DOT file (written for instance by ocamlgraph's dot_output)
svg ?engine dotthe SVG image of the graph, as a string
enginesthe layout engines (dot by default, neato, circo…)

Differences with OCaml on a computer ​

OCaml is compiled to JavaScript here (js_of_ocaml):

  • integers are 32 bits (max_int is 2,147,483,647): use zarith for big integers;
  • the #trace directive does not exist: Visu.trace replaces it;
  • the blocking functions of Graphics are replaced by those of Graphics_js;
  • the .ml files opened in Basthon are modules, loaded when first used (see Files and modules).