Forester 4.2 › 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.