Skip to content

Notation and quotation

This page states how a quotation is read, what term it means, how terms print, and how notations are scoped. The surface is in the notation reference. The grammar is fixed: tree-sitter reads a quotation as a token tree (names, literals, symbol runs, antiquotations, and bracketed groups), and the front end gives it structure against the notations active where it stands, as Honu and Rhombus enforest shrubbery. No program regenerates the parser.

1. Productions

A notation \(N\) for a binding signature \(\Sigma\) is a set of productions. A production \(p\) has a shape, a sequence of tokens and holes, and a template \(t_p\), a term of \(\Sigma\) over the holes. A hole \(h\) of \(p\) is

  • a binder hole when an abstraction of \(t_p\) binds \(h\);
  • a data hole when \(t_p\) uses \(h\) at an argument of a data sort;
  • an operand hole otherwise, of the sort of the argument \(t_p\) uses it at.

Every hole has one role and one sort, and every hole occurs in \(t_p\); otherwise the production is refused. The production builds the sort of the head constructor of \(t_p\). A binder or data hole reads one token, so for reading a production only its operand holes at the ends matter: \(p\) is closed, prefix, postfix, or infix as it has no outer operand, one at its end, one at its start, or one at each.

2. Reading against a precedence DAG

The active productions form a precedence graph in the sense of Danielsson and Norell. Its nodes (levels) are the classes of the relation =; the relation a > b puts the level of \(a\) above the level of \(b\), and \(\uparrow L\) is the set of levels strictly above \(L\) in the transitive closure, which must be acyclic. For a level \(L\), the expressions of \(L\) are

\[ \begin{aligned} E_L \;::=\;& \uparrow_L\; \text{infix}^{\text{none}}_L\; \uparrow_L \;\mid\; (\text{prefix}_L \mid \uparrow_L\,\text{infix}^{\text{right}}_L)^{+}\; \uparrow_L \\ \;\mid\;& \uparrow_L\; (\text{postfix}_L \mid \text{infix}^{\text{left}}_L\,\uparrow_L)^{+}, \end{aligned} \]

where \(\uparrow_L\) is an atom or an expression of a level in \(\uparrow L\), and an operator's inner holes (between its tokens) read any expression of their sort. Atoms are names, antiquotations, constructor applications \(\mathrm{Op}(a_1, \ldots, x\,y.\,b)\), parenthesized expressions, and the closed productions. Productions of one level must chain the same way. A quotation means its unique reading of the sort it is checked at:

  • with no reading, the reader reads again with every pair of unrelated levels nestable; a reading found then nests two productions with no precedence relation, and the refusal names both, with their notations and their modules;
  • with two readings, the refusal names the productions at the outermost point where they differ.

3. Hygiene by sets of scopes

A reading expands to syntax whose names carry sets of scopes, following Flatt:

  • a name the quotation's author writes carries the quotation's use-site scope \(u\);
  • a use of production \(p\) of notation \(N\) instantiates \(t_p\), whose own names carry \(\{d_N, i\}\): \(d_N\) is \(N\)'s definition scope and \(i\) a fresh introduction scope;
  • a hole is replaced by its fill, which keeps its scopes; a binder hole by the author's name.

Resolution then walks the expanded term. An abstraction adds a fresh scope \(s\) to its binders and to every name in its body. A reference with scope set \(S\) resolves to the binding of its name whose scope set is the largest subset of \(S\); two largest is a refusal. Then:

  1. A binder a template introduces never captures a name the author wrote: the binder's scopes include \(d_N\) and \(i\), which the author's names lack. twice F => Lam(y. App(F, App(F, y))) reads λy. twice y as \(\lambda.\,\lambda.\,1\,(1\,0)\), not \(\lambda.\,\lambda.\,0\,(0\,0)\).
  2. A binder the author writes never captures a name a template introduced, for the same reason with the roles exchanged: self x. M => Lam(f. Lam(x. App(f, M))) reads self f. f as \(\lambda.\,\lambda.\,1\,0\).

A resolved reference becomes the de Bruijn index of its binder; a name bound nowhere is a variable of the quotation's declared context (\(x, y \vdash t\)), or a nullary constructor, or it is refused.

4. Splices

An antiquotation $e stands for a value e of the host program. Its context is the binders its author can see, the bindings whose scope sets are subsets of its own. When the quotation places it under binders its author cannot see, the spliced term is weakened past each of them: walking the binders around it from the innermost, with \(k\) binders already accounted for, an invisible binder applies \(\mathrm{shift}_{k,1}\). In a value this is a call of the generated Sig.S.shift, bound before the statement; a pattern would have to strengthen instead, and is refused.

5. Elaboration

The resolved term is written as the ordinary surface syntax of its constructor tree: a constructor applied to its arguments, a variable as Sig.S.var(i), a literal as itself, and a splice as its expression. A pattern quotation becomes the corresponding constructor pattern, a variable the variable constructor at its index, a binding argument a binder pattern, and $x a name pattern. The checker then checks the tree like hand-written code: a quotation has exactly the meaning (and the content hash) of the constructor tree it stands for, and a splice of the wrong type is refused at the field it fills, as typed literal macros check splices in the client's context.

6. Printing

Printing inverts reading. A term of sort \(S\) prints through the production of \(S\) whose template matches it most specifically (most constructors, ties to the earlier active production). Matching a template binds each operand hole to a subterm; under a binder the template introduces, the subterm must not mention that binder and is strengthened past it, and a hole used twice must match equal subterms. A hole is printed recursively and parenthesized exactly when §2 does not allow its production there; a constructor no production matches prints as \(\mathrm{Op}(\ldots)\). A binder is named apart from every name in scope, every nullary constructor, and every word token; the generated Show instances name the binder at de Bruijn level \(\ell\) as \(x_\ell\), which is fresh by construction. Two printed pieces are separated by a space when the first ends and the second starts like a name. Reading a printed term gives the term back.

7. Scoping

Notations are module items. Static interpretation resolves each module's open notation statements against the module's own declarations and imports before the module graph is flattened, and records on each quotation the notations active at its position; activation is lexical and never reaches an importing module. The program's printing (its Show instances) uses the root module's open notations at the end of the module.

8. Code quotations

A quotation annotated with a quoted-syntax family of the meta phase (SynDecl, SynComp, SynValue, SynType, or SynPattern) quotes QVR: its contents, with each antiquotation replaced by a fresh name, are parsed as a declaration, a computation, a value, a type, or a pattern and written as the constructor tree of that family, each fresh name standing for the antiquoted value at the family (or String) its position takes. These are the families the meta phase splices, so a code quotation adds a way to write quoted syntax, not a second representation of it. A quotation may span lines: tree-sitter keeps its line breaks and indentation as blocks of the token tree, so a quoted declaration keeps the layout it is parsed with, and the indentation around the quotation stays balanced.