Issue #1367: A test with parameters: prove it for all values first, sample only what the prover cannot decide

Open and answer this issue on GitHub.

tests, decision, solver · opened 2026-11-07 · updated 2026-11-22 · 0 comments

What this is about, from zero

A test with parameters says something about every value of its parameters, not about one example:

fn square(x: i32) -> i64 { return (x as i64) * (x as i64) }

test "a square is not negative" (x: i32) {
    assert(square(x) >= 0)
}

Other languages run such a test as a property test: a generator draws random values (QuickCheck, Hypothesis, Rust’s proptest), maybe tries some edge cases first, and the test passes if no drawn value fails. That is a sample. For i32 it may try a thousand of four billion values, and for two parameters the space is practically endless. A pass says “nothing found”, not “it holds”.

Nikaia already has a prover that shows an assert holds for every value of a function’s parameters (ADR-269, ADR-270, ADR-361). The question is whether a test with parameters should first go to that prover, and only be sampled where the prover cannot decide. Then the whole combinatorics is covered by a proof instead of being tried.

Where the question comes from

The owner asked in a session of #1053 (2026-11-07) whether tests could be “proved”, in the sense that all possibilities were covered: a generator that in other languages must try the whole combinatorics, settled in Nikaia by a mathematical proof. Nothing is blocked on the answer today. It decides what Part III §14.3 means before anyone builds it.

What is decided and what is not

Today, checked

On nikaia 0.1.0 of 2026-11-07 a test with parameters does not parse:

$ nikaia check p.nika        # p.nika = the example above, plus fn main
error: Expected `{` here, but found `(`.
  --> p.nika:3:31
 3 | test "square is not negative" (x: i32) {
   |                               ^

No generator, no fuzzer, no Generator trait exists in std. So nothing built has to be changed. Only the specification text would.

How others do it

Tool What a parameterized test is Covers all values?
QuickCheck, Hypothesis, proptest random generation, shrinking of a failing case no, a sample
SmallCheck (Haskell) all values up to a size, exhaustively only up to the bound
Dafny, F* lemmas, Lean theorems a lemma with parameters, proved for all yes, but a failed proof is an error, not a fallback
Liquid Haskell, SPARK contracts proved; tests stay separate yes, for contracts
Kani (Rust), CBMC kani::any() inputs, bounded model checking yes, within loop bounds

Kani is the closest: a test-like harness whose inputs are symbolic, so a pass covers all of them. No mainstream language falls back from proof to sampling inside the same test. That combination would be Nikaia’s own.

Options

A. Do nothing: §14.3 as written, sampling only.

B. Prove first, sample only what stays unknown. nikaia test hands a test with parameters to the prover like a function body, with its parameters free:

C. Prove only; a test with parameters that is not proved fails.

D. Sample only, then prove what the samples suggest (properties mined from runs, then proved as contracts of the function under test).

Sub-question for B: how a parameter’s domain is stated

  1. An assert at the top of the test, read as the test’s precondition, the way ADR-269 D5 reads one in a function: test "t" (age: i32) { assert(age >= 0 && age < 150) … }. Values outside are not inputs; the generator in the unknown case draws from inside. No new syntax.
  2. The type’s own rule (ADR-381 properties, a type’s invariant): a User whose rule says age >= 0 brings it with it.
  3. Generator::arbitrary() stays for the sampling fallback only, and is not read by the prover.

Recommended: 1 and 2 for the prover, 3 for the fallback.

Recommendation

B, with domains as 1 and 2 above. It keeps §14.3’s sampling as the fallback, so no test becomes worse than in other languages. Where the prover succeeds, it turns “tried many values” into “holds for all values”. That is the point of having a prover in the language. It needs no new syntax and no forall in the logic.

What it needs, once decided:

  1. a new decision in ADR-269 (or its own record) for tests with parameters, saying how D11 applies to them;
  2. §14.3 rewritten to describe the three outcomes and the domain rule;
  3. the build (parser, test runner, prover entry), as its own ready issue with a To build list.

Risk, honestly: the prover’s reach is integers, indexes and lengths today. For tests over strings or nested structures, most results will be “unknown → sampled” for a long time. Option B is then no worse than A, but the gain shows only where the prover is strong. A second risk: a refuted test whose counterexample does not fail when run means a prover fault, and it must be reported as one, never silently passed.

Found while #1053. Related: #640 (ADR-361, invariants from outside programs and test runs).


Generated by Claude Code

Back to Open Issues