Documentation

CslibFrontier.Computability.Circuit.Encoding.Combinators

Signature-generic circuit code combinators and typed fun_prop bridges #

Wire-relocating transformations on raw circuit codes (List (Op × List ℕ) × List ℕ) depend only on wire numbers, never on the operation type Op:

Each raw combinator is polynomial-time under Circuit.encCode encOp for any explicit operation encoding encOp : Op ↪ List Bool (with no finiteness assumption on Op), and each has a typed Circuit.code_* bridge equation. Layering of_enc_eq on those bridges gives direct @[fun_prop] rules for (fun a => (C a).code) across Circuit.wiring, Circuit.oneGate, Circuit.append, Circuit.pi, Circuit.comp, Circuit.replicate, Circuit.balanced, and Formula.toCircuit, so uniform circuit-family proofs compose directly over typed Circuit definitions.

Main definitions #

Main results #

References #

def Cslib.Circuits.Program.appendWireCode (n m s : ℕ) (feed : Fin m → Wire n s) (i : ℕ) :

The wire-number map when continuing a program with another program.

theorem Cslib.Circuits.Program.appendedWire_toNat {n m s t : ℕ} (feed : Fin m → Wire n s) (w : Wire m t) :
(appendedWire feed w).toNat = appendWireCode n m s feed w.toNat

Numbering a continued wire agrees with the raw wire-number map.

theorem Cslib.Circuits.Program.code_append {σ : Signature} {n m s t : ℕ} (p : Program σ n s) (feed : Fin m → Wire n s) (q : Program σ m t) :
(p.append feed q).code = p.code ++ List.map (fun (g : σ.Op × List ℕ) => (g.1, List.map (appendWireCode n m s feed) g.2)) q.code

Program continuation acts on codes by appending the rewired gate list.

Shared-input append and bundling #

Retain an input wire, or shift a gate wire past offset preceding gates.

def Cslib.Circuits.Circuit.shiftCode {Op : Type u_1} (n offset : ℕ) (code : List (Op × List ℕ) × List ℕ) :

Shift the gate references and output references of a raw code. Input wires are preserved.

def Cslib.Circuits.Circuit.appendCode {Op : Type u_1} (n : ℕ) (c d : List (Op × List ℕ) × List ℕ) :

Run two raw codes on shared inputs, listing the first code's outputs before the second's. Malformed codes are transformed as data; semantic correctness is asserted on typed circuits.

@[simp]
theorem Cslib.Circuits.Circuit.length_appendCode {Op : Type u_1} (n : ℕ) (c d : List (Op × List ℕ) × List ℕ) :
(appendCode n c d).1.length = c.1.length + d.1.length

Appending two raw codes sums their gate counts.

@[simp]
theorem Cslib.Circuits.Circuit.length_appendCode_outputs {Op : Type u_1} (n : ℕ) (c d : List (Op × List ℕ) × List ℕ) :
(appendCode n c d).2.length = c.2.length + d.2.length

Appending two raw codes sums their output counts.

theorem Cslib.Circuits.Circuit.code_append {σ : Signature} {n m p : ℕ} (c : Circuit σ n m) (d : Circuit σ n p) :

The serialized code of c.append d is appendCode n c.code d.code.

def Cslib.Circuits.Circuit.appendCodes {Op : Type u_1} (n : ℕ) (codes : List (List (Op × List ℕ) × List ℕ)) :

Combine a list of raw codes on shared inputs, preserving block and output order. Blocks may have different gate counts and any number of outputs.

@[simp]

Combining an empty list of raw codes yields the empty code.

theorem Cslib.Circuits.Circuit.appendCodes_append_singleton {Op : Type u_1} (n : ℕ) (codes : List (List (Op × List ℕ) × List ℕ)) (code : List (Op × List ℕ) × List ℕ) :
appendCodes n (codes ++ [code]) = appendCode n (appendCodes n codes) code

Adding a final block is ordinary shared-input append.

@[simp]
theorem Cslib.Circuits.Circuit.length_appendCodes {Op : Type u_1} (n : ℕ) (codes : List (List (Op × List ℕ) × List ℕ)) :

The combined code retains every gate, without padding the individual blocks.

@[simp]

The combined code retains all outputs, including repeated outputs and zero-gate blocks.

theorem Cslib.Circuits.Circuit.code_pi {σ : Signature} {k n : ℕ} (cs : Fin k → Circuit σ n 1) :
(pi cs).code = appendCodes n (List.ofFn fun (j : Fin k) => (cs j).code)

The serialized code of Circuit.pi cs is appendCodes n of the component codes.

def Cslib.Circuits.Circuit.bundleCode {Op : Type u_1} (n blockSize : ℕ) (codes : List (List (Op × List ℕ) × List ℕ)) :

Combine equal-sized gate blocks on shared inputs. Block j is relocated by j * blockSize. The construction is total on raw data; its append semantics requires the stated block sizes.

theorem Cslib.Circuits.Circuit.length_bundleCode {Op : Type u_1} (n blockSize : ℕ) (codes : List (List (Op × List ℕ) × List ℕ)) (hlen : ∀ c ∈ codes, c.1.length = blockSize) :
(bundleCode n blockSize codes).1.length = codes.length * blockSize

Equal-sized blocks occupy exactly their number times their common gate count.

theorem Cslib.Circuits.Circuit.bundleCode_append_singleton {Op : Type u_1} (n blockSize : ℕ) (codes : List (List (Op × List ℕ) × List ℕ)) (code : List (Op × List ℕ) × List ℕ) (hlen : ∀ c ∈ codes, c.1.length = blockSize) :
bundleCode n blockSize (codes ++ [code]) = appendCode n (bundleCode n blockSize codes) code

Adding the next equal-sized block is shared-input append.

theorem Cslib.Circuits.Circuit.bundleCode_eq_foldl_appendCode {Op : Type u_1} (n blockSize : ℕ) (codes : List (List (Op × List ℕ) × List ℕ)) (hlen : ∀ c ∈ codes, c.1.length = blockSize) :
bundleCode n blockSize codes = List.foldl (appendCode n) ([], []) codes

For equal-sized blocks, direct relocation agrees with repeated shared-input append.

theorem Cslib.Circuits.Circuit.bundleCode_eq_appendCodes {Op : Type u_1} (n blockSize : ℕ) (codes : List (List (Op × List ℕ) × List ℕ)) (hlen : ∀ c ∈ codes, c.1.length = blockSize) :
bundleCode n blockSize codes = appendCodes n codes

Fixed-size bundling is a specialization of the varying-size constructor.

theorem Cslib.Circuits.Circuit.code_pi_bundle {σ : Signature} {k n : ℕ} (cs : Fin k → Circuit σ n 1) (blockSize : ℕ) (hs : ∀ (j : Fin k), (cs j).size = blockSize) :
(pi cs).code = bundleCode n blockSize (List.ofFn fun (j : Fin k) => (cs j).code)

When every component has size blockSize, bundleCode computes (Circuit.pi cs).code.

theorem Cslib.Circuits.Circuit.code_pi_singleton {σ : Signature} {n m : ℕ} (cs : Fin m → Circuit σ n 1) (rows : Fin m → σ.Op × List ℕ) (hc : ∀ (i : Fin m), (cs i).code = ([rows i], [n])) :
(pi cs).code = (List.ofFn rows, List.map (fun (x : ℕ) => n + x) (List.range m))

A parallel layer of single-gate circuits lists their gate rows in output order. Since each component returns its gate, its output is relocated past the preceding gates.

Sequential composition #

def Cslib.Circuits.Circuit.compCodeWire {Op : Type u_1} (n : ℕ) (c : List (Op × List ℕ) × List ℕ) (i : ℕ) :

Route a later circuit's inputs through c's outputs and shift its gates past c's gates.

def Cslib.Circuits.Circuit.compCode {Op : Type u_1} (n : ℕ) (d c : List (Op × List ℕ) × List ℕ) :

Feed the outputs of c to the inputs of d, matching the argument order of Circuit.comp. The constructor is total on raw data, including malformed circuit codes.

theorem Cslib.Circuits.Circuit.code_comp {σ : Signature} {n m p : ℕ} (d : Circuit σ m p) (c : Circuit σ n m) :
(d.comp c).code = compCode n d.code c.code

The serialized code of d.comp c is compCode n d.code c.code.

@[simp]
theorem Cslib.Circuits.Circuit.length_compCode {Op : Type u_1} (n : ℕ) (d c : List (Op × List ℕ) × List ℕ) :
(compCode n d c).1.length = c.1.length + d.1.length

Sequential composition retains both gate lists.

@[simp]
theorem Cslib.Circuits.Circuit.length_compCode_outputs {Op : Type u_1} (n : ℕ) (d c : List (Op × List ℕ) × List ℕ) :
(compCode n d c).2.length = d.2.length

Sequential composition retains the later circuit's number of outputs.

Disjoint-input replication #

The number of wire i of a circuit with n inputs and s gates in copy t of k disjoint copies: input i < n is input t * n + i of the replicated circuit, and gate wire n + g is gate t * s + g of the replicated circuit.

def Cslib.Circuits.Circuit.replicateCode {Op : Type u_1} (n k : ℕ) (code : List (Op × List ℕ) × List ℕ) :

The code of k disjoint copies of the circuit with code code on n inputs.

@[simp]
theorem Cslib.Circuits.Circuit.length_replicateCode {Op : Type u_1} (n k : ℕ) (code : List (Op × List ℕ) × List ℕ) :
(replicateCode n k code).1.length = k * code.1.length

Replicating a raw code multiplies its gate count by k.

theorem Cslib.Circuits.Circuit.code_replicate {σ : Signature} {n m : ℕ} (c : Circuit σ n m) (k : ℕ) :

The serialized code of c.replicate k is replicateCode n k c.code.

Single-gate and formula-template serialization #

theorem Cslib.Circuits.Circuit.code_oneGate {σ : Signature} {n : ℕ} (op : σ.Op) (args : Fin (σ.Arity op) → Fin n) :
(oneGate op args).code = ([(op, List.ofFn fun (a : Fin (σ.Arity op)) => ↑(args a))], [n])

The serialized code of Circuit.oneGate op args on n inputs has one gate reading the wire indices (args a).val and designated output wire n.

theorem Cslib.Circuits.Circuit.code_pi_oneGate {σ : Signature} {n k : ℕ} (op : Fin k → σ.Op) (wire : (i : Fin k) → Fin (σ.Arity (op i)) → Fin n) :
(pi fun (i : Fin k) => oneGate (op i) (wire i)).code = (List.ofFn fun (i : Fin k) => (op i, List.ofFn fun (j : Fin (σ.Arity (op i))) => ↑(wire i j)), List.ofFn fun (i : Fin k) => n + ↑i)

Serializing one gate per output lists those gates in output order. They use only input wires, so block relocation leaves every gate argument unchanged.

def Cslib.Circuits.Circuit.formulaCode {σ : Signature} {m : ℕ} (n : ℕ) (F : Formula σ m) (wires : Fin m → ℕ) :

Instantiate a formula F over σ at raw wire numbers. Total even when a selected wire is outside the declared input count; typed correctness holds on Fin n-valued wires.

theorem Cslib.Circuits.Circuit.formulaCode_eq_code {σ : Signature} {n m : ℕ} (F : Formula σ m) (wires : Fin m → Fin n) :
(formulaCode n F fun (i : Fin m) => ↑(wires i)) = (Formula.rename wires F).toCircuit.code

Instantiating valid input wires constructs the exact circuit code of (F.rename wires).toCircuit.

Polynomial-time certificates for raw combinators #

Shifting wire numbers is polynomial-time with all quantities encoded in binary.

theorem Cslib.Circuits.Circuit.computableInPolyTime_shiftCode {Op α : Type} {encOp : Op ↪ List Bool} {enc : α ↪ List Bool} {n offset : α → ℕ} {code : α → List (Op × List ℕ) × List ℕ} (hn : Turing.MultiTapeTM.ComputableInPolyTime n enc Turing.MultiTapeTM.encNat) (hoffset : Turing.MultiTapeTM.ComputableInPolyTime offset enc Turing.MultiTapeTM.encNat) (hcode : Turing.MultiTapeTM.ComputableInPolyTime code enc (encCode encOp)) :
Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => shiftCode (n a) (offset a) (code a)) enc (encCode encOp)

Relocating a circuit-code block is polynomial-time with binary offsets.

Combining circuit codes is polynomial-time, including binary input and wire counts.

Combining arbitrary raw blocks is polynomial-time in their complete encodings. Input and wire numbers are binary; no uniform block-size or well-formedness premise is needed.

theorem Cslib.Circuits.Circuit.computableInPolyTime_bundleCode {Op α : Type} {encOp : Op ↪ List Bool} {enc : α ↪ List Bool} {n blockSize : α → ℕ} {codes : α → List (List (Op × List ℕ) × List ℕ)} (hn : Turing.MultiTapeTM.ComputableInPolyTime n enc Turing.MultiTapeTM.encNat) (hblock : Turing.MultiTapeTM.ComputableInPolyTime blockSize enc Turing.MultiTapeTM.encNat) (hcodes : Turing.MultiTapeTM.ComputableInPolyTime codes enc (Turing.MultiTapeTM.encList (encCode encOp))) :
Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => bundleCode (n a) (blockSize a) (codes a)) enc (encCode encOp)

Bundling raw blocks is polynomial-time in their encodings and binary layout parameters.

Resolve a composed wire in polynomial time, including binary output lookup and offsets.

Construct sequential circuit codes in polynomial time. All wire indices and input counts are binary; list lengths, input lookup, gate relocation and the whole output code are charged.

Replicating a raw code is polynomial-time, with a binary input count and a unary number of copies.

theorem Cslib.Circuits.Circuit.computableInPolyTime_formulaCode {α : Type} {enc : α ↪ List Bool} {σ : Signature} {encOp : σ.Op ↪ List Bool} {n : α → ℕ} {m : ℕ} (F : Formula σ m) {wires : α → Fin m → ℕ} (hn : Turing.MultiTapeTM.ComputableInPolyTime n enc Turing.MultiTapeTM.encNat) (hwires : ∀ (i : Fin m), Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => wires a i) enc Turing.MultiTapeTM.encNat) :
Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => formulaCode (n a) F (wires a)) enc (encCode encOp)

A fixed formula's instantiated code is polynomial-time in the target input count and selected binary wire numbers, over any signature σ.

Balanced raw code tree #

def Cslib.Circuits.Circuit.balancedCode {Op : Type u_1} (n : ℕ) (op unit : List (Op × List ℕ) × List ℕ) (leaves : List (List (Op × List ℕ) × List ℕ)) :

Generate the exact half-split code on shared inputs. The supplied fallback is returned only for an empty leaf list. Malformed codes are transformed as data, without arity-sized loops.

theorem Cslib.Circuits.Circuit.code_balanced {σ : Signature} {n r : ℕ} (op : Circuit σ (r + r) r) (unit : Circuit σ n r) (leaves : List (Circuit σ n r)) :
(op.balanced unit leaves).code = balancedCode n op.code unit.code (List.map code leaves)

The serialized code of Circuit.balanced op unit leaves is balancedCode of their codes, including empty fallback programs, unused operators, dead gates and intermediate gate order.

theorem Cslib.Circuits.Circuit.computableInPolyTime_balancedCode {Op α : Type} {encOp : Op ↪ List Bool} {enc : α ↪ List Bool} {n : α → ℕ} {op unit : α → List (Op × List ℕ) × List ℕ} {leaves : α → List (List (Op × List ℕ) × List ℕ)} (hn : Turing.MultiTapeTM.ComputableInPolyTime n enc Turing.MultiTapeTM.encNat) (hop : Turing.MultiTapeTM.ComputableInPolyTime op enc (encCode encOp)) (hu : Turing.MultiTapeTM.ComputableInPolyTime unit enc (encCode encOp)) (hl : Turing.MultiTapeTM.ComputableInPolyTime leaves enc (Turing.MultiTapeTM.encList (encCode encOp))) :
Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => balancedCode (n a) (op a) (unit a) (leaves a)) enc (encCode encOp)

Uniform native generation from the binary shared-input count and the complete operator, fallback and leaf codes, over any operation encoding encOp : Op ↪ List Bool.

Typed @[fun_prop] (·.code) rules #

def Cslib.Circuits.Circuit.encWithFinUnary {α : Type} (enc : α ↪ List Bool) (k : α → ℕ) :
(a : α) × Fin (k a) ↪ List Bool

An input a : α paired with a bounded index i : Fin (k a): a is framed under enc followed by i.val in unary (encUnary).

@[simp]
theorem Cslib.Circuits.Circuit.encWithFinUnary_apply {α : Type} (enc : α ↪ List Bool) (k : α → ℕ) (q : (a : α) × Fin (k a)) :

encWithFinUnary encodes ⟨a, i⟩ as the framed pair (a, i.val) with i.val in unary.

theorem Cslib.Circuits.Circuit.computableInPolyTime_withFinUnary_fst {α β : Type} {encIn : β ↪ List Bool} {enc : α ↪ List Bool} {k : α → ℕ} {f : β → (a : α) × Fin (k a)} (hf : Turing.MultiTapeTM.ComputableInPolyTime f encIn (encWithFinUnary enc k)) :
Turing.MultiTapeTM.ComputableInPolyTime (fun (b : β) => (f b).fst) encIn enc

Project the outer input from an encWithFinUnary-encoded dependent pair.

theorem Cslib.Circuits.Circuit.computableInPolyTime_withFinUnary_val {α β : Type} {encIn : β ↪ List Bool} {enc : α ↪ List Bool} {k : α → ℕ} {f : β → (a : α) × Fin (k a)} (hf : Turing.MultiTapeTM.ComputableInPolyTime f encIn (encWithFinUnary enc k)) :

Project the Fin index as a unary natural from an encWithFinUnary-encoded dependent pair.

theorem Cslib.Circuits.Circuit.computableInPolyTime_withFinUnary_val_binary {α β : Type} {encIn : β ↪ List Bool} {enc : α ↪ List Bool} {k : α → ℕ} {f : β → (a : α) × Fin (k a)} (hf : Turing.MultiTapeTM.ComputableInPolyTime f encIn (encWithFinUnary enc k)) :

Project the Fin index as a binary natural from an encWithFinUnary-encoded dependent pair.

theorem Cslib.Circuits.Circuit.computableInPolyTime_ofFn_unary {α β : Type} {enc : α ↪ List Bool} {encB : β ↪ List Bool} {k : α → ℕ} {f : (a : α) → Fin (k a) → β} (hk : Turing.MultiTapeTM.ComputableInPolyTime k enc Turing.MultiTapeTM.encUnary) (hf : Turing.MultiTapeTM.ComputableInPolyTime (fun (q : (a : α) × Fin (k a)) => f q.fst q.snd) (encWithFinUnary enc k) encB) :

Tabulate a runtime-indexed Fin (k a) → β family in polynomial time when k is computed in unary and the indexed body is polynomial-time on encWithFinUnary enc k.

theorem Cslib.Circuits.Circuit.computableInPolyTime_code_wiring {σ : Signature} {encOp : σ.Op ↪ List Bool} {α : Type} {enc : α ↪ List Bool} {n m : α → ℕ} {sel : (a : α) → Fin (m a) → Fin (n a)} (hm : Turing.MultiTapeTM.ComputableInPolyTime m enc Turing.MultiTapeTM.encUnary) (hsel : Turing.MultiTapeTM.ComputableInPolyTime (fun (q : (a : α) × Fin (m a)) => ↑(sel q.fst q.snd)) (encWithFinUnary enc m) Turing.MultiTapeTM.encNat) :
Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => (wiring σ (sel a)).code) enc (encCode encOp)

Emit the code of a typed Circuit.wiring from unary output arity and binary wire indices.

Emit the code of a typed identity circuit Circuit.id σ (n a) from unary arity.

theorem Cslib.Circuits.Circuit.computableInPolyTime_code_oneGate {σ : Signature} {encOp : σ.Op ↪ List Bool} {α : Type} {enc : α ↪ List Bool} {n : α → ℕ} (op : σ.Op) {args : (a : α) → Fin (σ.Arity op) → Fin (n a)} (hn : Turing.MultiTapeTM.ComputableInPolyTime n enc Turing.MultiTapeTM.encNat) (hargs : ∀ (j : Fin (σ.Arity op)), Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => ↑(args a j)) enc Turing.MultiTapeTM.encNat) :
Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => (oneGate op (args a)).code) enc (encCode encOp)

Emit the code of a typed Circuit.oneGate from binary input arity and binary wire indices.

theorem Cslib.Circuits.Circuit.computableInPolyTime_code_append {σ : Signature} {encOp : σ.Op ↪ List Bool} {α : Type} {enc : α ↪ List Bool} {n m p : α → ℕ} {c : (a : α) → Circuit σ (n a) (m a)} {d : (a : α) → Circuit σ (n a) (p a)} (hn : Turing.MultiTapeTM.ComputableInPolyTime n enc Turing.MultiTapeTM.encNat) (hc : Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => (c a).code) enc (encCode encOp)) (hd : Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => (d a).code) enc (encCode encOp)) :
Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => ((c a).append (d a)).code) enc (encCode encOp)

Emit the code of a typed Circuit.append from binary input arity and the two subcircuit codes.

theorem Cslib.Circuits.Circuit.computableInPolyTime_code_pi {σ : Signature} {encOp : σ.Op ↪ List Bool} {α : Type} {enc : α ↪ List Bool} {n k : α → ℕ} {cs : (a : α) → Fin (k a) → Circuit σ (n a) 1} (hn : Turing.MultiTapeTM.ComputableInPolyTime n enc Turing.MultiTapeTM.encNat) (hk : Turing.MultiTapeTM.ComputableInPolyTime k enc Turing.MultiTapeTM.encUnary) (hcs : Turing.MultiTapeTM.ComputableInPolyTime (fun (q : (a : α) × Fin (k a)) => (cs q.fst q.snd).code) (encWithFinUnary enc k) (encCode encOp)) :
Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => (pi (cs a)).code) enc (encCode encOp)

Emit the code of a typed Circuit.pi family from binary input arity, unary output count, and uniformly generated component codes.

theorem Cslib.Circuits.Circuit.computableInPolyTime_code_comp {σ : Signature} {encOp : σ.Op ↪ List Bool} {α : Type} {enc : α ↪ List Bool} {n m p : α → ℕ} {d : (a : α) → Circuit σ (m a) (p a)} {c : (a : α) → Circuit σ (n a) (m a)} (hn : Turing.MultiTapeTM.ComputableInPolyTime n enc Turing.MultiTapeTM.encNat) (hd : Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => (d a).code) enc (encCode encOp)) (hc : Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => (c a).code) enc (encCode encOp)) :
Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => ((d a).comp (c a)).code) enc (encCode encOp)

Emit the code of a typed sequential composition d.comp c from binary input arity and the two subcircuit codes.

theorem Cslib.Circuits.Circuit.computableInPolyTime_code_replicate {σ : Signature} {encOp : σ.Op ↪ List Bool} {α : Type} {enc : α ↪ List Bool} {n m k : α → ℕ} {c : (a : α) → Circuit σ (n a) (m a)} (hn : Turing.MultiTapeTM.ComputableInPolyTime n enc Turing.MultiTapeTM.encNat) (hk : Turing.MultiTapeTM.ComputableInPolyTime k enc Turing.MultiTapeTM.encUnary) (hc : Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => (c a).code) enc (encCode encOp)) :
Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => ((c a).replicate (k a)).code) enc (encCode encOp)

Emit the code of a typed Circuit.replicate from binary input arity, unary copy count, and the base circuit code.

theorem Cslib.Circuits.Circuit.computableInPolyTime_code_balanced {σ : Signature} {encOp : σ.Op ↪ List Bool} {α : Type} {enc : α ↪ List Bool} {n r : α → ℕ} {op : (a : α) → Circuit σ (r a + r a) (r a)} {unit : (a : α) → Circuit σ (n a) (r a)} {leaves : (a : α) → List (Circuit σ (n a) (r a))} (hn : Turing.MultiTapeTM.ComputableInPolyTime n enc Turing.MultiTapeTM.encNat) (hop : Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => (op a).code) enc (encCode encOp)) (hunit : Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => (unit a).code) enc (encCode encOp)) (hleaves : Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => List.map code (leaves a)) enc (Turing.MultiTapeTM.encList (encCode encOp))) :
Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => ((op a).balanced (unit a) (leaves a)).code) enc (encCode encOp)

Emit the code of a typed Circuit.balanced tree from binary input arity and the operator, fallback, and leaf circuit codes.

theorem Cslib.Circuits.Circuit.computableInPolyTime_code_ite {σ : Signature} {encOp : σ.Op ↪ List Bool} {α : Type} {enc : α ↪ List Bool} {n m : α → ℕ} {p : α → Prop} [(a : α) → Decidable (p a)] {t e : (a : α) → Circuit σ (n a) (m a)} (hp : Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => decide (p a)) enc Turing.MultiTapeTM.encBool) (ht : Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => (t a).code) enc (encCode encOp)) (he : Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => (e a).code) enc (encCode encOp)) :
Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => (if p a then t a else e a).code) enc (encCode encOp)

Emit the code of a conditional choice of typed circuits.

@[irreducible]
def Cslib.Circuits.Circuit.ComputableCircuitCode {σ : Signature} {α : Type} {n m : α → ℕ} (c : (a : α) → Circuit σ (n a) (m a)) (enc : α ↪ List Bool) (encOp : σ.Op ↪ List Bool) :

Uniform polynomial-time generation of the serialized code (c a).code of a typed circuit family c : (a : α) → Circuit σ (n a) (m a) under explicit input encoding enc and operation encoding encOp. Indexing @[fun_prop] rules on ComputableCircuitCode keys each rule by its outer Circuit constructor (comp, append, replicate, wiring, pi, balanced, ite) instead of colliding on Circuit.code.

theorem Cslib.Circuits.Circuit.computableInPolyTime_code {σ : Signature} {encOp : σ.Op ↪ List Bool} {α : Type} {enc : α ↪ List Bool} {n m : α → ℕ} {c : (a : α) → Circuit σ (n a) (m a)} (hc : ComputableCircuitCode c enc encOp) :
Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => (c a).code) enc (encCode encOp)

Bridge ComputableInPolyTime (fun a => (c a).code) to constructor-indexed ComputableCircuitCode.

theorem Cslib.Circuits.Circuit.computableCircuitCode_of_computableInPolyTime {σ : Signature} {encOp : σ.Op ↪ List Bool} {α : Type} {enc : α ↪ List Bool} {n m : α → ℕ} {c : (a : α) → Circuit σ (n a) (m a)} (hc : Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => (c a).code) enc (encCode encOp)) :

Bridge a plain ComputableInPolyTime (fun a => (c a).code) hypothesis (such as a free circuit-family parameter) into ComputableCircuitCode.

theorem Cslib.Circuits.Circuit.ComputableCircuitCode.computableInPolyTime {σ : Signature} {encOp : σ.Op ↪ List Bool} {α : Type} {enc : α ↪ List Bool} {n m : α → ℕ} {c : (a : α) → Circuit σ (n a) (m a)} (hc : ComputableCircuitCode c enc encOp) :
Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => (c a).code) enc (encCode encOp)

Extract the underlying ComputableInPolyTime certificate from ComputableCircuitCode.

theorem Cslib.Circuits.Circuit.computableCircuitCode_const {σ : Signature} {encOp : σ.Op ↪ List Bool} {α : Type} {enc : α ↪ List Bool} {n m : ℕ} (c : Circuit σ n m) :
ComputableCircuitCode (fun (x : α) => c) enc encOp

A fixed typed circuit has constant code in polynomial time.

theorem Cslib.Circuits.Circuit.computableCircuitCode_id {σ : Signature} {encOp : σ.Op ↪ List Bool} {α : Type} {enc : α ↪ List Bool} {n : α → ℕ} (hn : Turing.MultiTapeTM.ComputableInPolyTime n enc Turing.MultiTapeTM.encUnary) :
ComputableCircuitCode (fun (a : α) => Circuit.id σ (n a)) enc encOp

@[fun_prop] rule for Circuit.id.

theorem Cslib.Circuits.Circuit.computableCircuitCode_wiring {σ : Signature} {encOp : σ.Op ↪ List Bool} {α : Type} {enc : α ↪ List Bool} {n m : α → ℕ} {sel : (a : α) → Fin (m a) → Fin (n a)} (hm : Turing.MultiTapeTM.ComputableInPolyTime m enc Turing.MultiTapeTM.encUnary) (hsel : Turing.MultiTapeTM.ComputableInPolyTime (fun (q : (a : α) × Fin (m a)) => ↑(sel q.fst q.snd)) (encWithFinUnary enc m) Turing.MultiTapeTM.encNat) :
ComputableCircuitCode (fun (a : α) => wiring σ (sel a)) enc encOp

@[fun_prop] rule for Circuit.wiring.

theorem Cslib.Circuits.Circuit.computableCircuitCode_append {σ : Signature} {encOp : σ.Op ↪ List Bool} {α : Type} {enc : α ↪ List Bool} {n m p : α → ℕ} {c : (a : α) → Circuit σ (n a) (m a)} {d : (a : α) → Circuit σ (n a) (p a)} (hn : Turing.MultiTapeTM.ComputableInPolyTime n enc Turing.MultiTapeTM.encNat) (hc : ComputableCircuitCode c enc encOp) (hd : ComputableCircuitCode d enc encOp) :
ComputableCircuitCode (fun (a : α) => (c a).append (d a)) enc encOp

@[fun_prop] rule for Circuit.append.

theorem Cslib.Circuits.Circuit.computableCircuitCode_pi {σ : Signature} {encOp : σ.Op ↪ List Bool} {α : Type} {enc : α ↪ List Bool} {n k : α → ℕ} {cs : (a : α) → Fin (k a) → Circuit σ (n a) 1} (hn : Turing.MultiTapeTM.ComputableInPolyTime n enc Turing.MultiTapeTM.encNat) (hk : Turing.MultiTapeTM.ComputableInPolyTime k enc Turing.MultiTapeTM.encUnary) (hcs : ComputableCircuitCode (fun (q : (a : α) × Fin (k a)) => cs q.fst q.snd) (encWithFinUnary enc k) encOp) :
ComputableCircuitCode (fun (a : α) => pi (cs a)) enc encOp

@[fun_prop] rule for Circuit.pi.

theorem Cslib.Circuits.Circuit.computableCircuitCode_comp {σ : Signature} {encOp : σ.Op ↪ List Bool} {α : Type} {enc : α ↪ List Bool} {n m p : α → ℕ} {d : (a : α) → Circuit σ (m a) (p a)} {c : (a : α) → Circuit σ (n a) (m a)} (hn : Turing.MultiTapeTM.ComputableInPolyTime n enc Turing.MultiTapeTM.encNat) (hd : ComputableCircuitCode d enc encOp) (hc : ComputableCircuitCode c enc encOp) :
ComputableCircuitCode (fun (a : α) => (d a).comp (c a)) enc encOp

@[fun_prop] rule for Circuit.comp.

theorem Cslib.Circuits.Circuit.computableCircuitCode_replicate {σ : Signature} {encOp : σ.Op ↪ List Bool} {α : Type} {enc : α ↪ List Bool} {n m k : α → ℕ} {c : (a : α) → Circuit σ (n a) (m a)} (hn : Turing.MultiTapeTM.ComputableInPolyTime n enc Turing.MultiTapeTM.encNat) (hk : Turing.MultiTapeTM.ComputableInPolyTime k enc Turing.MultiTapeTM.encUnary) (hc : ComputableCircuitCode c enc encOp) :
ComputableCircuitCode (fun (a : α) => (c a).replicate (k a)) enc encOp

@[fun_prop] rule for Circuit.replicate.

theorem Cslib.Circuits.Circuit.computableCircuitCode_balanced {σ : Signature} {encOp : σ.Op ↪ List Bool} {α : Type} {enc : α ↪ List Bool} {n r : α → ℕ} {op : (a : α) → Circuit σ (r a + r a) (r a)} {unit : (a : α) → Circuit σ (n a) (r a)} {leaves : (a : α) → List (Circuit σ (n a) (r a))} (hn : Turing.MultiTapeTM.ComputableInPolyTime n enc Turing.MultiTapeTM.encNat) (hop : ComputableCircuitCode op enc encOp) (hunit : ComputableCircuitCode unit enc encOp) (hleaves : Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => List.map code (leaves a)) enc (Turing.MultiTapeTM.encList (encCode encOp))) :
ComputableCircuitCode (fun (a : α) => (op a).balanced (unit a) (leaves a)) enc encOp

@[fun_prop] rule for Circuit.balanced.

theorem Cslib.Circuits.Circuit.computableCircuitCode_ite {σ : Signature} {encOp : σ.Op ↪ List Bool} {α : Type} {enc : α ↪ List Bool} {n m : α → ℕ} {p : α → Prop} [(a : α) → Decidable (p a)] {t e : (a : α) → Circuit σ (n a) (m a)} (hp : Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => decide (p a)) enc Turing.MultiTapeTM.encBool) (ht : ComputableCircuitCode t enc encOp) (he : ComputableCircuitCode e enc encOp) :
ComputableCircuitCode (fun (a : α) => if p a then t a else e a) enc encOp

@[fun_prop] rule for conditional choice of typed circuits.

theorem Cslib.Circuits.Formula.computableInPolyTime_code_input_toCircuit {σ : Signature} {encOp : σ.Op ↪ List Bool} {α : Type} {enc : α ↪ List Bool} {n : α → ℕ} {w : (a : α) → Fin (n a)} (hw : Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => ↑(w a)) enc Turing.MultiTapeTM.encNat) :

Emit the circuit code of an input formula (Formula.input (w a)).toCircuit.

theorem Cslib.Circuits.Formula.computableInPolyTime_code_gate_toCircuit {σ : Signature} {encOp : σ.Op ↪ List Bool} {α : Type} {enc : α ↪ List Bool} {n : α → ℕ} (op : σ.Op) {args : (a : α) → Fin (σ.Arity op) → Formula σ (n a)} (hn : Turing.MultiTapeTM.ComputableInPolyTime n enc Turing.MultiTapeTM.encNat) (hargs : ∀ (j : Fin (σ.Arity op)), Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => (args a j).toCircuit.code) enc (Circuit.encCode encOp)) :
Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => (gate op (args a)).toCircuit.code) enc (Circuit.encCode encOp)

Emit the circuit code of a fixed-operation formula node (Formula.gate op (args a)).toCircuit from binary input arity and the subformula circuit codes.

theorem Cslib.Circuits.Formula.computableInPolyTime_code_ite_toCircuit {σ : Signature} {encOp : σ.Op ↪ List Bool} {α : Type} {enc : α ↪ List Bool} {n : α → ℕ} {p : α → Prop} [(a : α) → Decidable (p a)] {t e : (a : α) → Formula σ (n a)} (hp : Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => decide (p a)) enc Turing.MultiTapeTM.encBool) (ht : Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => (t a).toCircuit.code) enc (Circuit.encCode encOp)) (he : Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => (e a).toCircuit.code) enc (Circuit.encCode encOp)) :

Emit the circuit code of a conditional choice of formulas (if p a then t a else e a).toCircuit.