Skip to content

Literate modules

A literate module is a Markdown document whose code and prose are one artifact. Following Knuth's division of a literate source into the program it tangles to and the document it weaves to, the code of a .qvr.md file is a module of the language and its prose is metadata about that module. This page states how the module is obtained, what of the prose is checked, and when a result written into the prose is current. The surface is in Literate modules.

Tangling

Let a document \(D\) have lines \(\ell_1, \ldots, \ell_n\) and code blocks \(B_1, \ldots, B_k\) in document order, a block being a fenced qvr block that is unmarked or marked cumulative or qiec-cumulative. The tangle of \(D\) is the source with lines \(t_1, \ldots, t_n\), where \(t_i\) is \(\ell_i\) with its block's fence indentation removed when \(\ell_i\) is a line of some \(B_j\), and the empty line otherwise. The module of \(D\) is the module this source denotes, named by its path like a .qvr file.

Three properties follow from the definition.

  1. Order. The module's declarations are those of its blocks in document order, as if the blocks were concatenated; empty lines between them are not syntax.
  2. Locations. A line of the tangle is the line of the document it came from, so every diagnostic of the module, from parsing to checking, names the document's line.
  3. Prose is metadata. The tangle is invariant under every edit to a line outside the code blocks that keeps the number of lines, and an edit that adds or removes prose lines only moves declarations. Content hashes are computed from the checked core, which records no positions, and source hashes normalize positions away, so no edit to the prose changes a declaration's identity, a fit's cache key, or an experiment's hash.

Examples

A ```qvr example fence holds the steps of an example. It is a test of the module in the sense of tests and exact evaluation: its steps are evaluated against the module and are not part of it, so it enters no hash. Its steps keep their document lines.

References

A prose reference {qvr:ref}`x` is resolved in the scope of the document's module exactly as a reference written in the module's code at top level is (see Modules and static interpretation): the head of a dotted name is looked up among the module's declarations and its imports, and the rest selects members. The reference denotes the flat-program name it resolves to, and it is valid when the checked program declares that name: a statement of the flat program or a declaration of its core, including generated ones (a deduction's item family, a derived instance). An invalid reference is an error of the document, located at the reference. Since the check is resolution, a reference broken by a renaming is found by the same rule that finds a broken name in code.

Woven results

An {experiment} E directive names an experiment of the module, and weaving writes into it the experiment's hash \(h(E)\) together with the result of its runs. The hash determines the result: it covers the program's content hash, the data pins, the values, the method and options, and the seed. A woven result recording \(h'\) is current when \(h' = h(E)\) for the document as it stands, and stale otherwise. Checking a document recomputes \(h(E)\) from its declarations and pins without running anything, and refuses every stale result, every unwoven directive, and every directive that names no experiment. A number in the document is therefore the output of the program and data the document now declares, or the document does not check.

References