Documentation

CslibFrontier.Logics.RealArithmetic.FormulaWordValidation

Polynomial-time syntax validation for real-arithmetic formula words #

This adapts LeanCanny.ETR.Grammar.recognizeFormula from lean-canny at 970f28a5ecefc1327402ade16c068aa4b1b2c183 to the frontier formula encoding. The strict formula-token decoder handles framing. The recursive-encoding checker verifies that the constructor stream describes one tree, and term recognition at modulus one checks the comparison payloads. Valid words encode formulas in the dimension given by their length.

The total view adapts LeanCanny.ETR.FormulaView at the same revision. It preserves the exact bits of valid encodings and gives malformed words the semantics of falsity. Its indexed token reader adapts the constructor and term-word access of LeanCanny.ETR.FormulaBooleanCircuitSource to the frontier prefix token list. Subtree spans locate the second operand with the native splitter for framed prefix tokens. The child-index query returns the operands in their syntactic order. The indexed gate operation adapts the local wire evaluation of LeanCanny.ETR.FormulaBooleanCircuitCorrect at the same revision. It reads comparison signs from a supplied term-word interpretation and connective operands from supplied wire values. Comparison requests retain the exact term word and the required sign. The Boolean part of gate evaluation has a polynomial-time certificate given the comparison answer and access to the operand values.

Check that a raw bit word encodes one formula, including every comparison's term syntax.

A canonical formula encoding passes the syntax validator in any variable dimension.

Validation recognizes exactly the formula encodings, using the input length as a variable dimension large enough to contain every unary variable rank in the word.

Preserve a valid formula word and replace a malformed word with the encoding of falsity.

The total formula denoted by a word, using its length for the ambient variable dimension. Malformed words denote falsity.

The formula view of a malformed word is falsity.

The bit-level normalization computes the exact encoding of the total formula view.

Re-encoding the total formula view is uniformly polynomial time on raw bit words.

The prefix tokens of the total formula view, retaining the original comparison term words.

The executable token stream agrees with the typed formula view on every input.

Each token of a valid word occupies at least one bit. Malformed words have one falsity token, including for the empty input.

Read a prefix token by position. Positions beyond the stream return the falsity token.

The number of prefix tokens in the subtree beginning at this index of the total formula view. A position outside the token stream has no subtree.

A serialized subtree starting at the given position is consumed exactly, leaving any following tokens out of the span.

Subtree span takes polynomial time with a binary index and an optional binary result. The index is capped by the number of tokens before converting it to unary for list dropping.

The immediate child indices of a connective in the prefix stream of the total formula view, in operand order. Leaf tokens and positions outside the stream have no children.

theorem Cslib.Logic.RealArithmetic.formulaWordChildIndices_of_prefix {n : ℕ} {word : List Bool} (p : Formula n) (before after : List FormulaToken) (h : formulaWordTokens word = before ++ (List.map FormulaToken.encodeTerms (encFormula_tokens n p) ++ after)) :
formulaWordChildIndices word before.length = match p, h with | a.not, h => [before.length + 1] | left.and a, h => [before.length + 1, before.length + 1 + (encFormula_tokens n left).length] | left.or a, h => [before.length + 1, before.length + 1 + (encFormula_tokens n left).length] | x, h => []

At a serialized subformula, the query returns precisely the starting positions of its immediate operands. A second operand starts just after the first operand's complete token stream.

Child indices are computable in polynomial time from a raw word and a binary index. The result is a framed list of up to two binary child positions.

A comparison gate's arithmetic query.

  • termWord : List Bool

    The term word carried by the comparison token.

  • isStrict : Bool

    A strict comparison requests a negative sign. An equality requests zero.

theorem Cslib.Logic.RealArithmetic.encFormulaComparisonRequest_mk_inO {γ : Type} {encIn : γ ↪ List Bool} {x0 : γ → List Bool} {T0 S0 : γ → ℕ} {x1 : γ → Bool} {T1 S1 : γ → ℕ} (h0 : Turing.MultiTapeTM.ComputableInO x0 encIn Turing.MultiTapeTM.encBits T0 S0) (h1 : Turing.MultiTapeTM.ComputableInO x1 encIn Turing.MultiTapeTM.encBool T1 S1) :
Turing.MultiTapeTM.ComputableInO (fun (x : γ) => { termWord := x0 x, isStrict := x1 x }) encIn encFormulaComparisonRequest (fun (x : γ) => T0 x + T1 x) fun (x : γ) => S0 x + S1 x

Frame the raw term word and append one bit indicating strict inequality.

theorem Cslib.Logic.RealArithmetic.encFormulaComparisonRequest_mk_budget {γ : Type} {encIn : γ ↪ List Bool} {budget : γ → ℕ} {x0 : γ → List Bool} {x1 : γ → Bool} (h0 : Turing.MultiTapeTM.ComputableInSpace x0 encIn Turing.MultiTapeTM.encBits budget) (h1 : Turing.MultiTapeTM.ComputableInSpace x1 encIn Turing.MultiTapeTM.encBool budget) :
Turing.MultiTapeTM.ComputableInSpace (fun (x : γ) => { termWord := x0 x, isStrict := x1 x }) encIn encFormulaComparisonRequest budget

The sign that makes the requested comparison true.

Read the comparison request at a binary index of the total formula view. Connectives, falsity and out-of-range positions have no request.

theorem Cslib.Logic.RealArithmetic.formulaWordComparisonRequest_termWord_exists {word : List Bool} {index : ℕ} {request : FormulaComparisonRequest} (hi : index < (formulaWordTokens word).length) (h : formulaWordComparisonRequest word index = some request) :
∃ (t : Term word.length), request.termWord = (encTerm word.length) t

A comparison in the total formula view retains the canonical encoding of a term in the word's dimension.

A comparison request's term payload occupies strictly fewer bits than the original formula word. A malformed word has no comparison requests.

Read an indexed gate's exact comparison term word. Unused slots return the encoding of the constant zero term.

noncomputable def Cslib.Logic.RealArithmetic.formulaWordGateTerm (word : List Bool) (index : ℕ) :

The arithmetic term of an indexed formula gate in the word's ambient dimension. Non-comparisons and indices beyond the token stream have the constant zero term.

Encoding the typed gate term recovers the executable reader's exact payload.

The typed gate terms have polynomial-time access under the raw formula word and binary index encoding. This adapts the term access of LeanCanny.ETR.FormulaAtomPolynomials.termSource to the frontier prefix token view.

The common length budget for gate terms, including the encoded constant zero in unused slots.

Compute the gate-term budget in binary from the raw formula word.

The term word at every gate fits within the common length budget.

The formula word gives a single degree and coefficient norm budget for all of its gate polynomials, including the zero padding at unused slots.

Evaluate the comparison term at an indexed gate, using zero for missing coordinates. Gates without a comparison request have no residue.

theorem Cslib.Logic.RealArithmetic.formulaWordComparisonResidue_eq_some_iff {word : List Bool} {index p residue : ℕ} (hp : 0 < p) (hi : index < (formulaWordTokens word).length) (point : List ℕ) :
formulaWordComparisonResidue word index p point = some residue ↔ ∃ (request : FormulaComparisonRequest) (t : Term word.length), formulaWordComparisonRequest word index = some request ∧ request.termWord = (encTerm word.length) t ∧ (MvPolynomial.eval₂ (Int.castRingHom (ZMod p)) (fun (i : Fin word.length) => ↑(point[↑i]?.getD 0)) t.toPolynomial).val = residue

At an in-bounds gate, the residue is present exactly when a comparison term exists and its polynomial has that residue. Coordinates missing from point are read as zero.

Comparison residue queries take polynomial time in the raw formula word, binary gate index and positive binary modulus, and framed binary coordinates.

Evaluate the indexed gate's arithmetic term at the supplied residues. Unused slots give zero, and missing coordinates are read as zero.

theorem Cslib.Logic.RealArithmetic.formulaWordGateResidue_eq (word : List Bool) (index : ℕ) {p : ℕ} (hp : 0 < p) (point : List ℕ) :
formulaWordGateResidue word index p point = ((MvPolynomial.aeval fun (k : Fin word.length) => ↑(point[↑k]?.getD 0)) (formulaWordGateTerm word index).toPolynomial).val

For a positive modulus, the executable gate query returns the canonical residue of the indexed term polynomial at the zero-padded coordinates.

The total residue query is polynomial time in the raw formula word, binary gate index and positive binary modulus, and framed list of binary coordinate values.

Test a supplied sign against the comparison requested at a gate. Gates without a comparison request return false.

Testing an indexed comparison is polynomial time in the formula word, binary gate index, and supplied sign using encSign.

theorem Cslib.Logic.RealArithmetic.formulaWordComparisonRequest_of_prefix {n : ℕ} {word : List Bool} (p : Formula n) (before after : List FormulaToken) (h : formulaWordTokens word = before ++ (List.map FormulaToken.encodeTerms (encFormula_tokens n p) ++ after)) :
formulaWordComparisonRequest word before.length = match p, h with | Formula.eqZero t, h => some { termWord := (encTerm n) t, isStrict := false } | Formula.ltZero t, h => some { termWord := (encTerm n) t, isStrict := true } | x, h => none

Read the comparison request of a serialized subformula, retaining its exact term word.

def Cslib.Logic.RealArithmetic.formulaWordGateValueWithAnswer (word : List Bool) (index : ℕ) (answer : Bool) (value : ℕ → Bool) :

Evaluate an indexed gate with a supplied comparison answer. Connectives read the supplied child values, and falsity and positions outside the token stream give false.

theorem Cslib.Logic.RealArithmetic.formulaWordGateValueWithAnswer_congr_children (word : List Bool) (index : ℕ) (answer : Bool) {v w : ℕ → Bool} (hl : v ((formulaWordChildIndices word index).getD 0 0) = w ((formulaWordChildIndices word index).getD 0 0)) (hr : v ((formulaWordChildIndices word index).getD 1 0) = w ((formulaWordChildIndices word index).getD 1 0)) :
formulaWordGateValueWithAnswer word index answer v = formulaWordGateValueWithAnswer word index answer w

The Boolean gate reads its value function only at the two child lookup positions.

theorem Cslib.Logic.RealArithmetic.formulaWordGateValueWithAnswer_of_request {word : List Bool} {index : ℕ} {request : FormulaComparisonRequest} (h : formulaWordComparisonRequest word index = some request) (answer : Bool) (value : ℕ → Bool) :
formulaWordGateValueWithAnswer word index answer value = answer

Comparison gates use the supplied answer.

theorem Cslib.Logic.RealArithmetic.formulaWordGateValueWithAnswer_congr (word : List Bool) (index : ℕ) (value : ℕ → Bool) {a b : Bool} (h : (formulaWordComparisonRequest word index).isSome = true → a = b) :

Two supplied answers give the same gate value if they agree whenever the gate requests a comparison.

Evaluate the Boolean part of a gate in polynomial time from a raw word, binary position, comparison answer, and a certified reader of values at binary positions.

def Cslib.Logic.RealArithmetic.formulaWordGateValue (word : List Bool) (index : ℕ) (termSign : List Bool → SignType) (value : ℕ → Bool) :

Evaluate one Boolean gate of the total formula view. Comparison gates ask for the sign of their encoded term. Connectives read the supplied values at their child indices. Falsity and positions outside the token stream return false.

theorem Cslib.Logic.RealArithmetic.formulaWordGateValueWithAnswer_eq (word : List Bool) (index : ℕ) (termSign : List Bool → SignType) (value : ℕ → Bool) :
formulaWordGateValueWithAnswer word index (Option.any (fun (request : FormulaComparisonRequest) => decide (termSign request.termWord = request.requestedSign)) (formulaWordComparisonRequest word index)) value = formulaWordGateValue word index termSign value

Computing the comparison answer from the indexed request recovers the gate's value under the supplied term signs.

theorem Cslib.Logic.RealArithmetic.formulaWordGateValue_of_prefix {n : ℕ} {word : List Bool} (p : Formula n) (before after : List FormulaToken) (h : formulaWordTokens word = before ++ (List.map FormulaToken.encodeTerms (encFormula_tokens n p) ++ after)) (termSign : List Bool → SignType) (value : ℕ → Bool) :
formulaWordGateValue word before.length termSign value = match p, h with | Formula.false, h => false | Formula.eqZero t, h => decide (termSign ((encTerm n) t) = 0) | Formula.ltZero t, h => decide (termSign ((encTerm n) t) = -1) | a.not, h => !value (before.length + 1) | left.and a, h => value (before.length + 1) && value (before.length + 1 + (encFormula_tokens n left).length) | left.or a, h => value (before.length + 1) || value (before.length + 1 + (encFormula_tokens n left).length)

A gate at the start of an encoded subformula reads the exact term payload for a comparison, or combines the values at the starts of its immediate operands.