The packed-history compiler
A primitive program with \(m\) instructions over \(r\) registers is compiled by packing a run of length \(q\) into base-\(B\) integers, with \(K=2^c\), \(B=4K>m\), \(T=B^q\), \(F_q=1+B+\dots+B^q\) and \(D=(K-1)F_q\), every input being below \(K\).
DefinitionThe packed-history construction#✓
Let \(p=(p_0,\dots,p_{m-1})\) be a primitive program valid for \(r\) registers and let \(1\le k\le r\). The packed-history construction of \(p\) is the natural construction in hierarchical mode with \(k\) inputs \(a_0,\dots,a_{k-1}\) (\(=X_0,\dots,X_{k-1}\)) which takes the fresh variables $\(q,\ c,\ K,\ B,\ T,\ F,\ D,\qquad R_0,\dots,R_{r-1},\qquad N_0,\dots,N_{r-1},\qquad S_0,\dots,S_{m-1}\)$ in this order and returns the conjunction of the following formulas, in this order:
- \(\mathrm{lt}(0,c)\), \(\mathrm{power}(K,2,c)\), \(B\doteq4K\), \(\mathrm{lt}(m,B)\), \(\mathrm{power}(T,B,q)\), then \(\mathrm{lt}(a_i,K)\) for \(i<k\);
- \(BF+1\doteq F+BT\), \(D+F\doteq KF\), \(S_0+\dots+S_{m-1}\doteq F\), \(S_{m-1}\doteq T\);
- \(\mathrm{mask}(S_i,F)\) for \(i<m\);
- for each \(j<r\): \(\mathrm{mask}(R_j,D)\), \(\mathrm{lt}(R_j,T)\), \(\mathrm{bitAnd}(K\cdot N_j,\ R_j+D,\ K\cdot F)\);
- for each sub instruction \(p_i=\mathrm{dec}(j,x,y)\), in increasing \(i\): fresh \(V_i,W_i\), then \(\mathrm{bitAnd}(V_i,S_i,N_j)\) and \(S_i\doteq V_i+W_i\);
- for each \(j<r\): \(R_j+B\cdot C_j\doteq\iota_j+B\cdot R_j+B\cdot A_j\), where \(\iota_j=a_j\) for \(j<k\) and \(0\) otherwise, \(A_j=\sum_{i:\,p_i=\mathrm{inc}(j,\cdot)}S_i\) and \(C_j=\sum_{i:\,p_i=\mathrm{dec}(j,\cdot,\cdot)}V_i\);
- for each \(i<m\): \(S_i\doteq[i=0]+B\cdot I_i\), where \(I_i\) is the sum, in program order, of \(S_{i'}\) for \(p_{i'}=\mathrm{inc}(\cdot,i)\), of \(V_{i'}\) for \(p_{i'}=\mathrm{dec}(\cdot,i,\cdot)\) and of \(W_{i'}\) for \(p_{i'}=\mathrm{dec}(\cdot,\cdot,i)\) (for a sub instruction its nonzero target is listed before its zero target).
Empty sums are the constant \(0\). The compiled circuit \(\mathcal C_{p,k}\) is the natural circuit of this construction. Here \(R_j\) is the packed history of register \(j\), \(S_i\) that of state \(i\), \(N_j\) the packed nonzero indicator, \(V_i,W_i\) the nonzero and zero branch histories, and \(F=F_q=1+B+\dots+B^q\), \(D=(K-1)F_q\) by item 2.
This is a definition. No equation is repeated \(q\) times: \(q\) is one variable and the number of conjuncts depends only on \(p\).
ZInQ.Compiler.Packed.construction · 16 lines
def construction (p : Program) (r k : ℕ) : NM NForm := do
let a := inputs k
let zero ← constant 0
let one ← constant 1
let q ← fresh
let c ← fresh
let K ← fresh
let B ← fresh
let T ← fresh
let F ← fresh
let D ← fresh
let R ← freshN r
let N ← freshN r
let S ← freshN p.length
let φs ← packedFormulas p r a zero one q c K B T F D R N S
pure (.and φs)
def compiled (p : Program) (r k : ℕ) : NatCircuit := (construction p r k).toCircuit k
abbrev Program : Type := List Instr
ZInQKernel/Compiler/Circuits/RegisterMachine.lean:60
def NM (α : Type) : Type := State → α × State
ZInQKernel/Compiler/Circuits/Construction.lean:118
inductive NForm
/-- The atom `a ≐ b`. -/
| eq (a b : Ex)
/-- The atom `⟨f⟩`. -/
| atom (f : Ex)
/-- The call atom `⟨𝒫[P, Q, R; w]⟩`, with the index of its call node. -/
| call (P Q R : Ex) (w : ℕ) (node : ℕ)
/-- The conjunction. -/
| and (fs : List NForm)
/-- The disjunction. -/
| or (fs : List NForm)
ZInQKernel/Compiler/Circuits/Construction.lean:65
def inputs (k : ℕ) : List Ex := (List.range k).map fun i => ⟨i, X i⟩
ZInQKernel/Compiler/Circuits/Circuit.lean:104
def constant (c : ℤ) : NM Ex := do
let i ← node (.const c)
pure ⟨i, C c⟩
ZInQKernel/Compiler/Circuits/Construction.lean:148
def fresh : NM Ex := fun s =>
let r := node (.var s.next)
{ s with next := s.next + 1, vars := s.vars ++ [(s.next, .witness)] }
(⟨r.1, X s.next⟩, r.2)
ZInQKernel/Compiler/Circuits/Construction.lean:153
def freshN : ℕ → NM (List Ex)
| 0 => pure []
| n + 1 => do
let h ← fresh
let hs ← freshN n
pure (h :: hs)
ZInQKernel/Compiler/Circuits/Packed/Construction.lean:39
def packedFormulas (p : Program) (r : ℕ) (a : List Ex) (zero one q c K B T F D : Ex)
(R N S : List Ex) : NM (List NForm) :=
appendM (items1 p a zero q c K B T) <|
appendM (items2 zero one K B T F D S) <|
appendM (items3 F S) <|
appendM (items4 r zero K D T F R N) do
let sb ← subBranches zero S N 0 p
appendM (pure sb.1) <|
appendM (items6 p r a zero B R S sb.2) (items7 p zero one B S sb.2)
ZInQKernel/Compiler/Circuits/Packed/Construction.lean:154
structure NatCircuit where
/-- The number `k` of inputs. -/
inputCount : ℕ
/-- The explicit variables (index and kind), the inputs first. -/
explicitVars : List (ℕ × VarKind)
/-- The node list. -/
nodes : List NatNode
/-- The root node. -/
root : ℕ
/-- The variable count `V`. -/
varCount : ℕ
ZInQKernel/Compiler/Circuits/Circuit.lean:33
def construction (p : Program) (r k : ℕ) : NM NForm := do
let a := inputs k
let zero ← constant 0
let one ← constant 1
let q ← fresh
let c ← fresh
let K ← fresh
let B ← fresh
let T ← fresh
let F ← fresh
let D ← fresh
let R ← freshN r
⋯
ZInQKernel/Compiler/Circuits/Packed/Construction.lean:165
Sources
Uses 3 · Used by 6
Masks by \(D\) and \(F_q\) and the bound \(R_j<T\) make register digits smaller than \(K\) with a zero final digit, and state digits zero or one; since \(B>m\) the state sum selects exactly one state at each time.
TheoremDigits of masked histories#✓
Let \(c>0\), \(K=2^c\), \(B=4K\), \(q\in\mathbb N\), \(T=B^q\), and let \(F,D\in\mathbb N\) satisfy \(BF+1=F+BT\) and \(D+F=KF\). Then:
- \(F=F_q=\sum_{t=0}^qB^t\) and \(D=(K-1)F_q=\sum_{t=0}^q(K-1)B^t\);
- a natural number \(R\) is masked by \(D\) and satisfies \(R<T\) if and only if \(R=\sum_{t=0}^q\rho_tB^t\) with \(\rho_t<K\) for all \(t\) and \(\rho_q=0\);
- a natural number \(S\) is masked by \(F_q\) if and only if \(S=\sum_{t=0}^q\sigma_tB^t\) with \(\sigma_t\in\{0,1\}\).
In words: register digits are below \(K\), the terminal register digit is zero, and state digits are zero or one. The digit sequences are unique.
Proof sketch. (1) \(B\ge8\), so \(BF+1=F+BT\) gives \((B-1)F=B^{q+1}-1\), hence \(F=\sum_{t\le q}B^t\); then \(D=KF-F\). (2), (3) \(B=2^{c+2}\), so the binary expansion of a number splits into base-\(B\) slots of \(c+2\) bits. \(D\) has, in each slot \(t\le q\), the bits \(0,\dots,c-1\) set (value \(K-1\)) and nothing above slot \(q\); \(F_q\) has bit \(0\) of each slot \(t\le q\). Masking by \(D\) (resp. \(F_q\)) therefore says exactly that every slot digit is below \(K\) (resp. at most \(1\)) and that the slots above \(q\) vanish. Finally \(R<B^q\) is equivalent to \(\rho_q=0\) given the other slots vanish.
theorem packed_masks {c q K B T F D : ℕ} (_hc : 0 < c) (hK : K = 2 ^ c) (hB : B = 4 * K)
(hT : T = B ^ q) (hF : B * F + 1 = F + B * T) (hD : D + F = K * F) :
(F = ∑ t ∈ range (q + 1), B ^ t ∧ D = ∑ t ∈ range (q + 1), (K - 1) * B ^ t) ∧
(∀ R, (Nat.land R D = R ∧ R < T) ↔
∃ ρ : ℕ → ℕ, (∀ t ≤ q, ρ t < K) ∧ ρ q = 0 ∧ R = ∑ t ∈ range (q + 1), ρ t * B ^ t) ∧
(∀ S, Nat.land S F = S ↔
∃ σ : ℕ → ℕ, (∀ t ≤ q, σ t ≤ 1) ∧ S = ∑ t ∈ range (q + 1), σ t * B ^ t)
TheoremOne active state per time slot#✓
Let \(B,q,T=B^q,F_q\) be as in the masked-digit statement, let \(1\le m<B\), and let \(S_0,\dots,S_{m-1}\) be masked by \(F_q\), with digits \(\sigma_i(t)\in\{0,1\}\) (\(t\le q\)). If $\(S_0+\dots+S_{m-1}=F_q\qquad\text{and}\qquad S_{m-1}=T,\)$ then for every \(t\le q\) there is exactly one \(i<m\) with \(\sigma_i(t)=1\), and \(\sigma_{m-1}(t)=1\) if and only if \(t=q\). Conversely, if exactly one state digit is \(1\) in each slot \(t\le q\) and the last state's digit is \(1\) exactly at \(t=q\), both equations hold.
Proof sketch. In slot \(t\) the digits sum to \(\sum_i\sigma_i(t)\le m<B\), so the sum of the \(S_i\) is computed without carries and its slot-\(t\) digit is \(\sum_i\sigma_i(t)\); comparing with the digit \(1\) of \(F_q\) gives exactly one active state. \(S_{m-1}=B^q\) says the last state's digits are \(0\) except at \(t=q\).
theorem one_active_state {B q m : ℕ} (hm : m < B) (hm0 : 0 < m) (σ : ℕ → ℕ → ℕ)
(hσ : ∀ i < m, ∀ t ≤ q, σ i t ≤ 1) :
(∑ i ∈ range m, ∑ t ∈ range (q + 1), σ i t * B ^ t = ∑ t ∈ range (q + 1), B ^ t ∧
∑ t ∈ range (q + 1), σ (m - 1) t * B ^ t = B ^ q) ↔
(∀ t ≤ q, ∃! i, i < m ∧ σ i t = 1) ∧ (∀ t ≤ q, σ (m - 1) t = 1 ↔ t = q)
Uses 1 · Used by 2
The nonzero indicator is read off one bit per slot of \(R_j+D\), and a sub instruction splits its state history into nonzero and zero branch histories; the increment, decrement and incoming indicators then have zero-one digits.
TheoremThe packed nonzero indicator#✓
Let \(c>0\), \(K=2^c\), \(B=4K\), \(F_q\), \(D=(K-1)F_q\) be as in the masked-digit statement and \(R=\sum_{t=0}^q\rho_tB^t\) with all \(\rho_t<K\). Then $\((R+D)\mathbin{\mathrm{AND}}(KF_q)=K\cdot\sum_{t=0}^q[\rho_t>0]\,B^t .\)$ Consequently, for \(N\in\mathbb N\), \(KN=(R+D)\mathbin{\mathrm{AND}}(KF_q)\) holds if and only if \(N=\sum_{t\le q}[\rho_t>0]B^t\), the packed nonzero indicator of \(R\); no separate mask condition on \(N\) is needed.
Proof sketch. \(R+D=\sum_t(\rho_t+K-1)B^t\), and each slot digit satisfies \(\rho_t+K-1\le2K-2<2K<B\), so the sum has no carries. \(KF_q\) has exactly bit \(c\) of each slot \(t\le q\). The bit \(c\) (value \(K\)) of the slot digit \(\rho_t+K-1\in[K-1,2K-2]\) is set exactly when \(\rho_t+K-1\ge K\), that is when \(\rho_t>0\). Cancel the factor \(K=2^c\).
theorem nonzero_indicator {c q K B F D : ℕ} (_hc : 0 < c) (hK : K = 2 ^ c) (hB : B = 4 * K)
(hF : F = ∑ t ∈ range (q + 1), B ^ t) (hD : D = (K - 1) * F) (ρ : ℕ → ℕ)
(hρ : ∀ t ≤ q, ρ t < K) :
Nat.land (∑ t ∈ range (q + 1), ρ t * B ^ t + D) (K * F) =
K * ∑ t ∈ range (q + 1), (if 0 < ρ t then 1 else 0) * B ^ t ∧
∀ N, K * N = Nat.land (∑ t ∈ range (q + 1), ρ t * B ^ t + D) (K * F) ↔
N = ∑ t ∈ range (q + 1), (if 0 < ρ t then 1 else 0) * B ^ t
Uses 1 · Used by 2
TheoremBranch histories and indicator digits#✓
Let \(B\ge2\), \(q\in\mathbb N\), \(S=\sum_{t\le q}\sigma_tB^t\) and \(N=\sum_{t\le q}\nu_tB^t\) with \(\sigma_t,\nu_t\in\{0,1\}\), \(B\) a power of two.
- \(V=S\mathbin{\mathrm{AND}}N\) and \(S=V+W\) hold (for natural \(V,W\)) if and only if \(V=\sum_t\sigma_t\nu_tB^t\) and \(W=\sum_t\sigma_t(1-\nu_t)B^t\).
- In the setting of the packed-history construction, suppose that in every slot \(t\le q\) exactly one state digit \(\sigma_i(t)\) is \(1\) and that the halt state has no outgoing instruction. Then the packed indicators \(A_j\), \(C_j\) and \(I_i\) of items 6 and 7 of the construction have all slot digits in \(\{0,1\}\); precisely, writing \(i_t\) for the active state at time \(t\), the slot-\(t\) digit of \(A_j\) is \([p_{i_t}=\mathrm{inc}(j,\cdot)]\), that of \(C_j\) is \([p_{i_t}=\mathrm{dec}(j,\cdot,\cdot)]\,\nu_j(t)\), and that of \(I_i\) is \(1\) exactly when the instruction at \(i_t\) takes, at time \(t\), a branch to \(i\) (the nonzero branch of a sub instruction if \(\nu_j(t)=1\), its zero branch otherwise).
Proof sketch. (1) Both \(S\) and \(N\) have zero-one digits in distinct bit positions, so \(S\mathbin{\mathrm{AND}}N\) is computed slotwise; \(W=S-V\) slotwise. (2) A sum of packed numbers with zero-one digits is computed slotwise as long as the slot sums stay below \(B\); at time \(t\) only the instruction at \(i_t\) contributes, through \(S_{i_t}\), \(V_{i_t}\) or \(W_{i_t}\), and for a sub instruction exactly one of \(V_{i_t},W_{i_t}\) has digit \(1\) by (1); this holds even if two branches of the program have the same target, since only one branch is taken at a time.
theorem branch_split {b q : ℕ} (σ ν : ℕ → ℕ) (hσ : ∀ t ≤ q, σ t ≤ 1) (hν : ∀ t ≤ q, ν t ≤ 1)
(hb : 1 ≤ b) (V W : ℕ) :
(V = Nat.land (∑ t ∈ range (q + 1), σ t * (2 ^ b) ^ t) (∑ t ∈ range (q + 1), ν t * (2 ^ b) ^ t) ∧
∑ t ∈ range (q + 1), σ t * (2 ^ b) ^ t = V + W) ↔
(V = ∑ t ∈ range (q + 1), σ t * ν t * (2 ^ b) ^ t ∧
W = ∑ t ∈ range (q + 1), σ t * (1 - ν t) * (2 ^ b) ^ t)
theorem indicator_digits {B q : ℕ} (p : Program) (act : ℕ → ℕ) (ν : ℕ → ℕ → ℕ)
(hact : ∀ t ≤ q, act t < p.length) (S V W : ℕ → ℕ)
(hS : ∀ i < p.length, S i = ∑ t ∈ range (q + 1), (if act t = i then 1 else 0) * B ^ t)
(hV : ∀ i < p.length, ∀ j, decOn p j i →
V i = ∑ t ∈ range (q + 1), (if act t = i then 1 else 0) * ν j t * B ^ t)
(hW : ∀ i < p.length, ∀ j, decOn p j i →
W i = ∑ t ∈ range (q + 1), (if act t = i then 1 else 0) * (1 - ν j t) * B ^ t) :
(∀ j, incSum p S j = ∑ t ∈ range (q + 1), (if incOn p j (act t) then 1 else 0) * B ^ t) ∧
(∀ j, decSum p V j = ∑ t ∈ range (q + 1), (if decOn p j (act t) then ν j t else 0) * B ^ t) ∧
(∀ i, inSum p S V W i =
∑ t ∈ range (q + 1), inDigit p (act t) i (ν (decReg p (act t)) t) * B ^ t)
abbrev Program : Type := List Instr
ZInQKernel/Compiler/Circuits/RegisterMachine.lean:60
def decOn (p : Program) (j i : ℕ) : Prop := ∃ x y, p[i]? = some (.dec j x y)
ZInQKernel/Compiler/Circuits/Packed/Branches.lean:61
def incSum (p : Program) (S : ℕ → ℕ) (j : ℕ) : ℕ :=
∑ i ∈ range p.length, if incOn p j i then S i else 0
ZInQKernel/Compiler/Circuits/Packed/Branches.lean:122
def incOn (p : Program) (j i : ℕ) : Prop := ∃ x, p[i]? = some (.inc j x)
ZInQKernel/Compiler/Circuits/Packed/Branches.lean:58
def decSum (p : Program) (V : ℕ → ℕ) (j : ℕ) : ℕ :=
∑ i ∈ range p.length, if decOn p j i then V i else 0
ZInQKernel/Compiler/Circuits/Packed/Branches.lean:126
def inSum (p : Program) (S V W : ℕ → ℕ) (i : ℕ) : ℕ :=
∑ i' ∈ range p.length,
((if incTo p i' i then S i' else 0) + (if nzTo p i' i then V i' else 0) +
(if zTo p i' i then W i' else 0))
ZInQKernel/Compiler/Circuits/Packed/Branches.lean:131
def inDigit (p : Program) (a i n : ℕ) : ℕ :=
(if incTo p a i then 1 else 0) + (if nzTo p a i then n else 0) + (if zTo p a i then 1 - n else 0)
ZInQKernel/Compiler/Circuits/Packed/Branches.lean:138
def decReg (p : Program) (i : ℕ) : ℕ :=
match p[i]? with
| some (.dec j _ _) => j
| _ => 0
ZInQKernel/Compiler/Circuits/Packed/Branches.lean:73
Uses 2 · Used by 2
The register and state equations have all coefficients below \(B\), so uniqueness of base-\(B\) digits turns them into the initial conditions and the exact transitions, the extra final slot vanishing.
TheoremDecoding the transition equations#✓
Let \(K<B\) be natural numbers and \(q\in\mathbb N\).
- (Registers) Let \(\rho_t<K\), \(\alpha_t\le1\), \(\gamma_t\le1\) for \(t\le q\), let \(\iota<K\), and put \(R=\sum_{t\le q}\rho_tB^t\), \(A=\sum_{t\le q}\alpha_tB^t\), \(C=\sum_{t\le q}\gamma_tB^t\). Then $\(R+BC=\iota+BR+BA\)$ holds if and only if \(\rho_0=\iota\), \(\rho_{t+1}+\gamma_t=\rho_t+\alpha_t\) for all \(t<q\), and \(\gamma_q=\rho_q+\alpha_q\).
- (States) Suppose \(B\ge2\). Let \(\sigma_t\le1\) and \(\eta_t\le1\) for \(t\le q\), \(S=\sum_{t\le q}\sigma_tB^t\), \(I=\sum_{t\le q}\eta_tB^t\) and \(e\in\{0,1\}\). Then \(S=e+BI\) holds if and only if \(\sigma_0=e\), \(\sigma_{t+1}=\eta_t\) for \(t<q\), and \(\eta_q=0\).
Proof sketch. Compare base-\(B\) digits. In item 1 the slot-\(0\) digits are \(\rho_0\) and \(\iota\), the slot-\((t+1)\) digits are \(\rho_{t+1}+\gamma_t\le K\) and \(\rho_t+\alpha_t\le K\) for \(t<q\), and the slot-\((q+1)\) digits are \(\gamma_q\) and \(\rho_q+\alpha_q\le K\); every digit on either side is at most \(K<B\), so uniqueness of base-\(B\) expansions (with an unbounded top part) gives digitwise equality, and conversely. Item 2 is the same with digits at most \(1<B\).
theorem packed_register_iff {B K q ι : ℕ} (hKB : K < B) (ρ α γ : ℕ → ℕ)
(hρ : ∀ t ≤ q, ρ t < K) (hα : ∀ t ≤ q, α t ≤ 1) (hγ : ∀ t ≤ q, γ t ≤ 1) (hι : ι < K) :
(∑ t ∈ range (q + 1), ρ t * B ^ t) + B * (∑ t ∈ range (q + 1), γ t * B ^ t) =
ι + B * (∑ t ∈ range (q + 1), ρ t * B ^ t) + B * (∑ t ∈ range (q + 1), α t * B ^ t) ↔
ρ 0 = ι ∧ (∀ t < q, ρ (t + 1) + γ t = ρ t + α t) ∧ γ q = ρ q + α q
theorem packed_state_iff {B q e : ℕ} (hB : 1 < B) (σ η : ℕ → ℕ) (hσ : ∀ t ≤ q, σ t ≤ 1)
(hη : ∀ t ≤ q, η t ≤ 1) (he : e ≤ 1) :
(∑ t ∈ range (q + 1), σ t * B ^ t) = e + B * (∑ t ∈ range (q + 1), η t * B ^ t) ↔
σ 0 = e ∧ (∀ t < q, σ (t + 1) = η t) ∧ η q = 0
Every solution decodes to an accepting run, and every accepting run fits once \(c\) is large. The compiled circuit therefore has a natural zero exactly at the accepted inputs.
TheoremSoundness of the packed-history construction#✓
Let \(p\) be valid for \(r\) registers with \(m\) instructions, \(1\le k\le r\) and \(a\in\mathbb N^k\). If the formula of the packed-history construction of \(p\) holds for some natural values of its fresh variables, with every macro read as its relation (masks as bit inclusion, bitAnd as bitwise conjunction, power as exponentiation, lt as strict inequality), then \(p\) accepts \(a\), and its accepting run has exactly \(q\) steps.
Proof sketch. From \(c>0\), \(K=2^c\), \(B=4K\), \(T=B^q\) and the two equations for \(F,D\), the masked-digit statement decodes \(R_j=\sum_{t\le q}\rho_j(t)B^t\) with \(\rho_j(t)<K\), \(\rho_j(q)=0\), and \(S_i=\sum_{t\le q}\sigma_i(t)B^t\) with zero-one digits; \(m<B\) and the state equations give one active state \(i_t\) per slot with the halt state \(m-1\) active exactly at \(t=q\). The nonzero indicator gives \(N_j\) with digits \(\nu_j(t)=[\rho_j(t)>0]\), and the branch statement gives \(V_i,W_i\) and the indicator digits. Define the configuration at time \(t\le q\) as \((i_t,\rho(t))\). The state equations (decoded with \(e=[i=0]\)) give \(i_0=0\) and \(i_{t+1}=\) the branch target taken at time \(t\); the register equations (decoded with \(\iota_j=a_j<K\) or \(0\)) give \(\rho_j(0)=\iota_j\) and \(\rho_j(t+1)=\rho_j(t)+\alpha_j(t)-\gamma_j(t)\), which is exactly the effect of the instruction at \(i_t\) (a decrement happens only when \(\nu_j(t)=1\), that is when \(\rho_j(t)>0\)). Since the halt state is active only at \(t=q\), the configurations form the run of \(p\) from the initial file, it reaches the final state at time \(q\), and there \(\rho_j(q)=0\) for all \(j\). The extra slot \(q+1\) of the register equation, \(\gamma_j(q)=\rho_j(q)+\alpha_j(q)\), is \(0=0\) because halt has no outgoing instruction, and the extra slot of the state equation says \(\eta_i(q)=0\) for the same reason. If \(q=0\) the state \(0\) is the halt state (so \(m=1\)) and every input vanishes.
theorem packed_sound {p : Program} {r k : ℕ} (hp : p.Valid r) (hk1 : 1 ≤ k) (hkr : k ≤ r)
{a : Fin k → ℕ} {q c K B T F D : ℕ} {R N S V W : ℕ → ℕ}
(h : PackedConditions p r k a q c K B T F D R N S V W) :
∃ cfg, RM.stepN p.toMachine q ⟨0, initFile a⟩ = some cfg ∧ cfg.state = p.length - 1 ∧
∀ j, cfg.regs j = 0
theorem packed_sound_accepts {p : Program} {r k : ℕ} (hp : p.Valid r) (hk1 : 1 ≤ k)
(hkr : k ≤ r) {a : Fin k → ℕ} {q c K B T F D : ℕ} {R N S V W : ℕ → ℕ}
(h : PackedConditions p r k a q c K B T F D R N S V W) : p.Accepts a
abbrev Program : Type := List Instr
ZInQKernel/Compiler/Circuits/RegisterMachine.lean:60
structure PackedConditions (p : Program) (r k : ℕ) (a : Fin k → ℕ) (q c K B T F D : ℕ)
(R N S V W : ℕ → ℕ) : Prop where
c_pos : 0 < c
hK : K = 2 ^ c
hB : B = 4 * K
m_lt : p.length < B
hT : T = B ^ q
inputs_lt : ∀ i : Fin k, a i < K
hF : B * F + 1 = F + B * T
hD : D + F = K * F
sumS : ∑ i ∈ range p.length, S i = F
lastS : S (p.length - 1) = T
⋯
ZInQKernel/Compiler/Circuits/Packed/Sound.lean:23
def stepN (μ : Machine) : ℕ → Config → Option Config
| 0, c => some c
| n + 1, c => (step μ c).bind (stepN μ n)
ZInQKernel/Compiler/Circuits/RegisterMachine.lean:51
def initFile {k : ℕ} (a : Fin k → ℕ) : ℕ → ℕ := fun i => if h : i < k then a ⟨i, h⟩ else 0
ZInQKernel/Compiler/Circuits/RegisterMachine.lean:81
Sources
Uses 7 · Used by 1
TheoremCompleteness of the packed-history construction#✓
Let \(p\) be valid for \(r\) registers with \(m\) instructions, \(1\le k\le r\) and \(a\in\mathbb N^k\). If \(p\) accepts \(a\), then the formula of the packed-history construction of \(p\) holds for some natural values of its fresh variables, with every macro read as its relation.
Proof sketch. Let the accepting run have \(q\) steps, configurations \((i_t,\rho(t))\) for \(t\le q\), with \(i_q=m-1\) and \(\rho(q)=0\). Choose \(c\ge1\) with \(2^c\) larger than every input, every register value \(\rho_j(t)\), and with \(4\cdot2^c>m\); put \(K=2^c\), \(B=4K\), \(T=B^q\), \(F=\sum_{t\le q}B^t\), \(D=(K-1)F\), \(R_j=\sum_t\rho_j(t)B^t\), \(S_i=\sum_t[i_t=i]B^t\), \(N_j=\sum_t[\rho_j(t)>0]B^t\), and for each sub instruction \(i\) on register \(j\), \(V_i=\sum_t[i_t=i][\rho_j(t)>0]B^t\), \(W_i=S_i-V_i\). Every equation of items 1, 2 holds by construction; the masks, the bounds \(R_j<T\) and the bitAnd conditions hold by the masked-digit, nonzero-indicator and branch statements read backwards; the register and state equations hold by the transition statement read backwards, since consecutive configurations obey the one-step rule, the run starts at state \(0\) with the initial file, halt has no outgoing branch and \(\rho(q)=0\).
theorem packed_complete {p : Program} {r k : ℕ} (hp : p.Valid r) (_hk1 : 1 ≤ k) (hkr : k ≤ r)
{a : Fin k → ℕ} (h : p.Accepts a) :
∃ q c K B T F D : ℕ, ∃ R N S V W : ℕ → ℕ,
PackedConditions p r k a q c K B T F D R N S V W
abbrev Program : Type := List Instr
ZInQKernel/Compiler/Circuits/RegisterMachine.lean:60
structure PackedConditions (p : Program) (r k : ℕ) (a : Fin k → ℕ) (q c K B T F D : ℕ)
(R N S V W : ℕ → ℕ) : Prop where
c_pos : 0 < c
hK : K = 2 ^ c
hB : B = 4 * K
m_lt : p.length < B
hT : T = B ^ q
inputs_lt : ∀ i : Fin k, a i < K
hF : B * F + 1 = F + B * T
hD : D + F = K * F
sumS : ∑ i ∈ range p.length, S i = F
lastS : S (p.length - 1) = T
⋯
ZInQKernel/Compiler/Circuits/Packed/Sound.lean:23
Uses 7 · Used by 1
TheoremThe compiled circuit of a register machine#✓
Let \(p\) be a primitive program valid for \(r\) registers and \(1\le k\le r\). For every \(a\in\mathbb N^k\), $\(Z_{\mathcal C_{p,k}}(a)\iff p\text{ accepts }a,\)$ where \(\mathcal C_{p,k}\) is the compiled circuit of the packed-history construction. In particular the compiled polynomial has a natural zero with inputs \(a\) exactly when \(p\) reaches its final halt from the initial file with all registers zero. This holds for every run length, every size of the register values and every number of inputs.
Proof sketch. All macro arguments of the construction are polynomials in previously allocated variables, so the hierarchical-construction statement applies: \(Z_{\mathcal C}(a)\) is equivalent to the existence of natural values of the fresh variables satisfying the conjunction with every macro replaced by its relation. Soundness and completeness of the packed-history construction give the two implications.
theorem compiled_natRel_iff (p : Program) (r k : ℕ) (hp : p.Valid r) (hk1 : 1 ≤ k)
(hkr : k ≤ r) (a : Fin k → ℕ) :
(compiled p r k).NatRel powerTemplate a ↔ p.Accepts a
abbrev Program : Type := List Instr
ZInQKernel/Compiler/Circuits/RegisterMachine.lean:60
def compiled (p : Program) (r k : ℕ) : NatCircuit := (construction p r k).toCircuit k
ZInQKernel/Compiler/Circuits/Packed/Construction.lean:184
def powerTemplate : ExPoly := ⟨3, 79, powerTemplatePoly⟩
ZInQKernel/Compiler/Circuits/PowerTemplate.lean:40
Uses 7 · Used by 1
The numbers of explicit variables and power calls are linear in \(m\), \(r\), the number of sub instructions and the number of inputs.
TheoremVariable and gadget counts of the packed construction#✓
Let \(p\) be valid for \(r\) registers with \(m\) instructions, of which \(s\) are sub instructions, and \(1\le k\le r\). The packed-history construction of \(p\) creates
- \(m+r\) mask macros, \(r+s\) bitAnd macros, \(3(r+s)\) orthogonal macros, \(m+4r+3s\) binomial macros, \(m+4r+3s\) odd gadgets and \(2+k+r+2(m+4r+3s)\) strict-inequality gadgets (\(2+k+r\) outside the binomials and two in each binomial);
- \(2+3(m+4r+3s)=2+3m+12r+9s\) power calls, each with a private block of \(79\) variables;
- \(2k+9+41r+10m+31s\) explicit variables, the \(k\) inputs included.
Hence the compiled circuit \(\mathcal C_{p,k}\) has variable count $\(V=(2k+9+41r+10m+31s)+79\,(2+3m+12r+9s).\)$ These counts are independent of the order of the instructions and of the jump targets. The numbers of addition and multiplication nodes depend on hash-consing and on the targets, and are not given in closed form here.
Proof sketch. Count the allocations of each step of the construction: seven fresh variables \(q,\dots,D\), \(2r\) for \(R_j,N_j\), \(m\) for \(S_i\), \(2s\) for \(V_i,W_i\); each lt, divides and odd gadget takes one variable and each congruent two (only lt and odd occur outside the power template); a binomial takes \(5\) fresh variables, three power calls and two lt gadgets; a mask or orthogonal macro takes one fresh variable, one binomial and one odd gadget; a bitAnd takes two fresh variables and three orthogonals. Summing: \(k+7+2r+m+2s+(2+k+r)+2(m+4r+3s)+5(m+4r+3s)+(m+r)+3(r+s)+(m+4r+3s)+2(r+s) =2k+9+41r+10m+31s\), and the power calls are the two of item 1 plus three per binomial.
theorem packed_counts (p : Program) (r k : ℕ) :
let m := p.length
let s := p.subCount
let L := (construction p r k (State.init k)).2.log
L.count .mask = m + r ∧ L.count .bitAnd = r + s ∧ L.count .orthogonal = 3 * (r + s) ∧
L.count .binomial = m + 4 * r + 3 * s ∧ L.count .odd = m + 4 * r + 3 * s ∧
L.count .lt = 2 + k + r + 2 * (m + 4 * r + 3 * s) ∧
L.count .power = 2 + 3 * (m + 4 * r + 3 * s) ∧
(compiled p r k).blocks.length = 2 + 3 * m + 12 * r + 9 * s ∧
(compiled p r k).explicitVars.length = 2 * k + 9 + 41 * r + 10 * m + 31 * s ∧
(compiled p r k).varCount =
(2 * k + 9 + 41 * r + 10 * m + 31 * s) + 79 * (2 + 3 * m + 12 * r + 9 * s)
abbrev Program : Type := List Instr
ZInQKernel/Compiler/Circuits/RegisterMachine.lean:60
def construction (p : Program) (r k : ℕ) : NM NForm := do
let a := inputs k
let zero ← constant 0
let one ← constant 1
let q ← fresh
let c ← fresh
let K ← fresh
let B ← fresh
let T ← fresh
let F ← fresh
let D ← fresh
let R ← freshN r
⋯
ZInQKernel/Compiler/Circuits/Packed/Construction.lean:165
def State.init (k : ℕ) : State :=
⟨k, (List.range k).map fun i => (i, .input), (List.range k).map .var, [], []⟩
ZInQKernel/Compiler/Circuits/Circuit.lean:100
def compiled (p : Program) (r k : ℕ) : NatCircuit := (construction p r k).toCircuit k
ZInQKernel/Compiler/Circuits/Packed/Construction.lean:184
Uses 1 · Used by 1
Loading dependency explorer…
The dependency explorer needs JavaScript and its graph data.