Module Gprinttyp

This module provides function for printing type expressions as digraph using graphviz format. This is mostly aimed at providing a better representation of type expressions during debugging session.

A type node is printed as

.------------.
| <desc>  id |---->
|            |--->
.------------.

where the description part might be:

The more complex encoding for polymorphic variants and object types uses nodes as head of the subgraph representing those types

Then each nodes is relied by arrows to any of its children types.

.-------------.   0     .-------.
| Result.t  5 |-------->| int! 2|
.-------------.         .-------.
   |
   | 1
   v
 .----------.
 | float! 3 |
 .----------.

Moreover, type abbreviations might be linked to the expanded nodes. If I define: type 'a pair = 'a * 'a, a type expression int pair might correspond to the nodes:

.--------.   0    .--------.
| pair 6 |------> | int! 2 |
.--------.        .--------.
   ┆                  ^
   ┆ expand           |
   ┆                  |
.------.   0 + 1      |
| *  7 |------>-------.
.------.
.----------------.
| .----------.    |
| | [obj] 14 |<------<-----<-----.
| .----------.    |              |
|       ┆         |              |
| .-------------. |    .------.  |    .-------.
| | a public 15 |----->| ∀ 18 |----->| int! 2 |
| .-------------. |    .------.  |    .-------.
|        ┆        |              |
| .-------------. |   .------.   |
| | m public 16 |-----| ∀ 19 |>--|
| .------------.  |   .------.
|     ┆           |
|     ┆ row var   |
|     ┆           |
|   .-------.     |
|   | '_ 17 |     |
|   .-------.     |
.-----------------.
type digraph

Digraph with nodes, edges, hyperedges and subgraphes

type params

Various possible choices on how to represent types, see the params functions for more detail.

type element

Graph element, see the node, edge and hyperedge function

type decoration

Visual decoration on graph elements, see the Decoration module.

val types : title:string -> params -> (decoration * Types.type_expr) list -> unit

Print a graph to the file asprintf "%s/%04d-%s-%a.dot" dump_dir session_unique_id title pp_context context

If the dump_dir flag is not set, the local directory is used. See the context type on how and why to setup the context.

val nodes : title:string -> params -> (decoration * element) list -> unit

Full version of types that allow to print any kind of graph element

val params : ?elide_links:bool -> ?expansion_as_hyperedge:bool -> ?short_ids:bool -> ?colorize:bool -> ?follow_expansions:bool -> unit -> params

Choice of details for printing type graphes:

  • if elide_links is true link nodes are not displayed (default:true)
  • with expansion_as_hyperedge, memoized constructor expansion are displayed as a hyperedge between the node storing the memoized expansion, the expanded node and the expansion (default:false).
  • with short_ids, we use an independent counter for node ids, in order to have shorter ids for small digraphs (default:true).
  • with colorize nodes are colorized according to their typechecker ids (default:true).
  • with follow_expansions, we add memoized type constructor expansions to the digraph (default:true).
val update_params : ?elide_links:bool -> ?expansion_as_hyperedge:bool -> ?short_ids:bool -> ?colorize:bool -> ?follow_expansions:bool -> params -> params

Update an existing params with new values.

val node : Types.type_expr -> element
type dir =
  1. | Toward
  2. | From
val hyperedge : (dir * decoration * Types.type_expr) list -> element

Edges between more than two elements.

module Decoration : sig ... end

Digraph construction and printing

val make : params -> (decoration * element) list -> digraph
val add : params -> (decoration * element) list -> digraph -> digraph
val add_subgraph : params -> decoration -> (decoration * element) list -> digraph -> digraph

add a subgraph to a digraph, only fresh nodes are added to the subgraph

val group_nodes : (decoration * digraph) -> digraph -> digraph

groups existing nodes inside a subgraph

val pp : Stdlib.Format.formatter -> digraph -> unit

Debugging helper functions

Generic print debugging function

val debug_on : (unit -> bool) ref

Conditional graph printing

val debug_off : (unit -> 'a) -> 'a

debug_off f switches off debugging before running f.

val debug : (unit -> unit) -> unit

debug f runs f when !debug_on ()

Node tracking functions

val register_type : (decoration * Types.type_expr) -> unit

register_type (lbl,ty) adds the type t to all graph printed until forget is called

val register_subgraph : params -> ?decoration:decoration -> Types.type_expr list -> unit

register_subgraph params tys groups together all types reachable from tys at this point in printed digraphs, until forget is called

val forget : unit -> unit

Forget all recorded context types

type 'a context

Contextual information

Those functions can be used to modify the filename of the generated digraphs. Use those functions to provide contextual information on a graph emitted during an execution trace.

val global : string context
val set_context : 'a context -> 'a -> unit
val with_context : 'a context -> 'a -> (unit -> 'b) -> 'b