Skip to content

Literate modules

A literate module is a Markdown document whose name ends in .qvr.md. Its fenced qvr blocks, in document order, are the module's code, and its prose explains them. It is a module like a .qvr file: it imports other modules, other modules import it, and qvr check, qvr run, qvr test, and qvr experiment accept it. Its prose can refer to its declarations, and checked examples and experiment results can be written into it. The rules are stated in Literate modules.

Code blocks

# Doubling

Doubling adds a number to itself.

```qvr
export define double(n : Int) : Int !{} =
    return n + n
```

Tripling adds it twice.

```qvr
export define triple(n : Int) : Int !{} =
    return n + n + n
```

The document tangles to the module's source: every line of a code block keeps the line it has in the document, and every other line is empty. A diagnostic therefore names the document's own line, and an edit to the prose changes no declaration and no content hash.

Unmarked blocks, and blocks marked <!-- compile: cumulative --> or <!-- compile: qiec-cumulative --> on the line before the fence, are code. The other markers make a block something other than code of the module:

Marker The block is The documentation gate
standalone, qiec a separate module compiles it (qiec also lowers it through QIEC)
file PATH the file PATH of the document's package writes every file block below one root and checks each file as qvr check does, so the files may import each other, declare holes, and link them
refused "TEXT" a module shown for its diagnostic requires the compiler to refuse it with a message containing TEXT
false, because REASON an illustrative fragment requires the reason, which says why the fragment cannot be a module

A block whose body includes a corpus file with --8<-- shows that file and is not code either; import the file's module to use it.

A module path names a .qvr.md file as it names a .qvr file: pkg.arith is <root>/pkg/arith.qvr or <root>/pkg/arith.qvr.md. A root that provides both is refused as ambiguous.

Checked examples

```qvr example
expect triple(2) == 6
expect double(21) == 42
```

A fence written ```qvr example holds the steps of an example: let bindings and expect lines. It is a test of the module, run by qvr test, named by the document and the fence's line (<document>:<line>), and a failed expectation names its own document line. An example is not code, so it enters no content hash.

References to declarations

The prose names a declaration with the role {qvr:ref}`Name`:

Tripling is {qvr:ref}`triple`, and {qvr:ref}`A.quad` is a declaration of the imported module `A`.

A reference resolves like a name written in the module's code: a bare name is one of the module's declarations or a member of one (a deduction's Grammar.Item, a derived Term.eq), and M.name is a declaration of an imported module M. A reference that resolves to nothing fails the document's check with its line and column, so renaming a declaration breaks the prose that names it. A reference inside a fenced block is part of that block, not prose.

Woven experiment results

```{experiment} tripled
```

An {experiment} directive names an experiment of the module. qvr weave runs the experiment (or reads its stored records) and writes the directive's body:

```{experiment} tripled
:hash: 6d0c4b0e...
:program: 9a1f3e2c...
value = 12
```

The body records the experiment's hash, the content hash of its program, the pins of its data, and the result: a run's value, or each site's posterior mean and standard deviation. A swept experiment writes one line per point, prefixed by its values.

A woven result is stale when the experiment's hash is no longer the one it records: after its program, data, fixed values, method, or seed changed. qvr weave --check refuses a stale result, an unwoven directive, a directive naming no experiment, and a broken prose reference, each with its line and column, and writes nothing:

qvr weave doc.qvr.md            # write every directive's result
qvr weave --check doc.qvr.md    # refuse stale results and broken references

Documents in the documentation

The documentation's Markdown pages are checked the same way: each page compiles as the modules it contains. A page's cumulative blocks form one module; each unmarked block of an ordinary page, and each standalone or qiec block, is a module of its own; a compile: false fragment is not compiled. A literate page is also checked for broken references and stale results.