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 library | To… |
|---|---|
Basthon | display (images, SVG, HTML, canvases), download, wait |
Graphics_js | draw, animate, react to the mouse and the keyboard |
#require | load a library |
Basthon.Visu | visualize recursive calls, memory, trees, graphs, arrays |
Basthon.Graphviz | draw 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.
| Function | Role |
|---|---|
help : unit -> unit | shows the help |
version : unit -> string | version of the module |
download : string -> unit | download path downloads a file of the filesystem |
sleep : float -> unit | sleep d waits d seconds |
display_image : string -> unit | display_image path shows a PNG image of the filesystem |
display_svg : string -> unit | shows an SVG image given as a string |
display_html : string -> unit | shows HTML given as a string |
display : (string * string) list -> unit | shows a content according to its MIME type, for instance [("text/html", "<b>hi</b>")] |
create_canvas : unit -> canvas | creates a canvas, to draw on with Graphics_js |
display_canvas : canvas -> unit | shows a canvas, which stays up to date and receives the mouse and the keyboard |
save_canvas : canvas -> string -> unit | save_canvas c path saves the canvas into a PNG or JPG file (according to the extension) |
download_canvas : ?format:string -> canvas -> unit | downloads 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:
| Function | Role |
|---|---|
open_canvas : canvas -> unit | draws from now on on this canvas (created by Basthon.create_canvas) |
loop : event list -> (status -> unit) -> unit | loop 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.t | position of the mouse |
button_down : unit -> bool Lwt.t | is the mouse button down? |
read_key : unit -> char Lwt.t | waits for a key press |
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 canvasThe 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.
| Library | Modules | To… |
|---|---|---|
visu | Basthon.Visu | visualize recursive calls, memory, trees, graphs |
graphviz | Basthon.Graphviz | draw a graph described in DOT |
ocamlgraph | Graph | graph structures and algorithms |
zarith | Z, Q | big integers and rationals |
containers | CCList, CCHeap… | extensions of the standard library |
containers-data | CCDeque, CCFQueue, CCVector, CCBV… | data structures (deques, queues, bit vectors…) |
yojson | Yojson | read and write JSON |
csv | Csv | read 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.
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;;| Function | Role |
|---|---|
trace ?nom montre_arg montre_res f x | computes 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 x | computes f x and draws the call tree, the calls already computed in orange (unless ~redondants:false), at most max_appels (300) calls |
Memory
| Function | Role |
|---|---|
memoire ?nom ?max_blocs v | draws the memory representation of v: blocks and pointers, in orange those to a block already drawn (sharing, cycle) |
Trees
| Function | Role |
|---|---|
noeud ?detail ?couleur texte fils | a node, with its children, a detail written in grey and a background colour |
feuille ?detail ?couleur texte | a node without children |
vide | an empty subtree (drawn as a dot) |
arbre a | draws the tree a |
Visu.(arbre (noeud "+" [ feuille "1"; noeud "*" [ feuille "2"; feuille "3" ] ]));;Graphs and arrays
| Function | Role |
|---|---|
graphe ?oriente ?noms ?couleurs ?details g | draws the adjacency-list graph g (vertices 0 to n - 1), on a circle; couleurs: list of (vertex, colour) |
tableau ?surligne ?titre montre a | draws the array a and its indices, the surligne cells in orange |
barres ?surligne a | draws 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";;.
| Function | Role |
|---|---|
display ?engine dot | draws the graph described by the dot string |
display_file ?engine path | draws the graph of a DOT file (written for instance by ocamlgraph's dot_output) |
svg ?engine dot | the SVG image of the graph, as a string |
engines | the 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_intis 2,147,483,647): use zarith for big integers; - the
#tracedirective does not exist:Visu.tracereplaces it; - the blocking functions of
Graphicsare replaced by those ofGraphics_js; - the
.mlfiles opened in Basthon are modules, loaded when first used (see Files and modules).