Piecewise polynomial certificates #
This module implements a Lean checker for one-dimensional piecewise-polynomial certificates.
Why this exists:
- In TorchLean’s “external producer, Lean checker” workflow, an external tool (Julia, Python, ...) can fit or search for a piecewise-polynomial surrogate and export a compact JSON artifact.
- Lean then checks basic algebraic consistency conditions on that artifact.
Our checker is conservative: it checks exact equalities over Rat with no floating-point
tolerance.
Optionally, you can also run an executable float32 cross-check via checkJsonIEEE32ExecExact:
if every rational in the certificate is exactly representable as a finite IEEE-754 binary32
(ExecFloat.Binary 8 23), we re-run the endpoint equalities under ExecFloat.Binary 8 23
arithmetic. This is useful
when the producer is actually operating in float32 and you want to confirm the certificate’s
equalities hold under the same semantics.
What is checked in format = "piecewise_poly_v0":
xsis strictly increasing,yshas the same length asxs,- there is one
pieceper interval $[\mathrm{xs}[i],\mathrm{xs}[i+1]]$, - each piece’s
(lo, hi)matches the corresponding interval endpoints, - the local-coordinate polynomial interpolates the endpoints exactly: $p_i(\mathrm{xs}[i])=\mathrm{ys}[i]$ and $p_i(\mathrm{xs}[i+1])=\mathrm{ys}[i+1]$.
This certificate format focuses on endpoint and piece consistency. It does not claim global range bounds for higher-degree polynomials; those require additional machinery such as Bernstein bounds or interval arithmetic with derivative-based splitting.
References (background):
- de Boor, A Practical Guide to Splines (1978).
- Certificate checking pattern: export an artifact, then check it in a small Lean checker core.
Utilities: exact rationals in JSON #
Parse a rational in the format emitted by TorchLean’s Arb helpers:
- integer:
"5","-3" - fraction:
"5/2","-7/10"
We avoid JSON numbers here because they are stored as Scientific and are not guaranteed to
round-trip exactly for large integers.
Instances For
Certificate schema #
Parsed certificate payload for piecewise_poly_v0.
degree is redundant (it should equal coeffs.size - 1), but it is useful for fast validation.
- degree : ℕ
Polynomial degree
d(so each piece hasd+1coefficients). - n : ℕ
Number of knot points.
- xs : TorchLean.Tensor ℚ [self.n]
Knot x-coordinates as a rank-one tensor of length
n. - ys : TorchLean.Tensor ℚ [self.n]
Knot y-values as a rank-one tensor of length
n. - pieces : Array PolynomialPiece
One piece per interval $[\mathrm{xs}[i],\mathrm{xs}[i+1]]$.
Instances For
Polynomial evaluation #
Evaluate a polynomial in local coordinate $t$ using Horner’s rule.
Given coefficients $[a_0,a_1,\ldots,a_d]$ representing:
$$ a_0+a_1t+a_2t^2+\cdots+a_dt^d. $$
This helper is generic so we can reuse it for Rat (exact checking) and for ExecFloat.Binary 8 23
(executable float32 semantics).
Instances For
Parsing #
Parse one polynomial segment from the certificate JSON object.
Instances For
Parse the piecewise_poly_v0 JSON payload into a structured certificate.
Instances For
Checking #
Check a piecewise-polynomial certificate exactly over Rat.
This is the smallest checker core for this format: it uses exact rational arithmetic with no tolerances, so a passing check means the certificate's equalities hold exactly as stated.
Instances For
Check a piecewise_poly_v0 certificate payload.
Raises IO.userError on the first mismatch; prints a short success message otherwise.
Instances For
Attempt to convert a rational to an exact finite ExecFloat.Binary 8 23 value.
This is used for optional “float32 semantics” checking: we only accept values that are exactly representable on the binary32 grid, so equality checks are meaningful at the executable IEEE level.
Implementation note:
- FloatLib rounds the rational outward in its binary32 model; packing that model preserves its bits. We accept only when the lower and upper rounding compare equal.
Instances For
Check the same endpoint equalities as checkCertificateRat, but under ExecFloat.Binary 8 23
arithmetic.
This is an additional (optional) check that answers: “if we interpret the certificate data as binary32 values and run the polynomial evaluation with executable IEEE-754 ops, do we still hit the claimed endpoints?”
The check is deliberately strict:
- every rational in the cert must be exactly representable as a finite
ExecFloat.Binary 8 23, - comparisons use IEEE-style
BEq(so+0 == -0, and NaNs never compare equal).
Instances For
Check a piecewise-polynomial certificate stored as JSON, including ExecFloat.Binary 8 23
semantics.
Instances For
Check a piecewise-polynomial certificate stored as a JSON file.