18 KiB
Contract proof classification and report grammar candidate
Non-normative continuation of RFC 0039 and the accounting/status candidate, at a4181f2. These are proposed closed rules, not implemented verification or an accepted schema. All executable P predicates remain available; this classifier only distinguishes two runtime statuses. Module-claim metadata, source clause elaboration, diagnostic collection, adoption and vertical delivery remain part of full P6-6.
Eligible helper and terms
Classification runs only after ordinary verification, P/S membership and virtual binding checks succeed. An eligible helper has exactly one block, ends in a value ret, and contains only the following well-typed operations. Its result is bool for a predicate or Data for old. All instructions, including unused definitions, must qualify.
| Operation | Operand positions, including one instruction position | Logical node positions |
|---|---|---|
const.I, const.bool |
1 | 1 |
copy |
2 | 0 |
Data struct.make with n fields |
1+n | 1 |
Data struct.get |
2 | 1 |
shared binding-derived borrow.field |
2 | 1 |
load of Data from such a view or its root |
2 | 1 |
borrow.end of an owned child view |
2 | 0 |
eq/ne.I, eq/ne.bool, lt/le/gt/ge.I |
3 | 1 |
and/or/xor.bool |
3 | 1 |
value ret |
2 | 0 |
I is an existing AIR integer type, including isize/usize. Constants are compared by their exact accepted mathematical values; classification assumes no target pointer width. No bool ordering, integer bitwise operations, array operations, calls, assertions, casts, arithmetic, checked extraction, owner/enum views or branch instructions qualify. The predicate may execute any of those permitted by P; it then remains runtime-classified. A helper's signature cannot contain mutable parameters under the boundary rules. A view is eligible only when its root is a helper's bound shared parameter and every edge is an ordinary struct-field projection. Loading a whole linear/authority value does not become eligible.
Terms form a typed immutable DAG: constants; original Data parameter identities; opaque current Data SSA identities; field projections; Data records in declared field order; scalar comparisons; bool combinations. Copy aliases its input. Projection of a constructed record selects that field; projection of a parameter/opaque record appends the nominal field identity. Other records stay structural, with no expansion of arrays or repeated subtrees. Constant identity includes its type and exact mathematical value; field identity includes its resolved nominal declaration and ordinal. Paths are parent-linked nodes, not copied strings.
Bindings substitute in parameter order. By-value Data and explicit ref of Copy Data use the same value terms. Shared observations of original Data parameters likewise use their immutable terms. A shared root originally typed ref<T> is identified by its enclosing parameter ordinal and field path, independent of entry/return epoch. An original exclusive/owned root instead includes its observation boundary: entry for old/pre, the particular ret for post. The helper's temporary ref type does not make an exclusive/owned enclosing root stable. Slice operations and borrow-result observations are unsupported initially. Different parameter identities are never equated because a particular caller aliases them.
Old is normalized once at entry, with its own bindings. Its result is substituted for a snapshot binding; it never contains a body SSA identity. Result/Copy make origins follow only const, copy, Data struct.make/get and original Data parameters. Every other Data value, including a block parameter or load, is an opaque identity containing function/value identity and this boundary. Traversal stops there; loops do not turn it into an entry term. Linear make representations are unsupported for proof initially, while their runtime construction remains fully supported. Modify is always runtime-classified.
Terms of bool type evaluate to true, false or unknown. A constant supplies its value. A comparison evaluates typed constants exactly; identical scalar terms give true for eq/le/ge and false for ne/lt/gt. Otherwise it is unknown. A definitely true/false bool operand can be folded to its typed constant before eq/ne; two identical bool formula terms can still be equal when their truth is unknown. AND/OR use strong three-valued logic, XOR with unknown is unknown. No arithmetic identities, path facts, precondition facts, caller facts, commutative operand sorting or SMT are imported. A raw symbolic bool is unknown. Truth is proved only when the returned formula is definitely true.
Runtime pre has one universal entry boundary. Post must be true at every syntactic ret, including unreachable blocks, and has no proof when there are zero ret sites. Make has one construction boundary. A constant false formula is reported as not proven; mandatory execution still handles its trap. Classification never rejects a module, eliminates a check, changes fuel or promises termination.
Once published, this classification, its limits and reasons are frozen as part of the contracts feature's semantics (spec 07 §8.7 and the feature registry's published-feature immutability rule). Toolchain prover strength, cache strategy and optimizations cannot classify more or fewer rows as proven. Changing these rules needs a separately specified compatibility path through an RFC; adding a feature does not itself authorize changing existing format-0 constructs or report bytes.
Exact logical work and fallback
For each runtime record independently, let H be the sum of the table's operand-position charges for its predicate and N the sum of logical node positions. These counts are before folding/interning, including unused definitions. Let n be the predicate binding count and B the prescribed boundary count. For a post using old, O is old helper's operand-position sum plus its binding count plus its logical node count; otherwise O=0. Let D be the operand-position cost of the union of origin-DAG-reachable result/make definitions, counted once per function/value identity for this record across every prescribed boundary, including CFG-unreachable blocks. Post has result-origin work exactly when a binding names result, even if that helper parameter is unused; otherwise D=0.
Origin constants, copy and struct.make/get use the same position charges as the table. A terminal original Data parameter, block parameter or other opaque Data definition costs one; its operands are not traversed. Pre has D=0. Type/function/field metadata, result identifiers, literal digits, source positions and cache hits add no position. Shared origin definitions are charged once, whether or not an implementation had cached them earlier.
C = H + O + D + B * (n + N)
limit = 1,048,576 positions per record
Compute with checked unsigned 64-bit counters; overflow or C>limit gives no proof. C=limit is within budget. Under current structural limits C cannot overflow u64; that fallback is defensive, not a reachable current-module test. Helpers/origin DAGs may be cached but the logical charge is independent of cache state. This bounds classification positions, not elapsed time or every module analysis. Structural type/property scans retain their independent memoized bounds. Unrelated body instructions do not affect a pre's cost.
Choose exactly one reason in this order, without sampling a run:
- Assume/document:
not_attempted, with no cost or classifier invocation. - Modify:
update, with no cost. - Post with zero returns:
no_normal_return, with no cost. - Noneligible predicate:
unsupported_predicate, with no cost. - Used noneligible old helper:
unsupported_old, with no cost. - Unsupported binding/linear make:
unsupported_binding, with no cost. - Overflow or over-budget C:
budget, with cost"overflow"or the integer C. - Every boundary definitely true:
closed_scalar, proof proven, cost C. - Otherwise:
not_established, proof none, cost C.
Eligibility checks do not follow arbitrary helper calls or execute predicates. Unsupported forms and budget fallback preserve acceptance and runtime enforcement. The report's reason is an explanation of the closed classifier, not a repair proposal or a frequency estimate. These exact charges refine the earlier accounting candidate's open node/operand conventions.
Initially unsupported bindings are observations of a borrowed/slice result, or a linear make representation. Other verified bindings supply Data terms or shared view anchors, including unused slice/authority-containing anchors; an unsupported operation on an anchor already makes the helper noneligible. Runtime reasons other than closed_scalar have proof none. Only explicit assume/document has proof not_attempted.
Report rows and canonical form
For modules declaring the proposed contracts feature, append a contracts object after existing unsafe. The proposed checked-type and assumption extensions depend on it, so their full declared dependency closure also triggers this section. Otherwise emit exactly today's report bytes. Its fixed field order is rows, totals, then types under the checked inventory refinement. Feature-declared modules with no truth sites emit an empty rows array and four zero totals; types is empty when no checked identity occurs. Every new object has the field order specified below or in that refinement; no optional omissions, unknown keys, whitespace or floating-point numbers. Use the existing canonical-record string escaping.
Rows iterate functions, blocks and instructions in textual module order, one per pre/post or make/modify operation. Old/view/take are not truth rows. Snapshot helper execution remains in the call graph. This gives stable declaration-site counts independent of callers, number of returns or invocations.
Row field order:
site, kind, scope, predicate, callback, bindings, status, proof, enforcement, boundaries
kind is pre, post, make or modify. Scope is boundary for pre/post and value for make/modify: the latter establishes a persistent checked claim, rather than a relation that holds only at a function boundary. Predicate is the resolved AIR function name, without @. Callback is the resolved modify callback name, otherwise null. Checked identity still includes R and the resolved predicate; a later metadata integration must represent it without a source-only claim.
Site and boundary-position object field order is function, block, inst, source. Function/block are AIR names without prefixes; inst is the existing zero-based instruction index. Input-text AIR line/col is omitted so comments/formatting do not affect semantic rows. Source is {file,line,col} from the supplied lowering map, or null, with one-based line/col. Use the mapping for that exact instruction; never guess a variable name or an enclosing block. No filesystem lookup, path rewriting or source hashing is performed by report generation. Borrowing truth or positions from a caller is forbidden.
The source field requires an explicit adoption-time clarification to spec 10 §6.1: semantic report data depends only on verified AIR, while locations may use supplied frontend provenance. Currently the CLI passes only the module to report.write; implementing this field needs that provenance context. It changes no contract-free report. An AIR-only input has source null even if its AIR file happens to have a .aji-looking name. Equal verified AIR with different supplied source maps has equal semantic rows: replacing every source value with null gives identical bytes, while the original reports may differ. A generated helper's location is never substituted for the source clause/operation.
Bindings are in helper parameter order, each {kind,name,view}. Kind is parameter, snapshot, result or representation; name is the actual AIR value name without % for parameter/snapshot/representation, null for result. View is value or ref. Pre/post retain their explicit bindings; make/modify have one implicit ref representation binding. Make names its R operand, modify names its capsule-borrow operand. A post's return operand is identified by the corresponding boundary position, not an invented entry result SSA name.
Proof field order is result, reason, cost, limit; result is proven, none or not_attempted, reason is from the ordered list above, cost is null/integer/"overflow", limit is always 1048576. Enforcement is runtime, assumed_unsafe or documented. Status is the corresponding uppercase spec 13 name; only runtime+proven yields PROVEN_STATICALLY. Assumed/documented use not_attempted regardless of formula truth.
Boundaries are positions in textual order: the pre/make/modify site itself, or all ret positions for post. A zero-ret post has an empty list. Documented clauses retain their syntactic coverage even though no helper executes. Totals field order is PROVEN_STATICALLY, CHECKED_AT_RUNTIME, ASSUMED_UNSAFE, UNPROVEN; each counts rows of that status exactly once. A snapshot is not a fifth status or a truth guarantee.
The module/source candidate proposes rooted state declarations and a type inventory after rows/totals so unused declared invariants are visible. Its exact inventory schema and universal-root classification still need integration; the component vector here specifies site rows/totals only. Persistent checked state and pre/post relations have distinct scopes, but site rows cannot replace declarations, root mappings or coverage. Full P6-6 cannot close until the chosen module model and its report representation are integrated.
Call/effect/check accounting
At each executable pre/old/post site, add its helper to calls in instruction order, deduplicating with ordinary call sites. Executable includes runtime and assume, independently of backend elision. A post contributes one call edge at its declaration position, not one per ret; the edge denotes possible invocation, not execution order. Documented clauses add no executable edge. Make adds its predicate; modify adds callback then predicate. Adoption must extend spec 10's calls definition to named invocations of these gated operations, explicitly excluding document; contract-free meanings/bytes stay unchanged. The callback's declared/used effects propagate as for an ordinary call, including trusted-boundary discharge. P helpers have no declared effects but their ordinary call edges remain visible.
Helper instructions contribute runtime_checks/proven_checks only in their own function rows. Do not copy their asserts, bounds checks or counts into each caller/return. Existing checks fields retain their instruction meanings; contract predicate tests appear in contracts rows rather than new assertion counts. A PROVEN contract still executes and does not increment proven_checks.bounds. Assume variants contribute a direct unsafe operation at their prefix site and unsafe used effect, independent of whether the helper is true. Document adds neither. Rejected modules produce diagnostics and no audit object.
Required implementation vectors cover exact and one-over proof budgets, cache-independent costs, shared DAGs and many returns, unsupported unused instructions, typed signed/bool comparisons, mutated old/current roots, opaque loop origins, source maps and missing maps, explicit modes, empty feature sections, site totals versus coverage, snapshot-only call edges, callbacks/trusted effects, and unchanged contract-free bytes. The candidate allocates no code/feature and enters no normative/source bundle.
Research evidence
Five current-language probes at a4181f2 confirm byte-identical reports after AIR comments/whitespace changes, source versus its lowered AIR, and before/after an unsafe T0009 trap. A fourth verifies a shared Pair predicate using i8 limits -128/127, field views/load/end, copy/struct.make/get and bool eq/and/or/xor; it returns true, with the expected ordinary call edge. A fifth verifies the ordinary AIR helper/8,191-return structure used below, without any contract operation. Runs use fuel 10,000. These constrain compatibility and eligible operation shapes; no contract opcode or report section is implemented.
Fifteen scalar/cost vectors pin the proposed arithmetic and truth rules. Symbols denote distinct u64 observations unless explicitly typed otherwise; repeat multiplies boundary count without duplicating the stored formula. The formula representation is research data, not an AIR encoding. Independent concrete evaluation checked 328,965 assignments from 0..255 and all 27 three-valued Boolean combinations/completions. Every classified-true formula held over the sampled assignments; each not_established example had a concrete counterexample. This finite check does not prove the full classifier or the verifier implementation sound.
The exact-limit vector is attainable within ordinary structural bounds: a 63-parameter helper has 64 bool constants, a one-field Data struct.make, 30 copies and one ret (H=128, N=65). One post covers 8,191 syntactic returns, binds original parameters and has D=O=0, so C=1,048,576. Replacing one unused binding with result from an original parameter gives D=1 and budget fallback at 1,048,577, without changing truth or acceptance. These are proposed classifier vectors, not an executed new-contract test. Old-identity vectors use a two-position identity old helper plus one binding, O=3; Range field predicates have H=17, N=5.
The report component vector pins field order and all four statuses, including a two-return post, a modify callback and a constant Range make. It has six rows and seven covered boundaries, with totals counting six sites. It is a proposed contracts component, formatted for inspection, not output captured from today's CLI. Its conceptual function f has pre sites 0..2, old at 3 and post at 4; left/right return different Data parameters, yielding the cost-16 vector above. Function g's modify row is runtime/update over StateRep with an unsupported classifier predicate. Function h constructs Rep {lo:0,hi:5} in raw and makes its checked value at instruction 3. The inventory lists StateRep first through g's signature, Rep second at h's make, and an unused always-true u64 witness last; ordinary declarations and earlier signatures contain no checked type. Universal within stays runtime/not_established even though h's make is proven. No module-state declaration or source provenance is implied.