Skip to content

How the WL algorithm works¤

Wadler–Lindig pretty printing happens in two steps.

First, we represent the object as a nested collection of Wadler–Lindig documents, corresponding to unbroken text, places that newlines can be inserted, etc. This describes the pretty representation, but for which no maximum width has yet been set.

Second, once provided a width, this nested structure of documents is then passed to the Wadler–Lindig algorithm, which lays out the text within the space constraints.

There are five kinds of Wadler–Lindig document:

As such, the wadler_lindig library actually does two distinct things:

  1. It converts Python objects into Wadler–Lindig documents.
  2. It runs the Wadler–Lindig algorithm for formatting Wadler–Lindig documents.

When we run wadler_lindig.pprint or wadler_lindig.pformat, then both of these happen back-to-back.

Now that we know this, we can customise how types are displayed by specifying how to turn custom Python objects into Wadler–Lindig documents. This is the subject of our next example.