Skip to content

Tests and exact evaluation

This page states what a test, property, or example declaration means, what an exact run of the reference machine computes, and what a mutation run establishes. The surface and the command line are in the language reference.

The exact number domain

A run of the reference machine carries its numbers in a number domain (NumberDomain). A floating domain holds every Real and LogWeight as a tensor of a floating dtype. The exact domain holds them as rational tensors (RationalTensor), whose entries are elements of the field of rational numbers \(\mathbb Q\). Int and Bool values are exact in every domain and are the same in both.

The machine's rules do not mention the domain: every rule that computes a number does so by a tensor operation. Write \(\llbracket e \rrbracket_D\) for the value a computation \(e\) returns in domain \(D\). For each tensor operation \(f\) the exact domain does one of two things:

  • if \(\mathbb Q\) is closed under \(f\), it computes \(f\) on the rational entries with no rounding: the field operations, negation and absolute value, comparisons, floor, ceil, trunc and round, an integer power, sums, products, means, extrema and cumulative sums along axes, matrix products, and every structural operation (indexing, gathering, stacking, concatenation, reshaping, transposition);
  • otherwise it stops the run with InexactOperation naming \(f\). The operations \(\mathbb Q\) is not closed under (NOT_CLOSED) include exp, log, sqrt, the trigonometric and hyperbolic functions, erf, lgamma, sigmoid, softplus, softmax, logsumexp, and the Cholesky factor. An operation in neither set is refused because no exact implementation is declared for it.

So the exact run is total on the computations built from closed operations alone, which are the finite models: enumerations of finitely many worlds weighted by rational probabilities, handlers that sum over choices, and charts over finite semirings. On those, \(\llbracket e \rrbracket_{\mathbb Q}\) is the exact value, and for every floating domain \(F\) with unit roundoff \(u\), \(\llbracket e \rrbracket_F\) is \(\llbracket e \rrbracket_{\mathbb Q}\) rounded at each of the finitely many operations the run performs. The tests of tests/qiec/test_exact_numbers.py pin the difference between the two on finite models, including the gallery's referent model, at a relative \(10^{-12}\).

Where a number enters an exact run it is read exactly. An integer or truth value is the integer it is. A host float is the shortest decimal that rounds to it (decimal_fraction): the literal 0.1 is \(\tfrac{1}{10}\), not the binary fraction nearest to it. This is the decimal as written for every literal of at most fifteen significant digits. A floating tensor met inside an exact run carries no record of how it was rounded, so it is admitted only when each entry is an integer, which a floating format holds exactly: the zeros and units that machine code writes. Reading a rational as a float is refused inside a run; a report rounds it to the nearest float only at the host boundary.

The rational semiring

RationalAlgebra is the semiring \((\mathbb Q, +, \cdot, 0, 1)\) of the algebra hierarchy, a field and so a semifield and a ring (proof). Its operations take and return rational tensors. An operand that is not rational is read as above, so composing stochastic matrices in it (RATIONAL.join(RATIONAL.tensor_op(P, Q), k)) yields the exact rational transition matrix the float composition approximates.

Test bodies

A test body is a sequence of steps over a scope \(\sigma\) of named values. Terms are let expressions evaluated on the host:

  • a numeric literal denotes an exact number: an integer literal an integer, a decimal literal the rational it spells;
  • \(+\), \(-\) and \(\times\) are exact and keep integers integral, and \(/\) is division in \(\mathbb Q\), so 1/4 denotes \(\tfrac14\);
  • a name denotes \(\sigma(x)\) when bound, and otherwise the constructor of that name with no fields;
  • an application \(f(a_1, \ldots, a_n)\) denotes \(\llbracket f(v_1, \ldots, v_n) \rrbracket_D\) when \(f\) is a computation or program of the module, where each \(v_i\) is the value of \(a_i\) checked against \(f\)'s \(i\)-th parameter type, and the data value \(f(v_1, \ldots, v_n)\) when \(f\) is a constructor, its fields checked against the constructor's field types at the expected instance of its family.

Name resolution is by scope: a binding shadows a constructor of the same name. Checking an argument against its parameter type is what gives a type to the terms that carry none: an integer passed to a Real is that real, and a constructor of a family with parameters takes the instance its parameter expects. A constructor of a parameterized family with no expected type is an error.

let x = t extends \(\sigma\) with \(x \mapsto\) the value of \(t\). expect t requires \(t\) to denote true; the body fails at the first expectation that does not hold and errs at the first term that cannot be evaluated. When \(t\) is a comparison \(a \bowtie b\), both sides are evaluated and the relation is checked:

  • == and != compare structurally: numbers by exact value, tuples componentwise, data values by constructor and fields;
  • when either side is or contains a real, an equality states its meaning: exactly requires structural equality in the exact domain, and within t requires \(|a - b| \le t\) componentwise in a floating domain, the tolerance \(t\) being part of the test's content;
  • the orderings compare numbers.

A test's domain is exact unless it declares numbers, or unless an expectation states within, which makes it float64. An exactly in a floating test, a within in a test declared exact, or an exactly or within on anything but an equality is an error of the declaration, reported before anything runs.

An example means the same as a test. A doc-comment example is the example its indented lines spell, with the documented declaration as its subject.

Properties

A property \(\forall (x_1 : T_1, \ldots, x_k : T_k).\; B\) holds when \(B\) passes for every assignment. The runner checks it in one of two ways, chosen by the declaration alone:

  • Exhaustively. When each \(x_i\) ranges over a finite set \(S_i\) (a list in [...], or a finite type), the runner evaluates \(B\) under every assignment in \(S_1 \times \cdots \times S_k\). A pass is then a proof of the property, over those sets. A product larger than the declared limit is refused before any case runs.
  • By generation. Otherwise it evaluates \(B\) under cases assignments drawn from a source seeded with seed, the \(n\)-th at size \(1 + \lfloor n \cdot \textit{size} / \textit{cases} \rfloor\) (Claessen and Hughes, 2000). A pass is evidence, not proof, and the report says how many cases ran.

A type's generator (generator_for) is determined by the type's structure: Bool, Unit, Int, Real (rationals), String, products, tensors at literal shapes, and closed families. A family's values are its constructors applied to generated fields; a field whose type is the family itself (a recursive position) takes a share of the remaining size, and at size zero only constructors without such a field are chosen, so every drawn value is finite. A family is finite, and checked exhaustively, when it has no parameters or indices and no constructor reaches the family again. Families the structural generator cannot cover are refused with the declared property that rules them out: an opaque family (its constructors are not all known), an indexed family (which constructors build a value depends on the index), a constructor with existential binders, and a family reached again through another family. Any of them is generated instead by a module computation F__arbitrary(size : Int) : F, which the runner runs with a seed drawn from the property's source.

When an assignment fails, the runner shrinks it greedily: it tries the shrink candidates of each variable in turn and keeps the first candidate assignment whose body still fails (an assignment that errs is not kept), and stops when no candidate fails or shrinks candidates have been tried. Each candidate is smaller than the value it replaces under a well-founded order: an integer \(n\) by \((|n|, n < 0)\), a rational \(p/q\) by \((q, |p|, p < 0)\), a string by length, a listed value by position, a truth value with false below true, a data value by its number of constructors and then field by field, and a tuple componentwise. So shrinking terminates, and the reported counterexample is a failing assignment none of whose candidates fails, unless the shrinks budget ran out first.

Content identity and caching

The code a test checks is identified by the module digest of module_hashes over the checked module's declarations, which is invariant under renaming and reordering (code_digest). A test is identified by the digest of its declaration's structure with source positions removed (declaration_digest). Neither includes the other: tests are not statements of the module, and the computations the runner adds to type a property's variables are left out of the code digest.

A test's outcome is a function of its declaration, the code, and its options (the domain, seeds, and sizes are among them), since every run is deterministic given its seed. So a pass recorded under the triple (test digest, code digest, options) is the outcome of running the test again, and ResultCache reuses it. A failure is never reused.

Mutation analysis

A mutant is the module with one change to its code (mutants_of): a deduction rule or top-level rule removed, a lexical entry removed or given another entry's category, or a literal replaced. Its tests are unchanged. The tests kill a mutant when one of them fails or errs on it, and the mutant is invalid when the compiler rejects it. A surviving mutant shows one of two things: the suite accepts a module that differs from the original in a rule, entry, or constant, so it does not pin that part's behavior, or the mutant is equivalent to the original, which is undecidable in general and is left to the reader of the report. A suite with no survivors over a module's rule set rejects the removal of every rule, which is what the per-rule showcase sentences of a grammar are meant to establish.

Cited sources

Koen Claessen and John Hughes. QuickCheck: A Lightweight Tool for Random Testing of Haskell Programs. In Proceedings of the Fifth ACM SIGPLAN International Conference on Functional Programming (ICFP '00), 268–279. Association for Computing Machinery, 2000. URL: https://doi.org/10.1145/351240.351266, doi:10.1145/351240.351266.