Design decisions

20. Program structure: the @!multitest divider and top-level return (0.2.2, approved 2026-11-20)

Revises 6.3. A single-test program and a multitest program have the same shape: flat, unindented top-level statements, run top to bottom. Neither needs a fn solve() wrapper.

  • 20.1 @!multitest is a divider. @! marks a file-level directive: it stands on a line of its own and is in force from that line down, where @ always decorates the single item below it (the same split as Rust's #![...] and #[...]). @!multitest is the one directive, and appears once per file. Everything above it runs once, before the test count is read: sieves, Comb tables, anything shared. Everything below it runs once per test case. The count t is read automatically at the divider (@!multitest(until_eof) reads cases until the input ends instead).

    let sv = Sieve(1e6)                   // above: built once
    
    @!multitest
    let n: 2..=1e6 = read()               // below: once per test case
    print(sv.is_prime(n))
    

    Delete the divider and move the sieve down and what is left is the single-test program for the same problem. That symmetry is the point: turning a solution into a multitest one is adding a line and lifting the shared setup above it, never re-indenting.

  • 20.2 Each test case is a fresh scope. Every let, var, domain and fn below the divider is created anew for each case and gone at its end, so nothing leaks from one case to the next. A @memo fn below the divider belongs to its case: its cache starts empty every time. State that must outlive a case is declared above the divider, where it is visible to (and, for a var, writable from) every case.

    @!multitest
    let n: 1..=2e5 = read()
    domain V = 1..=n
    let a: [V]Int = read()
    
    @memo
    fn best(i: V) -> Int:                 // cache keyed by this case's V, emptied for the next case
        if let j = i.next:
            return max(a[i], a[i] + best(j))
        a[i]
    
    print(best(V.first))
    
  • 20.3 Top-level return. A return outside every fn takes no value and ends the current unit of work, from any depth of loops: the whole program in a single-test file (and above the divider), the current test case below the divider. Output printed so far is kept and flushed. This is the early exit that fn solve() wrappers existed to provide.

    let n: 0..=2e5 = read()               // single-test: `return` ends the program
    if n == 0:
        print("empty")
        return
    
    @!multitest
    let n: 0..=2e5 = read()               // multitest: `return` ends this case, the next one starts
    if n == 0:
        print("empty")
        return
    

    The two bodies are identical. return x at top level is an error (there is no caller to receive x).

  • 20.4 @spec and @checker see the same inputs in both modes. They read the top-level input lets by name. In a single-test file those are the program's inputs; below the divider they are the current test case's inputs, and the @spec / @checker runs once per case. The declaration is written the same way in both files.

    @!multitest
    let n: 1..=5 = read()
    print(n * n)
    
    @checker
    fn check():
        let x: Int = read_output()
        assert x == n * n                 // `n` is this case's `n`
    
  • 20.5 What is not kept. @multitest fn solve(): is no longer part of the language as taught: the divider covers precomputation (the one reason the wrapper existed) and top-level return covers early exit. A second @!multitest in one file is an error, as is any other @!name and a @!multitest that is not at top level. A bare @multitest as a divider is the deprecated spelling of @!multitest. Problems whose case loop has another shape (a count per group, a sentinel) still write a plain for _ in 0..tests: or read_until loop.
  • 20.6 Prototype status. proto/cpc and proto/cpi implement 20.1 to 20.3 and 20.5's errors; crates/cpl-cli/tests/cases/program/ holds the cases, run by both. The prototype still compiles the older @multitest fn solve(): wrapper and the bare @multitest divider, so existing ports keep building until they are migrated; both should then become errors that point to @!multitest. @spec and @checker are not implemented in the prototype, so 20.4 is specified here and not yet tested.