(hnet : ∀ tr, IsGivenNetwork (gridProcs g tr) O (c, y) (levelNetP g tr lam y Rm) (g ^ 3 * lam) (g * B + g ^ 2 * lam + 1) (a * (B + lam) ^ a) S) :
∃τ : ℕ, (Stepped.ite (useRowP g fun c : ListUnit×ListUnit=> c.1.length) (networkOnSteps g NM ((gridProcs g false).close O)) (networkOnSteps g NM ((gridProcs g true).close O))).Runs (c, y) τ
(size (c, y) + C₁ * (g ^ 3 * lam + (g * B + g ^ 2 * lam + 1)) ^ C₁ + S + 4 + 1) (Eight.Zι (gridR0 g g lam Rm) (Level.patOf g lam y)) ∧
/-- The paragraph «Cost» for the step machine that `levelMachine` becomes when the tables are built
in: value, number of steps, sizes. c holds λ and B in unary. The text is that of
`Printed.level_runs_of`, with `stGridGiven` in the place of `Printed.grid_isNetwork_of`. -/
theoremlevel_runs {K g C₁ : ℕ} {e : ℝ} {NM : NetMachine} (hK : 2≤K) (hg : K≤ g) (h0 : 0≤ e) (hNM : ComputesNetworksNMC₁ g K e) :
∃Cc' : ℕ, ∀ (c : ListUnit×ListUnit) (lam B : ℕ) (W : ℕ→ListBool→ℤ), c.1.length = lam → c.2.length = B→1≤ lam → (∀ w < g * g, ∀ z ∈ words ((g * g - 1) * lam), |W w z| < 2 ^ B) →
∃T : ℕ, (Stepped.bind (Stepped.sumRange (numPat8 g fun c : ListUnit×ListUnit=> c.1.length)
(Stepped.ite (useRowP g fun c : ListUnit×ListUnit=> c.1.length) (networkOnSteps g NM ((gridProcs g false).close (tables (g * g) lam W))) (networkOnSteps g NM ((gridProcs g true).close (tables (g * g) lam W)))))
(Stepped.ofFun fun p : (ListUnit×ListUnit) ×ℤ=> p.2 / ((2 ^ (tOf p.1.1.length * nRect g) : ℕ) : ℤ))).Runs c T (Cc' * (B + lam) ^ Cc') (Inst.ZW lam [] (listTables (g * g) W)) ∧
havekey : ∀y : ℕ, ∃τ : ℕ, y < (tOf n + 1) ^ nRect g →
(Stepped.ite (useRowP g fun c : ListUnit×ListUnit=> c.1.length) (networkOnSteps g NM ((gridProcs g false).close O)) (networkOnSteps g NM ((gridProcs g true).close O))).Runs (c, y) τ
(size c + (M + 1) + C₁ * (g ^ 3 * n + (g * B + g ^ 2 * n + 1)) ^ C₁ + S' + 5) (Zt y) ∧ (τ : ℝ) + 1≤ bd :=by
obtain ⟨v, hv⟩ : ∃ v, v = g * (g ^ 3 * n + (g * B + g ^ 2 * tOf n)) + g * g * (g * B + g ^ 2 * tOf n) := ⟨_, rfl⟩
havehN : numPat8 g (func : ListUnit×ListUnit=> c.1.length) c = (tOf n + 1) ^ nRect g :=by
show (tOf c.1.length + 1) ^ nRect g = _
rw [hcl]
havehabs : ∀ y < (tOf n + 1) ^ nRect g, (Zt y).natAbs ≤2 ^ v :=fun y hy =>by
haveh0 : Network.value (J:=Fin g) (netOfι g false n y Rm) = Zt y :=by rw [hZt]; exact value_netOfι hl g false y Rm
have h1 :=Groundwork.Prog.abs_value_le (J:=Fin g) (netOfι g false n y Rm) (netOfι_sized_t hl g false y hBd)
rw [h0, Int.abs_eq_natAbs, Fintype.card_fin, ← hv] at h1
exact_mod_cast h1
have hrun :=Stepped.sumRange_runs (N:=numPat8 g fun c : ListUnit×ListUnit=> c.1.length) (a := c)
(M:=Stepped.ite (useRowP g fun c : ListUnit×ListUnit=> c.1.length) (networkOnSteps g NM ((gridProcs g false).close O)) (networkOnSteps g NM ((gridProcs g true).close O)))
τ Zt (2 ^ v) (size c + (M + 1) + C₁ * (g ^ 3 * n + (g * B + g ^ 2 * n + 1)) ^ C₁ + S' + 5)
(fun y hy => (hτ y (hN ▸ hy)).1) (fun y hy => habs y (hN ▸ hy))
rw [hN] at hrun
-- the division
havehZ : ((2 : ℤ) ^ tOf n) ^ nRect g * Inst.ZW n [] Rm = ∑ y ∈ range ((tOf n + 1) ^ nRect g), Zt y :=by
rw [hZt, ← pow_mul]
exact eqExpansion_words hl Rm
havehdiv : (∑ y ∈ range ((tOf n + 1) ^ nRect g), Zt y) / ((2 ^ (tOf c.1.length * nRect g) : ℕ) : ℤ) = Inst.ZW n [] Rm:=by
rw [← hZ, hcl, ← pow_mul]
push_cast
exact Int.mul_ediv_cancel_left _ (by positivity)
have hfin :=Stepped.ofFun_runs (funp : (ListUnit×ListUnit) ×ℤ=> p.2 / ((2 ^ (tOf p.1.1.length * nRect g) : ℕ) : ℤ)) (c, ∑ y ∈ range ((tOf n + 1) ^ nRect g), Zt y)
simp only [hdiv] at hfin
have hall :=Stepped.bind_runs hrun hfin
refine ⟨_, hall.mono le_rfl ?_, ?_⟩
· -- sizes
havehsc : size c = 2 * n + 1 + (2 * B + 1) :=by rw [size_prod, size_units, size_units, hcl, hcB]
have h2 := hA2 (size c) B n M hl hg1
(by
rw [hM]
calc (tOf n + 1) * nRect g ≤2 * n * nRect g :=Nat.mul_le_mul_right _ (by omega)
/-- The machine of Proposition 3 has polynomial-time steps if M has. The statement is
`Printed.StLevelPoly`; the text is that of `Printed.levelLoop_poly` and `Printed.stLevelPoly_of`,
with `gridProcs_poly`, the second conjunct of `stGridGiven`, for the two networks. -/
theoremstLevelPoly : StLevelPoly:=by
intro K g M hM
have hl :=Printed.lamOf_unary
have hN := stGroupingPoly K g M hM
obtain ⟨i1⟩ := networkOn_steps g _ _ hN (gridProcs_poly g false)
obtain ⟨i2⟩ := networkOn_steps g _ _ hN (gridProcs_poly g true)
have i3 :=IsPolyQ.ite (Eight.Level.Cert.useRowP_polyT (lam :=func : ListUnit×ListUnit=> c.1.length) Eight.stKeeps g hl) (networkOn g (groupingMachine K g M) (gridProcs g false)) (networkOn g (groupingMachine K g M) (gridProcs g true))
have i4 :=IsPolyQ.sumRange (QueryMachine.ite (useRowP g fun c : ListUnit×ListUnit=> c.1.length) (networkOn g (groupingMachine K g M) (gridProcs g false)) (networkOn g (groupingMachine K g M) (gridProcs g true))) (Eight.Level.Cert.numPat8_polyT (lam :=func : ListUnit×ListUnit=> c.1.length) g hl)