Release. Forester 4.2 [00WK]
- June 20, 2024
- Jon Sterling, with contributions from Kento Okura, Jinser Kafka
This release brings several internal changes that are meant to facilitate the further improvement of Forester, as well as some user-facing changes (most notably a new query language). These are breaking changes: existing queries will need to be translated, and you will need to upgrade your theme.
First-class functions and lazy arguments [00YF]
- June 20, 2024
- Jon Sterling
This release adds λ-abstraction, which can be written like so:
\fun[x][y]{Hello, \x and \y}In addition, lazy arguments are now supported. In any place that a function argument can be bound (e.g. in \def, \let, or \fun), an argument marked with a tilde is automatically wrapped in a thunk when it is passed. For example, consider the following code:
\def\my-fun[x][~y]{Hello, \x and \y{}!}
\p{\my-fun{foo}{bar}}In the above, the argument x is strict and y is a thunk that must be forced before being used—hence the application to {}; note that the results are not memoised. The code above is functionally equivalent to the following:
\def\my-fun[x][y]{Hello, \x and \y{}!}
\p{\my-fun{foo}{\fun[z]{bar}}}Of course, when your code is pure, passing by name vs. by value does not make a difference. The purpose of this feature is, however, to enable more expressive macros that make use of fluid bindings.
Example Combining fluid bindings and lazy arguments [00YG]
- June 20, 2024
- Jon Sterling
Forester 4.0’s enhanced XML support makes it possible (see Example [00RL]) to write MathML directly as an alternative to using Forester’s TeX-like math mode via KaTeX. For example, we can write the following wrappers for some basic MathML elements:
\xmlns:mml{http://www.w3.org/1998/Math/MathML}
\def\math[body]{\<mml:math>{\body}}
\def\mrow[body]{\<mml:mrow>{\body}}
\def\mi[x]{\<mml:mi>{\x}}
\def\mo[o]{\<mml:mo>{\o}}
\def\mn[n]{\<mml:mn>{\n}}With this in hand, we can write prose involving MathML content, like so:
\p{Let \math{\mi{x}} and \math{\mi{y}} be integers such that \math{\mrow{\mrow{\mi{x}\mo{-}\mi{y}}\mo{=}\mn{0}}}.}The above is a bit messy. We can clean it up with some function definitions:
\def\plus[x][y]{\mrow{\x \mo{+} \y}}
\def\equals[x][y]{\mrow{\x \mo{=} \y}}
\let\x{\mi{x}}
\let\y{\mi{y}}
\p{Let \math{\x} and \math{\y} be integers such that \math{\equals{\plus{\x}{\y}}{\mn{0}}}.}It is annoying that we have to always wrap things in \math. By combining first-class functions, lazy arguments, and fluid bindings, we can lift this requirement:
% Allocate a fluid binding to hold the function that enters math mode.
\alloc\math-wrapper
% Ensure that a given (lazy) argument is evaluated inside a
% <mml:math> element.
\def\ensure-math[~body]{%
\scope{%
% Set a default value for \math-wrapper (no-op if already set).
\put?\math-wrapper{\fun[x]{\<mml:math>{\x}}}%
%
% Apply the current function bound to \math-wrapper.
\get\math-wrapper{%
% Bind \math-wrapper to the identity function in the current scope so that
% we do not get redundant <mml:math> nodes.
\put\math-wrapper{\fun[x]{\x}}%
%
% Force the \body thunk to evaluate the body within the wrapper.
\body{}%
}%
}%
}We now re-define our macro library to use the \ensure-math:
\def\mrow[~x]{\ensure-math{\<mml:mrow>{\x{}}}}
\def\mi[~x]{\ensure-math{\<mml:mi>{\x{}}}}
\def\mo[~x]{\ensure-math{\<mml:mo>{\x{}}}}
\def\mn[~x]{\ensure-math{\<mml:mn>{\x{}}}}
\def\plus[~x][~y]{\mrow{\x{} \mo{+} \y{}}}
\def\equals[~x][~y]{\mrow{\x{} \mo{=} \y{}}}Now, we can put MathML content freely into prose without needing to wrap it each time.
\let\x{\mi{x}}
\let\y{\mi{y}}
\p{Let \x and \y be integers such that \equals{\plus{\x}{\y}}{\mn{0}}.}Warning Lazy arguments are experimental [00YH]
- June 20, 2024
- Jon Sterling
Please note that these features are experimental and subject to change—only build large macro libraries relying on them at your own risk.
New query language [00WS]
- June 20, 2024
- Jon Sterling
The existing querying feature, introduced in Forester 2.2, has been replaced by something much better — at the cost of a breaking change that will require existing queries to be rewritten.
New syntax for set operations [00WU]
- June 20, 2024
- Jon Sterling
Intersections of queries are formed using a variadic constructor \query/isect. For example, the following computes the intersection of the queries q1, q2, and q3:
\query/isect{q1}{q2}{q3}Unions of queries are formed similarly using the variadic \query/union constructor. The complement of a query q is written \query/compl{q}.
Note: as before, to render the results of a query you must use the \query command; for example:
\query{
\query/isect{q1}{q2}{q3}
}Query operations are first-class values [00WV]
- June 20, 2024
- Jon Sterling
Previously, the built-in query operations were written like \query/and, etc.; these names, however, were hard-coded into the lexer which meant that they did not work in the expected way with Forester’s hierarchical namespacing. This hardcoding has been removed, and query operations are treated as first-class values that are resolved from primitive names in the \query/ namespace. As such, the following code is equivalent to \query/compl{\query/taxon{theorem}}:
\scope{
\open\query
\compl{\taxon{Theorem}}
}A further consequence of this change is that queries can be defined compositionally by macros.
Relational queries [00WW]
- June 20, 2024
- Jon Sterling
The most significant user-facing change to the query language is the ability to form basic relational queries along several built-in relation-symbols; in fact, the built-in query constructors \query/taxon, \query/tag, and \query/author are all expressible in these terms.
Built-in relation symbols [00WX]
- June 20, 2024
- Jon Sterling
Forester includes several built-in relation symbols, maintained as graphs that are accumulated during evaluation:
\rel/transclusioncontains an edge fromxtoywhenxdirectly transcludesy, i.e. contains\transclude{y}.\rel/linkscontains an edge fromxtoywhenxlinks toy, i.e. contains something like[...](y)or[[y]].\rel/taxacontains an edge fromxtoywhenxhas taxony, i.e. contains\taxon{Y}.\rel/tagscontains an edge fromxtoywhenxhas tagy, i.e. contains\tag{y}.\rel/authorscontains an edge fromxtoywhenxhas authory, i.e. contains\author{y}.\rel/contributorscontains an edge fromxtoywhenxis a tree with contributory, i.e. contains\contributor{y}.
The implementation supports arbitrarily many relation symbols; this is not yet exposed to the user, but could in the future be employed by advanced users of Forester to support the development of sophisticated ontologies.
Query modes and polarities [00WZ]
- June 20, 2024
- Jon Sterling
Given an address x and a relation symbol r, there are several useful queries that we might wish to make:
- We might wish to find all incoming edges to
xinr, or we may wish to find all outgoing edges fromxinr. We shall refer to the differenec between incoming and outgoing edges is referred to as a polarity. - Rather than searching for edges, we may wish to search for paths — which corresponds to passing from a relation to its preorder reflection. We will refer to this distinction as a mode.
Forester supports both polarities and modes for relational queries via the following built-in constants:
\query/incomingand\query/outgoingdenote the two polarities.\query/edgesand\query/pathsrepresent the two modes.
The syntax of a relational query [00WY]
- June 20, 2024
- Jon Sterling
Example Expressing built-in queries [00X0]
- June 20, 2024
- Jon Sterling
The built-in basic queries can be expressed in terms of \query/rel in the following way:
\def\my-query/author[x]{
\open\query
\rel{\edges}{\incoming}{\rel/authors}{\x}
}
\def\my-query/taxon[x]{
\open\query
\rel{\edges}{\incoming}{\rel/taxa}{\x}
}
\def\my-query/tag[x]{
\open\query
\rel{\edges}{\incoming}{\rel/tags}{\x}
}Example Querying the (back)links and context a given tree [00X2]
- June 20, 2024
- Jon Sterling
Both the backlinks and (forward) links of a given tree can be computed using a query to the \rel/links relation, and the context (immediate parents) of a given tree can be obatined from the \query/transclusion relation:
\def\query/backlinks[x]{
\open\query
\rel{\edges}{\incoming}{\rel/links}{\x}
}
\def\query/links[x]{
\open\query
\rel{\edges}{\outgoing}{\rel/links}{\x}
}
\def\query/context[x]{
\open\query
\rel{\edges}{\incoming}{\rel/transclusion}{\x}
}Example Querying a coslice of the transclusion preorder [00X4]
- June 20, 2024
- Jon Sterling
We can also write a query to compute the set of trees underneath a given tree in the transclusion preorder (including the original tree) — in other words, the coslice.
\def\query/transclusion-coslice[x]{
\open\query
\rel{\paths}{\outgoing}{\rel/transclusion}{\x}
}Unions and intersections of families of queries [00X1]
- June 20, 2024
- Jon Sterling
A further primitive query-former is the union or intersection of a certain family of queries indexed in the results of a fixed query. These are written as follows:
\query/union-fam{q}{qfam}
\query/isect-fam{q}{qfam}In the above, q is a query expression and qfam is a first-class function that takes an address and returns a query expression. There are a couple built-in instantiations of \query/union-fam and \query/isect-fam for the specific case where qfam is a family of relational queries; for example:
\query/union-fam-rel{q}{mode}{polarity}{rel}is equivalent to the following more verbose code:
\query/union-fam{q}{
\fun[x]{
\query/rel{mode}{pol}{\x}
}
}Example Querying the bibliography of a given tree [00X3]
- June 20, 2024
- Jon Sterling
We can define a macro to compute the bibliography of a given tree, making use of \query/transclusion-coslice from Example [00X4].
\def\query/bibliography[x]{
\open\query
\isect{
\union-fam-rel{\transclusion-coslice{\x}}{\edges}{\outgoing}{\rel/links}
}{
\taxon{Reference}
}
}The above code first accumulates all the outgoing links from the given tree and any of its children, and then restricts (by intersection) to those with taxon reference.
Example Querying all hereditary contributors to a tree [00X5]
- June 20, 2024
- Jon Sterling
We can use the union of families of relational queries to compute the set of (proper) contributors to a given tree. This is slightly subtle, because if a bibliographic tree is transcluded, we do not want its contributors to be included.
\def\query/contributors[x]{
\open\query
\union-fam-rel{
\isect{
\transclusion-coslice{\x}
}{
\compl{
\taxon{Reference}
}
}
}{\edges}{\outgoing}{\rel/contributors}
}In the future, the support for query-indexed families of queries might be generalised to allow arbitrary queries in the components rather than only relational ones. The current version of the feature should be viewed as somewhat provisional.
Warning Queries are experimental [00WT]
- June 20, 2024
- Jon Sterling
It is strongly recommended that inexperienced users of Forester continue to limit their usage of queries until the feature has reached a stable interface.
Intermediate representation of the XML format [00WL]
- June 20, 2024
- Jon Sterling
Forester’s primary output format is XML, rendered live in the browser to HTML using an XSLT stylesheet. Although this will continue to be the main output format, together with Kento Okura as well as our friends from the Topos Institute I am hoping to explore richer hypermedia interfaces for forests that could transcend the limitations of static HTML output.
In order to support multiple backends without creating a log of code churn, I have created an OCaml abstraction of the XML format that will serve as a “source of truth” for Forester’s output — which can be serialised directly to XML, or rendered in a different way (e.g. as HTML or htmx emitted by a hypermedia server).
With this change, I would expect Forester’s XML format to begin to change less and less over time, perhaps culminating in a versioned schema. I do not, however, guarantee that I will not make breaking changes to the format going forward.
Generalised backmatter [00WM]
- June 20, 2024
- Jon Sterling
Currently, the backmatter of a tree is not customisable; although the default backmatter sections are useful for most trees, it is likely that some of the sections are less relevant at times, and that other kinds of queries would be useful to be able to include on a tree-by-tree basis.
In order to facilitate such a feature in the future, this release changes the representation of backmatter to support arbitrary content and queries. This is a breaking change to the XML format: the <backmatter> element now contains arbitrary <tree> elements that encode the backmatter sections. By virtue of the improved query engine, all default backmatter sections are computed from queries.
HTML figures and captions; embedded TeX [00WN]
- June 20, 2024
- Jon Sterling
This release adds \figure and \figcaption primitives that are interpreted by the XSLT stylesheet as HTML’s <figure> and <figcaption> elements.
The stylesheet no longer emits the images corresponding to embedded \LaTeX inside a <center> tag, as this is deprecated in HTML. Users are encouraged to wrap Forester’s \tex primitive in a macro that embeds a diagram in a figure. For example, the following code comes from my own macro preamble:
% Allocate a fluid binding to hold the default LaTeX preamble,
% e.g. for loading tikz packages.
\alloc\base/tex-preamble
\def\texfig[~body]{
\scope{
% Install a LaTeX preamble to be used by default if none is set.
\put?\base/tex-preamble{
% Some useful things from my own library.
\latex-preamble/string-diagrams
\latex-preamble/diagrams
}
% Wrap the diagram in a figure
\figure{
% Render the LaTeX code with the current preamble
\tex{\get\base/tex-preamble}{\body{}}
}
}
}Performance improvements in graph analysis [00ZF]
- June 20, 2024
- Jon Sterling
The previous graph implementation has been replaced with one that is more time-efficient (at the cost of space). Computing the transclusion preorder (or, more generally, the reflexive-transitive closure of relations) is orders of magnitude faster; my own forest ultimately compiles in twice the speed.