Documentation

CslibFrontier.Computability.Circuit.Boolean.ExactOne

Sequential exact-one De Morgan circuits and width-3 Tseitin compilation #

For a list of input wires ws, seqExactlyOneClause n base ws builds a De Morgan gate list of only 3 * ws.length + 3 gates (O(ws.length) gates rather than quadratic pairwise clauses) whose output wire evaluates to true exactly when precisely one wire in ws is true. Repeating across a family wss of wire lists (seqExactlyOneAll n wss) yields a gate list of 1 + 4 * wss.length + 3 * (wss.map List.length).sum gates, and tseitinSatCNF [] n gates out compiles it to a width-3 CNF formula preserving the exact number of satisfying input bitstrings in O(n + ∑ |ws|) variables.

Main definitions #

Main results #

Prefix model-count preservation for tseitinSatCNF #

def Cslib.Circuits.Boolean.tseitinSatAssignmentEquiv {n : ℕ} {gates : List (Op × List ℕ)} {o : ℕ} (hcode : Program.IsCodeFrom signature n 0 gates) (ho : o < n + gates.length) :
{ x : Fin n → Bool // evalOutput (List.ofFn x) gates o = true } ≃ { σ : ↥(Finset.range (n + gates.length)) → Bool // Logic.PL.CNF.eval (fun (w : ℕ) => if hw : w ∈ Finset.range (n + gates.length) then σ ⟨w, hw⟩ else false) (tseitinSatCNF [] n gates o) = true }

Explicit assignment bijection between bit strings accepted by a single-output well-formed gate list and satisfying assignments of tseitinSatCNF [] n gates o on Finset.range (n + gates.length).

theorem Cslib.Circuits.Boolean.card_evalOutput_eq_modelCountOn_tseitinSatCNF {n : ℕ} {gates : List (Op × List ℕ)} {o : ℕ} (hcode : Program.IsCodeFrom signature n 0 gates) (ho : o < n + gates.length) :
{x : Fin n → Bool | evalOutput (List.ofFn x) gates o = true}.card = (tseitinSatCNF [] n gates o).modelCountOn (Finset.range (n + gates.length))

tseitinSatCNF preserves the exact number of accepting input bitstrings.

theorem Cslib.Circuits.Boolean.vars_tseitinSatCNF_subset {n : ℕ} {gates : List (Op × List ℕ)} {o : ℕ} (hcode : Program.IsCodeFrom signature n 0 gates) (ho : o < n + gates.length) (hpos : 0 < n + gates.length) :
(tseitinSatCNF [] n gates o).vars.toFinset ⊆ Finset.range (n + gates.length)

Every occurring variable of tseitinSatCNF [] n gates o is strictly below n + gates.length when gates is well-formed (IsCodeFrom signature n 0 gates) and o < n + gates.length.

IsCodeFrom and single-gate evalCode helpers #

Sequential exact-one De Morgan gate list #

Intermediate state of seqExactlyOneStep, pairing the emitted De Morgan gates with the wires recording whether at least one and at least two true inputs have been seen so far.

  • gates : List (Op × List ℕ)

    Emitted De Morgan gates (op, args).

  • seen1Wire : ℕ

    Wire index holding true iff at least one true wire has been seen.

  • seen2Wire : ℕ

    Wire index holding true iff at least two true wires have been seen.

Instances For

    Single-output De Morgan circuit fragment pairing its gate list with its designated output wire index.

    • gates : List (Op × List ℕ)

      Emitted De Morgan gates (op, args).

    • outWire : ℕ

      Designated output wire index.

    Instances For

      Recursive linear-size state transition tracking seen1 and seen2 across wire list ws. Returns the emitted gates together with the updated seen1Wire and seen2Wire.

      Sequential exact-one circuit on input wires ws, starting at gate offset base on n total input wires. Returns the gate list and its output wire. Uses 3 * ws.length + 3 gates.

      @[simp]
      theorem Cslib.Circuits.Boolean.length_seqExactlyOneStep (nextWire seen1 seen2 : ℕ) (ws : List ℕ) :
      (seqExactlyOneStep nextWire seen1 seen2 ws).gates.length = 3 * ws.length
      theorem Cslib.Circuits.Boolean.isCodeFrom_seqExactlyOneClause (n base : ℕ) {ws : List ℕ} (hws : ∀ w ∈ ws, w < n + base) :

      seqExactlyOneClause n base ws is well-formed starting at gate base whenever all wires in ws are < n + base.

      theorem Cslib.Circuits.Boolean.evalCode_seqExactlyOneClause (x : List Bool) (prefixGates : List (Op × List ℕ)) (ws : List ℕ) (hws : ∀ w ∈ ws, w < x.length + prefixGates.length) :
      have clause := seqExactlyOneClause x.length prefixGates.length ws; have vals := evalCode interpretation false x (prefixGates ++ clause.gates); vals.getD clause.outWire false = decide (List.countP (fun (w : ℕ) => (evalCode interpretation false x prefixGates).getD w false) ws = 1)

      Evaluation correctness of seqExactlyOneClause: after appending seqExactlyOneClause, its output wire holds true iff precisely one wire in ws holds true.

      Fold a list of wire lists into a conjunction of sequential exact-one De Morgan gadgets. Returns the gate list and its output wire. Uses 4 * wss.length + 3 * (wss.map List.length).sum extra gates.

      Linear-size sequential exact-one De Morgan circuit checking that every list ws ∈ wss has precisely one true input wire among 0, …, n - 1. Uses at most 1 + 4 * wss.length + 3 * (wss.map List.length).sum gates.

      @[simp]
      theorem Cslib.Circuits.Boolean.isCodeFrom_seqExactlyOneAll (n : ℕ) {wss : List (List ℕ)} (hwss : ∀ ws ∈ wss, ∀ w ∈ ws, w < n) :

      seqExactlyOneAll n wss is a well-formed De Morgan gate list on n inputs whenever every wire in every list of wss is < n.

      theorem Cslib.Circuits.Boolean.outWire_seqExactlyOneAll_lt (n : ℕ) {wss : List (List ℕ)} (hwss : ∀ ws ∈ wss, ∀ w ∈ ws, w < n) :

      The output wire of seqExactlyOneAll n wss lies strictly below n + gates.length.

      theorem Cslib.Circuits.Boolean.evalOutput_seqExactlyOneAll (x : List Bool) {wss : List (List ℕ)} (hwss : ∀ ws ∈ wss, ∀ w ∈ ws, w < x.length) :
      have res := seqExactlyOneAll x.length wss; evalOutput x res.gates res.outWire = wss.all fun (ws : List ℕ) => decide (List.countP (fun (w : ℕ) => x.getD w false) ws = 1)

      Evaluation correctness of seqExactlyOneAll: evalOutput x gates out holds iff every list ws ∈ wss contains precisely one wire where x evaluates to true.