Skip to content

Z3 encode

opulse.solver.z3_encode

Encode operators as z3 terms following docs/reference/semantics.md (exact mode).

A value is a pair :class:ZVal (defined, value): defined is a z3 Bool that is false exactly when the spec gives NaN, value a term of the operator's result sort that matters only when defined holds. Division and remainder truncate toward zero (z3's own / and % are Euclidean and must not be used directly).

Every base operation is encodable when the semantics has a bound B: all values then lie in [-B, B], so @ (reverse), & (digit sum) and $ (popcount) are encoded exactly by a fixed number of decimal digits / bits, each a division by a constant. Without a bound only + - * / % neg # are encoded; :meth:Z3Encoder.expressible says which operators qualify.

Recursion is unrolled. By the termination argument of spec section 4.1 a template-A operator unfolds at most m - 1 times and a template-B operator at most T times, so unrolling one level further than that is exact; the tests compare the encoding with the reference interpreter to confirm it.

Z3Encoder

Builds z3 terms for operator applications; oracle supplies definitions.

admit

admit(value: int) -> None

Record a literal that can reach a digit operation (call before encoding it).

apply

apply(operator: Operator, args: list[ZVal]) -> ZVal

The z3 value of operator(*args).

reset

reset(seconds: float | None = None) -> None

Start a new formula with a fresh node budget and memo table.

trunc_div

trunc_div(a, b)

Quotient rounded toward zero, for b != 0.

recursion_control

recursion_control(
    definition: Definition,
) -> tuple[str, ast.Expr, int | None]

(loop variable, control expression, span of the control value) for the two templates.

recursion_depth

recursion_depth(definition: Definition) -> int

Maximum number of self-calls for the two recursive templates (spec 4.1).

evaluate_concrete

evaluate_concrete(
    encoder: Z3Encoder, operator: Operator, args: tuple
) -> object

Evaluate an expressible encoding on concrete typed inputs.