A set of functions for creating and manipulating algebra documents, as described in "Strictly Pretty" (2000) by Christian Lindig.
An algebra document is represented by an Inspect.Algebra
node
or a regular string.
iex> Inspect.Algebra.empty
:doc_nil
iex> "foo"
"foo"
With the functions in this module, we can concatenate different elements together and render them:
iex> doc = Inspect.Algebra.concat(Inspect.Algebra.empty, "foo")
iex> Inspect.Algebra.format(doc, 80)
["foo"]
The functions nest/2
, space/2
and line/2
help you put the
document together into a rigid structure. However, the document
algebra gets interesting when using functions like break/2
, which
converts the given string into a line break depending on how much space
there is to print. Let's glue two docs together with a break and then
render it:
iex> doc = Inspect.Algebra.glue("a", " ", "b")
iex> Inspect.Algebra.format(doc, 80)
["a", " ", "b"]
Notice the break was represented as is, because we haven't reached a line limit. Once we do, it is replaced by a newline:
iex> doc = Inspect.Algebra.glue(String.duplicate("a", 20), " ", "b")
iex> Inspect.Algebra.format(doc, 10)
["aaaaaaaaaaaaaaaaaaaa", "\n", "b"]
Finally, this module also contains Elixir related functions, a bit
tied to Elixir formatting, namely surround/3
and surround_many/5
.
Implementation details
The original Haskell implementation of the algorithm by Wadler
relies on lazy evaluation to unfold document groups on two alternatives:
:flat
(breaks as spaces) and :break
(breaks as newlines).
Implementing the same logic in a strict language such as Elixir leads
to an exponential growth of possible documents, unless document groups
are encoded explictly as :flat
or :break
. Those groups are then reduced
to a simple document, where the layout is already decided, per Lindig.
This implementation slightly changes the semantic of Lindig's algorithm
to allow elements that belong to the same group to be printed together
in the same line, even if they do not fit the line fully. This was achieved
by changing :break
to mean a possible break and :flat
to force a flat
structure. Then deciding if a break works as a newline is just a matter
of checking if we have enough space until the next break that is not
inside a group (which is still flat).
Custom pretty printers can be implemented using the documents returned by this module and by providing their own rendering functions.