This section is in the process of proving \(\mathrm {P}\ne \mathrm {NP}\) in Lean: the map of the route, the barrier record that every step of the main chain must pass (Section 82.1), and the pieces proved so far, each checked with no axioms. The definitions of P, NP and SAT are those of the computation root (found/RootComp.lean). The map (PNP_MAP.md) separates the structural side (reductions, symmetries, structure of formulas and circuits) from the exact-size side (gate counts, counting, lower bounds), records for every method whether it relativizes, naturalizes or algebrizes, and keeps the open lemmas as propositions in OPEN_LEMMAS_PNP.lean.
Each method toward \(\mathrm {P}\ne \mathrm {NP}\) with the barrier that stops it and the source of that barrier; candidate rows are not yet in the main chain, and the row for the machine laws is a barrier proved here.
| Method |
Barrier |
Grounds |
| Diagonalization |
relativization |
time hierarchy: simulation and diagonalization hold with any oracle; \(\mathrm {P}^A=\mathrm {NP}^A\) and \(\mathrm {P}^B\ne \mathrm {NP}^B\) (Baker–Gill–Solovay 1975) |
| Random restrictions |
natural proofs |
on \(\mathrm {AC}^0\) the property used (not constant after a restriction) is large and decidable in polynomial time (Håstad 1986; Razborov–Rudich 1997) |
| Approximation method |
natural proofs, for general circuits |
no large lower bound for general circuits by approximation (Razborov 1989); the extended property is large and easy |
| Arithmetization |
algebrization |
\(\mathrm {IP}=\mathrm {PSPACE}\) and \(\mathrm {MIP}=\mathrm {NEXP}\) hold relative to a low-degree extension oracle \(\tilde A\) (Aaronson–Wigderson 2009) |
| Polynomial method |
natural proofs |
not approximable by low degree is a large and easy property (Razborov 1987; Smolensky 1987; Razborov–Rudich 1997) |
| Crossing sequences |
avoids relativization; not a circuit bound |
one-tape bounds up to \(n^2\) only, no extension to many tapes or circuits; cut and paste proved in found/Pnp16 |
| Machine laws only |
relativization, abstract form (proved here) |
a model of all functions satisfies the laws of an output machine model and has \(\mathrm {P}=\mathrm {NP}\): found/Pnp15 |
| Hardness magnification |
locality barrier |
known magnification arguments hold for small fan-in oracle circuits (Chen–Hirahara–Oliveira–Pich–Rajgopal–Santhanam 2020) |
| Algorithmic method |
candidate; avoids all three, target class differs |
fast circuit SAT gives \(\mathrm {NEXP}\not \subseteq \mathrm {ACC}^0\) (Williams 2011); relativization and algebrization are avoided since an algebrizing oracle with \(\mathrm {NEXP}^A\subseteq \mathrm {ACC}^A\) exists (Aaronson 2011, from Aaronson–Wigderson 2009); the conclusion is about \(\mathrm {NEXP}\), with no known way down to \(\mathrm {NP}\) |
| Geometric complexity |
candidate; claimed to avoid all three |
occurrence obstructions do not exist (Bürgisser–Ikenmeyer–Panova 2016); multiplicity obstructions open; the target is \(\mathrm {VP}\) vs \(\mathrm {VNP}\) (Mulmuley–Sohoni 2001) |
| Proof complexity |
candidate; per proof system; interpolation barrier for strong systems |
superpolynomial bounds for all proof systems \(\Leftrightarrow \mathrm {NP}\ne \mathrm {coNP}\) (Cook–Reckhow 1979); bounds exist for resolution (Haken’s bound proved here: found/Pnp101) and cutting planes (tree-like superpolynomial bound proved here: found/Pnp131), Frege open; feasible interpolation, the main method, fails for Extended Frege and Frege if RSA or factoring is hard (Krajíček–Pudlák 1998; Bonet–Pitassi–Raz 2000) |
Every function of \(n\) bits has a formula with exactly \(3(2^n-1)\) binary gates, and a formula not mentioning variable \(i\) does not depend on it.
found/Pnp1.lean
/-
T330 piece 1. found/Pnp1 -- Boolean formulas: the size side of the P vs NP map.
Tool results only (not used for the main conclusion; barrier status in PNP_MAP.md):
* formulas over variables, constants, not, and, or; evaluation; gate count;
* Shannon expansion: every function of n bits has a formula with exactly
tw n = 3 (2^n - 1) binary gates (upper bound, constructive);
* insensitivity: a formula that does not mention variable i does not depend on it
(the base step of gate elimination);
* hence a formula for a function that depends on variable i mentions i.
Written with ASCII forms only. CIC dependence: none.
-/
namespace Found.Pnp1
inductive Fm where
| var : Nat -> Fm
| cst : Bool -> Fm
| neg : Fm -> Fm
| and : Fm -> Fm -> Fm
| or : Fm -> Fm -> Fm
def ev (a : Nat -> Bool) : Fm -> Bool
| .var i => a i
| .cst b => b
| .neg f => !(ev a f)
| .and f g => ev a f && ev a g
| .or f g => ev a f || ev a g
/-- Number of binary gates. -/
def gates : Fm -> Nat
| .var _ => 0
| .cst _ => 0
| .neg f => gates f
| .and f g => gates f + gates g + 1
| .or f g => gates f + gates g + 1
/-- Whether the formula mentions variable i. -/
def mentions (i : Nat) : Fm -> Bool
| .var j => Nat.beq i j
| .cst _ => false
| .neg f => mentions i f
| .and f g => mentions i f || mentions i g
| .or f g => mentions i f || mentions i g
/-! ## Shannon expansion -/
/-- The values of the variables k, k+1, ..., k+n-1. -/
def read (a : Nat -> Bool) : Nat -> Nat -> List Bool
| _, 0 => []
| k, n + 1 => a k :: read a (k + 1) n
/-- The Shannon formula of f (a function of the next n bits), starting at variable k. -/
def build : Nat -> (List Bool -> Bool) -> Nat -> Fm
| 0, f, _ => .cst (f [])
| n + 1, f, k =>
.or (.and (.var k) (build n (fun xs => f (true :: xs)) (k + 1)))
(.and (.neg (.var k)) (build n (fun xs => f (false :: xs)) (k + 1)))
theorem or_false' (x : Bool) : (x || false) = x := by cases x <;> rfl
/-- The Shannon formula computes f. -/
theorem ev_build (a : Nat -> Bool) : forall (n : Nat) (f : List Bool -> Bool) (k : Nat),
ev a (build n f k) = f (read a k n) := by
intro n
induction n with
| zero => intro f k; rfl
| succ n ih =>
intro f k
show ((a k && ev a (build n (fun xs => f (true :: xs)) (k + 1))) ||
(!(a k) && ev a (build n (fun xs => f (false :: xs)) (k + 1))))
= f (a k :: read a (k + 1) n)
rw [ih, ih]
cases a k with
| true => exact or_false' _
| false => rfl
/-- tw n = 3 (2^n - 1). -/
def tw : Nat -> Nat
| 0 => 0
| n + 1 => tw n + tw n + 3
def pow2 : Nat -> Nat
| 0 => 1
| n + 1 => pow2 n + pow2 n
theorem gate_step (t : Nat) : (0 + t + 1) + (0 + t + 1) + 1 = t + t + 3 := by
rw [Nat.zero_add]
exact congrArg (fun x => Nat.succ (Nat.succ x)) (Nat.succ_add t t)
theorem gates_build : forall (n : Nat) (f : List Bool -> Bool) (k : Nat),
gates (build n f k) = tw n := by
intro n
induction n with
| zero => intro f k; rfl
| succ n ih =>
intro f k
show (0 + gates (build n (fun xs => f (true :: xs)) (k + 1)) + 1) +
(0 + gates (build n (fun xs => f (false :: xs)) (k + 1)) + 1) + 1 = tw n + tw n + 3
rw [ih, ih]
exact gate_step (tw n)
theorem add3 (x : Nat) : (x + x + 3) + 3 = (x + 3) + (x + 3) := by
rw [Nat.add_assoc x x 3, Nat.add_assoc x (x + 3) 3, Nat.add_assoc x 3 (x + 3),
Nat.add_comm 3 (x + 3), Nat.add_assoc x 3 3]
/-- The gate count of the Shannon formula: tw n + 3 = 3 * 2^n. -/
theorem tw_closed : forall n : Nat, tw n + 3 = 3 * pow2 n := by
intro n
induction n with
| zero => rfl
| succ n ih =>
show (tw n + tw n + 3) + 3 = 3 * (pow2 n + pow2 n)
rw [add3, ih, Nat.left_distrib]
/-- Every function of n bits has a formula with tw n gates. -/
theorem shannon_upper (n : Nat) (f : List Bool -> Bool) :
Exists fun F : Fm => gates F = tw n /\ forall a, ev a F = f (read a 0 n) :=
Exists.intro (build n f 0) (And.intro (gates_build n f 0) (fun a => ev_build a n f 0))
/-! ## Insensitivity -/
theorem or_false_l {x y : Bool} (h : (x || y) = false) : x = false := by
cases x with
| false => rfl
| true => exact Bool.noConfusion h
theorem or_false_r {x y : Bool} (h : (x || y) = false) : y = false := by
cases x with
| false => exact h
| true => exact Bool.noConfusion h
/-- A formula that does not mention i gives the same value on assignments that differ
only at i. -/
theorem insensitive (i : Nat) (a b : Nat -> Bool)
(hab : forall j, Nat.beq i j = false -> a j = b j) :
forall F : Fm, mentions i F = false -> ev a F = ev b F := by
intro F
induction F with
| var j => intro h; exact hab j h
| cst c => intro _; rfl
| neg f ih => intro h; exact congrArg (fun x => !x) (ih h)
| and f g ihf ihg =>
intro h
show (ev a f && ev a g) = (ev b f && ev b g)
rw [ihf (or_false_l h), ihg (or_false_r h)]
| or f g ihf ihg =>
intro h
show (ev a f || ev a g) = (ev b f || ev b g)
rw [ihf (or_false_l h), ihg (or_false_r h)]
/-- g depends on variable i. -/
def Depends (i : Nat) (g : (Nat -> Bool) -> Bool) : Prop :=
Exists fun a : Nat -> Bool => Exists fun b : Nat -> Bool =>
(forall j, Nat.beq i j = false -> a j = b j) /\ (g a = g b -> False)
/-- A formula computing a function that depends on i mentions i. -/
theorem mentions_of_depends (i : Nat) (g : (Nat -> Bool) -> Bool) (F : Fm)
(hF : forall a, ev a F = g a) (hd : Depends i g) : mentions i F = true := by
cases hm : mentions i F with
| true => rfl
| false =>
exact Exists.elim hd fun a h1 => Exists.elim h1 fun b h2 =>
False.elim (h2.2 (((hF a).symm.trans (insensitive i a b h2.1 F hm)).trans (hF b)))
/-- Example: x0 xor x1 depends on variable 0. -/
def xor01 (a : Nat -> Bool) : Bool := xor (a 0) (a 1)
theorem xor01_depends : Depends 0 xor01 :=
Exists.intro (fun _ => false) (Exists.intro (fun j => Nat.beq 0 j)
(And.intro (fun _ h => h.symm) (fun h => Bool.noConfusion h)))
end Found.Pnp1
#print axioms Found.Pnp1.or_false'
#print axioms Found.Pnp1.ev_build
#print axioms Found.Pnp1.gate_step
#print axioms Found.Pnp1.gates_build
#print axioms Found.Pnp1.add3
#print axioms Found.Pnp1.tw_closed
#print axioms Found.Pnp1.shannon_upper
#print axioms Found.Pnp1.or_false_l
#print axioms Found.Pnp1.or_false_r
#print axioms Found.Pnp1.insensitive
#print axioms Found.Pnp1.mentions_of_depends
#print axioms Found.Pnp1.xor01_depends
#print axioms: 12 theorems, no axioms.
A formula for a function that depends on every variable \(i<n\) has at least \(n-1\) binary gates (pigeonhole on its variable list, and leaves \(=\) gates \(+1\)).
found/Pnp2.lean
/-
T330 piece 2. found/Pnp2 -- leaves of a formula versus the variables it depends on.
Tool result (exact-size side; not used for the main conclusion):
* the variable list of a formula; mentions i F = memb i (vars F);
* pigeonhole: a list that contains every i < n has length at least n
(by erasing one occurrence of n, structural recursion only);
* leaves = gates + 1, and the variable list is not longer than the leaves;
* hence a formula for a function that depends on every variable i < n has at least
n - 1 binary gates: n <= gates F + 1.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp1
namespace Found.Pnp2
open Found.Pnp1
/-! ## Nat.beq -/
theorem beq_eq : forall a b : Nat, Nat.beq a b = true -> a = b := by
intro a
induction a with
| zero => intro b h; cases b with
| zero => rfl
| succ b => exact Bool.noConfusion h
| succ a ih => intro b h; cases b with
| zero => exact Bool.noConfusion h
| succ b => exact congrArg Nat.succ (ih b h)
theorem beq_false_of_ne (a b : Nat) (hne : a = b -> False) : Nat.beq a b = false := by
cases h : Nat.beq a b with
| false => rfl
| true => exact False.elim (hne (beq_eq a b h))
/-! ## Membership and erasure -/
def memb (i : Nat) : List Nat -> Bool
| [] => false
| j :: l => Nat.beq i j || memb i l
def pick : Bool -> List Nat -> List Nat -> List Nat
| true, a, _ => a
| false, _, b => b
def erase (n : Nat) : List Nat -> List Nat
| [] => []
| j :: l => pick (Nat.beq n j) l (j :: erase n l)
theorem erase_length (n : Nat) : forall L : List Nat, memb n L = true ->
(erase n L).length + 1 = L.length := by
intro L
induction L with
| nil => intro h; exact Bool.noConfusion h
| cons j l ih =>
intro h
have h' : (Nat.beq n j || memb n l) = true := h
show (pick (Nat.beq n j) l (j :: erase n l)).length + 1 = l.length + 1
cases hb : Nat.beq n j with
| true => rfl
| false =>
rw [hb] at h'
exact congrArg (fun t => t + 1) (ih h')
theorem memb_erase (n i : Nat) (hni : Nat.beq n i = false) :
forall L : List Nat, memb i L = true -> memb i (erase n L) = true := by
intro L
induction L with
| nil => intro h; exact h
| cons j l ih =>
intro h
have h' : (Nat.beq i j || memb i l) = true := h
show memb i (pick (Nat.beq n j) l (j :: erase n l)) = true
cases hb : Nat.beq n j with
| true =>
cases hij : Nat.beq i j with
| true =>
have e1 : i = j := beq_eq i j hij
have e3 : Nat.beq n i = true := by rw [e1]; exact hb
exact Bool.noConfusion (hni.symm.trans e3)
| false =>
rw [hij] at h'
exact h'
| false =>
show (Nat.beq i j || memb i (erase n l)) = true
cases hij : Nat.beq i j with
| true => rfl
| false =>
rw [hij] at h'
exact ih h'
/-- Pigeonhole: a list containing every i < n has length at least n. -/
theorem length_ge : forall (n : Nat) (L : List Nat),
(forall i, i < n -> memb i L = true) -> n <= L.length := by
intro n
induction n with
| zero => intro L _; exact Nat.zero_le _
| succ n ih =>
intro L h
have hn : memb n L = true := h n (Nat.lt_succ_self n)
have h2 : forall i, i < n -> memb i (erase n L) = true := fun i hi =>
memb_erase n i (beq_false_of_ne n i (fun e => Nat.ne_of_lt hi e.symm)) L
(h i (Nat.lt_succ_of_lt hi))
have h3 : n + 1 <= (erase n L).length + 1 := Nat.succ_le_succ (ih (erase n L) h2)
rw [erase_length n L hn] at h3
exact h3
/-! ## Variables and leaves of a formula -/
def vars : Fm -> List Nat
| .var i => [i]
| .cst _ => []
| .neg f => vars f
| .and f g => vars f ++ vars g
| .or f g => vars f ++ vars g
def leaves : Fm -> Nat
| .var _ => 1
| .cst _ => 1
| .neg f => leaves f
| .and f g => leaves f + leaves g
| .or f g => leaves f + leaves g
theorem or_assoc' (x y z : Bool) : ((x || y) || z) = (x || (y || z)) := by
cases x <;> rfl
theorem memb_append (i : Nat) (l2 : List Nat) : forall l1 : List Nat,
memb i (l1 ++ l2) = (memb i l1 || memb i l2) := by
intro l1
induction l1 with
| nil => rfl
| cons j l ih =>
show (Nat.beq i j || memb i (l ++ l2)) = ((Nat.beq i j || memb i l) || memb i l2)
rw [ih, or_assoc']
theorem or_false_r' (x : Bool) : (x || false) = x := by cases x <;> rfl
theorem mentions_memb (i : Nat) : forall F : Fm, mentions i F = memb i (vars F) := by
intro F
induction F with
| var j => exact (or_false_r' (Nat.beq i j)).symm
| cst _ => rfl
| neg f ih => exact ih
| and f g ihf ihg =>
show (mentions i f || mentions i g) = memb i (vars f ++ vars g)
rw [memb_append, ihf, ihg]
| or f g ihf ihg =>
show (mentions i f || mentions i g) = memb i (vars f ++ vars g)
rw [memb_append, ihf, ihg]
theorem length_append' (l2 : List Nat) : forall l1 : List Nat,
(l1 ++ l2).length = l1.length + l2.length := by
intro l1
induction l1 with
| nil => exact (Nat.zero_add _).symm
| cons j l ih =>
show (l ++ l2).length + 1 = (l.length + 1) + l2.length
rw [ih]
exact (Nat.succ_add _ _).symm
theorem vars_le_leaves : forall F : Fm, (vars F).length <= leaves F := by
intro F
induction F with
| var _ => exact Nat.le_refl 1
| cst _ => exact Nat.zero_le 1
| neg f ih => exact ih
| and f g ihf ihg =>
show (vars f ++ vars g).length <= leaves f + leaves g
rw [length_append']
exact Nat.add_le_add ihf ihg
| or f g ihf ihg =>
show (vars f ++ vars g).length <= leaves f + leaves g
rw [length_append']
exact Nat.add_le_add ihf ihg
theorem leaf_step (x y : Nat) : (x + 1) + (y + 1) = (x + y + 1) + 1 :=
congrArg Nat.succ (Nat.succ_add x y)
theorem leaves_gates : forall F : Fm, leaves F = gates F + 1 := by
intro F
induction F with
| var _ => rfl
| cst _ => rfl
| neg f ih => exact ih
| and f g ihf ihg =>
show leaves f + leaves g = (gates f + gates g + 1) + 1
rw [ihf, ihg]
exact leaf_step _ _
| or f g ihf ihg =>
show leaves f + leaves g = (gates f + gates g + 1) + 1
rw [ihf, ihg]
exact leaf_step _ _
/-- A formula for a function that depends on every variable i < n has at least n - 1
binary gates. -/
theorem gates_lower (n : Nat) (g : (Nat -> Bool) -> Bool) (F : Fm)
(hF : forall a, ev a F = g a) (hd : forall i, i < n -> Depends i g) :
n <= gates F + 1 := by
have h1 : forall i, i < n -> memb i (vars F) = true := fun i hi =>
(mentions_memb i F).symm.trans (mentions_of_depends i g F hF (hd i hi))
have h2 : n <= (vars F).length := length_ge n (vars F) h1
have h3 : n <= leaves F := Nat.le_trans h2 (vars_le_leaves F)
rw [leaves_gates] at h3
exact h3
end Found.Pnp2
#print axioms Found.Pnp2.beq_eq
#print axioms Found.Pnp2.beq_false_of_ne
#print axioms Found.Pnp2.erase_length
#print axioms Found.Pnp2.memb_erase
#print axioms Found.Pnp2.length_ge
#print axioms Found.Pnp2.or_assoc'
#print axioms Found.Pnp2.memb_append
#print axioms Found.Pnp2.or_false_r'
#print axioms Found.Pnp2.mentions_memb
#print axioms Found.Pnp2.length_append'
#print axioms Found.Pnp2.vars_le_leaves
#print axioms Found.Pnp2.leaf_step
#print axioms Found.Pnp2.leaves_gates
#print axioms Found.Pnp2.gates_lower
#print axioms: 14 theorems, no axioms.
Polynomial bounds as expression trees compose by substitution; over any machine model with output, budget monotonicity and sequential composition, reductions are transitive and P is closed under them.
found/Pnp3.lean
/-
T330 piece 3. found/Pnp3 -- polynomial-time reductions (structure side).
Tool results (they relativize, so they are tools only; PNP_MAP.md):
* polynomial expressions as trees; evaluation is monotone and closed under
substitution (so polynomial bounds compose without exponent arithmetic);
* a machine model with output (the tool missing in piece 1): runs with a step budget,
monotone in the budget, output no longer than budget plus input, and sequential
composition adding budgets -- any model of the Church-Turing line is one value;
* composition of polynomial-time maps is polynomial-time;
* reductions are transitive, and P is closed under reductions.
Written with ASCII forms only. CIC dependence: none.
-/
namespace Found.Pnp3
/-! ## Polynomial expressions -/
inductive PE where
| x : PE
| c : Nat -> PE
| add : PE -> PE -> PE
| mul : PE -> PE -> PE
def pe : PE -> Nat -> Nat
| .x, n => n
| .c k, _ => k
| .add a b, n => pe a n + pe b n
| .mul a b, n => pe a n * pe b n
/-- Substitute f for the variable. -/
def comp : PE -> PE -> PE
| .x, f => f
| .c k, _ => .c k
| .add a b, f => .add (comp a f) (comp b f)
| .mul a b, f => .mul (comp a f) (comp b f)
theorem pe_comp (f : PE) (n : Nat) : forall e : PE, pe (comp e f) n = pe e (pe f n) := by
intro e
induction e with
| x => rfl
| c k => rfl
| add a b iha ihb =>
show pe (comp a f) n + pe (comp b f) n = pe a (pe f n) + pe b (pe f n)
rw [iha, ihb]
| mul a b iha ihb =>
show pe (comp a f) n * pe (comp b f) n = pe a (pe f n) * pe b (pe f n)
rw [iha, ihb]
theorem pe_mono (m n : Nat) (h : m <= n) : forall e : PE, pe e m <= pe e n := by
intro e
induction e with
| x => exact h
| c k => exact Nat.le_refl k
| add a b iha ihb => exact Nat.add_le_add iha ihb
| mul a b iha ihb => exact Nat.mul_le_mul iha ihb
/-! ## A machine model with output -/
structure OM where
Prog : Type
run : Prog -> Nat -> List Bool -> Option (List Bool)
seq : Prog -> Prog -> Prog
fuel_mono : forall p t t' x y, run p t x = some y -> t <= t' -> run p t' x = some y
out_le : forall p t x y, run p t x = some y -> y.length <= t + x.length
seq_run : forall p q t s x y z, run p t x = some y -> run q s y = some z ->
run (seq p q) (t + s) x = some z
variable (M : OM)
/-- Program p computes f within the polynomial e of the input length. -/
def Computes (p : M.Prog) (e : PE) (f : List Bool -> List Bool) : Prop :=
forall x, M.run p (pe e x.length) x = some (f x)
/-- The bound of a composition: e(n) + e'(e(n) + n). -/
def compBound (e e' : PE) : PE := .add e (comp e' (.add e .x))
theorem comp_computes (p q : M.Prog) (e e' : PE) (f g : List Bool -> List Bool)
(hp : Computes M p e f) (hq : Computes M q e' g) :
Computes M (M.seq p q) (compBound e e') (fun x => g (f x)) := by
intro x
have h1 : M.run p (pe e x.length) x = some (f x) := hp x
have hl : (f x).length <= pe e x.length + x.length := M.out_le p _ x (f x) h1
have h2 : M.run q (pe e' (pe e x.length + x.length)) (f x) = some (g (f x)) :=
M.fuel_mono q _ _ (f x) (g (f x)) (hq (f x)) (pe_mono _ _ hl e')
have h3 := M.seq_run p q _ _ x (f x) (g (f x)) h1 h2
show M.run (M.seq p q) (pe e x.length + pe (comp e' (.add e .x)) x.length) x = some (g (f x))
rw [pe_comp]
exact h3
/-! ## Reductions -/
/-- L reduces to L' in polynomial time. -/
def Red (L L' : List Bool -> Bool) : Prop :=
Exists fun p : M.Prog => Exists fun e : PE => Exists fun r : List Bool -> List Bool =>
Computes M p e r /\ forall x, L x = L' (r x)
/-- L is decided in polynomial time (the output is the one-bit list [L x]). -/
def InPo (L : List Bool -> Bool) : Prop :=
Exists fun p : M.Prog => Exists fun e : PE => Computes M p e (fun x => [L x])
theorem red_trans (L1 L2 L3 : List Bool -> Bool) (h12 : Red M L1 L2) (h23 : Red M L2 L3) :
Red M L1 L3 :=
Exists.elim h12 fun p hp => Exists.elim hp fun e he => Exists.elim he fun r hr =>
Exists.elim h23 fun q hq => Exists.elim hq fun e' he' => Exists.elim he' fun r' hr' =>
Exists.intro (M.seq p q) (Exists.intro (compBound e e') (Exists.intro (fun x => r' (r x))
(And.intro (comp_computes M p q e e' r r' hr.1 hr'.1)
(fun x => (hr.2 x).trans (hr'.2 (r x))))))
theorem red_P (L L' : List Bool -> Bool) (hr : Red M L L') (hP : InPo M L') : InPo M L :=
Exists.elim hr fun p hp => Exists.elim hp fun e he => Exists.elim he fun r hr' =>
Exists.elim hP fun q hq => Exists.elim hq fun e' he' =>
Exists.intro (M.seq p q) (Exists.intro (compBound e e') (fun x =>
Eq.symm (Eq.trans (congrArg (fun b => some [b]) (hr'.2 x))
(comp_computes M p q e e' r (fun y => [L' y]) hr'.1 he' x).symm)))
theorem red_refl (L : List Bool -> Bool) (p : M.Prog) (e : PE)
(hid : Computes M p e (fun x => x)) : Red M L L :=
Exists.intro p (Exists.intro e (Exists.intro (fun x => x) (And.intro hid (fun _ => rfl))))
end Found.Pnp3
#print axioms Found.Pnp3.pe_comp
#print axioms Found.Pnp3.pe_mono
#print axioms Found.Pnp3.comp_computes
#print axioms Found.Pnp3.red_trans
#print axioms Found.Pnp3.red_P
#print axioms Found.Pnp3.red_refl
#print axioms: 6 theorems, no axioms.
NP-hardness is inherited along reductions, and an NP-hard language in P makes every NP language polynomial-time.
found/Pnp4.lean
/-
T330 piece 4. found/Pnp4 -- NP-hardness over the machine model with output.
Tool results (they relativize; PNP_MAP.md):
* NP over OM: a polynomial-time verifier and polynomially long certificates;
* NP-hardness is inherited along reductions;
* an NP-hard language in P gives P = NP (every NP language is in P);
* contrapositive: if some NP language is outside P, no NP-hard language is in P.
With Cook-Levin (open lemma O3) this makes "P = NP iff SAT is in P".
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp3
namespace Found.Pnp4
open Found.Pnp3
variable (M : OM)
/-- Pairing of an input and a certificate: each input bit b is written as (false, b), then
the separator (true, true), then the certificate. -/
def dbl : List Bool -> List Bool
| [] => []
| b :: l => false :: b :: dbl l
def enc (x w : List Bool) : List Bool := dbl x ++ (true :: true :: w)
/-- L is in NP over M: a polynomial-time verifier V on encoded pairs, and L x holds
exactly when some certificate of polynomial length is accepted. -/
def InNPo (L : List Bool -> Bool) : Prop :=
Exists fun V : List Bool -> Bool => Exists fun b : PE =>
InPo M V /\
forall x, L x = true <-> Exists fun w : List Bool => w.length <= pe b x.length /\ V (enc x w) = true
/-- L is NP-hard: every NP language reduces to it. -/
def Hard (L : List Bool -> Bool) : Prop :=
forall L' : List Bool -> Bool, InNPo M L' -> Red M L' L
theorem hard_trans (L L2 : List Bool -> Bool) (h : Hard M L) (r : Red M L L2) : Hard M L2 :=
fun L' hL' => red_trans M L' L L2 (h L' hL') r
/-- An NP-hard language in P gives P = NP. -/
theorem hard_in_P (L : List Bool -> Bool) (h : Hard M L) (hP : InPo M L) :
forall L' : List Bool -> Bool, InNPo M L' -> InPo M L' :=
fun L' hL' => red_P M L' L (h L' hL') hP
/-- If some NP language is outside P, no NP-hard language is in P. -/
theorem sep_hard (L : List Bool -> Bool) (h : Hard M L)
(sep : Exists fun L' : List Bool -> Bool => InNPo M L' /\ (InPo M L' -> False)) :
InPo M L -> False :=
fun hP => Exists.elim sep fun L' hL' => hL'.2 (hard_in_P M L h hP L' hL'.1)
/-- Two NP-hard languages in NP reduce to each other; P-membership of one is that of the
other. -/
theorem hard_equiv (L1 L2 : List Bool -> Bool) (h1 : Hard M L1) (h2 : Hard M L2)
(n1 : InNPo M L1) (n2 : InNPo M L2) : (InPo M L1 -> InPo M L2) /\ (InPo M L2 -> InPo M L1) :=
And.intro (fun hP => red_P M L2 L1 (h1 L2 n2) hP) (fun hP => red_P M L1 L2 (h2 L1 n1) hP)
end Found.Pnp4
#print axioms Found.Pnp4.hard_trans
#print axioms Found.Pnp4.hard_in_P
#print axioms Found.Pnp4.sep_hard
#print axioms Found.Pnp4.hard_equiv
#print axioms: 4 theorems, no axioms.
A formula is satisfiable exactly when its Tseitin CNF is; gate variables are the subformulas themselves, with at most three clauses per node.
found/Pnp5.lean
/-
T330 piece 5. found/Pnp5 -- Tseitin translation: formula satisfiability reduces to CNF
satisfiability (the circuit-to-CNF step of Cook-Levin; structure side, a tool).
Variables are inputs (inl i) or subformulas (inr G): the gate variable of G is G itself,
so equal subformulas share one variable and no freshness argument is needed. Every node
gives at most three clauses of at most three literals, so the CNF is linear in the
formula. Proved:
* soundness: an assignment satisfying F extends (gate G := its value) to one
satisfying the CNF;
* completeness: any assignment satisfying the gate clauses gives each gate variable
the value of its subformula on the inputs;
* F is satisfiable iff its Tseitin CNF is.
It relativizes (a tool only; PNP_MAP.md). Written with ASCII forms only.
CIC dependence: none.
-/
import found.Pnp1
namespace Found.Pnp5
open Found.Pnp1
abbrev V := Sum Nat Fm
abbrev Lit := Bool × V
abbrev Clause := List Lit
abbrev CNF := List Clause
def evalLit (b : V -> Bool) (l : Lit) : Bool :=
match l.1 with
| true => b l.2
| false => !(b l.2)
def evalC (b : V -> Bool) : Clause -> Bool
| [] => false
| l :: c => evalLit b l || evalC b c
def evalF (b : V -> Bool) : CNF -> Bool
| [] => true
| c :: f => evalC b c && evalF b f
/-- The clauses of one node: the gate variable equals the operation on the children. -/
def loc : Fm -> CNF
| .var i => [[(false, .inr (.var i)), (true, .inl i)], [(true, .inr (.var i)), (false, .inl i)]]
| .cst true => [[(true, .inr (.cst true))]]
| .cst false => [[(false, .inr (.cst false))]]
| .neg f => [[(false, .inr (.neg f)), (false, .inr f)], [(true, .inr (.neg f)), (true, .inr f)]]
| .and f h => [[(false, .inr (.and f h)), (true, .inr f)], [(false, .inr (.and f h)), (true, .inr h)],
[(true, .inr (.and f h)), (false, .inr f), (false, .inr h)]]
| .or f h => [[(true, .inr (.or f h)), (false, .inr f)], [(true, .inr (.or f h)), (false, .inr h)],
[(false, .inr (.or f h)), (true, .inr f), (true, .inr h)]]
/-- The Tseitin clauses of all nodes. -/
def tse : Fm -> CNF
| .var i => loc (.var i)
| .cst c => loc (.cst c)
| .neg f => loc (.neg f) ++ tse f
| .and f h => loc (.and f h) ++ (tse f ++ tse h)
| .or f h => loc (.or f h) ++ (tse f ++ tse h)
/-- The Tseitin CNF of F: the output gate is true, and all gate clauses hold. -/
def full (F : Fm) : CNF := [[(true, .inr F)]] ++ tse F
/-! ## Bool facts -/
theorem and_assoc' (x y z : Bool) : ((x && y) && z) = (x && (y && z)) := by
cases x <;> rfl
theorem and_l {x y : Bool} (h : (x && y) = true) : x = true := by
cases x with
| true => rfl
| false => exact Bool.noConfusion h
theorem and_r {x y : Bool} (h : (x && y) = true) : y = true := by
cases x with
| true => exact h
| false => exact Bool.noConfusion h
theorem and_tt {x y : Bool} (hx : x = true) (hy : y = true) : (x && y) = true := by
subst hx; subst hy; rfl
theorem evalF_append (b : V -> Bool) (B : CNF) : forall A : CNF,
evalF b (A ++ B) = (evalF b A && evalF b B) := by
intro A
induction A with
| nil => rfl
| cons c A ih =>
show (evalC b c && evalF b (A ++ B)) = ((evalC b c && evalF b A) && evalF b B)
rw [ih, and_assoc']
/-! ## Local clauses as Bool functions -/
def locEq (g x : Bool) : Bool := (!g || (x || false)) && ((g || (!x || false)) && true)
def locT (g : Bool) : Bool := (g || false) && true
def locF (g : Bool) : Bool := (!g || false) && true
def locNeg (g x : Bool) : Bool := (!g || (!x || false)) && ((g || (x || false)) && true)
def locAnd (g x y : Bool) : Bool :=
(!g || (x || false)) && ((!g || (y || false)) && ((g || (!x || (!y || false))) && true))
def locOr (g x y : Bool) : Bool :=
(g || (!x || false)) && ((g || (!y || false)) && ((!g || (x || (y || false))) && true))
theorem locEq_ok (x : Bool) : locEq x x = true := by cases x <;> rfl
theorem locNeg_ok (x : Bool) : locNeg (!x) x = true := by cases x <;> rfl
theorem locAnd_ok (x y : Bool) : locAnd (x && y) x y = true := by cases x <;> cases y <;> rfl
theorem locOr_ok (x y : Bool) : locOr (x || y) x y = true := by cases x <;> cases y <;> rfl
theorem locEq_inv (g x : Bool) (h : locEq g x = true) : g = x := by
cases g <;> cases x <;> first | rfl | exact Bool.noConfusion h
theorem locT_inv (g : Bool) (h : locT g = true) : g = true := by
cases g <;> first | rfl | exact Bool.noConfusion h
theorem locF_inv (g : Bool) (h : locF g = true) : g = false := by
cases g <;> first | rfl | exact Bool.noConfusion h
theorem locNeg_inv (g x : Bool) (h : locNeg g x = true) : g = !x := by
cases g <;> cases x <;> first | rfl | exact Bool.noConfusion h
theorem locAnd_inv (g x y : Bool) (h : locAnd g x y = true) : g = (x && y) := by
cases g <;> cases x <;> cases y <;> first | rfl | exact Bool.noConfusion h
theorem locOr_inv (g x y : Bool) (h : locOr g x y = true) : g = (x || y) := by
cases g <;> cases x <;> cases y <;> first | rfl | exact Bool.noConfusion h
/-! ## Soundness -/
/-- The extended assignment: inputs from a, each gate its value. -/
def ext (a : Nat -> Bool) : V -> Bool
| .inl i => a i
| .inr G => ev a G
theorem sound (a : Nat -> Bool) : forall G : Fm, evalF (ext a) (tse G) = true := by
intro G
induction G with
| var i => exact locEq_ok (a i)
| cst c => cases c with
| true => rfl
| false => rfl
| neg f ih =>
show evalF (ext a) (loc (.neg f) ++ tse f) = true
rw [evalF_append]
exact and_tt (locNeg_ok (ev a f)) ih
| and f h ihf ihh =>
show evalF (ext a) (loc (.and f h) ++ (tse f ++ tse h)) = true
rw [evalF_append, evalF_append]
exact and_tt (locAnd_ok (ev a f) (ev a h)) (and_tt ihf ihh)
| or f h ihf ihh =>
show evalF (ext a) (loc (.or f h) ++ (tse f ++ tse h)) = true
rw [evalF_append, evalF_append]
exact and_tt (locOr_ok (ev a f) (ev a h)) (and_tt ihf ihh)
/-! ## Completeness -/
theorem complete (b : V -> Bool) : forall G : Fm, evalF b (tse G) = true ->
b (.inr G) = ev (fun i => b (.inl i)) G := by
intro G
induction G with
| var i => intro h; exact locEq_inv _ _ h
| cst c => cases c with
| true => intro h; exact locT_inv _ h
| false => intro h; exact locF_inv _ h
| neg f ih =>
intro h
have h' : (evalF b (loc (.neg f)) && evalF b (tse f)) = true :=
(evalF_append b (tse f) (loc (.neg f))).symm.trans h
have e : b (.inr (.neg f)) = !(b (.inr f)) := locNeg_inv _ _ (and_l h')
rw [ih (and_r h')] at e
exact e
| and f g ihf ihg =>
intro h
have h1 : (evalF b (loc (.and f g)) && evalF b (tse f ++ tse g)) = true :=
(evalF_append b (tse f ++ tse g) (loc (.and f g))).symm.trans h
have h2 : (evalF b (tse f) && evalF b (tse g)) = true :=
(evalF_append b (tse g) (tse f)).symm.trans (and_r h1)
have e : b (.inr (.and f g)) = (b (.inr f) && b (.inr g)) := locAnd_inv _ _ _ (and_l h1)
rw [ihf (and_l h2), ihg (and_r h2)] at e
exact e
| or f g ihf ihg =>
intro h
have h1 : (evalF b (loc (.or f g)) && evalF b (tse f ++ tse g)) = true :=
(evalF_append b (tse f ++ tse g) (loc (.or f g))).symm.trans h
have h2 : (evalF b (tse f) && evalF b (tse g)) = true :=
(evalF_append b (tse g) (tse f)).symm.trans (and_r h1)
have e : b (.inr (.or f g)) = (b (.inr f) || b (.inr g)) := locOr_inv _ _ _ (and_l h1)
rw [ihf (and_l h2), ihg (and_r h2)] at e
exact e
/-! ## The reduction -/
theorem tseitin (F : Fm) :
((Exists fun a : Nat -> Bool => ev a F = true) ->
Exists fun b : V -> Bool => evalF b (full F) = true) /\
((Exists fun b : V -> Bool => evalF b (full F) = true) ->
Exists fun a : Nat -> Bool => ev a F = true) := by
apply And.intro
· intro h
exact Exists.elim h fun a ha => Exists.intro (ext a) (by
show ((ext a (.inr F) || false) && evalF (ext a) (tse F)) = true
exact and_tt ((or_false' (ev a F)).trans ha) (sound a F))
· intro h
exact Exists.elim h fun b hb => by
have hb' : ((b (.inr F) || false) && evalF b (tse F)) = true := hb
have h2 : b (.inr F) = true := (or_false' (b (.inr F))).symm.trans (and_l hb')
exact Exists.intro (fun i => b (.inl i)) ((complete b F (and_r hb')).symm.trans h2)
end Found.Pnp5
#print axioms Found.Pnp5.and_assoc'
#print axioms Found.Pnp5.and_l
#print axioms Found.Pnp5.and_r
#print axioms Found.Pnp5.and_tt
#print axioms Found.Pnp5.evalF_append
#print axioms Found.Pnp5.locEq_ok
#print axioms Found.Pnp5.locNeg_ok
#print axioms Found.Pnp5.locAnd_ok
#print axioms Found.Pnp5.locOr_ok
#print axioms Found.Pnp5.locEq_inv
#print axioms Found.Pnp5.locT_inv
#print axioms Found.Pnp5.locF_inv
#print axioms Found.Pnp5.locNeg_inv
#print axioms Found.Pnp5.locAnd_inv
#print axioms Found.Pnp5.locOr_inv
#print axioms Found.Pnp5.sound
#print axioms Found.Pnp5.complete
#print axioms Found.Pnp5.tseitin
#print axioms: 18 theorems, no axioms.
For a one-bit cellular automaton with a formula rule, the tableau formula of cell \(j\) at time \(t\) evaluates to that cell after \(t\) steps, so acceptance with some certificate is satisfiability of the pinned tableau.
found/Pnp6.lean
/-
T330 piece 6. found/Pnp6 -- the tableau step of Cook-Levin for a local machine.
A one-bit cellular automaton on the cells 0, 1, 2, ... (the cell left of 0 reads false)
with a local rule given as a formula over the variables 0 (left), 1 (self), 2 (right).
* substitution of formulas for variables, and its evaluation law;
* the tableau formula of cell j at time t: evaluating it on the initial cells gives the
cell after t steps;
* pinning input cells to constants: acceptance with some certificate in the free cells
is satisfiability of the pinned tableau formula (with Pnp5: of a CNF).
Open (PNP_MAP.md): multi-bit cells (Turing machines as automata), and the size count
with shared subformulas. It relativizes (a tool). Written with ASCII forms only.
CIC dependence: none.
-/
import found.Pnp1
namespace Found.Pnp6
open Found.Pnp1
/-! ## Substitution -/
def subst (s : Nat -> Fm) : Fm -> Fm
| .var i => s i
| .cst b => .cst b
| .neg f => .neg (subst s f)
| .and f g => .and (subst s f) (subst s g)
| .or f g => .or (subst s f) (subst s g)
theorem ev_subst (a : Nat -> Bool) (s : Nat -> Fm) :
forall F : Fm, ev a (subst s F) = ev (fun i => ev a (s i)) F := by
intro F
induction F with
| var i => rfl
| cst b => rfl
| neg f ih => exact congrArg (fun t => !t) ih
| and f g ihf ihg =>
show (ev a (subst s f) && ev a (subst s g)) = _
rw [ihf, ihg]; rfl
| or f g ihf ihg =>
show (ev a (subst s f) || ev a (subst s g)) = _
rw [ihf, ihg]; rfl
/-! ## The automaton -/
def left (c : Nat -> Bool) : Nat -> Bool
| 0 => false
| j + 1 => c j
/-- The neighbourhood of cell j as an assignment of the variables 0, 1, 2. -/
def nbr (c : Nat -> Bool) (j : Nat) : Nat -> Bool
| 0 => left c j
| 1 => c j
| 2 => c (j + 1)
| _ + 3 => false
def step (r : Fm) (c : Nat -> Bool) : Nat -> Bool := fun j => ev (nbr c j) r
def run (r : Fm) : Nat -> (Nat -> Bool) -> Nat -> Bool
| 0, c => c
| t + 1, c => step r (run r t c)
/-! ## The tableau -/
def leftF (prev : Nat -> Fm) : Nat -> Fm
| 0 => .cst false
| j + 1 => prev j
def nbrF (prev : Nat -> Fm) (j : Nat) : Nat -> Fm
| 0 => leftF prev j
| 1 => prev j
| 2 => prev (j + 1)
| _ + 3 => .cst false
/-- The formula of cell j at time t, over the initial cells. -/
def cellF (r : Fm) : Nat -> Nat -> Fm
| 0, j => .var j
| t + 1, j => subst (nbrF (cellF r t) j) r
/-- Evaluation depends only on the values of the variables (pointwise; no funext). -/
theorem ev_ext (a b : Nat -> Bool) (h : forall i, a i = b i) : forall F : Fm, ev a F = ev b F := by
intro F
induction F with
| var i => exact h i
| cst _ => rfl
| neg f ih => exact congrArg (fun t => !t) ih
| and f g ihf ihg =>
show (ev a f && ev a g) = (ev b f && ev b g)
rw [ihf, ihg]
| or f g ihf ihg =>
show (ev a f || ev a g) = (ev b f || ev b g)
rw [ihf, ihg]
theorem ev_nbrF (a : Nat -> Bool) (prev : Nat -> Fm) (c : Nat -> Bool)
(h : forall j, ev a (prev j) = c j) (j : Nat) :
forall i, ev a (nbrF prev j i) = nbr c j i := by
intro i
match i with
| 0 =>
match j with
| 0 => rfl
| j + 1 => exact h j
| 1 => exact h j
| 2 => exact h (j + 1)
| _ + 3 => rfl
/-- The tableau is correct: its formula of cell j at time t gives the cell after t steps. -/
theorem tableau (r : Fm) (a : Nat -> Bool) : forall (t j : Nat), ev a (cellF r t j) = run r t a j := by
intro t
induction t with
| zero => intro j; rfl
| succ t ih =>
intro j
show ev a (subst (nbrF (cellF r t) j) r) = ev (nbr (run r t a) j) r
rw [ev_subst]
exact ev_ext _ _ (ev_nbrF a (cellF r t) (run r t a) ih j) r
/-! ## Pinning input cells -/
def pinO : Option Bool -> Nat -> Fm
| some b, _ => .cst b
| none, i => .var i
def mergeO : Option Bool -> Bool -> Bool
| some b, _ => b
| none, x => x
theorem ev_pinO (a : Nat -> Bool) (i : Nat) : forall o : Option Bool, ev a (pinO o i) = mergeO o (a i)
| some _ => rfl
| none => rfl
def pinS (pin : Nat -> Option Bool) (i : Nat) : Fm := pinO (pin i) i
def merge (pin : Nat -> Option Bool) (a : Nat -> Bool) (i : Nat) : Bool := mergeO (pin i) (a i)
theorem ev_pin (pin : Nat -> Option Bool) (a : Nat -> Bool) (F : Fm) :
ev a (subst (pinS pin) F) = ev (merge pin a) F :=
(ev_subst a (pinS pin) F).trans (ev_ext _ _ (fun i => ev_pinO a i (pin i)) F)
/-- Acceptance with some certificate in the free cells is satisfiability of the pinned
tableau formula. -/
theorem accept_iff_sat (r : Fm) (pin : Nat -> Option Bool) (T j : Nat) :
((Exists fun a : Nat -> Bool => run r T (merge pin a) j = true) ->
Exists fun a : Nat -> Bool => ev a (subst (pinS pin) (cellF r T j)) = true) /\
((Exists fun a : Nat -> Bool => ev a (subst (pinS pin) (cellF r T j)) = true) ->
Exists fun a : Nat -> Bool => run r T (merge pin a) j = true) :=
And.intro
(fun h => Exists.elim h fun a ha => Exists.intro a
(((ev_pin pin a _).trans (tableau r (merge pin a) T j)).trans ha))
(fun h => Exists.elim h fun a ha => Exists.intro a
(((ev_pin pin a _).trans (tableau r (merge pin a) T j)).symm.trans ha))
end Found.Pnp6
#print axioms Found.Pnp6.ev_subst
#print axioms Found.Pnp6.ev_ext
#print axioms Found.Pnp6.ev_nbrF
#print axioms Found.Pnp6.tableau
#print axioms Found.Pnp6.ev_pinO
#print axioms Found.Pnp6.ev_pin
#print axioms Found.Pnp6.accept_iff_sat
#print axioms: 7 theorems, no axioms.
For cells of \(k\) bits and any local rule \(\delta \) on the \(3k\) neighbourhood bits, each output bit becomes a Shannon formula, and the tableau formula of bit \(b\) of cell \(j\) at time \(t\) evaluates to that bit after \(t\) steps.
found/Pnp7.lean
/-
T330 piece 7. found/Pnp7 -- the Cook-Levin tableau for multi-bit cells and an arbitrary
local rule.
Cells hold k bits; the local rule is any function delta of the 3k neighbourhood bits
(left, self, right; the cell left of 0 reads all false) giving each new bit. Each output
bit of delta becomes a formula by Shannon expansion (Pnp1.build), so any finite local
rule -- in particular the transition table of a Turing machine written on cells holding
(symbol, head, state) bits -- has a tableau. Proved: the tableau formula of bit b of
cell j at time t evaluates, on the initial bits a (enc j b), to that bit after t steps.
The initial layout enc is any map (no injectivity is needed for correctness).
Open (PNP_MAP.md): the Turing machine encoding itself, and the size count with shared
subformulas. It relativizes (a tool). Written with ASCII forms only.
CIC dependence: none.
-/
import found.Pnp6
namespace Found.Pnp7
open Found.Pnp1
open Found.Pnp6 (subst ev_subst)
/-! ## Lists -/
def nthd : List Bool -> Nat -> Bool
| [], _ => false
| x :: _, 0 => x
| _ :: L, i + 1 => nthd L i
def nthF : List Fm -> Nat -> Fm
| [], _ => .cst false
| f :: _, 0 => f
| _ :: L, i + 1 => nthF L i
def mapEv (a : Nat -> Bool) : List Fm -> List Bool
| [] => []
| f :: L => ev a f :: mapEv a L
def readF (g : Nat -> Fm) : Nat -> Nat -> List Fm
| _, 0 => []
| k, n + 1 => g k :: readF g (k + 1) n
theorem read_ext (f g : Nat -> Bool) (h : forall i, f i = g i) :
forall n k, read f k n = read g k n := by
intro n
induction n with
| zero => intro k; rfl
| succ n ih =>
intro k
show f k :: read f (k + 1) n = g k :: read g (k + 1) n
rw [h k, ih (k + 1)]
theorem read_shift (f : Nat -> Bool) : forall n k, read (fun i => f (i + 1)) k n = read f (k + 1) n := by
intro n
induction n with
| zero => intro k; rfl
| succ n ih =>
intro k
show f (k + 1) :: read (fun i => f (i + 1)) (k + 1) n = f (k + 1) :: read f (k + 1 + 1) n
rw [ih (k + 1)]
theorem read_nthd : forall L : List Bool, read (nthd L) 0 L.length = L := by
intro L
induction L with
| nil => rfl
| cons x L ih =>
show x :: read (nthd (x :: L)) (0 + 1) L.length = x :: L
rw [← read_shift (nthd (x :: L)) L.length 0]
exact congrArg (fun t => x :: t) ih
theorem ev_nthF (a : Nat -> Bool) : forall (Ls : List Fm) (i : Nat),
ev a (nthF Ls i) = nthd (mapEv a Ls) i := by
intro Ls
induction Ls with
| nil => intro i; cases i <;> rfl
| cons f L ih =>
intro i
cases i with
| zero => rfl
| succ i => exact ih i
theorem mapEv_append (a : Nat -> Bool) (B : List Fm) : forall A : List Fm,
mapEv a (A ++ B) = mapEv a A ++ mapEv a B := by
intro A
induction A with
| nil => rfl
| cons f A ih =>
show ev a f :: mapEv a (A ++ B) = ev a f :: (mapEv a A ++ mapEv a B)
rw [ih]
theorem mapEv_readF (a : Nat -> Bool) (g : Nat -> Fm) :
forall n k, mapEv a (readF g k n) = read (fun i => ev a (g i)) k n := by
intro n
induction n with
| zero => intro k; rfl
| succ n ih =>
intro k
show ev a (g k) :: mapEv a (readF g (k + 1) n) = ev a (g k) :: read (fun i => ev a (g i)) (k + 1) n
rw [ih (k + 1)]
theorem length_read (f : Nat -> Bool) : forall n k, (read f k n).length = n := by
intro n
induction n with
| zero => intro k; rfl
| succ n ih => intro k; exact congrArg (fun t => t + 1) (ih (k + 1))
theorem length_appendB (B : List Bool) : forall A : List Bool,
(A ++ B).length = A.length + B.length := by
intro A
induction A with
| nil => exact (Nat.zero_add _).symm
| cons x A ih =>
show (A ++ B).length + 1 = (A.length + 1) + B.length
rw [ih]
exact (Nat.succ_add _ _).symm
/-! ## The automaton with k-bit cells -/
abbrev Conf := Nat -> Nat -> Bool
def leftC (c : Conf) : Nat -> Nat -> Bool
| 0 => fun _ => false
| j + 1 => c j
/-- The 3k neighbourhood bits of cell j. -/
def nlist (c : Conf) (k j : Nat) : List Bool :=
read (leftC c j) 0 k ++ (read (c j) 0 k ++ read (c (j + 1)) 0 k)
def stepK (delta : List Bool -> Nat -> Bool) (k : Nat) (c : Conf) : Conf :=
fun j b => delta (nlist c k j) b
def runK (delta : List Bool -> Nat -> Bool) (k : Nat) : Nat -> Conf -> Conf
| 0, c => c
| t + 1, c => stepK delta k (runK delta k t c)
theorem length_nlist (c : Conf) (k j : Nat) : (nlist c k j).length = k + (k + k) := by
show (read (leftC c j) 0 k ++ (read (c j) 0 k ++ read (c (j + 1)) 0 k)).length = k + (k + k)
rw [length_appendB, length_appendB, length_read, length_read, length_read]
/-! ## The tableau -/
def leftFF (cf : Nat -> Nat -> Fm) : Nat -> Nat -> Fm
| 0 => fun _ => .cst false
| j + 1 => cf j
def nlistF (cf : Nat -> Nat -> Fm) (k j : Nat) : List Fm :=
readF (leftFF cf j) 0 k ++ (readF (cf j) 0 k ++ readF (cf (j + 1)) 0 k)
/-- The formula of output bit b of the rule, over the 3k neighbourhood variables. -/
def ruleF (delta : List Bool -> Nat -> Bool) (k b : Nat) : Fm :=
build (k + (k + k)) (fun xs => delta xs b) 0
def cellK (delta : List Bool -> Nat -> Bool) (k : Nat) (enc : Nat -> Nat -> Nat) :
Nat -> Nat -> Nat -> Fm
| 0, j, b => .var (enc j b)
| t + 1, j, b => subst (nthF (nlistF (cellK delta k enc t) k j)) (ruleF delta k b)
theorem mapEv_nlistF (a : Nat -> Bool) (cf : Nat -> Nat -> Fm) (c : Conf)
(h : forall j b, ev a (cf j b) = c j b) (k j : Nat) :
mapEv a (nlistF cf k j) = nlist c k j := by
show mapEv a (readF (leftFF cf j) 0 k ++ (readF (cf j) 0 k ++ readF (cf (j + 1)) 0 k)) =
read (leftC c j) 0 k ++ (read (c j) 0 k ++ read (c (j + 1)) 0 k)
rw [mapEv_append, mapEv_append, mapEv_readF, mapEv_readF, mapEv_readF,
read_ext _ _ (h j) k 0, read_ext _ _ (h (j + 1)) k 0]
have hl : forall i, ev a (leftFF cf j i) = leftC c j i := by
intro i
cases j with
| zero => rfl
| succ j => exact h j i
rw [read_ext _ _ hl k 0]
/-- The tableau is correct for k-bit cells and any local rule. -/
theorem tableauK (delta : List Bool -> Nat -> Bool) (k : Nat) (enc : Nat -> Nat -> Nat)
(a : Nat -> Bool) : forall t j b,
ev a (cellK delta k enc t j b) = runK delta k t (fun j b => a (enc j b)) j b := by
intro t
induction t with
| zero => intro j b; rfl
| succ t ih =>
intro j b
have hN := mapEv_nlistF a (cellK delta k enc t) _ ih k j
show ev a (subst (nthF (nlistF (cellK delta k enc t) k j)) (ruleF delta k b)) =
delta (nlist (runK delta k t (fun j b => a (enc j b))) k j) b
rw [ev_subst]
show ev (fun i => ev a (nthF (nlistF (cellK delta k enc t) k j) i))
(build (k + (k + k)) (fun xs => delta xs b) 0) = _
rw [ev_build, read_ext _ _ (ev_nthF a _) (k + (k + k)) 0, hN,
← length_nlist (runK delta k t (fun j b => a (enc j b))) k j, read_nthd]
end Found.Pnp7
#print axioms Found.Pnp7.read_ext
#print axioms Found.Pnp7.read_shift
#print axioms Found.Pnp7.read_nthd
#print axioms Found.Pnp7.ev_nthF
#print axioms Found.Pnp7.mapEv_append
#print axioms Found.Pnp7.mapEv_readF
#print axioms Found.Pnp7.length_read
#print axioms Found.Pnp7.length_appendB
#print axioms Found.Pnp7.length_nlist
#print axioms Found.Pnp7.mapEv_nlistF
#print axioms Found.Pnp7.tableauK
#print axioms: 11 theorems, no axioms.
One clause per neighbourhood pattern \(p\) (“the neighbourhood differs from \(p\), or \(x=d(p)\)”); an assignment satisfies these \(2^n\) clauses exactly when it gives \(x\) the rule’s value.
found/Pnp8.lean
/-
T330 piece 8. found/Pnp8 -- the Cook-Levin window clauses, directly in CNF.
For a cell bit x and its neighbourhood bits ys (n of them), and a local rule d giving
the new bit from the neighbourhood pattern, the window CNF has one clause per pattern
p of n bits: "ys differs from p, or x = d p". With n fixed by the machine, this is
2^n clauses of n + 1 literals -- a constant per cell, so the whole tableau CNF has
size linear in (time x width), with no formula blow-up and no Tseitin step.
Proved: an assignment satisfies the window CNF iff it gives x the value d (ys).
Variables are of any type (time, cell, bit). It relativizes (a tool; PNP_MAP.md).
Written with ASCII forms only. CIC dependence: none.
-/
namespace Found.Pnp8
universe u
variable {α : Type u}
/-! ## CNF over any variable type -/
def pol : Bool -> Bool -> Bool
| true, v => v
| false, v => !v
def litv (s : α -> Bool) (l : Bool × α) : Bool := pol l.1 (s l.2)
def clv (s : α -> Bool) : List (Bool × α) -> Bool
| [] => false
| l :: c => litv s l || clv s c
def cnfv (s : α -> Bool) : List (List (Bool × α)) -> Bool
| [] => true
| c :: f => clv s c && cnfv s f
def beqB : Bool -> Bool -> Bool
| true, true => true
| false, false => true
| true, false => false
| false, true => false
/-- Equality on the common prefix. -/
def eqP : List Bool -> List Bool -> Bool
| [], _ => true
| _ :: _, [] => true
| a :: as, b :: bs => beqB a b && eqP as bs
def mapv (s : α -> Bool) : List α -> List Bool
| [] => []
| y :: ys => s y :: mapv s ys
/-- The literals "ys differs from p", then the tail. -/
def wc : List α -> List Bool -> List (Bool × α) -> List (Bool × α)
| [], _, tl => tl
| _ :: _, [], tl => tl
| y :: ys, q :: p, tl => (!q, y) :: wc ys p tl
/-! ## Bool facts -/
theorem pol_neq (q v : Bool) : pol (!q) v = !(beqB v q) := by
cases q <;> cases v <;> rfl
theorem not_and_or (a b c : Bool) : (!a || (!b || c)) = (!(a && b) || c) := by
cases a <;> rfl
theorem beqB_refl (a : Bool) : beqB a a = true := by cases a <;> rfl
theorem beqB_eq (a b : Bool) (h : beqB a b = true) : a = b := by
cases a <;> cases b <;> first | rfl | exact Bool.noConfusion h
theorem pol_self (c : Bool) : pol c c = true := by cases c <;> rfl
theorem pol_eq (c v : Bool) (h : pol c v = true) : v = c := by
cases c <;> cases v <;> first | rfl | exact Bool.noConfusion h
theorem and_l {x y : Bool} (h : (x && y) = true) : x = true := by
cases x with
| true => rfl
| false => exact Bool.noConfusion h
theorem and_r {x y : Bool} (h : (x && y) = true) : y = true := by
cases x with
| true => exact h
| false => exact Bool.noConfusion h
theorem or_false_eq (x : Bool) : (x || false) = x := by cases x <;> rfl
/-! ## The clause value -/
theorem clv_wc (s : α -> Bool) (tl : List (Bool × α)) : forall (ys : List α) (p : List Bool),
clv s (wc ys p tl) = (!(eqP (mapv s ys) p) || clv s tl) := by
intro ys
induction ys with
| nil => intro p; rfl
| cons y ys ih =>
intro p
cases p with
| nil => rfl
| cons q p =>
show (pol (!q) (s y) || clv s (wc ys p tl)) = (!(beqB (s y) q && eqP (mapv s ys) p) || clv s tl)
rw [ih p, pol_neq]
exact not_and_or _ _ _
theorem eqP_refl : forall L : List Bool, eqP L L = true := by
intro L
induction L with
| nil => rfl
| cons a L ih =>
show (beqB a a && eqP L L) = true
rw [beqB_refl, ih]; rfl
theorem eqP_eq : forall (L p : List Bool), eqP L p = true -> L.length = p.length -> L = p := by
intro L
induction L with
| nil => intro p _ hl; cases p with
| nil => rfl
| cons _ _ => exact Nat.noConfusion hl
| cons a L ih => intro p h hl; cases p with
| nil => exact Nat.noConfusion hl
| cons q p =>
have h' : (beqB a q && eqP L p) = true := h
have e1 : a = q := beqB_eq a q (and_l h')
have e2 : L = p := ih p (and_r h') (Nat.succ.inj hl)
rw [e1, e2]
theorem length_mapv (s : α -> Bool) : forall ys : List α, (mapv s ys).length = ys.length := by
intro ys
induction ys with
| nil => rfl
| cons y ys ih => exact congrArg (fun t => t + 1) ih
/-! ## All patterns -/
def mapC (b : Bool) : List (List Bool) -> List (List Bool)
| [] => []
| p :: ps => (b :: p) :: mapC b ps
def allBits : Nat -> List (List Bool)
| 0 => [[]]
| n + 1 => mapC false (allBits n) ++ mapC true (allBits n)
def InL (p : List Bool) : List (List Bool) -> Prop
| [] => False
| q :: qs => p = q \/ InL p qs
theorem InL_app_l (p : List Bool) (B : List (List Bool)) : forall A, InL p A -> InL p (A ++ B) := by
intro A
induction A with
| nil => intro h; exact False.elim h
| cons q A ih => intro h; exact Or.elim h (fun e => Or.inl e) (fun h' => Or.inr (ih h'))
theorem InL_app_r (p : List Bool) (B : List (List Bool)) : forall A, InL p B -> InL p (A ++ B) := by
intro A
induction A with
| nil => intro h; exact h
| cons q A ih => intro h; exact Or.inr (ih h)
theorem InL_app_inv (p : List Bool) (B : List (List Bool)) : forall A, InL p (A ++ B) -> InL p A \/ InL p B := by
intro A
induction A with
| nil => intro h; exact Or.inr h
| cons q A ih =>
intro h
exact Or.elim h (fun e => Or.inl (Or.inl e))
(fun h' => Or.elim (ih h') (fun a => Or.inl (Or.inr a)) (fun b => Or.inr b))
theorem InL_mapC (b : Bool) (p : List Bool) : forall A, InL p A -> InL (b :: p) (mapC b A) := by
intro A
induction A with
| nil => intro h; exact False.elim h
| cons q A ih =>
intro h
exact Or.elim h (fun e => Or.inl (congrArg (fun t => b :: t) e)) (fun h' => Or.inr (ih h'))
theorem InL_mapC_inv (b : Bool) (p : List Bool) : forall A, InL p (mapC b A) ->
Exists fun q => p = b :: q /\ InL q A := by
intro A
induction A with
| nil => intro h; exact False.elim h
| cons q A ih =>
intro h
exact Or.elim h (fun e => Exists.intro q (And.intro e (Or.inl rfl)))
(fun h' => Exists.elim (ih h') fun r hr => Exists.intro r (And.intro hr.1 (Or.inr hr.2)))
theorem allBits_complete : forall L : List Bool, InL L (allBits L.length) := by
intro L
induction L with
| nil => exact Or.inl rfl
| cons a L ih =>
cases a with
| false => exact InL_app_l _ _ _ (InL_mapC false L _ ih)
| true => exact InL_app_r _ _ _ (InL_mapC true L _ ih)
theorem allBits_len : forall (n : Nat) (p : List Bool), InL p (allBits n) -> p.length = n := by
intro n
induction n with
| zero => intro p h; exact Or.elim h (fun e => by rw [e]; rfl) (fun f => False.elim f)
| succ n ih =>
intro p h
have hm : forall b, InL p (mapC b (allBits n)) -> p.length = n + 1 := fun b hb =>
Exists.elim (InL_mapC_inv b p _ hb) fun q hq => by
rw [hq.1]; exact congrArg (fun t => t + 1) (ih q hq.2)
exact Or.elim (InL_app_inv p _ _ h) (hm false) (hm true)
/-! ## The window CNF of one cell bit -/
def mapCl (ys : List α) (x : α) (d : List Bool -> Bool) : List (List Bool) -> List (List (Bool × α))
| [] => []
| p :: ps => wc ys p [(d p, x)] :: mapCl ys x d ps
def window (ys : List α) (x : α) (d : List Bool -> Bool) (n : Nat) : List (List (Bool × α)) :=
mapCl ys x d (allBits n)
theorem clv_one (s : α -> Bool) (ys : List α) (x : α) (d : List Bool -> Bool) (p : List Bool) :
clv s (wc ys p [(d p, x)]) = (!(eqP (mapv s ys) p) || (pol (d p) (s x) || false)) :=
clv_wc s _ ys p
theorem mapCl_mem (s : α -> Bool) (ys : List α) (x : α) (d : List Bool -> Bool) :
forall ps, cnfv s (mapCl ys x d ps) = true -> forall p, InL p ps -> clv s (wc ys p [(d p, x)]) = true := by
intro ps
induction ps with
| nil => intro _ p hp; exact False.elim hp
| cons q ps ih =>
intro h p hp
have h' : (clv s (wc ys q [(d q, x)]) && cnfv s (mapCl ys x d ps)) = true := h
exact Or.elim hp (fun e => by rw [e]; exact and_l h') (fun hp' => ih (and_r h') p hp')
theorem mapCl_all (s : α -> Bool) (ys : List α) (x : α) (d : List Bool -> Bool) :
forall ps, (forall p, InL p ps -> clv s (wc ys p [(d p, x)]) = true) -> cnfv s (mapCl ys x d ps) = true := by
intro ps
induction ps with
| nil => intro _; rfl
| cons q ps ih =>
intro h
show (clv s (wc ys q [(d q, x)]) && cnfv s (mapCl ys x d ps)) = true
rw [h q (Or.inl rfl), ih (fun p hp => h p (Or.inr hp))]
rfl
/-- The window CNF holds iff the cell bit is the rule applied to its neighbourhood. -/
theorem window_iff (s : α -> Bool) (ys : List α) (x : α) (d : List Bool -> Bool) (n : Nat)
(hn : ys.length = n) :
(cnfv s (window ys x d n) = true -> s x = d (mapv s ys)) /\
(s x = d (mapv s ys) -> cnfv s (window ys x d n) = true) := by
have hL : (mapv s ys).length = n := (length_mapv s ys).trans hn
apply And.intro
· intro h
have hin : InL (mapv s ys) (allBits n) := by
rw [← hL]; exact allBits_complete _
have hc := mapCl_mem s ys x d (allBits n) h (mapv s ys) hin
rw [clv_one, eqP_refl] at hc
have hc' : (pol (d (mapv s ys)) (s x) || false) = true := hc
rw [or_false_eq] at hc'
exact pol_eq _ _ hc'
· intro hx
apply mapCl_all
intro p hp
rw [clv_one]
cases he : eqP (mapv s ys) p with
| false => rfl
| true =>
have e : mapv s ys = p := eqP_eq _ _ he (hL.trans (allBits_len n p hp).symm)
rw [← e, hx, pol_self]
rfl
end Found.Pnp8
#print axioms Found.Pnp8.pol_neq
#print axioms Found.Pnp8.not_and_or
#print axioms Found.Pnp8.beqB_refl
#print axioms Found.Pnp8.beqB_eq
#print axioms Found.Pnp8.pol_self
#print axioms Found.Pnp8.pol_eq
#print axioms Found.Pnp8.and_l
#print axioms Found.Pnp8.and_r
#print axioms Found.Pnp8.or_false_eq
#print axioms Found.Pnp8.clv_wc
#print axioms Found.Pnp8.eqP_refl
#print axioms Found.Pnp8.eqP_eq
#print axioms Found.Pnp8.length_mapv
#print axioms Found.Pnp8.InL_app_l
#print axioms Found.Pnp8.InL_app_r
#print axioms Found.Pnp8.InL_app_inv
#print axioms Found.Pnp8.InL_mapC
#print axioms Found.Pnp8.InL_mapC_inv
#print axioms Found.Pnp8.allBits_complete
#print axioms Found.Pnp8.allBits_len
#print axioms Found.Pnp8.clv_one
#print axioms Found.Pnp8.mapCl_mem
#print axioms Found.Pnp8.mapCl_all
#print axioms Found.Pnp8.window_iff
#print axioms: 24 theorems, no axioms.
For a machine of width \(W\) with \(k\)-bit cells run for \(T\) steps, the tableau CNF holds exactly when every step obeys the rule and the boundary cell is false, and its clause count is polynomial in \(T\) and \(W\).
found/Pnp9.lean
/-
T330 piece 9. found/Pnp9 -- the whole tableau CNF of a width-W, k-bit local machine,
with its exact clause count.
Variables (t, j, b): bit b of cell j at time t. Cell W is the boundary cell: it is the
left neighbour of cell 0 and the right neighbour of cell W-1, and unit clauses force it
false at every time. The CNF is the window CNF (Pnp8) of every (t < T, j < W, b < k)
plus the boundary units. Proved:
* an assignment satisfies it iff every step obeys the rule and the boundary is false;
* it has exactly T * (W * (k * 2^(3k))) + (T + 1) * k clauses (each window clause has
3k + 1 literals) -- polynomial in T and W for a fixed machine.
It relativizes (a tool; PNP_MAP.md). Written with ASCII forms only.
CIC dependence: none.
-/
import found.Pnp8
namespace Found.Pnp9
open Found.Pnp8
abbrev V := Nat × (Nat × Nat)
/-! ## Generic facts -/
theorem cnfv_append {α : Type} (s : α -> Bool) (B : List (List (Bool × α))) :
forall A, cnfv s (A ++ B) = (cnfv s A && cnfv s B) := by
intro A
induction A with
| nil => rfl
| cons c A ih =>
show (clv s c && cnfv s (A ++ B)) = ((clv s c && cnfv s A) && cnfv s B)
rw [ih]
cases clv s c <;> rfl
theorem and_tt {x y : Bool} (hx : x = true) (hy : y = true) : (x && y) = true := by
subst hx; subst hy; rfl
theorem length_app {β : Type} (B : List β) : forall A : List β, (A ++ B).length = A.length + B.length := by
intro A
induction A with
| nil => exact (Nat.zero_add _).symm
| cons x A ih =>
show (A ++ B).length + 1 = (A.length + 1) + B.length
rw [ih]
exact (Nat.succ_add _ _).symm
theorem lt_succ_cases (i n : Nat) (h : i < n + 1) : i < n \/ i = n := by
cases Nat.le_of_lt_succ h with
| refl => exact Or.inr rfl
| step h' => exact Or.inl (Nat.lt_succ_of_le h')
/-- Concatenation over i < n. -/
def forR {β : Type} : Nat -> (Nat -> List β) -> List β
| 0, _ => []
| n + 1, g => forR n g ++ g n
theorem forR_all {α : Type} (s : α -> Bool) (g : Nat -> List (List (Bool × α))) :
forall n, (cnfv s (forR n g) = true -> forall i, i < n -> cnfv s (g i) = true) /\
((forall i, i < n -> cnfv s (g i) = true) -> cnfv s (forR n g) = true) := by
intro n
induction n with
| zero => exact And.intro (fun _ i hi => False.elim (Nat.not_lt_zero i hi)) (fun _ => rfl)
| succ n ih =>
apply And.intro
· intro h i hi
have h' : (cnfv s (forR n g) && cnfv s (g n)) = true :=
(cnfv_append s (g n) (forR n g)).symm.trans h
exact Or.elim (lt_succ_cases i n hi) (fun hl => ih.1 (and_l h') i hl)
(fun e => by rw [e]; exact and_r h')
· intro h
show cnfv s (forR n g ++ g n) = true
rw [cnfv_append]
exact and_tt (ih.2 (fun i hi => h i (Nat.lt_succ_of_lt hi))) (h n (Nat.lt_succ_self n))
theorem forR_length {β : Type} (g : Nat -> List β) (m : Nat) (hg : forall i, (g i).length = m) :
forall n, (forR n g).length = n * m := by
intro n
induction n with
| zero => exact (Nat.zero_mul m).symm
| succ n ih =>
show (forR n g ++ g n).length = (n + 1) * m
rw [length_app, ih, hg n, Nat.succ_mul]
/-! ## Pattern count -/
def pow2 : Nat -> Nat
| 0 => 1
| n + 1 => pow2 n + pow2 n
theorem length_mapC (b : Bool) : forall A : List (List Bool), (mapC b A).length = A.length := by
intro A
induction A with
| nil => rfl
| cons p A ih => exact congrArg (fun t => t + 1) ih
theorem length_allBits : forall n, (allBits n).length = pow2 n := by
intro n
induction n with
| zero => rfl
| succ n ih =>
show (mapC false (allBits n) ++ mapC true (allBits n)).length = pow2 n + pow2 n
rw [length_app, length_mapC, length_mapC, ih]
theorem length_mapCl {α : Type} (ys : List α) (x : α) (d : List Bool -> Bool) :
forall ps : List (List Bool), (mapCl ys x d ps).length = ps.length := by
intro ps
induction ps with
| nil => rfl
| cons p ps ih => exact congrArg (fun t => t + 1) ih
theorem length_window {α : Type} (ys : List α) (x : α) (d : List Bool -> Bool) (n : Nat) :
(window ys x d n).length = pow2 n :=
(length_mapCl ys x d (allBits n)).trans (length_allBits n)
/-! ## The tableau CNF -/
/-- Bits b0, ..., b0+k-1 of cell c at time t. -/
def readV (t c : Nat) : Nat -> Nat -> List V
| _, 0 => []
| b, k + 1 => (t, (c, b)) :: readV t c (b + 1) k
def leftIdx (W : Nat) : Nat -> Nat
| 0 => W
| j + 1 => j
def nbV (k W t j : Nat) : List V :=
readV t (leftIdx W j) 0 k ++ (readV t j 0 k ++ readV t (j + 1) 0 k)
theorem length_readV (t c : Nat) : forall k b, (readV t c b k).length = k := by
intro k
induction k with
| zero => intro b; rfl
| succ k ih => intro b; exact congrArg (fun x => x + 1) (ih (b + 1))
theorem length_nbV (k W t j : Nat) : (nbV k W t j).length = k + (k + k) := by
show (readV t (leftIdx W j) 0 k ++ (readV t j 0 k ++ readV t (j + 1) 0 k)).length = k + (k + k)
rw [length_app, length_app, length_readV, length_readV, length_readV]
def windows (delta : List Bool -> Nat -> Bool) (k W T : Nat) : List (List (Bool × V)) :=
forR T fun t => forR W fun j => forR k fun b =>
window (nbV k W t j) (t + 1, (j, b)) (fun p => delta p b) (k + (k + k))
def bounds (k W T : Nat) : List (List (Bool × V)) :=
forR (T + 1) fun t => forR k fun b => [[(false, (t, (W, b)))]]
def tab (delta : List Bool -> Nat -> Bool) (k W T : Nat) : List (List (Bool × V)) :=
windows delta k W T ++ bounds k W T
/-- The step condition. -/
def Steps (s : V -> Bool) (delta : List Bool -> Nat -> Bool) (k W T : Nat) : Prop :=
forall t, t < T -> forall j, j < W -> forall b, b < k ->
s (t + 1, (j, b)) = delta (mapv s (nbV k W t j)) b
/-- The boundary condition. -/
def Bound (s : V -> Bool) (k W T : Nat) : Prop :=
forall t, t < T + 1 -> forall b, b < k -> s (t, (W, b)) = false
theorem unit_iff (s : V -> Bool) (v : V) : (cnfv s [[(false, v)]] = true -> s v = false) /\
(s v = false -> cnfv s [[(false, v)]] = true) := by
apply And.intro
· intro h
have h' : ((!(s v) || false) && true) = true := h
cases hv : s v with
| false => rfl
| true => rw [hv] at h'; exact Bool.noConfusion h'
· intro h
show ((!(s v) || false) && true) = true
rw [h]; rfl
theorem windows_iff (s : V -> Bool) (delta : List Bool -> Nat -> Bool) (k W T : Nat) :
(cnfv s (windows delta k W T) = true -> Steps s delta k W T) /\
(Steps s delta k W T -> cnfv s (windows delta k W T) = true) := by
apply And.intro
· intro h t ht j hj b hb
have h1 := (forR_all s _ T).1 h t ht
have h2 := (forR_all s _ W).1 h1 j hj
have h3 := (forR_all s _ k).1 h2 b hb
exact (window_iff s (nbV k W t j) (t + 1, (j, b)) (fun p => delta p b) _ (length_nbV k W t j)).1 h3
· intro h
exact (forR_all s _ T).2 fun t ht => (forR_all s _ W).2 fun j hj => (forR_all s _ k).2 fun b hb =>
(window_iff s (nbV k W t j) (t + 1, (j, b)) (fun p => delta p b) _ (length_nbV k W t j)).2
(h t ht j hj b hb)
theorem bounds_iff (s : V -> Bool) (k W T : Nat) :
(cnfv s (bounds k W T) = true -> Bound s k W T) /\
(Bound s k W T -> cnfv s (bounds k W T) = true) := by
apply And.intro
· intro h t ht b hb
exact (unit_iff s _).1 ((forR_all s _ k).1 ((forR_all s _ (T + 1)).1 h t ht) b hb)
· intro h
exact (forR_all s _ (T + 1)).2 fun t ht => (forR_all s _ k).2 fun b hb =>
(unit_iff s _).2 (h t ht b hb)
/-- The tableau CNF holds iff every step obeys the rule and the boundary cell is false. -/
theorem tab_iff (s : V -> Bool) (delta : List Bool -> Nat -> Bool) (k W T : Nat) :
(cnfv s (tab delta k W T) = true -> Steps s delta k W T /\ Bound s k W T) /\
(Steps s delta k W T /\ Bound s k W T -> cnfv s (tab delta k W T) = true) := by
apply And.intro
· intro h
have h' : (cnfv s (windows delta k W T) && cnfv s (bounds k W T)) = true :=
(cnfv_append s _ _).symm.trans h
exact And.intro ((windows_iff s delta k W T).1 (and_l h')) ((bounds_iff s k W T).1 (and_r h'))
· intro h
show cnfv s (windows delta k W T ++ bounds k W T) = true
rw [cnfv_append]
exact and_tt ((windows_iff s delta k W T).2 h.1) ((bounds_iff s k W T).2 h.2)
/-- The exact clause count. -/
theorem tab_count (delta : List Bool -> Nat -> Bool) (k W T : Nat) :
(tab delta k W T).length = T * (W * (k * pow2 (k + (k + k)))) + (T + 1) * k := by
show (windows delta k W T ++ bounds k W T).length = _
rw [length_app]
have hw : (windows delta k W T).length = T * (W * (k * pow2 (k + (k + k)))) :=
forR_length _ _ (fun t => forR_length _ _ (fun j => forR_length _ _
(fun b => length_window _ _ _ _) k) W) T
have hb : (bounds k W T).length = (T + 1) * k :=
forR_length _ _ (fun t => (forR_length _ 1 (fun b => rfl) k).trans (Nat.mul_one k)) (T + 1)
rw [hw, hb]
end Found.Pnp9
#print axioms Found.Pnp9.cnfv_append
#print axioms Found.Pnp9.and_tt
#print axioms Found.Pnp9.length_app
#print axioms Found.Pnp9.lt_succ_cases
#print axioms Found.Pnp9.forR_all
#print axioms Found.Pnp9.forR_length
#print axioms Found.Pnp9.length_mapC
#print axioms Found.Pnp9.length_allBits
#print axioms Found.Pnp9.length_mapCl
#print axioms Found.Pnp9.length_window
#print axioms Found.Pnp9.length_readV
#print axioms Found.Pnp9.length_nbV
#print axioms Found.Pnp9.unit_iff
#print axioms Found.Pnp9.windows_iff
#print axioms Found.Pnp9.bounds_iff
#print axioms Found.Pnp9.tab_iff
#print axioms Found.Pnp9.tab_count
#print axioms: 17 theorems, no axioms.
An assignment satisfying the step and boundary conditions equals the run of the width-\(W\) machine from its time-0 layer, and every run from a start with a false boundary satisfies them.
found/Pnp10.lean
/-
T330 piece 10. found/Pnp10 -- satisfying assignments of the tableau CNF are exactly the
runs of the width-W machine.
The width-W machine: cells 0..W-1 of k bits, cell W is the boundary (always false, the
left neighbour of cell 0 and the right neighbour of cell W-1), and each step applies the
local rule delta to the 3k neighbourhood bits. Proved:
* an assignment satisfying the step and boundary conditions of Pnp9 equals, at every
t <= T, j <= W, b < k, the run of the machine from its time-0 layer;
* conversely the run from any start with a false boundary satisfies them.
It relativizes (a tool; PNP_MAP.md). Written with ASCII forms only.
CIC dependence: none.
-/
import found.Pnp9
import found.Pnp2
namespace Found.Pnp10
open Found.Pnp1 (read)
open Found.Pnp8
open Found.Pnp9
/-! ## The width-W machine -/
abbrev Conf := Nat -> Nat -> Bool
def nbC (c : Conf) (k W j : Nat) : List Bool :=
read (c (leftIdx W j)) 0 k ++ (read (c j) 0 k ++ read (c (j + 1)) 0 k)
def gate : Bool -> Bool -> Bool
| true, _ => false
| false, x => x
def stepW (delta : List Bool -> Nat -> Bool) (k W : Nat) (c : Conf) : Conf :=
fun j b => gate (Nat.beq j W) (delta (nbC c k W j) b)
def runW (delta : List Bool -> Nat -> Bool) (k W : Nat) : Nat -> Conf -> Conf
| 0, c => c
| t + 1, c => stepW delta k W (runW delta k W t c)
/-- The layer of an assignment at time t. -/
def sl (s : V -> Bool) (t : Nat) : Conf := fun c b => s (t, (c, b))
/-! ## Lemmas -/
theorem beq_refl : forall n : Nat, Nat.beq n n = true := by
intro n
induction n with
| zero => rfl
| succ n ih => exact ih
theorem mapv_append {α : Type} (s : α -> Bool) (B : List α) : forall A : List α,
mapv s (A ++ B) = mapv s A ++ mapv s B := by
intro A
induction A with
| nil => rfl
| cons y A ih =>
show s y :: mapv s (A ++ B) = s y :: (mapv s A ++ mapv s B)
rw [ih]
theorem mapv_readV (s : V -> Bool) (t c : Nat) : forall n b0,
mapv s (readV t c b0 n) = read (fun b => s (t, (c, b))) b0 n := by
intro n
induction n with
| zero => intro b0; rfl
| succ n ih =>
intro b0
show s (t, (c, b0)) :: mapv s (readV t c (b0 + 1) n) = s (t, (c, b0)) :: read (fun b => s (t, (c, b))) (b0 + 1) n
rw [ih (b0 + 1)]
theorem mapv_nbV (s : V -> Bool) (k W t j : Nat) : mapv s (nbV k W t j) = nbC (sl s t) k W j := by
show mapv s (readV t (leftIdx W j) 0 k ++ (readV t j 0 k ++ readV t (j + 1) 0 k)) = _
rw [mapv_append, mapv_append, mapv_readV, mapv_readV, mapv_readV]
rfl
theorem read_ext_lt (f g : Nat -> Bool) : forall n b0,
(forall i, i < b0 + n -> f i = g i) -> read f b0 n = read g b0 n := by
intro n
induction n with
| zero => intro b0 _; rfl
| succ n ih =>
intro b0 h
show f b0 :: read f (b0 + 1) n = g b0 :: read g (b0 + 1) n
have h0 : f b0 = g b0 := h b0 (Nat.lt_succ_of_le (Nat.le_add_right b0 n))
have h1 : read f (b0 + 1) n = read g (b0 + 1) n := ih (b0 + 1) (fun i hi =>
h i ((congrArg (fun m => i < m) (Nat.add_right_comm b0 1 n)).mp hi))
rw [h0, h1]
theorem read_ext_k (f g : Nat -> Bool) (k : Nat) (h : forall i, i < k -> f i = g i) :
read f 0 k = read g 0 k :=
read_ext_lt f g k 0 (fun i hi => h i ((congrArg (fun m => i < m) (Nat.zero_add k)).mp hi))
theorem leftIdx_lt (W : Nat) : forall j, j < W -> leftIdx W j < W + 1
| 0, _ => Nat.lt_succ_self W
| _ + 1, hj => Nat.lt_succ_of_lt (Nat.lt_of_succ_lt hj)
theorem nbC_ext (x y : Conf) (k W j : Nat) (hj : j < W)
(h : forall c, c < W + 1 -> forall b, b < k -> x c b = y c b) : nbC x k W j = nbC y k W j := by
show read (x (leftIdx W j)) 0 k ++ (read (x j) 0 k ++ read (x (j + 1)) 0 k) =
read (y (leftIdx W j)) 0 k ++ (read (y j) 0 k ++ read (y (j + 1)) 0 k)
rw [read_ext_k _ _ k (h _ (leftIdx_lt W j hj)), read_ext_k _ _ k (h j (Nat.lt_succ_of_lt hj)),
read_ext_k _ _ k (h (j + 1) (Nat.succ_lt_succ hj))]
/-! ## Satisfying assignments are runs -/
theorem sat_is_run (s : V -> Bool) (delta : List Bool -> Nat -> Bool) (k W T : Nat)
(hs : Steps s delta k W T) (hb : Bound s k W T) :
forall t, t < T + 1 -> forall j, j < W + 1 -> forall b, b < k ->
s (t, (j, b)) = runW delta k W t (sl s 0) j b := by
intro t
induction t with
| zero => intro _ j _ b _; rfl
| succ t ih =>
intro ht j hj b hbk
have ht' : t < T := Nat.lt_of_succ_lt_succ ht
show s (t + 1, (j, b)) = gate (Nat.beq j W) (delta (nbC (runW delta k W t (sl s 0)) k W j) b)
exact Or.elim (lt_succ_cases j W hj)
(fun hjW => by
rw [Found.Pnp2.beq_false_of_ne j W (Nat.ne_of_lt hjW), hs t ht' j hjW b hbk, mapv_nbV,
nbC_ext (sl s t) (runW delta k W t (sl s 0)) k W j hjW
(fun c hc b' hb' => ih (Nat.lt_succ_of_lt ht') c hc b' hb')]
rfl)
(fun e => by
rw [e, beq_refl W]
exact hb (t + 1) ht b hbk)
/-! ## Runs are satisfying assignments -/
/-- The assignment of a run. -/
def ofRun (delta : List Bool -> Nat -> Bool) (k W : Nat) (c0 : Conf) : V -> Bool :=
fun v => runW delta k W v.1 c0 v.2.1 v.2.2
theorem run_steps (delta : List Bool -> Nat -> Bool) (k W T : Nat) (c0 : Conf) :
Steps (ofRun delta k W c0) delta k W T := by
intro t _ j hj b _
show gate (Nat.beq j W) (delta (nbC (runW delta k W t c0) k W j) b) =
delta (mapv (ofRun delta k W c0) (nbV k W t j)) b
rw [Found.Pnp2.beq_false_of_ne j W (Nat.ne_of_lt hj), mapv_nbV]
rfl
theorem run_bound (delta : List Bool -> Nat -> Bool) (k W T : Nat) (c0 : Conf)
(h0 : forall b, b < k -> c0 W b = false) : Bound (ofRun delta k W c0) k W T := by
intro t _ b hb
cases t with
| zero => exact h0 b hb
| succ t =>
show gate (Nat.beq W W) (delta (nbC (runW delta k W t c0) k W W) b) = false
rw [beq_refl W]
rfl
end Found.Pnp10
#print axioms Found.Pnp10.beq_refl
#print axioms Found.Pnp10.mapv_append
#print axioms Found.Pnp10.mapv_readV
#print axioms Found.Pnp10.mapv_nbV
#print axioms Found.Pnp10.read_ext_lt
#print axioms Found.Pnp10.read_ext_k
#print axioms Found.Pnp10.leftIdx_lt
#print axioms Found.Pnp10.nbC_ext
#print axioms Found.Pnp10.sat_is_run
#print axioms Found.Pnp10.run_steps
#print axioms Found.Pnp10.run_bound
#print axioms: 11 theorems, no axioms.
The tableau CNF with an acceptance unit clause and pinned input cells is satisfiable exactly when some certificate in the free cells makes the width-\(W\) machine accept after \(T\) steps.
found/Pnp11.lean
/-
T330 piece 11. found/Pnp11 -- Cook-Levin for local machines.
The CNF of a width-W, k-bit local machine run for T steps, with some input cells pinned
(unit clauses on the time-0 layer) and acceptance read at bit 0 of cell 0 at time T:
full = tableau (Pnp9) ++ [accept] ++ pins.
Proved: full is satisfiable iff some start (boundary false, pins respected; the other
cells are the certificate) makes the machine accept after T steps. Its size is
T W k 2^(3k) + (T + 1) k + 1 + (number of pins) clauses (Pnp9.tab_count).
Open (PNP_MAP.md): Turing machines (or the machine model OM) as local machines. It
relativizes (a tool). Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp10
namespace Found.Pnp11
open Found.Pnp8
open Found.Pnp9
open Found.Pnp10
/-- Pins: (cell, bit, value) on the time-0 layer. -/
def pinCl : List (Nat × (Nat × Bool)) -> List (List (Bool × V))
| [] => []
| (j, (b, v)) :: ps => [(v, (0, (j, b)))] :: pinCl ps
def PinsOK (c0 : Conf) : List (Nat × (Nat × Bool)) -> Prop
| [] => True
| (j, (b, v)) :: ps => c0 j b = v /\ PinsOK c0 ps
def accCl (T : Nat) : List (Bool × V) := [(true, (T, (0, 0)))]
def full (delta : List Bool -> Nat -> Bool) (k W T : Nat) (ps : List (Nat × (Nat × Bool))) :
List (List (Bool × V)) :=
tab delta k W T ++ (accCl T :: pinCl ps)
theorem unit_clause (s : V -> Bool) (v : Bool) (x : V) :
(clv s [(v, x)] = true -> s x = v) /\ (s x = v -> clv s [(v, x)] = true) := by
apply And.intro
· intro h
have h' : (pol v (s x) || false) = true := h
rw [or_false_eq] at h'
exact pol_eq _ _ h'
· intro h
show (pol v (s x) || false) = true
rw [h, pol_self]
rfl
theorem pins_iff (s : V -> Bool) : forall ps : List (Nat × (Nat × Bool)),
(cnfv s (pinCl ps) = true -> PinsOK (sl s 0) ps) /\
(PinsOK (sl s 0) ps -> cnfv s (pinCl ps) = true) := by
intro ps
induction ps with
| nil => exact And.intro (fun _ => True.intro) (fun _ => rfl)
| cons e ps ih =>
cases e with
| mk j bv =>
cases bv with
| mk b v =>
apply And.intro
· intro h
have h' : (clv s [(v, (0, (j, b)))] && cnfv s (pinCl ps)) = true := h
exact And.intro ((unit_clause s v _).1 (and_l h')) (ih.1 (and_r h'))
· intro h
show (clv s [(v, (0, (j, b)))] && cnfv s (pinCl ps)) = true
rw [(unit_clause s v _).2 h.1, ih.2 h.2]
rfl
theorem pins_run (delta : List Bool -> Nat -> Bool) (k W : Nat) (c0 : Conf) :
forall ps, PinsOK c0 ps -> PinsOK (sl (ofRun delta k W c0) 0) ps := by
intro ps h
exact h
/-- Cook-Levin for local machines: the CNF is satisfiable iff some certificate makes the
machine accept. -/
theorem cook_levin_local (delta : List Bool -> Nat -> Bool) (k W T : Nat) (hk : 0 < k)
(ps : List (Nat × (Nat × Bool))) :
((Exists fun s : V -> Bool => cnfv s (full delta k W T ps) = true) ->
Exists fun c0 : Conf => (forall b, b < k -> c0 W b = false) /\ PinsOK c0 ps /\
runW delta k W T c0 0 0 = true) /\
((Exists fun c0 : Conf => (forall b, b < k -> c0 W b = false) /\ PinsOK c0 ps /\
runW delta k W T c0 0 0 = true) ->
Exists fun s : V -> Bool => cnfv s (full delta k W T ps) = true) := by
apply And.intro
· intro h
exact Exists.elim h fun s hs => by
have h1 : (cnfv s (tab delta k W T) && (clv s (accCl T) && cnfv s (pinCl ps))) = true :=
(cnfv_append s _ _).symm.trans hs
have htab := (tab_iff s delta k W T).1 (and_l h1)
have hacc : s (T, (0, 0)) = true := (unit_clause s true _).1 (and_l (and_r h1))
have hpin := (pins_iff s ps).1 (and_r (and_r h1))
exact Exists.intro (sl s 0) (And.intro (fun b hb => htab.2 0 (Nat.zero_lt_succ T) b hb)
(And.intro hpin ((sat_is_run s delta k W T htab.1 htab.2 T (Nat.lt_succ_self T) 0
(Nat.zero_lt_succ W) 0 hk).symm.trans hacc)))
· intro h
exact Exists.elim h fun c0 hc => by
apply Exists.intro (ofRun delta k W c0)
show cnfv (ofRun delta k W c0) (tab delta k W T ++ (accCl T :: pinCl ps)) = true
rw [cnfv_append]
exact and_tt ((tab_iff _ delta k W T).2
(And.intro (run_steps delta k W T c0) (run_bound delta k W T c0 hc.1)))
(and_tt ((unit_clause _ true _).2 hc.2.2) ((pins_iff _ ps).2 hc.2.1))
end Found.Pnp11
#print axioms Found.Pnp11.unit_clause
#print axioms Found.Pnp11.pins_iff
#print axioms Found.Pnp11.pins_run
#print axioms Found.Pnp11.cook_levin_local
#print axioms: 4 theorems, no axioms.
Laying a configuration out on cells of \(m+2\) bits (symbol, head, state), one step of the width-\(W\) local machine is one step of the Turing machine at every cell, while the head stays inside.
found/Pnp12.lean
/-
T330 piece 12. found/Pnp12 -- a Turing machine as a local machine (one step).
A one-tape Turing machine with states as bit lists of length m: from state q reading s
it moves to next q s, writes write q s and moves right iff right q s. A configuration
(tape, pos, q) is laid out on cells of k = m + 2 bits: bit 0 the symbol, bit 1 "the head
is here", bits 2.. the state where the head is (false elsewhere). The local rule reads
the left, own and right cells. Proved: one step of the width-W local machine (Pnp10)
on the layout of a configuration is the layout of the next configuration, at every
cell j <= W, when the head is at a cell p + 1 with p + 2 < W, the boundary cell W holds
false and the state has m bits.
It relativizes (a tool; PNP_MAP.md). Written with ASCII forms only.
CIC dependence: none.
-/
import found.Pnp11
import found.Pnp7
namespace Found.Pnp12
open Found.Pnp1 (read)
open Found.Pnp7 (nthd read_nthd length_read)
open Found.Pnp9 (leftIdx)
open Found.Pnp10 (Conf nbC stepW gate read_ext_k beq_refl)
structure TM where
m : Nat
next : List Bool -> Bool -> List Bool
write : List Bool -> Bool -> Bool
right : List Bool -> Bool -> Bool
def sel : Bool -> Bool -> Bool -> Bool
| true, x, _ => x
| false, _, y => y
def selN : Bool -> Nat -> Nat -> Nat
| true, x, _ => x
| false, _, y => y
variable (M : TM)
/-! ## The local rule -/
def stG (g : Nat -> Bool) : List Bool := read (fun i => g (i + 2)) 0 M.m
def lb (L : List Bool) (i : Nat) : Bool := nthd L i
def cb (L : List Bool) (i : Nat) : Bool := nthd L (M.m + 2 + i)
def rb (L : List Bool) (i : Nat) : Bool := nthd L (M.m + 2 + (M.m + 2 + i))
def rule (L : List Bool) : Nat -> Bool
| 0 => sel (cb M L 1) (M.write (stG M (cb M L)) (cb M L 0)) (cb M L 0)
| 1 => (lb L 1 && M.right (stG M (lb L)) (lb L 0)) ||
(rb M L 1 && !(M.right (stG M (rb M L)) (rb M L 0)))
| i + 2 => ((lb L 1 && M.right (stG M (lb L)) (lb L 0)) && nthd (M.next (stG M (lb L)) (lb L 0)) i) ||
((rb M L 1 && !(M.right (stG M (rb M L)) (rb M L 0))) &&
nthd (M.next (stG M (rb M L)) (rb M L 0)) i)
/-! ## Layout and Turing step -/
def cellBit (s h : Bool) (q : List Bool) : Nat -> Bool
| 0 => s
| 1 => h
| i + 2 => h && nthd q i
def encC (tape : Nat -> Bool) (pos : Nat) (q : List Bool) : Conf :=
fun j b => cellBit (tape j) (Nat.beq j pos) q b
def tapeN (tape : Nat -> Bool) (pos : Nat) (q : List Bool) : Nat -> Bool :=
fun j => sel (Nat.beq j pos) (M.write q (tape pos)) (tape j)
def posN (tape : Nat -> Bool) (pos : Nat) (q : List Bool) : Nat :=
selN (M.right q (tape pos)) (pos + 1) (pos - 1)
def qN (tape : Nat -> Bool) (pos : Nat) (q : List Bool) : List Bool := M.next q (tape pos)
/-! ## List access -/
theorem nthd_app_l : forall (A B : List Bool) (i : Nat), i < A.length -> nthd (A ++ B) i = nthd A i := by
intro A
induction A with
| nil => intro B i h; exact False.elim (Nat.not_lt_zero i h)
| cons a A ih =>
intro B i h
cases i with
| zero => rfl
| succ i => exact ih B i (Nat.lt_of_succ_lt_succ h)
theorem nthd_app_r : forall (A B : List Bool) (i : Nat), nthd (A ++ B) (A.length + i) = nthd B i := by
intro A
induction A with
| nil => intro B i; exact congrArg (nthd B) (Nat.zero_add i)
| cons a A ih =>
intro B i
show nthd (a :: (A ++ B)) (A.length + 1 + i) = nthd B i
rw [Nat.add_right_comm]
exact ih B i
theorem nthd_read (f : Nat -> Bool) : forall (n s i : Nat), i < n -> nthd (read f s n) i = f (s + i) := by
intro n
induction n with
| zero => intro s i h; exact False.elim (Nat.not_lt_zero i h)
| succ n ih =>
intro s i h
cases i with
| zero => rfl
| succ i =>
exact (ih (s + 1) i (Nat.lt_of_succ_lt_succ h)).trans (congrArg f (Nat.succ_add s i))
theorem accL (c : Conf) (k W j i : Nat) (hi : i < k) : nthd (nbC c k W j) i = c (leftIdx W j) i := by
show nthd (read (c (leftIdx W j)) 0 k ++ (read (c j) 0 k ++ read (c (j + 1)) 0 k)) i = _
rw [nthd_app_l _ _ i (by rw [length_read]; exact hi), nthd_read _ k 0 i hi, Nat.zero_add]
theorem accC (c : Conf) (k W j i : Nat) (hi : i < k) : nthd (nbC c k W j) (k + i) = c j i := by
show nthd (read (c (leftIdx W j)) 0 k ++ (read (c j) 0 k ++ read (c (j + 1)) 0 k)) (k + i) = _
have h := nthd_app_r (read (c (leftIdx W j)) 0 k) (read (c j) 0 k ++ read (c (j + 1)) 0 k) i
rw [length_read] at h
rw [h, nthd_app_l _ _ i ((length_read _ k 0).symm ▸ hi), nthd_read _ k 0 i hi, Nat.zero_add]
theorem accR (c : Conf) (k W j i : Nat) (hi : i < k) : nthd (nbC c k W j) (k + (k + i)) = c (j + 1) i := by
show nthd (read (c (leftIdx W j)) 0 k ++ (read (c j) 0 k ++ read (c (j + 1)) 0 k)) (k + (k + i)) = _
have h1 := nthd_app_r (read (c (leftIdx W j)) 0 k) (read (c j) 0 k ++ read (c (j + 1)) 0 k) (k + i)
have h2 := nthd_app_r (read (c j) 0 k) (read (c (j + 1)) 0 k) i
rw [length_read] at h1 h2
rw [h1, h2, nthd_read _ k 0 i hi, Nat.zero_add]
/-! ## The head substitution -/
theorem stq (tape : Nat -> Bool) (pos : Nat) (q : List Bool) (hq : q.length = M.m)
(f : List Bool -> Bool -> Bool) (x : Nat) :
(Nat.beq x pos && f (read (fun i => encC tape pos q x (i + 2)) 0 M.m) (tape x)) =
(Nat.beq x pos && f q (tape pos)) := by
show (Nat.beq x pos && f (read (fun i => Nat.beq x pos && nthd q i) 0 M.m) (tape x)) = _
cases h : Nat.beq x pos with
| false => rfl
| true =>
have e : x = pos := Found.Pnp2.beq_eq x pos h
have r : read (fun i => true && nthd q i) 0 M.m = q := by
rw [← hq]; exact read_nthd q
rw [r, e]
theorem selq (tape : Nat -> Bool) (pos : Nat) (q : List Bool) (hq : q.length = M.m)
(f : List Bool -> Bool -> Bool) (x : Nat) (y : Bool) :
sel (Nat.beq x pos) (f (read (fun i => encC tape pos q x (i + 2)) 0 M.m) (tape x)) y =
sel (Nat.beq x pos) (f q (tape pos)) y := by
show sel (Nat.beq x pos) (f (read (fun i => Nat.beq x pos && nthd q i) 0 M.m) (tape x)) y = _
cases h : Nat.beq x pos with
| false => rfl
| true =>
have e : x = pos := Found.Pnp2.beq_eq x pos h
have r : read (fun i => true && nthd q i) 0 M.m = q := by
rw [← hq]; exact read_nthd q
rw [r, e]
/-! ## Accessors on the layout -/
def nbE (tape : Nat -> Bool) (pos : Nat) (q : List Bool) (W j : Nat) : List Bool :=
nbC (encC tape pos q) (M.m + 2) W j
theorem lt0 : 0 < M.m + 2 := Nat.zero_lt_succ _
theorem lt1 : 1 < M.m + 2 := Nat.succ_lt_succ (Nat.zero_lt_succ _)
theorem ltS (i : Nat) (h : i < M.m) : i + 2 < M.m + 2 := Nat.succ_lt_succ (Nat.succ_lt_succ h)
theorem stL (tape : Nat -> Bool) (pos : Nat) (q : List Bool) (W j : Nat) :
stG M (lb (nbE M tape pos q W j)) = read (fun i => encC tape pos q (leftIdx W j) (i + 2)) 0 M.m :=
read_ext_k _ _ M.m (fun i hi => accL (encC tape pos q) (M.m + 2) W j (i + 2) (ltS M i hi))
theorem stC (tape : Nat -> Bool) (pos : Nat) (q : List Bool) (W j : Nat) :
stG M (cb M (nbE M tape pos q W j)) = read (fun i => encC tape pos q j (i + 2)) 0 M.m :=
read_ext_k _ _ M.m (fun i hi => accC (encC tape pos q) (M.m + 2) W j (i + 2) (ltS M i hi))
theorem stR (tape : Nat -> Bool) (pos : Nat) (q : List Bool) (W j : Nat) :
stG M (rb M (nbE M tape pos q W j)) = read (fun i => encC tape pos q (j + 1) (i + 2)) 0 M.m :=
read_ext_k _ _ M.m (fun i hi => accR (encC tape pos q) (M.m + 2) W j (i + 2) (ltS M i hi))
/-! ## Bool and position facts -/
theorem g1 (a c : Bool) : ((a && true) || (c && !true)) = a := by cases a <;> cases c <;> rfl
theorem g2 (a c : Bool) : ((a && false) || (c && !false)) = c := by cases a <;> cases c <;> rfl
theorem g3 (a c y : Bool) : ((a && (true && y)) || (c && (!true && y))) = (a && y) := by
cases a <;> cases c <;> cases y <;> rfl
theorem g4 (a c y : Bool) : ((a && (false && y)) || (c && (!false && y))) = (c && y) := by
cases a <;> cases c <;> cases y <;> rfl
theorem and_assoc3 (a b c : Bool) : ((a && b) && c) = (a && (b && c)) := by cases a <;> rfl
theorem beqW (W p : Nat) (h : p + 1 < W) : Nat.beq W (p + 1) = false :=
Found.Pnp2.beq_false_of_ne W (p + 1) (fun e => Nat.ne_of_lt h e.symm)
/-- The head bit after a step: it comes from the left cell moving right or the right cell
moving left. -/
theorem head_eq (W p j : Nat) (hW : p + 1 < W) (R : Bool) :
((Nat.beq (leftIdx W j) (p + 1) && R) || (Nat.beq (j + 1) (p + 1) && !R)) =
Nat.beq j (selN R (p + 1 + 1) (p + 1 - 1)) := by
cases R with
| true =>
rw [g1]
cases j with
| zero => exact beqW W p hW
| succ j => rfl
| false => rw [g2]; rfl
theorem head_eq2 (W p j : Nat) (hW : p + 1 < W) (R y : Bool) :
((Nat.beq (leftIdx W j) (p + 1) && (R && y)) || (Nat.beq (j + 1) (p + 1) && (!R && y))) =
(Nat.beq j (selN R (p + 1 + 1) (p + 1 - 1)) && y) := by
cases R with
| true =>
rw [g3]
cases j with
| zero => exact congrArg (fun t => t && y) (beqW W p hW)
| succ j => rfl
| false => rw [g4]; rfl
/-! ## One step -/
theorem step_inside (tape : Nat -> Bool) (p : Nat) (q : List Bool) (hq : q.length = M.m)
(W : Nat) (hW : p + 2 < W) (j : Nat) (hj : j < W) (b : Nat) :
stepW (rule M) (M.m + 2) W (encC tape (p + 1) q) j b =
encC (tapeN M tape (p + 1) q) (posN M tape (p + 1) q) (qN M tape (p + 1) q) j b := by
have hW1 : p + 1 < W := Nat.lt_of_succ_lt hW
show gate (Nat.beq j W) (rule M (nbE M tape (p + 1) q W j) b) = _
rw [Found.Pnp2.beq_false_of_ne j W (Nat.ne_of_lt hj)]
have l0 : lb (nbE M tape (p + 1) q W j) 0 = tape (leftIdx W j) := accL (encC tape (p + 1) q) (M.m + 2) W j 0 (lt0 M)
have l1 : lb (nbE M tape (p + 1) q W j) 1 = Nat.beq (leftIdx W j) (p + 1) := accL (encC tape (p + 1) q) (M.m + 2) W j 1 (lt1 M)
have c0 : cb M (nbE M tape (p + 1) q W j) 0 = tape j := accC (encC tape (p + 1) q) (M.m + 2) W j 0 (lt0 M)
have c1 : cb M (nbE M tape (p + 1) q W j) 1 = Nat.beq j (p + 1) := accC (encC tape (p + 1) q) (M.m + 2) W j 1 (lt1 M)
have r0 : rb M (nbE M tape (p + 1) q W j) 0 = tape (j + 1) := accR (encC tape (p + 1) q) (M.m + 2) W j 0 (lt0 M)
have r1 : rb M (nbE M tape (p + 1) q W j) 1 = Nat.beq (j + 1) (p + 1) := accR (encC tape (p + 1) q) (M.m + 2) W j 1 (lt1 M)
match b with
| 0 =>
show sel (cb M (nbE M tape (p + 1) q W j) 1)
(M.write (stG M (cb M (nbE M tape (p + 1) q W j))) (cb M (nbE M tape (p + 1) q W j) 0))
(cb M (nbE M tape (p + 1) q W j) 0) = sel (Nat.beq j (p + 1)) (M.write q (tape (p + 1))) (tape j)
rw [c1, stC, c0]
exact selq M tape (p + 1) q hq M.write j (tape j)
| 1 =>
show ((lb (nbE M tape (p + 1) q W j) 1 && M.right (stG M (lb (nbE M tape (p + 1) q W j))) (lb (nbE M tape (p + 1) q W j) 0)) ||
(rb M (nbE M tape (p + 1) q W j) 1 && !(M.right (stG M (rb M (nbE M tape (p + 1) q W j))) (rb M (nbE M tape (p + 1) q W j) 0)))) =
Nat.beq j (selN (M.right q (tape (p + 1))) (p + 1 + 1) (p + 1 - 1))
rw [l1, stL, l0, r1, stR, r0]
have hL : (Nat.beq (leftIdx W j) (p + 1) &&
M.right (read (fun i => encC tape (p + 1) q (leftIdx W j) (i + 2)) 0 M.m) (tape (leftIdx W j))) =
(Nat.beq (leftIdx W j) (p + 1) && M.right q (tape (p + 1))) :=
stq M tape (p + 1) q hq M.right (leftIdx W j)
have hR : (Nat.beq (j + 1) (p + 1) &&
!(M.right (read (fun i => encC tape (p + 1) q (j + 1) (i + 2)) 0 M.m) (tape (j + 1)))) =
(Nat.beq (j + 1) (p + 1) && !(M.right q (tape (p + 1)))) :=
stq M tape (p + 1) q hq (fun a s => !(M.right a s)) (j + 1)
rw [hL, hR]
exact head_eq W p j hW1 _
| i + 2 =>
show (((lb (nbE M tape (p + 1) q W j) 1 && M.right (stG M (lb (nbE M tape (p + 1) q W j))) (lb (nbE M tape (p + 1) q W j) 0)) &&
nthd (M.next (stG M (lb (nbE M tape (p + 1) q W j))) (lb (nbE M tape (p + 1) q W j) 0)) i) ||
((rb M (nbE M tape (p + 1) q W j) 1 && !(M.right (stG M (rb M (nbE M tape (p + 1) q W j))) (rb M (nbE M tape (p + 1) q W j) 0))) &&
nthd (M.next (stG M (rb M (nbE M tape (p + 1) q W j))) (rb M (nbE M tape (p + 1) q W j) 0)) i)) =
(Nat.beq j (selN (M.right q (tape (p + 1))) (p + 1 + 1) (p + 1 - 1)) && nthd (M.next q (tape (p + 1))) i)
rw [l1, stL, l0, r1, stR, r0, and_assoc3, and_assoc3]
have hL : (Nat.beq (leftIdx W j) (p + 1) &&
(M.right (read (fun i => encC tape (p + 1) q (leftIdx W j) (i + 2)) 0 M.m) (tape (leftIdx W j)) &&
nthd (M.next (read (fun i => encC tape (p + 1) q (leftIdx W j) (i + 2)) 0 M.m) (tape (leftIdx W j))) i)) =
(Nat.beq (leftIdx W j) (p + 1) && (M.right q (tape (p + 1)) && nthd (M.next q (tape (p + 1))) i)) :=
stq M tape (p + 1) q hq (fun a s => M.right a s && nthd (M.next a s) i) (leftIdx W j)
have hR : (Nat.beq (j + 1) (p + 1) &&
(!(M.right (read (fun i => encC tape (p + 1) q (j + 1) (i + 2)) 0 M.m) (tape (j + 1))) &&
nthd (M.next (read (fun i => encC tape (p + 1) q (j + 1) (i + 2)) 0 M.m) (tape (j + 1))) i)) =
(Nat.beq (j + 1) (p + 1) && (!(M.right q (tape (p + 1))) && nthd (M.next q (tape (p + 1))) i)) :=
stq M tape (p + 1) q hq (fun a s => !(M.right a s) && nthd (M.next a s) i) (j + 1)
rw [hL, hR]
exact head_eq2 W p j hW1 _ _
/-- At the boundary cell W the local machine writes false, and so does the layout of the
next configuration when the head stays inside and the tape is false at W. -/
theorem step_boundary (tape : Nat -> Bool) (p : Nat) (q : List Bool) (W : Nat) (hW : p + 2 < W)
(htW : tape W = false) (b : Nat) :
stepW (rule M) (M.m + 2) W (encC tape (p + 1) q) W b =
encC (tapeN M tape (p + 1) q) (posN M tape (p + 1) q) (qN M tape (p + 1) q) W b := by
have hW1 : p + 1 < W := Nat.lt_of_succ_lt hW
have hpos : Nat.beq W (posN M tape (p + 1) q) = false := by
show Nat.beq W (selN (M.right q (tape (p + 1))) (p + 1 + 1) (p + 1 - 1)) = false
cases M.right q (tape (p + 1)) with
| true => exact Found.Pnp2.beq_false_of_ne W (p + 2) (fun e => Nat.ne_of_lt hW e.symm)
| false => exact Found.Pnp2.beq_false_of_ne W p (fun e => Nat.ne_of_lt (Nat.lt_of_succ_lt hW1) e.symm)
show gate (Nat.beq W W) _ = cellBit (sel (Nat.beq W (p + 1)) (M.write q (tape (p + 1))) (tape W))
(Nat.beq W (posN M tape (p + 1) q)) (qN M tape (p + 1) q) b
rw [beq_refl W, beqW W p hW1, hpos]
match b with
| 0 => exact htW.symm
| 1 => rfl
| _ + 2 => rfl
end Found.Pnp12
#print axioms Found.Pnp12.nthd_app_l
#print axioms Found.Pnp12.nthd_app_r
#print axioms Found.Pnp12.nthd_read
#print axioms Found.Pnp12.accL
#print axioms Found.Pnp12.accC
#print axioms Found.Pnp12.accR
#print axioms Found.Pnp12.stq
#print axioms Found.Pnp12.selq
#print axioms Found.Pnp12.stL
#print axioms Found.Pnp12.stC
#print axioms Found.Pnp12.stR
#print axioms Found.Pnp12.g1
#print axioms Found.Pnp12.g2
#print axioms Found.Pnp12.g3
#print axioms Found.Pnp12.g4
#print axioms Found.Pnp12.and_assoc3
#print axioms Found.Pnp12.beqW
#print axioms Found.Pnp12.head_eq
#print axioms Found.Pnp12.head_eq2
#print axioms Found.Pnp12.step_inside
#print axioms Found.Pnp12.step_boundary
#print axioms: 21 theorems, no axioms.
While the head stays inside the width, the run of the local machine from the layout of a start configuration is, at every time \(t\le T\) and cell \(j\le W\), the layout of the Turing configuration at time \(t\).
found/Pnp13.lean
/-
T330 piece 13. found/Pnp13 -- a Turing machine run as a local machine run (many steps).
Configurations (tape, pos, q) and the Turing run. Proved: if the start has a false
boundary cell W and an m-bit state, next keeps states m bits long, and the head stays
at some cell p + 1 with p + 2 < W at every time before T, then for every t <= T the
width-W local machine (Pnp10, rule of Pnp12) run from the layout of the start is the
layout of the Turing configuration at time t, at every cell j <= W.
It relativizes (a tool; PNP_MAP.md). Written with ASCII forms only.
CIC dependence: none.
-/
import found.Pnp12
namespace Found.Pnp13
open Found.Pnp9 (lt_succ_cases)
open Found.Pnp10 (Conf nbC stepW runW gate beq_refl nbC_ext)
open Found.Pnp12
structure TC where
tape : Nat -> Bool
pos : Nat
q : List Bool
variable (M : TM)
def tmStep (c : TC) : TC := TC.mk (tapeN M c.tape c.pos c.q) (posN M c.tape c.pos c.q) (qN M c.tape c.pos c.q)
def tmRun : Nat -> TC -> TC
| 0, c => c
| t + 1, c => tmStep M (tmRun t c)
def lay (c : TC) : Conf := encC c.tape c.pos c.q
/-- The head stays inside before time T. -/
def Inside (W T : Nat) (c : TC) : Prop :=
forall t, t < T -> Exists fun p => (tmRun M t c).pos = p + 1 /\ p + 2 < W
theorem preserve (W T : Nat) (c : TC) (hin : Inside M W T c) (htW : c.tape W = false)
(hq : c.q.length = M.m) (hnext : forall a s, (M.next a s).length = M.m) :
forall t, t < T + 1 -> (tmRun M t c).tape W = false /\ (tmRun M t c).q.length = M.m := by
intro t
induction t with
| zero => intro _; exact And.intro htW hq
| succ t ih =>
intro ht
have ht' : t < T := Nat.lt_of_succ_lt_succ ht
have ih' := ih (Nat.lt_succ_of_lt ht')
exact Exists.elim (hin t ht') fun p hp => by
apply And.intro
· show sel (Nat.beq W (tmRun M t c).pos) _ ((tmRun M t c).tape W) = false
rw [hp.1, beqW W p (Nat.lt_of_succ_lt hp.2)]
exact ih'.1
· exact hnext _ _
theorem stepW_ext (W : Nat) (x y : Conf) (h : forall c, c < W + 1 -> forall b, b < M.m + 2 -> x c b = y c b) :
forall j, j < W + 1 -> forall b, stepW (rule M) (M.m + 2) W x j b = stepW (rule M) (M.m + 2) W y j b := by
intro j hj b
exact Or.elim (lt_succ_cases j W hj)
(fun hjW => by
show gate (Nat.beq j W) (rule M (nbC x (M.m + 2) W j) b) = gate (Nat.beq j W) (rule M (nbC y (M.m + 2) W j) b)
rw [nbC_ext x y (M.m + 2) W j hjW h])
(fun e => by
rw [e]
show gate (Nat.beq W W) _ = gate (Nat.beq W W) _
rw [beq_refl W]
rfl)
/-- The local machine follows the Turing machine. -/
theorem simulate (W T : Nat) (c : TC) (hin : Inside M W T c) (htW : c.tape W = false)
(hq : c.q.length = M.m) (hnext : forall a s, (M.next a s).length = M.m) :
forall t, t < T + 1 -> forall j, j < W + 1 -> forall b,
runW (rule M) (M.m + 2) W t (lay c) j b = lay (tmRun M t c) j b := by
intro t
induction t with
| zero => intro _ j _ b; rfl
| succ t ih =>
intro ht j hj b
have ht' : t < T := Nat.lt_of_succ_lt_succ ht
have pr := preserve M W T c hin htW hq hnext t (Nat.lt_succ_of_lt ht')
show stepW (rule M) (M.m + 2) W (runW (rule M) (M.m + 2) W t (lay c)) j b = lay (tmStep M (tmRun M t c)) j b
rw [stepW_ext M W _ (lay (tmRun M t c)) (fun c' hc b' _ => ih (Nat.lt_succ_of_lt ht') c' hc b') j hj b]
exact Exists.elim (hin t ht') fun p hp => by
have e : lay (tmRun M t c) = encC (tmRun M t c).tape (p + 1) (tmRun M t c).q := by
show encC (tmRun M t c).tape (tmRun M t c).pos (tmRun M t c).q = _
rw [hp.1]
show _ = encC (tapeN M (tmRun M t c).tape (tmRun M t c).pos (tmRun M t c).q)
(posN M (tmRun M t c).tape (tmRun M t c).pos (tmRun M t c).q)
(qN M (tmRun M t c).tape (tmRun M t c).pos (tmRun M t c).q) j b
rw [e, hp.1]
exact Or.elim (lt_succ_cases j W hj)
(fun hjW => step_inside M _ p _ pr.2 W hp.2 j hjW b)
(fun ej => by rw [ej]; exact step_boundary M _ p _ W hp.2 pr.1 b)
end Found.Pnp13
#print axioms Found.Pnp13.preserve
#print axioms Found.Pnp13.stepW_ext
#print axioms Found.Pnp13.simulate
#print axioms: 3 theorems, no axioms.
Pinning the time-0 layer to a Turing layout (head, state, input symbols) and leaving the other symbols free, the tableau CNF is satisfiable exactly when some certificate tape makes the machine accept after \(T\) steps.
found/Pnp14.lean
/-
T330 piece 14. found/Pnp14 -- Cook-Levin for Turing machines.
A one-tape Turing machine (Pnp12) started with its head at cell p0 + 1 in state q0
(m bits). Unit clauses pin the time-0 layer to a Turing layout: on every cell j < W the
head bit is [j = p0 + 1] and the state bits are those of q0 at the head; the input
cells' symbols are pinned to the input; the other symbols are the certificate. With the
tableau CNF of the local rule (Pnp9-Pnp11), proved: the CNF is satisfiable iff some
tape (agreeing with the input, false at W) makes the machine hold true at tape cell 0
after T steps -- for a machine whose head stays inside the width before T on every tape.
Size: the tableau part has T W (m+2) 2^(3(m+2)) + (T+1)(m+2) clauses (Pnp9.tab_count);
the acceptance and pin clauses add 1 + W (m+1) + (input length) by construction.
It relativizes (a tool; PNP_MAP.md). Written with ASCII forms only.
CIC dependence: none.
-/
import found.Pnp13
namespace Found.Pnp14
open Found.Pnp7 (nthd)
open Found.Pnp9 (lt_succ_cases)
open Found.Pnp10 (Conf runW)
open Found.Pnp11 (PinsOK full cook_levin_local)
open Found.Pnp12
open Found.Pnp13
/-! ## Pins of a Turing layout -/
def cellPins (pos0 : Nat) (q0 : List Bool) (j : Nat) : Nat -> List (Nat × (Nat × Bool))
| 0 => [(j, (1, Nat.beq j pos0))]
| i + 1 => cellPins pos0 q0 j i ++ [(j, (i + 2, Nat.beq j pos0 && nthd q0 i))]
def allPins (pos0 : Nat) (q0 : List Bool) (m : Nat) : Nat -> List (Nat × (Nat × Bool))
| 0 => []
| n + 1 => allPins pos0 q0 m n ++ cellPins pos0 q0 n m
def inPins : List (Nat × Bool) -> List (Nat × (Nat × Bool))
| [] => []
| (j, v) :: r => (j, (0, v)) :: inPins r
def InputOK (tape : Nat -> Bool) : List (Nat × Bool) -> Prop
| [] => True
| (j, v) :: r => tape j = v /\ InputOK tape r
theorem pins_app (c : Conf) (B : List (Nat × (Nat × Bool))) : forall A,
(PinsOK c (A ++ B) -> PinsOK c A /\ PinsOK c B) /\ (PinsOK c A /\ PinsOK c B -> PinsOK c (A ++ B)) := by
intro A
induction A with
| nil => exact And.intro (fun h => And.intro True.intro h) (fun h => h.2)
| cons e A ih =>
cases e with
| mk j bv =>
cases bv with
| mk b v =>
exact And.intro
(fun h => And.intro (And.intro h.1 (ih.1 h.2).1) (ih.1 h.2).2)
(fun h => And.intro h.1.1 (ih.2 (And.intro h.1.2 h.2)))
theorem cellPins_iff (c : Conf) (pos0 : Nat) (q0 : List Bool) (j : Nat) : forall n,
(PinsOK c (cellPins pos0 q0 j n) ->
c j 1 = Nat.beq j pos0 /\ forall i, i < n -> c j (i + 2) = (Nat.beq j pos0 && nthd q0 i)) /\
(c j 1 = Nat.beq j pos0 /\ (forall i, i < n -> c j (i + 2) = (Nat.beq j pos0 && nthd q0 i)) ->
PinsOK c (cellPins pos0 q0 j n)) := by
intro n
induction n with
| zero =>
exact And.intro (fun h => And.intro h.1 (fun i hi => False.elim (Nat.not_lt_zero i hi)))
(fun h => And.intro h.1 True.intro)
| succ n ih =>
apply And.intro
· intro h
have hs := (pins_app c _ _).1 h
have h1 := ih.1 hs.1
exact And.intro h1.1 (fun i hi => Or.elim (lt_succ_cases i n hi) (h1.2 i)
(fun e => by rw [e]; exact hs.2.1))
· intro h
exact (pins_app c _ _).2 (And.intro
(ih.2 (And.intro h.1 (fun i hi => h.2 i (Nat.lt_succ_of_lt hi))))
(And.intro (h.2 n (Nat.lt_succ_self n)) True.intro))
theorem allPins_iff (c : Conf) (pos0 : Nat) (q0 : List Bool) (m : Nat) : forall n,
(PinsOK c (allPins pos0 q0 m n) -> forall j, j < n -> PinsOK c (cellPins pos0 q0 j m)) /\
((forall j, j < n -> PinsOK c (cellPins pos0 q0 j m)) -> PinsOK c (allPins pos0 q0 m n)) := by
intro n
induction n with
| zero => exact And.intro (fun _ j hj => False.elim (Nat.not_lt_zero j hj)) (fun _ => True.intro)
| succ n ih =>
apply And.intro
· intro h j hj
have hs := (pins_app c _ _).1 h
exact Or.elim (lt_succ_cases j n hj) (fun hl => ih.1 hs.1 j hl) (fun e => by rw [e]; exact hs.2)
· intro h
exact (pins_app c _ _).2 (And.intro (ih.2 (fun j hj => h j (Nat.lt_succ_of_lt hj)))
(h n (Nat.lt_succ_self n)))
theorem inPins_iff (c : Conf) : forall inp,
(PinsOK c (inPins inp) -> InputOK (fun j => c j 0) inp) /\
(InputOK (fun j => c j 0) inp -> PinsOK c (inPins inp)) := by
intro inp
induction inp with
| nil => exact And.intro (fun _ => True.intro) (fun _ => True.intro)
| cons e r ih =>
cases e with
| mk j v =>
exact And.intro (fun h => And.intro h.1 (ih.1 h.2)) (fun h => And.intro h.1 (ih.2 h.2))
/-! ## Runs depend only on cells <= W and bits < k -/
theorem runW_ext (M : TM) (W : Nat) (x y : Conf)
(h : forall c, c < W + 1 -> forall b, b < M.m + 2 -> x c b = y c b) :
forall t, forall c, c < W + 1 -> forall b, b < M.m + 2 ->
runW (rule M) (M.m + 2) W t x c b = runW (rule M) (M.m + 2) W t y c b := by
intro t
induction t with
| zero => exact h
| succ t ih => intro c hc b _; exact stepW_ext M W _ _ ih c hc b
/-! ## Cook-Levin for Turing machines -/
def pinsTM (M : TM) (W p0 : Nat) (q0 : List Bool) (inp : List (Nat × Bool)) : List (Nat × (Nat × Bool)) :=
allPins (p0 + 1) q0 M.m W ++ inPins inp
theorem cook_levin_tm (M : TM) (W T p0 : Nat) (q0 : List Bool) (hq0 : q0.length = M.m)
(hnext : forall a s, (M.next a s).length = M.m) (hp : p0 + 2 < W) (inp : List (Nat × Bool))
(hin : forall tape : Nat -> Bool, Inside M W T (TC.mk tape (p0 + 1) q0)) :
((Exists fun s => Found.Pnp8.cnfv s (full (rule M) (M.m + 2) W T (pinsTM M W p0 q0 inp)) = true) ->
Exists fun tape : Nat -> Bool => InputOK tape inp /\ tape W = false /\
(tmRun M T (TC.mk tape (p0 + 1) q0)).tape 0 = true) /\
((Exists fun tape : Nat -> Bool => InputOK tape inp /\ tape W = false /\
(tmRun M T (TC.mk tape (p0 + 1) q0)).tape 0 = true) ->
Exists fun s => Found.Pnp8.cnfv s (full (rule M) (M.m + 2) W T (pinsTM M W p0 q0 inp)) = true) := by
have hk : 0 < M.m + 2 := lt0 M
have hW1 : p0 + 1 < W := Nat.lt_of_succ_lt hp
have hbW : Nat.beq W (p0 + 1) = false := beqW W p0 hW1
apply And.intro
· intro h
exact Exists.elim ((cook_levin_local (rule M) (M.m + 2) W T hk _).1 h) fun c0 hc => by
have hpins := (pins_app c0 _ _).1 hc.2.1
let c : TC := TC.mk (fun j => c0 j 0) (p0 + 1) q0
have agree : forall j, j < W + 1 -> forall b, b < M.m + 2 -> c0 j b = lay c j b := by
intro j hj b hb
exact Or.elim (lt_succ_cases j W hj)
(fun hjW => by
have hcell := (cellPins_iff c0 (p0 + 1) q0 j M.m).1 ((allPins_iff c0 (p0 + 1) q0 M.m W).1 hpins.1 j hjW)
match b, hb with
| 0, _ => rfl
| 1, _ => exact hcell.1
| i + 2, hb' => exact hcell.2 i (Nat.lt_of_succ_lt_succ (Nat.lt_of_succ_lt_succ hb')))
(fun e => by
rw [e]
match b, hb with
| 0, _ => rfl
| 1, hb' => exact (hc.1 1 hb').trans hbW.symm
| i + 2, hb' =>
show c0 W (i + 2) = (Nat.beq W (p0 + 1) && nthd q0 i)
rw [hbW]; exact hc.1 (i + 2) hb')
have hrun := runW_ext M W c0 (lay c) agree T 0 (Nat.zero_lt_succ W) 0 hk
have hsim := simulate M W T c (hin _) (hc.1 0 hk) hq0 hnext T (Nat.lt_succ_self T) 0 (Nat.zero_lt_succ W) 0
exact Exists.intro (fun j => c0 j 0) (And.intro ((inPins_iff c0 inp).1 hpins.2)
(And.intro (hc.1 0 hk) ((hsim.symm.trans hrun.symm).trans hc.2.2)))
· intro h
exact Exists.elim h fun tape ht => by
let c : TC := TC.mk tape (p0 + 1) q0
apply (cook_levin_local (rule M) (M.m + 2) W T hk _).2
apply Exists.intro (lay c)
apply And.intro
· intro b hb
match b, hb with
| 0, _ => exact ht.2.1
| 1, _ => exact hbW
| i + 2, _ =>
show (Nat.beq W (p0 + 1) && nthd q0 i) = false
rw [hbW]; rfl
apply And.intro
· exact (pins_app (lay c) _ _).2 (And.intro
((allPins_iff (lay c) (p0 + 1) q0 M.m W).2 (fun j _ =>
(cellPins_iff (lay c) (p0 + 1) q0 j M.m).2 (And.intro rfl (fun i _ => rfl))))
((inPins_iff (lay c) inp).2 ht.1))
· exact (simulate M W T c (hin _) ht.2.1 hq0 hnext T (Nat.lt_succ_self T) 0
(Nat.zero_lt_succ W) 0).trans ht.2.2
end Found.Pnp14
#print axioms Found.Pnp14.pins_app
#print axioms Found.Pnp14.cellPins_iff
#print axioms Found.Pnp14.allPins_iff
#print axioms Found.Pnp14.inPins_iff
#print axioms Found.Pnp14.runW_ext
#print axioms Found.Pnp14.cook_levin_tm
#print axioms: 6 theorems, no axioms.
The laws of a machine model with output hold for the model whose programs are arbitrary maps, and there every language is in P; so no argument from those laws alone gives \(\mathrm {P}\ne \mathrm {NP}\).
found/Pnp15.lean
/-
T330 piece 15. found/Pnp15 -- the machine-model laws alone do not separate P from NP
(a barrier for the abstract chain, in the spirit of relativization).
The laws of a machine model with output (Pnp3.OM: budget monotonicity, output bounded
by budget plus input, sequential composition) hold for the "all-functions" model,
whose programs are arbitrary maps on bit strings that run as soon as the budget covers
the output length. In that model every language is in P, so P = NP there. Hence the
separation O0o cannot follow from the OM laws together with the tool results Pnp3-Pnp4
(which hold for every OM): a proof of P != NP must use properties of a concrete model
(such as the Turing machine of Pnp12-Pnp14) that this model lacks.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp4
namespace Found.Pnp15
open Found.Pnp3
open Found.Pnp4
def runB : Bool -> List Bool -> Option (List Bool)
| true, y => some y
| false, _ => none
def runAll (p : List Bool -> List Bool) (t : Nat) (x : List Bool) : Option (List Bool) :=
runB (Nat.ble (p x).length (t + x.length)) (p x)
theorem runAll_inv (p : List Bool -> List Bool) (t : Nat) (x y : List Bool)
(h : runAll p t x = some y) : y = p x /\ (p x).length <= t + x.length := by
have h' : runB (Nat.ble (p x).length (t + x.length)) (p x) = some y := h
cases hb : Nat.ble (p x).length (t + x.length) with
| false => rw [hb] at h'; cases h'
| true =>
rw [hb] at h'
exact And.intro (Option.some.inj h').symm (Nat.le_of_ble_eq_true hb)
theorem runAll_of (p : List Bool -> List Bool) (t : Nat) (x : List Bool)
(h : (p x).length <= t + x.length) : runAll p t x = some (p x) := by
show runB (Nat.ble (p x).length (t + x.length)) (p x) = some (p x)
rw [Nat.ble_eq_true_of_le h]
rfl
theorem budget_comm (s t n : Nat) : s + (t + n) = t + s + n := by
rw [Nat.add_comm s (t + n), Nat.add_assoc t n s, Nat.add_comm n s, ← Nat.add_assoc]
/-- The all-functions machine model satisfies the OM laws. -/
def OMall : OM where
Prog := List Bool -> List Bool
run := runAll
seq := fun p q x => q (p x)
fuel_mono := fun p t t' x y h ht => by
have hi := runAll_inv p t x y h
rw [hi.1]
exact runAll_of p t' x (Nat.le_trans hi.2 (Nat.add_le_add_right ht x.length))
out_le := fun p t x y h => by
have hi := runAll_inv p t x y h
rw [hi.1]
exact hi.2
seq_run := fun p q t s x y z h1 h2 => by
have i1 := runAll_inv p t x y h1
have i2 := runAll_inv q s y z h2
rw [i2.1, i1.1]
rw [i1.1] at i2
apply runAll_of (fun x => q (p x)) (t + s) x
have hb : (q (p x)).length <= s + (t + x.length) :=
Nat.le_trans i2.2 (Nat.add_le_add_left i1.2 s)
rw [budget_comm] at hb
exact hb
/-- In the all-functions model every language is in P. -/
theorem all_in_P (L : List Bool -> Bool) : InPo OMall L :=
Exists.intro (fun x => [L x]) (Exists.intro (PE.c 1) (fun x =>
runAll_of (fun x => [L x]) 1 x (Nat.le_add_right 1 x.length)))
/-- Hence P = NP in that model: no language separates them. -/
theorem no_separation :
(Exists fun L : List Bool -> Bool => InNPo OMall L /\ (InPo OMall L -> False)) -> False :=
fun h => Exists.elim h fun L hL => hL.2 (all_in_P L)
end Found.Pnp15
#print axioms Found.Pnp15.runAll_inv
#print axioms Found.Pnp15.runAll_of
#print axioms Found.Pnp15.budget_comm
#print axioms Found.Pnp15.all_in_P
#print axioms Found.Pnp15.no_separation
#print axioms: 5 theorems, no axioms.
Left of a cut cell \(j_0<W\), a run depends only on its own start and the history of the cut cell: two runs that agree there agree on every cell left of the cut up to time \(T\).
found/Pnp16.lean
/-
T330 piece 16. found/Pnp16 -- cut and paste for local machines (the base of crossing
sequence arguments; a tool, not used for the main conclusion).
For the width-W local machine (Pnp10): the cells left of a cut j0 < W evolve from their
own start and the history of the cut cell j0 only. If two runs agree on the cells left of
the cut and on the boundary cell at time 0, and the cut cell has the same bits at every
time before T, then the two runs agree on every cell left of the cut at every time <= T.
Locality is a property that an oracle machine lacks (an oracle query reads the whole
tape in one step), so arguments built on it do not relativize; how far they reach is
recorded in PNP_MAP.md. Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp10
import found.Pnp2
namespace Found.Pnp16
open Found.Pnp1 (read)
open Found.Pnp9 (leftIdx lt_succ_cases)
open Found.Pnp10 (Conf nbC stepW runW gate beq_refl read_ext_k)
theorem run_boundary (delta : List Bool -> Nat -> Bool) (k W t : Nat) (x : Conf) (b : Nat) :
runW delta k W (t + 1) x W b = false := by
show gate (Nat.beq W W) _ = false
rw [beq_refl W]
rfl
theorem nbC_eq (x y : Conf) (k W j : Nat)
(hl : forall b, b < k -> x (leftIdx W j) b = y (leftIdx W j) b)
(hc : forall b, b < k -> x j b = y j b)
(hr : forall b, b < k -> x (j + 1) b = y (j + 1) b) : nbC x k W j = nbC y k W j := by
show read (x (leftIdx W j)) 0 k ++ (read (x j) 0 k ++ read (x (j + 1)) 0 k) =
read (y (leftIdx W j)) 0 k ++ (read (y j) 0 k ++ read (y (j + 1)) 0 k)
rw [read_ext_k _ _ k hl, read_ext_k _ _ k hc, read_ext_k _ _ k hr]
/-- Cut and paste: the left of the cut depends only on its start and the cut's history. -/
theorem cut (delta : List Bool -> Nat -> Bool) (k W T j0 : Nat) (hj0 : j0 < W) (x y : Conf)
(h0 : forall j, j < j0 -> forall b, b < k -> x j b = y j b)
(hW : forall b, b < k -> x W b = y W b)
(hcol : forall t, t < T -> forall b, b < k -> runW delta k W t x j0 b = runW delta k W t y j0 b) :
forall t, t < T + 1 -> forall j, j < j0 -> forall b, b < k ->
runW delta k W t x j b = runW delta k W t y j b := by
intro t
induction t with
| zero => intro _; exact h0
| succ t ih =>
intro ht j hj b _
have ht' : t < T := Nat.lt_of_succ_lt_succ ht
have ih' := ih (Nat.lt_succ_of_lt ht')
have hjW : j < W := Nat.lt_trans hj hj0
-- the left neighbour: the boundary cell W for j = 0, cell j - 1 otherwise
have hl : forall b', b' < k -> runW delta k W t x (leftIdx W j) b' = runW delta k W t y (leftIdx W j) b' := by
intro b' hb'
cases j with
| zero =>
cases t with
| zero => exact hW b' hb'
| succ t => exact (run_boundary delta k W t x b').trans (run_boundary delta k W t y b').symm
| succ j => exact ih' j (Nat.lt_of_succ_lt hj) b' hb'
have hr : forall b', b' < k -> runW delta k W t x (j + 1) b' = runW delta k W t y (j + 1) b' := by
intro b' hb'
exact Or.elim (lt_succ_cases (j + 1) j0 (Nat.succ_lt_succ hj))
(fun h => ih' (j + 1) h b' hb')
(fun e => by rw [e]; exact hcol t ht' b' hb')
show gate (Nat.beq j W) (delta (nbC (runW delta k W t x) k W j) b) =
gate (Nat.beq j W) (delta (nbC (runW delta k W t y) k W j) b)
rw [nbC_eq _ _ k W j hl (ih' j hj) hr]
end Found.Pnp16
#print axioms Found.Pnp16.run_boundary
#print axioms Found.Pnp16.nbC_eq
#print axioms Found.Pnp16.cut
#print axioms: 3 theorems, no axioms.
The \(2^m\) tables of length \(m\) are pairwise different, so by the pigeonhole any list of fewer than \(2^m\) tables misses one, found by search.
found/Pnp17.lean
/-
T330 piece 17. found/Pnp17 -- a missing truth table (the counting step of the
Shannon-Riordan lower bound; exact-size side, a tool only).
Truth tables are bit lists. Proved:
* the 2^m bit lists of length m (Pnp8.allBits) are pairwise different;
* pigeonhole for lists of bit lists: a list of pairwise different tables that all occur
in A is not longer than A (by erasing one occurrence at a time);
* hence any list A of fewer than 2^m tables misses some table of length m, and the
missing table is found by a search (no classical logic).
With an enumeration of the functions computed by small formulas this gives functions
that need large formulas (O2). The property "needs many gates" is large, so by the
natural-proofs barrier this is not used for the main conclusion (PNP_MAP.md).
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp9
namespace Found.Pnp17
open Found.Pnp8 (beqB beqB_refl beqB_eq and_l and_r allBits mapC InL allBits_len InL_mapC_inv InL_app_inv)
open Found.Pnp9 (pow2 length_allBits)
def eqL : List Bool -> List Bool -> Bool
| [], [] => true
| [], _ :: _ => false
| _ :: _, [] => false
| a :: as, b :: bs => beqB a b && eqL as bs
theorem eqL_refl : forall p : List Bool, eqL p p = true := by
intro p
induction p with
| nil => rfl
| cons a p ih =>
show (beqB a a && eqL p p) = true
rw [beqB_refl, ih]; rfl
theorem eqL_eq : forall p q : List Bool, eqL p q = true -> p = q := by
intro p
induction p with
| nil => intro q h; cases q with
| nil => rfl
| cons _ _ => exact Bool.noConfusion h
| cons a p ih => intro q h; cases q with
| nil => exact Bool.noConfusion h
| cons b q =>
have h' : (beqB a b && eqL p q) = true := h
rw [beqB_eq a b (and_l h'), ih q (and_r h')]
def memL (p : List Bool) : List (List Bool) -> Bool
| [] => false
| q :: A => eqL p q || memL p A
def pickE : Bool -> List (List Bool) -> List (List Bool) -> List (List Bool)
| true, a, _ => a
| false, _, b => b
def eraseL (x : List Bool) : List (List Bool) -> List (List Bool)
| [] => []
| q :: A => pickE (eqL x q) A (q :: eraseL x A)
theorem or_l {x y : Bool} (h : (x || y) = false) : x = false := by
cases x with
| false => rfl
| true => exact Bool.noConfusion h
theorem erase_len (x : List Bool) : forall A, memL x A = true -> (eraseL x A).length + 1 = A.length := by
intro A
induction A with
| nil => intro h; exact Bool.noConfusion h
| cons q A ih =>
intro h
have h' : (eqL x q || memL x A) = true := h
show (pickE (eqL x q) A (q :: eraseL x A)).length + 1 = A.length + 1
cases hq : eqL x q with
| true => rfl
| false => rw [hq] at h'; exact congrArg (fun t => t + 1) (ih h')
theorem mem_erase (x y : List Bool) (hxy : eqL x y = false) : forall A, memL y A = true -> memL y (eraseL x A) = true := by
intro A
induction A with
| nil => intro h; exact h
| cons q A ih =>
intro h
have h' : (eqL y q || memL y A) = true := h
show memL y (pickE (eqL x q) A (q :: eraseL x A)) = true
cases hq : eqL x q with
| true =>
cases hy : eqL y q with
| true =>
have e : x = y := (eqL_eq x q hq).trans (eqL_eq y q hy).symm
rw [e, eqL_refl] at hxy
exact Bool.noConfusion hxy
| false => rw [hy] at h'; exact h'
| false =>
show (eqL y q || memL y (eraseL x A)) = true
cases hy : eqL y q with
| true => rfl
| false => rw [hy] at h'; exact ih h'
theorem mem_of_InL (p : List Bool) : forall A, InL p A -> memL p A = true := by
intro A
induction A with
| nil => intro h; exact False.elim h
| cons q A ih =>
intro h
exact Or.elim h (fun e => by show (eqL p q || memL p A) = true; rw [e, eqL_refl]; rfl)
(fun h' => by
show (eqL p q || memL p A) = true
rw [ih h']
cases eqL p q <;> rfl)
/-! ## Pairwise different lists and the pigeonhole -/
def Nodup : List (List Bool) -> Prop
| [] => True
| x :: L => (forall y, InL y L -> eqL x y = false) /\ Nodup L
theorem pigeon : forall (L A : List (List Bool)), Nodup L ->
(forall y, InL y L -> memL y A = true) -> L.length <= A.length := by
intro L
induction L with
| nil => intro A _ _; exact Nat.zero_le _
| cons x L ih =>
intro A hn h
have hx : memL x A = true := h x (Or.inl rfl)
have hL : L.length <= (eraseL x A).length :=
ih (eraseL x A) hn.2 (fun y hy => mem_erase x y (hn.1 y hy) A (h y (Or.inr hy)))
have h2 : L.length + 1 <= (eraseL x A).length + 1 := Nat.succ_le_succ hL
rw [erase_len x A hx] at h2
exact h2
theorem nodup_mapC (b : Bool) : forall A, Nodup A -> Nodup (mapC b A) := by
intro A
induction A with
| nil => intro _; exact True.intro
| cons x A ih =>
intro h
apply And.intro
· intro y hy
exact Exists.elim (InL_mapC_inv b y A hy) fun z hz => by
rw [hz.1]
show (beqB b b && eqL x z) = false
rw [beqB_refl]
exact h.1 z hz.2
· exact ih h.2
theorem InL_app_l' (p : List Bool) (B : List (List Bool)) : forall A, InL p A -> InL p (A ++ B) :=
Found.Pnp8.InL_app_l p B
theorem nodup_app (B : List (List Bool)) (hB : Nodup B) : forall A, Nodup A ->
(forall x, InL x A -> forall y, InL y B -> eqL x y = false) -> Nodup (A ++ B) := by
intro A
induction A with
| nil => intro _ _; exact hB
| cons x A ih =>
intro hA hd
apply And.intro
· intro y hy
exact Or.elim (InL_app_inv y B A hy) (hA.1 y) (hd x (Or.inl rfl) y)
· exact ih hA.2 (fun x' hx' y hy => hd x' (Or.inr hx') y hy)
theorem nodup_allBits : forall m, Nodup (allBits m) := by
intro m
induction m with
| zero => exact And.intro (fun _ h => False.elim h) True.intro
| succ m ih =>
show Nodup (mapC false (allBits m) ++ mapC true (allBits m))
apply nodup_app _ (nodup_mapC true _ ih) _ (nodup_mapC false _ ih)
intro x hx y hy
exact Exists.elim (InL_mapC_inv false x _ hx) fun u hu =>
Exists.elim (InL_mapC_inv true y _ hy) fun v hv => by
rw [hu.1, hv.1]; rfl
/-! ## A missing table -/
def pickO : Bool -> Option (List Bool) -> Option (List Bool) -> Option (List Bool)
| true, a, _ => a
| false, _, b => b
def findMiss (A : List (List Bool)) : List (List Bool) -> Option (List Bool)
| [] => none
| p :: ps => pickO (memL p A) (findMiss A ps) (some p)
theorem findMiss_none (A : List (List Bool)) : forall L, findMiss A L = none ->
forall y, InL y L -> memL y A = true := by
intro L
induction L with
| nil => intro _ y hy; exact False.elim hy
| cons p L ih =>
intro h y hy
have h' : pickO (memL p A) (findMiss A L) (some p) = none := h
cases hp : memL p A with
| false => rw [hp] at h'; cases h'
| true =>
rw [hp] at h'
exact Or.elim hy (fun e => by rw [e]; exact hp) (fun hy' => ih h' y hy')
theorem findMiss_some (A : List (List Bool)) (q : List Bool) : forall L, findMiss A L = some q ->
InL q L /\ memL q A = false := by
intro L
induction L with
| nil => intro h; cases h
| cons p L ih =>
intro h
have h' : pickO (memL p A) (findMiss A L) (some p) = some q := h
cases hp : memL p A with
| true =>
rw [hp] at h'
have r := ih h'
exact And.intro (Or.inr r.1) r.2
| false =>
rw [hp] at h'
have e : p = q := Option.some.inj h'
rw [← e]
exact And.intro (Or.inl rfl) hp
/-- Fewer than 2^m tables miss some table of length m. -/
theorem missing (m : Nat) (A : List (List Bool)) (h : A.length < pow2 m) :
Exists fun p : List Bool => p.length = m /\ memL p A = false := by
cases hf : findMiss A (allBits m) with
| none =>
have hle := pigeon (allBits m) A (nodup_allBits m) (findMiss_none A (allBits m) hf)
rw [length_allBits] at hle
exact False.elim (Nat.lt_irrefl _ (Nat.lt_of_lt_of_le h hle))
| some p =>
have r := findMiss_some A p (allBits m) hf
exact Exists.intro p (And.intro (allBits_len m p r.1) r.2)
end Found.Pnp17
#print axioms Found.Pnp17.eqL_refl
#print axioms Found.Pnp17.eqL_eq
#print axioms Found.Pnp17.or_l
#print axioms Found.Pnp17.erase_len
#print axioms Found.Pnp17.mem_erase
#print axioms Found.Pnp17.mem_of_InL
#print axioms Found.Pnp17.pigeon
#print axioms Found.Pnp17.nodup_mapC
#print axioms Found.Pnp17.InL_app_l'
#print axioms Found.Pnp17.nodup_app
#print axioms Found.Pnp17.nodup_allBits
#print axioms Found.Pnp17.findMiss_none
#print axioms Found.Pnp17.findMiss_some
#print axioms Found.Pnp17.missing
#print axioms: 14 theorems, no axioms.
For a language checked by a Turing machine verifier, \(x\) is in the language exactly when the CNF of \(x\) (tableau, layout and input pins) is satisfiable, and its clause count is an explicit polynomial of \(|x|\).
found/Pnp18.lean
/-
T330 piece 18. found/Pnp18 -- the Cook-Levin reduction map and its size (CL1, CL2).
A language decided by a one-tape Turing machine verifier (Pnp12-14): x is in L iff some
certificate tape, agreeing with the input written at fixed cells and false at the
boundary W(|x|), makes the machine (head at p0 + 1, state q0) hold true at cell 0 after
T(|x|) steps. The reduction map sends x to the tableau CNF with the layout and input
pins (Pnp14). Proved:
* CL1: x is in L iff the CNF of x is satisfiable (from Pnp14.cook_levin_tm);
* CL2: the CNF of x has exactly
T (W (k 2^(3k))) + (T + 1) k + (W (m + 1) + |input| + 1) clauses, k = m + 2,
and when T, W are polynomials of |x| (Pnp3.PE) and the input pins are |x| many, this
is a polynomial of |x| (an explicit PE).
Open (CL3): the map is computed in polynomial time by a Turing machine.
It relativizes (a tool). Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp14
import found.Pnp3
namespace Found.Pnp18
open Found.Pnp3 (PE pe)
open Found.Pnp9 (tab tab_count pow2 length_app)
open Found.Pnp11 (full accCl pinCl)
open Found.Pnp12 (TM rule)
open Found.Pnp13 (TC tmRun Inside)
open Found.Pnp14 (allPins cellPins inPins InputOK pinsTM cook_levin_tm)
/-- The reduction map: x to the tableau CNF with layout and input pins. -/
def red (M : TM) (W T p0 : Nat) (q0 : List Bool) (inp : List (Nat × Bool)) :=
full (rule M) (M.m + 2) W T (pinsTM M W p0 q0 inp)
/-- CL1: membership iff satisfiability. -/
theorem cl1 (M : TM) (W T p0 : Nat) (q0 : List Bool) (hq0 : q0.length = M.m)
(hnext : forall a s, (M.next a s).length = M.m) (hp : p0 + 2 < W) (inp : List (Nat × Bool))
(hin : forall tape : Nat -> Bool, Inside M W T (TC.mk tape (p0 + 1) q0))
(Lx : Prop)
(hL : Lx <-> Exists fun tape : Nat -> Bool => InputOK tape inp /\ tape W = false /\
(tmRun M T (TC.mk tape (p0 + 1) q0)).tape 0 = true) :
(Lx -> Exists fun s => Found.Pnp8.cnfv s (red M W T p0 q0 inp) = true) /\
((Exists fun s => Found.Pnp8.cnfv s (red M W T p0 q0 inp) = true) -> Lx) :=
And.intro (fun h => (cook_levin_tm M W T p0 q0 hq0 hnext hp inp hin).2 (hL.mp h))
(fun h => hL.mpr ((cook_levin_tm M W T p0 q0 hq0 hnext hp inp hin).1 h))
/-! ## CL2: size -/
theorem length_cellPins (pos0 : Nat) (q0 : List Bool) (j : Nat) : forall i, (cellPins pos0 q0 j i).length = i + 1 := by
intro i
induction i with
| zero => rfl
| succ i ih =>
show (cellPins pos0 q0 j i ++ _).length = i + 1 + 1
rw [length_app, ih]; rfl
theorem length_allPins (pos0 : Nat) (q0 : List Bool) (m : Nat) : forall n, (allPins pos0 q0 m n).length = n * (m + 1) := by
intro n
induction n with
| zero => exact (Nat.zero_mul _).symm
| succ n ih =>
show (allPins pos0 q0 m n ++ cellPins pos0 q0 n m).length = (n + 1) * (m + 1)
rw [length_app, ih, length_cellPins, Nat.succ_mul]
theorem length_inPins : forall inp : List (Nat × Bool), (inPins inp).length = inp.length := by
intro inp
induction inp with
| nil => rfl
| cons e r ih =>
cases e with
| mk j v => exact congrArg (fun t => t + 1) ih
theorem length_pinCl : forall ps : List (Nat × (Nat × Bool)), (pinCl ps).length = ps.length := by
intro ps
induction ps with
| nil => rfl
| cons e r ih =>
cases e with
| mk j bv => cases bv with
| mk b v => exact congrArg (fun t => t + 1) ih
/-- CL2: the exact clause count of the reduction's CNF. -/
theorem cl2 (M : TM) (W T p0 : Nat) (q0 : List Bool) (inp : List (Nat × Bool)) :
(red M W T p0 q0 inp).length =
T * (W * ((M.m + 2) * pow2 ((M.m + 2) + ((M.m + 2) + (M.m + 2))))) + (T + 1) * (M.m + 2) +
(W * (M.m + 1) + inp.length + 1) := by
show (tab (rule M) (M.m + 2) W T ++ (accCl T :: pinCl (pinsTM M W p0 q0 inp))).length = _
rw [length_app, tab_count]
show _ + ((pinCl (allPins (p0 + 1) q0 M.m W ++ inPins inp)).length + 1) = _
rw [length_pinCl, length_app, length_allPins, length_inPins]
/-- The count as a polynomial of the input length n, when T = eT(n), W = eW(n) and there
are n input pins. -/
def sizePE (M : TM) (eT eW : PE) : PE :=
.add (.add (.mul eT (.mul eW (.c ((M.m + 2) * pow2 ((M.m + 2) + ((M.m + 2) + (M.m + 2)))))))
(.mul (.add eT (.c 1)) (.c (M.m + 2))))
(.add (.add (.mul eW (.c (M.m + 1))) .x) (.c 1))
theorem cl2_poly (M : TM) (eT eW : PE) (p0 : Nat) (q0 : List Bool) (inp : List (Nat × Bool)) (n : Nat)
(hn : inp.length = n) :
(red M (pe eW n) (pe eT n) p0 q0 inp).length = pe (sizePE M eT eW) n := by
rw [cl2, hn]
rfl
end Found.Pnp18
#print axioms Found.Pnp18.cl1
#print axioms Found.Pnp18.length_cellPins
#print axioms Found.Pnp18.length_allPins
#print axioms Found.Pnp18.length_inPins
#print axioms Found.Pnp18.length_pinCl
#print axioms Found.Pnp18.cl2
#print axioms Found.Pnp18.cl2_poly
#print axioms: 7 theorems, no axioms.
CNFs are written as bit strings with numbers in unary; decoding inverts encoding, so the encoding is injective and the bit language SAT holds of the encoding of a CNF exactly when the CNF is satisfiable.
found/Pnp19.lean
/-
T330 piece 19. found/Pnp19 -- bit encoding of CNFs (Cook-Levin CL3a).
A CNF over variables (t, (j, b)) is written as a bit string: numbers in unary
(n ones then a zero), a literal as its polarity bit and its three numbers, a clause as
its literals each after a 1 and a closing 0, a CNF as its clauses each after a 1 and a
closing 0. Proved: decoding inverts encoding (so the encoding is injective), and hence
the bit language SAT = { y : y encodes a satisfiable CNF } contains enc (red x) exactly
when x is in the verifier's language (with Pnp18.cl1). The unary numbers keep the size
polynomial for the reduction (variables are below T, W, k). Written with ASCII forms
only. CIC dependence: none.
-/
import found.Pnp9
namespace Found.Pnp19
open Found.Pnp8 (cnfv)
open Found.Pnp9 (V)
/-! ## Encoding -/
def un : Nat -> List Bool
| 0 => [false]
| n + 1 => true :: un n
def encLit (l : Bool × V) : List Bool := l.1 :: (un l.2.1 ++ (un l.2.2.1 ++ un l.2.2.2))
def encCl : List (Bool × V) -> List Bool
| [] => [false]
| l :: c => true :: (encLit l ++ encCl c)
def encCNF : List (List (Bool × V)) -> List Bool
| [] => [false]
| c :: f => true :: (encCl c ++ encCNF f)
/-! ## Decoding -/
def bindN (o : Option (Nat × List Bool)) : Option (Nat × List Bool) :=
match o with
| some (n, r) => some (n + 1, r)
| none => none
def decUn : List Bool -> Option (Nat × List Bool)
| [] => none
| false :: r => some (0, r)
| true :: r => bindN (decUn r)
theorem app_assoc' {α : Type} (A B C : List α) : (A ++ B) ++ C = A ++ (B ++ C) := by
induction A with
| nil => rfl
| cons a A ih => exact congrArg (fun t => a :: t) ih
theorem decUn_un (r : List Bool) : forall n, decUn (un n ++ r) = some (n, r) := by
intro n
induction n with
| zero => rfl
| succ n ih =>
show bindN (decUn (un n ++ r)) = some (n + 1, r)
rw [ih]; rfl
def decLit (y : List Bool) : Option ((Bool × V) × List Bool) :=
match y with
| [] => none
| p :: r =>
match decUn r with
| none => none
| some (t, r1) =>
match decUn r1 with
| none => none
| some (j, r2) =>
match decUn r2 with
| none => none
| some (b, r3) => some ((p, (t, (j, b))), r3)
theorem decLit_enc (l : Bool × V) (r : List Bool) : decLit (encLit l ++ r) = some (l, r) := by
cases l with
| mk p v =>
cases v with
| mk t jb =>
cases jb with
| mk j b =>
show decLit (p :: ((un t ++ (un j ++ un b)) ++ r)) = some ((p, (t, (j, b))), r)
rw [app_assoc', app_assoc']
show (match decUn (un t ++ (un j ++ (un b ++ r))) with
| none => none
| some (t', r1) =>
match decUn r1 with
| none => none
| some (j', r2) =>
match decUn r2 with
| none => none
| some (b', r3) => some ((p, (t', (j', b'))), r3)) = _
rw [decUn_un]
show (match decUn (un j ++ (un b ++ r)) with
| none => none
| some (j', r2) =>
match decUn r2 with
| none => none
| some (b', r3) => some ((p, (t, (j', b'))), r3)) = _
rw [decUn_un]
show (match decUn (un b ++ r) with
| none => none
| some (b', r3) => some ((p, (t, (j, b'))), r3)) = _
rw [decUn_un]
try rfl
/-- Decoding with fuel (the fuel bounds the number of items; the input length is enough). -/
def decCl : Nat -> List Bool -> Option (List (Bool × V) × List Bool)
| 0, _ => none
| _ + 1, [] => none
| _ + 1, false :: r => some ([], r)
| fuel + 1, true :: r =>
match decLit r with
| none => none
| some (l, r1) =>
match decCl fuel r1 with
| none => none
| some (c, r2) => some (l :: c, r2)
def decCNF : Nat -> List Bool -> Option (List (List (Bool × V)) × List Bool)
| 0, _ => none
| _ + 1, [] => none
| _ + 1, false :: r => some ([], r)
| fuel + 1, true :: r =>
match decCl fuel r with
| none => none
| some (c, r1) =>
match decCNF fuel r1 with
| none => none
| some (f, r2) => some (c :: f, r2)
theorem decCl_enc (r : List Bool) : forall (c : List (Bool × V)) (fuel : Nat), c.length < fuel ->
decCl fuel (encCl c ++ r) = some (c, r) := by
intro c
induction c with
| nil => intro fuel h; cases fuel with
| zero => exact False.elim (Nat.not_lt_zero _ h)
| succ fuel => rfl
| cons l c ih => intro fuel h; cases fuel with
| zero => exact False.elim (Nat.not_lt_zero _ h)
| succ fuel =>
show (match decLit ((encLit l ++ encCl c) ++ r) with
| none => none
| some (l, r1) =>
match decCl fuel r1 with
| none => none
| some (c, r2) => some (l :: c, r2)) = some (l :: c, r)
rw [app_assoc', decLit_enc]
show (match decCl fuel (encCl c ++ r) with
| none => none
| some (c', r2) => some (l :: c', r2)) = some (l :: c, r)
rw [ih fuel (Nat.lt_of_succ_lt_succ h)]
try rfl
theorem len_encCl : forall c : List (Bool × V), c.length < (encCl c).length := by
intro c
induction c with
| nil => exact Nat.zero_lt_succ 0
| cons l c ih =>
show c.length + 1 < (encLit l ++ encCl c).length + 1
apply Nat.succ_lt_succ
rw [Found.Pnp9.length_app]
exact Nat.lt_of_lt_of_le ih (Nat.le_add_left _ _)
theorem decCNF_enc (r : List Bool) : forall (f : List (List (Bool × V))) (fuel : Nat),
(encCNF f).length <= fuel -> decCNF fuel (encCNF f ++ r) = some (f, r) := by
intro f
induction f with
| nil => intro fuel h; cases fuel with
| zero => exact False.elim (Nat.not_succ_le_zero _ h)
| succ fuel => rfl
| cons c f ih => intro fuel h; cases fuel with
| zero => exact False.elim (Nat.not_succ_le_zero _ h)
| succ fuel =>
have h' : (encCl c ++ encCNF f).length <= fuel := Nat.le_of_succ_le_succ h
rw [Found.Pnp9.length_app] at h'
show (match decCl fuel ((encCl c ++ encCNF f) ++ r) with
| none => none
| some (c, r1) =>
match decCNF fuel r1 with
| none => none
| some (f, r2) => some (c :: f, r2)) = some (c :: f, r)
rw [app_assoc', decCl_enc (encCNF f ++ r) c fuel
(Nat.lt_of_lt_of_le (len_encCl c) (Nat.le_trans (Nat.le_add_right _ _) h'))]
show (match decCNF fuel (encCNF f ++ r) with
| none => none
| some (f', r2) => some (c :: f', r2)) = some (c :: f, r)
rw [ih fuel (Nat.le_trans (Nat.le_add_left _ _) h')]
try rfl
/-- Decoding the whole string. -/
def decode (y : List Bool) : Option (List (List (Bool × V)) × List Bool) := decCNF y.length y
theorem app_nil' {α : Type} : forall A : List α, A ++ [] = A := by
intro A
induction A with
| nil => rfl
| cons a A ih => exact congrArg (fun t => a :: t) ih
theorem decode_enc (f : List (List (Bool × V))) : decode (encCNF f) = some (f, []) := by
have h := decCNF_enc [] f (encCNF f).length (Nat.le_refl _)
rw [app_nil'] at h
exact h
/-- The encoding is injective. -/
theorem enc_inj (f g : List (List (Bool × V))) (h : encCNF f = encCNF g) : f = g := by
have hf := decode_enc f
rw [h, decode_enc g] at hf
exact (congrArg Prod.fst (Option.some.inj hf)).symm
/-! ## The bit language SAT -/
def SatB (y : List Bool) : Prop :=
Exists fun f : List (List (Bool × V)) => encCNF f = y /\ Exists fun s : V -> Bool => cnfv s f = true
theorem satB_enc (f : List (List (Bool × V))) :
(SatB (encCNF f) -> Exists fun s : V -> Bool => cnfv s f = true) /\
((Exists fun s : V -> Bool => cnfv s f = true) -> SatB (encCNF f)) :=
And.intro (fun h => Exists.elim h fun g hg => by rw [enc_inj g f hg.1] at hg; exact hg.2)
(fun h => Exists.intro f (And.intro rfl h))
end Found.Pnp19
#print axioms Found.Pnp19.app_assoc'
#print axioms Found.Pnp19.decUn_un
#print axioms Found.Pnp19.decLit_enc
#print axioms Found.Pnp19.decCl_enc
#print axioms Found.Pnp19.len_encCl
#print axioms Found.Pnp19.decCNF_enc
#print axioms Found.Pnp19.app_nil'
#print axioms Found.Pnp19.decode_enc
#print axioms Found.Pnp19.enc_inj
#print axioms Found.Pnp19.satB_enc
#print axioms: 10 theorems, no axioms.
Registers hold bit lists; push, pop with a three-way jump, and jump. Runs respect pointwise equal registers, and the program MOVE moves one register onto another reversed in exactly \(3|r|+1\) steps.
found/Pnp20.lean
/-
T330 piece 20. found/Pnp20 -- a register machine for computing the reduction (CL3b, 1).
To show that the Cook-Levin map x |-> enc (red x) is computed in polynomial time, it is
first programmed on a machine that is easy to program: registers holding bit lists,
with three instructions -- push a bit on a register, pop a register and jump on what was
there (empty / 0 / 1), and jump. A program halts when the counter leaves the program.
(The register machine is simulated by a Turing machine with polynomial overhead: open,
CL3c.) Proved here:
* register updates and the step function, with extensionality (pointwise registers,
no funext): runs from pointwise equal configurations stay pointwise equal;
* the first program, MOVE r s: moves register r onto s reversed, in exactly
3 |r| + 1 steps, leaving every other register unchanged.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp2
namespace Found.Pnp20
inductive Ins where
| push : Nat -> Bool -> Ins
| pop : Nat -> Nat -> Nat -> Nat -> Ins -- register, target if empty, if 0, if 1
| jmp : Nat -> Ins
structure Cfg where
pc : Nat
regs : Nat -> List Bool
def selR : Bool -> List Bool -> List Bool -> List Bool
| true, v, _ => v
| false, _, w => w
/-- Register r set to v. -/
def upd (regs : Nat -> List Bool) (r : Nat) (v : List Bool) : Nat -> List Bool :=
fun i => selR (Nat.beq i r) v (regs i)
def nthI : List Ins -> Nat -> Option Ins
| [], _ => none
| i :: _, 0 => some i
| _ :: p, n + 1 => nthI p n
def popTo (c : Cfg) (r e z o : Nat) : List Bool -> Cfg
| [] => Cfg.mk e c.regs
| false :: t => Cfg.mk z (upd c.regs r t)
| true :: t => Cfg.mk o (upd c.regs r t)
def exec (c : Cfg) : Option Ins -> Cfg
| none => c
| some (.push r b) => Cfg.mk (c.pc + 1) (upd c.regs r (b :: c.regs r))
| some (.pop r e z o) => popTo c r e z o (c.regs r)
| some (.jmp l) => Cfg.mk l c.regs
def step (p : List Ins) (c : Cfg) : Cfg := exec c (nthI p c.pc)
def run (p : List Ins) : Nat -> Cfg -> Cfg
| 0, c => c
| n + 1, c => run p n (step p c)
/-- Pointwise equal configurations. -/
def Eqv (c d : Cfg) : Prop := c.pc = d.pc /\ forall i, c.regs i = d.regs i
theorem upd_eqv (x y : Nat -> List Bool) (h : forall i, x i = y i) (r : Nat) (v : List Bool) :
forall i, upd x r v i = upd y r v i := by
intro i
show selR (Nat.beq i r) v (x i) = selR (Nat.beq i r) v (y i)
rw [h i]
theorem step_eqv (p : List Ins) (c d : Cfg) (h : Eqv c d) : Eqv (step p c) (step p d) := by
cases c with
| mk pc x =>
cases d with
| mk pd y =>
have e : pc = pd := h.1
subst e
show Eqv (exec (Cfg.mk pc x) (nthI p pc)) (exec (Cfg.mk pc y) (nthI p pc))
cases nthI p pc with
| none => exact h
| some ins =>
cases ins with
| push r b =>
refine And.intro rfl (fun i => ?_)
show upd x r (b :: x r) i = upd y r (b :: y r) i
have hxy : x r = y r := h.2 r
rw [hxy]; exact upd_eqv x y h.2 r _ i
| pop r e z o =>
show Eqv (popTo (Cfg.mk pc x) r e z o (x r)) (popTo (Cfg.mk pc y) r e z o (y r))
have hxy : x r = y r := h.2 r
rw [hxy]
cases y r with
| nil => exact And.intro rfl h.2
| cons b t =>
cases b with
| false => exact And.intro rfl (upd_eqv x y h.2 r t)
| true => exact And.intro rfl (upd_eqv x y h.2 r t)
| jmp l => exact And.intro rfl h.2
theorem run_eqv (p : List Ins) : forall n (c d : Cfg), Eqv c d -> Eqv (run p n c) (run p n d) := by
intro n
induction n with
| zero => intro c d h; exact h
| succ n ih => intro c d h; exact ih _ _ (step_eqv p c d h)
theorem run_add (p : List Ins) : forall m n (c : Cfg), run p (m + n) c = run p n (run p m c) := by
intro m
induction m with
| zero => intro n c; rw [Nat.zero_add]; rfl
| succ m ih =>
intro n c
rw [Nat.add_right_comm m 1 n]
exact ih n (step p c)
/-! ## MOVE r s -/
/-- MOVE r s: pop r; on 0 push 0 on s and loop; on 1 push 1 on s and loop; on empty stop. -/
def move (r s : Nat) : List Ins :=
[.pop r 5 1 3, .push s false, .jmp 0, .push s true, .jmp 0]
def revApp : List Bool -> List Bool -> List Bool
| [], m => m
| b :: l, m => revApp l (b :: m)
theorem beq_refl' : forall n : Nat, Nat.beq n n = true := by
intro n
induction n with
| zero => rfl
| succ n ih => exact ih
theorem upd_same (x : Nat -> List Bool) (r : Nat) (v : List Bool) : upd x r v r = v := by
show selR (Nat.beq r r) v (x r) = v
rw [beq_refl']; rfl
theorem upd_other (x : Nat -> List Bool) (r i : Nat) (v : List Bool) (h : Nat.beq i r = false) : upd x r v i = x i := by
show selR (Nat.beq i r) v (x i) = x i
rw [h]; rfl
/-- MOVE r s from the start, with r holding l and s holding m: after 3 |l| + 1 steps the
counter is 5 (halted), r is empty, s holds the reverse of l in front of m, and every other
register is as it was. -/
theorem move_run (r s : Nat) (hrs : Nat.beq r s = false) : forall (l m : List Bool) (x : Nat -> List Bool),
x r = l -> x s = m ->
(run (move r s) (3 * l.length + 1) (Cfg.mk 0 x)).pc = 5 /\
(run (move r s) (3 * l.length + 1) (Cfg.mk 0 x)).regs r = [] /\
(run (move r s) (3 * l.length + 1) (Cfg.mk 0 x)).regs s = revApp l m /\
(forall i, Nat.beq i r = false -> Nat.beq i s = false ->
(run (move r s) (3 * l.length + 1) (Cfg.mk 0 x)).regs i = x i) := by
have hsr : Nat.beq s r = false :=
Found.Pnp2.beq_false_of_ne s r (fun e => by rw [e, beq_refl'] at hrs; exact Bool.noConfusion hrs)
intro l
induction l with
| nil =>
intro m x hr hs
have e : step (move r s) (Cfg.mk 0 x) = Cfg.mk 5 x := by
show popTo (Cfg.mk 0 x) r 5 1 3 (x r) = Cfg.mk 5 x
rw [hr]; rfl
have e1 : run (move r s) (3 * ([] : List Bool).length + 1) (Cfg.mk 0 x) = Cfg.mk 5 x := by
show run (move r s) 0 (step (move r s) (Cfg.mk 0 x)) = Cfg.mk 5 x
rw [e]; rfl
rw [e1]
exact And.intro rfl (And.intro hr (And.intro hs (fun i _ _ => rfl)))
| cons b l ih =>
intro m x hr hs
-- three steps: pop, push, jump
have e : run (move r s) 3 (Cfg.mk 0 x) = Cfg.mk 0 (upd (upd x r l) s (b :: upd x r l s)) := by
show run (move r s) 2 (popTo (Cfg.mk 0 x) r 5 1 3 (x r)) = _
rw [hr]
cases b with
| false => rfl
| true => rfl
have hlen : 3 * (b :: l).length + 1 = 3 + (3 * l.length + 1) := by
show 3 * (l.length + 1) + 1 = 3 + (3 * l.length + 1)
rw [Nat.left_distrib, Nat.mul_one, Nat.add_comm (3 * l.length) 3, Nat.add_assoc]
have hx2r : upd (upd x r l) s (b :: upd x r l s) r = l := by
rw [upd_other (upd x r l) s r _ hrs]; exact upd_same x r l
have hx2s : upd (upd x r l) s (b :: upd x r l s) s = b :: m := by
rw [upd_same, upd_other x r s l hsr, hs]
have hrec := ih (b :: m) _ hx2r hx2s
rw [hlen, run_add, e]
refine And.intro hrec.1 (And.intro hrec.2.1 (And.intro hrec.2.2.1 (fun i hir his => ?_)))
rw [hrec.2.2.2 i hir his, upd_other (upd x r l) s i _ his]
exact upd_other x r i l hir
end Found.Pnp20
#print axioms Found.Pnp20.upd_eqv
#print axioms Found.Pnp20.step_eqv
#print axioms Found.Pnp20.run_eqv
#print axioms Found.Pnp20.run_add
#print axioms Found.Pnp20.beq_refl'
#print axioms Found.Pnp20.upd_same
#print axioms Found.Pnp20.upd_other
#print axioms Found.Pnp20.move_run
#print axioms: 8 theorems, no axioms.
A program placed at an offset inside a larger program keeps its behaviour there, and fragments placed one after another compose by adding their step counts; MOVE and EMIT are such fragments.
found/Pnp21.lean
/-
T330 piece 21. found/Pnp21 -- code fragments: programs placed at an offset inside a
larger program (CL3b, 2).
A fragment P sits at offset o in a program prog when every instruction of P, with its
jump targets moved by o, is in prog at o + i. A fragment theorem then holds in every
program that contains the fragment, and fragments placed one after another compose by
adding their step counts (Pnp20.run_add) -- no "first exit time" is needed. Proved:
* sequencing two fragment runs;
* the MOVE fragment at any offset (3 |l| + 1 steps, from o to o + 5);
* the EMIT fragment (push a fixed bit list, one step per bit).
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp20
namespace Found.Pnp21
open Found.Pnp20
def shift (o : Nat) : Ins -> Ins
| .push r b => .push r b
| .pop r e z w => .pop r (o + e) (o + z) (o + w)
| .jmp l => .jmp (o + l)
/-- P sits at offset o in prog. -/
def Frag (prog : List Ins) (o : Nat) (P : List Ins) : Prop :=
forall i ins, nthI P i = some ins -> nthI prog (o + i) = some (shift o ins)
/-- A fragment run: from pc o with registers x, n steps reach pc o' with registers y
(pointwise). -/
def Runs (prog : List Ins) (n o : Nat) (x : Nat -> List Bool) (o' : Nat) (y : Nat -> List Bool) : Prop :=
(run prog n (Cfg.mk o x)).pc = o' /\ forall i, (run prog n (Cfg.mk o x)).regs i = y i
theorem runs_seq (prog : List Ins) (n1 n2 o1 o2 o3 : Nat) (x y z : Nat -> List Bool)
(h1 : Runs prog n1 o1 x o2 y) (h2 : Runs prog n2 o2 y o3 z) : Runs prog (n1 + n2) o1 x o3 z := by
have e : Eqv (run prog n1 (Cfg.mk o1 x)) (Cfg.mk o2 y) := And.intro h1.1 h1.2
have e2 := run_eqv prog n2 _ _ e
show (run prog (n1 + n2) (Cfg.mk o1 x)).pc = o3 /\ forall i, (run prog (n1 + n2) (Cfg.mk o1 x)).regs i = z i
rw [run_add]
exact And.intro (e2.1.trans h2.1) (fun i => (e2.2 i).trans (h2.2 i))
/-- Runs depend on the registers only pointwise. -/
theorem runs_congr (prog : List Ins) (n o o' : Nat) (x x' y : Nat -> List Bool) (hx : forall i, x i = x' i)
(h : Runs prog n o x' o' y) : Runs prog n o x o' y := by
have e := run_eqv prog n (Cfg.mk o x) (Cfg.mk o x') (And.intro rfl hx)
exact And.intro (e.1.trans h.1) (fun i => (e.2 i).trans (h.2 i))
/-! ## MOVE at an offset -/
theorem frag_move_run (prog : List Ins) (o r s : Nat) (hf : Frag prog o (move r s)) (hrs : Nat.beq r s = false) :
forall (l m : List Bool) (x : Nat -> List Bool), x r = l -> x s = m ->
(run prog (3 * l.length + 1) (Cfg.mk o x)).pc = o + 5 /\
(run prog (3 * l.length + 1) (Cfg.mk o x)).regs r = [] /\
(run prog (3 * l.length + 1) (Cfg.mk o x)).regs s = revApp l m /\
(forall i, Nat.beq i r = false -> Nat.beq i s = false ->
(run prog (3 * l.length + 1) (Cfg.mk o x)).regs i = x i) := by
have hsr : Nat.beq s r = false :=
Found.Pnp2.beq_false_of_ne s r (fun e => by rw [e, beq_refl'] at hrs; exact Bool.noConfusion hrs)
have i0 : nthI prog o = some (.pop r (o + 5) (o + 1) (o + 3)) := hf 0 _ rfl
have i1 : nthI prog (o + 1) = some (.push s false) := hf 1 _ rfl
have i2 : nthI prog (o + 2) = some (.jmp (o + 0)) := hf 2 _ rfl
have i3 : nthI prog (o + 3) = some (.push s true) := hf 3 _ rfl
have i4 : nthI prog (o + 4) = some (.jmp (o + 0)) := hf 4 _ rfl
intro l
induction l with
| nil =>
intro m x hr hs
have e1 : run prog (3 * ([] : List Bool).length + 1) (Cfg.mk o x) = Cfg.mk (o + 5) x := by
show run prog 0 (exec (Cfg.mk o x) (nthI prog o)) = _
rw [i0]
show popTo (Cfg.mk o x) r (o + 5) (o + 1) (o + 3) (x r) = _
rw [hr]; rfl
rw [e1]
exact And.intro rfl (And.intro hr (And.intro hs (fun i _ _ => rfl)))
| cons b l ih =>
intro m x hr hs
have e : run prog 3 (Cfg.mk o x) = Cfg.mk o (upd (upd x r l) s (b :: upd x r l s)) := by
show run prog 2 (exec (Cfg.mk o x) (nthI prog o)) = _
rw [i0]
show run prog 2 (popTo (Cfg.mk o x) r (o + 5) (o + 1) (o + 3) (x r)) = _
rw [hr]
cases b with
| false =>
show run prog 1 (exec (Cfg.mk (o + 1) (upd x r l)) (nthI prog (o + 1))) = _
rw [i1]
show run prog 0 (exec (Cfg.mk (o + 1 + 1) (upd (upd x r l) s (false :: upd x r l s))) (nthI prog (o + 2))) = _
rw [i2]; rfl
| true =>
show run prog 1 (exec (Cfg.mk (o + 3) (upd x r l)) (nthI prog (o + 3))) = _
rw [i3]
show run prog 0 (exec (Cfg.mk (o + 3 + 1) (upd (upd x r l) s (true :: upd x r l s))) (nthI prog (o + 4))) = _
rw [i4]; rfl
have hlen : 3 * (b :: l).length + 1 = 3 + (3 * l.length + 1) := by
show 3 * (l.length + 1) + 1 = 3 + (3 * l.length + 1)
rw [Nat.left_distrib, Nat.mul_one, Nat.add_comm (3 * l.length) 3, Nat.add_assoc]
have hx2r : upd (upd x r l) s (b :: upd x r l s) r = l := by
rw [upd_other (upd x r l) s r _ hrs]; exact upd_same x r l
have hx2s : upd (upd x r l) s (b :: upd x r l s) s = b :: m := by
rw [upd_same, upd_other x r s l hsr, hs]
have hrec := ih (b :: m) _ hx2r hx2s
rw [hlen, run_add, e]
refine And.intro hrec.1 (And.intro hrec.2.1 (And.intro hrec.2.2.1 (fun i hir his => ?_)))
rw [hrec.2.2.2 i hir his, upd_other (upd x r l) s i _ his]
exact upd_other x r i l hir
/-! ## EMIT -/
def emit (r : Nat) : List Bool -> List Ins
| [] => []
| b :: w => .push r b :: emit r w
theorem emit_shift (r : Nat) : forall (w : List Bool) (i : Nat) (ins : Ins), nthI (emit r w) i = some ins ->
forall a c, shift a ins = shift c ins := by
intro w
induction w with
| nil => intro i ins h; cases i <;> cases h
| cons b w ih =>
intro i ins h a c
cases i with
| zero => cases h; rfl
| succ i => exact ih i ins h a c
theorem frag_emit_tail (prog : List Ins) (o r : Nat) (b : Bool) (w : List Bool)
(h : Frag prog o (emit r (b :: w))) : nthI prog o = some (.push r b) /\ Frag prog (o + 1) (emit r w) := by
apply And.intro (h 0 _ rfl)
intro i ins hi
have h2 := h (i + 1) ins hi
rw [← Nat.add_assoc] at h2
rw [Nat.add_right_comm o 1 i, h2, emit_shift r w i ins hi o (o + 1)]
/-- Pushing the bits of w onto r, in order. -/
def pushAll (x : Nat -> List Bool) (r : Nat) : List Bool -> Nat -> List Bool
| [], i => x i
| b :: w, i => pushAll (upd x r (b :: x r)) r w i
theorem frag_emit_run (prog : List Ins) (r : Nat) : forall (w : List Bool) (o : Nat) (x : Nat -> List Bool),
Frag prog o (emit r w) -> Runs prog w.length o x (o + w.length) (pushAll x r w) := by
intro w
induction w with
| nil => intro o x _; exact And.intro rfl (fun _ => rfl)
| cons b w ih =>
intro o x hf
have ht := frag_emit_tail prog o r b w hf
have h1 : Runs prog 1 o x (o + 1) (upd x r (b :: x r)) := by
show (run prog 0 (exec (Cfg.mk o x) (nthI prog o))).pc = o + 1 /\
forall i, (run prog 0 (exec (Cfg.mk o x) (nthI prog o))).regs i = upd x r (b :: x r) i
rw [ht.1]
exact And.intro rfl (fun _ => rfl)
have h2 := ih (o + 1) (upd x r (b :: x r)) ht.2
have h3 := runs_seq prog 1 w.length o (o + 1) (o + 1 + w.length) x _ _ h1 h2
rw [Nat.add_comm 1 w.length, Nat.add_assoc o 1 w.length, Nat.add_comm 1 w.length] at h3
exact h3
end Found.Pnp21
#print axioms Found.Pnp21.runs_seq
#print axioms Found.Pnp21.runs_congr
#print axioms Found.Pnp21.frag_move_run
#print axioms Found.Pnp21.emit_shift
#print axioms Found.Pnp21.frag_emit_tail
#print axioms Found.Pnp21.frag_emit_run
#print axioms: 6 theorems, no axioms.
A loop pops a counter register and runs its body once per popped bit; with bodies of at most \(B\) steps the loop takes at most \(|l|(B+2)+1\) steps.
found/Pnp22.lean
/-
T330 piece 22. found/Pnp22 -- the loop fragment (CL3b, 3).
LOOP over a counter register c: at o, pop c (empty: go to the exit; 0 or 1: go to the
body at o + 1); the body occupies o + 1 .. o + bl; at o + 1 + bl, jump back to o. The
body is any code that, from o + 1, runs nb x steps and reaches o + 1 + bl with registers
f x, leaving c unchanged. Proved: from o with c holding l, the loop runs
steps l x steps and exits with registers iter l x (the body applied once per element of
l, after each pop). Nested loops are loops whose body is a loop.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp21
namespace Found.Pnp22
open Found.Pnp20
open Found.Pnp21
variable (c : Nat) (nb : (Nat -> List Bool) -> Nat) (f : (Nat -> List Bool) -> (Nat -> List Bool))
/-- Steps of the loop. -/
def steps : List Bool -> (Nat -> List Bool) -> Nat
| [], _ => 1
| _ :: l, x => ((1 + nb (upd x c l)) + 1) + steps l (f (upd x c l))
/-- Registers after the loop. -/
def iter : List Bool -> (Nat -> List Bool) -> (Nat -> List Bool)
| [], x => x
| _ :: l, x => iter l (f (upd x c l))
theorem loop_run (prog : List Ins) (o bl ex : Nat)
(hpop : nthI prog o = some (.pop c ex (o + 1) (o + 1)))
(hjmp : nthI prog (o + 1 + bl) = some (.jmp o))
(hbody : forall x, Runs prog (nb x) (o + 1) x (o + 1 + bl) (f x))
(hkeep : forall x, f x c = x c) :
forall (l : List Bool) (x : Nat -> List Bool), x c = l ->
Runs prog (steps c nb f l x) o x ex (iter c f l x) := by
intro l
induction l with
| nil =>
intro x hx
show (run prog 0 (exec (Cfg.mk o x) (nthI prog o))).pc = ex /\
forall i, (run prog 0 (exec (Cfg.mk o x) (nthI prog o))).regs i = x i
rw [hpop]
show (popTo (Cfg.mk o x) c ex (o + 1) (o + 1) (x c)).pc = ex /\
forall i, (popTo (Cfg.mk o x) c ex (o + 1) (o + 1) (x c)).regs i = x i
rw [hx]
exact And.intro rfl (fun _ => rfl)
| cons b l ih =>
intro x hx
have h1 : Runs prog 1 o x (o + 1) (upd x c l) := by
show (run prog 0 (exec (Cfg.mk o x) (nthI prog o))).pc = o + 1 /\
forall i, (run prog 0 (exec (Cfg.mk o x) (nthI prog o))).regs i = upd x c l i
rw [hpop]
show (popTo (Cfg.mk o x) c ex (o + 1) (o + 1) (x c)).pc = o + 1 /\
forall i, (popTo (Cfg.mk o x) c ex (o + 1) (o + 1) (x c)).regs i = upd x c l i
rw [hx]
cases b with
| false => exact And.intro rfl (fun _ => rfl)
| true => exact And.intro rfl (fun _ => rfl)
have h2 := hbody (upd x c l)
have h3 : Runs prog 1 (o + 1 + bl) (f (upd x c l)) o (f (upd x c l)) := by
show (run prog 0 (exec (Cfg.mk (o + 1 + bl) (f (upd x c l))) (nthI prog (o + 1 + bl)))).pc = o /\
forall i, (run prog 0 (exec (Cfg.mk (o + 1 + bl) (f (upd x c l))) (nthI prog (o + 1 + bl)))).regs i = f (upd x c l) i
rw [hjmp]
exact And.intro rfl (fun _ => rfl)
have hc : f (upd x c l) c = l := (hkeep _).trans (upd_same x c l)
have h4 := ih (f (upd x c l)) hc
exact runs_seq prog _ _ o o ex x _ _
(runs_seq prog _ _ o (o + 1 + bl) o x _ _ (runs_seq prog _ _ o (o + 1) (o + 1 + bl) x _ _ h1 h2) h3) h4
/-- A step bound: bodies of at most B steps give at most |l| (B + 2) + 1 steps. -/
theorem steps_le (B : Nat) (hB : forall x, nb x <= B) :
forall (l : List Bool) (x : Nat -> List Bool), steps c nb f l x <= l.length * (B + 2) + 1 := by
intro l
induction l with
| nil =>
intro x
show 1 <= 0 * (B + 2) + 1
rw [Nat.zero_mul]
exact Nat.le_refl 1
| cons b l ih =>
intro x
show ((1 + nb (upd x c l)) + 1) + steps c nb f l (f (upd x c l)) <= (l.length + 1) * (B + 2) + 1
have h1 : (1 + nb (upd x c l)) + 1 <= B + 2 :=
Nat.le_trans (Nat.add_le_add_right (Nat.add_le_add_left (hB _) 1) 1)
(Nat.le_of_eq ((Nat.add_right_comm 1 B 1).trans (Nat.add_comm (1 + 1) B)))
have h2 := Nat.add_le_add h1 (ih (f (upd x c l)))
have e : (B + 2) + (l.length * (B + 2) + 1) = (l.length + 1) * (B + 2) + 1 := by
show (B + 2) + (l.length * (B + 2) + 1) = Nat.succ l.length * (B + 2) + 1
rw [Nat.succ_mul, Nat.add_comm (B + 2), Nat.add_right_comm]
exact Nat.le_trans h2 (Nat.le_of_eq e)
end Found.Pnp22
#print axioms Found.Pnp22.loop_run
#print axioms Found.Pnp22.steps_le
#print axioms: 2 theorems, no axioms.
With a counter register holding \(n\) ones, the fragment UNARY pushes the bits of the unary code of \(n\) onto the output register in \(7n+3\) steps and leaves the counter as it was.
found/Pnp23.lean
/-
T330 piece 23. found/Pnp23 -- emitting a counter in unary (CL3b, 4).
The output of the reduction is built by pushing the bits of enc (red x) in order onto
an output register (which then holds them reversed; one final MOVE turns it around).
Numbers are unary (Pnp19.un n = n ones then a zero), and the loop counters hold n ones.
The fragment UNARY A B out pushes the bits of un n, where A holds n ones, onto out, using
B as scratch, and leaves A as it was:
o : pop A -> exit o+4 / body o+1
o+1 : push out 1; o+2 : push B 1; o+3 : jump o
o+4 : push out 0; o+5 .. o+9 : MOVE B A; exit o+10.
Proved: it runs 3n + 1 + 1 + 3n + 1 steps and leaves out = revApp (un n) out, A = n ones,
B as it was (empty), other registers unchanged. Also: placing a shifted program after
another (Frag of an append), and revApp facts.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp22
import found.Pnp19
namespace Found.Pnp23
open Found.Pnp20
open Found.Pnp21
open Found.Pnp22 (steps iter loop_run)
/-! ## Instruction lists -/
def mapI (g : Ins -> Ins) : List Ins -> List Ins
| [] => []
| i :: p => g i :: mapI g p
def appI : List Ins -> List Ins -> List Ins
| [], q => q
| i :: p, q => i :: appI p q
theorem nthI_app_r : forall (p q : List Ins) (i : Nat), nthI (appI p q) (p.length + i) = nthI q i := by
intro p
induction p with
| nil => intro q i; show nthI q (0 + i) = nthI q i; rw [Nat.zero_add]
| cons a p ih =>
intro q i
show nthI (a :: appI p q) (p.length + 1 + i) = nthI q i
rw [Nat.add_right_comm]
exact ih q i
theorem nthI_app_l : forall (p q : List Ins) (i : Nat) (ins : Ins), nthI p i = some ins -> nthI (appI p q) i = some ins := by
intro p
induction p with
| nil => intro q i ins h; cases i <;> cases h
| cons a p ih =>
intro q i ins h
cases i with
| zero => exact h
| succ i => exact ih q i ins h
theorem nthI_mapI (g : Ins -> Ins) : forall (p : List Ins) (i : Nat) (ins : Ins), nthI p i = some ins -> nthI (mapI g p) i = some (g ins) := by
intro p
induction p with
| nil => intro i ins h; cases i <;> cases h
| cons a p ih =>
intro i ins h
cases i with
| zero => cases h; rfl
| succ i => exact ih i ins h
theorem shift_shift (a b : Nat) (ins : Ins) : shift a (shift b ins) = shift (a + b) ins := by
cases ins with
| push r v => rfl
| pop r e z w =>
show Ins.pop r (a + (b + e)) (a + (b + z)) (a + (b + w)) = Ins.pop r (a + b + e) (a + b + z) (a + b + w)
rw [Nat.add_assoc a b e, Nat.add_assoc a b z, Nat.add_assoc a b w]
| jmp l =>
show Ins.jmp (a + (b + l)) = Ins.jmp (a + b + l)
rw [Nat.add_assoc]
/-- A shifted program placed after p sits after p. -/
theorem frag_app (prog : List Ins) (o : Nat) (p q : List Ins) (h : Frag prog o (appI p (mapI (shift p.length) q))) :
Frag prog (o + p.length) q := by
intro i ins hi
have h1 := h (p.length + i) (shift p.length ins)
((nthI_app_r p _ i).trans (nthI_mapI (shift p.length) q i ins hi))
rw [← Nat.add_assoc, shift_shift] at h1
exact h1
theorem frag_app_l (prog : List Ins) (o : Nat) (p q : List Ins) (h : Frag prog o (appI p q)) : Frag prog o p :=
fun i ins hi => h i ins (nthI_app_l p q i ins hi)
/-! ## Unary lists -/
def tr : Nat -> List Bool
| 0 => []
| n + 1 => true :: tr n
theorem tr_len : forall n : Nat, (tr n).length = n := by
intro n
induction n with
| zero => rfl
| succ n ih => exact congrArg (fun t => t + 1) ih
theorem tr_cons : forall (n : Nat) (m : List Bool), tr n ++ true :: m = true :: (tr n ++ m) := by
intro n
induction n with
| zero => intro m; rfl
| succ n ih => intro m; exact congrArg (fun t => true :: t) (ih m)
theorem revApp_tr : forall (n : Nat) (m : List Bool), revApp (tr n) m = tr n ++ m := by
intro n
induction n with
| zero => intro m; rfl
| succ n ih =>
intro m
show revApp (tr n) (true :: m) = true :: (tr n ++ m)
rw [ih, tr_cons]
theorem revApp_app : forall (A B m : List Bool), revApp (A ++ B) m = revApp B (revApp A m) := by
intro A
induction A with
| nil => intro B m; rfl
| cons a A ih => intro B m; exact ih B (a :: m)
theorem un_tr : forall n : Nat, Found.Pnp19.un n = tr n ++ [false] := by
intro n
induction n with
| zero => rfl
| succ n ih => exact congrArg (fun t => true :: t) ih
theorem revApp_un (n : Nat) (m : List Bool) : revApp (Found.Pnp19.un n) m = false :: (tr n ++ m) := by
rw [un_tr, revApp_app, revApp_tr]
rfl
theorem app_nil'' : forall A : List Bool, A ++ [] = A := by
intro A
induction A with
| nil => rfl
| cons a A ih => exact congrArg (fun t => a :: t) ih
/-! ## The fragment -/
def unary (A B out : Nat) : List Ins :=
appI [.pop A 4 1 1, .push out true, .push B true, .jmp 0, .push out false] (mapI (shift 5) (move B A))
/-- The loop body: push 1 on out and on B. -/
def body (B out : Nat) (x : Nat -> List Bool) : Nat -> List Bool :=
upd (upd x out (true :: x out)) B (true :: upd x out (true :: x out) B)
theorem steps_two (c : Nat) (f : (Nat -> List Bool) -> (Nat -> List Bool)) :
forall (l : List Bool) (x : Nat -> List Bool), steps c (fun _ => 2) f l x = 4 * l.length + 1 := by
intro l
induction l with
| nil => intro x; rfl
| cons b l ih =>
intro x
show 4 + steps c (fun _ => 2) f l (f (upd x c l)) = 4 * Nat.succ l.length + 1
rw [ih, Nat.mul_succ, Nat.add_comm 4, Nat.add_right_comm]
/-- What the loop leaves. -/
theorem iter_body (A B out : Nat) (hAB : Nat.beq A B = false) (hAo : Nat.beq A out = false) (hBo : Nat.beq B out = false) :
forall (l : List Bool) (x : Nat -> List Bool), x A = l ->
iter A (body B out) l x A = [] /\
iter A (body B out) l x out = tr l.length ++ x out /\
iter A (body B out) l x B = tr l.length ++ x B /\
(forall i, Nat.beq i A = false -> Nat.beq i B = false -> Nat.beq i out = false ->
iter A (body B out) l x i = x i) := by
have hBA : Nat.beq B A = false := Found.Pnp2.beq_false_of_ne B A (fun e => by rw [e, beq_refl'] at hAB; exact Bool.noConfusion hAB)
have hoA : Nat.beq out A = false := Found.Pnp2.beq_false_of_ne out A (fun e => by rw [e, beq_refl'] at hAo; exact Bool.noConfusion hAo)
have hoB : Nat.beq out B = false := Found.Pnp2.beq_false_of_ne out B (fun e => by rw [e, beq_refl'] at hBo; exact Bool.noConfusion hBo)
intro l
induction l with
| nil => intro x hx; exact And.intro hx (And.intro rfl (And.intro rfl (fun _ _ _ _ => rfl)))
| cons b l ih =>
intro x _
have hy : body B out (upd x A l) A = l := by
show upd (upd (upd x A l) out _) B _ A = l
rw [upd_other _ B A _ hAB, upd_other _ out A _ hAo, upd_same]
have r := ih (body B out (upd x A l)) hy
have yo : body B out (upd x A l) out = true :: x out := by
show upd (upd (upd x A l) out (true :: upd x A l out)) B _ out = _
rw [upd_other _ B out _ hoB, upd_same, upd_other x A out l hoA]
have yB : body B out (upd x A l) B = true :: x B := by
show upd (upd (upd x A l) out _) B (true :: upd (upd x A l) out (true :: upd x A l out) B) B = _
rw [upd_same, upd_other _ out B _ hBo, upd_other x A B l hBA]
refine And.intro r.1 (And.intro ?_ (And.intro ?_ ?_))
· show iter A (body B out) l (body B out (upd x A l)) out = tr (l.length + 1) ++ x out
rw [r.2.1, yo]; exact tr_cons l.length (x out)
· show iter A (body B out) l (body B out (upd x A l)) B = tr (l.length + 1) ++ x B
rw [r.2.2.1, yB]; exact tr_cons l.length (x B)
· intro i hiA hiB hio
show iter A (body B out) l (body B out (upd x A l)) i = x i
rw [r.2.2.2 i hiA hiB hio]
show upd (upd (upd x A l) out _) B _ i = x i
rw [upd_other _ B i _ hiB, upd_other _ out i _ hio, upd_other x A i l hiA]
theorem ne_symm' (a b : Nat) (h : Nat.beq a b = false) : Nat.beq b a = false :=
Found.Pnp2.beq_false_of_ne b a (fun e => by rw [e, beq_refl'] at h; exact Bool.noConfusion h)
/-- UNARY A B out: with A holding n ones and B empty, it pushes the bits of un n onto out,
in 4n + 1 + 1 + (3n + 1) steps, from o to o + 10, and changes no other register. -/
theorem unary_run (prog : List Ins) (o A B out : Nat) (hf : Frag prog o (unary A B out))
(hAB : Nat.beq A B = false) (hAo : Nat.beq A out = false) (hBo : Nat.beq B out = false)
(n : Nat) (x : Nat -> List Bool) (hA : x A = tr n) (hB : x B = []) :
Runs prog (4 * n + 1 + 1 + (3 * n + 1)) o x (o + 10) (upd x out (false :: (tr n ++ x out))) := by
have hBA := ne_symm' A B hAB
have hoA := ne_symm' A out hAo
have hoB := ne_symm' B out hBo
have hfirst := frag_app_l prog o [.pop A 4 1 1, .push out true, .push B true, .jmp 0, .push out false] _ hf
have hmv : Frag prog (o + 5) (move B A) := frag_app prog o [.pop A 4 1 1, .push out true, .push B true, .jmp 0, .push out false] _ hf
have i0 : nthI prog o = some (.pop A (o + 4) (o + 1) (o + 1)) := hfirst 0 _ rfl
have i1 : nthI prog (o + 1) = some (.push out true) := hfirst 1 _ rfl
have i2 : nthI prog (o + 2) = some (.push B true) := hfirst 2 _ rfl
have i3 : nthI prog (o + 1 + 2) = some (.jmp o) := hfirst 3 _ rfl
have i4 : nthI prog (o + 4) = some (.push out false) := hfirst 4 _ rfl
have hbody : forall y, Runs prog 2 (o + 1) y (o + 1 + 2) (body B out y) := by
intro y
show (run prog 1 (exec (Cfg.mk (o + 1) y) (nthI prog (o + 1)))).pc = o + 1 + 2 /\
forall i, (run prog 1 (exec (Cfg.mk (o + 1) y) (nthI prog (o + 1)))).regs i = body B out y i
rw [i1]
show (run prog 0 (exec (Cfg.mk (o + 2) (upd y out (true :: y out))) (nthI prog (o + 2)))).pc = o + 1 + 2 /\
forall i, (run prog 0 (exec (Cfg.mk (o + 2) (upd y out (true :: y out))) (nthI prog (o + 2)))).regs i = body B out y i
rw [i2]
exact And.intro rfl (fun _ => rfl)
have hkeep : forall y, body B out y A = y A := fun y => by
show upd (upd y out _) B _ A = y A
rw [upd_other _ B A _ hAB, upd_other _ out A _ hAo]
have L := loop_run A (fun _ => 2) (body B out) prog o 2 (o + 4) i0 i3 hbody hkeep (tr n) x hA
rw [steps_two, tr_len] at L
have it := iter_body A B out hAB hAo hBo (tr n) x hA
rw [tr_len, hB, app_nil''] at it
-- push 0 on out
have P : Runs prog 1 (o + 4) (iter A (body B out) (tr n) x) (o + 5)
(upd (iter A (body B out) (tr n) x) out (false :: iter A (body B out) (tr n) x out)) := by
show (run prog 0 (exec (Cfg.mk (o + 4) _) (nthI prog (o + 4)))).pc = o + 5 /\
forall i, (run prog 0 (exec (Cfg.mk (o + 4) (iter A (body B out) (tr n) x)) (nthI prog (o + 4)))).regs i = _
rw [i4]
exact And.intro rfl (fun _ => rfl)
-- MOVE B A
let y2 := upd (iter A (body B out) (tr n) x) out (false :: iter A (body B out) (tr n) x out)
have y2B : y2 B = tr n := by
show upd _ out _ B = tr n
rw [upd_other _ out B _ hBo]; exact it.2.2.1
have y2A : y2 A = [] := by
show upd _ out _ A = []
rw [upd_other _ out A _ hAo]; exact it.1
have mv := frag_move_run prog (o + 5) B A hmv hBA (tr n) [] y2 y2B y2A
rw [tr_len] at mv
have M : Runs prog (3 * n + 1) (o + 5) y2 (o + 10) (upd x out (false :: (tr n ++ x out))) := by
refine And.intro mv.1 (fun i => ?_)
cases hiB : Nat.beq i B with
| true =>
rw [Found.Pnp2.beq_eq i B hiB, mv.2.1, upd_other x out B _ hBo, hB]
| false =>
cases hiA : Nat.beq i A with
| true =>
rw [Found.Pnp2.beq_eq i A hiA, mv.2.2.1, revApp_tr, app_nil'', upd_other x out A _ hAo, hA]
| false =>
rw [mv.2.2.2 i hiB hiA]
show upd _ out _ i = upd x out _ i
cases hio : Nat.beq i out with
| true =>
rw [Found.Pnp2.beq_eq i out hio, upd_same, upd_same, it.2.1]
| false =>
rw [upd_other _ out i _ hio, upd_other x out i _ hio]
exact it.2.2.2 i hiA hiB hio
exact runs_seq prog _ _ o (o + 5) (o + 10) x _ _ (runs_seq prog _ _ o (o + 4) (o + 5) x _ _ L P) M
end Found.Pnp23
#print axioms Found.Pnp23.nthI_app_r
#print axioms Found.Pnp23.nthI_app_l
#print axioms Found.Pnp23.nthI_mapI
#print axioms Found.Pnp23.shift_shift
#print axioms Found.Pnp23.frag_app
#print axioms Found.Pnp23.frag_app_l
#print axioms Found.Pnp23.tr_len
#print axioms Found.Pnp23.tr_cons
#print axioms Found.Pnp23.revApp_tr
#print axioms Found.Pnp23.revApp_app
#print axioms Found.Pnp23.un_tr
#print axioms Found.Pnp23.revApp_un
#print axioms Found.Pnp23.app_nil''
#print axioms Found.Pnp23.steps_two
#print axioms Found.Pnp23.iter_body
#print axioms Found.Pnp23.ne_symm'
#print axioms Found.Pnp23.unary_run
#print axioms: 17 theorems, no axioms.
A block of output operations (fixed bit lists, unary codes of counters) compiles to fragments placed one after another, and pushes exactly the bits of the block onto the output register.
found/Pnp24.lean
/-
T330 piece 24. found/Pnp24 -- straight-line output blocks (CL3b, 5).
A block is a list of output operations: emit a fixed bit list, or emit the unary code of
the counter held in a register. It compiles to UNARY and EMIT fragments placed one after
another. Proved: a compiled block, with its counters held in registers different from
the scratch register B and the output register, pushes exactly the bits of the block
(fixed lists and unary codes, in order) onto the output, in a step count that is a sum
over the operations, and changes no other register. The window clauses of the tableau
are such blocks (one per time and cell), written next.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp23
namespace Found.Pnp24
open Found.Pnp20
open Found.Pnp21
open Found.Pnp23
inductive Op where
| emit : List Bool -> Op
| un : Nat -> Op
variable (B out : Nat)
def code : Op -> List Ins
| .emit w => emit out w
| .un R => unary R B out
def cmp : List Op -> List Ins
| [] => []
| op :: rest => appI (code B out op) (mapI (shift (code B out op).length) (cmp rest))
/-- The bits of a block, with the counters cnt R. -/
def bits (cnt : Nat -> Nat) : List Op -> List Bool
| [] => []
| .emit w :: rest => w ++ bits cnt rest
| .un R :: rest => Found.Pnp19.un (cnt R) ++ bits cnt rest
/-- The steps of a block. -/
def cost (cnt : Nat -> Nat) : List Op -> Nat
| [] => 0
| .emit w :: rest => w.length + cost cnt rest
| .un R :: rest => (4 * cnt R + 1 + 1 + (3 * cnt R + 1)) + cost cnt rest
/-- Every counter register of the block is different from B and out and holds its count. -/
def Good (x : Nat -> List Bool) (cnt : Nat -> Nat) : List Op -> Prop
| [] => True
| .emit _ :: rest => Good x cnt rest
| .un R :: rest => (Nat.beq R B = false /\ Nat.beq R out = false /\ x R = tr (cnt R)) /\ Good x cnt rest
theorem pushAll_eq (r : Nat) : forall (w : List Bool) (x : Nat -> List Bool) (i : Nat),
pushAll x r w i = upd x r (revApp w (x r)) i := by
intro w
induction w with
| nil =>
intro x i
show x i = selR (Nat.beq i r) (x r) (x i)
cases h : Nat.beq i r with
| false => rfl
| true => rw [Found.Pnp2.beq_eq i r h]; rfl
| cons b w ih =>
intro x i
show pushAll (upd x r (b :: x r)) r w i = _
rw [ih, upd_same]
show selR (Nat.beq i r) _ (upd x r (b :: x r) i) = selR (Nat.beq i r) _ (x i)
cases h : Nat.beq i r with
| true => rfl
| false => rw [upd_other x r i _ h]; rfl
theorem upd_upd (x : Nat -> List Bool) (r : Nat) (a b : List Bool) :
forall i, upd (upd x r a) r b i = upd x r b i := by
intro i
show selR (Nat.beq i r) b (upd x r a i) = selR (Nat.beq i r) b (x i)
cases h : Nat.beq i r with
| true => rfl
| false => rw [upd_other x r i a h]
theorem good_upd (cnt : Nat -> Nat) (x : Nat -> List Bool) (v : List Bool) :
forall ops, Good B out x cnt ops -> Good B out (upd x out v) cnt ops := by
intro ops
induction ops with
| nil => intro _; exact True.intro
| cons op rest ih =>
cases op with
| emit w => intro h; exact ih h
| un R =>
intro h
exact And.intro (And.intro h.1.1 (And.intro h.1.2.1 ((upd_other x out R v h.1.2.1).trans h.1.2.2))) (ih h.2)
theorem frag_cons (prog : List Ins) (o : Nat) (op : Op) (rest : List Op) (h : Frag prog o (cmp B out (op :: rest))) :
Frag prog o (code B out op) /\ Frag prog (o + (code B out op).length) (cmp B out rest) :=
And.intro (frag_app_l prog o _ _ h) (frag_app prog o _ _ h)
theorem len_emit (r : Nat) : forall w : List Bool, (emit r w).length = w.length := by
intro w
induction w with
| nil => rfl
| cons b w ih => exact congrArg (fun t => t + 1) ih
theorem len_appI : forall (p q : List Ins), (appI p q).length = p.length + q.length := by
intro p
induction p with
| nil => intro q; exact (Nat.zero_add _).symm
| cons a p ih =>
intro q
show (appI p q).length + 1 = (p.length + 1) + q.length
rw [ih]; exact (Nat.succ_add _ _).symm
theorem len_mapI (g : Ins -> Ins) : forall p : List Ins, (mapI g p).length = p.length := by
intro p
induction p with
| nil => rfl
| cons a p ih => exact congrArg (fun t => t + 1) ih
/-- A compiled block pushes its bits onto out. -/
theorem block_run (prog : List Ins) (hBo : Nat.beq B out = false) (cnt : Nat -> Nat) :
forall (ops : List Op) (o : Nat) (x : Nat -> List Bool), Frag prog o (cmp B out ops) ->
x B = [] -> Good B out x cnt ops ->
Runs prog (cost cnt ops) o x (o + (cmp B out ops).length) (upd x out (revApp (bits cnt ops) (x out))) := by
intro ops
induction ops with
| nil =>
intro o x _ _ _
exact And.intro rfl (fun i => pushAll_eq out [] x i)
| cons op rest ih =>
intro o x hf hB hg
have fc := frag_cons B out prog o op rest hf
have hlen : (cmp B out (op :: rest)).length = (code B out op).length + (cmp B out rest).length := by
show (appI (code B out op) (mapI (shift (code B out op).length) (cmp B out rest))).length = _
rw [len_appI, len_mapI]
cases op with
| emit w =>
have hcl : (code B out (.emit w)).length = w.length := len_emit out w
have f2 : Frag prog (o + w.length) (cmp B out rest) := by rw [← hcl]; exact fc.2
have r1 : Runs prog w.length o x (o + w.length) (upd x out (revApp w (x out))) := by
have e := frag_emit_run prog out w o x fc.1
exact And.intro e.1 (fun i => (e.2 i).trans (pushAll_eq out w x i))
have hB' : upd x out (revApp w (x out)) B = [] := (upd_other x out B _ hBo).trans hB
have r2 := ih (o + w.length) _ f2 hB' (good_upd B out cnt x _ rest hg)
have r3 := runs_seq prog _ _ _ _ _ x _ _ r1 r2
rw [hlen, hcl, ← Nat.add_assoc]
refine And.intro r3.1 (fun i => (r3.2 i).trans ?_)
rw [upd_upd, upd_same]
show upd x out (revApp (bits cnt rest) (revApp w (x out))) i = upd x out (revApp (w ++ bits cnt rest) (x out)) i
rw [revApp_app]
| un R =>
have hR := hg.1
have hcl : (code B out (.un R)).length = 10 := rfl
have f2 : Frag prog (o + 10) (cmp B out rest) := by rw [← hcl]; exact fc.2
have r1 : Runs prog (4 * cnt R + 1 + 1 + (3 * cnt R + 1)) o x (o + 10)
(upd x out (revApp (Found.Pnp19.un (cnt R)) (x out))) := by
have e := unary_run prog o R B out fc.1 hR.1 hR.2.1 hBo (cnt R) x hR.2.2 hB
refine And.intro e.1 (fun i => (e.2 i).trans ?_)
rw [revApp_un]
have hB' : upd x out (revApp (Found.Pnp19.un (cnt R)) (x out)) B = [] := (upd_other x out B _ hBo).trans hB
have r2 := ih (o + 10) _ f2 hB' (good_upd B out cnt x _ rest hg.2)
have r3 := runs_seq prog _ _ _ _ _ x _ _ r1 r2
rw [hlen, hcl, ← Nat.add_assoc]
refine And.intro r3.1 (fun i => (r3.2 i).trans ?_)
rw [upd_upd, upd_same]
show upd x out (revApp (bits cnt rest) (revApp (Found.Pnp19.un (cnt R)) (x out))) i =
upd x out (revApp (Found.Pnp19.un (cnt R) ++ bits cnt rest) (x out)) i
rw [revApp_app]
end Found.Pnp24
#print axioms Found.Pnp24.pushAll_eq
#print axioms Found.Pnp24.upd_upd
#print axioms Found.Pnp24.good_upd
#print axioms Found.Pnp24.frag_cons
#print axioms Found.Pnp24.len_emit
#print axioms Found.Pnp24.len_appI
#print axioms Found.Pnp24.len_mapI
#print axioms Found.Pnp24.block_run
#print axioms: 8 theorems, no axioms.
For time \(t\) and cell \(j\), one fixed block with counters \(t\), \(t+1\), the left cell, \(j\) and \(j+1\) in registers writes exactly the encodings of the window clauses of \((t,j)\), in the order of the tableau.
found/Pnp25.lean
/-
T330 piece 25. found/Pnp25 -- the window clauses of one time and cell as a block (CL3b, 6).
For time t and cell j, the window clauses of the tableau (Pnp9: for every bit b < k and
every pattern p of the 3k neighbourhood bits, the clause "neighbourhood differs from p,
or bit b of cell j at time t+1 is d p b") are written by one straight-line block whose
counters are held in registers RT (t), RT1 (t+1), RL (the left cell), RJ (j), RR (j+1).
Proved: with those counts, the bits of the block are exactly the encodings (Pnp19) of
those clauses, each after its CNF marker 1, in the order of the tableau CNF.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp24
namespace Found.Pnp25
open Found.Pnp8 (wc mapCl window allBits)
open Found.Pnp9 (V readV nbV leftIdx forR)
open Found.Pnp19 (un encLit encCl app_assoc')
open Found.Pnp24 (Op bits)
/-! ## Encodings of literal lists and clause lists -/
def encLs : List (Bool × V) -> List Bool
| [] => []
| l :: c => true :: (encLit l ++ encLs c)
def encList : List (List (Bool × V)) -> List Bool
| [] => []
| c :: f => true :: (encCl c ++ encList f)
theorem encCl_app : forall (A B : List (Bool × V)), encCl (A ++ B) = encLs A ++ encCl B := by
intro A
induction A with
| nil => intro B; rfl
| cons l A ih =>
intro B
show true :: (encLit l ++ encCl (A ++ B)) = true :: ((encLit l ++ encLs A) ++ encCl B)
rw [ih, app_assoc']
theorem encList_app : forall (A B : List (List (Bool × V))), encList (A ++ B) = encList A ++ encList B := by
intro A
induction A with
| nil => intro B; rfl
| cons c A ih =>
intro B
show true :: (encCl c ++ encList (A ++ B)) = true :: ((encCl c ++ encList A) ++ encList B)
rw [ih, app_assoc']
theorem wc_app : forall (ys : List V) (p : List Bool) (tl : List (Bool × V)), wc ys p tl = wc ys p [] ++ tl := by
intro ys
induction ys with
| nil => intro p tl; rfl
| cons y ys ih =>
intro p tl
cases p with
| nil => rfl
| cons q p => exact congrArg (fun t => (!q, y) :: t) (ih p tl)
theorem bits_app (cnt : Nat -> Nat) : forall (A B : List Op), bits cnt (A ++ B) = bits cnt A ++ bits cnt B := by
intro A
induction A with
| nil => intro B; rfl
| cons op A ih =>
intro B
cases op with
| emit w =>
show w ++ bits cnt (A ++ B) = (w ++ bits cnt A) ++ bits cnt B
rw [ih, app_assoc']
| un R =>
show un (cnt R) ++ bits cnt (A ++ B) = (un (cnt R) ++ bits cnt A) ++ bits cnt B
rw [ih, app_assoc']
/-! ## Literals read from registers -/
def ysOf (cnt : Nat -> Nat) (t : Nat) : List (Nat × Nat) -> List V
| [] => []
| (R, b) :: ds => (t, (cnt R, b)) :: ysOf cnt t ds
def dR (R : Nat) : Nat -> Nat -> List (Nat × Nat)
| _, 0 => []
| b, n + 1 => (R, b) :: dR R (b + 1) n
theorem ysOf_dR (cnt : Nat -> Nat) (t R : Nat) : forall n b0, ysOf cnt t (dR R b0 n) = readV t (cnt R) b0 n := by
intro n
induction n with
| zero => intro b0; rfl
| succ n ih => intro b0; exact congrArg (fun l => (t, (cnt R, b0)) :: l) (ih (b0 + 1))
theorem ysOf_app (cnt : Nat -> Nat) (t : Nat) : forall (A B : List (Nat × Nat)), ysOf cnt t (A ++ B) = ysOf cnt t A ++ ysOf cnt t B := by
intro A
induction A with
| nil => intro B; rfl
| cons d A ih =>
intro B
cases d with
| mk R b => exact congrArg (fun l => (t, (cnt R, b)) :: l) (ih B)
def litOps (RT : Nat) : List (Nat × Nat) -> List Bool -> List Op
| [], _ => []
| _ :: _, [] => []
| (R, b) :: ds, q :: p => [.emit [true, !q], .un RT, .un R, .emit (un b)] ++ litOps RT ds p
theorem litOps_bits (cnt : Nat -> Nat) (RT t : Nat) (hT : cnt RT = t) :
forall (ds : List (Nat × Nat)) (p : List Bool), bits cnt (litOps RT ds p) = encLs (wc (ysOf cnt t ds) p []) := by
intro ds
induction ds with
| nil => intro p; rfl
| cons d ds ih =>
intro p
cases d with
| mk R b =>
cases p with
| nil => rfl
| cons q p =>
show bits cnt ([.emit [true, !q], .un RT, .un R, .emit (un b)] ++ litOps RT ds p) =
true :: (((!q) :: (un t ++ (un (cnt R) ++ un b))) ++ encLs (wc (ysOf cnt t ds) p []))
rw [bits_app, ih p]
show [true, !q] ++ (un (cnt RT) ++ (un (cnt R) ++ (un b ++ []))) ++ encLs (wc (ysOf cnt t ds) p []) = _
rw [hT, Found.Pnp23.app_nil'' (un b)]
show true :: ((!q) :: (un t ++ (un (cnt R) ++ un b)) ++ _) = _
rfl
/-! ## One clause, one pattern list, all bits -/
def clauseOps (RT RT1 RJ : Nat) (ds : List (Nat × Nat)) (dbit : Bool) (b : Nat) (p : List Bool) : List Op :=
litOps RT ds p ++ [.emit [true, dbit], .un RT1, .un RJ, .emit (un b), .emit [false]]
theorem clause_bits (cnt : Nat -> Nat) (RT RT1 RJ t j : Nat) (hT : cnt RT = t) (hT1 : cnt RT1 = t + 1) (hJ : cnt RJ = j)
(ds : List (Nat × Nat)) (dbit : Bool) (b : Nat) (p : List Bool) :
bits cnt (clauseOps RT RT1 RJ ds dbit b p) = encCl (wc (ysOf cnt t ds) p [(dbit, (t + 1, (j, b)))]) := by
show bits cnt (litOps RT ds p ++ _) = _
rw [bits_app, litOps_bits cnt RT t hT, wc_app (ysOf cnt t ds) p [(dbit, (t + 1, (j, b)))], encCl_app]
show _ ++ ([true, dbit] ++ (un (cnt RT1) ++ (un (cnt RJ) ++ (un b ++ ([false] ++ []))))) = _
rw [hT1, hJ]
show encLs (wc (ysOf cnt t ds) p []) ++ (true :: dbit :: (un (t + 1) ++ (un j ++ (un b ++ [false])))) =
encLs (wc (ysOf cnt t ds) p []) ++ (true :: dbit :: ((un (t + 1) ++ (un j ++ un b)) ++ [false]))
rw [app_assoc', app_assoc']
def patOps (RT RT1 RJ : Nat) (ds : List (Nat × Nat)) (d : List Bool -> Bool) (b : Nat) : List (List Bool) -> List Op
| [] => []
| p :: ps => (.emit [true] :: clauseOps RT RT1 RJ ds (d p) b p) ++ patOps RT RT1 RJ ds d b ps
theorem patOps_bits (cnt : Nat -> Nat) (RT RT1 RJ t j : Nat) (hT : cnt RT = t) (hT1 : cnt RT1 = t + 1) (hJ : cnt RJ = j)
(ds : List (Nat × Nat)) (d : List Bool -> Bool) (b : Nat) :
forall ps, bits cnt (patOps RT RT1 RJ ds d b ps) = encList (mapCl (ysOf cnt t ds) (t + 1, (j, b)) d ps) := by
intro ps
induction ps with
| nil => rfl
| cons p ps ih =>
show bits cnt ((.emit [true] :: clauseOps RT RT1 RJ ds (d p) b p) ++ patOps RT RT1 RJ ds d b ps) =
true :: (encCl (wc (ysOf cnt t ds) p [(d p, (t + 1, (j, b)))]) ++ encList (mapCl (ysOf cnt t ds) (t + 1, (j, b)) d ps))
rw [bits_app, ih]
show ([true] ++ bits cnt (clauseOps RT RT1 RJ ds (d p) b p)) ++ _ = _
rw [clause_bits cnt RT RT1 RJ t j hT hT1 hJ]
rfl
/-- The neighbourhood description: k bits of the left cell, of j, of j + 1. -/
def nbD (RL RJ RR k : Nat) : List (Nat × Nat) := dR RL 0 k ++ (dR RJ 0 k ++ dR RR 0 k)
def bOps (RT RT1 RL RJ RR k : Nat) (delta : List Bool -> Nat -> Bool) : Nat -> List Op
| 0 => []
| b + 1 => bOps RT RT1 RL RJ RR k delta b ++
patOps RT RT1 RJ (nbD RL RJ RR k) (fun p => delta p b) b (allBits (k + (k + k)))
/-- The block of time t and cell j writes the window clauses of (t, j), in tableau order. -/
theorem bOps_bits (cnt : Nat -> Nat) (RT RT1 RL RJ RR k W t j : Nat) (delta : List Bool -> Nat -> Bool)
(hT : cnt RT = t) (hT1 : cnt RT1 = t + 1) (hL : cnt RL = leftIdx W j) (hJ : cnt RJ = j) (hR : cnt RR = j + 1) :
forall n, bits cnt (bOps RT RT1 RL RJ RR k delta n) =
encList (forR n fun b => window (nbV k W t j) (t + 1, (j, b)) (fun p => delta p b) (k + (k + k))) := by
have hys : ysOf cnt t (nbD RL RJ RR k) = nbV k W t j := by
show ysOf cnt t (dR RL 0 k ++ (dR RJ 0 k ++ dR RR 0 k)) = _
rw [ysOf_app, ysOf_app, ysOf_dR, ysOf_dR, ysOf_dR, hL, hJ, hR]
rfl
intro n
induction n with
| zero => rfl
| succ n ih =>
show bits cnt (bOps RT RT1 RL RJ RR k delta n ++ _) = encList (forR n _ ++ _)
rw [bits_app, ih, encList_app, patOps_bits cnt RT RT1 RJ t j hT hT1 hJ, hys]
rfl
end Found.Pnp25
#print axioms Found.Pnp25.encCl_app
#print axioms Found.Pnp25.encList_app
#print axioms Found.Pnp25.wc_app
#print axioms Found.Pnp25.bits_app
#print axioms Found.Pnp25.ysOf_dR
#print axioms Found.Pnp25.ysOf_app
#print axioms Found.Pnp25.litOps_bits
#print axioms Found.Pnp25.clause_bits
#print axioms Found.Pnp25.patOps_bits
#print axioms Found.Pnp25.bOps_bits
#print axioms: 10 theorems, no axioms.
With fixed register numbers, a loop whose body writes the window block of \((t,j)\) and then increments \(j\) writes, after \(m\) rounds from \(j=s+1\), the window clauses of \((t,s+1),\dots ,(t,s+m)\) in order.
found/Pnp26.lean
/-
T330 piece 26. found/Pnp26 -- the loop over cells j >= 1 for one time t (CL3b, 7).
Registers (fixed numbers): 0 output, 1 scratch, 2 loop counter, 3 = t ones, 4 = t+1 ones,
5 = j-1 ones (the left cell), 6 = j ones, 7 = j+1 ones. The body writes the window block
of (t, j) (Pnp25) and then pushes a 1 on registers 5, 6, 7 (j becomes j+1). Proved:
* every counter register of the window block is one of 3..7 (so the block's side
conditions hold whenever those registers hold ones);
* the body runs as a fragment;
* after the loop has run over a counter of m bits from j = s + 1, the output holds the
window clauses of (t, s+1), ..., (t, s+m) in order (reversed, as everything is
pushed), and registers 5, 6, 7 hold s+m, s+m+1, s+m+2 ones.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp25
namespace Found.Pnp26
open Found.Pnp8 (window)
open Found.Pnp9 (nbV leftIdx forR)
open Found.Pnp20
open Found.Pnp21
open Found.Pnp22 (steps iter loop_run)
open Found.Pnp23 (tr tr_len mapI appI frag_app frag_app_l revApp_app)
open Found.Pnp24 (Op bits cost Good cmp block_run upd_upd)
open Found.Pnp25 (encList litOps patOps clauseOps bOps nbD dR bOps_bits)
/-! ## Counter registers of a block lie in 2..7 -/
def InR (R : Nat) : Prop := 2 <= R /\ R <= 7
def UnIn : List Op -> Prop
| [] => True
| .emit _ :: rest => UnIn rest
| .un R :: rest => InR R /\ UnIn rest
theorem unIn_app : forall (A B : List Op), UnIn A -> UnIn B -> UnIn (A ++ B) := by
intro A
induction A with
| nil => intro B _ hB; exact hB
| cons op A ih =>
intro B hA hB
cases op with
| emit w => exact ih B hA hB
| un R => exact And.intro hA.1 (ih B hA.2 hB)
def DsIn : List (Nat × Nat) -> Prop
| [] => True
| (R, _) :: ds => InR R /\ DsIn ds
theorem dsIn_app : forall (A B : List (Nat × Nat)), DsIn A -> DsIn B -> DsIn (A ++ B) := by
intro A
induction A with
| nil => intro B _ hB; exact hB
| cons d A ih =>
intro B hA hB
cases d with
| mk R b => exact And.intro hA.1 (ih B hA.2 hB)
theorem dsIn_dR (R : Nat) (h : InR R) : forall n b, DsIn (dR R b n) := by
intro n
induction n with
| zero => intro b; exact True.intro
| succ n ih => intro b; exact And.intro h (ih (b + 1))
theorem unIn_litOps (RT : Nat) (hT : InR RT) : forall (ds : List (Nat × Nat)) (p : List Bool), DsIn ds -> UnIn (litOps RT ds p) := by
intro ds
induction ds with
| nil => intro p _; exact True.intro
| cons d ds ih =>
intro p hd
cases d with
| mk R b =>
cases p with
| nil => exact True.intro
| cons q p => exact And.intro hT (And.intro hd.1 (ih p hd.2))
theorem unIn_clause (RT RT1 RJ : Nat) (hT : InR RT) (hT1 : InR RT1) (hJ : InR RJ) (ds : List (Nat × Nat)) (hd : DsIn ds)
(dbit : Bool) (b : Nat) (p : List Bool) : UnIn (clauseOps RT RT1 RJ ds dbit b p) :=
unIn_app _ _ (unIn_litOps RT hT ds p hd) (And.intro hT1 (And.intro hJ True.intro))
theorem unIn_pat (RT RT1 RJ : Nat) (hT : InR RT) (hT1 : InR RT1) (hJ : InR RJ) (ds : List (Nat × Nat)) (hd : DsIn ds)
(d : List Bool -> Bool) (b : Nat) : forall ps, UnIn (patOps RT RT1 RJ ds d b ps) := by
intro ps
induction ps with
| nil => exact True.intro
| cons p ps ih => exact unIn_app _ _ (unIn_clause RT RT1 RJ hT hT1 hJ ds hd (d p) b p) ih
theorem in3 : InR 3 := And.intro (Nat.le_succ 2) (Nat.le_of_ble_eq_true rfl)
theorem in4 : InR 4 := And.intro (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)
theorem in5 : InR 5 := And.intro (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)
theorem in6 : InR 6 := And.intro (Nat.le_of_ble_eq_true rfl) (Nat.le_of_ble_eq_true rfl)
theorem in7 : InR 7 := And.intro (Nat.le_of_ble_eq_true rfl) (Nat.le_refl 7)
def ops (k : Nat) (delta : List Bool -> Nat -> Bool) : List Op := bOps 3 4 5 6 7 k delta k
theorem unIn_ops (k : Nat) (delta : List Bool -> Nat -> Bool) : UnIn (ops k delta) := by
have hd : DsIn (nbD 5 6 7 k) :=
dsIn_app _ _ (dsIn_dR 5 in5 k 0) (dsIn_app _ _ (dsIn_dR 6 in6 k 0) (dsIn_dR 7 in7 k 0))
have g : forall n, UnIn (bOps 3 4 5 6 7 k delta n) := by
intro n
induction n with
| zero => exact True.intro
| succ n ih => exact unIn_app _ _ ih (unIn_pat 3 4 6 in3 in4 in6 _ hd _ n _)
exact g k
/-- The count of a register: its length. -/
def cntOf (x : Nat -> List Bool) (R : Nat) : Nat := (x R).length
/-- Registers 2..7 hold ones. -/
def Ones (x : Nat -> List Bool) : Prop := forall R, InR R -> x R = tr (cntOf x R)
theorem ne_lo (R : Nat) (h : InR R) (a : Nat) (ha : a < 2) : Nat.beq R a = false :=
Found.Pnp2.beq_false_of_ne R a (fun e => Nat.lt_irrefl a (Nat.lt_of_lt_of_le ha (e ▸ h.1)))
theorem good_of (x : Nat -> List Bool) (hx : Ones x) : forall o, UnIn o -> Good 1 0 x (cntOf x) o := by
intro o
induction o with
| nil => intro _; exact True.intro
| cons op rest ih =>
intro h
cases op with
| emit w => exact ih h
| un R =>
exact And.intro (And.intro (ne_lo R h.1 1 (Nat.lt_succ_self 1))
(And.intro (ne_lo R h.1 0 (Nat.zero_lt_succ 1)) (hx R h.1))) (ih h.2)
theorem tr_cnt (x : Nat -> List Bool) (R n : Nat) (h : x R = tr n) : x R = tr (cntOf x R) := by
show x R = tr (x R).length
rw [h, tr_len]
theorem ones_of (x : Nat -> List Bool) (a2 a3 a4 a5 a6 a7 : Nat)
(h2 : x 2 = tr a2) (h3 : x 3 = tr a3) (h4 : x 4 = tr a4) (h5 : x 5 = tr a5) (h6 : x 6 = tr a6) (h7 : x 7 = tr a7) :
Ones x := by
intro R hR
match R, hR with
| 0, h => exact False.elim (Nat.not_succ_le_zero 1 h.1)
| 1, h => exact False.elim (Nat.not_succ_le_self 1 h.1)
| 2, _ => exact tr_cnt x 2 a2 h2
| 3, _ => exact tr_cnt x 3 a3 h3
| 4, _ => exact tr_cnt x 4 a4 h4
| 5, _ => exact tr_cnt x 5 a5 h5
| 6, _ => exact tr_cnt x 6 a6 h6
| 7, _ => exact tr_cnt x 7 a7 h7
| n + 8, h => exact False.elim (Nat.not_succ_le_self 7 (Nat.le_trans (Nat.succ_le_succ (Nat.le_add_left 7 n)) h.2))
/-! ## The body: the window block, then j := j + 1 -/
def incs : List Ins := [.push 5 true, .push 6 true, .push 7 true]
def bodyC (k : Nat) (delta : List Bool -> Nat -> Bool) : List Ins :=
appI (cmp 1 0 (ops k delta)) (mapI (shift (cmp 1 0 (ops k delta)).length) incs)
def y0 (k : Nat) (delta : List Bool -> Nat -> Bool) (x : Nat -> List Bool) : Nat -> List Bool :=
upd x 0 (revApp (bits (cntOf x) (ops k delta)) (x 0))
def fB (k : Nat) (delta : List Bool -> Nat -> Bool) (x : Nat -> List Bool) : Nat -> List Bool :=
let a := y0 k delta x
let b := upd a 5 (true :: a 5)
let c := upd b 6 (true :: b 6)
upd c 7 (true :: c 7)
def nbB (k : Nat) (delta : List Bool -> Nat -> Bool) (x : Nat -> List Bool) : Nat := cost (cntOf x) (ops k delta) + 3
theorem body_run (prog : List Ins) (o k : Nat) (delta : List Bool -> Nat -> Bool) (hf : Frag prog o (bodyC k delta))
(x : Nat -> List Bool) (hB : x 1 = []) (hx : Ones x) :
Runs prog (nbB k delta x) o x (o + (cmp 1 0 (ops k delta)).length + 3) (fB k delta x) := by
have f1 := frag_app_l prog o _ _ hf
have f2 : Frag prog (o + (cmp 1 0 (ops k delta)).length) incs := frag_app prog o _ _ hf
have r1 := block_run 1 0 prog rfl (cntOf x) (ops k delta) o x f1 hB (good_of x hx _ (unIn_ops k delta))
have i0 : nthI prog (o + (cmp 1 0 (ops k delta)).length) = some (.push 5 true) := f2 0 _ rfl
have i1 : nthI prog (o + (cmp 1 0 (ops k delta)).length + 1) = some (.push 6 true) := f2 1 _ rfl
have i2 : nthI prog (o + (cmp 1 0 (ops k delta)).length + 2) = some (.push 7 true) := f2 2 _ rfl
have r2 : Runs prog 3 (o + (cmp 1 0 (ops k delta)).length) (y0 k delta x) (o + (cmp 1 0 (ops k delta)).length + 3) (fB k delta x) := by
show (run prog 2 (exec (Cfg.mk (o + (cmp 1 0 (ops k delta)).length) (y0 k delta x)) (nthI prog (o + (cmp 1 0 (ops k delta)).length)))).pc = _ /\
forall i, (run prog 2 (exec (Cfg.mk (o + (cmp 1 0 (ops k delta)).length) (y0 k delta x)) (nthI prog (o + (cmp 1 0 (ops k delta)).length)))).regs i = _
rw [i0]
show (run prog 1 (exec (Cfg.mk (o + (cmp 1 0 (ops k delta)).length + 1) _) (nthI prog (o + (cmp 1 0 (ops k delta)).length + 1)))).pc = _ /\
forall i, (run prog 1 (exec (Cfg.mk (o + (cmp 1 0 (ops k delta)).length + 1) (upd (y0 k delta x) 5 (true :: y0 k delta x 5))) (nthI prog (o + (cmp 1 0 (ops k delta)).length + 1)))).regs i = _
rw [i1]
show (run prog 0 (exec (Cfg.mk (o + (cmp 1 0 (ops k delta)).length + 2) _) (nthI prog (o + (cmp 1 0 (ops k delta)).length + 2)))).pc = _ /\
forall i, (run prog 0 (exec (Cfg.mk (o + (cmp 1 0 (ops k delta)).length + 2)
(upd (upd (y0 k delta x) 5 (true :: y0 k delta x 5)) 6 (true :: upd (y0 k delta x) 5 (true :: y0 k delta x 5) 6)))
(nthI prog (o + (cmp 1 0 (ops k delta)).length + 2)))).regs i = _
rw [i2]
exact And.intro rfl (fun _ => rfl)
exact runs_seq prog _ _ _ _ _ x _ _ r1 r2
/-! ## A loop with an invariant -/
theorem loop_inv (c : Nat) (nb : (Nat -> List Bool) -> Nat) (f : (Nat -> List Bool) -> (Nat -> List Bool))
(P : (Nat -> List Bool) -> Prop) (prog : List Ins) (o bl ex : Nat)
(hpop : nthI prog o = some (.pop c ex (o + 1) (o + 1)))
(hjmp : nthI prog (o + 1 + bl) = some (.jmp o))
(hbody : forall x, P x -> Runs prog (nb x) (o + 1) x (o + 1 + bl) (f x))
(hkeep : forall x, f x c = x c)
(hP1 : forall x b l, P x -> x c = b :: l -> P (upd x c l))
(hP2 : forall x, P x -> P (f x)) :
forall (l : List Bool) (x : Nat -> List Bool), x c = l -> P x ->
Runs prog (steps c nb f l x) o x ex (iter c f l x) := by
intro l
induction l with
| nil =>
intro x hx _
show (run prog 0 (exec (Cfg.mk o x) (nthI prog o))).pc = ex /\
forall i, (run prog 0 (exec (Cfg.mk o x) (nthI prog o))).regs i = x i
rw [hpop]
show (popTo (Cfg.mk o x) c ex (o + 1) (o + 1) (x c)).pc = ex /\
forall i, (popTo (Cfg.mk o x) c ex (o + 1) (o + 1) (x c)).regs i = x i
rw [hx]
exact And.intro rfl (fun _ => rfl)
| cons b l ih =>
intro x hx hPx
have h1 : Runs prog 1 o x (o + 1) (upd x c l) := by
show (run prog 0 (exec (Cfg.mk o x) (nthI prog o))).pc = o + 1 /\
forall i, (run prog 0 (exec (Cfg.mk o x) (nthI prog o))).regs i = upd x c l i
rw [hpop]
show (popTo (Cfg.mk o x) c ex (o + 1) (o + 1) (x c)).pc = o + 1 /\
forall i, (popTo (Cfg.mk o x) c ex (o + 1) (o + 1) (x c)).regs i = upd x c l i
rw [hx]
cases b with
| false => exact And.intro rfl (fun _ => rfl)
| true => exact And.intro rfl (fun _ => rfl)
have hP' := hP1 x b l hPx hx
have h2 := hbody (upd x c l) hP'
have h3 : Runs prog 1 (o + 1 + bl) (f (upd x c l)) o (f (upd x c l)) := by
show (run prog 0 (exec (Cfg.mk (o + 1 + bl) (f (upd x c l))) (nthI prog (o + 1 + bl)))).pc = o /\
forall i, (run prog 0 (exec (Cfg.mk (o + 1 + bl) (f (upd x c l))) (nthI prog (o + 1 + bl)))).regs i = f (upd x c l) i
rw [hjmp]
exact And.intro rfl (fun _ => rfl)
have hc : f (upd x c l) c = l := (hkeep _).trans (upd_same x c l)
have h4 := ih (f (upd x c l)) hc (hP2 _ hP')
exact runs_seq prog _ _ o o ex x _ _
(runs_seq prog _ _ o (o + 1 + bl) o x _ _ (runs_seq prog _ _ o (o + 1) (o + 1 + bl) x _ _ h1 h2) h3) h4
/-! ## What the j-loop writes -/
def winJ (k : Nat) (delta : List Bool -> Nat -> Bool) (W t : Nat) : Nat -> Nat -> List Bool
| _, 0 => []
| a, m + 1 => encList (forR k fun b => window (nbV k W t a) (t + 1, (a, b)) (fun p => delta p b) (k + (k + k))) ++
winJ k delta W t (a + 1) m
theorem iter_spec (k : Nat) (delta : List Bool -> Nat -> Bool) (W t : Nat) :
forall (m s : Nat) (x : Nat -> List Bool), x 2 = tr m -> x 3 = tr t -> x 4 = tr (t + 1) ->
x 5 = tr s -> x 6 = tr (s + 1) -> x 7 = tr (s + 2) ->
iter 2 (fB k delta) (tr m) x 0 = revApp (winJ k delta W t (s + 1) m) (x 0) /\
iter 2 (fB k delta) (tr m) x 5 = tr (s + m) /\
iter 2 (fB k delta) (tr m) x 6 = tr (s + 1 + m) /\
iter 2 (fB k delta) (tr m) x 7 = tr (s + 2 + m) /\
iter 2 (fB k delta) (tr m) x 1 = x 1 /\
iter 2 (fB k delta) (tr m) x 3 = tr t /\
iter 2 (fB k delta) (tr m) x 4 = tr (t + 1) := by
intro m
induction m with
| zero => intro s x _ h3 h4 h5 h6 h7; exact And.intro rfl (And.intro h5 (And.intro h6 (And.intro h7 (And.intro rfl (And.intro h3 h4)))))
| succ m ih =>
intro s x _ h3 h4 h5 h6 h7
have c3 : cntOf (upd x 2 (tr m)) 3 = t := by show (x 3).length = t; rw [h3, tr_len]
have c4 : cntOf (upd x 2 (tr m)) 4 = t + 1 := by show (x 4).length = t + 1; rw [h4, tr_len]
have c5 : cntOf (upd x 2 (tr m)) 5 = leftIdx W (s + 1) := by show (x 5).length = s; rw [h5, tr_len]
have c6 : cntOf (upd x 2 (tr m)) 6 = s + 1 := by show (x 6).length = s + 1; rw [h6, tr_len]
have c7 : cntOf (upd x 2 (tr m)) 7 = s + 1 + 1 := by show (x 7).length = s + 2; rw [h7, tr_len]
have hb := bOps_bits (cntOf (upd x 2 (tr m))) 3 4 5 6 7 k W t (s + 1) delta c3 c4 c5 c6 c7 k
have e0 : fB k delta (upd x 2 (tr m)) 0 =
revApp (encList (forR k fun b => window (nbV k W t (s + 1)) (t + 1, (s + 1, b)) (fun p => delta p b) (k + (k + k)))) (x 0) := by
show revApp (bits (cntOf (upd x 2 (tr m))) (bOps 3 4 5 6 7 k delta k)) (x 0) = _
rw [hb]
have r := ih (s + 1) (fB k delta (upd x 2 (tr m))) rfl h3 h4
(congrArg (fun l => true :: l) h5) (congrArg (fun l => true :: l) h6) (congrArg (fun l => true :: l) h7)
refine And.intro ?_ (And.intro ?_ (And.intro ?_ (And.intro ?_ (And.intro r.2.2.2.2.1 r.2.2.2.2.2))))
· show iter 2 (fB k delta) (tr m) (fB k delta (upd x 2 (tr m))) 0 = _
rw [r.1, e0, ← revApp_app]
rfl
· show iter 2 (fB k delta) (tr m) (fB k delta (upd x 2 (tr m))) 5 = tr (s + (m + 1))
exact r.2.1.trans (congrArg tr (Nat.add_right_comm s 1 m))
· show iter 2 (fB k delta) (tr m) (fB k delta (upd x 2 (tr m))) 6 = tr (s + 1 + (m + 1))
exact r.2.2.1.trans (congrArg tr (Nat.add_right_comm (s + 1) 1 m))
· show iter 2 (fB k delta) (tr m) (fB k delta (upd x 2 (tr m))) 7 = tr (s + 2 + (m + 1))
exact r.2.2.2.1.trans (congrArg tr (Nat.add_right_comm (s + 2) 1 m))
end Found.Pnp26
#print axioms Found.Pnp26.unIn_app
#print axioms Found.Pnp26.dsIn_app
#print axioms Found.Pnp26.dsIn_dR
#print axioms Found.Pnp26.unIn_litOps
#print axioms Found.Pnp26.unIn_clause
#print axioms Found.Pnp26.unIn_pat
#print axioms Found.Pnp26.in3
#print axioms Found.Pnp26.unIn_ops
#print axioms Found.Pnp26.ne_lo
#print axioms Found.Pnp26.good_of
#print axioms Found.Pnp26.tr_cnt
#print axioms Found.Pnp26.ones_of
#print axioms Found.Pnp26.body_run
#print axioms Found.Pnp26.loop_inv
#print axioms Found.Pnp26.iter_spec
#print axioms: 15 theorems, no axioms.
CLEAR empties a register in \(2|r|+1\) steps; COPY puts a copy of a counter in front of another register in \(7n+2\) steps and leaves the counter as it was.
found/Pnp27.lean
/-
T330 piece 27. found/Pnp27 -- CLEAR and COPY fragments (CL3b, 8).
CLEAR r: pop r until it is empty (2 |r| + 1 steps).
COPY A B D: with A holding n ones and B empty, put n more ones in front of D, leaving A
as it was (loop: pop A, push 1 on D and on B; then MOVE B A), in 4n + 1 + (3n + 1) steps.
Both change no other register. They reset and copy the cell and time counters between
the rounds of the tableau loops.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp24
namespace Found.Pnp27
open Found.Pnp20
open Found.Pnp21
open Found.Pnp22 (steps iter loop_run)
open Found.Pnp23
open Found.Pnp24 (upd_upd)
/-! ## CLEAR -/
def clear (r : Nat) : List Ins := [.pop r 2 1 1, .jmp 0]
theorem steps_zero (r : Nat) : forall (l : List Bool) (x : Nat -> List Bool),
steps r (fun _ => 0) (fun y => y) l x = 2 * l.length + 1 := by
intro l
induction l with
| nil => intro x; rfl
| cons b l ih =>
intro x
show 2 + steps r (fun _ => 0) (fun y => y) l (upd x r l) = 2 * Nat.succ l.length + 1
rw [ih, Nat.mul_succ, Nat.add_comm 2, Nat.add_right_comm]
theorem iter_id (r : Nat) : forall (l : List Bool) (x : Nat -> List Bool), x r = l ->
forall i, iter r (fun y => y) l x i = upd x r [] i := by
intro l
induction l with
| nil =>
intro x hx i
show x i = selR (Nat.beq i r) [] (x i)
cases h : Nat.beq i r with
| false => rfl
| true => rw [Found.Pnp2.beq_eq i r h, hx]; rfl
| cons b l ih =>
intro x _ i
exact (ih (upd x r l) (upd_same x r l) i).trans (upd_upd x r l [] i)
theorem clear_run (prog : List Ins) (o r : Nat) (hf : Frag prog o (clear r)) (l : List Bool) (x : Nat -> List Bool)
(hx : x r = l) : Runs prog (2 * l.length + 1) o x (o + 2) (upd x r []) := by
have i0 : nthI prog o = some (.pop r (o + 2) (o + 1) (o + 1)) := hf 0 _ rfl
have i1 : nthI prog (o + 1 + 0) = some (.jmp o) := hf 1 _ rfl
have L := loop_run r (fun _ => 0) (fun y => y) prog o 0 (o + 2) i0 i1 (fun y => And.intro rfl (fun _ => rfl))
(fun _ => rfl) l x hx
rw [steps_zero] at L
exact And.intro L.1 (fun i => (L.2 i).trans (iter_id r l x hx i))
/-! ## COPY -/
def copyF (A B D : Nat) : List Ins :=
appI [.pop A 4 1 1, .push D true, .push B true, .jmp 0] (mapI (shift 4) (move B A))
theorem copy_run (prog : List Ins) (o A B D : Nat) (hf : Frag prog o (copyF A B D))
(hAB : Nat.beq A B = false) (hAD : Nat.beq A D = false) (hBD : Nat.beq B D = false)
(n : Nat) (x : Nat -> List Bool) (hA : x A = tr n) (hB : x B = []) :
Runs prog (4 * n + 1 + (3 * n + 1)) o x (o + 9) (upd x D (tr n ++ x D)) := by
have hBA := ne_symm' A B hAB
have hDA := ne_symm' A D hAD
have hDB := ne_symm' B D hBD
have hfirst := frag_app_l prog o [.pop A 4 1 1, .push D true, .push B true, .jmp 0] _ hf
have hmv : Frag prog (o + 4) (move B A) := frag_app prog o [.pop A 4 1 1, .push D true, .push B true, .jmp 0] _ hf
have i0 : nthI prog o = some (.pop A (o + 4) (o + 1) (o + 1)) := hfirst 0 _ rfl
have i1 : nthI prog (o + 1) = some (.push D true) := hfirst 1 _ rfl
have i2 : nthI prog (o + 2) = some (.push B true) := hfirst 2 _ rfl
have i3 : nthI prog (o + 1 + 2) = some (.jmp o) := hfirst 3 _ rfl
have hbody : forall y, Runs prog 2 (o + 1) y (o + 1 + 2) (body B D y) := by
intro y
show (run prog 1 (exec (Cfg.mk (o + 1) y) (nthI prog (o + 1)))).pc = o + 1 + 2 /\
forall i, (run prog 1 (exec (Cfg.mk (o + 1) y) (nthI prog (o + 1)))).regs i = body B D y i
rw [i1]
show (run prog 0 (exec (Cfg.mk (o + 2) (upd y D (true :: y D))) (nthI prog (o + 2)))).pc = o + 1 + 2 /\
forall i, (run prog 0 (exec (Cfg.mk (o + 2) (upd y D (true :: y D))) (nthI prog (o + 2)))).regs i = body B D y i
rw [i2]
exact And.intro rfl (fun _ => rfl)
have hkeep : forall y, body B D y A = y A := fun y => by
show upd (upd y D _) B _ A = y A
rw [upd_other _ B A _ hAB, upd_other _ D A _ hAD]
have L := loop_run A (fun _ => 2) (body B D) prog o 2 (o + 4) i0 i3 hbody hkeep (tr n) x hA
rw [steps_two, tr_len] at L
have it := iter_body A B D hAB hAD hBD (tr n) x hA
rw [tr_len, hB, app_nil''] at it
have mv := frag_move_run prog (o + 4) B A hmv hBA (tr n) [] (iter A (body B D) (tr n) x) it.2.2.1 it.1
rw [tr_len] at mv
have M : Runs prog (3 * n + 1) (o + 4) (iter A (body B D) (tr n) x) (o + 9) (upd x D (tr n ++ x D)) := by
refine And.intro mv.1 (fun i => ?_)
cases hiB : Nat.beq i B with
| true =>
rw [Found.Pnp2.beq_eq i B hiB, mv.2.1, upd_other x D B _ hBD, hB]
| false =>
cases hiA : Nat.beq i A with
| true =>
rw [Found.Pnp2.beq_eq i A hiA, mv.2.2.1, revApp_tr, app_nil'', upd_other x D A _ hAD, hA]
| false =>
rw [mv.2.2.2 i hiB hiA]
cases hiD : Nat.beq i D with
| true =>
rw [Found.Pnp2.beq_eq i D hiD, upd_same, it.2.1]
| false =>
rw [upd_other x D i _ hiD]
exact it.2.2.2 i hiA hiB hiD
exact runs_seq prog _ _ o (o + 4) (o + 9) x _ _ L M
end Found.Pnp27
#print axioms Found.Pnp27.steps_zero
#print axioms Found.Pnp27.iter_id
#print axioms Found.Pnp27.clear_run
#print axioms Found.Pnp27.copy_run
#print axioms: 4 theorems, no axioms.
The cell loop (pop the counter, run the body, jump back) is a fragment: it writes the window clauses of \((t,s+1),\dots ,(t,s+m)\), moves the cell counters on by \(m\), empties its counter and keeps every register from 8 on.
found/Pnp28.lean
/-
T330 piece 28. found/Pnp28 -- the cell loop as one fragment (CL3b, 9).
Sequencing of code (seqI p q = p followed by q with q's jumps moved past p) and the cell
loop JLOOP = [pop 2 -> exit / body], body (Pnp26), [jump back] as a fragment. Proved:
from registers 3, 4, 5, 6, 7 holding t, t+1, s, s+1, s+2 ones, register 2 holding m ones
and register 1 empty, JLOOP exits with the window clauses of (t, s+1) .. (t, s+m)
pushed on register 0, registers 5, 6, 7 holding s+m, s+m+1, s+m+2 ones, registers 1, 3, 4
as they were, register 2 empty, and every register from 8 on unchanged.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp26
namespace Found.Pnp28
open Found.Pnp20
open Found.Pnp21
open Found.Pnp22 (steps iter)
open Found.Pnp23 (tr tr_len mapI appI frag_app frag_app_l)
open Found.Pnp24 (cmp)
open Found.Pnp26
/-! ## Sequencing -/
def seqI (p q : List Ins) : List Ins := appI p (mapI (shift p.length) q)
theorem frag_seq (prog : List Ins) (o : Nat) (p q : List Ins) (h : Frag prog o (seqI p q)) :
Frag prog o p /\ Frag prog (o + p.length) q :=
And.intro (frag_app_l prog o _ _ h) (frag_app prog o _ _ h)
/-! ## The cell loop -/
def jloop (k : Nat) (delta : List Bool -> Nat -> Bool) : List Ins :=
appI (appI [.pop 2 ((bodyC k delta).length + 2) 1 1] (mapI (shift 1) (bodyC k delta))) [.jmp 0]
/-- The loop invariant: scratch empty, registers 2..7 hold ones. -/
def Inv (x : Nat -> List Bool) : Prop := x 1 = [] /\ Ones x
theorem tr_cons_inv (n : Nat) (b : Bool) (l : List Bool) (h : b :: l = tr n) : l = tr (n - 1) := by
cases n with
| zero => cases h
| succ n => exact (List.cons.inj h).2
theorem inv_pop (x : Nat -> List Bool) (b : Bool) (l : List Bool) (h : Inv x) (hx : x 2 = b :: l) : Inv (upd x 2 l) := by
refine And.intro h.1 (ones_of _ (cntOf x 2 - 1) (cntOf x 3) (cntOf x 4) (cntOf x 5) (cntOf x 6) (cntOf x 7)
?_ (h.2 3 in3) (h.2 4 in4) (h.2 5 in5) (h.2 6 in6) (h.2 7 in7))
show l = tr (cntOf x 2 - 1)
exact tr_cons_inv _ b l (hx.symm.trans (h.2 2 (And.intro (Nat.le_refl 2) (Nat.le_of_ble_eq_true rfl))))
theorem inv_body (k : Nat) (delta : List Bool -> Nat -> Bool) (x : Nat -> List Bool) (h : Inv x) : Inv (fB k delta x) := by
refine And.intro h.1 (ones_of _ (cntOf x 2) (cntOf x 3) (cntOf x 4) (cntOf x 5 + 1) (cntOf x 6 + 1) (cntOf x 7 + 1)
(h.2 2 (And.intro (Nat.le_refl 2) (Nat.le_of_ble_eq_true rfl))) (h.2 3 in3) (h.2 4 in4) ?_ ?_ ?_)
· exact congrArg (fun l => true :: l) (h.2 5 in5)
· exact congrArg (fun l => true :: l) (h.2 6 in6)
· exact congrArg (fun l => true :: l) (h.2 7 in7)
theorem iter_keep (k : Nat) (delta : List Bool -> Nat -> Bool) (i : Nat) (hi : 8 <= i) :
forall (l : List Bool) (x : Nat -> List Bool), iter 2 (fB k delta) l x i = x i := by
have ne : forall a, a + 1 <= 8 -> Nat.beq i a = false := fun a ha =>
Found.Pnp2.beq_false_of_ne i a (fun e => Nat.lt_irrefl a (Nat.lt_of_lt_of_le ha (e ▸ hi)))
have h0 := ne 0 (Nat.le_of_ble_eq_true rfl)
have h2 := ne 2 (Nat.le_of_ble_eq_true rfl)
have h5 := ne 5 (Nat.le_of_ble_eq_true rfl)
have h6 := ne 6 (Nat.le_of_ble_eq_true rfl)
have h7 := ne 7 (Nat.le_of_ble_eq_true rfl)
intro l
induction l with
| nil => intro x; rfl
| cons b l ih =>
intro x
show iter 2 (fB k delta) l (fB k delta (upd x 2 l)) i = x i
rw [ih]
show upd (upd (upd (upd (upd x 2 l) 0 _) 5 _) 6 _) 7 _ i = x i
rw [upd_other _ 7 i _ h7, upd_other _ 6 i _ h6, upd_other _ 5 i _ h5, upd_other _ 0 i _ h0, upd_other _ 2 i _ h2]
theorem iter_two (k : Nat) (delta : List Bool -> Nat -> Bool) :
forall (l : List Bool) (x : Nat -> List Bool), x 2 = l -> iter 2 (fB k delta) l x 2 = [] := by
intro l
induction l with
| nil => intro x hx; exact hx
| cons b l ih => intro x _; exact ih _ rfl
/-- JLOOP from o: the window clauses of (t, s+1) .. (t, s+m), then exit. -/
theorem jloop_run (prog : List Ins) (o k : Nat) (delta : List Bool -> Nat -> Bool) (W t m s : Nat)
(hf : Frag prog o (jloop k delta)) (x : Nat -> List Bool)
(h1 : x 1 = []) (h2 : x 2 = tr m) (h3 : x 3 = tr t) (h4 : x 4 = tr (t + 1))
(h5 : x 5 = tr s) (h6 : x 6 = tr (s + 1)) (h7 : x 7 = tr (s + 2)) :
Runs prog (steps 2 (nbB k delta) (fB k delta) (tr m) x) o x (o + ((bodyC k delta).length + 2))
(iter 2 (fB k delta) (tr m) x) /\
iter 2 (fB k delta) (tr m) x 0 = revApp (winJ k delta W t (s + 1) m) (x 0) /\
iter 2 (fB k delta) (tr m) x 1 = [] /\ iter 2 (fB k delta) (tr m) x 2 = [] /\
iter 2 (fB k delta) (tr m) x 3 = tr t /\ iter 2 (fB k delta) (tr m) x 4 = tr (t + 1) /\
iter 2 (fB k delta) (tr m) x 5 = tr (s + m) /\ iter 2 (fB k delta) (tr m) x 6 = tr (s + 1 + m) /\
iter 2 (fB k delta) (tr m) x 7 = tr (s + 2 + m) /\
(forall i, 8 <= i -> iter 2 (fB k delta) (tr m) x i = x i) := by
have fa := frag_app_l prog o _ _ hf
have fb : Frag prog (o + 1) (bodyC k delta) := frag_app prog o [.pop 2 ((bodyC k delta).length + 2) 1 1] _ fa
have i0 : nthI prog o = some (.pop 2 (o + ((bodyC k delta).length + 2)) (o + 1) (o + 1)) := fa 0 _ rfl
have hl : (appI [.pop 2 ((bodyC k delta).length + 2) 1 1] (mapI (shift 1) (bodyC k delta))).length = 1 + (bodyC k delta).length := by
rw [Found.Pnp24.len_appI, Found.Pnp24.len_mapI]; rfl
have i1 : nthI prog (o + 1 + (bodyC k delta).length) = some (.jmp o) := by
have e := hf ((appI [.pop 2 ((bodyC k delta).length + 2) 1 1] (mapI (shift 1) (bodyC k delta))).length + 0) (.jmp 0)
(Found.Pnp23.nthI_app_r _ [.jmp 0] 0)
rw [hl, Nat.add_zero, ← Nat.add_assoc] at e
exact e
have hbody : forall y, Inv y -> Runs prog (nbB k delta y) (o + 1) y (o + 1 + (bodyC k delta).length) (fB k delta y) := by
intro y hy
have r := body_run prog (o + 1) k delta fb y hy.1 hy.2
have e : (bodyC k delta).length = (cmp 1 0 (ops k delta)).length + 3 := by
show (appI (cmp 1 0 (ops k delta)) (mapI _ incs)).length = _
rw [Found.Pnp24.len_appI, Found.Pnp24.len_mapI]
rfl
rw [e, ← Nat.add_assoc]
exact r
have hx : Inv x := And.intro h1 (ones_of x m t (t + 1) s (s + 1) (s + 2) h2 h3 h4 h5 h6 h7)
have L := loop_inv 2 (nbB k delta) (fB k delta) Inv prog o (bodyC k delta).length (o + ((bodyC k delta).length + 2))
i0 i1 hbody (fun _ => rfl) inv_pop (inv_body k delta) (tr m) x h2 hx
have sp := iter_spec k delta W t m s x h2 h3 h4 h5 h6 h7
exact And.intro L (And.intro sp.1 (And.intro (sp.2.2.2.2.1.trans h1) (And.intro (iter_two k delta (tr m) x h2)
(And.intro sp.2.2.2.2.2.1 (And.intro sp.2.2.2.2.2.2 (And.intro sp.2.1 (And.intro sp.2.2.1 (And.intro sp.2.2.2.1
(fun i hi => iter_keep k delta i hi (tr m) x)))))))))
end Found.Pnp28
#print axioms Found.Pnp28.frag_seq
#print axioms Found.Pnp28.tr_cons_inv
#print axioms Found.Pnp28.inv_pop
#print axioms Found.Pnp28.inv_body
#print axioms Found.Pnp28.iter_keep
#print axioms Found.Pnp28.iter_two
#print axioms Found.Pnp28.jloop_run
#print axioms: 7 theorems, no axioms.
One round resets the cell counters, writes the window block of \((t,0)\) with the boundary cell as left neighbour, runs the cell loop over \(j=1,\dots ,W-1\), and moves \(t\) on; it writes the window clauses of \((t,0),\dots ,(t,W-1)\) in order.
found/Pnp29.lean
/-
T330 piece 29. found/Pnp29 -- one time round of the tableau writer (CL3b, 10).
Round code for time t (registers as in Pnp26; register 9 holds W ones, register 10 holds
W - 1 ones, W = W' + 1):
CLEAR 5; CLEAR 6; CLEAR 7; COPY 9 -> 5 (5 := W, the left cell of j = 0); push 1 on 7;
body (window block of (t, 0), then 5, 6, 7 += 1); CLEAR 5; COPY 10 -> 2 (loop counter
W - 1); JLOOP (cells 1 .. W - 1); push 1 on 3 and on 4 (t := t + 1).
Proved: from registers 1, 2 empty, 3 = t, 4 = t + 1, 9 = W, 10 = W - 1 ones, the round runs
as a fragment and leaves on register 0 the window clauses of (t, 0), (t, 1), ..., (t, W-1)
in order (pushed), registers 3, 4 = t + 1, t + 2, registers 1, 2 empty, 9, 10 kept.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp28
import found.Pnp27
namespace Found.Pnp29
open Found.Pnp8 (window)
open Found.Pnp9 (nbV forR)
open Found.Pnp20
open Found.Pnp21
open Found.Pnp22 (steps iter)
open Found.Pnp23 (tr tr_len mapI appI app_nil'' revApp_app)
open Found.Pnp24 (cmp bits)
open Found.Pnp25 (encList bOps bOps_bits)
open Found.Pnp26
open Found.Pnp27 (clear copyF clear_run copy_run)
open Found.Pnp28
def rnd (k : Nat) (delta : List Bool -> Nat -> Bool) : List Ins :=
seqI (clear 5) (seqI (clear 6) (seqI (clear 7) (seqI (copyF 9 1 5) (seqI [.push 7 true]
(seqI (bodyC k delta) (seqI (clear 5) (seqI (copyF 10 1 2) (seqI (jloop k delta) [.push 3 true, .push 4 true]))))))))
theorem len_seq (p q : List Ins) : (seqI p q).length = p.length + q.length := by
show (appI p (mapI (shift p.length) q)).length = _
rw [Found.Pnp24.len_appI, Found.Pnp24.len_mapI]
theorem len_body (k : Nat) (delta : List Bool -> Nat -> Bool) :
(bodyC k delta).length = (cmp 1 0 (ops k delta)).length + 3 := by
show (appI (cmp 1 0 (ops k delta)) (mapI _ incs)).length = _
rw [Found.Pnp24.len_appI, Found.Pnp24.len_mapI]
rfl
theorem len_jloop (k : Nat) (delta : List Bool -> Nat -> Bool) : (jloop k delta).length = (bodyC k delta).length + 2 := by
show (appI (appI [_] (mapI (shift 1) (bodyC k delta))) [_]).length = _
rw [Found.Pnp24.len_appI, Found.Pnp24.len_appI, Found.Pnp24.len_mapI]
show 1 + (bodyC k delta).length + 1 = (bodyC k delta).length + 2
rw [Nat.add_comm 1 (bodyC k delta).length]
theorem assoc10 (o a b c d e f g h i j : Nat) :
o + a + b + c + d + e + f + g + h + i + j = o + (a + (b + (c + (d + (e + (f + (g + (h + (i + j))))))))) :=
(Nat.add_assoc (o + a + b + c + d + e + f + g + h) i j).trans
((Nat.add_assoc (o + a + b + c + d + e + f + g) h (i + j)).trans
((Nat.add_assoc (o + a + b + c + d + e + f) g (h + (i + j))).trans
((Nat.add_assoc (o + a + b + c + d + e) f (g + (h + (i + j)))).trans
((Nat.add_assoc (o + a + b + c + d) e (f + (g + (h + (i + j))))).trans
((Nat.add_assoc (o + a + b + c) d (e + (f + (g + (h + (i + j)))))).trans
((Nat.add_assoc (o + a + b) c (d + (e + (f + (g + (h + (i + j))))))).trans
((Nat.add_assoc (o + a) b (c + (d + (e + (f + (g + (h + (i + j)))))))).trans
(Nat.add_assoc o a (b + (c + (d + (e + (f + (g + (h + (i + j))))))))))))))))
theorem len_rnd (k : Nat) (delta : List Bool -> Nat -> Bool) :
(rnd k delta).length = 2 + (2 + (2 + (9 + (1 + ((bodyC k delta).length + (2 + (9 + (((bodyC k delta).length + 2) + 2)))))))) := by
show (seqI (clear 5) _).length = _
rw [len_seq, len_seq, len_seq, len_seq, len_seq, len_seq, len_seq, len_seq, len_seq, len_jloop]
rfl
/-- The window clauses of (t, j). -/
def X (k : Nat) (delta : List Bool -> Nat -> Bool) (W t j : Nat) : List (List (Bool × Found.Pnp9.V)) :=
forR k fun b => window (nbV k W t j) (t + 1, (j, b)) (fun p => delta p b) (k + (k + k))
theorem ne_ge (i a : Nat) (hi : 11 <= i) (ha : a + 1 <= 11) : Nat.beq i a = false :=
Found.Pnp2.beq_false_of_ne i a (fun e => Nat.lt_irrefl a (Nat.lt_of_lt_of_le ha (e ▸ hi)))
/-- The states of the round. -/
def st5 (W' : Nat) (x : Nat -> List Bool) : Nat -> List Bool :=
let x3 := upd (upd (upd x 5 []) 6 []) 7 []
let x4 := upd x3 5 (tr (W' + 1) ++ x3 5)
upd x4 7 (true :: x4 7)
def st8 (k : Nat) (delta : List Bool -> Nat -> Bool) (W' : Nat) (x : Nat -> List Bool) : Nat -> List Bool :=
let x7 := upd (fB k delta (st5 W' x)) 5 []
upd x7 2 (tr W' ++ x7 2)
/-- The steps of the round. -/
def rndCost (k : Nat) (delta : List Bool -> Nat -> Bool) (W' : Nat) (x : Nat -> List Bool) : Nat :=
2 * (x 5).length + 1 + (2 * (x 6).length + 1) + (2 * (x 7).length + 1) + (4 * (W' + 1) + 1 + (3 * (W' + 1) + 1)) + 1 +
nbB k delta (st5 W' x) + (2 * (fB k delta (st5 W' x) 5).length + 1) + (4 * W' + 1 + (3 * W' + 1)) +
steps 2 (nbB k delta) (fB k delta) (tr W') (st8 k delta W' x) + 2
/-- The registers after the round. -/
def rndF (k : Nat) (delta : List Bool -> Nat -> Bool) (W' : Nat) (x : Nat -> List Bool) : Nat -> List Bool :=
let x9 := iter 2 (fB k delta) (tr W') (st8 k delta W' x)
upd (upd x9 3 (true :: x9 3)) 4 (true :: upd x9 3 (true :: x9 3) 4)
theorem rnd_run (prog : List Ins) (o k : Nat) (delta : List Bool -> Nat -> Bool) (W' t : Nat)
(hf : Frag prog o (rnd k delta)) (x : Nat -> List Bool)
(h1 : x 1 = []) (h2 : x 2 = []) (h3 : x 3 = tr t) (h4 : x 4 = tr (t + 1))
(h9 : x 9 = tr (W' + 1)) (h10 : x 10 = tr W') :
Runs prog (rndCost k delta W' x) o x (o + (rnd k delta).length) (rndF k delta W' x) /\
rndF k delta W' x 0 = revApp (encList (X k delta (W' + 1) t 0) ++ winJ k delta (W' + 1) t 1 W') (x 0) /\
rndF k delta W' x 1 = [] /\ rndF k delta W' x 2 = [] /\ rndF k delta W' x 3 = tr (t + 1) /\
rndF k delta W' x 4 = tr (t + 2) /\
(forall i, 9 <= i -> rndF k delta W' x i = x i) := by
-- the pieces of the code
have g0 := frag_seq prog o _ _ hf
have g1 := frag_seq prog _ _ _ g0.2
have g2 := frag_seq prog _ _ _ g1.2
have g3 := frag_seq prog _ _ _ g2.2
have g4 := frag_seq prog _ _ _ g3.2
have g5 := frag_seq prog _ _ _ g4.2
have g6 := frag_seq prog _ _ _ g5.2
have g7 := frag_seq prog _ _ _ g6.2
have g8 := frag_seq prog _ _ _ g7.2
-- the states
let x1 := upd x 5 []
let x2 := upd x1 6 []
let x3 := upd x2 7 []
let x4 := upd x3 5 (tr (W' + 1) ++ x3 5)
let x5 := upd x4 7 (true :: x4 7)
let x6 := fB k delta x5
let x7 := upd x6 5 []
let x8 := upd x7 2 (tr W' ++ x7 2)
let x9 := iter 2 (fB k delta) (tr W') x8
let x10 := upd (upd x9 3 (true :: x9 3)) 4 (true :: upd x9 3 (true :: x9 3) 4)
have R1 := clear_run prog o 5 g0.1 (x 5) x rfl
have R2 := clear_run prog _ 6 g1.1 (x1 6) x1 rfl
have R3 := clear_run prog _ 7 g2.1 (x2 7) x2 rfl
have R4 := copy_run prog _ 9 1 5 g3.1 rfl rfl rfl (W' + 1) x3 h9 h1
have R5 : Runs prog 1 (o + 2 + 2 + 2 + 9) x4 (o + 2 + 2 + 2 + 9 + 1) x5 := by
have i0 : nthI prog (o + 2 + 2 + 2 + 9) = some (.push 7 true) := g4.1 0 _ rfl
show (run prog 0 (exec (Cfg.mk (o + 2 + 2 + 2 + 9) x4) (nthI prog (o + 2 + 2 + 2 + 9)))).pc = _ /\
forall i, (run prog 0 (exec (Cfg.mk (o + 2 + 2 + 2 + 9) x4) (nthI prog (o + 2 + 2 + 2 + 9)))).regs i = x5 i
rw [i0]
exact And.intro rfl (fun _ => rfl)
have o5 : Ones x5 := ones_of x5 0 t (t + 1) (W' + 1) 0 1 h2 h3 h4 (app_nil'' (tr (W' + 1))) rfl rfl
have R6 : Runs prog (nbB k delta x5) (o + 2 + 2 + 2 + 9 + 1) x5 (o + 2 + 2 + 2 + 9 + 1 + (bodyC k delta).length) x6 := by
have r := body_run prog _ k delta g5.1 x5 h1 o5
rw [len_body, ← Nat.add_assoc]
exact r
have R7 := clear_run prog _ 5 g6.1 (x6 5) x6 rfl
have R8 := copy_run prog _ 10 1 2 g7.1 rfl rfl rfl W' x7 h10 h1
have h82 : x8 2 = tr W' := by show tr W' ++ x 2 = tr W'; rw [h2, app_nil'']
have J := jloop_run prog _ k delta (W' + 1) t W' 0 g8.1 x8 h1 h82 h3 h4 rfl rfl rfl
have R10 : Runs prog 2 (o + 2 + 2 + 2 + 9 + 1 + (bodyC k delta).length + 2 + 9 + (jloop k delta).length) x9
(o + 2 + 2 + 2 + 9 + 1 + (bodyC k delta).length + 2 + 9 + (jloop k delta).length + 2) x10 := by
have i0 : nthI prog (o + 2 + 2 + 2 + 9 + 1 + (bodyC k delta).length + 2 + 9 + (jloop k delta).length + 0) =
some (.push 3 true) := g8.2 0 _ rfl
have i1 : nthI prog (o + 2 + 2 + 2 + 9 + 1 + (bodyC k delta).length + 2 + 9 + (jloop k delta).length + 1) =
some (.push 4 true) := g8.2 1 _ rfl
show (run prog 1 (exec (Cfg.mk _ x9) (nthI prog (o + 2 + 2 + 2 + 9 + 1 + (bodyC k delta).length + 2 + 9 + (jloop k delta).length + 0)))).pc = _ /\
forall i, (run prog 1 (exec (Cfg.mk _ x9) (nthI prog (o + 2 + 2 + 2 + 9 + 1 + (bodyC k delta).length + 2 + 9 + (jloop k delta).length + 0)))).regs i = x10 i
rw [i0]
show (run prog 0 (exec (Cfg.mk _ _) (nthI prog (o + 2 + 2 + 2 + 9 + 1 + (bodyC k delta).length + 2 + 9 + (jloop k delta).length + 1)))).pc = _ /\
forall i, (run prog 0 (exec (Cfg.mk _ (upd x9 3 (true :: x9 3))) (nthI prog (o + 2 + 2 + 2 + 9 + 1 + (bodyC k delta).length + 2 + 9 + (jloop k delta).length + 1)))).regs i = x10 i
rw [i1]
exact And.intro rfl (fun _ => rfl)
have J1 : Runs prog (steps 2 (nbB k delta) (fB k delta) (tr W') x8) (o + 2 + 2 + 2 + 9 + 1 + (bodyC k delta).length + 2 + 9) x8
(o + 2 + 2 + 2 + 9 + 1 + (bodyC k delta).length + 2 + 9 + (jloop k delta).length) x9 := by
have r := J.1
rw [← len_jloop] at r
exact r
have all := runs_seq prog _ _ _ _ _ x _ _
(runs_seq prog _ _ _ _ _ x _ _
(runs_seq prog _ _ _ _ _ x _ _
(runs_seq prog _ _ _ _ _ x _ _
(runs_seq prog _ _ _ _ _ x _ _
(runs_seq prog _ _ _ _ _ x _ _
(runs_seq prog _ _ _ _ _ x _ _
(runs_seq prog _ _ _ _ _ x _ _
(runs_seq prog _ _ _ _ _ x _ _ R1 R2) R3) R4) R5) R6) R7) R8) J1) R10
have ePC : o + 2 + 2 + 2 + 9 + 1 + (bodyC k delta).length + 2 + 9 + (jloop k delta).length + 2 = o + (rnd k delta).length := by
rw [len_rnd, len_jloop]
exact assoc10 o 2 2 2 9 1 (bodyC k delta).length 2 9 ((bodyC k delta).length + 2) 2
rw [ePC] at all
refine And.intro all ?_
-- the register facts
have c3 : cntOf x5 3 = t := by show (x 3).length = t; rw [h3, tr_len]
have c4 : cntOf x5 4 = t + 1 := by show (x 4).length = t + 1; rw [h4, tr_len]
have c5 : cntOf x5 5 = Found.Pnp9.leftIdx (W' + 1) 0 := by show (tr (W' + 1) ++ []).length = W' + 1; rw [app_nil'', tr_len]
have hb := bOps_bits (cntOf x5) 3 4 5 6 7 k (W' + 1) t 0 delta c3 c4 c5 rfl rfl k
refine And.intro ?_ (And.intro J.2.2.1 (And.intro J.2.2.2.1 (And.intro ?_ (And.intro ?_ ?_))))
· show iter 2 (fB k delta) (tr W') x8 0 = _
rw [J.2.1]
show revApp (winJ k delta (W' + 1) t (0 + 1) W') (revApp (bits (cntOf x5) (bOps 3 4 5 6 7 k delta k)) (x 0)) = _
rw [hb, ← revApp_app]
rfl
· show true :: iter 2 (fB k delta) (tr W') x8 3 = tr (t + 1); rw [J.2.2.2.2.1]; rfl
· show true :: iter 2 (fB k delta) (tr W') x8 4 = tr (t + 2); rw [J.2.2.2.2.2.1]; rfl
· intro i hi
have hi8 : 8 <= i := Nat.le_trans (Nat.le_succ 8) hi
show upd (upd x9 3 _) 4 _ i = x i
have n4 : Nat.beq i 4 = false := Found.Pnp2.beq_false_of_ne i 4 (fun e => Nat.lt_irrefl 4 (Nat.lt_of_lt_of_le (Nat.le_of_ble_eq_true rfl) (e ▸ hi)))
have n3 : Nat.beq i 3 = false := Found.Pnp2.beq_false_of_ne i 3 (fun e => Nat.lt_irrefl 3 (Nat.lt_of_lt_of_le (Nat.le_of_ble_eq_true rfl) (e ▸ hi)))
have n2 : Nat.beq i 2 = false := Found.Pnp2.beq_false_of_ne i 2 (fun e => Nat.lt_irrefl 2 (Nat.lt_of_lt_of_le (Nat.le_of_ble_eq_true rfl) (e ▸ hi)))
have n5 : Nat.beq i 5 = false := Found.Pnp2.beq_false_of_ne i 5 (fun e => Nat.lt_irrefl 5 (Nat.lt_of_lt_of_le (Nat.le_of_ble_eq_true rfl) (e ▸ hi)))
have n6 : Nat.beq i 6 = false := Found.Pnp2.beq_false_of_ne i 6 (fun e => Nat.lt_irrefl 6 (Nat.lt_of_lt_of_le (Nat.le_of_ble_eq_true rfl) (e ▸ hi)))
have n7 : Nat.beq i 7 = false := Found.Pnp2.beq_false_of_ne i 7 (fun e => Nat.lt_irrefl 7 (Nat.lt_of_lt_of_le (Nat.le_of_ble_eq_true rfl) (e ▸ hi)))
have n0 : Nat.beq i 0 = false := Found.Pnp2.beq_false_of_ne i 0 (fun e => Nat.lt_irrefl 0 (Nat.lt_of_lt_of_le (Nat.le_of_ble_eq_true rfl) (e ▸ hi)))
rw [upd_other _ 4 i _ n4, upd_other _ 3 i _ n3]
show iter 2 (fB k delta) (tr W') x8 i = x i
rw [J.2.2.2.2.2.2.2.2.2 i hi8]
show upd x7 2 _ i = x i
rw [upd_other _ 2 i _ n2]
show upd x6 5 [] i = x i
rw [upd_other _ 5 i _ n5]
show upd (upd (upd (upd x5 0 _) 5 _) 6 _) 7 _ i = x i
rw [upd_other _ 7 i _ n7, upd_other _ 6 i _ n6, upd_other _ 5 i _ n5, upd_other _ 0 i _ n0]
show upd x4 7 _ i = x i
rw [upd_other _ 7 i _ n7]
show upd x3 5 _ i = x i
rw [upd_other _ 5 i _ n5]
show upd (upd (upd x 5 []) 6 []) 7 [] i = x i
rw [upd_other _ 7 i _ n7, upd_other _ 6 i _ n6, upd_other _ 5 i _ n5]
/-- The round keeps every register from 9 on, whatever the registers hold. -/
theorem rndF_keep (k : Nat) (delta : List Bool -> Nat -> Bool) (W' : Nat) (x : Nat -> List Bool) (i : Nat) (hi : 9 <= i) :
rndF k delta W' x i = x i := by
have ne : forall a, a + 1 <= 9 -> Nat.beq i a = false := fun a ha =>
Found.Pnp2.beq_false_of_ne i a (fun e => Nat.lt_irrefl a (Nat.lt_of_lt_of_le ha (e ▸ hi)))
have n0 := ne 0 (Nat.le_of_ble_eq_true rfl)
have n2 := ne 2 (Nat.le_of_ble_eq_true rfl)
have n3 := ne 3 (Nat.le_of_ble_eq_true rfl)
have n4 := ne 4 (Nat.le_of_ble_eq_true rfl)
have n5 := ne 5 (Nat.le_of_ble_eq_true rfl)
have n6 := ne 6 (Nat.le_of_ble_eq_true rfl)
have n7 := ne 7 (Nat.le_of_ble_eq_true rfl)
show upd (upd (iter 2 (fB k delta) (tr W') (st8 k delta W' x)) 3 _) 4 _ i = x i
rw [upd_other _ 4 i _ n4, upd_other _ 3 i _ n3, iter_keep k delta i (Nat.le_trans (Nat.le_succ 8) hi)]
show upd (upd (fB k delta (st5 W' x)) 5 []) 2 _ i = x i
rw [upd_other _ 2 i _ n2, upd_other _ 5 i _ n5]
show upd (upd (upd (upd (st5 W' x) 0 _) 5 _) 6 _) 7 _ i = x i
rw [upd_other _ 7 i _ n7, upd_other _ 6 i _ n6, upd_other _ 5 i _ n5, upd_other _ 0 i _ n0]
show upd (upd (upd (upd (upd x 5 []) 6 []) 7 []) 5 _) 7 _ i = x i
rw [upd_other _ 7 i _ n7, upd_other _ 5 i _ n5, upd_other _ 7 i _ n7, upd_other _ 6 i _ n6, upd_other _ 5 i _ n5]
end Found.Pnp29
#print axioms Found.Pnp29.len_seq
#print axioms Found.Pnp29.len_body
#print axioms Found.Pnp29.len_jloop
#print axioms Found.Pnp29.assoc10
#print axioms Found.Pnp29.len_rnd
#print axioms Found.Pnp29.ne_ge
#print axioms Found.Pnp29.rnd_run
#print axioms Found.Pnp29.rndF_keep
#print axioms: 8 theorems, no axioms.
The time loop runs one round per time step; from \(t=0\) over \(T\) rounds it pushes the encoding of all window clauses of the tableau, in the order of the tableau CNF.
found/Pnp30.lean
/-
T330 piece 30. found/Pnp30 -- the time loop: all window clauses of the tableau (CL3b, 11).
TLOOP: pop register 11 (T ones) -> exit / one time round (Pnp29), jump back. Proved:
* the round invariant (scratch registers empty, 3 = t, 4 = t + 1 for some t, 9 = W,
10 = W - 1) is kept, and the round keeps register 11;
* after the loop has run over T ones from t = 0, register 0 holds (pushed) the
encoding of all window clauses of the tableau, Pnp9.windows, in order: the
encList of windows delta k W T equals the rows written round by round.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp29
namespace Found.Pnp30
open Found.Pnp8 (window)
open Found.Pnp9 (nbV forR windows)
open Found.Pnp20
open Found.Pnp21
open Found.Pnp22 (steps iter)
open Found.Pnp23 (tr tr_len mapI appI app_nil'' revApp_app)
open Found.Pnp25 (encList encList_app)
open Found.Pnp26 (winJ loop_inv)
open Found.Pnp28 (seqI)
open Found.Pnp29
/-! ## Rows -/
def row (k : Nat) (delta : List Bool -> Nat -> Bool) (W' t : Nat) : List Bool :=
encList (X k delta (W' + 1) t 0) ++ winJ k delta (W' + 1) t 1 W'
def tRows (k : Nat) (delta : List Bool -> Nat -> Bool) (W' : Nat) : Nat -> Nat -> List Bool
| _, 0 => []
| t, m + 1 => row k delta W' t ++ tRows k delta W' (t + 1) m
theorem app_assoc3 (A B C : List Bool) : (A ++ B) ++ C = A ++ (B ++ C) := Found.Pnp19.app_assoc' A B C
theorem winJ_snoc (k : Nat) (delta : List Bool -> Nat -> Bool) (W t : Nat) :
forall m a, winJ k delta W t a (m + 1) = winJ k delta W t a m ++ encList (X k delta W t (a + m)) := by
intro m
induction m with
| zero => intro a; show encList (X k delta W t a) ++ [] = [] ++ encList (X k delta W t (a + 0)); rw [app_nil'']; rfl
| succ m ih =>
intro a
show encList (X k delta W t a) ++ winJ k delta W t (a + 1) (m + 1) =
(encList (X k delta W t a) ++ winJ k delta W t (a + 1) m) ++ encList (X k delta W t (a + (m + 1)))
rw [ih (a + 1), app_assoc3, Nat.add_right_comm a 1 m]
rfl
theorem row_eq (k : Nat) (delta : List Bool -> Nat -> Bool) (W' t : Nat) :
forall m, encList (forR (m + 1) fun j => X k delta (W' + 1) t j) =
encList (X k delta (W' + 1) t 0) ++ winJ k delta (W' + 1) t 1 m := by
intro m
induction m with
| zero => show encList ([] ++ X k delta (W' + 1) t 0) = encList (X k delta (W' + 1) t 0) ++ []; rw [app_nil'']; rfl
| succ m ih =>
show encList (forR (m + 1) (fun j => X k delta (W' + 1) t j) ++ X k delta (W' + 1) t (m + 1)) = _
rw [encList_app, ih, winJ_snoc, app_assoc3, Nat.add_comm 1 m]
theorem tRows_snoc (k : Nat) (delta : List Bool -> Nat -> Bool) (W' : Nat) :
forall m t, tRows k delta W' t (m + 1) = tRows k delta W' t m ++ row k delta W' (t + m) := by
intro m
induction m with
| zero => intro t; show row k delta W' t ++ [] = [] ++ row k delta W' (t + 0); rw [app_nil'']; rfl
| succ m ih =>
intro t
show row k delta W' t ++ tRows k delta W' (t + 1) (m + 1) = (row k delta W' t ++ tRows k delta W' (t + 1) m) ++ _
rw [ih (t + 1), app_assoc3, Nat.add_right_comm t 1 m]
rfl
/-- The window clauses of the tableau are the rows, in order. -/
theorem windows_rows (k : Nat) (delta : List Bool -> Nat -> Bool) (W' : Nat) :
forall T, encList (windows delta k (W' + 1) T) = tRows k delta W' 0 T := by
intro T
induction T with
| zero => rfl
| succ T ih =>
show encList (windows delta k (W' + 1) T ++ forR (W' + 1) (fun j => X k delta (W' + 1) T j)) = _
rw [encList_app, ih, row_eq, tRows_snoc, Nat.zero_add]
rfl
/-! ## The time loop -/
def tloop (k : Nat) (delta : List Bool -> Nat -> Bool) : List Ins :=
appI (appI [.pop 11 ((rnd k delta).length + 2) 1 1] (mapI (shift 1) (rnd k delta))) [.jmp 0]
def Pt (W' : Nat) (x : Nat -> List Bool) : Prop :=
x 1 = [] /\ x 2 = [] /\ (Exists fun t => x 3 = tr t /\ x 4 = tr (t + 1)) /\ x 9 = tr (W' + 1) /\ x 10 = tr W'
/-- What a round does to the registers (no program needed). -/
theorem rnd_facts (k : Nat) (delta : List Bool -> Nat -> Bool) (W' t : Nat) (x : Nat -> List Bool)
(h1 : x 1 = []) (h2 : x 2 = []) (h3 : x 3 = tr t) (h4 : x 4 = tr (t + 1)) :
rndF k delta W' x 0 = revApp (row k delta W' t) (x 0) /\ rndF k delta W' x 1 = [] /\ rndF k delta W' x 2 = [] /\
rndF k delta W' x 3 = tr (t + 1) /\ rndF k delta W' x 4 = tr (t + 2) := by
have h82 : st8 k delta W' x 2 = tr W' := by show tr W' ++ x 2 = tr W'; rw [h2, app_nil'']
have sp := Found.Pnp26.iter_spec k delta (W' + 1) t W' 0 (st8 k delta W' x) h82 h3 h4 rfl rfl rfl
have c3 : Found.Pnp26.cntOf (st5 W' x) 3 = t := by show (x 3).length = t; rw [h3, tr_len]
have c4 : Found.Pnp26.cntOf (st5 W' x) 4 = t + 1 := by show (x 4).length = t + 1; rw [h4, tr_len]
have c5 : Found.Pnp26.cntOf (st5 W' x) 5 = Found.Pnp9.leftIdx (W' + 1) 0 := by
show (tr (W' + 1) ++ []).length = W' + 1; rw [app_nil'', tr_len]
have hb := Found.Pnp25.bOps_bits (Found.Pnp26.cntOf (st5 W' x)) 3 4 5 6 7 k (W' + 1) t 0 delta c3 c4 c5 rfl rfl k
refine And.intro ?_ (And.intro (sp.2.2.2.2.1.trans h1) (And.intro (Found.Pnp28.iter_two k delta _ _ h82) (And.intro ?_ ?_)))
· show iter 2 (Found.Pnp26.fB k delta) (tr W') (st8 k delta W' x) 0 = _
rw [sp.1]
show revApp (winJ k delta (W' + 1) t (0 + 1) W')
(revApp (Found.Pnp24.bits (Found.Pnp26.cntOf (st5 W' x)) (Found.Pnp25.bOps 3 4 5 6 7 k delta k)) (x 0)) = _
rw [hb, ← revApp_app]
rfl
· show true :: iter 2 (Found.Pnp26.fB k delta) (tr W') (st8 k delta W' x) 3 = tr (t + 1)
rw [sp.2.2.2.2.2.1]; rfl
· show true :: iter 2 (Found.Pnp26.fB k delta) (tr W') (st8 k delta W' x) 4 = tr (t + 2)
rw [sp.2.2.2.2.2.2]; rfl
theorem tIter (k : Nat) (delta : List Bool -> Nat -> Bool) (W' : Nat) :
forall m t (x : Nat -> List Bool), x 1 = [] -> x 2 = [] -> x 3 = tr t -> x 4 = tr (t + 1) ->
iter 11 (rndF k delta W') (tr m) x 0 = revApp (tRows k delta W' t m) (x 0) := by
intro m
induction m with
| zero => intro t x _ _ _ _; rfl
| succ m ih =>
intro t x h1 h2 h3 h4
have r := rnd_facts k delta W' t (upd x 11 (tr m)) h1 h2 h3 h4
show iter 11 (rndF k delta W') (tr m) (rndF k delta W' (upd x 11 (tr m))) 0 = _
rw [ih (t + 1) _ r.2.1 r.2.2.1 r.2.2.2.1 r.2.2.2.2, r.1, ← revApp_app]
rfl
/-- The time loop from o: from t = 0 with register 11 holding T ones, it pushes the
encoding of all window clauses of the tableau onto register 0. -/
theorem tloop_run (prog : List Ins) (o k : Nat) (delta : List Bool -> Nat -> Bool) (W' T : Nat)
(hf : Frag prog o (tloop k delta)) (x : Nat -> List Bool)
(h1 : x 1 = []) (h2 : x 2 = []) (h3 : x 3 = tr 0) (h4 : x 4 = tr 1)
(h9 : x 9 = tr (W' + 1)) (h10 : x 10 = tr W') (h11 : x 11 = tr T) :
Runs prog (steps 11 (rndCost k delta W') (rndF k delta W') (tr T) x) o x (o + ((rnd k delta).length + 2))
(iter 11 (rndF k delta W') (tr T) x) /\
iter 11 (rndF k delta W') (tr T) x 0 = revApp (encList (windows delta k (W' + 1) T)) (x 0) := by
have fa := Found.Pnp23.frag_app_l prog o _ _ hf
have fb : Frag prog (o + 1) (rnd k delta) := Found.Pnp23.frag_app prog o [.pop 11 ((rnd k delta).length + 2) 1 1] _ fa
have i0 : nthI prog o = some (.pop 11 (o + ((rnd k delta).length + 2)) (o + 1) (o + 1)) := fa 0 _ rfl
have hl : (appI [.pop 11 ((rnd k delta).length + 2) 1 1] (mapI (shift 1) (rnd k delta))).length = 1 + (rnd k delta).length := by
rw [Found.Pnp24.len_appI, Found.Pnp24.len_mapI]; rfl
have i1 : nthI prog (o + 1 + (rnd k delta).length) = some (.jmp o) := by
have e := hf ((appI [.pop 11 ((rnd k delta).length + 2) 1 1] (mapI (shift 1) (rnd k delta))).length + 0) (.jmp 0)
(Found.Pnp23.nthI_app_r _ [.jmp 0] 0)
rw [hl, Nat.add_zero, ← Nat.add_assoc] at e
exact e
have hbody : forall y, Pt W' y -> Runs prog (rndCost k delta W' y) (o + 1) y (o + 1 + (rnd k delta).length) (rndF k delta W' y) := by
intro y hy
exact Exists.elim hy.2.2.1 fun t ht => (rnd_run prog (o + 1) k delta W' t fb y hy.1 hy.2.1 ht.1 ht.2 hy.2.2.2.1 hy.2.2.2.2).1
have hP1 : forall y b l, Pt W' y -> y 11 = b :: l -> Pt W' (upd y 11 l) := fun y _ _ hy _ => hy
have hP2 : forall y, Pt W' y -> Pt W' (rndF k delta W' y) := by
intro y hy
exact Exists.elim hy.2.2.1 fun t ht => by
have r := rnd_facts k delta W' t y hy.1 hy.2.1 ht.1 ht.2
exact And.intro r.2.1 (And.intro r.2.2.1 (And.intro (Exists.intro (t + 1) (And.intro r.2.2.2.1 r.2.2.2.2))
(And.intro ((rndF_keep k delta W' y 9 (Nat.le_refl 9)).trans hy.2.2.2.1)
((rndF_keep k delta W' y 10 (Nat.le_succ 9)).trans hy.2.2.2.2))))
have hx : Pt W' x := And.intro h1 (And.intro h2 (And.intro (Exists.intro 0 (And.intro h3 h4)) (And.intro h9 h10)))
have L := loop_inv 11 (rndCost k delta W') (rndF k delta W') (Pt W') prog o (rnd k delta).length
(o + ((rnd k delta).length + 2)) i0 i1 hbody (fun y => rndF_keep k delta W' y 11 (Nat.le_of_ble_eq_true rfl)) hP1 hP2 (tr T) x h11 hx
refine And.intro L ?_
rw [tIter k delta W' T 0 x h1 h2 h3 h4, windows_rows]
end Found.Pnp30
#print axioms Found.Pnp30.app_assoc3
#print axioms Found.Pnp30.winJ_snoc
#print axioms Found.Pnp30.row_eq
#print axioms Found.Pnp30.tRows_snoc
#print axioms Found.Pnp30.windows_rows
#print axioms Found.Pnp30.rnd_facts
#print axioms Found.Pnp30.tIter
#print axioms Found.Pnp30.tloop_run
#print axioms: 8 theorems, no axioms.
The boundary loop runs one straight-line block per time step \(t\le T\), writing the \(k\) unit clauses that force the boundary cell false; from \(t=0\) it pushes the encoding of all boundary clauses of the tableau, in order.
found/Pnp31.lean
/-
T330 piece 31. found/Pnp31 -- the boundary unit clauses of the tableau (CL3b, 12).
The tableau CNF (Pnp9) ends with the unit clauses [(false, (t, (W, b)))] for t <= T,
b < k. For time t (register 3) and the width W (register 9) the k unit clauses of t are
a straight-line block (Pnp24): per bit b, emit [true, true, false], the unary codes of
registers 3 and 9, the fixed unary code of b, and [false]. BLOOP pops register 12
(T + 1 ones) and runs that block, then pushes a one on register 3. Proved:
* the block of time t writes the encodings of the k unit clauses of t, in order;
* the block body runs as a fragment and keeps the invariant (register 1 empty,
register 3 = t for some t, register 9 = W) and register 12;
* from t = 0 with register 12 holding T + 1 ones, BLOOP pushes onto register 0 the
encoding of all boundary clauses of the tableau, Pnp9.bounds, in order;
* the loop changes no register other than 0, 3 and 12.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp30
namespace Found.Pnp31
open Found.Pnp9 (V forR bounds)
open Found.Pnp19 (un app_assoc')
open Found.Pnp20
open Found.Pnp21
open Found.Pnp22 (steps iter)
open Found.Pnp23 (tr tr_len mapI appI app_nil'' revApp_app)
open Found.Pnp24 (Op cmp bits cost Good block_run)
open Found.Pnp25 (encList encList_app bits_app)
open Found.Pnp26 (cntOf loop_inv)
open Found.Pnp28 (seqI frag_seq)
/-! ## The block of one time -/
/-- The unit clauses of time t. -/
def U (k W t : Nat) : List (List (Bool × V)) := forR k fun b => [[(false, (t, (W, b)))]]
def uOps (RT RW b : Nat) : List Op := [.emit [true, true, false], .un RT, .un RW, .emit (un b), .emit [false]]
theorem u_bits (cnt : Nat -> Nat) (RT RW t W b : Nat) (hT : cnt RT = t) (hW : cnt RW = W) :
bits cnt (uOps RT RW b) = encList [[(false, (t, (W, b)))]] := by
show [true, true, false] ++ (un (cnt RT) ++ (un (cnt RW) ++ (un b ++ ([false] ++ [])))) = _
rw [hT, hW]
show true :: true :: false :: (un t ++ (un W ++ (un b ++ [false]))) =
true :: true :: false :: (((un t ++ (un W ++ un b)) ++ [false]) ++ [])
rw [app_nil'', app_assoc', app_assoc']
def bdOps (RT RW : Nat) : Nat -> List Op
| 0 => []
| b + 1 => bdOps RT RW b ++ uOps RT RW b
theorem bd_bits (cnt : Nat -> Nat) (RT RW t W : Nat) (hT : cnt RT = t) (hW : cnt RW = W) :
forall n, bits cnt (bdOps RT RW n) = encList (U n W t) := by
intro n
induction n with
| zero => rfl
| succ n ih =>
show bits cnt (bdOps RT RW n ++ uOps RT RW n) = encList (U n W t ++ [[(false, (t, (W, n)))]])
rw [bits_app, ih, encList_app, u_bits cnt RT RW t W n hT hW]
theorem good_app (x : Nat -> List Bool) (cnt : Nat -> Nat) :
forall (A B : List Op), Good 1 0 x cnt A -> Good 1 0 x cnt B -> Good 1 0 x cnt (A ++ B) := by
intro A
induction A with
| nil => intro B _ hB; exact hB
| cons op A ih =>
intro B hA hB
cases op with
| emit w => exact ih B hA hB
| un R => exact And.intro hA.1 (ih B hA.2 hB)
theorem good_bd (x : Nat -> List Bool) (t W : Nat) (h3 : x 3 = tr t) (h9 : x 9 = tr W) :
forall n, Good 1 0 x (cntOf x) (bdOps 3 9 n) := by
have c3 : x 3 = tr (cntOf x 3) := Found.Pnp26.tr_cnt x 3 t h3
have c9 : x 9 = tr (cntOf x 9) := Found.Pnp26.tr_cnt x 9 W h9
intro n
induction n with
| zero => exact True.intro
| succ n ih =>
exact good_app x (cntOf x) _ _ ih
(And.intro (And.intro rfl (And.intro rfl c3)) (And.intro (And.intro rfl (And.intro rfl c9)) True.intro))
/-! ## The body: the block, then t := t + 1 -/
def bbody (k : Nat) : List Ins := seqI (cmp 1 0 (bdOps 3 9 k)) [.push 3 true]
def fb (k : Nat) (x : Nat -> List Bool) : Nat -> List Bool :=
let y := upd x 0 (revApp (bits (cntOf x) (bdOps 3 9 k)) (x 0))
upd y 3 (true :: y 3)
def nbb (k : Nat) (x : Nat -> List Bool) : Nat := cost (cntOf x) (bdOps 3 9 k) + 1
theorem len_bbody (k : Nat) : (bbody k).length = (cmp 1 0 (bdOps 3 9 k)).length + 1 :=
Found.Pnp29.len_seq _ _
theorem bbody_run (prog : List Ins) (o k t W : Nat) (hf : Frag prog o (bbody k)) (x : Nat -> List Bool)
(h1 : x 1 = []) (h3 : x 3 = tr t) (h9 : x 9 = tr W) :
Runs prog (nbb k x) o x (o + (bbody k).length) (fb k x) := by
have g := frag_seq prog o _ _ hf
have r1 := block_run 1 0 prog rfl (cntOf x) (bdOps 3 9 k) o x g.1 h1 (good_bd x t W h3 h9 k)
have i0 : nthI prog (o + (cmp 1 0 (bdOps 3 9 k)).length) = some (.push 3 true) := g.2 0 _ rfl
have r2 : Runs prog 1 (o + (cmp 1 0 (bdOps 3 9 k)).length) (upd x 0 (revApp (bits (cntOf x) (bdOps 3 9 k)) (x 0)))
(o + (cmp 1 0 (bdOps 3 9 k)).length + 1) (fb k x) := by
show (run prog 0 (exec (Cfg.mk (o + (cmp 1 0 (bdOps 3 9 k)).length) _) (nthI prog (o + (cmp 1 0 (bdOps 3 9 k)).length)))).pc = _ /\
forall i, (run prog 0 (exec (Cfg.mk (o + (cmp 1 0 (bdOps 3 9 k)).length)
(upd x 0 (revApp (bits (cntOf x) (bdOps 3 9 k)) (x 0)))) (nthI prog (o + (cmp 1 0 (bdOps 3 9 k)).length)))).regs i = _
rw [i0]
exact And.intro rfl (fun _ => rfl)
have r3 := runs_seq prog _ _ _ _ _ x _ _ r1 r2
rw [len_bbody, <- Nat.add_assoc]
exact r3
/-- What the body does to the registers (no program needed). -/
theorem fb_facts (k t W : Nat) (x : Nat -> List Bool) (h1 : x 1 = []) (h3 : x 3 = tr t) (h9 : x 9 = tr W) :
fb k x 0 = revApp (encList (U k W t)) (x 0) /\ fb k x 1 = [] /\ fb k x 3 = tr (t + 1) /\ fb k x 9 = tr W := by
have c3 : cntOf x 3 = t := by show (x 3).length = t; rw [h3, tr_len]
have c9 : cntOf x 9 = W := by show (x 9).length = W; rw [h9, tr_len]
refine And.intro ?_ (And.intro h1 (And.intro ?_ h9))
· show revApp (bits (cntOf x) (bdOps 3 9 k)) (x 0) = _
rw [bd_bits (cntOf x) 3 9 t W c3 c9 k]
· show true :: x 3 = tr (t + 1)
rw [h3]
rfl
/-- The body changes no register other than 0 and 3. -/
theorem fb_keep (k : Nat) (x : Nat -> List Bool) (i : Nat) (h0 : Nat.beq i 0 = false) (h3 : Nat.beq i 3 = false) :
fb k x i = x i :=
(upd_other _ 3 i _ h3).trans (upd_other x 0 i _ h0)
/-! ## The loop -/
def bloop (k : Nat) : List Ins :=
appI (appI [.pop 12 ((bbody k).length + 2) 1 1] (mapI (shift 1) (bbody k))) [.jmp 0]
def bRows (k W : Nat) : Nat -> Nat -> List Bool
| _, 0 => []
| t, m + 1 => encList (U k W t) ++ bRows k W (t + 1) m
theorem bRows_snoc (k W : Nat) :
forall m t, bRows k W t (m + 1) = bRows k W t m ++ encList (U k W (t + m)) := by
intro m
induction m with
| zero => intro t; show encList (U k W t) ++ [] = [] ++ encList (U k W (t + 0)); rw [app_nil'']; rfl
| succ m ih =>
intro t
show encList (U k W t) ++ bRows k W (t + 1) (m + 1) = (encList (U k W t) ++ bRows k W (t + 1) m) ++ _
rw [ih (t + 1), app_assoc', Nat.add_right_comm t 1 m]
rfl
theorem bounds_rows (k W : Nat) : forall n, encList (forR n fun t => U k W t) = bRows k W 0 n := by
intro n
induction n with
| zero => rfl
| succ n ih =>
show encList (forR n (fun t => U k W t) ++ U k W n) = _
rw [encList_app, ih, bRows_snoc, Nat.zero_add]
theorem bIter (k W : Nat) :
forall m t (x : Nat -> List Bool), x 1 = [] -> x 3 = tr t -> x 9 = tr W ->
iter 12 (fb k) (tr m) x 0 = revApp (bRows k W t m) (x 0) := by
intro m
induction m with
| zero => intro t x _ _ _; rfl
| succ m ih =>
intro t x h1 h3 h9
have r := fb_facts k t W (upd x 12 (tr m)) h1 h3 h9
show iter 12 (fb k) (tr m) (fb k (upd x 12 (tr m))) 0 = _
rw [ih (t + 1) _ r.2.1 r.2.2.1 r.2.2.2, r.1, <- revApp_app]
rfl
theorem iter_keep (k : Nat) (i : Nat) (h0 : Nat.beq i 0 = false) (h3 : Nat.beq i 3 = false) (h12 : Nat.beq i 12 = false) :
forall (l : List Bool) (x : Nat -> List Bool), iter 12 (fb k) l x i = x i := by
intro l
induction l with
| nil => intro x; rfl
| cons b l ih =>
intro x
show iter 12 (fb k) l (fb k (upd x 12 l)) i = x i
rw [ih, fb_keep k _ i h0 h3]
exact upd_other x 12 i l h12
def Pb (W : Nat) (x : Nat -> List Bool) : Prop :=
x 1 = [] /\ (Exists fun t => x 3 = tr t) /\ x 9 = tr W
/-- The boundary loop from o: from t = 0 with register 12 holding T + 1 ones, it pushes
the encoding of all boundary clauses of the tableau onto register 0. -/
theorem bloop_run (prog : List Ins) (o k W T : Nat) (hf : Frag prog o (bloop k)) (x : Nat -> List Bool)
(h1 : x 1 = []) (h3 : x 3 = tr 0) (h9 : x 9 = tr W) (h12 : x 12 = tr (T + 1)) :
Runs prog (steps 12 (nbb k) (fb k) (tr (T + 1)) x) o x (o + ((bbody k).length + 2)) (iter 12 (fb k) (tr (T + 1)) x) /\
iter 12 (fb k) (tr (T + 1)) x 0 = revApp (encList (bounds k W T)) (x 0) := by
have fa := Found.Pnp23.frag_app_l prog o _ _ hf
have fbd : Frag prog (o + 1) (bbody k) := Found.Pnp23.frag_app prog o [.pop 12 ((bbody k).length + 2) 1 1] _ fa
have i0 : nthI prog o = some (.pop 12 (o + ((bbody k).length + 2)) (o + 1) (o + 1)) := fa 0 _ rfl
have hl : (appI [.pop 12 ((bbody k).length + 2) 1 1] (mapI (shift 1) (bbody k))).length = 1 + (bbody k).length := by
rw [Found.Pnp24.len_appI, Found.Pnp24.len_mapI]; rfl
have i1 : nthI prog (o + 1 + (bbody k).length) = some (.jmp o) := by
have e := hf ((appI [.pop 12 ((bbody k).length + 2) 1 1] (mapI (shift 1) (bbody k))).length + 0) (.jmp 0)
(Found.Pnp23.nthI_app_r _ [.jmp 0] 0)
rw [hl, Nat.add_zero, <- Nat.add_assoc] at e
exact e
have hbody : forall y, Pb W y -> Runs prog (nbb k y) (o + 1) y (o + 1 + (bbody k).length) (fb k y) := by
intro y hy
exact Exists.elim hy.2.1 fun t ht => bbody_run prog (o + 1) k t W fbd y hy.1 ht hy.2.2
have hP1 : forall y b l, Pb W y -> y 12 = b :: l -> Pb W (upd y 12 l) := fun y _ _ hy _ => hy
have hP2 : forall y, Pb W y -> Pb W (fb k y) := by
intro y hy
exact Exists.elim hy.2.1 fun t ht => by
have r := fb_facts k t W y hy.1 ht hy.2.2
exact And.intro r.2.1 (And.intro (Exists.intro (t + 1) r.2.2.1) r.2.2.2)
have hx : Pb W x := And.intro h1 (And.intro (Exists.intro 0 h3) h9)
have L := loop_inv 12 (nbb k) (fb k) (Pb W) prog o (bbody k).length (o + ((bbody k).length + 2)) i0 i1 hbody
(fun y => fb_keep k y 12 rfl rfl) hP1 hP2 (tr (T + 1)) x h12 hx
refine And.intro L ?_
rw [bIter k W (T + 1) 0 x h1 h3 h9]
show revApp (bRows k W 0 (T + 1)) (x 0) = revApp (encList (forR (T + 1) fun t => U k W t)) (x 0)
rw [bounds_rows]
end Found.Pnp31
#print axioms Found.Pnp31.u_bits
#print axioms Found.Pnp31.bd_bits
#print axioms Found.Pnp31.good_app
#print axioms Found.Pnp31.good_bd
#print axioms Found.Pnp31.len_bbody
#print axioms Found.Pnp31.bbody_run
#print axioms Found.Pnp31.fb_facts
#print axioms Found.Pnp31.fb_keep
#print axioms Found.Pnp31.bRows_snoc
#print axioms Found.Pnp31.bounds_rows
#print axioms Found.Pnp31.bIter
#print axioms Found.Pnp31.iter_keep
#print axioms Found.Pnp31.bloop_run
#print axioms: 13 theorems, no axioms.
A loop that pops a bit and branches to one of two bodies writes, for the input \(x\) held in a register, the pin clause of every input bit at cells \(s, s+1, \dots \), in order.
found/Pnp32.lean
/-
T330 piece 32. found/Pnp32 -- the input pin clauses (CL3b, 13).
A branching loop: pop the counter register c; empty -> exit, bit 0 -> body 0, bit 1 ->
body 1; each body jumps back. With the input x held in register 13 (one bit per entry)
and the cell of the first input bit in register 14 (ones), body v writes the pin clause
[(v, (0, (j, 0)))] of the current cell j (a block, Pnp24) and pushes a one on 14.
Proved:
* the branching loop with an invariant (general: any two bodies, any registers);
* from register 14 = s, the input loop pushes onto register 0 the encoding of the pin
clauses of the input pins of x placed at cells s, s + 1, ..., in order:
pinCl (inPins (inpOf s x)) (Pnp11, Pnp14);
* it changes no register other than 0, 13 and 14.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp31
import found.Pnp14
namespace Found.Pnp32
open Found.Pnp9 (V)
open Found.Pnp11 (pinCl)
open Found.Pnp14 (inPins)
open Found.Pnp19 (un app_assoc')
open Found.Pnp20
open Found.Pnp21
open Found.Pnp23 (tr tr_len mapI appI app_nil'' revApp_app)
open Found.Pnp24 (Op cmp bits cost Good block_run)
open Found.Pnp25 (encList encList_app)
open Found.Pnp26 (cntOf)
open Found.Pnp28 (seqI frag_seq)
/-! ## The branching loop -/
section
variable (c : Nat) (nb : Bool -> (Nat -> List Bool) -> Nat) (f : Bool -> (Nat -> List Bool) -> (Nat -> List Bool))
def steps2 : List Bool -> (Nat -> List Bool) -> Nat
| [], _ => 1
| b :: l, x => ((1 + nb b (upd x c l)) + 1) + steps2 l (f b (upd x c l))
def iter2 : List Bool -> (Nat -> List Bool) -> (Nat -> List Bool)
| [], x => x
| b :: l, x => iter2 l (f b (upd x c l))
theorem loop2_inv (P : (Nat -> List Bool) -> Prop) (prog : List Ins) (o z1 e0 e1 ex : Nat)
(hpop : nthI prog o = some (.pop c ex (o + 1) z1))
(hj0 : nthI prog e0 = some (.jmp o)) (hj1 : nthI prog e1 = some (.jmp o))
(hb0 : forall x, P x -> Runs prog (nb false x) (o + 1) x e0 (f false x))
(hb1 : forall x, P x -> Runs prog (nb true x) z1 x e1 (f true x))
(hkeep : forall b x, f b x c = x c)
(hP1 : forall x b l, P x -> x c = b :: l -> P (upd x c l))
(hP2 : forall b x, P x -> P (f b x)) :
forall (l : List Bool) (x : Nat -> List Bool), x c = l -> P x ->
Runs prog (steps2 c nb f l x) o x ex (iter2 c f l x) := by
intro l
induction l with
| nil =>
intro x hx _
show (run prog 0 (exec (Cfg.mk o x) (nthI prog o))).pc = ex /\
forall i, (run prog 0 (exec (Cfg.mk o x) (nthI prog o))).regs i = x i
rw [hpop]
show (popTo (Cfg.mk o x) c ex (o + 1) z1 (x c)).pc = ex /\
forall i, (popTo (Cfg.mk o x) c ex (o + 1) z1 (x c)).regs i = x i
rw [hx]
exact And.intro rfl (fun _ => rfl)
| cons b l ih =>
intro x hx hPx
have hP' := hP1 x b l hPx hx
have hc : f b (upd x c l) c = l := (hkeep b _).trans (upd_same x c l)
have h4 := ih (f b (upd x c l)) hc (hP2 b _ hP')
cases b with
| false =>
have h1 : Runs prog 1 o x (o + 1) (upd x c l) := by
show (run prog 0 (exec (Cfg.mk o x) (nthI prog o))).pc = o + 1 /\
forall i, (run prog 0 (exec (Cfg.mk o x) (nthI prog o))).regs i = upd x c l i
rw [hpop]
show (popTo (Cfg.mk o x) c ex (o + 1) z1 (x c)).pc = o + 1 /\
forall i, (popTo (Cfg.mk o x) c ex (o + 1) z1 (x c)).regs i = upd x c l i
rw [hx]
exact And.intro rfl (fun _ => rfl)
have h2 := hb0 (upd x c l) hP'
have h3 : Runs prog 1 e0 (f false (upd x c l)) o (f false (upd x c l)) := by
show (run prog 0 (exec (Cfg.mk e0 (f false (upd x c l))) (nthI prog e0))).pc = o /\
forall i, (run prog 0 (exec (Cfg.mk e0 (f false (upd x c l))) (nthI prog e0))).regs i = f false (upd x c l) i
rw [hj0]
exact And.intro rfl (fun _ => rfl)
exact runs_seq prog _ _ o o ex x _ _
(runs_seq prog _ _ o e0 o x _ _ (runs_seq prog _ _ o (o + 1) e0 x _ _ h1 h2) h3) h4
| true =>
have h1 : Runs prog 1 o x z1 (upd x c l) := by
show (run prog 0 (exec (Cfg.mk o x) (nthI prog o))).pc = z1 /\
forall i, (run prog 0 (exec (Cfg.mk o x) (nthI prog o))).regs i = upd x c l i
rw [hpop]
show (popTo (Cfg.mk o x) c ex (o + 1) z1 (x c)).pc = z1 /\
forall i, (popTo (Cfg.mk o x) c ex (o + 1) z1 (x c)).regs i = upd x c l i
rw [hx]
exact And.intro rfl (fun _ => rfl)
have h2 := hb1 (upd x c l) hP'
have h3 : Runs prog 1 e1 (f true (upd x c l)) o (f true (upd x c l)) := by
show (run prog 0 (exec (Cfg.mk e1 (f true (upd x c l))) (nthI prog e1))).pc = o /\
forall i, (run prog 0 (exec (Cfg.mk e1 (f true (upd x c l))) (nthI prog e1))).regs i = f true (upd x c l) i
rw [hj1]
exact And.intro rfl (fun _ => rfl)
exact runs_seq prog _ _ o o ex x _ _
(runs_seq prog _ _ o e1 o x _ _ (runs_seq prog _ _ o z1 e1 x _ _ h1 h2) h3) h4
end
/-! ## Input pins -/
/-- The input pins of x placed at cells s, s + 1, .... -/
def inpOf : Nat -> List Bool -> List (Nat × Bool)
| _, [] => []
| s, b :: l => (s, b) :: inpOf (s + 1) l
def pOps (v : Bool) : List Op := [.emit [true, true, v, false], .un 14, .emit [false, false]]
theorem p_bits (cnt : Nat -> Nat) (v : Bool) (j : Nat) (h : cnt 14 = j) :
bits cnt (pOps v) = encList [[(v, (0, (j, 0)))]] := by
show [true, true, v, false] ++ (un (cnt 14) ++ ([false, false] ++ [])) = _
rw [h]
show true :: true :: v :: false :: (un j ++ ([false] ++ [false])) =
true :: true :: v :: false :: (((un j ++ [false]) ++ [false]) ++ [])
rw [app_nil'', app_assoc']
def pbody (v : Bool) : List Ins := seqI (cmp 1 0 (pOps v)) [.push 14 true]
def fp (v : Bool) (x : Nat -> List Bool) : Nat -> List Bool :=
let y := upd x 0 (revApp (bits (cntOf x) (pOps v)) (x 0))
upd y 14 (true :: y 14)
def nbp (v : Bool) (x : Nat -> List Bool) : Nat := cost (cntOf x) (pOps v) + 1
theorem pbody_run (prog : List Ins) (o s : Nat) (v : Bool) (hf : Frag prog o (pbody v)) (x : Nat -> List Bool)
(h1 : x 1 = []) (h14 : x 14 = tr s) :
Runs prog (nbp v x) o x (o + 17) (fp v x) := by
have g := frag_seq prog o _ _ hf
have hg : Good 1 0 x (cntOf x) (pOps v) :=
And.intro (And.intro rfl (And.intro rfl (Found.Pnp26.tr_cnt x 14 s h14))) True.intro
have r1 := block_run 1 0 prog rfl (cntOf x) (pOps v) o x g.1 h1 hg
have i0 : nthI prog (o + (cmp 1 0 (pOps v)).length) = some (.push 14 true) := g.2 0 _ rfl
have r2 : Runs prog 1 (o + (cmp 1 0 (pOps v)).length) (upd x 0 (revApp (bits (cntOf x) (pOps v)) (x 0)))
(o + (cmp 1 0 (pOps v)).length + 1) (fp v x) := by
show (run prog 0 (exec (Cfg.mk (o + (cmp 1 0 (pOps v)).length) _) (nthI prog (o + (cmp 1 0 (pOps v)).length)))).pc = _ /\
forall i, (run prog 0 (exec (Cfg.mk (o + (cmp 1 0 (pOps v)).length)
(upd x 0 (revApp (bits (cntOf x) (pOps v)) (x 0)))) (nthI prog (o + (cmp 1 0 (pOps v)).length)))).regs i = _
rw [i0]
exact And.intro rfl (fun _ => rfl)
exact runs_seq prog _ _ _ _ _ x _ _ r1 r2
theorem fp_facts (v : Bool) (s : Nat) (x : Nat -> List Bool) (h1 : x 1 = []) (h14 : x 14 = tr s) :
fp v x 0 = revApp (encList [[(v, (0, (s, 0)))]]) (x 0) /\ fp v x 1 = [] /\ fp v x 14 = tr (s + 1) := by
have c14 : cntOf x 14 = s := by show (x 14).length = s; rw [h14, tr_len]
refine And.intro ?_ (And.intro h1 ?_)
· show revApp (bits (cntOf x) (pOps v)) (x 0) = _
rw [p_bits (cntOf x) v s c14]
· show true :: x 14 = tr (s + 1)
rw [h14]
rfl
theorem fp_keep (v : Bool) (x : Nat -> List Bool) (i : Nat) (h0 : Nat.beq i 0 = false) (h14 : Nat.beq i 14 = false) :
fp v x i = x i :=
(upd_other _ 14 i _ h14).trans (upd_other x 0 i _ h0)
/-! ## The input loop -/
def inLoop : List Ins :=
appI (appI (appI (appI [.pop 13 37 1 19] (mapI (shift 1) (pbody false))) [.jmp 0]) (mapI (shift 19) (pbody true))) [.jmp 0]
theorem pIter : forall (l : List Bool) (s : Nat) (x : Nat -> List Bool), x 1 = [] -> x 14 = tr s ->
iter2 13 fp l x 0 = revApp (encList (pinCl (inPins (inpOf s l)))) (x 0) := by
intro l
induction l with
| nil => intro s x _ _; rfl
| cons b l ih =>
intro s x h1 h14
have r := fp_facts b s (upd x 13 l) h1 h14
show iter2 13 fp l (fp b (upd x 13 l)) 0 = _
rw [ih (s + 1) _ r.2.1 r.2.2, r.1, <- revApp_app, <- encList_app]
rfl
theorem pIter_keep (i : Nat) (h0 : Nat.beq i 0 = false) (h13 : Nat.beq i 13 = false) (h14 : Nat.beq i 14 = false) :
forall (l : List Bool) (x : Nat -> List Bool), iter2 13 fp l x i = x i := by
intro l
induction l with
| nil => intro x; rfl
| cons b l ih =>
intro x
show iter2 13 fp l (fp b (upd x 13 l)) i = x i
rw [ih, fp_keep b _ i h0 h14]
exact upd_other x 13 i l h13
def Pp (x : Nat -> List Bool) : Prop := x 1 = [] /\ Exists fun s => x 14 = tr s
/-- The input loop from o: with the input l in register 13 and register 14 = s, it pushes
the encoding of the pin clauses of the input pins at cells s, s + 1, ... onto register 0. -/
theorem inLoop_run (prog : List Ins) (o s : Nat) (hf : Frag prog o inLoop) (l : List Bool) (x : Nat -> List Bool)
(h1 : x 1 = []) (h13 : x 13 = l) (h14 : x 14 = tr s) :
Runs prog (steps2 13 nbp fp l x) o x (o + 37) (iter2 13 fp l x) /\
iter2 13 fp l x 0 = revApp (encList (pinCl (inPins (inpOf s l)))) (x 0) := by
have f2 := Found.Pnp23.frag_app_l prog o _ _ hf
have f1 := Found.Pnp23.frag_app_l prog o _ _ f2
have f0 := Found.Pnp23.frag_app_l prog o _ _ f1
have fb1 : Frag prog (o + 19) (pbody true) := Found.Pnp23.frag_app prog o _ _ f2
have fb0 : Frag prog (o + 1) (pbody false) := Found.Pnp23.frag_app prog o [.pop 13 37 1 19] _ f0
have i0 : nthI prog o = some (.pop 13 (o + 37) (o + 1) (o + 19)) := hf 0 _ rfl
have i1 : nthI prog (o + 1 + 17) = some (.jmp o) := hf 18 _ rfl
have i2 : nthI prog (o + 19 + 17) = some (.jmp o) := hf 36 _ rfl
have hb0 : forall y, Pp y -> Runs prog (nbp false y) (o + 1) y (o + 1 + 17) (fp false y) := by
intro y hy
exact Exists.elim hy.2 fun t ht => pbody_run prog (o + 1) t false fb0 y hy.1 ht
have hb1 : forall y, Pp y -> Runs prog (nbp true y) (o + 19) y (o + 19 + 17) (fp true y) := by
intro y hy
exact Exists.elim hy.2 fun t ht => pbody_run prog (o + 19) t true fb1 y hy.1 ht
have hP2 : forall b y, Pp y -> Pp (fp b y) := by
intro b y hy
exact Exists.elim hy.2 fun t ht => by
have r := fp_facts b t y hy.1 ht
exact And.intro r.2.1 (Exists.intro (t + 1) r.2.2)
have L := loop2_inv 13 nbp fp Pp prog o (o + 19) (o + 1 + 17) (o + 19 + 17) (o + 37) i0 i1 i2 hb0 hb1
(fun b y => fp_keep b y 13 rfl rfl) (fun y _ _ hy _ => hy) hP2 l x h13 (And.intro h1 (Exists.intro s h14))
exact And.intro L (pIter l s x h1 h14)
end Found.Pnp32
#print axioms Found.Pnp32.loop2_inv
#print axioms Found.Pnp32.p_bits
#print axioms Found.Pnp32.pbody_run
#print axioms Found.Pnp32.fp_facts
#print axioms Found.Pnp32.fp_keep
#print axioms Found.Pnp32.pIter
#print axioms Found.Pnp32.pIter_keep
#print axioms Found.Pnp32.inLoop_run
#print axioms: 8 theorems, no axioms.
The accept clause is one block; the layout pins of cells \(0,\dots ,p_0\) are a fixed block, and every other cell is pinned to “no head”, written by a cell loop.
found/Pnp33.lean
/-
T330 piece 33. found/Pnp33 -- the accept clause and the layout pins (CL3b, 14).
After the window and boundary clauses, the tableau CNF of a Turing machine (Pnp11,
Pnp14) has the accept clause [(true, (T, (0, 0)))] and the pin clauses of the layout
pins allPins pos0 q0 m W (cell pos0 holds the head and the start state q0, every other
cell is pinned to "no head"). Here W = pos0 + 1 + r. Proved:
* the accept clause is a block that reads T from any counter register R (not 0, 1);
* the layout pins of cells 0 .. pos0 are fixed by the machine, so they are one fixed
EMIT block;
* for a cell j other than pos0 the layout pins are all false (cell_false), and the
layout pins of W = pos0 + 1 + r cells split into the fixed part and r such cells;
* CLOOP (pop register 12, holding r ones; block for cell j in register 14; push a one
on 14) pushes the pin clauses of cells a, a + 1, ..., a + r - 1, and changes no
register other than 0, 12 and 14.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp32
namespace Found.Pnp33
open Found.Pnp9 (V)
open Found.Pnp11 (pinCl accCl)
open Found.Pnp14 (allPins cellPins)
open Found.Pnp19 (un app_assoc' app_nil')
open Found.Pnp20
open Found.Pnp21
open Found.Pnp22 (steps iter)
open Found.Pnp23 (tr tr_len mapI appI app_nil'' revApp_app)
open Found.Pnp24 (Op cmp bits cost Good block_run)
open Found.Pnp25 (encList encList_app bits_app)
open Found.Pnp26 (cntOf loop_inv)
open Found.Pnp28 (seqI frag_seq)
open Found.Pnp31 (good_app)
theorem pinCl_app (B : List (Nat × (Nat × Bool))) : forall A, pinCl (A ++ B) = pinCl A ++ pinCl B := by
intro A
induction A with
| nil => rfl
| cons e A ih =>
cases e with
| mk j bv =>
cases bv with
| mk b v => exact congrArg (fun l => [(v, (0, (j, b)))] :: l) ih
/-! ## The accept clause -/
def accOps (R : Nat) : List Op := [.emit [true, true, true], .un R, .emit [false, false, false]]
theorem acc_bits (cnt : Nat -> Nat) (R T : Nat) (h : cnt R = T) : bits cnt (accOps R) = encList [accCl T] := by
show [true, true, true] ++ (un (cnt R) ++ ([false, false, false] ++ [])) = _
rw [h]
show true :: true :: true :: (un T ++ ([false, false] ++ [false])) =
true :: true :: true :: (((un T ++ [false, false]) ++ [false]) ++ [])
rw [app_nil'', app_assoc']
/-- The accept block, with T in register R. -/
theorem acc_run (prog : List Ins) (o R T : Nat) (hf : Frag prog o (cmp 1 0 (accOps R)))
(h1R : Nat.beq R 1 = false) (h0R : Nat.beq R 0 = false) (x : Nat -> List Bool) (h1 : x 1 = []) (hR : x R = tr T) :
Runs prog (cost (cntOf x) (accOps R)) o x (o + (cmp 1 0 (accOps R)).length)
(upd x 0 (revApp (encList [accCl T]) (x 0))) := by
have hg : Good 1 0 x (cntOf x) (accOps R) :=
And.intro (And.intro h1R (And.intro h0R (Found.Pnp26.tr_cnt x R T hR))) True.intro
have c : cntOf x R = T := by show (x R).length = T; rw [hR, tr_len]
have r := block_run 1 0 prog rfl (cntOf x) (accOps R) o x hf h1 hg
rw [acc_bits (cntOf x) R T c] at r
exact r
/-! ## A fixed block -/
theorem fixed_run (prog : List Ins) (o : Nat) (w : List Bool) (hf : Frag prog o (cmp 1 0 [.emit w]))
(x : Nat -> List Bool) (h1 : x 1 = []) :
Runs prog (cost (cntOf x) [.emit w]) o x (o + (cmp 1 0 [.emit w]).length) (upd x 0 (revApp w (x 0))) := by
have r := block_run 1 0 prog rfl (cntOf x) [.emit w] o x hf h1 True.intro
have e : bits (cntOf x) [.emit w] = w := app_nil'' w
rw [e] at r
exact r
/-! ## Layout pins away from the head cell -/
def fc (j : Nat) : Nat -> List (Nat × (Nat × Bool))
| 0 => [(j, (1, false))]
| i + 1 => fc j i ++ [(j, (i + 2, false))]
theorem cell_false (pos0 : Nat) (q0 : List Bool) (j : Nat) (h : Nat.beq j pos0 = false) :
forall i, cellPins pos0 q0 j i = fc j i := by
intro i
induction i with
| zero => exact congrArg (fun z => [(j, (1, z))]) h
| succ i ih =>
show cellPins pos0 q0 j i ++ [(j, (i + 2, Nat.beq j pos0 && Found.Pnp7.nthd q0 i))] = fc j i ++ [(j, (i + 2, false))]
rw [ih]
exact congrArg (fun z => fc j i ++ [(j, (i + 2, z))]) (congrArg (fun z => z && Found.Pnp7.nthd q0 i) h)
def fcRows (m : Nat) : Nat -> Nat -> List (Nat × (Nat × Bool))
| _, 0 => []
| a, r + 1 => fc a m ++ fcRows m (a + 1) r
theorem fcRows_snoc (m : Nat) : forall r a, fcRows m a (r + 1) = fcRows m a r ++ fc (a + r) m := by
intro r
induction r with
| zero => intro a; show fc a m ++ [] = [] ++ fc (a + 0) m; rw [app_nil']; rfl
| succ r ih =>
intro a
show fc a m ++ fcRows m (a + 1) (r + 1) = (fc a m ++ fcRows m (a + 1) r) ++ _
rw [ih (a + 1), app_assoc', Nat.add_right_comm a 1 r]
rfl
theorem allPins_split (pos0 : Nat) (q0 : List Bool) (m : Nat) :
forall r, allPins pos0 q0 m (pos0 + 1 + r) = allPins pos0 q0 m (pos0 + 1) ++ fcRows m (pos0 + 1) r := by
intro r
induction r with
| zero => show allPins pos0 q0 m (pos0 + 1) = allPins pos0 q0 m (pos0 + 1) ++ []; rw [app_nil']
| succ r ih =>
have hlt : pos0 < pos0 + 1 + r := Nat.lt_of_lt_of_le (Nat.lt_succ_self pos0) (Nat.le_add_right (pos0 + 1) r)
have hne : Nat.beq (pos0 + 1 + r) pos0 = false :=
Found.Pnp2.beq_false_of_ne _ _ (fun e => Nat.lt_irrefl pos0 (Eq.subst (motive := fun z => pos0 < z) e hlt))
show allPins pos0 q0 m (pos0 + 1 + r) ++ cellPins pos0 q0 (pos0 + 1 + r) m = _
rw [ih, cell_false pos0 q0 (pos0 + 1 + r) hne m, fcRows_snoc, app_assoc']
/-! ## The block of one cell -/
def uo (b : Nat) : List Op := [.emit [true, true, false, false], .un 14, .emit (un b ++ [false])]
theorem uo_bits (cnt : Nat -> Nat) (j b : Nat) (h : cnt 14 = j) :
bits cnt (uo b) = encList (pinCl [(j, (b, false))]) := by
show [true, true, false, false] ++ (un (cnt 14) ++ ((un b ++ [false]) ++ [])) = _
rw [h]
show true :: true :: false :: false :: (un j ++ ((un b ++ [false]) ++ [])) =
true :: true :: false :: false :: (((un j ++ un b) ++ [false]) ++ [])
rw [app_nil'', app_nil'', app_assoc']
def fcOps : Nat -> List Op
| 0 => uo 1
| i + 1 => fcOps i ++ uo (i + 2)
theorem fc_bits (cnt : Nat -> Nat) (j : Nat) (h : cnt 14 = j) :
forall i, bits cnt (fcOps i) = encList (pinCl (fc j i)) := by
intro i
induction i with
| zero => exact uo_bits cnt j 1 h
| succ i ih =>
show bits cnt (fcOps i ++ uo (i + 2)) = encList (pinCl (fc j i ++ [(j, (i + 2, false))]))
rw [bits_app, ih, pinCl_app, encList_app, uo_bits cnt j (i + 2) h]
theorem good_fc (x : Nat -> List Bool) (j : Nat) (h14 : x 14 = tr j) : forall i, Good 1 0 x (cntOf x) (fcOps i) := by
have g : forall b, Good 1 0 x (cntOf x) (uo b) := fun _ =>
And.intro (And.intro rfl (And.intro rfl (Found.Pnp26.tr_cnt x 14 j h14))) True.intro
intro i
induction i with
| zero => exact g 1
| succ i ih => exact good_app x (cntOf x) _ _ ih (g (i + 2))
def cbody (m : Nat) : List Ins := seqI (cmp 1 0 (fcOps m)) [.push 14 true]
def fcF (m : Nat) (x : Nat -> List Bool) : Nat -> List Bool :=
let y := upd x 0 (revApp (bits (cntOf x) (fcOps m)) (x 0))
upd y 14 (true :: y 14)
def nbc (m : Nat) (x : Nat -> List Bool) : Nat := cost (cntOf x) (fcOps m) + 1
theorem len_cbody (m : Nat) : (cbody m).length = (cmp 1 0 (fcOps m)).length + 1 := Found.Pnp29.len_seq _ _
theorem cbody_run (prog : List Ins) (o m j : Nat) (hf : Frag prog o (cbody m)) (x : Nat -> List Bool)
(h1 : x 1 = []) (h14 : x 14 = tr j) :
Runs prog (nbc m x) o x (o + (cbody m).length) (fcF m x) := by
have g := frag_seq prog o _ _ hf
have r1 := block_run 1 0 prog rfl (cntOf x) (fcOps m) o x g.1 h1 (good_fc x j h14 m)
have i0 : nthI prog (o + (cmp 1 0 (fcOps m)).length) = some (.push 14 true) := g.2 0 _ rfl
have r2 : Runs prog 1 (o + (cmp 1 0 (fcOps m)).length) (upd x 0 (revApp (bits (cntOf x) (fcOps m)) (x 0)))
(o + (cmp 1 0 (fcOps m)).length + 1) (fcF m x) := by
show (run prog 0 (exec (Cfg.mk (o + (cmp 1 0 (fcOps m)).length) _) (nthI prog (o + (cmp 1 0 (fcOps m)).length)))).pc = _ /\
forall i, (run prog 0 (exec (Cfg.mk (o + (cmp 1 0 (fcOps m)).length)
(upd x 0 (revApp (bits (cntOf x) (fcOps m)) (x 0)))) (nthI prog (o + (cmp 1 0 (fcOps m)).length)))).regs i = _
rw [i0]
exact And.intro rfl (fun _ => rfl)
have r3 := runs_seq prog _ _ _ _ _ x _ _ r1 r2
rw [len_cbody, <- Nat.add_assoc]
exact r3
theorem fcF_facts (m j : Nat) (x : Nat -> List Bool) (h1 : x 1 = []) (h14 : x 14 = tr j) :
fcF m x 0 = revApp (encList (pinCl (fc j m))) (x 0) /\ fcF m x 1 = [] /\ fcF m x 14 = tr (j + 1) := by
have c14 : cntOf x 14 = j := by show (x 14).length = j; rw [h14, tr_len]
refine And.intro ?_ (And.intro h1 ?_)
· show revApp (bits (cntOf x) (fcOps m)) (x 0) = _
rw [fc_bits (cntOf x) j c14 m]
· show true :: x 14 = tr (j + 1)
rw [h14]
rfl
theorem fcF_keep (m : Nat) (x : Nat -> List Bool) (i : Nat) (h0 : Nat.beq i 0 = false) (h14 : Nat.beq i 14 = false) :
fcF m x i = x i :=
(upd_other _ 14 i _ h14).trans (upd_other x 0 i _ h0)
/-! ## The cell loop for the layout pins -/
def cloop (m : Nat) : List Ins :=
appI (appI [.pop 12 ((cbody m).length + 2) 1 1] (mapI (shift 1) (cbody m))) [.jmp 0]
theorem cIter (m : Nat) : forall r a (x : Nat -> List Bool), x 1 = [] -> x 14 = tr a ->
iter 12 (fcF m) (tr r) x 0 = revApp (encList (pinCl (fcRows m a r))) (x 0) := by
intro r
induction r with
| zero => intro a x _ _; rfl
| succ r ih =>
intro a x h1 h14
have q := fcF_facts m a (upd x 12 (tr r)) h1 h14
show iter 12 (fcF m) (tr r) (fcF m (upd x 12 (tr r))) 0 = _
rw [ih (a + 1) _ q.2.1 q.2.2, q.1, <- revApp_app, <- encList_app, <- pinCl_app]
rfl
theorem cIter_keep (m i : Nat) (h0 : Nat.beq i 0 = false) (h12 : Nat.beq i 12 = false) (h14 : Nat.beq i 14 = false) :
forall (l : List Bool) (x : Nat -> List Bool), iter 12 (fcF m) l x i = x i := by
intro l
induction l with
| nil => intro x; rfl
| cons b l ih =>
intro x
show iter 12 (fcF m) l (fcF m (upd x 12 l)) i = x i
rw [ih, fcF_keep m _ i h0 h14]
exact upd_other x 12 i l h12
def Pc (x : Nat -> List Bool) : Prop := x 1 = [] /\ Exists fun j => x 14 = tr j
theorem cloop_run (prog : List Ins) (o m a r : Nat) (hf : Frag prog o (cloop m)) (x : Nat -> List Bool)
(h1 : x 1 = []) (h14 : x 14 = tr a) (h12 : x 12 = tr r) :
Runs prog (steps 12 (nbc m) (fcF m) (tr r) x) o x (o + ((cbody m).length + 2)) (iter 12 (fcF m) (tr r) x) /\
iter 12 (fcF m) (tr r) x 0 = revApp (encList (pinCl (fcRows m a r))) (x 0) := by
have fa := Found.Pnp23.frag_app_l prog o _ _ hf
have fbd : Frag prog (o + 1) (cbody m) := Found.Pnp23.frag_app prog o [.pop 12 ((cbody m).length + 2) 1 1] _ fa
have i0 : nthI prog o = some (.pop 12 (o + ((cbody m).length + 2)) (o + 1) (o + 1)) := fa 0 _ rfl
have hl : (appI [.pop 12 ((cbody m).length + 2) 1 1] (mapI (shift 1) (cbody m))).length = 1 + (cbody m).length := by
rw [Found.Pnp24.len_appI, Found.Pnp24.len_mapI]; rfl
have i1 : nthI prog (o + 1 + (cbody m).length) = some (.jmp o) := by
have e := hf ((appI [.pop 12 ((cbody m).length + 2) 1 1] (mapI (shift 1) (cbody m))).length + 0) (.jmp 0)
(Found.Pnp23.nthI_app_r _ [.jmp 0] 0)
rw [hl, Nat.add_zero, <- Nat.add_assoc] at e
exact e
have hbody : forall y, Pc y -> Runs prog (nbc m y) (o + 1) y (o + 1 + (cbody m).length) (fcF m y) := by
intro y hy
exact Exists.elim hy.2 fun j hj => cbody_run prog (o + 1) m j fbd y hy.1 hj
have hP2 : forall y, Pc y -> Pc (fcF m y) := by
intro y hy
exact Exists.elim hy.2 fun j hj => by
have q := fcF_facts m j y hy.1 hj
exact And.intro q.2.1 (Exists.intro (j + 1) q.2.2)
have L := loop_inv 12 (nbc m) (fcF m) Pc prog o (cbody m).length (o + ((cbody m).length + 2)) i0 i1 hbody
(fun y => fcF_keep m y 12 rfl rfl) (fun y _ _ hy _ => hy) hP2 (tr r) x h12 (And.intro h1 (Exists.intro a h14))
exact And.intro L (cIter m r a x h1 h14)
/-- The layout pins of W = pos0 + 1 + r cells: the fixed part, then what CLOOP writes from
cell pos0 + 1. -/
theorem allPins_enc (pos0 : Nat) (q0 : List Bool) (m r : Nat) :
encList (pinCl (allPins pos0 q0 m (pos0 + 1 + r))) =
encList (pinCl (allPins pos0 q0 m (pos0 + 1))) ++ encList (pinCl (fcRows m (pos0 + 1) r)) := by
rw [allPins_split, pinCl_app, encList_app]
end Found.Pnp33
#print axioms Found.Pnp33.pinCl_app
#print axioms Found.Pnp33.acc_bits
#print axioms Found.Pnp33.acc_run
#print axioms Found.Pnp33.fixed_run
#print axioms Found.Pnp33.cell_false
#print axioms Found.Pnp33.fcRows_snoc
#print axioms Found.Pnp33.allPins_split
#print axioms Found.Pnp33.uo_bits
#print axioms Found.Pnp33.fc_bits
#print axioms Found.Pnp33.good_fc
#print axioms Found.Pnp33.len_cbody
#print axioms Found.Pnp33.cbody_run
#print axioms Found.Pnp33.fcF_facts
#print axioms Found.Pnp33.fcF_keep
#print axioms Found.Pnp33.cIter
#print axioms Found.Pnp33.cIter_keep
#print axioms Found.Pnp33.cloop_run
#print axioms Found.Pnp33.allPins_enc
#print axioms: 18 theorems, no axioms.
DROP, loop invariants at the loop’s end, the time registers after the time loop, reversing twice, and the CNF encoding of the whole tableau CNF as its four parts and the closing bit.
found/Pnp34.lean
/-
T330 piece 34. found/Pnp34 -- glue for the whole tableau writer (CL3b, 15).
Small facts used when the pieces Pnp30-Pnp33 are put one after another:
* DROP r d: d single pops; from r holding n + d ones it leaves n ones in d steps;
* a loop invariant carries over to the loop's end registers, and a loop's counter is
empty at the end;
* after the time loop from t over m rounds, registers 3, 4 hold t + m, t + m + 1 ones,
and registers from 12 on are unchanged;
* revApp twice is the identity, so a final MOVE of the reversed output gives the bits
in order;
* the CNF encoding of the tableau CNF of a Turing machine (Pnp11.full) is the window
part, the boundary part, the accept clause and the pins, then the closing 0.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp33
namespace Found.Pnp34
open Found.Pnp9 (V windows bounds tab)
open Found.Pnp11 (pinCl accCl full)
open Found.Pnp19 (encCNF encCl app_assoc')
open Found.Pnp20
open Found.Pnp21
open Found.Pnp22 (steps iter)
open Found.Pnp23 (tr tr_len mapI appI app_nil'')
open Found.Pnp25 (encList encList_app)
open Found.Pnp28 (seqI frag_seq)
/-! ## DROP -/
def dropI (r : Nat) : Nat -> List Ins
| 0 => []
| d + 1 => seqI [.pop r 1 1 1] (dropI r d)
theorem drop_run (prog : List Ins) (r : Nat) :
forall (d o n : Nat) (x : Nat -> List Bool), Frag prog o (dropI r d) -> x r = tr (n + d) ->
Runs prog d o x (o + d) (upd x r (tr n)) := by
intro d
induction d with
| zero =>
intro o n x _ hx
refine And.intro rfl (fun i => ?_)
show x i = selR (Nat.beq i r) (tr n) (x i)
cases h : Nat.beq i r with
| false => rfl
| true =>
have e : i = r := Found.Pnp2.beq_eq i r h
rw [e, hx]
rfl
| succ d ih =>
intro o n x hf hx
have g := frag_seq prog o _ _ hf
have i0 : nthI prog o = some (.pop r (o + 1) (o + 1) (o + 1)) := g.1 0 _ rfl
have r1 : Runs prog 1 o x (o + 1) (upd x r (tr (n + d))) := by
show (run prog 0 (exec (Cfg.mk o x) (nthI prog o))).pc = o + 1 /\
forall i, (run prog 0 (exec (Cfg.mk o x) (nthI prog o))).regs i = upd x r (tr (n + d)) i
rw [i0]
show (popTo (Cfg.mk o x) r (o + 1) (o + 1) (o + 1) (x r)).pc = o + 1 /\
forall i, (popTo (Cfg.mk o x) r (o + 1) (o + 1) (o + 1) (x r)).regs i = upd x r (tr (n + d)) i
rw [hx]
exact And.intro rfl (fun _ => rfl)
have r2 := ih (o + 1) n (upd x r (tr (n + d))) g.2 (upd_same x r _)
have r3 := runs_seq prog _ _ _ _ _ x _ _ r1 r2
rw [Nat.add_comm 1 d] at r3
refine And.intro ?_ (fun i => (r3.2 i).trans (Found.Pnp24.upd_upd x r _ _ i))
show (run prog (d + 1) (Cfg.mk o x)).pc = o + (d + 1)
rw [r3.1, Nat.add_assoc, Nat.add_comm 1 d]
/-! ## Loops: invariants and the empty counter at the end -/
theorem iter_inv (c : Nat) (f : (Nat -> List Bool) -> (Nat -> List Bool)) (Q : (Nat -> List Bool) -> Prop)
(h : forall y l, Q y -> Q (f (upd y c l))) : forall l x, Q x -> Q (iter c f l x) := by
intro l
induction l with
| nil => intro x hx; exact hx
| cons b l ih => intro x hx; exact ih _ (h x l hx)
theorem iter_empty (c : Nat) (f : (Nat -> List Bool) -> (Nat -> List Bool)) (hk : forall y, f y c = y c) :
forall l x, x c = l -> iter c f l x c = [] := by
intro l
induction l with
| nil => intro x hx; exact hx
| cons b l ih => intro x _; exact ih _ ((hk _).trans (upd_same x c l))
/-! ## The time loop: the time registers and the registers from 12 on -/
theorem tIter_regs (k : Nat) (delta : List Bool -> Nat -> Bool) (W' : Nat) :
forall m t (x : Nat -> List Bool), x 1 = [] -> x 2 = [] -> x 3 = tr t -> x 4 = tr (t + 1) ->
iter 11 (Found.Pnp29.rndF k delta W') (tr m) x 1 = [] /\
iter 11 (Found.Pnp29.rndF k delta W') (tr m) x 3 = tr (t + m) /\
iter 11 (Found.Pnp29.rndF k delta W') (tr m) x 4 = tr (t + m + 1) := by
intro m
induction m with
| zero => intro t x h1 _ h3 h4; exact And.intro h1 (And.intro h3 h4)
| succ m ih =>
intro t x h1 h2 h3 h4
have q := Found.Pnp30.rnd_facts k delta W' t (upd x 11 (tr m)) h1 h2 h3 h4
have r := ih (t + 1) _ q.2.1 q.2.2.1 q.2.2.2.1 q.2.2.2.2
have e : t + 1 + m = t + (m + 1) := Nat.add_right_comm t 1 m
rw [e] at r
exact r
theorem tIter_keep (k : Nat) (delta : List Bool -> Nat -> Bool) (W' i : Nat) (hi : 12 <= i) :
forall (l : List Bool) (x : Nat -> List Bool), iter 11 (Found.Pnp29.rndF k delta W') l x i = x i := by
have h11 : Nat.beq i 11 = false :=
Found.Pnp2.beq_false_of_ne i 11 (fun e => Nat.lt_irrefl 11 (Nat.lt_of_lt_of_le (Nat.lt_succ_self 11) (Eq.subst (motive := fun z => 12 <= z) e hi)))
have h9 : 9 <= i := Nat.le_trans (Nat.le_of_ble_eq_true rfl) hi
intro l
induction l with
| nil => intro x; rfl
| cons b l ih =>
intro x
show iter 11 (Found.Pnp29.rndF k delta W') l (Found.Pnp29.rndF k delta W' (upd x 11 l)) i = x i
rw [ih, Found.Pnp29.rndF_keep k delta W' _ i h9]
exact upd_other x 11 i l h11
/-! ## Reversing twice -/
theorem revApp_revApp : forall (A m n : List Bool), revApp (revApp A m) n = revApp m (A ++ n) := by
intro A
induction A with
| nil => intro m n; rfl
| cons b A ih => intro m n; exact ih (b :: m) n
theorem revApp_twice (A : List Bool) : revApp (revApp A []) [] = A := by
rw [revApp_revApp, app_nil'']
rfl
/-! ## The encoding of the whole CNF -/
theorem encCNF_eq : forall f : List (List (Bool × V)), encCNF f = encList f ++ [false] := by
intro f
induction f with
| nil => rfl
| cons c f ih =>
show true :: (encCl c ++ encCNF f) = true :: ((encCl c ++ encList f) ++ [false])
rw [ih, app_assoc']
theorem full_enc (delta : List Bool -> Nat -> Bool) (k W T : Nat) (ps : List (Nat × (Nat × Bool))) :
encCNF (full delta k W T ps) =
encList (windows delta k W T) ++ (encList (bounds k W T) ++ (encList [accCl T] ++ (encList (pinCl ps) ++ [false]))) := by
show encCNF ((windows delta k W T ++ bounds k W T) ++ ([accCl T] ++ pinCl ps)) = _
rw [encCNF_eq, encList_app, encList_app, encList_app]
show ((encList (windows delta k W T) ++ encList (bounds k W T)) ++ (encList [accCl T] ++ encList (pinCl ps))) ++ [false] = _
rw [app_assoc', app_assoc', app_assoc']
end Found.Pnp34
#print axioms Found.Pnp34.drop_run
#print axioms Found.Pnp34.iter_inv
#print axioms Found.Pnp34.iter_empty
#print axioms Found.Pnp34.tIter_regs
#print axioms Found.Pnp34.tIter_keep
#print axioms Found.Pnp34.revApp_revApp
#print axioms Found.Pnp34.revApp_twice
#print axioms Found.Pnp34.encCNF_eq
#print axioms Found.Pnp34.full_enc
#print axioms: 9 theorems, no axioms.
Fourteen fragments in a row write the CNF encoding of the whole tableau CNF of the machine with its layout and input pins, from \(W\), \(W-1\), \(T\) and the input in registers.
found/Pnp35.lean
/-
T330 piece 35. found/Pnp35 -- the whole tableau writer (CL3b, 16).
WRITER = TLOOP (window clauses); COPY 3 -> 15 (T); COPY 4 -> 12 (T + 1); CLEAR 3;
BLOOP (boundary clauses); the accept block (T from 15); the fixed block of the layout
pins of cells 0 .. pos0; EMIT pos0 + 1 ones on 14; COPY 10 -> 12; DROP 12 pos0 (12 = r);
CLOOP (layout pins of cells pos0 + 1 .. W - 1); CLEAR 14; EMIT s ones on 14; the input
loop (pins of the input at cells s, s + 1, ...); push 0 on 0.
Proved: from registers 0 .. 4 = [], [], [], [], 1; 9 = W = pos0 + r + 1; 10 = W - 1;
11 = T; 12, 14, 15 empty; 13 = the input l, the writer runs as one fragment and leaves on
register 0 the reversed CNF encoding (Pnp19.encCNF) of the whole tableau CNF of the
machine with layout and input pins (Pnp11.full, Pnp14.allPins, inPins), and keeps every
register from 16 on.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp34
import found.Pnp41
namespace Found.Pnp35
open Found.Pnp9 (V windows bounds)
open Found.Pnp11 (pinCl accCl full)
open Found.Pnp14 (allPins inPins)
open Found.Pnp19 (encCNF)
open Found.Pnp20
open Found.Pnp21
open Found.Pnp22 (steps iter)
open Found.Pnp23 (tr tr_len mapI appI app_nil'' revApp_app revApp_tr)
open Found.Pnp24 (cmp pushAll_eq len_appI len_mapI len_emit)
open Found.Pnp25 (encList encList_app)
open Found.Pnp27 (clear copyF clear_run copy_run)
open Found.Pnp28 (seqI frag_seq)
open Found.Pnp29 (len_seq rnd rndF)
open Found.Pnp30 (tloop tloop_run)
open Found.Pnp31 (bloop bbody bloop_run fb)
open Found.Pnp32 (inLoop inLoop_run inpOf iter2 fp)
open Found.Pnp33 (accOps acc_run fixed_run cloop cbody cloop_run fcF fcRows pinCl_app allPins_enc)
open Found.Pnp34 (dropI drop_run iter_inv iter_empty tIter_regs tIter_keep full_enc)
/-! ## Lengths and small runs -/
theorem len_tloop (k : Nat) (delta : List Bool -> Nat -> Bool) : (tloop k delta).length = (rnd k delta).length + 2 := by
show (appI (appI [_] (mapI (shift 1) (rnd k delta))) [_]).length = _
rw [len_appI, len_appI, len_mapI]
show 1 + (rnd k delta).length + 1 = (rnd k delta).length + 2
rw [Nat.add_comm 1 (rnd k delta).length]
theorem len_bloop (k : Nat) : (bloop k).length = (bbody k).length + 2 := by
show (appI (appI [_] (mapI (shift 1) (bbody k))) [_]).length = _
rw [len_appI, len_appI, len_mapI]
show 1 + (bbody k).length + 1 = (bbody k).length + 2
rw [Nat.add_comm 1 (bbody k).length]
theorem len_cloop (m : Nat) : (cloop m).length = (cbody m).length + 2 := by
show (appI (appI [_] (mapI (shift 1) (cbody m))) [_]).length = _
rw [len_appI, len_appI, len_mapI]
show 1 + (cbody m).length + 1 = (cbody m).length + 2
rw [Nat.add_comm 1 (cbody m).length]
theorem len_drop (r : Nat) : forall d, (dropI r d).length = d := by
intro d
induction d with
| zero => rfl
| succ d ih =>
show (seqI [Ins.pop r 1 1 1] (dropI r d)).length = d + 1
rw [len_seq, ih]
exact Nat.add_comm 1 d
theorem runs_len (prog : List Ins) (n o : Nat) (x y : Nat -> List Bool) (a b : Nat) (e : a = b)
(h : Runs prog n o x (o + a) y) : Runs prog n o x (o + b) y :=
Eq.subst (motive := fun z => Runs prog n o x (o + z) y) e h
theorem seq_runs (prog : List Ins) (o : Nat) (p q : List Ins) (n1 n2 : Nat) (x y z : Nat -> List Bool)
(h1 : Runs prog n1 o x (o + p.length) y) (h2 : Runs prog n2 (o + p.length) y (o + p.length + q.length) z) :
Runs prog (n1 + n2) o x (o + (seqI p q).length) z := by
have r := runs_seq prog _ _ _ _ _ x y z h1 h2
rw [len_seq, <- Nat.add_assoc]
exact r
theorem push_run (prog : List Ins) (o r : Nat) (b : Bool) (hf : Frag prog o [.push r b]) (x : Nat -> List Bool) :
Runs prog 1 o x (o + [Ins.push r b].length) (upd x r (b :: x r)) := by
have i0 : nthI prog o = some (.push r b) := hf 0 _ rfl
show (run prog 0 (exec (Cfg.mk o x) (nthI prog o))).pc = _ /\
forall i, (run prog 0 (exec (Cfg.mk o x) (nthI prog o))).regs i = _
rw [i0]
exact And.intro rfl (fun _ => rfl)
theorem ne16 (i a : Nat) (hi : 16 <= i) (ha : a + 1 <= 16) : Nat.beq i a = false :=
Found.Pnp2.beq_false_of_ne i a (fun e => Nat.lt_irrefl a (Nat.lt_of_lt_of_le ha (Eq.subst (motive := fun z => 16 <= z) e hi)))
/-! ## The writer -/
/-- The fixed bits of the layout pins of cells 0 .. pos0. -/
def hw (m pos0 : Nat) (q0 : List Bool) : List Bool := encList (pinCl (allPins pos0 q0 m (pos0 + 1)))
def writer (k : Nat) (delta : List Bool -> Nat -> Bool) (m pos0 : Nat) (q0 : List Bool) (s : Nat) : List Ins :=
seqI (tloop k delta) (seqI (copyF 3 1 15) (seqI (copyF 4 1 12) (seqI (clear 3) (seqI (bloop k)
(seqI (cmp 1 0 (accOps 15)) (seqI (cmp 1 0 [.emit (hw m pos0 q0)]) (seqI (emit 14 (tr (pos0 + 1)))
(seqI (copyF 10 1 12) (seqI (dropI 12 pos0) (seqI (cloop m) (seqI (clear 14) (seqI (emit 14 (tr s))
(seqI inLoop [.push 0 false])))))))))))))
/-- The step bound of the writer (L is the input length). -/
def WB (k : Nat) (delta : List Bool -> Nat -> Bool) (m pos0 : Nat) (q0 : List Bool) (s r T L : Nat) : Nat :=
(T * (Found.Pnp40.RB k delta (pos0 + r) T (pos0 + r + 2) + 2) + 1) +
((4 * T + 1 + (3 * T + 1)) +
((4 * (T + 1) + 1 + (3 * (T + 1) + 1)) +
((2 * T + 1) +
(((T + 1) * (Found.Pnp41.Bb k (pos0 + r + 1) T + 2) + 1) +
((Found.Pnp38.esum (accOps 15) + (accOps 15).length * Found.Pnp39.KK T) +
(((hw m pos0 q0).length + 0) +
((pos0 + 1) +
((4 * (pos0 + r) + 1 + (3 * (pos0 + r) + 1)) +
(pos0 +
((r * (Found.Pnp41.Bc m (pos0 + 1) r + 2) + 1) +
((2 * (pos0 + 1 + r) + 1) +
(s +
((L * (Found.Pnp41.Bp s L + 2) + 1) + 1)))))))))))))
theorem writer_run (prog : List Ins) (o k : Nat) (delta : List Bool -> Nat -> Bool) (m pos0 : Nat) (q0 : List Bool)
(s r T : Nat) (l : List Bool) (hf : Frag prog o (writer k delta m pos0 q0 s)) (x : Nat -> List Bool)
(h0 : x 0 = []) (h1 : x 1 = []) (h2 : x 2 = []) (h3 : x 3 = []) (h4 : x 4 = tr 1)
(h5 : x 5 = []) (h6 : x 6 = []) (h7 : x 7 = [])
(h9 : x 9 = tr (pos0 + r + 1)) (h10 : x 10 = tr (pos0 + r)) (h11 : x 11 = tr T) (h12 : x 12 = [])
(h13 : x 13 = l) (h14 : x 14 = []) (h15 : x 15 = []) :
Exists fun N => Exists fun y => Runs prog N o x (o + (writer k delta m pos0 q0 s).length) y /\
y 0 = revApp (encCNF (full delta k (pos0 + r + 1) T (allPins pos0 q0 m (pos0 + r + 1) ++ inPins (inpOf s l)))) [] /\
(forall i, 16 <= i -> y i = x i) /\ N <= WB k delta m pos0 q0 s r T l.length := by
-- the pieces of the code
have g0 := frag_seq prog o _ _ hf
have g1 := frag_seq prog _ _ _ g0.2
have g2 := frag_seq prog _ _ _ g1.2
have g3 := frag_seq prog _ _ _ g2.2
have g4 := frag_seq prog _ _ _ g3.2
have g5 := frag_seq prog _ _ _ g4.2
have g6 := frag_seq prog _ _ _ g5.2
have g7 := frag_seq prog _ _ _ g6.2
have g8 := frag_seq prog _ _ _ g7.2
have g9 := frag_seq prog _ _ _ g8.2
have g10 := frag_seq prog _ _ _ g9.2
have g11 := frag_seq prog _ _ _ g10.2
have g12 := frag_seq prog _ _ _ g11.2
have g13 := frag_seq prog _ _ _ g12.2
-- 1. the time loop
have T1 := tloop_run prog o k delta (pos0 + r) T g0.1 x h1 h2 h3 h4 h9 h10 h11
let x1 := iter 11 (rndF k delta (pos0 + r)) (tr T) x
have R1 := runs_len prog _ _ _ _ _ _ (len_tloop k delta).symm T1.1
have t1 := tIter_regs k delta (pos0 + r) T 0 x h1 h2 h3 h4
have k1 : forall i, 12 <= i -> x1 i = x i := fun i hi => tIter_keep k delta (pos0 + r) i hi (tr T) x
have Q1 := iter_inv 11 (rndF k delta (pos0 + r)) (fun y => y 9 = tr (pos0 + r + 1) /\ y 10 = tr (pos0 + r))
(fun y l' hy => And.intro ((Found.Pnp29.rndF_keep k delta (pos0 + r) (upd y 11 l') 9 (Nat.le_refl 9)).trans hy.1)
((Found.Pnp29.rndF_keep k delta (pos0 + r) (upd y 11 l') 10 (Nat.le_succ 9)).trans hy.2)) (tr T) x (And.intro h9 h10)
have e1_3 : x1 3 = tr T := t1.2.1.trans (congrArg tr (Nat.zero_add T))
have e1_4 : x1 4 = tr (T + 1) := t1.2.2.trans (congrArg (fun z => tr (z + 1)) (Nat.zero_add T))
-- 2. COPY 3 -> 15
let x2 := upd x1 15 (tr T ++ x1 15)
have R2 := runs_len prog _ _ _ _ 9 (copyF 3 1 15).length rfl (copy_run prog _ 3 1 15 g1.1 rfl rfl rfl T x1 e1_3 t1.1)
-- 3. COPY 4 -> 12
let x3 := upd x2 12 (tr (T + 1) ++ x2 12)
have R3 := runs_len prog _ _ _ _ 9 (copyF 4 1 12).length rfl (copy_run prog _ 4 1 12 g2.1 rfl rfl rfl (T + 1) x2 e1_4 t1.1)
-- 4. CLEAR 3
let x4 := upd x3 3 []
have R4 := runs_len prog _ _ _ _ 2 (clear 3).length rfl (clear_run prog _ 3 g3.1 (x3 3) x3 rfl)
-- 5. the boundary loop
have e4_12 : x4 12 = tr (T + 1) := by
show tr (T + 1) ++ x1 12 = tr (T + 1)
rw [k1 12 (Nat.le_refl 12), h12, app_nil'']
have B5 := bloop_run prog _ k (pos0 + r + 1) T g4.1 x4 t1.1 rfl Q1.1 e4_12
let x5 := iter 12 (fb k) (tr (T + 1)) x4
have R5 := runs_len prog _ _ _ _ _ _ (len_bloop k).symm B5.1
have e5_1 : x5 1 = [] := iter_inv 12 (fb k) (fun y => y 1 = []) (fun _ _ hy => hy) (tr (T + 1)) x4 t1.1
have e5_12 : x5 12 = [] := iter_empty 12 (fb k) (fun y => Found.Pnp31.fb_keep k y 12 rfl rfl) (tr (T + 1)) x4 e4_12
have k5 : forall i, Nat.beq i 0 = false -> Nat.beq i 3 = false -> Nat.beq i 12 = false -> x5 i = x4 i :=
fun i a b c => Found.Pnp31.iter_keep k i a b c (tr (T + 1)) x4
have e5_15 : x5 15 = tr T := by
rw [k5 15 rfl rfl rfl]
show tr T ++ x1 15 = tr T
rw [k1 15 (Nat.le_of_ble_eq_true rfl), h15, app_nil'']
-- 6. the accept block
let x6 := upd x5 0 (revApp (encList [accCl T]) (x5 0))
have R6 := acc_run prog _ 15 T g5.1 rfl rfl x5 e5_1 e5_15
-- 7. the fixed layout pins
let x7 := upd x6 0 (revApp (hw m pos0 q0) (x6 0))
have R7 := fixed_run prog _ (hw m pos0 q0) g6.1 x6 e5_1
-- 8. EMIT pos0 + 1 ones on 14
let x8 := pushAll x7 14 (tr (pos0 + 1))
have R8 := runs_len prog _ _ _ _ _ _ (len_emit 14 (tr (pos0 + 1))).symm (frag_emit_run prog 14 (tr (pos0 + 1)) _ x7 g7.1)
have p8 : forall i, x8 i = upd x7 14 (revApp (tr (pos0 + 1)) (x7 14)) i := fun i => pushAll_eq 14 (tr (pos0 + 1)) x7 i
have e8_1 : x8 1 = [] := (p8 1).trans e5_1
have e8_10 : x8 10 = tr (pos0 + r) := by
rw [p8 10]
show x5 10 = _
rw [k5 10 rfl rfl rfl]
exact Q1.2
have e8_12 : x8 12 = [] := (p8 12).trans e5_12
have e8_14 : x8 14 = tr (pos0 + 1) := by
rw [p8 14, upd_same]
show revApp (tr (pos0 + 1)) (x5 14) = _
rw [k5 14 rfl rfl rfl]
show revApp (tr (pos0 + 1)) (x1 14) = _
rw [k1 14 (Nat.le_of_ble_eq_true rfl), h14, revApp_tr, app_nil'']
-- 9. COPY 10 -> 12
let x9 := upd x8 12 (tr (pos0 + r) ++ x8 12)
have R9 := runs_len prog _ _ _ _ 9 (copyF 10 1 12).length rfl (copy_run prog _ 10 1 12 g8.1 rfl rfl rfl (pos0 + r) x8 e8_10 e8_1)
-- 10. DROP 12 pos0
have e9_12 : x9 12 = tr (r + pos0) := by
show tr (pos0 + r) ++ x8 12 = _
rw [e8_12, app_nil'', Nat.add_comm pos0 r]
let x10 := upd x9 12 (tr r)
have R10 := runs_len prog _ _ _ _ _ _ (len_drop 12 pos0).symm (drop_run prog 12 pos0 _ r x9 g9.1 e9_12)
-- 11. the layout pin loop
have C11 := cloop_run prog _ m (pos0 + 1) r g10.1 x10 e8_1 e8_14 (upd_same x9 12 (tr r))
let x11 := iter 12 (fcF m) (tr r) x10
have R11 := runs_len prog _ _ _ _ _ _ (len_cloop m).symm C11.1
have e11_1 : x11 1 = [] := iter_inv 12 (fcF m) (fun y => y 1 = []) (fun _ _ hy => hy) (tr r) x10 e8_1
have k11 : forall i, Nat.beq i 0 = false -> Nat.beq i 12 = false -> Nat.beq i 14 = false -> x11 i = x10 i :=
fun i a b c => Found.Pnp33.cIter_keep m i a b c (tr r) x10
-- 12. CLEAR 14
let x12 := upd x11 14 []
have R12 := runs_len prog _ _ _ _ 2 (clear 14).length rfl (clear_run prog _ 14 g11.1 (x11 14) x11 rfl)
-- 13. EMIT s ones on 14
let x13 := pushAll x12 14 (tr s)
have R13 := runs_len prog _ _ _ _ _ _ (len_emit 14 (tr s)).symm (frag_emit_run prog 14 (tr s) _ x12 g12.1)
have p13 : forall i, x13 i = upd x12 14 (revApp (tr s) (x12 14)) i := fun i => pushAll_eq 14 (tr s) x12 i
have e13_1 : x13 1 = [] := (p13 1).trans e11_1
have e13_14 : x13 14 = tr s := by
rw [p13 14, upd_same]
show revApp (tr s) [] = tr s
rw [revApp_tr, app_nil'']
have e13_13 : x13 13 = l := by
rw [p13 13]
show x11 13 = l
rw [k11 13 rfl rfl rfl]
show x8 13 = l
rw [p8 13]
show x5 13 = l
rw [k5 13 rfl rfl rfl]
show x1 13 = l
rw [k1 13 (Nat.le_of_ble_eq_true rfl), h13]
-- 14. the input loop
have I14 := inLoop_run prog _ s g13.1 l x13 e13_1 e13_13 e13_14
let x14 := iter2 13 fp l x13
-- 15. push 0
let x15 := upd x14 0 (false :: x14 0)
have R15 := push_run prog _ 0 false g13.2 x14
-- the whole run
have RUN := seq_runs prog _ _ _ _ _ _ _ _ R1 (seq_runs prog _ _ _ _ _ _ _ _ R2 (seq_runs prog _ _ _ _ _ _ _ _ R3
(seq_runs prog _ _ _ _ _ _ _ _ R4 (seq_runs prog _ _ _ _ _ _ _ _ R5 (seq_runs prog _ _ _ _ _ _ _ _ R6
(seq_runs prog _ _ _ _ _ _ _ _ R7 (seq_runs prog _ _ _ _ _ _ _ _ R8 (seq_runs prog _ _ _ _ _ _ _ _ R9
(seq_runs prog _ _ _ _ _ _ _ _ R10 (seq_runs prog _ _ _ _ _ _ _ _ R11 (seq_runs prog _ _ _ _ _ _ _ _ R12
(seq_runs prog _ _ _ _ _ _ _ _ R13 (seq_runs prog _ _ _ _ _ _ _ _ (runs_len prog _ _ _ _ 37 inLoop.length rfl I14.1) R15)))))))))))))
-- register 0
have z13 : x13 0 = revApp (encList (pinCl (fcRows m (pos0 + 1) r)))
(revApp (hw m pos0 q0) (revApp (encList [accCl T]) (revApp (encList (bounds k (pos0 + r + 1) T))
(revApp (encList (windows delta k (pos0 + r + 1) T)) [])))) := by
rw [p13 0]
show iter 12 (fcF m) (tr r) x10 0 = _
rw [C11.2]
show revApp _ (x8 0) = _
rw [p8 0]
show revApp _ (revApp (hw m pos0 q0) (revApp (encList [accCl T]) (iter 12 (fb k) (tr (T + 1)) x4 0))) = _
rw [B5.2]
show revApp _ (revApp _ (revApp _ (revApp _ (iter 11 (rndF k delta (pos0 + r)) (tr T) x 0)))) = _
rw [T1.2, h0]
have ea : encList (pinCl (allPins pos0 q0 m (pos0 + r + 1))) =
hw m pos0 q0 ++ encList (pinCl (fcRows m (pos0 + 1) r)) := by
rw [Nat.add_right_comm pos0 r 1, allPins_enc]
rfl
have Z : x15 0 = revApp (encCNF (full delta k (pos0 + r + 1) T (allPins pos0 q0 m (pos0 + r + 1) ++ inPins (inpOf s l)))) [] := by
show revApp [false] (iter2 13 fp l x13 0) = _
rw [I14.2, z13, full_enc, pinCl_app, encList_app, ea]
repeat rw [revApp_app]
-- registers from 16 on
have K : forall i, 16 <= i -> x15 i = x i := by
intro i hi
have n0 := ne16 i 0 hi (Nat.le_of_ble_eq_true rfl)
have n3 := ne16 i 3 hi (Nat.le_of_ble_eq_true rfl)
have n12 := ne16 i 12 hi (Nat.le_of_ble_eq_true rfl)
have n13 := ne16 i 13 hi (Nat.le_of_ble_eq_true rfl)
have n14 := ne16 i 14 hi (Nat.le_of_ble_eq_true rfl)
have n15 := ne16 i 15 hi (Nat.le_refl 16)
have a14 : x14 i = x13 i := Found.Pnp32.pIter_keep i n0 n13 n14 l x13
have a13 : x13 i = x11 i := (p13 i).trans ((upd_other x12 14 i _ n14).trans (upd_other x11 14 i _ n14))
have a11 : x11 i = x8 i := (k11 i n0 n12 n14).trans ((upd_other x9 12 i _ n12).trans (upd_other x8 12 i _ n12))
have a8 : x8 i = x5 i := (p8 i).trans ((upd_other x7 14 i _ n14).trans ((upd_other x6 0 i _ n0).trans (upd_other x5 0 i _ n0)))
have a5 : x5 i = x1 i := (k5 i n0 n3 n12).trans ((upd_other x3 3 i _ n3).trans ((upd_other x2 12 i _ n12).trans (upd_other x1 15 i _ n15)))
have a1 : x1 i = x i := k1 i (Nat.le_trans (Nat.le_of_ble_eq_true rfl) hi)
exact (upd_other x14 0 i _ n0).trans (a14.trans (a13.trans (a11.trans (a8.trans (a5.trans a1)))))
-- the step bound
have b1 := Found.Pnp40.tloop_steps_le k delta (pos0 + r) T x
(And.intro h1 (And.intro h2 (And.intro (Exists.intro 0 (And.intro h3 (And.intro h4
(Nat.le_of_eq ((congrArg (fun z => 0 + z.length) h11).trans ((congrArg (fun z => 0 + z) (tr_len T)).trans (Nat.zero_add T)))))))
(And.intro (Nat.le_trans (Nat.le_of_eq (congrArg List.length h5)) (Nat.zero_le _)) (And.intro (Nat.le_trans (Nat.le_of_eq (congrArg List.length h6)) (Nat.zero_le _)) (Nat.le_trans (Nat.le_of_eq (congrArg List.length h7)) (Nat.zero_le _))))))) h11
have b4 : 2 * (x3 3).length + 1 <= 2 * T + 1 := Nat.le_of_eq (by
show 2 * (x1 3).length + 1 = 2 * T + 1
rw [e1_3, tr_len])
have b5 := Found.Pnp41.bloop_steps_le k (pos0 + r + 1) T x4
(And.intro (Nat.le_of_eq (by show ([] : List Bool).length + (x4 12).length = T + 1; rw [e4_12, tr_len]; exact Nat.zero_add _)) Q1.1) e4_12
have b6 := Found.Pnp38.cost_le (Found.Pnp26.cntOf x5) T (accOps 15)
(And.intro (Nat.le_of_eq ((congrArg List.length e5_15).trans (tr_len T))) True.intro)
have b11 := Found.Pnp41.cloop_steps_le m (pos0 + 1) r x10
(Nat.le_of_eq (by show (x8 14).length + (tr r).length = pos0 + 1 + r; rw [e8_14, tr_len, tr_len])) (upd_same x9 12 (tr r))
have b12 : 2 * (x11 14).length + 1 <= 2 * (pos0 + 1 + r) + 1 := Nat.le_of_eq (by
show 2 * (iter 12 (fcF m) (tr r) x10 14).length + 1 = _
rw [Found.Pnp41.cIter_14 m r (pos0 + 1) x10 e8_14, tr_len])
have b14 := Found.Pnp41.inLoop_steps_le s l x13 (Nat.le_of_eq (by rw [e13_14, e13_13, tr_len])) e13_13
have BND := Nat.add_le_add b1 (Nat.add_le_add (Nat.le_refl (4 * T + 1 + (3 * T + 1)))
(Nat.add_le_add (Nat.le_refl (4 * (T + 1) + 1 + (3 * (T + 1) + 1))) (Nat.add_le_add b4 (Nat.add_le_add b5
(Nat.add_le_add b6 (Nat.add_le_add (Nat.le_refl ((hw m pos0 q0).length + 0))
(Nat.add_le_add (Nat.le_of_eq (tr_len (pos0 + 1))) (Nat.add_le_add (Nat.le_refl (4 * (pos0 + r) + 1 + (3 * (pos0 + r) + 1)))
(Nat.add_le_add (Nat.le_refl pos0) (Nat.add_le_add b11 (Nat.add_le_add b12 (Nat.add_le_add (Nat.le_of_eq (tr_len s))
(Nat.add_le_add b14 (Nat.le_refl 1))))))))))))))
exact Exists.intro _ (Exists.intro x15 (And.intro RUN (And.intro Z (And.intro K BND))))
end Found.Pnp35
#print axioms Found.Pnp35.len_tloop
#print axioms Found.Pnp35.len_bloop
#print axioms Found.Pnp35.len_cloop
#print axioms Found.Pnp35.len_drop
#print axioms Found.Pnp35.runs_len
#print axioms Found.Pnp35.seq_runs
#print axioms Found.Pnp35.push_run
#print axioms Found.Pnp35.ne16
#print axioms Found.Pnp35.writer_run
#print axioms: 9 theorems, no axioms.
A polynomial in the input length compiles to register code that adds its value in ones to a target register, in an explicit number of steps, changing no other register.
found/Pnp36.lean
/-
T330 piece 36. found/Pnp36 -- a unary evaluator for polynomials (CL3b, 17).
The time bound T and the width W of the tableau are polynomials of the input length n
(Pnp3.PE). EVAL e D f compiles e to register code: with n ones in register N and the
scratch register B empty, it adds pe e n ones in front of register D. Constants are
EMIT blocks, the variable is COPY N -> D, a sum is one code after the other, and a
product a * b computes a into the fresh register f and loops over it, adding b each
time (fresh registers from f + 1 for the inner codes). Proved: if D, B, N lie below f
and are distinct and every register from f on is empty, EVAL e D f runs as a fragment
in exactly ecost e n steps (an explicit function: a sum of n-linear terms and products
of the values of subterms) and changes only register D, which gets pe e n more ones.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp35
import found.Pnp3
namespace Found.Pnp36
open Found.Pnp3 (PE pe)
open Found.Pnp20
open Found.Pnp21
open Found.Pnp22 (steps iter)
open Found.Pnp23 (tr tr_len mapI appI app_nil'' revApp_tr ne_symm')
open Found.Pnp24 (pushAll_eq len_appI len_mapI len_emit upd_upd)
open Found.Pnp26 (loop_inv)
open Found.Pnp27 (copyF copy_run)
open Found.Pnp28 (seqI frag_seq)
open Found.Pnp35 (seq_runs runs_len)
/-! ## Small facts -/
theorem lt_beq (a b : Nat) (h : a < b) : Nat.beq a b = false :=
Found.Pnp2.beq_false_of_ne a b (fun e => Nat.lt_irrefl b (Eq.subst (motive := fun z => z < b) e h))
theorem gt_beq (a b : Nat) (h : a < b) : Nat.beq b a = false := ne_symm' a b (lt_beq a b h)
theorem upd_self (x : Nat -> List Bool) (r : Nat) (v : List Bool) (h : x r = v) : forall i, upd x r v i = x i := by
intro i
show selR (Nat.beq i r) v (x i) = x i
cases e : Nat.beq i r with
| false => rfl
| true =>
have ei : i = r := Found.Pnp2.beq_eq i r e
rw [ei, h]
rfl
theorem upd_congr (x y : Nat -> List Bool) (h : forall i, x i = y i) (r : Nat) (v : List Bool) :
forall i, upd x r v i = upd y r v i := by
intro i
show selR (Nat.beq i r) v (x i) = selR (Nat.beq i r) v (y i)
rw [h i]
theorem tr_app' (b : Nat) : forall a, tr a ++ tr b = tr (b + a) := by
intro a
induction a with
| zero => rfl
| succ a ih => exact congrArg (fun l => true :: l) ih
theorem runs_post (prog : List Ins) (n o o' : Nat) (x y z : Nat -> List Bool) (h : Runs prog n o x o' y)
(e : forall i, y i = z i) : Runs prog n o x o' z :=
And.intro h.1 (fun i => (h.2 i).trans (e i))
theorem runs_cnt (prog : List Ins) (a b o o' : Nat) (x y : Nat -> List Bool) (e : a = b)
(h : Runs prog a o x o' y) : Runs prog b o x o' y :=
Eq.subst (motive := fun z => Runs prog z o x o' y) e h
theorem steps_const (c C : Nat) (F : (Nat -> List Bool) -> (Nat -> List Bool)) :
forall m (y : Nat -> List Bool), steps c (fun _ => C) F (tr m) y = m * (C + 2) + 1 := by
intro m
induction m with
| zero => intro y; show 1 = 0 * (C + 2) + 1; rw [Nat.zero_mul]
| succ m ih =>
intro y
show ((1 + C) + 1) + steps c (fun _ => C) F (tr m) (F (upd y c (tr m))) = (m + 1) * (C + 2) + 1
rw [ih, Nat.succ_mul, Nat.add_comm 1 C, Nat.add_comm (C + 1 + 1) (m * (C + 2) + 1), Nat.add_right_comm (m * (C + 2)) 1]
/-! ## The evaluator -/
section
variable (B N : Nat)
def comp : PE -> Nat -> Nat -> List Ins
| .x, D, _ => copyF N B D
| .c k, D, _ => emit D (tr k)
| .add a b, D, f => seqI (comp a D f) (comp b D f)
| .mul a b, D, f => seqI (comp a f (f + 1))
(appI (appI [.pop f ((comp b D (f + 1)).length + 2) 1 1] (mapI (shift 1) (comp b D (f + 1)))) [.jmp 0])
def ecost : PE -> Nat -> Nat
| .x, n => 4 * n + 1 + (3 * n + 1)
| .c k, _ => k
| .add a b, n => ecost a n + ecost b n
| .mul a b, n => ecost a n + (pe a n * (ecost b n + 2) + 1)
/-- The preconditions: scratch empty, n in N, every register from f on empty. -/
def Pre (n f : Nat) (x : Nat -> List Bool) : Prop :=
x B = [] /\ x N = tr n /\ forall i, f <= i -> x i = []
theorem pre_upd (n f : Nat) (x : Nat -> List Bool) (h : Pre B N n f x) (D : Nat) (hD : D < f)
(hBD : Nat.beq B D = false) (hND : Nat.beq N D = false) (v : List Bool) : Pre B N n f (upd x D v) :=
And.intro ((upd_other x D B v hBD).trans h.1) (And.intro ((upd_other x D N v hND).trans h.2.1)
(fun i hi => (upd_other x D i v (gt_beq D i (Nat.lt_of_lt_of_le hD hi))).trans (h.2.2 i hi)))
/-- The loop of a product: what it does to the registers. -/
theorem mul_iter (D f pb : Nat) (hDf : Nat.beq D f = false) (hfD : Nat.beq f D = false) :
forall m (z : Nat -> List Bool), z f = tr m ->
forall i, iter f (fun y => upd y D (tr pb ++ y D)) (tr m) z i = upd (upd z f []) D (tr (m * pb) ++ z D) i := by
intro m
induction m with
| zero =>
intro z hz i
show z i = upd (upd z f []) D (tr (0 * pb) ++ z D) i
rw [Nat.zero_mul]
show z i = upd (upd z f []) D ([] ++ z D) i
have a := upd_self z D (z D) rfl i
have b := upd_congr (upd z f []) z (upd_self z f [] hz) D ([] ++ z D) i
exact (b.trans a).symm
| succ m ih =>
intro z hz i
let z' := upd (upd z f (tr m)) D (tr pb ++ upd z f (tr m) D)
have hz' : z' f = tr m := (upd_other _ D f _ hfD).trans (upd_same z f (tr m))
show iter f (fun y => upd y D (tr pb ++ y D)) (tr m) z' i = _
rw [ih z' hz' i]
have eD : z' D = tr pb ++ z D := by
show upd (upd z f (tr m)) D (tr pb ++ upd z f (tr m) D) D = _
rw [upd_same, upd_other z f D _ hDf]
show selR (Nat.beq i D) (tr (m * pb) ++ z' D) (upd z' f [] i) =
selR (Nat.beq i D) (tr ((m + 1) * pb) ++ z D) (upd z f [] i)
rw [eD, <- Found.Pnp19.app_assoc', tr_app', Nat.succ_mul, Nat.add_comm pb (m * pb)]
cases e : Nat.beq i D with
| true => rfl
| false =>
show selR (Nat.beq i f) [] (z' i) = selR (Nat.beq i f) [] (z i)
cases e2 : Nat.beq i f with
| true => rfl
| false =>
show upd (upd z f (tr m)) D _ i = z i
rw [upd_other _ D i _ e, upd_other z f i _ e2]
theorem comp_run (prog : List Ins) (n : Nat) (hBN : Nat.beq B N = false) :
forall (e : PE) (D f o : Nat) (x : Nat -> List Bool), Frag prog o (comp B N e D f) -> Pre B N n f x ->
D < f -> B < f -> N < f -> Nat.beq D B = false -> Nat.beq D N = false ->
Runs prog (ecost e n) o x (o + (comp B N e D f).length) (upd x D (tr (pe e n) ++ x D)) := by
intro e
induction e with
| x =>
intro D f o x hf hp _ _ _ hDB hDN
exact copy_run prog o N B D hf (ne_symm' B N hBN) (ne_symm' D N hDN) (ne_symm' D B hDB) n x hp.2.1 hp.1
| c k =>
intro D f o x hf _ _ _ _ _ _
have r := runs_len prog _ _ _ _ _ _ (len_emit D (tr k)).symm (frag_emit_run prog D (tr k) o x hf)
have r2 := runs_cnt prog _ _ _ _ _ _ (tr_len k) r
exact runs_post prog _ _ _ _ _ _ r2 (fun i => (pushAll_eq D (tr k) x i).trans
(congrArg (fun v => upd x D v i) (revApp_tr k (x D))))
| add a b iha ihb =>
intro D f o x hf hp hD hB hN hDB hDN
have g := frag_seq prog o _ _ hf
have ra := iha D f o x g.1 hp hD hB hN hDB hDN
let xa := upd x D (tr (pe a n) ++ x D)
have hpa : Pre B N n f xa := pre_upd B N n f x hp D hD (ne_symm' D B hDB) (ne_symm' D N hDN) _
have rb := ihb D f _ xa g.2 hpa hD hB hN hDB hDN
have r := seq_runs prog o _ _ _ _ x xa _ ra rb
refine runs_post prog _ _ _ _ _ _ r (fun i => ?_)
rw [upd_upd]
show selR (Nat.beq i D) (tr (pe b n) ++ xa D) (x i) = selR (Nat.beq i D) (tr (pe a n + pe b n) ++ x D) (x i)
have eD : xa D = tr (pe a n) ++ x D := upd_same x D _
rw [eD, <- Found.Pnp19.app_assoc', tr_app']
| mul a b iha ihb =>
intro D f o x hf hp hD hB hN hDB hDN
have g := frag_seq prog o _ _ hf
have hfB : Nat.beq f B = false := gt_beq B f hB
have hfN : Nat.beq f N = false := gt_beq N f hN
have hDf : Nat.beq D f = false := lt_beq D f hD
have hfD : Nat.beq f D = false := gt_beq D f hD
-- a into register f
have hp1 : Pre B N n (f + 1) x := And.intro hp.1 (And.intro hp.2.1 (fun i hi => hp.2.2 i (Nat.le_of_succ_le hi)))
have ra := iha f (f + 1) o x g.1 hp1 (Nat.lt_succ_self f) (Nat.lt_succ_of_lt hB) (Nat.lt_succ_of_lt hN) hfB hfN
have xf : x f = [] := hp.2.2 f (Nat.le_refl f)
let xa := upd x f (tr (pe a n) ++ x f)
-- the loop
let L := (comp B N b D (f + 1)).length
let o' := o + (comp B N a f (f + 1)).length
have gl := g.2
have fa := Found.Pnp23.frag_app_l prog o' _ _ gl
have fbd : Frag prog (o' + 1) (comp B N b D (f + 1)) := Found.Pnp23.frag_app prog o' [.pop f (L + 2) 1 1] _ fa
have i0 : nthI prog o' = some (.pop f (o' + (L + 2)) (o' + 1) (o' + 1)) := fa 0 _ rfl
have hl : (appI [.pop f (L + 2) 1 1] (mapI (shift 1) (comp B N b D (f + 1)))).length = 1 + L := by
rw [len_appI, len_mapI]; rfl
have i1 : nthI prog (o' + 1 + L) = some (.jmp o') := by
have e0 := gl ((appI [.pop f (L + 2) 1 1] (mapI (shift 1) (comp B N b D (f + 1)))).length + 0) (.jmp 0)
(Found.Pnp23.nthI_app_r _ [.jmp 0] 0)
rw [hl] at e0
have e : nthI prog (o' + (1 + L)) = some (.jmp o') := e0
rw [Nat.add_assoc]
exact e
let P : (Nat -> List Bool) -> Prop := fun z => Pre B N n (f + 1) z
have hbody : forall z, P z -> Runs prog (ecost b n) (o' + 1) z (o' + 1 + L) (upd z D (tr (pe b n) ++ z D)) :=
fun z hz => ihb D (f + 1) (o' + 1) z fbd hz (Nat.lt_succ_of_lt hD) (Nat.lt_succ_of_lt hB) (Nat.lt_succ_of_lt hN) hDB hDN
have hkeep : forall z, upd z D (tr (pe b n) ++ z D) f = z f := fun z => upd_other z D f _ hfD
have hP1 : forall z c l, P z -> z f = c :: l -> P (upd z f l) := fun z _ l hz _ =>
pre_upd B N n (f + 1) z hz f (Nat.lt_succ_self f) (ne_symm' f B hfB) (ne_symm' f N hfN) l
have hP2 : forall z, P z -> P (upd z D (tr (pe b n) ++ z D)) := fun z hz =>
pre_upd B N n (f + 1) z hz D (Nat.lt_succ_of_lt hD) (ne_symm' D B hDB) (ne_symm' D N hDN) _
have hxa : xa f = tr (pe a n) := by
show upd x f (tr (pe a n) ++ x f) f = _
rw [upd_same, xf, app_nil'']
have hPa : P xa := pre_upd B N n (f + 1) x hp1 f (Nat.lt_succ_self f) (ne_symm' f B hfB) (ne_symm' f N hfN) _
have LP := loop_inv f (fun _ => ecost b n) (fun z => upd z D (tr (pe b n) ++ z D)) P prog o' L (o' + (L + 2))
i0 i1 hbody hkeep hP1 hP2 (tr (pe a n)) xa hxa hPa
have LP2 := runs_cnt prog _ _ _ _ _ _ (steps_const f (ecost b n) _ (pe a n) xa) LP
have hll : (appI (appI [.pop f (L + 2) 1 1] (mapI (shift 1) (comp B N b D (f + 1)))) [.jmp 0]).length = L + 2 := by
rw [len_appI, hl]
show 1 + L + 1 = L + 2
rw [Nat.add_comm 1 L]
have LP3 := runs_len prog _ _ _ _ _ _ hll.symm LP2
have r := seq_runs prog o _ _ _ _ x xa _ ra LP3
refine runs_post prog _ _ _ _ _ _ r (fun i => ?_)
rw [mul_iter D f (pe b n) hDf hfD (pe a n) xa hxa i]
have exD : xa D = x D := upd_other x f D _ hDf
rw [exD]
show selR (Nat.beq i D) _ (upd xa f [] i) = selR (Nat.beq i D) _ (x i)
rw [upd_upd, upd_self x f [] xf i]
rfl
end
end Found.Pnp36
#print axioms Found.Pnp36.lt_beq
#print axioms Found.Pnp36.gt_beq
#print axioms Found.Pnp36.upd_self
#print axioms Found.Pnp36.upd_congr
#print axioms Found.Pnp36.tr_app'
#print axioms Found.Pnp36.runs_post
#print axioms Found.Pnp36.runs_cnt
#print axioms Found.Pnp36.steps_const
#print axioms Found.Pnp36.pre_upd
#print axioms Found.Pnp36.mul_iter
#print axioms Found.Pnp36.comp_run
#print axioms: 11 theorems, no axioms.
Counting the input, evaluating \(W\) and \(T\), the writer and a final move give one register program that leaves the CNF encoding of the Cook–Levin tableau CNF of the input in an output register.
found/Pnp37.lean
/-
T330 piece 37. found/Pnp37 -- the reduction map as one register program (CL3b, 18).
RED = LEN (count the input bits of register 13 into register 17, keeping a reversed copy
in 2); MOVE 2 -> 13 (the input back in order); EVAL W into 9 (W = pos0 + R(n) + 1 for a
polynomial R); COPY 9 -> 10; DROP 10 1 (W - 1); EVAL T into 11; push 1 on 4; WRITER
(Pnp35); MOVE 0 -> 16. Proved: started with the input l in register 13 and every other
register empty, RED runs as one program fragment and leaves in register 16 exactly the CNF
encoding (Pnp19) of the Cook-Levin tableau CNF of l (Pnp11.full with the layout pins and
the input pins at cells s, s + 1, ...), with T = pe eT |l| and W = pos0 + pe eR |l| + 1;
for a Turing machine verifier this is enc (Pnp18.red M W T p0 q0 (inpOf s l)).
The step count is still an existential here (the polynomial bound is the next piece).
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp36
import found.Pnp18
import found.Pnp41
namespace Found.Pnp37
open Found.Pnp3 (PE pe)
open Found.Pnp11 (full)
open Found.Pnp12 (TM rule)
open Found.Pnp14 (allPins inPins pinsTM)
open Found.Pnp19 (encCNF)
open Found.Pnp20
open Found.Pnp21
open Found.Pnp23 (tr tr_len tr_cons app_nil'' ne_symm')
open Found.Pnp27 (copyF copy_run)
open Found.Pnp28 (seqI frag_seq)
open Found.Pnp32 (steps2 iter2 loop2_inv inpOf)
open Found.Pnp34 (dropI drop_run revApp_twice)
open Found.Pnp35 (seq_runs runs_len push_run writer writer_run len_drop)
open Found.Pnp36 (comp ecost Pre comp_run runs_post runs_cnt upd_self)
/-! ## MOVE as a run -/
theorem move_runs (prog : List Ins) (o r s : Nat) (hf : Frag prog o (move r s)) (hrs : Nat.beq r s = false)
(l m : List Bool) (x : Nat -> List Bool) (hr : x r = l) (hs : x s = m) :
Runs prog (3 * l.length + 1) o x (o + 5) (upd (upd x r []) s (revApp l m)) := by
have h := frag_move_run prog o r s hf hrs l m x hr hs
refine And.intro h.1 (fun i => ?_)
show _ = selR (Nat.beq i s) (revApp l m) (selR (Nat.beq i r) [] (x i))
cases e1 : Nat.beq i s with
| true => rw [Found.Pnp2.beq_eq i s e1]; exact h.2.2.1
| false =>
cases e2 : Nat.beq i r with
| true => rw [Found.Pnp2.beq_eq i r e2]; exact h.2.1
| false => exact h.2.2.2 i e2 e1
/-! ## LEN: count the input, keep it -/
def lenL : List Ins := [.pop 13 7 1 4, .push 2 false, .push 17 true, .jmp 0, .push 2 true, .push 17 true, .jmp 0]
def lenF (v : Bool) (z : Nat -> List Bool) : Nat -> List Bool :=
upd (upd z 2 (v :: z 2)) 17 (true :: upd z 2 (v :: z 2) 17)
theorem len_iter : forall (l : List Bool) (z : Nat -> List Bool), z 13 = l ->
iter2 13 lenF l z 2 = revApp l (z 2) /\ iter2 13 lenF l z 17 = tr l.length ++ z 17 /\
iter2 13 lenF l z 13 = [] /\
(forall i, Nat.beq i 2 = false -> Nat.beq i 13 = false -> Nat.beq i 17 = false -> iter2 13 lenF l z i = z i) := by
intro l
induction l with
| nil => intro z hz; exact And.intro rfl (And.intro rfl (And.intro hz (fun _ _ _ _ => rfl)))
| cons b l ih =>
intro z _
have r := ih (lenF b (upd z 13 l)) rfl
refine And.intro r.1 (And.intro ?_ (And.intro r.2.2.1 (fun i a c d => ?_)))
· show iter2 13 lenF l (lenF b (upd z 13 l)) 17 = _
rw [r.2.1]
exact tr_cons l.length (z 17)
· show iter2 13 lenF l (lenF b (upd z 13 l)) i = _
rw [r.2.2.2 i a c d]
exact (upd_other _ 17 i _ d).trans ((upd_other _ 2 i _ a).trans (upd_other z 13 i _ c))
theorem len_steps : forall (l : List Bool) (z : Nat -> List Bool), steps2 13 (fun _ _ => 2) lenF l z = 4 * l.length + 1 := by
intro l
induction l with
| nil => intro z; rfl
| cons b l ih =>
intro z
show 4 + steps2 13 (fun _ _ => 2) lenF l (lenF b (upd z 13 l)) = 4 * l.length + 4 + 1
rw [ih, Nat.add_comm 4 (4 * l.length + 1), Nat.add_right_comm]
theorem lenL_run (prog : List Ins) (o : Nat) (hf : Frag prog o lenL) (l : List Bool) (x : Nat -> List Bool)
(h13 : x 13 = l) :
Runs prog (4 * l.length + 1) o x (o + 7) (iter2 13 lenF l x) := by
have i0 : nthI prog o = some (.pop 13 (o + 7) (o + 1) (o + 4)) := hf 0 _ rfl
have i3 : nthI prog (o + 1 + 2) = some (.jmp o) := hf 3 _ rfl
have i6 : nthI prog (o + 4 + 2) = some (.jmp o) := hf 6 _ rfl
have i1 : nthI prog (o + 1) = some (.push 2 false) := hf 1 _ rfl
have i2 : nthI prog (o + 2) = some (.push 17 true) := hf 2 _ rfl
have i4 : nthI prog (o + 4) = some (.push 2 true) := hf 4 _ rfl
have i5 : nthI prog (o + 5) = some (.push 17 true) := hf 5 _ rfl
have hb0 : forall z, True -> Runs prog 2 (o + 1) z (o + 1 + 2) (lenF false z) := by
intro z _
show (run prog 1 (exec (Cfg.mk (o + 1) z) (nthI prog (o + 1)))).pc = _ /\
forall i, (run prog 1 (exec (Cfg.mk (o + 1) z) (nthI prog (o + 1)))).regs i = _
rw [i1]
show (run prog 0 (exec (Cfg.mk (o + 2) _) (nthI prog (o + 2)))).pc = _ /\
forall i, (run prog 0 (exec (Cfg.mk (o + 2) (upd z 2 (false :: z 2))) (nthI prog (o + 2)))).regs i = _
rw [i2]
exact And.intro rfl (fun _ => rfl)
have hb1 : forall z, True -> Runs prog 2 (o + 4) z (o + 4 + 2) (lenF true z) := by
intro z _
show (run prog 1 (exec (Cfg.mk (o + 4) z) (nthI prog (o + 4)))).pc = _ /\
forall i, (run prog 1 (exec (Cfg.mk (o + 4) z) (nthI prog (o + 4)))).regs i = _
rw [i4]
show (run prog 0 (exec (Cfg.mk (o + 5) _) (nthI prog (o + 5)))).pc = _ /\
forall i, (run prog 0 (exec (Cfg.mk (o + 5) (upd z 2 (true :: z 2))) (nthI prog (o + 5)))).regs i = _
rw [i5]
exact And.intro rfl (fun _ => rfl)
have L := loop2_inv 13 (fun _ _ => 2) lenF (fun _ => True) prog o (o + 4) (o + 1 + 2) (o + 4 + 2) (o + 7)
i0 i3 i6 hb0 hb1 (fun _ _ => rfl) (fun _ _ _ _ _ => True.intro) (fun _ _ _ => True.intro) l x h13 True.intro
exact runs_cnt prog _ _ _ _ _ _ (len_steps l x) L
/-! ## RED -/
theorem ne18 (i a : Nat) (hi : 18 <= i) (ha : a + 1 <= 18) : Nat.beq i a = false :=
Found.Pnp2.beq_false_of_ne i a (fun e => Nat.lt_irrefl a (Nat.lt_of_lt_of_le ha (Eq.subst (motive := fun z => 18 <= z) e hi)))
theorem lt18 (a : Nat) (h : Nat.ble (a + 1) 18 = true) : a < 18 := Nat.le_of_ble_eq_true h
def eW (pos0 : Nat) (eR : PE) : PE := .add (.add (.c pos0) eR) (.c 1)
def redP (k : Nat) (delta : List Bool -> Nat -> Bool) (m pos0 : Nat) (q0 : List Bool) (s : Nat) (eR eT : PE) : List Ins :=
seqI lenL (seqI (move 2 13) (seqI (comp 1 17 (eW pos0 eR) 9 18) (seqI (copyF 9 1 10) (seqI (dropI 10 1)
(seqI (comp 1 17 eT 11 18) (seqI [.push 4 true] (seqI (writer k delta m pos0 q0 s) (move 0 16))))))))
theorem revApp_len : forall (l m : List Bool), (revApp l m).length = l.length + m.length := by
intro l
induction l with
| nil => intro m; exact (Nat.zero_add _).symm
| cons b l ih =>
intro m
show (revApp l (b :: m)).length = l.length + 1 + m.length
rw [ih]
exact (Nat.add_right_comm l.length 1 m.length).symm
/-- The step bound of RED, n the input length. -/
def redB (k : Nat) (delta : List Bool -> Nat -> Bool) (m pos0 : Nat) (q0 : List Bool) (s : Nat) (eR eT : PE) (n : Nat) : Nat :=
(4 * n + 1) + ((3 * n + 1) + (ecost (eW pos0 eR) n + ((4 * (pos0 + pe eR n + 1) + 1 + (3 * (pos0 + pe eR n + 1) + 1)) +
(1 + (ecost eT n + (1 + (Found.Pnp35.WB k delta m pos0 q0 s (pe eR n) (pe eT n) n +
(3 * Found.Pnp35.WB k delta m pos0 q0 s (pe eR n) (pe eT n) n + 1))))))))
theorem redP_run (prog : List Ins) (o k : Nat) (delta : List Bool -> Nat -> Bool) (m pos0 : Nat) (q0 : List Bool)
(s : Nat) (eR eT : PE) (hf : Frag prog o (redP k delta m pos0 q0 s eR eT)) (l : List Bool) (x : Nat -> List Bool)
(h13 : x 13 = l) (hx : forall i, Nat.beq i 13 = false -> x i = []) :
Exists fun N => Exists fun y => Runs prog N o x (o + (redP k delta m pos0 q0 s eR eT).length) y /\
y 16 = encCNF (full delta k (pos0 + pe eR l.length + 1) (pe eT l.length)
(allPins pos0 q0 m (pos0 + pe eR l.length + 1) ++ inPins (inpOf s l))) /\
N <= redB k delta m pos0 q0 s eR eT l.length := by
have g0 := frag_seq prog o _ _ hf
have g1 := frag_seq prog _ _ _ g0.2
have g2 := frag_seq prog _ _ _ g1.2
have g3 := frag_seq prog _ _ _ g2.2
have g4 := frag_seq prog _ _ _ g3.2
have g5 := frag_seq prog _ _ _ g4.2
have g6 := frag_seq prog _ _ _ g5.2
have g7 := frag_seq prog _ _ _ g6.2
-- 1. LEN
have R1 := runs_len prog _ _ _ _ 7 lenL.length rfl (lenL_run prog o g0.1 l x h13)
let x1 := iter2 13 lenF l x
have q1 := len_iter l x h13
-- 2. MOVE 2 -> 13
have e1_2 : x1 2 = revApp l [] := q1.1.trans (congrArg (revApp l) (hx 2 rfl))
have R2 := runs_len prog _ _ _ _ 5 (move 2 13).length rfl (move_runs prog _ 2 13 g1.1 rfl (revApp l []) [] x1 e1_2 q1.2.2.1)
let x2 := upd (upd x1 2 []) 13 (revApp (revApp l []) [])
have k2 : forall i, 18 <= i -> x2 i = [] := by
intro i hi
have a : x2 i = x i := (upd_other _ 13 i _ (ne18 i 13 hi (Nat.le_of_ble_eq_true rfl))).trans
((upd_other x1 2 i _ (ne18 i 2 hi (Nat.le_of_ble_eq_true rfl))).trans
(q1.2.2.2 i (ne18 i 2 hi (Nat.le_of_ble_eq_true rfl)) (ne18 i 13 hi (Nat.le_of_ble_eq_true rfl))
(ne18 i 17 hi (Nat.le_refl 18))))
exact a.trans (hx i (ne18 i 13 hi (Nat.le_of_ble_eq_true rfl)))
have e2_1 : x2 1 = [] := (q1.2.2.2 1 rfl rfl rfl).trans (hx 1 rfl)
have e2_17 : x2 17 = tr l.length := by
show iter2 13 lenF l x 17 = tr l.length
rw [q1.2.1, hx 17 rfl, app_nil'']
have p2 : Pre 1 17 l.length 18 x2 := And.intro e2_1 (And.intro e2_17 k2)
-- 3. EVAL W into 9
have R3 := comp_run 1 17 prog l.length rfl (eW pos0 eR) 9 18 _ x2 g2.1 p2 (lt18 _ rfl)
(lt18 _ rfl) (lt18 _ rfl) rfl rfl
let x3 := upd x2 9 (tr (pe (eW pos0 eR) l.length) ++ x2 9)
have e2_9 : x2 9 = [] := (q1.2.2.2 9 rfl rfl rfl).trans (hx 9 rfl)
have e3_9 : x3 9 = tr (pos0 + pe eR l.length + 1) := by
show tr (pe (eW pos0 eR) l.length) ++ x2 9 = _
rw [e2_9, app_nil'']
rfl
-- 4. COPY 9 -> 10
have R4 := runs_len prog _ _ _ _ 9 (copyF 9 1 10).length rfl
(copy_run prog _ 9 1 10 g3.1 rfl rfl rfl (pos0 + pe eR l.length + 1) x3 e3_9 e2_1)
let x4 := upd x3 10 (tr (pos0 + pe eR l.length + 1) ++ x3 10)
have e4_10 : x4 10 = tr (pos0 + pe eR l.length + 1) := by
show tr (pos0 + pe eR l.length + 1) ++ iter2 13 lenF l x 10 = _
rw [(q1.2.2.2 10 rfl rfl rfl).trans (hx 10 rfl), app_nil'']
-- 5. DROP 10 1
have R5 := runs_len prog _ _ _ _ _ _ (len_drop 10 1).symm (drop_run prog 10 1 _ (pos0 + pe eR l.length) x4 g4.1 e4_10)
let x5 := upd x4 10 (tr (pos0 + pe eR l.length))
have k5 : forall i, 18 <= i -> x5 i = [] := fun i hi =>
(upd_other _ 10 i _ (ne18 i 10 hi (Nat.le_of_ble_eq_true rfl))).trans
((upd_other _ 10 i _ (ne18 i 10 hi (Nat.le_of_ble_eq_true rfl))).trans
((upd_other _ 9 i _ (ne18 i 9 hi (Nat.le_of_ble_eq_true rfl))).trans (k2 i hi)))
have p5 : Pre 1 17 l.length 18 x5 := And.intro e2_1 (And.intro e2_17 k5)
-- 6. EVAL T into 11
have R6 := comp_run 1 17 prog l.length rfl eT 11 18 _ x5 g5.1 p5 (lt18 _ rfl)
(lt18 _ rfl) (lt18 _ rfl) rfl rfl
let x6 := upd x5 11 (tr (pe eT l.length) ++ x5 11)
-- 7. push 1 on 4
have R7 := push_run prog _ 4 true g6.1 x6
let x7 := upd x6 4 (true :: x6 4)
-- 8. WRITER
have z : forall i, Nat.beq i 2 = false -> Nat.beq i 13 = false -> Nat.beq i 17 = false -> x1 i = [] :=
fun i a b c => (q1.2.2.2 i a b c).trans (hx i b)
have w11 : x7 11 = tr (pe eT l.length) := by
show tr (pe eT l.length) ++ x1 11 = _
rw [z 11 rfl rfl rfl, app_nil'']
have w4 : x7 4 = tr 1 := by
show true :: x1 4 = tr 1
rw [z 4 rfl rfl rfl]
rfl
have W8 := writer_run prog _ k delta m pos0 q0 s (pe eR l.length) (pe eT l.length) l g7.1 x7
(z 0 rfl rfl rfl) e2_1 rfl (z 3 rfl rfl rfl) w4 (z 5 rfl rfl rfl) (z 6 rfl rfl rfl) (z 7 rfl rfl rfl) e3_9 rfl w11 (z 12 rfl rfl rfl) (revApp_twice l)
(z 14 rfl rfl rfl) (z 15 rfl rfl rfl)
exact Exists.elim W8 fun N8 hN => Exists.elim hN fun y8 hy => by
have y16 : y8 16 = [] := (hy.2.2.1 16 (Nat.le_refl 16)).trans (z 16 rfl rfl rfl)
have R9 := runs_len prog _ _ _ _ 5 (move 0 16).length rfl
(move_runs prog _ 0 16 g7.2 rfl _ [] y8 hy.2.1 y16)
have RUN := seq_runs prog _ _ _ _ _ _ _ _ R1 (seq_runs prog _ _ _ _ _ _ _ _ R2 (seq_runs prog _ _ _ _ _ _ _ _ R3
(seq_runs prog _ _ _ _ _ _ _ _ R4 (seq_runs prog _ _ _ _ _ _ _ _ R5 (seq_runs prog _ _ _ _ _ _ _ _ R6
(seq_runs prog _ _ _ _ _ _ _ _ R7 (seq_runs prog _ _ _ _ _ _ _ _ hy.1 R9)))))))
have hx70 : (x7 0).length = 0 := congrArg List.length (z 0 rfl rfl rfl)
have o8 : (y8 0).length <= Found.Pnp35.WB k delta m pos0 q0 s (pe eR l.length) (pe eT l.length) l.length :=
Nat.le_trans (Found.Pnp38.runs_len_le prog _ _ _ x7 y8 hy.1 0)
(Nat.le_trans (Nat.le_of_eq ((congrArg (fun z => z + N8) hx70).trans (Nat.zero_add N8))) hy.2.2.2)
have b9 : 3 * (revApp (encCNF (full delta k (pos0 + pe eR l.length + 1) (pe eT l.length)
(allPins pos0 q0 m (pos0 + pe eR l.length + 1) ++ inPins (inpOf s l)))) []).length + 1 <=
3 * Found.Pnp35.WB k delta m pos0 q0 s (pe eR l.length) (pe eT l.length) l.length + 1 :=
Nat.add_le_add_right (Nat.mul_le_mul (Nat.le_refl 3) (Nat.le_trans (Nat.le_of_eq (congrArg List.length hy.2.1).symm) o8)) 1
have b2 : 3 * (revApp l []).length + 1 <= 3 * l.length + 1 := Nat.le_of_eq (by rw [revApp_len]; rfl)
have BND := Nat.add_le_add (Nat.le_refl (4 * l.length + 1)) (Nat.add_le_add b2
(Nat.add_le_add (Nat.le_refl (ecost (eW pos0 eR) l.length))
(Nat.add_le_add (Nat.le_refl (4 * (pos0 + pe eR l.length + 1) + 1 + (3 * (pos0 + pe eR l.length + 1) + 1)))
(Nat.add_le_add (Nat.le_refl 1) (Nat.add_le_add (Nat.le_refl (ecost eT l.length))
(Nat.add_le_add (Nat.le_refl 1) (Nat.add_le_add hy.2.2.2 b9)))))))
refine Exists.intro _ (Exists.intro _ (And.intro RUN (And.intro ?_ BND)))
show selR (Nat.beq 16 16) _ _ = _
exact revApp_twice _
/-- For a Turing machine verifier (Pnp18.red): the register program writes enc (red x). -/
theorem redP_tm (prog : List Ins) (o : Nat) (M : TM) (p0 : Nat) (q0 : List Bool) (s : Nat) (eR eT : PE)
(hf : Frag prog o (redP (M.m + 2) (rule M) M.m (p0 + 1) q0 s eR eT)) (l : List Bool) (x : Nat -> List Bool)
(h13 : x 13 = l) (hx : forall i, Nat.beq i 13 = false -> x i = []) :
Exists fun N => Exists fun y => Runs prog N o x (o + (redP (M.m + 2) (rule M) M.m (p0 + 1) q0 s eR eT).length) y /\
y 16 = encCNF (Found.Pnp18.red M (p0 + 1 + pe eR l.length + 1) (pe eT l.length) p0 q0 (inpOf s l)) /\
N <= redB (M.m + 2) (rule M) M.m (p0 + 1) q0 s eR eT l.length :=
redP_run prog o (M.m + 2) (rule M) M.m (p0 + 1) q0 s eR eT hf l x h13 hx
end Found.Pnp37
#print axioms Found.Pnp37.move_runs
#print axioms Found.Pnp37.len_iter
#print axioms Found.Pnp37.len_steps
#print axioms Found.Pnp37.lenL_run
#print axioms Found.Pnp37.ne18
#print axioms Found.Pnp37.revApp_len
#print axioms Found.Pnp37.lt18
#print axioms Found.Pnp37.redP_run
#print axioms Found.Pnp37.redP_tm
#print axioms: 8 theorems, no axioms.
One step adds at most one bit to a register; a straight-line block costs at most its fixed bits plus a linear term per counter operation.
found/Pnp38.lean
/-
T330 piece 38. found/Pnp38 -- growth of registers and the cost of blocks (CL3b, 19).
Two tools for the step bound of the reduction program:
* one step of the register machine adds at most one bit to any register, so after n
steps a register is at most n bits longer than before (len_run, runs_len_le); in
particular the output of a program is no longer than its step count;
* a straight-line block (Pnp24) whose counter registers hold at most M ones costs at
most esum ops + |ops| (7M + 3) steps, where esum ops is the total length of its fixed
bit lists (a constant of the block).
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp33
namespace Found.Pnp38
open Found.Pnp20
open Found.Pnp21
open Found.Pnp24 (Op cost)
/-! ## A step adds at most one bit to a register -/
theorem len_upd (x : Nat -> List Bool) (r i : Nat) (v : List Bool) (h : v.length <= (x r).length + 1) :
(upd x r v i).length <= (x i).length + 1 := by
show (selR (Nat.beq i r) v (x i)).length <= (x i).length + 1
cases e : Nat.beq i r with
| false => exact Nat.le_succ _
| true =>
have ei : i = r := Found.Pnp2.beq_eq i r e
rw [ei]
exact h
theorem len_step (p : List Ins) (c : Cfg) (i : Nat) : ((step p c).regs i).length <= (c.regs i).length + 1 := by
show ((exec c (nthI p c.pc)).regs i).length <= _
cases nthI p c.pc with
| none => exact Nat.le_succ _
| some ins =>
cases ins with
| push r b => exact len_upd c.regs r i (b :: c.regs r) (Nat.le_refl _)
| jmp l => exact Nat.le_succ _
| pop r e z o =>
show ((popTo c r e z o (c.regs r)).regs i).length <= _
cases h : c.regs r with
| nil => exact Nat.le_succ _
| cons b t =>
cases b with
| false =>
exact len_upd c.regs r i t (by rw [h]; exact Nat.le_trans (Nat.le_succ t.length) (Nat.le_succ _))
| true =>
exact len_upd c.regs r i t (by rw [h]; exact Nat.le_trans (Nat.le_succ t.length) (Nat.le_succ _))
theorem len_run (p : List Ins) (i : Nat) : forall n (c : Cfg), ((run p n c).regs i).length <= (c.regs i).length + n := by
intro n
induction n with
| zero => intro c; exact Nat.le_refl _
| succ n ih =>
intro c
show ((run p n (step p c)).regs i).length <= (c.regs i).length + (n + 1)
have a := ih (step p c)
have b := Nat.add_le_add_right (len_step p c i) n
have e : (c.regs i).length + 1 + n = (c.regs i).length + (n + 1) := by
rw [Nat.add_assoc, Nat.add_comm 1 n]
exact Nat.le_trans a (Nat.le_trans b (Nat.le_of_eq e))
/-- After a fragment run of n steps, every register grew by at most n bits. -/
theorem runs_len_le (prog : List Ins) (n o o' : Nat) (x y : Nat -> List Bool) (h : Runs prog n o x o' y) (i : Nat) :
(y i).length <= (x i).length + n := by
rw [<- h.2 i]
exact len_run prog i n (Cfg.mk o x)
/-! ## The cost of a block -/
/-- The total length of the fixed bit lists of a block. -/
def esum : List Op -> Nat
| [] => 0
| .emit w :: rest => w.length + esum rest
| .un _ :: rest => esum rest
/-- Every counter register of the block holds at most M. -/
def UnBd (cnt : Nat -> Nat) (M : Nat) : List Op -> Prop
| [] => True
| .emit _ :: rest => UnBd cnt M rest
| .un R :: rest => cnt R <= M /\ UnBd cnt M rest
theorem cost_le (cnt : Nat -> Nat) (M : Nat) :
forall ops : List Op, UnBd cnt M ops -> cost cnt ops <= esum ops + ops.length * (4 * M + 1 + 1 + (3 * M + 1)) := by
intro ops
induction ops with
| nil => intro _; exact Nat.zero_le _
| cons op rest ih =>
intro h
have hK : forall a, a * (4 * M + 1 + 1 + (3 * M + 1)) <= (a + 1) * (4 * M + 1 + 1 + (3 * M + 1)) :=
fun a => Nat.mul_le_mul (Nat.le_succ a) (Nat.le_refl _)
cases op with
| emit w =>
show w.length + cost cnt rest <= (w.length + esum rest) + (rest.length + 1) * (4 * M + 1 + 1 + (3 * M + 1))
have a : cost cnt rest <= esum rest + (rest.length + 1) * (4 * M + 1 + 1 + (3 * M + 1)) :=
Nat.le_trans (ih h) (Nat.add_le_add_left (hK rest.length) _)
exact Nat.le_trans (Nat.add_le_add_left a w.length) (Nat.le_of_eq (Nat.add_assoc _ _ _).symm)
| un R =>
show (4 * cnt R + 1 + 1 + (3 * cnt R + 1)) + cost cnt rest <=
esum rest + (rest.length + 1) * (4 * M + 1 + 1 + (3 * M + 1))
have k1 : 4 * cnt R <= 4 * M := Nat.mul_le_mul (Nat.le_refl 4) h.1
have k2 : 3 * cnt R <= 3 * M := Nat.mul_le_mul (Nat.le_refl 3) h.1
have k : 4 * cnt R + 1 + 1 + (3 * cnt R + 1) <= 4 * M + 1 + 1 + (3 * M + 1) :=
Nat.add_le_add (Nat.add_le_add_right (Nat.add_le_add_right k1 1) 1) (Nat.add_le_add_right k2 1)
have a := Nat.add_le_add (ih h.2) k
have e : esum rest + rest.length * (4 * M + 1 + 1 + (3 * M + 1)) + (4 * M + 1 + 1 + (3 * M + 1)) =
esum rest + (rest.length + 1) * (4 * M + 1 + 1 + (3 * M + 1)) :=
(Nat.add_assoc _ _ _).trans (congrArg (fun z => esum rest + z) (Nat.succ_mul rest.length _).symm)
exact Nat.le_trans (Nat.le_of_eq (Nat.add_comm _ _)) (Nat.le_trans a (Nat.le_of_eq e))
/-- A block whose counter registers all lie in 2..7 (Pnp26) is bounded when those registers are. -/
theorem unBd_of (cnt : Nat -> Nat) (M : Nat) (h : forall R, Found.Pnp26.InR R -> cnt R <= M) :
forall ops, Found.Pnp26.UnIn ops -> UnBd cnt M ops := by
intro ops
induction ops with
| nil => intro _; exact True.intro
| cons op rest ih =>
intro hu
cases op with
| emit w => exact ih hu
| un R => exact And.intro (h R hu.1) (ih hu.2)
end Found.Pnp38
#print axioms Found.Pnp38.len_upd
#print axioms Found.Pnp38.len_step
#print axioms Found.Pnp38.len_run
#print axioms Found.Pnp38.runs_len_le
#print axioms Found.Pnp38.cost_le
#print axioms Found.Pnp38.unBd_of
#print axioms: 6 theorems, no axioms.
A loop whose body costs at most \(B\) on an invariant runs in at most \(|l|(B+2)+1\) steps; the cell loop keeps such an invariant.
found/Pnp39.lean
/-
T330 piece 39. found/Pnp39 -- the cost of one time round is bounded (CL3b, 20).
Proved:
* a loop whose body costs at most B on every state of an invariant runs in at most
|l| (B + 2) + 1 steps (steps_le_inv);
* the window block body costs at most BB M = esum ops + |ops| (7M + 3) + 3 when the
registers 2..7 hold at most M ones (a machine constant plus a linear term in M);
* the cell loop keeps "register 2 plus each of 5, 6, 7 is at most C" and "3, 4 at most
T'", so its steps are at most m (BB (C + T') + 2) + 1;
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp38
namespace Found.Pnp39
open Found.Pnp20
open Found.Pnp21
open Found.Pnp22 (steps iter)
open Found.Pnp23 (tr tr_len app_nil'')
open Found.Pnp24 (Op cost)
open Found.Pnp26 (InR cntOf fB nbB ops unIn_ops)
open Found.Pnp29 (st5 st8 rndCost)
open Found.Pnp38 (esum UnBd cost_le unBd_of)
/-! ## Loops with a bounded body -/
theorem steps_le_inv (c : Nat) (nb : (Nat -> List Bool) -> Nat) (f : (Nat -> List Bool) -> (Nat -> List Bool))
(P : (Nat -> List Bool) -> Prop) (B : Nat)
(hB : forall y b l, P y -> y c = b :: l -> nb (upd y c l) <= B)
(hP : forall y b l, P y -> y c = b :: l -> P (f (upd y c l)))
(hk : forall y, f y c = y c) :
forall (l : List Bool) (x : Nat -> List Bool), x c = l -> P x -> steps c nb f l x <= l.length * (B + 2) + 1 := by
intro l
induction l with
| nil => intro x _ _; show 1 <= 0 * (B + 2) + 1; rw [Nat.zero_mul]; exact Nat.le_refl 1
| cons b l ih =>
intro x hx hPx
show ((1 + nb (upd x c l)) + 1) + steps c nb f l (f (upd x c l)) <= (l.length + 1) * (B + 2) + 1
have h1 : (1 + nb (upd x c l)) + 1 <= B + 2 :=
Nat.le_trans (Nat.add_le_add_right (Nat.add_le_add_left (hB x b l hPx hx) 1) 1)
(Nat.le_of_eq ((Nat.add_right_comm 1 B 1).trans (Nat.add_comm (1 + 1) B)))
have h2 := Nat.add_le_add h1 (ih (f (upd x c l)) ((hk _).trans (upd_same x c l)) (hP x b l hPx hx))
have e : (B + 2) + (l.length * (B + 2) + 1) = (l.length + 1) * (B + 2) + 1 := by
rw [Nat.succ_mul, Nat.add_comm (B + 2), Nat.add_right_comm]
exact Nat.le_trans h2 (Nat.le_of_eq e)
/-! ## The window block body -/
def KK (M : Nat) : Nat := 4 * M + 1 + 1 + (3 * M + 1)
def BB (k : Nat) (delta : List Bool -> Nat -> Bool) (M : Nat) : Nat := esum (ops k delta) + (ops k delta).length * KK M + 3
theorem inr_cases (R : Nat) (h : InR R) :
R = 2 \/ R = 3 \/ R = 4 \/ R = 5 \/ R = 6 \/ R = 7 := by
match R, h with
| 0, h => exact False.elim (Nat.not_succ_le_zero 1 h.1)
| 1, h => exact False.elim (Nat.not_succ_le_self 1 h.1)
| 2, _ => exact Or.inl rfl
| 3, _ => exact Or.inr (Or.inl rfl)
| 4, _ => exact Or.inr (Or.inr (Or.inl rfl))
| 5, _ => exact Or.inr (Or.inr (Or.inr (Or.inl rfl)))
| 6, _ => exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl rfl))))
| 7, _ => exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr rfl))))
| n + 8, h => exact False.elim (Nat.not_succ_le_self 7 (Nat.le_trans (Nat.succ_le_succ (Nat.le_add_left 7 n)) h.2))
theorem nbB_le (k : Nat) (delta : List Bool -> Nat -> Bool) (M : Nat) (y : Nat -> List Bool)
(h2 : (y 2).length <= M) (h3 : (y 3).length <= M) (h4 : (y 4).length <= M)
(h5 : (y 5).length <= M) (h6 : (y 6).length <= M) (h7 : (y 7).length <= M) :
nbB k delta y <= BB k delta M := by
have hu : UnBd (cntOf y) M (ops k delta) := unBd_of (cntOf y) M (fun R hR => by
cases inr_cases R hR with
| inl e => rw [e]; exact h2
| inr e => cases e with
| inl e => rw [e]; exact h3
| inr e => cases e with
| inl e => rw [e]; exact h4
| inr e => cases e with
| inl e => rw [e]; exact h5
| inr e => cases e with
| inl e => rw [e]; exact h6
| inr e => rw [e]; exact h7) (ops k delta) (unIn_ops k delta)
exact Nat.add_le_add_right (cost_le (cntOf y) M (ops k delta) hu) 3
/-! ## The cell loop -/
/-- The cell loop invariant: the counter plus each of 5, 6, 7 is at most C; 3, 4 at most T'. -/
def J (C T' : Nat) (y : Nat -> List Bool) : Prop :=
(y 2).length + (y 5).length <= C /\ (y 2).length + (y 6).length <= C /\ (y 2).length + (y 7).length <= C /\
(y 3).length <= T' /\ (y 4).length <= T'
theorem jsteps_le (k : Nat) (delta : List Bool -> Nat -> Bool) (C T' : Nat) :
forall (m : Nat) (y : Nat -> List Bool), y 2 = tr m -> J C T' y ->
steps 2 (nbB k delta) (fB k delta) (tr m) y <= m * (BB k delta (C + T') + 2) + 1 := by
intro m y hy hJ
have L := steps_le_inv 2 (nbB k delta) (fB k delta) (J C T') (BB k delta (C + T'))
(fun z b l hz hz2 => by
have a2 : l.length + 1 = (z 2).length := (congrArg List.length hz2).symm
have b2 : l.length <= C + T' := Nat.le_trans (Nat.le_succ _) (Nat.le_trans (Nat.le_of_eq a2)
(Nat.le_trans (Nat.le_add_right _ _) (Nat.le_trans hz.1 (Nat.le_add_right C T'))))
have b5 : (z 5).length <= C + T' := Nat.le_trans (Nat.le_add_left _ _) (Nat.le_trans hz.1 (Nat.le_add_right C T'))
have b6 : (z 6).length <= C + T' := Nat.le_trans (Nat.le_add_left _ _) (Nat.le_trans hz.2.1 (Nat.le_add_right C T'))
have b7 : (z 7).length <= C + T' := Nat.le_trans (Nat.le_add_left _ _) (Nat.le_trans hz.2.2.1 (Nat.le_add_right C T'))
have b3 : (z 3).length <= C + T' := Nat.le_trans hz.2.2.2.1 (Nat.le_add_left T' C)
have b4 : (z 4).length <= C + T' := Nat.le_trans hz.2.2.2.2 (Nat.le_add_left T' C)
exact nbB_le k delta (C + T') (upd z 2 l) b2 b3 b4 b5 b6 b7)
(fun z b l hz hz2 => by
have a2 : l.length + 1 = (z 2).length := (congrArg List.length hz2).symm
have q : forall R, (l.length + 1) + (z R).length <= C -> (fB k delta (upd z 2 l) 2).length + ((z R).length + 1) <= C :=
fun R h => Nat.le_trans (Nat.le_of_eq ((Nat.add_assoc _ _ _).symm.trans (Nat.add_right_comm _ _ _))) h
refine And.intro (q 5 ?_) (And.intro (q 6 ?_) (And.intro (q 7 ?_) (And.intro hz.2.2.2.1 hz.2.2.2.2)))
· rw [a2]; exact hz.1
· rw [a2]; exact hz.2.1
· rw [a2]; exact hz.2.2.1)
(fun _ => rfl) (tr m) y hy hJ
rw [tr_len] at L
exact L
end Found.Pnp39
#print axioms Found.Pnp39.steps_le_inv
#print axioms Found.Pnp39.inr_cases
#print axioms Found.Pnp39.nbB_le
#print axioms Found.Pnp39.jsteps_le
#print axioms: 4 theorems, no axioms.
One time round costs at most an explicit \(RB\); the time loop over \(T\) rounds costs at most \(T(RB+2)+1\) steps.
found/Pnp40.lean
/-
T330 piece 40. found/Pnp40 -- the time loop runs in polynomially many steps (CL3b, 21).
Proved:
* after one time round, registers 5, 6, 7 hold W - 1, W, W + 1 ones (rnd_regs);
* one time round from a state with 2 empty, 3 = t, 4 = t + 1, t + 1 <= T and
5, 6, 7 at most C0 ones costs at most RB, an explicit sum of terms linear in W, T,
C0 and of W' (BB (W' + 2 + T) + 2), where BB is the window block bound (Pnp39);
* the time loop over T rounds from t = 0 (5, 6, 7 at most W' + 2 ones) runs in at
most T (RB + 2) + 1 steps.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp39
namespace Found.Pnp40
open Found.Pnp20
open Found.Pnp21
open Found.Pnp22 (steps iter)
open Found.Pnp23 (tr tr_len app_nil'')
open Found.Pnp26 (fB nbB)
open Found.Pnp29 (st5 st8 rndCost rndF rndF_keep)
open Found.Pnp39 (BB J steps_le_inv nbB_le jsteps_le)
theorem h82 (k : Nat) (delta : List Bool -> Nat -> Bool) (W' : Nat) (x : Nat -> List Bool) (h2 : x 2 = []) :
st8 k delta W' x 2 = tr W' := by
show tr W' ++ x 2 = tr W'
rw [h2, app_nil'']
theorem rnd_regs (k : Nat) (delta : List Bool -> Nat -> Bool) (W' t : Nat) (x : Nat -> List Bool)
(h2 : x 2 = []) (h3 : x 3 = tr t) (h4 : x 4 = tr (t + 1)) :
(rndF k delta W' x 5).length = W' /\ (rndF k delta W' x 6).length = W' + 1 /\ (rndF k delta W' x 7).length = W' + 2 := by
have sp := Found.Pnp26.iter_spec k delta (W' + 1) t W' 0 (st8 k delta W' x) (h82 k delta W' x h2) h3 h4 rfl rfl rfl
refine And.intro ?_ (And.intro ?_ ?_)
· show (iter 2 (fB k delta) (tr W') (st8 k delta W' x) 5).length = W'
rw [sp.2.1, tr_len, Nat.zero_add]
· show (iter 2 (fB k delta) (tr W') (st8 k delta W' x) 6).length = W' + 1
rw [sp.2.2.1, tr_len, Nat.zero_add, Nat.add_comm 1 W']
· show (iter 2 (fB k delta) (tr W') (st8 k delta W' x) 7).length = W' + 2
rw [sp.2.2.2.1, tr_len, Nat.zero_add, Nat.add_comm 2 W']
/-- The bound of one round. -/
def RB (k : Nat) (delta : List Bool -> Nat -> Bool) (W' T' C0 : Nat) : Nat :=
2 * C0 + 1 + (2 * C0 + 1) + (2 * C0 + 1) + (4 * (W' + 1) + 1 + (3 * (W' + 1) + 1)) + 1 +
BB k delta (W' + 2 + T') + (2 * (W' + 2) + 1) + (4 * W' + 1 + (3 * W' + 1)) +
(W' * (BB k delta (W' + 2 + T') + 2) + 1) + 2
theorem one_le (a b : Nat) : 1 <= a + 1 + b := Nat.le_trans (Nat.succ_le_succ (Nat.zero_le a)) (Nat.le_add_right _ _)
theorem rnd_le (k : Nat) (delta : List Bool -> Nat -> Bool) (W' T' C0 t : Nat) (x : Nat -> List Bool)
(h2 : x 2 = []) (h3 : x 3 = tr t) (h4 : x 4 = tr (t + 1)) (ht : t + 1 <= T')
(h5 : (x 5).length <= C0) (h6 : (x 6).length <= C0) (h7 : (x 7).length <= C0) :
rndCost k delta W' x <= RB k delta W' T' C0 := by
have tT : t <= T' := Nat.le_trans (Nat.le_succ t) ht
have M3 : T' <= W' + 2 + T' := Nat.le_add_left T' (W' + 2)
have t1 := Nat.add_le_add_right (Nat.mul_le_mul (Nat.le_refl 2) h5) 1
have t2 := Nat.add_le_add_right (Nat.mul_le_mul (Nat.le_refl 2) h6) 1
have t3 := Nat.add_le_add_right (Nat.mul_le_mul (Nat.le_refl 2) h7) 1
have t6 : nbB k delta (st5 W' x) <= BB k delta (W' + 2 + T') := by
apply nbB_le
· show (x 2).length <= _
rw [h2]; exact Nat.zero_le _
· show (x 3).length <= _
rw [h3, tr_len]; exact Nat.le_trans tT M3
· show (x 4).length <= _
rw [h4, tr_len]; exact Nat.le_trans ht M3
· show (tr (W' + 1) ++ []).length <= _
rw [app_nil'', tr_len]; exact Nat.le_trans (Nat.le_succ (W' + 1)) (Nat.le_add_right _ _)
· exact Nat.zero_le _
· exact one_le (W' + 1) T'
have t7 : 2 * (fB k delta (st5 W' x) 5).length + 1 <= 2 * (W' + 2) + 1 := Nat.le_of_eq (by
show 2 * (tr (W' + 2) ++ []).length + 1 = 2 * (W' + 2) + 1
rw [app_nil'', tr_len])
have e8 : (tr W' ++ x 2).length = W' := by rw [h2, app_nil'', tr_len]
have hJ : J (W' + 2) T' (st8 k delta W' x) := by
refine And.intro ?_ (And.intro ?_ (And.intro ?_ (And.intro ?_ ?_)))
· show (tr W' ++ x 2).length + 0 <= W' + 2
exact Nat.le_trans (Nat.le_of_eq (congrArg (fun z => z + 0) e8)) (Nat.le_add_right W' 2)
· show (tr W' ++ x 2).length + 1 <= W' + 2
exact Nat.le_trans (Nat.le_of_eq (congrArg (fun z => z + 1) e8)) (Nat.le_succ (W' + 1))
· show (tr W' ++ x 2).length + 2 <= W' + 2
exact Nat.le_of_eq (congrArg (fun z => z + 2) e8)
· show (x 3).length <= T'
rw [h3, tr_len]; exact tT
· show (x 4).length <= T'
rw [h4, tr_len]; exact ht
have t9 := jsteps_le k delta (W' + 2) T' W' (st8 k delta W' x) (h82 k delta W' x h2) hJ
have a3 := Nat.add_le_add (Nat.add_le_add t1 t2) t3
have a5 := Nat.add_le_add (Nat.add_le_add_right (Nat.add_le_add_right a3 (4 * (W' + 1) + 1 + (3 * (W' + 1) + 1))) 1) t6
have a8 := Nat.add_le_add (Nat.add_le_add_right (Nat.add_le_add a5 t7) (4 * W' + 1 + (3 * W' + 1))) t9
exact Nat.add_le_add_right a8 2
/-- The time loop invariant used for the step bound. -/
def PT (W' T : Nat) (y : Nat -> List Bool) : Prop :=
y 1 = [] /\ y 2 = [] /\ (Exists fun t => y 3 = tr t /\ y 4 = tr (t + 1) /\ t + (y 11).length <= T) /\
(y 5).length <= W' + 2 /\ (y 6).length <= W' + 2 /\ (y 7).length <= W' + 2
theorem tloop_steps_le (k : Nat) (delta : List Bool -> Nat -> Bool) (W' T : Nat) (x : Nat -> List Bool)
(hx : PT W' T x) (h11 : x 11 = tr T) :
steps 11 (rndCost k delta W') (rndF k delta W') (tr T) x <= T * (RB k delta W' T (W' + 2) + 2) + 1 := by
have L := steps_le_inv 11 (rndCost k delta W') (rndF k delta W') (PT W' T) (RB k delta W' T (W' + 2))
(fun y b l hy hy11 => Exists.elim hy.2.2.1 fun t ht => by
have e : t + (l.length + 1) <= T := by
have q := ht.2.2
rw [hy11] at q
exact q
have ht1 : t + 1 <= T := Nat.le_trans (Nat.add_le_add_left (Nat.le_add_left 1 l.length) t) e
exact rnd_le k delta W' T (W' + 2) t (upd y 11 l) hy.2.1 ht.1 ht.2.1 ht1 hy.2.2.2.1 hy.2.2.2.2.1 hy.2.2.2.2.2)
(fun y b l hy hy11 => Exists.elim hy.2.2.1 fun t ht => by
have e : t + (l.length + 1) <= T := by
have q := ht.2.2
rw [hy11] at q
exact q
have r := Found.Pnp30.rnd_facts k delta W' t (upd y 11 l) hy.1 hy.2.1 ht.1 ht.2.1
have g := rnd_regs k delta W' t (upd y 11 l) hy.2.1 ht.1 ht.2.1
have k11 : rndF k delta W' (upd y 11 l) 11 = l := (rndF_keep k delta W' _ 11 (Nat.le_of_ble_eq_true rfl)).trans (upd_same y 11 l)
refine And.intro r.2.1 (And.intro r.2.2.1 (And.intro (Exists.intro (t + 1) (And.intro r.2.2.2.1 (And.intro r.2.2.2.2 ?_)))
(And.intro ?_ (And.intro ?_ ?_))))
· rw [k11]
exact Nat.le_trans (Nat.le_of_eq (Nat.add_right_comm t 1 l.length)) e
· rw [g.1]; exact Nat.le_add_right W' 2
· rw [g.2.1]; exact Nat.le_succ (W' + 1)
· exact Nat.le_of_eq g.2.2)
(fun y => rndF_keep k delta W' y 11 (Nat.le_of_ble_eq_true rfl)) (tr T) x h11 hx
rw [tr_len] at L
exact L
end Found.Pnp40
#print axioms Found.Pnp40.h82
#print axioms Found.Pnp40.rnd_regs
#print axioms Found.Pnp40.one_le
#print axioms Found.Pnp40.rnd_le
#print axioms Found.Pnp40.tloop_steps_le
#print axioms: 5 theorems, no axioms.
The boundary loop, the layout pin loop and the input loop have explicit linear-times-linear step bounds.
found/Pnp41.lean
/-
T330 piece 41. found/Pnp41 -- step bounds of the boundary, layout and input loops (CL3b, 22).
Proved:
* a block bound for a concatenation is the pair of bounds (unBd_app);
* the boundary loop from t = 0 over T + 1 rounds, with W in register 9, runs in at most
(T + 1) (Bb + 2) + 1 steps, Bb = esum (bdOps 3 9 k) + |bdOps 3 9 k| KK (W + T) + 1;
* the layout pin loop over r cells from cell a runs in at most r (Bc + 2) + 1 steps,
Bc = esum (fcOps m) + |fcOps m| KK (a + r) + 1, and leaves a + r ones in register 14;
* the branching input loop over the input l from cell s runs in at most
|l| (Bp + 2) + 1 steps, Bp = 6 + 3 KK (s + |l|) + 1.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp40
namespace Found.Pnp41
open Found.Pnp20
open Found.Pnp21
open Found.Pnp22 (steps iter)
open Found.Pnp23 (tr tr_len)
open Found.Pnp24 (Op cost)
open Found.Pnp26 (cntOf)
open Found.Pnp31 (bdOps uOps fb nbb)
open Found.Pnp32 (steps2 pOps fp nbp)
open Found.Pnp33 (fcOps uo fcF nbc)
open Found.Pnp38 (esum UnBd cost_le)
open Found.Pnp39 (KK steps_le_inv)
theorem unBd_app (cnt : Nat -> Nat) (M : Nat) :
forall (A B : List Op), UnBd cnt M A -> UnBd cnt M B -> UnBd cnt M (A ++ B) := by
intro A
induction A with
| nil => intro B _ hB; exact hB
| cons op A ih =>
intro B hA hB
cases op with
| emit w => exact ih B hA hB
| un R => exact And.intro hA.1 (ih B hA.2 hB)
theorem le_of_add_le_l (a b c : Nat) (h : a + b <= c) : a <= c := Nat.le_trans (Nat.le_add_right a b) h
theorem le_of_add_le_r (a b c : Nat) (h : a + b <= c) : b <= c := Nat.le_trans (Nat.le_add_left b a) h
/-- From "a + (l + 1) <= C" (a counter entry) to "a <= C" and its successor form. -/
theorem shift_le (a L C : Nat) (h : a + (L + 1) <= C) : (a + 1) + L <= C :=
Nat.le_trans (Nat.le_of_eq (Nat.add_right_comm a 1 L)) h
/-! ## The boundary loop -/
theorem unBd_bd (cnt : Nat -> Nat) (M : Nat) (h3 : cnt 3 <= M) (h9 : cnt 9 <= M) :
forall n, UnBd cnt M (bdOps 3 9 n) := by
intro n
induction n with
| zero => exact True.intro
| succ n ih => exact unBd_app cnt M _ _ ih (And.intro h3 (And.intro h9 True.intro))
def Bb (k W T : Nat) : Nat := esum (bdOps 3 9 k) + (bdOps 3 9 k).length * KK (W + T) + 1
def PB (W T : Nat) (y : Nat -> List Bool) : Prop := (y 3).length + (y 12).length <= T + 1 /\ y 9 = tr W
theorem bloop_steps_le (k W T : Nat) (x : Nat -> List Bool) (hx : PB W T x) (h12 : x 12 = tr (T + 1)) :
steps 12 (nbb k) (fb k) (tr (T + 1)) x <= (T + 1) * (Bb k W T + 2) + 1 := by
have L := steps_le_inv 12 (nbb k) (fb k) (PB W T) (Bb k W T)
(fun y b l hy hy12 => by
have e : (y 3).length + (l.length + 1) <= T + 1 := by
have q := hy.1
rw [hy12] at q
exact q
have h3 : (y 3).length <= W + T :=
Nat.le_trans (Nat.le_of_succ_le_succ (Nat.le_trans (Nat.add_le_add_left (Nat.le_add_left 1 l.length) _) e))
(Nat.le_add_left T W)
have h9 : cntOf (upd y 12 l) 9 <= W + T := by
show (y 9).length <= W + T
rw [hy.2, tr_len]; exact Nat.le_add_right W T
exact Nat.add_le_add_right (cost_le (cntOf (upd y 12 l)) (W + T) _ (unBd_bd _ _ h3 h9 k)) 1)
(fun y b l hy hy12 => by
have e : (y 3).length + (l.length + 1) <= T + 1 := by
have q := hy.1
rw [hy12] at q
exact q
exact And.intro (shift_le _ _ _ e) hy.2)
(fun y => Found.Pnp31.fb_keep k y 12 rfl rfl) (tr (T + 1)) x h12 hx
rw [tr_len] at L
exact L
/-! ## The layout pin loop -/
theorem unBd_fc (cnt : Nat -> Nat) (M : Nat) (h : cnt 14 <= M) : forall i, UnBd cnt M (fcOps i) := by
intro i
induction i with
| zero => exact And.intro h True.intro
| succ i ih => exact unBd_app cnt M _ _ ih (And.intro h True.intro)
def Bc (m a r : Nat) : Nat := esum (fcOps m) + (fcOps m).length * KK (a + r) + 1
def PC (a r : Nat) (y : Nat -> List Bool) : Prop := (y 14).length + (y 12).length <= a + r
theorem cloop_steps_le (m a r : Nat) (x : Nat -> List Bool) (hx : PC a r x) (h12 : x 12 = tr r) :
steps 12 (nbc m) (fcF m) (tr r) x <= r * (Bc m a r + 2) + 1 := by
have L := steps_le_inv 12 (nbc m) (fcF m) (PC a r) (Bc m a r)
(fun y b l hy hy12 => by
have e : (y 14).length + (l.length + 1) <= a + r := by
have q := hy
unfold PC at q
rw [hy12] at q
exact q
have h14 : cntOf (upd y 12 l) 14 <= a + r := le_of_add_le_l _ _ _ e
exact Nat.add_le_add_right (cost_le (cntOf (upd y 12 l)) (a + r) _ (unBd_fc _ _ h14 m)) 1)
(fun y b l hy hy12 => by
have e : (y 14).length + (l.length + 1) <= a + r := by
have q := hy
unfold PC at q
rw [hy12] at q
exact q
exact shift_le _ _ _ e)
(fun y => Found.Pnp33.fcF_keep m y 12 rfl rfl) (tr r) x h12 hx
rw [tr_len] at L
exact L
theorem cIter_14 (m : Nat) : forall r a (x : Nat -> List Bool), x 14 = tr a -> iter 12 (fcF m) (tr r) x 14 = tr (a + r) := by
intro r
induction r with
| zero => intro a x h; exact h
| succ r ih =>
intro a x h
show iter 12 (fcF m) (tr r) (fcF m (upd x 12 (tr r))) 14 = tr (a + (r + 1))
rw [ih (a + 1) _ (congrArg (fun l => true :: l) h), Nat.add_right_comm a 1 r]
rfl
/-! ## The input loop -/
theorem steps2_le_inv (c : Nat) (nb : Bool -> (Nat -> List Bool) -> Nat) (f : Bool -> (Nat -> List Bool) -> (Nat -> List Bool))
(P : (Nat -> List Bool) -> Prop) (B : Nat)
(hB : forall y b l, P y -> y c = b :: l -> nb b (upd y c l) <= B)
(hP : forall y b l, P y -> y c = b :: l -> P (f b (upd y c l)))
(hk : forall b y, f b y c = y c) :
forall (l : List Bool) (x : Nat -> List Bool), x c = l -> P x -> steps2 c nb f l x <= l.length * (B + 2) + 1 := by
intro l
induction l with
| nil => intro x _ _; show 1 <= 0 * (B + 2) + 1; rw [Nat.zero_mul]; exact Nat.le_refl 1
| cons b l ih =>
intro x hx hPx
show ((1 + nb b (upd x c l)) + 1) + steps2 c nb f l (f b (upd x c l)) <= (l.length + 1) * (B + 2) + 1
have h1 : (1 + nb b (upd x c l)) + 1 <= B + 2 :=
Nat.le_trans (Nat.add_le_add_right (Nat.add_le_add_left (hB x b l hPx hx) 1) 1)
(Nat.le_of_eq ((Nat.add_right_comm 1 B 1).trans (Nat.add_comm (1 + 1) B)))
have hc : f b (upd x c l) c = l := (hk b _).trans (upd_same x c l)
have h2 := Nat.add_le_add h1 (ih (f b (upd x c l)) hc (hP x b l hPx hx))
have e : (B + 2) + (l.length * (B + 2) + 1) = (l.length + 1) * (B + 2) + 1 := by
rw [Nat.succ_mul, Nat.add_comm (B + 2), Nat.add_right_comm]
exact Nat.le_trans h2 (Nat.le_of_eq e)
def Bp (s L : Nat) : Nat := 6 + 3 * KK (s + L) + 1
def PI (s L : Nat) (y : Nat -> List Bool) : Prop := (y 14).length + (y 13).length <= s + L
theorem inLoop_steps_le (s : Nat) (l : List Bool) (x : Nat -> List Bool) (hx : PI s l.length x) (h13 : x 13 = l) :
steps2 13 nbp fp l x <= l.length * (Bp s l.length + 2) + 1 :=
steps2_le_inv 13 nbp fp (PI s l.length) (Bp s l.length)
(fun y b l' hy hy13 => by
have e : (y 14).length + (l'.length + 1) <= s + l.length := by
have q := hy
unfold PI at q
rw [hy13] at q
exact q
have h14 : cntOf (upd y 13 l') 14 <= s + l.length := le_of_add_le_l _ _ _ e
exact Nat.add_le_add_right (cost_le (cntOf (upd y 13 l')) (s + l.length) (pOps b) (And.intro h14 True.intro)) 1)
(fun y b l' hy hy13 => by
have e : (y 14).length + (l'.length + 1) <= s + l.length := by
have q := hy
unfold PI at q
rw [hy13] at q
exact q
exact shift_le _ _ _ e)
(fun b y => Found.Pnp32.fp_keep b y 13 rfl rfl) l x h13 hx
end Found.Pnp41
#print axioms Found.Pnp41.unBd_app
#print axioms Found.Pnp41.le_of_add_le_l
#print axioms Found.Pnp41.le_of_add_le_r
#print axioms Found.Pnp41.shift_le
#print axioms Found.Pnp41.unBd_bd
#print axioms Found.Pnp41.bloop_steps_le
#print axioms Found.Pnp41.unBd_fc
#print axioms Found.Pnp41.cloop_steps_le
#print axioms Found.Pnp41.cIter_14
#print axioms Found.Pnp41.steps2_le_inv
#print axioms Found.Pnp41.inLoop_steps_le
#print axioms: 11 theorems, no axioms.
Every bound is a polynomial expression in the input length; the register program computing the Cook–Levin map runs in at most that many steps.
found/Pnp42.lean
/-
T330 piece 42. found/Pnp42 -- the reduction program runs in polynomial time (CL3b, 23).
Every step bound of Pnp35-Pnp41 is written again as a polynomial expression (Pnp3.PE)
in the input length, with the machine and the layout as constants: ecP e for the
evaluator cost, KKp, BBp, RBp, Bbp, Bcp, Bpp, WBp, and redPE for the whole program.
Proved: pe (ecP e) n = ecost e n, pe (redPE ...) n = redB ... n, so the register program
RED computes x -> enc (red M W T p0 q0 (inpOf s x)) (Pnp18) in at most pe redPE |x| steps:
CL3b, polynomial time on the register machine, for W = p0 + 1 + R(|x|) + 1 and
T = T(|x|) with R, T polynomials.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp37
namespace Found.Pnp42
open Found.Pnp3 (PE pe)
open Found.Pnp12 (TM rule)
open Found.Pnp19 (encCNF)
open Found.Pnp20
open Found.Pnp21
open Found.Pnp32 (inpOf)
open Found.Pnp36 (ecost)
open Found.Pnp37 (eW redB redP redP_tm)
def a (p q : PE) : PE := .add p q
def mu (p q : PE) : PE := .mul p q
def k_ (c : Nat) : PE := .c c
/-! ## The evaluator cost -/
def ecP : PE -> PE
| .x => a (a (mu (k_ 4) .x) (k_ 1)) (a (mu (k_ 3) .x) (k_ 1))
| .c c => .c c
| .add p q => a (ecP p) (ecP q)
| .mul p q => a (ecP p) (a (mu p (a (ecP q) (k_ 2))) (k_ 1))
theorem ecP_pe (n : Nat) : forall e : PE, pe (ecP e) n = ecost e n := by
intro e
induction e with
| x => rfl
| c c => rfl
| add p q ihp ihq =>
show pe (ecP p) n + pe (ecP q) n = ecost p n + ecost q n
rw [ihp, ihq]
| mul p q ihp ihq =>
show pe (ecP p) n + (pe p n * (pe (ecP q) n + 2) + 1) = ecost p n + (pe p n * (ecost q n + 2) + 1)
rw [ihp, ihq]
/-! ## The bounds as polynomials -/
def KKp (M : PE) : PE := a (a (a (mu (k_ 4) M) (k_ 1)) (k_ 1)) (a (mu (k_ 3) M) (k_ 1))
def BBp (k : Nat) (delta : List Bool -> Nat -> Bool) (M : PE) : PE :=
a (a (k_ (Found.Pnp38.esum (Found.Pnp26.ops k delta))) (mu (k_ (Found.Pnp26.ops k delta).length) (KKp M))) (k_ 3)
def RBp (k : Nat) (delta : List Bool -> Nat -> Bool) (W' T' C0 : PE) : PE :=
a (a (a (a (a (a (a (a (a (a (mu (k_ 2) C0) (k_ 1)) (a (mu (k_ 2) C0) (k_ 1))) (a (mu (k_ 2) C0) (k_ 1)))
(a (a (mu (k_ 4) (a W' (k_ 1))) (k_ 1)) (a (mu (k_ 3) (a W' (k_ 1))) (k_ 1)))) (k_ 1))
(BBp k delta (a (a W' (k_ 2)) T'))) (a (mu (k_ 2) (a W' (k_ 2))) (k_ 1)))
(a (a (mu (k_ 4) W') (k_ 1)) (a (mu (k_ 3) W') (k_ 1))))
(a (mu W' (a (BBp k delta (a (a W' (k_ 2)) T')) (k_ 2))) (k_ 1))) (k_ 2)
def Bbp (k : Nat) (W T : PE) : PE :=
a (a (k_ (Found.Pnp38.esum (Found.Pnp31.bdOps 3 9 k))) (mu (k_ (Found.Pnp31.bdOps 3 9 k).length) (KKp (a W T)))) (k_ 1)
def Bcp (m : Nat) (A R : PE) : PE :=
a (a (k_ (Found.Pnp38.esum (Found.Pnp33.fcOps m))) (mu (k_ (Found.Pnp33.fcOps m).length) (KKp (a A R)))) (k_ 1)
def Bpp (S L : PE) : PE := a (a (k_ 6) (mu (k_ 3) (KKp (a S L)))) (k_ 1)
def WBp (k : Nat) (delta : List Bool -> Nat -> Bool) (m pos0 : Nat) (q0 : List Bool) (s : Nat) (r T L : PE) : PE :=
a (a (mu T (a (RBp k delta (a (k_ pos0) r) T (a (a (k_ pos0) r) (k_ 2))) (k_ 2))) (k_ 1))
(a (a (a (mu (k_ 4) T) (k_ 1)) (a (mu (k_ 3) T) (k_ 1)))
(a (a (a (mu (k_ 4) (a T (k_ 1))) (k_ 1)) (a (mu (k_ 3) (a T (k_ 1))) (k_ 1)))
(a (a (mu (k_ 2) T) (k_ 1))
(a (a (mu (a T (k_ 1)) (a (Bbp k (a (a (k_ pos0) r) (k_ 1)) T) (k_ 2))) (k_ 1))
(a (a (k_ (Found.Pnp38.esum (Found.Pnp33.accOps 15))) (mu (k_ (Found.Pnp33.accOps 15).length) (KKp T)))
(a (a (k_ (Found.Pnp35.hw m pos0 q0).length) (k_ 0))
(a (a (k_ pos0) (k_ 1))
(a (a (a (mu (k_ 4) (a (k_ pos0) r)) (k_ 1)) (a (mu (k_ 3) (a (k_ pos0) r)) (k_ 1)))
(a (k_ pos0)
(a (a (mu r (a (Bcp m (a (k_ pos0) (k_ 1)) r) (k_ 2))) (k_ 1))
(a (a (mu (k_ 2) (a (a (k_ pos0) (k_ 1)) r)) (k_ 1))
(a (k_ s)
(a (a (mu L (a (Bpp (k_ s) L) (k_ 2))) (k_ 1)) (k_ 1))))))))))))))
theorem WBp_pe (k : Nat) (delta : List Bool -> Nat -> Bool) (m pos0 : Nat) (q0 : List Bool) (s : Nat) (r T L : PE) (n : Nat) :
pe (WBp k delta m pos0 q0 s r T L) n = Found.Pnp35.WB k delta m pos0 q0 s (pe r n) (pe T n) (pe L n) := rfl
/-- The step bound of RED as a polynomial of the input length. -/
def redPE (k : Nat) (delta : List Bool -> Nat -> Bool) (m pos0 : Nat) (q0 : List Bool) (s : Nat) (eR eT : PE) : PE :=
a (a (mu (k_ 4) .x) (k_ 1)) (a (a (mu (k_ 3) .x) (k_ 1)) (a (ecP (eW pos0 eR))
(a (a (a (mu (k_ 4) (a (a (k_ pos0) eR) (k_ 1))) (k_ 1)) (a (mu (k_ 3) (a (a (k_ pos0) eR) (k_ 1))) (k_ 1)))
(a (k_ 1) (a (ecP eT) (a (k_ 1) (a (WBp k delta m pos0 q0 s eR eT .x)
(a (mu (k_ 3) (WBp k delta m pos0 q0 s eR eT .x)) (k_ 1)))))))))
theorem redPE_pe (k : Nat) (delta : List Bool -> Nat -> Bool) (m pos0 : Nat) (q0 : List Bool) (s : Nat) (eR eT : PE) (n : Nat) :
pe (redPE k delta m pos0 q0 s eR eT) n = redB k delta m pos0 q0 s eR eT n := by
show (4 * n + 1) + ((3 * n + 1) + (pe (ecP (eW pos0 eR)) n + ((4 * (pos0 + pe eR n + 1) + 1 + (3 * (pos0 + pe eR n + 1) + 1)) +
(1 + (pe (ecP eT) n + (1 + (Found.Pnp35.WB k delta m pos0 q0 s (pe eR n) (pe eT n) n +
(3 * Found.Pnp35.WB k delta m pos0 q0 s (pe eR n) (pe eT n) n + 1)))))))) = _
rw [ecP_pe, ecP_pe]
rfl
/-- CL3b: the reduction map is computed by one register program in polynomially many steps. -/
theorem cl3b (prog : List Ins) (o : Nat) (M : TM) (p0 : Nat) (q0 : List Bool) (s : Nat) (eR eT : PE)
(hf : Frag prog o (redP (M.m + 2) (rule M) M.m (p0 + 1) q0 s eR eT)) (l : List Bool) (x : Nat -> List Bool)
(h13 : x 13 = l) (hx : forall i, Nat.beq i 13 = false -> x i = []) :
Exists fun N => Exists fun y => Runs prog N o x (o + (redP (M.m + 2) (rule M) M.m (p0 + 1) q0 s eR eT).length) y /\
y 16 = encCNF (Found.Pnp18.red M (p0 + 1 + pe eR l.length + 1) (pe eT l.length) p0 q0 (inpOf s l)) /\
N <= pe (redPE (M.m + 2) (rule M) M.m (p0 + 1) q0 s eR eT) l.length := by
have h := redP_tm prog o M p0 q0 s eR eT hf l x h13 hx
rw [redPE_pe]
exact h
end Found.Pnp42
#print axioms Found.Pnp42.ecP_pe
#print axioms Found.Pnp42.WBp_pe
#print axioms Found.Pnp42.redPE_pe
#print axioms Found.Pnp42.cl3b
#print axioms: 4 theorems, no axioms.
A Turing machine with \(k+1\) bit tracks per cell is one local machine step on cells of \(m+2+k\) bits.
found/Pnp43.lean
/-
T330 piece 43. found/Pnp43 -- a multi-track Turing machine as a local machine (CL3c, 1).
CL3c simulates the register machine (Pnp20) by a Turing machine. Registers are easiest
on separate tracks, so the one-bit tape of Pnp12 is widened: a tape cell holds k + 1
bits (tracks 0..k), states are bit lists of length m, and from state q reading the
symbol s (the k + 1 bits of the head cell) the machine moves to next q s, writes
write q s and moves right iff right q s. A configuration is laid out on cells of
K = m + 2 + k bits in the shape of Pnp12, so that the pins and the reduction program of
Pnp14-Pnp42 apply unchanged: bit 0 track 0, bit 1 "the head is here", bits 2..m + 1 the
state where the head is (false elsewhere), bits m + 2 + i track i + 1. (k = 0 is the
machine of Pnp12.) Proved: one step of the width-W local machine (Pnp10) with the rule
below, on the layout of a configuration, is the layout of the next configuration at
every cell j <= W and bit b < K, when the head is at a cell p + 1 with p + 2 < W, the
boundary cell W holds false and the state has m bits.
It relativizes (a tool; PNP_MAP.md). Written with ASCII forms only.
CIC dependence: none.
-/
import found.Pnp12
namespace Found.Pnp43
open Found.Pnp1 (read)
open Found.Pnp7 (nthd read_nthd length_read)
open Found.Pnp9 (leftIdx)
open Found.Pnp10 (Conf nbC stepW gate read_ext_k beq_refl)
open Found.Pnp12 (sel selN accL accC accR head_eq head_eq2 beqW and_assoc3)
structure TMk where
m : Nat
k : Nat
next : List Bool -> List Bool -> List Bool
write : List Bool -> List Bool -> List Bool
right : List Bool -> List Bool -> Bool
/-- Tapes: cell j, bit i. -/
abbrev Tape := Nat -> Nat -> Bool
/-- pick m i f g: f i for i < m, g (i - m) otherwise (structural in m). -/
def pick : Nat -> Nat -> (Nat -> Bool) -> (Nat -> Bool) -> Bool
| 0, i, _, g => g i
| _ + 1, 0, f, _ => f 0
| m + 1, i + 1, f, g => pick m i (fun j => f (j + 1)) g
theorem pick_lo : forall m i (f g : Nat -> Bool), i < m -> pick m i f g = f i := by
intro m
induction m with
| zero => intro i f g h; exact False.elim (Nat.not_lt_zero i h)
| succ m ih =>
intro i f g h
cases i with
| zero => rfl
| succ i => exact ih i (fun j => f (j + 1)) g (Nat.lt_of_succ_lt_succ h)
theorem pick_hi : forall m i (f g : Nat -> Bool), pick m (m + i) f g = g i := by
intro m
induction m with
| zero => intro i f g; show g (0 + i) = g i; rw [Nat.zero_add]
| succ m ih =>
intro i f g
show pick (m + 1) (m + 1 + i) f g = g i
rw [Nat.add_right_comm m 1 i]
exact ih i (fun j => f (j + 1)) g
theorem pick_congr : forall m n i (f g f' g' : Nat -> Bool),
(forall i, i < m -> f i = f' i) -> (forall i, i < n -> g i = g' i) -> i < m + n ->
pick m i f g = pick m i f' g' := by
intro m
induction m with
| zero =>
intro n i f g f' g' _ hg hi
exact hg i ((congrArg (fun t => i < t) (Nat.zero_add n)).mp hi)
| succ m ih =>
intro n i f g f' g' hf hg hi
cases i with
| zero => exact hf 0 (Nat.zero_lt_succ m)
| succ i =>
have hi' : i + 1 < m + n + 1 := (congrArg (fun t => i + 1 < t) (Nat.add_right_comm m 1 n)).mp hi
exact ih n i (fun j => f (j + 1)) g (fun j => f' (j + 1)) g'
(fun j hj => hf (j + 1) (Nat.succ_lt_succ hj)) hg (Nat.lt_of_succ_lt_succ hi')
theorem pick_ff : forall m i, pick m i (fun _ => false) (fun _ => false) = false := by
intro m
induction m with
| zero => intro i; rfl
| succ m ih =>
intro i
cases i with
| zero => rfl
| succ i => exact ih i
variable (M : TMk)
/-- Cells have K = m + 2 + k bits; symbols have k + 1 tracks. -/
def K : Nat := M.m + 2 + M.k
/-! ## Layout -/
def cellK (h : Bool) (q : List Bool) (s : Nat -> Bool) : Nat -> Bool
| 0 => s 0
| 1 => h
| i + 2 => pick M.m i (fun i => h && nthd q i) (fun i => s (i + 1))
def encK (tape : Tape) (pos : Nat) (q : List Bool) : Conf :=
fun j b => cellK M (Nat.beq j pos) q (tape j) b
/-! ## The local rule -/
/-- Track i of a cell: bit 0, or bit m + 2 + (i - 1). -/
def trk (g : Nat -> Bool) : Nat -> Bool
| 0 => g 0
| i + 1 => g (M.m + 2 + i)
def stG (g : Nat -> Bool) : List Bool := read (fun i => g (i + 2)) 0 M.m
def syG (g : Nat -> Bool) : List Bool := read (trk M g) 0 (M.k + 1)
def lb (L : List Bool) (i : Nat) : Bool := nthd L i
def cb (L : List Bool) (i : Nat) : Bool := nthd L (K M + i)
def rb (L : List Bool) (i : Nat) : Bool := nthd L (K M + (K M + i))
def Rl (L : List Bool) : Bool := lb L 1 && M.right (stG M (lb L)) (syG M (lb L))
def Rr (L : List Bool) : Bool := rb M L 1 && !(M.right (stG M (rb M L)) (syG M (rb M L)))
def wr (L : List Bool) (i : Nat) : Bool :=
sel (cb M L 1) (nthd (M.write (stG M (cb M L)) (syG M (cb M L))) i) (trk M (cb M L) i)
def rule (L : List Bool) : Nat -> Bool
| 0 => wr M L 0
| 1 => Rl M L || Rr M L
| i + 2 => pick M.m i
(fun i => (Rl M L && nthd (M.next (stG M (lb L)) (syG M (lb L))) i) ||
(Rr M L && nthd (M.next (stG M (rb M L)) (syG M (rb M L))) i))
(fun i => wr M L (i + 1))
/-! ## The Turing step -/
def symAt (tape : Tape) (pos : Nat) : List Bool := read (tape pos) 0 (M.k + 1)
def tapeN (tape : Tape) (pos : Nat) (q : List Bool) : Tape :=
fun j i => sel (Nat.beq j pos) (nthd (M.write q (symAt M tape pos)) i) (tape j i)
def posN (tape : Tape) (pos : Nat) (q : List Bool) : Nat :=
selN (M.right q (symAt M tape pos)) (pos + 1) (pos - 1)
def qN (tape : Tape) (pos : Nat) (q : List Bool) : List Bool := M.next q (symAt M tape pos)
/-! ## Reading the layout -/
theorem trkE (tape : Tape) (pos : Nat) (q : List Bool) (x : Nat) : forall i,
trk M (encK M tape pos q x) i = tape x i
| 0 => rfl
| i + 1 => by
show cellK M (Nat.beq x pos) q (tape x) (M.m + 2 + i) = tape x (i + 1)
rw [Nat.add_right_comm M.m 2 i]
exact pick_hi M.m i _ _
theorem syE (tape : Tape) (pos : Nat) (q : List Bool) (x : Nat) :
syG M (encK M tape pos q x) = symAt M tape x :=
read_ext_k _ _ (M.k + 1) (fun i _ => trkE M tape pos q x i)
theorem stE (tape : Tape) (pos : Nat) (q : List Bool) (hq : q.length = M.m) (x : Nat)
(h : Nat.beq x pos = true) : stG M (encK M tape pos q x) = q := by
have e : read (fun i => encK M tape pos q x (i + 2)) 0 M.m = read (nthd q) 0 M.m :=
read_ext_k _ _ M.m (fun i hi => by
show pick M.m i (fun i => Nat.beq x pos && nthd q i) (fun i => tape x (i + 1)) = nthd q i
rw [pick_lo M.m i _ _ hi, h]; rfl)
show read (fun i => encK M tape pos q x (i + 2)) 0 M.m = q
rw [e, ← hq]
exact read_nthd q
/-- The head substitution: where the head is, the cell's state and symbol are q and the
head symbol. -/
theorem stq (tape : Tape) (pos : Nat) (q : List Bool) (hq : q.length = M.m)
(f : List Bool -> List Bool -> Bool) (x : Nat) :
(Nat.beq x pos && f (stG M (encK M tape pos q x)) (syG M (encK M tape pos q x))) =
(Nat.beq x pos && f q (symAt M tape pos)) := by
cases h : Nat.beq x pos with
| false => rfl
| true =>
have e : x = pos := Found.Pnp2.beq_eq x pos h
rw [stE M tape pos q hq x h, syE, e]
theorem selq (tape : Tape) (pos : Nat) (q : List Bool) (hq : q.length = M.m)
(f : List Bool -> List Bool -> Bool) (x : Nat) (y : Bool) :
sel (Nat.beq x pos) (f (stG M (encK M tape pos q x)) (syG M (encK M tape pos q x))) y =
sel (Nat.beq x pos) (f q (symAt M tape pos)) y := by
cases h : Nat.beq x pos with
| false => rfl
| true =>
have e : x = pos := Found.Pnp2.beq_eq x pos h
rw [stE M tape pos q hq x h, syE, e]
/-! ## Accessors on the neighbourhood -/
def nbE (tape : Tape) (pos : Nat) (q : List Bool) (W j : Nat) : List Bool :=
nbC (encK M tape pos q) (K M) W j
theorem ltS (i : Nat) (h : i < M.m) : i + 2 < K M :=
Nat.lt_of_lt_of_le (Nat.add_lt_add_right h 2) (Nat.le_add_right _ _)
/-- The bit index of track i. -/
def trkIdx : Nat -> Nat
| 0 => 0
| i + 1 => M.m + 2 + i
theorem ltT : forall i, i < M.k + 1 -> trkIdx M i < K M
| 0, _ => Nat.lt_of_lt_of_le (Nat.zero_lt_succ _) (Nat.le_add_right _ _)
| _ + 1, h => Nat.add_lt_add_left (Nat.lt_of_succ_lt_succ h) _
theorem lt1 : 1 < K M :=
Nat.lt_of_lt_of_le (Nat.succ_lt_succ (Nat.zero_lt_succ M.m)) (Nat.le_add_right _ _)
theorem trk_acc (g h : Nat -> Bool) (hg : forall b, b < K M -> g b = h b) : forall i, i < M.k + 1 ->
trk M g i = trk M h i
| i, hi => by
have e := hg (trkIdx M i) (ltT M i hi)
cases i with
| zero => exact e
| succ i => exact e
theorem stL (tape : Tape) (pos : Nat) (q : List Bool) (W j : Nat) :
stG M (lb (nbE M tape pos q W j)) = stG M (encK M tape pos q (leftIdx W j)) :=
read_ext_k _ _ M.m (fun i hi => accL (encK M tape pos q) (K M) W j (i + 2) (ltS M i hi))
theorem syL (tape : Tape) (pos : Nat) (q : List Bool) (W j : Nat) :
syG M (lb (nbE M tape pos q W j)) = syG M (encK M tape pos q (leftIdx W j)) :=
read_ext_k _ _ (M.k + 1) (trk_acc M _ _ (fun b hb => accL (encK M tape pos q) (K M) W j b hb))
theorem stC (tape : Tape) (pos : Nat) (q : List Bool) (W j : Nat) :
stG M (cb M (nbE M tape pos q W j)) = stG M (encK M tape pos q j) :=
read_ext_k _ _ M.m (fun i hi => accC (encK M tape pos q) (K M) W j (i + 2) (ltS M i hi))
theorem syC (tape : Tape) (pos : Nat) (q : List Bool) (W j : Nat) :
syG M (cb M (nbE M tape pos q W j)) = syG M (encK M tape pos q j) :=
read_ext_k _ _ (M.k + 1) (trk_acc M _ _ (fun b hb => accC (encK M tape pos q) (K M) W j b hb))
theorem stR (tape : Tape) (pos : Nat) (q : List Bool) (W j : Nat) :
stG M (rb M (nbE M tape pos q W j)) = stG M (encK M tape pos q (j + 1)) :=
read_ext_k _ _ M.m (fun i hi => accR (encK M tape pos q) (K M) W j (i + 2) (ltS M i hi))
theorem syR (tape : Tape) (pos : Nat) (q : List Bool) (W j : Nat) :
syG M (rb M (nbE M tape pos q W j)) = syG M (encK M tape pos q (j + 1)) :=
read_ext_k _ _ (M.k + 1) (trk_acc M _ _ (fun b hb => accR (encK M tape pos q) (K M) W j b hb))
/-- The two movement signals of the rule, on a layout. -/
theorem rl_eq (tape : Tape) (p : Nat) (q : List Bool) (hq : q.length = M.m) (W j : Nat)
(f : List Bool -> List Bool -> Bool) :
(lb (nbE M tape (p + 1) q W j) 1 && f (stG M (lb (nbE M tape (p + 1) q W j))) (syG M (lb (nbE M tape (p + 1) q W j)))) =
(Nat.beq (leftIdx W j) (p + 1) && f q (symAt M tape (p + 1))) := by
have l0 : lb (nbE M tape (p + 1) q W j) 1 = Nat.beq (leftIdx W j) (p + 1) :=
accL (encK M tape (p + 1) q) (K M) W j 1 (lt1 M)
rw [l0, stL, syL]
exact stq M tape (p + 1) q hq f (leftIdx W j)
theorem rr_eq (tape : Tape) (p : Nat) (q : List Bool) (hq : q.length = M.m) (W j : Nat)
(f : List Bool -> List Bool -> Bool) :
(rb M (nbE M tape (p + 1) q W j) 1 && f (stG M (rb M (nbE M tape (p + 1) q W j))) (syG M (rb M (nbE M tape (p + 1) q W j)))) =
(Nat.beq (j + 1) (p + 1) && f q (symAt M tape (p + 1))) := by
have r0 : rb M (nbE M tape (p + 1) q W j) 1 = Nat.beq (j + 1) (p + 1) :=
accR (encK M tape (p + 1) q) (K M) W j 1 (lt1 M)
rw [r0, stR, syR]
exact stq M tape (p + 1) q hq f (j + 1)
/-- The written track i at cell j. -/
theorem wr_eq (tape : Tape) (p : Nat) (q : List Bool) (hq : q.length = M.m) (W j : Nat) (i : Nat)
(hi : i < M.k + 1) :
wr M (nbE M tape (p + 1) q W j) i = sel (Nat.beq j (p + 1)) (nthd (M.write q (symAt M tape (p + 1))) i) (tape j i) := by
have c1 : cb M (nbE M tape (p + 1) q W j) 1 = Nat.beq j (p + 1) :=
accC (encK M tape (p + 1) q) (K M) W j 1 (lt1 M)
have cy : trk M (cb M (nbE M tape (p + 1) q W j)) i = tape j i :=
(trk_acc M _ _ (fun b hb => accC (encK M tape (p + 1) q) (K M) W j b hb) i hi).trans (trkE M tape (p + 1) q j i)
show sel (cb M (nbE M tape (p + 1) q W j) 1)
(nthd (M.write (stG M (cb M (nbE M tape (p + 1) q W j))) (syG M (cb M (nbE M tape (p + 1) q W j)))) i)
(trk M (cb M (nbE M tape (p + 1) q W j)) i) = _
rw [c1, cy, stC, syC]
exact selq M tape (p + 1) q hq (fun a s => nthd (M.write a s) i) j (tape j i)
/-! ## One step -/
theorem step_inside (tape : Tape) (p : Nat) (q : List Bool) (hq : q.length = M.m)
(W : Nat) (hW : p + 2 < W) (j : Nat) (hj : j < W) (b : Nat) (hb : b < K M) :
stepW (rule M) (K M) W (encK M tape (p + 1) q) j b =
encK M (tapeN M tape (p + 1) q) (posN M tape (p + 1) q) (qN M tape (p + 1) q) j b := by
have hW1 : p + 1 < W := Nat.lt_of_succ_lt hW
show gate (Nat.beq j W) (rule M (nbE M tape (p + 1) q W j) b) = _
rw [Found.Pnp2.beq_false_of_ne j W (Nat.ne_of_lt hj)]
match b, hb with
| 0, _ => exact wr_eq M tape p q hq W j 0 (Nat.zero_lt_succ _)
| 1, _ =>
show ((lb (nbE M tape (p + 1) q W j) 1 && M.right (stG M (lb (nbE M tape (p + 1) q W j))) (syG M (lb (nbE M tape (p + 1) q W j)))) ||
(rb M (nbE M tape (p + 1) q W j) 1 && (fun a s => !(M.right a s)) (stG M (rb M (nbE M tape (p + 1) q W j))) (syG M (rb M (nbE M tape (p + 1) q W j))))) =
Nat.beq j (selN (M.right q (symAt M tape (p + 1))) (p + 1 + 1) (p + 1 - 1))
rw [rl_eq M tape p q hq W j M.right, rr_eq M tape p q hq W j (fun a s => !(M.right a s))]
exact head_eq W p j hW1 _
| i + 2, hb' =>
have hi : i < M.m + M.k :=
Nat.lt_of_add_lt_add_right ((congrArg (fun t => i + 2 < t) (Nat.add_right_comm M.m 2 M.k)).mp hb')
show pick M.m i _ _ = pick M.m i _ _
apply pick_congr M.m M.k i _ _ _ _ _ _ hi
· intro i _
show (((lb (nbE M tape (p + 1) q W j) 1 && M.right (stG M (lb (nbE M tape (p + 1) q W j))) (syG M (lb (nbE M tape (p + 1) q W j)))) &&
nthd (M.next (stG M (lb (nbE M tape (p + 1) q W j))) (syG M (lb (nbE M tape (p + 1) q W j)))) i) ||
((rb M (nbE M tape (p + 1) q W j) 1 && !(M.right (stG M (rb M (nbE M tape (p + 1) q W j))) (syG M (rb M (nbE M tape (p + 1) q W j))))) &&
nthd (M.next (stG M (rb M (nbE M tape (p + 1) q W j))) (syG M (rb M (nbE M tape (p + 1) q W j)))) i)) =
(Nat.beq j (selN (M.right q (symAt M tape (p + 1))) (p + 1 + 1) (p + 1 - 1)) && nthd (M.next q (symAt M tape (p + 1))) i)
rw [and_assoc3, and_assoc3]
rw [rl_eq M tape p q hq W j (fun a s => M.right a s && nthd (M.next a s) i),
rr_eq M tape p q hq W j (fun a s => !(M.right a s) && nthd (M.next a s) i)]
exact head_eq2 W p j hW1 _ _
· intro i hik
exact wr_eq M tape p q hq W j (i + 1) (Nat.succ_lt_succ hik)
/-- At the boundary cell W the local machine writes false, and so does the layout of the
next configuration when the head stays inside and the tape is false at W. -/
theorem step_boundary (tape : Tape) (p : Nat) (q : List Bool) (W : Nat) (hW : p + 2 < W)
(htW : forall i, i < M.k + 1 -> tape W i = false) (b : Nat) (hb : b < K M) :
stepW (rule M) (K M) W (encK M tape (p + 1) q) W b =
encK M (tapeN M tape (p + 1) q) (posN M tape (p + 1) q) (qN M tape (p + 1) q) W b := by
have hW1 : p + 1 < W := Nat.lt_of_succ_lt hW
have hpos : Nat.beq W (posN M tape (p + 1) q) = false := by
show Nat.beq W (selN (M.right q (symAt M tape (p + 1))) (p + 1 + 1) (p + 1 - 1)) = false
cases M.right q (symAt M tape (p + 1)) with
| true => exact Found.Pnp2.beq_false_of_ne W (p + 2) (fun e => Nat.ne_of_lt hW e.symm)
| false => exact Found.Pnp2.beq_false_of_ne W p (fun e => Nat.ne_of_lt (Nat.lt_of_succ_lt hW1) e.symm)
have hT : forall i, i < M.k + 1 -> tapeN M tape (p + 1) q W i = false := fun i hi => by
show sel (Nat.beq W (p + 1)) _ (tape W i) = false
rw [beqW W p hW1]; exact htW i hi
show gate (Nat.beq W W) _ = cellK M (Nat.beq W (posN M tape (p + 1) q)) (qN M tape (p + 1) q)
(tapeN M tape (p + 1) q W) b
rw [beq_refl W, hpos]
match b, hb with
| 0, _ => exact (hT 0 (Nat.zero_lt_succ _)).symm
| 1, _ => rfl
| i + 2, hb' =>
have hi : i < M.m + M.k :=
Nat.lt_of_add_lt_add_right ((congrArg (fun t => i + 2 < t) (Nat.add_right_comm M.m 2 M.k)).mp hb')
show false = pick M.m i (fun i => false && nthd (qN M tape (p + 1) q) i) (fun i => tapeN M tape (p + 1) q W (i + 1))
have e := pick_congr M.m M.k i (fun i => false && nthd (qN M tape (p + 1) q) i)
(fun i => tapeN M tape (p + 1) q W (i + 1)) (fun _ => false) (fun _ => false) (fun _ _ => rfl)
(fun i hik => hT (i + 1) (Nat.succ_lt_succ hik)) hi
rw [e]
exact (pick_ff M.m i).symm
end Found.Pnp43
#print axioms Found.Pnp43.pick_lo
#print axioms Found.Pnp43.pick_hi
#print axioms Found.Pnp43.pick_congr
#print axioms Found.Pnp43.pick_ff
#print axioms Found.Pnp43.trkE
#print axioms Found.Pnp43.syE
#print axioms Found.Pnp43.stE
#print axioms Found.Pnp43.stq
#print axioms Found.Pnp43.selq
#print axioms Found.Pnp43.ltT
#print axioms Found.Pnp43.trk_acc
#print axioms Found.Pnp43.stL
#print axioms Found.Pnp43.syL
#print axioms Found.Pnp43.stC
#print axioms Found.Pnp43.syC
#print axioms Found.Pnp43.stR
#print axioms Found.Pnp43.syR
#print axioms Found.Pnp43.rl_eq
#print axioms Found.Pnp43.rr_eq
#print axioms Found.Pnp43.wr_eq
#print axioms Found.Pnp43.step_inside
#print axioms Found.Pnp43.step_boundary
#print axioms: 22 theorems, no axioms.
The tableau CNF with layout and input pins is satisfiable iff some certificate makes the multi-track machine accept.
found/Pnp44.lean
/-
T330 piece 44. found/Pnp44 -- Cook-Levin for multi-track Turing machines (CL3c, 2).
Runs of the multi-track machine of Pnp43 (configurations: tape, head position, state)
and the Cook-Levin CNF of their tableau. Proved:
* simulate: if the boundary cell W is false, the state has m bits, next keeps m bits,
and the head stays at some cell p + 1 with p + 2 < W before time T, then the width-W
local machine run from the layout is the layout of the Turing configuration at every
time t <= T, cell j <= W and bit b < K;
* cook_levin_tmk: with the pins of Pnp14 (head at p0 + 1 in state q0 on the layout
bits 1, 2.., input bits on track 0), the CNF full (rule M) K W T pins is satisfiable
iff some tape agreeing with the input on track 0 and false at W makes the machine
hold true at track 0 of cell 0 after T steps -- for a machine whose head stays
inside on every such tape. The pins are those the reduction program of
Pnp37-Pnp42 writes, so that program serves multi-track machines unchanged.
It relativizes (a tool; PNP_MAP.md). Written with ASCII forms only.
CIC dependence: none.
-/
import found.Pnp43
import found.Pnp14
namespace Found.Pnp44
open Found.Pnp7 (nthd)
open Found.Pnp9 (lt_succ_cases leftIdx)
open Found.Pnp10 (Conf nbC stepW runW gate beq_refl nbC_ext)
open Found.Pnp11 (PinsOK full cook_levin_local)
open Found.Pnp12 (sel beqW)
open Found.Pnp14 (pins_app allPins cellPins_iff allPins_iff inPins inPins_iff InputOK)
open Found.Pnp43
structure TCk where
tape : Tape
pos : Nat
q : List Bool
variable (M : TMk)
def tmStep (c : TCk) : TCk := TCk.mk (tapeN M c.tape c.pos c.q) (posN M c.tape c.pos c.q) (qN M c.tape c.pos c.q)
def tmRun : Nat -> TCk -> TCk
| 0, c => c
| t + 1, c => tmStep M (tmRun t c)
def lay (c : TCk) : Conf := encK M c.tape c.pos c.q
/-- The head stays inside before time T. -/
def Inside (W T : Nat) (c : TCk) : Prop :=
forall t, t < T -> Exists fun p => (tmRun M t c).pos = p + 1 /\ p + 2 < W
theorem preserve (W T : Nat) (c : TCk) (hin : Inside M W T c) (htW : forall i, i < M.k + 1 -> c.tape W i = false)
(hq : c.q.length = M.m) (hnext : forall a s, (M.next a s).length = M.m) :
forall t, t < T + 1 -> (forall i, i < M.k + 1 -> (tmRun M t c).tape W i = false) /\ (tmRun M t c).q.length = M.m := by
intro t
induction t with
| zero => intro _; exact And.intro htW hq
| succ t ih =>
intro ht
have ht' : t < T := Nat.lt_of_succ_lt_succ ht
have ih' := ih (Nat.lt_succ_of_lt ht')
exact Exists.elim (hin t ht') fun p hp => by
apply And.intro
· intro i hi
show sel (Nat.beq W (tmRun M t c).pos) _ ((tmRun M t c).tape W i) = false
rw [hp.1, beqW W p (Nat.lt_of_succ_lt hp.2)]
exact ih'.1 i hi
· exact hnext _ _
theorem stepW_ext (W : Nat) (x y : Conf) (h : forall c, c < W + 1 -> forall b, b < K M -> x c b = y c b) :
forall j, j < W + 1 -> forall b, stepW (rule M) (K M) W x j b = stepW (rule M) (K M) W y j b := by
intro j hj b
exact Or.elim (lt_succ_cases j W hj)
(fun hjW => by
show gate (Nat.beq j W) (rule M (nbC x (K M) W j) b) = gate (Nat.beq j W) (rule M (nbC y (K M) W j) b)
rw [nbC_ext x y (K M) W j hjW h])
(fun e => by
rw [e]
show gate (Nat.beq W W) _ = gate (Nat.beq W W) _
rw [beq_refl W]
rfl)
theorem runW_ext (W : Nat) (x y : Conf)
(h : forall c, c < W + 1 -> forall b, b < K M -> x c b = y c b) :
forall t, forall c, c < W + 1 -> forall b, b < K M ->
runW (rule M) (K M) W t x c b = runW (rule M) (K M) W t y c b := by
intro t
induction t with
| zero => exact h
| succ t ih => intro c hc b _; exact stepW_ext M W _ _ ih c hc b
/-- The local machine follows the Turing machine. -/
theorem simulate (W T : Nat) (c : TCk) (hin : Inside M W T c) (htW : forall i, i < M.k + 1 -> c.tape W i = false)
(hq : c.q.length = M.m) (hnext : forall a s, (M.next a s).length = M.m) :
forall t, t < T + 1 -> forall j, j < W + 1 -> forall b, b < K M ->
runW (rule M) (K M) W t (lay M c) j b = lay M (tmRun M t c) j b := by
intro t
induction t with
| zero => intro _ j _ b _; rfl
| succ t ih =>
intro ht j hj b hb
have ht' : t < T := Nat.lt_of_succ_lt_succ ht
have pr := preserve M W T c hin htW hq hnext t (Nat.lt_succ_of_lt ht')
show stepW (rule M) (K M) W (runW (rule M) (K M) W t (lay M c)) j b = lay M (tmStep M (tmRun M t c)) j b
rw [stepW_ext M W _ (lay M (tmRun M t c)) (fun c' hc b' hb' => ih (Nat.lt_succ_of_lt ht') c' hc b' hb') j hj b]
exact Exists.elim (hin t ht') fun p hp => by
have e : lay M (tmRun M t c) = encK M (tmRun M t c).tape (p + 1) (tmRun M t c).q := by
show encK M (tmRun M t c).tape (tmRun M t c).pos (tmRun M t c).q = _
rw [hp.1]
show _ = encK M (tapeN M (tmRun M t c).tape (tmRun M t c).pos (tmRun M t c).q)
(posN M (tmRun M t c).tape (tmRun M t c).pos (tmRun M t c).q)
(qN M (tmRun M t c).tape (tmRun M t c).pos (tmRun M t c).q) j b
rw [e, hp.1]
exact Or.elim (lt_succ_cases j W hj)
(fun hjW => step_inside M _ p _ pr.2 W hp.2 j hjW b hb)
(fun ej => by rw [ej]; exact step_boundary M _ p _ W hp.2 pr.1 b hb)
/-! ## Cook-Levin -/
/-- A function agrees with a pick of itself and g when g lists its values from m on. -/
theorem pick_self (n : Nat) : forall m b (f g : Nat -> Bool), b < m + n ->
(forall r, r < n -> f (m + r) = g r) -> f b = pick m b f g := by
intro m
induction m with
| zero =>
intro b f g hb hg
show f b = g b
exact (congrArg f (Nat.zero_add b).symm).trans (hg b ((congrArg (fun t => b < t) (Nat.zero_add n)).mp hb))
| succ m ih =>
intro b f g hb hg
cases b with
| zero => rfl
| succ b =>
have hb' : b + 1 < m + n + 1 := (congrArg (fun t => b + 1 < t) (Nat.add_right_comm m 1 n)).mp hb
exact ih b (fun j => f (j + 1)) g (Nat.lt_of_succ_lt_succ hb')
(fun r hr => (congrArg f (Nat.add_right_comm m r 1)).trans (hg r hr))
theorem hi2 (i : Nat) (hb : i + 2 < K M) : i < M.m + M.k :=
Nat.lt_of_add_lt_add_right ((congrArg (fun t => i + 2 < t) (Nat.add_right_comm M.m 2 M.k)).mp hb)
/-- A conf that agrees with a layout's shape is that layout of its own tracks. -/
theorem agree_lay (c0 : Conf) (j : Nat) (h : Bool) (q0 : List Bool)
(h1 : c0 j 1 = h) (hs : forall i, i < M.m -> c0 j (i + 2) = (h && nthd q0 i)) :
forall b, b < K M -> c0 j b = cellK M h q0 (trk M (c0 j)) b
| 0, _ => rfl
| 1, _ => h1
| i + 2, hb => by
show c0 j (i + 2) = pick M.m i (fun i => h && nthd q0 i) (fun i => c0 j (M.m + 2 + i))
rw [pick_self M.k M.m i (fun i => c0 j (i + 2)) (fun i => c0 j (M.m + 2 + i)) (hi2 M i hb)
(fun r _ => congrArg (c0 j) (Nat.add_right_comm M.m r 2))]
exact pick_congr M.m M.k i _ _ _ _ hs (fun _ _ => rfl) (hi2 M i hb)
def pinsTMk (W p0 : Nat) (q0 : List Bool) (inp : List (Nat × Bool)) : List (Nat × (Nat × Bool)) :=
allPins (p0 + 1) q0 M.m W ++ inPins inp
/-- Cook-Levin for multi-track Turing machines (pins as for Pnp14: the reduction program
of Pnp37-Pnp42 writes this CNF). -/
theorem cook_levin_tmk (W T p0 : Nat) (q0 : List Bool) (hq0 : q0.length = M.m)
(hnext : forall a s, (M.next a s).length = M.m) (hp : p0 + 2 < W) (inp : List (Nat × Bool))
(hin : forall tape : Tape, InputOK (fun j => tape j 0) inp -> (forall i, i < M.k + 1 -> tape W i = false) ->
Inside M W T (TCk.mk tape (p0 + 1) q0)) :
((Exists fun s => Found.Pnp8.cnfv s (full (rule M) (K M) W T (pinsTMk M W p0 q0 inp)) = true) ->
Exists fun tape : Tape => InputOK (fun j => tape j 0) inp /\ (forall i, i < M.k + 1 -> tape W i = false) /\
(tmRun M T (TCk.mk tape (p0 + 1) q0)).tape 0 0 = true) /\
((Exists fun tape : Tape => InputOK (fun j => tape j 0) inp /\ (forall i, i < M.k + 1 -> tape W i = false) /\
(tmRun M T (TCk.mk tape (p0 + 1) q0)).tape 0 0 = true) ->
Exists fun s => Found.Pnp8.cnfv s (full (rule M) (K M) W T (pinsTMk M W p0 q0 inp)) = true) := by
have hK : 0 < K M := Nat.lt_of_succ_lt (lt1 M)
have hW1 : p0 + 1 < W := Nat.lt_of_succ_lt hp
have hbW : Nat.beq W (p0 + 1) = false := beqW W p0 hW1
apply And.intro
· intro h
exact Exists.elim ((cook_levin_local (rule M) (K M) W T hK _).1 h) fun c0 hc => by
have hpins := (pins_app c0 _ _).1 hc.2.1
let tape : Tape := fun j => trk M (c0 j)
let c : TCk := TCk.mk tape (p0 + 1) q0
have htW : forall i, i < M.k + 1 -> tape W i = false := fun i hi =>
hc.1 (trkIdx M i) (ltT M i hi) ▸ (by cases i <;> rfl)
have agree : forall j, j < W + 1 -> forall b, b < K M -> c0 j b = lay M c j b := by
intro j hj b hb
exact Or.elim (lt_succ_cases j W hj)
(fun hjW => by
have hcell := (cellPins_iff c0 (p0 + 1) q0 j M.m).1 ((allPins_iff c0 (p0 + 1) q0 M.m W).1 hpins.1 j hjW)
exact agree_lay M c0 j _ q0 hcell.1 hcell.2 b hb)
(fun e => by
rw [e]
show c0 W b = cellK M (Nat.beq W (p0 + 1)) q0 (trk M (c0 W)) b
exact agree_lay M c0 W _ q0 ((hc.1 1 (lt1 M)).trans hbW.symm)
(fun i hi => by rw [hbW]; exact hc.1 (i + 2) (ltS M i hi)) b hb)
have hrun := runW_ext M W c0 (lay M c) agree T 0 (Nat.zero_lt_succ W) 0 hK
have hsim := simulate M W T c (hin tape ((inPins_iff c0 inp).1 hpins.2) htW) htW hq0 hnext T
(Nat.lt_succ_self T) 0 (Nat.zero_lt_succ W) 0 hK
exact Exists.intro tape (And.intro ((inPins_iff c0 inp).1 hpins.2) (And.intro htW
((hsim.symm.trans hrun.symm).trans hc.2.2)))
· intro h
exact Exists.elim h fun tape ht => by
let c : TCk := TCk.mk tape (p0 + 1) q0
apply (cook_levin_local (rule M) (K M) W T hK _).2
apply Exists.intro (lay M c)
apply And.intro
· intro b hb
show cellK M (Nat.beq W (p0 + 1)) q0 (tape W) b = false
rw [hbW]
match b, hb with
| 0, _ => exact ht.2.1 0 (Nat.zero_lt_succ _)
| 1, _ => rfl
| i + 2, hb' =>
show pick M.m i (fun i => false && nthd q0 i) (fun i => tape W (i + 1)) = false
exact (pick_congr M.m M.k i (fun i => false && nthd q0 i) (fun i => tape W (i + 1))
(fun _ => false) (fun _ => false) (fun _ _ => rfl)
(fun i hik => ht.2.1 (i + 1) (Nat.succ_lt_succ hik)) (hi2 M i hb')).trans (pick_ff M.m i)
apply And.intro
· exact (pins_app (lay M c) _ _).2 (And.intro
((allPins_iff (lay M c) (p0 + 1) q0 M.m W).2 (fun j _ =>
(cellPins_iff (lay M c) (p0 + 1) q0 j M.m).2 (And.intro rfl
(fun i hi => pick_lo M.m i _ _ hi))))
((inPins_iff (lay M c) inp).2 ht.1))
· exact (simulate M W T c (hin tape ht.1 ht.2.1) ht.2.1 hq0 hnext T (Nat.lt_succ_self T) 0
(Nat.zero_lt_succ W) 0 hK).trans ht.2.2
end Found.Pnp44
#print axioms Found.Pnp44.preserve
#print axioms Found.Pnp44.stepW_ext
#print axioms Found.Pnp44.runW_ext
#print axioms Found.Pnp44.simulate
#print axioms Found.Pnp44.pick_self
#print axioms Found.Pnp44.hi2
#print axioms Found.Pnp44.agree_lay
#print axioms Found.Pnp44.cook_levin_tmk
#print axioms: 8 theorems, no axioms.
A state that keeps its symbols and moves one way passes \(n\) cells in \(n\) steps.
found/Pnp45.lean
/-
T330 piece 45. found/Pnp45 -- sweeps of the multi-track machine (CL3c, 3).
A register instruction is simulated by the multi-track machine of Pnp43/Pnp44 in a
number of steps linear in the register lengths: the head sweeps over a register track
to its end and back. This piece proves the sweep itself, for any machine M:
* tapes agreeing on the tracks 0..k (TEq) have the same symbols and step to tapes
agreeing on the tracks 0..k, with the same head and state;
* sweepR: if for d < n the machine in state q reading the symbol of cell pos + d keeps
the state, writes the symbol back and moves right, then after n steps the head is
at pos + n, the state is q and the tape agrees with the start on every track;
* sweepL: the same moving left over pos + 1 .. pos + n, from pos + n down to pos.
So a sweep over n cells costs exactly n steps. It relativizes (a tool; PNP_MAP.md).
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp44
namespace Found.Pnp45
open Found.Pnp1 (read)
open Found.Pnp7 (nthd)
open Found.Pnp10 (read_ext_k)
open Found.Pnp12 (sel selN)
open Found.Pnp43
open Found.Pnp44
variable (M : TMk)
/-- Tapes agreeing on the tracks 0..k. -/
def TEq (t u : Tape) : Prop := forall j i, i < M.k + 1 -> t j i = u j i
theorem nthd_read (f : Nat -> Bool) : forall n k i, i < n -> nthd (read f k n) i = f (k + i) := by
intro n
induction n with
| zero => intro k i h; exact False.elim (Nat.not_lt_zero i h)
| succ n ih =>
intro k i h
cases i with
| zero => rfl
| succ i =>
show nthd (read f (k + 1) n) i = f (k + (i + 1))
rw [ih (k + 1) i (Nat.lt_of_succ_lt_succ h), Nat.add_right_comm k 1 i, Nat.add_assoc k i 1]
theorem symAt_ext (t u : Tape) (h : TEq M t u) (p : Nat) : symAt M t p = symAt M u p :=
read_ext_k (t p) (u p) (M.k + 1) (fun i hi => h p i hi)
/-- Writing back the symbol read leaves every track as it was. -/
theorem tapeN_keep (t : Tape) (p : Nat) (q : List Bool) (hw : M.write q (symAt M t p) = symAt M t p) :
TEq M (tapeN M t p q) t := by
intro j i hi
show sel (Nat.beq j p) (nthd (M.write q (symAt M t p)) i) (t j i) = t j i
cases h : Nat.beq j p with
| false => rfl
| true =>
have e : j = p := Found.Pnp2.beq_eq j p h
rw [e, hw]
show nthd (read (t p) 0 (M.k + 1)) i = t p i
rw [nthd_read (t p) (M.k + 1) 0 i hi, Nat.zero_add]
theorem teq_trans (a b c : Tape) (h1 : TEq M a b) (h2 : TEq M b c) : TEq M a c :=
fun j i hi => (h1 j i hi).trans (h2 j i hi)
/-- One step in a state that keeps itself and writes back. -/
structure Keep (q : List Bool) (s : List Bool) (r : Bool) : Prop where
nx : M.next q s = q
wr : M.write q s = s
rt : M.right q s = r
theorem step_keep (t t0 : Tape) (p : Nat) (q : List Bool) (r : Bool)
(ht : TEq M t t0) (hk : Keep M q (symAt M t0 p) r) :
TEq M (tmStep M (TCk.mk t p q)).tape t0 /\
(tmStep M (TCk.mk t p q)).pos = selN r (p + 1) (p - 1) /\
(tmStep M (TCk.mk t p q)).q = q := by
have es : symAt M t p = symAt M t0 p := symAt_ext M t t0 ht p
refine And.intro ?_ (And.intro ?_ ?_)
· show TEq M (tapeN M t p q) t0
have hw : M.write q (symAt M t p) = symAt M t p := by rw [es]; exact hk.wr
exact teq_trans M _ _ _ (tapeN_keep M t p q hw) ht
· show selN (M.right q (symAt M t p)) (p + 1) (p - 1) = selN r (p + 1) (p - 1)
rw [es, hk.rt]
· show M.next q (symAt M t p) = q
rw [es]; exact hk.nx
/-- Sweep right over n cells: n steps. -/
theorem sweepR (t0 : Tape) (pos : Nat) (q : List Bool) :
forall n, (forall d, d < n -> Keep M q (symAt M t0 (pos + d)) true) ->
TEq M (tmRun M n (TCk.mk t0 pos q)).tape t0 /\
(tmRun M n (TCk.mk t0 pos q)).pos = pos + n /\
(tmRun M n (TCk.mk t0 pos q)).q = q := by
intro n
induction n with
| zero =>
intro _
exact And.intro (fun _ _ _ => rfl) (And.intro rfl rfl)
| succ n ih =>
intro hk
have h := ih (fun d hd => hk d (Nat.lt_succ_of_lt hd))
have e : tmRun M n (TCk.mk t0 pos q) =
TCk.mk (tmRun M n (TCk.mk t0 pos q)).tape (pos + n) q := by
cases c : tmRun M n (TCk.mk t0 pos q) with
| mk tp ps qq =>
rw [c] at h
have e1 : ps = pos + n := h.2.1
have e2 : qq = q := h.2.2
rw [e1, e2]
have s := step_keep M (tmRun M n (TCk.mk t0 pos q)).tape t0 (pos + n) q true h.1
(hk n (Nat.lt_succ_self n))
show TEq M (tmStep M (tmRun M n (TCk.mk t0 pos q))).tape t0 /\
(tmStep M (tmRun M n (TCk.mk t0 pos q))).pos = pos + n + 1 /\
(tmStep M (tmRun M n (TCk.mk t0 pos q))).q = q
rw [e]
exact s
/-- Sweep left over the cells pos + 1 .. pos + n, from pos + n down to pos: n steps. -/
theorem sweepL : forall n (t0 : Tape) (pos : Nat) (q : List Bool),
(forall d, d < n -> Keep M q (symAt M t0 (pos + 1 + d)) false) ->
TEq M (tmRun M n (TCk.mk t0 (pos + n) q)).tape t0 /\
(tmRun M n (TCk.mk t0 (pos + n) q)).pos = pos /\
(tmRun M n (TCk.mk t0 (pos + n) q)).q = q := by
intro n
induction n with
| zero =>
intro t0 pos q _
exact And.intro (fun _ _ _ => rfl) (And.intro rfl rfl)
| succ n ih =>
intro t0 pos q hk
have h := ih t0 (pos + 1) q (fun d hd => by
have e : pos + 1 + 1 + d = pos + 1 + (d + 1) := by rw [Nat.add_assoc (pos + 1) 1 d, Nat.add_comm 1 d]
rw [e]; exact hk (d + 1) (Nat.succ_lt_succ hd))
have e0 : pos + (n + 1) = pos + 1 + n := by
rw [Nat.add_assoc, Nat.add_comm n 1]
rw [e0]
have e : tmRun M n (TCk.mk t0 (pos + 1 + n) q) =
TCk.mk (tmRun M n (TCk.mk t0 (pos + 1 + n) q)).tape (pos + 1) q := by
cases c : tmRun M n (TCk.mk t0 (pos + 1 + n) q) with
| mk tp ps qq =>
rw [c] at h
have e1 : ps = pos + 1 := h.2.1
have e2 : qq = q := h.2.2
rw [e1, e2]
have hlast : Keep M q (symAt M t0 (pos + 1)) false := hk 0 (Nat.zero_lt_succ n)
have s := step_keep M (tmRun M n (TCk.mk t0 (pos + 1 + n) q)).tape t0 (pos + 1) q false h.1 hlast
show TEq M (tmStep M (tmRun M n (TCk.mk t0 (pos + 1 + n) q))).tape t0 /\
(tmStep M (tmRun M n (TCk.mk t0 (pos + 1 + n) q))).pos = pos /\
(tmStep M (tmRun M n (TCk.mk t0 (pos + 1 + n) q))).q = q
rw [e]
exact s
end Found.Pnp45
#print axioms Found.Pnp45.nthd_read
#print axioms Found.Pnp45.symAt_ext
#print axioms Found.Pnp45.tapeN_keep
#print axioms Found.Pnp45.teq_trans
#print axioms Found.Pnp45.step_keep
#print axioms Found.Pnp45.sweepR
#print axioms Found.Pnp45.sweepL
#print axioms: 7 theorems, no axioms.
A sweep followed by one step at its end; runs compose and respect tape agreement.
found/Pnp46.lean
/-
T330 piece 46. found/Pnp46 -- a sweep followed by one writing step (CL3c, 4).
A push or pop on a register track is a sweep to the end of the register (Pnp45) and one
step that writes the end cell. This piece proves, for any machine M:
* tapeN_ext / step_ext / run_ext: runs from configurations agreeing on the tracks
0..k, with the same head and state, agree on the tracks 0..k, head and state;
* run_add: running a steps and then b steps is running a + b steps;
* sweep_step: after a sweep right over n cells (n steps), one more step does at
cell pos + n exactly what the machine does on the start tape there: the tape agrees
with tapeN of the start tape, the head and the state are posN and qN of it.
So a push or pop of one cell at the end of a register of length n costs n + 1 steps.
It relativizes (a tool; PNP_MAP.md). Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp45
namespace Found.Pnp46
open Found.Pnp12 (sel)
open Found.Pnp43
open Found.Pnp44
open Found.Pnp45
variable (M : TMk)
theorem tapeN_ext (t u : Tape) (h : TEq M t u) (p : Nat) (q : List Bool) :
TEq M (tapeN M t p q) (tapeN M u p q) := by
intro j i hi
have es : symAt M t p = symAt M u p := symAt_ext M t u h p
show sel (Nat.beq j p) (Found.Pnp7.nthd (M.write q (symAt M t p)) i) (t j i) =
sel (Nat.beq j p) (Found.Pnp7.nthd (M.write q (symAt M u p)) i) (u j i)
rw [es]
cases Nat.beq j p with
| true => rfl
| false => exact h j i hi
/-- Agreement of configurations: tracks 0..k, head, state. -/
def CEq (c d : TCk) : Prop := TEq M c.tape d.tape /\ c.pos = d.pos /\ c.q = d.q
theorem step_ext (c d : TCk) (h : CEq M c d) : CEq M (tmStep M c) (tmStep M d) := by
cases c with
| mk t p q =>
cases d with
| mk u p' q' =>
have hp : p = p' := h.2.1
have hq : q = q' := h.2.2
subst hp; subst hq
have es : symAt M t p = symAt M u p := symAt_ext M t u h.1 p
refine And.intro (tapeN_ext M t u h.1 p q) (And.intro ?_ ?_)
· show Found.Pnp12.selN (M.right q (symAt M t p)) (p + 1) (p - 1) =
Found.Pnp12.selN (M.right q (symAt M u p)) (p + 1) (p - 1)
rw [es]
· show M.next q (symAt M t p) = M.next q (symAt M u p)
rw [es]
theorem run_ext (c d : TCk) (h : CEq M c d) : forall n, CEq M (tmRun M n c) (tmRun M n d) := by
intro n
induction n with
| zero => exact h
| succ n ih => exact step_ext M _ _ ih
theorem run_add (a : Nat) (c : TCk) : forall b, tmRun M b (tmRun M a c) = tmRun M (a + b) c := by
intro b
induction b with
| zero => rfl
| succ b ih =>
show tmStep M (tmRun M b (tmRun M a c)) = tmStep M (tmRun M (a + b) c)
rw [ih]
/-- A sweep right over n cells, then one step at cell pos + n: n + 1 steps. -/
theorem sweep_step (t0 : Tape) (pos n : Nat) (q : List Bool)
(hk : forall d, d < n -> Keep M q (symAt M t0 (pos + d)) true) :
CEq M (tmRun M (n + 1) (TCk.mk t0 pos q)) (tmStep M (TCk.mk t0 (pos + n) q)) := by
have h := sweepR M t0 pos q n hk
have hc : CEq M (tmRun M n (TCk.mk t0 pos q)) (TCk.mk t0 (pos + n) q) := h
exact step_ext M _ _ hc
end Found.Pnp46
#print axioms Found.Pnp46.tapeN_ext
#print axioms Found.Pnp46.step_ext
#print axioms Found.Pnp46.run_ext
#print axioms Found.Pnp46.run_add
#print axioms Found.Pnp46.sweep_step
#print axioms: 5 theorems, no axioms.
A register is laid on a present track and a value track; one push takes \(|xs|+1\) steps.
found/Pnp47.lean
/-
T330 piece 47. found/Pnp47 -- a register on two tracks and one push (CL3c, 5).
A register of the register machine (Pnp20) is kept on two tracks of the multi-track
machine (Pnp43): a "present" track a and a "value" track v. Lay t a v p xs says that
the cells p, p + 1, .. hold the bits xs on track v with track a true, and track a is
false on every cell from p + |xs| on. Proved, for any machine M:
* lay_present / lay_tail: track a is true on the |xs| cells of the list and false from
p + |xs| on;
* lay_ext / lay_teq: Lay only reads the tracks a and v;
* lay_snoc: writing a present bit and the value b at cell p + |xs| (and nothing else on
the tracks a, v) lays xs ++ [b];
* push_lay: if the machine in state q keeps itself and moves right on every symbol with
track a true, and at the first cell with track a false writes track a true and track
v = b, then after |xs| + 1 steps the tape lays xs ++ [b]: a push costs |xs| + 1 steps.
It relativizes (a tool; PNP_MAP.md). Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp46
namespace Found.Pnp47
open Found.Pnp7 (nthd)
open Found.Pnp12 (sel)
open Found.Pnp43
open Found.Pnp44
open Found.Pnp45
open Found.Pnp46
/-- The bits xs on track v from cell p, track a true there and false from p + |xs| on. -/
def Lay (t : Tape) (a v : Nat) : Nat -> List Bool -> Prop
| p, [] => forall j, p <= j -> t j a = false
| p, x :: xs => t p a = true /\ t p v = x /\ Lay t a v (p + 1) xs
theorem shift (p n : Nat) : p + 1 + n = p + (n + 1) := by
rw [Nat.add_assoc, Nat.add_comm 1 n]
theorem lay_present (t : Tape) (a v : Nat) : forall (xs : List Bool) p, Lay t a v p xs ->
forall d, d < xs.length -> t (p + d) a = true := by
intro xs
induction xs with
| nil => intro p _ d hd; exact False.elim (Nat.not_lt_zero d hd)
| cons x xs ih =>
intro p h d hd
cases d with
| zero => exact h.1
| succ d =>
have r := ih (p + 1) h.2.2 d (Nat.lt_of_succ_lt_succ hd)
rw [shift] at r
exact r
theorem lay_tail (t : Tape) (a v : Nat) : forall (xs : List Bool) p, Lay t a v p xs ->
forall j, p + xs.length <= j -> t j a = false := by
intro xs
induction xs with
| nil => intro p h j hj; exact h j hj
| cons x xs ih =>
intro p h j hj
have e : p + (xs.length + 1) = p + 1 + xs.length := (shift p xs.length).symm
have hj' : p + 1 + xs.length <= j := by
have q : p + (x :: xs).length <= j := hj
exact (congrArg (fun m => m <= j) e).mp q
exact ih (p + 1) h.2.2 j hj'
theorem lay_ext (t u : Tape) (a v : Nat) (ha : forall j, u j a = t j a) (hv : forall j, u j v = t j v) :
forall (xs : List Bool) p, Lay t a v p xs -> Lay u a v p xs := by
intro xs
induction xs with
| nil => intro p h j hj; rw [ha j]; exact h j hj
| cons x xs ih =>
intro p h
refine And.intro ?_ (And.intro ?_ ?_)
· rw [ha p]; exact h.1
· rw [hv p]; exact h.2.1
· exact ih (p + 1) h.2.2
theorem lay_teq (M : TMk) (t u : Tape) (a v : Nat) (h : TEq M u t) (ha : a < M.k + 1) (hv : v < M.k + 1)
(xs : List Bool) (p : Nat) (hl : Lay t a v p xs) : Lay u a v p xs :=
lay_ext t u a v (fun j => h j a ha) (fun j => h j v hv) xs p hl
theorem lt_self_succ (p n : Nat) : p < p + (n + 1) :=
Nat.lt_succ_of_le (Nat.le_add_right p n)
theorem ne_of_lt' {a b : Nat} (h : a < b) : a = b -> False := by
intro e
rw [e] at h
exact Nat.lt_irrefl b h
/-- Writing the present bit and b at the end cell lays xs ++ [b]. -/
theorem lay_snoc (t u : Tape) (a v : Nat) (b : Bool) : forall (xs : List Bool) p, Lay t a v p xs ->
(forall j, (j = p + xs.length -> False) -> u j a = t j a /\ u j v = t j v) ->
u (p + xs.length) a = true -> u (p + xs.length) v = b ->
Lay u a v p (xs ++ [b]) := by
intro xs
induction xs with
| nil =>
intro p h hu h1 h2
refine And.intro h1 (And.intro h2 ?_)
intro j hj
have hne : j = p + 0 -> False := fun e => by
have q : p + 1 <= p := by rw [e] at hj; exact hj
exact Nat.not_succ_le_self p q
rw [(hu j hne).1]
exact h j (Nat.le_of_succ_le hj)
| cons x xs ih =>
intro p h hu h1 h2
have e : p + 1 + xs.length = p + (x :: xs).length := shift p xs.length
have hne : p = p + (x :: xs).length -> False := ne_of_lt' (lt_self_succ p xs.length)
refine And.intro ?_ (And.intro ?_ ?_)
· rw [(hu p hne).1]; exact h.1
· rw [(hu p hne).2]; exact h.2.1
· refine ih (p + 1) h.2.2 ?_ ?_ ?_
· intro j hj; rw [e] at hj; exact hu j hj
· rw [e]; exact h1
· rw [e]; exact h2
theorem beq_false_of_ne : forall a b : Nat, (a = b -> False) -> Nat.beq a b = false := by
intro a b h
cases e : Nat.beq a b with
| false => rfl
| true => exact False.elim (h (Found.Pnp2.beq_eq a b e))
theorem nthd_sym (M : TMk) (t : Tape) (j i : Nat) (hi : i < M.k + 1) : nthd (symAt M t j) i = t j i := by
have r := nthd_read (t j) (M.k + 1) 0 i hi
rw [Nat.zero_add] at r
exact r
/-- One push: |xs| + 1 steps. -/
theorem push_lay (M : TMk) (t0 : Tape) (a v p : Nat) (q : List Bool) (b : Bool) (xs : List Bool)
(ha : a < M.k + 1) (hv : v < M.k + 1) (hl : Lay t0 a v p xs)
(hkeep : forall s, nthd s a = true -> Keep M q s true)
(hwa : nthd (M.write q (symAt M t0 (p + xs.length))) a = true)
(hwv : nthd (M.write q (symAt M t0 (p + xs.length))) v = b) :
Lay (tmRun M (xs.length + 1) (TCk.mk t0 p q)).tape a v p (xs ++ [b]) := by
have hk : forall d, d < xs.length -> Keep M q (symAt M t0 (p + d)) true := by
intro d hd
apply hkeep
rw [nthd_sym M t0 (p + d) a ha]
exact lay_present t0 a v xs p hl d hd
have hc := sweep_step M t0 p xs.length q hk
have hu : Lay (tapeN M t0 (p + xs.length) q) a v p (xs ++ [b]) := by
refine lay_snoc t0 (tapeN M t0 (p + xs.length) q) a v b xs p hl ?_ ?_ ?_
· intro j hj
have e : Nat.beq j (p + xs.length) = false := beq_false_of_ne j (p + xs.length) hj
refine And.intro ?_ ?_
· show sel (Nat.beq j (p + xs.length)) _ (t0 j a) = t0 j a
rw [e]; rfl
· show sel (Nat.beq j (p + xs.length)) _ (t0 j v) = t0 j v
rw [e]; rfl
· show sel (Nat.beq (p + xs.length) (p + xs.length)) _ _ = true
rw [Found.Pnp10.beq_refl]; exact hwa
· show sel (Nat.beq (p + xs.length) (p + xs.length)) _ _ = b
rw [Found.Pnp10.beq_refl]; exact hwv
exact lay_teq M _ _ a v hc.1 ha hv (xs ++ [b]) p hu
end Found.Pnp47
#print axioms Found.Pnp47.shift
#print axioms Found.Pnp47.lay_present
#print axioms Found.Pnp47.lay_tail
#print axioms Found.Pnp47.lay_ext
#print axioms Found.Pnp47.lay_teq
#print axioms Found.Pnp47.lt_self_succ
#print axioms Found.Pnp47.ne_of_lt'
#print axioms Found.Pnp47.lay_snoc
#print axioms Found.Pnp47.beq_false_of_ne
#print axioms Found.Pnp47.nthd_sym
#print axioms Found.Pnp47.push_lay
#print axioms: 11 theorems, no axioms.
One pop from a laid register takes \(|ys|+3\) steps and reads the last bit.
found/Pnp48.lean
/-
T330 piece 48. found/Pnp48 -- one pop from a register on two tracks (CL3c, 6).
The register of Pnp47 (Lay t a v p xs) is popped at its end cell: the head sweeps right
over the present cells, turns at the first absent cell, steps back and erases the last
present cell, reading its value. Proved, for any machine M:
* len_snoc: |ys ++ [b]| = |ys| + 1 (by induction, no library lemma);
* lay_last: Lay of ys ++ [b] has track a true and value b at cell p + |ys|;
* lay_unsnoc: erasing track a at the end cell of ys ++ [b] (nothing else changed on the
tracks a, v) lays ys;
* ceq_trans: agreement of configurations is transitive;
* pop_lay: if the machine in state q keeps itself and moves right on present cells,
turns left into state q1 at the first absent cell (writing it back), and in state q1
erases track a, then after |ys| + 3 steps the tape lays ys, the head is at the end cell
and the state is the next state of q1 on a symbol whose value bit is b.
So a pop costs |ys| + 3 steps. It relativizes (a tool; PNP_MAP.md).
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp47
namespace Found.Pnp48
open Found.Pnp7 (nthd)
open Found.Pnp12 (sel selN)
open Found.Pnp43
open Found.Pnp44
open Found.Pnp45
open Found.Pnp46
open Found.Pnp47
theorem len_snoc (b : Bool) : forall ys : List Bool, (ys ++ [b]).length = ys.length + 1 := by
intro ys
induction ys with
| nil => rfl
| cons y ys ih =>
show (ys ++ [b]).length + 1 = ys.length + 1 + 1
rw [ih]
theorem lay_last (t : Tape) (a v : Nat) (b : Bool) : forall (ys : List Bool) p,
Lay t a v p (ys ++ [b]) -> t (p + ys.length) a = true /\ t (p + ys.length) v = b := by
intro ys
induction ys with
| nil => intro p h; exact And.intro h.1 h.2.1
| cons y ys ih =>
intro p h
have r := ih (p + 1) h.2.2
have e : p + 1 + ys.length = p + (y :: ys).length := shift p ys.length
rw [e] at r
exact r
/-- p <= j and j different from p give p + 1 <= j (by cases on <=). -/
theorem succ_le_of_le_ne {p j : Nat} (h : p <= j) (hn : j = p -> False) : p + 1 <= j := by
cases h with
| refl => exact False.elim (hn rfl)
| step h' => exact Nat.succ_le_succ h'
/-- Erasing track a at the end cell of ys ++ [b] lays ys. -/
theorem lay_unsnoc (t u : Tape) (a v : Nat) (b : Bool) : forall (ys : List Bool) p,
Lay t a v p (ys ++ [b]) ->
(forall j, (j = p + ys.length -> False) -> u j a = t j a /\ u j v = t j v) ->
u (p + ys.length) a = false -> Lay u a v p ys := by
intro ys
induction ys with
| nil =>
intro p h hu h0 j hj
cases ej : Nat.beq j p with
| true =>
have e : j = p := Found.Pnp2.beq_eq j p ej
rw [e]; exact h0
| false =>
have hn : j = p -> False := fun e => by
rw [e, Found.Pnp10.beq_refl] at ej; exact Bool.noConfusion ej
have hn0 : j = p + 0 -> False := hn
rw [(hu j hn0).1]
exact h.2.2 j (succ_le_of_le_ne hj hn)
| cons y ys ih =>
intro p h hu h0
have e : p + 1 + ys.length = p + (y :: ys).length := shift p ys.length
have hne : p = p + (y :: ys).length -> False := ne_of_lt' (lt_self_succ p ys.length)
refine And.intro ?_ (And.intro ?_ ?_)
· rw [(hu p hne).1]; exact h.1
· rw [(hu p hne).2]; exact h.2.1
· refine ih (p + 1) h.2.2 ?_ ?_
· intro j hj; rw [e] at hj; exact hu j hj
· rw [e]; exact h0
theorem ceq_trans (M : TMk) (c d e : TCk) (h1 : CEq M c d) (h2 : CEq M d e) : CEq M c e :=
And.intro (teq_trans M _ _ _ h1.1 h2.1) (And.intro (h1.2.1.trans h2.2.1) (h1.2.2.trans h2.2.2))
/-- Turning left into q1, writing the cell back. -/
structure Turn (M : TMk) (q q1 : List Bool) (s : List Bool) : Prop where
nx : M.next q s = q1
wr : M.write q s = s
rt : M.right q s = false
/-- One pop: |ys| + 3 steps. -/
theorem pop_lay (M : TMk) (t0 : Tape) (a v p : Nat) (q q1 : List Bool) (b : Bool) (ys : List Bool)
(ha : a < M.k + 1) (hv : v < M.k + 1) (hl : Lay t0 a v p (ys ++ [b]))
(hkeep : forall s, nthd s a = true -> Keep M q s true)
(hturn : Turn M q q1 (symAt M t0 (p + (ys ++ [b]).length)))
(herase : nthd (M.write q1 (symAt M t0 (p + ys.length))) a = false) :
Lay (tmRun M ((ys ++ [b]).length + 1 + 1) (TCk.mk t0 p q)).tape a v p ys /\
(tmRun M ((ys ++ [b]).length + 1 + 1) (TCk.mk t0 p q)).pos =
selN (M.right q1 (symAt M t0 (p + ys.length))) (p + ys.length + 1) (p + ys.length - 1) /\
(tmRun M ((ys ++ [b]).length + 1 + 1) (TCk.mk t0 p q)).q = M.next q1 (symAt M t0 (p + ys.length)) /\
nthd (symAt M t0 (p + ys.length)) v = b := by
have hk : forall d, d < (ys ++ [b]).length -> Keep M q (symAt M t0 (p + d)) true := by
intro d hd
apply hkeep
rw [nthd_sym M t0 (p + d) a ha]
exact lay_present t0 a v (ys ++ [b]) p hl d hd
have h1 := sweep_step M t0 p (ys ++ [b]).length q hk
-- the turning step: back at cell p + |ys| in state q1, tape as at the start
have h2 : CEq M (tmStep M (TCk.mk t0 (p + (ys ++ [b]).length) q)) (TCk.mk t0 (p + ys.length) q1) := by
refine And.intro ?_ (And.intro ?_ ?_)
· exact tapeN_keep M t0 (p + (ys ++ [b]).length) q hturn.wr
· show selN (M.right q (symAt M t0 (p + (ys ++ [b]).length))) _ _ = p + ys.length
rw [hturn.rt]
show p + (ys ++ [b]).length - 1 = p + ys.length
rw [len_snoc b ys]
rfl
· exact hturn.nx
have h3 := step_ext M _ _ (ceq_trans M _ _ _ h1 h2)
have hu : Lay (tapeN M t0 (p + ys.length) q1) a v p ys := by
refine lay_unsnoc t0 (tapeN M t0 (p + ys.length) q1) a v b ys p hl ?_ ?_
· intro j hj
have e : Nat.beq j (p + ys.length) = false := beq_false_of_ne j (p + ys.length) hj
refine And.intro ?_ ?_
· show sel (Nat.beq j (p + ys.length)) _ (t0 j a) = t0 j a
rw [e]; rfl
· show sel (Nat.beq j (p + ys.length)) _ (t0 j v) = t0 j v
rw [e]; rfl
· show sel (Nat.beq (p + ys.length) (p + ys.length)) _ _ = false
rw [Found.Pnp10.beq_refl]; exact herase
refine And.intro (lay_teq M _ _ a v h3.1 ha hv ys p hu) (And.intro h3.2.1 (And.intro h3.2.2 ?_))
rw [nthd_sym M t0 (p + ys.length) v hv]
exact (lay_last t0 a v b ys p hl).2
end Found.Pnp48
#print axioms Found.Pnp48.len_snoc
#print axioms Found.Pnp48.lay_last
#print axioms Found.Pnp48.succ_le_of_le_ne
#print axioms Found.Pnp48.lay_unsnoc
#print axioms Found.Pnp48.ceq_trans
#print axioms Found.Pnp48.pop_lay
#print axioms: 6 theorems, no axioms.
The head returns to the cell before the register after a push.
found/Pnp49.lean
/-
T330 piece 49. found/Pnp49 -- a push that returns the head (CL3c, 7).
The register of Pnp47 starts at cell pos + 1, so the head can come back to cell pos after
an instruction. Proved, for any machine M:
* ret: in a state r that keeps itself and moves left on present cells, a head at
pos + n over n present cells pos + 1 .. pos + n comes back to pos in n steps, the
tape agreeing with the start;
* push_ret: a push as in Pnp47 whose writing step moves left into r, followed by the
return: after 2 |xs| + 1 steps the tape lays xs ++ [b] from pos + 1, the head is at
pos and the state is r.
So one push instruction, head back home, costs 2 |xs| + 1 steps.
It relativizes (a tool; PNP_MAP.md). Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp48
namespace Found.Pnp49
open Found.Pnp7 (nthd)
open Found.Pnp12 (selN)
open Found.Pnp43
open Found.Pnp44
open Found.Pnp45
open Found.Pnp46
open Found.Pnp47
open Found.Pnp48
/-- The return: n steps left over n present cells. -/
theorem ret (M : TMk) (t : Tape) (a pos n : Nat) (r : List Bool) (ha : a < M.k + 1)
(hp : forall d, d < n -> t (pos + 1 + d) a = true)
(hkr : forall s, nthd s a = true -> Keep M r s false) :
CEq M (tmRun M n (TCk.mk t (pos + n) r)) (TCk.mk t pos r) := by
have h := sweepL M n t pos r (fun d hd => by
apply hkr
rw [nthd_sym M t (pos + 1 + d) a ha]
exact hp d hd)
exact h
/-- A push with the head back at pos: 2 |xs| + 1 steps. -/
theorem push_ret (M : TMk) (t0 : Tape) (a v pos : Nat) (q r : List Bool) (b : Bool) (xs : List Bool)
(ha : a < M.k + 1) (hv : v < M.k + 1) (hl : Lay t0 a v (pos + 1) xs)
(hkeep : forall s, nthd s a = true -> Keep M q s true)
(hwa : nthd (M.write q (symAt M t0 (pos + 1 + xs.length))) a = true)
(hwv : nthd (M.write q (symAt M t0 (pos + 1 + xs.length))) v = b)
(hrt : M.right q (symAt M t0 (pos + 1 + xs.length)) = false)
(hnx : M.next q (symAt M t0 (pos + 1 + xs.length)) = r)
(hkr : forall s, nthd s a = true -> Keep M r s false) :
Lay (tmRun M (xs.length + 1 + xs.length) (TCk.mk t0 (pos + 1) q)).tape a v (pos + 1) (xs ++ [b]) /\
(tmRun M (xs.length + 1 + xs.length) (TCk.mk t0 (pos + 1) q)).pos = pos /\
(tmRun M (xs.length + 1 + xs.length) (TCk.mk t0 (pos + 1) q)).q = r := by
have hl1 := push_lay M t0 a v (pos + 1) q b xs ha hv hl hkeep hwa hwv
have hk : forall d, d < xs.length -> Keep M q (symAt M t0 (pos + 1 + d)) true := by
intro d hd
apply hkeep
rw [nthd_sym M t0 (pos + 1 + d) a ha]
exact lay_present t0 a v xs (pos + 1) hl d hd
have hc := sweep_step M t0 (pos + 1) xs.length q hk
-- d1: the configuration after the push
have hpos : (tmRun M (xs.length + 1) (TCk.mk t0 (pos + 1) q)).pos = pos + xs.length := by
rw [hc.2.1]
show selN (M.right q (symAt M t0 (pos + 1 + xs.length))) _ _ = pos + xs.length
rw [hrt]
show pos + 1 + xs.length - 1 = pos + xs.length
rw [Nat.add_right_comm pos 1 xs.length]
rfl
have hq : (tmRun M (xs.length + 1) (TCk.mk t0 (pos + 1) q)).q = r := by
rw [hc.2.2]; exact hnx
have hd1 : CEq M (tmRun M (xs.length + 1) (TCk.mk t0 (pos + 1) q))
(TCk.mk (tmRun M (xs.length + 1) (TCk.mk t0 (pos + 1) q)).tape (pos + xs.length) r) :=
And.intro (fun _ _ _ => rfl) (And.intro hpos hq)
have hp : forall d, d < xs.length ->
(tmRun M (xs.length + 1) (TCk.mk t0 (pos + 1) q)).tape (pos + 1 + d) a = true := by
intro d hd
have hd' : d < (xs ++ [b]).length := by
rw [len_snoc b xs]; exact Nat.lt_succ_of_lt hd
exact lay_present _ a v (xs ++ [b]) (pos + 1) hl1 d hd'
have h2 := ret M (tmRun M (xs.length + 1) (TCk.mk t0 (pos + 1) q)).tape a pos xs.length r ha hp hkr
have h3 := ceq_trans M _ _ _ (run_ext M _ _ hd1 xs.length) h2
rw [(run_add M (xs.length + 1) (TCk.mk t0 (pos + 1) q) xs.length).symm]
refine And.intro ?_ (And.intro h3.2.1 h3.2.2)
exact lay_teq M _ _ a v h3.1 ha hv (xs ++ [b]) (pos + 1) hl1
end Found.Pnp49
#print axioms Found.Pnp49.ret
#print axioms Found.Pnp49.push_ret
#print axioms: 2 theorems, no axioms.
Pops on empty and nonempty registers return the head.
found/Pnp50.lean
/-
T330 piece 50. found/Pnp50 -- pops that return the head (CL3c, 8).
The pop instruction of the register machine (Pnp20) jumps on what it finds: empty, 0 or
1. On the two-track register of Pnp47 starting at cell pos + 1, for any machine M:
* pop_empty: if the register is empty, one step that turns left from cell pos + 1 into
a state e (writing the cell back) leaves the head at pos in state e, the register
still empty: 1 step;
* pop_ret: if the register holds ys ++ [b], the pop of Pnp48 whose erasing step moves
left, followed by the return of Pnp49 in the state r1 the erasing step chose (which
may depend on b): after |ys ++ [b]| + 2 + |ys| steps the tape lays ys from pos + 1,
the head is at pos, the state is r1, and b is the value read.
So one pop instruction, head back home, costs at most 2 |ys| + 3 steps.
It relativizes (a tool; PNP_MAP.md). Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp49
namespace Found.Pnp50
open Found.Pnp7 (nthd)
open Found.Pnp12 (selN)
open Found.Pnp43
open Found.Pnp44
open Found.Pnp45
open Found.Pnp46
open Found.Pnp47
open Found.Pnp48
open Found.Pnp49
/-- Pop on an empty register: 1 step. -/
theorem pop_empty (M : TMk) (t0 : Tape) (a v pos : Nat) (q e : List Bool)
(ha : a < M.k + 1) (hv : v < M.k + 1) (hl : Lay t0 a v (pos + 1) [])
(hturn : Turn M q e (symAt M t0 (pos + 1))) :
Lay (tmRun M 1 (TCk.mk t0 (pos + 1) q)).tape a v (pos + 1) [] /\
(tmRun M 1 (TCk.mk t0 (pos + 1) q)).pos = pos /\
(tmRun M 1 (TCk.mk t0 (pos + 1) q)).q = e := by
have h : CEq M (tmStep M (TCk.mk t0 (pos + 1) q)) (TCk.mk t0 pos e) := by
refine And.intro ?_ (And.intro ?_ ?_)
· exact tapeN_keep M t0 (pos + 1) q hturn.wr
· show selN (M.right q (symAt M t0 (pos + 1))) _ _ = pos
rw [hturn.rt]
rfl
· exact hturn.nx
exact And.intro (lay_teq M _ _ a v h.1 ha hv [] (pos + 1) hl) (And.intro h.2.1 h.2.2)
/-- Pop of ys ++ [b] with the head back at pos. -/
theorem pop_ret (M : TMk) (t0 : Tape) (a v pos : Nat) (q q1 : List Bool) (b : Bool) (ys : List Bool)
(ha : a < M.k + 1) (hv : v < M.k + 1) (hl : Lay t0 a v (pos + 1) (ys ++ [b]))
(hkeep : forall s, nthd s a = true -> Keep M q s true)
(hturn : Turn M q q1 (symAt M t0 (pos + 1 + (ys ++ [b]).length)))
(herase : nthd (M.write q1 (symAt M t0 (pos + 1 + ys.length))) a = false)
(hrt : M.right q1 (symAt M t0 (pos + 1 + ys.length)) = false)
(hkr : forall s, nthd s a = true -> Keep M (M.next q1 (symAt M t0 (pos + 1 + ys.length))) s false) :
Lay (tmRun M ((ys ++ [b]).length + 1 + 1 + ys.length) (TCk.mk t0 (pos + 1) q)).tape a v (pos + 1) ys /\
(tmRun M ((ys ++ [b]).length + 1 + 1 + ys.length) (TCk.mk t0 (pos + 1) q)).pos = pos /\
(tmRun M ((ys ++ [b]).length + 1 + 1 + ys.length) (TCk.mk t0 (pos + 1) q)).q =
M.next q1 (symAt M t0 (pos + 1 + ys.length)) /\
nthd (symAt M t0 (pos + 1 + ys.length)) v = b := by
have h := pop_lay M t0 a v (pos + 1) q q1 b ys ha hv hl hkeep hturn herase
have hpos : (tmRun M ((ys ++ [b]).length + 1 + 1) (TCk.mk t0 (pos + 1) q)).pos = pos + ys.length := by
rw [h.2.1, hrt]
show pos + 1 + ys.length - 1 = pos + ys.length
rw [Nat.add_right_comm pos 1 ys.length]
rfl
have hd1 : CEq M (tmRun M ((ys ++ [b]).length + 1 + 1) (TCk.mk t0 (pos + 1) q))
(TCk.mk (tmRun M ((ys ++ [b]).length + 1 + 1) (TCk.mk t0 (pos + 1) q)).tape (pos + ys.length)
(M.next q1 (symAt M t0 (pos + 1 + ys.length)))) :=
And.intro (fun _ _ _ => rfl) (And.intro hpos h.2.2.1)
have hp : forall d, d < ys.length ->
(tmRun M ((ys ++ [b]).length + 1 + 1) (TCk.mk t0 (pos + 1) q)).tape (pos + 1 + d) a = true :=
fun d hd => lay_present _ a v ys (pos + 1) h.1 d hd
have h2 := ret M _ a pos ys.length _ ha hp hkr
have h3 := ceq_trans M _ _ _ (run_ext M _ _ hd1 ys.length) h2
rw [(run_add M ((ys ++ [b]).length + 1 + 1) (TCk.mk t0 (pos + 1) q) ys.length).symm]
exact And.intro (lay_teq M _ _ a v h3.1 ha hv ys (pos + 1) h.1)
(And.intro h3.2.1 (And.intro h3.2.2 h.2.2.2))
end Found.Pnp50
#print axioms Found.Pnp50.pop_empty
#print axioms Found.Pnp50.pop_ret
#print axioms: 2 theorems, no axioms.
Push and pop leave every other laid register unchanged.
found/Pnp51.lean
/-
T330 piece 51. found/Pnp51 -- push and pop leave the other registers alone (CL3c, 9).
The sweeps of a push or pop write every cell back; only one step writes. So the whole
instruction changes the tape exactly as that one step does. Proved, for any machine M:
* push_tape: after the push of Pnp49 (2 |xs| + 1 steps) the tape agrees on all tracks
with tapeN of the start tape at the end cell pos + 1 + |xs| in state q;
* pop_tape: after the pop of Pnp50 the tape agrees on all tracks with tapeN of the
start tape at the cell pos + 1 + |ys| in state q1;
* lay_other: a register on tracks a', v' is kept by tapeN at a cell where the writing
leaves the bits a', v' of the symbol as they were;
* push_other / pop_other: hence a register on other tracks is kept by a whole push or
pop instruction, when the one writing step leaves its two bits alone.
It relativizes (a tool; PNP_MAP.md). Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp50
namespace Found.Pnp51
open Found.Pnp7 (nthd)
open Found.Pnp12 (sel selN)
open Found.Pnp43
open Found.Pnp44
open Found.Pnp45
open Found.Pnp46
open Found.Pnp47
open Found.Pnp48
open Found.Pnp49
/-- tapeN keeps a track at every cell when the write keeps that bit of the symbol. -/
theorem tapeN_track (M : TMk) (t : Tape) (c : Nat) (q : List Bool) (i : Nat) (hi : i < M.k + 1)
(hw : nthd (M.write q (symAt M t c)) i = nthd (symAt M t c) i) :
forall j, tapeN M t c q j i = t j i := by
intro j
show sel (Nat.beq j c) (nthd (M.write q (symAt M t c)) i) (t j i) = t j i
cases e : Nat.beq j c with
| false => rfl
| true =>
have ej : j = c := Found.Pnp2.beq_eq j c e
rw [ej]
show nthd (M.write q (symAt M t c)) i = t c i
rw [hw]
exact nthd_sym M t c i hi
theorem lay_other (M : TMk) (t : Tape) (c : Nat) (q : List Bool) (a v p : Nat) (zs : List Bool)
(ha : a < M.k + 1) (hv : v < M.k + 1) (hl : Lay t a v p zs)
(hwa : nthd (M.write q (symAt M t c)) a = nthd (symAt M t c) a)
(hwv : nthd (M.write q (symAt M t c)) v = nthd (symAt M t c) v) :
Lay (tapeN M t c q) a v p zs :=
lay_ext t (tapeN M t c q) a v (tapeN_track M t c q a ha hwa) (tapeN_track M t c q v hv hwv) zs p hl
theorem push_tape (M : TMk) (t0 : Tape) (a v pos : Nat) (q r : List Bool) (xs : List Bool)
(ha : a < M.k + 1) (hl : Lay t0 a v (pos + 1) xs) (b : Bool) (hv : v < M.k + 1)
(hkeep : forall s, nthd s a = true -> Keep M q s true)
(hwa : nthd (M.write q (symAt M t0 (pos + 1 + xs.length))) a = true)
(hwv : nthd (M.write q (symAt M t0 (pos + 1 + xs.length))) v = b)
(hrt : M.right q (symAt M t0 (pos + 1 + xs.length)) = false)
(hnx : M.next q (symAt M t0 (pos + 1 + xs.length)) = r)
(hkr : forall s, nthd s a = true -> Keep M r s false) :
TEq M (tmRun M (xs.length + 1 + xs.length) (TCk.mk t0 (pos + 1) q)).tape
(tapeN M t0 (pos + 1 + xs.length) q) := by
have hl1 := push_lay M t0 a v (pos + 1) q b xs ha hv hl hkeep hwa hwv
have hk : forall d, d < xs.length -> Keep M q (symAt M t0 (pos + 1 + d)) true := by
intro d hd
apply hkeep
rw [nthd_sym M t0 (pos + 1 + d) a ha]
exact lay_present t0 a v xs (pos + 1) hl d hd
have hc := sweep_step M t0 (pos + 1) xs.length q hk
have hpos : (tmRun M (xs.length + 1) (TCk.mk t0 (pos + 1) q)).pos = pos + xs.length := by
rw [hc.2.1]
show selN (M.right q (symAt M t0 (pos + 1 + xs.length))) _ _ = pos + xs.length
rw [hrt]
show pos + 1 + xs.length - 1 = pos + xs.length
rw [Nat.add_right_comm pos 1 xs.length]
rfl
have hq : (tmRun M (xs.length + 1) (TCk.mk t0 (pos + 1) q)).q = r := by
rw [hc.2.2]; exact hnx
have hd1 : CEq M (tmRun M (xs.length + 1) (TCk.mk t0 (pos + 1) q))
(TCk.mk (tmRun M (xs.length + 1) (TCk.mk t0 (pos + 1) q)).tape (pos + xs.length) r) :=
And.intro (fun _ _ _ => rfl) (And.intro hpos hq)
have hp : forall d, d < xs.length ->
(tmRun M (xs.length + 1) (TCk.mk t0 (pos + 1) q)).tape (pos + 1 + d) a = true := by
intro d hd
have hd' : d < (xs ++ [b]).length := by
rw [len_snoc b xs]; exact Nat.lt_succ_of_lt hd
exact lay_present _ a v (xs ++ [b]) (pos + 1) hl1 d hd'
have h2 := ret M (tmRun M (xs.length + 1) (TCk.mk t0 (pos + 1) q)).tape a pos xs.length r ha hp hkr
have h3 := ceq_trans M _ _ _ (run_ext M _ _ hd1 xs.length) h2
rw [(run_add M (xs.length + 1) (TCk.mk t0 (pos + 1) q) xs.length).symm]
exact teq_trans M _ _ _ h3.1 hc.1
theorem pop_tape (M : TMk) (t0 : Tape) (a v pos : Nat) (q q1 : List Bool) (b : Bool) (ys : List Bool)
(ha : a < M.k + 1) (hv : v < M.k + 1) (hl : Lay t0 a v (pos + 1) (ys ++ [b]))
(hkeep : forall s, nthd s a = true -> Keep M q s true)
(hturn : Turn M q q1 (symAt M t0 (pos + 1 + (ys ++ [b]).length)))
(herase : nthd (M.write q1 (symAt M t0 (pos + 1 + ys.length))) a = false)
(hrt : M.right q1 (symAt M t0 (pos + 1 + ys.length)) = false)
(hkr : forall s, nthd s a = true -> Keep M (M.next q1 (symAt M t0 (pos + 1 + ys.length))) s false) :
TEq M (tmRun M ((ys ++ [b]).length + 1 + 1 + ys.length) (TCk.mk t0 (pos + 1) q)).tape
(tapeN M t0 (pos + 1 + ys.length) q1) := by
have h := pop_lay M t0 a v (pos + 1) q q1 b ys ha hv hl hkeep hturn herase
have hk : forall d, d < (ys ++ [b]).length -> Keep M q (symAt M t0 (pos + 1 + d)) true := by
intro d hd
apply hkeep
rw [nthd_sym M t0 (pos + 1 + d) a ha]
exact lay_present t0 a v (ys ++ [b]) (pos + 1) hl d hd
have h1 := sweep_step M t0 (pos + 1) (ys ++ [b]).length q hk
have h2 : CEq M (tmStep M (TCk.mk t0 (pos + 1 + (ys ++ [b]).length) q)) (TCk.mk t0 (pos + 1 + ys.length) q1) := by
refine And.intro ?_ (And.intro ?_ ?_)
· exact tapeN_keep M t0 (pos + 1 + (ys ++ [b]).length) q hturn.wr
· show selN (M.right q (symAt M t0 (pos + 1 + (ys ++ [b]).length))) _ _ = pos + 1 + ys.length
rw [hturn.rt]
show pos + 1 + (ys ++ [b]).length - 1 = pos + 1 + ys.length
rw [len_snoc b ys]
rfl
· exact hturn.nx
have h3 := step_ext M _ _ (ceq_trans M _ _ _ h1 h2)
have hpos : (tmRun M ((ys ++ [b]).length + 1 + 1) (TCk.mk t0 (pos + 1) q)).pos = pos + ys.length := by
rw [h.2.1, hrt]
show pos + 1 + ys.length - 1 = pos + ys.length
rw [Nat.add_right_comm pos 1 ys.length]
rfl
have hd1 : CEq M (tmRun M ((ys ++ [b]).length + 1 + 1) (TCk.mk t0 (pos + 1) q))
(TCk.mk (tmRun M ((ys ++ [b]).length + 1 + 1) (TCk.mk t0 (pos + 1) q)).tape (pos + ys.length)
(M.next q1 (symAt M t0 (pos + 1 + ys.length)))) :=
And.intro (fun _ _ _ => rfl) (And.intro hpos h.2.2.1)
have hp : forall d, d < ys.length ->
(tmRun M ((ys ++ [b]).length + 1 + 1) (TCk.mk t0 (pos + 1) q)).tape (pos + 1 + d) a = true :=
fun d hd => lay_present _ a v ys (pos + 1) h.1 d hd
have h4 := ret M _ a pos ys.length _ ha hp hkr
have h5 := ceq_trans M _ _ _ (run_ext M _ _ hd1 ys.length) h4
rw [(run_add M ((ys ++ [b]).length + 1 + 1) (TCk.mk t0 (pos + 1) q) ys.length).symm]
exact teq_trans M _ _ _ h5.1 h3.1
theorem push_other (M : TMk) (t0 : Tape) (a v pos : Nat) (q r : List Bool) (xs : List Bool)
(ha : a < M.k + 1) (hl : Lay t0 a v (pos + 1) xs) (b : Bool) (hv : v < M.k + 1)
(hkeep : forall s, nthd s a = true -> Keep M q s true)
(hwa : nthd (M.write q (symAt M t0 (pos + 1 + xs.length))) a = true)
(hwv : nthd (M.write q (symAt M t0 (pos + 1 + xs.length))) v = b)
(hrt : M.right q (symAt M t0 (pos + 1 + xs.length)) = false)
(hnx : M.next q (symAt M t0 (pos + 1 + xs.length)) = r)
(hkr : forall s, nthd s a = true -> Keep M r s false)
(a' v' p' : Nat) (zs : List Bool) (ha' : a' < M.k + 1) (hv' : v' < M.k + 1) (hz : Lay t0 a' v' p' zs)
(hwa' : nthd (M.write q (symAt M t0 (pos + 1 + xs.length))) a' = nthd (symAt M t0 (pos + 1 + xs.length)) a')
(hwv' : nthd (M.write q (symAt M t0 (pos + 1 + xs.length))) v' = nthd (symAt M t0 (pos + 1 + xs.length)) v') :
Lay (tmRun M (xs.length + 1 + xs.length) (TCk.mk t0 (pos + 1) q)).tape a' v' p' zs :=
lay_teq M _ _ a' v' (push_tape M t0 a v pos q r xs ha hl b hv hkeep hwa hwv hrt hnx hkr) ha' hv' zs p'
(lay_other M t0 _ q a' v' p' zs ha' hv' hz hwa' hwv')
theorem pop_other (M : TMk) (t0 : Tape) (a v pos : Nat) (q q1 : List Bool) (b : Bool) (ys : List Bool)
(ha : a < M.k + 1) (hv : v < M.k + 1) (hl : Lay t0 a v (pos + 1) (ys ++ [b]))
(hkeep : forall s, nthd s a = true -> Keep M q s true)
(hturn : Turn M q q1 (symAt M t0 (pos + 1 + (ys ++ [b]).length)))
(herase : nthd (M.write q1 (symAt M t0 (pos + 1 + ys.length))) a = false)
(hrt : M.right q1 (symAt M t0 (pos + 1 + ys.length)) = false)
(hkr : forall s, nthd s a = true -> Keep M (M.next q1 (symAt M t0 (pos + 1 + ys.length))) s false)
(a' v' p' : Nat) (zs : List Bool) (ha' : a' < M.k + 1) (hv' : v' < M.k + 1) (hz : Lay t0 a' v' p' zs)
(hwa' : nthd (M.write q1 (symAt M t0 (pos + 1 + ys.length))) a' = nthd (symAt M t0 (pos + 1 + ys.length)) a')
(hwv' : nthd (M.write q1 (symAt M t0 (pos + 1 + ys.length))) v' = nthd (symAt M t0 (pos + 1 + ys.length)) v') :
Lay (tmRun M ((ys ++ [b]).length + 1 + 1 + ys.length) (TCk.mk t0 (pos + 1) q)).tape a' v' p' zs :=
lay_teq M _ _ a' v' (pop_tape M t0 a v pos q q1 b ys ha hv hl hkeep hturn herase hrt hkr) ha' hv' zs p'
(lay_other M t0 _ q1 a' v' p' zs ha' hv' hz hwa' hwv')
end Found.Pnp51
#print axioms Found.Pnp51.tapeN_track
#print axioms Found.Pnp51.lay_other
#print axioms Found.Pnp51.push_tape
#print axioms Found.Pnp51.pop_tape
#print axioms Found.Pnp51.push_other
#print axioms Found.Pnp51.pop_other
#print axioms: 6 theorems, no axioms.
Register \(i\) stored reversed on tracks \(A_i,V_i\); push and pop match the register machine.
found/Pnp52.lean
/-
T330 piece 52. found/Pnp52 -- register machine instructions on the track machine (CL3c, 10).
The registers of the register machine (Pnp20) are kept on the tracks of the multi-track
machine (Pnp43): register i on the tracks A i (present) and V i (value), from cell 1,
stored reversed (rv), so that the top of the register is the last laid cell. The head is
at home cell 0 between instructions. Proved, for any machine M and track maps A, V:
* rv_len: the stored list has the length of the register;
* push_sim: a push of b on register rr (the push of Pnp49, 2 |x rr| + 1 steps) lays
every register i < R as rv of the register after Pnp20's push instruction;
* pop_empty_sim: a pop on an empty register (1 step) keeps every register, as Pnp20's
pop does on an empty register;
* pop_cons_sim: a pop on a register holding b :: tl (the pop of Pnp50) lays every
register as rv of the register after Pnp20's pop, and the bit read is b.
The other registers are kept when the one writing step of the instruction leaves their
two tracks alone (Pnp51).
It relativizes (a tool; PNP_MAP.md). Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp51
import found.Pnp20
namespace Found.Pnp52
open Found.Pnp7 (nthd)
open Found.Pnp20 (upd upd_same upd_other)
open Found.Pnp43
open Found.Pnp44
open Found.Pnp45
open Found.Pnp46
open Found.Pnp47
open Found.Pnp48
open Found.Pnp49
open Found.Pnp50
open Found.Pnp51
/-- Reverse, by structural recursion: the top of a register is the last cell. -/
def rv : List Bool -> List Bool
| [] => []
| b :: l => rv l ++ [b]
theorem rv_len : forall l : List Bool, (rv l).length = l.length := by
intro l
induction l with
| nil => rfl
| cons b l ih =>
show (rv l ++ [b]).length = l.length + 1
rw [len_snoc b (rv l), ih]
/-- All registers below R laid on their tracks from cell 1. -/
def Regs (t : Tape) (A V : Nat -> Nat) (R : Nat) (x : Nat -> List Bool) : Prop :=
forall i, i < R -> Lay t (A i) (V i) (0 + 1) (rv (x i))
/-- Push of b on register rr. -/
theorem push_sim (M : TMk) (A V : Nat -> Nat) (R : Nat) (x : Nat -> List Bool) (t0 : Tape)
(q r : List Bool) (rr : Nat) (b : Bool) (hR : rr < R)
(hAV : forall i, i < R -> A i < M.k + 1 /\ V i < M.k + 1)
(hl : Regs t0 A V R x)
(hkeep : forall s, nthd s (A rr) = true -> Keep M q s true)
(hwa : nthd (M.write q (symAt M t0 (0 + 1 + (rv (x rr)).length))) (A rr) = true)
(hwv : nthd (M.write q (symAt M t0 (0 + 1 + (rv (x rr)).length))) (V rr) = b)
(hrt : M.right q (symAt M t0 (0 + 1 + (rv (x rr)).length)) = false)
(hnx : M.next q (symAt M t0 (0 + 1 + (rv (x rr)).length)) = r)
(hkr : forall s, nthd s (A rr) = true -> Keep M r s false)
(hoth : forall i, i < R -> Nat.beq i rr = false ->
nthd (M.write q (symAt M t0 (0 + 1 + (rv (x rr)).length))) (A i) =
nthd (symAt M t0 (0 + 1 + (rv (x rr)).length)) (A i) /\
nthd (M.write q (symAt M t0 (0 + 1 + (rv (x rr)).length))) (V i) =
nthd (symAt M t0 (0 + 1 + (rv (x rr)).length)) (V i)) :
Regs (tmRun M ((rv (x rr)).length + 1 + (rv (x rr)).length) (TCk.mk t0 (0 + 1) q)).tape A V R
(upd x rr (b :: x rr)) /\
(tmRun M ((rv (x rr)).length + 1 + (rv (x rr)).length) (TCk.mk t0 (0 + 1) q)).pos = 0 /\
(tmRun M ((rv (x rr)).length + 1 + (rv (x rr)).length) (TCk.mk t0 (0 + 1) q)).q = r := by
have hp := push_ret M t0 (A rr) (V rr) 0 q r b (rv (x rr)) (hAV rr hR).1 (hAV rr hR).2 (hl rr hR)
hkeep hwa hwv hrt hnx hkr
refine And.intro ?_ (And.intro hp.2.1 hp.2.2)
intro i hi
cases e : Nat.beq i rr with
| true =>
have ei : i = rr := Found.Pnp2.beq_eq i rr e
rw [ei, upd_same]
exact hp.1
| false =>
rw [upd_other x rr i _ e]
exact push_other M t0 (A rr) (V rr) 0 q r (rv (x rr)) (hAV rr hR).1 (hl rr hR) b (hAV rr hR).2
hkeep hwa hwv hrt hnx hkr (A i) (V i) (0 + 1) (rv (x i)) (hAV i hi).1 (hAV i hi).2 (hl i hi)
(hoth i hi e).1 (hoth i hi e).2
/-- Pop on an empty register rr: every register kept, 1 step. -/
theorem pop_empty_sim (M : TMk) (A V : Nat -> Nat) (R : Nat) (x : Nat -> List Bool) (t0 : Tape)
(q e : List Bool) (rr : Nat) (hR : rr < R)
(hAV : forall i, i < R -> A i < M.k + 1 /\ V i < M.k + 1)
(hl : Regs t0 A V R x) (hx : x rr = [])
(hturn : Turn M q e (symAt M t0 (0 + 1))) :
Regs (tmRun M 1 (TCk.mk t0 (0 + 1) q)).tape A V R x /\
(tmRun M 1 (TCk.mk t0 (0 + 1) q)).pos = 0 /\
(tmRun M 1 (TCk.mk t0 (0 + 1) q)).q = e := by
have hrr : Lay t0 (A rr) (V rr) (0 + 1) [] := by
have h := hl rr hR
rw [hx] at h
exact h
have hp := pop_empty M t0 (A rr) (V rr) 0 q e (hAV rr hR).1 (hAV rr hR).2 hrr hturn
have hT : TEq M (tmRun M 1 (TCk.mk t0 (0 + 1) q)).tape t0 := tapeN_keep M t0 (0 + 1) q hturn.wr
refine And.intro ?_ (And.intro hp.2.1 hp.2.2)
intro i hi
exact lay_teq M _ _ (A i) (V i) hT (hAV i hi).1 (hAV i hi).2 (rv (x i)) (0 + 1) (hl i hi)
/-- Pop on register rr holding b :: tl: the register becomes tl, the others are kept, b is read. -/
theorem pop_cons_sim (M : TMk) (A V : Nat -> Nat) (R : Nat) (x : Nat -> List Bool) (t0 : Tape)
(q q1 : List Bool) (rr : Nat) (b : Bool) (tl : List Bool) (hR : rr < R)
(hAV : forall i, i < R -> A i < M.k + 1 /\ V i < M.k + 1)
(hl : Regs t0 A V R x) (hx : x rr = b :: tl)
(hkeep : forall s, nthd s (A rr) = true -> Keep M q s true)
(hturn : Turn M q q1 (symAt M t0 (0 + 1 + (rv tl ++ [b]).length)))
(herase : nthd (M.write q1 (symAt M t0 (0 + 1 + (rv tl).length))) (A rr) = false)
(hrt : M.right q1 (symAt M t0 (0 + 1 + (rv tl).length)) = false)
(hkr : forall s, nthd s (A rr) = true -> Keep M (M.next q1 (symAt M t0 (0 + 1 + (rv tl).length))) s false)
(hoth : forall i, i < R -> Nat.beq i rr = false ->
nthd (M.write q1 (symAt M t0 (0 + 1 + (rv tl).length))) (A i) =
nthd (symAt M t0 (0 + 1 + (rv tl).length)) (A i) /\
nthd (M.write q1 (symAt M t0 (0 + 1 + (rv tl).length))) (V i) =
nthd (symAt M t0 (0 + 1 + (rv tl).length)) (V i)) :
Regs (tmRun M ((rv tl ++ [b]).length + 1 + 1 + (rv tl).length) (TCk.mk t0 (0 + 1) q)).tape A V R
(upd x rr tl) /\
(tmRun M ((rv tl ++ [b]).length + 1 + 1 + (rv tl).length) (TCk.mk t0 (0 + 1) q)).pos = 0 /\
(tmRun M ((rv tl ++ [b]).length + 1 + 1 + (rv tl).length) (TCk.mk t0 (0 + 1) q)).q =
M.next q1 (symAt M t0 (0 + 1 + (rv tl).length)) /\
nthd (symAt M t0 (0 + 1 + (rv tl).length)) (V rr) = b := by
have hrr : Lay t0 (A rr) (V rr) (0 + 1) (rv tl ++ [b]) := by
have h := hl rr hR
rw [hx] at h
exact h
have hp := pop_ret M t0 (A rr) (V rr) 0 q q1 b (rv tl) (hAV rr hR).1 (hAV rr hR).2 hrr
hkeep hturn herase hrt hkr
refine And.intro ?_ (And.intro hp.2.1 (And.intro hp.2.2.1 hp.2.2.2))
intro i hi
cases e : Nat.beq i rr with
| true =>
have ei : i = rr := Found.Pnp2.beq_eq i rr e
rw [ei, upd_same]
exact hp.1
| false =>
rw [upd_other x rr i _ e]
exact pop_other M t0 (A rr) (V rr) 0 q q1 b (rv tl) (hAV rr hR).1 (hAV rr hR).2 hrr
hkeep hturn herase hrt hkr (A i) (V i) (0 + 1) (rv (x i)) (hAV i hi).1 (hAV i hi).2 (hl i hi)
(hoth i hi e).1 (hoth i hi e).2
end Found.Pnp52
#print axioms Found.Pnp52.rv_len
#print axioms Found.Pnp52.push_sim
#print axioms Found.Pnp52.pop_empty_sim
#print axioms Found.Pnp52.pop_cons_sim
#print axioms: 4 theorems, no axioms.
From the home cell, one register step (push, pop, jump) is simulated in a counted number of steps.
found/Pnp53.lean
/-
T330 piece 53. found/Pnp53 -- one whole register machine step on the track machine (CL3c, 11).
The head waits at a home cell h between instructions; register i is laid on the tracks
A i, V i from cell h + 1, reversed (Pnp52's rv). The state at home is code pc. One step of
the register machine (Pnp20) is: one step right off home into the instruction state
(Fwd), then the push or pop of Pnp49/Pnp50 back to home, or for a jump one step back.
Proved, for any machine M, track maps A, V, home h and state code:
* enter, after_enter: the step off home keeps the tape and puts the head on h + 1;
* push_simH, pop_empty_simH, pop_cons_simH: Pnp52 with the home cell h in place of 0;
* sim_push, sim_pop_empty, sim_pop_cons, sim_jmp: from a configuration related (Sim) to
a register configuration whose instruction is push / pop / jump, the track machine
reaches, after 1 + (2 n + 1), 1 + 1, 1 + (2 n + 3) (n the length of the popped
register's tail) and 2 steps, a configuration related to Pnp20's step.
It relativizes (a tool; PNP_MAP.md). Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp52
namespace Found.Pnp53
open Found.Pnp7 (nthd)
open Found.Pnp12 (selN)
open Found.Pnp20 (Ins Cfg upd upd_same upd_other nthI step exec popTo)
open Found.Pnp43
open Found.Pnp44
open Found.Pnp45
open Found.Pnp46
open Found.Pnp47
open Found.Pnp48
open Found.Pnp49
open Found.Pnp50
open Found.Pnp51
open Found.Pnp52 (rv)
/-- All registers below R laid on their tracks from cell h + 1. -/
def RegsAt (t : Tape) (A V : Nat -> Nat) (R h : Nat) (x : Nat -> List Bool) : Prop :=
forall i, i < R -> Lay t (A i) (V i) (h + 1) (rv (x i))
/-- Head at home, state = code of the counter, registers laid. -/
def Sim (_M : TMk) (A V : Nat -> Nat) (R h : Nat) (code : Nat -> List Bool) (c : TCk) (d : Cfg) : Prop :=
c.pos = h /\ c.q = code d.pc /\ RegsAt c.tape A V R h d.regs
/-- Moving right into q1, writing the cell back. -/
structure Fwd (M : TMk) (q q1 : List Bool) (s : List Bool) : Prop where
nx : M.next q s = q1
wr : M.write q s = s
rt : M.right q s = true
theorem enter (M : TMk) (t0 : Tape) (h : Nat) (q q1 : List Bool) (hf : Fwd M q q1 (symAt M t0 h)) :
CEq M (tmRun M 1 (TCk.mk t0 h q)) (TCk.mk t0 (h + 1) q1) := by
refine And.intro ?_ (And.intro ?_ ?_)
· exact tapeN_keep M t0 h q hf.wr
· show selN (M.right q (symAt M t0 h)) (h + 1) (h - 1) = h + 1
rw [hf.rt]
rfl
· exact hf.nx
theorem after_enter (M : TMk) (t0 : Tape) (h : Nat) (q q1 : List Bool) (hf : Fwd M q q1 (symAt M t0 h))
(n : Nat) : CEq M (tmRun M (1 + n) (TCk.mk t0 h q)) (tmRun M n (TCk.mk t0 (h + 1) q1)) := by
rw [(run_add M 1 (TCk.mk t0 h q) n).symm]
exact run_ext M _ _ (enter M t0 h q q1 hf) n
/-- Turning left off cell h + 1 back home. -/
theorem back (M : TMk) (t0 : Tape) (h : Nat) (q q1 : List Bool) (ht : Turn M q q1 (symAt M t0 (h + 1))) :
CEq M (tmRun M 1 (TCk.mk t0 (h + 1) q)) (TCk.mk t0 h q1) := by
refine And.intro ?_ (And.intro ?_ ?_)
· exact tapeN_keep M t0 (h + 1) q ht.wr
· show selN (M.right q (symAt M t0 (h + 1))) (h + 1 + 1) (h + 1 - 1) = h
rw [ht.rt]
rfl
· exact ht.nx
/-- A configuration agreeing with one at home with laid registers is related. -/
theorem sim_of (M : TMk) (A V : Nat -> Nat) (R h : Nat) (code : Nat -> List Bool) (c d : TCk)
(hc : CEq M c d) (hAV : forall i, i < R -> A i < M.k + 1 /\ V i < M.k + 1)
(pc : Nat) (x : Nat -> List Bool)
(hpos : d.pos = h) (hq : d.q = code pc) (hr : RegsAt d.tape A V R h x) :
Sim M A V R h code c (Cfg.mk pc x) := by
refine And.intro (hc.2.1.trans hpos) (And.intro (hc.2.2.trans hq) ?_)
intro i hi
exact lay_teq M d.tape c.tape (A i) (V i) hc.1 (hAV i hi).1 (hAV i hi).2 (rv (x i)) (h + 1) (hr i hi)
/-! ## Pnp52 at home cell h -/
theorem push_simH (M : TMk) (A V : Nat -> Nat) (R h : Nat) (x : Nat -> List Bool) (t0 : Tape)
(q r : List Bool) (rr : Nat) (b : Bool) (hR : rr < R)
(hAV : forall i, i < R -> A i < M.k + 1 /\ V i < M.k + 1)
(hl : RegsAt t0 A V R h x)
(hkeep : forall s, nthd s (A rr) = true -> Keep M q s true)
(hwa : nthd (M.write q (symAt M t0 (h + 1 + (rv (x rr)).length))) (A rr) = true)
(hwv : nthd (M.write q (symAt M t0 (h + 1 + (rv (x rr)).length))) (V rr) = b)
(hrt : M.right q (symAt M t0 (h + 1 + (rv (x rr)).length)) = false)
(hnx : M.next q (symAt M t0 (h + 1 + (rv (x rr)).length)) = r)
(hkr : forall s, nthd s (A rr) = true -> Keep M r s false)
(hoth : forall i, i < R -> Nat.beq i rr = false ->
nthd (M.write q (symAt M t0 (h + 1 + (rv (x rr)).length))) (A i) =
nthd (symAt M t0 (h + 1 + (rv (x rr)).length)) (A i) /\
nthd (M.write q (symAt M t0 (h + 1 + (rv (x rr)).length))) (V i) =
nthd (symAt M t0 (h + 1 + (rv (x rr)).length)) (V i)) :
RegsAt (tmRun M ((rv (x rr)).length + 1 + (rv (x rr)).length) (TCk.mk t0 (h + 1) q)).tape A V R h
(upd x rr (b :: x rr)) /\
(tmRun M ((rv (x rr)).length + 1 + (rv (x rr)).length) (TCk.mk t0 (h + 1) q)).pos = h /\
(tmRun M ((rv (x rr)).length + 1 + (rv (x rr)).length) (TCk.mk t0 (h + 1) q)).q = r := by
have hp := push_ret M t0 (A rr) (V rr) h q r b (rv (x rr)) (hAV rr hR).1 (hAV rr hR).2 (hl rr hR)
hkeep hwa hwv hrt hnx hkr
refine And.intro ?_ (And.intro hp.2.1 hp.2.2)
intro i hi
cases e : Nat.beq i rr with
| true =>
have ei : i = rr := Found.Pnp2.beq_eq i rr e
rw [ei, upd_same]
exact hp.1
| false =>
rw [upd_other x rr i _ e]
exact push_other M t0 (A rr) (V rr) h q r (rv (x rr)) (hAV rr hR).1 (hl rr hR) b (hAV rr hR).2
hkeep hwa hwv hrt hnx hkr (A i) (V i) (h + 1) (rv (x i)) (hAV i hi).1 (hAV i hi).2 (hl i hi)
(hoth i hi e).1 (hoth i hi e).2
theorem pop_empty_simH (M : TMk) (A V : Nat -> Nat) (R h : Nat) (x : Nat -> List Bool) (t0 : Tape)
(q e : List Bool) (rr : Nat) (hR : rr < R)
(hAV : forall i, i < R -> A i < M.k + 1 /\ V i < M.k + 1)
(hl : RegsAt t0 A V R h x) (hx : x rr = [])
(hturn : Turn M q e (symAt M t0 (h + 1))) :
RegsAt (tmRun M 1 (TCk.mk t0 (h + 1) q)).tape A V R h x /\
(tmRun M 1 (TCk.mk t0 (h + 1) q)).pos = h /\
(tmRun M 1 (TCk.mk t0 (h + 1) q)).q = e := by
have hrr : Lay t0 (A rr) (V rr) (h + 1) [] := by
have h' := hl rr hR
rw [hx] at h'
exact h'
have hp := pop_empty M t0 (A rr) (V rr) h q e (hAV rr hR).1 (hAV rr hR).2 hrr hturn
have hT : TEq M (tmRun M 1 (TCk.mk t0 (h + 1) q)).tape t0 := tapeN_keep M t0 (h + 1) q hturn.wr
refine And.intro ?_ (And.intro hp.2.1 hp.2.2)
intro i hi
exact lay_teq M _ _ (A i) (V i) hT (hAV i hi).1 (hAV i hi).2 (rv (x i)) (h + 1) (hl i hi)
theorem pop_cons_simH (M : TMk) (A V : Nat -> Nat) (R h : Nat) (x : Nat -> List Bool) (t0 : Tape)
(q q1 : List Bool) (rr : Nat) (b : Bool) (tl : List Bool) (hR : rr < R)
(hAV : forall i, i < R -> A i < M.k + 1 /\ V i < M.k + 1)
(hl : RegsAt t0 A V R h x) (hx : x rr = b :: tl)
(hkeep : forall s, nthd s (A rr) = true -> Keep M q s true)
(hturn : Turn M q q1 (symAt M t0 (h + 1 + (rv tl ++ [b]).length)))
(herase : nthd (M.write q1 (symAt M t0 (h + 1 + (rv tl).length))) (A rr) = false)
(hrt : M.right q1 (symAt M t0 (h + 1 + (rv tl).length)) = false)
(hkr : forall s, nthd s (A rr) = true -> Keep M (M.next q1 (symAt M t0 (h + 1 + (rv tl).length))) s false)
(hoth : forall i, i < R -> Nat.beq i rr = false ->
nthd (M.write q1 (symAt M t0 (h + 1 + (rv tl).length))) (A i) =
nthd (symAt M t0 (h + 1 + (rv tl).length)) (A i) /\
nthd (M.write q1 (symAt M t0 (h + 1 + (rv tl).length))) (V i) =
nthd (symAt M t0 (h + 1 + (rv tl).length)) (V i)) :
RegsAt (tmRun M ((rv tl ++ [b]).length + 1 + 1 + (rv tl).length) (TCk.mk t0 (h + 1) q)).tape A V R h
(upd x rr tl) /\
(tmRun M ((rv tl ++ [b]).length + 1 + 1 + (rv tl).length) (TCk.mk t0 (h + 1) q)).pos = h /\
(tmRun M ((rv tl ++ [b]).length + 1 + 1 + (rv tl).length) (TCk.mk t0 (h + 1) q)).q =
M.next q1 (symAt M t0 (h + 1 + (rv tl).length)) /\
nthd (symAt M t0 (h + 1 + (rv tl).length)) (V rr) = b := by
have hrr : Lay t0 (A rr) (V rr) (h + 1) (rv tl ++ [b]) := by
have h' := hl rr hR
rw [hx] at h'
exact h'
have hp := pop_ret M t0 (A rr) (V rr) h q q1 b (rv tl) (hAV rr hR).1 (hAV rr hR).2 hrr
hkeep hturn herase hrt hkr
refine And.intro ?_ (And.intro hp.2.1 (And.intro hp.2.2.1 hp.2.2.2))
intro i hi
cases e : Nat.beq i rr with
| true =>
have ei : i = rr := Found.Pnp2.beq_eq i rr e
rw [ei, upd_same]
exact hp.1
| false =>
rw [upd_other x rr i _ e]
exact pop_other M t0 (A rr) (V rr) h q q1 b (rv tl) (hAV rr hR).1 (hAV rr hR).2 hrr
hkeep hturn herase hrt hkr (A i) (V i) (h + 1) (rv (x i)) (hAV i hi).1 (hAV i hi).2 (hl i hi)
(hoth i hi e).1 (hoth i hi e).2
/-! ## One whole register machine step -/
/-- The jump target of a pop on a nonempty register: o on 1, z on 0. -/
def bsel : Bool -> Nat -> Nat -> Nat
| true, o, _ => o
| false, _, z => z
theorem pop_step_cons (p : List Ins) (pc : Nat) (x : Nat -> List Bool) (rr e z o : Nat) (b : Bool)
(tl : List Bool) (hins : nthI p pc = some (Ins.pop rr e z o)) (hx : x rr = b :: tl) :
step p (Cfg.mk pc x) = Cfg.mk (bsel b o z) (upd x rr tl) := by
show exec (Cfg.mk pc x) (nthI p pc) = _
rw [hins]
show popTo (Cfg.mk pc x) rr e z o (x rr) = _
rw [hx]
cases b with
| false => rfl
| true => rfl
theorem sim_push (M : TMk) (A V : Nat -> Nat) (R h : Nat) (code : Nat -> List Bool) (p : List Ins)
(pc : Nat) (x : Nat -> List Bool) (t0 : Tape) (q : List Bool) (rr : Nat) (b : Bool)
(hins : nthI p pc = some (Ins.push rr b)) (hR : rr < R)
(hAV : forall i, i < R -> A i < M.k + 1 /\ V i < M.k + 1)
(hl : RegsAt t0 A V R h x)
(hf : Fwd M (code pc) q (symAt M t0 h))
(hkeep : forall s, nthd s (A rr) = true -> Keep M q s true)
(hwa : nthd (M.write q (symAt M t0 (h + 1 + (rv (x rr)).length))) (A rr) = true)
(hwv : nthd (M.write q (symAt M t0 (h + 1 + (rv (x rr)).length))) (V rr) = b)
(hrt : M.right q (symAt M t0 (h + 1 + (rv (x rr)).length)) = false)
(hnx : M.next q (symAt M t0 (h + 1 + (rv (x rr)).length)) = code (pc + 1))
(hkr : forall s, nthd s (A rr) = true -> Keep M (code (pc + 1)) s false)
(hoth : forall i, i < R -> Nat.beq i rr = false ->
nthd (M.write q (symAt M t0 (h + 1 + (rv (x rr)).length))) (A i) =
nthd (symAt M t0 (h + 1 + (rv (x rr)).length)) (A i) /\
nthd (M.write q (symAt M t0 (h + 1 + (rv (x rr)).length))) (V i) =
nthd (symAt M t0 (h + 1 + (rv (x rr)).length)) (V i)) :
Sim M A V R h code
(tmRun M (1 + ((rv (x rr)).length + 1 + (rv (x rr)).length)) (TCk.mk t0 h (code pc)))
(step p (Cfg.mk pc x)) := by
have hs := push_simH M A V R h x t0 q (code (pc + 1)) rr b hR hAV hl hkeep hwa hwv hrt hnx hkr hoth
have hc := after_enter M t0 h (code pc) q hf ((rv (x rr)).length + 1 + (rv (x rr)).length)
have e : step p (Cfg.mk pc x) = Cfg.mk (pc + 1) (upd x rr (b :: x rr)) := by
show exec (Cfg.mk pc x) (nthI p pc) = _
rw [hins]
rfl
rw [e]
exact sim_of M A V R h code _ _ hc hAV (pc + 1) _ hs.2.1 hs.2.2 hs.1
theorem sim_pop_empty (M : TMk) (A V : Nat -> Nat) (R h : Nat) (code : Nat -> List Bool) (p : List Ins)
(pc : Nat) (x : Nat -> List Bool) (t0 : Tape) (q : List Bool) (rr e z o : Nat)
(hins : nthI p pc = some (Ins.pop rr e z o)) (hR : rr < R)
(hAV : forall i, i < R -> A i < M.k + 1 /\ V i < M.k + 1)
(hl : RegsAt t0 A V R h x) (hx : x rr = [])
(hf : Fwd M (code pc) q (symAt M t0 h))
(hturn : Turn M q (code e) (symAt M t0 (h + 1))) :
Sim M A V R h code (tmRun M (1 + 1) (TCk.mk t0 h (code pc))) (step p (Cfg.mk pc x)) := by
have hs := pop_empty_simH M A V R h x t0 q (code e) rr hR hAV hl hx hturn
have hc := after_enter M t0 h (code pc) q hf 1
have es : step p (Cfg.mk pc x) = Cfg.mk e x := by
show exec (Cfg.mk pc x) (nthI p pc) = _
rw [hins]
show popTo (Cfg.mk pc x) rr e z o (x rr) = _
rw [hx]
rfl
rw [es]
exact sim_of M A V R h code _ _ hc hAV e x hs.2.1 hs.2.2 hs.1
theorem sim_pop_cons (M : TMk) (A V : Nat -> Nat) (R h : Nat) (code : Nat -> List Bool) (p : List Ins)
(pc : Nat) (x : Nat -> List Bool) (t0 : Tape) (q q1 : List Bool) (rr e z o : Nat) (b : Bool)
(tl : List Bool)
(hins : nthI p pc = some (Ins.pop rr e z o)) (hR : rr < R)
(hAV : forall i, i < R -> A i < M.k + 1 /\ V i < M.k + 1)
(hl : RegsAt t0 A V R h x) (hx : x rr = b :: tl)
(hf : Fwd M (code pc) q (symAt M t0 h))
(hkeep : forall s, nthd s (A rr) = true -> Keep M q s true)
(hturn : Turn M q q1 (symAt M t0 (h + 1 + (rv tl ++ [b]).length)))
(herase : nthd (M.write q1 (symAt M t0 (h + 1 + (rv tl).length))) (A rr) = false)
(hrt : M.right q1 (symAt M t0 (h + 1 + (rv tl).length)) = false)
(hnx : M.next q1 (symAt M t0 (h + 1 + (rv tl).length)) = code (bsel b o z))
(hkr : forall s, nthd s (A rr) = true -> Keep M (code (bsel b o z)) s false)
(hoth : forall i, i < R -> Nat.beq i rr = false ->
nthd (M.write q1 (symAt M t0 (h + 1 + (rv tl).length))) (A i) =
nthd (symAt M t0 (h + 1 + (rv tl).length)) (A i) /\
nthd (M.write q1 (symAt M t0 (h + 1 + (rv tl).length))) (V i) =
nthd (symAt M t0 (h + 1 + (rv tl).length)) (V i)) :
Sim M A V R h code
(tmRun M (1 + ((rv tl ++ [b]).length + 1 + 1 + (rv tl).length)) (TCk.mk t0 h (code pc)))
(step p (Cfg.mk pc x)) := by
have hkr' : forall s, nthd s (A rr) = true -> Keep M (M.next q1 (symAt M t0 (h + 1 + (rv tl).length))) s false := by
rw [hnx]; exact hkr
have hs := pop_cons_simH M A V R h x t0 q q1 rr b tl hR hAV hl hx hkeep hturn herase hrt hkr' hoth
have hc := after_enter M t0 h (code pc) q hf ((rv tl ++ [b]).length + 1 + 1 + (rv tl).length)
rw [pop_step_cons p pc x rr e z o b tl hins hx]
exact sim_of M A V R h code _ _ hc hAV (bsel b o z) _ hs.2.1 (hs.2.2.1.trans hnx) hs.1
theorem sim_jmp (M : TMk) (A V : Nat -> Nat) (R h : Nat) (code : Nat -> List Bool) (p : List Ins)
(pc : Nat) (x : Nat -> List Bool) (t0 : Tape) (q : List Bool) (l : Nat)
(hins : nthI p pc = some (Ins.jmp l))
(hAV : forall i, i < R -> A i < M.k + 1 /\ V i < M.k + 1)
(hl : RegsAt t0 A V R h x)
(hf : Fwd M (code pc) q (symAt M t0 h))
(hturn : Turn M q (code l) (symAt M t0 (h + 1))) :
Sim M A V R h code (tmRun M (1 + 1) (TCk.mk t0 h (code pc))) (step p (Cfg.mk pc x)) := by
have hc := ceq_trans M _ _ _ (after_enter M t0 h (code pc) q hf 1) (back M t0 h q (code l) hturn)
have es : step p (Cfg.mk pc x) = Cfg.mk l x := by
show exec (Cfg.mk pc x) (nthI p pc) = _
rw [hins]
rfl
rw [es]
exact sim_of M A V R h code _ (TCk.mk t0 h (code l)) hc hAV l x rfl rfl hl
end Found.Pnp53
#print axioms Found.Pnp53.enter
#print axioms Found.Pnp53.after_enter
#print axioms Found.Pnp53.back
#print axioms Found.Pnp53.sim_of
#print axioms Found.Pnp53.push_simH
#print axioms Found.Pnp53.pop_empty_simH
#print axioms Found.Pnp53.pop_cons_simH
#print axioms Found.Pnp53.pop_step_cons
#print axioms Found.Pnp53.sim_push
#print axioms Found.Pnp53.sim_pop_empty
#print axioms Found.Pnp53.sim_pop_cons
#print axioms Found.Pnp53.sim_jmp
#print axioms: 12 theorems, no axioms.
If each simulated step costs at most \(e(f(d))\) and \(f\) grows by one per step, \(n\) steps cost at most \(n\,e(f(d)+n)\).
found/Pnp54.lean
/-
T330 piece 54. found/Pnp54 -- whole register machine runs on the track machine, with a
polynomial step count (CL3c, 12).
Pnp53 simulates one register machine step from a related configuration. Here the steps are
chained. The size of a register configuration is the total length of the registers below
R (regSz); one step of Pnp20 adds at most one to it. Proved, for any track machine M, any
relation S between track configurations and register configurations, any program p, any
size measure f and any step bound given as a polynomial expression e (Pnp3.PE):
* regSz_upd_le, len_le_regSz, step_sz, run_sz: one register step grows the size by at
most one, so n steps by at most n; each register below R is at most the size;
* run_sim: if every related pair (c, d) reaches a pair related to (., step p d) within
pe e (f d) machine steps, and f grows by at most one per step, then n register steps
are simulated within n * pe e (f d + n) machine steps -- a polynomial in n and f d;
* halt_ex: a halted register configuration is simulated in zero steps;
* push_cost, popc_cost, short_cost: the step counts of Pnp53 (sim_push, sim_pop_cons,
sim_pop_empty, sim_jmp) are at most pe stepE (regSz x R), stepE = 1 + (s + 1 + 1 + s).
It relativizes (a tool; PNP_MAP.md). Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp53
import found.Pnp3
namespace Found.Pnp54
open Found.Pnp3 (PE pe pe_mono)
open Found.Pnp20 (Ins Cfg upd upd_same upd_other nthI step exec popTo run)
open Found.Pnp43
open Found.Pnp44
open Found.Pnp52 (rv rv_len)
/-! ## Size of the registers below R -/
def regSz (x : Nat -> List Bool) : Nat -> Nat
| 0 => 0
| R + 1 => regSz x R + (x R).length
theorem regSz_upd_far (x : Nat -> List Bool) (r : Nat) (v : List Bool) :
forall R, R <= r -> regSz (upd x r v) R = regSz x R := by
intro R
induction R with
| zero => intro _; rfl
| succ R ih =>
intro hR
have hne : Nat.beq R r = false :=
Found.Pnp2.beq_false_of_ne R r (fun e => Nat.not_succ_le_self r (e ▸ hR))
show regSz (upd x r v) R + (upd x r v R).length = regSz x R + (x R).length
rw [ih (Nat.le_of_succ_le hR), upd_other x r R v hne]
theorem regSz_upd_le (x : Nat -> List Bool) (r : Nat) (v : List Bool)
(hv : v.length <= (x r).length + 1) :
forall R, regSz (upd x r v) R <= regSz x R + 1 := by
intro R
induction R with
| zero => exact Nat.zero_le _
| succ R ih =>
show regSz (upd x r v) R + (upd x r v R).length <= regSz x R + (x R).length + 1
cases e : Nat.beq R r with
| true =>
have eR : R = r := Found.Pnp2.beq_eq R r e
subst eR
rw [regSz_upd_far x R v R (Nat.le_refl R), upd_same, Nat.add_assoc]
exact Nat.add_le_add_left hv _
| false =>
rw [upd_other x r R v e, Nat.add_right_comm (regSz x R) (x R).length 1]
exact Nat.add_le_add_right ih _
theorem len_le_regSz (x : Nat -> List Bool) (r : Nat) :
forall R, r < R -> (x r).length <= regSz x R := by
intro R
induction R with
| zero => intro h; exact absurd h (Nat.not_succ_le_zero r)
| succ R ih =>
intro h
show (x r).length <= regSz x R + (x R).length
have hle : r <= R := Nat.le_of_lt_succ h
cases hle with
| refl => exact Nat.le_add_left _ _
| step hm => exact Nat.le_trans (ih (Nat.succ_le_succ hm)) (Nat.le_add_right _ _)
theorem step_sz (p : List Ins) (R : Nat) (d : Cfg) :
regSz (step p d).regs R <= regSz d.regs R + 1 := by
cases d with
| mk pc x =>
show regSz (exec (Cfg.mk pc x) (nthI p pc)).regs R <= regSz x R + 1
cases nthI p pc with
| none => exact Nat.le_succ _
| some ins =>
cases ins with
| push r b =>
exact regSz_upd_le x r (b :: x r) (Nat.le_refl _) R
| pop r e z o =>
show regSz (popTo (Cfg.mk pc x) r e z o (x r)).regs R <= regSz x R + 1
cases hx : x r with
| nil => exact Nat.le_succ _
| cons b t =>
have ht : t.length <= (x r).length + 1 := by
rw [hx]
exact Nat.le_trans (Nat.le_succ t.length) (Nat.le_succ (t.length + 1))
cases b with
| false => exact regSz_upd_le x r t ht R
| true => exact regSz_upd_le x r t ht R
| jmp l => exact Nat.le_succ _
theorem run_sz (p : List Ins) (R : Nat) :
forall n d, regSz (run p n d).regs R <= regSz d.regs R + n := by
intro n
induction n with
| zero => intro d; exact Nat.le_refl _
| succ n ih =>
intro d
show regSz (run p n (step p d)).regs R <= regSz d.regs R + (n + 1)
refine Nat.le_trans (ih (step p d)) ?_
refine Nat.le_trans (Nat.add_le_add_right (step_sz p R d) n) (Nat.le_of_eq ?_)
rw [Nat.add_assoc, Nat.add_comm 1 n]
/-! ## Chaining simulated steps -/
theorem run_sim (M : TMk) (S : TCk -> Cfg -> Prop) (p : List Ins) (f : Cfg -> Nat) (e : PE)
(hsz : forall d, f (step p d) <= f d + 1)
(hst : forall c d, S c d -> Exists (fun k => k <= pe e (f d) /\ S (tmRun M k c) (step p d))) :
forall n c d, S c d ->
Exists (fun K => K <= n * pe e (f d + n) /\ S (tmRun M K c) (run p n d)) := by
intro n
induction n with
| zero => intro c d h; exact Exists.intro 0 (And.intro (Nat.zero_le _) h)
| succ n ih =>
intro c d h
cases hst c d h with
| intro k hk =>
cases ih (tmRun M k c) (step p d) hk.2 with
| intro K hK =>
refine Exists.intro (k + K) (And.intro ?_ ?_)
· have hX : f (step p d) + n <= f d + (n + 1) := by
refine Nat.le_trans (Nat.add_le_add_right (hsz d) n) (Nat.le_of_eq ?_)
rw [Nat.add_assoc, Nat.add_comm 1 n]
have h1 : k <= pe e (f d + (n + 1)) :=
Nat.le_trans hk.1 (pe_mono _ _ (Nat.le_add_right _ _) e)
have h2 : K <= n * pe e (f d + (n + 1)) :=
Nat.le_trans hK.1 (Nat.mul_le_mul (Nat.le_refl n) (pe_mono _ _ hX e))
refine Nat.le_trans (Nat.add_le_add h1 h2) (Nat.le_of_eq ?_)
rw [Nat.succ_mul, Nat.add_comm]
· rw [(Found.Pnp46.run_add M k c K).symm]
exact hK.2
theorem halt_ex (M : TMk) (S : TCk -> Cfg -> Prop) (p : List Ins) (B : Nat) (d : Cfg)
(tc : TCk) (h : S tc d) (hn : nthI p d.pc = none) :
Exists (fun k => k <= B /\ S (tmRun M k tc) (step p d)) := by
refine Exists.intro 0 (And.intro (Nat.zero_le B) ?_)
have e : step p d = d := by
cases d with
| mk pc x =>
show exec (Cfg.mk pc x) (nthI p pc) = Cfg.mk pc x
have hn' : nthI p pc = none := hn
rw [hn']
rfl
rw [e]
exact h
/-! ## The step counts of Pnp53 -/
/-- The bound for one register step: 1 + (s + 1 + 1 + s). -/
def stepE : PE := .add (.c 1) (.add (.add (.add .x (.c 1)) (.c 1)) .x)
theorem push_cost (x : Nat -> List Bool) (R rr : Nat) (hR : rr < R) :
1 + ((rv (x rr)).length + 1 + (rv (x rr)).length) <= pe stepE (regSz x R) := by
have hL : (rv (x rr)).length <= regSz x R := by
rw [rv_len]; exact len_le_regSz x rr R hR
show 1 + ((rv (x rr)).length + 1 + (rv (x rr)).length) <= 1 + (regSz x R + 1 + 1 + regSz x R)
refine Nat.add_le_add_left (Nat.add_le_add ?_ hL) 1
exact Nat.le_trans (Nat.add_le_add_right hL 1) (Nat.le_succ _)
theorem popc_cost (x : Nat -> List Bool) (R rr : Nat) (hR : rr < R) (b : Bool) (tl : List Bool)
(hx : x rr = b :: tl) :
1 + ((rv tl ++ [b]).length + 1 + 1 + (rv tl).length) <= pe stepE (regSz x R) := by
have hL : tl.length + 1 <= regSz x R := by
have h := len_le_regSz x rr R hR
rw [hx] at h
exact h
have h1 : (rv tl ++ [b]).length <= regSz x R := by
show (rv (b :: tl)).length <= regSz x R
rw [rv_len]; exact hL
have h2 : (rv tl).length <= regSz x R := by
rw [rv_len]; exact Nat.le_trans (Nat.le_succ _) hL
show 1 + ((rv tl ++ [b]).length + 1 + 1 + (rv tl).length) <= 1 + (regSz x R + 1 + 1 + regSz x R)
exact Nat.add_le_add_left (Nat.add_le_add (Nat.add_le_add_right (Nat.add_le_add_right h1 1) 1) h2) 1
theorem short_cost (s : Nat) : 1 + 1 <= pe stepE s := by
show 1 + 1 <= 1 + (s + 1 + 1 + s)
refine Nat.add_le_add_left ?_ 1
exact Nat.le_trans (Nat.le_add_left 1 s) (Nat.le_trans (Nat.le_succ _) (Nat.le_add_right _ s))
end Found.Pnp54
#print axioms Found.Pnp54.regSz_upd_far
#print axioms Found.Pnp54.regSz_upd_le
#print axioms Found.Pnp54.len_le_regSz
#print axioms Found.Pnp54.step_sz
#print axioms Found.Pnp54.run_sz
#print axioms Found.Pnp54.run_sim
#print axioms Found.Pnp54.halt_ex
#print axioms Found.Pnp54.push_cost
#print axioms Found.Pnp54.popc_cost
#print axioms Found.Pnp54.short_cost
#print axioms: 10 theorems, no axioms.
Setting one bit of a symbol, the test for a present track, and marker bits that no write changes stay along runs.
found/Pnp55.lean
/-
T330 piece 55. found/Pnp55 -- tools for building the track machine of a register program
(CL3c, 13).
The track machine that runs a register program (Pnp56) writes single bits of a cell and
tests whether any register track is set. It also needs two facts about whole runs: marker
tracks that no write touches stay as they are, and a cell carrying a marker bit on which
every write is the identity keeps all its bits. Proved, for any machine M:
* setd, nthd_setd_same, nthd_setd_other: setting bit i of a symbol changes bit i only;
* anyT, anyT_true, anyT_false: the test "some track A i, i < R, is set";
* run_bit: if no write changes bit a, bit a of every cell is constant along a run;
* run_marked: if no write changes bit mk, and every write on a symbol with bit mk set
leaves bit a, then bit a of every cell marked mk is constant along a run.
It relativizes (a tool; PNP_MAP.md). Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp53
namespace Found.Pnp55
open Found.Pnp7 (nthd)
open Found.Pnp43
open Found.Pnp44
/-! ## Setting one bit of a symbol -/
def setd : List Bool -> Nat -> Bool -> List Bool
| [], 0, b => [b]
| [], i + 1, b => false :: setd [] i b
| _ :: L, 0, b => b :: L
| x :: L, i + 1, b => x :: setd L i b
theorem nthd_setd_same : forall (L : List Bool) (i : Nat) (b : Bool), nthd (setd L i b) i = b := by
intro L
induction L with
| nil =>
intro i
induction i with
| zero => intro b; rfl
| succ i ih => intro b; exact ih b
| cons x L ih =>
intro i b
cases i with
| zero => rfl
| succ i => exact ih i b
theorem nthd_nil_setd : forall (i j : Nat) (b : Bool), Nat.beq j i = false -> nthd (setd [] i b) j = false := by
intro i
induction i with
| zero =>
intro j b h
cases j with
| zero => exact Bool.noConfusion h
| succ j => rfl
| succ i ih =>
intro j b h
cases j with
| zero => rfl
| succ j => exact ih j b h
theorem nthd_setd_other : forall (L : List Bool) (i j : Nat) (b : Bool), Nat.beq j i = false ->
nthd (setd L i b) j = nthd L j := by
intro L
induction L with
| nil => intro i j b h; exact nthd_nil_setd i j b h
| cons x L ih =>
intro i j b h
cases i with
| zero =>
cases j with
| zero => exact Bool.noConfusion h
| succ j => rfl
| succ i =>
cases j with
| zero => rfl
| succ j => exact ih i j b h
/-! ## Some register track set -/
def anyT (s : List Bool) (A : Nat -> Nat) : Nat -> Bool
| 0 => false
| R + 1 => anyT s A R || nthd s (A R)
theorem or_true_r (a : Bool) : (a || true) = true := by
cases a with
| false => rfl
| true => rfl
theorem anyT_true (s : List Bool) (A : Nat -> Nat) (rr : Nat) (hs : nthd s (A rr) = true) :
forall R, rr < R -> anyT s A R = true := by
intro R
induction R with
| zero => intro h; exact absurd h (Nat.not_succ_le_zero rr)
| succ R ih =>
intro h
show (anyT s A R || nthd s (A R)) = true
have hle : rr <= R := Nat.le_of_lt_succ h
cases hle with
| refl => rw [hs]; exact or_true_r _
| step hm => rw [ih (Nat.succ_le_succ hm)]; rfl
theorem anyT_false (s : List Bool) (A : Nat -> Nat) :
forall R, (forall i, i < R -> nthd s (A i) = false) -> anyT s A R = false := by
intro R
induction R with
| zero => intro _; rfl
| succ R ih =>
intro h
show (anyT s A R || nthd s (A R)) = false
rw [ih (fun i hi => h i (Nat.le_trans hi (Nat.le_succ R))), h R (Nat.le_refl _)]
rfl
/-! ## Bits kept along runs -/
theorem step_bit (M : TMk) (a : Nat) (ha : a < M.k + 1)
(hw : forall q s, nthd (M.write q s) a = nthd s a) (c : TCk) (j : Nat) :
(tmStep M c).tape j a = c.tape j a := by
show Found.Pnp12.sel (Nat.beq j c.pos) (nthd (M.write c.q (symAt M c.tape c.pos)) a) (c.tape j a) = c.tape j a
cases e : Nat.beq j c.pos with
| true =>
have ej : j = c.pos := Found.Pnp2.beq_eq j c.pos e
rw [ej, hw, Found.Pnp47.nthd_sym M c.tape c.pos a ha]
rfl
| false => rfl
theorem run_bit (M : TMk) (a : Nat) (ha : a < M.k + 1)
(hw : forall q s, nthd (M.write q s) a = nthd s a) :
forall n c j, (tmRun M n c).tape j a = c.tape j a := by
intro n
induction n with
| zero => intro c j; rfl
| succ n ih =>
intro c j
show (tmStep M (tmRun M n c)).tape j a = c.tape j a
rw [step_bit M a ha hw]
exact ih c j
theorem run_marked (M : TMk) (mk a : Nat) (hmk : mk < M.k + 1) (ha : a < M.k + 1)
(hwm : forall q s, nthd (M.write q s) mk = nthd s mk)
(hw : forall q s, nthd s mk = true -> nthd (M.write q s) a = nthd s a) :
forall n c j, c.tape j mk = true -> (tmRun M n c).tape j a = c.tape j a := by
intro n
induction n with
| zero => intro c j _; rfl
| succ n ih =>
intro c j hj
show Found.Pnp12.sel (Nat.beq j (tmRun M n c).pos)
(nthd (M.write (tmRun M n c).q (symAt M (tmRun M n c).tape (tmRun M n c).pos)) a)
((tmRun M n c).tape j a) = c.tape j a
cases e : Nat.beq j (tmRun M n c).pos with
| true =>
have ej : j = (tmRun M n c).pos := Found.Pnp2.beq_eq j _ e
have hm : nthd (symAt M (tmRun M n c).tape (tmRun M n c).pos) mk = true := by
rw [Found.Pnp47.nthd_sym M _ _ mk hmk, ej.symm, run_bit M mk hmk hwm n c j]
exact hj
rw [hw _ _ hm, Found.Pnp47.nthd_sym M _ _ a ha, ej.symm]
exact ih c j hj
| false => exact ih c j hj
end Found.Pnp55
#print axioms Found.Pnp55.nthd_setd_same
#print axioms Found.Pnp55.nthd_nil_setd
#print axioms Found.Pnp55.nthd_setd_other
#print axioms Found.Pnp55.or_true_r
#print axioms Found.Pnp55.anyT_true
#print axioms Found.Pnp55.anyT_false
#print axioms Found.Pnp55.step_bit
#print axioms Found.Pnp55.run_bit
#print axioms Found.Pnp55.run_marked
#print axioms: 9 theorems, no axioms.
A concrete track machine built from a register program: thermometer-coded counter, home and first-cell marks.
found/Pnp56.lean
/-
T330 piece 56. found/Pnp56 -- the track machine of a register program (CL3c, 14).
From a register program p (Pnp20), a register count R and a counter width W, a concrete
track machine mach p R W is built. Tracks: 0 marks the home cell, 1 marks the first
register cell, register i lies on tracks trA i = i + 2 (present) and trV R i = i + R + 2
(value). States are two tag bits and the counter in thermometer code thm W pc (fixed width
W, so every state has W + 2 bits):
* code pc (tags 0 0): at home and on the way back; any present bit set: stay, go left;
none set: go right into ist pc;
* ist pc (tags 1 0): the instruction pc. push r b: run right over present cells, write
the first absent cell (present, value b), go left in code (pc + 1). pop r e z o: run
right over present cells; on the first absent cell turn left, into code e if it is
the first register cell (empty register), into qst pc otherwise. jmp l: turn left into
code l;
* qst pc (tags 0 1): pop's second phase: clear the present bit, go left into code o or
code z by the value bit.
On a cell with the home mark every write is the identity. Proved: the behaviour of each
state on symbols, in the forms used by Pnp53 (Fwd, Keep, Turn, the bits written), and
that writes never change the two mark tracks (marks_kept, home_fixed).
It relativizes (a tool; PNP_MAP.md). Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp55
import found.Pnp54
namespace Found.Pnp56
open Found.Pnp7 (nthd)
open Found.Pnp20 (Ins Cfg selR nthI)
open Found.Pnp43
open Found.Pnp44
open Found.Pnp45 (Keep)
open Found.Pnp48 (Turn)
open Found.Pnp53 (Fwd bsel)
open Found.Pnp55 (setd nthd_setd_same nthd_setd_other anyT anyT_true anyT_false)
/-! ## Counter code -/
def thm : Nat -> Nat -> List Bool
| 0, _ => []
| W + 1, 0 => false :: thm W 0
| W + 1, n + 1 => true :: thm W n
def cnt : List Bool -> Nat
| [] => 0
| false :: _ => 0
| true :: l => cnt l + 1
theorem cnt_thm : forall W n, n <= W -> cnt (thm W n) = n := by
intro W
induction W with
| zero =>
intro n h
cases n with
| zero => rfl
| succ n => exact absurd h (Nat.not_succ_le_zero n)
| succ W ih =>
intro n h
cases n with
| zero => rfl
| succ n =>
show cnt (thm W n) + 1 = n + 1
rw [ih n (Nat.le_of_succ_le_succ h)]
theorem thm_len : forall W n, (thm W n).length = W := by
intro W
induction W with
| zero => intro n; rfl
| succ W ih =>
intro n
cases n with
| zero => show (thm W 0).length + 1 = W + 1; rw [ih 0]
| succ n => show (thm W n).length + 1 = W + 1; rw [ih n]
theorem nthI_lt : forall (p : List Ins) (n : Nat) (i : Ins), nthI p n = some i -> n < p.length := by
intro p
induction p with
| nil =>
intro n i h
have h' : (none : Option Ins) = some i := h
cases h'
| cons a p ih =>
intro n i h
cases n with
| zero => exact Nat.succ_le_succ (Nat.zero_le _)
| succ n => exact Nat.succ_le_succ (ih n i h)
/-! ## Tracks and states -/
def trA (i : Nat) : Nat := i + 2
def trV (R i : Nat) : Nat := i + R + 2
def code (W pc : Nat) : List Bool := false :: false :: thm W pc
def ist (W pc : Nat) : List Bool := true :: false :: thm W pc
def qst (W pc : Nat) : List Bool := false :: true :: thm W pc
def iNext (W : Nat) (c s : List Bool) : Option Ins -> List Bool
| none => true :: false :: c
| some (.push rr _) => selR (nthd s (trA rr)) (true :: false :: c) (code W (cnt c + 1))
| some (.pop rr e _ _) =>
selR (nthd s (trA rr)) (true :: false :: c) (selR (nthd s 1) (code W e) (false :: true :: c))
| some (.jmp l) => code W l
def qNext (R W : Nat) (c s : List Bool) : Option Ins -> List Bool
| none => false :: true :: c
| some (.push _ _) => false :: true :: c
| some (.pop rr _ z o) => code W (bsel (nthd s (trV R rr)) o z)
| some (.jmp _) => false :: true :: c
def nxt (p : List Ins) (R W : Nat) : List Bool -> List Bool -> List Bool
| [], _ => []
| [x], _ => [x]
| false :: false :: c, s => selR (anyT s trA R) (false :: false :: c) (true :: false :: c)
| true :: false :: c, s => iNext W c s (nthI p (cnt c))
| false :: true :: c, s => qNext R W c s (nthI p (cnt c))
| true :: true :: c, _ => true :: true :: c
def iWr (R : Nat) (s : List Bool) : Option Ins -> List Bool
| none => s
| some (.push rr b) => selR (nthd s (trA rr)) s (setd (setd s (trA rr) true) (trV R rr) b)
| some (.pop _ _ _ _) => s
| some (.jmp _) => s
def qWr (s : List Bool) : Option Ins -> List Bool
| none => s
| some (.push _ _) => s
| some (.pop rr _ _ _) => setd s (trA rr) false
| some (.jmp _) => s
def wr0 (p : List Ins) (R : Nat) : List Bool -> List Bool -> List Bool
| [], s => s
| [_], s => s
| false :: false :: _, s => s
| true :: false :: c, s => iWr R s (nthI p (cnt c))
| false :: true :: c, s => qWr s (nthI p (cnt c))
| true :: true :: _, s => s
def wrt (p : List Ins) (R : Nat) (q s : List Bool) : List Bool := selR (nthd s 0) s (wr0 p R q s)
def iRt (s : List Bool) : Option Ins -> Bool
| none => false
| some (.push rr _) => nthd s (trA rr)
| some (.pop rr _ _ _) => nthd s (trA rr)
| some (.jmp _) => false
def rt (p : List Ins) (R : Nat) : List Bool -> List Bool -> Bool
| [], _ => false
| [_], _ => false
| false :: false :: _, s => !(anyT s trA R)
| true :: false :: c, s => iRt s (nthI p (cnt c))
| false :: true :: _, _ => false
| true :: true :: _, _ => false
def mach (p : List Ins) (R W : Nat) : TMk :=
TMk.mk (W + 2) (R + R + 1) (nxt p R W) (wrt p R) (rt p R)
/-! ## Small facts -/
theorem selR_same (b : Bool) (v : List Bool) : selR b v v = v := by
cases b with
| false => rfl
| true => rfl
theorem beq_add_r (i j : Nat) : forall R, Nat.beq (i + R) (j + R) = Nat.beq i j := by
intro R
induction R with
| zero => rfl
| succ R ih => exact ih
theorem beq_lt (a b : Nat) (h : a < b) : Nat.beq a b = false :=
Found.Pnp2.beq_false_of_ne a b (fun e => Nat.not_succ_le_self b (e ▸ h))
theorem beq_gt (a b : Nat) (h : b < a) : Nat.beq a b = false :=
Found.Pnp2.beq_false_of_ne a b (fun e => Nat.not_succ_le_self a (e.symm ▸ h))
theorem lt_addR (i R : Nat) (rr : Nat) (h : i < R) : i < rr + R :=
Nat.lt_of_lt_of_le h (Nat.le_add_left R rr)
/-- Tracks of different registers, or present and value tracks, differ. -/
theorem bAV (R i rr : Nat) (hi : i < R) : Nat.beq (trA i) (trV R rr) = false :=
beq_lt i (rr + R) (lt_addR i R rr hi)
theorem bVA (R i rr : Nat) (hr : rr < R) : Nat.beq (trV R i) (trA rr) = false :=
beq_gt (i + R) rr (lt_addR rr R i hr)
theorem bAA (i rr : Nat) (h : Nat.beq i rr = false) : Nat.beq (trA i) (trA rr) = false := h
theorem bVV (R i rr : Nat) (h : Nat.beq i rr = false) : Nat.beq (trV R i) (trV R rr) = false := by
show Nat.beq (i + R) (rr + R) = false
rw [beq_add_r]
exact h
theorem trA_lt (R i : Nat) (hi : i < R) : trA i < (mach [] R 0).k + 1 :=
Nat.succ_le_succ (Nat.succ_le_succ (Nat.le_trans hi (Nat.le_add_left R R)))
theorem trV_lt (R i : Nat) (hi : i < R) : trV R i < (mach [] R 0).k + 1 :=
Nat.succ_le_succ (Nat.succ_le_succ
(Nat.le_trans (Nat.le_of_eq (Nat.add_right_comm i R 1)) (Nat.add_le_add_right hi R)))
theorem k_eq (p : List Ins) (R W : Nat) : (mach p R W).k = R + R + 1 := rfl
/-! ## Writes and marks -/
theorem wr0_keep (p : List Ins) (R : Nat) (j : Nat) (hA : forall r, Nat.beq j (trA r) = false)
(hV : forall r, Nat.beq j (trV R r) = false) :
forall q s, nthd (wr0 p R q s) j = nthd s j := by
intro q s
match q with
| [] => rfl
| [_] => rfl
| false :: false :: _ => rfl
| true :: true :: _ => rfl
| true :: false :: c =>
show nthd (iWr R s (nthI p (cnt c))) j = nthd s j
cases nthI p (cnt c) with
| none => rfl
| some ins =>
cases ins with
| push rr b =>
show nthd (selR (nthd s (trA rr)) s (setd (setd s (trA rr) true) (trV R rr) b)) j = nthd s j
cases nthd s (trA rr) with
| true => rfl
| false =>
show nthd (setd (setd s (trA rr) true) (trV R rr) b) j = nthd s j
rw [nthd_setd_other _ _ _ _ (hV rr), nthd_setd_other _ _ _ _ (hA rr)]
| pop rr e z o => rfl
| jmp l => rfl
| false :: true :: c =>
show nthd (qWr s (nthI p (cnt c))) j = nthd s j
cases nthI p (cnt c) with
| none => rfl
| some ins =>
cases ins with
| push rr b => rfl
| pop rr e z o =>
show nthd (setd s (trA rr) false) j = nthd s j
exact nthd_setd_other _ _ _ _ (hA rr)
| jmp l => rfl
theorem wrt_bit (p : List Ins) (R : Nat) (j : Nat) (hA : forall r, Nat.beq j (trA r) = false)
(hV : forall r, Nat.beq j (trV R r) = false) (q s : List Bool) :
nthd (wrt p R q s) j = nthd s j := by
show nthd (selR (nthd s 0) s (wr0 p R q s)) j = nthd s j
cases nthd s 0 with
| true => rfl
| false => exact wr0_keep p R j hA hV q s
/-- The two mark tracks are never written. -/
theorem marks_kept (p : List Ins) (R W : Nat) (q s : List Bool) :
nthd ((mach p R W).write q s) 0 = nthd s 0 /\ nthd ((mach p R W).write q s) 1 = nthd s 1 :=
And.intro (wrt_bit p R 0 (fun _ => rfl) (fun _ => rfl) q s)
(wrt_bit p R 1 (fun _ => rfl) (fun _ => rfl) q s)
/-- On a home-marked symbol every write is the identity. -/
theorem home_fixed (p : List Ins) (R W : Nat) (q s : List Bool) (h : nthd s 0 = true) :
(mach p R W).write q s = s := by
show selR (nthd s 0) s (wr0 p R q s) = s
rw [h]
rfl
theorem wrt_id_of (p : List Ins) (R : Nat) (q s : List Bool) (h : wr0 p R q s = s) :
wrt p R q s = s := by
show selR (nthd s 0) s (wr0 p R q s) = s
rw [h]
exact selR_same _ s
/-! ## The home and return state -/
theorem fwd_code (p : List Ins) (R W pc : Nat) (s : List Bool) (h : anyT s trA R = false) :
Fwd (mach p R W) (code W pc) (ist W pc) s := by
refine Fwd.mk ?_ (wrt_id_of p R _ s rfl) ?_
· show selR (anyT s trA R) (code W pc) (ist W pc) = ist W pc
rw [h]; rfl
· show (!(anyT s trA R)) = true
rw [h]; rfl
theorem keep_code (p : List Ins) (R W pc rr : Nat) (hr : rr < R) (s : List Bool)
(hs : nthd s (trA rr) = true) : Keep (mach p R W) (code W pc) s false := by
have ha : anyT s trA R = true := anyT_true s trA rr hs R hr
refine Keep.mk ?_ (wrt_id_of p R _ s rfl) ?_
· show selR (anyT s trA R) (code W pc) (ist W pc) = code W pc
rw [ha]; rfl
· show (!(anyT s trA R)) = false
rw [ha]; rfl
/-! ## The instruction state -/
theorem ist_look (p : List Ins) (W pc : Nat) (i : Ins) (hp : p.length <= W) (hi : nthI p pc = some i) :
nthI p (cnt (thm W pc)) = some i := by
rw [cnt_thm W pc (Nat.le_trans (Nat.le_of_lt (nthI_lt p pc i hi)) hp)]
exact hi
theorem keep_ist (p : List Ins) (R W pc rr : Nat) (i : Ins) (hp : p.length <= W)
(hi : nthI p pc = some i) (hrr : forall s, iRt s (some i) = nthd s (trA rr))
(hnx : forall s, nthd s (trA rr) = true -> iNext W (thm W pc) s (some i) = ist W pc)
(hwr : forall s, nthd s (trA rr) = true -> iWr R s (some i) = s)
(s : List Bool) (hs : nthd s (trA rr) = true) : Keep (mach p R W) (ist W pc) s true := by
have hl := ist_look p W pc i hp hi
refine Keep.mk ?_ ?_ ?_
· show iNext W (thm W pc) s (nthI p (cnt (thm W pc))) = ist W pc
rw [hl]; exact hnx s hs
· apply wrt_id_of
show iWr R s (nthI p (cnt (thm W pc))) = s
rw [hl]; exact hwr s hs
· show iRt s (nthI p (cnt (thm W pc))) = true
rw [hl, hrr s]; exact hs
theorem keep_push (p : List Ins) (R W pc rr : Nat) (b : Bool) (hp : p.length <= W)
(hi : nthI p pc = some (Ins.push rr b)) (s : List Bool) (hs : nthd s (trA rr) = true) :
Keep (mach p R W) (ist W pc) s true :=
keep_ist p R W pc rr _ hp hi (fun _ => rfl)
(fun s hs => by show selR (nthd s (trA rr)) _ _ = _; rw [hs]; rfl)
(fun s hs => by show selR (nthd s (trA rr)) _ _ = _; rw [hs]; rfl) s hs
theorem keep_pop (p : List Ins) (R W pc rr e z o : Nat) (hp : p.length <= W)
(hi : nthI p pc = some (Ins.pop rr e z o)) (s : List Bool) (hs : nthd s (trA rr) = true) :
Keep (mach p R W) (ist W pc) s true :=
keep_ist p R W pc rr _ hp hi (fun _ => rfl)
(fun s hs => by show selR (nthd s (trA rr)) _ _ = _; rw [hs]; rfl)
(fun _ _ => rfl) s hs
/-- push at the first absent cell (not home): the bits written, the turn, the next state. -/
theorem push_write (p : List Ins) (R W pc rr : Nat) (b : Bool) (hp : p.length <= W) (hR : rr < R)
(hi : nthI p pc = some (Ins.push rr b)) (s : List Bool) (hs : nthd s (trA rr) = false)
(h0 : nthd s 0 = false) :
(mach p R W).write (ist W pc) s = setd (setd s (trA rr) true) (trV R rr) b /\
(mach p R W).right (ist W pc) s = false /\
(mach p R W).next (ist W pc) s = code W (pc + 1) /\
nthd (setd (setd s (trA rr) true) (trV R rr) b) (trA rr) = true /\
nthd (setd (setd s (trA rr) true) (trV R rr) b) (trV R rr) = b /\
(forall i, i < R -> Nat.beq i rr = false ->
nthd (setd (setd s (trA rr) true) (trV R rr) b) (trA i) = nthd s (trA i) /\
nthd (setd (setd s (trA rr) true) (trV R rr) b) (trV R i) = nthd s (trV R i)) := by
have hl := ist_look p W pc _ hp hi
have hpc : pc <= W := Nat.le_trans (Nat.le_of_lt (nthI_lt p pc _ hi)) hp
refine And.intro ?_ (And.intro ?_ (And.intro ?_ (And.intro ?_ (And.intro ?_ ?_))))
· show selR (nthd s 0) s (wr0 p R (ist W pc) s) = _
rw [h0]
show iWr R s (nthI p (cnt (thm W pc))) = _
rw [hl]
show selR (nthd s (trA rr)) s _ = _
rw [hs]; rfl
· show iRt s (nthI p (cnt (thm W pc))) = false
rw [hl]; exact hs
· show iNext W (thm W pc) s (nthI p (cnt (thm W pc))) = code W (pc + 1)
rw [hl]
show selR (nthd s (trA rr)) _ (code W (cnt (thm W pc) + 1)) = _
rw [hs, cnt_thm W pc hpc]; rfl
· rw [nthd_setd_other _ _ _ _ (bAV R rr rr hR)]
exact nthd_setd_same _ _ _
· exact nthd_setd_same _ _ _
· intro i hiR hne
refine And.intro ?_ ?_
· rw [nthd_setd_other _ _ _ _ (bAV R i rr hiR), nthd_setd_other _ _ _ _ (bAA i rr hne)]
· rw [nthd_setd_other _ _ _ _ (bVV R i rr hne), nthd_setd_other _ _ _ _ (bVA R i rr hR)]
/-- pop on the first absent cell: the turn into code e (first cell) or qst pc (later cell). -/
theorem pop_turn_empty (p : List Ins) (R W pc rr e z o : Nat) (hp : p.length <= W)
(hi : nthI p pc = some (Ins.pop rr e z o)) (s : List Bool) (hs : nthd s (trA rr) = false)
(hf : nthd s 1 = true) : Turn (mach p R W) (ist W pc) (code W e) s := by
have hl := ist_look p W pc _ hp hi
refine Turn.mk ?_ ?_ ?_
· show iNext W (thm W pc) s (nthI p (cnt (thm W pc))) = code W e
rw [hl]
show selR (nthd s (trA rr)) _ (selR (nthd s 1) (code W e) _) = _
rw [hs, hf]; rfl
· apply wrt_id_of
show iWr R s (nthI p (cnt (thm W pc))) = s
rw [hl]; rfl
· show iRt s (nthI p (cnt (thm W pc))) = false
rw [hl]; exact hs
theorem pop_turn_cons (p : List Ins) (R W pc rr e z o : Nat) (hp : p.length <= W)
(hi : nthI p pc = some (Ins.pop rr e z o)) (s : List Bool) (hs : nthd s (trA rr) = false)
(hf : nthd s 1 = false) : Turn (mach p R W) (ist W pc) (qst W pc) s := by
have hl := ist_look p W pc _ hp hi
refine Turn.mk ?_ ?_ ?_
· show iNext W (thm W pc) s (nthI p (cnt (thm W pc))) = qst W pc
rw [hl]
show selR (nthd s (trA rr)) _ (selR (nthd s 1) (code W e) _) = _
rw [hs, hf]; rfl
· apply wrt_id_of
show iWr R s (nthI p (cnt (thm W pc))) = s
rw [hl]; rfl
· show iRt s (nthI p (cnt (thm W pc))) = false
rw [hl]; exact hs
/-- pop's second state on the last present cell (not home). -/
theorem pop_erase (p : List Ins) (R W pc rr e z o : Nat) (hp : p.length <= W) (hR : rr < R)
(hi : nthI p pc = some (Ins.pop rr e z o)) (s : List Bool) (h0 : nthd s 0 = false) :
(mach p R W).write (qst W pc) s = setd s (trA rr) false /\
(mach p R W).right (qst W pc) s = false /\
(mach p R W).next (qst W pc) s = code W (bsel (nthd s (trV R rr)) o z) /\
nthd (setd s (trA rr) false) (trA rr) = false /\
(forall i, i < R -> Nat.beq i rr = false ->
nthd (setd s (trA rr) false) (trA i) = nthd s (trA i) /\
nthd (setd s (trA rr) false) (trV R i) = nthd s (trV R i)) := by
have hl := ist_look p W pc _ hp hi
refine And.intro ?_ (And.intro rfl (And.intro ?_ (And.intro (nthd_setd_same _ _ _) ?_)))
· show selR (nthd s 0) s (wr0 p R (qst W pc) s) = _
rw [h0]
show qWr s (nthI p (cnt (thm W pc))) = _
rw [hl]; rfl
· show qNext R W (thm W pc) s (nthI p (cnt (thm W pc))) = _
rw [hl]; rfl
· intro i _ hne
exact And.intro (nthd_setd_other _ _ _ _ (bAA i rr hne)) (nthd_setd_other _ _ _ _ (bVA R i rr hR))
theorem jmp_turn (p : List Ins) (R W pc l : Nat) (hp : p.length <= W)
(hi : nthI p pc = some (Ins.jmp l)) (s : List Bool) : Turn (mach p R W) (ist W pc) (code W l) s := by
have hl := ist_look p W pc _ hp hi
refine Turn.mk ?_ ?_ ?_
· show iNext W (thm W pc) s (nthI p (cnt (thm W pc))) = code W l
rw [hl]; rfl
· apply wrt_id_of
show iWr R s (nthI p (cnt (thm W pc))) = s
rw [hl]; rfl
· show iRt s (nthI p (cnt (thm W pc))) = false
rw [hl]; rfl
/-- Every state of the machine has W + 2 bits. -/
theorem code_len (W pc : Nat) : (code W pc).length = W + 2 := by
show (thm W pc).length + 1 + 1 = W + 2
rw [thm_len]
end Found.Pnp56
#print axioms Found.Pnp56.cnt_thm
#print axioms Found.Pnp56.thm_len
#print axioms Found.Pnp56.nthI_lt
#print axioms Found.Pnp56.selR_same
#print axioms Found.Pnp56.beq_add_r
#print axioms Found.Pnp56.beq_lt
#print axioms Found.Pnp56.beq_gt
#print axioms Found.Pnp56.bAV
#print axioms Found.Pnp56.bVA
#print axioms Found.Pnp56.bVV
#print axioms Found.Pnp56.trA_lt
#print axioms Found.Pnp56.trV_lt
#print axioms Found.Pnp56.wr0_keep
#print axioms Found.Pnp56.wrt_bit
#print axioms Found.Pnp56.marks_kept
#print axioms Found.Pnp56.home_fixed
#print axioms Found.Pnp56.fwd_code
#print axioms Found.Pnp56.keep_code
#print axioms Found.Pnp56.keep_push
#print axioms Found.Pnp56.keep_pop
#print axioms Found.Pnp56.push_write
#print axioms Found.Pnp56.pop_turn_empty
#print axioms Found.Pnp56.pop_turn_cons
#print axioms Found.Pnp56.pop_erase
#print axioms Found.Pnp56.jmp_turn
#print axioms Found.Pnp56.code_len
#print axioms: 26 theorems, no axioms.
Every register program with registers below \(R\) runs on its track machine within a polynomial step count.
found/Pnp57.lean
/-
T330 piece 57. found/Pnp57 -- a register program runs on its track machine in polynomially
many steps (CL3c, 15).
The track machine mach p R W of Pnp56 is related to the register machine of Pnp20 by S2:
Pnp53's Sim (head at home h, state code pc, registers laid from h + 1) together with Inv
(the home mark on h only, the first-cell mark on h + 1 only, no present bit at home).
Proved, for any program p whose registers are below R (ProgOK) and W >= length p:
* inv_run: Inv is kept along every run (Pnp55 run_bit, run_marked);
* step_ex: from an S2-related pair the machine reaches a pair S2-related to Pnp20's step
within pe stepE (regSz x R) steps (Pnp53 sim_push, sim_pop_empty, sim_pop_cons,
sim_jmp with the hypotheses discharged by Pnp56; zero steps once halted);
* rm_on_tm: n register machine steps take at most n * pe stepE (regSz x R + n) steps of
the track machine (Pnp54 run_sim): a polynomial in n and the register size.
It relativizes (a tool; PNP_MAP.md). Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp56
namespace Found.Pnp57
open Found.Pnp7 (nthd)
open Found.Pnp20 (Ins Cfg nthI step exec run)
open Found.Pnp43
open Found.Pnp44
open Found.Pnp47 (nthd_sym lay_tail)
open Found.Pnp52 (rv)
open Found.Pnp53 (Sim RegsAt sim_push sim_pop_empty sim_pop_cons sim_jmp bsel)
open Found.Pnp54 (regSz stepE push_cost popc_cost short_cost run_sim step_sz)
open Found.Pnp55 (anyT anyT_false run_bit run_marked)
open Found.Pnp3 (pe)
open Found.Pnp47 (Lay)
open Found.Pnp56
/-- The marks and the empty home cell. -/
def Inv (t : Tape) (R h : Nat) : Prop :=
t h 0 = true /\ (forall j, h + 1 <= j -> t j 0 = false) /\ t (h + 1) 1 = true /\
(forall j, h + 1 + 1 <= j -> t j 1 = false) /\ (forall i, i < R -> t h (trA i) = false)
def S2 (p : List Ins) (R W h : Nat) (c : TCk) (d : Cfg) : Prop :=
Sim (mach p R W) trA (trV R) R h (code W) c d /\ Inv c.tape R h
/-- All registers named by the program are below R. -/
def ProgOK (p : List Ins) (R : Nat) : Prop :=
(forall pc rr b, nthI p pc = some (Ins.push rr b) -> rr < R) /\
(forall pc rr e z o, nthI p pc = some (Ins.pop rr e z o) -> rr < R)
theorem lt0 (R : Nat) : 0 < R + R + 1 + 1 := Nat.succ_le_succ (Nat.zero_le _)
theorem lt1 (R : Nat) : 1 < R + R + 1 + 1 := Nat.succ_le_succ (Nat.succ_le_succ (Nat.zero_le _))
theorem inv_run (p : List Ins) (R W h : Nat) :
forall n c, Inv c.tape R h -> Inv (tmRun (mach p R W) n c).tape R h := by
intro n c hi
have b0 : forall j, (tmRun (mach p R W) n c).tape j 0 = c.tape j 0 :=
run_bit (mach p R W) 0 (lt0 R) (fun q s => (marks_kept p R W q s).1) n c
have b1 : forall j, (tmRun (mach p R W) n c).tape j 1 = c.tape j 1 :=
run_bit (mach p R W) 1 (lt1 R) (fun q s => (marks_kept p R W q s).2) n c
refine And.intro ?_ (And.intro ?_ (And.intro ?_ (And.intro ?_ ?_)))
· rw [b0]; exact hi.1
· intro j hj; rw [b0]; exact hi.2.1 j hj
· rw [b1]; exact hi.2.2.1
· intro j hj; rw [b1]; exact hi.2.2.2.1 j hj
· intro i hiR
have r := run_marked (mach p R W) 0 (trA i) (lt0 R) (trA_lt R i hiR)
(fun q s => (marks_kept p R W q s).1)
(fun q s hs => by rw [home_fixed p R W q s hs]) n c h hi.1
rw [r]
exact hi.2.2.2.2 i hiR
theorem step_ex (p : List Ins) (R W h : Nat) (hp : p.length <= W) (hok : ProgOK p R)
(c : TCk) (d : Cfg) (hS : S2 p R W h c d) :
Exists (fun k => k <= pe stepE (regSz d.regs R) /\
S2 p R W h (tmRun (mach p R W) k c) (step p d)) := by
cases c with
| mk t0 pos q =>
cases d with
| mk pc x =>
have e1 : pos = h := hS.1.1
have e2 : q = code W pc := hS.1.2.1
have hl : RegsAt t0 trA (trV R) R h x := hS.1.2.2
have hinv : Inv t0 R h := hS.2
have hAV : forall i, i < R -> trA i < (mach p R W).k + 1 /\ trV R i < (mach p R W).k + 1 :=
fun i hi => And.intro (trA_lt R i hi) (trV_lt R i hi)
have hhome : anyT (symAt (mach p R W) t0 h) trA R = false :=
anyT_false _ _ R (fun i hi => by
rw [nthd_sym (mach p R W) t0 h (trA i) (trA_lt R i hi)]
exact hinv.2.2.2.2 i hi)
have hf := fwd_code p R W pc (symAt (mach p R W) t0 h) hhome
have ec : TCk.mk t0 pos q = TCk.mk t0 h (code W pc) := by rw [e1, e2]
cases hI : nthI p pc with
| none =>
refine Exists.intro 0 (And.intro (Nat.zero_le _) ?_)
show S2 p R W h (TCk.mk t0 pos q) (exec (Cfg.mk pc x) (nthI p pc))
rw [hI]
exact hS
| some ins =>
rw [ec]
cases ins with
| push rr b =>
have hR : rr < R := hok.1 pc rr b hI
have hA0 : nthd (symAt (mach p R W) t0 (h + 1 + (rv (x rr)).length)) (trA rr) = false := by
rw [nthd_sym (mach p R W) _ _ _ (trA_lt R rr hR)]
exact lay_tail t0 (trA rr) (trV R rr) (rv (x rr)) (h + 1) (hl rr hR) _ (Nat.le_refl _)
have h00 : nthd (symAt (mach p R W) t0 (h + 1 + (rv (x rr)).length)) 0 = false := by
rw [nthd_sym (mach p R W) _ _ _ (lt0 R)]
exact hinv.2.1 _ (Nat.le_add_right _ _)
have pw := push_write p R W pc rr b hp hR hI _ hA0 h00
have hs := sim_push (mach p R W) trA (trV R) R h (code W) p pc x t0 (ist W pc) rr b hI hR hAV hl hf
(keep_push p R W pc rr b hp hI)
(by rw [pw.1]; exact pw.2.2.2.1) (by rw [pw.1]; exact pw.2.2.2.2.1) pw.2.1 pw.2.2.1
(keep_code p R W (pc + 1) rr hR)
(fun i hi hne => by rw [pw.1]; exact pw.2.2.2.2.2 i hi hne)
exact Exists.intro _ (And.intro (push_cost x R rr hR)
(And.intro hs (inv_run p R W h _ (TCk.mk t0 h (code W pc)) hinv)))
| pop rr e z o =>
have hR : rr < R := hok.2 pc rr e z o hI
cases hx : x rr with
| nil =>
have hlr : Lay t0 (trA rr) (trV R rr) (h + 1) [] := by
have h' := hl rr hR
rw [hx] at h'
exact h'
have hA : nthd (symAt (mach p R W) t0 (h + 1)) (trA rr) = false := by
rw [nthd_sym (mach p R W) _ _ _ (trA_lt R rr hR)]
exact hlr (h + 1) (Nat.le_refl _)
have hF : nthd (symAt (mach p R W) t0 (h + 1)) 1 = true := by
rw [nthd_sym (mach p R W) _ _ _ (lt1 R)]
exact hinv.2.2.1
have hs := sim_pop_empty (mach p R W) trA (trV R) R h (code W) p pc x t0 (ist W pc) rr e z o
hI hR hAV hl hx hf (pop_turn_empty p R W pc rr e z o hp hI _ hA hF)
exact Exists.intro _ (And.intro (short_cost _)
(And.intro hs (inv_run p R W h _ (TCk.mk t0 h (code W pc)) hinv)))
| cons b tl =>
have hlr : Lay t0 (trA rr) (trV R rr) (h + 1) (rv tl ++ [b]) := by
have h' := hl rr hR
rw [hx] at h'
exact h'
have hA : nthd (symAt (mach p R W) t0 (h + 1 + (rv tl ++ [b]).length)) (trA rr) = false := by
rw [nthd_sym (mach p R W) _ _ _ (trA_lt R rr hR)]
exact lay_tail t0 (trA rr) (trV R rr) _ (h + 1) hlr _ (Nat.le_refl _)
have hF : nthd (symAt (mach p R W) t0 (h + 1 + (rv tl ++ [b]).length)) 1 = false := by
rw [nthd_sym (mach p R W) _ _ _ (lt1 R)]
refine hinv.2.2.2.1 _ ?_
rw [Found.Pnp48.len_snoc]
exact Nat.add_le_add_left (Nat.succ_le_succ (Nat.zero_le _)) (h + 1)
have hturn := pop_turn_cons p R W pc rr e z o hp hI _ hA hF
have h02 : nthd (symAt (mach p R W) t0 (h + 1 + (rv tl).length)) 0 = false := by
rw [nthd_sym (mach p R W) _ _ _ (lt0 R)]
exact hinv.2.1 _ (Nat.le_add_right _ _)
have pe2 := pop_erase p R W pc rr e z o hp hR hI _ h02
have hvb : nthd (symAt (mach p R W) t0 (h + 1 + (rv tl).length)) (trV R rr) = b := by
rw [nthd_sym (mach p R W) _ _ _ (trV_lt R rr hR)]
exact (Found.Pnp48.lay_last t0 (trA rr) (trV R rr) b (rv tl) (h + 1) hlr).2
have hnx : (mach p R W).next (qst W pc) (symAt (mach p R W) t0 (h + 1 + (rv tl).length)) =
code W (bsel b o z) := by
rw [pe2.2.2.1, hvb]
have hs := sim_pop_cons (mach p R W) trA (trV R) R h (code W) p pc x t0 (ist W pc) (qst W pc)
rr e z o b tl hI hR hAV hl hx hf (keep_pop p R W pc rr e z o hp hI) hturn
(by rw [pe2.1]; exact pe2.2.2.2.1) pe2.2.1 hnx (keep_code p R W (bsel b o z) rr hR)
(fun i hi hne => by rw [pe2.1]; exact pe2.2.2.2.2 i hi hne)
exact Exists.intro _ (And.intro (popc_cost x R rr hR b tl hx)
(And.intro hs (inv_run p R W h _ (TCk.mk t0 h (code W pc)) hinv)))
| jmp l =>
have hs := sim_jmp (mach p R W) trA (trV R) R h (code W) p pc x t0 (ist W pc) l hI hAV hl hf
(jmp_turn p R W pc l hp hI _)
exact Exists.intro _ (And.intro (short_cost _)
(And.intro hs (inv_run p R W h _ (TCk.mk t0 h (code W pc)) hinv)))
theorem rm_on_tm (p : List Ins) (R W h : Nat) (hp : p.length <= W) (hok : ProgOK p R) :
forall n c d, S2 p R W h c d ->
Exists (fun K => K <= n * pe stepE (regSz d.regs R + n) /\
S2 p R W h (tmRun (mach p R W) K c) (run p n d)) :=
run_sim (mach p R W) (S2 p R W h) p (fun d => regSz d.regs R) stepE (fun d => step_sz p R d)
(fun c d hS => step_ex p R W h hp hok c d hS)
end Found.Pnp57
#print axioms Found.Pnp57.lt0
#print axioms Found.Pnp57.lt1
#print axioms Found.Pnp57.inv_run
#print axioms Found.Pnp57.step_ex
#print axioms Found.Pnp57.rm_on_tm
#print axioms: 5 theorems, no axioms.
The start tape lays the input registers; a program run is reproduced on the tape.
found/Pnp58.lean
/-
T330 piece 58. found/Pnp58 -- the start tape and whole program runs on the track machine
(CL3c, 16).
The start tape of the track machine for registers x has the home mark on cell 0, the
first-cell mark on cell 1, and register r laid (reversed) from cell 1 on its present and
value tracks. Proved, for any program p with registers below R and W >= length p:
* le_ex: m <= n gives n = m + d (by induction on <=; no subtraction);
* layOf: a tape whose cells off + 1 + d carry (has L d, nthd L d) lays L from off + 1;
* start_inv, start_regs, start_S2: the start tape with head at 0 in state code o is
S2-related to the register configuration (o, x);
* tm_runs: if the program, started at o on x, reaches pc o' with registers y in N steps
(Pnp21 Runs), the track machine reaches, within N * pe stepE (regSz x R + N) steps,
head 0, state code o', and every register y i (i < R) laid from cell 1.
It relativizes (a tool; PNP_MAP.md). Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp57
import found.Pnp21
namespace Found.Pnp58
open Found.Pnp7 (nthd)
open Found.Pnp12 (sel)
open Found.Pnp20 (Ins Cfg nthI step run)
open Found.Pnp43
open Found.Pnp44
open Found.Pnp47 (Lay shift)
open Found.Pnp52 (rv)
open Found.Pnp53 (Sim RegsAt)
open Found.Pnp54 (regSz stepE)
open Found.Pnp3 (pe)
open Found.Pnp56
open Found.Pnp57
/-! ## Laying a list -/
def has : List Bool -> Nat -> Bool
| [], _ => false
| _ :: _, 0 => true
| _ :: l, d + 1 => has l d
theorem le_ex (m : Nat) : forall n, m <= n -> Exists (fun d => n = m + d) := by
intro n h
induction h with
| refl => exact Exists.intro 0 rfl
| step _ ih =>
cases ih with
| intro d hd => exact Exists.intro (d + 1) (congrArg Nat.succ hd)
theorem layOf (t : Tape) (a v : Nat) : forall (L : List Bool) (off : Nat),
(forall d, t (off + 1 + d) a = has L d) -> (forall d, t (off + 1 + d) v = nthd L d) ->
Lay t a v (off + 1) L := by
intro L
induction L with
| nil =>
intro off ha _ j hj
cases le_ex (off + 1) j hj with
| intro d hd => rw [hd]; exact ha d
| cons y L ih =>
intro off ha hv
refine And.intro (ha 0) (And.intro (hv 0) ?_)
refine ih (off + 1) (fun d => ?_) (fun d => ?_)
· rw [shift (off + 1) d]; exact ha (d + 1)
· rw [shift (off + 1) d]; exact hv (d + 1)
/-! ## The start tape -/
/-- m minus R, by structural recursion (no core subtraction lemmas). -/
def dropR : Nat -> Nat -> Nat
| m, 0 => m
| 0, _ + 1 => 0
| m + 1, R + 1 => dropR m R
theorem dropR_add (r : Nat) : forall R, dropR (r + R) R = r := by
intro R
induction R with
| zero => cases r with
| zero => rfl
| succ r => rfl
| succ R ih => exact ih
theorem blt_add (r : Nat) : forall R, Nat.blt (r + R) R = false := by
intro R
induction R with
| zero => cases r with
| zero => rfl
| succ r => rfl
| succ R ih => exact ih
theorem blt_lt : forall (r R : Nat), r < R -> Nat.blt r R = true := by
intro r
induction r with
| zero =>
intro R h
cases R with
| zero => exact absurd h (Nat.not_succ_le_zero 0)
| succ R => rfl
| succ r ih =>
intro R h
cases R with
| zero => exact absurd h (Nat.not_succ_le_zero _)
| succ R => exact ih R (Nat.le_of_succ_le_succ h)
def stB (R : Nat) (x : Nat -> List Bool) : Nat -> Nat -> Bool
| j, 0 => Nat.beq j 0
| j, 1 => Nat.beq j 1
| 0, _ + 2 => false
| d + 1, m + 2 => sel (Nat.blt m R) (has (rv (x m)) d) (nthd (rv (x (dropR m R))) d)
def startT (R : Nat) (x : Nat -> List Bool) : Tape := fun j i => stB R x j i
theorem one_add (d : Nat) : 0 + 1 + d = d + 1 := by
rw [Nat.zero_add, Nat.add_comm]
theorem start_inv (R : Nat) (x : Nat -> List Bool) : Inv (startT R x) R 0 := by
refine And.intro rfl (And.intro ?_ (And.intro rfl (And.intro ?_ (fun _ _ => rfl))))
· intro j hj
cases j with
| zero => exact absurd hj (Nat.not_succ_le_zero 0)
| succ j => rfl
· intro j hj
cases j with
| zero => exact absurd hj (Nat.not_succ_le_zero _)
| succ j =>
cases j with
| zero => exact absurd hj (Nat.not_succ_le_self 1)
| succ j => rfl
theorem start_regs (R : Nat) (x : Nat -> List Bool) : RegsAt (startT R x) trA (trV R) R 0 x := by
intro i hi
refine layOf (startT R x) (trA i) (trV R i) (rv (x i)) 0 (fun d => ?_) (fun d => ?_)
· rw [one_add]
show sel (Nat.blt i R) (has (rv (x i)) d) _ = has (rv (x i)) d
rw [blt_lt i R hi]
rfl
· rw [one_add]
show sel (Nat.blt (i + R) R) _ (nthd (rv (x (dropR (i + R) R))) d) = nthd (rv (x i)) d
rw [blt_add, dropR_add]
rfl
theorem start_S2 (p : List Ins) (R W o : Nat) (x : Nat -> List Bool) :
S2 p R W 0 (TCk.mk (startT R x) 0 (code W o)) (Cfg.mk o x) :=
And.intro (And.intro rfl (And.intro rfl (start_regs R x))) (start_inv R x)
/-! ## Whole runs -/
theorem regs_ext (t : Tape) (R h : Nat) (x y : Nat -> List Bool) (hxy : forall i, x i = y i)
(hx : RegsAt t trA (trV R) R h x) : RegsAt t trA (trV R) R h y := by
intro i hi
rw [(hxy i).symm]
exact hx i hi
theorem tm_runs (p : List Ins) (R W : Nat) (hp : p.length <= W) (hok : ProgOK p R)
(N o o' : Nat) (x y : Nat -> List Bool) (hr : Found.Pnp21.Runs p N o x o' y) :
Exists (fun K => K <= N * pe stepE (regSz x R + N) /\
(tmRun (mach p R W) K (TCk.mk (startT R x) 0 (code W o))).pos = 0 /\
(tmRun (mach p R W) K (TCk.mk (startT R x) 0 (code W o))).q = code W o' /\
RegsAt (tmRun (mach p R W) K (TCk.mk (startT R x) 0 (code W o))).tape trA (trV R) R 0 y) := by
cases rm_on_tm p R W 0 hp hok N _ (Cfg.mk o x) (start_S2 p R W o x) with
| intro K hK =>
refine Exists.intro K (And.intro hK.1 (And.intro hK.2.1.1 (And.intro ?_ ?_)))
· rw [hK.2.1.2.1, hr.1]
· exact regs_ext _ R 0 _ y hr.2 hK.2.1.2.2
end Found.Pnp58
#print axioms Found.Pnp58.le_ex
#print axioms Found.Pnp58.layOf
#print axioms Found.Pnp58.dropR_add
#print axioms Found.Pnp58.blt_add
#print axioms Found.Pnp58.blt_lt
#print axioms Found.Pnp58.one_add
#print axioms Found.Pnp58.start_inv
#print axioms Found.Pnp58.start_regs
#print axioms Found.Pnp58.start_S2
#print axioms Found.Pnp58.regs_ext
#print axioms Found.Pnp58.tm_runs
#print axioms: 11 theorems, no axioms.
The Cook–Levin map is computed by one concrete track machine within a fixed polynomial of the input length.
found/Pnp59.lean
/-
T330 piece 59. found/Pnp59 -- the Cook-Levin reduction computed by a concrete track
machine in polynomially many steps (CL3c, 17).
Pnp42.cl3b runs the register program RED = redP ... on input l (register 13) and leaves
enc (red ...) in register 16 within pe redPE |l| steps. With Pnp58 the program runs on its
own track machine mach RED R W, W = length RED, R = 14 + (sumR RED + 3) (sumR bounds every
register the program names). Proved:
* sumR, insReg_lt, progOK_sum: every program's registers lie below sumR p, so
ProgOK p (14 + (sumR p + 3)) holds for every program p;
* frag_self: a program is a fragment of itself at offset 0;
* inpR, regSz_low, regSz_inp: the input configuration (l in register 13, all else
empty) has size exactly |l| below any R = 14 + d;
* red_tm: for every Turing machine M and its parameters, the track machine of RED,
started on the start tape of l, reaches within pe tmE |l| steps -- one polynomial
expression tmE, independent of l -- a configuration with head 0, the halting state
code W |RED|, and enc (red ...) laid (reversed) from cell 1 on the tracks of register 16.
It relativizes (a tool; PNP_MAP.md). Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp58
import found.Pnp42
namespace Found.Pnp59
open Found.Pnp3 (PE pe pe_mono pe_comp)
open Found.Pnp12 (TM rule)
open Found.Pnp19 (encCNF)
open Found.Pnp20 (Ins Cfg nthI selR beq_refl')
open Found.Pnp21 (Frag Runs shift)
open Found.Pnp32 (inpOf)
open Found.Pnp37 (redP)
open Found.Pnp42 (redPE cl3b)
open Found.Pnp43
open Found.Pnp44
open Found.Pnp47 (Lay)
open Found.Pnp52 (rv)
open Found.Pnp54 (regSz stepE)
open Found.Pnp56
open Found.Pnp57
open Found.Pnp58
/-! ## A register bound for every program -/
def insReg : Ins -> Nat
| .push r _ => r
| .pop r _ _ _ => r
| .jmp _ => 0
def sumR : List Ins -> Nat
| [] => 0
| i :: p => insReg i + 1 + sumR p
theorem insReg_lt : forall (p : List Ins) (n : Nat) (i : Ins), nthI p n = some i -> insReg i < sumR p := by
intro p
induction p with
| nil =>
intro n i h
have h' : (none : Option Ins) = some i := h
cases h'
| cons a p ih =>
intro n i h
cases n with
| zero =>
have e : some a = some i := h
cases e
exact Nat.le_add_right (insReg a + 1) (sumR p)
| succ n =>
exact Nat.lt_of_lt_of_le (ih n i h) (Nat.le_add_left _ _)
theorem lt_big (a s : Nat) (h : a < s) : a < 14 + (s + 3) :=
Nat.lt_of_lt_of_le h (Nat.le_trans (Nat.le_add_right s 3) (Nat.le_add_left _ 14))
theorem progOK_sum (p : List Ins) : ProgOK p (14 + (sumR p + 3)) :=
And.intro
(fun pc _ _ h => lt_big _ _ (insReg_lt p pc _ h))
(fun pc _ _ _ _ h => lt_big _ _ (insReg_lt p pc _ h))
theorem shift_zero (ins : Ins) : shift 0 ins = ins := by
cases ins with
| push r b => rfl
| pop r e z w =>
show Ins.pop r (0 + e) (0 + z) (0 + w) = Ins.pop r e z w
rw [Nat.zero_add, Nat.zero_add, Nat.zero_add]
| jmp l =>
show Ins.jmp (0 + l) = Ins.jmp l
rw [Nat.zero_add]
theorem frag_self (p : List Ins) : Frag p 0 p := by
intro i ins h
rw [Nat.zero_add, shift_zero]
exact h
/-! ## The input configuration -/
def inpR (l : List Bool) : Nat -> List Bool := fun i => selR (Nat.beq i 13) l []
theorem inpR_13 (l : List Bool) : inpR l 13 = l := by
show selR (Nat.beq 13 13) l [] = l
rw [beq_refl']
rfl
theorem inpR_other (l : List Bool) (i : Nat) (h : Nat.beq i 13 = false) : inpR l i = [] := by
show selR (Nat.beq i 13) l [] = []
rw [h]
rfl
theorem regSz_low (l : List Bool) : forall R, R <= 13 -> regSz (inpR l) R = 0 := by
intro R
induction R with
| zero => intro _; rfl
| succ R ih =>
intro h
show regSz (inpR l) R + (inpR l R).length = 0
rw [ih (Nat.le_of_succ_le h), inpR_other l R (beq_lt R 13 h)]
rfl
theorem regSz_inp (l : List Bool) : forall d, regSz (inpR l) (14 + d) = l.length := by
intro d
induction d with
| zero =>
show regSz (inpR l) 13 + (inpR l 13).length = l.length
rw [regSz_low l 13 (Nat.le_refl _), inpR_13, Nat.zero_add]
| succ d ih =>
show regSz (inpR l) (14 + d) + (inpR l (14 + d)).length = l.length
rw [ih, inpR_other l (14 + d) (beq_gt (14 + d) 13 (Nat.le_add_right 14 d))]
rfl
/-! ## RED on its track machine -/
/-- The step bound: N * stepE (|l| + N) with N = redPE. -/
def tmE (e : PE) : PE := .mul e (Found.Pnp3.comp stepE (.add .x e))
theorem red_tm (M : TM) (p0 : Nat) (q0 : List Bool) (s : Nat) (eR eT : PE) (l : List Bool) :
Exists (fun K =>
K <= pe (tmE (redPE (M.m + 2) (rule M) M.m (p0 + 1) q0 s eR eT)) l.length /\
(tmRun (mach (redP (M.m + 2) (rule M) M.m (p0 + 1) q0 s eR eT)
(14 + (sumR (redP (M.m + 2) (rule M) M.m (p0 + 1) q0 s eR eT) + 3))
(redP (M.m + 2) (rule M) M.m (p0 + 1) q0 s eR eT).length) K
(TCk.mk (startT (14 + (sumR (redP (M.m + 2) (rule M) M.m (p0 + 1) q0 s eR eT) + 3)) (inpR l)) 0
(code (redP (M.m + 2) (rule M) M.m (p0 + 1) q0 s eR eT).length 0))).pos = 0 /\
(tmRun (mach (redP (M.m + 2) (rule M) M.m (p0 + 1) q0 s eR eT)
(14 + (sumR (redP (M.m + 2) (rule M) M.m (p0 + 1) q0 s eR eT) + 3))
(redP (M.m + 2) (rule M) M.m (p0 + 1) q0 s eR eT).length) K
(TCk.mk (startT (14 + (sumR (redP (M.m + 2) (rule M) M.m (p0 + 1) q0 s eR eT) + 3)) (inpR l)) 0
(code (redP (M.m + 2) (rule M) M.m (p0 + 1) q0 s eR eT).length 0))).q =
code (redP (M.m + 2) (rule M) M.m (p0 + 1) q0 s eR eT).length
(redP (M.m + 2) (rule M) M.m (p0 + 1) q0 s eR eT).length /\
Lay (tmRun (mach (redP (M.m + 2) (rule M) M.m (p0 + 1) q0 s eR eT)
(14 + (sumR (redP (M.m + 2) (rule M) M.m (p0 + 1) q0 s eR eT) + 3))
(redP (M.m + 2) (rule M) M.m (p0 + 1) q0 s eR eT).length) K
(TCk.mk (startT (14 + (sumR (redP (M.m + 2) (rule M) M.m (p0 + 1) q0 s eR eT) + 3)) (inpR l)) 0
(code (redP (M.m + 2) (rule M) M.m (p0 + 1) q0 s eR eT).length 0))).tape
(trA 16) (trV (14 + (sumR (redP (M.m + 2) (rule M) M.m (p0 + 1) q0 s eR eT) + 3)) 16) (0 + 1)
(rv (encCNF (Found.Pnp18.red M (p0 + 1 + pe eR l.length + 1) (pe eT l.length) p0 q0 (inpOf s l))))) := by
cases cl3b (redP (M.m + 2) (rule M) M.m (p0 + 1) q0 s eR eT) 0 M p0 q0 s eR eT (frag_self _) l (inpR l)
(inpR_13 l) (inpR_other l) with
| intro N hN =>
cases hN with
| intro y hy =>
have hr : Runs (redP (M.m + 2) (rule M) M.m (p0 + 1) q0 s eR eT) N 0 (inpR l)
(redP (M.m + 2) (rule M) M.m (p0 + 1) q0 s eR eT).length y := by
have r := hy.1
rw [Nat.zero_add] at r
exact r
cases tm_runs _ _ _ (Nat.le_refl _) (progOK_sum _) N 0 _ (inpR l) y hr with
| intro K hK =>
refine Exists.intro K (And.intro ?_ (And.intro hK.2.1 (And.intro hK.2.2.1 ?_)))
· refine Nat.le_trans hK.1 ?_
rw [regSz_inp l]
show N * pe stepE (l.length + N) <=
pe (redPE (M.m + 2) (rule M) M.m (p0 + 1) q0 s eR eT) l.length *
pe (Found.Pnp3.comp stepE (.add .x (redPE (M.m + 2) (rule M) M.m (p0 + 1) q0 s eR eT))) l.length
rw [pe_comp]
exact Nat.mul_le_mul hy.2.2 (pe_mono _ _ (Nat.add_le_add_left hy.2.2 _) stepE)
· have h16 := hK.2.2.2 16 (Nat.lt_of_lt_of_le (Nat.lt_succ_self 16)
(Nat.add_le_add_left (Nat.le_add_left 3 _) 14))
rw [hy.2.1] at h16
exact h16
end Found.Pnp59
#print axioms Found.Pnp59.insReg_lt
#print axioms Found.Pnp59.lt_big
#print axioms Found.Pnp59.progOK_sum
#print axioms Found.Pnp59.shift_zero
#print axioms Found.Pnp59.frag_self
#print axioms Found.Pnp59.inpR_13
#print axioms Found.Pnp59.inpR_other
#print axioms Found.Pnp59.regSz_low
#print axioms Found.Pnp59.regSz_inp
#print axioms Found.Pnp59.red_tm
#print axioms: 10 theorems, no axioms.
For a language verified by a Turing machine, the reduction runs on a concrete machine in polynomial time and \(x\in L\) iff the output CNF is satisfiable.
found/Pnp60.lean
/-
T330 piece 60. found/Pnp60 -- the Cook-Levin theorem with the reduction computed by a
concrete machine in polynomial time (CL3c, 18; closes O3 in the form of Pnp18).
For a language L verified by a one-tape Turing machine M (Pnp12-14) with polynomial
bounds W(n) = p0 + 1 + eR(n) + 1, T(n) = eT(n) (the assumptions of Pnp18.cl1, for every
input), the reduction x |-> red x (the tableau CNF) is computed by one fixed concrete
track machine TMred = mach RED R |RED| (Pnp56) from the start tape of x (Pnp58), and:
* cook_levin: for every input l there is K <= pe (tmE redPE) |l| such that after K steps
TMred has its head at 0, is in the halting state of RED, and carries enc (red l) laid
on the output tracks (register 16); and L l holds iff red l is satisfiable;
* out_unique: the laid output determines the CNF (Lay determines the list; Pnp19 enc_inj).
The bound pe (tmE redPE) is one polynomial expression of |l| (Pnp3.PE), fixed by M and the
bounds. Relativizes (a tool; PNP_MAP.md). ASCII forms only. CIC dependence: none.
-/
import found.Pnp59
import found.Pnp18
namespace Found.Pnp60
open Found.Pnp3 (PE pe)
open Found.Pnp7 (nthd)
open Found.Pnp12 (TM rule)
open Found.Pnp13 (TC Inside)
open Found.Pnp14 (InputOK)
open Found.Pnp19 (encCNF)
open Found.Pnp32 (inpOf)
open Found.Pnp37 (redP)
open Found.Pnp42 (redPE)
open Found.Pnp43
open Found.Pnp44
open Found.Pnp47 (Lay)
open Found.Pnp52 (rv)
open Found.Pnp56
open Found.Pnp58
open Found.Pnp59
/-- The register program of the reduction, its register bound and its track machine. -/
def RED (M : TM) (p0 : Nat) (q0 : List Bool) (s : Nat) (eR eT : PE) : List Found.Pnp20.Ins :=
redP (M.m + 2) (rule M) M.m (p0 + 1) q0 s eR eT
def RR (M : TM) (p0 : Nat) (q0 : List Bool) (s : Nat) (eR eT : PE) : Nat :=
14 + (sumR (RED M p0 q0 s eR eT) + 3)
def TMred (M : TM) (p0 : Nat) (q0 : List Bool) (s : Nat) (eR eT : PE) : TMk :=
mach (RED M p0 q0 s eR eT) (RR M p0 q0 s eR eT) (RED M p0 q0 s eR eT).length
def startRed (M : TM) (p0 : Nat) (q0 : List Bool) (s : Nat) (eR eT : PE) (l : List Bool) : TCk :=
TCk.mk (startT (RR M p0 q0 s eR eT) (inpR l)) 0 (code (RED M p0 q0 s eR eT).length 0)
/-- The verifier bounds of Pnp18.cl1 at input l. -/
def Wn (p0 : Nat) (eR : PE) (l : List Bool) : Nat := p0 + 1 + pe eR l.length + 1
def Tn (eT : PE) (l : List Bool) : Nat := pe eT l.length
def Verifies (M : TM) (p0 : Nat) (q0 : List Bool) (s : Nat) (eR eT : PE) (L : List Bool -> Prop) : Prop :=
forall l, p0 + 2 < Wn p0 eR l /\
(forall tape : Nat -> Bool, Inside M (Wn p0 eR l) (Tn eT l) (TC.mk tape (p0 + 1) q0)) /\
(L l <-> Exists fun tape : Nat -> Bool => InputOK tape (inpOf s l) /\ tape (Wn p0 eR l) = false /\
(Found.Pnp13.tmRun M (Tn eT l) (TC.mk tape (p0 + 1) q0)).tape 0 = true)
theorem cook_levin (M : TM) (p0 : Nat) (q0 : List Bool) (s : Nat) (eR eT : PE)
(hq0 : q0.length = M.m) (hnext : forall a b, (M.next a b).length = M.m)
(L : List Bool -> Prop) (hv : Verifies M p0 q0 s eR eT L) (l : List Bool) :
Exists (fun K =>
K <= pe (tmE (redPE (M.m + 2) (rule M) M.m (p0 + 1) q0 s eR eT)) l.length /\
(tmRun (TMred M p0 q0 s eR eT) K (startRed M p0 q0 s eR eT l)).pos = 0 /\
(tmRun (TMred M p0 q0 s eR eT) K (startRed M p0 q0 s eR eT l)).q =
code (RED M p0 q0 s eR eT).length (RED M p0 q0 s eR eT).length /\
Lay (tmRun (TMred M p0 q0 s eR eT) K (startRed M p0 q0 s eR eT l)).tape
(trA 16) (trV (RR M p0 q0 s eR eT) 16) (0 + 1)
(rv (encCNF (Found.Pnp18.red M (Wn p0 eR l) (Tn eT l) p0 q0 (inpOf s l))))) /\
(L l <-> Exists fun v => Found.Pnp8.cnfv v (Found.Pnp18.red M (Wn p0 eR l) (Tn eT l) p0 q0 (inpOf s l)) = true) := by
refine And.intro (red_tm M p0 q0 s eR eT l) ?_
have h := hv l
have c := Found.Pnp18.cl1 M (Wn p0 eR l) (Tn eT l) p0 q0 hq0 hnext h.1 (inpOf s l) h.2.1 (L l) h.2.2
exact Iff.intro c.1 c.2
/-! ## The output determines the CNF -/
theorem lay_unique (t : Tape) (a v : Nat) : forall (xs ys : List Bool) p,
Lay t a v p xs -> Lay t a v p ys -> xs = ys := by
intro xs
induction xs with
| nil =>
intro ys p hx hy
cases ys with
| nil => rfl
| cons y ys =>
have e1 : t p a = true := hy.1
have e2 : t p a = false := hx p (Nat.le_refl p)
rw [e1] at e2
exact Bool.noConfusion e2
| cons x xs ih =>
intro ys p hx hy
cases ys with
| nil =>
have e1 : t p a = true := hx.1
have e2 : t p a = false := hy p (Nat.le_refl p)
rw [e1] at e2
exact Bool.noConfusion e2
| cons y ys =>
have ex : x = y := hx.2.1.symm.trans hy.2.1
rw [ex, ih ys (p + 1) hx.2.2 hy.2.2]
theorem rv_inj : forall (a b : List Bool), rv a = rv b -> a = b := by
intro a b h
have ra : Found.Pnp52.rv (Found.Pnp52.rv a) = a := rv_rv a
have rb : Found.Pnp52.rv (Found.Pnp52.rv b) = b := rv_rv b
rw [ra.symm, rb.symm, h]
where
rv_app : forall (l : List Bool) (b : Bool), rv (l ++ [b]) = b :: rv l := by
intro l
induction l with
| nil => intro b; rfl
| cons c l ih =>
intro b
show rv (l ++ [b]) ++ [c] = b :: (rv l ++ [c])
rw [ih b]
rfl
rv_rv : forall (l : List Bool), rv (rv l) = l := by
intro l
induction l with
| nil => rfl
| cons c l ih =>
show rv (rv l ++ [c]) = c :: l
rw [rv_app (rv l) c, ih]
theorem out_unique (t : Tape) (a v p : Nat) (f g : List (List (Bool × Found.Pnp9.V)))
(hf : Lay t a v p (rv (encCNF f))) (hg : Lay t a v p (rv (encCNF g))) : f = g :=
Found.Pnp19.enc_inj f g (rv_inj _ _ (lay_unique t a v _ _ p hf hg))
end Found.Pnp60
#print axioms Found.Pnp60.cook_levin
#print axioms Found.Pnp60.lay_unique
#print axioms Found.Pnp60.rv_inj
#print axioms Found.Pnp60.out_unique
#print axioms: 4 theorems, no axioms.
Under the laws that P is closed under complement and inside NP, a language in NP whose complement is not in NP lies outside P.
found/Pnp61.lean
/-
T330 piece 61. found/Pnp61 -- the proof complexity bridge: NP different from coNP gives
P different from NP (the easy direction of Cook's program).
Over a machine model of RootComp (T329) with two laws -- P is closed under complement, and
P is inside NP -- equality of P and NP makes NP closed under complement. Hence a language in
NP whose complement is not in NP (NP /= coNP, the statement that no propositional proof
system is polynomially bounded; Cook--Reckhow 1979) is a language in NP outside P.
Proved:
* np_closed_of_eq: if NP is inside P then every NP language has its complement in NP;
* p_ne_np_of_np_ne_conp: a language in NP with complement outside NP is not in P.
The laws are hypotheses (fields of a structure), so the bridge holds in every model with
them. Relativizes (a bridge; PNP_MAP.md). ASCII forms only. CIC dependence: none.
-/
import found.RootComp
namespace Found.Pnp61
open Found.RootComp (Machine InP InNP)
/-- The complement of a language. -/
def compl (L : List Bool -> Bool) : List Bool -> Bool := fun x => !(L x)
/-- coNP: the complement is in NP. -/
def InCoNP (M : Machine) (L : List Bool -> Bool) : Prop := InNP M (compl L)
/-- Two laws of a machine model. -/
structure Laws (M : Machine) : Prop where
p_compl : forall L, InP M L -> InP M (compl L)
p_np : forall L, InP M L -> InNP M L
theorem np_closed_of_eq (M : Machine) (hl : Laws M) (heq : forall L, InNP M L -> InP M L)
(L : List Bool -> Bool) (hL : InNP M L) : InCoNP M L :=
hl.p_np (compl L) (hl.p_compl L (heq L hL))
theorem p_ne_np_of_np_ne_conp (M : Machine) (hl : Laws M) (L : List Bool -> Bool)
(_hL : InNP M L) (hc : InCoNP M L -> False) : InP M L -> False :=
fun hP => hc (hl.p_np (compl L) (hl.p_compl L hP))
/-- The same in global form: NP /= coNP (some NP language with complement outside NP)
gives a language in NP outside P. -/
theorem sep_of_conp (M : Machine) (hl : Laws M)
(h : Exists fun L : List Bool -> Bool => InNP M L /\ (InCoNP M L -> False)) :
Exists fun L : List Bool -> Bool => InNP M L /\ (InP M L -> False) :=
match h with
| Exists.intro L hL => Exists.intro L (And.intro hL.1 (p_ne_np_of_np_ne_conp M hl L hL.1 hL.2))
end Found.Pnp61
#print axioms Found.Pnp61.np_closed_of_eq
#print axioms Found.Pnp61.p_ne_np_of_np_ne_conp
#print axioms Found.Pnp61.sep_of_conp
#print axioms: 3 theorems, no axioms.
With the success of an inverter counted over all \(2^n\) inputs, a one-way function has no polynomial-time inverter; with search-to-decision (open lemma O5) it gives \(\mathrm {NP}\not \subseteq \mathrm {P}\). The earlier definition with a free success count is satisfied by the identity.
found/Owf1.lean
/-
T332 piece 1. found/Owf1 -- one-way functions: the T329 definition is degenerate; a
definition with the success counted from runs; one-way functions make inversion a hard
search problem; with search-to-decision, one-way functions give P /= NP.
RootComp.OneWay (T329) takes the success count succ as a free argument. Choosing
succ = (0, 1) satisfies its hardness clause for every function with polynomially bounded
output, so that definition carries no hardness (t329_degenerate). Here, over the machine
model with output (Pnp3.OM), the success of an inverter inv with budget e on length n is
counted over all 2^n inputs (succN), and
* OneWay2 M f: f is computed within a polynomial e; for every inverter and every
polynomial e, from some length on, succN * (e(n) + 2) <= 2^n (success below 1/(e+2));
* SearchEasy M f: some inverter finds a preimage of f x for every x in polynomial time;
* count_all: a test true on all lists of length n counts 2^n of them;
* owf_not_search: a one-way function has no polynomial-time inverter;
* owf_p_ne_np: if equality of P and NP makes every polynomial-time function invertible
(search-to-decision, the hypothesis S2D: open as a law of a concrete model,
OPEN_LEMMAS_PNP O5), then a one-way function gives an NP language outside P
(in the form: NP is not inside P).
Relativizes (a bridge; PNP_MAP.md). ASCII forms only. CIC dependence: none.
-/
import found.Pnp4
import found.Pnp9
import found.RootComp
namespace Found.Owf1
open Found.Pnp3 (OM PE pe InPo)
open Found.Pnp4 (InNPo)
open Found.Pnp8 (mapC allBits)
open Found.Pnp9 (pow2)
/-! ## The T329 definition is degenerate -/
theorem t329_degenerate (M : Found.RootComp.Machine) (f : List Bool -> List Bool)
(easy : Exists fun c => Exists fun k => forall x, (f x).length <= Found.RootComp.pbound c k x.length) :
Found.RootComp.OneWay M f (fun _ _ => (0, 1)) :=
Found.RootComp.OneWay.mk easy (fun _ c k => Exists.intro 0 (fun n _ => by
show 0 * Found.RootComp.pbound c k n <= 1
rw [Nat.zero_mul]
exact Nat.zero_le 1))
/-- Even the identity is "one-way" in the T329 sense. -/
theorem t329_identity (M : Found.RootComp.Machine) : Found.RootComp.OneWay M (fun x => x) (fun _ _ => (0, 1)) :=
t329_degenerate M _ (Exists.intro 1 (Exists.intro 1 (fun x => by
show x.length <= 1 * (1 * x.length) + 1
rw [Nat.one_mul, Nat.one_mul]
exact Nat.le_succ _)))
/-! ## Success counted from runs -/
def beqL : List Bool -> List Bool -> Bool
| [], [] => true
| [], _ :: _ => false
| _ :: _, [] => false
| a :: l, b :: m => (a == b) && beqL l m
def count (g : List Bool -> Bool) : List (List Bool) -> Nat
| [] => 0
| x :: xs => count g xs + (match g x with | true => 1 | false => 0)
def invOut (f : List Bool -> List Bool) (x : List Bool) : Option (List Bool) -> Bool
| none => false
| some y => beqL (f y) (f x)
/-- inv, run within budget t on f x, returns a preimage of f x. -/
def inverts (M : OM) (f : List Bool -> List Bool) (inv : M.Prog) (t : Nat) (x : List Bool) : Bool :=
invOut f x (M.run inv t (f x))
def succN (M : OM) (f : List Bool -> List Bool) (inv : M.Prog) (e : PE) (n : Nat) : Nat :=
count (inverts M f inv (pe e n)) (allBits n)
structure OneWay2 (M : OM) (f : List Bool -> List Bool) : Prop where
easy : Exists fun pf : M.Prog => Exists fun e : PE => forall x, M.run pf (pe e x.length) x = some (f x)
hard : forall (inv : M.Prog) (e : PE), Exists fun n0 => forall n, n0 <= n ->
succN M f inv e n * (pe e n + 2) <= pow2 n
def SearchEasy (M : OM) (f : List Bool -> List Bool) : Prop :=
Exists fun inv : M.Prog => Exists fun e : PE => forall x, inverts M f inv (pe e x.length) x = true
/-! ## Counting -/
theorem count_app (g : List Bool -> Bool) (ys : List (List Bool)) :
forall xs, count g (xs ++ ys) = count g xs + count g ys := by
intro xs
induction xs with
| nil => exact (Nat.zero_add _).symm
| cons x xs ih =>
show count g (xs ++ ys) + _ = count g xs + _ + count g ys
rw [ih, Nat.add_right_comm]
theorem count_mapC (g : List Bool -> Bool) (b : Bool) :
forall xs, count g (mapC b xs) = count (fun x => g (b :: x)) xs := by
intro xs
induction xs with
| nil => rfl
| cons x xs ih =>
show count g (mapC b xs) + _ = count (fun x => g (b :: x)) xs + _
rw [ih]
theorem count_all : forall n (g : List Bool -> Bool), (forall x, x.length = n -> g x = true) ->
count g (allBits n) = pow2 n := by
intro n
induction n with
| zero =>
intro g h
show 0 + (match g [] with | true => 1 | false => 0) = 1
rw [h [] rfl]
rfl
| succ n ih =>
intro g h
show count g (mapC false (allBits n) ++ mapC true (allBits n)) = pow2 n + pow2 n
rw [count_app, count_mapC, count_mapC]
rw [ih (fun x => g (false :: x)) (fun x hx => h (false :: x) (congrArg Nat.succ hx))]
rw [ih (fun x => g (true :: x)) (fun x hx => h (true :: x) (congrArg Nat.succ hx))]
theorem pow2_pos : forall n, 1 <= pow2 n := by
intro n
induction n with
| zero => exact Nat.le_refl 1
| succ n ih => exact Nat.le_trans ih (Nat.le_add_right _ _)
theorem not_double (P Y : Nat) (hP : 1 <= P) (h : P * (Y + 2) <= P) : False := by
have h1 : P + P <= P * (Y + 2) := Nat.add_le_add_right (Nat.le_add_left P (P * Y)) P
have h2 : P + 1 <= P + P := Nat.add_le_add_left hP P
exact Nat.not_succ_le_self P (Nat.le_trans h2 (Nat.le_trans h1 h))
/-! ## One-way functions against search -/
theorem owf_not_search (M : OM) (f : List Bool -> List Bool) (ho : OneWay2 M f) (hs : SearchEasy M f) : False :=
match hs with
| Exists.intro inv (Exists.intro e he) =>
match ho.hard inv e with
| Exists.intro n0 hn =>
have hc : succN M f inv e n0 = pow2 n0 :=
count_all n0 (inverts M f inv (pe e n0)) (fun x hx => by rw [hx.symm]; exact he x)
have hb := hn n0 (Nat.le_refl n0)
not_double (pow2 n0) (pe e n0) (pow2_pos n0) (by rw [hc] at hb; exact hb)
/-- Search-to-decision as a law: if NP is inside P, every function computed in polynomial
time is polynomial-time invertible (open for a concrete model: OPEN_LEMMAS_PNP O5). -/
def S2D (M : OM) : Prop :=
(forall L, InNPo M L -> InPo M L) ->
forall f : List Bool -> List Bool,
(Exists fun pf : M.Prog => Exists fun e : PE => forall x, M.run pf (pe e x.length) x = some (f x)) ->
SearchEasy M f
theorem owf_p_ne_np (M : OM) (hs : S2D M) (f : List Bool -> List Bool) (ho : OneWay2 M f) :
(forall L, InNPo M L -> InPo M L) -> False :=
fun heq => owf_not_search M f ho (hs heq f ho.easy)
end Found.Owf1
#print axioms Found.Owf1.t329_degenerate
#print axioms Found.Owf1.t329_identity
#print axioms Found.Owf1.count_app
#print axioms Found.Owf1.count_mapC
#print axioms Found.Owf1.count_all
#print axioms Found.Owf1.pow2_pos
#print axioms Found.Owf1.not_double
#print axioms Found.Owf1.owf_not_search
#print axioms Found.Owf1.owf_p_ne_np
#print axioms: 9 theorems, no axioms.
Each string of length \(n\) occurs once among all strings of length \(n\), and strings in an image are counted by their seeds.
found/Owf2.lean
/-
T332 piece 2. found/Owf2 -- counting tools for pseudorandom generators: a list of all bit
strings of length n holds each string once, and the strings in an image are counted by
their preimages.
Counts are over the list allBits n (Pnp8) and use Owf1's count and beqL. Proved:
* beqL_refl, beqL_eq: beqL decides equality of bit lists;
* count_mono, count_le_add, count_zero: monotone and subadditive counting;
* count_one, count_le_one: a string of length n occurs exactly once in allBits n, and
any string at most once;
* image_count: if every u counted by g has a preimage s of length n under G, then the
strings of a list holding each string at most once that g counts are at most the
seeds s in allBits n with g (G s): #{u : g u} <= #{s : g (G s)}.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Owf1
namespace Found.Owf2
open Found.Pnp8 (mapC allBits)
open Found.Owf1 (beqL count count_app count_mapC)
def bv (b : Bool) : Nat := match b with | true => 1 | false => 0
theorem beqB_eq (a b : Bool) (h : (a == b) = true) : a = b := by
cases a with
| false => cases b with
| false => rfl
| true => exact Bool.noConfusion h
| true => cases b with
| false => exact Bool.noConfusion h
| true => rfl
theorem beqL_refl : forall x : List Bool, beqL x x = true := by
intro x
induction x with
| nil => rfl
| cons a x ih =>
show ((a == a) && beqL x x) = true
rw [ih]
cases a with
| false => rfl
| true => rfl
theorem and_left {a b : Bool} (h : (a && b) = true) : a = true := by
cases a with
| false => exact Bool.noConfusion h
| true => rfl
theorem and_right {a b : Bool} (h : (a && b) = true) : b = true := by
cases a with
| false => exact Bool.noConfusion h
| true => exact h
theorem beqL_eq : forall x y : List Bool, beqL x y = true -> x = y := by
intro x
induction x with
| nil =>
intro y h
cases y with
| nil => rfl
| cons b y => exact Bool.noConfusion h
| cons a x ih =>
intro y h
cases y with
| nil => exact Bool.noConfusion h
| cons b y =>
have h' : ((a == b) && beqL x y) = true := h
rw [beqB_eq a b (and_left h'), ih y (and_right h')]
/-! ## Counting -/
theorem bv_le (a b : Bool) (h : a = true -> b = true) : bv a <= bv b := by
cases a with
| false => exact Nat.zero_le _
| true => rw [h rfl]; exact Nat.le_refl 1
theorem count_mono (f g : List Bool -> Bool) (h : forall u, f u = true -> g u = true) :
forall A, count f A <= count g A := by
intro A
induction A with
| nil => exact Nat.le_refl 0
| cons u A ih => exact Nat.add_le_add ih (bv_le (f u) (g u) (h u))
theorem count_zero (f : List Bool -> Bool) (h : forall u, f u = false) : forall A, count f A = 0 := by
intro A
induction A with
| nil => rfl
| cons u A ih =>
show count f A + bv (f u) = 0
rw [ih, h u]
rfl
theorem bv_or (a b : Bool) : bv (a || b) <= bv a + bv b := by
cases a with
| false => exact Nat.le_of_eq (Nat.zero_add _).symm
| true => exact Nat.le_add_right 1 _
theorem count_le_add (f f1 f2 : List Bool -> Bool) (h : forall u, bv (f u) <= bv (f1 u) + bv (f2 u)) :
forall A, count f A <= count f1 A + count f2 A := by
intro A
induction A with
| nil => exact Nat.le_refl 0
| cons u A ih =>
show count f A + bv (f u) <= count f1 A + bv (f1 u) + (count f2 A + bv (f2 u))
refine Nat.le_trans (Nat.add_le_add ih (h u)) (Nat.le_of_eq ?_)
rw [Nat.add_assoc, Nat.add_assoc, Nat.add_left_comm (count f2 A) (bv (f1 u)) (bv (f2 u))]
/-! ## Each string once in allBits -/
theorem count_one : forall n (x : List Bool), x.length = n -> count (fun y => beqL y x) (allBits n) = 1 := by
intro n
induction n with
| zero =>
intro x hx
cases x with
| nil => rfl
| cons b x => exact Nat.noConfusion hx
| succ n ih =>
intro x hx
cases x with
| nil => exact Nat.noConfusion hx
| cons b x =>
have hx' : x.length = n := Nat.succ.inj hx
show count (fun y => beqL y (b :: x)) (mapC false (allBits n) ++ mapC true (allBits n)) = 1
rw [count_app, count_mapC, count_mapC]
cases b with
| false =>
show count (fun y => beqL y x) (allBits n) + count (fun _ => false) (allBits n) = 1
rw [ih x hx', count_zero (fun _ => false) (fun _ => rfl)]
| true =>
show count (fun _ => false) (allBits n) + count (fun y => beqL y x) (allBits n) = 1
rw [ih x hx', count_zero (fun _ => false) (fun _ => rfl)]
theorem count_le_one : forall n (x : List Bool), count (fun y => beqL y x) (allBits n) <= 1 := by
intro n
induction n with
| zero =>
intro x
show 0 + bv (beqL [] x) <= 1
rw [Nat.zero_add]
cases beqL [] x with
| false => exact Nat.zero_le 1
| true => exact Nat.le_refl 1
| succ n ih =>
intro x
show count (fun y => beqL y x) (mapC false (allBits n) ++ mapC true (allBits n)) <= 1
rw [count_app, count_mapC, count_mapC]
cases x with
| nil =>
show count (fun _ => false) (allBits n) + count (fun _ => false) (allBits n) <= 1
rw [count_zero (fun _ => false) (fun _ => rfl)]
exact Nat.zero_le 1
| cons b x =>
cases b with
| false =>
show count (fun y => beqL y x) (allBits n) + count (fun _ => false) (allBits n) <= 1
rw [count_zero (fun _ => false) (fun _ => rfl)]
exact ih x
| true =>
show count (fun _ => false) (allBits n) + count (fun y => beqL y x) (allBits n) <= 1
rw [count_zero (fun _ => false) (fun _ => rfl), Nat.zero_add]
exact ih x
/-! ## Images counted by preimages -/
/-- Some seed in B maps to u. -/
def hitc (G : List Bool -> List Bool) (u : List Bool) : List (List Bool) -> Bool
| [] => false
| s :: B => beqL u (G s) || hitc G u B
theorem or_l {a b : Bool} (h : a = true) : (a || b) = true := by rw [h]; rfl
theorem or_r {a b : Bool} (h : b = true) : (a || b) = true := by
cases a with
| false => exact h
| true => rfl
theorem hitc_of (G : List Bool -> List Bool) (u s : List Bool) (hs : G s = u) :
forall B, 1 <= count (fun y => beqL y s) B -> hitc G u B = true := by
intro B
induction B with
| nil => intro h; exact absurd h (Nat.not_succ_le_zero 0)
| cons s' B ih =>
intro h
show (beqL u (G s') || hitc G u B) = true
cases e : beqL s' s with
| true =>
have es : s' = s := beqL_eq s' s e
apply or_l
rw [es, hs]
exact beqL_refl u
| false =>
apply or_r
apply ih
have h' : 1 <= count (fun y => beqL y s) B + bv (beqL s' s) := h
rw [e] at h'
exact h'
theorem image_le (G : List Bool -> List Bool) (g : List Bool -> Bool) (A : List (List Bool))
(hA : forall v, count (fun y => beqL y v) A <= 1) :
forall B, count (fun u => g u && hitc G u B) A <= count (fun s => g (G s)) B := by
intro B
induction B with
| nil =>
show count (fun u => g u && false) A <= 0
rw [count_zero _ (fun u => by cases g u with | false => rfl | true => rfl)]
exact Nat.le_refl 0
| cons s B ih =>
show count (fun u => g u && (beqL u (G s) || hitc G u B)) A <= count (fun s => g (G s)) B + bv (g (G s))
have hsplit := count_le_add (fun u => g u && (beqL u (G s) || hitc G u B))
(fun u => g u && beqL u (G s)) (fun u => g u && hitc G u B)
(fun u => by
cases g u with
| false => exact Nat.zero_le _
| true => exact bv_or _ _) A
have hone : count (fun u => g u && beqL u (G s)) A <= bv (g (G s)) := by
cases eg : g (G s) with
| true =>
exact Nat.le_trans (count_mono _ (fun y => beqL y (G s)) (fun u hu => and_right hu) A) (hA (G s))
| false =>
rw [count_zero _ (fun u => by
cases eu : beqL u (G s) with
| false => cases g u with | false => rfl | true => rfl
| true =>
have e1 : u = G s := beqL_eq u (G s) eu
rw [e1, eg]
rfl)]
exact Nat.zero_le 0
refine Nat.le_trans hsplit ?_
rw [Nat.add_comm (count (fun s => g (G s)) B)]
exact Nat.add_le_add hone ih
theorem image_count (G : List Bool -> List Bool) (g : List Bool -> Bool) (n : Nat) (A : List (List Bool))
(hA : forall v, count (fun y => beqL y v) A <= 1)
(hpre : forall u, g u = true -> Exists fun s => s.length = n /\ G s = u) :
count g A <= count (fun s => g (G s)) (allBits n) := by
refine Nat.le_trans (count_mono g (fun u => g u && hitc G u (allBits n)) ?_ A) (image_le G g A hA (allBits n))
intro u hu
cases hpre u hu with
| intro s hs =>
rw [hu, hitc_of G u s hs.2 (allBits n) (Nat.le_of_eq (count_one n s hs.1).symm)]
rfl
end Found.Owf2
#print axioms Found.Owf2.beqB_eq
#print axioms Found.Owf2.beqL_refl
#print axioms Found.Owf2.beqL_eq
#print axioms Found.Owf2.count_mono
#print axioms Found.Owf2.count_zero
#print axioms Found.Owf2.count_le_add
#print axioms Found.Owf2.count_one
#print axioms Found.Owf2.count_le_one
#print axioms Found.Owf2.hitc_of
#print axioms Found.Owf2.image_le
#print axioms Found.Owf2.image_count
#print axioms: 11 theorems, no axioms.
A generator stretching by one bit that fools every polynomial-time distinguisher is a one-way function (the easy half of Håstad–Impagliazzo–Levin–Luby 1999).
found/Owf3.lean
/-
T332 piece 3. found/Owf3 -- a pseudorandom generator is a one-way function (the easy half
of Hastad--Impagliazzo--Levin--Luby 1999, read off the chain).
Over the machine model with output (Pnp3.OM), with counts over all strings (Owf1, Owf2):
* PRG M G: G stretches by one bit, is computed in polynomial time, and every
distinguisher D with budget b has, for every polynomial e and all large n, advantage
(2 cntG - cntU) / 2^(n+1) at most 1 / (e(n) + 2), where cntG counts seeds s of length
n with D (G s) = 1 and cntU counts strings u of length n + 1 with D u = 1;
* DistLaw M G: for an inverter inv and budget e there is a program D that, on u, runs
inv on u and outputs whether G maps the result to u (a law of the model: running one
program, then G, then comparing);
* prg_owf: a PRG (with DistLaw) is a one-way function in the sense of Owf1.OneWay2.
Proof: the inversion distinguisher accepts G s exactly when inv inverts s, so cntG is the
inverter's success S; every u it accepts has a seed of length n (stretch), so cntU <= S
(Owf2.image_count); the advantage bound then gives S (e(n) + 2) <= 2^n.
Relativizes (a bridge; PNP_MAP.md). ASCII forms only. CIC dependence: none.
-/
import found.Owf2
import found.Pnp20
namespace Found.Owf3
open Found.Pnp3 (OM PE pe)
open Found.Pnp8 (mapC allBits)
open Found.Pnp9 (pow2)
open Found.Owf1 (beqL count count_app count_mapC succN inverts invOut OneWay2)
open Found.Owf2 (bv beqL_eq count_le_one image_count)
def bitOut : Option (List Bool) -> Bool
| none => false
| some [] => false
| some (b :: _) => b
def acc (M : OM) (D : M.Prog) (t : Nat) (u : List Bool) : Bool := bitOut (M.run D t u)
def cntG (M : OM) (G : List Bool -> List Bool) (D : M.Prog) (b : PE) (n : Nat) : Nat :=
count (fun s => acc M D (pe b n) (G s)) (allBits n)
def cntU (M : OM) (D : M.Prog) (b : PE) (n : Nat) : Nat :=
count (acc M D (pe b n)) (allBits (n + 1))
structure PRG (M : OM) (G : List Bool -> List Bool) : Prop where
easy : Exists fun pf : M.Prog => Exists fun e : PE => forall x, M.run pf (pe e x.length) x = some (G x)
stretch : forall s, (G s).length = s.length + 1
fool : forall (D : M.Prog) (b e : PE), Exists fun n0 => forall n, n0 <= n ->
(cntG M G D b n + cntG M G D b n) * (pe e n + 2) <= cntU M D b n * (pe e n + 2) + pow2 (n + 1)
/-- Output of the inversion test on u. -/
def outHit (G : List Bool -> List Bool) (u : List Bool) : Option (List Bool) -> Bool
| none => false
| some y => beqL (G y) u
def DistLaw (M : OM) (G : List Bool -> List Bool) : Prop :=
forall (inv : M.Prog) (e : PE), Exists fun D : M.Prog =>
forall n u, acc M D (pe e n) u = outHit G u (M.run inv (pe e n) u)
/-! ## Counting helpers -/
theorem count_ext (f g : List Bool -> Bool) (h : forall x, f x = g x) : forall A, count f A = count g A := by
intro A
induction A with
| nil => rfl
| cons x A ih =>
show count f A + bv (f x) = count g A + bv (g x)
rw [ih, h x]
theorem count_ext_len : forall m (f g : List Bool -> Bool), (forall x, x.length = m -> f x = g x) ->
count f (allBits m) = count g (allBits m) := by
intro m
induction m with
| zero =>
intro f g h
show 0 + bv (f []) = 0 + bv (g [])
rw [h [] rfl]
| succ m ih =>
intro f g h
show count f (mapC false (allBits m) ++ mapC true (allBits m)) =
count g (mapC false (allBits m) ++ mapC true (allBits m))
rw [count_app, count_app, count_mapC, count_mapC, count_mapC, count_mapC]
rw [ih (fun x => f (false :: x)) (fun x => g (false :: x)) (fun x hx => h (false :: x) (congrArg Nat.succ hx))]
rw [ih (fun x => f (true :: x)) (fun x => g (true :: x)) (fun x hx => h (true :: x) (congrArg Nat.succ hx))]
theorem add_cancel_le : forall a b c : Nat, a + b <= a + c -> b <= c := by
intro a
induction a with
| zero => intro b c h; rw [Nat.zero_add, Nat.zero_add] at h; exact h
| succ a ih =>
intro b c h
rw [Nat.succ_add, Nat.succ_add] at h
exact ih b c (Nat.le_of_succ_le_succ h)
theorem half_le : forall a b : Nat, a + a <= b + b -> a <= b := by
intro a
induction a with
| zero => intro b _; exact Nat.zero_le b
| succ a ih =>
intro b h
cases b with
| zero => exact absurd h (Nat.not_succ_le_zero _)
| succ b =>
have h1 : a + 1 + a <= b + 1 + b := Nat.le_of_succ_le_succ h
rw [Nat.succ_add, Nat.succ_add] at h1
exact Nat.succ_le_succ (ih b (Nat.le_of_succ_le_succ h1))
theorem add_mul' (a b : Nat) : forall X, (a + b) * X = a * X + b * X := by
intro X
induction X with
| zero => rfl
| succ X ih =>
show (a + b) * X + (a + b) = (a * X + a) + (b * X + b)
rw [ih, Nat.add_assoc, Nat.add_assoc, Nat.add_left_comm (b * X) a b]
theorem beq_self (n : Nat) : Nat.beq n n = true := Found.Pnp20.beq_refl' n
theorem and_true' (a : Bool) : (a && true) = a := by
cases a with
| false => rfl
| true => rfl
/-! ## PRG => OWF -/
/-- Twice the bound plus two: pe (e2 e) n + 2 = (pe e n + 2) + (pe e n + 2). -/
def e2 (e : PE) : PE := .add (.add e (.c 2)) e
theorem prg_owf (M : OM) (G : List Bool -> List Bool) (hp : PRG M G) (hl : DistLaw M G) : OneWay2 M G := by
refine OneWay2.mk hp.easy ?_
intro inv e
cases hl inv e with
| intro D hD =>
cases hp.fool D e (e2 e) with
| intro n0 hn =>
refine Exists.intro n0 (fun n hn0 => ?_)
have hG : cntG M G D e n = succN M G inv e n := by
refine count_ext _ _ (fun s => ?_) (allBits n)
show acc M D (pe e n) (G s) = invOut G s (M.run inv (pe e n) (G s))
rw [hD n (G s)]
cases M.run inv (pe e n) (G s) with
| none => rfl
| some y => rfl
have hU : cntU M D e n <= succN M G inv e n := by
have h1 : cntU M D e n =
count (fun u => acc M D (pe e n) u && Nat.beq u.length (n + 1)) (allBits (n + 1)) :=
count_ext_len (n + 1) _ _ (fun x hx => by rw [hx, beq_self, and_true'])
have h2 := image_count G (fun u => acc M D (pe e n) u && Nat.beq u.length (n + 1)) n (allBits (n + 1))
(fun v => count_le_one (n + 1) v)
(fun u hu => by
have ha := Found.Owf2.and_left hu
have hlen : u.length = n + 1 := Found.Pnp2.beq_eq _ _ (Found.Owf2.and_right hu)
rw [hD n u] at ha
cases hr : M.run inv (pe e n) u with
| none =>
rw [hr] at ha
exact Bool.noConfusion ha
| some y =>
rw [hr] at ha
have hy : G y = u := beqL_eq (G y) u ha
refine Exists.intro y (And.intro ?_ hy)
have hs := hp.stretch y
rw [hy, hlen] at hs
exact (Nat.succ.inj hs).symm)
have h3 : count (fun s => acc M D (pe e n) (G s) && Nat.beq (G s).length (n + 1)) (allBits n) =
cntG M G D e n :=
count_ext_len n _ _ (fun x hx => by rw [hp.stretch x, hx, beq_self, and_true'])
rw [h1]
exact Nat.le_trans h2 (Nat.le_of_eq (h3.trans hG))
have hb := hn n hn0
rw [hG] at hb
have hb2 : (succN M G inv e n + succN M G inv e n) * (pe (e2 e) n + 2) <=
succN M G inv e n * (pe (e2 e) n + 2) + pow2 (n + 1) :=
Nat.le_trans hb (Nat.add_le_add_right (Nat.mul_le_mul hU (Nat.le_refl _)) _)
rw [add_mul'] at hb2
have hc := add_cancel_le _ _ _ hb2
have hx : succN M G inv e n * (pe (e2 e) n + 2) =
succN M G inv e n * (pe e n + 2) + succN M G inv e n * (pe e n + 2) := by
show succN M G inv e n * ((pe e n + 2) + (pe e n + 2)) = _
rw [Nat.left_distrib]
rw [hx] at hc
exact half_le _ (pow2 n) hc
end Found.Owf3
#print axioms Found.Owf3.count_ext
#print axioms Found.Owf3.count_ext_len
#print axioms Found.Owf3.add_cancel_le
#print axioms Found.Owf3.half_le
#print axioms Found.Owf3.add_mul'
#print axioms Found.Owf3.beq_self
#print axioms Found.Owf3.and_true'
#print axioms Found.Owf3.prg_owf
#print axioms: 8 theorems, no axioms.
Resolution proofs as trees over the CNFs of the computation root; every proved clause holds under every satisfying assignment, so a refutation shows unsatisfiability.
found/Pnp62.lean
/-
T330 piece 62. found/Pnp62 -- the resolution proof system on the CNFs of RootComp, with its
soundness (the first system of the proof complexity candidate; PNP_MAP.md).
A resolution proof of a clause from a CNF F is a tree: leaves are clauses of F, and an
inner node resolves two clauses on a variable v (one has the literal (true, v), the other
(false, v)); the resolvent contains all other literals of both (weakening allowed). The
size is the number of nodes. Proved:
* beqLit_eq, beqCl_eq: literal and clause comparison decide equality;
* mem_true, of_mem: a clause is true iff one of its literals is;
* res_sound: an assignment satisfying both premises satisfies the resolvent;
* sound: every clause with a resolution proof from F is true under every assignment
satisfying F;
* refute_unsat: a resolution refutation (a proof of the empty clause) shows F
unsatisfiable.
Written with ASCII forms only. CIC dependence: none.
-/
import found.RootComp
import found.Pnp2
namespace Found.Pnp62
open Found.RootComp (Lit Clause CNF evalLit evalClause evalCNF Sat)
def beqB (a b : Bool) : Bool :=
match a, b with
| true, true => true
| false, false => true
| true, false => false
| false, true => false
def beqLit (l m : Lit) : Bool := beqB l.1 m.1 && Nat.beq l.2 m.2
def memL (l : Lit) : Clause -> Bool
| [] => false
| m :: c => beqLit m l || memL l c
def beqCl : Clause -> Clause -> Bool
| [], [] => true
| [], _ :: _ => false
| _ :: _, [] => false
| l :: c, m :: d => beqLit l m && beqCl c d
def memC (C : Clause) : CNF -> Bool
| [] => false
| D :: F => beqCl D C || memC C F
theorem beqB_eq (a b : Bool) (h : beqB a b = true) : a = b := by
cases a with
| false => cases b with
| false => rfl
| true => exact Bool.noConfusion h
| true => cases b with
| false => exact Bool.noConfusion h
| true => rfl
theorem and_l {a b : Bool} (h : (a && b) = true) : a = true := by
cases a with
| false => exact Bool.noConfusion h
| true => rfl
theorem and_r {a b : Bool} (h : (a && b) = true) : b = true := by
cases a with
| false => exact Bool.noConfusion h
| true => exact h
theorem or_l {a b : Bool} (h : a = true) : (a || b) = true := by rw [h]; rfl
theorem or_r {a b : Bool} (h : b = true) : (a || b) = true := by
cases a with
| false => exact h
| true => rfl
theorem beqLit_eq (l m : Lit) (h : beqLit l m = true) : l = m := by
cases l with
| mk a i =>
cases m with
| mk b j =>
have h1 : a = b := beqB_eq a b (and_l h)
have h2 : i = j := Found.Pnp2.beq_eq i j (and_r h)
rw [h1, h2]
theorem beqB_refl (a : Bool) : beqB a a = true := by
cases a with
| false => rfl
| true => rfl
theorem beq_refl (n : Nat) : Nat.beq n n = true := by
induction n with
| zero => rfl
| succ n ih => exact ih
theorem beqLit_refl (l : Lit) : beqLit l l = true := by
show (beqB l.1 l.1 && Nat.beq l.2 l.2) = true
rw [beqB_refl, beq_refl]
rfl
theorem beqCl_eq : forall C D : Clause, beqCl C D = true -> C = D := by
intro C
induction C with
| nil =>
intro D h
cases D with
| nil => rfl
| cons m D => exact Bool.noConfusion h
| cons l C ih =>
intro D h
cases D with
| nil => exact Bool.noConfusion h
| cons m D =>
have h' : (beqLit l m && beqCl C D) = true := h
rw [beqLit_eq l m (and_l h'), ih D (and_r h')]
/-! ## Truth of clauses through literals -/
theorem mem_true (a : Nat -> Bool) : forall C : Clause, evalClause a C = true ->
Exists fun l => memL l C = true /\ evalLit a l = true := by
intro C
induction C with
| nil => intro h; exact Bool.noConfusion h
| cons m C ih =>
intro h
have h' : (evalLit a m || evalClause a C) = true := h
cases e : evalLit a m with
| true => exact Exists.intro m (And.intro (or_l (beqLit_refl m)) e)
| false =>
rw [e] at h'
cases ih h' with
| intro l hl => exact Exists.intro l (And.intro (or_r hl.1) hl.2)
theorem of_mem (a : Nat -> Bool) (l : Lit) (hl : evalLit a l = true) :
forall R : Clause, memL l R = true -> evalClause a R = true := by
intro R
induction R with
| nil => intro h; exact Bool.noConfusion h
| cons m R ih =>
intro h
show (evalLit a m || evalClause a R) = true
have h' : (beqLit m l || memL l R) = true := h
cases e : beqLit m l with
| true =>
rw [beqLit_eq m l e]
exact or_l hl
| false =>
rw [e] at h'
exact or_r (ih h')
theorem cnf_mem (a : Nat -> Bool) (C : Clause) : forall F : CNF, evalCNF a F = true -> memC C F = true ->
evalClause a C = true := by
intro F
induction F with
| nil => intro _ h; exact Bool.noConfusion h
| cons D F ih =>
intro hF h
have hF' : (evalClause a D && evalCNF a F) = true := hF
have h' : (beqCl D C || memC C F) = true := h
cases e : beqCl D C with
| true =>
rw [(beqCl_eq D C e).symm]
exact and_l hF'
| false =>
rw [e] at h'
exact ih (and_r hF') h'
/-! ## Resolution -/
/-- R is a resolvent of C1 (holding (true, v)) and C2 (holding (false, v)), with weakening. -/
structure ResOK (C1 C2 : Clause) (v : Nat) (R : Clause) : Prop where
pos : memL (true, v) C1 = true
neg : memL (false, v) C2 = true
keep1 : forall l, memL l C1 = true -> beqLit l (true, v) = false -> memL l R = true
keep2 : forall l, memL l C2 = true -> beqLit l (false, v) = false -> memL l R = true
theorem res_sound (a : Nat -> Bool) (C1 C2 R : Clause) (v : Nat) (h : ResOK C1 C2 v R)
(h1 : evalClause a C1 = true) (h2 : evalClause a C2 = true) : evalClause a R = true := by
cases mem_true a C1 h1 with
| intro l1 hl1 =>
cases e1 : beqLit l1 (true, v) with
| false => exact of_mem a l1 hl1.2 R (h.keep1 l1 hl1.1 e1)
| true =>
have hv : a v = true := by
have t := hl1.2
rw [beqLit_eq l1 (true, v) e1] at t
exact t
cases mem_true a C2 h2 with
| intro l2 hl2 =>
cases e2 : beqLit l2 (false, v) with
| false => exact of_mem a l2 hl2.2 R (h.keep2 l2 hl2.1 e2)
| true =>
have t := hl2.2
rw [beqLit_eq l2 (false, v) e2] at t
have t' : (!(a v)) = true := t
rw [hv] at t'
exact Bool.noConfusion t'
/-- Resolution proofs (trees) of a clause from F. -/
inductive RProof (F : CNF) : Clause -> Type where
| ax (C : Clause) (h : memC C F = true) : RProof F C
| res (C1 C2 R : Clause) (v : Nat) (p1 : RProof F C1) (p2 : RProof F C2) (h : ResOK C1 C2 v R) : RProof F R
def RProof.size {F : CNF} : {C : Clause} -> RProof F C -> Nat
| _, .ax _ _ => 1
| _, .res _ _ _ _ p1 p2 _ => p1.size + p2.size + 1
theorem sound (F : CNF) (a : Nat -> Bool) (hF : evalCNF a F = true) :
forall (C : Clause), RProof F C -> evalClause a C = true := by
intro C p
induction p with
| ax C h => exact cnf_mem a C F hF h
| res C1 C2 R v _ _ h ih1 ih2 => exact res_sound a C1 C2 R v h ih1 ih2
theorem refute_unsat (F : CNF) (p : RProof F []) : Sat F -> False := by
intro hs
cases hs with
| intro a ha =>
have t := sound F a ha [] p
exact Bool.noConfusion t
end Found.Pnp62
#print axioms Found.Pnp62.beqB_eq
#print axioms Found.Pnp62.beqLit_eq
#print axioms Found.Pnp62.beqLit_refl
#print axioms Found.Pnp62.beqCl_eq
#print axioms Found.Pnp62.mem_true
#print axioms Found.Pnp62.of_mem
#print axioms Found.Pnp62.cnf_mem
#print axioms Found.Pnp62.res_sound
#print axioms Found.Pnp62.sound
#print axioms Found.Pnp62.refute_unsat
#print axioms: 10 theorems, no axioms.
Polynomially bounded proof systems are NP certificates; if the complement of an NP language has none, the language is outside P.
found/Pnp63.lean
/-
T330 piece 63. found/Pnp63 -- Cook--Reckhow proof systems and the bridge to P /= NP.
A proof system for a language L over a machine model of RootComp (Cook--Reckhow 1979) is a
program that checks proofs w of members x in polynomial time: L x holds iff some w is
accepted. It is polynomially bounded when every member has an accepted proof of
polynomial length. Proved:
* pb_np, np_pb: polynomially bounded proof systems for L are exactly NP certificates for
L (both directions, by the definitions);
* cr_bridge: for L in NP, if the complement of L has no polynomially bounded proof
system, then L is not in P (with Pnp61's laws). For L = SAT, the complement is the
unsatisfiable CNFs (resolution, Pnp62, is one proof system for it); lower bounds for
every proof system are the proof complexity route to P /= NP.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp61
namespace Found.Pnp63
open Found.RootComp (Machine InP InNP pbound)
open Found.Pnp61 (compl InCoNP Laws p_ne_np_of_np_ne_conp)
/-- A proof system for L checked by program p within the bound c n^k + c. -/
def ProofSystem (M : Machine) (L : List Bool -> Bool) (p : M.Prog) (c k : Nat) : Prop :=
forall x, L x = true <->
Exists fun w : List Bool => M.run p (pbound c k (x.length + w.length)) (x ++ w) = some true
/-- Polynomially bounded: every member has an accepted proof of polynomial length. -/
def PolyBounded (M : Machine) (L : List Bool -> Bool) : Prop :=
Exists fun p : M.Prog => Exists fun c => Exists fun k =>
forall x, L x = true <->
Exists fun w : List Bool => w.length <= pbound c k x.length /\
M.run p (pbound c k (x.length + w.length)) (x ++ w) = some true
theorem pb_np (M : Machine) (L : List Bool -> Bool) (h : PolyBounded M L) : InNP M L := h
theorem np_pb (M : Machine) (L : List Bool -> Bool) (h : InNP M L) : PolyBounded M L := h
theorem cr_bridge (M : Machine) (hl : Laws M) (L : List Bool -> Bool) (hL : InNP M L)
(hpb : PolyBounded M (compl L) -> False) : InP M L -> False :=
p_ne_np_of_np_ne_conp M hl L hL (fun hc => hpb (np_pb M (compl L) hc))
end Found.Pnp63
#print axioms Found.Pnp63.pb_np
#print axioms Found.Pnp63.np_pb
#print axioms Found.Pnp63.cr_bridge
#print axioms: 3 theorems, no axioms.
A Delayer that never loses potential when it fixes a variable and loses at most one point when it offers both values forces every tree resolution refutation to have at least \(2^{\varphi (\rho _0)}\) nodes (Pudlák–Impagliazzo 2000).
found/Pnp64.lean
/-
T330 piece 64. found/Pnp64 -- the Prover--Delayer lower bound for tree resolution
(Pudlak--Impagliazzo 2000), in general form.
A Delayer is a set S of partial assignments with a potential phi (the points the Delayer is
still sure to score). If (D1) a state in S falsifying a clause of F has potential 0, and
(D2) at every state and unassigned variable the Delayer either fixes the variable without
losing potential or offers both values losing at most one point, then every tree
resolution proof (Pnp62) of a clause falsified by a state rho in S has at least
2^(phi rho) nodes. Proved:
* fals_ext: extending a partial assignment at an unassigned variable keeps every
falsified clause falsified;
* side_true, side_false: if rho falsifies the resolvent and sets v, it falsifies the
premise whose literal on v is false;
* pd_bound: size >= 2^(phi rho);
* pd_refute: every tree resolution refutation of F has at least 2^(phi rho0) nodes for
every rho0 in S.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp62
import found.Pnp9
namespace Found.Pnp64
open Found.RootComp (Lit Clause CNF)
open Found.Pnp62 (beqLit beqLit_eq memL memC ResOK RProof)
open Found.Pnp9 (pow2)
abbrev PA := Nat -> Option Bool
/-- rho falsifies C: every literal of C is set to its false value. -/
def Fals (rho : PA) (C : Clause) : Prop := forall l, memL l C = true -> rho l.2 = some (!l.1)
def selO : Bool -> Option Bool -> Option Bool -> Option Bool
| true, x, _ => x
| false, _, y => y
def ext (rho : PA) (v : Nat) (b : Bool) : PA := fun w => selO (Nat.beq w v) (some b) (rho w)
theorem pow2_mono : forall m n, m <= n -> pow2 m <= pow2 n := by
intro m n h
induction h with
| refl => exact Nat.le_refl _
| step _ ih => exact Nat.le_trans ih (Nat.le_add_right _ _)
theorem ext_same (rho : PA) (v : Nat) (b : Bool) : ext rho v b v = some b := by
show selO (Nat.beq v v) (some b) (rho v) = some b
rw [Found.Pnp62.beq_refl v]
rfl
theorem fals_ext (rho : PA) (v : Nat) (b : Bool) (hv : rho v = none) (C : Clause) (h : Fals rho C) :
Fals (ext rho v b) C := by
intro l hl
show selO (Nat.beq l.2 v) (some b) (rho l.2) = some (!l.1)
cases e : Nat.beq l.2 v with
| false => exact h l hl
| true =>
have ev : l.2 = v := Found.Pnp2.beq_eq _ _ e
have t := h l hl
rw [ev, hv] at t
cases t
theorem side_true (rho : PA) (C1 C2 R : Clause) (v : Nat) (hr : ResOK C1 C2 v R) (hR : Fals rho R)
(hv : rho v = some true) : Fals rho C2 := by
intro l hl
cases e : beqLit l (false, v) with
| false => exact hR l (hr.keep2 l hl e)
| true =>
rw [beqLit_eq l (false, v) e]
exact hv
theorem side_false (rho : PA) (C1 C2 R : Clause) (v : Nat) (hr : ResOK C1 C2 v R) (hR : Fals rho R)
(hv : rho v = some false) : Fals rho C1 := by
intro l hl
cases e : beqLit l (true, v) with
| false => exact hR l (hr.keep1 l hl e)
| true =>
rw [beqLit_eq l (true, v) e]
exact hv
/-- The Delayer's two rules. -/
structure Delayer (F : CNF) (S : PA -> Prop) (phi : PA -> Nat) : Prop where
lose : forall rho, S rho -> forall C, memC C F = true -> Fals rho C -> phi rho = 0
move : forall rho v, S rho -> rho v = none ->
Or (Exists fun b => S (ext rho v b) /\ phi rho <= phi (ext rho v b))
(S (ext rho v false) /\ S (ext rho v true) /\
phi rho <= phi (ext rho v false) + 1 /\ phi rho <= phi (ext rho v true) + 1)
theorem both_le (k a b : Nat) (ha : pow2 k <= a) (hb : pow2 k <= b) : pow2 (k + 1) <= a + b + 1 :=
Nat.le_trans (Nat.add_le_add ha hb) (Nat.le_add_right _ 1)
theorem pd_bound (F : CNF) (S : PA -> Prop) (phi : PA -> Nat) (hd : Delayer F S phi) :
forall (C : Clause) (p : RProof F C) (rho : PA), S rho -> Fals rho C -> pow2 (phi rho) <= p.size := by
intro C p
induction p with
| ax C h =>
intro rho hs hf
rw [hd.lose rho hs C h hf]
exact Nat.le_refl 1
| res C1 C2 R v p1 p2 hr ih1 ih2 =>
intro rho hs hf
show pow2 (phi rho) <= p1.size + p2.size + 1
have up1 : p1.size <= p1.size + p2.size + 1 :=
Nat.le_trans (Nat.le_add_right p1.size p2.size) (Nat.le_add_right _ 1)
have up2 : p2.size <= p1.size + p2.size + 1 :=
Nat.le_trans (Nat.le_add_left p2.size p1.size) (Nat.le_add_right _ 1)
-- a state that sets v and falsifies R
have setv : forall rho', S rho' -> Fals rho' R -> forall b, rho' v = some b ->
pow2 (phi rho') <= p1.size + p2.size + 1 := by
intro rho' hs' hf' b hb
cases b with
| true => exact Nat.le_trans (ih2 rho' hs' (side_true rho' C1 C2 R v hr hf' hb)) up2
| false => exact Nat.le_trans (ih1 rho' hs' (side_false rho' C1 C2 R v hr hf' hb)) up1
cases ev : rho v with
| some b => exact setv rho hs hf b ev
| none =>
cases hd.move rho v hs ev with
| inl hf1 =>
cases hf1 with
| intro b hb =>
exact Nat.le_trans (pow2_mono _ _ hb.2)
(setv (ext rho v b) hb.1 (fals_ext rho v b ev R hf) b (ext_same rho v b))
| inr h2 =>
have a0 := ih1 (ext rho v false) h2.1
(side_false _ C1 C2 R v hr (fals_ext rho v false ev R hf) (ext_same rho v false))
have a1 := ih2 (ext rho v true) h2.2.1
(side_true _ C1 C2 R v hr (fals_ext rho v true ev R hf) (ext_same rho v true))
cases ep : phi rho with
| zero => exact Nat.le_add_left 1 _
| succ k =>
have k0 : k <= phi (ext rho v false) := by
have t := h2.2.2.1
rw [ep] at t
exact Nat.le_of_succ_le_succ t
have k1 : k <= phi (ext rho v true) := by
have t := h2.2.2.2
rw [ep] at t
exact Nat.le_of_succ_le_succ t
exact both_le k _ _ (Nat.le_trans (pow2_mono _ _ k0) a0) (Nat.le_trans (pow2_mono _ _ k1) a1)
theorem pd_refute (F : CNF) (S : PA -> Prop) (phi : PA -> Nat) (hd : Delayer F S phi)
(p : RProof F []) (rho0 : PA) (h0 : S rho0) : pow2 (phi rho0) <= p.size :=
pd_bound F S phi hd [] p rho0 h0 (fun _ hl => Bool.noConfusion hl)
end Found.Pnp64
#print axioms Found.Pnp64.pow2_mono
#print axioms Found.Pnp64.ext_same
#print axioms Found.Pnp64.fals_ext
#print axioms Found.Pnp64.side_true
#print axioms Found.Pnp64.side_false
#print axioms Found.Pnp64.both_le
#print axioms Found.Pnp64.pd_bound
#print axioms Found.Pnp64.pd_refute
#print axioms: 8 theorems, no axioms.
The code \(x(i,j)=in+j\) is injective, and bounded counts and minima change by at most one under one changed index.
found/Pnp65.lean
/-
T330 piece 65. found/Pnp65 -- tools for the pigeonhole Delayer: the variable code
x(i, j) = i n + j, bounded or, bounded counts and bounded minima, with monotonicity.
Proved:
* add_cancel_r, var_inj: i n + j = i' n + j' with j, j' < n gives i = i', j = j';
* anyI_true, anyI_false, anyI_congr: bounded or;
* cntI_le, cntI_le_one, cntI_zero: bounded counts; one exceptional index costs at most 1;
* ble_false_le, mn_le_l, mn_le_r: minimum of two numbers;
* minU_le, minU_mono: the minimum of av i over unmarked i < k (default n) is at most each
such av i, and decreases by at most d when every index unmarked after a change was
unmarked before and lost at most d.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp64
namespace Found.Pnp65
def selN : Bool -> Nat -> Nat -> Nat
| true, x, _ => x
| false, _, y => y
def bv (b : Bool) : Nat := match b with | true => 1 | false => 0
/-! ## The variable code -/
def var (n i j : Nat) : Nat := i * n + j
theorem add_cancel_r : forall n a b : Nat, a + n = b + n -> a = b := by
intro n
induction n with
| zero => intro a b h; exact h
| succ n ih => intro a b h; exact ih a b (Nat.succ.inj h)
theorem not_lt_ge {j n : Nat} (hj : j < n) (h : n <= j) : False :=
Nat.not_succ_le_self j (Nat.le_trans hj h)
theorem var_inj (n : Nat) : forall i i' j j', j < n -> j' < n -> var n i j = var n i' j' -> i = i' /\ j = j' := by
intro i
induction i with
| zero =>
intro i' j j' hj hj' h
cases i' with
| zero =>
have e : j = j' := by
have h' : 0 * n + j = 0 * n + j' := h
rw [Nat.zero_mul, Nat.zero_add, Nat.zero_add] at h'
exact h'
exact And.intro rfl e
| succ k =>
have h' : 0 * n + j = (k + 1) * n + j' := h
rw [Nat.zero_mul, Nat.zero_add, Nat.succ_mul] at h'
exact absurd (Nat.le_trans (Nat.le_trans (Nat.le_add_left n (k * n)) (Nat.le_add_right _ j'))
(Nat.le_of_eq h'.symm)) (fun hh => not_lt_ge hj hh)
| succ k ih =>
intro i' j j' hj hj' h
cases i' with
| zero =>
have h' : (k + 1) * n + j = 0 * n + j' := h
rw [Nat.zero_mul, Nat.zero_add, Nat.succ_mul] at h'
exact absurd (Nat.le_trans (Nat.le_trans (Nat.le_add_left n (k * n)) (Nat.le_add_right _ j))
(Nat.le_of_eq h')) (fun hh => not_lt_ge hj' hh)
| succ k' =>
have h' : (k + 1) * n + j = (k' + 1) * n + j' := h
rw [Nat.succ_mul, Nat.succ_mul, Nat.add_right_comm (k * n) n j, Nat.add_right_comm (k' * n) n j'] at h'
have r := ih k' j j' hj hj' (add_cancel_r n _ _ h')
exact And.intro (congrArg Nat.succ r.1) r.2
/-! ## Bounded or -/
def anyI (g : Nat -> Bool) : Nat -> Bool
| 0 => false
| k + 1 => anyI g k || g k
theorem or_true_r (a : Bool) : (a || true) = true := by
cases a with
| false => rfl
| true => rfl
theorem anyI_true (g : Nat -> Bool) (j : Nat) (hg : g j = true) : forall k, j < k -> anyI g k = true := by
intro k
induction k with
| zero => intro h; exact absurd h (Nat.not_succ_le_zero j)
| succ k ih =>
intro h
show (anyI g k || g k) = true
have hle : j <= k := Nat.le_of_lt_succ h
cases hle with
| refl => rw [hg]; exact or_true_r _
| step hm => rw [ih (Nat.succ_le_succ hm)]; rfl
theorem anyI_false (g : Nat -> Bool) : forall k, anyI g k = false -> forall j, j < k -> g j = false := by
intro k
induction k with
| zero => intro _ j h; exact absurd h (Nat.not_succ_le_zero j)
| succ k ih =>
intro h j hj
have h' : (anyI g k || g k) = false := h
cases e1 : anyI g k with
| true => rw [e1] at h'; exact Bool.noConfusion h'
| false =>
rw [e1] at h'
have hle : j <= k := Nat.le_of_lt_succ hj
cases hle with
| refl => exact h'
| step hm => exact ih e1 j (Nat.succ_le_succ hm)
theorem anyI_congr (g g' : Nat -> Bool) : forall k, (forall j, j < k -> g j = g' j) -> anyI g k = anyI g' k := by
intro k
induction k with
| zero => intro _; rfl
| succ k ih =>
intro h
show (anyI g k || g k) = (anyI g' k || g' k)
rw [ih (fun j hj => h j (Nat.le_trans hj (Nat.le_succ k))), h k (Nat.le_refl _)]
/-! ## Bounded counts -/
def cntI (g : Nat -> Bool) : Nat -> Nat
| 0 => 0
| k + 1 => cntI g k + bv (g k)
theorem bv_le (a b : Bool) (h : a = true -> b = true) : bv a <= bv b := by
cases a with
| false => exact Nat.zero_le _
| true => rw [h rfl]; exact Nat.le_refl 1
theorem cntI_le (g g' : Nat -> Bool) : forall k, (forall j, j < k -> g j = true -> g' j = true) ->
cntI g k <= cntI g' k := by
intro k
induction k with
| zero => intro _; exact Nat.le_refl 0
| succ k ih =>
intro h
exact Nat.add_le_add (ih (fun j hj => h j (Nat.le_trans hj (Nat.le_succ k))))
(bv_le _ _ (h k (Nat.le_refl _)))
theorem cntI_zero (g : Nat -> Bool) : forall k, (forall j, j < k -> g j = false) -> cntI g k = 0 := by
intro k
induction k with
| zero => intro _; rfl
| succ k ih =>
intro h
show cntI g k + bv (g k) = 0
rw [ih (fun j hj => h j (Nat.le_trans hj (Nat.le_succ k))), h k (Nat.le_refl _)]
rfl
/-- One exceptional index j0 costs at most one. -/
theorem cntI_le_one (g g' : Nat -> Bool) (j0 : Nat) : forall k,
(forall j, j < k -> g j = true -> Nat.beq j j0 = false -> g' j = true) ->
cntI g k <= cntI g' k + 1 := by
intro k
induction k with
| zero => intro _; exact Nat.zero_le 1
| succ k ih =>
intro h
show cntI g k + bv (g k) <= cntI g' k + bv (g' k) + 1
cases e : Nat.beq k j0 with
| false =>
have a := ih (fun j hj => h j (Nat.le_trans hj (Nat.le_succ k)))
have b := bv_le (g k) (g' k) (fun hg => h k (Nat.le_refl _) hg e)
refine Nat.le_trans (Nat.add_le_add a b) (Nat.le_of_eq ?_)
rw [Nat.add_right_comm]
| true =>
have ek : k = j0 := Found.Pnp2.beq_eq k j0 e
have a : cntI g k <= cntI g' k := cntI_le g g' k (fun j hj hg => h j (Nat.le_trans hj (Nat.le_succ k)) hg
(Found.Pnp2.beq_false_of_ne j j0 (fun ej => not_lt_ge hj (Nat.le_of_eq (ek.trans ej.symm)))))
have b : bv (g k) <= 1 := by
cases g k with
| false => exact Nat.zero_le 1
| true => exact Nat.le_refl 1
exact Nat.le_trans (Nat.add_le_add a b) (Nat.add_le_add_right (Nat.le_add_right _ _) 1)
/-! ## Minima -/
def mn (a b : Nat) : Nat := selN (Nat.ble a b) a b
theorem ble_false_le : forall a b : Nat, Nat.ble a b = false -> b <= a := by
intro a
induction a with
| zero => intro b h; exact Bool.noConfusion h
| succ a ih =>
intro b h
cases b with
| zero => exact Nat.zero_le _
| succ b => exact Nat.succ_le_succ (ih b h)
theorem mn_le_l (a b : Nat) : mn a b <= a := by
show selN (Nat.ble a b) a b <= a
cases e : Nat.ble a b with
| true => exact Nat.le_refl a
| false => exact ble_false_le a b e
theorem mn_le_r (a b : Nat) : mn a b <= b := by
show selN (Nat.ble a b) a b <= b
cases e : Nat.ble a b with
| true => exact Nat.le_of_ble_eq_true e
| false => exact Nat.le_refl b
theorem mn_mono (a b a' b' d : Nat) (ha : a <= a' + d) (hb : b <= b' + d) : mn a b <= mn a' b' + d := by
show mn a b <= selN (Nat.ble a' b') a' b' + d
cases Nat.ble a' b' with
| true => exact Nat.le_trans (mn_le_l a b) ha
| false => exact Nat.le_trans (mn_le_r a b) hb
/-- The minimum of av i over the indices i < k with mt i = false; n if there are none. -/
def minU (n : Nat) (mt : Nat -> Bool) (av : Nat -> Nat) : Nat -> Nat
| 0 => n
| i + 1 => selN (mt i) (minU n mt av i) (mn (minU n mt av i) (av i))
theorem minU_step_le (n : Nat) (mt : Nat -> Bool) (av : Nat -> Nat) (i : Nat) :
minU n mt av (i + 1) <= minU n mt av i := by
show selN (mt i) (minU n mt av i) (mn (minU n mt av i) (av i)) <= minU n mt av i
cases mt i with
| true => exact Nat.le_refl _
| false => exact mn_le_l _ _
theorem minU_le (n : Nat) (mt : Nat -> Bool) (av : Nat -> Nat) (i : Nat) (hm : mt i = false) :
forall k, i < k -> minU n mt av k <= av i := by
intro k
induction k with
| zero => intro h; exact absurd h (Nat.not_succ_le_zero i)
| succ k ih =>
intro h
have hle : i <= k := Nat.le_of_lt_succ h
cases hle with
| refl =>
show selN (mt i) (minU n mt av i) (mn (minU n mt av i) (av i)) <= av i
rw [hm]
exact mn_le_r _ _
| step hm' => exact Nat.le_trans (minU_step_le n mt av _) (ih (Nat.succ_le_succ hm'))
theorem minU_mono (n d : Nat) (mt mt' : Nat -> Bool) (av av' : Nat -> Nat) : forall k,
(forall i, i < k -> mt' i = false -> mt i = false /\ av i <= av' i + d) ->
minU n mt av k <= minU n mt' av' k + d := by
intro k
induction k with
| zero => intro _; exact Nat.le_add_right n d
| succ k ih =>
intro h
have ih' := ih (fun i hi => h i (Nat.le_trans hi (Nat.le_succ k)))
show minU n mt av (k + 1) <= selN (mt' k) (minU n mt' av' k) (mn (minU n mt' av' k) (av' k)) + d
cases e' : mt' k with
| true => exact Nat.le_trans (minU_step_le n mt av k) ih'
| false =>
have hk := h k (Nat.le_refl _) e'
show selN (mt k) (minU n mt av k) (mn (minU n mt av k) (av k)) <= mn (minU n mt' av' k) (av' k) + d
rw [hk.1]
exact mn_mono _ _ _ _ d ih' hk.2
end Found.Pnp65
#print axioms Found.Pnp65.add_cancel_r
#print axioms Found.Pnp65.var_inj
#print axioms Found.Pnp65.anyI_true
#print axioms Found.Pnp65.anyI_false
#print axioms Found.Pnp65.anyI_congr
#print axioms Found.Pnp65.cntI_le
#print axioms Found.Pnp65.cntI_zero
#print axioms Found.Pnp65.cntI_le_one
#print axioms Found.Pnp65.ble_false_le
#print axioms Found.Pnp65.mn_le_l
#print axioms Found.Pnp65.mn_le_r
#print axioms Found.Pnp65.mn_mono
#print axioms Found.Pnp65.minU_step_le
#print axioms Found.Pnp65.minU_le
#print axioms Found.Pnp65.minU_mono
#print axioms: 15 theorems, no axioms.
The Delayer keeps a partial matching and the least number of available holes of an unmatched pigeon as potential; every tree resolution refutation of \(\mathrm {PHP}^{n+1}_n\) has at least \(2^n\) nodes.
found/Pnp66.lean
/-
T330 piece 66. found/Pnp66 -- tree resolution refutations of the pigeonhole principle
PHP(n+1, n) have at least 2^n nodes (a Prover--Delayer bound, Pnp64).
Variables x(i, j) = var n i j (pigeon i < n + 1 in hole j < n). Clauses: for each pigeon,
x(i, 0) or ... or x(i, n-1); for each hole j and pigeons i' < i, not x(i', j) or not
x(i, j). The Delayer keeps the true values a partial matching (S). Available holes of
pigeon i: unset x(i, j) with hole j not taken (av); potential phi = the least av over
unmatched pigeons (n at the start). Queried x(i, j): if pigeon i is matched or hole j
taken, answer false (phi kept); otherwise offer both (false: pigeon i loses one hole;
true: pigeon i leaves the minimum and other pigeons lose hole j); other variables: false.
Proved:
* lose: a state of S falsifying a clause of PHP has phi = 0;
* move: the Delayer's two rules;
* php_tree_lb: every tree resolution refutation of PHP(n+1, n) has >= 2^n nodes.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp65
namespace Found.Pnp66
open Found.RootComp (Lit Clause CNF)
open Found.Pnp62 (beqLit beqLit_refl memL memC beqCl beqCl_eq RProof or_l or_r)
open Found.Pnp64 (PA Fals ext selO ext_same Delayer pd_refute pow2_mono)
open Found.Pnp65
open Found.Pnp9 (pow2)
/-! ## The formula -/
def pcl (n i : Nat) : Nat -> Clause
| 0 => []
| j + 1 => (true, var n i j) :: pcl n i j
def pigeons (n : Nat) : Nat -> CNF
| 0 => []
| i + 1 => pcl n i n :: pigeons n i
def hcl (n j i : Nat) : Nat -> CNF
| 0 => []
| i' + 1 => [(false, var n i' j), (false, var n i j)] :: hcl n j i i'
def holesJ (n j : Nat) : Nat -> CNF
| 0 => []
| i + 1 => hcl n j i i ++ holesJ n j i
def holes (n : Nat) : Nat -> CNF
| 0 => []
| j + 1 => holesJ n j (n + 1) ++ holes n j
def PHP (n : Nat) : CNF := pigeons n (n + 1) ++ holes n n
/-! ## Membership -/
theorem beqCl_refl : forall C : Clause, beqCl C C = true := by
intro C
induction C with
| nil => rfl
| cons l C ih =>
show (beqLit l l && beqCl C C) = true
rw [beqLit_refl, ih]
rfl
theorem memC_cons (C D : Clause) (F : CNF) (h : memC C (D :: F) = true) : C = D \/ memC C F = true := by
have h' : (beqCl D C || memC C F) = true := h
cases e : beqCl D C with
| true => exact Or.inl (beqCl_eq D C e).symm
| false => rw [e] at h'; exact Or.inr h'
theorem memC_app (C : Clause) (B : CNF) : forall A : CNF, memC C (A ++ B) = true -> memC C A = true \/ memC C B = true := by
intro A
induction A with
| nil => intro h; exact Or.inr h
| cons D A ih =>
intro h
cases memC_cons C D (A ++ B) h with
| inl e => exact Or.inl (by rw [e]; exact or_l (beqCl_refl D))
| inr h2 =>
cases ih h2 with
| inl h3 => exact Or.inl (or_r h3)
| inr h3 => exact Or.inr h3
theorem pigeons_mem (n : Nat) (C : Clause) : forall k, memC C (pigeons n k) = true ->
Exists fun i => i < k /\ C = pcl n i n := by
intro k
induction k with
| zero => intro h; exact Bool.noConfusion h
| succ k ih =>
intro h
cases memC_cons C _ _ h with
| inl e => exact Exists.intro k (And.intro (Nat.le_refl _) e)
| inr h2 =>
cases ih h2 with
| intro i hi => exact Exists.intro i (And.intro (Nat.le_trans hi.1 (Nat.le_succ k)) hi.2)
theorem hcl_mem (n j i : Nat) (C : Clause) : forall k, memC C (hcl n j i k) = true ->
Exists fun i' => i' < k /\ C = [(false, var n i' j), (false, var n i j)] := by
intro k
induction k with
| zero => intro h; exact Bool.noConfusion h
| succ k ih =>
intro h
cases memC_cons C _ _ h with
| inl e => exact Exists.intro k (And.intro (Nat.le_refl _) e)
| inr h2 =>
cases ih h2 with
| intro i' hi => exact Exists.intro i' (And.intro (Nat.le_trans hi.1 (Nat.le_succ k)) hi.2)
theorem holesJ_mem (n j : Nat) (C : Clause) : forall k, memC C (holesJ n j k) = true ->
Exists fun i => Exists fun i' => i < k /\ i' < i /\ C = [(false, var n i' j), (false, var n i j)] := by
intro k
induction k with
| zero => intro h; exact Bool.noConfusion h
| succ k ih =>
intro h
cases memC_app C _ _ h with
| inl h1 =>
cases hcl_mem n j k C k h1 with
| intro i' hi => exact Exists.intro k (Exists.intro i' (And.intro (Nat.le_refl _) hi))
| inr h2 =>
cases ih h2 with
| intro i hi =>
cases hi with
| intro i' hh => exact Exists.intro i (Exists.intro i' (And.intro (Nat.le_trans hh.1 (Nat.le_succ k)) hh.2))
theorem holes_mem (n : Nat) (C : Clause) : forall k, memC C (holes n k) = true ->
Exists fun j => Exists fun i => Exists fun i' => j < k /\ i < n + 1 /\ i' < i /\
C = [(false, var n i' j), (false, var n i j)] := by
intro k
induction k with
| zero => intro h; exact Bool.noConfusion h
| succ k ih =>
intro h
cases memC_app C _ _ h with
| inl h1 =>
cases holesJ_mem n k C (n + 1) h1 with
| intro i hi =>
cases hi with
| intro i' hh => exact Exists.intro k (Exists.intro i (Exists.intro i' (And.intro (Nat.le_refl _) hh)))
| inr h2 =>
cases ih h2 with
| intro j hj =>
cases hj with
| intro i hi =>
cases hi with
| intro i' hh =>
exact Exists.intro j (Exists.intro i (Exists.intro i'
(And.intro (Nat.le_trans hh.1 (Nat.le_succ k)) hh.2)))
theorem mem_pcl (n i : Nat) : forall k j, j < k -> memL (true, var n i j) (pcl n i k) = true := by
intro k
induction k with
| zero => intro j h; exact absurd h (Nat.not_succ_le_zero j)
| succ k ih =>
intro j h
show (beqLit (true, var n i k) (true, var n i j) || memL (true, var n i j) (pcl n i k)) = true
have hle : j <= k := Nat.le_of_lt_succ h
cases hle with
| refl => exact or_l (beqLit_refl _)
| step hm => exact or_r (ih j (Nat.succ_le_succ hm))
/-! ## The Delayer -/
def isT : Option Bool -> Bool
| none => false
| some false => false
| some true => true
def isN : Option Bool -> Bool
| none => true
| some _ => false
def taken (n : Nat) (rho : PA) (j : Nat) : Bool := anyI (fun i => isT (rho (var n i j))) (n + 1)
def mtd (n : Nat) (rho : PA) (i : Nat) : Bool := anyI (fun j => isT (rho (var n i j))) n
def avg (n : Nat) (rho : PA) (i : Nat) (j : Nat) : Bool := isN (rho (var n i j)) && !(taken n rho j)
def av (n : Nat) (rho : PA) (i : Nat) : Nat := cntI (avg n rho i) n
def phi (n : Nat) (rho : PA) : Nat := minU n (mtd n rho) (av n rho) (n + 1)
def S (n : Nat) (rho : PA) : Prop :=
forall i i' j, i < n + 1 -> i' < n + 1 -> j < n ->
rho (var n i j) = some true -> rho (var n i' j) = some true -> i = i'
theorem le_zero_eq {a : Nat} (h : a <= 0) : a = 0 := by
cases h with
| refl => rfl
theorem anyI_none (g : Nat -> Bool) : forall k, (forall j, j < k -> g j = false) -> anyI g k = false := by
intro k
induction k with
| zero => intro _; rfl
| succ k ih =>
intro h
show (anyI g k || g k) = false
rw [ih (fun j hj => h j (Nat.le_trans hj (Nat.le_succ k))), h k (Nat.le_refl _)]
rfl
theorem lose (n : Nat) (rho : PA) (hs : S n rho) (C : Clause) (hm : memC C (PHP n) = true)
(hf : Fals rho C) : phi n rho = 0 := by
cases memC_app C _ _ hm with
| inl h1 =>
cases pigeons_mem n C (n + 1) h1 with
| intro i hi =>
have hz : forall j, j < n -> rho (var n i j) = some false := by
intro j hj
have t := hf (true, var n i j) (by rw [hi.2]; exact mem_pcl n i n j hj)
exact t
have hmt : mtd n rho i = false :=
anyI_none _ n (fun j hj => by show isT (rho (var n i j)) = false; rw [hz j hj]; rfl)
have hav : av n rho i = 0 :=
cntI_zero _ n (fun j hj => by show (isN (rho (var n i j)) && _) = false; rw [hz j hj]; rfl)
have t := minU_le n (mtd n rho) (av n rho) i hmt (n + 1) hi.1
rw [hav] at t
exact le_zero_eq t
| inr h2 =>
cases holes_mem n C n h2 with
| intro j hj =>
cases hj with
| intro i hi =>
cases hi with
| intro i' hh =>
have a1 : rho (var n i' j) = some true :=
hf (false, var n i' j) (by rw [hh.2.2.2]; exact or_l (beqLit_refl _))
have a2 : rho (var n i j) = some true :=
hf (false, var n i j) (by rw [hh.2.2.2]; exact or_r (or_l (beqLit_refl _)))
have e := hs i' i j (Nat.le_trans hh.2.2.1 (Nat.le_of_lt hh.2.1))
hh.2.1 hh.1 a1 a2
exact absurd hh.2.2.1 (fun hl => Nat.not_succ_le_self i (by rw [e] at hl; exact hl))
/-! ## Extensions -/
theorem ext_other (rho : PA) (v : Nat) (b : Bool) (w : Nat) (h : Nat.beq w v = false) : ext rho v b w = rho w := by
show selO (Nat.beq w v) (some b) (rho w) = rho w
rw [h]
rfl
theorem ext_eq (rho : PA) (v : Nat) (b : Bool) (w : Nat) (h : Nat.beq w v = true) : ext rho v b w = some b := by
show selO (Nat.beq w v) (some b) (rho w) = some b
rw [h]
rfl
theorem isT_zero (rho : PA) (v : Nat) (hv : rho v = none) (w : Nat) : isT (ext rho v false w) = isT (rho w) := by
cases e : Nat.beq w v with
| false => rw [ext_other rho v false w e]
| true =>
rw [ext_eq rho v false w e, Found.Pnp2.beq_eq w v e, hv]
rfl
theorem true_zero (rho : PA) (v : Nat) (w : Nat) (h : ext rho v false w = some true) : rho w = some true := by
cases e : Nat.beq w v with
| false => rw [ext_other rho v false w e] at h; exact h
| true => rw [ext_eq rho v false w e] at h; cases h
theorem mtd_zero (n : Nat) (rho : PA) (v : Nat) (hv : rho v = none) (i : Nat) :
mtd n (ext rho v false) i = mtd n rho i :=
anyI_congr _ _ n (fun _ _ => isT_zero rho v hv _)
theorem taken_zero (n : Nat) (rho : PA) (v : Nat) (hv : rho v = none) (j : Nat) :
taken n (ext rho v false) j = taken n rho j :=
anyI_congr _ _ (n + 1) (fun _ _ => isT_zero rho v hv _)
theorem avg_zero (n : Nat) (rho : PA) (v : Nat) (hv : rho v = none) (i j : Nat)
(hg : avg n rho i j = true) (hb : Nat.beq (var n i j) v = false) : avg n (ext rho v false) i j = true := by
show (isN (ext rho v false (var n i j)) && !(taken n (ext rho v false) j)) = true
rw [ext_other rho v false _ hb, taken_zero n rho v hv j]
exact hg
theorem S_zero (n : Nat) (rho : PA) (v : Nat) (hs : S n rho) : S n (ext rho v false) :=
fun i i' j hi hi' hj a1 a2 => hs i i' j hi hi' hj (true_zero rho v _ a1) (true_zero rho v _ a2)
/-- A forced false answer: phi drops by at most d when every unmatched pigeon loses at most d. -/
theorem zero_ok (n d : Nat) (rho : PA) (v : Nat) (hv : rho v = none)
(hav : forall i, i < n + 1 -> mtd n rho i = false -> av n rho i <= av n (ext rho v false) i + d) :
phi n rho <= phi n (ext rho v false) + d :=
minU_mono n d (mtd n rho) (mtd n (ext rho v false)) (av n rho) (av n (ext rho v false)) (n + 1)
(fun i hi hm => by
rw [mtd_zero n rho v hv i] at hm
exact And.intro hm (hav i hi hm))
/-! ## Finding a pigeonhole variable -/
def optOr {a : Type} : Option a -> Option a -> Option a
| some r, _ => some r
| none, o => o
def selOn (b : Bool) (j : Nat) : Option Nat := match b with | true => some j | false => none
def findJ (n v i : Nat) : Nat -> Option Nat
| 0 => none
| j + 1 => optOr (findJ n v i j) (selOn (Nat.beq (var n i j) v) j)
def mapJ (i : Nat) : Option Nat -> Option (Nat × Nat)
| none => none
| some j => some (i, j)
def findIJ (n v : Nat) : Nat -> Option (Nat × Nat)
| 0 => none
| i + 1 => optOr (findIJ n v i) (mapJ i (findJ n v i n))
theorem findJ_some (n v i : Nat) : forall k j, findJ n v i k = some j -> j < k /\ var n i j = v := by
intro k
induction k with
| zero => intro j h; cases h
| succ k ih =>
intro j h
have h' : optOr (findJ n v i k) (selOn (Nat.beq (var n i k) v) k) = some j := h
cases e : findJ n v i k with
| some r =>
rw [e] at h'
have er : r = j := Option.some.inj h'
rw [er] at e
have t := ih j e
exact And.intro (Nat.le_trans t.1 (Nat.le_succ k)) t.2
| none =>
rw [e] at h'
cases eb : Nat.beq (var n i k) v with
| false => rw [eb] at h'; cases h'
| true =>
rw [eb] at h'
have ek : k = j := Option.some.inj h'
rw [ek.symm]
exact And.intro (Nat.le_refl _) (Found.Pnp2.beq_eq _ _ eb)
theorem findJ_none (n v i : Nat) : forall k, findJ n v i k = none -> forall j, j < k -> Nat.beq (var n i j) v = false := by
intro k
induction k with
| zero => intro _ j h; exact absurd h (Nat.not_succ_le_zero j)
| succ k ih =>
intro h j hj
have h' : optOr (findJ n v i k) (selOn (Nat.beq (var n i k) v) k) = none := h
cases e : findJ n v i k with
| some r => rw [e] at h'; cases h'
| none =>
rw [e] at h'
have hle : j <= k := Nat.le_of_lt_succ hj
cases hle with
| refl =>
cases eb : Nat.beq (var n i k) v with
| false => rfl
| true => rw [eb] at h'; cases h'
| step hm => exact ih e j (Nat.succ_le_succ hm)
theorem findIJ_some (n v : Nat) : forall k i j, findIJ n v k = some (i, j) -> i < k /\ j < n /\ var n i j = v := by
intro k
induction k with
| zero => intro i j h; cases h
| succ k ih =>
intro i j h
have h' : optOr (findIJ n v k) (mapJ k (findJ n v k n)) = some (i, j) := h
cases e : findIJ n v k with
| some r =>
rw [e] at h'
have er : r = (i, j) := Option.some.inj h'
rw [er] at e
have t := ih i j e
exact And.intro (Nat.le_trans t.1 (Nat.le_succ k)) t.2
| none =>
rw [e] at h'
cases ej : findJ n v k n with
| none => rw [ej] at h'; cases h'
| some j' =>
rw [ej] at h'
have ep : (k, j') = (i, j) := Option.some.inj h'
cases ep
have t := findJ_some n v k n _ ej
exact And.intro (Nat.le_refl _) t
theorem findIJ_none (n v : Nat) : forall k, findIJ n v k = none -> forall i j, i < k -> j < n ->
Nat.beq (var n i j) v = false := by
intro k
induction k with
| zero => intro _ i j h; exact absurd h (Nat.not_succ_le_zero i)
| succ k ih =>
intro h i j hi hj
have h' : optOr (findIJ n v k) (mapJ k (findJ n v k n)) = none := h
cases e : findIJ n v k with
| some r => rw [e] at h'; cases h'
| none =>
rw [e] at h'
have hle : i <= k := Nat.le_of_lt_succ hi
cases hle with
| refl =>
cases ej : findJ n v k n with
| some j' => rw [ej] at h'; cases h'
| none => exact findJ_none n v k n ej j hj
| step hm => exact ih e i j (Nat.succ_le_succ hm) hj
/-! ## Offering true -/
theorem true_split (rho : PA) (v w : Nat) (h : ext rho v true w = some true) :
Nat.beq w v = true \/ rho w = some true := by
cases e : Nat.beq w v with
| true => exact Or.inl rfl
| false => rw [ext_other rho v true w e] at h; exact Or.inr h
theorem one_ok (n : Nat) (rho : PA) (i0 j0 : Nat) (_hi0 : i0 < n + 1) (hj0 : j0 < n) (hs : S n rho)
(_hm : mtd n rho i0 = false) (ht : taken n rho j0 = false) :
S n (ext rho (var n i0 j0) true) /\ phi n rho <= phi n (ext rho (var n i0 j0) true) + 1 := by
have notT : forall i, i < n + 1 -> rho (var n i j0) = some true -> False := by
intro i hi h
have t := anyI_false _ (n + 1) ht i hi
have t' : isT (rho (var n i j0)) = false := t
rw [h] at t'
exact Bool.noConfusion t'
have newv : forall i j, j < n -> Nat.beq (var n i j) (var n i0 j0) = true -> i = i0 /\ j = j0 :=
fun i j hj e => var_inj n i i0 j j0 hj hj0 (Found.Pnp2.beq_eq _ _ e)
refine And.intro ?_ ?_
· intro i i' j hi hi' hj a1 a2
cases true_split rho _ _ a1 with
| inl e1 =>
have p1 := newv i j hj e1
cases true_split rho _ _ a2 with
| inl e2 => exact (p1.1.trans (newv i' j hj e2).1.symm)
| inr o2 => rw [p1.2] at o2; exact absurd o2 (notT i' hi')
| inr o1 =>
cases true_split rho _ _ a2 with
| inl e2 =>
have p2 := newv i' j hj e2
rw [p2.2] at o1
exact absurd o1 (notT i hi)
| inr o2 => exact hs i i' j hi hi' hj o1 o2
· refine minU_mono n 1 (mtd n rho) (mtd n (ext rho (var n i0 j0) true)) (av n rho)
(av n (ext rho (var n i0 j0) true)) (n + 1) ?_
have m0 : mtd n (ext rho (var n i0 j0) true) i0 = true :=
anyI_true _ j0 (by show isT (ext rho (var n i0 j0) true (var n i0 j0)) = true; rw [ext_same]; rfl) n hj0
intro i _ hmi
have hne : forall j, j < n -> Nat.beq (var n i j) (var n i0 j0) = false := by
intro j hj
cases e : Nat.beq (var n i j) (var n i0 j0) with
| false => rfl
| true =>
have ei : i = i0 := (newv i j hj e).1
rw [ei, m0] at hmi
exact Bool.noConfusion hmi
have mt_eq : mtd n (ext rho (var n i0 j0) true) i = mtd n rho i :=
anyI_congr _ _ n (fun j hj => by
show isT (ext rho (var n i0 j0) true (var n i j)) = isT (rho (var n i j))
rw [ext_other _ _ _ _ (hne j hj)])
refine And.intro (by rw [mt_eq] at hmi; exact hmi) ?_
refine cntI_le_one (avg n rho i) (avg n (ext rho (var n i0 j0) true) i) j0 n (fun j hj hg hjj => ?_)
have tk : taken n (ext rho (var n i0 j0) true) j = taken n rho j :=
anyI_congr _ _ (n + 1) (fun i2 _ => by
cases e : Nat.beq (var n i2 j) (var n i0 j0) with
| false =>
show isT (ext rho (var n i0 j0) true (var n i2 j)) = isT (rho (var n i2 j))
rw [ext_other _ _ _ _ e]
| true =>
have ej : j = j0 := (newv i2 j hj e).2
rw [ej, Found.Pnp62.beq_refl] at hjj
exact Bool.noConfusion hjj)
show (isN (ext rho (var n i0 j0) true (var n i j)) && !(taken n (ext rho (var n i0 j0) true) j)) = true
rw [ext_other _ _ _ _ (hne j hj), tk]
exact hg
/-! ## The Delayer's rules and the bound -/
theorem andnot_true (a : Bool) : (a && !true) = false := by
cases a with
| false => rfl
| true => rfl
theorem move (n : Nat) (rho : PA) (v : Nat) (hs : S n rho) (hv : rho v = none) :
Or (Exists fun b => S n (ext rho v b) /\ phi n rho <= phi n (ext rho v b))
(S n (ext rho v false) /\ S n (ext rho v true) /\
phi n rho <= phi n (ext rho v false) + 1 /\ phi n rho <= phi n (ext rho v true) + 1) := by
have forced : (forall i, i < n + 1 -> mtd n rho i = false -> av n rho i <= av n (ext rho v false) i) ->
Or (Exists fun b => S n (ext rho v b) /\ phi n rho <= phi n (ext rho v b))
(S n (ext rho v false) /\ S n (ext rho v true) /\
phi n rho <= phi n (ext rho v false) + 1 /\ phi n rho <= phi n (ext rho v true) + 1) :=
fun h => Or.inl (Exists.intro false (And.intro (S_zero n rho v hs)
(zero_ok n 0 rho v hv (fun i hi hm => h i hi hm))))
have avle : forall i, (forall j, j < n -> avg n rho i j = true -> Nat.beq (var n i j) v = false) ->
av n rho i <= av n (ext rho v false) i :=
fun i h => cntI_le _ _ n (fun j hj hg => avg_zero n rho v hv i j hg (h j hj hg))
cases ef : findIJ n v (n + 1) with
| none => exact forced (fun i hi _ => avle i (fun j hj _ => findIJ_none n v (n + 1) ef i j hi hj))
| some pr =>
cases pr with
| mk i0 j0 =>
have hf := findIJ_some n v (n + 1) i0 j0 ef
have samej : forall i j, j < n -> Nat.beq (var n i j) v = true -> i = i0 /\ j = j0 := by
intro i j hj e
exact var_inj n i i0 j j0 hj hf.2.1 ((Found.Pnp2.beq_eq _ _ e).trans hf.2.2.symm)
cases em : mtd n rho i0 with
| true =>
exact forced (fun i _ hm => avle i (fun j hj _ => by
cases e : Nat.beq (var n i j) v with
| false => rfl
| true =>
rw [(samej i j hj e).1, em] at hm
exact Bool.noConfusion hm))
| false =>
cases et : taken n rho j0 with
| true =>
exact forced (fun i _ _ => avle i (fun j hj hg => by
cases e : Nat.beq (var n i j) v with
| false => rfl
| true =>
have hg' : (isN (rho (var n i j)) && !(taken n rho j)) = true := hg
rw [(samej i j hj e).2, et, andnot_true] at hg'
exact Bool.noConfusion hg'))
| false =>
have ev : v = var n i0 j0 := hf.2.2.symm
have one := one_ok n rho i0 j0 hf.1 hf.2.1 hs em et
rw [ev.symm] at one
refine Or.inr (And.intro (S_zero n rho v hs) (And.intro one.1 (And.intro ?_ one.2)))
refine zero_ok n 1 rho v hv (fun i _ _ => ?_)
refine cntI_le_one (avg n rho i) (avg n (ext rho v false) i) j0 n (fun j hj hg hjj => ?_)
refine avg_zero n rho v hv i j hg ?_
cases e : Nat.beq (var n i j) v with
| false => rfl
| true =>
rw [(samej i j hj e).2, Found.Pnp62.beq_refl] at hjj
exact Bool.noConfusion hjj
theorem delayer (n : Nat) : Delayer (PHP n) (S n) (phi n) :=
Delayer.mk (fun rho hs C hm hf => lose n rho hs C hm hf) (fun rho v hs hv => move n rho v hs hv)
theorem cntI_all (g : Nat -> Bool) : forall k, (forall j, j < k -> g j = true) -> cntI g k = k := by
intro k
induction k with
| zero => intro _; rfl
| succ k ih =>
intro h
show cntI g k + bv (g k) = k + 1
rw [ih (fun j hj => h j (Nat.le_trans hj (Nat.le_succ k))), h k (Nat.le_refl _)]
rfl
theorem minU_ge (n : Nat) (mt : Nat -> Bool) (av' : Nat -> Nat) : forall k,
(forall i, i < k -> mt i = false -> n <= av' i) -> n <= minU n mt av' k := by
intro k
induction k with
| zero => intro _; exact Nat.le_refl n
| succ k ih =>
intro h
have ih' := ih (fun i hi => h i (Nat.le_trans hi (Nat.le_succ k)))
show n <= selN (mt k) (minU n mt av' k) (mn (minU n mt av' k) (av' k))
cases e : mt k with
| true => exact ih'
| false =>
show n <= selN (Nat.ble (minU n mt av' k) (av' k)) (minU n mt av' k) (av' k)
cases Nat.ble (minU n mt av' k) (av' k) with
| true => exact ih'
| false => exact h k (Nat.le_refl _) e
def rho0 : PA := fun _ => none
theorem S0 (n : Nat) : S n rho0 := fun _ _ _ _ _ _ a _ => by cases a
theorem phi0 (n : Nat) : n <= phi n rho0 :=
minU_ge n _ _ (n + 1) (fun i _ _ => Nat.le_of_eq (cntI_all _ n (fun j _ => by
show (isN (rho0 (var n i j)) && !(anyI (fun i2 => isT (rho0 (var n i2 j))) (n + 1))) = true
rw [anyI_none (fun i2 => isT (rho0 (var n i2 j))) (n + 1) (fun _ _ => rfl)]
rfl)).symm)
/-- Tree resolution refutations of PHP(n+1, n) have at least 2^n nodes. -/
theorem php_tree_lb (n : Nat) (p : RProof (PHP n) []) : pow2 n <= p.size :=
Nat.le_trans (pow2_mono _ _ (phi0 n)) (pd_refute (PHP n) (S n) (phi n) (delayer n) p rho0 (S0 n))
end Found.Pnp66
#print axioms Found.Pnp66.beqCl_refl
#print axioms Found.Pnp66.memC_app
#print axioms Found.Pnp66.pigeons_mem
#print axioms Found.Pnp66.holes_mem
#print axioms Found.Pnp66.mem_pcl
#print axioms Found.Pnp66.lose
#print axioms Found.Pnp66.zero_ok
#print axioms Found.Pnp66.findIJ_some
#print axioms Found.Pnp66.findIJ_none
#print axioms Found.Pnp66.one_ok
#print axioms Found.Pnp66.move
#print axioms Found.Pnp66.delayer
#print axioms Found.Pnp66.phi0
#print axioms Found.Pnp66.php_tree_lb
#print axioms: 14 theorems, no axioms.
Adding \(k\) to every register of a program keeps its run: the relocated program on registers shifted by \(k\) follows the original and leaves the registers below \(k\) unchanged.
found/Pnp67.lean
/-
T330 piece 67. found/Pnp67 -- relocating the registers of a register program (a tool for
calling one program inside another; toward search-to-decision, open lemma O5).
relocP k p adds k to every register a program names. Proved, for every program p:
* nthI_reloc: the instruction at pc of relocP k p is the relocated instruction of p;
* step_reloc, run_reloc: if registers i + k of y hold the registers i of x, then the
relocated program on y keeps the same counter as p on x, keeps this correspondence,
and leaves the registers below k unchanged, for any number of steps.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp20
import found.Pnp56
namespace Found.Pnp67
open Found.Pnp20 (Ins Cfg selR upd nthI step exec popTo run)
def relocI (k : Nat) : Ins -> Ins
| .push r b => .push (r + k) b
| .pop r e z o => .pop (r + k) e z o
| .jmp l => .jmp l
def relocP (k : Nat) : List Ins -> List Ins
| [] => []
| i :: p => relocI k i :: relocP k p
def optMap (k : Nat) : Option Ins -> Option Ins
| none => none
| some i => some (relocI k i)
theorem nthI_reloc (k : Nat) : forall (p : List Ins) (n : Nat), nthI (relocP k p) n = optMap k (nthI p n) := by
intro p
induction p with
| nil => intro n; rfl
| cons i p ih =>
intro n
cases n with
| zero => rfl
| succ n => exact ih n
/-- y holds x from register k on, and agrees with y0 below k. -/
def Rel (k : Nat) (x y y0 : Nat -> List Bool) : Prop :=
(forall i, y (i + k) = x i) /\ (forall i, i < k -> y i = y0 i)
theorem upd_rel (k : Nat) (x y y0 : Nat -> List Bool) (h : Rel k x y y0) (r : Nat) (v : List Bool) :
Rel k (upd x r v) (upd y (r + k) v) y0 := by
refine And.intro (fun i => ?_) (fun i hi => ?_)
· show selR (Nat.beq (i + k) (r + k)) v (y (i + k)) = selR (Nat.beq i r) v (x i)
rw [Found.Pnp56.beq_add_r, h.1 i]
· show selR (Nat.beq i (r + k)) v (y i) = y0 i
rw [Found.Pnp56.beq_lt i (r + k) (Nat.lt_of_lt_of_le hi (Nat.le_add_left k r))]
exact h.2 i hi
theorem step_reloc (k : Nat) (p : List Ins) (pc : Nat) (x y y0 : Nat -> List Bool) (h : Rel k x y y0) :
(step (relocP k p) (Cfg.mk pc y)).pc = (step p (Cfg.mk pc x)).pc /\
Rel k (step p (Cfg.mk pc x)).regs (step (relocP k p) (Cfg.mk pc y)).regs y0 := by
show (exec (Cfg.mk pc y) (nthI (relocP k p) pc)).pc = (exec (Cfg.mk pc x) (nthI p pc)).pc /\
Rel k (exec (Cfg.mk pc x) (nthI p pc)).regs (exec (Cfg.mk pc y) (nthI (relocP k p) pc)).regs y0
rw [nthI_reloc]
cases nthI p pc with
| none => exact And.intro rfl h
| some ins =>
cases ins with
| push r b =>
refine And.intro rfl ?_
show Rel k (upd x r (b :: x r)) (upd y (r + k) (b :: y (r + k))) y0
rw [h.1 r]
exact upd_rel k x y y0 h r _
| pop r e z o =>
show (popTo (Cfg.mk pc y) (r + k) e z o (y (r + k))).pc = (popTo (Cfg.mk pc x) r e z o (x r)).pc /\
Rel k (popTo (Cfg.mk pc x) r e z o (x r)).regs (popTo (Cfg.mk pc y) (r + k) e z o (y (r + k))).regs y0
rw [h.1 r]
cases x r with
| nil => exact And.intro rfl h
| cons b t =>
cases b with
| false => exact And.intro rfl (upd_rel k x y y0 h r t)
| true => exact And.intro rfl (upd_rel k x y y0 h r t)
| jmp l => exact And.intro rfl h
theorem run_reloc (k : Nat) (p : List Ins) : forall n (c d : Cfg) (y0 : Nat -> List Bool),
d.pc = c.pc -> Rel k c.regs d.regs y0 ->
(run (relocP k p) n d).pc = (run p n c).pc /\ Rel k (run p n c).regs (run (relocP k p) n d).regs y0 := by
intro n
induction n with
| zero => intro c d y0 hp h; exact And.intro hp h
| succ n ih =>
intro c d y0 hp h
cases c with
| mk pc x =>
cases d with
| mk pd y =>
have e : pd = pc := hp
subst e
have s := step_reloc k p pd x y y0 h
exact ih (step p (Cfg.mk pd x)) (step (relocP k p) (Cfg.mk pd y)) y0 s.1 s.2
end Found.Pnp67
#print axioms Found.Pnp67.nthI_reloc
#print axioms Found.Pnp67.upd_rel
#print axioms Found.Pnp67.step_reloc
#print axioms Found.Pnp67.run_reloc
#print axioms: 4 theorems, no axioms.
A program placed at offset \(o\) inside a larger one runs as it runs alone while it stays on its own instructions.
found/Pnp68.lean
/-
T330 piece 68. found/Pnp68 -- a program placed as a fragment runs as it runs alone
(toward search-to-decision, open lemma O5).
If P sits at offset o in prog (Pnp21 Frag), every step of P from an instruction of P is a
step of prog with the counter shifted by o. Proved:
* frag_step: one step;
* frag_run: n steps, as long as the run of P stays on instructions of P before step n;
* frag_runs: in Runs form: a run of P from 0 to its end (pc = |P|) that stays inside is
a run of prog from o to o + |P| with the same registers.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp21
namespace Found.Pnp68
open Found.Pnp20 (Ins Cfg nthI step exec popTo run)
open Found.Pnp21 (shift Frag Runs)
theorem frag_step (prog : List Ins) (o : Nat) (P : List Ins) (hf : Frag prog o P) (pc : Nat)
(x : Nat -> List Bool) (ins : Ins) (hi : nthI P pc = some ins) :
(step prog (Cfg.mk (o + pc) x)).pc = o + (step P (Cfg.mk pc x)).pc /\
(step prog (Cfg.mk (o + pc) x)).regs = (step P (Cfg.mk pc x)).regs := by
have hp : nthI prog (o + pc) = some (shift o ins) := hf pc ins hi
show (exec (Cfg.mk (o + pc) x) (nthI prog (o + pc))).pc = o + (exec (Cfg.mk pc x) (nthI P pc)).pc /\
(exec (Cfg.mk (o + pc) x) (nthI prog (o + pc))).regs = (exec (Cfg.mk pc x) (nthI P pc)).regs
rw [hp, hi]
cases ins with
| push r b => exact And.intro (Nat.add_assoc o pc 1) rfl
| pop r e z w =>
show (popTo (Cfg.mk (o + pc) x) r (o + e) (o + z) (o + w) (x r)).pc = o + (popTo (Cfg.mk pc x) r e z w (x r)).pc /\
(popTo (Cfg.mk (o + pc) x) r (o + e) (o + z) (o + w) (x r)).regs = (popTo (Cfg.mk pc x) r e z w (x r)).regs
cases x r with
| nil => exact And.intro rfl rfl
| cons b t =>
cases b with
| false => exact And.intro rfl rfl
| true => exact And.intro rfl rfl
| jmp l => exact And.intro rfl rfl
/-- The run of P stays on its instructions before step n. -/
def Inside (P : List Ins) (n : Nat) (c : Cfg) : Prop :=
forall m, m < n -> Exists fun ins => nthI P (run P m c).pc = some ins
theorem frag_run (prog : List Ins) (o : Nat) (P : List Ins) (hf : Frag prog o P) :
forall n pc (x : Nat -> List Bool), Inside P n (Cfg.mk pc x) ->
(run prog n (Cfg.mk (o + pc) x)).pc = o + (run P n (Cfg.mk pc x)).pc /\
(run prog n (Cfg.mk (o + pc) x)).regs = (run P n (Cfg.mk pc x)).regs := by
intro n
induction n with
| zero => intro pc x _; exact And.intro rfl rfl
| succ n ih =>
intro pc x hin
cases hin 0 (Nat.succ_le_succ (Nat.zero_le n)) with
| intro ins hi =>
have s := frag_step prog o P hf pc x ins hi
have e : step prog (Cfg.mk (o + pc) x) =
Cfg.mk (o + (step P (Cfg.mk pc x)).pc) (step P (Cfg.mk pc x)).regs := by
rw [(s.1).symm, (s.2).symm]
have hin' : Inside P n (Cfg.mk (step P (Cfg.mk pc x)).pc (step P (Cfg.mk pc x)).regs) :=
fun m hm => hin (m + 1) (Nat.succ_le_succ hm)
show (run prog n (step prog (Cfg.mk (o + pc) x))).pc = o + (run P n (step P (Cfg.mk pc x))).pc /\
(run prog n (step prog (Cfg.mk (o + pc) x))).regs = (run P n (step P (Cfg.mk pc x))).regs
rw [e]
exact ih _ _ hin'
theorem frag_runs (prog : List Ins) (o : Nat) (P : List Ins) (hf : Frag prog o P) (n : Nat)
(x y : Nat -> List Bool) (hr : Runs P n 0 x P.length y) (hin : Inside P n (Cfg.mk 0 x)) :
Runs prog n o x (o + P.length) y := by
have r := frag_run prog o P hf n 0 x hin
refine And.intro ?_ (fun i => ?_)
· show (run prog n (Cfg.mk (o + 0) x)).pc = o + P.length
rw [r.1, hr.1]
· show (run prog n (Cfg.mk (o + 0) x)).regs i = y i
rw [r.2]
exact hr.2 i
end Found.Pnp68
#print axioms Found.Pnp68.frag_step
#print axioms Found.Pnp68.frag_run
#print axioms Found.Pnp68.frag_runs
#print axioms: 3 theorems, no axioms.
A program relocated by \(k\) registers and placed as a fragment is a subroutine: its run on the registers from \(k\) on is reproduced, and the registers below \(k\) are kept.
found/Pnp69.lean
/-
T330 piece 69. found/Pnp69 -- calling a program inside another (toward search-to-decision,
open lemma O5).
A program D, relocated by k registers (Pnp67) and placed at offset o in prog (Pnp21 Frag),
is a subroutine: if D alone runs from 0 on registers x to its end |D| with registers xf,
staying on its instructions (Pnp68 Inside), then prog runs from o on any registers y that
hold x from register k on, to o + |D|, with registers that hold xf from k on and keep the
registers below k. Proved:
* relocP_len: relocation keeps the length;
* inside_reloc: the relocated run stays inside when the original does;
* call_run: the subroutine call.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp67
import found.Pnp68
namespace Found.Pnp69
open Found.Pnp20 (Ins Cfg nthI run)
open Found.Pnp21 (Frag Runs)
open Found.Pnp67 (relocI relocP optMap nthI_reloc Rel run_reloc)
open Found.Pnp68 (Inside frag_runs)
theorem relocP_len (k : Nat) : forall p : List Ins, (relocP k p).length = p.length := by
intro p
induction p with
| nil => rfl
| cons i p ih => exact congrArg Nat.succ ih
theorem inside_reloc (k : Nat) (D : List Ins) (n : Nat) (x y y0 : Nat -> List Bool) (h : Rel k x y y0)
(hin : Inside D n (Cfg.mk 0 x)) : Inside (relocP k D) n (Cfg.mk 0 y) := by
intro m hm
have r := run_reloc k D m (Cfg.mk 0 x) (Cfg.mk 0 y) y0 rfl h
cases hin m hm with
| intro ins hi =>
refine Exists.intro (relocI k ins) ?_
rw [r.1, nthI_reloc, hi]
rfl
theorem call_run (prog : List Ins) (o k : Nat) (D : List Ins) (hf : Frag prog o (relocP k D)) (n : Nat)
(x xf y y0 : Nat -> List Bool) (h : Rel k x y y0) (hr : Runs D n 0 x D.length xf)
(hin : Inside D n (Cfg.mk 0 x)) :
Exists fun z => Runs prog n o y (o + D.length) z /\ (forall i, z (i + k) = xf i) /\
(forall i, i < k -> z i = y0 i) := by
have r := run_reloc k D n (Cfg.mk 0 x) (Cfg.mk 0 y) y0 rfl h
have hr' : Runs (relocP k D) n 0 y (relocP k D).length (run (relocP k D) n (Cfg.mk 0 y)).regs := by
refine And.intro ?_ (fun _ => rfl)
rw [r.1, hr.1, relocP_len]
have fr := frag_runs prog o (relocP k D) hf n y _ hr' (inside_reloc k D n x y y0 h hin)
rw [relocP_len] at fr
refine Exists.intro _ (And.intro fr (And.intro (fun i => ?_) r.2.2))
rw [r.2.1 i]
exact hr.2 i
end Found.Pnp69
#print axioms Found.Pnp69.relocP_len
#print axioms Found.Pnp69.inside_reloc
#print axioms Found.Pnp69.call_run
#print axioms: 3 theorems, no axioms.
On the register machine as a concrete model (input register 13, output register 16), a one-way function has no polynomial-time inverter; with search-to-decision there it gives \(\mathrm {NP}\not \subseteq \mathrm {P}\).
found/Owf4.lean
/-
T332 piece 4. found/Owf4 -- one-way functions on the register machine (Pnp20) as a
concrete model: P, NP, computing a function, inverting, and the bridge.
Conventions: input in register 13 (Pnp59 inpR), output in register 16; a program ends when
its counter reaches its length, staying on its instructions before (Pnp68 Inside).
* resRM I t z: the output of I on z if it has ended after t steps;
* DecRM, InPrm, InNPrm: deciding (first output bit) within a polynomial, P and NP
(certificates through Pnp4.enc);
* CompRM: computing a function within a polynomial;
* OneWayRM, SearchRM: one-way (success counted over all 2^n inputs, as Owf1) and
polynomial-time inversion;
* owf_not_search_rm: a one-way function has no polynomial-time inverter;
* S2DRM, owf_p_ne_np_rm: with search-to-decision on the register machine (open lemma
O5, now a statement about this concrete model), a one-way function gives NP not
inside P.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Owf1
import found.Pnp69
import found.Pnp59
namespace Found.Owf4
open Found.Pnp3 (PE pe)
open Found.Pnp8 (allBits)
open Found.Pnp9 (pow2)
open Found.Pnp20 (Ins Cfg run)
open Found.Pnp21 (Runs)
open Found.Pnp68 (Inside)
open Found.Pnp59 (inpR)
open Found.Owf1 (beqL count count_all pow2_pos not_double)
def outBit : List Bool -> Bool
| [] => false
| b :: _ => b
def selOL : Bool -> Option (List Bool) -> Option (List Bool) -> Option (List Bool)
| true, a, _ => a
| false, _, b => b
/-- The output of I on z if it has ended after t steps. -/
def resRM (I : List Ins) (t : Nat) (z : List Bool) : Option (List Bool) :=
selOL (Nat.beq (run I t (Cfg.mk 0 (inpR z))).pc I.length) (some ((run I t (Cfg.mk 0 (inpR z))).regs 16)) none
def DecRM (D : List Ins) (e : PE) (L : List Bool -> Bool) : Prop :=
forall x, Exists fun N => Exists fun y => N <= pe e x.length /\ Runs D N 0 (inpR x) D.length y /\
Inside D N (Cfg.mk 0 (inpR x)) /\ outBit (y 16) = L x
def InPrm (L : List Bool -> Bool) : Prop := Exists fun D => Exists fun e => DecRM D e L
def InNPrm (L : List Bool -> Bool) : Prop :=
Exists fun V : List Bool -> Bool => Exists fun b : PE => InPrm V /\
forall x, L x = true <-> Exists fun w : List Bool => w.length <= pe b x.length /\ V (Found.Pnp4.enc x w) = true
def CompRM (F : List Ins) (e : PE) (f : List Bool -> List Bool) : Prop :=
forall x, Exists fun N => Exists fun y => N <= pe e x.length /\ Runs F N 0 (inpR x) F.length y /\
Inside F N (Cfg.mk 0 (inpR x)) /\ y 16 = f x
def invOK (f : List Bool -> List Bool) (x : List Bool) : Option (List Bool) -> Bool
| none => false
| some y => beqL (f y) (f x)
def succRM (f : List Bool -> List Bool) (I : List Ins) (e : PE) (n : Nat) : Nat :=
count (fun x => invOK f x (resRM I (pe e n) (f x))) (allBits n)
structure OneWayRM (f : List Bool -> List Bool) : Prop where
easy : Exists fun F => Exists fun e => CompRM F e f
hard : forall (I : List Ins) (e : PE), Exists fun n0 => forall n, n0 <= n ->
succRM f I e n * (pe e n + 2) <= pow2 n
def SearchRM (f : List Bool -> List Bool) : Prop :=
Exists fun I : List Ins => Exists fun e : PE => forall x, invOK f x (resRM I (pe e x.length) (f x)) = true
theorem owf_not_search_rm (f : List Bool -> List Bool) (ho : OneWayRM f) (hs : SearchRM f) : False :=
match hs with
| Exists.intro I (Exists.intro e he) =>
match ho.hard I e with
| Exists.intro n0 hn =>
have hc : succRM f I e n0 = pow2 n0 :=
count_all n0 (fun x => invOK f x (resRM I (pe e n0) (f x))) (fun x hx => by rw [hx.symm]; exact he x)
have hb := hn n0 (Nat.le_refl n0)
not_double (pow2 n0) (pe e n0) (pow2_pos n0) (by rw [hc] at hb; exact hb)
/-- Search-to-decision on the register machine (open lemma O5 for this model). -/
def S2DRM : Prop :=
(forall L, InNPrm L -> InPrm L) ->
forall f : List Bool -> List Bool, (Exists fun F => Exists fun e => CompRM F e f) -> SearchRM f
theorem owf_p_ne_np_rm (hs : S2DRM) (f : List Bool -> List Bool) (ho : OneWayRM f) :
(forall L, InNPrm L -> InPrm L) -> False :=
fun heq => owf_not_search_rm f ho (hs heq f ho.easy)
end Found.Owf4
#print axioms Found.Owf4.owf_not_search_rm
#print axioms Found.Owf4.owf_p_ne_np_rm
#print axioms: 2 theorems, no axioms.
The EQ fragment pops two registers together and pushes whether they were equal, within \(2|a|+4\) steps, keeping every other register.
found/Pnp70.lean
/-
T330 piece 70. found/Pnp70 -- one-step facts and the EQ fragment: comparing two registers
(toward search-to-decision on the register machine, open lemma O5rm).
EQ A B O pops A and B together; it pushes true on O if they were equal, false otherwise.
The layout (offsets from o):
0: pop A (empty -> 3, 0 -> 1, 1 -> 2) 1: pop B (-> 6, 0 -> 0, 1 -> 6)
2: pop B (-> 6, 0 -> 6, 1 -> 0) 3: pop B (empty -> 4, else -> 6)
4: push O true 5: jmp 7 6: push O false (end at 7).
Proved:
* run1_push, run1_jmp, run1_pop_nil, run1_pop_cons: one step of each instruction;
* eq_run: from o with A = a and B = b, EQ ends at o + 7 within 2 |a| + 4 steps, O holds
(beqL a b) :: O, and every register other than A, B, O is unchanged.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Owf2
import found.Pnp21
namespace Found.Pnp70
open Found.Pnp20 (Ins Cfg upd upd_same upd_other nthI step exec popTo run)
open Found.Pnp21 (shift Frag Runs runs_seq)
open Found.Owf1 (beqL)
/-! ## One step -/
theorem run1_push (prog : List Ins) (o r : Nat) (b : Bool) (h : nthI prog o = some (.push r b))
(x : Nat -> List Bool) : Runs prog 1 o x (o + 1) (upd x r (b :: x r)) := by
show (exec (Cfg.mk o x) (nthI prog o)).pc = o + 1 /\ forall i, (exec (Cfg.mk o x) (nthI prog o)).regs i = _
rw [h]
exact And.intro rfl (fun _ => rfl)
theorem run1_jmp (prog : List Ins) (o l : Nat) (h : nthI prog o = some (.jmp l)) (x : Nat -> List Bool) :
Runs prog 1 o x l x := by
show (exec (Cfg.mk o x) (nthI prog o)).pc = l /\ forall i, (exec (Cfg.mk o x) (nthI prog o)).regs i = x i
rw [h]
exact And.intro rfl (fun _ => rfl)
theorem run1_pop_nil (prog : List Ins) (o r e z w : Nat) (h : nthI prog o = some (.pop r e z w))
(x : Nat -> List Bool) (hx : x r = []) : Runs prog 1 o x e x := by
show (exec (Cfg.mk o x) (nthI prog o)).pc = e /\ forall i, (exec (Cfg.mk o x) (nthI prog o)).regs i = x i
rw [h]
show (popTo (Cfg.mk o x) r e z w (x r)).pc = e /\ forall i, (popTo (Cfg.mk o x) r e z w (x r)).regs i = x i
rw [hx]
exact And.intro rfl (fun _ => rfl)
def bsel (b : Bool) (w z : Nat) : Nat := match b with | true => w | false => z
theorem run1_pop_cons (prog : List Ins) (o r e z w : Nat) (h : nthI prog o = some (.pop r e z w))
(x : Nat -> List Bool) (b : Bool) (t : List Bool) (hx : x r = b :: t) :
Runs prog 1 o x (bsel b w z) (upd x r t) := by
show (exec (Cfg.mk o x) (nthI prog o)).pc = _ /\ forall i, (exec (Cfg.mk o x) (nthI prog o)).regs i = _
rw [h]
show (popTo (Cfg.mk o x) r e z w (x r)).pc = _ /\ forall i, (popTo (Cfg.mk o x) r e z w (x r)).regs i = _
rw [hx]
cases b with
| false => exact And.intro rfl (fun _ => rfl)
| true => exact And.intro rfl (fun _ => rfl)
theorem nsym (a b : Nat) (h : Nat.beq a b = false) : Nat.beq b a = false := by
cases e : Nat.beq b a with
| false => rfl
| true =>
rw [Found.Pnp2.beq_eq b a e, Found.Pnp20.beq_refl'] at h
exact Bool.noConfusion h
theorem twoS (a : Nat) : (a + 1) + (a + 1) = (a + a) + 2 := by
rw [Nat.succ_add]
rfl
/-! ## EQ -/
def eqP (A B O : Nat) : List Ins :=
[.pop A 3 1 2, .pop B 6 0 6, .pop B 6 6 0, .pop B 4 6 6, .push O true, .jmp 7, .push O false]
/-- The instructions of EQ at offset o. -/
structure EqAt (prog : List Ins) (o A B O : Nat) : Prop where
i0 : nthI prog o = some (.pop A (o + 3) (o + 1) (o + 2))
i1 : nthI prog (o + 1) = some (.pop B (o + 6) o (o + 6))
i2 : nthI prog (o + 2) = some (.pop B (o + 6) (o + 6) o)
i3 : nthI prog (o + 3) = some (.pop B (o + 4) (o + 6) (o + 6))
i4 : nthI prog (o + 4) = some (.push O true)
i5 : nthI prog (o + 5) = some (.jmp (o + 7))
i6 : nthI prog (o + 6) = some (.push O false)
theorem eqAt_of_frag (prog : List Ins) (o A B O : Nat) (hf : Frag prog o (eqP A B O)) : EqAt prog o A B O :=
EqAt.mk (hf 0 _ rfl) (hf 1 _ rfl) (hf 2 _ rfl) (hf 3 _ rfl) (hf 4 _ rfl) (hf 5 _ rfl) (hf 6 _ rfl)
/-- The result of EQ: O gets the bit; A, B, O aside, registers are kept. -/
def EqOut (A B O : Nat) (x y : Nat -> List Bool) (bit : Bool) : Prop :=
y O = bit :: x O /\ forall i, Nat.beq i A = false -> Nat.beq i B = false -> Nat.beq i O = false -> y i = x i
theorem beqB_self (a : Bool) : (a == a) = true := by
cases a with
| false => rfl
| true => rfl
theorem beqB_ne (a b : Bool) (h : (a == b) = false) : (a == b) = false := h
theorem eq_out_push (A B O : Nat) (hAO : Nat.beq A O = false) (hBO : Nat.beq B O = false)
(x x1 : Nat -> List Bool) (bit : Bool)
(h1 : forall i, Nat.beq i A = false -> Nat.beq i B = false -> x1 i = x i) :
EqOut A B O x (upd x1 O (bit :: x1 O)) bit := by
have hO : x1 O = x O := h1 O (nsym A O hAO) (nsym B O hBO)
refine And.intro ?_ (fun i ha hb ho => ?_)
· rw [upd_same, hO]
· rw [upd_other x1 O i _ ho]
exact h1 i ha hb
theorem eq_run (prog : List Ins) (o A B O : Nat) (he : EqAt prog o A B O)
(hAB : Nat.beq A B = false) (hAO : Nat.beq A O = false) (hBO : Nat.beq B O = false) :
forall (a b : List Bool) (x : Nat -> List Bool), x A = a -> x B = b ->
Exists fun N => Exists fun y => N <= a.length + a.length + 4 /\ Runs prog N o x (o + 7) y /\
EqOut A B O x y (beqL a b) := by
intro a
induction a with
| nil =>
intro b x ha hb
have s0 := run1_pop_nil prog o A (o + 3) (o + 1) (o + 2) he.i0 x ha
cases b with
| nil =>
have s1 := run1_pop_nil prog (o + 3) B (o + 4) (o + 6) (o + 6) he.i3 x hb
have s2 := run1_push prog (o + 4) O true he.i4 x
have s3 := run1_jmp prog (o + 5) (o + 7) he.i5 (upd x O (true :: x O))
refine Exists.intro 4 (Exists.intro _ (And.intro (Nat.le_refl 4) (And.intro
(runs_seq prog 3 1 o (o + 5) (o + 7) x _ _
(runs_seq prog 2 1 o (o + 4) (o + 5) x x _ (runs_seq prog 1 1 o (o + 3) (o + 4) x x x s0 s1) s2) s3)
(eq_out_push A B O hAO hBO x x true (fun _ _ _ => rfl)))))
| cons c t =>
have s1 := run1_pop_cons prog (o + 3) B (o + 4) (o + 6) (o + 6) he.i3 x c t hb
have e6 : bsel c (o + 6) (o + 6) = o + 6 := by cases c with | false => rfl | true => rfl
rw [e6] at s1
have s2 := run1_push prog (o + 6) O false he.i6 (upd x B t)
refine Exists.intro 3 (Exists.intro _ (And.intro (Nat.le_succ 3) (And.intro
(runs_seq prog 2 1 o (o + 6) (o + 7) x _ _ (runs_seq prog 1 1 o (o + 3) (o + 6) x x _ s0 s1) s2)
(eq_out_push A B O hAO hBO x (upd x B t) false (fun i _ hb' => upd_other x B i t hb')))))
| cons a0 a ih =>
intro b x ha hb
have s0 := run1_pop_cons prog o A (o + 3) (o + 1) (o + 2) he.i0 x a0 a ha
have hb1 : upd x A a B = b := by rw [upd_other x A B a (nsym A B hAB)]; exact hb
-- the second pop, from o + 1 (a0 = false) or o + 2 (a0 = true)
cases b with
| nil =>
have s1 : Runs prog 1 (bsel a0 (o + 2) (o + 1)) (upd x A a) (o + 6) (upd x A a) := by
cases a0 with
| false => exact run1_pop_nil prog (o + 1) B (o + 6) o (o + 6) he.i1 _ hb1
| true => exact run1_pop_nil prog (o + 2) B (o + 6) (o + 6) o he.i2 _ hb1
have s2 := run1_push prog (o + 6) O false he.i6 (upd x A a)
refine Exists.intro 3 (Exists.intro _ (And.intro ?_ (And.intro
(runs_seq prog 2 1 o (o + 6) (o + 7) x _ _ (runs_seq prog 1 1 o _ (o + 6) x _ _ s0 s1) s2)
?_)))
· exact Nat.le_trans (Nat.le_succ 3) (Nat.le_add_left 4 _)
· have bf : beqL (a0 :: a) [] = false := rfl
rw [bf]
exact eq_out_push A B O hAO hBO x (upd x A a) false (fun i ha' _ => upd_other x A i a ha')
| cons c t =>
cases ec : (a0 == c) with
| false =>
have s1 : Runs prog 1 (bsel a0 (o + 2) (o + 1)) (upd x A a) (o + 6) (upd (upd x A a) B t) := by
cases a0 with
| false =>
cases c with
| false => exact Bool.noConfusion ec
| true => exact run1_pop_cons prog (o + 1) B (o + 6) o (o + 6) he.i1 _ true t hb1
| true =>
cases c with
| false => exact run1_pop_cons prog (o + 2) B (o + 6) (o + 6) o he.i2 _ false t hb1
| true => exact Bool.noConfusion ec
have s2 := run1_push prog (o + 6) O false he.i6 (upd (upd x A a) B t)
refine Exists.intro 3 (Exists.intro _ (And.intro ?_ (And.intro
(runs_seq prog 2 1 o (o + 6) (o + 7) x _ _ (runs_seq prog 1 1 o _ (o + 6) x _ _ s0 s1) s2) ?_)))
· exact Nat.le_trans (Nat.le_succ 3) (Nat.le_add_left 4 _)
· have bf : beqL (a0 :: a) (c :: t) = false := by
show ((a0 == c) && beqL a t) = false
rw [ec]
rfl
rw [bf]
exact eq_out_push A B O hAO hBO x _ false (fun i ha' hb' => by
rw [upd_other _ B i t hb', upd_other x A i a ha'])
| true =>
have s1 : Runs prog 1 (bsel a0 (o + 2) (o + 1)) (upd x A a) o (upd (upd x A a) B t) := by
cases a0 with
| false =>
cases c with
| false => exact run1_pop_cons prog (o + 1) B (o + 6) o (o + 6) he.i1 _ false t hb1
| true => exact Bool.noConfusion ec
| true =>
cases c with
| false => exact Bool.noConfusion ec
| true => exact run1_pop_cons prog (o + 2) B (o + 6) (o + 6) o he.i2 _ true t hb1
have hA2 : upd (upd x A a) B t A = a := by
rw [upd_other _ B A t hAB, upd_same]
have hB2 : upd (upd x A a) B t B = t := upd_same _ B t
cases ih t (upd (upd x A a) B t) hA2 hB2 with
| intro N hN =>
cases hN with
| intro y hy =>
refine Exists.intro (2 + N) (Exists.intro y (And.intro ?_ (And.intro
(runs_seq prog 2 N o o (o + 7) x _ y (runs_seq prog 1 1 o _ o x _ _ s0 s1) hy.2.1) ?_)))
· show 2 + N <= a.length + 1 + (a.length + 1) + 4
rw [twoS, Nat.add_comm 2 N]
refine Nat.le_trans (Nat.add_le_add_right hy.1 2) (Nat.le_of_eq ?_)
rw [Nat.add_right_comm]
· have bt : beqL (a0 :: a) (c :: t) = beqL a t := by
show ((a0 == c) && beqL a t) = beqL a t
rw [ec]
rfl
rw [bt]
refine And.intro ?_ (fun i ha' hb' ho' => ?_)
· rw [hy.2.2.1, upd_other _ B O t (nsym B O hBO),
upd_other x A O a (nsym A O hAO)]
· rw [hy.2.2.2 i ha' hb' ho', upd_other _ B i t hb', upd_other x A i a ha']
end Found.Pnp70
#print axioms Found.Pnp70.run1_push
#print axioms Found.Pnp70.run1_jmp
#print axioms Found.Pnp70.run1_pop_nil
#print axioms Found.Pnp70.run1_pop_cons
#print axioms Found.Pnp70.eqAt_of_frag
#print axioms Found.Pnp70.eq_out_push
#print axioms Found.Pnp70.eq_run
#print axioms: 7 theorems, no axioms.
The UNENC fragment reads \(\mathrm {enc}\,x\,w\) two bits at a time, puts \(x\) (reversed) on another register and leaves \(w\), in \(4|x|+2\) steps.
found/Pnp71.lean
/-
T330 piece 71. found/Pnp71 -- the UNENC fragment: splitting an encoded pair (toward
search-to-decision on the register machine, open lemma O5rm).
Register S holds enc x w = dbl x ++ [true, true] ++ w (Pnp4): every bit b of x as
(false, b), then the separator (true, true). UNENC S X pops S two bits at a time, pushes
the data bits on X and stops after the separator. Layout (offsets from o):
0: pop S (-> 7, 0 -> 1, 1 -> 6) 1: pop S (-> 7, 0 -> 2, 1 -> 4)
2: push X false 3: jmp 0 4: push X true 5: jmp 0 6: pop S (all -> 7) end 7.
Proved:
* unenc_run: from o with S = enc x w, UNENC ends at o + 7 after 4 |x| + 2 steps with
S = w and X = revApp x X (x reversed on top of X); other registers unchanged.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp70
import found.Pnp4
namespace Found.Pnp71
open Found.Pnp20 (Ins Cfg upd upd_same upd_other nthI revApp)
open Found.Pnp21 (Frag Runs runs_seq)
open Found.Pnp70 (run1_push run1_jmp run1_pop_cons nsym)
open Found.Pnp4 (dbl enc)
def unencP (S X : Nat) : List Ins :=
[.pop S 7 1 6, .pop S 7 2 4, .push X false, .jmp 0, .push X true, .jmp 0, .pop S 7 7 7]
structure UnAt (prog : List Ins) (o S X : Nat) : Prop where
i0 : nthI prog o = some (.pop S (o + 7) (o + 1) (o + 6))
i1 : nthI prog (o + 1) = some (.pop S (o + 7) (o + 2) (o + 4))
i2 : nthI prog (o + 2) = some (.push X false)
i3 : nthI prog (o + 3) = some (.jmp o)
i4 : nthI prog (o + 4) = some (.push X true)
i5 : nthI prog (o + 5) = some (.jmp o)
i6 : nthI prog (o + 6) = some (.pop S (o + 7) (o + 7) (o + 7))
theorem unAt_of_frag (prog : List Ins) (o S X : Nat) (hf : Frag prog o (unencP S X)) : UnAt prog o S X :=
UnAt.mk (hf 0 _ rfl) (hf 1 _ rfl) (hf 2 _ rfl) (hf 3 _ rfl) (hf 4 _ rfl) (hf 5 _ rfl) (hf 6 _ rfl)
/-- The final registers. -/
def unOut (S X : Nat) (r : Nat -> List Bool) (x w : List Bool) : Nat -> List Bool :=
upd (upd r S w) X (revApp x (r X))
theorem unenc_run (prog : List Ins) (o S X : Nat) (hu : UnAt prog o S X) (hSX : Nat.beq S X = false)
(w : List Bool) : forall (x : List Bool) (r : Nat -> List Bool), r S = enc x w ->
Runs prog (4 * x.length + 2) o r (o + 7) (unOut S X r x w) := by
have hXS := nsym S X hSX
intro x
induction x with
| nil =>
intro r hr
have s0 := run1_pop_cons prog o S (o + 7) (o + 1) (o + 6) hu.i0 r true (true :: w) hr
have s1 := run1_pop_cons prog (o + 6) S (o + 7) (o + 7) (o + 7) hu.i6 (upd r S (true :: w)) true w
(upd_same r S _)
have s := runs_seq prog 1 1 o (o + 6) (o + 7) r _ _ s0 s1
refine And.intro s.1 (fun i => ?_)
refine (s.2 i).trans ?_
show upd (upd r S (true :: w)) S w i = upd (upd r S w) X (r X) i
cases ex : Nat.beq i X with
| true =>
have e := Found.Pnp2.beq_eq i X ex
rw [e, upd_same, upd_other _ S X w hXS, upd_other r S X _ hXS]
| false =>
rw [upd_other _ X i _ ex]
cases es : Nat.beq i S with
| true => rw [Found.Pnp2.beq_eq i S es, upd_same, upd_same]
| false => rw [upd_other _ S i w es, upd_other _ S i w es, upd_other r S i _ es]
| cons b x ih =>
intro r hr
have hr' : r S = false :: b :: (dbl x ++ (true :: true :: w)) := hr
have s0 := run1_pop_cons prog o S (o + 7) (o + 1) (o + 6) hu.i0 r false _ hr'
have r1 : upd r S (b :: (dbl x ++ (true :: true :: w))) S = b :: (dbl x ++ (true :: true :: w)) :=
upd_same r S _
have s1 := run1_pop_cons prog (o + 1) S (o + 7) (o + 2) (o + 4) hu.i1 _ b _ r1
-- r2: registers after the pair
have hstep : Runs prog 4 o r o (upd (upd r S (dbl x ++ (true :: true :: w))) X (b :: r X)) := by
cases b with
| false =>
have s2 := run1_push prog (o + 2) X false hu.i2 (upd (upd r S (false :: (dbl x ++ (true :: true :: w)))) S
(dbl x ++ (true :: true :: w)))
have s := runs_seq prog 3 1 o (o + 3) o r _ _
(runs_seq prog 2 1 o (o + 2) (o + 3) r _ _ (runs_seq prog 1 1 o (o + 1) (o + 2) r _ _ s0 s1) s2) (run1_jmp prog (o + 3) o hu.i3 _)
refine And.intro s.1 (fun i => ?_)
refine (s.2 i).trans ?_
show upd (upd (upd r S (false :: (dbl x ++ (true :: true :: w)))) S (dbl x ++ (true :: true :: w))) X
(false :: upd (upd r S (false :: (dbl x ++ (true :: true :: w)))) S (dbl x ++ (true :: true :: w)) X) i =
upd (upd r S (dbl x ++ (true :: true :: w))) X (false :: r X) i
rw [upd_other _ S X _ hXS, upd_other r S X _ hXS]
cases ex : Nat.beq i X with
| true => rw [Found.Pnp2.beq_eq i X ex, upd_same, upd_same]
| false =>
rw [upd_other _ X i _ ex, upd_other _ X i _ ex]
cases es : Nat.beq i S with
| true => rw [Found.Pnp2.beq_eq i S es, upd_same, upd_same]
| false => rw [upd_other _ S i _ es, upd_other _ S i _ es, upd_other r S i _ es]
| true =>
have s2 := run1_push prog (o + 4) X true hu.i4 (upd (upd r S (true :: (dbl x ++ (true :: true :: w)))) S
(dbl x ++ (true :: true :: w)))
have s := runs_seq prog 3 1 o (o + 5) o r _ _
(runs_seq prog 2 1 o (o + 4) (o + 5) r _ _ (runs_seq prog 1 1 o (o + 1) (o + 4) r _ _ s0 s1) s2) (run1_jmp prog (o + 5) o hu.i5 _)
refine And.intro s.1 (fun i => ?_)
refine (s.2 i).trans ?_
show upd (upd (upd r S (true :: (dbl x ++ (true :: true :: w)))) S (dbl x ++ (true :: true :: w))) X
(true :: upd (upd r S (true :: (dbl x ++ (true :: true :: w)))) S (dbl x ++ (true :: true :: w)) X) i =
upd (upd r S (dbl x ++ (true :: true :: w))) X (true :: r X) i
rw [upd_other _ S X _ hXS, upd_other r S X _ hXS]
cases ex : Nat.beq i X with
| true => rw [Found.Pnp2.beq_eq i X ex, upd_same, upd_same]
| false =>
rw [upd_other _ X i _ ex, upd_other _ X i _ ex]
cases es : Nat.beq i S with
| true => rw [Found.Pnp2.beq_eq i S es, upd_same, upd_same]
| false => rw [upd_other _ S i _ es, upd_other _ S i _ es, upd_other r S i _ es]
have hS2 : upd (upd r S (dbl x ++ (true :: true :: w))) X (b :: r X) S = enc x w := by
rw [upd_other _ X S _ hSX, upd_same]
rfl
have rest := ih _ hS2
have s := runs_seq prog 4 (4 * x.length + 2) o o (o + 7) r _ _ hstep rest
have en : 4 + (4 * x.length + 2) = 4 * (b :: x).length + 2 := by
show 4 + (4 * x.length + 2) = 4 * (x.length + 1) + 2
rw [Nat.left_distrib, Nat.mul_one, Nat.add_comm 4 (4 * x.length + 2), Nat.add_right_comm]
rw [en] at s
refine And.intro s.1 (fun i => ?_)
refine (s.2 i).trans ?_
show upd (upd (upd (upd r S (dbl x ++ (true :: true :: w))) X (b :: r X)) S w) X
(revApp x (upd (upd r S (dbl x ++ (true :: true :: w))) X (b :: r X) X)) i =
upd (upd r S w) X (revApp x (b :: r X)) i
rw [upd_same]
cases ex : Nat.beq i X with
| true => rw [Found.Pnp2.beq_eq i X ex, upd_same, upd_same]
| false =>
rw [upd_other _ X i _ ex, upd_other _ X i _ ex]
cases es : Nat.beq i S with
| true => rw [Found.Pnp2.beq_eq i S es, upd_same, upd_same]
| false => rw [upd_other _ S i _ es, upd_other _ S i _ es, upd_other _ X i _ ex, upd_other r S i _ es]
end Found.Pnp71
#print axioms Found.Pnp71.unAt_of_frag
#print axioms Found.Pnp71.unenc_run
#print axioms: 2 theorems, no axioms.
UNENC is described on every input, with at most \(2|S|+2\) steps, and a run that reaches the end of a program reaches it while staying on its instructions.
found/Pnp72.lean
/-
T330 piece 72. found/Pnp72 -- the UNENC fragment on every input, and runs that end at the
end of the program (toward search-to-decision on the register machine, open lemma O5rm).
A verifier must run in polynomial time on every input, not only on well-formed encodings.
unG S a is what UNENC (Pnp71) does to register S holding S and register X holding a, on
any S; unN S is its number of steps. Proved:
* unenc_gen: from o, UNENC ends at o + 7 after unN S steps with S = (unG S a).2 and
X = (unG S a).1, other registers unchanged;
* unN_le: unN S <= |S| + |S| + 2;
* unG_enc, unN_enc: on enc x w these are (revApp x a, w) and 4 |x| + 2 (Pnp71);
* run_halt, first_end: a run that reaches the end of a program after N steps reaches it
after some N' <= N steps while staying on its instructions before (Pnp68 Inside).
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp71
import found.Pnp68
namespace Found.Pnp72
open Found.Pnp20 (Ins Cfg upd upd_same upd_other nthI step exec run revApp)
open Found.Pnp21 (Frag Runs runs_seq)
open Found.Pnp68 (Inside)
open Found.Pnp70 (run1_push run1_jmp run1_pop_nil run1_pop_cons nsym)
open Found.Pnp71 (UnAt)
open Found.Pnp4 (dbl enc)
/-- UNENC on any input: (bits pushed on X, what is left in S). -/
def unG : List Bool -> List Bool -> List Bool × List Bool
| [], a => (a, [])
| false :: [], a => (a, [])
| false :: b :: r, a => unG r (b :: a)
| true :: [], a => (a, [])
| true :: _ :: r, a => (a, r)
def unN : List Bool -> Nat
| [] => 1
| false :: [] => 2
| false :: _ :: r => 4 + unN r
| true :: [] => 2
| true :: _ :: _ => 2
theorem a4 (a : Nat) : a + a + 2 + 4 = (a + 1 + 1) + (a + 1 + 1) + 2 := by
have e : a + 2 + a = a + a + 2 := Nat.add_right_comm a 2 a
show a + a + 2 + 4 = (a + 2 + a) + 2 + 2
rw [e]
theorem unN_le : forall S : List Bool, unN S <= S.length + S.length + 2
| [] => Nat.le_succ 1
| false :: [] => Nat.le_add_left 2 2
| false :: _ :: r => by
have h := unN_le r
show 4 + unN r <= (r.length + 1 + 1) + (r.length + 1 + 1) + 2
refine Nat.le_trans (Nat.add_le_add_left h 4) (Nat.le_of_eq ?_)
rw [Nat.add_comm 4 (r.length + r.length + 2)]
exact a4 r.length
| true :: [] => Nat.le_add_left 2 2
| true :: _ :: _ => Nat.le_add_left 2 _
theorem unG_enc : forall (x w a : List Bool), unG (enc x w) a = (revApp x a, w)
| [], _, _ => rfl
| b :: x, w, a => unG_enc x w (b :: a)
theorem unN_enc : forall (x w : List Bool), unN (enc x w) = 4 * x.length + 2
| [], _ => rfl
| _ :: x, w => by
show 4 + unN (enc x w) = 4 * (x.length + 1) + 2
rw [unN_enc x w, Nat.left_distrib, Nat.mul_one, Nat.add_comm 4 (4 * x.length + 2), Nat.add_right_comm]
/-! ## pointwise register facts -/
theorem upd_over (x : Nat -> List Bool) (r : Nat) (v w : List Bool) :
forall i, upd (upd x r v) r w i = upd x r w i := by
intro i
cases e : Nat.beq i r with
| true => rw [Found.Pnp2.beq_eq i r e, upd_same, upd_same]
| false => rw [upd_other _ r i w e, upd_other _ r i v e, upd_other _ r i w e]
theorem upd_id (x : Nat -> List Bool) (r : Nat) (v : List Bool) (h : x r = v) :
forall i, upd x r v i = x i := by
intro i
cases e : Nat.beq i r with
| true => rw [Found.Pnp2.beq_eq i r e, upd_same, h]
| false => rw [upd_other _ r i v e]
/-- The shape of the final registers: S set, then X set. -/
def fin (r : Nat -> List Bool) (S X : Nat) (p : List Bool × List Bool) : Nat -> List Bool :=
upd (upd r S p.2) X p.1
theorem fin_cong (S X : Nat) (r r' : Nat -> List Bool) (h : forall i, r i = r' i) (p : List Bool × List Bool) :
forall i, fin r S X p i = fin r' S X p i :=
Found.Pnp20.upd_eqv _ _ (Found.Pnp20.upd_eqv r r' h S p.2) X p.1
/-- Setting S and X twice is setting them once. -/
theorem fin_fin (S X : Nat) (_hSX : Nat.beq S X = false) (r : Nat -> List Bool) (p q : List Bool × List Bool) :
forall i, fin (fin r S X p) S X q i = fin r S X q i := by
intro i
show upd (upd (upd (upd r S p.2) X p.1) S q.2) X q.1 i = upd (upd r S q.2) X q.1 i
cases ex : Nat.beq i X with
| true => rw [Found.Pnp2.beq_eq i X ex, upd_same, upd_same]
| false =>
rw [upd_other _ X i _ ex, upd_other _ X i _ ex]
cases es : Nat.beq i S with
| true => rw [Found.Pnp2.beq_eq i S es, upd_same, upd_same]
| false => rw [upd_other _ S i _ es, upd_other _ S i _ es, upd_other _ X i _ ex, upd_other r S i _ es]
/-! ## UNENC on every input -/
theorem unenc_gen (prog : List Ins) (o S X : Nat) (hu : UnAt prog o S X) (hSX : Nat.beq S X = false) :
forall (s : List Bool) (r : Nat -> List Bool), r S = s ->
Runs prog (unN s) o r (o + 7) (fin r S X (unG s (r X)))
| [], r, hr => by
have s0 := run1_pop_nil prog o S (o + 7) (o + 1) (o + 6) hu.i0 r hr
refine And.intro s0.1 (fun i => (s0.2 i).trans ?_)
show r i = upd (upd r S []) X (r X) i
have h1 : upd r S [] X = r X := upd_other r S X [] (nsym S X hSX)
rw [(upd_id (upd r S []) X (r X) h1 i), upd_id r S [] hr i]
| false :: [], r, hr => by
have s0 := run1_pop_cons prog o S (o + 7) (o + 1) (o + 6) hu.i0 r false [] hr
have s1 := run1_pop_nil prog (o + 1) S (o + 7) (o + 2) (o + 4) hu.i1 (upd r S []) (upd_same r S [])
have s := runs_seq prog 1 1 o (o + 1) (o + 7) r _ _ s0 s1
refine And.intro s.1 (fun i => (s.2 i).trans ?_)
show upd r S [] i = upd (upd r S []) X (r X) i
have h1 : upd r S [] X = r X := upd_other r S X [] (nsym S X hSX)
rw [upd_id (upd r S []) X (r X) h1 i]
| true :: [], r, hr => by
have s0 := run1_pop_cons prog o S (o + 7) (o + 1) (o + 6) hu.i0 r true [] hr
have s1 := run1_pop_nil prog (o + 6) S (o + 7) (o + 7) (o + 7) hu.i6 (upd r S []) (upd_same r S [])
have s := runs_seq prog 1 1 o (o + 6) (o + 7) r _ _ s0 s1
refine And.intro s.1 (fun i => (s.2 i).trans ?_)
show upd r S [] i = upd (upd r S []) X (r X) i
have h1 : upd r S [] X = r X := upd_other r S X [] (nsym S X hSX)
rw [upd_id (upd r S []) X (r X) h1 i]
| true :: c :: t, r, hr => by
have s0 := run1_pop_cons prog o S (o + 7) (o + 1) (o + 6) hu.i0 r true (c :: t) hr
have s1 := run1_pop_cons prog (o + 6) S (o + 7) (o + 7) (o + 7) hu.i6 (upd r S (c :: t)) c t
(upd_same r S _)
have e7 : Found.Pnp70.bsel c (o + 7) (o + 7) = o + 7 := by cases c with | false => rfl | true => rfl
rw [e7] at s1
have s := runs_seq prog 1 1 o (o + 6) (o + 7) r _ _ s0 s1
refine And.intro s.1 (fun i => (s.2 i).trans ?_)
show upd (upd r S (c :: t)) S t i = upd (upd r S t) X (r X) i
have h1 : upd r S t X = r X := upd_other r S X t (nsym S X hSX)
rw [upd_over r S (c :: t) t i, upd_id (upd r S t) X (r X) h1 i]
| false :: b :: t, r, hr => by
have s0 := run1_pop_cons prog o S (o + 7) (o + 1) (o + 6) hu.i0 r false (b :: t) hr
have s1 := run1_pop_cons prog (o + 1) S (o + 7) (o + 2) (o + 4) hu.i1 (upd r S (b :: t)) b t
(upd_same r S _)
-- after the pair: S = t, X = b :: r X
have hstep : Runs prog 4 o r o (fin r S X (b :: r X, t)) := by
have hX : upd (upd r S (b :: t)) S t X = r X := by
rw [upd_other _ S X t (nsym S X hSX), upd_other r S X _ (nsym S X hSX)]
cases b with
| false =>
have s2 := run1_push prog (o + 2) X false hu.i2 (upd (upd r S (false :: t)) S t)
have s := runs_seq prog 3 1 o (o + 3) o r _ _
(runs_seq prog 2 1 o (o + 2) (o + 3) r _ _ (runs_seq prog 1 1 o (o + 1) (o + 2) r _ _ s0 s1) s2)
(run1_jmp prog (o + 3) o hu.i3 _)
refine And.intro s.1 (fun i => (s.2 i).trans ?_)
show upd (upd (upd r S (false :: t)) S t) X (false :: upd (upd r S (false :: t)) S t X) i =
upd (upd r S t) X (false :: r X) i
rw [hX]
exact Found.Pnp20.upd_eqv _ _ (upd_over r S _ t) X _ i
| true =>
have s2 := run1_push prog (o + 4) X true hu.i4 (upd (upd r S (true :: t)) S t)
have s := runs_seq prog 3 1 o (o + 5) o r _ _
(runs_seq prog 2 1 o (o + 4) (o + 5) r _ _ (runs_seq prog 1 1 o (o + 1) (o + 4) r _ _ s0 s1) s2)
(run1_jmp prog (o + 5) o hu.i5 _)
refine And.intro s.1 (fun i => (s.2 i).trans ?_)
show upd (upd (upd r S (true :: t)) S t) X (true :: upd (upd r S (true :: t)) S t X) i =
upd (upd r S t) X (true :: r X) i
rw [hX]
exact Found.Pnp20.upd_eqv _ _ (upd_over r S _ t) X _ i
have hS2 : fin r S X (b :: r X, t) S = t := by
show upd (upd r S t) X (b :: r X) S = t
rw [upd_other _ X S _ hSX, upd_same]
have hX2 : fin r S X (b :: r X, t) X = b :: r X := upd_same _ X _
have rest := unenc_gen prog o S X hu hSX t _ hS2
rw [hX2] at rest
have s := runs_seq prog 4 (unN t) o o (o + 7) r _ _ hstep rest
refine And.intro s.1 (fun i => (s.2 i).trans ?_)
exact fin_fin S X hSX r _ _ i
/-! ## the first time a run reaches the end -/
theorem run_halt (p : List Ins) (c : Cfg) (h : nthI p c.pc = none) : forall n, run p n c = c := by
intro n
induction n with
| zero => rfl
| succ n ih =>
have e : step p c = c := by
show exec c (nthI p c.pc) = c
rw [h]
rfl
show run p n (step p c) = c
rw [e]
exact ih
theorem first_end (p : List Ins) : forall (N : Nat) (c : Cfg),
Exists fun N' => N' <= N /\ run p N' c = run p N c /\ Inside p N' c := by
intro N
induction N with
| zero => intro c; exact Exists.intro 0 (And.intro (Nat.le_refl 0) (And.intro rfl (fun _ hm => absurd hm (Nat.not_lt_zero _))))
| succ N ih =>
intro c
cases hc : nthI p c.pc with
| none =>
refine Exists.intro 0 (And.intro (Nat.zero_le _) (And.intro ?_ (fun _ hm => absurd hm (Nat.not_lt_zero _))))
exact (run_halt p c hc (N + 1)).symm
| some ins =>
cases ih (step p c) with
| intro N' h =>
refine Exists.intro (N' + 1) (And.intro (Nat.succ_le_succ h.1) (And.intro h.2.1 ?_))
intro m hm
cases m with
| zero => exact Exists.intro ins hc
| succ m => exact h.2.2 m (Nat.lt_of_succ_lt_succ hm)
/-- A run that is at the end of p after N steps is there after N' <= N steps, inside before. -/
theorem runs_first (p : List Ins) (N : Nat) (x y : Nat -> List Bool) (h : Runs p N 0 x p.length y) :
Exists fun N' => N' <= N /\ Runs p N' 0 x p.length y /\ Inside p N' (Cfg.mk 0 x) :=
match first_end p N (Cfg.mk 0 x) with
| Exists.intro N' hN =>
Exists.intro N' (And.intro hN.1 (And.intro
(by
refine And.intro ?_ (fun i => ?_)
· rw [hN.2.1]; exact h.1
· rw [hN.2.1]; exact h.2 i)
hN.2.2))
end Found.Pnp72
#print axioms Found.Pnp72.a4
#print axioms Found.Pnp72.unN_le
#print axioms Found.Pnp72.unG_enc
#print axioms Found.Pnp72.unN_enc
#print axioms Found.Pnp72.upd_over
#print axioms Found.Pnp72.upd_id
#print axioms Found.Pnp72.fin_cong
#print axioms Found.Pnp72.fin_fin
#print axioms Found.Pnp72.unenc_gen
#print axioms Found.Pnp72.run_halt
#print axioms Found.Pnp72.first_end
#print axioms Found.Pnp72.runs_first
#print axioms: 12 theorems, no axioms.
The LCHK fragment moves \(t\) while popping one bit of \(u\) per bit of \(t\), and leaves through a failure exit when \(u\) runs out.
found/Pnp73.lean
/-
T330 piece 73. found/Pnp73 -- the LCHK fragment: a length check in unary (toward
search-to-decision on the register machine, open lemma O5rm).
LCHK T U D fl moves register T onto D (reversed), popping one bit of U for every bit of T;
if U runs out first it leaves through the failure exit fl. Layout (offsets from o):
0: pop T (empty -> 7, 0 -> 1, 1 -> 4) 1: pop U (empty -> fl, else -> 2)
2: push D false 3: jmp 0 4: pop U (empty -> fl, else -> 5) 5: push D true 6: jmp 0
with the success exit 7. lc t u d = (ok, T, U, D) is the result on any contents, lcN its
number of steps. Proved:
* lchk_run: from o with T = t, U = u, LCHK runs lcN t u steps to o + 7 (ok) or o + fl
(not ok) with T, U, D as lc says and every other register unchanged;
* lcN_le: lcN t u <= 4 |t| + 1;
* lc_le: if |t| <= |u| then ok and D = revApp t d; lc_gt: if |u| < |t| then not ok.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp72
namespace Found.Pnp73
open Found.Pnp20 (Ins Cfg upd upd_same upd_other nthI revApp)
open Found.Pnp21 (Frag Runs runs_seq)
open Found.Pnp70 (run1_push run1_jmp run1_pop_nil run1_pop_cons nsym bsel)
def lchkP (T U D fl : Nat) : List Ins :=
[.pop T 7 1 4, .pop U fl 2 2, .push D false, .jmp 0, .pop U fl 5 5, .push D true, .jmp 0]
structure LcAt (prog : List Ins) (o T U D fl : Nat) : Prop where
i0 : nthI prog o = some (.pop T (o + 7) (o + 1) (o + 4))
i1 : nthI prog (o + 1) = some (.pop U (o + fl) (o + 2) (o + 2))
i2 : nthI prog (o + 2) = some (.push D false)
i3 : nthI prog (o + 3) = some (.jmp o)
i4 : nthI prog (o + 4) = some (.pop U (o + fl) (o + 5) (o + 5))
i5 : nthI prog (o + 5) = some (.push D true)
i6 : nthI prog (o + 6) = some (.jmp o)
theorem lcAt_of_frag (prog : List Ins) (o T U D fl : Nat) (hf : Frag prog o (lchkP T U D fl)) :
LcAt prog o T U D fl :=
LcAt.mk (hf 0 _ rfl) (hf 1 _ rfl) (hf 2 _ rfl) (hf 3 _ rfl) (hf 4 _ rfl) (hf 5 _ rfl) (hf 6 _ rfl)
/-- (ok, T, U, D) after LCHK. -/
def lc : List Bool -> List Bool -> List Bool -> Bool × List Bool × List Bool × List Bool
| [], u, d => (true, [], u, d)
| _ :: t, [], d => (false, t, [], d)
| b :: t, _ :: u, d => lc t u (b :: d)
def lcN : List Bool -> List Bool -> Nat
| [], _ => 1
| _ :: _, [] => 2
| _ :: t, _ :: u => 4 + lcN t u
/-- Registers T, U, D set. -/
def fin3 (r : Nat -> List Bool) (T U D : Nat) (q : Bool × List Bool × List Bool × List Bool) : Nat -> List Bool :=
upd (upd (upd r T q.2.1) U q.2.2.1) D q.2.2.2
/-- Two register functions that agree off T, U, D give the same fin3. -/
theorem fin3_absorb (T U D : Nat) (r y : Nat -> List Bool)
(h : forall i, Nat.beq i T = false -> Nat.beq i U = false -> Nat.beq i D = false -> y i = r i)
(q : Bool × List Bool × List Bool × List Bool) : forall i, fin3 y T U D q i = fin3 r T U D q i := by
intro i
show upd (upd (upd y T q.2.1) U q.2.2.1) D q.2.2.2 i = upd (upd (upd r T q.2.1) U q.2.2.1) D q.2.2.2 i
cases eD : Nat.beq i D with
| true => rw [Found.Pnp2.beq_eq i D eD, upd_same, upd_same]
| false =>
rw [upd_other _ D i _ eD, upd_other _ D i _ eD]
cases eU : Nat.beq i U with
| true => rw [Found.Pnp2.beq_eq i U eU, upd_same, upd_same]
| false =>
rw [upd_other _ U i _ eU, upd_other _ U i _ eU]
cases eT : Nat.beq i T with
| true => rw [Found.Pnp2.beq_eq i T eT, upd_same, upd_same]
| false => rw [upd_other _ T i _ eT, upd_other _ T i _ eT]; exact h i eT eU eD
/-- A register function with the right T, U, D that agrees with r elsewhere is fin3 r. -/
theorem fin3_ext (T U D : Nat) (r y : Nat -> List Bool) (q : Bool × List Bool × List Bool × List Bool)
(hT : y T = q.2.1) (hU : y U = q.2.2.1) (hD : y D = q.2.2.2)
(h : forall i, Nat.beq i T = false -> Nat.beq i U = false -> Nat.beq i D = false -> y i = r i) :
forall i, y i = fin3 r T U D q i := by
intro i
show y i = upd (upd (upd r T q.2.1) U q.2.2.1) D q.2.2.2 i
cases eD : Nat.beq i D with
| true => rw [Found.Pnp2.beq_eq i D eD, upd_same, hD]
| false =>
rw [upd_other _ D i _ eD]
cases eU : Nat.beq i U with
| true => rw [Found.Pnp2.beq_eq i U eU, upd_same, hU]
| false =>
rw [upd_other _ U i _ eU]
cases eT : Nat.beq i T with
| true => rw [Found.Pnp2.beq_eq i T eT, upd_same, hT]
| false => rw [upd_other _ T i _ eT]; exact h i eT eU eD
theorem lchk_run (prog : List Ins) (o T U D fl : Nat) (hl : LcAt prog o T U D fl)
(hTU : Nat.beq T U = false) (hTD : Nat.beq T D = false) (hUD : Nat.beq U D = false) :
forall (t u : List Bool) (r : Nat -> List Bool), r T = t -> r U = u ->
Runs prog (lcN t u) o r (bsel (lc t u (r D)).1 (o + 7) (o + fl)) (fin3 r T U D (lc t u (r D)))
| [], u, r, ht, hu => by
have s0 := run1_pop_nil prog o T (o + 7) (o + 1) (o + 4) hl.i0 r ht
exact And.intro s0.1 (fun i => (s0.2 i).trans (fin3_ext T U D r r _ ht hu rfl (fun _ _ _ _ => rfl) i))
| b :: t, [], r, ht, hu => by
have s0 := run1_pop_cons prog o T (o + 7) (o + 1) (o + 4) hl.i0 r b t ht
have hu1 : upd r T t U = [] := by rw [upd_other r T U t (nsym T U hTU)]; exact hu
have s1 : Runs prog 1 (bsel b (o + 4) (o + 1)) (upd r T t) (o + fl) (upd r T t) := by
cases b with
| false => exact run1_pop_nil prog (o + 1) U (o + fl) (o + 2) (o + 2) hl.i1 _ hu1
| true => exact run1_pop_nil prog (o + 4) U (o + fl) (o + 5) (o + 5) hl.i4 _ hu1
have s := runs_seq prog 1 1 o _ (o + fl) r _ _ s0 s1
refine And.intro s.1 (fun i => (s.2 i).trans ?_)
exact fin3_ext T U D r (upd r T t) _ (upd_same r T t) hu1 (upd_other r T D t (nsym T D hTD))
(fun j hj _ _ => upd_other r T j t hj) i
| b :: t, c :: u, r, ht, hu => by
have s0 := run1_pop_cons prog o T (o + 7) (o + 1) (o + 4) hl.i0 r b t ht
have hu1 : upd r T t U = c :: u := by rw [upd_other r T U t (nsym T U hTU)]; exact hu
-- y1: T = t, U = u, D = b :: r D
have hD1 : upd (upd r T t) U u D = r D := by
rw [upd_other _ U D u (nsym U D hUD), upd_other r T D t (nsym T D hTD)]
have hstep : Runs prog 4 o r o (upd (upd (upd r T t) U u) D (b :: r D)) := by
cases b with
| false =>
have s1 := run1_pop_cons prog (o + 1) U (o + fl) (o + 2) (o + 2) hl.i1 _ c u hu1
have e2 : bsel c (o + 2) (o + 2) = o + 2 := by cases c with | false => rfl | true => rfl
rw [e2] at s1
have s2 := run1_push prog (o + 2) D false hl.i2 (upd (upd r T t) U u)
have s := runs_seq prog 3 1 o (o + 3) o r _ _
(runs_seq prog 2 1 o (o + 2) (o + 3) r _ _ (runs_seq prog 1 1 o (o + 1) (o + 2) r _ _ s0 s1) s2)
(run1_jmp prog (o + 3) o hl.i3 _)
refine And.intro s.1 (fun i => (s.2 i).trans ?_)
rw [hD1]
| true =>
have s1 := run1_pop_cons prog (o + 4) U (o + fl) (o + 5) (o + 5) hl.i4 _ c u hu1
have e5 : bsel c (o + 5) (o + 5) = o + 5 := by cases c with | false => rfl | true => rfl
rw [e5] at s1
have s2 := run1_push prog (o + 5) D true hl.i5 (upd (upd r T t) U u)
have s := runs_seq prog 3 1 o (o + 6) o r _ _
(runs_seq prog 2 1 o (o + 5) (o + 6) r _ _ (runs_seq prog 1 1 o (o + 4) (o + 5) r _ _ s0 s1) s2)
(run1_jmp prog (o + 6) o hl.i6 _)
refine And.intro s.1 (fun i => (s.2 i).trans ?_)
rw [hD1]
have hT2 : upd (upd (upd r T t) U u) D (b :: r D) T = t := by
rw [upd_other _ D T _ hTD, upd_other _ U T u hTU, upd_same]
have hU2 : upd (upd (upd r T t) U u) D (b :: r D) U = u := by
rw [upd_other _ D U _ hUD, upd_same]
have hD2 : upd (upd (upd r T t) U u) D (b :: r D) D = b :: r D := upd_same _ D _
have rest := lchk_run prog o T U D fl hl hTU hTD hUD t u _ hT2 hU2
rw [hD2] at rest
have s := runs_seq prog 4 (lcN t u) o o _ r _ _ hstep rest
refine And.intro s.1 (fun i => (s.2 i).trans ?_)
refine fin3_absorb T U D r _ (fun j hj hu' hd => ?_) _ i
rw [upd_other _ D j _ hd, upd_other _ U j u hu', upd_other r T j t hj]
theorem lcN_le : forall t u : List Bool, lcN t u <= 4 * t.length + 1
| [], _ => Nat.le_refl 1
| _ :: t, [] => by
show 2 <= 4 * (t.length + 1) + 1
rw [Nat.left_distrib, Nat.mul_one]
exact Nat.le_trans (Nat.le_add_left 2 3) (Nat.le_add_left 5 (4 * t.length))
| _ :: t, _ :: u => by
show 4 + lcN t u <= 4 * (t.length + 1) + 1
rw [Nat.left_distrib, Nat.mul_one, Nat.add_right_comm, Nat.add_comm (4 * t.length + 1) 4]
exact Nat.add_le_add_left (lcN_le t u) 4
theorem lc_le : forall (t u d : List Bool), t.length <= u.length ->
(lc t u d).1 = true /\ (lc t u d).2.2.2 = revApp t d
| [], _, _, _ => And.intro rfl rfl
| _ :: t, [], _, h => absurd h (Nat.not_succ_le_zero t.length)
| b :: t, _ :: u, d, h => lc_le t u (b :: d) (Nat.le_of_succ_le_succ h)
theorem lc_gt : forall (t u d : List Bool), u.length < t.length -> (lc t u d).1 = false
| [], _, _, h => absurd h (Nat.not_lt_zero _)
| _ :: _, [], _, _ => rfl
| b :: t, _ :: u, d, h => lc_gt t u (b :: d) (Nat.lt_of_succ_lt_succ h)
end Found.Pnp73
#print axioms Found.Pnp73.lcAt_of_frag
#print axioms Found.Pnp73.fin3_absorb
#print axioms Found.Pnp73.fin3_ext
#print axioms Found.Pnp73.lchk_run
#print axioms Found.Pnp73.lcN_le
#print axioms Found.Pnp73.lc_le
#print axioms Found.Pnp73.lc_gt
#print axioms: 7 theorems, no axioms.
The verifier \(V_f\) assembled from UNENC, MOVE, LCHK, a relocated call of \(F\) and EQ ends on every input within a polynomial number of steps.
found/Pnp74.lean
/-
T330 piece 74. found/Pnp74 -- the verifier program V_f on the register machine (toward
search-to-decision on the register machine, open lemma O5rm).
For a program F computing f, vP F reads enc t s from register 13, with t meant to be
enc z (enc p u), and accepts when |s| <= |u| and f (p ++ s) = z. Layout (registers):
UNENC 13 -> 17; MOVE 17 -> 18; UNENC 18 -> 19 (z reversed); UNENC 18 -> 20 (p reversed,
18 = u); MOVE 13 -> 21; LCHK 21 18 45 (fail -> end handler); MOVE 20 -> 45 (45 = p ++ s);
F relocated by 32 (input 45, output 48); MOVE 19 -> 22 (z); EQ 22 48 16; jmp end;
fail handler: push 16 false.
vF f a is the bit it leaves, defined on every input. Proved:
* spec forms of UNENC, MOVE, LCHK with frame conditions;
* v_run: on every input a, vP F ends at its end within
10 (4 |a| + 4) + pe eF |a| steps with outBit of register 16 = vF f a.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp73
import found.Owf4
namespace Found.Pnp74
open Found.Pnp3 (PE pe pe_mono)
open Found.Pnp20 (Ins Cfg upd upd_same upd_other nthI run revApp move)
open Found.Pnp21 (Frag Runs runs_seq shift frag_move_run)
open Found.Pnp23 (appI mapI frag_app frag_app_l)
open Found.Pnp59 (inpR inpR_13 inpR_other frag_self)
open Found.Pnp67 (relocP)
open Found.Pnp69 (relocP_len call_run)
open Found.Pnp70 (run1_push run1_jmp nsym bsel eqP eqAt_of_frag eq_run EqOut)
open Found.Pnp71 (unencP unAt_of_frag UnAt)
open Found.Pnp72 (unG unN unN_le unenc_gen fin)
open Found.Pnp73 (lchkP lcAt_of_frag LcAt lc lcN lcN_le lchk_run fin3)
open Found.Owf1 (beqL)
open Found.Owf4 (outBit CompRM)
/-! ## the program -/
def seqI (p q : List Ins) : List Ins := appI p (mapI (shift p.length) q)
theorem fragHT (prog : List Ins) (o : Nat) (p q : List Ins) (h : Frag prog o (seqI p q)) :
Frag prog o p /\ Frag prog (o + p.length) q :=
And.intro (frag_app_l prog o p _ h) (frag_app prog o p q h)
def t11 : List Ins := seqI [.jmp 2] [.push 16 false]
def t10 : List Ins := seqI (eqP 22 48 16) t11
def t9 : List Ins := seqI (move 19 22) t10
def t8 (F : List Ins) : List Ins := seqI (relocP 32 F) t9
def t7 (F : List Ins) : List Ins := seqI (move 20 45) (t8 F)
def t6 (F : List Ins) : List Ins := seqI (lchkP 21 18 45 (25 + F.length)) (t7 F)
def t5 (F : List Ins) : List Ins := seqI (move 13 21) (t6 F)
def t4 (F : List Ins) : List Ins := seqI (unencP 18 20) (t5 F)
def t3 (F : List Ins) : List Ins := seqI (unencP 18 19) (t4 F)
def t2 (F : List Ins) : List Ins := seqI (move 17 18) (t3 F)
def vP (F : List Ins) : List Ins := seqI (unencP 13 17) (t2 F)
theorem appI_len : forall p q : List Ins, (appI p q).length = p.length + q.length
| [], q => (Nat.zero_add q.length).symm
| _ :: p, q => by
show (appI p q).length + 1 = p.length + 1 + q.length
rw [appI_len p q, Nat.add_right_comm]
theorem mapI_len (g : Ins -> Ins) : forall p : List Ins, (mapI g p).length = p.length
| [] => rfl
| _ :: p => congrArg Nat.succ (mapI_len g p)
theorem seqI_len (p q : List Ins) : (seqI p q).length = p.length + q.length := by
show (appI p (mapI (shift p.length) q)).length = _
rw [appI_len, mapI_len]
theorem vP_len (F : List Ins) : (vP F).length = 43 + F.length + 5 + 7 + 2 := by
show (seqI (unencP 13 17) (t2 F)).length = _
rw [seqI_len]; show 7 + (seqI (move 17 18) (t3 F)).length = _
rw [seqI_len]; show 7 + (5 + (seqI (unencP 18 19) (t4 F)).length) = _
rw [seqI_len]; show 7 + (5 + (7 + (seqI (unencP 18 20) (t5 F)).length)) = _
rw [seqI_len]; show 7 + (5 + (7 + (7 + (seqI (move 13 21) (t6 F)).length))) = _
rw [seqI_len]; show 7 + (5 + (7 + (7 + (5 + (seqI (lchkP 21 18 45 (25 + F.length)) (t7 F)).length)))) = _
rw [seqI_len]; show 7 + (5 + (7 + (7 + (5 + (7 + (seqI (move 20 45) (t8 F)).length))))) = _
rw [seqI_len]; show 7 + (5 + (7 + (7 + (5 + (7 + (5 + (seqI (relocP 32 F) t9).length)))))) = _
rw [seqI_len, relocP_len]; show 7 + (5 + (7 + (7 + (5 + (7 + (5 + (F.length + (seqI (move 19 22) t10).length))))))) = _
rw [seqI_len]; show 7 + (5 + (7 + (7 + (5 + (7 + (5 + (F.length + (5 + (seqI (eqP 22 48 16) t11).length)))))))) = _
rw [seqI_len]; show 7 + (5 + (7 + (7 + (5 + (7 + (5 + (F.length + (5 + (7 + (seqI [.jmp 2] [.push 16 false]).length))))))))) = _
rw [seqI_len]
show 7 + (5 + (7 + (7 + (5 + (7 + (5 + (F.length + 14))))))) = 43 + F.length + 14
repeat rw [← Nat.add_assoc]
theorem ar1 (n : Nat) : 31 + (25 + n) = 43 + n + 5 + 7 + 1 := by
rw [← Nat.add_assoc, Nat.add_comm 43 n, Nat.add_comm (31 + 25) n]
/-! ## the function computed -/
def selB : Bool -> Bool -> Bool -> Bool
| true, a, _ => a
| false, _, b => b
def vF (f : List Bool -> List Bool) (a : List Bool) : Bool :=
selB (lc (revApp (unG a []).2 []) (unG (unG (revApp (unG a []).1 []) []).2 []).2 []).1
(beqL (revApp (unG (revApp (unG a []).1 []) []).1 [])
(f (revApp (unG (unG (revApp (unG a []).1 []) []).2 []).1
(lc (revApp (unG a []).2 []) (unG (unG (revApp (unG a []).1 []) []).2 []).2 []).2.2.2)))
false
/-! ## spec forms -/
theorem unenc_spec (prog : List Ins) (o S X : Nat) (hu : UnAt prog o S X) (hSX : Nat.beq S X = false)
(s acc : List Bool) (x : Nat -> List Bool) (hs : x S = s) (hx : x X = acc) :
Exists fun y => Runs prog (unN s) o x (o + 7) y /\ y S = (unG s acc).2 /\ y X = (unG s acc).1 /\
(forall i, Nat.beq i S = false -> Nat.beq i X = false -> y i = x i) := by
have r := unenc_gen prog o S X hu hSX s x hs
rw [hx] at r
refine Exists.intro _ (And.intro r (And.intro ?_ (And.intro (upd_same _ X _) ?_)))
· show upd (upd x S (unG s acc).2) X (unG s acc).1 S = _
rw [upd_other _ X S _ hSX, upd_same]
· intro i hi1 hi2
show upd (upd x S (unG s acc).2) X (unG s acc).1 i = x i
rw [upd_other _ X i _ hi2, upd_other _ S i _ hi1]
theorem move_spec (prog : List Ins) (o r s : Nat) (hf : Frag prog o (move r s)) (hrs : Nat.beq r s = false)
(l m : List Bool) (x : Nat -> List Bool) (hl : x r = l) (hm : x s = m) :
Exists fun y => Runs prog (3 * l.length + 1) o x (o + 5) y /\ y r = [] /\ y s = revApp l m /\
(forall i, Nat.beq i r = false -> Nat.beq i s = false -> y i = x i) := by
have h := frag_move_run prog o r s hf hrs l m x hl hm
exact Exists.intro _ (And.intro (And.intro h.1 (fun _ => rfl)) h.2)
theorem lchk_spec (prog : List Ins) (o T U D fl : Nat) (hl : LcAt prog o T U D fl)
(hTU : Nat.beq T U = false) (hTD : Nat.beq T D = false) (hUD : Nat.beq U D = false)
(t u d : List Bool) (x : Nat -> List Bool) (ht : x T = t) (hu : x U = u) (hd : x D = d) :
Exists fun y => Runs prog (lcN t u) o x (bsel (lc t u d).1 (o + 7) (o + fl)) y /\
y D = (lc t u d).2.2.2 /\
(forall i, Nat.beq i T = false -> Nat.beq i U = false -> Nat.beq i D = false -> y i = x i) := by
have r := lchk_run prog o T U D fl hl hTU hTD hUD t u x ht hu
rw [hd] at r
refine Exists.intro _ (And.intro r (And.intro (upd_same _ D _) ?_))
intro i h1 h2 h3
show upd (upd (upd x T _) U _) D _ i = x i
rw [upd_other _ D i _ h3, upd_other _ U i _ h2, upd_other _ T i _ h1]
/-! ## the high registers (the block of F) -/
def Hi (y : Nat -> List Bool) (w : List Bool) : Prop := forall i, y (i + 32) = inpR w i
theorem lt32 (a : Nat) (h : Nat.ble (a + 1) 32 = true) : a < 32 := Nat.le_of_ble_eq_true h
theorem hi_lt (A i : Nat) (hA : A < 32) : Nat.beq (i + 32) A = false :=
Found.Pnp56.beq_gt (i + 32) A (Nat.lt_of_lt_of_le hA (Nat.le_add_left 32 i))
theorem inpR_nil (i : Nat) : inpR [] i = [] := by
show Found.Pnp20.selR (Nat.beq i 13) [] [] = []
cases Nat.beq i 13 with
| false => rfl
| true => rfl
theorem hi_init (a : List Bool) : Hi (inpR a) [] := by
intro i
rw [inpR_nil, inpR_other a (i + 32) (hi_lt 13 i (lt32 13 rfl))]
theorem hi_keep (x y : Nat -> List Bool) (w : List Bool) (A B : Nat) (hA : A < 32) (hB : B < 32)
(fr : forall i, Nat.beq i A = false -> Nat.beq i B = false -> y i = x i) (h : Hi x w) : Hi y w :=
fun i => (fr (i + 32) (hi_lt A i hA) (hi_lt B i hB)).trans (h i)
theorem hi_set (x y : Nat -> List Bool) (w d : List Bool)
(fr : forall i, Nat.beq i 13 = false -> y (i + 32) = x (i + 32)) (hd : y 45 = d) (h : Hi x w) : Hi y d := by
intro i
cases e : Nat.beq i 13 with
| true =>
have e' := Found.Pnp2.beq_eq i 13 e
rw [e', inpR_13]
exact hd
| false =>
rw [fr i e, h i, inpR_other w i e, inpR_other d i e]
theorem e45 (i : Nat) (e : Nat.beq i 13 = false) : Nat.beq (i + 32) 45 = false :=
(Found.Pnp56.beq_add_r i 13 32).trans e
/-! ## arithmetic for the bound -/
def Bn (n : Nat) : Nat := n + n + n + n + 4
theorem q4 (m n : Nat) (h : m <= n) : m + m + m + m + 4 <= Bn n :=
Nat.add_le_add_right (Nat.add_le_add (Nat.add_le_add (Nat.add_le_add h h) h) h) 4
theorem bA (m n : Nat) (h : m <= n) : m + m + 2 <= Bn n :=
Nat.le_trans (Nat.add_le_add (Nat.le_trans (Nat.le_add_right (m + m) m) (Nat.le_add_right (m + m + m) m))
(Nat.le_add_left 2 2)) (q4 m n h)
theorem three (m : Nat) : 3 * m = m + m + m := by
rw [Nat.mul_comm]
show 0 + m + m + m = m + m + m
rw [Nat.zero_add]
theorem four (m : Nat) : 4 * m = m + m + m + m := by
rw [Nat.mul_comm]
show 0 + m + m + m + m = m + m + m + m
rw [Nat.zero_add]
theorem bB (m n : Nat) (h : m <= n) : 3 * m + 1 <= Bn n := by
rw [three]
exact Nat.le_trans (Nat.add_le_add (Nat.le_add_right (m + m + m) m) (Nat.le_add_left 1 3)) (q4 m n h)
theorem bC (m n : Nat) (h : m <= n) : 4 * m + 1 <= Bn n := by
rw [four]
exact Nat.le_trans (Nat.add_le_add_left (Nat.le_add_left 1 3) _) (q4 m n h)
theorem bD (m n : Nat) (h : m <= n) : m + m + 4 <= Bn n :=
Nat.le_trans (Nat.add_le_add_right (Nat.le_trans (Nat.le_add_right (m + m) m) (Nat.le_add_right (m + m + m) m)) 4)
(q4 m n h)
theorem bE (n : Nat) : 1 <= Bn n := Nat.le_trans (Nat.le_add_left 1 3) (Nat.le_add_left 4 _)
theorem acc (x y k B C : Nat) (h1 : x <= k * B + C) (h2 : y <= B) : x + y <= (k + 1) * B + C := by
rw [Nat.succ_mul, Nat.add_right_comm]
exact Nat.add_le_add h1 h2
theorem acc1 (y B : Nat) (h : y <= B) : y <= 1 * B + 0 := by
rw [Nat.one_mul]; exact h
/-! ## lengths -/
theorem revApp_len : forall (l m : List Bool), (revApp l m).length = l.length + m.length
| [], m => (Nat.zero_add m.length).symm
| b :: l, m => by
show (revApp l (b :: m)).length = l.length + 1 + m.length
rw [revApp_len l (b :: m), Nat.add_right_comm]
rfl
theorem unG_len : forall (S acc : List Bool), (unG S acc).1.length + (unG S acc).2.length <= S.length + acc.length
| [], acc => Nat.le_of_eq (Nat.add_comm acc.length 0)
| false :: [], acc => Nat.le_trans (Nat.le_of_eq (Nat.add_comm acc.length 0)) (Nat.add_le_add_right (Nat.zero_le 1) _)
| false :: b :: r, acc => by
have h := unG_len r (b :: acc)
refine Nat.le_trans h ?_
show r.length + (acc.length + 1) <= r.length + 1 + 1 + acc.length
rw [Nat.add_comm acc.length 1, ← Nat.add_assoc, Nat.add_right_comm r.length 1 1, Nat.add_assoc (r.length + 1) 1 acc.length]
exact Nat.add_le_add_left (Nat.le_add_left acc.length 1) (r.length + 1)
| true :: [], acc => Nat.le_trans (Nat.le_of_eq (Nat.add_comm acc.length 0)) (Nat.add_le_add_right (Nat.zero_le 1) _)
| true :: _ :: r, acc => by
show acc.length + r.length <= r.length + 1 + 1 + acc.length
rw [Nat.add_comm acc.length r.length]
exact Nat.add_le_add_right (Nat.le_trans (Nat.le_add_right r.length 1) (Nat.le_add_right _ 1)) _
theorem lc_okD : forall (t u d : List Bool), (lc t u d).1 = true -> (lc t u d).2.2.2 = revApp t d
| [], _, _, _ => rfl
| _ :: _, [], _, h => Bool.noConfusion h
| b :: t, _ :: u, d, h => lc_okD t u (b :: d) h
theorem le_l (a b c : Nat) (h : a + b <= c) : a <= c := Nat.le_trans (Nat.le_add_right a b) h
theorem le_r (a b c : Nat) (h : a + b <= c) : b <= c := Nat.le_trans (Nat.le_add_left b a) h
/-! ## the run -/
def vE (eF : PE) : PE :=
.add (.mul (.c 10) (.add (.add (.add (.add .x .x) .x) .x) (.c 4))) eF
theorem v_run (F : List Ins) (eF : PE) (f : List Bool -> List Bool) (hF : CompRM F eF f) (a : List Bool) :
Exists fun N => Exists fun y => N <= pe (vE eF) a.length /\ Runs (vP F) N 0 (inpR a) (vP F).length y /\
outBit (y 16) = vF f a := by
have hf0 := frag_self (vP F)
have g1 := fragHT (vP F) 0 (unencP 13 17) (t2 F) hf0
have g2 := fragHT (vP F) 7 (move 17 18) (t3 F) g1.2
have g3 := fragHT (vP F) 12 (unencP 18 19) (t4 F) g2.2
have g4 := fragHT (vP F) 19 (unencP 18 20) (t5 F) g3.2
have g5 := fragHT (vP F) 26 (move 13 21) (t6 F) g4.2
have g6 := fragHT (vP F) 31 (lchkP 21 18 45 (25 + F.length)) (t7 F) g5.2
have g7 := fragHT (vP F) 38 (move 20 45) (t8 F) g6.2
have g8 := fragHT (vP F) 43 (relocP 32 F) t9 g7.2
have g8' : Frag (vP F) (43 + F.length) t9 := by
have e := g8.2
rw [relocP_len] at e
exact e
have g9 := fragHT (vP F) (43 + F.length) (move 19 22) t10 g8'
have g10 := fragHT (vP F) (43 + F.length + 5) (eqP 22 48 16) t11 g9.2
have g11 := fragHT (vP F) (43 + F.length + 5 + 7) [.jmp 2] [.push 16 false] g10.2
have hlen := vP_len F
-- A: UNENC 13 -> 17
cases unenc_spec (vP F) 0 13 17 (unAt_of_frag _ _ _ _ g1.1) rfl a [] (inpR a) (inpR_13 a) rfl with
| intro yA hA =>
-- B: MOVE 17 -> 18
cases move_spec (vP F) 7 17 18 g2.1 rfl (unG a []).1 [] yA hA.2.2.1 (hA.2.2.2 18 rfl rfl) with
| intro yB hB =>
-- C: UNENC 18 -> 19
cases unenc_spec (vP F) 12 18 19 (unAt_of_frag _ _ _ _ g3.1) rfl (revApp (unG a []).1 []) [] yB hB.2.2.1
((hB.2.2.2 19 rfl rfl).trans (hA.2.2.2 19 rfl rfl)) with
| intro yC hC =>
-- D: UNENC 18 -> 20
cases unenc_spec (vP F) 19 18 20 (unAt_of_frag _ _ _ _ g4.1) rfl (unG (revApp (unG a []).1 []) []).2 [] yC hC.2.1
((hC.2.2.2 20 rfl rfl).trans ((hB.2.2.2 20 rfl rfl).trans (hA.2.2.2 20 rfl rfl))) with
| intro yD hD =>
-- E: MOVE 13 -> 21
cases move_spec (vP F) 26 13 21 g5.1 rfl (unG a []).2 [] yD
((hD.2.2.2 13 rfl rfl).trans ((hC.2.2.2 13 rfl rfl).trans ((hB.2.2.2 13 rfl rfl).trans hA.2.1)))
((hD.2.2.2 21 rfl rfl).trans ((hC.2.2.2 21 rfl rfl).trans ((hB.2.2.2 21 rfl rfl).trans (hA.2.2.2 21 rfl rfl)))) with
| intro yE hE =>
-- F': LCHK 21 18 45
cases lchk_spec (vP F) 31 21 18 45 (25 + F.length) (lcAt_of_frag _ _ _ _ _ _ g6.1) rfl rfl rfl
(revApp (unG a []).2 []) (unG (unG (revApp (unG a []).1 []) []).2 []).2 [] yE
(hE.2.2.1)
((hE.2.2.2 18 rfl rfl).trans hD.2.1)
((hE.2.2.2 45 rfl rfl).trans ((hD.2.2.2 45 rfl rfl).trans ((hC.2.2.2 45 rfl rfl).trans
((hB.2.2.2 45 rfl rfl).trans (hA.2.2.2 45 rfl rfl))))) with
| intro yL hL =>
-- the lengths
have l1 := unG_len a []
have l2 := unG_len (revApp (unG a []).1 []) []
have l3 := unG_len (unG (revApp (unG a []).1 []) []).2 []
have ly : (revApp (unG a []).1 []).length = (unG a []).1.length := revApp_len _ []
have lt : (revApp (unG a []).2 []).length = (unG a []).2.length := revApp_len _ []
have m1 : (unG a []).1.length <= a.length := le_l _ _ _ l1
have m2 : (unG a []).2.length <= a.length := le_r _ _ _ l1
have myv : (revApp (unG a []).1 []).length <= a.length := by rw [ly]; exact m1
have m3 : (unG (revApp (unG a []).1 []) []).2.length <= a.length := Nat.le_trans (le_r _ _ _ l2) myv
have m4 : (unG (revApp (unG a []).1 []) []).1.length <= a.length := Nat.le_trans (le_l _ _ _ l2) myv
have m5 : (unG (unG (revApp (unG a []).1 []) []).2 []).1.length <= a.length := Nat.le_trans (le_l _ _ _ l3) m3
have mt : (revApp (unG a []).2 []).length <= a.length := by rw [lt]; exact m2
-- the steps before the check
have p5 : unN a + (3 * (unG a []).1.length + 1) + unN (revApp (unG a []).1 []) +
unN (unG (revApp (unG a []).1 []) []).2 + (3 * (unG a []).2.length + 1) +
lcN (revApp (unG a []).2 []) (unG (unG (revApp (unG a []).1 []) []).2 []).2 <= 6 * Bn a.length + 0 :=
acc _ _ 5 _ 0 (acc _ _ 4 _ 0 (acc _ _ 3 _ 0 (acc _ _ 2 _ 0 (acc _ _ 1 _ 0
(acc1 _ _ (Nat.le_trans (unN_le a) (bA a.length a.length (Nat.le_refl _))))
(bB _ _ m1))
(Nat.le_trans (unN_le _) (bA _ _ myv)))
(Nat.le_trans (unN_le _) (bA _ _ m3)))
(bB _ _ m2))
(Nat.le_trans (lcN_le _ _) (bC _ _ mt))
have s5 := runs_seq (vP F) _ _ 0 26 31 (inpR a) yD yE
(runs_seq (vP F) _ _ 0 19 26 (inpR a) yC yD
(runs_seq (vP F) _ _ 0 12 19 (inpR a) yB yC
(runs_seq (vP F) _ _ 0 7 12 (inpR a) yA yB hA.1 hB.1) hC.1) hD.1) hE.1
have hlr := hL.1
-- the bit of the check
cases hok : (lc (revApp (unG a []).2 []) (unG (unG (revApp (unG a []).1 []) []).2 []).2 []).1 with
| false =>
rw [hok] at hlr
have hlr' : Runs (vP F) (lcN (revApp (unG a []).2 []) (unG (unG (revApp (unG a []).1 []) []).2 []).2) 31 yE (43 + F.length + 5 + 7 + 1) yL := by
rw [(ar1 F.length).symm]; exact hlr
have sp := run1_push (vP F) (43 + F.length + 5 + 7 + 1) 16 false (g11.2 0 _ rfl) yL
have s := runs_seq (vP F) _ 1 0 _ (43 + F.length + 5 + 7 + 2) (inpR a) yL _
(runs_seq (vP F) _ _ 0 31 _ (inpR a) yE yL s5 hlr') sp
rw [hlen.symm] at s
refine Exists.intro _ (Exists.intro _ (And.intro ?_ (And.intro s ?_)))
· refine Nat.le_trans (acc _ _ 6 _ 0 p5 (bE a.length)) ?_
show 7 * Bn a.length + 0 <= 10 * Bn a.length + pe eF a.length
exact Nat.le_trans (Nat.mul_le_mul_right _ (Nat.le_add_left 7 3)) (Nat.le_add_right _ _)
· show outBit (upd yL 16 (false :: yL 16) 16) = vF f a
rw [upd_same]
show false = selB _ _ false
rw [hok]
rfl
| true =>
rw [hok] at hlr
have hd := lc_okD _ _ _ hok
-- G: MOVE 20 -> 45
cases move_spec (vP F) 38 20 45 g7.1 rfl (unG (unG (revApp (unG a []).1 []) []).2 []).1
(lc (revApp (unG a []).2 []) (unG (unG (revApp (unG a []).1 []) []).2 []).2 []).2.2.2 yL
((hL.2.2 20 rfl rfl rfl).trans ((hE.2.2.2 20 rfl rfl).trans hD.2.2.1)) hL.2.1 with
| intro yG hG =>
-- the high block
have hiE : Hi yE [] :=
hi_keep yD yE [] 13 21 (lt32 13 rfl) (lt32 21 rfl) hE.2.2.2
(hi_keep yC yD [] 18 20 (lt32 18 rfl) (lt32 20 rfl) hD.2.2.2
(hi_keep yB yC [] 18 19 (lt32 18 rfl) (lt32 19 rfl) hC.2.2.2
(hi_keep yA yB [] 17 18 (lt32 17 rfl) (lt32 18 rfl) hB.2.2.2
(hi_keep (inpR a) yA [] 13 17 (lt32 13 rfl) (lt32 17 rfl) hA.2.2.2 (hi_init a)))))
have hiL : Hi yL _ :=
hi_set yE yL [] _ (fun i e => hL.2.2 (i + 32) (hi_lt 21 i (lt32 21 rfl)) (hi_lt 18 i (lt32 18 rfl)) (e45 i e))
hL.2.1 hiE
have hiG : Hi yG _ :=
hi_set yL yG _ _ (fun i e => hG.2.2.2 (i + 32) (hi_lt 20 i (lt32 20 rfl)) (e45 i e)) hG.2.2.1 hiL
-- H: call F
cases hF (revApp (unG (unG (revApp (unG a []).1 []) []).2 []).1
(lc (revApp (unG a []).2 []) (unG (unG (revApp (unG a []).1 []) []).2 []).2 []).2.2.2) with
| intro NF hNF =>
cases hNF with
| intro yF hyF =>
cases call_run (vP F) 43 32 F g8.1 NF _ yF yG yG (And.intro hiG (fun _ _ => rfl)) hyF.2.1 hyF.2.2.1 with
| intro z hz =>
-- I: MOVE 19 -> 22
have z19 : z 19 = (unG (revApp (unG a []).1 []) []).1 :=
(hz.2.2 19 (lt32 19 rfl)).trans ((hG.2.2.2 19 rfl rfl).trans ((hL.2.2 19 rfl rfl rfl).trans
((hE.2.2.2 19 rfl rfl).trans ((hD.2.2.2 19 rfl rfl).trans hC.2.2.1))))
have z22 : z 22 = [] :=
(hz.2.2 22 (lt32 22 rfl)).trans ((hG.2.2.2 22 rfl rfl).trans ((hL.2.2 22 rfl rfl rfl).trans
((hE.2.2.2 22 rfl rfl).trans ((hD.2.2.2 22 rfl rfl).trans ((hC.2.2.2 22 rfl rfl).trans
((hB.2.2.2 22 rfl rfl).trans (hA.2.2.2 22 rfl rfl)))))))
have z16 : z 16 = [] :=
(hz.2.2 16 (lt32 16 rfl)).trans ((hG.2.2.2 16 rfl rfl).trans ((hL.2.2 16 rfl rfl rfl).trans
((hE.2.2.2 16 rfl rfl).trans ((hD.2.2.2 16 rfl rfl).trans ((hC.2.2.2 16 rfl rfl).trans
((hB.2.2.2 16 rfl rfl).trans (hA.2.2.2 16 rfl rfl)))))))
cases move_spec (vP F) (43 + F.length) 19 22 g9.1 rfl _ [] z z19 z22 with
| intro yI hI =>
-- J: EQ 22 48 16
have i48 : yI 48 = f (revApp (unG (unG (revApp (unG a []).1 []) []).2 []).1
(lc (revApp (unG a []).2 []) (unG (unG (revApp (unG a []).1 []) []).2 []).2 []).2.2.2) :=
(hI.2.2.2 48 rfl rfl).trans ((hz.2.1 16).trans hyF.2.2.2)
cases eq_run (vP F) (43 + F.length + 5) 22 48 16 (eqAt_of_frag _ _ _ _ _ g10.1) rfl rfl rfl _ _ yI hI.2.2.1 i48 with
| intro Ne hNe =>
cases hNe with
| intro yJ hJ =>
have sj := run1_jmp (vP F) (43 + F.length + 5 + 7) (43 + F.length + 5 + 7 + 2) (g11.1 0 _ rfl) yJ
have hcall : Runs (vP F) NF 43 yG (43 + F.length) z := hz.1
have s := runs_seq (vP F) _ 1 0 _ (43 + F.length + 5 + 7 + 2) (inpR a) yJ yJ
(runs_seq (vP F) _ Ne 0 _ (43 + F.length + 5 + 7) (inpR a) yI yJ
(runs_seq (vP F) _ _ 0 _ (43 + F.length + 5) (inpR a) z yI
(runs_seq (vP F) _ NF 0 43 (43 + F.length) (inpR a) yG z
(runs_seq (vP F) _ _ 0 38 43 (inpR a) yL yG
(runs_seq (vP F) _ _ 0 31 38 (inpR a) yE yL s5 hlr) hG.1) hcall) hI.1) hJ.2.1) sj
-- the length of the input of F
have mw : (revApp (unG (unG (revApp (unG a []).1 []) []).2 []).1
(lc (revApp (unG a []).2 []) (unG (unG (revApp (unG a []).1 []) []).2 []).2 []).2.2.2).length <= a.length := by
rw [revApp_len, hd, revApp_len, lt]
show (unG (unG (revApp (unG a []).1 []) []).2 []).1.length + ((unG a []).2.length + 0) <= a.length
refine Nat.le_trans (Nat.add_le_add_right (Nat.le_trans (le_l _ _ _ l3) (Nat.le_trans (le_r _ _ _ l2) (Nat.le_of_eq ly))) _) ?_
exact l1
rw [hlen.symm] at s
refine Exists.intro _ (Exists.intro _ (And.intro ?_ (And.intro s ?_)))
· refine acc _ _ 9 _ _ (acc _ _ 8 _ _ (acc _ _ 7 _ _ ?_ (bB _ _ m4)) ?_) (bE a.length)
· exact Nat.add_le_add (acc _ _ 6 _ 0 p5 (bB _ _ m5)) (Nat.le_trans hyF.1 (pe_mono _ _ mw eF))
· have hr : (revApp (unG (revApp (unG a []).1 []) []).1 []).length <= a.length := by
rw [revApp_len]; exact m4
exact Nat.le_trans hJ.1 (bD _ _ hr)
· rw [hJ.2.2.1, hI.2.2.2 16 rfl rfl, z16]
show beqL (revApp (unG (revApp (unG a []).1 []) []).1 []) _ = selB _ _ false
rw [hok]
rfl
end Found.Pnp74
#print axioms Found.Pnp74.fragHT
#print axioms Found.Pnp74.appI_len
#print axioms Found.Pnp74.mapI_len
#print axioms Found.Pnp74.seqI_len
#print axioms Found.Pnp74.vP_len
#print axioms Found.Pnp74.ar1
#print axioms Found.Pnp74.unenc_spec
#print axioms Found.Pnp74.move_spec
#print axioms Found.Pnp74.lchk_spec
#print axioms Found.Pnp74.lt32
#print axioms Found.Pnp74.hi_lt
#print axioms Found.Pnp74.inpR_nil
#print axioms Found.Pnp74.hi_init
#print axioms Found.Pnp74.hi_keep
#print axioms Found.Pnp74.hi_set
#print axioms Found.Pnp74.e45
#print axioms Found.Pnp74.q4
#print axioms Found.Pnp74.bA
#print axioms Found.Pnp74.three
#print axioms Found.Pnp74.four
#print axioms Found.Pnp74.bB
#print axioms Found.Pnp74.bC
#print axioms Found.Pnp74.bD
#print axioms Found.Pnp74.bE
#print axioms Found.Pnp74.acc
#print axioms Found.Pnp74.acc1
#print axioms Found.Pnp74.revApp_len
#print axioms Found.Pnp74.unG_len
#print axioms Found.Pnp74.lc_okD
#print axioms Found.Pnp74.le_l
#print axioms Found.Pnp74.le_r
#print axioms Found.Pnp74.v_run
#print axioms: 32 theorems, no axioms.
On well-formed inputs the verifier accepts exactly when \(|s|\le k\) and \(f(p\mathbin {+\!\!+}s)=z\), and it is in \(\mathrm {P}_{\mathrm {RM}}\).
found/Pnp75.lean
/-
T330 piece 75. found/Pnp75 -- what the verifier V_f decides, and that it is in P on the
register machine (toward search-to-decision on the register machine, open lemma O5rm).
On a well-formed input enc (enc z (enc p (tr k))) s (tr k: k ones, Pnp23), vF f (Pnp74)
accepts exactly when |s| <= k and f (p ++ s) = z. Proved:
* revApp_rr, revApp_nil2: reversing twice;
* vF_yes: if |s| <= k then vF f (enc (enc z (enc p (tr k))) s) = beqL z (f (p ++ s));
* vF_no: if k < |s| then it is false;
* v_inP: for every F computing f in polynomial time, vF f is in P_RM (Owf4.InPrm),
with Inside from Pnp72.runs_first.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp74
namespace Found.Pnp75
open Found.Pnp3 (PE pe)
open Found.Pnp20 (Ins revApp)
open Found.Pnp4 (enc)
open Found.Pnp23 (tr tr_len app_nil'')
open Found.Pnp72 (unG unG_enc runs_first)
open Found.Pnp73 (lc lc_le lc_gt)
open Found.Pnp74 (vP vF vE v_run selB revApp_len)
open Found.Owf1 (beqL)
open Found.Owf4 (CompRM DecRM InPrm)
theorem revApp_rr : forall (l m n : List Bool), revApp (revApp l m) n = revApp m (l ++ n)
| [], _, _ => rfl
| b :: l, m, n => revApp_rr l (b :: m) n
theorem revApp_nil2 (l : List Bool) : revApp (revApp l []) [] = l := by
rw [revApp_rr]
exact app_nil'' l
theorem unG1 (x w : List Bool) : (unG (enc x w) []).1 = revApp x [] := by rw [unG_enc]
theorem unG2 (x w : List Bool) : (unG (enc x w) []).2 = w := by rw [unG_enc]
theorem lenT (s : List Bool) (k : Nat) :
(revApp s []).length <= (tr k).length <-> s.length <= k := by
rw [revApp_len, tr_len]
exact Iff.intro (fun h => h) (fun h => h)
theorem vF_yes (f : List Bool -> List Bool) (z p s : List Bool) (k : Nat) (h : s.length <= k) :
vF f (enc (enc z (enc p (tr k))) s) = beqL z (f (p ++ s)) := by
have hl : (revApp s []).length <= (tr k).length := (lenT s k).2 h
have c := lc_le (revApp s []) (tr k) [] hl
unfold vF
rw [unG1, unG2, revApp_nil2, unG1, unG2, unG1, unG2, c.1, c.2, revApp_nil2, revApp_nil2, revApp_rr]
rfl
theorem vF_no (f : List Bool -> List Bool) (z p s : List Bool) (k : Nat) (h : k < s.length) :
vF f (enc (enc z (enc p (tr k))) s) = false := by
have hl : (tr k).length < (revApp s []).length := by
rw [revApp_len, tr_len]; exact h
have c := lc_gt (revApp s []) (tr k) [] hl
unfold vF
rw [unG1, unG2, revApp_nil2, unG1, unG2, unG1, unG2, c]
rfl
theorem v_inP (F : List Ins) (eF : PE) (f : List Bool -> List Bool) (hF : CompRM F eF f) : InPrm (vF f) := by
refine Exists.intro (vP F) (Exists.intro (vE eF) (fun a => ?_))
cases v_run F eF f hF a with
| intro N hN =>
cases hN with
| intro y hy =>
cases runs_first (vP F) N (Found.Pnp59.inpR a) y hy.2.1 with
| intro N' h' =>
exact Exists.intro N' (Exists.intro y (And.intro (Nat.le_trans h'.1 hy.1)
(And.intro h'.2.1 (And.intro h'.2.2 hy.2.2))))
end Found.Pnp75
#print axioms Found.Pnp75.revApp_rr
#print axioms Found.Pnp75.revApp_nil2
#print axioms Found.Pnp75.unG1
#print axioms Found.Pnp75.unG2
#print axioms Found.Pnp75.lenT
#print axioms Found.Pnp75.vF_yes
#print axioms Found.Pnp75.vF_no
#print axioms Found.Pnp75.v_inP
#print axioms: 8 theorems, no axioms.
The search language \(L_f\) runs through all certificates up to the input length; it is in \(\mathrm {NP}_{\mathrm {RM}}\) and answers whether a short extension of a prefix is a preimage.
found/Pnp76.lean
/-
T330 piece 76. found/Pnp76 -- the search language L_f is in NP on the register machine
(toward search-to-decision on the register machine, open lemma O5rm).
L_f t holds when some certificate w with |w| <= |t| makes the verifier vF f (Pnp74) accept
enc t w; it is written as a Bool function that runs through all bit lists up to |t|
(Pnp8 allBits). Proved:
* anyL_true, anyL_of: a Bool search through a list of lists;
* upTo_true, upTo_of: the search through all lists of length at most n;
* lf_inNP: for every F computing f in polynomial time, L_f is in NP_RM (Owf4.InNPrm),
with certificate bound |w| <= |t| and verifier vF f (Pnp75 v_inP);
* lf_yes, lf_sound: on enc z (enc p (tr k)), L_f holds exactly when some s with |s| <= k
gives f (p ++ s) = z (the certificate bound is met because |s| <= k <= |t|).
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp75
namespace Found.Pnp76
open Found.Pnp3 (PE pe)
open Found.Pnp4 (enc dbl)
open Found.Pnp8 (allBits InL allBits_complete allBits_len)
open Found.Pnp20 (Ins)
open Found.Pnp23 (tr tr_len)
open Found.Pnp74 (vF)
open Found.Pnp75 (vF_yes vF_no v_inP)
open Found.Owf1 (beqL)
open Found.Owf4 (CompRM InPrm InNPrm)
def anyL (g : List Bool -> Bool) : List (List Bool) -> Bool
| [] => false
| w :: ws => g w || anyL g ws
def upTo (g : List Bool -> Bool) : Nat -> Bool
| 0 => anyL g (allBits 0)
| n + 1 => upTo g n || anyL g (allBits (n + 1))
theorem or_true_cases (a b : Bool) (h : (a || b) = true) : a = true \/ b = true := by
cases a with
| true => exact Or.inl rfl
| false => exact Or.inr h
theorem or_true_l (a b : Bool) (h : a = true) : (a || b) = true := by rw [h]; rfl
theorem or_true_r (a b : Bool) (h : b = true) : (a || b) = true := by
rw [h]; cases a with
| true => rfl
| false => rfl
theorem anyL_true (g : List Bool -> Bool) : forall ws, anyL g ws = true ->
Exists fun w => InL w ws /\ g w = true
| [], h => Bool.noConfusion h
| w :: ws, h =>
Or.elim (or_true_cases _ _ h) (fun hw => Exists.intro w (And.intro (Or.inl rfl) hw))
(fun hr => match anyL_true g ws hr with
| Exists.intro v hv => Exists.intro v (And.intro (Or.inr hv.1) hv.2))
theorem anyL_of (g : List Bool -> Bool) (w : List Bool) (hw : g w = true) : forall ws, InL w ws -> anyL g ws = true
| [], h => False.elim h
| v :: ws, h =>
Or.elim h (fun e => or_true_l _ _ (by rw [e.symm]; exact hw))
(fun h' => or_true_r _ _ (anyL_of g w hw ws h'))
theorem upTo_true (g : List Bool -> Bool) : forall n, upTo g n = true ->
Exists fun w => w.length <= n /\ g w = true
| 0, h =>
match anyL_true g (allBits 0) h with
| Exists.intro w hw => Exists.intro w (And.intro (Nat.le_of_eq (allBits_len 0 w hw.1)) hw.2)
| n + 1, h =>
Or.elim (or_true_cases _ _ h)
(fun h1 => match upTo_true g n h1 with
| Exists.intro w hw => Exists.intro w (And.intro (Nat.le_succ_of_le hw.1) hw.2))
(fun h2 => match anyL_true g (allBits (n + 1)) h2 with
| Exists.intro w hw => Exists.intro w (And.intro (Nat.le_of_eq (allBits_len (n + 1) w hw.1)) hw.2))
theorem le_zero_eq : forall m : Nat, m <= 0 -> m = 0
| 0, _ => rfl
| _ + 1, h => absurd h (Nat.not_succ_le_zero _)
theorem le_succ_cases : forall (m n : Nat), m <= n + 1 -> m <= n \/ m = n + 1
| 0, _, _ => Or.inl (Nat.zero_le _)
| m + 1, 0, h => Or.inr (congrArg Nat.succ (le_zero_eq m (Nat.le_of_succ_le_succ h)))
| m + 1, n + 1, h =>
Or.elim (le_succ_cases m n (Nat.le_of_succ_le_succ h))
(fun h1 => Or.inl (Nat.succ_le_succ h1)) (fun h2 => Or.inr (congrArg Nat.succ h2))
theorem upTo_of (g : List Bool -> Bool) (w : List Bool) (hw : g w = true) :
forall n, w.length <= n -> upTo g n = true
| 0, h => by
have hc := allBits_complete w
rw [le_zero_eq _ h] at hc
exact anyL_of g w hw _ hc
| n + 1, h =>
Or.elim (le_succ_cases _ _ h)
(fun h1 => or_true_l _ _ (upTo_of g w hw n h1))
(fun h2 => or_true_r _ _ (by
have hc := allBits_complete w
rw [h2] at hc
exact anyL_of g w hw _ hc))
/-- The search language. -/
def Lf (f : List Bool -> List Bool) (t : List Bool) : Bool := upTo (fun w => vF f (enc t w)) t.length
theorem lf_inNP (F : List Ins) (eF : PE) (f : List Bool -> List Bool) (hF : CompRM F eF f) : InNPrm (Lf f) :=
Exists.intro (vF f) (Exists.intro PE.x (And.intro (v_inP F eF f hF) (fun t =>
Iff.intro (fun h => upTo_true _ t.length h)
(fun h => match h with
| Exists.intro w hw => upTo_of _ w hw.2 t.length hw.1))))
theorem app_len : forall (A B : List Bool), (A ++ B).length = A.length + B.length
| [], B => (Nat.zero_add B.length).symm
| _ :: A, B => by
show (A ++ B).length + 1 = A.length + 1 + B.length
rw [app_len A B, Nat.add_right_comm]
theorem len_enc_r (x w : List Bool) : w.length <= (enc x w).length := by
show w.length <= (dbl x ++ (true :: true :: w)).length
rw [app_len]
exact Nat.le_trans (Nat.le_add_right w.length 2) (Nat.le_add_left _ _)
theorem lt_or_le' : forall (k m : Nat), k < m \/ m <= k
| _, 0 => Or.inr (Nat.zero_le _)
| 0, _ + 1 => Or.inl (Nat.succ_le_succ (Nat.zero_le _))
| k + 1, m + 1 =>
Or.elim (lt_or_le' k m) (fun h => Or.inl (Nat.succ_le_succ h)) (fun h => Or.inr (Nat.succ_le_succ h))
/-- Completeness: a short extension s of p with f (p ++ s) = z puts the query in L_f. -/
theorem lf_yes (f : List Bool -> List Bool) (z p s : List Bool) (k : Nat) (hk : s.length <= k)
(he : beqL z (f (p ++ s)) = true) : Lf f (enc z (enc p (tr k))) = true := by
have hl : s.length <= (enc z (enc p (tr k))).length := by
refine Nat.le_trans hk (Nat.le_trans ?_ (len_enc_r z _))
refine Nat.le_trans (Nat.le_of_eq (tr_len k).symm) (len_enc_r p (tr k))
exact upTo_of _ s ((vF_yes f z p s k hk).trans he) _ hl
/-- Soundness: a query in L_f has such an extension. -/
theorem lf_sound (f : List Bool -> List Bool) (z p : List Bool) (k : Nat)
(h : Lf f (enc z (enc p (tr k))) = true) :
Exists fun s => s.length <= k /\ beqL z (f (p ++ s)) = true :=
match upTo_true _ _ h with
| Exists.intro w hw =>
Or.elim (lt_or_le' k w.length)
(fun hlt => by
have e := (vF_no f z p w k hlt).symm.trans hw.2
exact Bool.noConfusion e)
(fun hle => Exists.intro w (And.intro hle ((vF_yes f z p w k hle).symm.trans hw.2)))
end Found.Pnp76
#print axioms Found.Pnp76.or_true_cases
#print axioms Found.Pnp76.or_true_l
#print axioms Found.Pnp76.or_true_r
#print axioms Found.Pnp76.anyL_true
#print axioms Found.Pnp76.anyL_of
#print axioms Found.Pnp76.upTo_true
#print axioms Found.Pnp76.le_zero_eq
#print axioms Found.Pnp76.le_succ_cases
#print axioms Found.Pnp76.upTo_of
#print axioms Found.Pnp76.lf_inNP
#print axioms Found.Pnp76.app_len
#print axioms Found.Pnp76.len_enc_r
#print axioms Found.Pnp76.lt_or_le'
#print axioms Found.Pnp76.lf_yes
#print axioms Found.Pnp76.lf_sound
#print axioms: 15 theorems, no axioms.
A run keeps every register its program does not name, registers grow by at most one bit per step, and CLEAR fragments empty a block of registers.
found/Pnp77.lean
/-
T330 piece 77. found/Pnp77 -- registers a program does not name, register growth, and
the CLEAR fragments (toward search-to-decision on the register machine, open lemma O5rm).
To call a decider again and again in one block of registers, the block must be emptied
after every call. Proved:
* run_hi: a run of p keeps every register i >= sumR p (Pnp59: all named registers are
below sumR p);
* len_run: in n steps a register grows by at most n bits;
* clr_run: CLEAR r = [pop r (-> 1, else -> 0)] empties r in |r| + 1 steps;
* clrAll_run: clrAll b n empties registers b .. b + n - 1 within n (M + 1) steps when
each holds at most M bits, ending at o + n and keeping every register outside.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp74
namespace Found.Pnp77
open Found.Pnp20 (Ins Cfg upd upd_same upd_other nthI step exec popTo run selR)
open Found.Pnp21 (Frag Runs runs_seq)
open Found.Pnp59 (insReg sumR insReg_lt)
open Found.Pnp70 (run1_pop_nil run1_pop_cons bsel)
open Found.Pnp74 (seqI fragHT)
/-! ## registers not named -/
theorem step_hi (p : List Ins) (c : Cfg) (i : Nat) (hi : sumR p <= i) : (step p c).regs i = c.regs i := by
show (exec c (nthI p c.pc)).regs i = c.regs i
cases h : nthI p c.pc with
| none => rfl
| some ins =>
have hr := insReg_lt p c.pc ins h
cases ins with
| push r b =>
exact upd_other c.regs r i _ (Found.Pnp56.beq_gt i r (Nat.lt_of_lt_of_le hr hi))
| pop r e z o =>
have hne := Found.Pnp56.beq_gt i r (Nat.lt_of_lt_of_le hr hi)
show (popTo c r e z o (c.regs r)).regs i = c.regs i
cases c.regs r with
| nil => rfl
| cons b t =>
cases b with
| false => exact upd_other c.regs r i t hne
| true => exact upd_other c.regs r i t hne
| jmp l => rfl
theorem run_hi (p : List Ins) (i : Nat) (hi : sumR p <= i) : forall n (c : Cfg), (run p n c).regs i = c.regs i := by
intro n
induction n with
| zero => intro c; rfl
| succ n ih => intro c; exact (ih (step p c)).trans (step_hi p c i hi)
/-! ## growth -/
theorem len_upd_le (x : Nat -> List Bool) (r i : Nat) (v : List Bool) (h : v.length <= (x r).length + 1) :
(upd x r v i).length <= (x i).length + 1 := by
cases e : Nat.beq i r with
| true =>
have e' := Found.Pnp2.beq_eq i r e
rw [e', upd_same]
exact h
| false => rw [upd_other x r i v e]; exact Nat.le_succ _
theorem len_step (p : List Ins) (c : Cfg) (i : Nat) : ((step p c).regs i).length <= (c.regs i).length + 1 := by
show ((exec c (nthI p c.pc)).regs i).length <= _
cases nthI p c.pc with
| none => exact Nat.le_succ _
| some ins =>
cases ins with
| push r b => exact len_upd_le c.regs r i _ (Nat.le_refl _)
| pop r e z o =>
show ((popTo c r e z o (c.regs r)).regs i).length <= _
have hr : forall t : List Bool, c.regs r = t -> forall b : Bool, forall t', t = b :: t' ->
t'.length <= (c.regs r).length + 1 := fun t ht b t' e => by
rw [ht, e]; exact Nat.le_succ_of_le (Nat.le_succ _)
cases h : c.regs r with
| nil => exact Nat.le_succ _
| cons b t =>
cases b with
| false => exact len_upd_le c.regs r i t (hr _ h false t rfl)
| true => exact len_upd_le c.regs r i t (hr _ h true t rfl)
| jmp l => exact Nat.le_succ _
theorem len_run (p : List Ins) (i : Nat) : forall n (c : Cfg), ((run p n c).regs i).length <= (c.regs i).length + n := by
intro n
induction n with
| zero => intro c; exact Nat.le_refl _
| succ n ih =>
intro c
refine Nat.le_trans (ih (step p c)) ?_
rw [Nat.add_comm n 1, ← Nat.add_assoc]
exact Nat.add_le_add_right (len_step p c i) n
/-! ## CLEAR -/
def clr (r : Nat) : List Ins := [.pop r 1 0 0]
theorem clr_run (prog : List Ins) (o r : Nat) (hf : Frag prog o (clr r)) :
forall (l : List Bool) (x : Nat -> List Bool), x r = l -> Runs prog (l.length + 1) o x (o + 1) (upd x r [])
| [], x, h => by
have s := run1_pop_nil prog o r (o + 1) o o (hf 0 _ rfl) x h
refine And.intro s.1 (fun i => (s.2 i).trans ?_)
exact (Found.Pnp72.upd_id x r [] h i).symm
| b :: t, x, h => by
have s0 := run1_pop_cons prog o r (o + 1) o o (hf 0 _ rfl) x b t h
have e : bsel b o o = o := by cases b with | false => rfl | true => rfl
rw [e] at s0
have s1 := clr_run prog o r hf t (upd x r t) (upd_same x r t)
have s := runs_seq prog 1 (t.length + 1) o o (o + 1) x _ _ s0 s1
rw [Nat.add_comm 1 (t.length + 1)] at s
refine And.intro s.1 (fun i => (s.2 i).trans ?_)
exact Found.Pnp72.upd_over x r t [] i
def clrAll (b : Nat) : Nat -> List Ins
| 0 => []
| n + 1 => seqI (clr b) (clrAll (b + 1) n)
theorem idx (b j : Nat) : b + 1 + j = b + (j + 1) := by
rw [Nat.add_assoc, Nat.add_comm 1 j]
theorem clrAll_run (prog : List Ins) (M : Nat) : forall (n b o : Nat) (x : Nat -> List Bool),
Frag prog o (clrAll b n) -> (forall j, j < n -> (x (b + j)).length <= M) ->
Exists fun N => Exists fun y => N <= n * (M + 1) /\ Runs prog N o x (o + n) y /\
(forall j, j < n -> y (b + j) = []) /\ (forall i, (i < b \/ b + n <= i) -> y i = x i) := by
intro n
induction n with
| zero =>
intro b o x _ _
refine Exists.intro 0 (Exists.intro x (And.intro (Nat.zero_le _) (And.intro (And.intro rfl (fun _ => rfl))
(And.intro (fun j hj => absurd hj (Nat.not_lt_zero j)) (fun _ _ => rfl)))))
| succ n ih =>
intro b o x hf hM
have g := fragHT prog o (clr b) (clrAll (b + 1) n) hf
have s0 := clr_run prog o b g.1 (x b) x rfl
have hM' : forall j, j < n -> (upd x b [] (b + 1 + j)).length <= M := by
intro j hj
have hne : Nat.beq (b + 1 + j) b = false :=
Found.Pnp56.beq_gt _ b (Nat.lt_of_lt_of_le (Nat.lt_succ_self b) (Nat.le_add_right _ j))
rw [upd_other x b _ [] hne, idx]
exact hM (j + 1) (Nat.succ_lt_succ hj)
cases ih (b + 1) (o + 1) (upd x b []) g.2 hM' with
| intro N hN =>
cases hN with
| intro y hy =>
have s := runs_seq prog _ N o (o + 1) (o + 1 + n) x _ y s0 hy.2.1
have eo : o + 1 + n = o + (n + 1) := idx o n
rw [eo] at s
refine Exists.intro _ (Exists.intro y (And.intro ?_ (And.intro s (And.intro ?_ ?_))))
· rw [Nat.succ_mul, Nat.add_comm (n * (M + 1)) (M + 1)]
exact Nat.add_le_add (Nat.succ_le_succ (hM 0 (Nat.zero_lt_succ n))) hy.1
· intro j hj
cases j with
| zero =>
refine (hy.2.2.2 b (Or.inl (Nat.lt_succ_self b))).trans ?_
exact upd_same x b []
| succ j =>
rw [(idx b j).symm]
exact hy.2.2.1 j (Nat.lt_of_succ_lt_succ hj)
· intro i hi
have hne : Nat.beq i b = false := by
cases hi with
| inl h => exact Found.Pnp56.beq_lt i b h
| inr h => exact Found.Pnp56.beq_gt i b (Nat.lt_of_lt_of_le (Nat.lt_of_lt_of_le (Nat.lt_succ_self b)
(Nat.le_add_right (b + 1) n)) (Nat.le_trans (Nat.le_of_eq (idx b n)) h))
have hi' : i < b + 1 \/ b + 1 + n <= i := by
cases hi with
| inl h => exact Or.inl (Nat.lt_succ_of_lt h)
| inr h => exact Or.inr (Nat.le_trans (Nat.le_of_eq (idx b n)) h)
rw [hy.2.2.2 i hi']
exact upd_other x b i [] hne
end Found.Pnp77
#print axioms Found.Pnp77.step_hi
#print axioms Found.Pnp77.run_hi
#print axioms Found.Pnp77.len_upd_le
#print axioms Found.Pnp77.len_step
#print axioms Found.Pnp77.len_run
#print axioms Found.Pnp77.clr_run
#print axioms Found.Pnp77.idx
#print axioms Found.Pnp77.clrAll_run
#print axioms: 8 theorems, no axioms.
COPY and DCOPY copy a register onto another while keeping it; DCOPY of a reversed \(p\) puts \(\mathrm {dbl}\,p\) in front.
found/Pnp78.lean
/-
T332 piece (T330 file series) 78. found/Pnp78 -- the COPY and DCOPY fragments: copying a
register onto another while keeping it (toward search-to-decision on the register
machine, open lemma O5rm).
COPY A Q T pops A, pushing every bit on Q and on T (T is moved back to A afterwards by
MOVE). DCOPY A Q T does the same but pushes b and then false on Q, so that copying the
reverse of p puts dbl p (Pnp4) in front of Q. Layouts (offsets from o):
COPY: 0: pop A (empty -> 7, 0 -> 1, 1 -> 4) 1: push Q false 2: push T false 3: jmp 0
4: push Q true 5: push T true 6: jmp 0 end 7
DCOPY: 0: pop A (empty -> 9, 0 -> 1, 1 -> 5) 1: push Q false 2: push Q false
3: push T false 4: jmp 0 5: push Q true 6: push Q false 7: push T true
8: jmp 0 end 9
Proved:
* copy_run: 4 |a| + 1 steps; A = [], Q = revApp a q, T = revApp a t, others kept;
* dcopy_run: 5 |a| + 1 steps; A = [], Q = dblRev a q, T = revApp a t, others kept;
* dblRev_rev: dblRev (revApp p []) q = dbl p ++ q.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp77
namespace Found.Pnp78
open Found.Pnp20 (Ins Cfg upd upd_same upd_other nthI revApp)
open Found.Pnp21 (Frag Runs runs_seq)
open Found.Pnp70 (run1_push run1_jmp run1_pop_nil run1_pop_cons nsym bsel)
open Found.Pnp4 (dbl)
def copyP (A Q T : Nat) : List Ins :=
[.pop A 7 1 4, .push Q false, .push T false, .jmp 0, .push Q true, .push T true, .jmp 0]
def dcopyP (A Q T : Nat) : List Ins :=
[.pop A 9 1 5, .push Q false, .push Q false, .push T false, .jmp 0,
.push Q true, .push Q false, .push T true, .jmp 0]
def dblRev : List Bool -> List Bool -> List Bool
| [], q => q
| b :: a, q => dblRev a (false :: b :: q)
theorem dblRev_rev : forall (p q : List Bool), dblRev (revApp p []) q = dbl p ++ q := by
have gen : forall (p m q : List Bool), dblRev (revApp p m) q = dblRev m (dbl p ++ q) := by
intro p
induction p with
| nil => intro m q; rfl
| cons b p ih => intro m q; exact ih (b :: m) q
intro p q
exact gen p [] q
/-- Three registers set: A, Q, T. -/
def set3 (x : Nat -> List Bool) (A Q T : Nat) (a q t : List Bool) : Nat -> List Bool :=
upd (upd (upd x A a) Q q) T t
theorem set3_read (x : Nat -> List Bool) (A Q T : Nat) (a q t : List Bool)
(hAQ : Nat.beq A Q = false) (hAT : Nat.beq A T = false) (hQT : Nat.beq Q T = false) :
set3 x A Q T a q t A = a /\ set3 x A Q T a q t Q = q /\ set3 x A Q T a q t T = t /\
(forall i, Nat.beq i A = false -> Nat.beq i Q = false -> Nat.beq i T = false -> set3 x A Q T a q t i = x i) := by
refine And.intro ?_ (And.intro ?_ (And.intro (upd_same _ T t) ?_))
· show upd (upd (upd x A a) Q q) T t A = a
rw [upd_other _ T A t hAT, upd_other _ Q A q hAQ, upd_same]
· show upd (upd (upd x A a) Q q) T t Q = q
rw [upd_other _ T Q t hQT, upd_same]
· intro i h1 h2 h3
show upd (upd (upd x A a) Q q) T t i = x i
rw [upd_other _ T i t h3, upd_other _ Q i q h2, upd_other _ A i a h1]
theorem four_step (n : Nat) : 4 + (4 * n + 1) = 4 * (n + 1) + 1 := by
rw [Nat.left_distrib, Nat.mul_one, Nat.add_comm 4 (4 * n + 1), Nat.add_right_comm]
theorem five_step (n : Nat) : 5 + (5 * n + 1) = 5 * (n + 1) + 1 := by
rw [Nat.left_distrib, Nat.mul_one, Nat.add_comm 5 (5 * n + 1), Nat.add_right_comm]
theorem copy_run (prog : List Ins) (o A Q T : Nat) (hf : Frag prog o (copyP A Q T))
(hAQ : Nat.beq A Q = false) (hAT : Nat.beq A T = false) (hQT : Nat.beq Q T = false) :
forall (a q t : List Bool) (x : Nat -> List Bool), x A = a -> x Q = q -> x T = t ->
Exists fun y => Runs prog (4 * a.length + 1) o x (o + 7) y /\ y A = [] /\ y Q = revApp a q /\
y T = revApp a t /\
(forall i, Nat.beq i A = false -> Nat.beq i Q = false -> Nat.beq i T = false -> y i = x i)
| [], q, t, x, ha, hq, ht => by
have s := run1_pop_nil prog o A (o + 7) (o + 1) (o + 4) (hf 0 _ rfl) x ha
exact Exists.intro x (And.intro s (And.intro ha (And.intro hq (And.intro ht (fun _ _ _ _ => rfl)))))
| b :: a, q, t, x, ha, hq, ht => by
have s0 := run1_pop_cons prog o A (o + 7) (o + 1) (o + 4) (hf 0 _ rfl) x b a ha
have hq1 : upd x A a Q = q := by rw [upd_other x A Q a (nsym A Q hAQ)]; exact hq
have hstep : Runs prog 4 o x o (set3 x A Q T a (b :: q) (b :: t)) := by
have ht2 : upd (upd x A a) Q (b :: q) T = t := by
rw [upd_other _ Q T _ (nsym Q T hQT), upd_other x A T a (nsym A T hAT)]; exact ht
cases b with
| false =>
have s1 := run1_push prog (o + 1) Q false (hf 1 _ rfl) (upd x A a)
rw [hq1] at s1
have s2 := run1_push prog (o + 2) T false (hf 2 _ rfl) (upd (upd x A a) Q (false :: q))
rw [ht2] at s2
have s := runs_seq prog 3 1 o (o + 3) o x _ _
(runs_seq prog 2 1 o (o + 2) (o + 3) x _ _ (runs_seq prog 1 1 o (o + 1) (o + 2) x _ _ s0 s1) s2)
(run1_jmp prog (o + 3) o (hf 3 _ rfl) _)
exact s
| true =>
have s1 := run1_push prog (o + 4) Q true (hf 4 _ rfl) (upd x A a)
rw [hq1] at s1
have s2 := run1_push prog (o + 5) T true (hf 5 _ rfl) (upd (upd x A a) Q (true :: q))
rw [ht2] at s2
have s := runs_seq prog 3 1 o (o + 6) o x _ _
(runs_seq prog 2 1 o (o + 5) (o + 6) x _ _ (runs_seq prog 1 1 o (o + 4) (o + 5) x _ _ s0 s1) s2)
(run1_jmp prog (o + 6) o (hf 6 _ rfl) _)
exact s
have rd := set3_read x A Q T a (b :: q) (b :: t) hAQ hAT hQT
cases copy_run prog o A Q T hf hAQ hAT hQT a (b :: q) (b :: t) _ rd.1 rd.2.1 rd.2.2.1 with
| intro y hy =>
have s := runs_seq prog 4 (4 * a.length + 1) o o (o + 7) x _ y hstep hy.1
rw [four_step] at s
exact Exists.intro y (And.intro s (And.intro hy.2.1 (And.intro hy.2.2.1 (And.intro hy.2.2.2.1
(fun i h1 h2 h3 => (hy.2.2.2.2 i h1 h2 h3).trans (rd.2.2.2 i h1 h2 h3))))))
theorem dcopy_run (prog : List Ins) (o A Q T : Nat) (hf : Frag prog o (dcopyP A Q T))
(hAQ : Nat.beq A Q = false) (hAT : Nat.beq A T = false) (hQT : Nat.beq Q T = false) :
forall (a q t : List Bool) (x : Nat -> List Bool), x A = a -> x Q = q -> x T = t ->
Exists fun y => Runs prog (5 * a.length + 1) o x (o + 9) y /\ y A = [] /\ y Q = dblRev a q /\
y T = revApp a t /\
(forall i, Nat.beq i A = false -> Nat.beq i Q = false -> Nat.beq i T = false -> y i = x i)
| [], q, t, x, ha, hq, ht => by
have s := run1_pop_nil prog o A (o + 9) (o + 1) (o + 5) (hf 0 _ rfl) x ha
exact Exists.intro x (And.intro s (And.intro ha (And.intro hq (And.intro ht (fun _ _ _ _ => rfl)))))
| b :: a, q, t, x, ha, hq, ht => by
have s0 := run1_pop_cons prog o A (o + 9) (o + 1) (o + 5) (hf 0 _ rfl) x b a ha
have hq1 : upd x A a Q = q := by rw [upd_other x A Q a (nsym A Q hAQ)]; exact hq
have hstep : Runs prog 5 o x o (set3 x A Q T a (false :: b :: q) (b :: t)) := by
have ht3 : forall v : List Bool, upd (upd x A a) Q v T = t := fun v => by
rw [upd_other _ Q T _ (nsym Q T hQT), upd_other x A T a (nsym A T hAT)]; exact ht
cases b with
| false =>
have s1 := run1_push prog (o + 1) Q false (hf 1 _ rfl) (upd x A a)
rw [hq1] at s1
have s2 := run1_push prog (o + 2) Q false (hf 2 _ rfl) (upd (upd x A a) Q (false :: q))
rw [upd_same] at s2
have s3 := run1_push prog (o + 3) T false (hf 3 _ rfl) (upd (upd (upd x A a) Q (false :: q)) Q (false :: false :: q))
have e3 : upd (upd (upd x A a) Q (false :: q)) Q (false :: false :: q) T = t := by
rw [upd_other _ Q T _ (nsym Q T hQT)]; exact ht3 _
rw [e3] at s3
have s := runs_seq prog 4 1 o (o + 4) o x _ _
(runs_seq prog 3 1 o (o + 3) (o + 4) x _ _
(runs_seq prog 2 1 o (o + 2) (o + 3) x _ _ (runs_seq prog 1 1 o (o + 1) (o + 2) x _ _ s0 s1) s2) s3)
(run1_jmp prog (o + 4) o (hf 4 _ rfl) _)
refine And.intro s.1 (fun i => (s.2 i).trans ?_)
show _ = upd (upd (upd x A a) Q (false :: false :: q)) T (false :: t) i
exact Found.Pnp20.upd_eqv _ _ (Found.Pnp72.upd_over (upd x A a) Q _ _) T _ i
| true =>
have s1 := run1_push prog (o + 5) Q true (hf 5 _ rfl) (upd x A a)
rw [hq1] at s1
have s2 := run1_push prog (o + 6) Q false (hf 6 _ rfl) (upd (upd x A a) Q (true :: q))
rw [upd_same] at s2
have s3 := run1_push prog (o + 7) T true (hf 7 _ rfl) (upd (upd (upd x A a) Q (true :: q)) Q (false :: true :: q))
have e3 : upd (upd (upd x A a) Q (true :: q)) Q (false :: true :: q) T = t := by
rw [upd_other _ Q T _ (nsym Q T hQT)]; exact ht3 _
rw [e3] at s3
have s := runs_seq prog 4 1 o (o + 8) o x _ _
(runs_seq prog 3 1 o (o + 7) (o + 8) x _ _
(runs_seq prog 2 1 o (o + 6) (o + 7) x _ _ (runs_seq prog 1 1 o (o + 5) (o + 6) x _ _ s0 s1) s2) s3)
(run1_jmp prog (o + 8) o (hf 8 _ rfl) _)
refine And.intro s.1 (fun i => (s.2 i).trans ?_)
show _ = upd (upd (upd x A a) Q (false :: true :: q)) T (true :: t) i
exact Found.Pnp20.upd_eqv _ _ (Found.Pnp72.upd_over (upd x A a) Q _ _) T _ i
have rd := set3_read x A Q T a (false :: b :: q) (b :: t) hAQ hAT hQT
cases dcopy_run prog o A Q T hf hAQ hAT hQT a (false :: b :: q) (b :: t) _ rd.1 rd.2.1 rd.2.2.1 with
| intro y hy =>
have s := runs_seq prog 5 (5 * a.length + 1) o o (o + 9) x _ y hstep hy.1
rw [five_step] at s
exact Exists.intro y (And.intro s (And.intro hy.2.1 (And.intro hy.2.2.1 (And.intro hy.2.2.2.1
(fun i h1 h2 h3 => (hy.2.2.2.2 i h1 h2 h3).trans (rd.2.2.2 i h1 h2 h3))))))
end Found.Pnp78
#print axioms Found.Pnp78.dblRev_rev
#print axioms Found.Pnp78.set3_read
#print axioms Found.Pnp78.four_step
#print axioms Found.Pnp78.five_step
#print axioms Found.Pnp78.copy_run
#print axioms Found.Pnp78.dcopy_run
#print axioms: 6 theorems, no axioms.
The query \(\mathrm {enc}\,z\,(\mathrm {enc}\,p\,1^k)\) is built in the input register of the decider block, and the source registers are given back.
found/Pnp79.lean
/-
T332 piece (T330 file series) 79. found/Pnp79 -- building a query for the decider of L_f
(toward search-to-decision on the register machine, open lemma O5rm).
Inverter registers: 17 = k in unary (tr k), 18 = p reversed, 19 = z reversed, 21 = scratch,
45 = the input register of the decider block (13 + 32). bldP builds the query
enc z (enc p (tr k)) (Pnp76 lf_yes, lf_sound) in register 45 and gives 17, 18, 19 back:
COPY 17 45 21; MOVE 21 17; push 45 true (twice); DCOPY 18 45 21; MOVE 21 18;
push 45 true (twice); DCOPY 19 45 21; MOVE 21 19.
Proved:
* bld_run: from o with 17 = tr k, 18 = revApp p [], 19 = revApp z [], 21 = [], 45 = [],
bldP ends at o + 44 within 8 (4 (2 S) + 4) steps (S = k + |p| + |z|) with
45 = enc z (enc p (tr k)), 17, 18, 19, 21 as they were, other registers kept.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp78
import found.Pnp75
namespace Found.Pnp79
open Found.Pnp20 (Ins Cfg upd upd_same upd_other nthI revApp move)
open Found.Pnp21 (Frag Runs runs_seq)
open Found.Pnp23 (tr tr_len revApp_tr app_nil'')
open Found.Pnp4 (dbl enc)
open Found.Pnp70 (run1_push)
open Found.Pnp74 (seqI fragHT move_spec Bn bB bC acc acc1 four revApp_len)
open Found.Pnp75 (revApp_nil2)
open Found.Pnp78 (copyP dcopyP dblRev dblRev_rev copy_run dcopy_run)
def two45 : List Ins := [.push 45 true, .push 45 true]
def bldP : List Ins :=
seqI (copyP 17 45 21) (seqI (move 21 17) (seqI two45 (seqI (dcopyP 18 45 21) (seqI (move 21 18)
(seqI two45 (seqI (dcopyP 19 45 21) (move 21 19)))))))
theorem two_run (prog : List Ins) (o : Nat) (hf : Frag prog o two45) (x : Nat -> List Bool) (q : List Bool)
(hq : x 45 = q) : Exists fun y => Runs prog 2 o x (o + 2) y /\ y 45 = true :: true :: q /\
(forall i, Nat.beq i 45 = false -> y i = x i) := by
have s1 := run1_push prog o 45 true (hf 0 _ rfl) x
have s2 := run1_push prog (o + 1) 45 true (hf 1 _ rfl) (upd x 45 (true :: x 45))
have s := runs_seq prog 1 1 o (o + 1) (o + 2) x _ _ s1 s2
refine Exists.intro _ (And.intro s (And.intro ?_ ?_))
· rw [upd_same, upd_same, hq]
· intro i hi
rw [upd_other _ 45 i _ hi, upd_other _ 45 i _ hi]
theorem five_le (m : Nat) : 5 * m <= 4 * (m + m) := by
rw [Nat.succ_mul 4 m, Nat.left_distrib]
exact Nat.add_le_add_left (by rw [four]; exact Nat.le_add_left m _) _
theorem bF (m S : Nat) (h : m <= S) : 5 * m + 1 <= Bn (S + S) :=
Nat.le_trans (Nat.add_le_add_right (five_le m) 1) (bC (m + m) (S + S) (Nat.add_le_add h h))
theorem b2 (n : Nat) : 2 <= Bn n := Nat.le_trans (Nat.le_add_left 2 2) (Nat.le_add_left 4 _)
theorem leSS (m S : Nat) (h : m <= S) : m <= S + S := Nat.le_trans h (Nat.le_add_right S S)
theorem bld_run (prog : List Ins) (o : Nat) (hf : Frag prog o bldP) (k : Nat) (p z : List Bool)
(x : Nat -> List Bool) (h17 : x 17 = tr k) (h18 : x 18 = revApp p []) (h19 : x 19 = revApp z [])
(h21 : x 21 = []) (h45 : x 45 = []) :
Exists fun N => Exists fun y => N <= 8 * Bn ((k + p.length + z.length) + (k + p.length + z.length)) + 0 /\
Runs prog N o x (o + 44) y /\ y 45 = enc z (enc p (tr k)) /\ y 17 = tr k /\ y 18 = revApp p [] /\
y 19 = revApp z [] /\ y 21 = [] /\
(forall i, Nat.beq i 17 = false -> Nat.beq i 18 = false -> Nat.beq i 19 = false ->
Nat.beq i 21 = false -> Nat.beq i 45 = false -> y i = x i) := by
have g1 := fragHT prog o (copyP 17 45 21) _ hf
have g2 := fragHT prog (o + 7) (move 21 17) _ g1.2
have g3 := fragHT prog (o + 12) two45 _ g2.2
have g4 := fragHT prog (o + 14) (dcopyP 18 45 21) _ g3.2
have g5 := fragHT prog (o + 23) (move 21 18) _ g4.2
have g6 := fragHT prog (o + 28) two45 _ g5.2
have g7 := fragHT prog (o + 30) (dcopyP 19 45 21) (move 21 19) g6.2
-- COPY 17 45 21
cases copy_run prog o 17 45 21 g1.1 rfl rfl rfl (tr k) [] [] x h17 h45 h21 with
| intro y1 h1 =>
-- MOVE 21 17
cases move_spec prog (o + 7) 21 17 g2.1 rfl _ [] y1 h1.2.2.2.1 h1.2.1 with
| intro y2 h2 =>
-- push twice
cases two_run prog (o + 12) g3.1 y2 _ ((h2.2.2.2 45 rfl rfl).trans h1.2.2.1) with
| intro y3 h3 =>
-- DCOPY 18 45 21
have r18 : y3 18 = revApp p [] :=
(h3.2.2 18 rfl).trans ((h2.2.2.2 18 rfl rfl).trans ((h1.2.2.2.2 18 rfl rfl rfl).trans h18))
have r21 : y3 21 = [] := (h3.2.2 21 rfl).trans h2.2.1
cases dcopy_run prog (o + 14) 18 45 21 g4.1 rfl rfl rfl _ _ [] y3 r18 h3.2.1 r21 with
| intro y4 h4 =>
-- MOVE 21 18
cases move_spec prog (o + 23) 21 18 g5.1 rfl _ [] y4 h4.2.2.2.1 h4.2.1 with
| intro y5 h5 =>
cases two_run prog (o + 28) g6.1 y5 _ ((h5.2.2.2 45 rfl rfl).trans h4.2.2.1) with
| intro y6 h6 =>
have r19 : y6 19 = revApp z [] :=
(h6.2.2 19 rfl).trans ((h5.2.2.2 19 rfl rfl).trans ((h4.2.2.2.2 19 rfl rfl rfl).trans ((h3.2.2 19 rfl).trans
((h2.2.2.2 19 rfl rfl).trans ((h1.2.2.2.2 19 rfl rfl rfl).trans h19)))))
have r21' : y6 21 = [] := (h6.2.2 21 rfl).trans h5.2.1
cases dcopy_run prog (o + 30) 19 45 21 g7.1 rfl rfl rfl _ _ [] y6 r19 h6.2.1 r21' with
| intro y7 h7 =>
cases move_spec prog (o + 39) 21 19 g7.2 rfl _ [] y7 h7.2.2.2.1 h7.2.1 with
| intro y8 h8 =>
have s := runs_seq prog _ _ o (o + 39) (o + 44) x y7 y8
(runs_seq prog _ _ o (o + 30) (o + 39) x y6 y7
(runs_seq prog _ _ o (o + 28) (o + 30) x y5 y6
(runs_seq prog _ _ o (o + 23) (o + 28) x y4 y5
(runs_seq prog _ _ o (o + 14) (o + 23) x y3 y4
(runs_seq prog _ _ o (o + 12) (o + 14) x y2 y3
(runs_seq prog _ _ o (o + 7) (o + 12) x y1 y2 h1.1 h2.1) h3.1) h4.1) h5.1) h6.1) h7.1) h8.1
have mk : k <= k + p.length + z.length := Nat.le_trans (Nat.le_add_right k p.length) (Nat.le_add_right _ _)
have mp : p.length <= k + p.length + z.length := Nat.le_trans (Nat.le_add_left p.length k) (Nat.le_add_right _ _)
have mz : z.length <= k + p.length + z.length := Nat.le_add_left _ _
have lk : (revApp (tr k) []).length = k := by rw [revApp_len, tr_len]; rfl
have lp : (revApp (revApp p []) []).length = p.length := by rw [revApp_nil2]
have lz : (revApp (revApp z []) []).length = z.length := by rw [revApp_nil2]
have lpr : (revApp p []).length = p.length := by rw [revApp_len]; rfl
have lzr : (revApp z []).length = z.length := by rw [revApp_len]; rfl
refine Exists.intro _ (Exists.intro y8 (And.intro ?_ (And.intro s ?_)))
· refine acc _ _ 7 _ 0 (acc _ _ 6 _ 0 (acc _ _ 5 _ 0 (acc _ _ 4 _ 0 (acc _ _ 3 _ 0 (acc _ _ 2 _ 0 (acc _ _ 1 _ 0
(acc1 _ _ ?_) ?_) (b2 _)) ?_) ?_) (b2 _)) ?_) ?_
· rw [tr_len]; exact Nat.le_trans (bC k _ (leSS _ _ mk)) (Nat.le_refl _)
· rw [lk]; exact bB k _ (leSS _ _ mk)
· rw [lpr]; exact bF _ _ mp
· rw [lp]; exact bB _ _ (leSS _ _ mp)
· rw [lzr]; exact bF _ _ mz
· rw [lz]; exact bB _ _ (leSS _ _ mz)
· have e45 : y8 45 = enc z (enc p (tr k)) := by
show y8 45 = dbl z ++ true :: true :: (dbl p ++ true :: true :: tr k)
rw [h8.2.2.2 45 rfl rfl, h7.2.2.1, dblRev_rev, dblRev_rev, revApp_tr, app_nil'']
refine And.intro e45 (And.intro ?_ (And.intro ?_ (And.intro ?_ (And.intro h8.2.1 ?_))))
· rw [h8.2.2.2 17 rfl rfl, h7.2.2.2.2 17 rfl rfl rfl, h6.2.2 17 rfl, h5.2.2.2 17 rfl rfl,
h4.2.2.2.2 17 rfl rfl rfl, h3.2.2 17 rfl, h2.2.2.1, revApp_nil2]
· rw [h8.2.2.2 18 rfl rfl, h7.2.2.2.2 18 rfl rfl rfl, h6.2.2 18 rfl, h5.2.2.1, revApp_nil2]
· rw [h8.2.2.1, revApp_nil2]
· intro i a b c d e
rw [h8.2.2.2 i d c, h7.2.2.2.2 i c e d, h6.2.2 i e, h5.2.2.2 i d b, h4.2.2.2.2 i b e d, h3.2.2 i e,
h2.2.2.2 i d a, h1.2.2.2.2 i a e d]
end Found.Pnp79
#print axioms Found.Pnp79.two_run
#print axioms Found.Pnp79.five_le
#print axioms Found.Pnp79.bF
#print axioms Found.Pnp79.b2
#print axioms Found.Pnp79.leSS
#print axioms Found.Pnp79.bld_run
#print axioms: 6 theorems, no axioms.
One decider call: build the query, run the relocated decider, read its first output bit, and empty its block again.
found/Pnp80.lean
/-
T332 piece (T330 file series) 80. found/Pnp80 -- calling a decider on a built query and
emptying its block again (toward search-to-decision on the register machine, open lemma
O5rm).
callP D: bldP (Pnp79) builds q = enc z (enc p (tr k)) in register 45; D relocated by 32
(Pnp69) decides q; RD pushes the first bit of D's output (register 48) on register 22;
clrAll 32 (sumR D + 17) (Pnp77) empties the block of D. Proved:
* rd_run: RD = [pop 48 (empty -> 3, 0 -> 3, 1 -> 1), push 22 true, jmp 4, push 22 false];
* calld_run: if D decides L within pe eD steps (Owf4.DecRM) and every register >= 32 is
empty, then callP D ends with 22 = L q :: 22, registers 17, 18, 19, 21 as they were,
every register >= 32 empty again and every other register below 32 kept, within
8 Bn (2 S) + pe eD |q| + 3 + n (|q| + pe eD |q| + 1) steps (n = sumR D + 17).
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp79
import found.Pnp76
namespace Found.Pnp80
open Found.Pnp3 (PE pe)
open Found.Pnp20 (Ins Cfg upd upd_same upd_other nthI revApp run selR)
open Found.Pnp21 (Frag Runs runs_seq)
open Found.Pnp23 (tr)
open Found.Pnp4 (enc)
open Found.Pnp59 (inpR inpR_13 inpR_other sumR)
open Found.Pnp67 (relocP)
open Found.Pnp69 (relocP_len call_run)
open Found.Pnp70 (run1_push run1_jmp run1_pop_nil run1_pop_cons bsel)
open Found.Pnp74 (seqI fragHT Bn hi_lt e45 lt32)
open Found.Pnp77 (run_hi len_run clrAll clrAll_run)
open Found.Pnp79 (bldP bld_run)
open Found.Owf4 (DecRM outBit)
def rdP : List Ins := [.pop 48 3 3 1, .push 22 true, .jmp 4, .push 22 false]
def tl : List Bool -> List Bool
| [] => []
| _ :: t => t
theorem rd_run (prog : List Ins) (o : Nat) (hf : Frag prog o rdP) (x : Nat -> List Bool) :
Exists fun N => Exists fun y => N <= 3 /\ Runs prog N o x (o + 4) y /\ y 22 = outBit (x 48) :: x 22 /\
y 48 = tl (x 48) /\ (forall i, Nat.beq i 22 = false -> Nat.beq i 48 = false -> y i = x i) := by
cases h : x 48 with
| nil =>
have s0 := run1_pop_nil prog o 48 (o + 3) (o + 3) (o + 1) (hf 0 _ rfl) x h
have s1 := run1_push prog (o + 3) 22 false (hf 3 _ rfl) x
refine Exists.intro 2 (Exists.intro _ (And.intro (Nat.le_succ 2) (And.intro
(runs_seq prog 1 1 o (o + 3) (o + 4) x x _ s0 s1) (And.intro (upd_same _ _ _) (And.intro ?_ ?_)))))
· rw [upd_other x 22 48 _ rfl, h]; rfl
· intro i hi _; exact upd_other x 22 i _ hi
| cons b t =>
cases b with
| false =>
have s0 := run1_pop_cons prog o 48 (o + 3) (o + 3) (o + 1) (hf 0 _ rfl) x false t h
have s1 := run1_push prog (o + 3) 22 false (hf 3 _ rfl) (upd x 48 t)
refine Exists.intro 2 (Exists.intro _ (And.intro (Nat.le_succ 2) (And.intro
(runs_seq prog 1 1 o (o + 3) (o + 4) x _ _ s0 s1) (And.intro ?_ (And.intro ?_ ?_)))))
· rw [upd_same, upd_other x 48 22 t rfl]; rfl
· rw [upd_other _ 22 48 _ rfl, upd_same]; rfl
· intro i h1 h2; rw [upd_other _ 22 i _ h1, upd_other x 48 i t h2]
| true =>
have s0 := run1_pop_cons prog o 48 (o + 3) (o + 3) (o + 1) (hf 0 _ rfl) x true t h
have s1 := run1_push prog (o + 1) 22 true (hf 1 _ rfl) (upd x 48 t)
have s2 := run1_jmp prog (o + 2) (o + 4) (hf 2 _ rfl) (upd (upd x 48 t) 22 (true :: upd x 48 t 22))
refine Exists.intro 3 (Exists.intro _ (And.intro (Nat.le_refl 3) (And.intro
(runs_seq prog 2 1 o (o + 2) (o + 4) x _ _ (runs_seq prog 1 1 o (o + 1) (o + 2) x _ _ s0 s1) s2)
(And.intro ?_ (And.intro ?_ ?_)))))
· rw [upd_same, upd_other x 48 22 t rfl]; rfl
· rw [upd_other _ 22 48 _ rfl, upd_same]; rfl
· intro i h1 h2; rw [upd_other _ 22 i _ h1, upd_other x 48 i t h2]
theorem tl_len (l : List Bool) : (tl l).length <= l.length := by
cases l with
| nil => exact Nat.le_refl 0
| cons b t => exact Nat.le_succ _
def nB (D : List Ins) : Nat := sumR D + 17
def callP (D : List Ins) : List Ins := seqI bldP (seqI (relocP 32 D) (seqI rdP (clrAll 32 (nB D))))
theorem inpR_len (q : List Bool) (j : Nat) : (inpR q j).length <= q.length := by
show (selR (Nat.beq j 13) q []).length <= q.length
cases Nat.beq j 13 with
| true => exact Nat.le_refl _
| false => exact Nat.zero_le _
theorem lt_of_ble' (a b : Nat) (h : Nat.ble (a + 1) b = true) : a < b := Nat.le_of_ble_eq_true h
theorem calld_run (prog : List Ins) (o : Nat) (D : List Ins) (eD : PE) (L : List Bool -> Bool)
(hD : DecRM D eD L) (hf : Frag prog o (callP D)) (k : Nat) (p z : List Bool) (x : Nat -> List Bool)
(h17 : x 17 = tr k) (h18 : x 18 = revApp p []) (h19 : x 19 = revApp z []) (h21 : x 21 = [])
(hblk : forall i, x (i + 32) = []) :
Exists fun N => Exists fun y =>
N <= 8 * Bn ((k + p.length + z.length) + (k + p.length + z.length)) + 0 +
pe eD (enc z (enc p (tr k))).length + 3 +
nB D * ((enc z (enc p (tr k))).length + pe eD (enc z (enc p (tr k))).length + 1) /\
Runs prog N o x (o + 44 + D.length + 4 + nB D) y /\
y 22 = L (enc z (enc p (tr k))) :: x 22 /\ y 17 = tr k /\ y 18 = revApp p [] /\ y 19 = revApp z [] /\
y 21 = [] /\ (forall i, y (i + 32) = []) /\
(forall i, i < 32 -> Nat.beq i 17 = false -> Nat.beq i 18 = false -> Nat.beq i 19 = false ->
Nat.beq i 21 = false -> Nat.beq i 22 = false -> y i = x i) := by
have g1 := fragHT prog o bldP _ hf
have g2 := fragHT prog (o + 44) (relocP 32 D) _ g1.2
have g2' : Frag prog (o + 44 + D.length) (seqI rdP (clrAll 32 (nB D))) := by
have e := g2.2; rw [relocP_len] at e; exact e
have g3 := fragHT prog (o + 44 + D.length) rdP _ g2'
cases bld_run prog o g1.1 k p z x h17 h18 h19 h21 (hblk 13) with
| intro Nb hb =>
cases hb with
| intro y1 h1 =>
-- the block holds the query only
have hi1 : forall i, y1 (i + 32) = inpR (enc z (enc p (tr k))) i := by
intro i
cases e : Nat.beq i 13 with
| true =>
rw [Found.Pnp2.beq_eq i 13 e, inpR_13]
exact h1.2.2.1
| false =>
rw [inpR_other _ i e]
rw [h1.2.2.2.2.2.2.2 (i + 32) (hi_lt 17 i (lt32 17 rfl)) (hi_lt 18 i (lt32 18 rfl))
(hi_lt 19 i (lt32 19 rfl)) (hi_lt 21 i (lt32 21 rfl)) (e45 i e)]
exact hblk i
cases hD (enc z (enc p (tr k))) with
| intro ND hND =>
cases hND with
| intro yD hyD =>
cases call_run prog (o + 44) 32 D g2.1 ND _ yD y1 y1 (And.intro hi1 (fun _ _ => rfl)) hyD.2.1 hyD.2.2.1 with
| intro zz hz =>
cases rd_run prog (o + 44 + D.length) g3.1 zz with
| intro Nr hr =>
cases hr with
| intro w hw =>
-- the block after the call: bounded, and empty beyond n
have hzD : forall j, zz (j + 32) = (run D ND (Cfg.mk 0 (inpR (enc z (enc p (tr k)))))).regs j :=
fun j => (hz.2.1 j).trans (hyD.2.1.2 j).symm
have hM : forall j, j < nB D -> (w (32 + j)).length <=
(enc z (enc p (tr k))).length + pe eD (enc z (enc p (tr k))).length := by
intro j _
have b0 : (zz (32 + j)).length <= (enc z (enc p (tr k))).length + pe eD (enc z (enc p (tr k))).length := by
rw [Nat.add_comm 32 j, hzD j]
exact Nat.le_trans (len_run D j ND _) (Nat.add_le_add (inpR_len _ j) hyD.1)
cases e48 : Nat.beq (32 + j) 48 with
| true =>
rw [Found.Pnp2.beq_eq _ 48 e48, hw.2.2.2.1]
rw [Found.Pnp2.beq_eq _ 48 e48] at b0
exact Nat.le_trans (tl_len _) b0
| false =>
rw [hw.2.2.2.2 (32 + j) (Found.Pnp56.beq_gt _ 22 (Nat.lt_of_lt_of_le (lt32 22 rfl) (Nat.le_add_right 32 j))) e48]
exact b0
cases clrAll_run prog ((enc z (enc p (tr k))).length + pe eD (enc z (enc p (tr k))).length)
(nB D) 32 (o + 44 + D.length + 4) w g3.2 hM with
| intro Nc hc =>
cases hc with
| intro y hy =>
have s := runs_seq prog _ Nc o (o + 44 + D.length + 4) (o + 44 + D.length + 4 + nB D) x w y
(runs_seq prog _ Nr o (o + 44 + D.length) (o + 44 + D.length + 4) x zz w
(runs_seq prog Nb ND o (o + 44) (o + 44 + D.length) x y1 zz h1.2.1 hz.1) hw.2.1) hy.2.1
-- registers below 32
have low : forall i, i < 32 -> Nat.beq i 22 = false -> y i = zz i := by
intro i hi h22
rw [hy.2.2.2 i (Or.inl hi)]
exact hw.2.2.2.2 i h22 (Found.Pnp56.beq_lt i 48 (Nat.lt_trans hi (lt_of_ble' 32 48 rfl)))
have low1 : forall i, i < 32 -> Nat.beq i 22 = false -> y i = y1 i :=
fun i hi h22 => (low i hi h22).trans (hz.2.2 i hi)
refine Exists.intro _ (Exists.intro y (And.intro ?_ (And.intro s ?_)))
· exact Nat.add_le_add (Nat.add_le_add (Nat.add_le_add h1.1 hyD.1) hw.1) hy.1
· refine And.intro ?_ (And.intro ?_ (And.intro ?_ (And.intro ?_ (And.intro ?_ (And.intro ?_ ?_)))))
· rw [hy.2.2.2 22 (Or.inl (lt32 22 rfl)), hw.2.2.1, hz.2.2 22 (lt32 22 rfl), hz.2.1 16, hyD.2.2.2,
h1.2.2.2.2.2.2.2 22 rfl rfl rfl rfl rfl]
· exact (low1 17 (lt32 17 rfl) rfl).trans h1.2.2.2.1
· exact (low1 18 (lt32 18 rfl) rfl).trans h1.2.2.2.2.1
· exact (low1 19 (lt32 19 rfl) rfl).trans h1.2.2.2.2.2.1
· exact (low1 21 (lt32 21 rfl) rfl).trans h1.2.2.2.2.2.2.1
· intro i
rw [Nat.add_comm i 32]
cases Found.Pnp76.lt_or_le' i (nB D) with
| inl h => exact hy.2.2.1 i h
| inr h =>
have h17 : 17 <= i := Nat.le_trans (Nat.le_add_left 17 (sumR D)) h
rw [hy.2.2.2 (32 + i) (Or.inr (Nat.add_le_add_left h 32))]
have n22 : Nat.beq (32 + i) 22 = false :=
Found.Pnp56.beq_gt _ 22 (Nat.lt_of_lt_of_le (lt32 22 rfl) (Nat.le_add_right 32 i))
have n48 : Nat.beq (32 + i) 48 = false := by
rw [Nat.add_comm 32 i]
exact (Found.Pnp56.beq_add_r i 16 32).trans (Found.Pnp56.beq_gt i 16 (Nat.lt_of_lt_of_le (Nat.lt_succ_self 16) h17))
rw [hw.2.2.2.2 (32 + i) n22 n48, Nat.add_comm 32 i, hzD i,
run_hi D i (Nat.le_trans (Nat.le_add_right (sumR D) 17) h) ND _]
exact inpR_other _ i (Found.Pnp56.beq_gt i 13 (Nat.lt_of_lt_of_le (lt_of_ble' 13 17 rfl) h17))
· intro i hi a b c d e
rw [low1 i hi e]
exact h1.2.2.2.2.2.2.2 i a b c d (Found.Pnp56.beq_lt i 45 (Nat.lt_trans hi (lt_of_ble' 32 45 rfl)))
end Found.Pnp80
#print axioms Found.Pnp80.rd_run
#print axioms Found.Pnp80.tl_len
#print axioms Found.Pnp80.inpR_len
#print axioms Found.Pnp80.lt_of_ble'
#print axioms Found.Pnp80.calld_run
#print axioms: 5 theorems, no axioms.
Fragments inside a loop are placed with shifted copies and glue instructions with loop-relative targets; one call has a bound monotone in the sizes.
found/Pnp81.lean
/-
T332 piece (T330 file series) 81. found/Pnp81 -- placing fragments inside a loop, and a
monotone bound for one decider call (toward search-to-decision on the register machine,
open lemma O5rm).
A loop jumps back to its own start, so its glue instructions carry targets relative to the
loop start, while the fragments inside it are shifted to their places (mapI (shift off)).
FragAbs prog o base R: the instructions of R stand at base + i, shifted by o. Proved:
* fa_frag, fa_app_l, fa_app_r, fa_map, fa_ins: reading fragments and glue instructions
of appI chains;
* callP_len: |callP D| = 44 + |D| + 4 + nB D;
* dbl_len, q_len_le: |enc z (enc p (tr k))| <= Bn (S + S) when k + |p| + |z| <= S;
* calld_bnd: the steps of one decider call (Pnp80) are at most callB D eD S.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp80
namespace Found.Pnp81
open Found.Pnp3 (PE pe pe_mono)
open Found.Pnp20 (Ins nthI revApp move)
open Found.Pnp21 (Frag shift)
open Found.Pnp23 (appI mapI nthI_app_r nthI_app_l nthI_mapI shift_shift tr tr_len)
open Found.Pnp4 (dbl enc)
open Found.Pnp67 (relocP)
open Found.Pnp69 (relocP_len)
open Found.Pnp74 (seqI seqI_len Bn)
open Found.Pnp77 (clrAll)
open Found.Pnp79 (bldP)
open Found.Pnp80 (callP rdP nB)
def FragAbs (prog : List Ins) (o base : Nat) (R : List Ins) : Prop :=
forall i ins, nthI R i = some ins -> nthI prog (base + i) = some (shift o ins)
theorem fa_frag (prog : List Ins) (o : Nat) (R : List Ins) (h : Frag prog o R) : FragAbs prog o o R := h
theorem fa_app_l (prog : List Ins) (o base : Nat) (A B : List Ins) (h : FragAbs prog o base (appI A B)) :
FragAbs prog o base A :=
fun i ins hi => h i ins (nthI_app_l A B i ins hi)
theorem fa_app_r (prog : List Ins) (o base : Nat) (A B : List Ins) (h : FragAbs prog o base (appI A B)) :
FragAbs prog o (base + A.length) B := by
intro i ins hi
have e := h (A.length + i) ins ((nthI_app_r A B i).trans hi)
rw [← Nat.add_assoc] at e
exact e
theorem fa_map (prog : List Ins) (o base off : Nat) (P : List Ins) (h : FragAbs prog o base (mapI (shift off) P))
(hb : base = o + off) : Frag prog (o + off) P := by
intro i ins hi
have e := h i (shift off ins) (nthI_mapI (shift off) P i ins hi)
rw [shift_shift, hb] at e
exact e
theorem fa_ins (prog : List Ins) (o base : Nat) (R : List Ins) (h : FragAbs prog o base R) (i : Nat) (ins : Ins)
(hi : nthI R i = some ins) : nthI prog (base + i) = some (shift o ins) := h i ins hi
theorem callP_len (D : List Ins) : (callP D).length = 44 + D.length + 4 + nB D := by
show (seqI bldP (seqI (relocP 32 D) (seqI rdP (clrAll 32 (nB D))))).length = _
rw [seqI_len, seqI_len, seqI_len, relocP_len]
have cl : forall n b, (clrAll b n).length = n := by
intro n
induction n with
| zero => intro b; rfl
| succ n ih =>
intro b
show (seqI (Found.Pnp77.clr b) (clrAll (b + 1) n)).length = n + 1
rw [seqI_len, ih]
exact Nat.add_comm 1 n
rw [cl]
show 44 + (D.length + (4 + nB D)) = 44 + D.length + 4 + nB D
rw [Nat.add_assoc 44 D.length 4, Nat.add_assoc 44 (D.length + 4) (nB D), Nat.add_assoc D.length 4 (nB D)]
theorem dbl_len : forall x : List Bool, (dbl x).length = x.length + x.length
| [] => rfl
| _ :: x => by
show (dbl x).length + 1 + 1 = x.length + 1 + (x.length + 1)
rw [dbl_len x]
rw [Nat.add_assoc x.length 1 (x.length + 1), Nat.add_comm 1 (x.length + 1)]
rfl
theorem enc_len (x w : List Bool) : (enc x w).length = (x.length + x.length) + (w.length + 2) := by
show (dbl x ++ (true :: true :: w)).length = _
rw [Found.Pnp76.app_len, dbl_len]
rfl
theorem q_len_le (k : Nat) (p z : List Bool) (S : Nat) (hS : k + p.length + z.length <= S) :
(enc z (enc p (tr k))).length <= Bn (S + S) := by
have hk : k <= S := Nat.le_trans (Nat.le_trans (Nat.le_add_right k p.length) (Nat.le_add_right _ _)) hS
have hp : p.length <= S := Nat.le_trans (Nat.le_trans (Nat.le_add_left p.length k) (Nat.le_add_right _ _)) hS
have hz : z.length <= S := Nat.le_trans (Nat.le_add_left _ _) hS
rw [enc_len, enc_len, tr_len]
have b : (z.length + z.length) + ((p.length + p.length) + (k + 2) + 2) <= (S + S) + ((S + S) + (S + 2) + 2) :=
Nat.add_le_add (Nat.add_le_add hz hz)
(Nat.add_le_add_right (Nat.add_le_add (Nat.add_le_add hp hp) (Nat.add_le_add_right hk 2)) 2)
refine Nat.le_trans b ?_
show (S + S) + ((S + S) + S) + 4 <= (S + S) + (S + S) + (S + S) + (S + S) + 4
refine Nat.add_le_add_right ?_ 4
rw [← Nat.add_assoc]
exact Nat.le_trans (Nat.add_le_add_left (Nat.le_add_right S S) _) (Nat.le_add_right _ _)
/-- A bound for one call, monotone in S. -/
def callB (D : List Ins) (eD : PE) (S : Nat) : Nat :=
8 * Bn (S + S) + 0 + pe eD (Bn (S + S)) + 3 + nB D * (Bn (S + S) + pe eD (Bn (S + S)) + 1)
theorem Bn_mono (m n : Nat) (h : m <= n) : Bn m <= Bn n :=
Nat.add_le_add_right (Nat.add_le_add (Nat.add_le_add (Nat.add_le_add h h) h) h) 4
theorem calld_bnd (D : List Ins) (eD : PE) (k : Nat) (p z : List Bool) (S : Nat) (hS : k + p.length + z.length <= S)
(N : Nat) (hN : N <= 8 * Bn ((k + p.length + z.length) + (k + p.length + z.length)) + 0 +
pe eD (enc z (enc p (tr k))).length + 3 +
nB D * ((enc z (enc p (tr k))).length + pe eD (enc z (enc p (tr k))).length + 1)) :
N <= callB D eD S := by
have hq := q_len_le k p z S hS
have hpe := pe_mono _ _ hq eD
refine Nat.le_trans hN ?_
refine Nat.add_le_add (Nat.add_le_add_right (Nat.add_le_add (Nat.add_le_add_right
(Nat.mul_le_mul_left 8 (Bn_mono _ _ (Nat.add_le_add hS hS))) 0) hpe) 3) ?_
exact Nat.mul_le_mul_left _ (Nat.add_le_add_right (Nat.add_le_add hq hpe) 1)
theorem callB_mono (D : List Ins) (eD : PE) (m n : Nat) (h : m <= n) : callB D eD m <= callB D eD n := by
have h2 : Bn (m + m) <= Bn (n + n) := Bn_mono _ _ (Nat.add_le_add h h)
have hpe := pe_mono _ _ h2 eD
exact Nat.add_le_add (Nat.add_le_add_right (Nat.add_le_add (Nat.add_le_add_right
(Nat.mul_le_mul_left 8 h2) 0) hpe) 3) (Nat.mul_le_mul_left _ (Nat.add_le_add_right (Nat.add_le_add h2 hpe) 1))
end Found.Pnp81
#print axioms Found.Pnp81.fa_frag
#print axioms Found.Pnp81.fa_app_l
#print axioms Found.Pnp81.fa_app_r
#print axioms Found.Pnp81.fa_map
#print axioms Found.Pnp81.fa_ins
#print axioms Found.Pnp81.callP_len
#print axioms Found.Pnp81.dbl_len
#print axioms Found.Pnp81.enc_len
#print axioms Found.Pnp81.q_len_le
#print axioms Found.Pnp81.Bn_mono
#print axioms Found.Pnp81.calld_bnd
#print axioms Found.Pnp81.callB_mono
#print axioms: 12 theorems, no axioms.
The first loop asks for \(k=0,1,\dots \) and leaves at a \(k^\ast \le n\) with a yes, whenever the query at \(n\) is a yes.
found/Pnp82.lean
/-
T332 piece (T330 file series) 82. found/Pnp82 -- the first search loop: the least length
bound (toward search-to-decision on the register machine, open lemma O5rm).
loop1P D = callP D followed by the glue
C: pop 22 (empty -> C + 3, 0 -> C + 1, 1 -> C + 3) C + 1: push 17 true C + 2: jmp 0
(C = |callP D|; targets relative to the loop start). With p = [] in register 18 it asks the
decider about q k = enc z (enc [] (tr k)) for k, k + 1, ... and leaves at the first k with a
yes. Proved:
* loop1_run: if D decides L, L (q n) holds and the loop starts at k with k + d = n, then
it leaves at o + (C + 3) with register 17 = tr k*, k <= k* <= n, L (q k*), the block
>= 32 empty, registers 18, 19, 21, 22 as at the start and the other registers below 32
kept, within (d + 1) (callB D eD S + 3) steps whenever n + |z| <= S.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp81
namespace Found.Pnp82
open Found.Pnp3 (PE pe)
open Found.Pnp20 (Ins upd upd_same upd_other nthI revApp)
open Found.Pnp21 (Frag Runs runs_seq)
open Found.Pnp23 (appI tr)
open Found.Pnp4 (enc)
open Found.Pnp70 (run1_push run1_jmp run1_pop_cons)
open Found.Pnp74 (lt32)
open Found.Pnp80 (callP nB calld_run)
open Found.Pnp81 (FragAbs fa_frag fa_app_l fa_app_r fa_ins callP_len callB calld_bnd)
open Found.Owf4 (DecRM)
def glue1 (C : Nat) : List Ins := [.pop 22 (C + 3) (C + 1) (C + 3), .push 17 true, .jmp 0]
def loop1P (D : List Ins) : List Ins := appI (callP D) (glue1 (callP D).length)
/-- The loop state at k. -/
structure St1 (x : Nat -> List Bool) (k : Nat) (z : List Bool) : Prop where
r17 : x 17 = tr k
r18 : x 18 = revApp [] []
r19 : x 19 = revApp z []
r21 : x 21 = []
r22 : x 22 = []
blk : forall i, x (i + 32) = []
theorem exit_eq (o : Nat) (D : List Ins) : o + 44 + D.length + 4 + nB D = o + (callP D).length := by
rw [callP_len, ← Nat.add_assoc o (44 + D.length + 4) (nB D), ← Nat.add_assoc o (44 + D.length) 4,
← Nat.add_assoc o 44 D.length]
theorem le_mulS (d X : Nat) : X <= (d + 1) * X := by
rw [Nat.succ_mul]; exact Nat.le_add_left X _
theorem loop1_run (prog : List Ins) (o : Nat) (D : List Ins) (eD : PE) (L : List Bool -> Bool)
(hD : DecRM D eD L) (hf : Frag prog o (loop1P D)) (z : List Bool) (n S : Nat) (hS : n + z.length <= S)
(hn : L (enc z (enc [] (tr n))) = true) :
forall d k (x : Nat -> List Bool), k + d = n -> St1 x k z ->
Exists fun N => Exists fun y => Exists fun ks =>
k <= ks /\ ks <= n /\ L (enc z (enc [] (tr ks))) = true /\ St1 y ks z /\
N <= (d + 1) * (callB D eD S + 3) /\ Runs prog N o x (o + ((callP D).length + 3)) y /\
(forall i, i < 32 -> Nat.beq i 17 = false -> Nat.beq i 18 = false -> Nat.beq i 19 = false ->
Nat.beq i 21 = false -> Nat.beq i 22 = false -> y i = x i) := by
have fa := fa_frag prog o _ hf
have fc : Frag prog o (callP D) := fa_app_l prog o o _ _ fa
have fg := fa_app_r prog o o _ _ fa
have i0 : nthI prog (o + (callP D).length) =
some (.pop 22 (o + ((callP D).length + 3)) (o + ((callP D).length + 1)) (o + ((callP D).length + 3))) :=
fa_ins prog o _ _ fg 0 _ rfl
have i1 : nthI prog (o + (callP D).length + 1) = some (.push 17 true) := fa_ins prog o _ _ fg 1 _ rfl
have i2 : nthI prog (o + (callP D).length + 2) = some (.jmp o) := fa_ins prog o _ _ fg 2 _ rfl
intro d
induction d with
| zero =>
intro k x hk st
have ek : k = n := hk
cases calld_run prog o D eD L hD fc k [] z x st.r17 st.r18 st.r19 st.r21 st.blk with
| intro Nc hc =>
cases hc with
| intro y1 h1 =>
have hb := calld_bnd D eD k [] z S (by rw [ek]; exact hS) Nc h1.1
have r22 : y1 22 = true :: [] := by rw [h1.2.2.1, st.r22, ek, hn]
have s1 := run1_pop_cons prog (o + (callP D).length) 22 _ _ _ i0 y1 true [] r22
have s := runs_seq prog Nc 1 o (o + (callP D).length) _ x y1 _ (by rw [(exit_eq o D).symm]; exact h1.2.1) s1
refine Exists.intro _ (Exists.intro _ (Exists.intro k (And.intro (Nat.le_refl k) (And.intro (Nat.le_of_eq ek)
(And.intro (by rw [ek]; exact hn) (And.intro ?_ (And.intro ?_ (And.intro s ?_))))))))
· exact St1.mk ((upd_other y1 22 17 [] rfl).trans h1.2.2.2.1) ((upd_other y1 22 18 [] rfl).trans h1.2.2.2.2.1)
((upd_other y1 22 19 [] rfl).trans h1.2.2.2.2.2.1) ((upd_other y1 22 21 [] rfl).trans h1.2.2.2.2.2.2.1)
(upd_same y1 22 []) (fun i => (upd_other y1 22 (i + 32) [] (Found.Pnp74.hi_lt 22 i (lt32 22 rfl))).trans
(h1.2.2.2.2.2.2.2.1 i))
· exact Nat.le_trans (Nat.add_le_add hb (Nat.le_add_left 1 2)) (le_mulS 0 _)
· intro i hi a b c e g
rw [upd_other y1 22 i [] g]
exact h1.2.2.2.2.2.2.2.2 i hi a b c e g
| succ d ih =>
intro k x hk st
have hkS : k + ([] : List Bool).length + z.length <= S :=
Nat.le_trans (Nat.add_le_add_right (Nat.le_trans (Nat.le_add_right k (d + 1)) (Nat.le_of_eq hk)) _) hS
cases calld_run prog o D eD L hD fc k [] z x st.r17 st.r18 st.r19 st.r21 st.blk with
| intro Nc hc =>
cases hc with
| intro y1 h1 =>
have hb := calld_bnd D eD k [] z S hkS Nc h1.1
have sc : Runs prog Nc o x (o + (callP D).length) y1 := by rw [(exit_eq o D).symm]; exact h1.2.1
cases hq : L (enc z (enc [] (tr k))) with
| true =>
have r22 : y1 22 = true :: [] := by rw [h1.2.2.1, st.r22, hq]
have s1 := run1_pop_cons prog (o + (callP D).length) 22 _ _ _ i0 y1 true [] r22
have s := runs_seq prog Nc 1 o (o + (callP D).length) _ x y1 _ sc s1
refine Exists.intro _ (Exists.intro _ (Exists.intro k (And.intro (Nat.le_refl k)
(And.intro (Nat.le_trans (Nat.le_add_right k (d + 1)) (Nat.le_of_eq hk))
(And.intro hq (And.intro ?_ (And.intro ?_ (And.intro s ?_))))))))
· exact St1.mk ((upd_other y1 22 17 [] rfl).trans h1.2.2.2.1) ((upd_other y1 22 18 [] rfl).trans h1.2.2.2.2.1)
((upd_other y1 22 19 [] rfl).trans h1.2.2.2.2.2.1) ((upd_other y1 22 21 [] rfl).trans h1.2.2.2.2.2.2.1)
(upd_same y1 22 []) (fun i => (upd_other y1 22 (i + 32) [] (Found.Pnp74.hi_lt 22 i (lt32 22 rfl))).trans
(h1.2.2.2.2.2.2.2.1 i))
· exact Nat.le_trans (Nat.add_le_add hb (Nat.le_add_left 1 2)) (le_mulS (d + 1) _)
· intro i hi a b c e g
rw [upd_other y1 22 i [] g]
exact h1.2.2.2.2.2.2.2.2 i hi a b c e g
| false =>
have r22 : y1 22 = false :: [] := by rw [h1.2.2.1, st.r22, hq]
have s1 := run1_pop_cons prog (o + (callP D).length) 22 _ _ _ i0 y1 false [] r22
have s2 := run1_push prog (o + (callP D).length + 1) 17 true i1 (upd y1 22 [])
have s3 := run1_jmp prog (o + (callP D).length + 2) o i2 (upd (upd y1 22 []) 17 (true :: upd y1 22 [] 17))
have s := runs_seq prog (Nc + 1 + 1) 1 o _ o x _ _
(runs_seq prog (Nc + 1) 1 o (o + (callP D).length + 1) _ x _ _
(runs_seq prog Nc 1 o (o + (callP D).length) _ x y1 _ sc s1) s2) s3
have st2 : St1 (upd (upd y1 22 []) 17 (true :: upd y1 22 [] 17)) (k + 1) z := by
refine St1.mk ?_ ?_ ?_ ?_ ?_ ?_
· rw [upd_same, upd_other y1 22 17 [] rfl, h1.2.2.2.1]; rfl
· rw [upd_other _ 17 18 _ rfl, upd_other y1 22 18 [] rfl]; exact h1.2.2.2.2.1
· rw [upd_other _ 17 19 _ rfl, upd_other y1 22 19 [] rfl]; exact h1.2.2.2.2.2.1
· rw [upd_other _ 17 21 _ rfl, upd_other y1 22 21 [] rfl]; exact h1.2.2.2.2.2.2.1
· rw [upd_other _ 17 22 _ rfl, upd_same]
· intro i
rw [upd_other _ 17 (i + 32) _ (Found.Pnp74.hi_lt 17 i (lt32 17 rfl)),
upd_other y1 22 (i + 32) [] (Found.Pnp74.hi_lt 22 i (lt32 22 rfl))]
exact h1.2.2.2.2.2.2.2.1 i
have hk' : k + 1 + d = n := by rw [Nat.add_right_comm, Nat.add_assoc]; exact hk
cases ih (k + 1) _ hk' st2 with
| intro N hN =>
cases hN with
| intro y hy =>
cases hy with
| intro ks hks =>
have s' := runs_seq prog _ N o o _ x _ y s hks.2.2.2.2.2.1
refine Exists.intro _ (Exists.intro y (Exists.intro ks (And.intro (Nat.le_of_succ_le hks.1)
(And.intro hks.2.1 (And.intro hks.2.2.1 (And.intro hks.2.2.2.1 (And.intro ?_ (And.intro s' ?_))))))))
· rw [Nat.succ_mul (d + 1), Nat.add_comm ((d + 1) * (callB D eD S + 3)) (callB D eD S + 3)]
exact Nat.add_le_add (Nat.add_le_add_right hb 3) hks.2.2.2.2.1
· intro i hi a b c e g
rw [hks.2.2.2.2.2.2 i hi a b c e g, upd_other _ 17 i _ a, upd_other y1 22 i [] g]
exact h1.2.2.2.2.2.2.2.2 i hi a b c e g
end Found.Pnp82
#print axioms Found.Pnp82.exit_eq
#print axioms Found.Pnp82.le_mulS
#print axioms Found.Pnp82.loop1_run
#print axioms: 3 theorems, no axioms.
The layout of the second loop and its first half, which asks about \((p,0)\) with the length bound saved.
found/Pnp83.lean
/-
T332 piece (T330 file series) 83. found/Pnp83 -- the second search loop: its layout and
its first half (toward search-to-decision on the register machine, open lemma O5rm).
With C = |callP D|, loop2P D is (offsets from the loop start):
0: MOVE 17 23 (5) 5: callP D C + 5: MOVE 23 17 (5)
C + 10: pop 22 (empty -> E, 0 -> C + 11, 1 -> E) C + 11: pop 17 (empty -> E, else C + 12)
C + 12: push 18 false C + 13: callP D
C + C + 13: pop 22 (empty -> 0, 0 -> C + C + 14, 1 -> 0) C + C + 14: pop 18 (-> C + C + 15)
C + C + 15: push 18 true C + C + 16: jmp 0 exit E = C + C + 17.
Register 17 holds k, 18 holds p reversed, 23 is a save register for k. The first half asks
the decider about (p, 0) with k saved in 23. Proved:
* lp_frags: where the fragments and glue instructions of loop2P stand;
* revApp_snoc: revApp (p ++ [b]) [] = b :: revApp p [];
* head_run: from the loop state St2 at (k, p), the first half ends at o + (C + 10) with
22 = [L (q p 0)], 17 = tr k again, the rest of the state kept, within
(3 k + 1) + callB S + (3 k + 1) steps (|p| + |z| <= S).
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp82
namespace Found.Pnp83
open Found.Pnp3 (PE pe)
open Found.Pnp20 (Ins upd upd_same upd_other nthI revApp move)
open Found.Pnp21 (Frag Runs runs_seq shift)
open Found.Pnp23 (appI mapI tr tr_len revApp_tr app_nil'')
open Found.Pnp4 (enc)
open Found.Pnp74 (move_spec lt32 hi_lt mapI_len revApp_len)
open Found.Pnp75 (revApp_nil2)
open Found.Pnp80 (callP nB calld_run)
open Found.Pnp81 (FragAbs fa_frag fa_app_l fa_app_r fa_map fa_ins callB calld_bnd)
open Found.Pnp82 (exit_eq)
open Found.Owf4 (DecRM)
def glueA (C : Nat) : List Ins :=
[.pop 22 (C + C + 17) (C + 11) (C + C + 17), .pop 17 (C + C + 17) (C + 12) (C + 12), .push 18 false]
def glueB (C : Nat) : List Ins :=
[.pop 22 0 (C + C + 14) 0, .pop 18 (C + C + 15) (C + C + 15) (C + C + 15), .push 18 true, .jmp 0]
def loop2P (D : List Ins) : List Ins :=
appI (move 17 23) (appI (mapI (shift 5) (callP D)) (appI (mapI (shift ((callP D).length + 5)) (move 23 17))
(appI (glueA (callP D).length) (appI (mapI (shift ((callP D).length + 13)) (callP D)) (glueB (callP D).length)))))
/-- Where everything stands, C = |callP D|. -/
structure LpAt (prog : List Ins) (o : Nat) (D : List Ins) : Prop where
m1 : Frag prog o (move 17 23)
c1 : Frag prog (o + 5) (callP D)
m2 : Frag prog (o + ((callP D).length + 5)) (move 23 17)
a0 : nthI prog (o + ((callP D).length + 10)) =
some (.pop 22 (o + ((callP D).length + (callP D).length + 17)) (o + ((callP D).length + 11))
(o + ((callP D).length + (callP D).length + 17)))
a1 : nthI prog (o + ((callP D).length + 11)) =
some (.pop 17 (o + ((callP D).length + (callP D).length + 17)) (o + ((callP D).length + 12))
(o + ((callP D).length + 12)))
a2 : nthI prog (o + ((callP D).length + 12)) = some (.push 18 false)
c2 : Frag prog (o + ((callP D).length + 13)) (callP D)
b0 : nthI prog (o + ((callP D).length + (callP D).length + 13)) =
some (.pop 22 (o + 0) (o + ((callP D).length + (callP D).length + 14)) (o + 0))
b1 : nthI prog (o + ((callP D).length + (callP D).length + 14)) =
some (.pop 18 (o + ((callP D).length + (callP D).length + 15)) (o + ((callP D).length + (callP D).length + 15))
(o + ((callP D).length + (callP D).length + 15)))
b2 : nthI prog (o + ((callP D).length + (callP D).length + 15)) = some (.push 18 true)
b3 : nthI prog (o + ((callP D).length + (callP D).length + 16)) = some (.jmp (o + 0))
theorem fa_base (prog : List Ins) (o b b' : Nat) (R : List Ins) (h : FragAbs prog o b R) (e : b = b') :
FragAbs prog o b' R := by
rw [e.symm]; exact h
theorem lp_frags (prog : List Ins) (o : Nat) (D : List Ins) (hf : Frag prog o (loop2P D)) : LpAt prog o D := by
have fa := fa_frag prog o _ hf
have r1 := fa_app_r prog o o _ _ fa
have r2 := fa_app_r prog o _ _ _ r1
have r2' := fa_base prog o _ (o + ((callP D).length + 5)) _ r2 (by
show o + 5 + (mapI (shift 5) (callP D)).length = o + ((callP D).length + 5)
rw [mapI_len, Nat.add_assoc, Nat.add_comm 5 (callP D).length])
have r3 := fa_app_r prog o _ _ _ r2'
have r4 := fa_app_r prog o _ _ _ r3
have r5 := fa_app_r prog o _ _ _ r4
have r5' := fa_base prog o _ (o + ((callP D).length + (callP D).length + 13)) _ r5 (by
show o + ((callP D).length + 13) + (mapI (shift ((callP D).length + 13)) (callP D)).length =
o + ((callP D).length + (callP D).length + 13)
rw [mapI_len, Nat.add_assoc, Nat.add_right_comm (callP D).length 13 (callP D).length])
have ga := fa_app_l prog o _ _ _ r4
exact LpAt.mk
(fa_app_l prog o o _ _ fa)
(fa_map prog o (o + 5) 5 _ (fa_app_l prog o _ _ _ r1) rfl)
(fa_map prog o _ _ _ (fa_app_l prog o _ _ _ r2') rfl)
(fa_ins prog o _ _ (fa_app_l prog o _ _ _ r3) 0 _ rfl)
(fa_ins prog o _ _ (fa_app_l prog o _ _ _ r3) 1 _ rfl)
(fa_ins prog o _ _ (fa_app_l prog o _ _ _ r3) 2 _ rfl)
(fa_map prog o _ _ _ ga rfl)
(fa_ins prog o _ _ r5' 0 _ rfl)
(fa_ins prog o _ _ r5' 1 _ rfl)
(fa_ins prog o _ _ r5' 2 _ rfl)
(fa_ins prog o _ _ r5' 3 _ rfl)
theorem revApp_snoc : forall (p : List Bool) (b : Bool) (m : List Bool), revApp (p ++ [b]) m = b :: revApp p m
| [], _, _ => rfl
| c :: p, b, m => revApp_snoc p b (c :: m)
/-- The state of the second loop at (k, p). -/
structure St2 (x : Nat -> List Bool) (k : Nat) (p z : List Bool) : Prop where
r17 : x 17 = tr k
r18 : x 18 = revApp p []
r19 : x 19 = revApp z []
r21 : x 21 = []
r22 : x 22 = []
r23 : x 23 = []
blk : forall i, x (i + 32) = []
/-- The frame: registers below 32 other than 17, 18, 19, 21, 22, 23. -/
def Fr (y x : Nat -> List Bool) : Prop :=
forall i, i < 32 -> Nat.beq i 17 = false -> Nat.beq i 18 = false -> Nat.beq i 19 = false ->
Nat.beq i 21 = false -> Nat.beq i 22 = false -> Nat.beq i 23 = false -> y i = x i
theorem head_run (prog : List Ins) (o : Nat) (D : List Ins) (eD : PE) (L : List Bool -> Bool)
(hD : DecRM D eD L) (lp : LpAt prog o D) (z : List Bool) (S : Nat) (k : Nat) (p : List Bool)
(hS : p.length + z.length <= S) (x : Nat -> List Bool) (st : St2 x k p z) :
Exists fun N => Exists fun y => N <= (3 * k + 1) + callB D eD S + (3 * k + 1) /\
Runs prog N o x (o + ((callP D).length + 10)) y /\ y 22 = L (enc z (enc p (tr 0))) :: [] /\
y 17 = tr k /\ y 18 = revApp p [] /\ y 19 = revApp z [] /\ y 21 = [] /\ y 23 = [] /\
(forall i, y (i + 32) = []) /\ Fr y x := by
cases move_spec prog o 17 23 lp.m1 rfl (tr k) [] x st.r17 st.r23 with
| intro y1 h1 =>
have q17 : y1 17 = tr 0 := h1.2.1
have q18 : y1 18 = revApp p [] := (h1.2.2.2 18 rfl rfl).trans st.r18
have q19 : y1 19 = revApp z [] := (h1.2.2.2 19 rfl rfl).trans st.r19
have q21 : y1 21 = [] := (h1.2.2.2 21 rfl rfl).trans st.r21
have qb : forall i, y1 (i + 32) = [] := fun i =>
(h1.2.2.2 (i + 32) (hi_lt 17 i (lt32 17 rfl)) (hi_lt 23 i (lt32 23 rfl))).trans (st.blk i)
cases calld_run prog (o + 5) D eD L hD lp.c1 0 p z y1 q17 q18 q19 q21 qb with
| intro Nc hc =>
cases hc with
| intro y2 h2 =>
have sc : Runs prog Nc (o + 5) y1 (o + ((callP D).length + 5)) y2 := by
have e := h2.2.1
rw [exit_eq, Nat.add_assoc, Nat.add_comm 5 (callP D).length] at e
exact e
have q23 : y2 23 = revApp (tr k) [] := (h2.2.2.2.2.2.2.2.2 23 (lt32 23 rfl) rfl rfl rfl rfl rfl).trans h1.2.2.1
cases move_spec prog (o + ((callP D).length + 5)) 23 17 lp.m2 rfl _ _ y2 q23 h2.2.2.2.1 with
| intro y3 h3 =>
have s := runs_seq prog _ _ o (o + ((callP D).length + 5)) (o + ((callP D).length + 10)) x y2 y3
(runs_seq prog _ Nc o (o + 5) _ x y1 y2 h1.1 sc) h3.1
have hb := calld_bnd D eD 0 p z S (by rw [Nat.zero_add]; exact hS) Nc h2.1
refine Exists.intro _ (Exists.intro y3 (And.intro ?_ (And.intro s ?_)))
· have l1 : (revApp (tr k) []).length = k := by rw [revApp_len, tr_len]; rfl
rw [tr_len, l1]
exact Nat.add_le_add (Nat.add_le_add_left hb _) (Nat.le_refl _)
· refine And.intro ?_ (And.intro ?_ (And.intro ?_ (And.intro ?_ (And.intro ?_ (And.intro h3.2.1 (And.intro ?_ ?_))))))
· rw [h3.2.2.2 22 rfl rfl, h2.2.2.1, (h1.2.2.2 22 rfl rfl).trans st.r22]
· rw [h3.2.2.1]; exact revApp_nil2 (tr k)
· rw [h3.2.2.2 18 rfl rfl]; exact h2.2.2.2.2.1
· rw [h3.2.2.2 19 rfl rfl]; exact h2.2.2.2.2.2.1
· rw [h3.2.2.2 21 rfl rfl]; exact h2.2.2.2.2.2.2.1
· intro i
rw [h3.2.2.2 (i + 32) (hi_lt 23 i (lt32 23 rfl)) (hi_lt 17 i (lt32 17 rfl))]
exact h2.2.2.2.2.2.2.2.1 i
· intro i hi a b c d e g
rw [h3.2.2.2 i g a, h2.2.2.2.2.2.2.2.2 i hi a b c d e, h1.2.2.2 i a g]
end Found.Pnp83
#print axioms Found.Pnp83.fa_base
#print axioms Found.Pnp83.lp_frags
#print axioms Found.Pnp83.revApp_snoc
#print axioms Found.Pnp83.head_run
#print axioms: 4 theorems, no axioms.
The second loop extends the prefix one bit at a time while some short extension is a preimage, and leaves with a preimage.
found/Pnp84.lean
/-
T332 piece (T330 file series) 84. found/Pnp84 -- the second search loop: extending the
prefix one bit at a time (toward search-to-decision on the register machine, open lemma
O5rm).
G p k: some s with |s| <= k has f (p ++ s) = z. When the decider answers L (q p k) = G p k
(Pnp76 lf_yes, lf_sound), loop2P (Pnp83) keeps G as an invariant: if (p, 0) is a yes, p is
a preimage and the loop leaves; otherwise k > 0 and the bit b of p ++ [b] is chosen by
asking (p ++ [false], k - 1). Proved:
* app_snoc_assoc, len_snoc, it_le, it_le1: list and step arithmetic;
* loop2_run: from St2 at (k, p) with |p| + k <= m and G p k, the loop leaves at
o + (C + C + 17) with register 18 = revApp p' [] for some p' with f p' = z (beqL),
|p'| <= m, register 19 kept, the block >= 32 empty and the frame kept, within
(k + 1) It steps, It = (3 m + 1) + callB S + (3 m + 1) + (callB S + 8), m + |z| <= S.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp83
namespace Found.Pnp84
open Found.Pnp3 (PE pe)
open Found.Pnp20 (Ins upd upd_same upd_other nthI revApp)
open Found.Pnp21 (Frag Runs runs_seq)
open Found.Pnp23 (tr app_nil'')
open Found.Pnp4 (enc)
open Found.Pnp70 (run1_push run1_jmp run1_pop_cons)
open Found.Pnp74 (lt32 hi_lt)
open Found.Pnp76 (app_len)
open Found.Pnp80 (callP calld_run)
open Found.Pnp81 (callB calld_bnd)
open Found.Pnp82 (exit_eq)
open Found.Pnp83 (LpAt St2 Fr head_run revApp_snoc)
open Found.Owf1 (beqL)
open Found.Owf4 (DecRM)
theorem app_snoc_assoc : forall (p : List Bool) (b : Bool) (s : List Bool), (p ++ [b]) ++ s = p ++ (b :: s)
| [], _, _ => rfl
| c :: p, b, s => congrArg (fun t => c :: t) (app_snoc_assoc p b s)
theorem len_snoc (p : List Bool) (b : Bool) : (p ++ [b]).length = p.length + 1 := by
rw [app_len]; rfl
def It (D : List Ins) (eD : PE) (m S : Nat) : Nat :=
(3 * m + 1) + callB D eD S + (3 * m + 1) + (callB D eD S + 8)
theorem it_le (A B H CB : Nat) (hA : A <= H) (hB : B <= CB) : A + 1 + 1 + 1 + B + 1 + 1 + 1 + 1 <= H + (CB + 8) := by
show (A + 3 + B) + 4 <= (H + CB) + 8
rw [Nat.add_right_comm A 3 B]
exact Nat.le_succ_of_le (Nat.add_le_add_right (Nat.add_le_add_right (Nat.add_le_add hA hB) 3) 4)
theorem it_le1 (A B H CB : Nat) (hA : A <= H) (hB : B <= CB) : A + 1 + 1 + 1 + B + 1 <= H + (CB + 8) :=
Nat.le_trans (Nat.add_le_add_right (Nat.le_add_right _ 3) _) (it_le A B H CB hA hB)
theorem it_le0 (A H CB : Nat) (hA : A <= H) : A + 1 <= H + (CB + 8) :=
Nat.add_le_add hA (Nat.le_trans (Nat.le_add_left 1 7) (Nat.le_add_left 8 CB))
theorem k_le (m k : Nat) (p : List Bool) (h : p.length + k <= m) : k <= m := Nat.le_trans (Nat.le_add_left k _) h
theorem head_le (D : List Ins) (eD : PE) (m S k : Nat) (hk : k <= m) :
(3 * k + 1) + callB D eD S + (3 * k + 1) <= (3 * m + 1) + callB D eD S + (3 * m + 1) :=
Nat.add_le_add (Nat.add_le_add_right (Nat.add_le_add_right (Nat.mul_le_mul_left 3 hk) 1) _)
(Nat.add_le_add_right (Nat.mul_le_mul_left 3 hk) 1)
theorem loop2_run (prog : List Ins) (o : Nat) (D : List Ins) (eD : PE) (L : List Bool -> Bool)
(hD : DecRM D eD L) (lp : LpAt prog o D) (f : List Bool -> List Bool) (z : List Bool) (m S : Nat)
(hS : m + z.length <= S)
(hL1 : forall p k, (Exists fun s => s.length <= k /\ beqL z (f (p ++ s)) = true) ->
L (enc z (enc p (tr k))) = true)
(hL2 : forall p k, L (enc z (enc p (tr k))) = true ->
Exists fun s => s.length <= k /\ beqL z (f (p ++ s)) = true) :
forall k (p : List Bool) (x : Nat -> List Bool), p.length + k <= m -> St2 x k p z ->
(Exists fun s => s.length <= k /\ beqL z (f (p ++ s)) = true) ->
Exists fun N => Exists fun y => Exists fun p' : List Bool =>
N <= (k + 1) * It D eD m S /\ Runs prog N o x (o + ((callP D).length + (callP D).length + 17)) y /\
beqL z (f p') = true /\ y 18 = revApp p' [] /\ p'.length <= m /\ y 19 = revApp z [] /\
(forall i, y (i + 32) = []) /\ Fr y x := by
intro k
induction k with
| zero =>
intro p x hm st hG
have hpS : p.length + z.length <= S := Nat.le_trans (Nat.add_le_add_right hm _) hS
cases head_run prog o D eD L hD lp z S 0 p hpS x st with
| intro Nh hh =>
cases hh with
| intro y3 h3 =>
have hyes : L (enc z (enc p (tr 0))) = true := hL1 p 0 hG
rw [hyes] at h3
have s1 := run1_pop_cons prog _ 22 _ _ _ lp.a0 y3 true [] h3.2.2.1
have s := runs_seq prog Nh 1 o _ _ x y3 _ h3.2.1 s1
cases hG with
| intro s0 hs0 =>
have e0 : s0 = [] := by
cases s0 with
| nil => rfl
| cons b t => exact absurd hs0.1 (Nat.not_succ_le_zero _)
rw [e0, app_nil''] at hs0
refine Exists.intro _ (Exists.intro _ (Exists.intro p (And.intro ?_ (And.intro s (And.intro hs0.2
(And.intro ?_ (And.intro (Nat.le_trans (Nat.le_add_right _ 0) hm) (And.intro ?_ (And.intro ?_ ?_)))))))))
· rw [Nat.zero_add, Nat.one_mul]
exact it_le0 _ _ _ (head_le D eD m S 0 (Nat.zero_le m) |> Nat.le_trans h3.1)
· rw [upd_other y3 22 18 [] rfl]; exact h3.2.2.2.2.1
· rw [upd_other y3 22 19 [] rfl]; exact h3.2.2.2.2.2.1
· intro i; rw [upd_other y3 22 (i + 32) [] (hi_lt 22 i (lt32 22 rfl))]; exact h3.2.2.2.2.2.2.2.2.1 i
· intro i hi a b c d e g; rw [upd_other y3 22 i [] e]; exact h3.2.2.2.2.2.2.2.2.2 i hi a b c d e g
| succ k ih =>
intro p x hm st hG
have hk : k + 1 <= m := k_le m (k + 1) p hm
have hpS : p.length + z.length <= S :=
Nat.le_trans (Nat.add_le_add_right (Nat.le_trans (Nat.le_add_right _ _) hm) _) hS
cases head_run prog o D eD L hD lp z S (k + 1) p hpS x st with
| intro Nh hh =>
cases hh with
| intro y3 h3 =>
have hNh := Nat.le_trans h3.1 (head_le D eD m S (k + 1) hk)
cases hq0 : L (enc z (enc p (tr 0))) with
| true =>
rw [hq0] at h3
have s1 := run1_pop_cons prog _ 22 _ _ _ lp.a0 y3 true [] h3.2.2.1
have s := runs_seq prog Nh 1 o _ _ x y3 _ h3.2.1 s1
cases hL2 p 0 hq0 with
| intro s0 hs0 =>
have e0 : s0 = [] := by
cases s0 with
| nil => rfl
| cons b t => exact absurd hs0.1 (Nat.not_succ_le_zero _)
rw [e0, app_nil''] at hs0
refine Exists.intro _ (Exists.intro _ (Exists.intro p (And.intro ?_ (And.intro s (And.intro hs0.2
(And.intro ?_ (And.intro (Nat.le_trans (Nat.le_add_right _ _) hm) (And.intro ?_ (And.intro ?_ ?_)))))))))
· exact Nat.le_trans (it_le0 _ _ _ hNh) (Found.Pnp82.le_mulS (k + 1) _)
· rw [upd_other y3 22 18 [] rfl]; exact h3.2.2.2.2.1
· rw [upd_other y3 22 19 [] rfl]; exact h3.2.2.2.2.2.1
· intro i; rw [upd_other y3 22 (i + 32) [] (hi_lt 22 i (lt32 22 rfl))]; exact h3.2.2.2.2.2.2.2.2.1 i
· intro i hi a b c d e g; rw [upd_other y3 22 i [] e]; exact h3.2.2.2.2.2.2.2.2.2 i hi a b c d e g
| false =>
rw [hq0] at h3
-- pop 22, pop 17, push 18 false
have s1 := run1_pop_cons prog _ 22 _ _ _ lp.a0 y3 false [] h3.2.2.1
have w17 : upd y3 22 [] 17 = true :: tr k := by rw [upd_other y3 22 17 [] rfl]; exact h3.2.2.2.1
have s2 := run1_pop_cons prog _ 17 _ _ _ lp.a1 (upd y3 22 []) true (tr k) w17
have s3 := run1_push prog _ 18 false lp.a2 (upd (upd y3 22 []) 17 (tr k))
-- the state before the second call
have v17 : upd (upd (upd y3 22 []) 17 (tr k)) 18 (false :: upd (upd y3 22 []) 17 (tr k) 18) 17 = tr k := by
rw [upd_other _ 18 17 _ rfl, upd_same]
have v18 : upd (upd (upd y3 22 []) 17 (tr k)) 18 (false :: upd (upd y3 22 []) 17 (tr k) 18) 18 =
revApp (p ++ [false]) [] := by
rw [upd_same, upd_other _ 17 18 _ rfl, upd_other y3 22 18 [] rfl, h3.2.2.2.2.1, revApp_snoc]
have v19 : upd (upd (upd y3 22 []) 17 (tr k)) 18 (false :: upd (upd y3 22 []) 17 (tr k) 18) 19 = revApp z [] := by
rw [upd_other _ 18 19 _ rfl, upd_other _ 17 19 _ rfl, upd_other y3 22 19 [] rfl]; exact h3.2.2.2.2.2.1
have v21 : upd (upd (upd y3 22 []) 17 (tr k)) 18 (false :: upd (upd y3 22 []) 17 (tr k) 18) 21 = [] := by
rw [upd_other _ 18 21 _ rfl, upd_other _ 17 21 _ rfl, upd_other y3 22 21 [] rfl]; exact h3.2.2.2.2.2.2.1
have vb : forall i, upd (upd (upd y3 22 []) 17 (tr k)) 18 (false :: upd (upd y3 22 []) 17 (tr k) 18) (i + 32) = [] := by
intro i
rw [upd_other _ 18 _ _ (hi_lt 18 i (lt32 18 rfl)), upd_other _ 17 _ _ (hi_lt 17 i (lt32 17 rfl)),
upd_other y3 22 _ [] (hi_lt 22 i (lt32 22 rfl))]
exact h3.2.2.2.2.2.2.2.2.1 i
cases calld_run prog _ D eD L hD lp.c2 k (p ++ [false]) z _ v17 v18 v19 v21 vb with
| intro Nc hc =>
cases hc with
| intro y4 h4 =>
have hmS : k + (p ++ [false]).length + z.length <= S := by
rw [len_snoc]
refine Nat.le_trans (Nat.add_le_add_right (Nat.le_of_eq ?_) _) (Nat.le_trans (Nat.add_le_add_right hm _) hS)
rw [Nat.add_comm k, Nat.add_assoc, Nat.add_comm 1 k]
have hNc := calld_bnd D eD k (p ++ [false]) z S hmS Nc h4.1
have sc : Runs prog Nc (o + ((callP D).length + 13))
(upd (upd (upd y3 22 []) 17 (tr k)) 18 (false :: upd (upd y3 22 []) 17 (tr k) 18)) (o + ((callP D).length + (callP D).length + 13)) y4 := by
have e := h4.2.1
rw [exit_eq, Nat.add_assoc, Nat.add_right_comm (callP D).length 13 (callP D).length] at e
exact e
have s4 := runs_seq prog (Nh + 1 + 1 + 1) Nc o _ _ x _ y4
(runs_seq prog (Nh + 1 + 1) 1 o _ _ x _ _ (runs_seq prog (Nh + 1) 1 o _ _ x _ _
(runs_seq prog Nh 1 o _ _ x y3 _ h3.2.1 s1) s2) s3) sc
have q22 : y4 22 = L (enc z (enc (p ++ [false]) (tr k))) :: [] := by
rw [h4.2.2.1, upd_other _ 18 22 _ rfl, upd_other _ 17 22 _ rfl, upd_same]
-- registers of y4 that the next round needs
have y23 : y4 23 = [] := by
rw [h4.2.2.2.2.2.2.2.2 23 (lt32 23 rfl) rfl rfl rfl rfl rfl, upd_other _ 18 23 _ rfl,
upd_other _ 17 23 _ rfl, upd_other y3 22 23 [] rfl]
exact h3.2.2.2.2.2.2.2.1
have frY4 : Fr y4 x := by
intro i hi a b c d e g
rw [h4.2.2.2.2.2.2.2.2 i hi a b c d e, upd_other _ 18 i _ b, upd_other _ 17 i _ a, upd_other y3 22 i [] e]
exact h3.2.2.2.2.2.2.2.2.2 i hi a b c d e g
-- the invariant gives a nonempty extension
cases hG with
| intro s0 hs0 =>
cases s0 with
| nil =>
rw [app_nil''] at hs0
have hy : L (enc z (enc p (tr 0))) = true :=
hL1 p 0 (Exists.intro [] (And.intro (Nat.le_refl 0) (by rw [app_nil'']; exact hs0.2)))
rw [hq0] at hy
exact Bool.noConfusion hy
| cons b s' =>
have hs' : s'.length <= k := Nat.le_of_succ_le_succ hs0.1
have hlen : (p ++ [false]).length + k <= m := by
rw [len_snoc, Nat.add_assoc, Nat.add_comm 1 k]; exact hm
have hlen' : (p ++ [true]).length + k <= m := by
rw [len_snoc, Nat.add_assoc, Nat.add_comm 1 k]; exact hm
cases hq1 : L (enc z (enc (p ++ [false]) (tr k))) with
| true =>
rw [hq1] at q22
have s5 := run1_pop_cons prog _ 22 _ _ _ lp.b0 y4 true [] q22
have s := runs_seq prog _ 1 o _ (o + 0) x y4 _ s4 s5
have st' : St2 (upd y4 22 []) k (p ++ [false]) z := by
refine St2.mk ?_ ?_ ?_ ?_ (upd_same y4 22 []) ?_ ?_
· rw [upd_other y4 22 17 [] rfl]; exact h4.2.2.2.1
· rw [upd_other y4 22 18 [] rfl]; exact h4.2.2.2.2.1
· rw [upd_other y4 22 19 [] rfl]; exact h4.2.2.2.2.2.1
· rw [upd_other y4 22 21 [] rfl]; exact h4.2.2.2.2.2.2.1
· rw [upd_other y4 22 23 [] rfl]; exact y23
· intro i; rw [upd_other y4 22 (i + 32) [] (hi_lt 22 i (lt32 22 rfl))]; exact h4.2.2.2.2.2.2.2.1 i
cases ih (p ++ [false]) (upd y4 22 []) hlen st' (hL2 _ _ hq1) with
| intro N hN =>
cases hN with
| intro y hy =>
cases hy with
| intro p' hp' =>
have s' := runs_seq prog _ N o o _ x _ y s hp'.2.1
refine Exists.intro _ (Exists.intro y (Exists.intro p' (And.intro ?_ (And.intro s' (And.intro hp'.2.2.1
(And.intro hp'.2.2.2.1 (And.intro hp'.2.2.2.2.1 (And.intro hp'.2.2.2.2.2.1 (And.intro hp'.2.2.2.2.2.2.1 ?_)))))))))
· rw [Nat.succ_mul (k + 1), Nat.add_comm ((k + 1) * It D eD m S) (It D eD m S)]
exact Nat.add_le_add (it_le1 _ _ _ _ hNh hNc) hp'.1
· intro i hi a b c d e g
rw [hp'.2.2.2.2.2.2.2 i hi a b c d e g, upd_other y4 22 i [] e]
exact frY4 i hi a b c d e g
| false =>
rw [hq1] at q22
have s5 := run1_pop_cons prog _ 22 _ _ _ lp.b0 y4 false [] q22
have r18 : upd y4 22 [] 18 = false :: revApp p [] := by
rw [upd_other y4 22 18 [] rfl, h4.2.2.2.2.1, revApp_snoc]
have s6 := run1_pop_cons prog _ 18 _ _ _ lp.b1 (upd y4 22 []) false (revApp p []) r18
have s7 := run1_push prog _ 18 true lp.b2 (upd (upd y4 22 []) 18 (revApp p []))
have s8 := run1_jmp prog _ (o + 0) lp.b3
(upd (upd (upd y4 22 []) 18 (revApp p [])) 18 (true :: upd (upd y4 22 []) 18 (revApp p []) 18))
have s := runs_seq prog _ 1 o _ (o + 0) x _ _ (runs_seq prog _ 1 o _ _ x _ _
(runs_seq prog _ 1 o _ _ x _ _ (runs_seq prog _ 1 o _ _ x y4 _ s4 s5) s6) s7) s8
-- the bit is true
have hb : b = true := by
cases b with
| true => rfl
| false =>
have hy := hL1 (p ++ [false]) k (Exists.intro s' (And.intro hs' (by rw [app_snoc_assoc]; exact hs0.2)))
rw [hq1] at hy
exact Bool.noConfusion hy
rw [hb] at hs0
have w : forall i, Nat.beq i 18 = false -> Nat.beq i 22 = false ->
upd (upd (upd y4 22 []) 18 (revApp p [])) 18 (true :: upd (upd y4 22 []) 18 (revApp p []) 18) i = y4 i := by
intro i a e
rw [upd_other _ 18 i _ a, upd_other _ 18 i _ a, upd_other y4 22 i [] e]
have st' : St2 (upd (upd (upd y4 22 []) 18 (revApp p [])) 18 (true :: upd (upd y4 22 []) 18 (revApp p []) 18))
k (p ++ [true]) z := by
refine St2.mk ?_ ?_ ?_ ?_ ?_ ?_ ?_
· rw [w 17 rfl rfl]; exact h4.2.2.2.1
· rw [upd_same, upd_same, revApp_snoc]
· rw [w 19 rfl rfl]; exact h4.2.2.2.2.2.1
· rw [w 21 rfl rfl]; exact h4.2.2.2.2.2.2.1
· rw [upd_other _ 18 22 _ rfl, upd_other _ 18 22 _ rfl, upd_same]
· rw [w 23 rfl rfl]; exact y23
· intro i
rw [w (i + 32) (hi_lt 18 i (lt32 18 rfl)) (hi_lt 22 i (lt32 22 rfl))]
exact h4.2.2.2.2.2.2.2.1 i
cases ih (p ++ [true]) _ hlen' st'
(Exists.intro s' (And.intro hs' (by rw [app_snoc_assoc]; exact hs0.2))) with
| intro N hN =>
cases hN with
| intro y hy =>
cases hy with
| intro p' hp' =>
have s' := runs_seq prog _ N o o _ x _ y s hp'.2.1
refine Exists.intro _ (Exists.intro y (Exists.intro p' (And.intro ?_ (And.intro s' (And.intro hp'.2.2.1
(And.intro hp'.2.2.2.1 (And.intro hp'.2.2.2.2.1 (And.intro hp'.2.2.2.2.2.1 (And.intro hp'.2.2.2.2.2.2.1 ?_)))))))))
· rw [Nat.succ_mul (k + 1), Nat.add_comm ((k + 1) * It D eD m S) (It D eD m S)]
exact Nat.add_le_add (it_le _ _ _ _ hNh hNc) hp'.1
· intro i hi a b c d e g
rw [hp'.2.2.2.2.2.2.2 i hi a b c d e g, w i b e]
exact frY4 i hi a b c d e g
end Found.Pnp84
#print axioms Found.Pnp84.app_snoc_assoc
#print axioms Found.Pnp84.len_snoc
#print axioms Found.Pnp84.it_le
#print axioms Found.Pnp84.it_le1
#print axioms Found.Pnp84.it_le0
#print axioms Found.Pnp84.k_le
#print axioms Found.Pnp84.head_le
#print axioms Found.Pnp84.loop2_run
#print axioms: 8 theorems, no axioms.
The inverter runs both loops on \(f(x)\) and writes a preimage on the output register within a bound in \(|x|\).
found/Pnp85.lean
/-
T332 piece (T330 file series) 85. found/Pnp85 -- the inverter program and its run
(toward search-to-decision on the register machine, open lemma O5rm).
invP D = MOVE 13 19; loop1P D (Pnp82); loop2P D (Pnp83, 84); MOVE 18 16.
On input z = f x it finds a least-ish length bound k* <= |x| with a preimage of length at
most k*, then extends a prefix to a preimage p' with f p' = z, and writes p' on register 16.
Proved:
* loop1_len, loop2_len: |loop1P D| = C + 3, |loop2P D| = C + C + 17;
* le_dest, nthI_len, run_stay: a run that has reached the end stays there;
* out_len: |f x| <= pe eF |x| when F computes f (Owf4.CompRM);
* inv_run: if D decides L_f (Pnp76) and F computes f, then for every x the inverter
reaches its end from inpR (f x) within an explicit bound in |x| and pe eF |x|, with
register 16 holding some p' with f p' = f x.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp84
namespace Found.Pnp85
open Found.Pnp3 (PE pe pe_mono)
open Found.Pnp20 (Ins Cfg upd upd_same upd_other nthI revApp move run)
open Found.Pnp21 (Frag Runs runs_seq)
open Found.Pnp23 (appI mapI tr app_nil'')
open Found.Pnp4 (enc)
open Found.Pnp59 (inpR inpR_13 inpR_other frag_self)
open Found.Pnp74 (seqI seqI_len fragHT move_spec lt32 hi_lt appI_len mapI_len revApp_len inpR_nil)
open Found.Pnp75 (revApp_nil2)
open Found.Pnp76 (Lf lf_yes lf_sound)
open Found.Pnp80 (callP)
open Found.Pnp81 (callB)
open Found.Pnp82 (loop1P glue1 St1 loop1_run)
open Found.Pnp83 (loop2P glueA glueB St2 Fr lp_frags)
open Found.Pnp84 (It loop2_run)
open Found.Owf1 (beqL)
open Found.Owf2 (beqL_refl beqL_eq)
open Found.Owf4 (DecRM CompRM)
def invP (D : List Ins) : List Ins := seqI (move 13 19) (seqI (loop1P D) (seqI (loop2P D) (move 18 16)))
theorem loop1_len (D : List Ins) : (loop1P D).length = (callP D).length + 3 := by
show (appI (callP D) (glue1 (callP D).length)).length = _
rw [appI_len]; rfl
theorem ar2 (n : Nat) : 5 + (n + (5 + (3 + (n + 4)))) = n + n + 17 := by
have h1 : 3 + (n + 4) = n + 7 := Nat.add_comm 3 (n + 4)
rw [h1]
have h2 : 5 + (n + 7) = n + 12 := Nat.add_comm 5 (n + 7)
rw [h2, Nat.add_comm 5 (n + (n + 12))]
rfl
theorem loop2_len (D : List Ins) : (loop2P D).length = (callP D).length + (callP D).length + 17 := by
unfold loop2P
rw [appI_len, appI_len, appI_len, appI_len, appI_len, mapI_len, mapI_len, mapI_len]
exact ar2 (callP D).length
theorem le_dest {n m : Nat} (h : n <= m) : Exists fun d => n + d = m := by
induction h with
| refl => exact Exists.intro 0 rfl
| step _ ih => cases ih with | intro d hd => exact Exists.intro (d + 1) (congrArg Nat.succ hd)
theorem nthI_len : forall p : List Ins, nthI p p.length = none
| [] => rfl
| _ :: p => nthI_len p
theorem run_stay (I : List Ins) (N t : Nat) (h : N <= t) (x y : Nat -> List Bool) (hr : Runs I N 0 x I.length y) :
(run I t (Cfg.mk 0 x)).pc = I.length /\ forall i, (run I t (Cfg.mk 0 x)).regs i = y i := by
cases le_dest h with
| intro d hd =>
rw [hd.symm, Found.Pnp20.run_add]
have e : nthI I (run I N (Cfg.mk 0 x)).pc = none := by rw [hr.1]; exact nthI_len I
rw [Found.Pnp72.run_halt I _ e d]
exact hr
theorem out_len (F : List Ins) (eF : PE) (f : List Bool -> List Bool) (hF : CompRM F eF f) (x : List Bool) :
(f x).length <= pe eF x.length := by
cases hF x with
| intro N hN =>
cases hN with
| intro y hy =>
have e : (run F N (Cfg.mk 0 (inpR x))).regs 16 = f x := (hy.2.1.2 16).trans hy.2.2.2
rw [e.symm]
exact Nat.le_trans (Found.Pnp77.len_run F 16 N _)
(Nat.le_trans (Nat.add_le_add_right (Nat.le_of_eq (congrArg List.length (inpR_other x 16 rfl))) N)
(Nat.le_trans (Nat.le_of_eq (Nat.zero_add N)) hy.1))
/-- The bound, with n = |x| and Z = pe eF |x|. -/
def invB (D : List Ins) (eD : PE) (n Z : Nat) : Nat :=
(3 * Z + 1) + (n + 1) * (callB D eD (n + Z) + 3) + (n + 1) * It D eD n (n + Z) + (3 * n + 1)
theorem inv_run (D : List Ins) (eD : PE) (F : List Ins) (eF : PE) (f : List Bool -> List Bool)
(hD : DecRM D eD (Lf f)) (hF : CompRM F eF f) (x : List Bool) :
Exists fun N => Exists fun y => Exists fun p' : List Bool =>
N <= invB D eD x.length (pe eF x.length) /\ Runs (invP D) N 0 (inpR (f x)) (invP D).length y /\
y 16 = p' /\ beqL (f x) (f p') = true := by
have hf0 := frag_self (invP D)
have g1 := fragHT (invP D) 0 (move 13 19) _ hf0
have g2 := fragHT (invP D) 5 (loop1P D) _ g1.2
have g3 := fragHT (invP D) (5 + (loop1P D).length) (loop2P D) _ g2.2
have hz := out_len F eF f hF x
-- MOVE 13 19
cases move_spec (invP D) 0 13 19 g1.1 rfl (f x) [] (inpR (f x)) (inpR_13 (f x)) rfl with
| intro y0 h0 =>
have st1 : St1 y0 0 (f x) := by
refine St1.mk ((h0.2.2.2 17 rfl rfl)) ((h0.2.2.2 18 rfl rfl)) h0.2.2.1 ((h0.2.2.2 21 rfl rfl))
((h0.2.2.2 22 rfl rfl)) ?_
intro i
rw [h0.2.2.2 (i + 32) (hi_lt 13 i (lt32 13 rfl)) (hi_lt 19 i (lt32 19 rfl))]
exact inpR_other _ _ (hi_lt 13 i (lt32 13 rfl))
have hn : Lf f (enc (f x) (enc [] (tr x.length))) = true :=
lf_yes f (f x) [] x x.length (Nat.le_refl _) (beqL_refl (f x))
have hS : x.length + (f x).length <= x.length + pe eF x.length := Nat.add_le_add_left hz _
cases loop1_run (invP D) 5 D eD (Lf f) hD g2.1 (f x) x.length _ hS hn x.length 0 y0 (Nat.zero_add _) st1 with
| intro N1 h1 =>
cases h1 with
| intro y1 h1 =>
cases h1 with
| intro ks hk =>
-- the second loop
have st2 : St2 y1 ks [] (f x) := by
refine St2.mk hk.2.2.2.1.r17 hk.2.2.2.1.r18 hk.2.2.2.1.r19 hk.2.2.2.1.r21 hk.2.2.2.1.r22 ?_ hk.2.2.2.1.blk
rw [hk.2.2.2.2.2.2 23 (lt32 23 rfl) rfl rfl rfl rfl rfl, h0.2.2.2 23 rfl rfl]
rfl
have lp := lp_frags (invP D) (5 + (loop1P D).length) D g3.1
have hG := lf_sound f (f x) [] ks hk.2.2.1
cases loop2_run (invP D) (5 + (loop1P D).length) D eD (Lf f) hD lp f (f x) x.length _ hS
(fun p k h => match h with
| Exists.intro s hs => lf_yes f (f x) p s k hs.1 hs.2)
(fun p k h => lf_sound f (f x) p k h) ks [] y1 (Nat.le_trans (Nat.le_of_eq (Nat.zero_add ks)) hk.2.1) st2 hG with
| intro N2 h2 =>
cases h2 with
| intro y2 h2 =>
cases h2 with
| intro p' hp =>
-- MOVE 18 16
have o4 : 5 + (loop1P D).length + (loop2P D).length = 5 + (loop1P D).length + ((callP D).length + (callP D).length + 17) := by
rw [loop2_len]
have g4 := g3.2
rw [o4] at g4
have y16 : y2 16 = [] := by
rw [hp.2.2.2.2.2.2.2 16 (lt32 16 rfl) rfl rfl rfl rfl rfl rfl,
hk.2.2.2.2.2.2 16 (lt32 16 rfl) rfl rfl rfl rfl rfl, h0.2.2.2 16 rfl rfl]
rfl
cases move_spec (invP D) _ 18 16 g4 rfl _ _ y2 hp.2.2.2.1 y16 with
| intro y3 h3 =>
have s1 : Runs (invP D) N1 5 y0 (5 + (loop1P D).length) y1 := by
rw [loop1_len]; exact hk.2.2.2.2.2.1
have s := runs_seq (invP D) _ _ 0 _ _ (inpR (f x)) y2 y3
(runs_seq (invP D) _ N2 0 _ _ (inpR (f x)) y1 y2
(runs_seq (invP D) _ N1 0 5 _ (inpR (f x)) y0 y1 h0.1 s1) hp.2.1) h3.1
have hlen : (invP D).length = 5 + (loop1P D).length + ((callP D).length + (callP D).length + 17) + 5 := by
show (seqI (move 13 19) (seqI (loop1P D) (seqI (loop2P D) (move 18 16)))).length = _
rw [seqI_len, seqI_len, seqI_len, loop2_len]
show 5 + ((loop1P D).length + ((callP D).length + (callP D).length + 17 + 5)) = _
rw [Nat.add_assoc 5 (loop1P D).length]
rfl
rw [hlen.symm] at s
refine Exists.intro _ (Exists.intro y3 (Exists.intro p' (And.intro ?_ (And.intro s (And.intro ?_ ?_)))))
· have m2 : N2 <= (x.length + 1) * It D eD x.length (x.length + pe eF x.length) :=
Nat.le_trans hp.1 (Nat.mul_le_mul_right _ (Nat.succ_le_succ hk.2.1))
have m4 : 3 * (revApp p' []).length + 1 <= 3 * x.length + 1 := by
rw [revApp_len]
exact Nat.add_le_add_right (Nat.mul_le_mul_left 3 hp.2.2.2.2.1) 1
exact Nat.add_le_add (Nat.add_le_add (Nat.add_le_add (Nat.add_le_add_right (Nat.mul_le_mul_left 3 hz) 1)
(hk.2.2.2.2.1)) m2) m4
· rw [h3.2.2.1]; exact revApp_nil2 p'
· have e := beqL_eq _ _ hp.2.2.1
rw [e]
exact beqL_refl _
end Found.Pnp85
#print axioms Found.Pnp85.loop1_len
#print axioms Found.Pnp85.ar2
#print axioms Found.Pnp85.loop2_len
#print axioms Found.Pnp85.le_dest
#print axioms Found.Pnp85.nthI_len
#print axioms Found.Pnp85.run_stay
#print axioms Found.Pnp85.out_len
#print axioms Found.Pnp85.inv_run
#print axioms: 8 theorems, no axioms.
With the bound as a polynomial, search reduces to decision on the register machine, and a one-way function gives \(\mathrm {NP}_{\mathrm {RM}}\not \subseteq \mathrm {P}_{\mathrm {RM}}\).
found/Pnp86.lean
/-
T332 piece (T330 file series) 86. found/Pnp86 -- search-to-decision on the register
machine, and one-way functions give NP not inside P there (closes open lemma O5rm).
The bound of the inverter (Pnp85 invB) is a polynomial expression in |x| (Pnp3 PE, with
pe_comp for the decider's bound). Proved:
* callBE_eq, invBE_eq: the bounds as PE values;
* search_rm: if L_f (Pnp76) is decided in polynomial time and f is computed in polynomial
time, the inverter invP inverts f on every input within pe (invBE ..) |x| steps
(Owf4.SearchRM);
* s2drm: Owf4.S2DRM holds (O5rm);
* owf_p_ne_np: for every register-machine one-way function f (Owf4.OneWayRM),
NP_RM is not inside P_RM -- the hypothesis S2DRM of Owf4.owf_p_ne_np_rm is removed.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp85
namespace Found.Pnp86
open Found.Pnp3 (PE pe pe_comp comp)
open Found.Pnp20 (Ins Cfg run)
open Found.Pnp59 (inpR)
open Found.Pnp76 (Lf lf_inNP)
open Found.Pnp80 (nB)
open Found.Pnp81 (callB)
open Found.Pnp84 (It)
open Found.Pnp85 (invP invB inv_run run_stay)
open Found.Owf1 (beqL)
open Found.Owf2 (beqL_refl beqL_eq)
open Found.Owf4 (DecRM CompRM InPrm InNPrm SearchRM S2DRM OneWayRM resRM invOK owf_p_ne_np_rm)
def BnE (s : PE) : PE := .add (.add (.add (.add s s) s) s) (.c 4)
def B2 (eF : PE) : PE := BnE (.add (.add .x eF) (.add .x eF))
def callBE (D : List Ins) (eD eF : PE) : PE :=
.add (.add (.add (.add (.mul (.c 8) (B2 eF)) (.c 0)) (comp eD (B2 eF))) (.c 3))
(.mul (.c (nB D)) (.add (.add (B2 eF) (comp eD (B2 eF))) (.c 1)))
def invBE (D : List Ins) (eD eF : PE) : PE :=
.add (.add (.add (.add (.mul (.c 3) eF) (.c 1)) (.mul (.add .x (.c 1)) (.add (callBE D eD eF) (.c 3))))
(.mul (.add .x (.c 1)) (.add (.add (.add (.add (.mul (.c 3) .x) (.c 1)) (callBE D eD eF))
(.add (.mul (.c 3) .x) (.c 1))) (.add (callBE D eD eF) (.c 8)))))
(.add (.mul (.c 3) .x) (.c 1))
theorem callBE_eq (D : List Ins) (eD eF : PE) (n : Nat) :
pe (callBE D eD eF) n = callB D eD (n + pe eF n) := by
show 8 * pe (B2 eF) n + 0 + pe (comp eD (B2 eF)) n + 3 + nB D * (pe (B2 eF) n + pe (comp eD (B2 eF)) n + 1) = _
rw [pe_comp]
rfl
theorem invBE_eq (D : List Ins) (eD eF : PE) (n : Nat) :
pe (invBE D eD eF) n = invB D eD n (pe eF n) := by
show (3 * pe eF n + 1) + (n + 1) * (pe (callBE D eD eF) n + 3) +
(n + 1) * ((3 * n + 1) + pe (callBE D eD eF) n + (3 * n + 1) + (pe (callBE D eD eF) n + 8)) + (3 * n + 1) = _
rw [callBE_eq]
rfl
theorem search_rm (F : List Ins) (eF : PE) (f : List Bool -> List Bool) (hF : CompRM F eF f)
(D : List Ins) (eD : PE) (hD : DecRM D eD (Lf f)) : SearchRM f := by
refine Exists.intro (invP D) (Exists.intro (invBE D eD eF) (fun x => ?_))
cases inv_run D eD F eF f hD hF x with
| intro N hN =>
cases hN with
| intro y hy =>
cases hy with
| intro p' hp =>
have hle : N <= pe (invBE D eD eF) x.length := by rw [invBE_eq]; exact hp.1
have st := run_stay (invP D) N _ hle _ y hp.2.1
show invOK f x (Found.Owf4.selOL (Nat.beq (run (invP D) (pe (invBE D eD eF) x.length) (Cfg.mk 0 (inpR (f x)))).pc
(invP D).length) (some ((run (invP D) (pe (invBE D eD eF) x.length) (Cfg.mk 0 (inpR (f x)))).regs 16)) none) = true
rw [st.1, Found.Pnp20.beq_refl', st.2 16, hp.2.2.1]
show beqL (f p') (f x) = true
rw [beqL_eq _ _ hp.2.2.2]
exact beqL_refl _
/-- Search-to-decision on the register machine (open lemma O5rm). -/
theorem s2drm : S2DRM :=
fun hnp f hc =>
match hc with
| Exists.intro F (Exists.intro eF hF) =>
match hnp (Lf f) (lf_inNP F eF f hF) with
| Exists.intro D (Exists.intro eD hD) => search_rm F eF f hF D eD hD
/-- A register-machine one-way function gives NP_RM not inside P_RM. -/
theorem owf_p_ne_np (f : List Bool -> List Bool) (ho : OneWayRM f) :
(forall L, InNPrm L -> InPrm L) -> False :=
owf_p_ne_np_rm s2drm f ho
end Found.Pnp86
#print axioms Found.Pnp86.callBE_eq
#print axioms Found.Pnp86.invBE_eq
#print axioms Found.Pnp86.search_rm
#print axioms Found.Pnp86.s2drm
#print axioms Found.Pnp86.owf_p_ne_np
#print axioms: 5 theorems, no axioms.
A pigeon map sends each pigeon to a hole or to none; under it the clause of a pigeon holds exactly when the pigeon has a hole, and every hole clause holds when the map is injective.
found/Pnp87.lean
/-
T330 piece 87. found/Pnp87 -- assignments from pigeon maps, and the pigeonhole formula
under them (proof complexity ladder, step 1: general resolution; PNP_MAP.md).
A pigeon map g sends a pigeon i to some hole or to none; asgP n g gives x(i, j) = true
exactly when g i = some j (Pnp66 findIJ decodes a variable; other variables are false).
These are the assignments of the Haken / Beame--Pitassi argument (critical assignments are
the maps that are injective and send all pigeons but one). Proved:
* asg_var: asgP n g (var n i j) = optEq (g i) j for i < n + 1, j < n;
* evalCNF_app: evaluation of an appended CNF;
* pcl_true, pcl_false: the clause of pigeon i holds iff g i is some hole below n;
* holes_true: an injective map satisfies every hole clause of PHP(n + 1, n).
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp66
namespace Found.Pnp87
open Found.RootComp (Lit Clause CNF evalLit evalClause evalCNF)
open Found.Pnp65 (var var_inj)
open Found.Pnp66 (pcl pigeons hcl holesJ holes PHP findIJ findIJ_some findIJ_none)
def optEq : Option Nat -> Nat -> Bool
| none, _ => false
| some a, j => Nat.beq a j
def decA (g : Nat -> Option Nat) : Option (Nat × Nat) -> Bool
| none => false
| some p => optEq (g p.1) p.2
def asgP (n : Nat) (g : Nat -> Option Nat) (v : Nat) : Bool := decA g (findIJ n v (n + 1))
theorem asg_var (n : Nat) (g : Nat -> Option Nat) (i j : Nat) (hi : i < n + 1) (hj : j < n) :
asgP n g (var n i j) = optEq (g i) j := by
show decA g (findIJ n (var n i j) (n + 1)) = optEq (g i) j
cases e : findIJ n (var n i j) (n + 1) with
| none =>
have t := findIJ_none n _ (n + 1) e i j hi hj
rw [Found.Pnp62.beq_refl] at t
exact Bool.noConfusion t
| some p =>
cases p with
| mk i' j' =>
have t := findIJ_some n _ (n + 1) i' j' e
have u := var_inj n i' i j' j t.2.1 hj t.2.2
show optEq (g i') j' = optEq (g i) j
rw [u.1, u.2]
theorem optEq_some (a j : Nat) (h : optEq (some a) j = true) : a = j := Found.Pnp2.beq_eq a j h
theorem optEq_of (o : Option Nat) (j : Nat) (h : o = some j) : optEq o j = true := by
rw [h]; exact Found.Pnp62.beq_refl j
theorem evalCNF_app (a : Nat -> Bool) : forall F G : CNF, evalCNF a (F ++ G) = (evalCNF a F && evalCNF a G)
| [], _ => rfl
| C :: F, G => by
show (evalClause a C && evalCNF a (F ++ G)) = ((evalClause a C && evalCNF a F) && evalCNF a G)
rw [evalCNF_app a F G]
cases evalClause a C with
| false => rfl
| true => rfl
theorem and_true' {a b : Bool} (ha : a = true) (hb : b = true) : (a && b) = true := by rw [ha, hb]; rfl
theorem pcl_true (n : Nat) (g : Nat -> Option Nat) (i : Nat) (hi : i < n + 1) (j : Nat) (hgi : g i = some j) :
forall k, j < k -> k <= n -> evalClause (asgP n g) (pcl n i k) = true := by
intro k
induction k with
| zero => intro h; exact absurd h (Nat.not_lt_zero j)
| succ k ih =>
intro hjk hkn
show (evalLit (asgP n g) (true, var n i k) || evalClause (asgP n g) (pcl n i k)) = true
cases Nat.lt_or_ge j k with
| inl h => exact Found.Pnp62.or_r (ih h (Nat.le_of_succ_le hkn))
| inr h =>
have ejk : j = k := Nat.le_antisymm (Nat.le_of_lt_succ hjk) h
apply Found.Pnp62.or_l
show asgP n g (var n i k) = true
rw [asg_var n g i k hi hkn, ← ejk]
exact optEq_of _ j hgi
theorem pcl_false (n : Nat) (g : Nat -> Option Nat) (i : Nat) (hi : i < n + 1) (hgi : g i = none) :
forall k, k <= n -> evalClause (asgP n g) (pcl n i k) = false := by
intro k
induction k with
| zero => intro _; rfl
| succ k ih =>
intro hkn
show (evalLit (asgP n g) (true, var n i k) || evalClause (asgP n g) (pcl n i k)) = false
have e : evalLit (asgP n g) (true, var n i k) = false := by
show asgP n g (var n i k) = false
rw [asg_var n g i k hi hkn, hgi]
rfl
rw [e, ih (Nat.le_of_succ_le hkn)]
rfl
/-- g is injective on pigeons below n + 1. -/
def Inj (n : Nat) (g : Nat -> Option Nat) : Prop :=
forall i i' j, i < n + 1 -> i' < n + 1 -> g i = some j -> g i' = some j -> i = i'
theorem not_both (n : Nat) (g : Nat -> Option Nat) (hg : Inj n g) (i' i j : Nat) (hi' : i' < i) (hi : i < n + 1)
(hj : j < n) : ((!asgP n g (var n i' j)) || (!asgP n g (var n i j))) = true := by
rw [asg_var n g i' j (Nat.lt_trans hi' hi) hj, asg_var n g i j hi hj]
cases e1 : optEq (g i') j with
| false => rfl
| true =>
cases e2 : optEq (g i) j with
| false => rfl
| true =>
have s1 : g i' = some j := by
cases h : g i' with
| none => rw [h] at e1; exact Bool.noConfusion e1
| some a => rw [h] at e1; rw [optEq_some a j e1]
have s2 : g i = some j := by
cases h : g i with
| none => rw [h] at e2; exact Bool.noConfusion e2
| some a => rw [h] at e2; rw [optEq_some a j e2]
have ee := hg i' i j (Nat.lt_trans hi' hi) hi s1 s2
rw [ee] at hi'
exact absurd hi' (Nat.lt_irrefl i)
theorem hcl_true (n : Nat) (g : Nat -> Option Nat) (hg : Inj n g) (j i : Nat) (hi : i < n + 1) (hj : j < n) :
forall i', i' <= i -> evalCNF (asgP n g) (hcl n j i i') = true := by
intro i'
induction i' with
| zero => intro _; rfl
| succ i' ih =>
intro h
show (evalClause (asgP n g) [(false, var n i' j), (false, var n i j)] && evalCNF (asgP n g) (hcl n j i i')) = true
refine and_true' ?_ (ih (Nat.le_of_succ_le h))
show ((!asgP n g (var n i' j)) || ((!asgP n g (var n i j)) || false)) = true
have t := not_both n g hg i' i j h hi hj
cases e : (!asgP n g (var n i j)) with
| true => rw [e] at t; cases (!asgP n g (var n i' j)) <;> rfl
| false =>
rw [e] at t
revert t
cases (!asgP n g (var n i' j)) <;> intro t <;> first | rfl | exact t
theorem holesJ_true (n : Nat) (g : Nat -> Option Nat) (hg : Inj n g) (j : Nat) (hj : j < n) :
forall i, i <= n + 1 -> evalCNF (asgP n g) (holesJ n j i) = true := by
intro i
induction i with
| zero => intro _; rfl
| succ i ih =>
intro h
show evalCNF (asgP n g) (hcl n j i i ++ holesJ n j i) = true
rw [evalCNF_app]
exact and_true' (hcl_true n g hg j i h hj i (Nat.le_refl i)) (ih (Nat.le_of_succ_le h))
theorem holes_true (n : Nat) (g : Nat -> Option Nat) (hg : Inj n g) :
forall k, k <= n -> evalCNF (asgP n g) (holes n k) = true := by
intro k
induction k with
| zero => intro _; rfl
| succ k ih =>
intro h
show evalCNF (asgP n g) (holesJ n k (n + 1) ++ holes n k) = true
rw [evalCNF_app]
exact and_true' (holesJ_true n g hg k h (n + 1) (Nat.le_refl _)) (ih (Nat.le_of_succ_le h))
end Found.Pnp87
#print axioms Found.Pnp87.asg_var
#print axioms Found.Pnp87.optEq_some
#print axioms Found.Pnp87.optEq_of
#print axioms Found.Pnp87.evalCNF_app
#print axioms Found.Pnp87.and_true'
#print axioms Found.Pnp87.pcl_true
#print axioms Found.Pnp87.pcl_false
#print axioms Found.Pnp87.not_both
#print axioms Found.Pnp87.hcl_true
#print axioms Found.Pnp87.holesJ_true
#print axioms Found.Pnp87.holes_true
#print axioms: 11 theorems, no axioms.
When pigeon \(i\) takes the hole \(j\) of pigeon \(i'\), a clause that turns from false to true contains \(x(i,j)\) or \(\neg x(i',j)\).
found/Pnp88.lean
/-
T330 piece 88. found/Pnp88 -- the swap step of the Beame--Pitassi argument: a clause that
flips from false to true when a hole passes from one pigeon to another names that pair
(proof complexity ladder, step 1: general resolution).
swapM g i i' j: pigeon i takes hole j from pigeon i' (who then has none). If g i = none and
g i' = some j, the assignments of g and of swapM g i i' j differ only on x(i, j) and
x(i', j). Proved:
* asg_diff: where asgP n g and asgP n (swapM ..) differ, the variable is x(i, j) or
x(i', j);
* swap_cov: if C is false under g and true under the swap, then C holds (true, x(i, j))
or (false, x(i', j)) -- the monotone width of C covers (i, j);
* lit_false_all: every literal of a false clause is false.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp87
namespace Found.Pnp88
open Found.RootComp (Lit Clause CNF evalLit evalClause evalCNF)
open Found.Pnp62 (memL beqLit beqLit_eq mem_true of_mem)
open Found.Pnp65 (var)
open Found.Pnp66 (findIJ findIJ_some)
open Found.Pnp87 (optEq decA asgP asg_var optEq_of optEq_some)
def selOpt (b : Bool) (x y : Option Nat) : Option Nat := match b with | true => x | false => y
/-- Pigeon i takes hole j; pigeon i' gets none. -/
def swapM (g : Nat -> Option Nat) (i i' j : Nat) : Nat -> Option Nat :=
fun a => selOpt (Nat.beq a i) (some j) (selOpt (Nat.beq a i') none (g a))
theorem lit_false_all (a : Nat -> Bool) : forall C : Clause, evalClause a C = false ->
forall l, memL l C = true -> evalLit a l = false := by
intro C
induction C with
| nil => intro _ l h; exact Bool.noConfusion h
| cons m C ih =>
intro h l hl
have h' : (evalLit a m || evalClause a C) = false := h
have hm : evalLit a m = false := by
cases e : evalLit a m with
| false => rfl
| true => rw [e] at h'; exact Bool.noConfusion h'
rw [hm] at h'
have hl' : (beqLit m l || memL l C) = true := hl
cases e : beqLit m l with
| true => rw [(beqLit_eq m l e).symm]; exact hm
| false => rw [e] at hl'; exact ih h' l hl'
theorem asg_diff (n : Nat) (g : Nat -> Option Nat) (i i' j : Nat)
(hgi : g i = none) (hgi' : g i' = some j) (v : Nat) (hd : asgP n (swapM g i i' j) v = true -> asgP n g v = true -> False)
(hd2 : asgP n (swapM g i i' j) v = false -> asgP n g v = false -> False) :
(v = var n i j /\ asgP n (swapM g i i' j) v = true) \/ (v = var n i' j /\ asgP n g v = true) := by
cases e : findIJ n v (n + 1) with
| none =>
have a1 : asgP n (swapM g i i' j) v = false := by show decA _ (findIJ n v (n + 1)) = false; rw [e]; rfl
have a2 : asgP n g v = false := by show decA _ (findIJ n v (n + 1)) = false; rw [e]; rfl
exact absurd a2 (hd2 a1)
| some p =>
cases p with
| mk a b =>
have t := findIJ_some n v (n + 1) a b e
have r1 : asgP n (swapM g i i' j) v = optEq (swapM g i i' j a) b := by
show decA _ (findIJ n v (n + 1)) = _; rw [e]; rfl
have r2 : asgP n g v = optEq (g a) b := by
show decA _ (findIJ n v (n + 1)) = _; rw [e]; rfl
cases ea : Nat.beq a i with
| true =>
have ai : a = i := Found.Pnp2.beq_eq a i ea
have s1 : swapM g i i' j a = some j := by show selOpt (Nat.beq a i) _ _ = _; rw [ea]; rfl
rw [r1, s1] at hd hd2
rw [r2, ai, hgi] at hd hd2
cases eb : Nat.beq j b with
| false =>
have x1 : optEq (some j) b = false := eb
exact absurd rfl (hd2 x1)
| true =>
have jb : j = b := Found.Pnp2.beq_eq j b eb
refine Or.inl (And.intro ?_ ?_)
· rw [t.2.2.symm, ai, jb]
· rw [r1, s1]; exact eb
| false =>
cases ea' : Nat.beq a i' with
| true =>
have ai : a = i' := Found.Pnp2.beq_eq a i' ea'
have s1 : swapM g i i' j a = none := by
show selOpt (Nat.beq a i) _ (selOpt (Nat.beq a i') none (g a)) = none; rw [ea, ea']; rfl
rw [r1, s1] at hd hd2
rw [r2] at hd hd2
cases eg : optEq (g a) b with
| false => exact absurd eg (hd2 rfl)
| true =>
refine Or.inr (And.intro ?_ ?_)
· have hjb : j = b := by
rw [ai, hgi'] at eg
exact optEq_some j b eg
rw [t.2.2.symm, ai, hjb]
· rw [r2]; exact eg
| false =>
have s1 : swapM g i i' j a = g a := by
show selOpt (Nat.beq a i) _ (selOpt (Nat.beq a i') none (g a)) = g a; rw [ea, ea']; rfl
rw [r1, s1, r2] at hd hd2
cases eg : optEq (g a) b with
| false => exact absurd eg (hd2 eg)
| true => exact absurd eg (hd eg)
/-- The swap step: a clause false under g and true under the swap names (i, j). -/
theorem swap_cov (n : Nat) (g : Nat -> Option Nat) (i i' j : Nat) (hgi : g i = none) (hgi' : g i' = some j)
(C : Clause) (h0 : evalClause (asgP n g) C = false) (h1 : evalClause (asgP n (swapM g i i' j)) C = true) :
memL (true, var n i j) C = true \/ memL (false, var n i' j) C = true := by
cases mem_true _ C h1 with
| intro l hl =>
have hf := lit_false_all _ C h0 l hl.1
cases l with
| mk pol v =>
cases pol with
| true =>
have a1 : asgP n (swapM g i i' j) v = true := hl.2
have a2 : asgP n g v = false := hf
cases asg_diff n g i i' j hgi hgi' v (fun _ h => by rw [a2] at h; exact Bool.noConfusion h)
(fun h _ => by rw [a1] at h; exact Bool.noConfusion h) with
| inl r => rw [r.1.symm]; exact Or.inl hl.1
| inr r => rw [a2] at r; exact Bool.noConfusion r.2
| false =>
have a1 : asgP n (swapM g i i' j) v = false := by
have t : (!asgP n (swapM g i i' j) v) = true := hl.2
cases e : asgP n (swapM g i i' j) v with
| false => rfl
| true => rw [e] at t; exact Bool.noConfusion t
have a2 : asgP n g v = true := by
have t : (!asgP n g v) = false := hf
cases e : asgP n g v with
| true => rfl
| false => rw [e] at t; exact Bool.noConfusion t
cases asg_diff n g i i' j hgi hgi' v (fun h _ => by rw [a1] at h; exact Bool.noConfusion h)
(fun _ h => by rw [a2] at h; exact Bool.noConfusion h) with
| inl r => rw [a1] at r; exact Bool.noConfusion r.2
| inr r => rw [r.1.symm]; exact Or.inr hl.1
end Found.Pnp88
#print axioms Found.Pnp88.lit_false_all
#print axioms Found.Pnp88.asg_diff
#print axioms Found.Pnp88.swap_cov
#print axioms: 3 theorems, no axioms.
The pigeon maps of length \(n+1\) with holes below \(n\) form an explicit finite list that contains every such map.
found/Pnp89.lean
/-
T330 piece 89. found/Pnp89 -- all pigeon maps as a finite list (proof complexity ladder,
step 1: general resolution).
To count the pigeons that a clause can "lose" without classical logic, the critical
assignments are searched in a finite list. A pigeon map is a list of entries none or
some j (j < n); allMaps n k lists all of length k. Proved:
* nthO, the map of a list; opts_mem: every entry none / some j (j < n) is an option;
* allMaps_complete: every list of length k with entries in range is in allMaps n k;
* anyM_true, anyM_of: a Bool search through a list of maps.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp88
import found.Pnp76
namespace Found.Pnp89
def nthO : List (Option Nat) -> Nat -> Option Nat
| [], _ => none
| o :: _, 0 => o
| _ :: l, k + 1 => nthO l k
def someUp : Nat -> List (Option Nat)
| 0 => []
| j + 1 => some j :: someUp j
def opts (n : Nat) : List (Option Nat) := none :: someUp n
def consAll (o : Option Nat) : List (List (Option Nat)) -> List (List (Option Nat))
| [] => []
| l :: ls => (o :: l) :: consAll o ls
def bindO : List (Option Nat) -> List (List (Option Nat)) -> List (List (Option Nat))
| [], _ => []
| o :: os, ls => consAll o ls ++ bindO os ls
def allMaps (n : Nat) : Nat -> List (List (Option Nat))
| 0 => [[]]
| k + 1 => bindO (opts n) (allMaps n k)
def InO (o : Option Nat) : List (Option Nat) -> Prop
| [] => False
| p :: ps => o = p \/ InO o ps
def InM (l : List (Option Nat)) : List (List (Option Nat)) -> Prop
| [] => False
| m :: ms => l = m \/ InM l ms
def okE (n : Nat) : Option Nat -> Prop
| none => True
| some j => j < n
def allOk (n : Nat) : List (Option Nat) -> Prop
| [] => True
| o :: l => okE n o /\ allOk n l
theorem someUp_mem (j : Nat) : forall n, j < n -> InO (some j) (someUp n)
| 0, h => absurd h (Nat.not_lt_zero j)
| n + 1, h =>
Or.elim (Nat.lt_or_ge j n) (fun h' => Or.inr (someUp_mem j n h'))
(fun h' => Or.inl (congrArg some (Nat.le_antisymm (Nat.le_of_lt_succ h) h')))
theorem opts_mem (n : Nat) : forall o, okE n o -> InO o (opts n)
| none, _ => Or.inl rfl
| some j, h => Or.inr (someUp_mem j n h)
theorem InM_app_l (l : List (Option Nat)) (B : List (List (Option Nat))) : forall A, InM l A -> InM l (A ++ B)
| [], h => False.elim h
| _ :: A, h => Or.elim h (fun e => Or.inl e) (fun h' => Or.inr (InM_app_l l B A h'))
theorem InM_app_r (l : List (Option Nat)) (B : List (List (Option Nat))) : forall A, InM l B -> InM l (A ++ B)
| [], h => h
| _ :: A, h => Or.inr (InM_app_r l B A h)
theorem consAll_mem (o : Option Nat) (l : List (Option Nat)) : forall ls, InM l ls -> InM (o :: l) (consAll o ls)
| [], h => False.elim h
| _ :: ms, h => Or.elim h (fun e => Or.inl (congrArg (fun t => o :: t) e)) (fun h' => Or.inr (consAll_mem o l ms h'))
theorem bindO_mem (o : Option Nat) (l : List (Option Nat)) (ls : List (List (Option Nat))) (hl : InM l ls) :
forall os, InO o os -> InM (o :: l) (bindO os ls)
| [], h => False.elim h
| p :: os, h =>
Or.elim h (fun e => by rw [e]; exact InM_app_l _ _ _ (consAll_mem p l ls hl))
(fun h' => InM_app_r _ _ _ (bindO_mem o l ls hl os h'))
theorem allMaps_complete (n : Nat) : forall l : List (Option Nat), allOk n l -> InM l (allMaps n l.length)
| [], _ => Or.inl rfl
| o :: l, h => bindO_mem o l _ (allMaps_complete n l h.2) (opts n) (opts_mem n o h.1)
def anyM (g : List (Option Nat) -> Bool) : List (List (Option Nat)) -> Bool
| [] => false
| l :: ls => g l || anyM g ls
theorem anyM_true (g : List (Option Nat) -> Bool) : forall ls, anyM g ls = true ->
Exists fun l => InM l ls /\ g l = true
| [], h => Bool.noConfusion h
| l :: ls, h =>
Or.elim (Found.Pnp76.or_true_cases _ _ h) (fun h1 => Exists.intro l (And.intro (Or.inl rfl) h1))
(fun h2 => match anyM_true g ls h2 with
| Exists.intro m hm => Exists.intro m (And.intro (Or.inr hm.1) hm.2))
theorem anyM_of (g : List (Option Nat) -> Bool) (l : List (Option Nat)) (hg : g l = true) :
forall ls, InM l ls -> anyM g ls = true
| [], h => False.elim h
| m :: ms, h =>
Or.elim h (fun e => Found.Pnp76.or_true_l _ _ (by rw [e.symm]; exact hg))
(fun h' => Found.Pnp76.or_true_r _ _ (anyM_of g l hg ms h'))
end Found.Pnp89
#print axioms Found.Pnp89.someUp_mem
#print axioms Found.Pnp89.opts_mem
#print axioms Found.Pnp89.InM_app_l
#print axioms Found.Pnp89.InM_app_r
#print axioms Found.Pnp89.consAll_mem
#print axioms Found.Pnp89.bindO_mem
#print axioms Found.Pnp89.allMaps_complete
#print axioms Found.Pnp89.anyM_true
#print axioms Found.Pnp89.anyM_of
#print axioms: 9 theorems, no axioms.
A map is critical for pigeon \(i\) under a restriction \(\rho \) when \(i\) alone among the remaining pigeons has no hole, fixed pigeons keep their holes and no hole is shared; the Bool check agrees with the definition.
found/Pnp90.lean
/-
T330 piece 90. found/Pnp90 -- critical pigeon maps as a Bool check (proof complexity
ladder, step 1: general resolution).
A restriction rho fixes some pigeons in holes (rho a = some j) and leaves the others
(rho a = none). A critical map for a remaining pigeon i: i has no hole, every other
remaining pigeon has one, the fixed pigeons keep theirs, holes are below n and no two
pigeons share a hole. Crit is the Prop form on functions, critB the Bool form on lists.
Proved:
* allI_true, allI_of: bounded Bool conjunction;
* listOf, nthO_listOf: the list of a function;
* critB_sound: critB n rho i l = true -> Crit n rho i (nthO l);
* critB_complete: Crit n rho i g -> critB n rho i (listOf g (n + 1)) = true;
* listOf_ok, listOf_len: the list of a critical map is in allMaps.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp89
namespace Found.Pnp90
open Found.Pnp87 (Inj optEq optEq_some optEq_of)
open Found.Pnp89 (nthO allOk okE)
open Found.Pnp76 (or_true_cases or_true_l or_true_r)
def allI (g : Nat -> Bool) : Nat -> Bool
| 0 => true
| k + 1 => allI g k && g k
theorem allI_true (g : Nat -> Bool) : forall k, allI g k = true -> forall x, x < k -> g x = true
| 0, _, x, hx => absurd hx (Nat.not_lt_zero x)
| k + 1, h, x, hx => by
have h' : (allI g k && g k) = true := h
cases Nat.lt_or_ge x k with
| inl l => exact allI_true g k (Found.Pnp62.and_l h') x l
| inr r =>
have e : x = k := Nat.le_antisymm (Nat.le_of_lt_succ hx) r
rw [e]; exact Found.Pnp62.and_r h'
theorem allI_of (g : Nat -> Bool) : forall k, (forall x, x < k -> g x = true) -> allI g k = true
| 0, _ => rfl
| k + 1, h => Found.Pnp87.and_true' (allI_of g k (fun x hx => h x (Nat.lt_succ_of_lt hx))) (h k (Nat.lt_succ_self k))
def isNone : Option Nat -> Bool
| none => true
| some _ => false
def isSomeLt (n : Nat) : Option Nat -> Bool
| none => false
| some j => Nat.blt j n
/-- Entry a of a critical map for i, given rho a. -/
def entryB (n : Nat) (r : Option Nat) (o : Option Nat) : Bool :=
match r with
| none => isSomeLt n o
| some j => optEq o j && Nat.blt j n
def sameSome : Option Nat -> Option Nat -> Bool
| some a, some b => Nat.beq a b
| none, _ => false
| some _, none => false
def injB (n : Nat) (g : Nat -> Option Nat) : Bool :=
allI (fun a => allI (fun b => Nat.beq a b || !sameSome (g a) (g b)) (n + 1)) (n + 1)
def critF (n : Nat) (rho : Nat -> Option Nat) (i : Nat) (g : Nat -> Option Nat) : Bool :=
isNone (g i) && isNone (rho i) && Nat.blt i (n + 1) &&
allI (fun a => Nat.beq a i || entryB n (rho a) (g a)) (n + 1) && injB n g
def critB (n : Nat) (rho : Nat -> Option Nat) (i : Nat) (l : List (Option Nat)) : Bool := critF n rho i (nthO l)
/-- The Prop form. -/
structure Crit (n : Nat) (rho : Nat -> Option Nat) (i : Nat) (g : Nat -> Option Nat) : Prop where
gi : g i = none
ri : rho i = none
il : i < n + 1
free : forall a, a < n + 1 -> Nat.beq a i = false -> rho a = none -> Exists fun j => g a = some j /\ j < n
fixed : forall a j, a < n + 1 -> Nat.beq a i = false -> rho a = some j -> g a = some j
inj : Inj n g
rng : forall a j, a < n + 1 -> g a = some j -> j < n
theorem blt_lt (a b : Nat) (h : Nat.blt a b = true) : a < b := Nat.le_of_ble_eq_true h
theorem lt_blt (a b : Nat) (h : a < b) : Nat.blt a b = true := Nat.ble_eq_true_of_le h
theorem isNone_eq (o : Option Nat) (h : isNone o = true) : o = none := by
cases o with
| none => rfl
| some _ => exact Bool.noConfusion h
theorem sameSome_of (a b : Option Nat) (j : Nat) (ha : a = some j) (hb : b = some j) : sameSome a b = true := by
rw [ha, hb]; exact Found.Pnp62.beq_refl j
theorem critF_sound (n : Nat) (rho : Nat -> Option Nat) (i : Nat) (g : Nat -> Option Nat)
(h : critF n rho i g = true) : Crit n rho i g := by
have h5 := Found.Pnp62.and_r h
have h4 := Found.Pnp62.and_r (Found.Pnp62.and_l h)
have h3 := Found.Pnp62.and_r (Found.Pnp62.and_l (Found.Pnp62.and_l h))
have h2 := Found.Pnp62.and_r (Found.Pnp62.and_l (Found.Pnp62.and_l (Found.Pnp62.and_l h)))
have h1 := Found.Pnp62.and_l (Found.Pnp62.and_l (Found.Pnp62.and_l (Found.Pnp62.and_l h)))
have ent : forall a, a < n + 1 -> Nat.beq a i = false -> entryB n (rho a) (g a) = true := by
intro a ha hai
have t := allI_true _ (n + 1) h4 a ha
rw [hai] at t
exact t
have injP : forall a b, a < n + 1 -> b < n + 1 -> (Nat.beq a b || !sameSome (g a) (g b)) = true :=
fun a b ha hb => allI_true _ (n + 1) (allI_true _ (n + 1) h5 a ha) b hb
refine Crit.mk (isNone_eq _ h1) (isNone_eq _ h2) (blt_lt _ _ h3) ?_ ?_ ?_ ?_
· intro a ha hai hr
have t := ent a ha hai
rw [hr] at t
have t' : isSomeLt n (g a) = true := t
cases e : g a with
| none => rw [e] at t'; exact Bool.noConfusion t'
| some j => rw [e] at t'; exact Exists.intro j (And.intro rfl (blt_lt j n t'))
· intro a j ha hai hr
have t := ent a ha hai
rw [hr] at t
have t' : (optEq (g a) j && Nat.blt j n) = true := t
have t1 := Found.Pnp62.and_l t'
cases e : g a with
| none => rw [e] at t1; exact Bool.noConfusion t1
| some c => rw [e] at t1; rw [optEq_some c j t1]
· intro a b j ha hb hga hgb
have t := injP a b ha hb
rw [sameSome_of _ _ j hga hgb] at t
cases e : Nat.beq a b with
| true => exact Found.Pnp2.beq_eq a b e
| false => rw [e] at t; exact Bool.noConfusion t
· intro a j ha hga
cases eai : Nat.beq a i with
| true =>
rw [Found.Pnp2.beq_eq a i eai, isNone_eq _ h1] at hga
cases hga
| false =>
have t := ent a ha eai
cases er : rho a with
| none =>
rw [er] at t
have t' : isSomeLt n (g a) = true := t
rw [hga] at t'
exact blt_lt j n t'
| some c =>
rw [er] at t
have t' : (optEq (g a) c && Nat.blt c n) = true := t
rw [hga] at t'
rw [optEq_some j c (Found.Pnp62.and_l t')]
exact blt_lt c n (Found.Pnp62.and_r t')
theorem critF_complete (n : Nat) (rho : Nat -> Option Nat) (i : Nat) (g : Nat -> Option Nat)
(h : Crit n rho i g) : critF n rho i g = true := by
have e1 : isNone (g i) = true := by rw [h.gi]; rfl
have e2 : isNone (rho i) = true := by rw [h.ri]; rfl
have e4 : allI (fun a => Nat.beq a i || entryB n (rho a) (g a)) (n + 1) = true := by
refine allI_of _ (n + 1) (fun a ha => ?_)
cases eai : Nat.beq a i with
| true => rfl
| false =>
show entryB n (rho a) (g a) = true
cases er : rho a with
| none =>
cases h.free a ha eai er with
| intro j hj =>
show isSomeLt n (g a) = true
rw [hj.1]; exact lt_blt j n hj.2
| some c =>
have hg := h.fixed a c ha eai er
show (optEq (g a) c && Nat.blt c n) = true
rw [hg]
exact Found.Pnp87.and_true' (Found.Pnp62.beq_refl c) (lt_blt c n (h.rng a c ha hg))
have e5 : injB n g = true := by
refine allI_of _ (n + 1) (fun a ha => allI_of _ (n + 1) (fun b hb => ?_))
cases eab : Nat.beq a b with
| true => rfl
| false =>
show (!sameSome (g a) (g b)) = true
cases ga : g a with
| none => rfl
| some x =>
cases gb : g b with
| none => rfl
| some y =>
cases exy : Nat.beq x y with
| false => show (!Nat.beq x y) = true; rw [exy]; rfl
| true =>
have ey := Found.Pnp2.beq_eq x y exy
rw [ey] at ga
have eq := h.inj a b y ha hb ga gb
rw [eq, Found.Pnp62.beq_refl] at eab
exact Bool.noConfusion eab
show (isNone (g i) && isNone (rho i) && Nat.blt i (n + 1) &&
allI (fun a => Nat.beq a i || entryB n (rho a) (g a)) (n + 1) && injB n g) = true
rw [e1, e2, lt_blt i (n + 1) h.il, e4, e5]
rfl
/-! ## lists of functions -/
def listFrom (g : Nat -> Option Nat) (s : Nat) : Nat -> List (Option Nat)
| 0 => []
| k + 1 => g s :: listFrom g (s + 1) k
theorem nthO_listFrom (g : Nat -> Option Nat) : forall k s a, a < k -> nthO (listFrom g s k) a = g (s + a)
| 0, _, a, h => absurd h (Nat.not_lt_zero a)
| k + 1, s, 0, _ => rfl
| k + 1, s, a + 1, h => by
show nthO (listFrom g (s + 1) k) a = g (s + (a + 1))
rw [nthO_listFrom g k (s + 1) a (Nat.lt_of_succ_lt_succ h), Nat.add_right_comm s 1 a, Nat.add_assoc]
theorem listFrom_len (g : Nat -> Option Nat) : forall k s, (listFrom g s k).length = k
| 0, _ => rfl
| k + 1, s => congrArg Nat.succ (listFrom_len g k (s + 1))
theorem listFrom_ok (n : Nat) (g : Nat -> Option Nat) : forall k s,
(forall a, a < k -> okE n (g (s + a))) -> allOk n (listFrom g s k)
| 0, _, _ => True.intro
| k + 1, s, h => And.intro (h 0 (Nat.zero_lt_succ k))
(listFrom_ok n g k (s + 1) (fun a ha => by
have t := h (a + 1) (Nat.succ_lt_succ ha)
rw [Nat.add_assoc, Nat.add_comm 1 a]
exact t))
/-- Crit depends on g only below n + 1. -/
theorem crit_congr (n : Nat) (rho : Nat -> Option Nat) (i : Nat) (g g' : Nat -> Option Nat)
(he : forall a, a < n + 1 -> g a = g' a) (h : Crit n rho i g) : Crit n rho i g' := by
refine Crit.mk ((he i h.il).symm.trans h.gi) h.ri h.il ?_ ?_ ?_ ?_
· intro a ha hai hr
cases h.free a ha hai hr with
| intro j hj => exact Exists.intro j (And.intro ((he a ha).symm.trans hj.1) hj.2)
· intro a j ha hai hr; exact (he a ha).symm.trans (h.fixed a j ha hai hr)
· intro a b j ha hb h1 h2; exact h.inj a b j ha hb ((he a ha).trans h1) ((he b hb).trans h2)
· intro a j ha h1; exact h.rng a j ha ((he a ha).trans h1)
theorem crit_list (n : Nat) (rho : Nat -> Option Nat) (i : Nat) (g : Nat -> Option Nat) (h : Crit n rho i g) :
critB n rho i (listFrom g 0 (n + 1)) = true /\ allOk n (listFrom g 0 (n + 1)) /\
(listFrom g 0 (n + 1)).length = n + 1 /\ forall a, a < n + 1 -> nthO (listFrom g 0 (n + 1)) a = g a := by
have hn : forall a, a < n + 1 -> nthO (listFrom g 0 (n + 1)) a = g a := fun a ha => by
rw [nthO_listFrom g (n + 1) 0 a ha, Nat.zero_add]
refine And.intro ?_ (And.intro ?_ (And.intro (listFrom_len g (n + 1) 0) hn))
· exact critF_complete n rho i _ (crit_congr n rho i g _ (fun a ha => (hn a ha).symm) h)
· refine listFrom_ok n g (n + 1) 0 (fun a ha => ?_)
rw [Nat.zero_add]
cases e : g a with
| none => exact True.intro
| some j => exact h.rng a j ha e
end Found.Pnp90
#print axioms Found.Pnp90.allI_true
#print axioms Found.Pnp90.allI_of
#print axioms Found.Pnp90.blt_lt
#print axioms Found.Pnp90.lt_blt
#print axioms Found.Pnp90.isNone_eq
#print axioms Found.Pnp90.sameSome_of
#print axioms Found.Pnp90.critF_sound
#print axioms Found.Pnp90.critF_complete
#print axioms Found.Pnp90.nthO_listFrom
#print axioms Found.Pnp90.listFrom_len
#print axioms Found.Pnp90.listFrom_ok
#print axioms Found.Pnp90.crit_congr
#print axioms Found.Pnp90.crit_list
#print axioms: 13 theorems, no axioms.
A clause loses pigeon \(i\) when some critical map for \(i\) falsifies it; a resolvent loses only pigeons a premise loses, and an axiom loses at most one.
found/Pnp91.lean
/-
T330 piece 91. found/Pnp91 -- the pigeons a clause can lose, as a Bool search (proof
complexity ladder, step 1: general resolution).
pigsB n rho C i: some critical map for i (relative to the restriction rho) falsifies C.
It is computed by searching allMaps (Pnp89), so no classical logic is needed. Proved:
* asg_congr: asgP n g depends on g only below n + 1;
* pigs_sound, pigs_complete: pigsB is true exactly when some critical map falsifies C;
* pigs_res: a resolvent can lose only pigeons one of its premises can lose;
* pigs_axiom: a clause of PHP(n + 1, n) can lose at most one pigeon.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp90
namespace Found.Pnp91
open Found.RootComp (Lit Clause CNF evalLit evalClause evalCNF)
open Found.Pnp62 (memL memC ResOK res_sound cnf_mem)
open Found.Pnp65 (var)
open Found.Pnp66 (pcl PHP findIJ findIJ_some memC_app pigeons_mem holes_mem)
open Found.Pnp87 (decA asgP Inj pcl_true holes_true)
open Found.Pnp89 (nthO allMaps anyM anyM_true anyM_of allMaps_complete)
open Found.Pnp90 (critB critF Crit critF_sound crit_list)
def pigsB (n : Nat) (rho : Nat -> Option Nat) (C : Clause) (i : Nat) : Bool :=
anyM (fun l => critB n rho i l && !evalClause (asgP n (nthO l)) C) (allMaps n (n + 1))
theorem asg_congr (n : Nat) (g g' : Nat -> Option Nat) (he : forall a, a < n + 1 -> g a = g' a) (v : Nat) :
asgP n g v = asgP n g' v := by
show decA g (findIJ n v (n + 1)) = decA g' (findIJ n v (n + 1))
cases e : findIJ n v (n + 1) with
| none => rfl
| some p =>
cases p with
| mk a b =>
have t := findIJ_some n v (n + 1) a b e
show Found.Pnp87.optEq (g a) b = Found.Pnp87.optEq (g' a) b
rw [he a t.1]
theorem eval_congr (a a' : Nat -> Bool) (he : forall v, a v = a' v) : forall C : Clause, evalClause a C = evalClause a' C
| [] => rfl
| l :: C => by
show (evalLit a l || evalClause a C) = (evalLit a' l || evalClause a' C)
rw [eval_congr a a' he C]
show ((match l.1 with | true => a l.2 | false => !(a l.2)) || _) = ((match l.1 with | true => a' l.2 | false => !(a' l.2)) || _)
rw [he l.2]
theorem not_true {b : Bool} (h : (!b) = true) : b = false := by
cases b with
| false => rfl
| true => exact Bool.noConfusion h
theorem pigs_sound (n : Nat) (rho : Nat -> Option Nat) (C : Clause) (i : Nat) (h : pigsB n rho C i = true) :
Exists fun g => Crit n rho i g /\ evalClause (asgP n g) C = false :=
match anyM_true _ _ h with
| Exists.intro l hl =>
Exists.intro (nthO l) (And.intro (critF_sound n rho i _ (Found.Pnp62.and_l hl.2))
(not_true (Found.Pnp62.and_r hl.2)))
theorem pigs_complete (n : Nat) (rho : Nat -> Option Nat) (C : Clause) (i : Nat) (g : Nat -> Option Nat)
(hc : Crit n rho i g) (hf : evalClause (asgP n g) C = false) : pigsB n rho C i = true := by
have cl := crit_list n rho i g hc
have mem := allMaps_complete n _ cl.2.1
rw [cl.2.2.1] at mem
refine anyM_of _ _ ?_ _ mem
have ev : evalClause (asgP n (nthO (Found.Pnp90.listFrom g 0 (n + 1)))) C = false := by
rw [eval_congr _ (asgP n g) (fun v => asg_congr n _ g cl.2.2.2 v) C]
exact hf
rw [cl.1, ev]
rfl
theorem pigs_res (n : Nat) (rho : Nat -> Option Nat) (C1 C2 R : Clause) (v : Nat) (hr : ResOK C1 C2 v R)
(i : Nat) (h : pigsB n rho R i = true) : pigsB n rho C1 i = true \/ pigsB n rho C2 i = true := by
cases pigs_sound n rho R i h with
| intro g hg =>
cases e1 : evalClause (asgP n g) C1 with
| false => exact Or.inl (pigs_complete n rho C1 i g hg.1 e1)
| true =>
cases e2 : evalClause (asgP n g) C2 with
| false => exact Or.inr (pigs_complete n rho C2 i g hg.1 e2)
| true =>
have t := res_sound (asgP n g) C1 C2 R v hr e1 e2
rw [hg.2] at t
exact Bool.noConfusion t
/-- Under a critical map every hole clause holds and every pigeon clause but that of i. -/
theorem crit_pcl (n : Nat) (rho : Nat -> Option Nat) (i : Nat) (g : Nat -> Option Nat) (hc : Crit n rho i g)
(i0 : Nat) (hi0 : i0 < n + 1) (hne : Nat.beq i0 i = false) : evalClause (asgP n g) (pcl n i0 n) = true := by
cases er : rho i0 with
| none =>
cases hc.free i0 hi0 hne er with
| intro j hj => exact pcl_true n g i0 hi0 j hj.1 n hj.2 (Nat.le_refl n)
| some j =>
have hg := hc.fixed i0 j hi0 hne er
exact pcl_true n g i0 hi0 j hg n (hc.rng i0 j hi0 hg) (Nat.le_refl n)
theorem pigs_axiom (n : Nat) (rho : Nat -> Option Nat) (C : Clause) (hm : memC C (PHP n) = true)
(i i' : Nat) (h : pigsB n rho C i = true) (h' : pigsB n rho C i' = true) : i = i' := by
cases memC_app C _ _ hm with
| inl hp =>
cases pigeons_mem n C (n + 1) hp with
| intro i0 hi0 =>
have one : forall k, pigsB n rho C k = true -> k = i0 := by
intro k hk
cases pigs_sound n rho C k hk with
| intro g hg =>
cases e : Nat.beq i0 k with
| true => exact (Found.Pnp2.beq_eq i0 k e).symm
| false =>
have t := crit_pcl n rho k g hg.1 i0 hi0.1 e
rw [hi0.2] at hg
rw [hg.2] at t
exact Bool.noConfusion t
rw [one i h, one i' h']
| inr hh =>
cases pigs_sound n rho C i h with
| intro g hg =>
have t := cnf_mem (asgP n g) C _ (holes_true n g hg.1.inj n (Nat.le_refl n)) hh
rw [hg.2] at t
exact Bool.noConfusion t
end Found.Pnp91
#print axioms Found.Pnp91.asg_congr
#print axioms Found.Pnp91.eval_congr
#print axioms Found.Pnp91.not_true
#print axioms Found.Pnp91.pigs_sound
#print axioms Found.Pnp91.pigs_complete
#print axioms Found.Pnp91.pigs_res
#print axioms Found.Pnp91.crit_pcl
#print axioms Found.Pnp91.pigs_axiom
#print axioms: 8 theorems, no axioms.
A critical map for \(i\) swapped with a remaining pigeon \(i'\) is critical for \(i'\), so a clause that loses \(i\) but not \(i'\) covers the pair \((i,g(i'))\).
found/Pnp92.lean
/-
T330 piece 92. found/Pnp92 -- the swap keeps maps critical, and a clause that loses pigeon i
but not pigeon i' covers the pair (i, hole of i') (proof complexity ladder, step 1:
general resolution).
Proved:
* swap_crit: if g is critical for i and i' is a remaining pigeon (i' <> i) with hole j,
then swapM g i i' j is critical for i';
* cov_of_pigs: if g is critical for i and falsifies C, and C cannot lose i', then C holds
(true, x(i, j)) or (false, x(i', j)) where j is the hole of i' under g.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp91
namespace Found.Pnp92
open Found.RootComp (Clause evalClause)
open Found.Pnp62 (memL)
open Found.Pnp65 (var)
open Found.Pnp87 (asgP Inj)
open Found.Pnp88 (swapM selOpt swap_cov)
open Found.Pnp90 (Crit)
open Found.Pnp91 (pigsB pigs_complete)
theorem sw_i (g : Nat -> Option Nat) (i i' j : Nat) : swapM g i i' j i = some j := by
show selOpt (Nat.beq i i) (some j) _ = some j
rw [Found.Pnp62.beq_refl]; rfl
theorem sw_i' (g : Nat -> Option Nat) (i i' j : Nat) (h : Nat.beq i' i = false) : swapM g i i' j i' = none := by
show selOpt (Nat.beq i' i) (some j) (selOpt (Nat.beq i' i') none (g i')) = none
rw [h, Found.Pnp62.beq_refl]; rfl
theorem sw_other (g : Nat -> Option Nat) (i i' j a : Nat) (h1 : Nat.beq a i = false) (h2 : Nat.beq a i' = false) :
swapM g i i' j a = g a := by
show selOpt (Nat.beq a i) (some j) (selOpt (Nat.beq a i') none (g a)) = g a
rw [h1, h2]; rfl
theorem swap_crit (n : Nat) (rho : Nat -> Option Nat) (i i' j : Nat) (g : Nat -> Option Nat) (hc : Crit n rho i g)
(hi' : i' < n + 1) (hne : Nat.beq i' i = false) (hr : rho i' = none) (hj : g i' = some j) :
Crit n rho i' (swapM g i i' j) := by
have jn : j < n := hc.rng i' j hi' hj
have hne' : Nat.beq i i' = false := Found.Pnp70.nsym i' i hne
refine Crit.mk (sw_i' g i i' j hne) hr hi' ?_ ?_ ?_ ?_
· intro a ha hai hra
cases e : Nat.beq a i with
| true =>
rw [Found.Pnp2.beq_eq a i e, sw_i]
exact Exists.intro j (And.intro rfl jn)
| false =>
rw [sw_other g i i' j a e hai]
exact hc.free a ha e hra
· intro a c ha hai hra
cases e : Nat.beq a i with
| true =>
rw [Found.Pnp2.beq_eq a i e, hc.ri] at hra
cases hra
| false =>
rw [sw_other g i i' j a e hai]
exact hc.fixed a c ha e hra
· intro a b c ha hb h1 h2
-- values: i -> j, i' -> none, others -> g
have val : forall x, x < n + 1 -> swapM g i i' j x = some c ->
(x = i /\ c = j) \/ (Nat.beq x i = false /\ Nat.beq x i' = false /\ g x = some c) := by
intro x hx hxc
cases ex : Nat.beq x i with
| true =>
rw [Found.Pnp2.beq_eq x i ex, sw_i] at hxc
exact Or.inl (And.intro (Found.Pnp2.beq_eq x i ex) (Option.some.inj hxc).symm)
| false =>
cases ex' : Nat.beq x i' with
| true =>
rw [Found.Pnp2.beq_eq x i' ex', sw_i' g i i' j hne] at hxc
cases hxc
| false =>
rw [sw_other g i i' j x ex ex'] at hxc
exact Or.inr (And.intro rfl (And.intro rfl hxc))
cases val a ha h1 with
| inl va =>
cases val b hb h2 with
| inl vb => rw [va.1, vb.1]
| inr vb =>
rw [va.2] at vb
have e := hc.inj b i' j hb hi' vb.2.2 hj
rw [e, Found.Pnp62.beq_refl] at vb
exact Bool.noConfusion vb.2.1
| inr va =>
cases val b hb h2 with
| inl vb =>
rw [vb.2] at va
have e := hc.inj a i' j ha hi' va.2.2 hj
rw [e, Found.Pnp62.beq_refl] at va
exact Bool.noConfusion va.2.1
| inr vb => exact hc.inj a b c ha hb va.2.2 vb.2.2
· intro a c ha hac
cases e : Nat.beq a i with
| true =>
rw [Found.Pnp2.beq_eq a i e, sw_i] at hac
rw [(Option.some.inj hac).symm]
exact jn
| false =>
cases e' : Nat.beq a i' with
| true =>
rw [Found.Pnp2.beq_eq a i' e', sw_i' g i i' j hne] at hac
cases hac
| false =>
rw [sw_other g i i' j a e e'] at hac
exact hc.rng a c ha hac
theorem cov_of_pigs (n : Nat) (rho : Nat -> Option Nat) (C : Clause) (i i' j : Nat) (g : Nat -> Option Nat)
(hc : Crit n rho i g) (hf : evalClause (asgP n g) C = false)
(hi' : i' < n + 1) (hne : Nat.beq i' i = false) (hr : rho i' = none) (hj : g i' = some j)
(hnot : pigsB n rho C i' = false) :
memL (true, var n i j) C = true \/ memL (false, var n i' j) C = true := by
cases e : evalClause (asgP n (swapM g i i' j)) C with
| false =>
have t := pigs_complete n rho C i' _ (swap_crit n rho i i' j g hc hi' hne hr hj) e
rw [hnot] at t
exact Bool.noConfusion t
| true => exact swap_cov n g i i' j hc.gi hj C hf e
end Found.Pnp92
#print axioms Found.Pnp92.sw_i
#print axioms Found.Pnp92.sw_i'
#print axioms Found.Pnp92.sw_other
#print axioms Found.Pnp92.swap_crit
#print axioms Found.Pnp92.cov_of_pigs
#print axioms: 5 theorems, no axioms.
For any measure at most one on axioms and subadditive under resolution, a proof of a clause above \(b\ge 1\) contains a clause \(D\) with \(b/2<f(D)\le b\).
found/Pnp93.lean
/-
T330 piece 93. found/Pnp93 -- the middle clause of a resolution proof (proof complexity
ladder, step 1: general resolution; Beame--Pitassi form of Haken's argument).
nodes p lists the clauses of a resolution proof p. For a measure f on clauses that is at
most one on axioms and subadditive along resolution steps, a proof of a clause with
f > b (b >= 1) contains a clause D with f D <= b < 2 f D. With f = the number of pigeons a
clause can lose under a restriction (Pnp91) this is the middle clause of the argument.
Proved:
* cntI_sub: a count is at most the sum of two counts that cover it;
* cntI_le_one': a predicate true at no two places counts at most one;
* root_mem: the proved clause is a node;
* mid: the middle clause for any such measure;
* cntP_ax, cntP_res, mid_pigs: the middle clause for the pigeon count.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp92
namespace Found.Pnp93
open Found.RootComp (Lit Clause CNF evalClause)
open Found.Pnp62 (memC beqCl ResOK RProof)
open Found.Pnp65 (cntI bv cntI_zero)
open Found.Pnp66 (PHP beqCl_refl)
open Found.Pnp91 (pigsB pigs_res pigs_axiom)
/-- The clauses of a resolution proof (with repetition). -/
def nodes {F : CNF} : {C : Clause} -> RProof F C -> List Clause
| _, .ax C _ => [C]
| R, .res _ _ _ _ p1 p2 _ => R :: (nodes p1 ++ nodes p2)
theorem memC_cons_r (D C : Clause) (L : List Clause) (h : memC D L = true) : memC D (C :: L) = true :=
Found.Pnp62.or_r h
theorem memC_head (C : Clause) (L : List Clause) : memC C (C :: L) = true :=
Found.Pnp62.or_l (beqCl_refl C)
theorem memC_app_l (D : Clause) (B : List Clause) : forall A : List Clause, memC D A = true -> memC D (A ++ B) = true
| [], h => Bool.noConfusion h
| C :: A, h => by
have h' : (beqCl C D || memC D A) = true := h
show (beqCl C D || memC D (A ++ B)) = true
cases e : beqCl C D with
| true => rfl
| false => rw [e] at h'; exact memC_app_l D B A h'
theorem memC_app_r (D : Clause) (B : List Clause) : forall A : List Clause, memC D B = true -> memC D (A ++ B) = true
| [], h => h
| _ :: A, h => Found.Pnp62.or_r (memC_app_r D B A h)
theorem root_mem {F : CNF} : forall {C : Clause} (p : RProof F C), memC C (nodes p) = true
| _, .ax C _ => memC_head C []
| R, .res _ _ _ _ _ _ _ => memC_head R _
theorem bv_or (a b c : Bool) (h : a = true -> b = true \/ c = true) : bv a <= bv b + bv c := by
cases a with
| false => exact Nat.zero_le _
| true =>
cases h rfl with
| inl hb => rw [hb]; exact Nat.le_add_right 1 _
| inr hc => rw [hc]; exact Nat.le_add_left 1 _
theorem add4 (a b c d : Nat) : (a + b) + (c + d) = (a + c) + (b + d) := by
rw [Nat.add_assoc, Nat.add_assoc, Nat.add_left_comm b c d]
theorem cntI_sub (h f g : Nat -> Bool) : forall k, (forall j, j < k -> h j = true -> f j = true \/ g j = true) ->
cntI h k <= cntI f k + cntI g k
| 0, _ => Nat.le_refl 0
| k + 1, hh => by
show cntI h k + bv (h k) <= (cntI f k + bv (f k)) + (cntI g k + bv (g k))
rw [add4]
exact Nat.add_le_add (cntI_sub h f g k (fun j hj => hh j (Nat.lt_succ_of_lt hj)))
(bv_or _ _ _ (hh k (Nat.lt_succ_self k)))
theorem cntI_le_one' (g : Nat -> Bool) (hu : forall i i', g i = true -> g i' = true -> i = i') :
forall k, cntI g k <= 1
| 0 => Nat.zero_le 1
| k + 1 => by
show cntI g k + bv (g k) <= 1
cases e : g k with
| false => exact cntI_le_one' g hu k
| true =>
have z : cntI g k = 0 := cntI_zero g k (fun j hj => by
cases ej : g j with
| false => rfl
| true =>
have t := hu j k ej e
rw [t] at hj
exact absurd hj (Nat.lt_irrefl k))
rw [z]
exact Nat.le_refl 1
theorem le_of_lt_le {a b c : Nat} (h1 : b < a) (h2 : a <= c) (h3 : c <= b) : False :=
Nat.lt_irrefl b (Nat.lt_of_lt_of_le h1 (Nat.le_trans h2 h3))
/-- The middle clause, for any measure that is at most one on axioms and subadditive. -/
theorem mid (F : CNF) (f : Clause -> Nat) (hax : forall C, memC C F = true -> f C <= 1)
(hres : forall C1 C2 R v, ResOK C1 C2 v R -> f R <= f C1 + f C2) (b : Nat) (hb : 1 <= b) :
forall (C : Clause) (p : RProof F C), b < f C ->
Exists fun D => memC D (nodes p) = true /\ f D <= b /\ b < f D + f D := by
intro C p
induction p with
| ax C h => intro hc; exact absurd (hax C h) (fun t => le_of_lt_le hc t hb)
| res C1 C2 R v p1 p2 h ih1 ih2 =>
intro hc
have up : forall D, (memC D (nodes p1) = true \/ memC D (nodes p2) = true) ->
memC D (nodes (RProof.res C1 C2 R v p1 p2 h)) = true := by
intro D hD
apply memC_cons_r
cases hD with
| inl a => exact memC_app_l D _ _ a
| inr a => exact memC_app_r D _ _ a
cases Nat.lt_or_ge b (f C1) with
| inl l1 =>
cases ih1 l1 with
| intro D hD => exact Exists.intro D (And.intro (up D (Or.inl hD.1)) hD.2)
| inr g1 =>
cases Nat.lt_or_ge b (f C2) with
| inl l2 =>
cases ih2 l2 with
| intro D hD => exact Exists.intro D (And.intro (up D (Or.inr hD.1)) hD.2)
| inr g2 =>
have s := Nat.lt_of_lt_of_le hc (hres C1 C2 R v h)
cases Nat.lt_or_ge (f C1) (f C2) with
| inl l =>
refine Exists.intro C2 (And.intro (up C2 (Or.inr (root_mem p2))) (And.intro g2 ?_))
exact Nat.lt_of_lt_of_le s (Nat.add_le_add_right (Nat.le_of_lt l) _)
| inr l =>
refine Exists.intro C1 (And.intro (up C1 (Or.inl (root_mem p1))) (And.intro g1 ?_))
exact Nat.lt_of_lt_of_le s (Nat.add_le_add_left l _)
/-- The number of pigeons a clause can lose under rho. -/
def cntP (n : Nat) (rho : Nat -> Option Nat) (C : Clause) : Nat := cntI (pigsB n rho C) (n + 1)
theorem cntP_ax (n : Nat) (rho : Nat -> Option Nat) (C : Clause) (hm : memC C (PHP n) = true) :
cntP n rho C <= 1 :=
cntI_le_one' _ (fun i i' h h' => pigs_axiom n rho C hm i i' h h') (n + 1)
theorem cntP_res (n : Nat) (rho : Nat -> Option Nat) (C1 C2 R : Clause) (v : Nat) (hr : ResOK C1 C2 v R) :
cntP n rho R <= cntP n rho C1 + cntP n rho C2 :=
cntI_sub _ _ _ (n + 1) (fun i _ h => pigs_res n rho C1 C2 R v hr i h)
theorem mid_pigs (n : Nat) (rho : Nat -> Option Nat) (b : Nat) (hb : 1 <= b) (C : Clause)
(p : RProof (PHP n) C) (hc : b < cntP n rho C) :
Exists fun D => memC D (nodes p) = true /\ cntP n rho D <= b /\ b < cntP n rho D + cntP n rho D :=
mid (PHP n) (cntP n rho) (cntP_ax n rho) (cntP_res n rho) b hb C p hc
end Found.Pnp93
#print axioms Found.Pnp93.memC_cons_r
#print axioms Found.Pnp93.memC_head
#print axioms Found.Pnp93.memC_app_l
#print axioms Found.Pnp93.memC_app_r
#print axioms Found.Pnp93.root_mem
#print axioms Found.Pnp93.bv_or
#print axioms Found.Pnp93.add4
#print axioms Found.Pnp93.cntI_sub
#print axioms Found.Pnp93.cntI_le_one'
#print axioms Found.Pnp93.le_of_lt_le
#print axioms Found.Pnp93.mid
#print axioms Found.Pnp93.cntP_ax
#print axioms Found.Pnp93.cntP_res
#print axioms Found.Pnp93.mid_pigs
#print axioms: 14 theorems, no axioms.
For a good restriction the \(k\)-th remaining pigeon other than \(i\) goes to the \(k\)-th free hole, a critical map found by bounded search; the empty clause loses every remaining pigeon.
found/Pnp94.lean
/-
T330 piece 94. found/Pnp94 -- critical maps exist, so the empty clause loses every
remaining pigeon (proof complexity ladder, step 1: general resolution).
A restriction rho is good when its holes are below n, no two pigeons share a hole, and the
remaining pigeons are at most one more than the free holes. For a good rho and a remaining
pigeon i, cmap sends the k-th remaining pigeon other than i to the k-th free hole (a bounded
search, no choice). Proved:
* rank_ex: below cntI P m every rank k is reached by some j with cntI P j = k;
* search_some, search_of: bounded search;
* cntI_agree, cntI_mono, cntI_off, rank_lt: count tools;
* crit_exists: cmap n rho i is critical for i;
* cntP_nil: the empty clause loses exactly the remaining pigeons.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp93
namespace Found.Pnp94
open Found.RootComp (Clause evalClause)
open Found.Pnp65 (cntI bv anyI anyI_false anyI_true)
open Found.Pnp87 (optEq optEq_of optEq_some asgP)
open Found.Pnp88 (selOpt)
open Found.Pnp90 (Crit isNone isNone_eq)
open Found.Pnp91 (pigsB pigs_sound pigs_complete not_true)
open Found.Pnp93 (cntP)
def fillO (r x : Option Nat) : Option Nat :=
match r with
| some j => some j
| none => x
def search (q : Nat -> Bool) : Nat -> Option Nat
| 0 => none
| m + 1 => fillO (search q m) (selOpt (q m) (some m) none)
theorem search_some (q : Nat -> Bool) : forall m j, search q m = some j -> q j = true /\ j < m
| 0, _, h => by cases h
| m + 1, j, h => by
have h' : fillO (search q m) (selOpt (q m) (some m) none) = some j := h
cases e : search q m with
| some j0 =>
rw [e] at h'
have h2 : some j0 = some j := h'
have t := search_some q m j0 e
rw [(Option.some.inj h2).symm]
exact And.intro t.1 (Nat.lt_succ_of_lt t.2)
| none =>
rw [e] at h'
cases eq : q m with
| false => rw [eq] at h'; cases h'
| true =>
rw [eq] at h'
have h2 : some m = some j := h'
rw [(Option.some.inj h2).symm]
exact And.intro eq (Nat.lt_succ_self m)
theorem search_of (q : Nat -> Bool) (j : Nat) (hq : q j = true) : forall m, j < m -> Exists fun j' => search q m = some j'
| 0, h => absurd h (Nat.not_lt_zero j)
| m + 1, h => by
cases e : search q m with
| some j0 =>
refine Exists.intro j0 ?_
show fillO (search q m) _ = some j0
rw [e]; rfl
| none =>
cases Nat.lt_or_ge j m with
| inl l =>
cases search_of q j hq m l with
| intro j' hj' => rw [e] at hj'; cases hj'
| inr r =>
have ejm : j = m := Nat.le_antisymm (Nat.le_of_lt_succ h) r
rw [ejm] at hq
refine Exists.intro m ?_
show fillO (search q m) (selOpt (q m) (some m) none) = some m
rw [e, hq]; rfl
theorem rank_ex (P : Nat -> Bool) : forall m k, k < cntI P m -> Exists fun j => j < m /\ P j = true /\ cntI P j = k
| 0, k, h => absurd h (Nat.not_lt_zero k)
| m + 1, k, h => by
cases Nat.lt_or_ge k (cntI P m) with
| inl l =>
cases rank_ex P m k l with
| intro j hj => exact Exists.intro j (And.intro (Nat.lt_succ_of_lt hj.1) hj.2)
| inr r =>
have h' : k < cntI P m + bv (P m) := h
cases e : P m with
| false =>
rw [e] at h'
exact absurd (Nat.lt_of_lt_of_le (h' : k < cntI P m) r) (Nat.lt_irrefl k)
| true =>
rw [e] at h'
have ek : k = cntI P m := Nat.le_antisymm (Nat.le_of_lt_succ h') r
exact Exists.intro m (And.intro (Nat.lt_succ_self m) (And.intro e ek.symm))
theorem cntI_agree (g g' : Nat -> Bool) : forall k, (forall j, j < k -> g j = g' j) -> cntI g k = cntI g' k
| 0, _ => rfl
| k + 1, h => by
show cntI g k + bv (g k) = cntI g' k + bv (g' k)
rw [cntI_agree g g' k (fun j hj => h j (Nat.lt_succ_of_lt hj)), h k (Nat.lt_succ_self k)]
theorem cntI_mono (g : Nat -> Bool) : forall m' m, m <= m' -> cntI g m <= cntI g m'
| 0, m, h => by
have e : m = 0 := Found.Pnp66.le_zero_eq h
rw [e]; exact Nat.le_refl _
| m' + 1, m, h => by
cases Nat.lt_or_ge m' m with
| inl l =>
have e : m = m' + 1 := Nat.le_antisymm h l
rw [e]; exact Nat.le_refl _
| inr r => exact Nat.le_trans (cntI_mono g m' m r) (Nat.le_add_right _ _)
theorem cntI_off (g g' : Nat -> Bool) (p : Nat) (hp : g p = true) (hp' : g' p = false)
(he : forall j, Nat.beq j p = false -> g j = g' j) : forall k, p < k -> cntI g k = cntI g' k + 1
| 0, h => absurd h (Nat.not_lt_zero p)
| k + 1, h => by
show cntI g k + bv (g k) = cntI g' k + bv (g' k) + 1
cases Nat.lt_or_ge p k with
| inl l =>
rw [cntI_off g g' p hp hp' he k l, he k (Found.Pnp2.beq_false_of_ne k p (Nat.ne_of_lt l).symm)]
exact Nat.add_right_comm _ _ _
| inr r =>
have ek : k = p := Nat.le_antisymm r (Nat.le_of_lt_succ h)
rw [ek, hp, hp', cntI_agree g g' p (fun j hj => he j (Found.Pnp2.beq_false_of_ne j p (Nat.ne_of_lt hj)))]
rfl
theorem rank_lt (P : Nat -> Bool) (a b : Nat) (ha : P a = true) (hab : a < b) : cntI P a < cntI P b := by
have t : cntI P (a + 1) <= cntI P b := cntI_mono P b (a + 1) hab
have u : cntI P (a + 1) = cntI P a + 1 := by
show cntI P a + bv (P a) = cntI P a + 1
rw [ha]; rfl
rw [u] at t
exact t
/-- Free holes, remaining pigeons, remaining pigeons other than i. -/
def freeH (n : Nat) (rho : Nat -> Option Nat) (j : Nat) : Bool := !(anyI (fun a => optEq (rho a) j) (n + 1))
def remP (rho : Nat -> Option Nat) (a : Nat) : Bool := isNone (rho a)
def remX (rho : Nat -> Option Nat) (i a : Nat) : Bool := isNone (rho a) && !(Nat.beq a i)
def holeQ (n : Nat) (rho : Nat -> Option Nat) (i a : Nat) (j : Nat) : Bool :=
freeH n rho j && Nat.beq (cntI (freeH n rho) j) (cntI (remX rho i) a)
/-- The k-th remaining pigeon other than i goes to the k-th free hole. -/
def cmap (n : Nat) (rho : Nat -> Option Nat) (i : Nat) (a : Nat) : Option Nat :=
selOpt (Nat.beq a i) none (fillO (rho a) (search (holeQ n rho i a) n))
/-- rho is good: holes below n, injective, remaining at most free holes plus one. -/
structure Good (n : Nat) (rho : Nat -> Option Nat) : Prop where
rng : forall a j, a < n + 1 -> rho a = some j -> j < n
inj : forall a a' j, a < n + 1 -> a' < n + 1 -> rho a = some j -> rho a' = some j -> a = a'
cnt : cntI (remP rho) (n + 1) <= cntI (freeH n rho) n + 1
theorem freeH_not (n : Nat) (rho : Nat -> Option Nat) (j : Nat) (h : freeH n rho j = true) (a : Nat) (ha : a < n + 1)
(hr : rho a = some j) : False := by
have t := anyI_false _ (n + 1) (not_true h) a ha
rw [optEq_of (rho a) j hr] at t
exact Bool.noConfusion t
theorem cmap_val (n : Nat) (rho : Nat -> Option Nat) (i a c : Nat) (hai : Nat.beq a i = false)
(h : cmap n rho i a = some c) :
rho a = some c \/ (rho a = none /\ freeH n rho c = true /\ cntI (freeH n rho) c = cntI (remX rho i) a /\ c < n) := by
have h' : selOpt (Nat.beq a i) none (fillO (rho a) (search (holeQ n rho i a) n)) = some c := h
rw [hai] at h'
have h2 : fillO (rho a) (search (holeQ n rho i a) n) = some c := h'
cases er : rho a with
| some j =>
rw [er] at h2
have h3 : some j = some c := h2
rw [Option.some.inj h3]
exact Or.inl rfl
| none =>
rw [er] at h2
have t := search_some _ n c h2
exact Or.inr (And.intro rfl (And.intro (Found.Pnp62.and_l t.1)
(And.intro (Found.Pnp2.beq_eq _ _ (Found.Pnp62.and_r t.1)) t.2)))
theorem remX_true (rho : Nat -> Option Nat) (i a : Nat) (hai : Nat.beq a i = false) (hr : rho a = none) :
remX rho i a = true := by
show (isNone (rho a) && !(Nat.beq a i)) = true
rw [hr, hai]; rfl
theorem rem_count (n : Nat) (rho : Nat -> Option Nat) (i : Nat) (hi : i < n + 1) (hr : rho i = none) :
cntI (remP rho) (n + 1) = cntI (remX rho i) (n + 1) + 1 := by
refine cntI_off _ _ i ?_ ?_ ?_ (n + 1) hi
· show isNone (rho i) = true
rw [hr]; rfl
· show (isNone (rho i) && !(Nat.beq i i)) = false
rw [Found.Pnp62.beq_refl]
cases isNone (rho i) <;> rfl
· intro j hj
show isNone (rho j) = (isNone (rho j) && !(Nat.beq j i))
rw [hj]
cases isNone (rho j) <;> rfl
theorem crit_exists (n : Nat) (rho : Nat -> Option Nat) (hg : Good n rho) (i : Nat) (hi : i < n + 1)
(hr : rho i = none) : Crit n rho i (cmap n rho i) := by
have gi : cmap n rho i i = none := by
show selOpt (Nat.beq i i) none _ = none
rw [Found.Pnp62.beq_refl]; rfl
have other : forall a, Nat.beq a i = false -> cmap n rho i a = fillO (rho a) (search (holeQ n rho i a) n) := by
intro a hai
show selOpt (Nat.beq a i) none _ = _
rw [hai]; rfl
refine Crit.mk gi hr hi ?_ ?_ ?_ ?_
· intro a ha hai hra
have rx := remX_true rho i a hai hra
have k1 : cntI (remX rho i) a < cntI (remX rho i) (n + 1) := rank_lt _ a (n + 1) rx ha
have k2 : cntI (remX rho i) (n + 1) <= cntI (freeH n rho) n := by
have c := hg.cnt
rw [rem_count n rho i hi hr] at c
exact Nat.le_of_succ_le_succ c
cases rank_ex (freeH n rho) n _ (Nat.lt_of_lt_of_le k1 k2) with
| intro j0 hj0 =>
have q0 : holeQ n rho i a j0 = true := by
show (freeH n rho j0 && Nat.beq (cntI (freeH n rho) j0) (cntI (remX rho i) a)) = true
rw [hj0.2.1, hj0.2.2, Found.Pnp62.beq_refl]; rfl
cases search_of _ j0 q0 n hj0.1 with
| intro j' hj' =>
refine Exists.intro j' (And.intro ?_ (search_some _ n j' hj').2)
rw [other a hai, hra]
exact hj'
· intro a j ha hai hra
rw [other a hai, hra]; rfl
· intro a b c ha hb h1 h2
cases ea : Nat.beq a i with
| true => rw [Found.Pnp2.beq_eq a i ea, gi] at h1; cases h1
| false =>
cases eb : Nat.beq b i with
| true => rw [Found.Pnp2.beq_eq b i eb, gi] at h2; cases h2
| false =>
cases cmap_val n rho i a c ea h1 with
| inl va =>
cases cmap_val n rho i b c eb h2 with
| inl vb => exact hg.inj a b c ha hb va vb
| inr vb => exact absurd (freeH_not n rho c vb.2.1 a ha va) (fun x => x)
| inr va =>
cases cmap_val n rho i b c eb h2 with
| inl vb => exact absurd (freeH_not n rho c va.2.1 b hb vb) (fun x => x)
| inr vb =>
have ek : cntI (remX rho i) a = cntI (remX rho i) b := va.2.2.1.symm.trans vb.2.2.1
cases Nat.lt_or_ge a b with
| inl l =>
have t := rank_lt _ a b (remX_true rho i a ea va.1) l
rw [ek] at t
exact absurd t (Nat.lt_irrefl _)
| inr r =>
cases Nat.lt_or_ge b a with
| inl l =>
have t := rank_lt _ b a (remX_true rho i b eb vb.1) l
rw [ek] at t
exact absurd t (Nat.lt_irrefl _)
| inr r' => exact Nat.le_antisymm r' r
· intro a c ha hac
cases ea : Nat.beq a i with
| true => rw [Found.Pnp2.beq_eq a i ea, gi] at hac; cases hac
| false =>
cases cmap_val n rho i a c ea hac with
| inl va => exact hg.rng a c ha va
| inr va => exact va.2.2.2
/-- The empty clause loses exactly the remaining pigeons. -/
theorem cntP_nil (n : Nat) (rho : Nat -> Option Nat) (hg : Good n rho) :
cntP n rho [] = cntI (remP rho) (n + 1) := by
apply Nat.le_antisymm
· refine Found.Pnp65.cntI_le _ _ (n + 1) ?_
intro j _ h
cases pigs_sound n rho [] j h with
| intro g hg' =>
show isNone (rho j) = true
rw [hg'.1.ri]; rfl
· refine Found.Pnp65.cntI_le _ _ (n + 1) ?_
intro j hj h
have hr : rho j = none := isNone_eq _ h
exact pigs_complete n rho [] j _ (crit_exists n rho hg j hj hr) rfl
end Found.Pnp94
#print axioms Found.Pnp94.search_some
#print axioms Found.Pnp94.search_of
#print axioms Found.Pnp94.rank_ex
#print axioms Found.Pnp94.cntI_agree
#print axioms Found.Pnp94.cntI_mono
#print axioms Found.Pnp94.cntI_off
#print axioms Found.Pnp94.rank_lt
#print axioms Found.Pnp94.freeH_not
#print axioms Found.Pnp94.cmap_val
#print axioms Found.Pnp94.remX_true
#print axioms Found.Pnp94.rem_count
#print axioms Found.Pnp94.crit_exists
#print axioms Found.Pnp94.cntP_nil
#print axioms: 13 theorems, no axioms.
A clause that loses \(s\) pigeons and keeps \(u\) remaining pigeons covers at least \(su\) pairs of a remaining pigeon and a free hole.
found/Pnp95.lean
/-
T330 piece 95. found/Pnp95 -- a clause that loses s pigeons and keeps u remaining pigeons
covers at least s * u (pigeon, free hole) pairs (proof complexity ladder, step 1: general
resolution; the width step of Beame--Pitassi).
covB n C i j: C holds x(i, j), or holds not x(a, j) for some pigeon a other than i (the
monotone form of C names the pair (i, j)). covCnt n rho C counts the pairs with i remaining
and j a free hole. Proved:
* inj_count: an injective map from a counted set into another counts at most the second;
* cntI_split: a count splits along any Bool test;
* sumI_ge: a sum of rows each at least m over a set of size c is at least m * c;
* row_ge: a pigeon the clause loses has a row of at least u covered free holes;
* width: cntP C * u <= covCnt C, where u counts the remaining pigeons C keeps;
* split_rem: cntP C + u = the number of remaining pigeons.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp94
namespace Found.Pnp95
open Found.RootComp (Clause evalClause)
open Found.Pnp62 (memL)
open Found.Pnp65 (var cntI bv anyI anyI_true)
open Found.Pnp87 (optEq asgP)
open Found.Pnp90 (Crit isNone isNone_eq)
open Found.Pnp91 (pigsB pigs_sound)
open Found.Pnp92 (cov_of_pigs)
open Found.Pnp93 (cntP add4)
open Found.Pnp94 (freeH remP Good cntI_off cntI_agree)
def covB (n : Nat) (C : Clause) (i j : Nat) : Bool :=
memL (true, var n i j) C || anyI (fun a => !(Nat.beq a i) && memL (false, var n a j) C) (n + 1)
def sumI (h : Nat -> Nat) : Nat -> Nat
| 0 => 0
| k + 1 => sumI h k + h k
def rowC (n : Nat) (rho : Nat -> Option Nat) (C : Clause) (i : Nat) : Nat :=
cntI (fun j => remP rho i && freeH n rho j && covB n C i j) n
def covCnt (n : Nat) (rho : Nat -> Option Nat) (C : Clause) : Nat := sumI (rowC n rho C) (n + 1)
/-- Remaining pigeons the clause keeps. -/
def nonP (n : Nat) (rho : Nat -> Option Nat) (C : Clause) (a : Nat) : Bool := remP rho a && !(pigsB n rho C a)
def optGet : Option Nat -> Nat
| none => 0
| some j => j
theorem inj_count (A : Nat -> Bool) (f : Nat -> Nat) (M : Nat) :
forall N (B : Nat -> Bool), (forall x, x < N -> A x = true -> f x < M /\ B (f x) = true) ->
(forall x y, x < N -> y < N -> A x = true -> A y = true -> f x = f y -> x = y) ->
cntI A N <= cntI B M
| 0, _, _, _ => Nat.zero_le _
| N + 1, B, hm, hi => by
show cntI A N + bv (A N) <= cntI B M
cases eN : A N with
| false =>
exact inj_count A f M N B (fun x hx => hm x (Nat.lt_succ_of_lt hx))
(fun x y hx hy => hi x y (Nat.lt_succ_of_lt hx) (Nat.lt_succ_of_lt hy))
| true =>
have pN := hm N (Nat.lt_succ_self N) eN
let B' : Nat -> Bool := fun y => B y && !(Nat.beq y (f N))
have ih : cntI A N <= cntI B' M := by
refine inj_count A f M N B' ?_ (fun x y hx hy => hi x y (Nat.lt_succ_of_lt hx) (Nat.lt_succ_of_lt hy))
intro x hx hA
have px := hm x (Nat.lt_succ_of_lt hx) hA
refine And.intro px.1 ?_
show (B (f x) && !(Nat.beq (f x) (f N))) = true
have ne : Nat.beq (f x) (f N) = false := Found.Pnp2.beq_false_of_ne _ _ (fun e => by
have t := hi x N (Nat.lt_succ_of_lt hx) (Nat.lt_succ_self N) hA eN e
rw [t] at hx
exact Nat.lt_irrefl N hx)
rw [px.2, ne]; rfl
have off : cntI B M = cntI B' M + 1 := by
refine cntI_off B B' (f N) pN.2 ?_ ?_ M pN.1
· show (B (f N) && !(Nat.beq (f N) (f N))) = false
rw [Found.Pnp62.beq_refl]
cases B (f N) <;> rfl
· intro j hj
show B j = (B j && !(Nat.beq j (f N)))
rw [hj]
cases B j <;> rfl
rw [off]
exact Nat.succ_le_succ ih
theorem cntI_split (g h : Nat -> Bool) : forall k,
cntI g k = cntI (fun j => g j && h j) k + cntI (fun j => g j && !(h j)) k
| 0 => rfl
| k + 1 => by
show cntI g k + bv (g k) = (cntI (fun j => g j && h j) k + bv (g k && h k)) + (cntI (fun j => g j && !(h j)) k + bv (g k && !(h k)))
rw [cntI_split g h k, add4 (cntI _ k) (bv _) (cntI _ k) (bv _)]
cases g k <;> cases h k <;> rfl
theorem sumI_ge (h : Nat -> Nat) (P : Nat -> Bool) (m : Nat) : forall k,
(forall i, i < k -> P i = true -> m <= h i) -> m * cntI P k <= sumI h k
| 0, _ => Nat.le_refl 0
| k + 1, hh => by
have ih := sumI_ge h P m k (fun i hi => hh i (Nat.lt_succ_of_lt hi))
show m * (cntI P k + bv (P k)) <= sumI h k + h k
cases e : P k with
| false => exact Nat.le_trans ih (Nat.le_add_right _ _)
| true => exact Nat.add_le_add ih (hh k (Nat.lt_succ_self k) e)
theorem free_of_crit (n : Nat) (rho : Nat -> Option Nat) (i a j : Nat) (g : Nat -> Option Nat) (hc : Crit n rho i g)
(ha : a < n + 1) (hr : rho a = none) (hj : g a = some j) : freeH n rho j = true := by
have hz : anyI (fun b => optEq (rho b) j) (n + 1) = false := by
refine Found.Pnp66.anyI_none _ (n + 1) ?_
intro b hb
cases eb : rho b with
| none => rfl
| some c =>
show Nat.beq c j = false
cases ec : Nat.beq c j with
| false => rfl
| true =>
have cj : c = j := Found.Pnp2.beq_eq c j ec
rw [cj] at eb
have bi : Nat.beq b i = false := Found.Pnp2.beq_false_of_ne b i (fun e => by
rw [e, hc.ri] at eb; cases eb)
have gb := hc.fixed b j hb bi eb
have t := hc.inj b a j hb ha gb hj
rw [t, hr] at eb
cases eb
show (!(anyI (fun b => optEq (rho b) j) (n + 1))) = true
rw [hz]; rfl
theorem row_ge (n : Nat) (rho : Nat -> Option Nat) (C : Clause) (i : Nat) (hp : pigsB n rho C i = true) :
cntI (nonP n rho C) (n + 1) <= rowC n rho C i := by
cases pigs_sound n rho C i hp with
| intro g hg =>
have hc := hg.1
have gsome : forall a, a < n + 1 -> nonP n rho C a = true ->
rho a = none /\ pigsB n rho C a = false /\ Nat.beq a i = false /\ Exists fun j => g a = some j /\ j < n := by
intro a ha hA
have r : rho a = none := isNone_eq _ (Found.Pnp62.and_l hA)
have np : pigsB n rho C a = false := Found.Pnp91.not_true (Found.Pnp62.and_r hA)
have ai : Nat.beq a i = false := Found.Pnp2.beq_false_of_ne a i (fun e => by
rw [e, hp] at np; cases np)
exact And.intro r (And.intro np (And.intro ai (hc.free a ha ai r)))
refine inj_count _ (fun a => optGet (g a)) n (n + 1) _ ?_ ?_
· intro a ha hA
have s := gsome a ha hA
cases s.2.2.2 with
| intro j hj =>
show optGet (g a) < n /\ (remP rho i && freeH n rho (optGet (g a)) && covB n C i (optGet (g a))) = true
rw [hj.1]
refine And.intro hj.2 ?_
have ri : remP rho i = true := by show isNone (rho i) = true; rw [hc.ri]; rfl
have fr := free_of_crit n rho i a j g hc ha s.1 hj.1
have cv : covB n C i j = true := by
cases cov_of_pigs n rho C i a j g hc hg.2 ha s.2.2.1 s.1 hj.1 s.2.1 with
| inl h1 => exact Found.Pnp62.or_l h1
| inr h2 =>
apply Found.Pnp62.or_r
refine anyI_true _ a ?_ (n + 1) ha
show (!(Nat.beq a i) && memL (false, var n a j) C) = true
rw [s.2.2.1, h2]; rfl
show (remP rho i && freeH n rho j && covB n C i j) = true
rw [ri, fr, cv]; rfl
· intro x y hx hy hAx hAy e
cases (gsome x hx hAx).2.2.2 with
| intro jx hjx =>
cases (gsome y hy hAy).2.2.2 with
| intro jy hjy =>
have e' : optGet (g x) = optGet (g y) := e
rw [hjx.1, hjy.1] at e'
have e2 : jx = jy := e'
rw [e2] at hjx
exact hc.inj x y jy hx hy hjx.1 hjy.1
theorem width (n : Nat) (rho : Nat -> Option Nat) (C : Clause) :
cntP n rho C * cntI (nonP n rho C) (n + 1) <= covCnt n rho C := by
have t := sumI_ge (rowC n rho C) (pigsB n rho C) (cntI (nonP n rho C) (n + 1)) (n + 1)
(fun i _ hp => row_ge n rho C i hp)
rw [Nat.mul_comm]
exact t
theorem split_rem (n : Nat) (rho : Nat -> Option Nat) (C : Clause) :
cntP n rho C + cntI (nonP n rho C) (n + 1) = cntI (remP rho) (n + 1) := by
rw [cntI_split (remP rho) (pigsB n rho C) (n + 1)]
have e : cntI (fun j => remP rho j && pigsB n rho C j) (n + 1) = cntP n rho C := by
refine cntI_agree _ _ (n + 1) ?_
intro j _
cases ep : pigsB n rho C j with
| false => show (remP rho j && false) = false; cases remP rho j <;> rfl
| true =>
cases pigs_sound n rho C j ep with
| intro g hg =>
show (isNone (rho j) && true) = true
rw [hg.1.ri]; rfl
rw [e]
rfl
end Found.Pnp95
#print axioms Found.Pnp95.inj_count
#print axioms Found.Pnp95.cntI_split
#print axioms Found.Pnp95.sumI_ge
#print axioms Found.Pnp95.free_of_crit
#print axioms Found.Pnp95.row_ge
#print axioms Found.Pnp95.width
#print axioms Found.Pnp95.split_rem
#print axioms: 7 theorems, no axioms.
Fixing a remaining pigeon in a free hole keeps the restriction good, removes one pigeon and one hole, loses no new pigeon, and kills every clause covering that pair.
found/Pnp96.lean
/-
T330 piece 96. found/Pnp96 -- extending a restriction by one pigeon-hole pair (proof
complexity ladder, step 1: general resolution; the restriction step of Beame--Pitassi).
ext rho i j fixes the remaining pigeon i in the free hole j. Proved:
* anyI_ex: a true bounded disjunction has a witness;
* good_ext, rem_ext, free_ext: ext keeps rho good and removes one remaining pigeon and
one free hole;
* crit_back, pigs_back, cntP_back: a map critical for ext rho i j is critical for rho, so
a clause loses no new pigeon;
* kill: a clause covering (i, j) loses no pigeon under ext rho i j;
* covCnt_back: the covered pair count does not grow.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp95
namespace Found.Pnp96
open Found.RootComp (Clause evalClause evalLit)
open Found.Pnp62 (memL of_mem)
open Found.Pnp65 (var cntI bv anyI anyI_true anyI_false anyI_congr cntI_le cntI_zero)
open Found.Pnp87 (optEq optEq_of optEq_some asgP asg_var)
open Found.Pnp88 (selOpt)
open Found.Pnp90 (Crit isNone isNone_eq)
open Found.Pnp91 (pigsB pigs_sound pigs_complete not_true)
open Found.Pnp93 (cntP)
open Found.Pnp94 (freeH remP Good cntI_off cntI_agree freeH_not)
open Found.Pnp95 (covB sumI rowC covCnt)
def ext (rho : Nat -> Option Nat) (i j : Nat) : Nat -> Option Nat :=
fun a => selOpt (Nat.beq a i) (some j) (rho a)
theorem ext_i (rho : Nat -> Option Nat) (i j : Nat) : ext rho i j i = some j := by
show selOpt (Nat.beq i i) (some j) (rho i) = some j
rw [Found.Pnp62.beq_refl]; rfl
theorem ext_o (rho : Nat -> Option Nat) (i j a : Nat) (h : Nat.beq a i = false) : ext rho i j a = rho a := by
show selOpt (Nat.beq a i) (some j) (rho a) = rho a
rw [h]; rfl
theorem anyI_ex (g : Nat -> Bool) : forall k, anyI g k = true -> Exists fun j => j < k /\ g j = true
| 0, h => Bool.noConfusion h
| k + 1, h => by
have h' : (anyI g k || g k) = true := h
cases e : anyI g k with
| true =>
cases anyI_ex g k e with
| intro j hj => exact Exists.intro j (And.intro (Nat.lt_succ_of_lt hj.1) hj.2)
| false =>
rw [e] at h'
exact Exists.intro k (And.intro (Nat.lt_succ_self k) h')
theorem ne_of_rho (rho : Nat -> Option Nat) (i a c : Nat) (hr : rho i = none) (ha : rho a = some c) :
Nat.beq a i = false :=
Found.Pnp2.beq_false_of_ne a i (fun e => by rw [e, hr] at ha; cases ha)
/-- Free holes under ext: all those of rho but j. -/
theorem freeH_ext (n : Nat) (rho : Nat -> Option Nat) (i j : Nat) (hr : rho i = none) (j' : Nat)
(hj : Nat.beq j' j = false) : freeH n (ext rho i j) j' = freeH n rho j' := by
have t : anyI (fun a => optEq (ext rho i j a) j') (n + 1) = anyI (fun a => optEq (rho a) j') (n + 1) := by
refine anyI_congr _ _ (n + 1) ?_
intro a _
show optEq (ext rho i j a) j' = optEq (rho a) j'
cases ea : Nat.beq a i with
| false => rw [ext_o rho i j a ea]
| true =>
rw [Found.Pnp2.beq_eq a i ea, ext_i, hr]
exact Found.Pnp70.nsym j' j hj
show (!(anyI (fun a => optEq (ext rho i j a) j') (n + 1))) = (!(anyI (fun a => optEq (rho a) j') (n + 1)))
rw [t]
theorem freeH_ext_j (n : Nat) (rho : Nat -> Option Nat) (i j : Nat) (hi : i < n + 1) :
freeH n (ext rho i j) j = false := by
have t : anyI (fun a => optEq (ext rho i j a) j) (n + 1) = true := by
refine anyI_true _ i ?_ (n + 1) hi
show optEq (ext rho i j i) j = true
rw [ext_i]; exact optEq_of _ j rfl
show (!(anyI (fun a => optEq (ext rho i j a) j) (n + 1))) = false
rw [t]; rfl
theorem rem_ext (n : Nat) (rho : Nat -> Option Nat) (i j : Nat) (hi : i < n + 1) (hr : rho i = none) :
cntI (remP rho) (n + 1) = cntI (remP (ext rho i j)) (n + 1) + 1 := by
refine cntI_off _ _ i ?_ ?_ ?_ (n + 1) hi
· show isNone (rho i) = true
rw [hr]; rfl
· show isNone (ext rho i j i) = false
rw [ext_i]; rfl
· intro a ha
show isNone (rho a) = isNone (ext rho i j a)
rw [ext_o rho i j a ha]
theorem free_ext (n : Nat) (rho : Nat -> Option Nat) (i j : Nat) (hi : i < n + 1) (hr : rho i = none) (hj : j < n)
(hf : freeH n rho j = true) : cntI (freeH n rho) n = cntI (freeH n (ext rho i j)) n + 1 :=
cntI_off _ _ j hf (freeH_ext_j n rho i j hi) (fun j' h => (freeH_ext n rho i j hr j' h).symm) n hj
theorem good_ext (n : Nat) (rho : Nat -> Option Nat) (hg : Good n rho) (i j : Nat) (hi : i < n + 1)
(hr : rho i = none) (hj : j < n) (hf : freeH n rho j = true) : Good n (ext rho i j) := by
refine Good.mk ?_ ?_ ?_
· intro a c ha h
cases ea : Nat.beq a i with
| true =>
rw [Found.Pnp2.beq_eq a i ea, ext_i] at h
rw [(Option.some.inj h).symm]; exact hj
| false =>
rw [ext_o rho i j a ea] at h
exact hg.rng a c ha h
· intro a a' c ha ha' h h'
cases ea : Nat.beq a i with
| true =>
cases ea' : Nat.beq a' i with
| true => rw [Found.Pnp2.beq_eq a i ea, Found.Pnp2.beq_eq a' i ea']
| false =>
rw [Found.Pnp2.beq_eq a i ea, ext_i] at h
rw [ext_o rho i j a' ea', (Option.some.inj h).symm] at h'
exact absurd (freeH_not n rho j hf a' ha' h') (fun x => x)
| false =>
cases ea' : Nat.beq a' i with
| true =>
rw [Found.Pnp2.beq_eq a' i ea', ext_i] at h'
rw [ext_o rho i j a ea, (Option.some.inj h').symm] at h
exact absurd (freeH_not n rho j hf a ha h) (fun x => x)
| false =>
rw [ext_o rho i j a ea] at h
rw [ext_o rho i j a' ea'] at h'
exact hg.inj a a' c ha ha' h h'
· have c := hg.cnt
rw [rem_ext n rho i j hi hr, free_ext n rho i j hi hr hj hf] at c
exact Nat.le_of_succ_le_succ c
theorem crit_back (n : Nat) (rho : Nat -> Option Nat) (i j : Nat) (hr : rho i = none) (a : Nat)
(g : Nat -> Option Nat) (hc : Crit n (ext rho i j) a g) : Crit n rho a g := by
have ai : Nat.beq a i = false := by
cases ea : Nat.beq a i with
| false => rfl
| true =>
have t := hc.ri
rw [Found.Pnp2.beq_eq a i ea, ext_i] at t
cases t
refine Crit.mk hc.gi ?_ hc.il ?_ ?_ hc.inj hc.rng
· have t := hc.ri
rw [ext_o rho i j a ai] at t
exact t
· intro b hb hba hrb
cases eb : Nat.beq b i with
| true =>
have bi := Found.Pnp2.beq_eq b i eb
have gb := hc.fixed b j hb hba (by rw [bi]; exact ext_i rho i j)
exact Exists.intro j (And.intro gb (hc.rng b j hb gb))
| false =>
exact hc.free b hb hba (by rw [ext_o rho i j b eb]; exact hrb)
· intro b c hb hba hrb
exact hc.fixed b c hb hba (by rw [ext_o rho i j b (ne_of_rho rho i b c hr hrb)]; exact hrb)
theorem pigs_back (n : Nat) (rho : Nat -> Option Nat) (i j : Nat) (hr : rho i = none) (C : Clause) (a : Nat)
(h : pigsB n (ext rho i j) C a = true) : pigsB n rho C a = true :=
match pigs_sound n (ext rho i j) C a h with
| Exists.intro g hg => pigs_complete n rho C a g (crit_back n rho i j hr a g hg.1) hg.2
theorem cntP_back (n : Nat) (rho : Nat -> Option Nat) (i j : Nat) (hr : rho i = none) (C : Clause) :
cntP n (ext rho i j) C <= cntP n rho C :=
cntI_le _ _ (n + 1) (fun a _ h => pigs_back n rho i j hr C a h)
theorem kill (n : Nat) (rho : Nat -> Option Nat) (i j : Nat) (hi : i < n + 1) (hj : j < n) (C : Clause)
(hcov : covB n C i j = true) (a : Nat) : pigsB n (ext rho i j) C a = false := by
cases e : pigsB n (ext rho i j) C a with
| false => rfl
| true =>
cases pigs_sound n (ext rho i j) C a e with
| intro g hg =>
have hc := hg.1
have ai : Nat.beq a i = false := by
cases ea : Nat.beq a i with
| false => rfl
| true =>
have t := hc.ri
rw [Found.Pnp2.beq_eq a i ea, ext_i] at t
cases t
have gi : g i = some j := hc.fixed i j hi (Found.Pnp70.nsym a i ai) (ext_i rho i j)
have tr : evalClause (asgP n g) C = true := by
cases e1 : memL (true, var n i j) C with
| true =>
refine of_mem _ (true, var n i j) ?_ C e1
show asgP n g (var n i j) = true
rw [asg_var n g i j hi hj, gi]
exact optEq_of _ j rfl
| false =>
have h2 : (memL (true, var n i j) C || anyI (fun b => !(Nat.beq b i) && memL (false, var n b j) C) (n + 1)) = true := hcov
rw [e1] at h2
have h3 : anyI (fun b => !(Nat.beq b i) && memL (false, var n b j) C) (n + 1) = true := h2
cases anyI_ex _ (n + 1) h3 with
| intro a' ha' =>
have nb : Nat.beq a' i = false := not_true (Found.Pnp62.and_l ha'.2)
refine of_mem _ (false, var n a' j) ?_ C (Found.Pnp62.and_r ha'.2)
show (!(asgP n g (var n a' j))) = true
rw [asg_var n g a' j ha'.1 hj]
have o : optEq (g a') j = false := by
cases eg : g a' with
| none => rfl
| some c =>
show Nat.beq c j = false
cases ec : Nat.beq c j with
| false => rfl
| true =>
rw [Found.Pnp2.beq_eq c j ec] at eg
have t := hc.inj a' i j ha'.1 hi eg gi
rw [t, Found.Pnp62.beq_refl] at nb
exact Bool.noConfusion nb
rw [o]; rfl
rw [hg.2] at tr
exact Bool.noConfusion tr
theorem cntP_kill (n : Nat) (rho : Nat -> Option Nat) (i j : Nat) (hi : i < n + 1) (hj : j < n) (C : Clause)
(hcov : covB n C i j = true) : cntP n (ext rho i j) C = 0 :=
cntI_zero _ (n + 1) (fun a _ => kill n rho i j hi hj C hcov a)
theorem sumI_le (h h' : Nat -> Nat) : forall k, (forall x, x < k -> h x <= h' x) -> sumI h k <= sumI h' k
| 0, _ => Nat.le_refl 0
| k + 1, hh => Nat.add_le_add (sumI_le h h' k (fun x hx => hh x (Nat.lt_succ_of_lt hx))) (hh k (Nat.lt_succ_self k))
theorem covCnt_back (n : Nat) (rho : Nat -> Option Nat) (i j : Nat) (hi : i < n + 1) (hr : rho i = none) (C : Clause) :
covCnt n (ext rho i j) C <= covCnt n rho C := by
refine sumI_le _ _ (n + 1) ?_
intro a _
refine cntI_le _ _ n ?_
intro j' _ h
have h1 := Found.Pnp62.and_l h
have rm : remP (ext rho i j) a = true := Found.Pnp62.and_l h1
have fr : freeH n (ext rho i j) j' = true := Found.Pnp62.and_r h1
have cv : covB n C a j' = true := Found.Pnp62.and_r h
have ea : ext rho i j a = none := isNone_eq _ rm
have ai : Nat.beq a i = false := by
cases e : Nat.beq a i with
| false => rfl
| true => rw [Found.Pnp2.beq_eq a i e, ext_i] at ea; cases ea
have rm' : remP rho a = true := by
show isNone (rho a) = true
rw [(ext_o rho i j a ai).symm, ea]; rfl
have jj : Nat.beq j' j = false := by
cases e : Nat.beq j' j with
| false => rfl
| true =>
rw [Found.Pnp2.beq_eq j' j e, freeH_ext_j n rho i j hi] at fr
exact Bool.noConfusion fr
have fr' : freeH n rho j' = true := by rw [(freeH_ext n rho i j hr j' jj).symm]; exact fr
show (remP rho a && freeH n rho j' && covB n C a j') = true
rw [rm', fr', cv]; rfl
end Found.Pnp96
#print axioms Found.Pnp96.ext_i
#print axioms Found.Pnp96.ext_o
#print axioms Found.Pnp96.anyI_ex
#print axioms Found.Pnp96.ne_of_rho
#print axioms Found.Pnp96.freeH_ext
#print axioms Found.Pnp96.freeH_ext_j
#print axioms Found.Pnp96.rem_ext
#print axioms Found.Pnp96.free_ext
#print axioms Found.Pnp96.good_ext
#print axioms Found.Pnp96.crit_back
#print axioms Found.Pnp96.pigs_back
#print axioms Found.Pnp96.cntP_back
#print axioms Found.Pnp96.kill
#print axioms Found.Pnp96.cntP_kill
#print axioms Found.Pnp96.sumI_le
#print axioms Found.Pnp96.covCnt_back
#print axioms: 16 theorems, no axioms.
Some free pair is covered by at least a \(1/(|\mathrm {free}|\cdot |\mathrm {rem}|)\) share of the total covered pair count of a list of clauses.
found/Pnp97.lean
/-
T330 piece 97. found/Pnp97 -- averaging: some free pair is covered by many clauses of a
list (proof complexity ladder, step 1: general resolution; the averaging step of
Beame--Pitassi).
For a list Ws of clauses with total covered pair count T = sum of covCnt over Ws, if T > 0
there is a remaining pigeon i and a free hole j such that
T <= (number of clauses of Ws covering (i, j)) * (free holes) * (remaining pigeons).
The pair is found by a computed arg-max (no choice). Proved:
* sumI_zero, sumI_add, sumI_ext, cntI_sumI: sum tools;
* swap: a list sum of bounded sums is the bounded sum of list sums;
* amax_ge, amax_succ_lt: the computed arg-max;
* sum_le: a sum is at most its maximum times the size of its support;
* avg: the averaging step.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp96
namespace Found.Pnp97
open Found.RootComp (Clause)
open Found.Pnp65 (cntI bv selN)
open Found.Pnp93 (add4)
open Found.Pnp94 (freeH remP)
open Found.Pnp95 (covB sumI rowC covCnt)
open Found.Pnp96 (sumI_le)
def lsum (h : Clause -> Nat) : List Clause -> Nat
| [] => 0
| D :: L => h D + lsum h L
def lcnt (p : Clause -> Bool) (L : List Clause) : Nat := lsum (fun D => bv (p D)) L
theorem sumI_zero : forall k, sumI (fun _ => 0) k = 0
| 0 => rfl
| k + 1 => sumI_zero k
theorem sumI_add (a b : Nat -> Nat) : forall k, sumI (fun x => a x + b x) k = sumI a k + sumI b k
| 0 => rfl
| k + 1 => by
show sumI (fun x => a x + b x) k + (a k + b k) = (sumI a k + a k) + (sumI b k + b k)
rw [sumI_add a b k]
exact add4 _ _ _ _
theorem sumI_ext (h h' : Nat -> Nat) (he : forall x, h x = h' x) : forall k, sumI h k = sumI h' k
| 0 => rfl
| k + 1 => by
show sumI h k + h k = sumI h' k + h' k
rw [sumI_ext h h' he k, he k]
theorem lsum_ext (h h' : Clause -> Nat) (he : forall D, h D = h' D) : forall L, lsum h L = lsum h' L
| [] => rfl
| D :: L => by
show h D + lsum h L = h' D + lsum h' L
rw [he D, lsum_ext h h' he L]
theorem lsum_zero (h : Clause -> Nat) (hz : forall D, h D = 0) : forall L, lsum h L = 0
| [] => rfl
| D :: L => by
show h D + lsum h L = 0
rw [hz D, lsum_zero h hz L]
theorem cntI_sumI (g : Nat -> Bool) : forall k, cntI g k = sumI (fun j => bv (g j)) k
| 0 => rfl
| k + 1 => by
show cntI g k + bv (g k) = sumI (fun j => bv (g j)) k + bv (g k)
rw [cntI_sumI g k]
theorem swap (f : Clause -> Nat -> Nat) (k : Nat) : forall L,
lsum (fun D => sumI (f D) k) L = sumI (fun i => lsum (fun D => f D i) L) k
| [] => (sumI_zero k).symm
| D :: L => by
show sumI (f D) k + lsum (fun D => sumI (f D) k) L = sumI (fun i => f D i + lsum (fun D => f D i) L) k
rw [swap f k L]
exact (sumI_add _ _ k).symm
theorem zero_mul' : forall m : Nat, 0 * m = 0
| 0 => rfl
| m + 1 => zero_mul' m
def amax (h : Nat -> Nat) : Nat -> Nat
| 0 => 0
| k + 1 => selN (Nat.blt (h (amax h k)) (h k)) k (amax h k)
theorem amax_le (h : Nat -> Nat) : forall k, amax h k <= k
| 0 => Nat.le_refl 0
| k + 1 => by
show selN (Nat.blt (h (amax h k)) (h k)) k (amax h k) <= k + 1
cases Nat.blt (h (amax h k)) (h k) with
| true => exact Nat.le_succ k
| false => exact Nat.le_trans (amax_le h k) (Nat.le_succ k)
theorem amax_succ_lt (h : Nat -> Nat) (k : Nat) : amax h (k + 1) < k + 1 := by
show selN (Nat.blt (h (amax h k)) (h k)) k (amax h k) < k + 1
cases Nat.blt (h (amax h k)) (h k) with
| true => exact Nat.lt_succ_self k
| false => exact Nat.lt_succ_of_le (amax_le h k)
theorem amax_ge (h : Nat -> Nat) : forall k x, x < k -> h x <= h (amax h k)
| 0, x, hx => absurd hx (Nat.not_lt_zero x)
| k + 1, x, hx => by
show h x <= h (selN (Nat.blt (h (amax h k)) (h k)) k (amax h k))
cases e : Nat.blt (h (amax h k)) (h k) with
| true =>
have l := Found.Pnp90.blt_lt _ _ e
show h x <= h k
cases Nat.lt_or_ge x k with
| inl lx => exact Nat.le_trans (amax_ge h k x lx) (Nat.le_of_lt l)
| inr rx =>
have ex : x = k := Nat.le_antisymm (Nat.le_of_lt_succ hx) rx
rw [ex]; exact Nat.le_refl _
| false =>
show h x <= h (amax h k)
cases Nat.lt_or_ge x k with
| inl lx => exact amax_ge h k x lx
| inr rx =>
have ex : x = k := Nat.le_antisymm (Nat.le_of_lt_succ hx) rx
rw [ex]
cases Nat.lt_or_ge (h (amax h k)) (h k) with
| inl l => rw [Found.Pnp90.lt_blt _ _ l] at e; exact Bool.noConfusion e
| inr r => exact r
theorem sum_le (h : Nat -> Nat) (P : Nat -> Bool) (M : Nat) : forall k,
(forall x, x < k -> P x = false -> h x = 0) -> (forall x, x < k -> h x <= M) -> sumI h k <= M * cntI P k
| 0, _, _ => Nat.le_refl 0
| k + 1, hz, hM => by
have ih := sum_le h P M k (fun x hx => hz x (Nat.lt_succ_of_lt hx)) (fun x hx => hM x (Nat.lt_succ_of_lt hx))
show sumI h k + h k <= M * (cntI P k + bv (P k))
cases e : P k with
| false =>
rw [hz k (Nat.lt_succ_self k) e]
exact ih
| true => exact Nat.add_le_add ih (hM k (Nat.lt_succ_self k))
theorem and_false' (b : Bool) : (b && false) = false := by cases b <;> rfl
/-- The averaging step. -/
theorem avg (n : Nat) (rho : Nat -> Option Nat) (Ws : List Clause) (hT : 0 < lsum (covCnt n rho) Ws) :
Exists fun i => Exists fun j => i < n + 1 /\ j < n /\ remP rho i = true /\ freeH n rho j = true /\
lsum (covCnt n rho) Ws <= (lcnt (fun D => covB n D i j) Ws * cntI (freeH n rho) n) * cntI (remP rho) (n + 1) := by
let H : Nat -> Nat -> Nat := fun i j => lsum (fun D => bv (remP rho i && freeH n rho j && covB n D i j)) Ws
let F := cntI (freeH n rho) n
let R := cntI (remP rho) (n + 1)
let js : Nat -> Nat := fun i => amax (H i) n
let G : Nat -> Nat := fun i => H i (js i) * F
have eT : lsum (covCnt n rho) Ws = sumI (fun i => sumI (fun j => H i j) n) (n + 1) := by
show lsum (fun D => sumI (rowC n rho D) (n + 1)) Ws = _
rw [swap (fun D i => rowC n rho D i) (n + 1) Ws]
refine sumI_ext _ _ ?_ (n + 1)
intro i
show lsum (fun D => cntI (fun j => remP rho i && freeH n rho j && covB n D i j) n) Ws = _
rw [lsum_ext _ (fun D => sumI (fun j => bv (remP rho i && freeH n rho j && covB n D i j)) n)
(fun D => cntI_sumI _ n) Ws]
exact swap (fun D j => bv (remP rho i && freeH n rho j && covB n D i j)) n Ws
have Hz : forall i j, (remP rho i && freeH n rho j) = false -> H i j = 0 := by
intro i j e
refine lsum_zero _ ?_ Ws
intro D
show bv (remP rho i && freeH n rho j && covB n D i j) = 0
rw [e]; try rfl
have s1 : forall i, sumI (H i) n <= G i := by
intro i
refine sum_le (H i) (freeH n rho) (H i (js i)) n ?_ ?_
· intro j _ e
exact Hz i j (by rw [e]; exact and_false' _)
· intro j hj
exact amax_ge (H i) n j hj
have s2 : lsum (covCnt n rho) Ws <= sumI G (n + 1) := by
rw [eT]
exact sumI_le _ _ (n + 1) (fun i _ => s1 i)
let is := amax G (n + 1)
have s3 : sumI G (n + 1) <= G is * R := by
refine sum_le G (remP rho) (G is) (n + 1) ?_ ?_
· intro i _ e
show H i (js i) * F = 0
rw [Hz i (js i) (by rw [e]; try rfl)]
exact zero_mul' F
· intro i hi
exact amax_ge G (n + 1) i hi
have s4 := Nat.le_trans s2 s3
have isl : is < n + 1 := amax_succ_lt G n
cases n with
| zero =>
have f0 : G is = 0 := rfl
rw [f0, zero_mul'] at s4
exact absurd (Nat.lt_of_lt_of_le hT s4) (Nat.lt_irrefl 0)
| succ m =>
have jsl : js is < m + 1 := amax_succ_lt (H is) m
cases e : (remP rho is && freeH (m + 1) rho (js is)) with
| false =>
have g0 : G is = 0 := by
show H is (js is) * F = 0
rw [Hz is (js is) e]
exact zero_mul' F
rw [g0, zero_mul'] at s4
exact absurd (Nat.lt_of_lt_of_le hT s4) (Nat.lt_irrefl 0)
| true =>
refine Exists.intro is (Exists.intro (js is) (And.intro isl (And.intro jsl
(And.intro (Found.Pnp62.and_l e) (And.intro (Found.Pnp62.and_r e) ?_)))))
have eH : H is (js is) = lcnt (fun D => covB (m + 1) D is (js is)) Ws := by
refine lsum_ext _ _ ?_ Ws
intro D
show bv (remP rho is && freeH (m + 1) rho (js is) && covB (m + 1) D is (js is)) = bv (covB (m + 1) D is (js is))
rw [e]; try rfl
have s5 : lsum (covCnt (m + 1) rho) Ws <= (H is (js is) * F) * R := s4
rw [eH] at s5
exact s5
end Found.Pnp97
#print axioms Found.Pnp97.sumI_zero
#print axioms Found.Pnp97.sumI_add
#print axioms Found.Pnp97.sumI_ext
#print axioms Found.Pnp97.lsum_ext
#print axioms Found.Pnp97.lsum_zero
#print axioms Found.Pnp97.cntI_sumI
#print axioms Found.Pnp97.swap
#print axioms Found.Pnp97.zero_mul'
#print axioms Found.Pnp97.amax_le
#print axioms Found.Pnp97.amax_succ_lt
#print axioms Found.Pnp97.amax_ge
#print axioms Found.Pnp97.sum_le
#print axioms Found.Pnp97.and_false'
#print axioms Found.Pnp97.avg
#print axioms: 14 theorems, no axioms.
With \(K\) the number of wide clauses that can still lose a pigeon, one well chosen pair lowers \(K\) by at least a \(L/(FR)\) share.
found/Pnp98.lean
/-
T330 piece 98. found/Pnp98 -- one round of the restriction game (proof complexity ladder,
step 1: general resolution; Beame--Pitassi).
For a list Ws of clauses and a threshold L, a clause is wide and alive under rho when it
can lose at least one pigeon and covers at least L free pairs; K rho counts such clauses of
Ws. If K rho >= 1 and L >= 1 there is a remaining pigeon i and a free hole j with
(K (ext rho i j) * F) * R + K rho * L <= (K rho * F) * R,
F the free holes and R the remaining pigeons of rho. Proved:
* mul_add', add_mul', mul_le_r: arithmetic by hand;
* lcnt_filt, lsum_filt_ge, lcnt_split, lcnt_le: list counts;
* wa_back: wide and alive under ext rho i j means wide and alive under rho and not
covering (i, j);
* step: one round.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp97
namespace Found.Pnp98
open Found.RootComp (Clause)
open Found.Pnp65 (cntI bv)
open Found.Pnp90 (isNone_eq)
open Found.Pnp93 (cntP add4)
open Found.Pnp94 (freeH remP Good)
open Found.Pnp95 (covB covCnt)
open Found.Pnp96 (ext cntP_back cntP_kill covCnt_back)
open Found.Pnp97 (lsum lcnt avg)
theorem mul_add' (a b : Nat) : forall c, a * (b + c) = a * b + a * c
| 0 => rfl
| c + 1 => by
show a * (b + c) + a = a * b + (a * c + a)
rw [mul_add' a b c, Nat.add_assoc]
theorem add_mul' (a b c : Nat) : (a + b) * c = a * c + b * c := by
rw [Nat.mul_comm, mul_add', Nat.mul_comm c a, Nat.mul_comm c b]
theorem mul_le_r {a b : Nat} (h : a <= b) : forall c, a * c <= b * c
| 0 => Nat.le_refl 0
| c + 1 => Nat.add_le_add (mul_le_r h c) h
theorem mul_pos' {a b : Nat} (ha : 1 <= a) (hb : 1 <= b) : 1 <= a * b := by
cases b with
| zero => exact absurd hb (Nat.not_succ_le_zero 0)
| succ b => exact Nat.le_trans ha (Nat.le_add_left a (a * b))
def consIf (b : Bool) (D : Clause) (L : List Clause) : List Clause :=
match b with
| true => D :: L
| false => L
def filt (p : Clause -> Bool) : List Clause -> List Clause
| [] => []
| D :: L => consIf (p D) D (filt p L)
theorem lcnt_filt (p q : Clause -> Bool) : forall L, lcnt q (filt p L) = lcnt (fun D => p D && q D) L
| [] => rfl
| D :: L => by
show lcnt q (consIf (p D) D (filt p L)) = bv (p D && q D) + lcnt (fun D => p D && q D) L
cases e : p D with
| true =>
show bv (q D) + lcnt q (filt p L) = bv (true && q D) + _
rw [lcnt_filt p q L]; try rfl
| false =>
show lcnt q (filt p L) = bv (false && q D) + _
rw [lcnt_filt p q L]
exact (Nat.zero_add _).symm
theorem lsum_filt_ge (p : Clause -> Bool) (h : Clause -> Nat) (M : Nat) (hp : forall D, p D = true -> M <= h D) :
forall L, M * lcnt p L <= lsum h (filt p L)
| [] => Nat.le_refl 0
| D :: L => by
have ih := lsum_filt_ge p h M hp L
show M * (bv (p D) + lcnt p L) <= lsum h (consIf (p D) D (filt p L))
rw [mul_add']
cases e : p D with
| true =>
show M * 1 + M * lcnt p L <= h D + lsum h (filt p L)
rw [Nat.mul_one]
exact Nat.add_le_add (hp D e) ih
| false =>
show M * 0 + M * lcnt p L <= lsum h (filt p L)
rw [Nat.mul_zero, Nat.zero_add]
exact ih
theorem lcnt_split (p q : Clause -> Bool) : forall L,
lcnt p L = lcnt (fun D => p D && q D) L + lcnt (fun D => p D && !(q D)) L
| [] => rfl
| D :: L => by
show bv (p D) + lcnt p L = (bv (p D && q D) + lcnt _ L) + (bv (p D && !(q D)) + lcnt _ L)
rw [lcnt_split p q L, add4]
cases p D <;> cases q D <;> rfl
theorem lcnt_le (p q : Clause -> Bool) (h : forall D, p D = true -> q D = true) : forall L, lcnt p L <= lcnt q L
| [] => Nat.le_refl 0
| D :: L => by
show bv (p D) + lcnt p L <= bv (q D) + lcnt q L
refine Nat.add_le_add ?_ (lcnt_le p q h L)
cases e : p D with
| false => exact Nat.zero_le _
| true => rw [h D e]; exact Nat.le_refl _
/-- Wide and alive. -/
def wa (n : Nat) (rho : Nat -> Option Nat) (L : Nat) (D : Clause) : Bool :=
Nat.ble 1 (cntP n rho D) && Nat.ble L (covCnt n rho D)
def K (n : Nat) (rho : Nat -> Option Nat) (L : Nat) (Ws : List Clause) : Nat := lcnt (wa n rho L) Ws
theorem wa_back (n : Nat) (rho : Nat -> Option Nat) (L : Nat) (i j : Nat) (hi : i < n + 1) (hr : rho i = none)
(hj : j < n) (D : Clause) (h : wa n (ext rho i j) L D = true) :
(wa n rho L D && !(covB n D i j)) = true := by
have a1 : 1 <= cntP n (ext rho i j) D := Nat.le_of_ble_eq_true (Found.Pnp62.and_l h)
have a2 : L <= covCnt n (ext rho i j) D := Nat.le_of_ble_eq_true (Found.Pnp62.and_r h)
have b1 : Nat.ble 1 (cntP n rho D) = true := Nat.ble_eq_true_of_le (Nat.le_trans a1 (cntP_back n rho i j hr D))
have b2 : Nat.ble L (covCnt n rho D) = true := Nat.ble_eq_true_of_le (Nat.le_trans a2 (covCnt_back n rho i j hi hr D))
have c : covB n D i j = false := by
cases e : covB n D i j with
| false => rfl
| true =>
rw [cntP_kill n rho i j hi hj D e] at a1
exact absurd a1 (Nat.not_succ_le_zero 0)
show (Nat.ble 1 (cntP n rho D) && Nat.ble L (covCnt n rho D) && !(covB n D i j)) = true
rw [b1, b2, c]; rfl
/-- One round of the restriction game. -/
theorem step (n : Nat) (rho : Nat -> Option Nat) (L : Nat) (Ws : List Clause) (hK : 1 <= K n rho L Ws) (hL : 1 <= L) :
Exists fun i => Exists fun j => i < n + 1 /\ j < n /\ rho i = none /\ freeH n rho j = true /\
(K n (ext rho i j) L Ws * cntI (freeH n rho) n) * cntI (remP rho) (n + 1) + K n rho L Ws * L <=
(K n rho L Ws * cntI (freeH n rho) n) * cntI (remP rho) (n + 1) := by
let Wl := filt (wa n rho L) Ws
have t1 : L * K n rho L Ws <= lsum (covCnt n rho) Wl :=
lsum_filt_ge (wa n rho L) (covCnt n rho) L (fun D h => Nat.le_of_ble_eq_true (Found.Pnp62.and_r h)) Ws
have t0 : 0 < lsum (covCnt n rho) Wl := Nat.lt_of_lt_of_le (mul_pos' hL hK) t1
cases avg n rho Wl t0 with
| intro i hi =>
cases hi with
| intro j hj =>
have hr : rho i = none := isNone_eq _ hj.2.2.1
refine Exists.intro i (Exists.intro j (And.intro hj.1 (And.intro hj.2.1 (And.intro hr (And.intro hj.2.2.2.1 ?_)))))
let F := cntI (freeH n rho) n
let R := cntI (remP rho) (n + 1)
let c := lcnt (fun D => covB n D i j) Wl
let K2 := lcnt (fun D => wa n rho L D && !(covB n D i j)) Ws
have ec : c = lcnt (fun D => wa n rho L D && covB n D i j) Ws := lcnt_filt _ _ Ws
have eK : K n rho L Ws = c + K2 := by
rw [ec]; exact lcnt_split (wa n rho L) (fun D => covB n D i j) Ws
have k' : K n (ext rho i j) L Ws <= K2 :=
lcnt_le _ _ (fun D h => wa_back n rho L i j hj.1 hr hj.2.1 D h) Ws
have tT : K n rho L Ws * L <= (c * F) * R :=
Nat.le_trans (Nat.le_of_eq (Nat.mul_comm _ _)) (Nat.le_trans t1 hj.2.2.2.2)
have s : (K n (ext rho i j) L Ws * F) * R + K n rho L Ws * L <= (K2 * F) * R + (c * F) * R :=
Nat.add_le_add (mul_le_r (mul_le_r k' F) R) tT
have e2 : (K n rho L Ws * F) * R = (K2 * F) * R + (c * F) * R := by
rw [eK, add_mul', add_mul', Nat.add_comm]
rw [e2]
exact s
end Found.Pnp98
#print axioms Found.Pnp98.mul_add'
#print axioms Found.Pnp98.add_mul'
#print axioms Found.Pnp98.mul_le_r
#print axioms Found.Pnp98.mul_pos'
#print axioms Found.Pnp98.lcnt_filt
#print axioms Found.Pnp98.lsum_filt_ge
#print axioms Found.Pnp98.lcnt_split
#print axioms Found.Pnp98.lcnt_le
#print axioms Found.Pnp98.wa_back
#print axioms Found.Pnp98.step
#print axioms: 10 theorems, no axioms.
With \(M=n(n+1)=N+L\), \(r\) rounds from the empty restriction give \(M^r\le |W|\,N^r\) whenever every good restriction with \(t\) remaining pigeons keeps a wide clause.
found/Pnp99.lean
/-
T330 piece 99. found/Pnp99 -- r rounds of the restriction game (proof complexity ladder,
step 1: general resolution; Beame--Pitassi).
Let M = n * (n + 1) and M = N + L. If every good restriction with t remaining pigeons has
a wide alive clause in Ws (K >= 1), then for a good restriction with t + r remaining
pigeons (and one fewer free hole)
M ^ r <= K rho * N ^ r,
and for the empty restriction (n + 1 = t + r)
M ^ r <= (length of Ws) * N ^ r.
Proved:
* cntI_le_k, le_ex, add_cancel_r', mul_le_l', mul_assoc', pow_pos': arithmetic;
* K_mono: K does not grow under ext;
* round: one round in the form K' * M <= K * N;
* iter: r rounds;
* rho0_good, rem0, free0, lcnt_len, iter0: from the empty restriction.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp98
namespace Found.Pnp99
open Found.RootComp (Clause)
open Found.Pnp65 (cntI bv anyI)
open Found.Pnp87 (optEq)
open Found.Pnp90 (isNone)
open Found.Pnp94 (freeH remP Good rank_ex)
open Found.Pnp96 (ext good_ext rem_ext free_ext)
open Found.Pnp97 (lcnt)
open Found.Pnp98 (K wa wa_back step mul_add' mul_le_r mul_pos' lcnt_le)
theorem cntI_le_k (g : Nat -> Bool) : forall k, cntI g k <= k
| 0 => Nat.le_refl 0
| k + 1 => by
show cntI g k + bv (g k) <= k + 1
refine Nat.add_le_add (cntI_le_k g k) ?_
cases g k <;> first | exact Nat.zero_le 1 | exact Nat.le_refl 1
theorem le_ex : forall b a : Nat, a <= b -> Exists fun d => b = a + d
| 0, a, h => Exists.intro 0 (Found.Pnp66.le_zero_eq h).symm
| b + 1, a, h => by
cases Nat.lt_or_ge b a with
| inl l =>
have e : a = b + 1 := Nat.le_antisymm h l
exact Exists.intro 0 e.symm
| inr r =>
cases le_ex b a r with
| intro d hd => exact Exists.intro (d + 1) (by rw [hd]; rfl)
theorem add_cancel_r' {a b : Nat} : forall c, a + c <= b + c -> a <= b
| 0, h => h
| c + 1, h => add_cancel_r' c (Nat.le_of_succ_le_succ h)
theorem mul_le_l' {a b : Nat} (h : a <= b) (c : Nat) : c * a <= c * b := by
rw [Nat.mul_comm c a, Nat.mul_comm c b]
exact mul_le_r h c
theorem mul_assoc' (a b : Nat) : forall c, (a * b) * c = a * (b * c)
| 0 => rfl
| c + 1 => by
show (a * b) * c + a * b = a * (b * c + b)
rw [mul_assoc' a b c, mul_add']
theorem pow_pos' {M : Nat} (h : 1 <= M) : forall r, 1 <= M ^ r
| 0 => Nat.le_refl 1
| r + 1 => mul_pos' (pow_pos' h r) h
theorem K_mono (n : Nat) (rho : Nat -> Option Nat) (L : Nat) (Ws : List Clause) (i j : Nat) (hi : i < n + 1)
(hr : rho i = none) (hj : j < n) : K n (ext rho i j) L Ws <= K n rho L Ws :=
lcnt_le _ _ (fun D h => Found.Pnp62.and_l (wa_back n rho L i j hi hr hj D h)) Ws
/-- One round in multiplicative form. -/
theorem round (n N L K0 K1 F R : Nat) (hM : N + L = n * (n + 1)) (hF : F <= n) (hR : R <= n + 1)
(hK : K1 <= K0) (hs : (K1 * F) * R + K0 * L <= (K0 * F) * R) : K1 * (n * (n + 1)) <= K0 * N := by
have hQ : F * R <= n * (n + 1) := Nat.le_trans (mul_le_r hF R) (mul_le_l' hR n)
cases le_ex _ _ hQ with
| intro d hd =>
rw [mul_assoc', mul_assoc'] at hs
have a : K1 * (n * (n + 1)) + K0 * L <= K0 * (n * (n + 1)) := by
rw [hd, mul_add', mul_add', Nat.add_right_comm]
exact Nat.add_le_add hs (mul_le_r hK d)
have e : K0 * (n * (n + 1)) = K0 * N + K0 * L := by rw [<- hM, mul_add']
exact add_cancel_r' _ (Nat.le_trans a (Nat.le_of_eq e))
theorem rearr (a e b c d : Nat) (h : a * b <= c * d) : (a * e) * b <= c * (e * d) :=
calc (a * e) * b = a * (e * b) := mul_assoc' a e b
_ = a * (b * e) := by rw [Nat.mul_comm e b]
_ = (a * b) * e := (mul_assoc' a b e).symm
_ <= (c * d) * e := mul_le_r h e
_ = c * (d * e) := mul_assoc' c d e
_ = c * (e * d) := by rw [Nat.mul_comm d e]
theorem succ_inj' {a b : Nat} (h : a + 1 = b + 1) : a = b :=
Nat.le_antisymm (Nat.le_of_succ_le_succ (Nat.le_of_eq h)) (Nat.le_of_succ_le_succ (Nat.le_of_eq h.symm))
/-- r rounds. -/
theorem iter (n N L t : Nat) (Ws : List Clause) (hM : N + L = n * (n + 1)) (hL : 1 <= L) (ht : 1 <= t)
(hfin : forall rho, Good n rho -> cntI (freeH n rho) n + 1 = cntI (remP rho) (n + 1) ->
cntI (remP rho) (n + 1) = t -> 1 <= K n rho L Ws) :
forall r rho, Good n rho -> cntI (freeH n rho) n + 1 = cntI (remP rho) (n + 1) ->
cntI (remP rho) (n + 1) = t + r -> (n * (n + 1)) ^ r <= K n rho L Ws * N ^ r
| 0, rho, hg, hF, hR => by
show 1 <= K n rho L Ws * 1
rw [Nat.mul_one]
exact hfin rho hg hF hR
| r + 1, rho, hg, hF, hR => by
have M1 : 1 <= n * (n + 1) := by rw [hM.symm]; exact Nat.le_trans hL (Nat.le_add_left L N)
have next : forall i j, i < n + 1 -> j < n -> rho i = none -> freeH n rho j = true ->
Good n (ext rho i j) /\ cntI (freeH n (ext rho i j)) n + 1 = cntI (remP (ext rho i j)) (n + 1) /\
cntI (remP (ext rho i j)) (n + 1) = t + r := by
intro i j hi hj hr hf
have e1 := rem_ext n rho i j hi hr
have e2 := free_ext n rho i j hi hr hj hf
refine And.intro (good_ext n rho hg i j hi hr hj hf) (And.intro ?_ ?_)
· rw [e1, e2] at hF; exact succ_inj' hF
· rw [e1] at hR; exact succ_inj' hR
cases Nat.lt_or_ge 0 (K n rho L Ws) with
| inl kp =>
cases step n rho L Ws kp hL with
| intro i hi =>
cases hi with
| intro j hj =>
have nx := next i j hj.1 hj.2.1 hj.2.2.1 hj.2.2.2.1
have ih := iter n N L t Ws hM hL ht hfin r (ext rho i j) nx.1 nx.2.1 nx.2.2
have rd := round n N L (K n rho L Ws) (K n (ext rho i j) L Ws) (cntI (freeH n rho) n) (cntI (remP rho) (n + 1))
hM (cntI_le_k _ n) (cntI_le_k _ (n + 1)) (K_mono n rho L Ws i j hj.1 hj.2.2.1 hj.2.1) hj.2.2.2.2
show (n * (n + 1)) ^ r * (n * (n + 1)) <= K n rho L Ws * (N ^ r * N)
refine Nat.le_trans (mul_le_r ih _) ?_
exact rearr _ _ _ _ _ rd
| inr k0 =>
have z : K n rho L Ws = 0 := Found.Pnp66.le_zero_eq k0
have Rp : 0 < cntI (remP rho) (n + 1) := by rw [hR]; exact Nat.succ_pos _
have Fp : 0 < cntI (freeH n rho) n := by
have e : cntI (freeH n rho) n + 1 = t + r + 1 := hF.trans hR
have e' := succ_inj' e
rw [e']
exact Nat.lt_of_lt_of_le ht (Nat.le_add_right t r)
cases rank_ex _ _ 0 Rp with
| intro i hi =>
cases rank_ex _ _ 0 Fp with
| intro j hj =>
have hr : rho i = none := Found.Pnp90.isNone_eq _ hi.2.1
have nx := next i j hi.1 hj.1 hr hj.2.1
have ih := iter n N L t Ws hM hL ht hfin r (ext rho i j) nx.1 nx.2.1 nx.2.2
have km := K_mono n rho L Ws i j hi.1 hr hj.1
rw [z] at km
have z' : K n (ext rho i j) L Ws = 0 := Found.Pnp66.le_zero_eq km
rw [z', Found.Pnp97.zero_mul'] at ih
exact absurd (Nat.lt_of_lt_of_le (pow_pos' M1 r) ih) (Nat.lt_irrefl 0)
def rho0 : Nat -> Option Nat := fun _ => none
theorem cntI_all (g : Nat -> Bool) : forall k, (forall j, j < k -> g j = true) -> cntI g k = k
| 0, _ => rfl
| k + 1, h => by
show cntI g k + bv (g k) = k + 1
rw [cntI_all g k (fun j hj => h j (Nat.lt_succ_of_lt hj)), h k (Nat.lt_succ_self k)]
rfl
theorem rem0 (n : Nat) : cntI (remP rho0) (n + 1) = n + 1 := cntI_all _ (n + 1) (fun _ _ => rfl)
theorem free0 (n : Nat) : cntI (freeH n rho0) n = n := by
refine cntI_all _ n ?_
intro j _
show (!(anyI (fun a => optEq (rho0 a) j) (n + 1))) = true
have z : anyI (fun a => optEq (rho0 a) j) (n + 1) = false := Found.Pnp66.anyI_none _ (n + 1) (fun _ _ => rfl)
rw [z]
rfl
theorem rho0_good (n : Nat) : Good n rho0 := by
refine Good.mk (fun _ _ _ h => by cases h) (fun _ _ _ _ _ h => by cases h) ?_
rw [rem0, free0]
exact Nat.le_refl _
theorem lcnt_len (p : Clause -> Bool) : forall L : List Clause, lcnt p L <= L.length
| [] => Nat.le_refl 0
| D :: L => by
show bv (p D) + lcnt p L <= L.length + 1
rw [Nat.add_comm]
refine Nat.add_le_add (lcnt_len p L) ?_
cases p D <;> first | exact Nat.zero_le 1 | exact Nat.le_refl 1
/-- From the empty restriction. -/
theorem iter0 (n N L t r : Nat) (Ws : List Clause) (hM : N + L = n * (n + 1)) (hL : 1 <= L) (ht : 1 <= t)
(hn : n + 1 = t + r)
(hfin : forall rho, Good n rho -> cntI (freeH n rho) n + 1 = cntI (remP rho) (n + 1) ->
cntI (remP rho) (n + 1) = t -> 1 <= K n rho L Ws) :
(n * (n + 1)) ^ r <= Ws.length * N ^ r := by
have h := iter n N L t Ws hM hL ht hfin r rho0 (rho0_good n) (by rw [rem0, free0]) (by rw [rem0]; exact hn)
exact Nat.le_trans h (mul_le_r (lcnt_len _ Ws) _)
end Found.Pnp99
#print axioms Found.Pnp99.cntI_le_k
#print axioms Found.Pnp99.le_ex
#print axioms Found.Pnp99.add_cancel_r'
#print axioms Found.Pnp99.mul_le_l'
#print axioms Found.Pnp99.mul_assoc'
#print axioms Found.Pnp99.pow_pos'
#print axioms Found.Pnp99.K_mono
#print axioms Found.Pnp99.round
#print axioms Found.Pnp99.rearr
#print axioms Found.Pnp99.succ_inj'
#print axioms Found.Pnp99.iter
#print axioms Found.Pnp99.cntI_all
#print axioms Found.Pnp99.rem0
#print axioms Found.Pnp99.free0
#print axioms Found.Pnp99.rho0_good
#print axioms Found.Pnp99.lcnt_len
#print axioms Found.Pnp99.iter0
#print axioms: 17 theorems, no axioms.
The middle clause of a refutation is wide at every good restriction with \(t\) remaining pigeons, so the number of distinct clauses of any resolution refutation of \(\mathrm {PHP}^{n+1}_n\) satisfies the bound of the rounds.
found/Pnp100.lean
/-
T330 piece 100. found/Pnp100 -- the size bound for resolution refutations of the
pigeonhole principle, in parametric form (proof complexity ladder, step 1: general
resolution; Haken 1985, in the form of Beame--Pitassi 1996).
dd L removes repeated clauses (beqCl), so (dd (nodes p)).length is the number of distinct
clauses of a proof p. For a refutation p of PHP(n + 1, n), numbers with M = n (n + 1) =
N + L, n + 1 = t + r, 1 <= b < t, and L <= s u whenever s + u = t and s <= b < 2 s:
M ^ r <= (number of distinct clauses of p) * N ^ r.
Proved:
* dd_mem, dd_len: the list of distinct clauses;
* lcnt_pos: a member that passes the test counts;
* fin: every good restriction with t remaining pigeons has a wide alive clause (the
middle clause, Pnp93, with its width, Pnp95);
* haken: the parametric size bound.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp99
namespace Found.Pnp100
open Found.RootComp (Clause)
open Found.Pnp62 (memC beqCl RProof)
open Found.Pnp65 (cntI bv)
open Found.Pnp66 (PHP)
open Found.Pnp93 (nodes cntP mid_pigs memC_head memC_cons_r)
open Found.Pnp94 (freeH remP Good cntP_nil)
open Found.Pnp95 (covCnt nonP width split_rem)
open Found.Pnp97 (lcnt)
open Found.Pnp98 (K wa consIf)
open Found.Pnp99 (iter0)
/-- Distinct clauses. -/
def dd : List Clause -> List Clause
| [] => []
| C :: L => consIf (!(memC C (dd L))) C (dd L)
theorem dd_keep (C D : Clause) (L : List Clause) (h : memC D (dd L) = true) : memC D (dd (C :: L)) = true := by
show memC D (consIf (!(memC C (dd L))) C (dd L)) = true
cases memC C (dd L) with
| true => exact h
| false => exact memC_cons_r D C _ h
theorem dd_mem (D : Clause) : forall L : List Clause, memC D L = true -> memC D (dd L) = true
| [], h => h
| C :: L, h => by
have h' : (beqCl C D || memC D L) = true := h
cases e : beqCl C D with
| false =>
rw [e] at h'
exact dd_keep C D L (dd_mem D L h')
| true =>
have cd : C = D := Found.Pnp62.beqCl_eq C D e
rw [cd.symm]
show memC C (consIf (!(memC C (dd L))) C (dd L)) = true
cases ec : memC C (dd L) with
| true => exact ec
| false => exact memC_head C _
theorem dd_len : forall L : List Clause, (dd L).length <= L.length
| [] => Nat.le_refl 0
| C :: L => by
show (consIf (!(memC C (dd L))) C (dd L)).length <= L.length + 1
cases memC C (dd L) with
| true => exact Nat.le_trans (dd_len L) (Nat.le_succ _)
| false => exact Nat.succ_le_succ (dd_len L)
theorem lcnt_pos (p : Clause -> Bool) (D : Clause) (hp : p D = true) :
forall Ws : List Clause, memC D Ws = true -> 1 <= lcnt p Ws
| [], h => Bool.noConfusion h
| C :: Ws, h => by
show 1 <= bv (p C) + lcnt p Ws
have h' : (beqCl C D || memC D Ws) = true := h
cases e : beqCl C D with
| false =>
rw [e] at h'
exact Nat.le_trans (lcnt_pos p D hp Ws h') (Nat.le_add_left _ _)
| true =>
rw [Found.Pnp62.beqCl_eq C D e, hp]
exact Nat.le_add_right 1 _
/-- Every good restriction with t remaining pigeons has a wide alive clause. -/
theorem fin (n L t b : Nat) (p : RProof (PHP n) []) (Ws : List Clause)
(hWs : forall D, memC D (nodes p) = true -> memC D Ws = true) (hb : 1 <= b) (hbt : b < t)
(hw : forall s u, s + u = t -> s <= b -> b < s + s -> L <= s * u)
(rho : Nat -> Option Nat) (hg : Good n rho) (hR : cntI (remP rho) (n + 1) = t) : 1 <= K n rho L Ws := by
have c0 : cntP n rho [] = t := (cntP_nil n rho hg).trans hR
cases mid_pigs n rho b hb [] p (by rw [c0]; exact hbt) with
| intro D hD =>
have sp : cntP n rho D + cntI (nonP n rho D) (n + 1) = t := (split_rem n rho D).trans hR
have lw : L <= covCnt n rho D := Nat.le_trans (hw _ _ sp hD.2.1 hD.2.2) (width n rho D)
have s1 : 1 <= cntP n rho D := by
cases Nat.lt_or_ge 0 (cntP n rho D) with
| inl l => exact l
| inr r =>
have z := Found.Pnp66.le_zero_eq r
have t2 := hD.2.2
rw [z] at t2
exact absurd t2 (Nat.not_lt_zero b)
have w : wa n rho L D = true := by
show (Nat.ble 1 (cntP n rho D) && Nat.ble L (covCnt n rho D)) = true
rw [Nat.ble_eq_true_of_le s1, Nat.ble_eq_true_of_le lw]
rfl
exact lcnt_pos _ D w Ws (hWs D hD.1)
/-- The parametric size bound. -/
theorem haken (n N L t r b : Nat) (p : RProof (PHP n) []) (hM : N + L = n * (n + 1)) (hL : 1 <= L)
(hb : 1 <= b) (hbt : b < t) (hn : n + 1 = t + r)
(hw : forall s u, s + u = t -> s <= b -> b < s + s -> L <= s * u) :
(n * (n + 1)) ^ r <= (dd (nodes p)).length * N ^ r :=
iter0 n N L t r (dd (nodes p)) hM hL (Nat.lt_of_le_of_lt (Nat.zero_le b) hbt) hn
(fun rho hg _ hR => fin n L t b p (dd (nodes p)) (fun D h => dd_mem D _ h) hb hbt hw rho hg hR)
end Found.Pnp100
#print axioms Found.Pnp100.dd_keep
#print axioms Found.Pnp100.dd_mem
#print axioms Found.Pnp100.dd_len
#print axioms Found.Pnp100.lcnt_pos
#print axioms Found.Pnp100.fin
#print axioms Found.Pnp100.haken
#print axioms: 6 theorems, no axioms.
Every resolution refutation of \(\mathrm {PHP}^{6k}_{6k-1}\) has at least \((18/17)^{3k}\) distinct clauses (Haken 1985, in the form of Beame–Pitassi 1996).
found/Pnp101.lean
/-
T330 piece 101. found/Pnp101 -- Haken's theorem with numbers: every resolution refutation of
the pigeonhole principle PHP(6k, 6k - 1) has at least (18/17)^(3k) distinct clauses (proof
complexity ladder, step 1: general resolution).
Statement (haken_num): for k >= 1, n + 1 = 6 k and a resolution refutation p of PHP n,
18 ^ (3 k) <= 17 ^ (3 k) * (number of distinct clauses of p).
It is Pnp100.haken with t = r = 3 k, b = 2 k, L = 2 k^2 and N = n (n + 1) - L; the width
condition is (k + a)(c + k) >= 2 k^2 when a + c = k. Proved:
* two_mul', three_mul', six_mul', add_cancel_l', add_cancel_l_eq, swap4, pow_mul',
pow_le, cancel_r, sq6: arithmetic by hand;
* width_num: the width condition;
* haken_num: the bound.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp100
namespace Found.Pnp101
open Found.Pnp62 (RProof)
open Found.Pnp66 (PHP)
open Found.Pnp93 (nodes)
open Found.Pnp98 (mul_add' add_mul' mul_le_r mul_pos')
open Found.Pnp99 (le_ex add_cancel_r' mul_le_l' mul_assoc' pow_pos')
open Found.Pnp100 (dd haken)
theorem two_mul' (k : Nat) : 2 * k = k + k := by
have e := add_mul' 1 1 k
rw [Nat.one_mul] at e
exact e
theorem three_mul' (k : Nat) : 3 * k = 2 * k + k := by
have e := add_mul' 2 1 k
rw [Nat.one_mul] at e
exact e
theorem six_mul' (k : Nat) : 6 * k = 3 * k + 3 * k := add_mul' 3 3 k
theorem add_cancel_l' {x a b : Nat} (h : x + a <= x + b) : a <= b := by
rw [Nat.add_comm x a, Nat.add_comm x b] at h
exact add_cancel_r' x h
theorem add_cancel_l_eq {x a b : Nat} (h : x + a = x + b) : a = b :=
Nat.le_antisymm (add_cancel_l' (Nat.le_of_eq h)) (add_cancel_l' (Nat.le_of_eq h.symm))
theorem swap4 (x y z w : Nat) : (x * y) * (z * w) = (x * z) * (y * w) :=
calc (x * y) * (z * w) = x * (y * (z * w)) := mul_assoc' x y (z * w)
_ = x * ((y * z) * w) := by rw [mul_assoc' y z w]
_ = x * ((z * y) * w) := by rw [Nat.mul_comm y z]
_ = x * (z * (y * w)) := by rw [mul_assoc' z y w]
_ = (x * z) * (y * w) := (mul_assoc' x z (y * w)).symm
theorem pow_mul' (a b : Nat) : forall r, (a * b) ^ r = a ^ r * b ^ r
| 0 => rfl
| r + 1 => by
show (a * b) ^ r * (a * b) = (a ^ r * a) * (b ^ r * b)
rw [pow_mul' a b r, swap4]
theorem pow_le {a b : Nat} (h : a <= b) : forall r, a ^ r <= b ^ r
| 0 => Nat.le_refl 1
| r + 1 => Nat.le_trans (mul_le_r (pow_le h r) a) (mul_le_l' h (b ^ r))
theorem cancel_r {x y z : Nat} (hz : 1 <= z) (h : x * z <= y * z) : x <= y := by
cases Nat.lt_or_ge y x with
| inr r => exact r
| inl l =>
have a : (y + 1) * z <= y * z := Nat.le_trans (mul_le_r l z) h
rw [add_mul', Nat.one_mul] at a
have b : z <= 0 := add_cancel_l' (Nat.le_trans a (Nat.le_of_eq (Nat.add_zero _).symm))
exact absurd (Nat.le_trans hz b) (Nat.not_succ_le_zero 0)
theorem zpow {m : Nat} (h : 1 <= m) : (0 : Nat) ^ m = 0 := by
cases m with
| zero => exact absurd h (Nat.not_succ_le_zero 0)
| succ m => exact Nat.mul_zero (0 ^ m)
theorem sq6 (k : Nat) : (6 * k) * (6 * k) = 18 * (2 * (k * k)) :=
calc (6 * k) * (6 * k) = (6 * 6) * (k * k) := swap4 6 k 6 k
_ = (18 * 2) * (k * k) := rfl
_ = 18 * (2 * (k * k)) := mul_assoc' 18 2 (k * k)
/-- The width condition for t = 3 k, b = 2 k, L = 2 k^2. -/
theorem width_num (k s u : Nat) (hsu : s + u = 3 * k) (hs : s <= 2 * k) (hb : 2 * k < s + s) :
2 * (k * k) <= s * u := by
rw [three_mul', two_mul'] at hsu
rw [two_mul'] at hs hb
rw [two_mul']
have ks : k <= s := by
cases Nat.lt_or_ge s k with
| inr r => exact r
| inl l => exact absurd (Nat.lt_of_lt_of_le hb (Nat.add_le_add (Nat.le_of_lt l) (Nat.le_of_lt l))) (Nat.lt_irrefl _)
cases le_ex _ _ ks with
| intro a ha =>
rw [ha] at hs hsu
cases le_ex _ _ (add_cancel_l' hs) with
| intro c hc =>
have e1 : (k + a) + (c + k) = k + k + k := by
rw [Nat.add_assoc k a, <- Nat.add_assoc a c k, <- hc, <- Nat.add_assoc]
have hu : u = c + k := add_cancel_l_eq (hsu.trans e1.symm)
rw [ha, hu]
have e2 : k * k = a * k + c * k := by
have t := add_mul' a c k
rw [<- hc] at t
exact t
have lo : k * c + (k * k + a * k) <= (k + a) * (c + k) := by
rw [mul_add']
exact Nat.add_le_add (mul_le_r (Nat.le_add_right k a) c) (Nat.le_of_eq (add_mul' k a k).symm)
have eq : k * k + k * k = k * c + (k * k + a * k) := by
rw [congrArg (fun x => k * k + x) e2, Nat.mul_comm c k, <- Nat.add_assoc, Nat.add_comm]
rw [eq]
exact lo
/-- Haken's theorem with numbers. -/
theorem haken_num (k n : Nat) (hk : 1 <= k) (hn : n + 1 = 6 * k) (p : RProof (PHP n) []) :
18 ^ (3 * k) <= 17 ^ (3 * k) * (dd (nodes p)).length := by
have kk : 1 <= k * k := mul_pos' hk hk
have hL : 1 <= 2 * (k * k) := by rw [two_mul']; exact Nat.le_trans kk (Nat.le_add_right _ _)
have hb : 1 <= 2 * k := by rw [two_mul']; exact Nat.le_trans hk (Nat.le_add_right _ _)
have hbt : 2 * k < 3 * k := by
rw [three_mul']
exact Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (Nat.add_le_add_left hk _)
-- n >= 2 k and k <= n + 1
have e6 : 6 * k = 2 * k + 4 * k := add_mul' 2 4 k
have h4 : 1 <= 4 * k := Nat.le_trans (Nat.le_of_ble_eq_true rfl : 1 <= 4) (mul_le_l' hk 4)
have n2 : 2 * k <= n := by
have t : 2 * k + 1 <= n + 1 := by
rw [hn, e6]
exact Nat.add_le_add_left h4 _
exact Nat.le_of_succ_le_succ t
have k1 : k <= n + 1 := by
rw [hn, six_mul', three_mul']
exact Nat.le_trans (Nat.le_add_left k (2 * k)) (Nat.le_add_right _ _)
have LM : 2 * (k * k) <= n * (n + 1) := by
rw [<- mul_assoc' 2 k k]
exact Nat.le_trans (mul_le_l' k1 (2 * k)) (mul_le_r n2 (n + 1))
cases le_ex _ _ LM with
| intro N hN =>
have hM : N + 2 * (k * k) = n * (n + 1) := by rw [hN, Nat.add_comm]
have H := haken n N (2 * (k * k)) (3 * k) (3 * k) (2 * k) p hM hL hb hbt (by rw [hn]; exact six_mul' k)
(fun s u hsu hs hb' => width_num k s u hsu hs hb')
have M1 : 1 <= n * (n + 1) := Nat.le_trans hL LM
-- 18 N <= 17 M
have M18 : n * (n + 1) <= 18 * (2 * (k * k)) := by
rw [<- sq6, <- hn]
exact mul_le_r (Nat.le_succ n) (n + 1)
have e18 : 18 * N + 18 * (2 * (k * k)) = 17 * (n * (n + 1)) + n * (n + 1) := by
rw [<- mul_add', hM, Nat.mul_comm 18, Nat.mul_comm 17]
try rfl
have N17 : 18 * N <= 17 * (n * (n + 1)) := by
have t : 18 * N + 18 * (2 * (k * k)) <= 17 * (n * (n + 1)) + 18 * (2 * (k * k)) := by
rw [e18]; exact Nat.add_le_add_left M18 _
exact add_cancel_r' _ t
cases Nat.lt_or_ge 0 N with
| inr r =>
have z : N = 0 := Found.Pnp66.le_zero_eq r
rw [z, zpow (Nat.le_trans hb (Nat.le_of_lt hbt)), Nat.mul_zero] at H
exact absurd (Nat.le_trans (pow_pos' M1 _) H) (Nat.not_succ_le_zero 0)
| inl l =>
have a : 18 ^ (3 * k) * N ^ (3 * k) <= (17 ^ (3 * k) * (dd (nodes p)).length) * N ^ (3 * k) := by
rw [<- pow_mul' 18 N (3 * k)]
refine Nat.le_trans (pow_le N17 _) ?_
rw [pow_mul' 17 (n * (n + 1)) (3 * k), mul_assoc' (17 ^ (3 * k)) (dd (nodes p)).length (N ^ (3 * k))]
exact mul_le_l' H _
exact cancel_r (pow_pos' l _) a
end Found.Pnp101
#print axioms Found.Pnp101.two_mul'
#print axioms Found.Pnp101.three_mul'
#print axioms Found.Pnp101.six_mul'
#print axioms Found.Pnp101.add_cancel_l'
#print axioms Found.Pnp101.add_cancel_l_eq
#print axioms Found.Pnp101.swap4
#print axioms Found.Pnp101.pow_mul'
#print axioms Found.Pnp101.pow_le
#print axioms Found.Pnp101.cancel_r
#print axioms Found.Pnp101.zpow
#print axioms Found.Pnp101.sq6
#print axioms Found.Pnp101.width_num
#print axioms Found.Pnp101.haken_num
#print axioms: 13 theorems, no axioms.
Cutting planes lines are integer inequalities kept on two sides with natural numbers; the rules are the axioms, \(0\le x\le 1\), addition, multiplication and division with rounding, and a refutation derives \(0\ge 1\), so the axioms are unsatisfiable.
found/Pnp102.lean
/-
T330 piece 102. found/Pnp102 -- cutting planes proofs and their soundness (proof complexity
ladder, step 2: cutting planes).
A line is an integer linear inequality over 0/1 variables x_0 .. x_(m-1), kept on two sides
with natural numbers: sum P_i x_i + d >= sum N_i x_i + e (P, N dense coefficient lists), so
the net coefficient of x_i is P_i - N_i and no integer library is needed. Rules:
* axioms of the formula, x_i >= 0, x_i <= 1;
* addition of two lines; multiplication by a natural number;
* division by c >= 1 with rounding: from (P, N, d, e) to (P', N', d', e') when every net
coefficient is c times the new one (P_i + c N'_i = N_i + c P'_i) and the new constant is
at most the rounded quotient (c e' + d + 1 <= c d' + e + c).
A refutation derives 0 >= 1. Proved:
* val_add, val_mul, val_unit_le: values of lines;
* sound: every derived line holds under an assignment satisfying the formula;
* refute_unsat: a formula with a refutation has no satisfying assignment.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp101
namespace Found.Pnp102
open Found.Pnp65 (bv)
open Found.Pnp95 (sumI)
open Found.Pnp96 (sumI_le)
open Found.Pnp97 (sumI_add sumI_ext sumI_zero)
open Found.Pnp98 (mul_add' add_mul' mul_le_r)
open Found.Pnp99 (add_cancel_r' mul_le_l' mul_assoc')
open Found.Pnp101 (add_cancel_l')
def nth0 : List Nat -> Nat -> Nat
| [], _ => 0
| a :: _, 0 => a
| _ :: l, i + 1 => nth0 l i
def addL : List Nat -> List Nat -> List Nat
| [], m => m
| a :: l, [] => a :: l
| a :: l, b :: m => (a + b) :: addL l m
def mulL (k : Nat) : List Nat -> List Nat
| [] => []
| a :: l => (k * a) :: mulL k l
/-- The list with 1 at place i and 0 before it. -/
def unitL : Nat -> List Nat
| 0 => [1]
| i + 1 => 0 :: unitL i
structure Line where
P : List Nat
N : List Nat
d : Nat
e : Nat
def val (m : Nat) (l : List Nat) (a : Nat -> Bool) : Nat := sumI (fun i => nth0 l i * bv (a i)) m
/-- The line holds under a. -/
def Holds (m : Nat) (a : Nat -> Bool) (L : Line) : Prop := val m L.N a + L.e <= val m L.P a + L.d
def addLine (A B : Line) : Line := Line.mk (addL A.P B.P) (addL A.N B.N) (A.d + B.d) (A.e + B.e)
def mulLine (k : Nat) (A : Line) : Line := Line.mk (mulL k A.P) (mulL k A.N) (k * A.d) (k * A.e)
/-- Division: B is A divided by c, rounded. -/
structure DivOK (c : Nat) (A B : Line) : Prop where
pos : 1 <= c
coef : forall i, nth0 A.P i + c * nth0 B.N i = nth0 A.N i + c * nth0 B.P i
cst : c * B.e + A.d + 1 <= c * B.d + A.e + c
inductive CPProof (m : Nat) (F : List Line) : Line -> Type where
| ax (L : Line) (h : L ∈ F) : CPProof m F L
| nonneg (i : Nat) : CPProof m F (Line.mk (unitL i) [] 0 0)
| le1 (i : Nat) : CPProof m F (Line.mk [] (unitL i) 1 0)
| add (A B : Line) (p : CPProof m F A) (q : CPProof m F B) : CPProof m F (addLine A B)
| mul (k : Nat) (A : Line) (p : CPProof m F A) : CPProof m F (mulLine k A)
| div (c : Nat) (A B : Line) (p : CPProof m F A) (h : DivOK c A B) : CPProof m F B
def CPProof.size {m : Nat} {F : List Line} : {L : Line} -> CPProof m F L -> Nat
| _, .ax _ _ => 1
| _, .nonneg _ => 1
| _, .le1 _ => 1
| _, .add _ _ p q => p.size + q.size + 1
| _, .mul _ _ p => p.size + 1
| _, .div _ _ _ p _ => p.size + 1
/-- The contradiction 0 >= 1. -/
def falseLine : Line := Line.mk [] [] 0 1
theorem nth0_addL : forall (l m : List Nat) (i : Nat), nth0 (addL l m) i = nth0 l i + nth0 m i
| [], _, _ => (Nat.zero_add _).symm
| _ :: _, [], _ => rfl
| _ :: _, _ :: _, 0 => rfl
| _ :: l, _ :: m, i + 1 => nth0_addL l m i
theorem nth0_mulL (k : Nat) : forall (l : List Nat) (i : Nat), nth0 (mulL k l) i = k * nth0 l i
| [], _ => rfl
| _ :: _, 0 => rfl
| _ :: l, i + 1 => nth0_mulL k l i
theorem mul_sumI (c : Nat) (h : Nat -> Nat) : forall k, c * sumI h k = sumI (fun i => c * h i) k
| 0 => rfl
| k + 1 => by
show c * (sumI h k + h k) = sumI (fun i => c * h i) k + c * h k
rw [mul_add', mul_sumI c h k]
theorem val_add (m : Nat) (l l' : List Nat) (a : Nat -> Bool) : val m (addL l l') a = val m l a + val m l' a := by
show sumI (fun i => nth0 (addL l l') i * bv (a i)) m = sumI _ m + sumI _ m
rw [<- sumI_add]
refine sumI_ext _ _ ?_ m
intro i
show nth0 (addL l l') i * bv (a i) = nth0 l i * bv (a i) + nth0 l' i * bv (a i)
rw [nth0_addL, add_mul']
theorem val_mul (m k : Nat) (l : List Nat) (a : Nat -> Bool) : val m (mulL k l) a = k * val m l a := by
show sumI (fun i => nth0 (mulL k l) i * bv (a i)) m = k * sumI _ m
rw [mul_sumI]
refine sumI_ext _ _ ?_ m
intro i
show nth0 (mulL k l) i * bv (a i) = k * (nth0 l i * bv (a i))
rw [nth0_mulL, mul_assoc']
theorem nth0_unit : forall i j : Nat, nth0 (unitL i) j = bv (Nat.beq j i)
| 0, 0 => rfl
| 0, _ + 1 => rfl
| _ + 1, 0 => rfl
| i + 1, j + 1 => nth0_unit i j
theorem sumI_le_one (h : Nat -> Nat) (i : Nat) (h1 : h i <= 1) (h0 : forall j, Nat.beq j i = false -> h j = 0) :
forall k, sumI h k <= 1
| 0 => Nat.zero_le 1
| k + 1 => by
show sumI h k + h k <= 1
cases e : Nat.beq k i with
| false =>
rw [h0 k e]
exact sumI_le_one h i h1 h0 k
| true =>
have ki : k = i := Found.Pnp2.beq_eq k i e
have z : sumI h k = 0 := by
have t : forall k', k' <= k -> sumI h k' = 0 := by
intro k'
induction k' with
| zero => intro _; rfl
| succ k' ih =>
intro hk
show sumI h k' + h k' = 0
rw [ih (Nat.le_of_succ_le hk), h0 k' (Found.Pnp2.beq_false_of_ne k' i (fun e' => by
rw [e', <- ki] at hk; exact Nat.lt_irrefl k hk))]
exact t k (Nat.le_refl k)
rw [z, Nat.zero_add, ki]
exact h1
theorem val_unit_le (m i : Nat) (a : Nat -> Bool) : val m (unitL i) a <= 1 := by
refine sumI_le_one _ i ?_ ?_ m
· show nth0 (unitL i) i * bv (a i) <= 1
rw [nth0_unit, Found.Pnp62.beq_refl]
show 1 * bv (a i) <= 1
rw [Nat.one_mul]
cases a i <;> first | exact Nat.zero_le 1 | exact Nat.le_refl 1
· intro j hj
show nth0 (unitL i) j * bv (a j) = 0
rw [nth0_unit, hj]
exact Found.Pnp97.zero_mul' _
theorem val_nil (m : Nat) (a : Nat -> Bool) : val m [] a = 0 := by
show sumI (fun i => 0 * bv (a i)) m = 0
rw [sumI_ext _ (fun _ => 0) (fun i => Found.Pnp97.zero_mul' _) m]
exact sumI_zero m
theorem coef_val (m c : Nat) (A B : Line) (a : Nat -> Bool)
(h : forall i, nth0 A.P i + c * nth0 B.N i = nth0 A.N i + c * nth0 B.P i) :
val m A.P a + c * val m B.N a = val m A.N a + c * val m B.P a := by
rw [<- val_mul, <- val_mul, <- val_add, <- val_add]
refine sumI_ext _ _ ?_ m
intro i
show nth0 (addL A.P (mulL c B.N)) i * bv (a i) = nth0 (addL A.N (mulL c B.P)) i * bv (a i)
rw [nth0_addL, nth0_addL, nth0_mulL, nth0_mulL, h i]
theorem div_arith (c X Y U V d e d' e' : Nat) (cv : U + c * Y = V + c * X) (hA : V + e <= U + d)
(cst : c * e' + d + 1 <= c * d' + e + c) : Y + e' <= X + d' := by
cases Nat.lt_or_ge (X + d') (Y + e') with
| inr r => exact r
| inl l =>
have eB1 : c * (X + d' + 1) = c * X + (c * d' + c) := by
rw [mul_add', mul_add', Nat.mul_one, Nat.add_assoc]
have S1 : c * X + (c * d' + c) <= c * Y + c * e' := by
rw [<- eB1, <- mul_add']
exact mul_le_l' l c
have S2 := Nat.add_le_add S1 hA
have perm1 : (c * Y + c * e') + (U + d) = (V + c * X) + (c * e' + d) := by
rw [Found.Pnp93.add4 (c * Y) (c * e') U d, Nat.add_comm (c * Y) U, cv]
have perm2 : (c * X + (c * d' + c)) + (V + e) = (V + c * X) + (c * d' + e + c) := by
rw [Found.Pnp93.add4 (c * X) (c * d' + c) V e, Nat.add_comm (c * X) V, Nat.add_right_comm (c * d') c e]
rw [perm1, perm2] at S2
have S3 : (V + c * X) + (c * e' + d) + 1 <= (V + c * X) + (c * d' + e + c) := Nat.add_le_add_left cst (V + c * X)
exact absurd (Nat.le_trans S3 S2) (Nat.not_succ_le_self _)
theorem div_sound (m c : Nat) (A B : Line) (a : Nat -> Bool) (h : DivOK c A B) (hA : Holds m a A) : Holds m a B :=
div_arith c (val m B.P a) (val m B.N a) (val m A.P a) (val m A.N a) A.d A.e B.d B.e
(coef_val m c A B a h.coef) hA h.cst
theorem sound (m : Nat) (F : List Line) (a : Nat -> Bool) (hF : forall L, L ∈ F -> Holds m a L) :
forall (L : Line), CPProof m F L -> Holds m a L := by
intro L p
induction p with
| ax L h => exact hF L h
| nonneg i =>
show val m [] a + 0 <= val m (unitL i) a + 0
rw [val_nil]; exact Nat.zero_le _
| le1 i =>
show val m (unitL i) a + 0 <= val m [] a + 1
rw [val_nil]; exact val_unit_le m i a
| add A B p q ihp ihq =>
show val m (addL A.N B.N) a + (A.e + B.e) <= val m (addL A.P B.P) a + (A.d + B.d)
rw [val_add, val_add, Found.Pnp93.add4, Found.Pnp93.add4 (val m A.P a)]
exact Nat.add_le_add ihp ihq
| mul k A p ih =>
show val m (mulL k A.N) a + k * A.e <= val m (mulL k A.P) a + k * A.d
rw [val_mul, val_mul, <- mul_add', <- mul_add']
exact mul_le_l' ih k
| div c A B p h ih => exact div_sound m c A B a h ih
theorem refute_unsat (m : Nat) (F : List Line) (p : CPProof m F falseLine) (a : Nat -> Bool)
(hF : forall L, L ∈ F -> Holds m a L) : False := by
have t := sound m F a hF falseLine p
have t' : val m [] a + 1 <= val m [] a + 0 := t
rw [val_nil] at t'
exact Nat.not_succ_le_zero 0 t'
end Found.Pnp102
#print axioms Found.Pnp102.nth0_addL
#print axioms Found.Pnp102.nth0_mulL
#print axioms Found.Pnp102.mul_sumI
#print axioms Found.Pnp102.val_add
#print axioms Found.Pnp102.val_mul
#print axioms Found.Pnp102.nth0_unit
#print axioms Found.Pnp102.sumI_le_one
#print axioms Found.Pnp102.val_unit_le
#print axioms Found.Pnp102.val_nil
#print axioms Found.Pnp102.coef_val
#print axioms Found.Pnp102.div_arith
#print axioms Found.Pnp102.div_sound
#print axioms Found.Pnp102.sound
#print axioms Found.Pnp102.refute_unsat
#print axioms: 14 theorems, no axioms.
A clause becomes \(\sum _{x\ \mathrm {pos}}x+\sum _{x\ \mathrm {neg}}(1-x)\ge 1\); a true clause gives a line that holds, so a refutation of the lines of a CNF shows the CNF unsatisfiable.
found/Pnp103.lean
/-
T330 piece 103. found/Pnp103 -- clauses as cutting planes lines (proof complexity ladder,
step 2: cutting planes).
A clause l_1 v ... v l_s becomes the line sum_{x positive} x + sum_{x negative} (1 - x) >= 1,
kept on two sides: P has a 1 at every positive variable, N a 1 at every negative variable,
d counts the negative literals and e = 1. Proved:
* val_unit: the value of the unit list at v < m is x_v;
* line_base: every clause line satisfies N <= P + d under any assignment;
* line_holds: a true clause gives a line that holds;
* cnf_refute_unsat: a cutting planes refutation of the lines of a CNF (variables below m)
shows that the CNF has no satisfying assignment.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp102
namespace Found.Pnp103
open Found.RootComp (Lit Clause CNF evalLit evalClause evalCNF)
open Found.Pnp65 (bv)
open Found.Pnp95 (sumI)
open Found.Pnp102 (nth0 addL unitL Line val Holds CPProof falseLine val_add val_nil nth0_unit refute_unsat)
def lineOf : Clause -> Line
| [] => Line.mk [] [] 0 1
| (true, v) :: C => Line.mk (addL (unitL v) (lineOf C).P) (lineOf C).N (lineOf C).d (lineOf C).e
| (false, v) :: C => Line.mk (lineOf C).P (addL (unitL v) (lineOf C).N) ((lineOf C).d + 1) (lineOf C).e
def linesOf : CNF -> List Line
| [] => []
| C :: F => lineOf C :: linesOf F
/-- All variables of a clause are below m. -/
def VarsBelow (m : Nat) : Clause -> Prop
| [] => True
| l :: C => l.2 < m /\ VarsBelow m C
theorem val_unit (a : Nat -> Bool) (v : Nat) : forall m, v < m -> val m (unitL v) a = bv (a v)
| 0, h => absurd h (Nat.not_lt_zero v)
| m + 1, h => by
show sumI (fun i => nth0 (unitL v) i * bv (a i)) m + nth0 (unitL v) m * bv (a m) = bv (a v)
rw [nth0_unit]
cases Nat.lt_or_ge v m with
| inl l =>
have ih := val_unit a v m l
have e : Nat.beq m v = false := Found.Pnp2.beq_false_of_ne m v (fun x => by rw [x] at l; exact Nat.lt_irrefl v l)
rw [e]
show val m (unitL v) a + 0 * bv (a m) = bv (a v)
rw [ih, Found.Pnp97.zero_mul']
rfl
| inr r =>
have ev : v = m := Nat.le_antisymm (Nat.le_of_lt_succ h) r
rw [ev, Found.Pnp62.beq_refl]
have z : sumI (fun i => nth0 (unitL m) i * bv (a i)) m = 0 := by
have t : forall k, k <= m -> sumI (fun i => nth0 (unitL m) i * bv (a i)) k = 0 := by
intro k
induction k with
| zero => intro _; rfl
| succ k ih =>
intro hk
show sumI _ k + nth0 (unitL m) k * bv (a k) = 0
rw [ih (Nat.le_of_succ_le hk), nth0_unit,
Found.Pnp2.beq_false_of_ne k m (fun x => by rw [x] at hk; exact Nat.lt_irrefl m hk)]
show 0 + 0 * bv (a k) = 0
rw [Found.Pnp97.zero_mul']
try rfl
exact t m (Nat.le_refl m)
rw [z]
show 0 + 1 * bv (a m) = bv (a m)
rw [Nat.one_mul, Nat.zero_add]
theorem bv_le1 (b : Bool) : bv b <= 1 := by cases b <;> first | exact Nat.zero_le 1 | exact Nat.le_refl 1
theorem line_base (m : Nat) (a : Nat -> Bool) : forall C : Clause, VarsBelow m C ->
val m (lineOf C).N a <= val m (lineOf C).P a + (lineOf C).d
| [], _ => by show val m [] a <= val m [] a + 0; exact Nat.le_refl _
| (true, v) :: C, h => by
show val m (lineOf C).N a <= val m (addL (unitL v) (lineOf C).P) a + (lineOf C).d
rw [val_add]
refine Nat.le_trans (line_base m a C h.2) ?_
rw [Nat.add_assoc]
exact Nat.le_add_left _ _
| (false, v) :: C, h => by
show val m (addL (unitL v) (lineOf C).N) a <= val m (lineOf C).P a + ((lineOf C).d + 1)
rw [val_add, val_unit a v m h.1, Nat.add_comm (bv (a v)), <- Nat.add_assoc]
exact Nat.add_le_add (line_base m a C h.2) (bv_le1 _)
theorem line_holds (m : Nat) (a : Nat -> Bool) : forall C : Clause, VarsBelow m C -> evalClause a C = true ->
Holds m a (lineOf C)
| [], _, h => Bool.noConfusion h
| (true, v) :: C, hv, h => by
show val m (lineOf C).N a + (lineOf C).e <= val m (addL (unitL v) (lineOf C).P) a + (lineOf C).d
have e1 : (lineOf C).e = 1 := lineOf_e C
rw [e1, val_add, val_unit a v m hv.1]
have h' : (a v || evalClause a C) = true := h
cases ea : a v with
| true =>
show val m (lineOf C).N a + 1 <= 1 + val m (lineOf C).P a + (lineOf C).d
rw [Nat.add_assoc, Nat.add_comm 1]
exact Nat.add_le_add_right (line_base m a C hv.2) 1
| false =>
rw [ea] at h'
have t : Holds m a (lineOf C) := line_holds m a C hv.2 h'
have t' : val m (lineOf C).N a + (lineOf C).e <= val m (lineOf C).P a + (lineOf C).d := t
rw [e1] at t'
show val m (lineOf C).N a + 1 <= 0 + val m (lineOf C).P a + (lineOf C).d
rw [Nat.zero_add]
exact t'
| (false, v) :: C, hv, h => by
show val m (addL (unitL v) (lineOf C).N) a + (lineOf C).e <= val m (lineOf C).P a + ((lineOf C).d + 1)
have e1 : (lineOf C).e = 1 := lineOf_e C
rw [e1, val_add, val_unit a v m hv.1]
have h' : ((!a v) || evalClause a C) = true := h
cases ea : a v with
| false =>
show 0 + val m (lineOf C).N a + 1 <= val m (lineOf C).P a + ((lineOf C).d + 1)
rw [Nat.zero_add, <- Nat.add_assoc]
exact Nat.add_le_add_right (line_base m a C hv.2) 1
| true =>
rw [ea] at h'
have t : Holds m a (lineOf C) := line_holds m a C hv.2 h'
have t' : val m (lineOf C).N a + (lineOf C).e <= val m (lineOf C).P a + (lineOf C).d := t
rw [e1] at t'
show 1 + val m (lineOf C).N a + 1 <= val m (lineOf C).P a + ((lineOf C).d + 1)
rw [Nat.add_comm 1 (val m (lineOf C).N a), <- Nat.add_assoc]
exact Nat.add_le_add_right t' 1
where
lineOf_e : forall C : Clause, (lineOf C).e = 1
| [] => rfl
| (true, _) :: C => lineOf_e C
| (false, _) :: C => lineOf_e C
theorem mem_linesOf : forall (F : CNF) (L : Line), L ∈ linesOf F -> Exists fun C => C ∈ F /\ L = lineOf C
| [], _, h => by cases h
| C :: F, L, h => by
cases h with
| head => exact Exists.intro C (And.intro (List.Mem.head _) rfl)
| tail _ h' =>
cases mem_linesOf F L h' with
| intro D hD => exact Exists.intro D (And.intro (List.Mem.tail _ hD.1) hD.2)
theorem cnf_mem_true (a : Nat -> Bool) : forall (F : CNF) (C : Clause), evalCNF a F = true -> C ∈ F -> evalClause a C = true
| [], _, _, h => by cases h
| D :: F, C, hF, h => by
have hF' : (evalClause a D && evalCNF a F) = true := hF
cases h with
| head => exact Found.Pnp62.and_l hF'
| tail _ h' => exact cnf_mem_true a F C (Found.Pnp62.and_r hF') h'
theorem cnf_refute_unsat (m : Nat) (F : CNF) (hv : forall C, C ∈ F -> VarsBelow m C)
(p : CPProof m (linesOf F) falseLine) (a : Nat -> Bool) (ha : evalCNF a F = true) : False := by
refine refute_unsat m (linesOf F) p a ?_
intro L hL
cases mem_linesOf F L hL with
| intro C hC =>
rw [hC.2]
exact line_holds m a C (hv C hC.1) (cnf_mem_true a F C ha hC.1)
end Found.Pnp103
#print axioms Found.Pnp103.val_unit
#print axioms Found.Pnp103.bv_le1
#print axioms Found.Pnp103.line_base
#print axioms Found.Pnp103.line_holds
#print axioms Found.Pnp103.mem_linesOf
#print axioms Found.Pnp103.cnf_mem_true
#print axioms Found.Pnp103.cnf_refute_unsat
#print axioms: 7 theorems, no axioms.
Edge variables \(p_{ij}\), clique position variables \(q_{li}\) and colour variables \(r_{ia}\), all below \(n^2+kn+nc\); the edges occur positively in the clique part and negatively in the colouring part.
found/Pnp104.lean
/-
T330 piece 104. found/Pnp104 -- the clique-colouring formula (proof complexity ladder, step
2: cutting planes; the formula of Pudlak 1997).
For n vertices, clique size k and c colours the variables are
p(i, j) = i n + j (edge from i to j), i, j < n,
q(l, i) = n n + (l n + i) (position l of the clique is i), l < k, i < n,
r(i, a) = n n + k n + (i c + a) (vertex i has colour a), i < n, a < c,
all below m = n n + k n + n c. Clauses:
every position has a vertex; no vertex fills two positions l < l'; two positions l < l'
with vertices i, j force the edge p(i, j); every vertex has a colour; an edge p(i, j)
forbids colour a at both i and j.
The edge variables occur positively in the clique part and negatively in the colouring part.
Proved:
* mem_app_or, mem_upto, mem_bindUp: membership in the generated lists (by hand);
* lin_lt: a < A, b < B -> a B + b < A B;
* pv_lt, qv_lt, rv_lt: every variable is below m;
* cc_vars: every clause of the formula has its variables below m.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp103
namespace Found.Pnp104
open Found.RootComp (Lit Clause CNF)
open Found.Pnp103 (VarsBelow)
open Found.Pnp98 (add_mul' mul_le_r)
def upto {α : Type} (f : Nat -> α) : Nat -> List α
| 0 => []
| k + 1 => f k :: upto f k
def bindUp {α : Type} (g : Nat -> List α) : Nat -> List α
| 0 => []
| k + 1 => g k ++ bindUp g k
def pv (n i j : Nat) : Nat := i * n + j
def qv (n l i : Nat) : Nat := n * n + (l * n + i)
def rv (n k c i a : Nat) : Nat := n * n + k * n + (i * c + a)
def mCC (n k c : Nat) : Nat := n * n + k * n + n * c
/-- Position l has some vertex. -/
def qAll (n l : Nat) : Clause := upto (fun i => (true, qv n l i)) n
/-- Vertex i does not fill both l and l'. -/
def qTwo (n l l' i : Nat) : Clause := [(false, qv n l i), (false, qv n l' i)]
/-- Positions l, l' with vertices i, j force the edge (i, j). -/
def qEdge (n l l' i j : Nat) : Clause := [(false, qv n l i), (false, qv n l' j), (true, pv n i j)]
/-- Vertex i has some colour. -/
def rAll (n k c i : Nat) : Clause := upto (fun a => (true, rv n k c i a)) c
/-- An edge (i, j) forbids colour a at both ends. -/
def rEdge (n k c i j a : Nat) : Clause := [(false, pv n i j), (false, rv n k c i a), (false, rv n k c j a)]
/-- Pairs l < l' below k. -/
def pairsK {α : Type} (k : Nat) (g : Nat -> Nat -> List α) : List α :=
bindUp (fun l' => bindUp (fun l => g l l') l') k
def cliquePart (n k : Nat) : CNF :=
upto (fun l => qAll n l) k ++
pairsK k (fun l l' => upto (fun i => qTwo n l l' i) n) ++
pairsK k (fun l l' => bindUp (fun i => upto (fun j => qEdge n l l' i j) n) n)
def colourPart (n k c : Nat) : CNF :=
upto (fun i => rAll n k c i) n ++
bindUp (fun i => bindUp (fun j => upto (fun a => rEdge n k c i j a) c) n) n
def CC (n k c : Nat) : CNF := cliquePart n k ++ colourPart n k c
theorem mem_app_or {α : Type} (x : α) : forall (A B : List α), x ∈ A ++ B -> x ∈ A \/ x ∈ B
| [], _, h => Or.inr h
| _ :: A, B, h => by
cases h with
| head => exact Or.inl (List.Mem.head _)
| tail _ h' =>
cases mem_app_or x A B h' with
| inl a => exact Or.inl (List.Mem.tail _ a)
| inr b => exact Or.inr b
theorem mem_upto {α : Type} (f : Nat -> α) (x : α) : forall k, x ∈ upto f k -> Exists fun i => i < k /\ x = f i
| 0, h => by cases h
| k + 1, h => by
cases h with
| head => exact Exists.intro k (And.intro (Nat.lt_succ_self k) rfl)
| tail _ h' =>
cases mem_upto f x k h' with
| intro i hi => exact Exists.intro i (And.intro (Nat.lt_succ_of_lt hi.1) hi.2)
theorem mem_bindUp {α : Type} (g : Nat -> List α) (x : α) : forall k, x ∈ bindUp g k -> Exists fun i => i < k /\ x ∈ g i
| 0, h => by cases h
| k + 1, h => by
cases mem_app_or x (g k) (bindUp g k) h with
| inl a => exact Exists.intro k (And.intro (Nat.lt_succ_self k) a)
| inr b =>
cases mem_bindUp g x k b with
| intro i hi => exact Exists.intro i (And.intro (Nat.lt_succ_of_lt hi.1) hi.2)
theorem mem_pairsK {α : Type} (k : Nat) (g : Nat -> Nat -> List α) (x : α) (h : x ∈ pairsK k g) :
Exists fun l => Exists fun l' => l < l' /\ l' < k /\ x ∈ g l l' :=
match mem_bindUp _ x k h with
| Exists.intro l' hl' =>
match mem_bindUp _ x l' hl'.2 with
| Exists.intro l hl => Exists.intro l (Exists.intro l' (And.intro hl.1 (And.intro hl'.1 hl.2)))
theorem lin_lt (a b A B : Nat) (ha : a < A) (hb : b < B) : a * B + b < A * B := by
have t : a * B + b < a * B + B := Nat.add_lt_add_left hb _
have e : (a + 1) * B = a * B + B := by rw [add_mul', Nat.one_mul]
rw [<- e] at t
exact Nat.lt_of_lt_of_le t (mul_le_r ha B)
theorem pv_lt (n k c i j : Nat) (hi : i < n) (hj : j < n) : pv n i j < mCC n k c :=
Nat.lt_of_lt_of_le (lin_lt i j n n hi hj) (Nat.le_trans (Nat.le_add_right _ _) (Nat.le_add_right _ _))
theorem qv_lt (n k c l i : Nat) (hl : l < k) (hi : i < n) : qv n l i < mCC n k c := by
show n * n + (l * n + i) < n * n + k * n + n * c
exact Nat.lt_of_lt_of_le (Nat.add_lt_add_left (lin_lt l i k n hl hi) _) (Nat.le_add_right _ _)
theorem rv_lt (n k c i a : Nat) (hi : i < n) (ha : a < c) : rv n k c i a < mCC n k c := by
show n * n + k * n + (i * c + a) < n * n + k * n + n * c
exact Nat.add_lt_add_left (lin_lt i a n c hi ha) _
theorem vars_upto (m : Nat) (f : Nat -> Lit) : forall k, (forall i, i < k -> (f i).2 < m) -> VarsBelow m (upto f k)
| 0, _ => True.intro
| k + 1, h => And.intro (h k (Nat.lt_succ_self k)) (vars_upto m f k (fun i hi => h i (Nat.lt_succ_of_lt hi)))
theorem cc_vars (n k c : Nat) (C : Clause) (hC : C ∈ CC n k c) : VarsBelow (mCC n k c) C := by
cases mem_app_or C _ _ hC with
| inl h1 =>
cases mem_app_or C _ _ h1 with
| inl h2 =>
cases mem_app_or C _ _ h2 with
| inl h3 =>
cases mem_upto _ C k h3 with
| intro l hl =>
rw [hl.2]
exact vars_upto _ _ n (fun i hi => qv_lt n k c l i hl.1 hi)
| inr h3 =>
cases mem_pairsK k _ C h3 with
| intro l hl =>
cases hl with
| intro l' hl' =>
cases mem_upto _ C n hl'.2.2 with
| intro i hi =>
rw [hi.2]
exact And.intro (qv_lt n k c l i (Nat.lt_trans hl'.1 hl'.2.1) hi.1)
(And.intro (qv_lt n k c l' i hl'.2.1 hi.1) True.intro)
| inr h2 =>
cases mem_pairsK k _ C h2 with
| intro l hl =>
cases hl with
| intro l' hl' =>
cases mem_bindUp _ C n hl'.2.2 with
| intro i hi =>
cases mem_upto _ C n hi.2 with
| intro j hj =>
rw [hj.2]
exact And.intro (qv_lt n k c l i (Nat.lt_trans hl'.1 hl'.2.1) hi.1)
(And.intro (qv_lt n k c l' j hl'.2.1 hj.1) (And.intro (pv_lt n k c i j hi.1 hj.1) True.intro))
| inr h1 =>
cases mem_app_or C _ _ h1 with
| inl h2 =>
cases mem_upto _ C n h2 with
| intro i hi =>
rw [hi.2]
exact vars_upto _ _ c (fun a ha => rv_lt n k c i a hi.1 ha)
| inr h2 =>
cases mem_bindUp _ C n h2 with
| intro i hi =>
cases mem_bindUp _ C n hi.2 with
| intro j hj =>
cases mem_upto _ C c hj.2 with
| intro a ha =>
rw [ha.2]
exact And.intro (pv_lt n k c i j hi.1 hj.1)
(And.intro (rv_lt n k c i a hi.1 ha.1) (And.intro (rv_lt n k c j a hj.1 ha.1) True.intro))
end Found.Pnp104
#print axioms Found.Pnp104.mem_app_or
#print axioms Found.Pnp104.mem_upto
#print axioms Found.Pnp104.mem_bindUp
#print axioms Found.Pnp104.mem_pairsK
#print axioms Found.Pnp104.lin_lt
#print axioms Found.Pnp104.pv_lt
#print axioms Found.Pnp104.qv_lt
#print axioms Found.Pnp104.rv_lt
#print axioms Found.Pnp104.vars_upto
#print axioms Found.Pnp104.cc_vars
#print axioms: 10 theorems, no axioms.
An integer is a pair \((a,b)\) for \(a-b\); floor division by \(c'+1\) is defined by structural recursion, unique, shifts by multiples and is monotone.
found/Pnp105.lean
/-
T330 piece 105. found/Pnp105 -- integers as pairs and floor division (proof complexity
ladder, step 2: cutting planes; tools for the monotone interpolant of Pudlak 1997).
The interpolant of a cutting planes refutation computes an integer per line. An integer is
a pair (a, b) of natural numbers standing for a - b; (a, b) <= (a', b') when a + b' <= a' + b.
Floor division by c' + 1 of (a, b) is (divN (a + c' b) (c' + 1), b). divN is defined by
structural recursion. Proved:
* divN_spec: (c' + 1) * divN x (c' + 1) <= x < (c' + 1) * (divN x (c' + 1) + 1);
* divN_mono: x <= y -> divN x c <= divN y c;
* quot_unique, divN_shift: the quotient is unique, and shifts by multiples of c;
* zle_refl, zle_trans: the order;
* zdiv_low, zdiv_high: (c' + 1) * floor(v / (c' + 1)) <= v < (c' + 1) * (floor + 1), on pairs;
* zdiv_mono: floor division is monotone.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp104
namespace Found.Pnp105
open Found.Pnp98 (mul_add' add_mul' mul_le_r)
open Found.Pnp99 (add_cancel_r' mul_le_l' mul_assoc')
open Found.Pnp101 (add_cancel_l')
/-- Floor division of x by c (c >= 1), by structural recursion on x. -/
def divN : Nat -> Nat -> Nat
| 0, _ => 0
| x + 1, c => Found.Pnp65.selN (Nat.ble (c * (divN x c + 1)) (x + 1)) (divN x c + 1) (divN x c)
theorem lt_add_succ (n c' : Nat) : n < n + (c' + 1) := Nat.lt_succ_of_le (Nat.le_add_right n c')
theorem divN_spec (c' : Nat) : forall x, (c' + 1) * divN x (c' + 1) <= x /\ x < (c' + 1) * (divN x (c' + 1) + 1)
| 0 => And.intro (Nat.le_refl 0) (by
show 0 < (c' + 1) * (0 + 1)
rw [Nat.zero_add, Nat.mul_one]; exact Nat.succ_pos c')
| x + 1 => by
have ih := divN_spec c' x
show (c' + 1) * Found.Pnp65.selN (Nat.ble ((c' + 1) * (divN x (c' + 1) + 1)) (x + 1)) (divN x (c' + 1) + 1) (divN x (c' + 1)) <= x + 1 /\
x + 1 < (c' + 1) * (Found.Pnp65.selN (Nat.ble ((c' + 1) * (divN x (c' + 1) + 1)) (x + 1)) (divN x (c' + 1) + 1) (divN x (c' + 1)) + 1)
cases e : Nat.ble ((c' + 1) * (divN x (c' + 1) + 1)) (x + 1) with
| true =>
have l := Nat.le_of_ble_eq_true e
refine And.intro l ?_
show x + 1 < (c' + 1) * (divN x (c' + 1) + 1 + 1)
rw [mul_add' (c' + 1) (divN x (c' + 1) + 1) 1, Nat.mul_one]
exact Nat.lt_of_le_of_lt (Nat.succ_le_of_lt ih.2) (lt_add_succ _ c')
| false =>
refine And.intro (Nat.le_trans ih.1 (Nat.le_succ x)) ?_
show x + 1 < (c' + 1) * (divN x (c' + 1) + 1)
cases Nat.lt_or_ge (x + 1) ((c' + 1) * (divN x (c' + 1) + 1)) with
| inl l => exact l
| inr r => rw [Nat.ble_eq_true_of_le r] at e; exact Bool.noConfusion e
/-- Uniqueness of the quotient. -/
theorem quot_unique (c q q' y : Nat) (h1 : c * q <= y) (h2 : y < c * (q + 1)) (h1' : c * q' <= y) (h2' : y < c * (q' + 1)) :
q = q' := by
cases Nat.lt_or_ge q q' with
| inl l => exact absurd (Nat.lt_of_lt_of_le h2 (Nat.le_trans (mul_le_l' l c) h1')) (Nat.lt_irrefl y)
| inr r =>
cases Nat.lt_or_ge q' q with
| inl l => exact absurd (Nat.lt_of_lt_of_le h2' (Nat.le_trans (mul_le_l' l c) h1)) (Nat.lt_irrefl y)
| inr r' => exact Nat.le_antisymm r' r
theorem divN_shift (c' x t : Nat) : divN (x + (c' + 1) * t) (c' + 1) = divN x (c' + 1) + t := by
have s1 := divN_spec c' x
have s2 := divN_spec c' (x + (c' + 1) * t)
refine quot_unique (c' + 1) _ _ (x + (c' + 1) * t) s2.1 s2.2 ?_ ?_
· rw [mul_add']; exact Nat.add_le_add_right s1.1 _
· rw [Nat.add_right_comm, mul_add']; exact Nat.add_lt_add_right s1.2 _
theorem divN_mono (c' x y : Nat) (h : x <= y) : divN x (c' + 1) <= divN y (c' + 1) := by
cases Nat.lt_or_ge (divN y (c' + 1)) (divN x (c' + 1)) with
| inr r => exact r
| inl l =>
have a := (divN_spec c' x).1
have b := (divN_spec c' y).2
exact absurd (Nat.lt_of_lt_of_le b (Nat.le_trans (mul_le_l' l (c' + 1)) (Nat.le_trans a h))) (Nat.lt_irrefl y)
/-- Integers as pairs: (a, b) stands for a - b. -/
def zle (v w : Nat × Nat) : Prop := v.1 + w.2 <= w.1 + v.2
/-- Floor division by c' + 1. -/
def zdiv (c' : Nat) (v : Nat × Nat) : Nat × Nat := (divN (v.1 + c' * v.2) (c' + 1), v.2)
theorem zle_refl (v : Nat × Nat) : zle v v := Nat.le_refl _
theorem zle_trans (u v w : Nat × Nat) (h1 : zle u v) (h2 : zle v w) : zle u w := by
have s := Nat.add_le_add h1 h2
have e1 : (u.1 + v.2) + (v.1 + w.2) = (u.1 + w.2) + (v.2 + v.1) := by
rw [Nat.add_comm v.1 w.2, Found.Pnp93.add4]
have e2 : (v.1 + u.2) + (w.1 + v.2) = (w.1 + u.2) + (v.2 + v.1) := by
rw [Nat.add_comm v.1 u.2, Found.Pnp93.add4, Nat.add_comm u.2 w.1, Nat.add_comm v.2 v.1, Nat.add_comm v.1 v.2]
rw [e1, e2] at s
exact add_cancel_r' _ s
theorem zdiv_low (c' : Nat) (v : Nat × Nat) :
(c' + 1) * (zdiv c' v).1 + v.2 <= v.1 + (c' + 1) * v.2 := by
have s := (divN_spec c' (v.1 + c' * v.2)).1
have e : v.1 + (c' + 1) * v.2 = v.1 + c' * v.2 + v.2 := by rw [add_mul', Nat.one_mul, Nat.add_assoc]
rw [e]
exact Nat.add_le_add_right s _
theorem zdiv_high (c' : Nat) (v : Nat × Nat) :
v.1 + (c' + 1) * v.2 < (c' + 1) * ((zdiv c' v).1 + 1) + v.2 := by
have s := (divN_spec c' (v.1 + c' * v.2)).2
have e : v.1 + (c' + 1) * v.2 = v.1 + c' * v.2 + v.2 := by rw [add_mul', Nat.one_mul, Nat.add_assoc]
rw [e]
exact Nat.add_lt_add_right s _
theorem zdiv_mono (c' : Nat) (v w : Nat × Nat) (h : zle v w) : zle (zdiv c' v) (zdiv c' w) := by
show divN (v.1 + c' * v.2) (c' + 1) + w.2 <= divN (w.1 + c' * w.2) (c' + 1) + v.2
rw [<- divN_shift c' (v.1 + c' * v.2) w.2, <- divN_shift c' (w.1 + c' * w.2) v.2]
apply divN_mono
-- v1 + c' v2 + (c'+1) w2 <= w1 + c' w2 + (c'+1) v2
have e1 : v.1 + c' * v.2 + (c' + 1) * w.2 = (v.1 + w.2) + c' * (v.2 + w.2) := by
rw [add_mul', Nat.one_mul, mul_add', Nat.add_comm (c' * w.2) w.2]
exact Found.Pnp93.add4 _ _ _ _
have e2 : w.1 + c' * w.2 + (c' + 1) * v.2 = (w.1 + v.2) + c' * (v.2 + w.2) := by
rw [add_mul', Nat.one_mul, mul_add', Nat.add_comm (c' * v.2) v.2, Found.Pnp93.add4, Nat.add_comm (c' * w.2) (c' * v.2)]
rw [e1, e2]
exact Nat.add_le_add_right h _
end Found.Pnp105
#print axioms Found.Pnp105.lt_add_succ
#print axioms Found.Pnp105.divN_spec
#print axioms Found.Pnp105.quot_unique
#print axioms Found.Pnp105.divN_shift
#print axioms Found.Pnp105.divN_mono
#print axioms Found.Pnp105.zle_refl
#print axioms Found.Pnp105.zle_trans
#print axioms Found.Pnp105.zdiv_low
#print axioms Found.Pnp105.zdiv_high
#print axioms Found.Pnp105.zdiv_mono
#print axioms: 10 theorems, no axioms.
Two lists of atoms with the same count of every atom have the same sum; the counts are compared by a Boolean function evaluated on concrete lists.
found/Pnp106.lean
/-
T330 piece 106. found/Pnp106 -- a checker for rearranged sums (tool for the cutting planes
interpolant; proof complexity ladder, step 2).
The arithmetic of the interpolant adds several inequalities and rearranges sums of up to a
dozen atoms. sL env [i1, ..., ik] is env i1 + (env i2 + ... + env ik), definitionally the
right-nested sum, so a hypothesis written right-nested is already of this form. Two index
lists with the same count of every index have equal sums; the counts are compared by a Bool
function evaluated by rfl on concrete lists. Proved:
* sL_cons, sL_app: the sum of a cons and of an append;
* sumI_point: a bounded sum of an indicator picks one value;
* sL_counts: a sum is the bounded sum of counts times values;
* sL_perm: lists with the same counts (checked by sameC) have the same sum.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp105
namespace Found.Pnp106
open Found.Pnp65 (bv)
open Found.Pnp95 (sumI)
open Found.Pnp97 (sumI_add sumI_ext sumI_zero zero_mul')
open Found.Pnp98 (add_mul')
open Found.Pnp90 (allI allI_true)
def sL (env : Nat -> Nat) : List Nat -> Nat
| [] => 0
| [i] => env i
| i :: j :: l => env i + sL env (j :: l)
def cnt (k : Nat) : List Nat -> Nat
| [] => 0
| i :: l => bv (Nat.beq i k) + cnt k l
def allLtB (K : Nat) : List Nat -> Bool
| [] => true
| i :: l => Nat.blt i K && allLtB K l
def sameC (K : Nat) (l1 l2 : List Nat) : Bool := allI (fun k => Nat.beq (cnt k l1) (cnt k l2)) K
theorem sL_cons (env : Nat -> Nat) (i : Nat) : forall l, sL env (i :: l) = env i + sL env l
| [] => rfl
| _ :: _ => rfl
theorem sL_app (env : Nat -> Nat) (l2 : List Nat) : forall l1, sL env (l1 ++ l2) = sL env l1 + sL env l2
| [] => (Nat.zero_add _).symm
| i :: l1 => by
show sL env (i :: (l1 ++ l2)) = sL env (i :: l1) + sL env l2
rw [sL_cons, sL_cons, sL_app env l2 l1, Nat.add_assoc]
theorem sumI_point (f : Nat -> Nat) (i : Nat) : forall K, i < K -> sumI (fun k => bv (Nat.beq i k) * f k) K = f i
| 0, h => absurd h (Nat.not_lt_zero i)
| K + 1, h => by
show sumI (fun k => bv (Nat.beq i k) * f k) K + bv (Nat.beq i K) * f K = f i
cases Nat.lt_or_ge i K with
| inl l =>
rw [sumI_point f i K l, Found.Pnp2.beq_false_of_ne i K (fun e => by rw [e] at l; exact Nat.lt_irrefl K l)]
show f i + 0 * f K = f i
rw [zero_mul']
rfl
| inr r =>
have e : i = K := Nat.le_antisymm (Nat.le_of_lt_succ h) r
rw [e, Found.Pnp62.beq_refl]
have z : sumI (fun k => bv (Nat.beq K k) * f k) K = 0 := by
have t : forall k', k' <= K -> sumI (fun k => bv (Nat.beq K k) * f k) k' = 0 := by
intro k'
induction k' with
| zero => intro _; rfl
| succ k' ih =>
intro hk
show sumI _ k' + bv (Nat.beq K k') * f k' = 0
rw [ih (Nat.le_of_succ_le hk), Found.Pnp2.beq_false_of_ne K k' (fun x => by rw [x] at hk; exact Nat.lt_irrefl k' hk)]
show 0 + 0 * f k' = 0
rw [zero_mul']
exact t K (Nat.le_refl K)
rw [z]
show 0 + 1 * f K = f K
rw [Nat.one_mul, Nat.zero_add]
theorem allLt_head {K i : Nat} {l : List Nat} (h : allLtB K (i :: l) = true) : i < K :=
Found.Pnp90.blt_lt i K (Found.Pnp62.and_l h)
theorem allLt_tail {K i : Nat} {l : List Nat} (h : allLtB K (i :: l) = true) : allLtB K l = true :=
Found.Pnp62.and_r h
theorem sL_counts (env : Nat -> Nat) (K : Nat) : forall l, allLtB K l = true -> sL env l = sumI (fun k => cnt k l * env k) K
| [], _ => by
show 0 = sumI (fun k => 0 * env k) K
rw [sumI_ext _ (fun _ => 0) (fun k => zero_mul' _) K, sumI_zero]
| i :: l, h => by
rw [sL_cons, sL_counts env K l (allLt_tail h)]
show env i + sumI (fun k => cnt k l * env k) K = sumI (fun k => (bv (Nat.beq i k) + cnt k l) * env k) K
rw [sumI_ext _ (fun k => bv (Nat.beq i k) * env k + cnt k l * env k) (fun k => add_mul' _ _ _) K, sumI_add,
sumI_point env i K (allLt_head h)]
theorem sumI_ext_lt (f g : Nat -> Nat) : forall K, (forall k, k < K -> f k = g k) -> sumI f K = sumI g K
| 0, _ => rfl
| K + 1, h => by
show sumI f K + f K = sumI g K + g K
rw [sumI_ext_lt f g K (fun k hk => h k (Nat.lt_succ_of_lt hk)), h K (Nat.lt_succ_self K)]
theorem sL_perm (env : Nat -> Nat) (K : Nat) (l1 l2 : List Nat) (h1 : allLtB K l1 = true) (h2 : allLtB K l2 = true)
(hc : sameC K l1 l2 = true) : sL env l1 = sL env l2 := by
rw [sL_counts env K l1 h1, sL_counts env K l2 h2]
refine sumI_ext_lt _ _ K ?_
intro k hk
show cnt k l1 * env k = cnt k l2 * env k
rw [Found.Pnp2.beq_eq _ _ (allI_true _ K hc k hk)]
/-- A test: x + (y + z) + (u + v) = v + (z + (x + (u + y))). -/
theorem sL_test (x y z u v : Nat) : (x + (y + z)) + (u + v) = v + (z + (x + (u + y))) := by
have e := sL_perm (Found.Pnp102.nth0 [x, y, z, u, v]) 5 ([0, 1, 2] ++ [3, 4]) [4, 2, 0, 3, 1] rfl rfl rfl
rw [sL_app] at e
exact e
end Found.Pnp106
#print axioms Found.Pnp106.sL_cons
#print axioms Found.Pnp106.sL_app
#print axioms Found.Pnp106.sumI_point
#print axioms Found.Pnp106.allLt_head
#print axioms Found.Pnp106.allLt_tail
#print axioms Found.Pnp106.sL_counts
#print axioms Found.Pnp106.sumI_ext_lt
#print axioms Found.Pnp106.sL_perm
#print axioms Found.Pnp106.sL_test
#print axioms: 9 theorems, no axioms.
The value of a line splits into the part on the private variables of \(A\) and the rest; both invariants of the interpolant survive floor division.
found/Pnp107.lean
/-
T330 piece 107. found/Pnp107 -- partial values of lines and the arithmetic of the division
step of the interpolant (proof complexity ladder, step 2: cutting planes; Pudlak 1997).
vS m S l a is the part of the value of the coefficient list l on the variables selected by
S. The interpolant splits each line into the part on the private variables of A (S = Q) and
the rest. Proved:
* vS_add, vS_mul, vS_nil, vS_unit_le, vS_unit_zero, val_split: partial values;
* coef_vS: the coefficient identity of a division step holds on every part;
* coreA, divA: the A-side invariant survives floor division of the interpolant;
* coreB, divB: the B-side invariant survives it, with the rounded constant.
Sums are rearranged with Pnp106.sL_perm. Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp106
namespace Found.Pnp107
open Found.Pnp65 (bv)
open Found.Pnp95 (sumI)
open Found.Pnp97 (sumI_add sumI_ext sumI_zero zero_mul')
open Found.Pnp98 (mul_add' add_mul' mul_le_r)
open Found.Pnp99 (add_cancel_r' mul_le_l' mul_assoc')
open Found.Pnp101 (add_cancel_l')
open Found.Pnp102 (nth0 addL mulL unitL val nth0_addL nth0_mulL nth0_unit mul_sumI sumI_le_one)
open Found.Pnp106 (sL sL_app sL_cons sL_perm)
def vS (m : Nat) (S : Nat -> Bool) (l : List Nat) (a : Nat -> Bool) : Nat :=
sumI (fun i => bv (S i) * (nth0 l i * bv (a i))) m
theorem vS_add (m : Nat) (S : Nat -> Bool) (l l' : List Nat) (a : Nat -> Bool) :
vS m S (addL l l') a = vS m S l a + vS m S l' a := by
show sumI _ m = sumI _ m + sumI _ m
rw [<- sumI_add]
refine sumI_ext _ _ ?_ m
intro i
show bv (S i) * (nth0 (addL l l') i * bv (a i)) = bv (S i) * (nth0 l i * bv (a i)) + bv (S i) * (nth0 l' i * bv (a i))
rw [nth0_addL, add_mul', mul_add']
theorem vS_mul (m : Nat) (S : Nat -> Bool) (k : Nat) (l : List Nat) (a : Nat -> Bool) :
vS m S (mulL k l) a = k * vS m S l a := by
show sumI _ m = k * sumI _ m
rw [mul_sumI]
refine sumI_ext _ _ ?_ m
intro i
show bv (S i) * (nth0 (mulL k l) i * bv (a i)) = k * (bv (S i) * (nth0 l i * bv (a i)))
rw [nth0_mulL, mul_assoc', <- mul_assoc', Nat.mul_comm (bv (S i)) k, mul_assoc']
theorem vS_nil (m : Nat) (S : Nat -> Bool) (a : Nat -> Bool) : vS m S [] a = 0 := by
show sumI (fun i => bv (S i) * (0 * bv (a i))) m = 0
rw [sumI_ext _ (fun _ => 0) (fun i => by rw [zero_mul']; rfl) m]
exact sumI_zero m
theorem bv_le1 (b : Bool) : bv b <= 1 := by cases b <;> first | exact Nat.zero_le 1 | exact Nat.le_refl 1
theorem bv_mul_le1 (x y : Bool) : bv x * bv y <= 1 := by
cases x
· show 0 * bv y <= 1
rw [zero_mul']; exact Nat.zero_le 1
· show 1 * bv y <= 1
rw [Nat.one_mul]; exact bv_le1 y
theorem vS_unit_le (m : Nat) (S : Nat -> Bool) (i : Nat) (a : Nat -> Bool) : vS m S (unitL i) a <= 1 := by
refine sumI_le_one _ i ?_ ?_ m
· show bv (S i) * (nth0 (unitL i) i * bv (a i)) <= 1
rw [nth0_unit, Found.Pnp62.beq_refl]
show bv (S i) * (1 * bv (a i)) <= 1
rw [Nat.one_mul]
exact bv_mul_le1 _ _
· intro j hj
show bv (S j) * (nth0 (unitL i) j * bv (a j)) = 0
rw [nth0_unit, hj]
show bv (S j) * (0 * bv (a j)) = 0
rw [zero_mul']
rfl
theorem vS_unit_zero (m : Nat) (S : Nat -> Bool) (i : Nat) (a : Nat -> Bool) (hS : S i = false) :
vS m S (unitL i) a = 0 := by
show sumI _ m = 0
rw [sumI_ext _ (fun _ => 0) ?_ m]
· exact sumI_zero m
· intro j
show bv (S j) * (nth0 (unitL i) j * bv (a j)) = 0
rw [nth0_unit]
cases e : Nat.beq j i with
| false => show bv (S j) * (0 * bv (a j)) = 0; rw [zero_mul']; rfl
| true =>
rw [Found.Pnp2.beq_eq j i e, hS]
exact zero_mul' _
theorem val_split (m : Nat) (S : Nat -> Bool) (l : List Nat) (a : Nat -> Bool) :
val m l a = vS m S l a + vS m (fun i => !(S i)) l a := by
show sumI _ m = sumI _ m + sumI _ m
rw [<- sumI_add]
refine sumI_ext _ _ ?_ m
intro i
show nth0 l i * bv (a i) = bv (S i) * (nth0 l i * bv (a i)) + bv (!(S i)) * (nth0 l i * bv (a i))
cases S i
· show nth0 l i * bv (a i) = 0 * (nth0 l i * bv (a i)) + 1 * (nth0 l i * bv (a i))
rw [zero_mul', Nat.one_mul, Nat.zero_add]
· show nth0 l i * bv (a i) = 1 * (nth0 l i * bv (a i)) + 0 * (nth0 l i * bv (a i))
rw [zero_mul', Nat.one_mul]; rfl
theorem coef_vS (m c : Nat) (S : Nat -> Bool) (AP AN BP BN : List Nat) (a : Nat -> Bool)
(h : forall i, nth0 AP i + c * nth0 BN i = nth0 AN i + c * nth0 BP i) :
vS m S AP a + c * vS m S BN a = vS m S AN a + c * vS m S BP a := by
rw [<- vS_mul, <- vS_mul, <- vS_add, <- vS_add]
refine sumI_ext _ _ ?_ m
intro i
show bv (S i) * (nth0 (addL AP (mulL c BN)) i * bv (a i)) = bv (S i) * (nth0 (addL AN (mulL c BP)) i * bv (a i))
rw [nth0_addL, nth0_addL, nth0_mulL, nth0_mulL, h i]
theorem coreA (x y U V E1 E2 Qc F : Nat) (t1 : x + Qc <= y + F) (ia : V + E2 <= U + E1) (cv : U + y = V + x)
(zh : E1 + F < Qc + E2) : False := by
have s := Nat.add_le_add t1 ia
let env := nth0 [x, Qc, V, E2, y, F, U, E1]
have p1 : sL env ([0, 1] ++ [2, 3]) = sL env ([2, 0] ++ [1, 3]) := sL_perm env 8 _ _ rfl rfl rfl
have p2 : sL env ([4, 5] ++ [6, 7]) = sL env ([6, 4] ++ [7, 5]) := sL_perm env 8 _ _ rfl rfl rfl
rw [sL_app, sL_app] at p1
rw [sL_app, sL_app] at p2
have s2 : (V + x) + (Qc + E2) <= (U + y) + (E1 + F) :=
Nat.le_trans (Nat.le_of_eq p1.symm) (Nat.le_trans s (Nat.le_of_eq p2))
rw [cv] at s2
exact absurd (Nat.lt_of_lt_of_le zh (add_cancel_l' s2)) (Nat.lt_irrefl _)
/-- A side of the division step. -/
theorem divA (c' X Y U V E1 E2 q : Nat) (cv : U + (c' + 1) * Y = V + (c' + 1) * X) (ia : V + E2 <= U + E1)
(zh : E1 + (c' + 1) * E2 < (c' + 1) * (q + 1) + E2) : Y + E2 <= X + q := by
cases Nat.lt_or_ge (X + q) (Y + E2) with
| inr r => exact r
| inl l =>
have t1 : (c' + 1) * X + (c' + 1) * (q + 1) <= (c' + 1) * Y + (c' + 1) * E2 := by
rw [<- mul_add', <- mul_add']
exact mul_le_l' l (c' + 1)
exact (coreA _ _ U V E1 E2 _ _ t1 ia cv zh).elim
theorem coreB (x y D F Q G c U V e d E1 E2 : Nat) (H1 : x + (D + (F + c)) <= y + (G + Q)) (zl : Q + E2 <= E1 + F)
(cst : G + (d + 1) <= D + (e + c)) (ib : V + (e + E1) <= U + (d + E2)) (cv : U + y = V + x) : False := by
have s := Nat.add_le_add (Nat.add_le_add (Nat.add_le_add H1 zl) cst) ib
let env := nth0 [x, y, D, F, Q, G, c, U, V, e, d, E1, E2, 1]
have p1 : sL env ((([0, 2, 3, 6] ++ [4, 12]) ++ [5, 10, 13]) ++ [8, 9, 11]) =
sL env ([8, 0] ++ [13, 2, 3, 6, 4, 12, 5, 10, 9, 11]) := sL_perm env 14 _ _ rfl rfl rfl
have p2 : sL env ((([1, 5, 4] ++ [11, 3]) ++ [2, 9, 6]) ++ [7, 10, 12]) =
sL env ([7, 1] ++ [2, 3, 6, 4, 12, 5, 10, 9, 11]) := sL_perm env 14 _ _ rfl rfl rfl
rw [sL_app, sL_app, sL_app, sL_app] at p1
rw [sL_app, sL_app, sL_app, sL_app] at p2
have s2 : (V + x) + sL env [13, 2, 3, 6, 4, 12, 5, 10, 9, 11] <= (U + y) + sL env [2, 3, 6, 4, 12, 5, 10, 9, 11] :=
Nat.le_trans (Nat.le_of_eq p1.symm) (Nat.le_trans s (Nat.le_of_eq p2))
rw [cv, sL_cons] at s2
have s3 := add_cancel_l' s2
have s4 : sL env [2, 3, 6, 4, 12, 5, 10, 9, 11] + 1 <= sL env [2, 3, 6, 4, 12, 5, 10, 9, 11] + 0 := by
rw [Nat.add_comm _ 1]; exact s3
exact Nat.not_succ_le_self _ s4
/-- B side of the division step. -/
theorem divB (c' X Y U V E1 E2 q d e d' e' : Nat) (cv : U + (c' + 1) * Y = V + (c' + 1) * X)
(ib : V + e + E1 <= U + d + E2) (cst : (c' + 1) * e' + d + 1 <= (c' + 1) * d' + e + (c' + 1))
(zl : (c' + 1) * q + E2 <= E1 + (c' + 1) * E2) : Y + e' + q <= X + d' + E2 := by
cases Nat.lt_or_ge (X + d' + E2) (Y + e' + q) with
| inr r => exact r
| inl l =>
have l' : X + (d' + (E2 + 1)) <= Y + (e' + q) := by
rw [<- Nat.add_assoc, <- Nat.add_assoc, <- Nat.add_assoc]; exact l
have t1 := mul_le_l' l' (c' + 1)
rw [mul_add', mul_add', mul_add', Nat.mul_one, mul_add', mul_add'] at t1
rw [Nat.add_assoc] at ib
rw [Nat.add_assoc] at ib
rw [Nat.add_assoc, Nat.add_assoc] at cst
exact (coreB _ _ _ _ _ _ _ U V e d E1 E2 t1 zl cst ib cv).elim
end Found.Pnp107
#print axioms Found.Pnp107.vS_add
#print axioms Found.Pnp107.vS_mul
#print axioms Found.Pnp107.vS_nil
#print axioms Found.Pnp107.bv_le1
#print axioms Found.Pnp107.bv_mul_le1
#print axioms Found.Pnp107.vS_unit_le
#print axioms Found.Pnp107.vS_unit_zero
#print axioms Found.Pnp107.val_split
#print axioms Found.Pnp107.coef_vS
#print axioms Found.Pnp107.coreA
#print axioms Found.Pnp107.divA
#print axioms Found.Pnp107.coreB
#print axioms Found.Pnp107.divB
#print axioms: 13 theorems, no axioms.
For a fixed assignment of the shared variables each line gets an integer: axioms of \(A\) their shared part, other axioms \(0\), sums, multiples and floor quotients; at \(0\ge 1\) it is \(\ge 0\) where \(A\) is satisfiable and \(<0\) where \(B\) is (Pudlák 1997).
found/Pnp108.lean
/-
T330 piece 108. found/Pnp108 -- the interpolant of a cutting planes refutation (proof
complexity ladder, step 2: cutting planes; Pudlak 1997).
The axioms are split into A-lines (isA = true; their N side vanishes off the private A
variables Q and their P side vanishes on the private B variables R) and B-lines (both sides
vanish on Q). For a fixed assignment ap of the shared variables, interp p ap is an integer
(a pair) per line of the proof p:
A-axiom: (P part off Q at ap) + d - e; B-axiom, x >= 0: 0; x <= 1: 1 if x in Q, else 0;
sum, multiple: sum, multiple; division by c: floor division by c.
Invariants, for every assignment a that agrees with ap on the shared variables:
IA: if a satisfies A then N_Q(a) + E2 <= P_Q(a) + E1 (the Q part >= -E),
IB: if a satisfies B then N_O(a) + e + E1 <= P_O(a) + d + E2 (the rest >= D + E).
At the final line 0 >= 1 this gives: A satisfiable at ap -> E >= 0, and B satisfiable at
ap -> E < 0. Proved:
* vS_zero, vS_agree: parts with zero coefficients, parts on agreeing assignments;
* interp_inv: both invariants for every line;
* interp_A, interp_B: the two conclusions at the final line.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp107
namespace Found.Pnp108
open Found.Pnp65 (bv)
open Found.Pnp95 (sumI)
open Found.Pnp97 (sumI_ext sumI_zero zero_mul')
open Found.Pnp98 (mul_add')
open Found.Pnp99 (mul_le_l')
open Found.Pnp102 (nth0 unitL Line val Holds DivOK CPProof falseLine)
open Found.Pnp105 (zdiv zdiv_low zdiv_high)
open Found.Pnp106 (sL sL_app sL_perm)
open Found.Pnp107 (vS vS_add vS_mul vS_nil vS_unit_le vS_unit_zero val_split coef_vS divA divB)
open Found.Pnp93 (add4)
def selP (b : Bool) (x y : Nat × Nat) : Nat × Nat := match b with | true => x | false => y
def nQ (Q : Nat -> Bool) (i : Nat) : Bool := !(Q i)
/-- The interpolant. -/
def interp (m : Nat) (F : List Line) (Q : Nat -> Bool) (isA : Line -> Bool) (ap : Nat -> Bool) :
{L : Line} -> CPProof m F L -> Nat × Nat
| _, .ax L _ => selP (isA L) (vS m (nQ Q) L.P ap + L.d, L.e) (0, 0)
| _, .nonneg _ => (0, 0)
| _, .le1 i => selP (Q i) (1, 0) (0, 0)
| _, .add _ _ p q => ((interp m F Q isA ap p).1 + (interp m F Q isA ap q).1,
(interp m F Q isA ap p).2 + (interp m F Q isA ap q).2)
| _, .mul k _ p => (k * (interp m F Q isA ap p).1, k * (interp m F Q isA ap p).2)
| _, .div c _ _ p _ => zdiv (c - 1) (interp m F Q isA ap p)
theorem vS_zero (m : Nat) (S : Nat -> Bool) (l : List Nat) (a : Nat -> Bool) (h : forall i, S i = true -> nth0 l i = 0) :
vS m S l a = 0 := by
show sumI _ m = 0
rw [sumI_ext _ (fun _ => 0) ?_ m]
· exact sumI_zero m
· intro i
show bv (S i) * (nth0 l i * bv (a i)) = 0
cases e : S i with
| false => exact zero_mul' _
| true => rw [h i e, zero_mul']; rfl
theorem vS_agree (m : Nat) (S : Nat -> Bool) (l : List Nat) (a a' : Nat -> Bool)
(h : forall i, i < m -> S i = true -> nth0 l i = 0 \/ a i = a' i) : vS m S l a = vS m S l a' := by
refine Found.Pnp106.sumI_ext_lt _ _ m ?_
intro i hi
show bv (S i) * (nth0 l i * bv (a i)) = bv (S i) * (nth0 l i * bv (a' i))
cases e : S i with
| false =>
show 0 * (nth0 l i * bv (a i)) = 0 * (nth0 l i * bv (a' i))
rw [zero_mul', zero_mul']
| true =>
cases h i hi e with
| inl z => rw [z, zero_mul', zero_mul']
| inr q => rw [q]
/-- Conditions on the split of the axioms. -/
structure Split (m : Nat) (F : List Line) (Q R : Nat -> Bool) (isA : Line -> Bool) (SatA SatB : (Nat -> Bool) -> Prop) : Prop where
holdA : forall L, L ∈ F -> isA L = true -> forall a, SatA a -> Holds m a L
nA : forall L, L ∈ F -> isA L = true -> forall i, Q i = false -> nth0 L.N i = 0
pA : forall L, L ∈ F -> isA L = true -> forall i, R i = true -> nth0 L.P i = 0
holdB : forall L, L ∈ F -> isA L = false -> forall a, SatB a -> Holds m a L
qB : forall L, L ∈ F -> isA L = false -> forall i, Q i = true -> nth0 L.P i = 0 /\ nth0 L.N i = 0
/-- a agrees with ap on the shared variables. -/
def Agree (m : Nat) (Q R : Nat -> Bool) (a ap : Nat -> Bool) : Prop :=
forall i, i < m -> Q i = false -> R i = false -> a i = ap i
def IA (m : Nat) (Q : Nat -> Bool) (L : Line) (E : Nat × Nat) (a : Nat -> Bool) : Prop :=
vS m Q L.N a + E.2 <= vS m Q L.P a + E.1
def IB (m : Nat) (Q : Nat -> Bool) (L : Line) (E : Nat × Nat) (a : Nat -> Bool) : Prop :=
vS m (nQ Q) L.N a + L.e + E.1 <= vS m (nQ Q) L.P a + L.d + E.2
theorem pred_succ' : forall c : Nat, 1 <= c -> c - 1 + 1 = c
| 0, h => absurd h (Nat.not_succ_le_zero 0)
| _ + 1, _ => rfl
theorem interp_inv (m : Nat) (F : List Line) (Q R : Nat -> Bool) (isA : Line -> Bool) (SatA SatB : (Nat -> Bool) -> Prop)
(hs : Split m F Q R isA SatA SatB) (ap : Nat -> Bool) (a : Nat -> Bool) (hag : Agree m Q R a ap) :
forall (L : Line) (p : CPProof m F L),
(SatA a -> IA m Q L (interp m F Q isA ap p) a) /\ (SatB a -> IB m Q L (interp m F Q isA ap p) a) := by
intro L p
induction p with
| ax L h =>
cases eA : isA L with
| true =>
have ei : interp m F Q isA ap (CPProof.ax L h) = (vS m (nQ Q) L.P ap + L.d, L.e) := by
show selP (isA L) _ _ = _
rw [eA]; rfl
rw [ei]
have nz : vS m (nQ Q) L.N a = 0 := vS_zero m _ _ a (fun i hi => hs.nA L h eA i (Found.Pnp91.not_true hi))
have pe : vS m (nQ Q) L.P a = vS m (nQ Q) L.P ap := vS_agree m _ _ a ap (fun i him hi => by
cases eR : R i with
| true => exact Or.inl (hs.pA L h eA i eR)
| false => exact Or.inr (hag i him (Found.Pnp91.not_true hi) eR))
refine And.intro ?_ ?_
· intro sa
have ho : val m L.N a + L.e <= val m L.P a + L.d := hs.holdA L h eA a sa
rw [val_split m Q L.N a, val_split m Q L.P a] at ho
have ho' : vS m Q L.N a + vS m (nQ Q) L.N a + L.e <= vS m Q L.P a + vS m (nQ Q) L.P a + L.d := ho
rw [nz, pe, Nat.add_zero] at ho'
show vS m Q L.N a + L.e <= vS m Q L.P a + (vS m (nQ Q) L.P ap + L.d)
rw [<- Nat.add_assoc]
exact ho'
· intro _
show vS m (nQ Q) L.N a + L.e + (vS m (nQ Q) L.P ap + L.d) <= vS m (nQ Q) L.P a + L.d + L.e
rw [nz, pe, Nat.zero_add, Nat.add_comm L.e]
try exact Nat.le_refl _
| false =>
have ei : interp m F Q isA ap (CPProof.ax L h) = (0, 0) := by
show selP (isA L) _ _ = _
rw [eA]; rfl
rw [ei]
have pz : vS m Q L.P a = 0 := vS_zero m _ _ a (fun i hi => (hs.qB L h eA i hi).1)
have nzz : vS m Q L.N a = 0 := vS_zero m _ _ a (fun i hi => (hs.qB L h eA i hi).2)
refine And.intro ?_ ?_
· intro _
show vS m Q L.N a + 0 <= vS m Q L.P a + 0
rw [pz, nzz]; exact Nat.le_refl 0
· intro sb
have ho : val m L.N a + L.e <= val m L.P a + L.d := hs.holdB L h eA a sb
rw [val_split m Q L.N a, val_split m Q L.P a, pz, nzz, Nat.zero_add, Nat.zero_add] at ho
show vS m (nQ Q) L.N a + L.e + 0 <= vS m (nQ Q) L.P a + L.d + 0
exact ho
| nonneg i =>
refine And.intro (fun _ => ?_) (fun _ => ?_)
· show vS m Q [] a + 0 <= vS m Q (unitL i) a + 0
rw [vS_nil]; exact Nat.zero_le _
· show vS m (nQ Q) [] a + 0 + 0 <= vS m (nQ Q) (unitL i) a + 0 + 0
rw [vS_nil]; exact Nat.zero_le _
| le1 i =>
cases eq : Q i with
| true =>
have ei : interp m F Q isA ap (CPProof.le1 i : CPProof m F _) = (1, 0) := by
show selP (Q i) _ _ = _
rw [eq]; rfl
rw [ei]
refine And.intro (fun _ => ?_) (fun _ => ?_)
· show vS m Q (unitL i) a + 0 <= vS m Q [] a + 1
rw [vS_nil]; exact vS_unit_le m Q i a
· show vS m (nQ Q) (unitL i) a + 0 + 1 <= vS m (nQ Q) [] a + 1 + 0
rw [vS_nil, vS_unit_zero m (nQ Q) i a (by show (!(Q i)) = false; rw [eq]; rfl)]
exact Nat.le_refl _
| false =>
have ei : interp m F Q isA ap (CPProof.le1 i : CPProof m F _) = (0, 0) := by
show selP (Q i) _ _ = _
rw [eq]; rfl
rw [ei]
refine And.intro (fun _ => ?_) (fun _ => ?_)
· show vS m Q (unitL i) a + 0 <= vS m Q [] a + 0
rw [vS_nil, vS_unit_zero m Q i a eq]; exact Nat.le_refl _
· show vS m (nQ Q) (unitL i) a + 0 + 0 <= vS m (nQ Q) [] a + 1 + 0
rw [vS_nil]; exact vS_unit_le m (nQ Q) i a
| add A B p q ihp ihq =>
refine And.intro (fun sa => ?_) (fun sb => ?_)
· have h1 := ihp.1 sa
have h2 := ihq.1 sa
show vS m Q (Found.Pnp102.addL A.N B.N) a + ((interp m F Q isA ap p).2 + (interp m F Q isA ap q).2) <=
vS m Q (Found.Pnp102.addL A.P B.P) a + ((interp m F Q isA ap p).1 + (interp m F Q isA ap q).1)
rw [vS_add, vS_add, add4, add4 (vS m Q A.P a)]
exact Nat.add_le_add h1 h2
· have h1 := ihp.2 sb
have h2 := ihq.2 sb
show vS m (nQ Q) (Found.Pnp102.addL A.N B.N) a + (A.e + B.e) + ((interp m F Q isA ap p).1 + (interp m F Q isA ap q).1) <=
vS m (nQ Q) (Found.Pnp102.addL A.P B.P) a + (A.d + B.d) + ((interp m F Q isA ap p).2 + (interp m F Q isA ap q).2)
rw [vS_add, vS_add, add4 (vS m (nQ Q) A.N a), add4 (vS m (nQ Q) A.P a), add4 (vS m (nQ Q) A.N a + A.e),
add4 (vS m (nQ Q) A.P a + A.d)]
exact Nat.add_le_add h1 h2
| mul k A p ih =>
refine And.intro (fun sa => ?_) (fun sb => ?_)
· have h1 := ih.1 sa
show vS m Q (Found.Pnp102.mulL k A.N) a + k * (interp m F Q isA ap p).2 <=
vS m Q (Found.Pnp102.mulL k A.P) a + k * (interp m F Q isA ap p).1
rw [vS_mul, vS_mul, <- mul_add', <- mul_add']
exact mul_le_l' h1 k
· have h1 := ih.2 sb
show vS m (nQ Q) (Found.Pnp102.mulL k A.N) a + k * A.e + k * (interp m F Q isA ap p).1 <=
vS m (nQ Q) (Found.Pnp102.mulL k A.P) a + k * A.d + k * (interp m F Q isA ap p).2
rw [vS_mul, vS_mul, <- mul_add', <- mul_add', <- mul_add', <- mul_add']
exact mul_le_l' h1 k
| div c A B p h ih =>
cases c with
| zero => exact absurd h.pos (Nat.not_succ_le_zero 0)
| succ c' =>
refine And.intro (fun sa => ?_) (fun sb => ?_)
· have h1 := ih.1 sa
show vS m Q B.N a + (interp m F Q isA ap p).2 <= vS m Q B.P a + (zdiv c' (interp m F Q isA ap p)).1
exact divA c' _ _ _ _ _ _ _ (coef_vS m (c' + 1) Q A.P A.N B.P B.N a h.coef) h1
(zdiv_high c' (interp m F Q isA ap p))
· have h1 := ih.2 sb
show vS m (nQ Q) B.N a + B.e + (zdiv c' (interp m F Q isA ap p)).1 <= vS m (nQ Q) B.P a + B.d + (interp m F Q isA ap p).2
exact divB c' _ _ _ _ _ _ _ A.d A.e B.d B.e (coef_vS m (c' + 1) (nQ Q) A.P A.N B.P B.N a h.coef) h1 h.cst
(zdiv_low c' (interp m F Q isA ap p))
/-- A satisfiable at ap: the interpolant of a refutation is >= 0. -/
theorem interp_A (m : Nat) (F : List Line) (Q R : Nat -> Bool) (isA : Line -> Bool) (SatA SatB : (Nat -> Bool) -> Prop)
(hs : Split m F Q R isA SatA SatB) (ap : Nat -> Bool) (p : CPProof m F falseLine)
(a : Nat -> Bool) (hag : Agree m Q R a ap) (sa : SatA a) :
(interp m F Q isA ap p).2 <= (interp m F Q isA ap p).1 := by
have t := (interp_inv m F Q R isA SatA SatB hs ap a hag falseLine p).1 sa
have t' : vS m Q [] a + (interp m F Q isA ap p).2 <= vS m Q [] a + (interp m F Q isA ap p).1 := t
rw [vS_nil, Nat.zero_add, Nat.zero_add] at t'
exact t'
/-- B satisfiable at ap: the interpolant of a refutation is < 0. -/
theorem interp_B (m : Nat) (F : List Line) (Q R : Nat -> Bool) (isA : Line -> Bool) (SatA SatB : (Nat -> Bool) -> Prop)
(hs : Split m F Q R isA SatA SatB) (ap : Nat -> Bool) (p : CPProof m F falseLine)
(a : Nat -> Bool) (hag : Agree m Q R a ap) (sb : SatB a) :
(interp m F Q isA ap p).1 + 1 <= (interp m F Q isA ap p).2 := by
have t := (interp_inv m F Q R isA SatA SatB hs ap a hag falseLine p).2 sb
have t' : vS m (nQ Q) [] a + 1 + (interp m F Q isA ap p).1 <= vS m (nQ Q) [] a + 0 + (interp m F Q isA ap p).2 := t
rw [vS_nil] at t'
have e1 : (0 : Nat) + 1 + (interp m F Q isA ap p).1 = (interp m F Q isA ap p).1 + 1 := by rw [Nat.zero_add, Nat.add_comm]
have e2 : (0 : Nat) + 0 + (interp m F Q isA ap p).2 = (interp m F Q isA ap p).2 := by
rw [Nat.zero_add]; try rw [Nat.zero_add]
exact Nat.le_trans (Nat.le_of_eq e1.symm) (Nat.le_trans t' (Nat.le_of_eq e2))
end Found.Pnp108
#print axioms Found.Pnp108.vS_zero
#print axioms Found.Pnp108.vS_agree
#print axioms Found.Pnp108.pred_succ'
#print axioms Found.Pnp108.interp_inv
#print axioms Found.Pnp108.interp_A
#print axioms Found.Pnp108.interp_B
#print axioms: 6 theorems, no axioms.
When the shared assignment grows the interpolant of every line does not decrease, so the output \(E\ge 0\) is a monotone Boolean function.
found/Pnp109.lean
/-
T330 piece 109. found/Pnp109 -- the interpolant is monotone (proof complexity ladder, step
2: cutting planes; Pudlak 1997).
If the shared assignment grows (every true variable stays true), the interpolant of every
line does not decrease: the A-axioms give (P part off Q) + d - e, a sum of nonnegative
multiples of shared variables, and sums, multiples and floor division are monotone. So the
output "interp >= 0" is a monotone Boolean function of the shared variables; with
interp_A / interp_B (Pnp108) it is 1 where A is satisfiable and 0 where B is. Proved:
* vS_mono: the value of a part grows with the assignment;
* zadd_mono, zmul_mono: sum and multiple keep the order of pairs;
* interp_mono: the interpolant is monotone;
* out_mono: the output interp >= 0 is monotone.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp108
namespace Found.Pnp109
open Found.Pnp65 (bv)
open Found.Pnp95 (sumI)
open Found.Pnp96 (sumI_le)
open Found.Pnp98 (mul_add' mul_le_r)
open Found.Pnp99 (add_cancel_r' mul_le_l')
open Found.Pnp102 (nth0 Line CPProof)
open Found.Pnp105 (zle zdiv zdiv_mono zle_refl)
open Found.Pnp107 (vS)
open Found.Pnp108 (interp selP nQ)
open Found.Pnp93 (add4)
/-- ap' is at least ap. -/
def LeA (ap ap' : Nat -> Bool) : Prop := forall i, ap i = true -> ap' i = true
theorem bv_mono (x y : Bool) (h : x = true -> y = true) : bv x <= bv y := by
cases x with
| false => exact Nat.zero_le _
| true => rw [h rfl]; exact Nat.le_refl _
theorem vS_mono (m : Nat) (S : Nat -> Bool) (l : List Nat) (a a' : Nat -> Bool) (h : LeA a a') :
vS m S l a <= vS m S l a' :=
sumI_le _ _ m (fun i _ => mul_le_l' (mul_le_l' (bv_mono _ _ (h i)) _) _)
theorem zadd_mono (u v u' v' : Nat × Nat) (h1 : zle u u') (h2 : zle v v') :
zle (u.1 + v.1, u.2 + v.2) (u'.1 + v'.1, u'.2 + v'.2) := by
show (u.1 + v.1) + (u'.2 + v'.2) <= (u'.1 + v'.1) + (u.2 + v.2)
rw [add4, add4 u'.1]
exact Nat.add_le_add h1 h2
theorem zmul_mono (k : Nat) (u u' : Nat × Nat) (h : zle u u') : zle (k * u.1, k * u.2) (k * u'.1, k * u'.2) := by
show k * u.1 + k * u'.2 <= k * u'.1 + k * u.2
rw [<- mul_add', <- mul_add']
exact mul_le_l' h k
theorem interp_mono (m : Nat) (F : List Line) (Q : Nat -> Bool) (isA : Line -> Bool) (ap ap' : Nat -> Bool)
(hle : LeA ap ap') : forall (L : Line) (p : CPProof m F L),
zle (interp m F Q isA ap p) (interp m F Q isA ap' p) := by
intro L p
induction p with
| ax L h =>
show zle (selP (isA L) (vS m (nQ Q) L.P ap + L.d, L.e) (0, 0)) (selP (isA L) (vS m (nQ Q) L.P ap' + L.d, L.e) (0, 0))
cases isA L with
| true =>
show vS m (nQ Q) L.P ap + L.d + L.e <= vS m (nQ Q) L.P ap' + L.d + L.e
exact Nat.add_le_add_right (Nat.add_le_add_right (vS_mono m _ _ ap ap' hle) _) _
| false => exact zle_refl _
| nonneg i => exact zle_refl _
| le1 i =>
show zle (selP (Q i) (1, 0) (0, 0)) (selP (Q i) (1, 0) (0, 0))
exact zle_refl _
| add A B p q ihp ihq => exact zadd_mono _ _ _ _ ihp ihq
| mul k A p ih => exact zmul_mono k _ _ ih
| div c A B p h ih =>
cases c with
| zero => exact absurd h.pos (Nat.not_succ_le_zero 0)
| succ c' => exact zdiv_mono c' _ _ ih
/-- The output: interp >= 0. -/
def out (E : Nat × Nat) : Bool := Nat.ble E.2 E.1
theorem out_mono (E E' : Nat × Nat) (h : zle E E') (ho : out E = true) : out E' = true := by
have a : E.2 <= E.1 := Nat.le_of_ble_eq_true ho
have b : E.1 + E'.2 <= E'.1 + E.2 := h
have c : E'.2 + E.1 <= E'.1 + E.1 := by
rw [Nat.add_comm E'.2]
exact Nat.le_trans b (Nat.add_le_add_left a _)
exact Nat.ble_eq_true_of_le (add_cancel_r' _ c)
end Found.Pnp109
#print axioms Found.Pnp109.bv_mono
#print axioms Found.Pnp109.vS_mono
#print axioms Found.Pnp109.zadd_mono
#print axioms Found.Pnp109.zmul_mono
#print axioms Found.Pnp109.interp_mono
#print axioms Found.Pnp109.out_mono
#print axioms: 6 theorems, no axioms.
The coefficients of a clause line count its literals; a test below the variable bound sorts the lines into those of \(A\) and those of \(B\).
found/Pnp110.lean
/-
T330 piece 110. found/Pnp110 -- the coefficients of clause lines, and the test that sorts
axiom lines into A-lines and B-lines (proof complexity ladder, step 2: cutting planes).
The coefficient of x_i in the P side of the line of a clause C is the number of literals
(true, i) in C, and in the N side the number of literals (false, i). isAt M Q R L checks below
M that the P side vanishes on R and the N side vanishes off Q. Proved:
* coefP, coefN: the coefficients are the literal counts posC, negC;
* posC_zero, negC_zero: no such literal, coefficient 0;
* posC_pos, negC_pos: a literal present, coefficient >= 1;
* posC_big, negC_big: coefficients vanish at and above the variable bound;
* isAt_P, isAt_N: what isAt = true gives at every index;
* isAt_falseP, isAt_falseN: a witness index makes isAt false.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp109
namespace Found.Pnp110
open Found.RootComp (Lit Clause)
open Found.Pnp65 (bv)
open Found.Pnp90 (allI allI_true)
open Found.Pnp102 (nth0 addL unitL Line nth0_addL nth0_unit)
open Found.Pnp103 (lineOf VarsBelow)
def posC (i : Nat) : Clause -> Nat
| [] => 0
| (true, v) :: C => bv (Nat.beq i v) + posC i C
| (false, _) :: C => posC i C
def negC (i : Nat) : Clause -> Nat
| [] => 0
| (true, _) :: C => negC i C
| (false, v) :: C => bv (Nat.beq i v) + negC i C
theorem coefP (i : Nat) : forall C : Clause, nth0 (lineOf C).P i = posC i C
| [] => rfl
| (true, v) :: C => by
show nth0 (addL (unitL v) (lineOf C).P) i = bv (Nat.beq i v) + posC i C
rw [nth0_addL, nth0_unit, coefP i C]
| (false, _) :: C => coefP i C
theorem coefN (i : Nat) : forall C : Clause, nth0 (lineOf C).N i = negC i C
| [] => rfl
| (true, _) :: C => coefN i C
| (false, v) :: C => by
show nth0 (addL (unitL v) (lineOf C).N) i = bv (Nat.beq i v) + negC i C
rw [nth0_addL, nth0_unit, coefN i C]
theorem posC_zero (i : Nat) : forall C : Clause, (forall l, l ∈ C -> l.1 = true -> Nat.beq i l.2 = false) -> posC i C = 0
| [], _ => rfl
| (true, v) :: C, h => by
show bv (Nat.beq i v) + posC i C = 0
rw [h (true, v) (List.Mem.head _) rfl, posC_zero i C (fun l hl => h l (List.Mem.tail _ hl))]
rfl
| (false, _) :: C, h => posC_zero i C (fun l hl => h l (List.Mem.tail _ hl))
theorem negC_zero (i : Nat) : forall C : Clause, (forall l, l ∈ C -> l.1 = false -> Nat.beq i l.2 = false) -> negC i C = 0
| [], _ => rfl
| (true, _) :: C, h => negC_zero i C (fun l hl => h l (List.Mem.tail _ hl))
| (false, v) :: C, h => by
show bv (Nat.beq i v) + negC i C = 0
rw [h (false, v) (List.Mem.head _) rfl, negC_zero i C (fun l hl => h l (List.Mem.tail _ hl))]
rfl
theorem posC_pos (i : Nat) : forall C : Clause, (true, i) ∈ C -> 1 <= posC i C
| [], h => by cases h
| (true, v) :: C, h => by
show 1 <= bv (Nat.beq i v) + posC i C
cases h with
| head => rw [Found.Pnp62.beq_refl]; exact Nat.le_add_right 1 _
| tail _ h' => exact Nat.le_trans (posC_pos i C h') (Nat.le_add_left _ _)
| (false, v) :: C, h => by
cases h with
| tail _ h' => exact posC_pos i C h'
theorem negC_pos (i : Nat) : forall C : Clause, (false, i) ∈ C -> 1 <= negC i C
| [], h => by cases h
| (false, v) :: C, h => by
show 1 <= bv (Nat.beq i v) + negC i C
cases h with
| head => rw [Found.Pnp62.beq_refl]; exact Nat.le_add_right 1 _
| tail _ h' => exact Nat.le_trans (negC_pos i C h') (Nat.le_add_left _ _)
| (true, v) :: C, h => by
cases h with
| tail _ h' => exact negC_pos i C h'
theorem beq_big {i v m : Nat} (hv : v < m) (hi : m <= i) : Nat.beq i v = false :=
Found.Pnp2.beq_false_of_ne i v (fun e => by rw [e] at hi; exact Nat.lt_irrefl v (Nat.lt_of_lt_of_le hv hi))
theorem posC_big (m i : Nat) (hi : m <= i) : forall C : Clause, VarsBelow m C -> posC i C = 0
| [], _ => rfl
| (true, v) :: C, h => by
show bv (Nat.beq i v) + posC i C = 0
rw [beq_big h.1 hi, posC_big m i hi C h.2]; rfl
| (false, _) :: C, h => posC_big m i hi C h.2
theorem negC_big (m i : Nat) (hi : m <= i) : forall C : Clause, VarsBelow m C -> negC i C = 0
| [], _ => rfl
| (true, _) :: C, h => negC_big m i hi C h.2
| (false, v) :: C, h => by
show bv (Nat.beq i v) + negC i C = 0
rw [beq_big h.1 hi, negC_big m i hi C h.2]; rfl
/-- The A-line test below M. -/
def isAt (M : Nat) (Q R : Nat -> Bool) (L : Line) : Bool :=
allI (fun i => ((!(R i)) || Nat.beq (nth0 L.P i) 0) && (Q i || Nat.beq (nth0 L.N i) 0)) M
theorem isAt_P (M : Nat) (Q R : Nat -> Bool) (L : Line) (h : isAt M Q R L = true) (i : Nat) (hi : i < M)
(hR : R i = true) : nth0 L.P i = 0 := by
have t := Found.Pnp62.and_l (allI_true _ M h i hi)
rw [hR] at t
exact Found.Pnp2.beq_eq _ _ t
theorem isAt_N (M : Nat) (Q R : Nat -> Bool) (L : Line) (h : isAt M Q R L = true) (i : Nat) (hi : i < M)
(hQ : Q i = false) : nth0 L.N i = 0 := by
have t := Found.Pnp62.and_r (allI_true _ M h i hi)
rw [hQ] at t
exact Found.Pnp2.beq_eq _ _ t
theorem allI_false (g : Nat -> Bool) (i : Nat) (hg : g i = false) : forall M, i < M -> allI g M = false
| 0, h => absurd h (Nat.not_lt_zero i)
| M + 1, h => by
show (allI g M && g M) = false
cases Nat.lt_or_ge i M with
| inl l => rw [allI_false g i hg M l]; rfl
| inr r =>
have e : i = M := Nat.le_antisymm (Nat.le_of_lt_succ h) r
rw [<- e, hg]
cases allI g i <;> rfl
theorem pos_ne_zero {x : Nat} (h : 1 <= x) : Nat.beq x 0 = false :=
Found.Pnp2.beq_false_of_ne x 0 (fun e => by rw [e] at h; exact Nat.not_succ_le_zero 0 h)
theorem isAt_falseP (M : Nat) (Q R : Nat -> Bool) (L : Line) (i : Nat) (hi : i < M) (hR : R i = true)
(hp : 1 <= nth0 L.P i) : isAt M Q R L = false := by
refine allI_false _ i ?_ M hi
show (((!(R i)) || Nat.beq (nth0 L.P i) 0) && (Q i || Nat.beq (nth0 L.N i) 0)) = false
rw [hR, pos_ne_zero hp]; rfl
theorem isAt_falseN (M : Nat) (Q R : Nat -> Bool) (L : Line) (i : Nat) (hi : i < M) (hQ : Q i = false)
(hn : 1 <= nth0 L.N i) : isAt M Q R L = false := by
refine allI_false _ i ?_ M hi
show (((!(R i)) || Nat.beq (nth0 L.P i) 0) && (Q i || Nat.beq (nth0 L.N i) 0)) = false
rw [hQ, pos_ne_zero hn]
cases ((!(R i)) || Nat.beq (nth0 L.P i) 0) <;> rfl
end Found.Pnp110
#print axioms Found.Pnp110.coefP
#print axioms Found.Pnp110.coefN
#print axioms Found.Pnp110.posC_zero
#print axioms Found.Pnp110.negC_zero
#print axioms Found.Pnp110.posC_pos
#print axioms Found.Pnp110.negC_pos
#print axioms Found.Pnp110.beq_big
#print axioms Found.Pnp110.posC_big
#print axioms Found.Pnp110.negC_big
#print axioms Found.Pnp110.isAt_P
#print axioms Found.Pnp110.isAt_N
#print axioms Found.Pnp110.allI_false
#print axioms Found.Pnp110.pos_ne_zero
#print axioms Found.Pnp110.isAt_falseP
#print axioms Found.Pnp110.isAt_falseN
#print axioms: 15 theorems, no axioms.
Clique clause lines pass the test and colour clause lines fail it when \(c\ge 1\), which gives the split the interpolant needs.
found/Pnp111.lean
/-
T330 piece 111. found/Pnp111 -- the clique-colouring formula meets the interpolant (proof
complexity ladder, step 2: cutting planes; Pudlak 1997).
Q = the clique position variables q(l, i), R = the colour variables r(i, a), the shared
variables are the edges p(i, j). Every clique clause has its negative literals in Q and its
positive literals off R, so its line passes the A-test; every colour clause avoids Q, and with
c >= 1 colours it fails the A-test (a colour variable with a positive coefficient, or an edge
with a negative one). Proved:
* ble_false_of_lt, blt_false_of_le, and the place of p, q, r against Q and R;
* clique_lits, colour_lits: the literals of each part;
* clique_isA, colour_notA: the A-test on each part;
* cc_split: the split of the lines of CC n k c;
* cc_out_A, cc_out_B: for a cutting planes refutation of CC, the output of the interpolant
at a graph is 1 when the clique part is satisfiable there and 0 when the colour part is.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp110
namespace Found.Pnp111
open Found.RootComp (Lit Clause CNF evalCNF)
open Found.Pnp90 (allI allI_of)
open Found.Pnp102 (nth0 Line CPProof falseLine)
open Found.Pnp103 (lineOf linesOf VarsBelow line_holds mem_linesOf cnf_mem_true)
open Found.Pnp104 (pv qv rv mCC qAll qTwo qEdge rAll rEdge cliquePart colourPart CC mem_app_or mem_upto mem_bindUp
mem_pairsK lin_lt pv_lt qv_lt rv_lt cc_vars)
open Found.Pnp108 (Split Agree interp interp_A interp_B)
open Found.Pnp109 (out)
open Found.Pnp110 (posC negC coefP coefN posC_zero negC_zero posC_pos negC_pos posC_big negC_big isAt isAt_P isAt_N
isAt_falseP isAt_falseN)
def Qc (n k : Nat) (i : Nat) : Bool := Nat.ble (n * n) i && Nat.blt i (n * n + k * n)
def Rc (n k : Nat) (i : Nat) : Bool := Nat.ble (n * n + k * n) i
theorem ble_false_of_lt {a b : Nat} (h : b < a) : Nat.ble a b = false := by
cases e : Nat.ble a b with
| false => rfl
| true => exact absurd (Nat.lt_of_lt_of_le h (Nat.le_of_ble_eq_true e)) (Nat.lt_irrefl b)
theorem blt_false_of_le {a b : Nat} (h : b <= a) : Nat.blt a b = false := ble_false_of_lt (Nat.lt_succ_of_le h)
theorem pv_small (n i j : Nat) (hi : i < n) (hj : j < n) : pv n i j < n * n := lin_lt i j n n hi hj
theorem qv_in (n k l i : Nat) (hl : l < k) (hi : i < n) : qv n l i < n * n + k * n :=
Nat.add_lt_add_left (lin_lt l i k n hl hi) _
theorem Rc_p (n k i j : Nat) (hi : i < n) (hj : j < n) : Rc n k (pv n i j) = false :=
ble_false_of_lt (Nat.lt_of_lt_of_le (pv_small n i j hi hj) (Nat.le_add_right _ _))
theorem Qc_p (n k i j : Nat) (hi : i < n) (hj : j < n) : Qc n k (pv n i j) = false := by
show (Nat.ble (n * n) (pv n i j) && _) = false
rw [ble_false_of_lt (pv_small n i j hi hj)]; rfl
theorem Rc_q (n k l i : Nat) (hl : l < k) (hi : i < n) : Rc n k (qv n l i) = false := ble_false_of_lt (qv_in n k l i hl hi)
theorem Qc_q (n k l i : Nat) (hl : l < k) (hi : i < n) : Qc n k (qv n l i) = true := by
show (Nat.ble (n * n) (n * n + (l * n + i)) && Nat.blt (qv n l i) (n * n + k * n)) = true
rw [Nat.ble_eq_true_of_le (Nat.le_add_right _ _), Found.Pnp90.lt_blt _ _ (qv_in n k l i hl hi)]; rfl
theorem Rc_r (n k c i a : Nat) : Rc n k (rv n k c i a) = true := Nat.ble_eq_true_of_le (Nat.le_add_right _ _)
theorem Qc_r (n k c i a : Nat) : Qc n k (rv n k c i a) = false := by
show (Nat.ble (n * n) (rv n k c i a) && Nat.blt (rv n k c i a) (n * n + k * n)) = false
have h : Nat.blt (rv n k c i a) (n * n + k * n) = false := blt_false_of_le (Nat.le_add_right _ _)
rw [h]
cases Nat.ble (n * n) (rv n k c i a) <;> rfl
/-- The literals of the clique part. -/
theorem clique_lits (n k : Nat) (C : Clause) (hC : C ∈ cliquePart n k) (l : Lit) (hl : l ∈ C) :
(l.1 = true -> Rc n k l.2 = false) /\ (l.1 = false -> Qc n k l.2 = true) := by
cases mem_app_or C _ _ hC with
| inl h2 =>
cases mem_app_or C _ _ h2 with
| inl h3 =>
cases mem_upto _ C k h3 with
| intro l0 hl0 =>
rw [hl0.2] at hl
cases mem_upto _ l n hl with
| intro i hi =>
rw [hi.2]
exact And.intro (fun _ => Rc_q n k l0 i hl0.1 hi.1) (fun h => Bool.noConfusion h)
| inr h3 =>
cases mem_pairsK k _ C h3 with
| intro l0 hl0 =>
cases hl0 with
| intro l1 hl1 =>
cases mem_upto _ C n hl1.2.2 with
| intro i hi =>
rw [hi.2] at hl
cases hl with
| head => exact And.intro (fun h => Bool.noConfusion h) (fun _ => Qc_q n k l0 i (Nat.lt_trans hl1.1 hl1.2.1) hi.1)
| tail _ hl' =>
cases hl' with
| head => exact And.intro (fun h => Bool.noConfusion h) (fun _ => Qc_q n k l1 i hl1.2.1 hi.1)
| tail _ hl'' => cases hl''
| inr h2 =>
cases mem_pairsK k _ C h2 with
| intro l0 hl0 =>
cases hl0 with
| intro l1 hl1 =>
cases mem_bindUp _ C n hl1.2.2 with
| intro i hi =>
cases mem_upto _ C n hi.2 with
| intro j hj =>
rw [hj.2] at hl
cases hl with
| head => exact And.intro (fun h => Bool.noConfusion h) (fun _ => Qc_q n k l0 i (Nat.lt_trans hl1.1 hl1.2.1) hi.1)
| tail _ hl' =>
cases hl' with
| head => exact And.intro (fun h => Bool.noConfusion h) (fun _ => Qc_q n k l1 j hl1.2.1 hj.1)
| tail _ hl'' =>
cases hl'' with
| head => exact And.intro (fun _ => Rc_p n k i j hi.1 hj.1) (fun h => Bool.noConfusion h)
| tail _ h4 => cases h4
/-- The literals of the colour part avoid Q. -/
theorem colour_lits (n k c : Nat) (C : Clause) (hC : C ∈ colourPart n k c) (l : Lit) (hl : l ∈ C) : Qc n k l.2 = false := by
cases mem_app_or C _ _ hC with
| inl h2 =>
cases mem_upto _ C n h2 with
| intro i hi =>
rw [hi.2] at hl
cases mem_upto _ l c hl with
| intro a ha => rw [ha.2]; exact Qc_r n k c i a
| inr h2 =>
cases mem_bindUp _ C n h2 with
| intro i hi =>
cases mem_bindUp _ C n hi.2 with
| intro j hj =>
cases mem_upto _ C c hj.2 with
| intro a ha =>
rw [ha.2] at hl
cases hl with
| head => exact Qc_p n k i j hi.1 hj.1
| tail _ hl' =>
cases hl' with
| head => exact Qc_r n k c i a
| tail _ hl'' =>
cases hl'' with
| head => exact Qc_r n k c j a
| tail _ h4 => cases h4
theorem beq_of_ne_val {f : Nat -> Bool} {i v : Nat} (hi : f i = true) (hv : f v = false) : Nat.beq i v = false :=
Found.Pnp2.beq_false_of_ne i v (fun e => by rw [e, hv] at hi; exact Bool.noConfusion hi)
theorem beq_of_ne_val' {f : Nat -> Bool} {i v : Nat} (hi : f i = false) (hv : f v = true) : Nat.beq i v = false :=
Found.Pnp2.beq_false_of_ne i v (fun e => by rw [e, hv] at hi; exact Bool.noConfusion hi)
theorem clique_isA (n k c : Nat) (C : Clause) (hC : C ∈ cliquePart n k) :
isAt (mCC n k c) (Qc n k) (Rc n k) (lineOf C) = true := by
refine allI_of _ _ ?_
intro i _
have hp : (!(Rc n k i) || Nat.beq (nth0 (lineOf C).P i) 0) = true := by
cases eR : Rc n k i with
| false => rfl
| true =>
rw [coefP, posC_zero i C (fun l hl h1 => beq_of_ne_val eR ((clique_lits n k C hC l hl).1 h1))]
rfl
have hn : (Qc n k i || Nat.beq (nth0 (lineOf C).N i) 0) = true := by
cases eQ : Qc n k i with
| true => rfl
| false =>
rw [coefN, negC_zero i C (fun l hl h1 => beq_of_ne_val' eQ ((clique_lits n k C hC l hl).2 h1))]
rfl
rw [hp, hn]; rfl
theorem colour_notA (n k c : Nat) (hc : 1 <= c) (C : Clause) (hC : C ∈ colourPart n k c) :
isAt (mCC n k c) (Qc n k) (Rc n k) (lineOf C) = false := by
cases mem_app_or C _ _ hC with
| inl h2 =>
cases mem_upto _ C n h2 with
| intro i hi =>
have mem : (true, rv n k c i 0) ∈ C := by
rw [hi.2]
cases c with
| zero => exact absurd hc (Nat.not_succ_le_zero 0)
| succ c' =>
have t : forall c'', (true, rv n k (c' + 1) i 0) ∈ Found.Pnp104.upto (fun a => (true, rv n k (c' + 1) i a)) (c'' + 1) := by
intro c''
induction c'' with
| zero => exact List.Mem.head _
| succ c'' ih => exact List.Mem.tail _ ih
exact t c'
refine isAt_falseP _ _ _ _ (rv n k c i 0) (rv_lt n k c i 0 hi.1 hc) (Rc_r n k c i 0) ?_
rw [coefP]; exact posC_pos _ C mem
| inr h2 =>
cases mem_bindUp _ C n h2 with
| intro i hi =>
cases mem_bindUp _ C n hi.2 with
| intro j hj =>
cases mem_upto _ C c hj.2 with
| intro a ha =>
have mem : (false, pv n i j) ∈ C := by rw [ha.2]; exact List.Mem.head _
refine isAt_falseN _ _ _ _ (pv n i j) (pv_lt n k c i j hi.1 hj.1) (Qc_p n k i j hi.1 hj.1) ?_
rw [coefN]; exact negC_pos _ C mem
def SatA (n k : Nat) (a : Nat -> Bool) : Prop := evalCNF a (cliquePart n k) = true
def SatB (n k c : Nat) (a : Nat -> Bool) : Prop := evalCNF a (colourPart n k c) = true
theorem mem_CC (n k c : Nat) (C : Clause) (h : C ∈ CC n k c) : C ∈ cliquePart n k \/ C ∈ colourPart n k c :=
mem_app_or C _ _ h
theorem cc_split (n k c : Nat) (hc : 1 <= c) :
Split (mCC n k c) (linesOf (CC n k c)) (Qc n k) (Rc n k) (isAt (mCC n k c) (Qc n k) (Rc n k)) (SatA n k) (SatB n k c) := by
have src : forall L, L ∈ linesOf (CC n k c) -> Exists fun C => C ∈ CC n k c /\ L = lineOf C := fun L h => mem_linesOf _ L h
refine Split.mk ?_ ?_ ?_ ?_ ?_
· intro L hL hA a sa
cases src L hL with
| intro C hC =>
rw [hC.2]
cases mem_CC n k c C hC.1 with
| inl h => exact line_holds _ a C (cc_vars n k c C hC.1) (cnf_mem_true a _ C sa h)
| inr h => rw [hC.2, colour_notA n k c hc C h] at hA; exact Bool.noConfusion hA
· intro L hL hA i hQ
cases src L hL with
| intro C hC =>
cases Nat.lt_or_ge i (mCC n k c) with
| inl l => exact isAt_N _ _ _ L hA i l hQ
| inr r => rw [hC.2, coefN]; exact negC_big _ i r C (cc_vars n k c C hC.1)
· intro L hL hA i hR
cases src L hL with
| intro C hC =>
cases Nat.lt_or_ge i (mCC n k c) with
| inl l => exact isAt_P _ _ _ L hA i l hR
| inr r => rw [hC.2, coefP]; exact posC_big _ i r C (cc_vars n k c C hC.1)
· intro L hL hA a sb
cases src L hL with
| intro C hC =>
rw [hC.2]
cases mem_CC n k c C hC.1 with
| inl h => rw [hC.2, clique_isA n k c C h] at hA; exact Bool.noConfusion hA
| inr h => exact line_holds _ a C (cc_vars n k c C hC.1) (cnf_mem_true a _ C sb h)
· intro L hL hA i hQ
cases src L hL with
| intro C hC =>
cases mem_CC n k c C hC.1 with
| inl h => rw [hC.2, clique_isA n k c C h] at hA; exact Bool.noConfusion hA
| inr h =>
rw [hC.2, coefP, coefN]
exact And.intro (posC_zero i C (fun l hl _ => beq_of_ne_val hQ (colour_lits n k c C h l hl)))
(negC_zero i C (fun l hl _ => beq_of_ne_val hQ (colour_lits n k c C h l hl)))
/-- Clique part satisfiable at the graph ap: output 1. -/
theorem cc_out_A (n k c : Nat) (hc : 1 <= c) (p : CPProof (mCC n k c) (linesOf (CC n k c)) falseLine) (ap a : Nat -> Bool)
(hag : Agree (mCC n k c) (Qc n k) (Rc n k) a ap) (sa : SatA n k a) :
out (interp (mCC n k c) _ (Qc n k) (isAt (mCC n k c) (Qc n k) (Rc n k)) ap p) = true :=
Nat.ble_eq_true_of_le (interp_A _ _ _ _ _ _ _ (cc_split n k c hc) ap p a hag sa)
/-- Colour part satisfiable at the graph ap: output 0. -/
theorem cc_out_B (n k c : Nat) (hc : 1 <= c) (p : CPProof (mCC n k c) (linesOf (CC n k c)) falseLine) (ap a : Nat -> Bool)
(hag : Agree (mCC n k c) (Qc n k) (Rc n k) a ap) (sb : SatB n k c a) :
out (interp (mCC n k c) _ (Qc n k) (isAt (mCC n k c) (Qc n k) (Rc n k)) ap p) = false := by
have t := interp_B _ _ _ _ _ _ _ (cc_split n k c hc) ap p a hag sb
exact Found.Pnp111.ble_false_of_lt t
end Found.Pnp111
#print axioms Found.Pnp111.ble_false_of_lt
#print axioms Found.Pnp111.blt_false_of_le
#print axioms Found.Pnp111.pv_small
#print axioms Found.Pnp111.qv_in
#print axioms Found.Pnp111.Rc_p
#print axioms Found.Pnp111.Qc_p
#print axioms Found.Pnp111.Rc_q
#print axioms Found.Pnp111.Qc_q
#print axioms Found.Pnp111.Rc_r
#print axioms Found.Pnp111.Qc_r
#print axioms Found.Pnp111.clique_lits
#print axioms Found.Pnp111.colour_lits
#print axioms Found.Pnp111.beq_of_ne_val
#print axioms Found.Pnp111.beq_of_ne_val'
#print axioms Found.Pnp111.clique_isA
#print axioms Found.Pnp111.colour_notA
#print axioms Found.Pnp111.mem_CC
#print axioms Found.Pnp111.cc_split
#print axioms Found.Pnp111.cc_out_A
#print axioms Found.Pnp111.cc_out_B
#print axioms: 20 theorems, no axioms.
From a refutation of \(\mathrm {CC}(n,k,c)\) the interpolant outputs \(1\) on every graph with a \(k\)-clique and \(0\) on every \(c\)-colourable graph, monotonically in the edges.
found/Pnp112.lean
/-
T330 piece 112. found/Pnp112 -- the interpolant separates cliques from colourings (proof
complexity ladder, step 2: cutting planes; Pudlak 1997).
A graph is an assignment ap of the edge variables p(i, j). A k-clique is a map v from
positions l < k to vertices below n, injective, with ap(p(v l, v l')) = true for l < l'.
A c-colouring is a map col from vertices to colours below c with col i <> col j whenever
ap(p(i, j)) = true. From a clique the q variables are set to the clique (cliqA), from a
colouring the r variables to the colours (colB); both agree with ap on the edges. Proved:
* mem_memL, cnf_all: membership and evaluation tools;
* qv_inj, rv_inj: the variable codes are injective;
* cliqA_sat, colB_sat: the clique part, the colour part are satisfied;
* cliqA_agree, colB_agree: the assignments agree with ap on the edges;
* separate_clique, separate_colour: for a cutting planes refutation of CC n k c (c >= 1)
the output of the interpolant is 1 on graphs with a k-clique and 0 on c-colourable
graphs; with Pnp109 it is monotone in the edges.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp111
namespace Found.Pnp112
open Found.RootComp (Lit Clause CNF evalLit evalClause evalCNF)
open Found.Pnp65 (var var_inj anyI anyI_true)
open Found.Pnp62 (memL beqLit_refl of_mem)
open Found.Pnp96 (anyI_ex)
open Found.Pnp102 (CPProof falseLine)
open Found.Pnp103 (linesOf)
open Found.Pnp104 (pv qv rv mCC qAll qTwo qEdge rAll rEdge cliquePart colourPart CC upto bindUp mem_app_or mem_upto
mem_bindUp mem_pairsK)
open Found.Pnp108 (Agree interp)
open Found.Pnp109 (out)
open Found.Pnp111 (Qc Rc SatA SatB cc_out_A cc_out_B Qc_q Qc_p Qc_r Rc_r Rc_p Rc_q qv_in pv_small)
def selB (b x y : Bool) : Bool := match b with | true => x | false => y
theorem mem_memL (l : Lit) : forall C : Clause, l ∈ C -> memL l C = true
| [], h => by cases h
| m :: C, h => by
show (Found.Pnp62.beqLit m l || memL l C) = true
cases h with
| head => rw [beqLit_refl]; rfl
| tail _ h' => exact Found.Pnp62.or_r (mem_memL l C h')
theorem cnf_all (a : Nat -> Bool) : forall F : CNF, (forall C, C ∈ F -> evalClause a C = true) -> evalCNF a F = true
| [], _ => rfl
| C :: F, h => by
show (evalClause a C && evalCNF a F) = true
rw [h C (List.Mem.head _), cnf_all a F (fun D hD => h D (List.Mem.tail _ hD))]
rfl
theorem lit_true (a : Nat -> Bool) (l : Lit) (C : Clause) (hl : l ∈ C) (ht : evalLit a l = true) : evalClause a C = true :=
of_mem a l ht C (mem_memL l C hl)
theorem add_cancel_l_eq' {x a b : Nat} (h : x + a = x + b) : a = b := Found.Pnp101.add_cancel_l_eq h
theorem qv_inj (n l i l' i' : Nat) (hi : i < n) (hi' : i' < n) (h : qv n l i = qv n l' i') : l = l' /\ i = i' :=
var_inj n l l' i i' hi hi' (add_cancel_l_eq' h)
theorem rv_inj (n k c i a i' a' : Nat) (ha : a < c) (ha' : a' < c) (h : rv n k c i a = rv n k c i' a') : i = i' /\ a = a' :=
var_inj c i i' a a' ha ha' (add_cancel_l_eq' h)
/-- The assignment of a clique v: q(l, v l) true, the edges as in ap. -/
def cliqA (n k : Nat) (v : Nat -> Nat) (ap : Nat -> Bool) (x : Nat) : Bool :=
selB (Qc n k x) (anyI (fun l => Nat.beq x (qv n l (v l))) k) (ap x)
/-- The assignment of a colouring col: r(i, col i) true, the edges as in ap. -/
def colB (n k c : Nat) (col : Nat -> Nat) (ap : Nat -> Bool) (x : Nat) : Bool :=
selB (Rc n k x) (anyI (fun i => Nat.beq x (rv n k c i (col i))) n) (ap x)
structure Clique (n k : Nat) (v : Nat -> Nat) (ap : Nat -> Bool) : Prop where
rng : forall l, l < k -> v l < n
inj : forall l l', l < k -> l' < k -> v l = v l' -> l = l'
edge : forall l l', l < l' -> l' < k -> ap (pv n (v l) (v l')) = true
structure Colouring (n c : Nat) (col : Nat -> Nat) (ap : Nat -> Bool) : Prop where
rng : forall i, i < n -> col i < c
prop : forall i j, i < n -> j < n -> ap (pv n i j) = true -> col i = col j -> False
theorem mem_upto_of {α : Type} (f : Nat -> α) (i : Nat) : forall k, i < k -> f i ∈ upto f k
| 0, h => absurd h (Nat.not_lt_zero i)
| k + 1, h => by
cases Nat.lt_or_ge i k with
| inl l => exact List.Mem.tail _ (mem_upto_of f i k l)
| inr r =>
have e : i = k := Nat.le_antisymm (Nat.le_of_lt_succ h) r
rw [e]; exact List.Mem.head _
theorem cliqA_q (n k : Nat) (v : Nat -> Nat) (ap : Nat -> Bool) (hv : Clique n k v ap) (l i : Nat) (hl : l < k) (hi : i < n)
(h : cliqA n k v ap (qv n l i) = true) : i = v l := by
have h' : selB (Qc n k (qv n l i)) (anyI (fun l1 => Nat.beq (qv n l i) (qv n l1 (v l1))) k) (ap (qv n l i)) = true := h
rw [Qc_q n k l i hl hi] at h'
cases anyI_ex _ k h' with
| intro l1 hl1 =>
have e := Found.Pnp2.beq_eq _ _ hl1.2
have t := qv_inj n l i l1 (v l1) hi (hv.rng l1 hl1.1) e
rw [t.2, t.1]
theorem cliqA_p (n k : Nat) (v : Nat -> Nat) (ap : Nat -> Bool) (i j : Nat) (hi : i < n) (hj : j < n) :
cliqA n k v ap (pv n i j) = ap (pv n i j) := by
show selB (Qc n k (pv n i j)) _ _ = _
rw [Qc_p n k i j hi hj]; rfl
theorem cliqA_sat (n k : Nat) (v : Nat -> Nat) (ap : Nat -> Bool) (hv : Clique n k v ap) : SatA n k (cliqA n k v ap) := by
refine cnf_all _ _ ?_
intro C hC
cases mem_app_or C _ _ hC with
| inl h2 =>
cases mem_app_or C _ _ h2 with
| inl h3 =>
cases mem_upto _ C k h3 with
| intro l hl =>
rw [hl.2]
refine lit_true _ (true, qv n l (v l)) _ (mem_upto_of (fun i => (true, qv n l i)) (v l) n (hv.rng l hl.1)) ?_
show selB (Qc n k (qv n l (v l))) _ _ = true
rw [Qc_q n k l (v l) hl.1 (hv.rng l hl.1)]
show anyI (fun l1 => Nat.beq (qv n l (v l)) (qv n l1 (v l1))) k = true
exact anyI_true (fun l1 => Nat.beq (qv n l (v l)) (qv n l1 (v l1))) l (Found.Pnp62.beq_refl _) k hl.1
| inr h3 =>
cases mem_pairsK k _ C h3 with
| intro l0 hl0 =>
cases hl0 with
| intro l1 hl1 =>
cases mem_upto _ C n hl1.2.2 with
| intro i hi =>
rw [hi.2]
have l0k : l0 < k := Nat.lt_trans hl1.1 hl1.2.1
cases e0 : cliqA n k v ap (qv n l0 i) with
| false =>
refine lit_true _ (false, qv n l0 i) _ (List.Mem.head _) ?_
show (!(cliqA n k v ap (qv n l0 i))) = true
rw [e0]; rfl
| true =>
cases e1 : cliqA n k v ap (qv n l1 i) with
| false =>
refine lit_true _ (false, qv n l1 i) _ (List.Mem.tail _ (List.Mem.head _)) ?_
show (!(cliqA n k v ap (qv n l1 i))) = true
rw [e1]; rfl
| true =>
have a0 := cliqA_q n k v ap hv l0 i l0k hi.1 e0
have a1 := cliqA_q n k v ap hv l1 i hl1.2.1 hi.1 e1
have t := hv.inj l0 l1 l0k hl1.2.1 (a0.symm.trans a1)
rw [t] at hl1
exact absurd hl1.1 (Nat.lt_irrefl _)
| inr h2 =>
cases mem_pairsK k _ C h2 with
| intro l0 hl0 =>
cases hl0 with
| intro l1 hl1 =>
cases mem_bindUp _ C n hl1.2.2 with
| intro i hi =>
cases mem_upto _ C n hi.2 with
| intro j hj =>
rw [hj.2]
have l0k : l0 < k := Nat.lt_trans hl1.1 hl1.2.1
cases e0 : cliqA n k v ap (qv n l0 i) with
| false =>
refine lit_true _ (false, qv n l0 i) _ (List.Mem.head _) ?_
show (!(cliqA n k v ap (qv n l0 i))) = true
rw [e0]; rfl
| true =>
cases e1 : cliqA n k v ap (qv n l1 j) with
| false =>
refine lit_true _ (false, qv n l1 j) _ (List.Mem.tail _ (List.Mem.head _)) ?_
show (!(cliqA n k v ap (qv n l1 j))) = true
rw [e1]; rfl
| true =>
have a0 := cliqA_q n k v ap hv l0 i l0k hi.1 e0
have a1 := cliqA_q n k v ap hv l1 j hl1.2.1 hj.1 e1
refine lit_true _ (true, pv n i j) _ (List.Mem.tail _ (List.Mem.tail _ (List.Mem.head _))) ?_
show cliqA n k v ap (pv n i j) = true
rw [cliqA_p n k v ap i j hi.1 hj.1, a0, a1]
exact hv.edge l0 l1 hl1.1 hl1.2.1
theorem cliqA_agree (n k c : Nat) (v : Nat -> Nat) (ap : Nat -> Bool) :
Agree (mCC n k c) (Qc n k) (Rc n k) (cliqA n k v ap) ap := by
intro i _ hQ _
show selB (Qc n k i) _ _ = _
rw [hQ]; rfl
theorem colB_r (n k c : Nat) (col : Nat -> Nat) (ap : Nat -> Bool) (hc : Colouring n c col ap) (i a : Nat) (ha : a < c)
(h : colB n k c col ap (rv n k c i a) = true) : a = col i := by
have h' : selB (Rc n k (rv n k c i a)) (anyI (fun i1 => Nat.beq (rv n k c i a) (rv n k c i1 (col i1))) n) (ap (rv n k c i a)) = true := h
rw [Rc_r n k c i a] at h'
cases anyI_ex _ n h' with
| intro i1 hi1 =>
have e := Found.Pnp2.beq_eq _ _ hi1.2
have t := rv_inj n k c i a i1 (col i1) ha (hc.rng i1 hi1.1) e
rw [t.2, t.1]
theorem colB_p (n k c : Nat) (col : Nat -> Nat) (ap : Nat -> Bool) (i j : Nat) (hi : i < n) (hj : j < n) :
colB n k c col ap (pv n i j) = ap (pv n i j) := by
show selB (Rc n k (pv n i j)) _ _ = _
rw [Rc_p n k i j hi hj]; rfl
theorem colB_sat (n k c : Nat) (col : Nat -> Nat) (ap : Nat -> Bool) (hc : Colouring n c col ap) :
SatB n k c (colB n k c col ap) := by
refine cnf_all _ _ ?_
intro C hC
cases mem_app_or C _ _ hC with
| inl h2 =>
cases mem_upto _ C n h2 with
| intro i hi =>
rw [hi.2]
refine lit_true _ (true, rv n k c i (col i)) _ (mem_upto_of (fun a => (true, rv n k c i a)) (col i) c (hc.rng i hi.1)) ?_
show selB (Rc n k (rv n k c i (col i))) _ _ = true
rw [Rc_r n k c i (col i)]
show anyI (fun i1 => Nat.beq (rv n k c i (col i)) (rv n k c i1 (col i1))) n = true
exact anyI_true (fun i1 => Nat.beq (rv n k c i (col i)) (rv n k c i1 (col i1))) i (Found.Pnp62.beq_refl _) n hi.1
| inr h2 =>
cases mem_bindUp _ C n h2 with
| intro i hi =>
cases mem_bindUp _ C n hi.2 with
| intro j hj =>
cases mem_upto _ C c hj.2 with
| intro a ha =>
rw [ha.2]
cases e0 : colB n k c col ap (pv n i j) with
| false =>
refine lit_true _ (false, pv n i j) _ (List.Mem.head _) ?_
show (!(colB n k c col ap (pv n i j))) = true
rw [e0]; rfl
| true =>
cases e1 : colB n k c col ap (rv n k c i a) with
| false =>
refine lit_true _ (false, rv n k c i a) _ (List.Mem.tail _ (List.Mem.head _)) ?_
show (!(colB n k c col ap (rv n k c i a))) = true
rw [e1]; rfl
| true =>
cases e2 : colB n k c col ap (rv n k c j a) with
| false =>
refine lit_true _ (false, rv n k c j a) _ (List.Mem.tail _ (List.Mem.tail _ (List.Mem.head _))) ?_
show (!(colB n k c col ap (rv n k c j a))) = true
rw [e2]; rfl
| true =>
have a1 := colB_r n k c col ap hc i a ha.1 e1
have a2 := colB_r n k c col ap hc j a ha.1 e2
rw [colB_p n k c col ap i j hi.1 hj.1] at e0
exact (hc.prop i j hi.1 hj.1 e0 (a1.symm.trans a2)).elim
theorem colB_agree (n k c : Nat) (col : Nat -> Nat) (ap : Nat -> Bool) :
Agree (mCC n k c) (Qc n k) (Rc n k) (colB n k c col ap) ap := by
intro i _ _ hR
show selB (Rc n k i) _ _ = _
rw [hR]; rfl
/-- On a graph with a k-clique the interpolant outputs 1. -/
theorem separate_clique (n k c : Nat) (hc : 1 <= c) (p : CPProof (mCC n k c) (linesOf (CC n k c)) falseLine)
(ap : Nat -> Bool) (v : Nat -> Nat) (hv : Clique n k v ap) :
out (interp (mCC n k c) _ (Qc n k) (Found.Pnp110.isAt (mCC n k c) (Qc n k) (Rc n k)) ap p) = true :=
cc_out_A n k c hc p ap _ (cliqA_agree n k c v ap) (cliqA_sat n k v ap hv)
/-- On a c-colourable graph the interpolant outputs 0. -/
theorem separate_colour (n k c : Nat) (hc : 1 <= c) (p : CPProof (mCC n k c) (linesOf (CC n k c)) falseLine)
(ap : Nat -> Bool) (col : Nat -> Nat) (hcol : Colouring n c col ap) :
out (interp (mCC n k c) _ (Qc n k) (Found.Pnp110.isAt (mCC n k c) (Qc n k) (Rc n k)) ap p) = false :=
cc_out_B n k c hc p ap _ (colB_agree n k c col ap) (colB_sat n k c col ap hcol)
end Found.Pnp112
#print axioms Found.Pnp112.mem_memL
#print axioms Found.Pnp112.cnf_all
#print axioms Found.Pnp112.lit_true
#print axioms Found.Pnp112.qv_inj
#print axioms Found.Pnp112.rv_inj
#print axioms Found.Pnp112.mem_upto_of
#print axioms Found.Pnp112.cliqA_q
#print axioms Found.Pnp112.cliqA_p
#print axioms Found.Pnp112.cliqA_sat
#print axioms Found.Pnp112.cliqA_agree
#print axioms Found.Pnp112.colB_r
#print axioms Found.Pnp112.colB_p
#print axioms Found.Pnp112.colB_sat
#print axioms Found.Pnp112.colB_agree
#print axioms Found.Pnp112.separate_clique
#print axioms Found.Pnp112.separate_colour
#print axioms: 16 theorems, no axioms.
A monotone real circuit is a straight-line program of inputs, constants and gates nondecreasing in both arguments; its output is a monotone Boolean function.
found/Pnp113.lean
/-
T330 piece 113. found/Pnp113 -- monotone real circuits as straight-line programs (proof
complexity ladder, step 2: cutting planes).
Values are integers as pairs (Pnp105) under the order zle. A gate is an input x_i (value
(x_i, 0)), a constant, or op f j k: a binary function f, nondecreasing in both arguments,
applied to the values of the gates j and k positions back (gates are listed newest first,
so every reference is to an earlier gate). The size of a circuit is its number of gates; its
output is the value of its last gate, read as a Boolean by out (value >= 0). Proved:
* nthV_le: pointwise order of value lists passes to their entries;
* evalR_mono: if the input grows, every gate value does not decrease;
* out_circ_mono: the output of a monotone real circuit is a monotone Boolean function.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp112
namespace Found.Pnp113
open Found.Pnp65 (bv)
open Found.Pnp105 (zle zle_refl)
open Found.Pnp109 (LeA bv_mono out out_mono)
/-- f is nondecreasing in both arguments. -/
def Mono2 (f : Nat × Nat -> Nat × Nat -> Nat × Nat) : Prop :=
forall u u' v v', zle u u' -> zle v v' -> zle (f u v) (f u' v')
inductive Gate where
| inp (i : Nat) : Gate
| cst (v : Nat × Nat) : Gate
| op (f : Nat × Nat -> Nat × Nat -> Nat × Nat) (hf : Mono2 f) (j k : Nat) : Gate
/-- Entry j of a value list (newest first), (0, 0) past the end. -/
def nthV : List (Nat × Nat) -> Nat -> Nat × Nat
| [], _ => (0, 0)
| v :: _, 0 => v
| _ :: l, j + 1 => nthV l j
def gval (x : Nat -> Bool) (vs : List (Nat × Nat)) : Gate -> Nat × Nat
| .inp i => (bv (x i), 0)
| .cst v => v
| .op f _ j k => f (nthV vs j) (nthV vs k)
/-- The values of all gates, newest first. -/
def evalR (x : Nat -> Bool) : List Gate -> List (Nat × Nat)
| [] => []
| g :: gs => gval x (evalR x gs) g :: evalR x gs
/-- The output gate value. -/
def outV (x : Nat -> Bool) (C : List Gate) : Nat × Nat := nthV (evalR x C) 0
/-- Pointwise order of two value lists of the same shape. -/
def LeL : List (Nat × Nat) -> List (Nat × Nat) -> Prop
| [], [] => True
| v :: l, w :: l' => zle v w /\ LeL l l'
| [], _ :: _ => False
| _ :: _, [] => False
theorem nthV_le : forall (l l' : List (Nat × Nat)) (j : Nat), LeL l l' -> zle (nthV l j) (nthV l' j)
| [], [], _, _ => zle_refl _
| _ :: _, _ :: _, 0, h => h.1
| _ :: l, _ :: l', j + 1, h => nthV_le l l' j h.2
| [], _ :: _, _, h => h.elim
| _ :: _, [], _, h => h.elim
theorem evalR_mono (x x' : Nat -> Bool) (hx : LeA x x') : forall C : List Gate, LeL (evalR x C) (evalR x' C)
| [] => True.intro
| g :: gs => by
have ih := evalR_mono x x' hx gs
refine And.intro ?_ ih
cases g with
| inp i =>
show bv (x i) + 0 <= bv (x' i) + 0
exact Nat.add_le_add_right (bv_mono _ _ (hx i)) 0
| cst v => exact zle_refl v
| op f hf j k => exact hf _ _ _ _ (nthV_le _ _ j ih) (nthV_le _ _ k ih)
theorem outV_mono (x x' : Nat -> Bool) (hx : LeA x x') (C : List Gate) : zle (outV x C) (outV x' C) :=
nthV_le _ _ 0 (evalR_mono x x' hx C)
/-- The output of a monotone real circuit is a monotone Boolean function. -/
theorem out_circ_mono (x x' : Nat -> Bool) (hx : LeA x x') (C : List Gate) (h : out (outV x C) = true) :
out (outV x' C) = true :=
out_mono _ _ (outV_mono x x' hx C) h
end Found.Pnp113
#print axioms Found.Pnp113.nthV_le
#print axioms Found.Pnp113.evalR_mono
#print axioms Found.Pnp113.outV_mono
#print axioms Found.Pnp113.out_circ_mono
#print axioms: 4 theorems, no axioms.
Every cutting planes proof \(p\) compiles to a monotone real circuit whose output at the shared assignment is the interpolant, with at most \(2m+1\) gates per proof line.
found/Pnp114.lean
/-
T330 piece 114. found/Pnp114 -- the interpolant as a monotone real circuit, with a size
bound (proof complexity ladder, step 2: cutting planes; Pudlak 1997).
compile turns a cutting planes proof p into a straight-line monotone real circuit whose
output at a shared assignment ap is the interpolant interp p ap (Pnp108): an A-axiom becomes
a chain of 2 m + 1 gates computing (d + sum of kappa_i x_i, e) with kappa_i >= 0, other axioms
constants, and sum, multiple, floor division one gate each. Its size is at most
(2 m + 1) * size p. Proved:
* evalR_len, nthV_app_lt, nthV_app_len, evalR_app: evaluation of a concatenation whose
first part only refers to itself (WFc);
* addmul_mono, addf_mono, mulf_mono, divf_mono: the gates are monotone;
* linG_val, linG_wf, linG_len: the linear chain;
* compile_wf, compile_val, compile_len: the circuit, its value, its size.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp113
namespace Found.Pnp114
open Found.Pnp65 (bv)
open Found.Pnp95 (sumI)
open Found.Pnp97 (sumI_ext)
open Found.Pnp98 (mul_add' add_mul' mul_le_r)
open Found.Pnp99 (mul_le_l' mul_assoc')
open Found.Pnp102 (nth0 Line CPProof)
open Found.Pnp105 (zle zdiv zdiv_mono)
open Found.Pnp107 (vS)
open Found.Pnp108 (interp selP nQ)
open Found.Pnp109 (zadd_mono zmul_mono)
open Found.Pnp113 (Mono2 Gate nthV gval evalR outV)
open Found.Pnp93 (add4)
/-- All references of every gate point to later (older) gates of the same list. -/
def refsLt (n : Nat) : Gate -> Prop
| .inp _ => True
| .cst _ => True
| .op _ _ j k => j < n /\ k < n
def WFc : List Gate -> Prop
| [] => True
| g :: gs => refsLt gs.length g /\ WFc gs
theorem evalR_len (x : Nat -> Bool) : forall C : List Gate, (evalR x C).length = C.length
| [] => rfl
| _ :: gs => congrArg (· + 1) (evalR_len x gs)
theorem nthV_app_lt : forall (l1 l2 : List (Nat × Nat)) (j : Nat), j < l1.length -> nthV (l1 ++ l2) j = nthV l1 j
| [], _, j, h => absurd h (Nat.not_lt_zero j)
| _ :: _, _, 0, _ => rfl
| _ :: l1, l2, j + 1, h => nthV_app_lt l1 l2 j (Nat.lt_of_succ_lt_succ h)
theorem nthV_app_len : forall (l1 l2 : List (Nat × Nat)) (j : Nat), nthV (l1 ++ l2) (l1.length + j) = nthV l2 j
| [], l2, j => by
show nthV l2 (0 + j) = nthV l2 j
rw [Nat.zero_add]
| a :: l1, l2, j => by
show nthV (a :: (l1 ++ l2)) (l1.length + 1 + j) = nthV l2 j
rw [Nat.add_right_comm]
exact nthV_app_len l1 l2 j
theorem gval_app (x : Nat -> Bool) (l1 l2 : List (Nat × Nat)) (g : Gate) (h : refsLt l1.length g) :
gval x (l1 ++ l2) g = gval x l1 g := by
cases g with
| inp i => rfl
| cst v => rfl
| op f hf j k =>
show f (nthV (l1 ++ l2) j) (nthV (l1 ++ l2) k) = f (nthV l1 j) (nthV l1 k)
rw [nthV_app_lt l1 l2 j h.1, nthV_app_lt l1 l2 k h.2]
theorem evalR_app (x : Nat -> Bool) (B : List Gate) : forall A : List Gate, WFc A -> evalR x (A ++ B) = evalR x A ++ evalR x B
| [], _ => rfl
| g :: A, h => by
show gval x (evalR x (A ++ B)) g :: evalR x (A ++ B) = gval x (evalR x A) g :: (evalR x A ++ evalR x B)
rw [evalR_app x B A h.2, gval_app x (evalR x A) (evalR x B) g (by rw [evalR_len]; exact h.1)]
/-- Gates. -/
def addmul (c : Nat) (u v : Nat × Nat) : Nat × Nat := (u.1 + c * v.1, u.2 + c * v.2)
def addf (u v : Nat × Nat) : Nat × Nat := (u.1 + v.1, u.2 + v.2)
def mulf (k : Nat) (u _v : Nat × Nat) : Nat × Nat := (k * u.1, k * u.2)
def divf (c' : Nat) (u _v : Nat × Nat) : Nat × Nat := zdiv c' u
theorem addmul_mono (c : Nat) : Mono2 (addmul c) := by
intro u u' v v' h1 h2
show (u.1 + c * v.1) + (u'.2 + c * v'.2) <= (u'.1 + c * v'.1) + (u.2 + c * v.2)
rw [add4, add4 u'.1, <- mul_add', <- mul_add']
exact Nat.add_le_add h1 (mul_le_l' h2 c)
theorem addf_mono : Mono2 addf := fun u u' v v' h1 h2 => zadd_mono u v u' v' h1 h2
theorem mulf_mono (k : Nat) : Mono2 (mulf k) := fun u u' _ _ h1 _ => zmul_mono k u u' h1
theorem divf_mono (c' : Nat) : Mono2 (divf c') := fun u u' _ _ h1 _ => zdiv_mono c' u u' h1
/-- The linear chain: value (d + sum_{t < i} kappa t * x_t, e). -/
def linG (kappa : Nat -> Nat) (d e : Nat) : Nat -> List Gate
| 0 => [Gate.cst (d, e)]
| i + 1 => Gate.op (addmul (kappa i)) (addmul_mono (kappa i)) 1 0 :: Gate.inp i :: linG kappa d e i
theorem linG_len (kappa : Nat -> Nat) (d e : Nat) : forall i, (linG kappa d e i).length = 2 * i + 1
| 0 => rfl
| i + 1 => by
show (linG kappa d e i).length + 1 + 1 = 2 * (i + 1) + 1
rw [linG_len kappa d e i, mul_add']
theorem linG_wf (kappa : Nat -> Nat) (d e : Nat) : forall i, WFc (linG kappa d e i)
| 0 => And.intro True.intro True.intro
| i + 1 => by
refine And.intro ?_ (And.intro True.intro (linG_wf kappa d e i))
show 1 < (linG kappa d e i).length + 1 /\ 0 < (linG kappa d e i).length + 1
rw [linG_len]
exact And.intro (Nat.succ_lt_succ (Nat.succ_pos _)) (Nat.succ_pos _)
theorem linG_val (x : Nat -> Bool) (kappa : Nat -> Nat) (d e : Nat) : forall i,
nthV (evalR x (linG kappa d e i)) 0 = (d + sumI (fun t => kappa t * bv (x t)) i, e)
| 0 => rfl
| i + 1 => by
show addmul (kappa i) (nthV (evalR x (linG kappa d e i)) 0) (bv (x i), 0) = _
rw [linG_val x kappa d e i]
show (d + sumI (fun t => kappa t * bv (x t)) i + kappa i * bv (x i), e + kappa i * 0) =
(d + (sumI (fun t => kappa t * bv (x t)) i + kappa i * bv (x i)), e)
rw [Nat.add_assoc]
rfl
theorem len_app : forall (A B : List Gate), (A ++ B).length = A.length + B.length
| [], B => (Nat.zero_add _).symm
| _ :: A, B => by
show (A ++ B).length + 1 = A.length + 1 + B.length
rw [len_app A B, Nat.add_right_comm]
theorem refsLt_mono {n n' : Nat} (hn : n <= n') : forall g : Gate, refsLt n g -> refsLt n' g
| .inp _, _ => True.intro
| .cst _, _ => True.intro
| .op _ _ _ _, h => And.intro (Nat.lt_of_lt_of_le h.1 hn) (Nat.lt_of_lt_of_le h.2 hn)
theorem wfc_app (B : List Gate) (hB : WFc B) : forall A : List Gate, WFc A -> WFc (A ++ B)
| [], _ => hB
| g :: A, h => by
refine And.intro ?_ (wfc_app B hB A h.2)
refine refsLt_mono ?_ g h.1
exact Nat.le_trans (Nat.le_add_right A.length B.length) (Nat.le_of_eq (len_app A B).symm)
theorem mul_one' (n : Nat) : n * 1 = n := Nat.zero_add n
def selC (b : Bool) (x y : List Gate) : List Gate := match b with | true => x | false => y
/-- Weight of shared variable i in the A-axiom with P side P. -/
def kap (Q : Nat -> Bool) (P : List Nat) (i : Nat) : Nat := bv (nQ Q i) * nth0 P i
/-- The circuit of a cutting planes proof (gates newest first; the output is the head). -/
def compile (m : Nat) (F : List Line) (Q : Nat -> Bool) (isA : Line -> Bool) : {L : Line} -> CPProof m F L -> List Gate
| _, .ax L _ => selC (isA L) (linG (kap Q L.P) L.d L.e m) [Gate.cst (0, 0)]
| _, .nonneg _ => [Gate.cst (0, 0)]
| _, .le1 i => [Gate.cst (selP (Q i) (1, 0) (0, 0))]
| _, .add _ _ p q => Gate.op addf addf_mono 0 (compile m F Q isA p).length :: (compile m F Q isA p ++ compile m F Q isA q)
| _, .mul k _ p => Gate.op (mulf k) (mulf_mono k) 0 0 :: compile m F Q isA p
| _, .div c _ _ p _ => Gate.op (divf (c - 1)) (divf_mono (c - 1)) 0 0 :: compile m F Q isA p
theorem compile_pos (m : Nat) (F : List Line) (Q : Nat -> Bool) (isA : Line -> Bool) :
forall (L : Line) (p : CPProof m F L), 0 < (compile m F Q isA p).length := by
intro L p
cases p with
| ax L h =>
show 0 < (selC (isA L) _ _).length
cases isA L with
| true => show 0 < (linG _ _ _ m).length; rw [linG_len]; exact Nat.succ_pos _
| false => exact Nat.succ_pos 0
| nonneg _ => exact Nat.succ_pos 0
| le1 _ => exact Nat.succ_pos 0
| add _ _ _ _ => exact Nat.succ_pos _
| mul _ _ _ => exact Nat.succ_pos _
| div _ _ _ _ _ => exact Nat.succ_pos _
theorem compile_wf (m : Nat) (F : List Line) (Q : Nat -> Bool) (isA : Line -> Bool) :
forall (L : Line) (p : CPProof m F L), WFc (compile m F Q isA p) := by
intro L p
induction p with
| ax L h =>
show WFc (selC (isA L) _ _)
cases isA L with
| true => exact linG_wf _ _ _ m
| false => exact And.intro True.intro True.intro
| nonneg _ => exact And.intro True.intro True.intro
| le1 _ => exact And.intro True.intro True.intro
| add A B p q ihp ihq =>
refine And.intro ?_ (wfc_app _ ihq _ ihp)
have hp := compile_pos m F Q isA A p
have hq := compile_pos m F Q isA B q
show 0 < (compile m F Q isA p ++ compile m F Q isA q).length /\
(compile m F Q isA p).length < (compile m F Q isA p ++ compile m F Q isA q).length
rw [len_app]
exact And.intro (Nat.lt_of_lt_of_le hp (Nat.le_add_right _ _)) (Nat.add_le_add_left hq _)
| mul k A p ih =>
have hp := compile_pos m F Q isA A p
exact And.intro (And.intro hp hp) ih
| div c A B p h ih =>
have hp := compile_pos m F Q isA A p
exact And.intro (And.intro hp hp) ih
/-- The output of the circuit at the shared assignment ap is the interpolant. -/
theorem compile_val (m : Nat) (F : List Line) (Q : Nat -> Bool) (isA : Line -> Bool) (ap : Nat -> Bool) :
forall (L : Line) (p : CPProof m F L), outV ap (compile m F Q isA p) = interp m F Q isA ap p := by
intro L p
induction p with
| ax L h =>
cases eA : isA L with
| true =>
have ec : compile m F Q isA (CPProof.ax L h) = linG (kap Q L.P) L.d L.e m := by
show selC (isA L) _ _ = _
rw [eA]; rfl
have ei : interp m F Q isA ap (CPProof.ax L h) = (vS m (nQ Q) L.P ap + L.d, L.e) := by
show selP (isA L) _ _ = _
rw [eA]; rfl
have es : sumI (fun t => kap Q L.P t * bv (ap t)) m = vS m (nQ Q) L.P ap :=
sumI_ext _ _ (fun t => mul_assoc' _ _ _) m
show nthV (evalR ap (compile m F Q isA (CPProof.ax L h))) 0 = _
rw [ec, ei, linG_val, es, Nat.add_comm L.d]
| false =>
have ec : compile m F Q isA (CPProof.ax L h) = [Gate.cst (0, 0)] := by
show selC (isA L) _ _ = _
rw [eA]; rfl
have ei : interp m F Q isA ap (CPProof.ax L h) = (0, 0) := by
show selP (isA L) _ _ = _
rw [eA]; rfl
show nthV (evalR ap (compile m F Q isA (CPProof.ax L h))) 0 = _
rw [ec, ei]
rfl
| nonneg _ => rfl
| le1 _ => rfl
| add A B p q ihp ihq =>
have hp := compile_pos m F Q isA A p
have e1 : nthV (evalR ap (compile m F Q isA p) ++ evalR ap (compile m F Q isA q)) 0 =
nthV (evalR ap (compile m F Q isA p)) 0 :=
nthV_app_lt _ _ 0 (by rw [evalR_len]; exact hp)
have e2 : nthV (evalR ap (compile m F Q isA p) ++ evalR ap (compile m F Q isA q)) (compile m F Q isA p).length =
nthV (evalR ap (compile m F Q isA q)) 0 := by
have t := nthV_app_len (evalR ap (compile m F Q isA p)) (evalR ap (compile m F Q isA q)) 0
rw [evalR_len] at t
exact t
show addf (nthV (evalR ap (compile m F Q isA p ++ compile m F Q isA q)) 0)
(nthV (evalR ap (compile m F Q isA p ++ compile m F Q isA q)) (compile m F Q isA p).length) = _
rw [evalR_app ap _ _ (compile_wf m F Q isA A p), e1, e2]
have ihp' : nthV (evalR ap (compile m F Q isA p)) 0 = interp m F Q isA ap p := ihp
have ihq' : nthV (evalR ap (compile m F Q isA q)) 0 = interp m F Q isA ap q := ihq
rw [ihp', ihq']
rfl
| mul k A p ih =>
have ih' : nthV (evalR ap (compile m F Q isA p)) 0 = interp m F Q isA ap p := ih
show mulf k (nthV (evalR ap (compile m F Q isA p)) 0) (nthV (evalR ap (compile m F Q isA p)) 0) = _
rw [ih']
rfl
| div c A B p h ih =>
have ih' : nthV (evalR ap (compile m F Q isA p)) 0 = interp m F Q isA ap p := ih
show divf (c - 1) (nthV (evalR ap (compile m F Q isA p)) 0) (nthV (evalR ap (compile m F Q isA p)) 0) = _
rw [ih']
rfl
theorem one_le_odd (m : Nat) : 1 <= (2 * m + 1) * 1 := by
rw [mul_one']
exact Nat.le_add_left 1 (2 * m)
/-- Size: at most (2 m + 1) gates per proof line. -/
theorem compile_len (m : Nat) (F : List Line) (Q : Nat -> Bool) (isA : Line -> Bool) :
forall (L : Line) (p : CPProof m F L), (compile m F Q isA p).length <= (2 * m + 1) * p.size := by
intro L p
induction p with
| ax L h =>
show (selC (isA L) _ _).length <= (2 * m + 1) * 1
cases isA L with
| true =>
show (linG _ _ _ m).length <= _
rw [linG_len, mul_one']
exact Nat.le_refl _
| false => exact one_le_odd m
| nonneg _ => exact one_le_odd m
| le1 _ => exact one_le_odd m
| add A B p q ihp ihq =>
show (compile m F Q isA p ++ compile m F Q isA q).length + 1 <= (2 * m + 1) * (p.size + q.size + 1)
rw [len_app, mul_add', mul_add']
exact Nat.add_le_add (Nat.add_le_add ihp ihq) (one_le_odd m)
| mul k A p ih =>
show (compile m F Q isA p).length + 1 <= (2 * m + 1) * (p.size + 1)
rw [mul_add']
exact Nat.add_le_add ih (one_le_odd m)
| div c A B p h ih =>
show (compile m F Q isA p).length + 1 <= (2 * m + 1) * (p.size + 1)
rw [mul_add']
exact Nat.add_le_add ih (one_le_odd m)
end Found.Pnp114
#print axioms Found.Pnp114.evalR_len
#print axioms Found.Pnp114.nthV_app_lt
#print axioms Found.Pnp114.nthV_app_len
#print axioms Found.Pnp114.gval_app
#print axioms Found.Pnp114.evalR_app
#print axioms Found.Pnp114.addmul_mono
#print axioms Found.Pnp114.addf_mono
#print axioms Found.Pnp114.mulf_mono
#print axioms Found.Pnp114.divf_mono
#print axioms Found.Pnp114.linG_len
#print axioms Found.Pnp114.linG_wf
#print axioms Found.Pnp114.linG_val
#print axioms Found.Pnp114.len_app
#print axioms Found.Pnp114.refsLt_mono
#print axioms Found.Pnp114.wfc_app
#print axioms Found.Pnp114.mul_one'
#print axioms Found.Pnp114.compile_pos
#print axioms Found.Pnp114.compile_wf
#print axioms Found.Pnp114.compile_val
#print axioms Found.Pnp114.one_le_odd
#print axioms Found.Pnp114.compile_len
#print axioms: 21 theorems, no axioms.
A family of more than \(S_q(\ell )\) pairwise different sets above a base set, each with at most \(\ell \) further elements, contains \(q+1\) sets with one common pairwise intersection.
found/Pnp115.lean
/-
T330 piece 115. found/Pnp115 -- the sunflower lemma (Erdos--Rado), the combinatorial tool of
the approximation method for the monotone real circuit lower bound (proof complexity ladder,
step 2: cutting planes).
Sets are Boolean functions on the ground set {0, ..., m-1}. The lemma is proved for families
above a base set B: every member contains B, the parts outside B are nonempty with at most l
elements, and the members are pairwise different. If such a family has more than Sb q l
members, where Sb q 0 = 0 and Sb q (l+1) = q (l+1) (Sb q l + 1), then q + 1 of its members
form a sunflower: pairwise different, with one common pairwise intersection (the core).
Proof: a greedy pairwise disjoint subfamily (outside B); if it has q + 1 members they form a
sunflower with core B; otherwise its union has at most q (l+1) points, every member meets it,
so one point x lies in more than Sb q l + 1 members, and the family of those members above
B + {x} (at most one of them is exactly B + {x}) gives the sunflower by induction. Proved:
* mem_filt, pw_filt, mem_take, pw_take, len_take, pw_imp: lists;
* grd_mem, grd_keep, grd_pw, grd_cov: the greedy disjoint subfamily;
* swapS, mul_lsum, sumI_mul_r, lsum_const_le, sumI_pos: counting;
* findx: a point with a large count, or a uniform bound;
* card_split, cnt_le_one, fixed_set: the induction step's size and count;
* sunflower: the lemma.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp114
namespace Found.Pnp115
open Found.Pnp65 (bv anyI anyI_true anyI_false or_true_r)
open Found.Pnp95 (sumI)
open Found.Pnp96 (anyI_ex sumI_le)
open Found.Pnp97 (sumI_add sumI_ext sumI_zero zero_mul')
open Found.Pnp98 (mul_add' add_mul' mul_le_r)
open Found.Pnp99 (mul_le_l')
open Found.Pnp106 (sumI_ext_lt)
/-- Pairwise relation along a list (each element with every later one). -/
def Pw {α : Type} (R : α -> α -> Prop) : List α -> Prop
| [] => True
| a :: l => (forall b, b ∈ l -> R a b) /\ Pw R l
def SetEq (m : Nat) (S T : Nat -> Bool) : Prop := forall i, i < m -> S i = T i
def SubB (m : Nat) (B S : Nat -> Bool) : Prop := forall i, i < m -> B i = true -> S i = true
/-- The part of S outside B. -/
def dif (B S : Nat -> Bool) (i : Nat) : Bool := S i && !(B i)
def nonE (m : Nat) (B S : Nat -> Bool) : Bool := anyI (dif B S) m
def card (m : Nat) (S : Nat -> Bool) : Nat := sumI (fun i => bv (S i)) m
/-- S and T meet outside B. -/
def meets (m : Nat) (B S T : Nat -> Bool) : Bool := anyI (fun i => dif B S i && dif B T i) m
def Sunflower (m : Nat) (C : Nat -> Bool) (W : List (Nat -> Bool)) : Prop :=
Pw (fun S T => (forall i, i < m -> (S i && T i) = C i) /\ Not (SetEq m S T)) W
def Sb (q : Nat) : Nat -> Nat
| 0 => 0
| l + 1 => q * (l + 1) * (Sb q l + 1)
/-! Lists. -/
def lsum {α : Type} (h : α -> Nat) : List α -> Nat
| [] => 0
| a :: l => h a + lsum h l
def consB {α : Type} (b : Bool) (a : α) (l : List α) : List α :=
match b with
| true => a :: l
| false => l
def filt {α : Type} (r : α -> Bool) : List α -> List α
| [] => []
| a :: l => consB (r a) a (filt r l)
def takeL {α : Type} : Nat -> List α -> List α
| 0, _ => []
| _ + 1, [] => []
| n + 1, a :: l => a :: takeL n l
def anyL {α : Type} (g : α -> Bool) : List α -> Bool
| [] => false
| a :: l => g a || anyL g l
theorem mem_cons_cases {α : Type} {x a : α} {l : List α} (h : x ∈ a :: l) : x = a \/ x ∈ l := by
cases h with
| head => exact Or.inl rfl
| tail _ h2 => exact Or.inr h2
theorem mem_filt {α : Type} (r : α -> Bool) (x : α) : forall L : List α, x ∈ filt r L -> x ∈ L /\ r x = true
| [], h => nomatch h
| a :: l, h => by
cases e : r a with
| true =>
have t : filt r (a :: l) = a :: filt r l := congrArg (fun b => consB b a (filt r l)) e
rw [t] at h
cases mem_cons_cases h with
| inl q => rw [q]; exact And.intro (List.Mem.head l) e
| inr h2 => exact And.intro (List.Mem.tail a (mem_filt r x l h2).1) (mem_filt r x l h2).2
| false =>
have t : filt r (a :: l) = filt r l := congrArg (fun b => consB b a (filt r l)) e
rw [t] at h
exact And.intro (List.Mem.tail a (mem_filt r x l h).1) (mem_filt r x l h).2
theorem pw_filt {α : Type} (R : α -> α -> Prop) (r : α -> Bool) : forall L : List α, Pw R L -> Pw R (filt r L)
| [], _ => True.intro
| a :: l, h => by
cases e : r a with
| true =>
have t : filt r (a :: l) = a :: filt r l := congrArg (fun b => consB b a (filt r l)) e
rw [t]
exact And.intro (fun b hb => h.1 b (mem_filt r b l hb).1) (pw_filt R r l h.2)
| false =>
have t : filt r (a :: l) = filt r l := congrArg (fun b => consB b a (filt r l)) e
rw [t]
exact pw_filt R r l h.2
theorem mem_take {α : Type} (x : α) : forall (n : Nat) (L : List α), x ∈ takeL n L -> x ∈ L
| 0, _, h => nomatch h
| _ + 1, [], h => nomatch h
| n + 1, a :: l, h => by
cases mem_cons_cases h with
| inl q => rw [q]; exact List.Mem.head l
| inr h2 => exact List.Mem.tail a (mem_take x n l h2)
theorem pw_take {α : Type} (R : α -> α -> Prop) : forall (n : Nat) (L : List α), Pw R L -> Pw R (takeL n L)
| 0, _, _ => True.intro
| _ + 1, [], _ => True.intro
| n + 1, _ :: l, h => And.intro (fun b hb => h.1 b (mem_take b n l hb)) (pw_take R n l h.2)
theorem len_take {α : Type} : forall (n : Nat) (L : List α), n <= L.length -> (takeL n L).length = n
| 0, _, _ => rfl
| _ + 1, [], h => absurd h (Nat.not_succ_le_zero _)
| n + 1, _ :: l, h => congrArg (· + 1) (len_take n l (Nat.le_of_succ_le_succ h))
theorem pw_imp {α : Type} (R R' : α -> α -> Prop) : forall L : List α,
(forall a b, a ∈ L -> b ∈ L -> R a b -> R' a b) -> Pw R L -> Pw R' L
| [], _, _ => True.intro
| a :: l, hi, h => And.intro
(fun b hb => hi a b (List.Mem.head l) (List.Mem.tail a hb) (h.1 b hb))
(pw_imp R R' l (fun x y hx hy => hi x y (List.Mem.tail a hx) (List.Mem.tail a hy)) h.2)
theorem anyL_true {α : Type} (g : α -> Bool) (x : α) (hx : g x = true) : forall L : List α, x ∈ L -> anyL g L = true
| [], h => nomatch h
| a :: l, h => by
cases mem_cons_cases h with
| inl q =>
rw [<- q]
exact congrArg (· || anyL g l) hx
| inr h2 =>
show (g a || anyL g l) = true
rw [anyL_true g x hx l h2]
exact or_true_r _
theorem bv_or_le (a b : Bool) : bv (a || b) <= bv a + bv b := by
cases a <;> cases b <;> first | exact Nat.le_refl _ | exact Nat.zero_le _ | exact Nat.le_succ _
theorem bv_anyL_le {α : Type} (g : α -> Bool) : forall L : List α, bv (anyL g L) <= lsum (fun a => bv (g a)) L
| [] => Nat.le_refl 0
| a :: l => Nat.le_trans (bv_or_le (g a) (anyL g l)) (Nat.add_le_add_left (bv_anyL_le g l) _)
/-! Counting. -/
theorem one_mul' : forall c : Nat, 1 * c = c
| 0 => rfl
| c + 1 => congrArg (· + 1) (one_mul' c)
theorem len_lsum {α : Type} : forall L : List α, L.length = lsum (fun _ => 1) L
| [] => rfl
| _ :: l => by
show l.length + 1 = 1 + lsum (fun _ => 1) l
rw [len_lsum l, Nat.add_comm]
theorem lsum_le {α : Type} (h h' : α -> Nat) : forall L : List α, (forall a, a ∈ L -> h a <= h' a) -> lsum h L <= lsum h' L
| [], _ => Nat.le_refl 0
| a :: l, hl => Nat.add_le_add (hl a (List.Mem.head l)) (lsum_le h h' l (fun b hb => hl b (List.Mem.tail a hb)))
theorem swapS {α : Type} (f : α -> Nat -> Nat) (k : Nat) : forall L : List α,
sumI (fun i => lsum (fun a => f a i) L) k = lsum (fun a => sumI (f a) k) L
| [] => sumI_zero k
| a :: l => by
show sumI (fun i => f a i + lsum (fun a => f a i) l) k = sumI (f a) k + lsum (fun a => sumI (f a) k) l
rw [sumI_add (fun i => f a i) (fun i => lsum (fun a => f a i) l) k, swapS f k l]
theorem mul_lsum {α : Type} (c : Nat) (h : α -> Nat) : forall L : List α, c * lsum h L = lsum (fun a => c * h a) L
| [] => rfl
| a :: l => by
show c * (h a + lsum h l) = c * h a + lsum (fun a => c * h a) l
rw [mul_add', mul_lsum c h l]
theorem sumI_mul_r (h : Nat -> Nat) (M : Nat) : forall k, sumI (fun i => h i * M) k = sumI h k * M
| 0 => (zero_mul' M).symm
| k + 1 => by
show sumI (fun i => h i * M) k + h k * M = (sumI h k + h k) * M
rw [sumI_mul_r h M k, add_mul']
theorem lsum_const_le {α : Type} (h : α -> Nat) (c : Nat) : forall L : List α, (forall a, a ∈ L -> h a <= c) ->
lsum h L <= L.length * c
| [], _ => Nat.zero_le _
| a :: l, hl => by
show h a + lsum h l <= (l.length + 1) * c
rw [add_mul', one_mul', Nat.add_comm (l.length * c)]
exact Nat.add_le_add (hl a (List.Mem.head l)) (lsum_const_le h c l (fun b hb => hl b (List.Mem.tail a hb)))
theorem lt_succ_cases {i k : Nat} (h : i < k + 1) : i < k \/ i = k := by
cases Nat.lt_or_ge i k with
| inl l => exact Or.inl l
| inr g => exact Or.inr (Nat.le_antisymm (Nat.le_of_lt_succ h) g)
theorem sumI_pos (h : Nat -> Nat) (j : Nat) (hj : 1 <= h j) : forall k, j < k -> 1 <= sumI h k
| 0, hk => absurd hk (Nat.not_succ_le_zero j)
| k + 1, hk => by
show 1 <= sumI h k + h k
cases lt_succ_cases hk with
| inl l => exact Nat.le_trans (sumI_pos h j hj k l) (Nat.le_add_right _ _)
| inr e => rw [<- e]; exact Nat.le_trans hj (Nat.le_add_left _ _)
theorem findx (g : Nat -> Bool) (c : Nat -> Nat) (M : Nat) : forall k,
(Exists fun i => i < k /\ g i = true /\ M < c i) \/ (forall i, i < k -> g i = true -> c i <= M)
| 0 => Or.inr (fun i hi _ => absurd hi (Nat.not_succ_le_zero i))
| k + 1 => by
cases findx g c M k with
| inl ex =>
cases ex with
| intro i hi => exact Or.inl (Exists.intro i (And.intro (Nat.lt_succ_of_lt hi.1) hi.2))
| inr al =>
cases eg : g k with
| false =>
refine Or.inr (fun i hi hg => ?_)
cases lt_succ_cases hi with
| inl l => exact al i l hg
| inr e => rw [e, eg] at hg; exact Bool.noConfusion hg
| true =>
cases Nat.lt_or_ge M (c k) with
| inl l => exact Or.inl (Exists.intro k (And.intro (Nat.lt_succ_self k) (And.intro eg l)))
| inr g' =>
refine Or.inr (fun i hi hg => ?_)
cases lt_succ_cases hi with
| inl l => exact al i l hg
| inr e => rw [e]; exact g'
/-! The greedy disjoint subfamily. -/
def allDj (m : Nat) (B S : Nat -> Bool) : List (Nat -> Bool) -> Bool
| [] => true
| T :: K => !(meets m B S T) && allDj m B S K
def cIf (b : Bool) (S : Nat -> Bool) (K : List (Nat -> Bool)) : List (Nat -> Bool) :=
match b with
| true => S :: K
| false => K
def grd (m : Nat) (B : Nat -> Bool) : List (Nat -> Bool) -> List (Nat -> Bool) -> List (Nat -> Bool)
| [], K => K
| S :: L, K => grd m B L (cIf (allDj m B S K) S K)
def Dj (m : Nat) (B : Nat -> Bool) (S T : Nat -> Bool) : Prop := meets m B S T = false
theorem allDj_true (m : Nat) (B S : Nat -> Bool) : forall K : List (Nat -> Bool), allDj m B S K = true ->
forall T, T ∈ K -> meets m B S T = false
| [], _, _, hT => nomatch hT
| T' :: K, h, T, hT => by
have h' : (!(meets m B S T') && allDj m B S K) = true := h
cases e1 : meets m B S T' with
| true => rw [e1] at h'; exact Bool.noConfusion h'
| false =>
rw [e1] at h'
cases mem_cons_cases hT with
| inl q => rw [q]; exact e1
| inr h2 => exact allDj_true m B S K h' T h2
theorem allDj_false (m : Nat) (B S : Nat -> Bool) : forall K : List (Nat -> Bool), allDj m B S K = false ->
Exists fun T => T ∈ K /\ meets m B S T = true
| [], h => Bool.noConfusion h
| T' :: K, h => by
have h' : (!(meets m B S T') && allDj m B S K) = false := h
cases e1 : meets m B S T' with
| true => exact Exists.intro T' (And.intro (List.Mem.head K) e1)
| false =>
rw [e1] at h'
cases allDj_false m B S K h' with
| intro T hT => exact Exists.intro T (And.intro (List.Mem.tail T' hT.1) hT.2)
theorem cIf_keep (b : Bool) (S T : Nat -> Bool) (K : List (Nat -> Bool)) (h : T ∈ K) : T ∈ cIf b S K := by
cases b with
| true => exact List.Mem.tail S h
| false => exact h
theorem grd_keep (m : Nat) (B : Nat -> Bool) : forall (L K : List (Nat -> Bool)) (T : Nat -> Bool), T ∈ K -> T ∈ grd m B L K
| [], _, _, h => h
| S :: L, K, T, h => grd_keep m B L _ T (cIf_keep _ S T K h)
theorem grd_mem (m : Nat) (B : Nat -> Bool) : forall (L K : List (Nat -> Bool)) (T : Nat -> Bool), T ∈ grd m B L K ->
T ∈ L \/ T ∈ K
| [], _, _, h => Or.inr h
| S :: L, K, T, h => by
cases grd_mem m B L _ T h with
| inl l => exact Or.inl (List.Mem.tail S l)
| inr k =>
cases e : allDj m B S K with
| true =>
have t : cIf (allDj m B S K) S K = S :: K := congrArg (fun b => cIf b S K) e
rw [t] at k
cases mem_cons_cases k with
| inl q => rw [q]; exact Or.inl (List.Mem.head L)
| inr k2 => exact Or.inr k2
| false =>
have t : cIf (allDj m B S K) S K = K := congrArg (fun b => cIf b S K) e
rw [t] at k
exact Or.inr k
theorem grd_pw (m : Nat) (B : Nat -> Bool) : forall (L K : List (Nat -> Bool)), Pw (Dj m B) K -> Pw (Dj m B) (grd m B L K)
| [], _, h => h
| S :: L, K, h => by
refine grd_pw m B L _ ?_
cases e : allDj m B S K with
| true => exact And.intro (fun T hT => allDj_true m B S K e T hT) h
| false => exact h
theorem meets_self (m : Nat) (B S : Nat -> Bool) (h : nonE m B S = true) : meets m B S S = true := by
cases anyI_ex (dif B S) m h with
| intro j hj =>
refine anyI_true _ j ?_ m hj.1
exact congrArg (fun b => b && b) hj.2
theorem grd_cov (m : Nat) (B : Nat -> Bool) : forall (L K : List (Nat -> Bool)) (S : Nat -> Bool), S ∈ L ->
nonE m B S = true -> Exists fun T => T ∈ grd m B L K /\ meets m B S T = true
| [], _, _, h, _ => nomatch h
| S' :: L, K, S, h, hn => by
cases mem_cons_cases h with
| inr h2 => exact grd_cov m B L _ S h2 hn
| inl e =>
rw [e] at hn
rw [e]
cases e2 : allDj m B S' K with
| true =>
have t : cIf (allDj m B S' K) S' K = S' :: K := congrArg (fun b => cIf b S' K) e2
refine Exists.intro S' (And.intro ?_ (meets_self m B S' hn))
show S' ∈ grd m B L (cIf (allDj m B S' K) S' K)
rw [t]
exact grd_keep m B L _ S' (List.Mem.head K)
| false =>
cases allDj_false m B S' K e2 with
| intro T hT => exact Exists.intro T (And.intro (grd_keep m B L _ T (cIf_keep _ S' T K hT.1)) hT.2)
/-! Pieces of the main step. -/
theorem core_bool (s t b : Bool) (hs : b = true -> s = true) (ht : b = true -> t = true)
(hd : ((s && !b) && (t && !b)) = false) : (s && t) = b := by
cases s <;> cases t <;> cases b <;>
first | rfl | exact Bool.noConfusion (hs rfl) | exact Bool.noConfusion (ht rfl) | exact Bool.noConfusion hd
theorem and_true_l {a b : Bool} (h : (a && b) = true) : a = true := by
cases a with
| false => exact Bool.noConfusion h
| true => rfl
theorem and_true_r {a b : Bool} (h : (a && b) = true) : b = true := by
cases a with
| false => exact Bool.noConfusion h
| true => exact h
/-- Disjoint (outside B) members above B form a sunflower with core B. -/
theorem dj_sun (m : Nat) (B : Nat -> Bool) (S T : Nat -> Bool) (hS : SubB m B S) (hT : SubB m B T)
(hn : nonE m B S = true) (hd : Dj m B S T) :
(forall i, i < m -> (S i && T i) = B i) /\ Not (SetEq m S T) := by
refine And.intro (fun i hi => ?_) (fun he => ?_)
· exact core_bool (S i) (T i) (B i) (hS i hi) (hT i hi) (anyI_false _ m hd i hi)
· cases anyI_ex (dif B S) m hn with
| intro j hj =>
have hj' : dif B T j = true := by
show (T j && !(B j)) = true
rw [<- he j hj.1]; exact hj.2
have t : meets m B S T = true := by
refine anyI_true _ j ?_ m hj.1
show (dif B S j && dif B T j) = true
rw [hj.2, hj']
try rfl
rw [hd] at t
exact Bool.noConfusion t
theorem beq_eq : forall a b : Nat, Nat.beq a b = true -> a = b
| 0, 0, _ => rfl
| 0, _ + 1, h => Bool.noConfusion h
| _ + 1, 0, h => Bool.noConfusion h
| a + 1, b + 1, h => congrArg (· + 1) (beq_eq a b h)
theorem beq_self : forall a : Nat, Nat.beq a a = true
| 0 => rfl
| a + 1 => beq_self a
/-- The new base B + {x}. -/
def addB (B : Nat -> Bool) (x : Nat) (i : Nat) : Bool := B i || Nat.beq i x
theorem sumI_one_more (f g : Nat -> Nat) (x : Nat) (hf : f x = 1) (hg : g x = 0)
(he : forall i, Nat.beq i x = false -> f i = g i) : forall k, x < k -> sumI f k = sumI g k + 1
| 0, h => absurd h (Nat.not_succ_le_zero x)
| k + 1, h => by
show sumI f k + f k = sumI g k + g k + 1
cases lt_succ_cases h with
| inl l =>
have ek : f k = g k := by
apply he
cases e : Nat.beq k x with
| false => rfl
| true =>
have q := beq_eq k x e
rw [q] at l
exact absurd l (Nat.lt_irrefl x)
rw [sumI_one_more f g x hf hg he k l, ek, Nat.add_right_comm]
| inr e =>
have es : sumI f k = sumI g k := by
refine sumI_ext_lt f g k (fun i hi => he i ?_)
cases eb : Nat.beq i x with
| false => rfl
| true =>
have q := beq_eq i x eb
rw [q, e] at hi
exact absurd hi (Nat.lt_irrefl k)
rw [es, <- e, hf, hg]
try rfl
theorem card_split (m : Nat) (B S : Nat -> Bool) (x : Nat) (hx : x < m) (hS : dif B S x = true) :
card m (dif B S) = card m (dif (addB B x) S) + 1 := by
show sumI (fun i => bv (dif B S i)) m = sumI (fun i => bv (dif (addB B x) S i)) m + 1
refine sumI_one_more _ _ x ?_ ?_ ?_ m hx
· exact congrArg bv hS
· show bv (S x && !(B x || Nat.beq x x)) = 0
rw [beq_self x, or_true_r]
cases S x <;> rfl
· intro i hi
show bv (S i && !(B i)) = bv (S i && !(B i || Nat.beq i x))
rw [hi]
cases B i <;> rfl
theorem lsum_zero_of {α : Type} (h : α -> Nat) : forall L : List α, (forall a, a ∈ L -> h a = 0) -> lsum h L = 0
| [], _ => rfl
| a :: l, hl => by
show h a + lsum h l = 0
rw [hl a (List.Mem.head l), lsum_zero_of h l (fun b hb => hl b (List.Mem.tail a hb))]
/-- At most one member of a family of pairwise different sets satisfies a condition that fixes the set. -/
theorem cnt_le_one (m : Nat) (p : (Nat -> Bool) -> Bool) : forall L : List (Nat -> Bool),
Pw (fun S T => Not (SetEq m S T)) L ->
(forall S T, S ∈ L -> T ∈ L -> p S = true -> p T = true -> SetEq m S T) -> lsum (fun S => bv (p S)) L <= 1
| [], _, _ => Nat.zero_le 1
| S :: L, hd, hp => by
show bv (p S) + lsum (fun S => bv (p S)) L <= 1
cases e : p S with
| false =>
show 0 + _ <= 1
rw [Nat.zero_add]
exact cnt_le_one m p L hd.2 (fun a b ha hb => hp a b (List.Mem.tail S ha) (List.Mem.tail S hb))
| true =>
have z : lsum (fun S => bv (p S)) L = 0 := lsum_zero_of _ L (fun T hT => by
cases eT : p T with
| false => rfl
| true => exact absurd (hp S T (List.Mem.head L) (List.Mem.tail S hT) e eT) (hd.1 T hT))
exact Nat.le_of_eq (congrArg (bv true + ·) z)
theorem lsum_filt {α : Type} (r : α -> Bool) : forall L : List α, (filt r L).length = lsum (fun a => bv (r a)) L
| [] => rfl
| a :: l => by
cases e : r a with
| true =>
have t : filt r (a :: l) = a :: filt r l := congrArg (fun b => consB b a (filt r l)) e
rw [t]
show (filt r l).length + 1 = bv (r a) + lsum (fun a => bv (r a)) l
rw [lsum_filt r l, e]
exact Nat.add_comm _ _
| false =>
have t : filt r (a :: l) = filt r l := congrArg (fun b => consB b a (filt r l)) e
rw [t]
show (filt r l).length = bv (r a) + lsum (fun a => bv (r a)) l
rw [lsum_filt r l, e]
exact (Nat.zero_add _).symm
theorem bv_split (a b : Bool) : bv a = bv (a && b) + bv (a && !b) := by
cases a <;> cases b <;> rfl
theorem lsum_split {α : Type} (p q : α -> Bool) (L : List α) :
lsum (fun a => bv (p a)) L = lsum (fun a => bv (p a && q a)) L + lsum (fun a => bv (p a && !(q a))) L := by
induction L with
| nil => rfl
| cons a l ih =>
show bv (p a) + lsum (fun a => bv (p a)) l =
(bv (p a && q a) + lsum (fun a => bv (p a && q a)) l) + (bv (p a && !(q a)) + lsum (fun a => bv (p a && !(q a))) l)
rw [ih, bv_split (p a) (q a), Found.Pnp93.add4]
/-- A member above B containing x with nothing outside B + {x} is B + {x}. -/
theorem fixed_set (m : Nat) (B S : Nat -> Bool) (x : Nat) (hS : SubB m B S) (hx : S x = true)
(he : nonE m (addB B x) S = false) : forall i, i < m -> S i = addB B x i := by
intro i hi
have z : (S i && !(B i || Nat.beq i x)) = false := anyI_false _ m he i hi
show S i = (B i || Nat.beq i x)
cases eB : B i with
| true => rw [hS i hi eB]; rfl
| false =>
cases eb : Nat.beq i x with
| true => rw [beq_eq i x eb]; exact hx
| false =>
rw [eB, eb] at z
cases eS : S i with
| false => rfl
| true => rw [eS] at z; exact Bool.noConfusion z
/-! The lemma. -/
theorem sunflower (m q : Nat) : forall (l : Nat) (B : Nat -> Bool) (F : List (Nat -> Bool)),
(forall S, S ∈ F -> SubB m B S) -> Pw (fun S T => Not (SetEq m S T)) F ->
(forall S, S ∈ F -> nonE m B S = true /\ card m (dif B S) <= l) -> Sb q l < F.length ->
Exists fun W : List (Nat -> Bool) => Exists fun C : Nat -> Bool =>
Sunflower m C W /\ W.length = q + 1 /\ (forall S, S ∈ W -> S ∈ F)
| 0, B, F, _, _, hsz, hlen => by
cases F with
| nil => exact absurd hlen (Nat.lt_irrefl 0)
| cons S F =>
have h := hsz S (List.Mem.head F)
cases anyI_ex (dif B S) m h.1 with
| intro j hj =>
have p := sumI_pos (fun i => bv (dif B S i)) j (Nat.le_of_eq (congrArg bv hj.2).symm) m hj.1
exact absurd (Nat.le_trans p h.2) (Nat.not_succ_le_zero 0)
| l + 1, B, F, hsub, hdis, hsz, hlen => by
have hK := grd_pw m B F [] True.intro
have inF : forall S, S ∈ grd m B F [] -> S ∈ F := fun S hS => by
cases grd_mem m B F [] S hS with
| inl a => exact a
| inr b => exact nomatch b
cases Nat.lt_or_ge q (grd m B F []).length with
| inl big =>
-- q + 1 pairwise disjoint members: a sunflower with core B
refine Exists.intro (takeL (q + 1) (grd m B F [])) (Exists.intro B (And.intro ?_ (And.intro ?_ ?_)))
· refine pw_imp (Dj m B) _ _ ?_ (pw_take (Dj m B) (q + 1) _ hK)
intro S T hS hT hd
have hS' := inF S (mem_take S _ _ hS)
have hT' := inF T (mem_take T _ _ hT)
exact dj_sun m B S T (hsub S hS') (hsub T hT') (hsz S hS').1 hd
· exact len_take (q + 1) _ big
· intro S hS
exact inF S (mem_take S _ _ hS)
| inr small =>
let K := grd m B F []
let U : Nat -> Bool := fun i => anyL (fun T => dif B T i) K
let cnt : Nat -> Nat := fun i => lsum (fun S => bv (dif B S i)) F
cases findx U cnt (Sb q l + 1) m with
| inr al =>
-- every point of the union is in at most Sb q l + 1 members: too few members
have hA : F.length <= sumI (fun i => bv (U i) * cnt i) m := by
rw [len_lsum F]
have t : lsum (fun _ => 1) F <= lsum (fun S => sumI (fun i => bv (U i) * bv (dif B S i)) m) F := by
refine lsum_le _ _ F (fun S hS => ?_)
cases grd_cov m B F [] S hS (hsz S hS).1 with
| intro T hT =>
cases anyI_ex _ m hT.2 with
| intro j hj =>
refine sumI_pos _ j ?_ m hj.1
have d1 : dif B S j = true := and_true_l hj.2
have d2 : dif B T j = true := and_true_r hj.2
have u : U j = true := anyL_true (fun T => dif B T j) T d2 K hT.1
exact Nat.le_of_eq (congr (congrArg (fun a b => bv a * bv b) u) d1).symm
refine Nat.le_trans t (Nat.le_of_eq (Eq.trans (swapS (fun S i => bv (U i) * bv (dif B S i)) m F).symm ?_))
exact sumI_ext _ _ (fun i => (mul_lsum (bv (U i)) (fun S => bv (dif B S i)) F).symm) m
have hC : sumI (fun i => bv (U i) * cnt i) m <= sumI (fun i => bv (U i)) m * (Sb q l + 1) := by
rw [<- sumI_mul_r]
refine sumI_le _ _ m (fun i hi => ?_)
cases eU : U i with
| false => show 0 * cnt i <= 0 * (Sb q l + 1); rw [zero_mul', zero_mul']; exact Nat.le_refl 0
| true => exact mul_le_l' (al i hi eU) (bv true)
have hB : sumI (fun i => bv (U i)) m <= q * (l + 1) := by
have t1 : sumI (fun i => bv (U i)) m <= sumI (fun i => lsum (fun T => bv (dif B T i)) K) m :=
sumI_le _ _ m (fun i _ => bv_anyL_le (fun T => dif B T i) K)
have t1' := Nat.le_trans t1 (Nat.le_of_eq (swapS (fun T i => bv (dif B T i)) m K))
have t2 : lsum (fun T => sumI (fun i => bv (dif B T i)) m) K <= K.length * (l + 1) :=
lsum_const_le _ _ K (fun T hT => (hsz T (inF T hT)).2)
exact Nat.le_trans t1' (Nat.le_trans t2 (mul_le_r small (l + 1)))
have hall : F.length <= Sb q (l + 1) :=
Nat.le_trans hA (Nat.le_trans hC (mul_le_r hB (Sb q l + 1)))
exact absurd (Nat.lt_of_lt_of_le hlen hall) (Nat.lt_irrefl _)
| inl ex =>
cases ex with
| intro x hx =>
-- the members containing x, above B + {x}
let r : (Nat -> Bool) -> Bool := fun S => dif B S x && nonE m (addB B x) S
have hF' := sunflower m q l (addB B x) (filt r F)
(fun S hS => by
have h := mem_filt r S F hS
intro i hi hb
cases eB : B i with
| true => exact hsub S h.1 i hi eB
| false =>
have hb' : (B i || Nat.beq i x) = true := hb
rw [eB] at hb'
rw [beq_eq i x hb']
exact and_true_l (and_true_l h.2))
(pw_filt _ r F hdis)
(fun S hS => by
have h := mem_filt r S F hS
refine And.intro (and_true_r h.2) ?_
have e := card_split m B S x hx.1 (and_true_l h.2)
have t := (hsz S h.1).2
rw [e] at t
exact Nat.le_of_succ_le_succ t)
(by
show Sb q l < (filt r F).length
rw [lsum_filt r F]
have s := lsum_split (fun S => dif B S x) (fun S => nonE m (addB B x) S) F
have o : lsum (fun S => bv (dif B S x && !(nonE m (addB B x) S))) F <= 1 := by
refine cnt_le_one m (fun S => dif B S x && !(nonE m (addB B x) S)) F hdis ?_
intro S T hS hT hpS hpT
have fS := fixed_set m B S x (hsub S hS) (and_true_l (and_true_l hpS)) (Found.Pnp91.not_true (and_true_r hpS))
have fT := fixed_set m B T x (hsub T hT) (and_true_l (and_true_l hpT)) (Found.Pnp91.not_true (and_true_r hpT))
intro i hi
exact (fS i hi).trans (fT i hi).symm
have h1 : lsum (fun S => bv (dif B S x)) F <= lsum (fun a => bv (r a)) F + 1 :=
Nat.le_trans (Nat.le_of_eq s) (Nat.add_le_add_left o _)
exact Nat.lt_of_succ_lt_succ (Nat.lt_of_lt_of_le hx.2.2 h1))
cases hF' with
| intro W hW =>
cases hW with
| intro C hC =>
exact Exists.intro W (Exists.intro C (And.intro hC.1 (And.intro hC.2.1
(fun S hS => (mem_filt r S F (hC.2.2 S hS)).1))))
end Found.Pnp115
#print axioms Found.Pnp115.mem_cons_cases
#print axioms Found.Pnp115.mem_filt
#print axioms Found.Pnp115.pw_filt
#print axioms Found.Pnp115.mem_take
#print axioms Found.Pnp115.pw_take
#print axioms Found.Pnp115.len_take
#print axioms Found.Pnp115.pw_imp
#print axioms Found.Pnp115.anyL_true
#print axioms Found.Pnp115.bv_or_le
#print axioms Found.Pnp115.bv_anyL_le
#print axioms Found.Pnp115.one_mul'
#print axioms Found.Pnp115.len_lsum
#print axioms Found.Pnp115.lsum_le
#print axioms Found.Pnp115.swapS
#print axioms Found.Pnp115.mul_lsum
#print axioms Found.Pnp115.sumI_mul_r
#print axioms Found.Pnp115.lsum_const_le
#print axioms Found.Pnp115.lt_succ_cases
#print axioms Found.Pnp115.sumI_pos
#print axioms Found.Pnp115.findx
#print axioms Found.Pnp115.allDj_true
#print axioms Found.Pnp115.allDj_false
#print axioms Found.Pnp115.cIf_keep
#print axioms Found.Pnp115.grd_keep
#print axioms Found.Pnp115.grd_mem
#print axioms Found.Pnp115.grd_pw
#print axioms Found.Pnp115.meets_self
#print axioms Found.Pnp115.grd_cov
#print axioms Found.Pnp115.core_bool
#print axioms Found.Pnp115.and_true_l
#print axioms Found.Pnp115.and_true_r
#print axioms Found.Pnp115.dj_sun
#print axioms Found.Pnp115.beq_eq
#print axioms Found.Pnp115.beq_self
#print axioms Found.Pnp115.sumI_one_more
#print axioms Found.Pnp115.card_split
#print axioms Found.Pnp115.lsum_zero_of
#print axioms Found.Pnp115.cnt_le_one
#print axioms Found.Pnp115.lsum_filt
#print axioms Found.Pnp115.bv_split
#print axioms Found.Pnp115.lsum_split
#print axioms Found.Pnp115.fixed_set
#print axioms Found.Pnp115.sunflower
#print axioms: 43 theorems, no axioms.
The tree of the monotone switching lemma: its leaves pierce every set of the list, its cut paths have exactly \(d\) elements and number at most \(w^d\), and every assignment hitting all sets covers a leaf or a cut path.
found/Pnp116.lean
/-
T330 piece 116. found/Pnp116 -- the transversal tree of the monotone switching lemma
(Jukna, Boolean Function Complexity, Lemma 9.15; proof complexity ladder, step 2).
For a list C of sets (lists of variables), each with at most w elements, and a depth budget
d, the tree tr C d p grows the set p: if p pierces every member of C it is a leaf (first
list); with no budget left it is cut (second list); otherwise it branches on the variables of
the first member of C that p misses. Proved:
* tr_fst_pierce: every leaf pierces every member of C;
* tr_fst_len, tr_snd_len: leaves have fewer than |p| + d elements, cuts exactly |p| + d;
* tr_snd_count: there are at most w ^ d cuts;
* tr_complete: if y covers p and hits every member of C, then y covers some leaf or cut;
* leaf_hits: if y covers a set that pierces q, then y hits q.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp115
namespace Found.Pnp116
open Found.Pnp115 (anyL anyL_true mem_cons_cases and_true_l and_true_r beq_eq beq_self)
open Found.Pnp98 (mul_le_r)
open Found.Pnp65 (or_true_r)
def memN (v : Nat) : List Nat -> Bool
| [] => false
| a :: l => Nat.beq v a || memN v l
def allL (y : Nat -> Bool) : List Nat -> Bool
| [] => true
| a :: l => y a && allL y l
/-- p meets q. -/
def pierce (p q : List Nat) : Bool := anyL (fun v => memN v p) q
def selO (b : Bool) (q : List Nat) (o : Option (List Nat)) : Option (List Nat) :=
match b with
| true => o
| false => some q
/-- The first member of C that p misses. -/
def firstU (p : List Nat) : List (List Nat) -> Option (List Nat)
| [] => none
| q :: C => selO (pierce p q) q (firstU p C)
def app2 (a b : List (List Nat) × List (List Nat)) : List (List Nat) × List (List Nat) :=
(a.1 ++ b.1, a.2 ++ b.2)
def brs (f : Nat -> List (List Nat) × List (List Nat)) : List Nat -> List (List Nat) × List (List Nat)
| [] => ([], [])
| v :: q => app2 (f v) (brs f q)
def selT (o : Option (List Nat)) (p : List Nat) (g : List Nat -> List (List Nat) × List (List Nat)) :
List (List Nat) × List (List Nat) :=
match o with
| none => ([p], [])
| some q => g q
/-- The transversal tree: (leaves, cuts). -/
def tr (C : List (List Nat)) : Nat -> List Nat -> List (List Nat) × List (List Nat)
| 0, p => ([], [p])
| d + 1, p => selT (firstU p C) p (fun q => brs (fun v => tr C d (v :: p)) q)
/-! Lists. -/
theorem mem_app {α : Type} (x : α) : forall (A B : List α), x ∈ A ++ B -> x ∈ A \/ x ∈ B
| [], _, h => Or.inr h
| a :: A, B, h => by
cases mem_cons_cases h with
| inl q => rw [q]; exact Or.inl (List.Mem.head A)
| inr h2 =>
cases mem_app x A B h2 with
| inl l => exact Or.inl (List.Mem.tail a l)
| inr r => exact Or.inr r
theorem mem_app_l {α : Type} (x : α) (B : List α) : forall A : List α, x ∈ A -> x ∈ A ++ B
| [], h => nomatch h
| a :: A, h => by
cases mem_cons_cases h with
| inl q => rw [q]; exact List.Mem.head _
| inr h2 => exact List.Mem.tail a (mem_app_l x B A h2)
theorem mem_app_r {α : Type} (x : α) (B : List α) : forall A : List α, x ∈ B -> x ∈ A ++ B
| [], h => h
| a :: A, h => List.Mem.tail a (mem_app_r x B A h)
theorem len_app {α : Type} : forall (A B : List α), (A ++ B).length = A.length + B.length
| [], B => (Nat.zero_add _).symm
| _ :: A, B => by
show (A ++ B).length + 1 = A.length + 1 + B.length
rw [len_app A B, Nat.add_right_comm]
theorem anyL_ex {α : Type} (g : α -> Bool) : forall L : List α, anyL g L = true -> Exists fun a => a ∈ L /\ g a = true
| [], h => Bool.noConfusion h
| a :: l, h => by
cases e : g a with
| true => exact Exists.intro a (And.intro (List.Mem.head l) e)
| false =>
have h' : (g a || anyL g l) = true := h
rw [e] at h'
cases anyL_ex g l h' with
| intro b hb => exact Exists.intro b (And.intro (List.Mem.tail a hb.1) hb.2)
theorem memN_mem : forall (v : Nat) (l : List Nat), memN v l = true -> v ∈ l
| _, [], h => Bool.noConfusion h
| v, a :: l, h => by
cases e : Nat.beq v a with
| true => rw [beq_eq v a e]; exact List.Mem.head l
| false =>
have h' : (Nat.beq v a || memN v l) = true := h
rw [e] at h'
exact List.Mem.tail a (memN_mem v l h')
theorem allL_mem (y : Nat -> Bool) : forall (l : List Nat) (v : Nat), allL y l = true -> v ∈ l -> y v = true
| [], _, _, h => nomatch h
| a :: l, v, h, hv => by
cases mem_cons_cases hv with
| inl q => rw [q]; exact and_true_l h
| inr h2 => exact allL_mem y l v (and_true_r h) h2
/-! The first missed member. -/
theorem firstU_none (p : List Nat) : forall C : List (List Nat), firstU p C = none -> forall q, q ∈ C -> pierce p q = true
| [], _, _, hq => nomatch hq
| q' :: C, h, q, hq => by
cases e : pierce p q' with
| false =>
have h' : selO (pierce p q') q' (firstU p C) = none := h
rw [e] at h'
cases h'
| true =>
have h' : selO (pierce p q') q' (firstU p C) = none := h
rw [e] at h'
cases mem_cons_cases hq with
| inl r => rw [r]; exact e
| inr h2 => exact firstU_none p C h' q h2
theorem firstU_some (p : List Nat) : forall (C : List (List Nat)) (q : List Nat), firstU p C = some q ->
q ∈ C /\ pierce p q = false
| [], _, h => by cases h
| q' :: C, q, h => by
cases e : pierce p q' with
| false =>
have h' : selO (pierce p q') q' (firstU p C) = some q := h
rw [e] at h'
cases h'
exact And.intro (List.Mem.head C) e
| true =>
have h' : selO (pierce p q') q' (firstU p C) = some q := h
rw [e] at h'
have r := firstU_some p C q h'
exact And.intro (List.Mem.tail q' r.1) r.2
/-! Branching. -/
theorem mem_brs1 (f : Nat -> List (List Nat) × List (List Nat)) (m : List Nat) : forall q : List Nat,
m ∈ (brs f q).1 -> Exists fun v => v ∈ q /\ m ∈ (f v).1
| [], h => nomatch h
| v :: q, h => by
cases mem_app m (f v).1 (brs f q).1 h with
| inl l => exact Exists.intro v (And.intro (List.Mem.head q) l)
| inr r =>
cases mem_brs1 f m q r with
| intro u hu => exact Exists.intro u (And.intro (List.Mem.tail v hu.1) hu.2)
theorem mem_brs2 (f : Nat -> List (List Nat) × List (List Nat)) (m : List Nat) : forall q : List Nat,
m ∈ (brs f q).2 -> Exists fun v => v ∈ q /\ m ∈ (f v).2
| [], h => nomatch h
| v :: q, h => by
cases mem_app m (f v).2 (brs f q).2 h with
| inl l => exact Exists.intro v (And.intro (List.Mem.head q) l)
| inr r =>
cases mem_brs2 f m q r with
| intro u hu => exact Exists.intro u (And.intro (List.Mem.tail v hu.1) hu.2)
theorem brs_of1 (f : Nat -> List (List Nat) × List (List Nat)) (m : List Nat) (v : Nat) (hm : m ∈ (f v).1) :
forall q : List Nat, v ∈ q -> m ∈ (brs f q).1
| [], h => nomatch h
| u :: q, h => by
cases mem_cons_cases h with
| inl e => rw [<- e]; exact mem_app_l m _ _ hm
| inr h2 => exact mem_app_r m _ _ (brs_of1 f m v hm q h2)
theorem brs_of2 (f : Nat -> List (List Nat) × List (List Nat)) (m : List Nat) (v : Nat) (hm : m ∈ (f v).2) :
forall q : List Nat, v ∈ q -> m ∈ (brs f q).2
| [], h => nomatch h
| u :: q, h => by
cases mem_cons_cases h with
| inl e => rw [<- e]; exact mem_app_l m _ _ hm
| inr h2 => exact mem_app_r m _ _ (brs_of2 f m v hm q h2)
theorem brs_count (f : Nat -> List (List Nat) × List (List Nat)) (M : Nat) (hf : forall v, (f v).2.length <= M) :
forall q : List Nat, (brs f q).2.length <= q.length * M
| [] => by show 0 <= 0 * M; exact Nat.zero_le _
| v :: q => by
show ((f v).2 ++ (brs f q).2).length <= (q.length + 1) * M
rw [len_app, Found.Pnp98.add_mul', Found.Pnp115.one_mul', Nat.add_comm (q.length * M)]
exact Nat.add_le_add (hf v) (brs_count f M hf q)
/-! The tree. -/
theorem tr_none (C : List (List Nat)) (d : Nat) (p : List Nat) (e : firstU p C = none) : tr C (d + 1) p = ([p], []) :=
congrArg (fun o => selT o p (fun q => brs (fun v => tr C d (v :: p)) q)) e
theorem tr_some (C : List (List Nat)) (d : Nat) (p q : List Nat) (e : firstU p C = some q) :
tr C (d + 1) p = brs (fun v => tr C d (v :: p)) q :=
congrArg (fun o => selT o p (fun q => brs (fun v => tr C d (v :: p)) q)) e
/-- Every leaf pierces every member of C. -/
theorem tr_fst_pierce (C : List (List Nat)) : forall (d : Nat) (p m : List Nat), m ∈ (tr C d p).1 ->
forall q, q ∈ C -> pierce m q = true
| 0, _, _, h => nomatch h
| d + 1, p, m, h => by
cases e : firstU p C with
| none =>
rw [tr_none C d p e] at h
cases mem_cons_cases h with
| inl r => rw [r]; exact firstU_none p C e
| inr h2 => exact nomatch h2
| some q =>
rw [tr_some C d p q e] at h
cases mem_brs1 _ m q h with
| intro v hv => exact tr_fst_pierce C d (v :: p) m hv.2
/-- Leaves are short. -/
theorem tr_fst_len (C : List (List Nat)) : forall (d : Nat) (p m : List Nat), m ∈ (tr C d p).1 ->
m.length + 1 <= p.length + d
| 0, _, _, h => nomatch h
| d + 1, p, m, h => by
cases e : firstU p C with
| none =>
rw [tr_none C d p e] at h
cases mem_cons_cases h with
| inl r => rw [r]; exact Nat.add_le_add_left (Nat.succ_le_succ (Nat.zero_le d)) _
| inr h2 => exact nomatch h2
| some q =>
rw [tr_some C d p q e] at h
cases mem_brs1 _ m q h with
| intro v hv =>
have t := tr_fst_len C d (v :: p) m hv.2
have t' : m.length + 1 <= p.length + 1 + d := t
rw [Nat.add_right_comm] at t'
exact t'
/-- Cuts have exactly |p| + d elements. -/
theorem tr_snd_len (C : List (List Nat)) : forall (d : Nat) (p m : List Nat), m ∈ (tr C d p).2 ->
m.length = p.length + d
| 0, p, m, h => by
cases mem_cons_cases h with
| inl r => rw [r]; rfl
| inr h2 => exact nomatch h2
| d + 1, p, m, h => by
cases e : firstU p C with
| none =>
rw [tr_none C d p e] at h
exact nomatch h
| some q =>
rw [tr_some C d p q e] at h
cases mem_brs2 _ m q h with
| intro v hv =>
have t := tr_snd_len C d (v :: p) m hv.2
have t' : m.length = p.length + 1 + d := t
rw [Nat.add_right_comm] at t'
exact t'
/-- At most w ^ d cuts. -/
theorem tr_snd_count (C : List (List Nat)) (w : Nat) (hw : forall q, q ∈ C -> q.length <= w) :
forall (d : Nat) (p : List Nat), (tr C d p).2.length <= w ^ d
| 0, _ => Nat.le_refl 1
| d + 1, p => by
cases e : firstU p C with
| none =>
rw [tr_none C d p e]
exact Nat.zero_le _
| some q =>
rw [tr_some C d p q e]
have hq := firstU_some p C q e
have t := brs_count (fun v => tr C d (v :: p)) (w ^ d) (fun v => tr_snd_count C w hw d (v :: p)) q
refine Nat.le_trans t ?_
show q.length * w ^ d <= w ^ d * w
rw [Nat.mul_comm (w ^ d) w]
exact mul_le_r (hw q hq.1) (w ^ d)
/-- If y covers p and hits every member of C, it covers a leaf or a cut. -/
theorem tr_complete (C : List (List Nat)) (y : Nat -> Bool) (hC : forall q, q ∈ C -> anyL y q = true) :
forall (d : Nat) (p : List Nat), allL y p = true ->
Exists fun m => (m ∈ (tr C d p).1 \/ m ∈ (tr C d p).2) /\ allL y m = true
| 0, p, hp => Exists.intro p (And.intro (Or.inr (List.Mem.head [])) hp)
| d + 1, p, hp => by
cases e : firstU p C with
| none =>
rw [tr_none C d p e]
exact Exists.intro p (And.intro (Or.inl (List.Mem.head [])) hp)
| some q =>
rw [tr_some C d p q e]
have hq := firstU_some p C q e
cases anyL_ex y q (hC q hq.1) with
| intro v hv =>
have hp' : allL y (v :: p) = true := by
show (y v && allL y p) = true
rw [hv.2, hp]
try rfl
cases tr_complete C y hC d (v :: p) hp' with
| intro m hm =>
refine Exists.intro m (And.intro ?_ hm.2)
cases hm.1 with
| inl l => exact Or.inl (brs_of1 _ m v l q hv.1)
| inr r => exact Or.inr (brs_of2 _ m v r q hv.1)
/-- If y covers a set that pierces q, then y hits q. -/
theorem leaf_hits (y : Nat -> Bool) (m q : List Nat) (hm : allL y m = true) (hp : pierce m q = true) :
anyL y q = true := by
cases anyL_ex _ q hp with
| intro v hv => exact anyL_true y v (allL_mem y m v hm (memN_mem v m hv.2)) q hv.1
end Found.Pnp116
#print axioms Found.Pnp116.mem_app
#print axioms Found.Pnp116.mem_app_l
#print axioms Found.Pnp116.mem_app_r
#print axioms Found.Pnp116.len_app
#print axioms Found.Pnp116.anyL_ex
#print axioms Found.Pnp116.memN_mem
#print axioms Found.Pnp116.allL_mem
#print axioms Found.Pnp116.firstU_none
#print axioms Found.Pnp116.firstU_some
#print axioms Found.Pnp116.mem_brs1
#print axioms Found.Pnp116.mem_brs2
#print axioms Found.Pnp116.brs_of1
#print axioms Found.Pnp116.brs_of2
#print axioms Found.Pnp116.brs_count
#print axioms Found.Pnp116.tr_none
#print axioms Found.Pnp116.tr_some
#print axioms Found.Pnp116.tr_fst_pierce
#print axioms Found.Pnp116.tr_fst_len
#print axioms Found.Pnp116.tr_snd_len
#print axioms Found.Pnp116.tr_snd_count
#print axioms Found.Pnp116.tr_complete
#print axioms Found.Pnp116.leaf_hits
#print axioms: 22 theorems, no axioms.
A DNF \(D\) becomes the CNF \(L\) of the leaves of its transversal tree and a CNF \(C\) becomes the DNF \(R\) of its leaves; the cut sets catch every error, and the cut sets of a prefix are cut sets of the whole list.
found/Pnp117.lean
/-
T330 piece 117. found/Pnp117 -- the monotone switching lemma in both directions, and cuts of
a prefix (Jukna, Boolean Function Complexity, Lemma 9.15 and Section 9.6; proof complexity
ladder, step 2).
A monotone DNF or CNF is a list of sets of variables. For a DNF D whose monomials have at most
w variables, the leaves of the transversal tree tr D s [] form a CNF L with clauses of fewer
than s variables and the cuts form at most w ^ s sets K of s variables:
D <= L, and if L holds but D fails then x misses some set of K.
Dually for a CNF C, the leaves of tr C r [] form a DNF R with
R <= C, and if C holds but R fails then x covers some set of K.
The cuts of the tree of a prefix P are cuts of the tree of P ++ Q: the cut sets of a family
of nested prefixes are all among those of the longest list. Proved:
* firstU_app, tr_prefix: cuts of a prefix;
* dnf_le_cnf, cnf_cut: DNF to CNF;
* dnf_le_cnf', dnf_cut: CNF to DNF.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp116
namespace Found.Pnp117
open Found.Pnp115 (anyL anyL_true mem_cons_cases and_true_l and_true_r)
open Found.Pnp116 (allL pierce firstU selO tr tr_none tr_some mem_brs2 brs_of2 tr_fst_pierce tr_complete
leaf_hits anyL_ex memN_mem allL_mem)
/-- Every set of the list satisfies g. -/
def allLL (g : List Nat -> Bool) : List (List Nat) -> Bool
| [] => true
| a :: l => g a && allLL g l
def evDNF (x : Nat -> Bool) (D : List (List Nat)) : Bool := anyL (allL x) D
def evCNF (x : Nat -> Bool) (C : List (List Nat)) : Bool := allLL (anyL x) C
def negF (x : Nat -> Bool) (v : Nat) : Bool := !(x v)
theorem allLL_mem (g : List Nat -> Bool) : forall (L : List (List Nat)) (a : List Nat), allLL g L = true -> a ∈ L -> g a = true
| [], _, _, h => nomatch h
| b :: L, a, h, ha => by
cases mem_cons_cases ha with
| inl q => rw [q]; exact and_true_l h
| inr h2 => exact allLL_mem g L a (and_true_r h) h2
theorem allLL_of (g : List Nat -> Bool) : forall L : List (List Nat), (forall a, a ∈ L -> g a = true) -> allLL g L = true
| [], _ => rfl
| b :: L, h => by
show (g b && allLL g L) = true
rw [h b (List.Mem.head L), allLL_of g L (fun a ha => h a (List.Mem.tail b ha))]
try rfl
theorem allLL_false (g : List Nat -> Bool) : forall L : List (List Nat), allLL g L = false -> Exists fun a => a ∈ L /\ g a = false
| [], h => Bool.noConfusion h
| b :: L, h => by
cases e : g b with
| false => exact Exists.intro b (And.intro (List.Mem.head L) e)
| true =>
have h' : (g b && allLL g L) = false := h
rw [e] at h'
cases allLL_false g L h' with
| intro a ha => exact Exists.intro a (And.intro (List.Mem.tail b ha.1) ha.2)
/-- A set not all true under x is hit by the complement of x. -/
theorem allL_false_neg (x : Nat -> Bool) : forall M : List Nat, allL x M = false -> anyL (negF x) M = true
| [], h => Bool.noConfusion h
| v :: M, h => by
cases e : x v with
| false =>
show (negF x v || anyL (negF x) M) = true
show ((!(x v)) || anyL (negF x) M) = true
rw [e]
rfl
| true =>
have h' : (x v && allL x M) = false := h
rw [e] at h'
show (negF x v || anyL (negF x) M) = true
rw [allL_false_neg x M h']
exact Found.Pnp65.or_true_r _
/-- A set not hit by x is all true under the complement of x. -/
theorem anyL_false_neg (x : Nat -> Bool) : forall M : List Nat, anyL x M = false -> allL (negF x) M = true
| [], _ => rfl
| v :: M, h => by
have h' : (x v || anyL x M) = false := h
cases e : x v with
| true => rw [e] at h'; exact Bool.noConfusion h'
| false =>
rw [e] at h'
show ((!(x v)) && allL (negF x) M) = true
rw [e, anyL_false_neg x M h']
rfl
/-- If the complement of x covers M, then x misses M. -/
theorem neg_cover_miss (x : Nat -> Bool) (M : List Nat) (h : allL (negF x) M = true) : anyL x M = false := by
cases e : anyL x M with
| false => rfl
| true =>
cases anyL_ex x M e with
| intro v hv =>
have t : negF x v = true := allL_mem (negF x) M v h hv.1
have t' : (!(x v)) = true := t
rw [hv.2] at t'
exact Bool.noConfusion t'
/-! Cuts of a prefix. -/
theorem firstU_app (p : List Nat) (Q : List (List Nat)) : forall (P : List (List Nat)) (q : List Nat),
firstU p P = some q -> firstU p (P ++ Q) = some q
| [], _, h => by cases h
| q' :: P, q, h => by
show selO (pierce p q') q' (firstU p (P ++ Q)) = some q
have h' : selO (pierce p q') q' (firstU p P) = some q := h
cases e : pierce p q' with
| false => rw [e] at h'; exact h'
| true => rw [e] at h'; exact firstU_app p Q P q h'
theorem tr_prefix (P Q : List (List Nat)) : forall (d : Nat) (p m : List Nat), m ∈ (tr P d p).2 -> m ∈ (tr (P ++ Q) d p).2
| 0, _, _, h => h
| d + 1, p, m, h => by
cases e : firstU p P with
| none =>
rw [tr_none P d p e] at h
exact nomatch h
| some q =>
rw [tr_some P d p q e] at h
rw [tr_some (P ++ Q) d p q (firstU_app p Q P q e)]
cases mem_brs2 _ m q h with
| intro v hv => exact brs_of2 _ m v (tr_prefix P Q d (v :: p) m hv.2) q hv.1
/-! DNF to CNF: leaves as clauses. -/
/-- D <= L. -/
theorem dnf_le_cnf (D : List (List Nat)) (s : Nat) (x : Nat -> Bool) (h : evDNF x D = true) :
evCNF x (tr D s []).1 = true := by
cases anyL_ex (allL x) D h with
| intro M hM =>
refine allLL_of _ _ (fun T hT => ?_)
have hp := tr_fst_pierce D s [] T hT M hM.1
cases anyL_ex _ M hp with
| intro v hv => exact anyL_true x v (allL_mem x M v hM.2 hv.1) T (memN_mem v T hv.2)
/-- If L holds and D fails, x misses some cut. -/
theorem cnf_cut (D : List (List Nat)) (s : Nat) (x : Nat -> Bool) (hL : evCNF x (tr D s []).1 = true)
(hD : evDNF x D = false) : Exists fun T => T ∈ (tr D s []).2 /\ anyL x T = false := by
have hall : forall M, M ∈ D -> anyL (negF x) M = true := by
intro M hM
cases e : allL x M with
| false => exact allL_false_neg x M e
| true =>
have t : evDNF x D = true := anyL_true (allL x) M e D hM
rw [hD] at t
exact Bool.noConfusion t
cases tr_complete D (negF x) hall s [] rfl with
| intro m hm =>
cases hm.1 with
| inl l =>
have t := allLL_mem (anyL x) _ m hL l
rw [neg_cover_miss x m hm.2] at t
exact Bool.noConfusion t
| inr r => exact Exists.intro m (And.intro r (neg_cover_miss x m hm.2))
/-! CNF to DNF: leaves as monomials. -/
/-- R <= C. -/
theorem dnf_le_cnf' (C : List (List Nat)) (r : Nat) (x : Nat -> Bool) (h : evDNF x (tr C r []).1 = true) :
evCNF x C = true := by
cases anyL_ex (allL x) _ h with
| intro M hM =>
exact allLL_of _ C (fun q hq => leaf_hits x M q hM.2 (tr_fst_pierce C r [] M hM.1 q hq))
/-- If C holds and R fails, x covers some cut. -/
theorem dnf_cut (C : List (List Nat)) (r : Nat) (x : Nat -> Bool) (hC : evCNF x C = true)
(hR : evDNF x (tr C r []).1 = false) : Exists fun T => T ∈ (tr C r []).2 /\ allL x T = true := by
cases tr_complete C x (fun q hq => allLL_mem (anyL x) C q hC hq) r [] rfl with
| intro m hm =>
cases hm.1 with
| inl l =>
have t : evDNF x (tr C r []).1 = true := anyL_true (allL x) m hm.2 _ l
rw [hR] at t
exact Bool.noConfusion t
| inr r' => exact Exists.intro m (And.intro r' hm.2)
end Found.Pnp117
#print axioms Found.Pnp117.allLL_mem
#print axioms Found.Pnp117.allLL_of
#print axioms Found.Pnp117.allLL_false
#print axioms Found.Pnp117.allL_false_neg
#print axioms Found.Pnp117.anyL_false_neg
#print axioms Found.Pnp117.neg_cover_miss
#print axioms Found.Pnp117.firstU_app
#print axioms Found.Pnp117.tr_prefix
#print axioms Found.Pnp117.dnf_le_cnf
#print axioms Found.Pnp117.cnf_cut
#print axioms Found.Pnp117.dnf_le_cnf'
#print axioms Found.Pnp117.dnf_cut
#print axioms: 12 theorems, no axioms.
Sorting by an integer key in decreasing order puts every element with key at least \(a\) into the longest prefix of such elements.
found/Pnp118.lean
/-
T330 piece 118. found/Pnp118 -- sorting by an integer key, and thresholds as prefixes
(proof complexity ladder, step 2: the threshold chain of a real gate).
Keys are integers as pairs (Pnp105) under zle. isort key L lists L in decreasing key order
(insertion sort). For a list sorted in decreasing order, the elements with key >= a are
exactly its longest prefix tw (key >= a), and the list is that prefix followed by the rest.
So the formulas built from the pairs above each threshold are prefixes of one master list.
Proved:
* ble_le, le_ble, zleB_zle, zle_zleB, zle_total: the comparison;
* mem_ins, mem_ins_of, mem_isort, mem_isort_of: sorting keeps the elements;
* ins_sorted, isort_sorted: sorting sorts;
* tw_dw, mem_tw, tw_complete: the threshold prefix.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp117
namespace Found.Pnp118
open Found.Pnp115 (Pw mem_cons_cases)
open Found.Pnp105 (zle zle_trans zle_refl)
theorem ble_le : forall a b : Nat, Nat.ble a b = true -> a <= b
| 0, _, _ => Nat.zero_le _
| _ + 1, 0, h => Bool.noConfusion h
| a + 1, b + 1, h => Nat.succ_le_succ (ble_le a b h)
theorem le_ble : forall a b : Nat, a <= b -> Nat.ble a b = true
| 0, _, _ => rfl
| _ + 1, 0, h => absurd h (Nat.not_succ_le_zero _)
| a + 1, b + 1, h => le_ble a b (Nat.le_of_succ_le_succ h)
def zleB (v w : Nat × Nat) : Bool := Nat.ble (v.1 + w.2) (w.1 + v.2)
theorem zleB_zle (v w : Nat × Nat) (h : zleB v w = true) : zle v w := ble_le _ _ h
theorem zle_zleB (v w : Nat × Nat) (h : zle v w) : zleB v w = true := le_ble _ _ h
theorem zle_total (v w : Nat × Nat) : zle v w \/ zle w v := by
cases Nat.lt_or_ge (v.1 + w.2) (w.1 + v.2) with
| inl l => exact Or.inl (Nat.le_of_lt l)
| inr g =>
refine Or.inr ?_
show w.1 + v.2 <= v.1 + w.2
exact g
theorem zle_of_not (v w : Nat × Nat) (h : zleB v w = false) : zle w v := by
cases zle_total v w with
| inl l => rw [zle_zleB v w l] at h; exact Bool.noConfusion h
| inr r => exact r
/-! Insertion sort, decreasing. -/
def selL {α : Type} (b : Bool) (l1 l2 : List α) : List α :=
match b with
| true => l1
| false => l2
def ins {α : Type} (key : α -> Nat × Nat) (u : α) : List α -> List α
| [] => [u]
| v :: l => selL (zleB (key v) (key u)) (u :: v :: l) (v :: ins key u l)
def isort {α : Type} (key : α -> Nat × Nat) : List α -> List α
| [] => []
| u :: l => ins key u (isort key l)
def Desc {α : Type} (key : α -> Nat × Nat) (L : List α) : Prop := Pw (fun u v => zle (key v) (key u)) L
theorem ins_true {α : Type} (key : α -> Nat × Nat) (u v : α) (l : List α) (e : zleB (key v) (key u) = true) :
ins key u (v :: l) = u :: v :: l :=
congrArg (fun b => selL b (u :: v :: l) (v :: ins key u l)) e
theorem ins_false {α : Type} (key : α -> Nat × Nat) (u v : α) (l : List α) (e : zleB (key v) (key u) = false) :
ins key u (v :: l) = v :: ins key u l :=
congrArg (fun b => selL b (u :: v :: l) (v :: ins key u l)) e
theorem mem_ins {α : Type} (key : α -> Nat × Nat) (u w : α) : forall l : List α, w ∈ ins key u l -> w = u \/ w ∈ l
| [], h => by
cases mem_cons_cases h with
| inl q => exact Or.inl q
| inr h2 => exact nomatch h2
| v :: l, h => by
cases e : zleB (key v) (key u) with
| true =>
rw [ins_true key u v l e] at h
exact mem_cons_cases h
| false =>
rw [ins_false key u v l e] at h
cases mem_cons_cases h with
| inl q => rw [q]; exact Or.inr (List.Mem.head l)
| inr h2 =>
cases mem_ins key u w l h2 with
| inl q => exact Or.inl q
| inr h3 => exact Or.inr (List.Mem.tail v h3)
theorem mem_ins_of {α : Type} (key : α -> Nat × Nat) (u w : α) : forall l : List α, (w = u \/ w ∈ l) -> w ∈ ins key u l
| [], h => by
cases h with
| inl q => rw [q]; exact List.Mem.head []
| inr h2 => exact nomatch h2
| v :: l, h => by
cases e : zleB (key v) (key u) with
| true =>
rw [ins_true key u v l e]
cases h with
| inl q => rw [q]; exact List.Mem.head _
| inr h2 => exact List.Mem.tail u h2
| false =>
rw [ins_false key u v l e]
cases h with
| inl q => exact List.Mem.tail v (mem_ins_of key u w l (Or.inl q))
| inr h2 =>
cases mem_cons_cases h2 with
| inl q => rw [q]; exact List.Mem.head _
| inr h3 => exact List.Mem.tail v (mem_ins_of key u w l (Or.inr h3))
theorem mem_isort {α : Type} (key : α -> Nat × Nat) (w : α) : forall l : List α, w ∈ isort key l -> w ∈ l
| [], h => nomatch h
| u :: l, h => by
cases mem_ins key u w (isort key l) h with
| inl q => rw [q]; exact List.Mem.head l
| inr h2 => exact List.Mem.tail u (mem_isort key w l h2)
theorem mem_isort_of {α : Type} (key : α -> Nat × Nat) (w : α) : forall l : List α, w ∈ l -> w ∈ isort key l
| [], h => nomatch h
| u :: l, h => by
refine mem_ins_of key u w (isort key l) ?_
cases mem_cons_cases h with
| inl q => exact Or.inl q
| inr h2 => exact Or.inr (mem_isort_of key w l h2)
theorem ins_sorted {α : Type} (key : α -> Nat × Nat) (u : α) : forall l : List α, Desc key l -> Desc key (ins key u l)
| [], _ => And.intro (fun _ h => nomatch h) True.intro
| v :: l, h => by
cases e : zleB (key v) (key u) with
| true =>
rw [ins_true key u v l e]
refine And.intro (fun w hw => ?_) h
cases mem_cons_cases hw with
| inl q => rw [q]; exact zleB_zle _ _ e
| inr h2 => exact zle_trans _ _ _ (h.1 w h2) (zleB_zle _ _ e)
| false =>
rw [ins_false key u v l e]
refine And.intro (fun w hw => ?_) (ins_sorted key u l h.2)
cases mem_ins key u w l hw with
| inl q => rw [q]; exact zle_of_not _ _ e
| inr h2 => exact h.1 w h2
theorem isort_sorted {α : Type} (key : α -> Nat × Nat) : forall l : List α, Desc key (isort key l)
| [] => True.intro
| u :: l => ins_sorted key u (isort key l) (isort_sorted key l)
/-! The threshold prefix. -/
def twSel {α : Type} (b : Bool) (a : α) (rest : List α) : List α :=
match b with
| true => a :: rest
| false => []
def dwSel {α : Type} (b : Bool) (a : α) (l rest : List α) : List α :=
match b with
| true => rest
| false => a :: l
/-- The longest prefix whose elements satisfy g. -/
def tw {α : Type} (g : α -> Bool) : List α -> List α
| [] => []
| a :: l => twSel (g a) a (tw g l)
/-- The rest after that prefix. -/
def dw {α : Type} (g : α -> Bool) : List α -> List α
| [] => []
| a :: l => dwSel (g a) a l (dw g l)
theorem tw_dw {α : Type} (g : α -> Bool) : forall l : List α, tw g l ++ dw g l = l
| [] => rfl
| a :: l => by
cases e : g a with
| true =>
have t1 : tw g (a :: l) = a :: tw g l := congrArg (fun b => twSel b a (tw g l)) e
have t2 : dw g (a :: l) = dw g l := congrArg (fun b => dwSel b a l (dw g l)) e
rw [t1, t2]
exact congrArg (a :: ·) (tw_dw g l)
| false =>
have t1 : tw g (a :: l) = [] := congrArg (fun b => twSel b a (tw g l)) e
have t2 : dw g (a :: l) = a :: l := congrArg (fun b => dwSel b a l (dw g l)) e
rw [t1, t2]
rfl
theorem mem_tw {α : Type} (g : α -> Bool) (u : α) : forall l : List α, u ∈ tw g l -> u ∈ l /\ g u = true
| [], h => nomatch h
| a :: l, h => by
cases e : g a with
| true =>
have t1 : tw g (a :: l) = a :: tw g l := congrArg (fun b => twSel b a (tw g l)) e
rw [t1] at h
cases mem_cons_cases h with
| inl q => rw [q]; exact And.intro (List.Mem.head l) e
| inr h2 => exact And.intro (List.Mem.tail a (mem_tw g u l h2).1) (mem_tw g u l h2).2
| false =>
have t1 : tw g (a :: l) = [] := congrArg (fun b => twSel b a (tw g l)) e
rw [t1] at h
exact nomatch h
/-- In a decreasing list every element with key >= a lies in the prefix of keys >= a. -/
theorem tw_complete {α : Type} (key : α -> Nat × Nat) (a : Nat × Nat) : forall l : List α, Desc key l ->
forall u, u ∈ l -> zle a (key u) -> u ∈ tw (fun w => zleB a (key w)) l
| [], _, _, h, _ => nomatch h
| v :: l, hs, u, hu, ha => by
cases e : zleB a (key v) with
| true =>
have t1 : tw (fun w => zleB a (key w)) (v :: l) = v :: tw (fun w => zleB a (key w)) l :=
congrArg (fun b => twSel b v (tw (fun w => zleB a (key w)) l)) e
rw [t1]
cases mem_cons_cases hu with
| inl q => rw [q]; exact List.Mem.head _
| inr h2 => exact List.Mem.tail v (tw_complete key a l hs.2 u h2 ha)
| false =>
have hv : zle a (key v) := by
cases mem_cons_cases hu with
| inl q => rw [<- q]; exact ha
| inr h2 => exact zle_trans _ _ _ ha (hs.1 u h2)
rw [zle_zleB a (key v) hv] at e
exact Bool.noConfusion e
end Found.Pnp118
#print axioms Found.Pnp118.ble_le
#print axioms Found.Pnp118.le_ble
#print axioms Found.Pnp118.zleB_zle
#print axioms Found.Pnp118.zle_zleB
#print axioms Found.Pnp118.zle_total
#print axioms Found.Pnp118.zle_of_not
#print axioms Found.Pnp118.ins_true
#print axioms Found.Pnp118.ins_false
#print axioms Found.Pnp118.mem_ins
#print axioms Found.Pnp118.mem_ins_of
#print axioms Found.Pnp118.mem_isort
#print axioms Found.Pnp118.mem_isort_of
#print axioms Found.Pnp118.ins_sorted
#print axioms Found.Pnp118.isort_sorted
#print axioms Found.Pnp118.tw_dw
#print axioms Found.Pnp118.mem_tw
#print axioms Found.Pnp118.tw_complete
#print axioms: 17 theorems, no axioms.
For a gate \(f=\varphi (g,h)\) the threshold DNF \(D_a\) and CNF \(C_a\) are prefixes of sorted pair lists; their switched approximators stay on the right side of the truth unless the input meets a correction set.
found/Pnp119.lean
/-
T330 piece 119. found/Pnp119 -- one real gate: the threshold approximators and one step of
the error invariants (Wigderson's argument in Jukna, Boolean Function Complexity, Section
9.6, with strict thresholds; proof complexity ladder, step 2).
A gate f = phi(g, h) with phi monotone. For its inputs g, h we are given value lists Vg, Vh
and, for every integer threshold b, a CNF Lg b and a DNF Rg b (likewise for h). The pairs
(b, c) of Vg x Vh are sorted by phi(b, c), decreasing (Md) and increasing (Ma). For a
threshold a:
D_a = OR over pairs with phi(b,c) >= a of Rg b AND Rh c (a prefix of Md),
C_a = AND over pairs with phi(b,c) <= a - 1 of Lg (b+1) OR Lh (c+1) (a prefix of Ma),
Lf a = leaves of the transversal tree of D_a (a CNF), Rf a = leaves of that of C_a (a DNF).
The correction sets KL, KR are the cuts of the trees of the whole master lists: by Pnp117
they contain the cuts for every threshold. For an input x with gx in Vg, hx in Vh:
stepA: if the approximators of g and h are never above the truth at x and x misses no set
of KL entirely, then neither Lf a nor Rf a is above the truth at x;
stepB: if they are never below the truth at x and x covers no set of KR, then neither
Lf a nor Rf a is below the truth at x.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp118
namespace Found.Pnp119
open Found.Pnp115 (anyL anyL_true mem_cons_cases and_true_l and_true_r)
open Found.Pnp116 (allL tr anyL_ex mem_app mem_app_l mem_app_r)
open Found.Pnp117 (allLL allLL_mem allLL_of allLL_false evDNF evCNF tr_prefix dnf_le_cnf cnf_cut
dnf_le_cnf' dnf_cut)
open Found.Pnp118 (zleB zleB_zle zle_zleB le_ble isort isort_sorted mem_isort_of tw dw tw_dw mem_tw tw_complete)
open Found.Pnp105 (zle zle_trans zle_refl)
open Found.Pnp113 (Mono2)
/-! Integers as pairs. -/
def succZ (b : Nat × Nat) : Nat × Nat := (b.1 + 1, b.2)
def predZ (a : Nat × Nat) : Nat × Nat := (a.1, a.2 + 1)
def negZ (v : Nat × Nat) : Nat × Nat := (v.2, v.1)
theorem strict (a v : Nat × Nat) (h : zleB a v = false) : zle v (predZ a) := by
have h' : Nat.ble (a.1 + v.2) (v.1 + a.2) = false := h
cases Nat.lt_or_ge (v.1 + a.2) (a.1 + v.2) with
| inl l => exact l
| inr g => rw [le_ble _ _ g] at h'; exact Bool.noConfusion h'
theorem pred_lt (a v : Nat × Nat) (h1 : zle v (predZ a)) (h2 : zle a v) : False :=
Nat.not_succ_le_self _ (Nat.le_trans h1 h2)
theorem succ_self (v : Nat × Nat) (h : zle (succZ v) v) : False := by
have h' : v.1 + 1 + v.2 <= v.1 + v.2 := h
rw [Nat.add_right_comm] at h'
exact Nat.not_succ_le_self _ h'
theorem not_succ (b v : Nat × Nat) (h : zleB (succZ b) v = false) : zle v b := by
have t : v.1 + (b.2 + 1) <= b.1 + 1 + v.2 := strict (succZ b) v h
rw [Nat.add_right_comm] at t
exact Nat.le_of_succ_le_succ t
theorem negZ_le (v w : Nat × Nat) (h : zle v w) : zle (negZ w) (negZ v) := by
show w.2 + v.1 <= v.2 + w.1
rw [Nat.add_comm w.2, Nat.add_comm v.2]
exact h
theorem negZ_le' (v w : Nat × Nat) (h : zle (negZ w) (negZ v)) : zle v w := by
have h' : w.2 + v.1 <= v.2 + w.1 := h
rw [Nat.add_comm w.2, Nat.add_comm v.2] at h'
exact h'
/-! Concatenated maps and joins. -/
def cm {α β : Type} (f : α -> List β) : List α -> List β
| [] => []
| u :: l => f u ++ cm f l
theorem app_assoc {α : Type} : forall (A B C : List α), (A ++ B) ++ C = A ++ (B ++ C)
| [], _, _ => rfl
| a :: A, B, C => congrArg (a :: ·) (app_assoc A B C)
theorem cm_app {α β : Type} (f : α -> List β) (B : List α) : forall A : List α, cm f (A ++ B) = cm f A ++ cm f B
| [] => rfl
| u :: A => by
show f u ++ cm f (A ++ B) = (f u ++ cm f A) ++ cm f B
rw [cm_app f B A, app_assoc]
theorem mem_cm {α β : Type} (f : α -> List β) (y : β) : forall l : List α, y ∈ cm f l -> Exists fun u => u ∈ l /\ y ∈ f u
| [], h => nomatch h
| u :: l, h => by
cases mem_app y (f u) (cm f l) h with
| inl a => exact Exists.intro u (And.intro (List.Mem.head l) a)
| inr b =>
cases mem_cm f y l b with
| intro w hw => exact Exists.intro w (And.intro (List.Mem.tail u hw.1) hw.2)
theorem mem_cm_of {α β : Type} (f : α -> List β) (y : β) (u : α) (hy : y ∈ f u) : forall l : List α, u ∈ l -> y ∈ cm f l
| [], h => nomatch h
| w :: l, h => by
cases mem_cons_cases h with
| inl q => rw [<- q]; exact mem_app_l y _ _ hy
| inr h2 => exact mem_app_r y _ _ (mem_cm_of f y u hy l h2)
def prodL (A B : List (Nat × Nat)) : List ((Nat × Nat) × (Nat × Nat)) :=
cm (fun b => cm (fun c => [(b, c)]) B) A
theorem mem_prodL (A B : List (Nat × Nat)) (b c : Nat × Nat) (hb : b ∈ A) (hc : c ∈ B) : (b, c) ∈ prodL A B :=
mem_cm_of _ _ b (mem_cm_of _ _ c (List.Mem.head []) B hc) A hb
/-- All unions a ++ b with a from A and b from B (AND of DNFs, OR of CNFs). -/
def joinL (A B : List (List Nat)) : List (List Nat) := cm (fun a => cm (fun b => [a ++ b]) B) A
theorem mem_joinL (A B : List (List Nat)) (m : List Nat) (h : m ∈ joinL A B) :
Exists fun a => a ∈ A /\ Exists fun b => b ∈ B /\ m = a ++ b := by
cases mem_cm _ m A h with
| intro a ha =>
cases mem_cm _ m B ha.2 with
| intro b hb =>
cases mem_cons_cases hb.2 with
| inl q => exact Exists.intro a (And.intro ha.1 (Exists.intro b (And.intro hb.1 q)))
| inr h2 => exact nomatch h2
theorem mem_joinL_of (A B : List (List Nat)) (a b : List Nat) (ha : a ∈ A) (hb : b ∈ B) : a ++ b ∈ joinL A B :=
mem_cm_of _ _ a (mem_cm_of _ _ b (List.Mem.head []) B hb) A ha
theorem allL_app_l (x : Nat -> Bool) : forall (a b : List Nat), allL x (a ++ b) = true -> allL x a = true
| [], _, _ => rfl
| v :: a, b, h => by
show (x v && allL x a) = true
rw [and_true_l h, allL_app_l x a b (and_true_r h)]
try rfl
theorem allL_app_r (x : Nat -> Bool) : forall (a b : List Nat), allL x (a ++ b) = true -> allL x b = true
| [], _, h => h
| _ :: a, b, h => allL_app_r x a b (and_true_r h)
theorem allL_app_of (x : Nat -> Bool) : forall (a b : List Nat), allL x a = true -> allL x b = true -> allL x (a ++ b) = true
| [], _, _, hb => hb
| v :: a, b, ha, hb => by
show (x v && allL x (a ++ b)) = true
rw [and_true_l ha, allL_app_of x a b (and_true_r ha) hb]
try rfl
theorem anyL_app_l (x : Nat -> Bool) (a b : List Nat) (h : anyL x a = true) : anyL x (a ++ b) = true := by
cases anyL_ex x a h with
| intro v hv => exact anyL_true x v hv.2 _ (mem_app_l v b a hv.1)
theorem anyL_app_r (x : Nat -> Bool) (a b : List Nat) (h : anyL x b = true) : anyL x (a ++ b) = true := by
cases anyL_ex x b h with
| intro v hv => exact anyL_true x v hv.2 _ (mem_app_r v b a hv.1)
theorem anyL_app_false (x : Nat -> Bool) (a b : List Nat) (ha : anyL x a = false) (hb : anyL x b = false) :
anyL x (a ++ b) = false := by
cases e : anyL x (a ++ b) with
| false => rfl
| true =>
cases anyL_ex x _ e with
| intro v hv =>
cases mem_app v a b hv.1 with
| inl l => rw [anyL_true x v hv.2 a l] at ha; exact Bool.noConfusion ha
| inr r => rw [anyL_true x v hv.2 b r] at hb; exact Bool.noConfusion hb
/-! Evaluation of joins and concatenated maps. -/
theorem dnf_join_ex (x : Nat -> Bool) (A B : List (List Nat)) (h : evDNF x (joinL A B) = true) :
evDNF x A = true /\ evDNF x B = true := by
cases anyL_ex _ _ h with
| intro m hm =>
cases mem_joinL A B m hm.1 with
| intro a ha =>
cases ha.2 with
| intro b hb =>
have e := hb.2
have hm2 := hm.2
rw [e] at hm2
exact And.intro (anyL_true (allL x) a (allL_app_l x a b hm2) A ha.1)
(anyL_true (allL x) b (allL_app_r x a b hm2) B hb.1)
theorem dnf_join_of (x : Nat -> Bool) (A B : List (List Nat)) (hA : evDNF x A = true) (hB : evDNF x B = true) :
evDNF x (joinL A B) = true := by
cases anyL_ex _ _ hA with
| intro a ha =>
cases anyL_ex _ _ hB with
| intro b hb => exact anyL_true (allL x) (a ++ b) (allL_app_of x a b ha.2 hb.2) _ (mem_joinL_of A B a b ha.1 hb.1)
theorem cnf_join_of (x : Nat -> Bool) (A B : List (List Nat)) (h : evCNF x A = true \/ evCNF x B = true) :
evCNF x (joinL A B) = true := by
refine allLL_of _ _ (fun m hm => ?_)
cases mem_joinL A B m hm with
| intro a ha =>
cases ha.2 with
| intro b hb =>
rw [hb.2]
cases h with
| inl l => exact anyL_app_l x a b (allLL_mem _ A a l ha.1)
| inr r => exact anyL_app_r x a b (allLL_mem _ B b r hb.1)
theorem cnf_join_ex (x : Nat -> Bool) (A B : List (List Nat)) (h : evCNF x (joinL A B) = true) :
evCNF x A = true \/ evCNF x B = true := by
cases e1 : evCNF x A with
| true => exact Or.inl rfl
| false =>
cases e2 : evCNF x B with
| true => exact Or.inr rfl
| false =>
cases allLL_false _ A e1 with
| intro a ha =>
cases allLL_false _ B e2 with
| intro b hb =>
have t := allLL_mem _ _ (a ++ b) h (mem_joinL_of A B a b ha.1 hb.1)
rw [anyL_app_false x a b ha.2 hb.2] at t
exact Bool.noConfusion t
theorem dnf_cm_ex {α : Type} (x : Nat -> Bool) (f : α -> List (List Nat)) (P : List α) (h : evDNF x (cm f P) = true) :
Exists fun u => u ∈ P /\ evDNF x (f u) = true := by
cases anyL_ex _ _ h with
| intro m hm =>
cases mem_cm f m P hm.1 with
| intro u hu => exact Exists.intro u (And.intro hu.1 (anyL_true (allL x) m hm.2 _ hu.2))
theorem dnf_cm_of {α : Type} (x : Nat -> Bool) (f : α -> List (List Nat)) (P : List α) (u : α) (hu : u ∈ P)
(h : evDNF x (f u) = true) : evDNF x (cm f P) = true := by
cases anyL_ex _ _ h with
| intro m hm => exact anyL_true (allL x) m hm.2 _ (mem_cm_of f m u hm.1 P hu)
theorem cnf_cm_all {α : Type} (x : Nat -> Bool) (f : α -> List (List Nat)) (P : List α) (h : evCNF x (cm f P) = true)
(u : α) (hu : u ∈ P) : evCNF x (f u) = true :=
allLL_of _ _ (fun q hq => allLL_mem _ _ q h (mem_cm_of f q u hq P hu))
theorem cnf_cm_of {α : Type} (x : Nat -> Bool) (f : α -> List (List Nat)) (P : List α)
(h : forall u, u ∈ P -> evCNF x (f u) = true) : evCNF x (cm f P) = true := by
refine allLL_of _ _ (fun q hq => ?_)
cases mem_cm f q P hq with
| intro u hu => exact allLL_mem _ _ q (h u hu.1) hu.2
theorem tr_prefix_cm {α : Type} (f : α -> List (List Nat)) (A B : List α) (d : Nat) (p T : List Nat)
(h : T ∈ (tr (cm f A) d p).2) : T ∈ (tr (cm f (A ++ B)) d p).2 := by
rw [cm_app]
exact tr_prefix _ _ d p T h
/-! One gate. -/
structure GD where
phi : Nat × Nat -> Nat × Nat -> Nat × Nat
Vg : List (Nat × Nat)
Vh : List (Nat × Nat)
Lg : Nat × Nat -> List (List Nat)
Rg : Nat × Nat -> List (List Nat)
Lh : Nat × Nat -> List (List Nat)
Rh : Nat × Nat -> List (List Nat)
def key1 (G : GD) (u : (Nat × Nat) × (Nat × Nat)) : Nat × Nat := G.phi u.1 u.2
def key2 (G : GD) (u : (Nat × Nat) × (Nat × Nat)) : Nat × Nat := negZ (G.phi u.1 u.2)
def Md (G : GD) := isort (key1 G) (prodL G.Vg G.Vh)
def Ma (G : GD) := isort (key2 G) (prodL G.Vg G.Vh)
def gD (a : Nat × Nat) (G : GD) (u : (Nat × Nat) × (Nat × Nat)) : Bool := zleB a (key1 G u)
def gC (a : Nat × Nat) (G : GD) (u : (Nat × Nat) × (Nat × Nat)) : Bool := zleB (negZ (predZ a)) (key2 G u)
def fD (G : GD) (u : (Nat × Nat) × (Nat × Nat)) : List (List Nat) := joinL (G.Rg u.1) (G.Rh u.2)
def fC (G : GD) (u : (Nat × Nat) × (Nat × Nat)) : List (List Nat) := joinL (G.Lg (succZ u.1)) (G.Lh (succZ u.2))
/-- The DNF of the threshold a, its CNF approximator, and the correction cuts. -/
def Da (G : GD) (a : Nat × Nat) : List (List Nat) := cm (fD G) (tw (gD a G) (Md G))
def Ca (G : GD) (a : Nat × Nat) : List (List Nat) := cm (fC G) (tw (gC a G) (Ma G))
def Lf (s : Nat) (G : GD) (a : Nat × Nat) : List (List Nat) := (tr (Da G a) s []).1
def Rf (r : Nat) (G : GD) (a : Nat × Nat) : List (List Nat) := (tr (Ca G a) r []).1
def KL (s : Nat) (G : GD) : List (List Nat) := (tr (cm (fD G) (Md G)) s []).2
def KR (r : Nat) (G : GD) : List (List Nat) := (tr (cm (fC G) (Ma G)) r []).2
theorem KL_sub (s : Nat) (G : GD) (a : Nat × Nat) (T : List Nat) (h : T ∈ (tr (Da G a) s []).2) : T ∈ KL s G := by
have h' := tr_prefix_cm (fD G) (tw (gD a G) (Md G)) (dw (gD a G) (Md G)) s [] T h
rw [tw_dw] at h'
exact h'
theorem KR_sub (r : Nat) (G : GD) (a : Nat × Nat) (T : List Nat) (h : T ∈ (tr (Ca G a) r []).2) : T ∈ KR r G := by
have h' := tr_prefix_cm (fC G) (tw (gC a G) (Ma G)) (dw (gC a G) (Ma G)) r [] T h
rw [tw_dw] at h'
exact h'
theorem pair_in_Md (G : GD) (gx hx : Nat × Nat) (hg : gx ∈ G.Vg) (hh : hx ∈ G.Vh) : (gx, hx) ∈ Md G :=
mem_isort_of _ _ _ (mem_prodL _ _ gx hx hg hh)
theorem pair_in_Ma (G : GD) (gx hx : Nat × Nat) (hg : gx ∈ G.Vg) (hh : hx ∈ G.Vh) : (gx, hx) ∈ Ma G :=
mem_isort_of _ _ _ (mem_prodL _ _ gx hx hg hh)
/-- Never above the truth. -/
theorem stepA (s r : Nat) (G : GD) (hphi : Mono2 G.phi) (x : Nat -> Bool) (gx hx : Nat × Nat)
(hg : gx ∈ G.Vg) (hh : hx ∈ G.Vh)
(IgL : forall b, evCNF x (G.Lg b) = true -> zle b gx) (IgR : forall b, evDNF x (G.Rg b) = true -> zle b gx)
(IhL : forall c, evCNF x (G.Lh c) = true -> zle c hx) (IhR : forall c, evDNF x (G.Rh c) = true -> zle c hx)
(hK : forall T, T ∈ KL s G -> anyL x T = true) (a : Nat × Nat) :
(evCNF x (Lf s G a) = true -> zle a (G.phi gx hx)) /\ (evDNF x (Rf r G a) = true -> zle a (G.phi gx hx)) := by
refine And.intro (fun hL => ?_) (fun hR => ?_)
· have hD : evDNF x (Da G a) = true := by
cases e : evDNF x (Da G a) with
| true => rfl
| false =>
cases cnf_cut (Da G a) s x hL e with
| intro T hT =>
have t := hK T (KL_sub s G a T hT.1)
rw [hT.2] at t
exact Bool.noConfusion t
cases dnf_cm_ex x (fD G) _ hD with
| intro u hu =>
have j := dnf_join_ex x _ _ hu.2
have hb := IgR u.1 j.1
have hc := IhR u.2 j.2
have ha : zle a (G.phi u.1 u.2) := zleB_zle _ _ (mem_tw (gD a G) u (Md G) hu.1).2
exact zle_trans _ _ _ ha (hphi _ _ _ _ hb hc)
· have hC : evCNF x (Ca G a) = true := dnf_le_cnf' _ r x hR
cases e : zleB a (G.phi gx hx) with
| true => exact zleB_zle _ _ e
| false =>
have hlt : zle (G.phi gx hx) (predZ a) := strict a _ e
have hu : (gx, hx) ∈ tw (gC a G) (Ma G) :=
tw_complete (key2 G) (negZ (predZ a)) (Ma G) (isort_sorted _ _) (gx, hx) (pair_in_Ma G gx hx hg hh)
(negZ_le _ _ hlt)
cases cnf_join_ex x _ _ (cnf_cm_all x (fC G) _ hC (gx, hx) hu) with
| inl l => exact absurd (IgL _ l) (succ_self gx)
| inr r' => exact absurd (IhL _ r') (succ_self hx)
/-- Never below the truth. -/
theorem stepB (s r : Nat) (G : GD) (hphi : Mono2 G.phi) (x : Nat -> Bool) (gx hx : Nat × Nat)
(hg : gx ∈ G.Vg) (hh : hx ∈ G.Vh)
(IgB : forall b, zle b gx -> evCNF x (G.Lg b) = true /\ evDNF x (G.Rg b) = true)
(IhB : forall c, zle c hx -> evCNF x (G.Lh c) = true /\ evDNF x (G.Rh c) = true)
(hK : forall T, T ∈ KR r G -> allL x T = false) (a : Nat × Nat) (ha : zle a (G.phi gx hx)) :
evCNF x (Lf s G a) = true /\ evDNF x (Rf r G a) = true := by
refine And.intro ?_ ?_
· have hu : (gx, hx) ∈ tw (gD a G) (Md G) :=
tw_complete (key1 G) a (Md G) (isort_sorted _ _) (gx, hx) (pair_in_Md G gx hx hg hh) ha
have hD : evDNF x (Da G a) = true :=
dnf_cm_of x (fD G) _ (gx, hx) hu (dnf_join_of x _ _ (IgB gx (zle_refl gx)).2 (IhB hx (zle_refl hx)).2)
exact dnf_le_cnf _ s x hD
· have hC : evCNF x (Ca G a) = true := by
refine cnf_cm_of x (fC G) _ (fun u hu => ?_)
have hlt : zle (G.phi u.1 u.2) (predZ a) := negZ_le' _ _ (zleB_zle _ _ (mem_tw (gC a G) u (Ma G) hu).2)
cases e1 : zleB (succZ u.1) gx with
| true => exact cnf_join_of x _ _ (Or.inl (IgB _ (zleB_zle _ _ e1)).1)
| false =>
cases e2 : zleB (succZ u.2) hx with
| true => exact cnf_join_of x _ _ (Or.inr (IhB _ (zleB_zle _ _ e2)).1)
| false =>
have m := hphi _ _ _ _ (not_succ u.1 gx e1) (not_succ u.2 hx e2)
exact absurd ha (fun ha' => pred_lt a _ (zle_trans _ _ _ m hlt) ha')
cases e : evDNF x (Rf r G a) with
| true => rfl
| false =>
cases dnf_cut (Ca G a) r x hC e with
| intro T hT =>
have t := hK T (KR_sub r G a T hT.1)
rw [hT.2] at t
exact Bool.noConfusion t
end Found.Pnp119
#print axioms Found.Pnp119.strict
#print axioms Found.Pnp119.pred_lt
#print axioms Found.Pnp119.succ_self
#print axioms Found.Pnp119.not_succ
#print axioms Found.Pnp119.negZ_le
#print axioms Found.Pnp119.negZ_le'
#print axioms Found.Pnp119.app_assoc
#print axioms Found.Pnp119.cm_app
#print axioms Found.Pnp119.mem_cm
#print axioms Found.Pnp119.mem_cm_of
#print axioms Found.Pnp119.mem_prodL
#print axioms Found.Pnp119.mem_joinL
#print axioms Found.Pnp119.mem_joinL_of
#print axioms Found.Pnp119.allL_app_l
#print axioms Found.Pnp119.allL_app_r
#print axioms Found.Pnp119.allL_app_of
#print axioms Found.Pnp119.anyL_app_l
#print axioms Found.Pnp119.anyL_app_r
#print axioms Found.Pnp119.anyL_app_false
#print axioms Found.Pnp119.dnf_join_ex
#print axioms Found.Pnp119.dnf_join_of
#print axioms Found.Pnp119.cnf_join_of
#print axioms Found.Pnp119.cnf_join_ex
#print axioms Found.Pnp119.dnf_cm_ex
#print axioms Found.Pnp119.dnf_cm_of
#print axioms Found.Pnp119.cnf_cm_all
#print axioms Found.Pnp119.cnf_cm_of
#print axioms Found.Pnp119.tr_prefix_cm
#print axioms Found.Pnp119.KL_sub
#print axioms Found.Pnp119.KR_sub
#print axioms Found.Pnp119.pair_in_Md
#print axioms Found.Pnp119.pair_in_Ma
#print axioms Found.Pnp119.stepA
#print axioms Found.Pnp119.stepB
#print axioms: 34 theorems, no axioms.
Every gate gets threshold approximators; on a test input that meets no correction set they agree with the truth, so at the output the CNF approximator is false on negative and true on positive test inputs.
found/Pnp120.lean
/-
T330 piece 120. found/Pnp120 -- approximators for a whole monotone real circuit and the two
error invariants (proof complexity ladder, step 2: Wigderson's argument).
For a straight-line monotone real circuit (Pnp113), a list X of test inputs and parameters
s, r, apx builds for every gate a family of threshold approximators: for an input gate x_i
and a constant gate the exact CNF/DNF of the threshold function (no correction sets); for a
gate phi(g, h) the approximators of Pnp119 over the values of g and h on X. allKL, allKR
collect the correction sets of all gates. For a test input x in X:
apx_goodA: if x misses no set of allKL entirely, no approximator is above the truth;
apx_goodB: if x covers no set of allKR, no approximator is below the truth;
out_neg, out_pos: at the output gate and threshold 0, the CNF approximator is false on
such negative test inputs and true on such positive ones.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp119
namespace Found.Pnp120
open Found.Pnp65 (bv)
open Found.Pnp115 (anyL)
open Found.Pnp116 (allL mem_app_l mem_app_r)
open Found.Pnp117 (evDNF evCNF)
open Found.Pnp118 (zleB zleB_zle zle_zleB ble_le le_ble)
open Found.Pnp119 (GD Lf Rf KL KR stepA stepB)
open Found.Pnp105 (zle zle_trans zle_refl)
open Found.Pnp113 (Gate nthV gval evalR outV)
open Found.Pnp109 (out)
structure Apx where
L : Nat × Nat -> List (List Nat)
R : Nat × Nat -> List (List Nat)
KL : List (List Nat)
KR : List (List Nat)
def selF (b : Bool) (u w : List (List Nat)) : List (List Nat) :=
match b with
| true => u
| false => w
/-- Exact approximators of a constant gate. -/
def cstA (v : Nat × Nat) : Apx :=
Apx.mk (fun a => selF (zleB a v) [] [[]]) (fun a => selF (zleB a v) [[]] []) [] []
/-- Exact approximators of an input gate x_i (value (x_i, 0)). -/
def inpA (i : Nat) : Apx :=
Apx.mk (fun a => selF (zleB a (0, 0)) [] (selF (zleB a (1, 0)) [[i]] [[]]))
(fun a => selF (zleB a (0, 0)) [[]] (selF (zleB a (1, 0)) [[i]] [])) [] []
def nthA : List Apx -> Nat -> Apx
| [], _ => cstA (0, 0)
| a :: _, 0 => a
| _ :: l, j + 1 => nthA l j
def mapL {α β : Type} (f : α -> β) : List α -> List β
| [] => []
| a :: l => f a :: mapL f l
theorem mem_mapL {α β : Type} (f : α -> β) (a : α) : forall l : List α, a ∈ l -> f a ∈ mapL f l
| [], h => nomatch h
| b :: l, h => by
cases Found.Pnp115.mem_cons_cases h with
| inl q => rw [q]; exact List.Mem.head _
| inr h2 => exact List.Mem.tail (f b) (mem_mapL f a l h2)
/-- The values of gate j of gs on the test inputs. -/
def vals (X : List (Nat -> Bool)) (gs : List Gate) (j : Nat) : List (Nat × Nat) :=
mapL (fun y => nthV (evalR y gs) j) X
def gdOf (X : List (Nat -> Bool)) (gs : List Gate) (As : List Apx) (f : Nat × Nat -> Nat × Nat -> Nat × Nat)
(j k : Nat) : GD :=
GD.mk f (vals X gs j) (vals X gs k) (nthA As j).L (nthA As j).R (nthA As k).L (nthA As k).R
def apxG (s r : Nat) (X : List (Nat -> Bool)) (gs : List Gate) (As : List Apx) : Gate -> Apx
| .inp i => inpA i
| .cst v => cstA v
| .op f _ j k => Apx.mk (Lf s (gdOf X gs As f j k)) (Rf r (gdOf X gs As f j k))
(KL s (gdOf X gs As f j k)) (KR r (gdOf X gs As f j k))
def apx (s r : Nat) (X : List (Nat -> Bool)) : List Gate -> List Apx
| [] => []
| g :: gs => apxG s r X gs (apx s r X gs) g :: apx s r X gs
def allKL : List Apx -> List (List Nat)
| [] => []
| A :: l => A.KL ++ allKL l
def allKR : List Apx -> List (List Nat)
| [] => []
| A :: l => A.KR ++ allKR l
def GoodA (x : Nat -> Bool) (A : Apx) (v : Nat × Nat) : Prop :=
forall b, (evCNF x (A.L b) = true -> zle b v) /\ (evDNF x (A.R b) = true -> zle b v)
def GoodB (x : Nat -> Bool) (A : Apx) (v : Nat × Nat) : Prop :=
forall b, zle b v -> evCNF x (A.L b) = true /\ evDNF x (A.R b) = true
/-! Exact gates. -/
theorem cst_goodA (x : Nat -> Bool) (v : Nat × Nat) : GoodA x (cstA v) v := by
intro b
show (evCNF x (selF (zleB b v) [] [[]]) = true -> zle b v) /\ (evDNF x (selF (zleB b v) [[]] []) = true -> zle b v)
cases e : zleB b v with
| true => exact And.intro (fun _ => zleB_zle _ _ e) (fun _ => zleB_zle _ _ e)
| false => exact And.intro (fun h => Bool.noConfusion h) (fun h => Bool.noConfusion h)
theorem cst_goodB (x : Nat -> Bool) (v : Nat × Nat) : GoodB x (cstA v) v := by
intro b hb
show evCNF x (selF (zleB b v) [] [[]]) = true /\ evDNF x (selF (zleB b v) [[]] []) = true
rw [zle_zleB b v hb]
exact And.intro rfl rfl
theorem zero_le_inp (c : Bool) : zle (0, 0) (bv c, 0) := Nat.zero_le _
theorem inp_le_one (c : Bool) : zle (bv c, 0) (1, 0) := by
show bv c + 0 <= 1 + 0
exact Found.Pnp107.bv_le1 c
theorem inp_goodA (x : Nat -> Bool) (i : Nat) : GoodA x (inpA i) (bv (x i), 0) := by
intro b
show (evCNF x (selF (zleB b (0, 0)) [] (selF (zleB b (1, 0)) [[i]] [[]])) = true -> zle b (bv (x i), 0)) /\
(evDNF x (selF (zleB b (0, 0)) [[]] (selF (zleB b (1, 0)) [[i]] [])) = true -> zle b (bv (x i), 0))
cases e0 : zleB b (0, 0) with
| true =>
have t := zle_trans _ _ _ (zleB_zle _ _ e0) (zero_le_inp (x i))
exact And.intro (fun _ => t) (fun _ => t)
| false =>
cases e1 : zleB b (1, 0) with
| false => exact And.intro (fun h => Bool.noConfusion h) (fun h => Bool.noConfusion h)
| true =>
cases hx : x i with
| true => exact And.intro (fun _ => zleB_zle _ _ e1) (fun _ => zleB_zle _ _ e1)
| false =>
refine And.intro (fun h => ?_) (fun h => ?_)
· have h' : ((x i || false) && true) = true := h
rw [hx] at h'
exact Bool.noConfusion h'
· have h' : ((x i && true) || false) = true := h
rw [hx] at h'
exact Bool.noConfusion h'
theorem inp_goodB (x : Nat -> Bool) (i : Nat) : GoodB x (inpA i) (bv (x i), 0) := by
intro b hb
show evCNF x (selF (zleB b (0, 0)) [] (selF (zleB b (1, 0)) [[i]] [[]])) = true /\
evDNF x (selF (zleB b (0, 0)) [[]] (selF (zleB b (1, 0)) [[i]] [])) = true
cases e0 : zleB b (0, 0) with
| true => exact And.intro rfl rfl
| false =>
rw [zle_zleB b (1, 0) (zle_trans _ _ _ hb (inp_le_one (x i)))]
cases hx : x i with
| false =>
rw [hx] at hb
rw [zle_zleB b (0, 0) hb] at e0
exact Bool.noConfusion e0
| true =>
refine And.intro ?_ ?_
· show ((x i || false) && true) = true
rw [hx]; rfl
· show ((x i && true) || false) = true
rw [hx]; rfl
theorem nthA_nil_A (x : Nat -> Bool) (j : Nat) : GoodA x (nthA [] j) (nthV [] j) := cst_goodA x (0, 0)
theorem nthA_nil_B (x : Nat -> Bool) (j : Nat) : GoodB x (nthA [] j) (nthV [] j) := cst_goodB x (0, 0)
/-! The invariants. -/
theorem apx_goodA (s r : Nat) (X : List (Nat -> Bool)) (x : Nat -> Bool) (hx : x ∈ X) : forall C : List Gate,
(forall T, T ∈ allKL (apx s r X C) -> anyL x T = true) ->
forall j, GoodA x (nthA (apx s r X C) j) (nthV (evalR x C) j)
| [], _, j => nthA_nil_A x j
| g :: gs, hK, j => by
have hK' : forall T, T ∈ allKL (apx s r X gs) -> anyL x T = true := fun T hT => hK T (mem_app_r T _ _ hT)
have ih := apx_goodA s r X x hx gs hK'
cases j with
| succ j => exact ih j
| zero =>
show GoodA x (apxG s r X gs (apx s r X gs) g) (gval x (evalR x gs) g)
have hK0 : forall T, T ∈ (apxG s r X gs (apx s r X gs) g).KL -> anyL x T = true :=
fun T hT => hK T (mem_app_l T _ _ hT)
cases g with
| inp i => exact inp_goodA x i
| cst v => exact cst_goodA x v
| op f hf j k =>
intro b
exact stepA s r (gdOf X gs (apx s r X gs) f j k) hf x (nthV (evalR x gs) j) (nthV (evalR x gs) k)
(mem_mapL _ x X hx) (mem_mapL _ x X hx)
(fun c h => ((ih j) c).1 h) (fun c h => ((ih j) c).2 h)
(fun c h => ((ih k) c).1 h) (fun c h => ((ih k) c).2 h) hK0 b
theorem apx_goodB (s r : Nat) (X : List (Nat -> Bool)) (x : Nat -> Bool) (hx : x ∈ X) : forall C : List Gate,
(forall T, T ∈ allKR (apx s r X C) -> allL x T = false) ->
forall j, GoodB x (nthA (apx s r X C) j) (nthV (evalR x C) j)
| [], _, j => nthA_nil_B x j
| g :: gs, hK, j => by
have hK' : forall T, T ∈ allKR (apx s r X gs) -> allL x T = false := fun T hT => hK T (mem_app_r T _ _ hT)
have ih := apx_goodB s r X x hx gs hK'
cases j with
| succ j => exact ih j
| zero =>
show GoodB x (apxG s r X gs (apx s r X gs) g) (gval x (evalR x gs) g)
have hK0 : forall T, T ∈ (apxG s r X gs (apx s r X gs) g).KR -> allL x T = false :=
fun T hT => hK T (mem_app_l T _ _ hT)
cases g with
| inp i => exact inp_goodB x i
| cst v => exact cst_goodB x v
| op f hf j k =>
intro b hb
exact stepB s r (gdOf X gs (apx s r X gs) f j k) hf x (nthV (evalR x gs) j) (nthV (evalR x gs) k)
(mem_mapL _ x X hx) (mem_mapL _ x X hx) (ih j) (ih k) hK0 b hb
/-! The output gate at threshold 0. -/
theorem zle_out (E : Nat × Nat) (h : zle (0, 0) E) : out E = true := by
have h' : 0 + E.2 <= E.1 + 0 := h
rw [Nat.zero_add] at h'
exact le_ble _ _ h'
theorem out_zle (E : Nat × Nat) (h : out E = true) : zle (0, 0) E := by
show 0 + E.2 <= E.1 + 0
rw [Nat.zero_add]
exact ble_le _ _ h
/-- On a negative test input that misses no correction set entirely, the output CNF is false. -/
theorem out_neg (s r : Nat) (X : List (Nat -> Bool)) (x : Nat -> Bool) (hx : x ∈ X) (C : List Gate)
(hK : forall T, T ∈ allKL (apx s r X C) -> anyL x T = true) (ho : out (outV x C) = false) :
evCNF x ((nthA (apx s r X C) 0).L (0, 0)) = false := by
cases e : evCNF x ((nthA (apx s r X C) 0).L (0, 0)) with
| false => rfl
| true =>
have t := zle_out _ (((apx_goodA s r X x hx C hK 0) (0, 0)).1 e)
have t' : out (outV x C) = true := t
rw [ho] at t'
exact Bool.noConfusion t'
/-- On a positive test input that covers no correction set, the output CNF is true. -/
theorem out_pos (s r : Nat) (X : List (Nat -> Bool)) (x : Nat -> Bool) (hx : x ∈ X) (C : List Gate)
(hK : forall T, T ∈ allKR (apx s r X C) -> allL x T = false) (ho : out (outV x C) = true) :
evCNF x ((nthA (apx s r X C) 0).L (0, 0)) = true :=
((apx_goodB s r X x hx C hK 0) (0, 0) (out_zle _ ho)).1
end Found.Pnp120
#print axioms Found.Pnp120.mem_mapL
#print axioms Found.Pnp120.cst_goodA
#print axioms Found.Pnp120.cst_goodB
#print axioms Found.Pnp120.zero_le_inp
#print axioms Found.Pnp120.inp_le_one
#print axioms Found.Pnp120.inp_goodA
#print axioms Found.Pnp120.inp_goodB
#print axioms Found.Pnp120.nthA_nil_A
#print axioms Found.Pnp120.nthA_nil_B
#print axioms Found.Pnp120.apx_goodA
#print axioms Found.Pnp120.apx_goodB
#print axioms Found.Pnp120.zle_out
#print axioms Found.Pnp120.out_zle
#print axioms Found.Pnp120.out_neg
#print axioms Found.Pnp120.out_pos
#print axioms: 15 theorems, no axioms.
With \(s,r\ge 2\) the clauses have fewer than \(s\) variables, the monomials fewer than \(r\), and a circuit with \(t\) gates has at most \(t(2r)^s\) correction sets of \(s\) variables and \(t(2s)^r\) of \(r\) variables.
found/Pnp121.lean
/-
T330 piece 121. found/Pnp121 -- sizes of the approximators of a monotone real circuit
(proof complexity ladder, step 2).
With 2 <= s and 2 <= r: every clause of every CNF approximator has at most s - 1 variables
and every monomial of every DNF approximator at most r - 1; the correction sets of KL have
exactly s elements and those of KR exactly r; one gate has at most (2r)^s sets in KL and
(2s)^r in KR, so the whole circuit C has at most |C| (2r)^s and |C| (2s)^r. Proved:
* apx_sz: the clause and monomial bound for every gate;
* kl_len, kr_len: lengths of the correction sets;
* kl_count, kr_count: the counts for one gate;
* allKL_count, allKR_count, allKL_len, allKR_len: for the whole circuit.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp120
namespace Found.Pnp121
open Found.Pnp115 (mem_cons_cases)
open Found.Pnp116 (tr tr_fst_len tr_snd_len tr_snd_count mem_app len_app)
open Found.Pnp119 (GD Lf Rf KL KR cm mem_cm joinL mem_joinL fD fC Md Ma)
open Found.Pnp120 (Apx selF cstA inpA nthA apx apxG gdOf allKL allKR)
open Found.Pnp113 (Gate)
/-- Clause and monomial bounds of one gate's approximators. -/
def Sz (s r : Nat) (A : Apx) : Prop :=
(forall b m, m ∈ A.L b -> m.length + 1 <= s) /\ (forall b m, m ∈ A.R b -> m.length + 1 <= r)
theorem mem_selF (b : Bool) (u w : List (List Nat)) (m : List Nat) (h : m ∈ selF b u w) : m ∈ u \/ m ∈ w := by
cases b with
| true => exact Or.inl h
| false => exact Or.inr h
theorem small_one (t : Nat) (ht : 2 <= t) (i : Nat) : forall m, m ∈ ([[i]] : List (List Nat)) -> m.length + 1 <= t := by
intro m hm
cases mem_cons_cases hm with
| inl q => rw [q]; exact ht
| inr h2 => exact nomatch h2
theorem small_nil (t : Nat) (ht : 2 <= t) : forall m, m ∈ ([[]] : List (List Nat)) -> m.length + 1 <= t := by
intro m hm
cases mem_cons_cases hm with
| inl q => rw [q]; exact Nat.le_trans (Nat.le_succ 1) ht
| inr h2 => exact nomatch h2
theorem small_empty (t : Nat) : forall m, m ∈ ([] : List (List Nat)) -> m.length + 1 <= t := fun _ h => nomatch h
theorem cst_sz (s r : Nat) (hs : 2 <= s) (hr : 2 <= r) (v : Nat × Nat) : Sz s r (cstA v) := by
refine And.intro (fun b m hm => ?_) (fun b m hm => ?_)
· cases mem_selF _ _ _ m hm with
| inl l => exact small_empty s m l
| inr q => exact small_nil s hs m q
· cases mem_selF _ _ _ m hm with
| inl l => exact small_nil r hr m l
| inr q => exact small_empty r m q
theorem inp_sz (s r : Nat) (hs : 2 <= s) (hr : 2 <= r) (i : Nat) : Sz s r (inpA i) := by
refine And.intro (fun b m hm => ?_) (fun b m hm => ?_)
· cases mem_selF _ _ _ m hm with
| inl l => exact small_empty s m l
| inr q =>
cases mem_selF _ _ _ m q with
| inl l => exact small_one s hs i m l
| inr q' => exact small_nil s hs m q'
· cases mem_selF _ _ _ m hm with
| inl l => exact small_nil r hr m l
| inr q =>
cases mem_selF _ _ _ m q with
| inl l => exact small_one r hr i m l
| inr q' => exact small_empty r m q'
theorem leaf_sz (C : List (List Nat)) (d : Nat) (m : List Nat) (h : m ∈ (tr C d []).1) : m.length + 1 <= d := by
have t := tr_fst_len C d [] m h
rw [show ([] : List Nat).length + d = d from Nat.zero_add d] at t
exact t
theorem op_sz (s r : Nat) (G : GD) : Sz s r (Apx.mk (Lf s G) (Rf r G) (KL s G) (KR r G)) :=
And.intro (fun _ m hm => leaf_sz _ s m hm) (fun _ m hm => leaf_sz _ r m hm)
theorem nthA_sz (s r : Nat) (hs : 2 <= s) (hr : 2 <= r) : forall (As : List Apx), (forall A, A ∈ As -> Sz s r A) ->
forall j, Sz s r (nthA As j)
| [], _, _ => cst_sz s r hs hr (0, 0)
| A :: _, h, 0 => h A (List.Mem.head _)
| A :: As, h, j + 1 => nthA_sz s r hs hr As (fun B hB => h B (List.Mem.tail A hB)) j
theorem apx_sz (s r : Nat) (hs : 2 <= s) (hr : 2 <= r) (X : List (Nat -> Bool)) : forall (C : List Gate) (A : Apx),
A ∈ apx s r X C -> Sz s r A
| [], _, h => nomatch h
| g :: gs, A, h => by
cases mem_cons_cases h with
| inr h2 => exact apx_sz s r hs hr X gs A h2
| inl q =>
rw [q]
cases g with
| inp i => exact inp_sz s r hs hr i
| cst v => exact cst_sz s r hs hr v
| op f _ j k => exact op_sz s r _
/-! Correction sets. -/
theorem kl_len (s : Nat) (G : GD) (T : List Nat) (h : T ∈ KL s G) : T.length = s := by
have t := tr_snd_len _ s [] T h
rw [show ([] : List Nat).length + s = s from Nat.zero_add s] at t
exact t
theorem kr_len (r : Nat) (G : GD) (T : List Nat) (h : T ∈ KR r G) : T.length = r := by
have t := tr_snd_len _ r [] T h
rw [show ([] : List Nat).length + r = r from Nat.zero_add r] at t
exact t
theorem join_len (A B : List (List Nat)) (w : Nat) (hA : forall m, m ∈ A -> m.length + 1 <= w)
(hB : forall m, m ∈ B -> m.length + 1 <= w) (q : List Nat) (hq : q ∈ joinL A B) : q.length <= 2 * w := by
cases mem_joinL A B q hq with
| intro a ha =>
cases ha.2 with
| intro b hb =>
rw [hb.2, len_app, Nat.two_mul]
exact Nat.add_le_add (Nat.le_of_succ_le (hA a ha.1)) (Nat.le_of_succ_le (hB b hb.1))
theorem kl_count (s r : Nat) (G : GD) (hg : forall b m, m ∈ G.Rg b -> m.length + 1 <= r)
(hh : forall b m, m ∈ G.Rh b -> m.length + 1 <= r) : (KL s G).length <= (2 * r) ^ s := by
refine tr_snd_count _ (2 * r) (fun q hq => ?_) s []
cases mem_cm (fD G) q (Md G) hq with
| intro u hu => exact join_len _ _ r (hg u.1) (hh u.2) q hu.2
theorem kr_count (s r : Nat) (G : GD) (hg : forall b m, m ∈ G.Lg b -> m.length + 1 <= s)
(hh : forall b m, m ∈ G.Lh b -> m.length + 1 <= s) : (KR r G).length <= (2 * s) ^ r := by
refine tr_snd_count _ (2 * s) (fun q hq => ?_) r []
cases mem_cm (fC G) q (Ma G) hq with
| intro u hu => exact join_len _ _ s (hg _) (hh _) q hu.2
/-- One gate: the counts and lengths of its correction sets. -/
def KOK (s r : Nat) (A : Apx) : Prop :=
A.KL.length <= (2 * r) ^ s /\ A.KR.length <= (2 * s) ^ r /\
(forall T, T ∈ A.KL -> T.length = s) /\ (forall T, T ∈ A.KR -> T.length = r)
theorem apx_kok (s r : Nat) (hs : 2 <= s) (hr : 2 <= r) (X : List (Nat -> Bool)) : forall (C : List Gate) (A : Apx),
A ∈ apx s r X C -> KOK s r A
| [], _, h => nomatch h
| g :: gs, A, h => by
cases mem_cons_cases h with
| inr h2 => exact apx_kok s r hs hr X gs A h2
| inl q =>
rw [q]
cases g with
| inp i => exact And.intro (Nat.zero_le _) (And.intro (Nat.zero_le _) (And.intro (fun _ h => nomatch h) (fun _ h => nomatch h)))
| cst v => exact And.intro (Nat.zero_le _) (And.intro (Nat.zero_le _) (And.intro (fun _ h => nomatch h) (fun _ h => nomatch h)))
| op f _ j k =>
have hz := nthA_sz s r hs hr (apx s r X gs) (apx_sz s r hs hr X gs)
exact And.intro (kl_count s r _ (hz j).2 (hz k).2)
(And.intro (kr_count s r _ (hz j).1 (hz k).1)
(And.intro (kl_len s _) (kr_len r _)))
theorem apx_len (s r : Nat) (X : List (Nat -> Bool)) : forall C : List Gate, (apx s r X C).length = C.length
| [] => rfl
| _ :: gs => congrArg (· + 1) (apx_len s r X gs)
theorem allKL_count (s r : Nat) (M : Nat) : forall As : List Apx, (forall A, A ∈ As -> A.KL.length <= M) ->
(allKL As).length <= As.length * M
| [], _ => Nat.zero_le _
| A :: As, h => by
show (A.KL ++ allKL As).length <= (As.length + 1) * M
rw [len_app, Found.Pnp98.add_mul', Found.Pnp115.one_mul', Nat.add_comm (As.length * M)]
exact Nat.add_le_add (h A (List.Mem.head As)) (allKL_count s r M As (fun B hB => h B (List.Mem.tail A hB)))
theorem allKR_count (s r : Nat) (M : Nat) : forall As : List Apx, (forall A, A ∈ As -> A.KR.length <= M) ->
(allKR As).length <= As.length * M
| [], _ => Nat.zero_le _
| A :: As, h => by
show (A.KR ++ allKR As).length <= (As.length + 1) * M
rw [len_app, Found.Pnp98.add_mul', Found.Pnp115.one_mul', Nat.add_comm (As.length * M)]
exact Nat.add_le_add (h A (List.Mem.head As)) (allKR_count s r M As (fun B hB => h B (List.Mem.tail A hB)))
theorem mem_allKL (T : List Nat) : forall As : List Apx, T ∈ allKL As -> Exists fun A => A ∈ As /\ T ∈ A.KL
| [], h => nomatch h
| A :: As, h => by
cases mem_app T A.KL (allKL As) h with
| inl l => exact Exists.intro A (And.intro (List.Mem.head As) l)
| inr r' =>
cases mem_allKL T As r' with
| intro B hB => exact Exists.intro B (And.intro (List.Mem.tail A hB.1) hB.2)
theorem mem_allKR (T : List Nat) : forall As : List Apx, T ∈ allKR As -> Exists fun A => A ∈ As /\ T ∈ A.KR
| [], h => nomatch h
| A :: As, h => by
cases mem_app T A.KR (allKR As) h with
| inl l => exact Exists.intro A (And.intro (List.Mem.head As) l)
| inr r' =>
cases mem_allKR T As r' with
| intro B hB => exact Exists.intro B (And.intro (List.Mem.tail A hB.1) hB.2)
/-- The whole circuit: at most |C| (2r)^s sets of s elements in allKL, |C| (2s)^r sets of r elements in allKR. -/
theorem circuit_sizes (s r : Nat) (hs : 2 <= s) (hr : 2 <= r) (X : List (Nat -> Bool)) (C : List Gate) :
(allKL (apx s r X C)).length <= C.length * (2 * r) ^ s /\
(allKR (apx s r X C)).length <= C.length * (2 * s) ^ r /\
(forall T, T ∈ allKL (apx s r X C) -> T.length = s) /\
(forall T, T ∈ allKR (apx s r X C) -> T.length = r) := by
have hk := apx_kok s r hs hr X C
refine And.intro ?_ (And.intro ?_ (And.intro ?_ ?_))
· have t := allKL_count s r _ (apx s r X C) (fun A hA => (hk A hA).1)
rw [apx_len] at t
exact t
· have t := allKR_count s r _ (apx s r X C) (fun A hA => (hk A hA).2.1)
rw [apx_len] at t
exact t
· intro T hT
cases mem_allKL T _ hT with
| intro A hA => exact (hk A hA.1).2.2.1 T hA.2
· intro T hT
cases mem_allKR T _ hT with
| intro A hA => exact (hk A hA.1).2.2.2 T hA.2
/-- The output CNF approximator has clauses of at most s - 1 variables. -/
theorem out_clauses (s r : Nat) (hs : 2 <= s) (hr : 2 <= r) (X : List (Nat -> Bool)) (C : List Gate) :
forall m, m ∈ (nthA (apx s r X C) 0).L (0, 0) -> m.length + 1 <= s :=
(nthA_sz s r hs hr _ (apx_sz s r hs hr X C) 0).1 (0, 0)
end Found.Pnp121
#print axioms Found.Pnp121.mem_selF
#print axioms Found.Pnp121.small_one
#print axioms Found.Pnp121.small_nil
#print axioms Found.Pnp121.small_empty
#print axioms Found.Pnp121.cst_sz
#print axioms Found.Pnp121.inp_sz
#print axioms Found.Pnp121.leaf_sz
#print axioms Found.Pnp121.op_sz
#print axioms Found.Pnp121.nthA_sz
#print axioms Found.Pnp121.apx_sz
#print axioms Found.Pnp121.kl_len
#print axioms Found.Pnp121.kr_len
#print axioms Found.Pnp121.join_len
#print axioms Found.Pnp121.kl_count
#print axioms Found.Pnp121.kr_count
#print axioms Found.Pnp121.apx_kok
#print axioms Found.Pnp121.apx_len
#print axioms Found.Pnp121.allKL_count
#print axioms Found.Pnp121.allKR_count
#print axioms Found.Pnp121.mem_allKL
#print axioms Found.Pnp121.mem_allKR
#print axioms Found.Pnp121.circuit_sizes
#print axioms Found.Pnp121.out_clauses
#print axioms: 23 theorems, no axioms.
If a monotone real circuit separates positive from negative test inputs, then every negative input misses a correction set entirely, or some short clause \(q\) is hit or a correction set covered by every positive input.
found/Pnp122.lean
/-
T330 piece 122. found/Pnp122 -- the lower-bound criterion for monotone real circuits on test
inputs (Jukna, Boolean Function Complexity, Theorems 9.17, 9.18, 9.21 in counting form;
proof complexity ladder, step 2).
Let a monotone real circuit C output 1 on every positive test input (list Pos) and 0 on every
negative one (list Neg), and let 2 <= s, r. Then
(I) every negative input misses entirely some set of allKL (at most |C| (2r)^s sets of
s variables), or
(II) there is a set q of at most s - 1 variables such that every positive input hits q or
covers some set of allKR (at most |C| (2s)^r sets of r variables).
In counting form: |Neg| <= sum over T in allKL of #{x in Neg : x misses T}, or
|Pos| <= #{x in Pos : x hits q} + sum over T in allKR of #{x in Pos : x covers T}.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp121
namespace Found.Pnp122
open Found.Pnp65 (bv)
open Found.Pnp115 (anyL anyL_true lsum lsum_le len_lsum mem_cons_cases)
open Found.Pnp116 (allL anyL_ex mem_app_l mem_app_r)
open Found.Pnp117 (allLL allLL_mem allLL_false evCNF)
open Found.Pnp120 (apx nthA allKL allKR out_neg out_pos)
open Found.Pnp113 (Gate outV)
open Found.Pnp109 (out)
theorem anyL_false_all {α : Type} (g : α -> Bool) : forall L : List α, anyL g L = false -> forall a, a ∈ L -> g a = false
| [], _, _, h => nomatch h
| b :: L, h, a, ha => by
have h' : (g b || anyL g L) = false := h
cases e : g b with
| true => rw [e] at h'; exact Bool.noConfusion h'
| false =>
rw [e] at h'
cases mem_cons_cases ha with
| inl q => rw [q]; exact e
| inr h2 => exact anyL_false_all g L h' a h2
/-- The criterion. -/
theorem criterion (s r : Nat) (C : List Gate) (Pos Neg : List (Nat -> Bool))
(hP : forall x, x ∈ Pos -> out (outV x C) = true) (hN : forall x, x ∈ Neg -> out (outV x C) = false) :
(forall x, x ∈ Neg -> Exists fun T => T ∈ allKL (apx s r (Pos ++ Neg) C) /\ anyL x T = false) \/
(Exists fun q : List Nat => q ∈ (nthA (apx s r (Pos ++ Neg) C) 0).L (0, 0) /\
forall x, x ∈ Pos -> anyL x q = true \/ Exists fun T => T ∈ allKR (apx s r (Pos ++ Neg) C) /\ allL x T = true) := by
cases eL : (nthA (apx s r (Pos ++ Neg) C) 0).L (0, 0) with
| nil =>
refine Or.inl (fun x hx => ?_)
cases e : allLL (anyL x) (allKL (apx s r (Pos ++ Neg) C)) with
| false =>
cases allLL_false _ _ e with
| intro T hT => exact Exists.intro T hT
| true =>
have t := out_neg s r (Pos ++ Neg) x (mem_app_r x Neg Pos hx) C
(fun T hT => allLL_mem _ _ T e hT) (hN x hx)
rw [eL] at t
exact Bool.noConfusion t
| cons q rest =>
refine Or.inr (Exists.intro q (And.intro (List.Mem.head rest) (fun x hx => ?_)))
cases e : anyL (allL x) (allKR (apx s r (Pos ++ Neg) C)) with
| true =>
cases anyL_ex _ _ e with
| intro T hT => exact Or.inr (Exists.intro T hT)
| false =>
have t := out_pos s r (Pos ++ Neg) x (mem_app_l x Neg Pos hx) C
(anyL_false_all _ _ e) (hP x hx)
rw [eL] at t
exact Or.inl (allLL_mem _ _ q t (List.Mem.head rest))
/-! Counting. -/
theorem lsum_swap {α β : Type} (f : α -> β -> Nat) (B : List β) : forall A : List α,
lsum (fun a => lsum (fun b => f a b) B) A = lsum (fun b => lsum (fun a => f a b) A) B
| [] => (Found.Pnp115.lsum_zero_of _ B (fun _ _ => rfl)).symm
| a :: A => by
show lsum (fun b => f a b) B + lsum (fun a => lsum (fun b => f a b) B) A =
lsum (fun b => f a b + lsum (fun a => f a b) A) B
rw [lsum_swap f B A]
exact (lsum_add _ _ B).symm
where
lsum_add (g h : β -> Nat) : forall L : List β, lsum (fun b => g b + h b) L = lsum g L + lsum h L
| [] => rfl
| b :: L => by
show g b + h b + lsum (fun b => g b + h b) L = (g b + lsum g L) + (h b + lsum h L)
rw [lsum_add g h L, Found.Pnp93.add4]
theorem one_le_lsum {β : Type} (g : β -> Nat) : forall (L : List β) (b : β), b ∈ L -> 1 <= g b -> 1 <= lsum g L
| [], _, h, _ => nomatch h
| c :: L, b, h, hb => by
show 1 <= g c + lsum g L
cases mem_cons_cases h with
| inl q => rw [<- q]; exact Nat.le_trans hb (Nat.le_add_right _ _)
| inr h2 => exact Nat.le_trans (one_le_lsum g L b h2 hb) (Nat.le_add_left _ _)
/-- Union bound: if every x has a witness T in K with P x T, then |Xs| <= sum over T of #{x : P x T}. -/
theorem union_bound {α β : Type} (P : α -> β -> Bool) (K : List β) (Xs : List α)
(h : forall x, x ∈ Xs -> Exists fun T => T ∈ K /\ P x T = true) :
Xs.length <= lsum (fun T => lsum (fun x => bv (P x T)) Xs) K := by
rw [len_lsum Xs, <- lsum_swap (fun x T => bv (P x T)) K Xs]
refine lsum_le _ _ Xs (fun x hx => ?_)
cases h x hx with
| intro T hT =>
refine one_le_lsum (fun T => bv (P x T)) K T hT.1 ?_
show 1 <= bv (P x T)
rw [hT.2]
exact Nat.le_refl 1
def missB (x : Nat -> Bool) (T : List Nat) : Bool := !(anyL x T)
/-- The criterion in counting form. -/
theorem criterion_count (s r : Nat) (C : List Gate) (Pos Neg : List (Nat -> Bool))
(hP : forall x, x ∈ Pos -> out (outV x C) = true) (hN : forall x, x ∈ Neg -> out (outV x C) = false) :
Neg.length <= lsum (fun T => lsum (fun x => bv (missB x T)) Neg) (allKL (apx s r (Pos ++ Neg) C)) \/
(Exists fun q : List Nat => q ∈ (nthA (apx s r (Pos ++ Neg) C) 0).L (0, 0) /\
Pos.length <= lsum (fun x => bv (anyL x q)) Pos +
lsum (fun T => lsum (fun x => bv (allL x T)) Pos) (allKR (apx s r (Pos ++ Neg) C))) := by
cases criterion s r C Pos Neg hP hN with
| inl hI =>
refine Or.inl (union_bound missB _ Neg (fun x hx => ?_))
cases hI x hx with
| intro T hT =>
refine Exists.intro T (And.intro hT.1 ?_)
show (!(anyL x T)) = true
rw [hT.2]
rfl
| inr hII =>
cases hII with
| intro q hq =>
refine Or.inr (Exists.intro q (And.intro hq.1 ?_))
-- split Pos by whether x hits q
have hs : forall L : List (Nat -> Bool), (forall x, x ∈ L -> anyL x q = true \/
Exists fun T => T ∈ allKR (apx s r (Pos ++ Neg) C) /\ allL x T = true) ->
L.length <= lsum (fun x => bv (anyL x q)) L +
lsum (fun T => lsum (fun x => bv (allL x T)) L) (allKR (apx s r (Pos ++ Neg) C)) := by
intro L hL
let K := allKR (apx s r (Pos ++ Neg) C)
let P' : (Nat -> Bool) -> Option (List Nat) -> Bool := fun x o =>
match o with
| none => anyL x q
| some T => allL x T
have ub := union_bound P' (none :: Found.Pnp120.mapL some K) L (fun x hx => by
cases hL x hx with
| inl a => exact Exists.intro none (And.intro (List.Mem.head _) a)
| inr b =>
cases b with
| intro T hT => exact Exists.intro (some T) (And.intro
(List.Mem.tail none (Found.Pnp120.mem_mapL some T K hT.1)) hT.2))
refine Nat.le_trans ub (Nat.le_of_eq ?_)
show lsum (fun x => bv (anyL x q)) L + lsum (fun o => lsum (fun x => bv (P' x o)) L) (Found.Pnp120.mapL some K) = _
congr 1
exact lsum_map K
exact hs Pos hq.2
where
lsum_map {g : Option (List Nat) -> Nat} : forall K : List (List Nat),
lsum g (Found.Pnp120.mapL some K) = lsum (fun T => g (some T)) K
| [] => rfl
| T :: K => congrArg (g (some T) + ·) (lsum_map K)
end Found.Pnp122
#print axioms Found.Pnp122.anyL_false_all
#print axioms Found.Pnp122.criterion
#print axioms Found.Pnp122.lsum_swap
#print axioms Found.Pnp122.one_le_lsum
#print axioms Found.Pnp122.union_bound
#print axioms Found.Pnp122.criterion_count
#print axioms: 6 theorems, no axioms.
Approximators and correction sets use only variables read by input gates, and correction sets have pairwise different variables; restricting a circuit to off-diagonal edge variables computes it on the masked input.
found/Pnp123.lean
/-
T330 piece 123. found/Pnp123 -- the variables of the approximation, and the circuit restricted
to off-diagonal edge variables (proof complexity ladder, step 2).
The sets produced by the transversal tree use only variables of the starting set and of the
list; the cut sets add a new variable at each step, so their variables are pairwise
different. Hence, if every input gate of a circuit reads a variable with property P, every
clause, monomial and correction set of its approximation uses only such variables, and every
correction set has pairwise different variables. The restriction of a circuit replaces every
input gate whose variable is not an off-diagonal edge variable p(i, j) (i <> j, i, j < n) by
the constant 0; it computes the original circuit on the input masked to those variables.
Proved:
* tr_vars, tr_snd_distinct: variables and distinctness in the tree;
* apx_vars, allKL_distinct, allKR_distinct: for a whole circuit;
* restrict_eval, restrict_inp: the restricted circuit.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp122
namespace Found.Pnp123
open Found.Pnp65 (bv anyI)
open Found.Pnp115 (Pw anyL mem_cons_cases beq_self)
open Found.Pnp116 (memN pierce firstU tr tr_none tr_some mem_brs1 mem_brs2 firstU_some mem_app memN_mem)
open Found.Pnp119 (GD Lf Rf KL KR cm mem_cm joinL mem_joinL fD fC Md Ma Da Ca)
open Found.Pnp120 (Apx selF cstA inpA nthA apx apxG gdOf allKL allKR mapL)
open Found.Pnp121 (mem_selF mem_allKL mem_allKR)
open Found.Pnp122 (anyL_false_all)
open Found.Pnp113 (Gate gval evalR nthV)
open Found.Pnp104 (pv)
/-! Variables in the tree. -/
def VarsOK (P : Nat -> Bool) (F : List (List Nat)) : Prop := forall m, m ∈ F -> forall v, v ∈ m -> P v = true
theorem tr_vars (C : List (List Nat)) : forall (d : Nat) (p m : List Nat), (m ∈ (tr C d p).1 \/ m ∈ (tr C d p).2) ->
forall v, v ∈ m -> v ∈ p \/ Exists fun q => q ∈ C /\ v ∈ q
| 0, p, m, h, v, hv => by
cases h with
| inl l => exact nomatch l
| inr r =>
cases mem_cons_cases r with
| inl q => rw [q] at hv; exact Or.inl hv
| inr h2 => exact nomatch h2
| d + 1, p, m, h, v, hv => by
cases e : firstU p C with
| none =>
rw [tr_none C d p e] at h
cases h with
| inl l =>
cases mem_cons_cases l with
| inl q => rw [q] at hv; exact Or.inl hv
| inr h2 => exact nomatch h2
| inr r => exact nomatch r
| some q0 =>
rw [tr_some C d p q0 e] at h
have hq0 := firstU_some p C q0 e
have step : forall u, u ∈ q0 -> (m ∈ (tr C d (u :: p)).1 \/ m ∈ (tr C d (u :: p)).2) ->
v ∈ p \/ Exists fun q => q ∈ C /\ v ∈ q := by
intro u hu hm
cases tr_vars C d (u :: p) m hm v hv with
| inl l =>
cases mem_cons_cases l with
| inl q => rw [q]; exact Or.inr (Exists.intro q0 (And.intro hq0.1 hu))
| inr h2 => exact Or.inl h2
| inr r => exact Or.inr r
cases h with
| inl l =>
cases mem_brs1 _ m q0 l with
| intro u hu => exact step u hu.1 (Or.inl hu.2)
| inr r =>
cases mem_brs2 _ m q0 r with
| intro u hu => exact step u hu.1 (Or.inr hu.2)
theorem tr_ok (P : Nat -> Bool) (C : List (List Nat)) (hC : VarsOK P C) (d : Nat) :
VarsOK P (tr C d []).1 /\ VarsOK P (tr C d []).2 := by
refine And.intro (fun m hm v hv => ?_) (fun m hm v hv => ?_)
· cases tr_vars C d [] m (Or.inl hm) v hv with
| inl l => exact nomatch l
| inr r => cases r with | intro q hq => exact hC q hq.1 v hq.2
· cases tr_vars C d [] m (Or.inr hm) v hv with
| inl l => exact nomatch l
| inr r => cases r with | intro q hq => exact hC q hq.1 v hq.2
/-! Distinct variables in cut sets. -/
def Dist (m : List Nat) : Prop := Pw (fun a b => Not (a = b)) m
theorem memN_of_mem (v : Nat) : forall l : List Nat, v ∈ l -> memN v l = true
| [], h => nomatch h
| a :: l, h => by
show (Nat.beq v a || memN v l) = true
cases mem_cons_cases h with
| inl q => rw [q, beq_self]; rfl
| inr h2 => rw [memN_of_mem v l h2]; exact Found.Pnp65.or_true_r _
theorem tr_snd_distinct (C : List (List Nat)) : forall (d : Nat) (p m : List Nat), Dist p -> m ∈ (tr C d p).2 -> Dist m
| 0, p, m, hp, h => by
cases mem_cons_cases h with
| inl q => rw [q]; exact hp
| inr h2 => exact nomatch h2
| d + 1, p, m, hp, h => by
cases e : firstU p C with
| none =>
rw [tr_none C d p e] at h
exact nomatch h
| some q0 =>
rw [tr_some C d p q0 e] at h
have hq0 := firstU_some p C q0 e
cases mem_brs2 _ m q0 h with
| intro u hu =>
refine tr_snd_distinct C d (u :: p) m (And.intro (fun w hw ewu => ?_) hp) hu.2
have t : memN u p = false := anyL_false_all (fun v => memN v p) q0 hq0.2 u hu.1
rw [memN_of_mem u p (ewu ▸ hw)] at t
exact Bool.noConfusion t
/-! A whole circuit. -/
def gOK (P : Nat -> Bool) : Gate -> Prop
| .inp i => P i = true
| .cst _ => True
| .op _ _ _ _ => True
def InpOK (P : Nat -> Bool) : List Gate -> Prop
| [] => True
| g :: gs => gOK P g /\ InpOK P gs
def AOK (P : Nat -> Bool) (A : Apx) : Prop :=
(forall b, VarsOK P (A.L b)) /\ (forall b, VarsOK P (A.R b)) /\ VarsOK P A.KL /\ VarsOK P A.KR
theorem ok_selF (P : Nat -> Bool) (b : Bool) (u w : List (List Nat)) (hu : VarsOK P u) (hw : VarsOK P w) :
VarsOK P (selF b u w) := fun m hm => by
cases mem_selF b u w m hm with
| inl l => exact hu m l
| inr r => exact hw m r
theorem ok_nil (P : Nat -> Bool) : VarsOK P [] := fun _ h => nomatch h
theorem ok_nilnil (P : Nat -> Bool) : VarsOK P [[]] := fun m hm v hv => by
cases mem_cons_cases hm with
| inl q => rw [q] at hv; exact nomatch hv
| inr h2 => exact nomatch h2
theorem ok_one (P : Nat -> Bool) (i : Nat) (hi : P i = true) : VarsOK P [[i]] := fun m hm v hv => by
cases mem_cons_cases hm with
| inl q =>
rw [q] at hv
cases mem_cons_cases hv with
| inl q' => rw [q']; exact hi
| inr h2 => exact nomatch h2
| inr h2 => exact nomatch h2
theorem cst_ok (P : Nat -> Bool) (v : Nat × Nat) : AOK P (cstA v) :=
And.intro (fun _ => ok_selF P _ _ _ (ok_nil P) (ok_nilnil P))
(And.intro (fun _ => ok_selF P _ _ _ (ok_nilnil P) (ok_nil P)) (And.intro (ok_nil P) (ok_nil P)))
theorem inp_ok (P : Nat -> Bool) (i : Nat) (hi : P i = true) : AOK P (inpA i) :=
And.intro (fun _ => ok_selF P _ _ _ (ok_nil P) (ok_selF P _ _ _ (ok_one P i hi) (ok_nilnil P)))
(And.intro (fun _ => ok_selF P _ _ _ (ok_nilnil P) (ok_selF P _ _ _ (ok_one P i hi) (ok_nil P)))
(And.intro (ok_nil P) (ok_nil P)))
theorem ok_join (P : Nat -> Bool) (A B : List (List Nat)) (hA : VarsOK P A) (hB : VarsOK P B) : VarsOK P (joinL A B) :=
fun m hm v hv => by
cases mem_joinL A B m hm with
| intro a ha =>
cases ha.2 with
| intro b hb =>
rw [hb.2] at hv
cases mem_app v a b hv with
| inl l => exact hA a ha.1 v l
| inr r => exact hB b hb.1 v r
theorem ok_cm {α : Type} (P : Nat -> Bool) (f : α -> List (List Nat)) (L : List α) (h : forall u, VarsOK P (f u)) :
VarsOK P (cm f L) := fun m hm v hv => by
cases mem_cm f m L hm with
| intro u hu => exact h u m hu.2 v hv
theorem nthA_ok (P : Nat -> Bool) : forall (As : List Apx), (forall A, A ∈ As -> AOK P A) -> forall j, AOK P (nthA As j)
| [], _, _ => cst_ok P (0, 0)
| A :: _, h, 0 => h A (List.Mem.head _)
| A :: As, h, j + 1 => nthA_ok P As (fun B hB => h B (List.Mem.tail A hB)) j
theorem apx_vars (P : Nat -> Bool) (s r : Nat) (X : List (Nat -> Bool)) : forall C : List Gate, InpOK P C ->
forall A, A ∈ apx s r X C -> AOK P A
| [], _, _, h => nomatch h
| g :: gs, hC, A, h => by
cases mem_cons_cases h with
| inr h2 => exact apx_vars P s r X gs hC.2 A h2
| inl q =>
rw [q]
have hch := nthA_ok P (apx s r X gs) (apx_vars P s r X gs hC.2)
cases g with
| inp i => exact inp_ok P i hC.1
| cst v => exact cst_ok P v
| op f _ j k =>
have hfD : forall u, VarsOK P (fD (gdOf X gs (apx s r X gs) f j k) u) :=
fun u => ok_join P _ _ ((hch j).2.1 u.1) ((hch k).2.1 u.2)
have hfC : forall u, VarsOK P (fC (gdOf X gs (apx s r X gs) f j k) u) :=
fun u => ok_join P _ _ ((hch j).1 _) ((hch k).1 _)
exact And.intro (fun a => (tr_ok P _ (ok_cm P _ _ hfD) s).1)
(And.intro (fun a => (tr_ok P _ (ok_cm P _ _ hfC) r).1)
(And.intro (tr_ok P _ (ok_cm P _ _ hfD) s).2 (tr_ok P _ (ok_cm P _ _ hfC) r).2))
theorem allKL_vars (P : Nat -> Bool) (s r : Nat) (X : List (Nat -> Bool)) (C : List Gate) (hC : InpOK P C) :
VarsOK P (allKL (apx s r X C)) := fun T hT v hv => by
cases mem_allKL T _ hT with
| intro A hA => exact (apx_vars P s r X C hC A hA.1).2.2.1 T hA.2 v hv
theorem allKR_vars (P : Nat -> Bool) (s r : Nat) (X : List (Nat -> Bool)) (C : List Gate) (hC : InpOK P C) :
VarsOK P (allKR (apx s r X C)) := fun T hT v hv => by
cases mem_allKR T _ hT with
| intro A hA => exact (apx_vars P s r X C hC A hA.1).2.2.2 T hA.2 v hv
theorem out_vars (P : Nat -> Bool) (s r : Nat) (X : List (Nat -> Bool)) (C : List Gate) (hC : InpOK P C) :
VarsOK P ((nthA (apx s r X C) 0).L (0, 0)) :=
(nthA_ok P _ (apx_vars P s r X C hC) 0).1 (0, 0)
theorem apx_dist (s r : Nat) (X : List (Nat -> Bool)) : forall (C : List Gate) (A : Apx), A ∈ apx s r X C ->
(forall T, T ∈ A.KL -> Dist T) /\ (forall T, T ∈ A.KR -> Dist T)
| [], _, h => nomatch h
| g :: gs, A, h => by
cases mem_cons_cases h with
| inr h2 => exact apx_dist s r X gs A h2
| inl q =>
rw [q]
cases g with
| inp i => exact And.intro (fun _ h => nomatch h) (fun _ h => nomatch h)
| cst v => exact And.intro (fun _ h => nomatch h) (fun _ h => nomatch h)
| op f _ j k =>
exact And.intro (fun T hT => tr_snd_distinct _ s [] T True.intro hT)
(fun T hT => tr_snd_distinct _ r [] T True.intro hT)
theorem allKL_distinct (s r : Nat) (X : List (Nat -> Bool)) (C : List Gate) (T : List Nat)
(hT : T ∈ allKL (apx s r X C)) : Dist T := by
cases mem_allKL T _ hT with
| intro A hA => exact (apx_dist s r X C A hA.1).1 T hA.2
theorem allKR_distinct (s r : Nat) (X : List (Nat -> Bool)) (C : List Gate) (T : List Nat)
(hT : T ∈ allKR (apx s r X C)) : Dist T := by
cases mem_allKR T _ hT with
| intro A hA => exact (apx_dist s r X C A hA.1).2 T hA.2
/-! The restriction to off-diagonal edge variables. -/
/-- v is p(i, j) with i, j < n and i <> j. -/
def offE (n v : Nat) : Bool := Nat.blt v (n * n) && !(anyI (fun i => Nat.beq v (pv n i i)) n)
def selG (b : Bool) (g h : Gate) : Gate := match b with | true => g | false => h
def restrictG (n : Nat) : Gate -> Gate
| .inp i => selG (offE n i) (Gate.inp i) (Gate.cst (0, 0))
| .cst v => Gate.cst v
| .op f hf j k => Gate.op f hf j k
def restrict (n : Nat) (C : List Gate) : List Gate := mapL (restrictG n) C
def maskX (n : Nat) (x : Nat -> Bool) (v : Nat) : Bool := offE n v && x v
theorem restrict_eval (n : Nat) (x : Nat -> Bool) : forall C : List Gate, evalR x (restrict n C) = evalR (maskX n x) C
| [] => rfl
| g :: gs => by
show gval x (evalR x (restrict n gs)) (restrictG n g) :: evalR x (restrict n gs) =
gval (maskX n x) (evalR (maskX n x) gs) g :: evalR (maskX n x) gs
rw [restrict_eval n x gs]
cases g with
| inp i =>
show gval x _ (selG (offE n i) (Gate.inp i) (Gate.cst (0, 0))) :: _ = (bv (offE n i && x i), 0) :: _
cases e : offE n i with
| true => rfl
| false => rfl
| cst v => rfl
| op f hf j k => rfl
theorem restrict_inp (n : Nat) : forall C : List Gate, InpOK (offE n) (restrict n C)
| [] => True.intro
| g :: gs => by
refine And.intro ?_ (restrict_inp n gs)
show gOK (offE n) (restrictG n g)
cases g with
| inp i =>
show gOK (offE n) (selG (offE n i) (Gate.inp i) (Gate.cst (0, 0)))
cases e : offE n i with
| true => exact e
| false => exact True.intro
| cst v => exact True.intro
| op f hf j k => exact True.intro
theorem restrict_len (n : Nat) : forall C : List Gate, (restrict n C).length = C.length
| [] => rfl
| _ :: gs => congrArg (· + 1) (restrict_len n gs)
end Found.Pnp123
#print axioms Found.Pnp123.tr_vars
#print axioms Found.Pnp123.tr_ok
#print axioms Found.Pnp123.memN_of_mem
#print axioms Found.Pnp123.tr_snd_distinct
#print axioms Found.Pnp123.ok_selF
#print axioms Found.Pnp123.ok_nil
#print axioms Found.Pnp123.ok_nilnil
#print axioms Found.Pnp123.ok_one
#print axioms Found.Pnp123.cst_ok
#print axioms Found.Pnp123.inp_ok
#print axioms Found.Pnp123.ok_join
#print axioms Found.Pnp123.ok_cm
#print axioms Found.Pnp123.nthA_ok
#print axioms Found.Pnp123.apx_vars
#print axioms Found.Pnp123.allKL_vars
#print axioms Found.Pnp123.allKR_vars
#print axioms Found.Pnp123.out_vars
#print axioms Found.Pnp123.apx_dist
#print axioms Found.Pnp123.allKL_distinct
#print axioms Found.Pnp123.allKR_distinct
#print axioms Found.Pnp123.restrict_eval
#print axioms Found.Pnp123.restrict_inp
#print axioms Found.Pnp123.restrict_len
#print axioms: 23 theorems, no axioms.
Among the \(c^n\) sequences of length \(n\), those passing local tests that admit at most one value at each of \(F\) forced positions number at most \(c^{\,n-F}\).
found/Pnp124.lean
/-
T330 piece 124. found/Pnp124 -- sequences and forced positions (proof complexity ladder,
step 2: counting test inputs).
allSeq c n lists c ^ n sequences f with f i < c for i < n (built position by position with
extF). A test t p f at each position p looks only at f on positions <= p; at a forced
position at most one value passes, whatever came before. Then the sequences passing all
tests below n number at most c ^ (n - F), where F is the number of forced positions below n;
stated without subtraction:
#{f in allSeq c n : all tests pass} * c ^ F <= c ^ n.
Proved:
* allSeq_len, allSeq_rng: the list;
* lsum_cm, lsum_mapL, lsum_mul_r, lsum_const_eq, le_one_unique: counting tools;
* forced_count: the bound.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp123
namespace Found.Pnp124
open Found.Pnp65 (bv selN cntI)
open Found.Pnp90 (allI)
open Found.Pnp115 (Pw lsum lsum_le lsum_const_le mem_cons_cases beq_eq beq_self and_true_l and_true_r one_mul')
open Found.Pnp116 (mem_app len_app)
open Found.Pnp119 (cm mem_cm mem_cm_of)
open Found.Pnp120 (mapL mem_mapL)
open Found.Pnp123 (Dist)
open Found.Pnp98 (mul_le_r add_mul' mul_add')
open Found.Pnp99 (mul_le_l' mul_assoc')
open Found.Pnp114 (mul_one')
def rng : Nat -> List Nat
| 0 => []
| c + 1 => c :: rng c
theorem mem_rng : forall (c v : Nat), v ∈ rng c -> v < c
| 0, _, h => nomatch h
| c + 1, v, h => by
cases mem_cons_cases h with
| inl q => rw [q]; exact Nat.lt_succ_self c
| inr h2 => exact Nat.lt_succ_of_lt (mem_rng c v h2)
theorem mem_rng_of : forall (c v : Nat), v < c -> v ∈ rng c
| 0, v, h => absurd h (Nat.not_lt_zero v)
| c + 1, v, h => by
cases Found.Pnp115.lt_succ_cases h with
| inl l => exact List.Mem.tail c (mem_rng_of c v l)
| inr e => rw [e]; exact List.Mem.head _
theorem rng_dist : forall c, Dist (rng c)
| 0 => True.intro
| c + 1 => And.intro (fun w hw e => Nat.lt_irrefl c (e ▸ mem_rng c w hw)) (rng_dist c)
theorem len_rng : forall c, (rng c).length = c
| 0 => rfl
| c + 1 => congrArg (· + 1) (len_rng c)
def extF (f : Nat -> Nat) (p v : Nat) (i : Nat) : Nat := selN (Nat.beq i p) v (f i)
theorem beq_lt (i p : Nat) (h : i < p) : Nat.beq i p = false := by
cases e : Nat.beq i p with
| false => rfl
| true => rw [beq_eq i p e] at h; exact absurd h (Nat.lt_irrefl p)
theorem extF_at (f : Nat -> Nat) (p v : Nat) : extF f p v p = v := by
show selN (Nat.beq p p) v (f p) = v
rw [beq_self]
rfl
theorem extF_lt (f : Nat -> Nat) (p v i : Nat) (h : i < p) : extF f p v i = f i := by
show selN (Nat.beq i p) v (f i) = f i
rw [beq_lt i p h]
rfl
def allSeq (c : Nat) : Nat -> List (Nat -> Nat)
| 0 => [fun _ => 0]
| n + 1 => cm (fun f => mapL (fun v => extF f n v) (rng c)) (allSeq c n)
/-! Counting tools. -/
theorem lsum_app {α : Type} (h : α -> Nat) (B : List α) : forall A : List α, lsum h (A ++ B) = lsum h A + lsum h B
| [] => (Nat.zero_add _).symm
| a :: A => by
show h a + lsum h (A ++ B) = h a + lsum h A + lsum h B
rw [lsum_app h B A, Nat.add_assoc]
theorem lsum_cm {α β : Type} (h : β -> Nat) (f : α -> List β) : forall L : List α,
lsum h (cm f L) = lsum (fun u => lsum h (f u)) L
| [] => rfl
| u :: L => by
show lsum h (f u ++ cm f L) = lsum h (f u) + lsum (fun u => lsum h (f u)) L
rw [lsum_app, lsum_cm h f L]
theorem lsum_mapL {α β : Type} (h : β -> Nat) (f : α -> β) : forall L : List α,
lsum h (mapL f L) = lsum (fun u => h (f u)) L
| [] => rfl
| u :: L => congrArg (h (f u) + ·) (lsum_mapL h f L)
theorem lsum_mul_r {α : Type} (h : α -> Nat) (M : Nat) : forall L : List α, lsum (fun u => h u * M) L = lsum h L * M
| [] => (Found.Pnp97.zero_mul' M).symm
| u :: L => by
show h u * M + lsum (fun u => h u * M) L = (h u + lsum h L) * M
rw [lsum_mul_r h M L, add_mul']
theorem lsum_const_eq {α : Type} (c : Nat) : forall L : List α, lsum (fun _ => c) L = L.length * c
| [] => (Found.Pnp97.zero_mul' c).symm
| _ :: L => by
show c + lsum (fun _ => c) L = (L.length + 1) * c
rw [lsum_const_eq c L, add_mul', one_mul', Nat.add_comm]
theorem len_cm {α β : Type} (f : α -> List β) : forall L : List α, (cm f L).length = lsum (fun u => (f u).length) L
| [] => rfl
| u :: L => by
show (f u ++ cm f L).length = (f u).length + lsum (fun u => (f u).length) L
rw [len_app, len_cm f L]
theorem len_mapL {α β : Type} (f : α -> β) : forall L : List α, (mapL f L).length = L.length
| [] => rfl
| _ :: L => congrArg (· + 1) (len_mapL f L)
theorem allSeq_len (c : Nat) : forall n, (allSeq c n).length = c ^ n
| 0 => rfl
| n + 1 => by
show (cm (fun f => mapL (fun v => extF f n v) (rng c)) (allSeq c n)).length = c ^ n * c
rw [len_cm]
have e : lsum (fun f => (mapL (fun v => extF f n v) (rng c)).length) (allSeq c n) = lsum (fun _ => c) (allSeq c n) := by
refine Nat.le_antisymm (lsum_le _ _ _ (fun f _ => ?_)) (lsum_le _ _ _ (fun f _ => ?_))
· rw [len_mapL, len_rng]; exact Nat.le_refl c
· rw [len_mapL, len_rng]; exact Nat.le_refl c
rw [e, lsum_const_eq, allSeq_len c n]
theorem allSeq_rng (c : Nat) : forall (n : Nat) (f : Nat -> Nat), f ∈ allSeq c n -> forall i, i < n -> f i < c
| 0, _, _, i, hi => absurd hi (Nat.not_lt_zero i)
| n + 1, f, h, i, hi => by
cases mem_cm _ f _ h with
| intro g hg =>
have hm : f ∈ mapL (fun v => extF g n v) (rng c) := hg.2
have ex : forall L : List Nat, f ∈ mapL (fun v => extF g n v) L -> Exists fun v => v ∈ L /\ f = extF g n v := by
intro L
induction L with
| nil => intro h'; exact nomatch h'
| cons v L ih =>
intro h'
cases mem_cons_cases h' with
| inl q => exact Exists.intro v (And.intro (List.Mem.head L) q)
| inr h2 =>
cases ih h2 with
| intro w hw => exact Exists.intro w (And.intro (List.Mem.tail v hw.1) hw.2)
cases ex (rng c) hm with
| intro v hv =>
rw [hv.2]
cases Found.Pnp115.lt_succ_cases hi with
| inl l => rw [extF_lt g n v i l]; exact allSeq_rng c n g hg.1 i l
| inr e => rw [e, extF_at]; exact mem_rng c v hv.1
/-- At most one element of a list of distinct numbers satisfies a property with at most one witness. -/
theorem le_one_unique (P : Nat -> Bool) : forall L : List Nat, Dist L ->
(forall v w, v ∈ L -> w ∈ L -> P v = true -> P w = true -> v = w) -> lsum (fun v => bv (P v)) L <= 1
| [], _, _ => Nat.zero_le 1
| v :: L, hd, hu => by
show bv (P v) + lsum (fun v => bv (P v)) L <= 1
cases e : P v with
| false =>
show 0 + _ <= 1
rw [Nat.zero_add]
exact le_one_unique P L hd.2 (fun a b ha hb => hu a b (List.Mem.tail v ha) (List.Mem.tail v hb))
| true =>
have z : lsum (fun v => bv (P v)) L = 0 := Found.Pnp115.lsum_zero_of _ L (fun w hw => by
cases ew : P w with
| false => rfl
| true => exact absurd (hu v w (List.Mem.head L) (List.Mem.tail v hw) e ew) (hd.1 w hw))
exact Nat.le_of_eq (congrArg (bv true + ·) z)
/-! Forced positions. -/
def good (t : Nat -> (Nat -> Nat) -> Bool) (n : Nat) (f : Nat -> Nat) : Bool := allI (fun p => t p f) n
theorem good_ext (t : Nat -> (Nat -> Nat) -> Bool)
(loc : forall p f f', (forall i, i <= p -> f i = f' i) -> t p f = t p f') (f : Nat -> Nat) (n v : Nat) :
forall k, k <= n -> allI (fun p => t p (extF f n v)) k = allI (fun p => t p f) k
| 0, _ => rfl
| k + 1, hk => by
show (allI (fun p => t p (extF f n v)) k && t k (extF f n v)) = (allI (fun p => t p f) k && t k f)
rw [good_ext t loc f n v k (Nat.le_of_succ_le hk),
loc k (extF f n v) f (fun i hi => extF_lt f n v i (Nat.lt_of_le_of_lt hi hk))]
theorem forced_count (c : Nat) (t : Nat -> (Nat -> Nat) -> Bool) (forced : Nat -> Bool)
(loc : forall p f f', (forall i, i <= p -> f i = f' i) -> t p f = t p f')
(uniq : forall p f, forced p = true -> forall v w, t p (extF f p v) = true -> t p (extF f p w) = true -> v = w) :
forall n, lsum (fun f => bv (good t n f)) (allSeq c n) * c ^ (cntI forced n) <= c ^ n
| 0 => by
show (bv true + 0) * 1 <= 1
exact Nat.le_refl 1
| n + 1 => by
have inner : forall f, lsum (fun v => bv (good t (n + 1) (extF f n v))) (rng c) <= bv (good t n f) * selN (forced n) 1 c := by
intro f
have e1 : forall v, good t (n + 1) (extF f n v) = (good t n f && t n (extF f n v)) := by
intro v
show (allI (fun p => t p (extF f n v)) n && t n (extF f n v)) = (good t n f && t n (extF f n v))
rw [good_ext t loc f n v n (Nat.le_refl n)]
rfl
have e2 : lsum (fun v => bv (good t (n + 1) (extF f n v))) (rng c) =
lsum (fun v => bv (good t n f && t n (extF f n v))) (rng c) :=
Nat.le_antisymm (lsum_le _ _ _ (fun v _ => by rw [e1 v]; exact Nat.le_refl _))
(lsum_le _ _ _ (fun v _ => by rw [e1 v]; exact Nat.le_refl _))
rw [e2]
cases eg : good t n f with
| false =>
have z := Found.Pnp115.lsum_zero_of (fun v => bv (false && t n (extF f n v))) (rng c) (fun _ _ => rfl)
rw [z]
exact Nat.zero_le _
| true =>
show lsum (fun v => bv (true && t n (extF f n v))) (rng c) <= 1 * selN (forced n) 1 c
rw [one_mul']
cases ef : forced n with
| true =>
show lsum (fun v => bv (true && t n (extF f n v))) (rng c) <= selN true 1 c
exact le_one_unique (fun v => true && t n (extF f n v)) (rng c) (rng_dist c)
(fun v w _ _ hv hw => uniq n f ef v w hv hw)
| false =>
show lsum (fun v => bv (true && t n (extF f n v))) (rng c) <= selN false 1 c
have t1 := lsum_const_le (fun v => bv (true && t n (extF f n v))) 1 (rng c)
(fun v _ => Found.Pnp107.bv_le1 _)
rw [len_rng, mul_one'] at t1
exact t1
have hstep : lsum (fun f => bv (good t (n + 1) f)) (allSeq c (n + 1)) <=
lsum (fun f => bv (good t n f)) (allSeq c n) * selN (forced n) 1 c := by
show lsum (fun f => bv (good t (n + 1) f)) (cm (fun f => mapL (fun v => extF f n v) (rng c)) (allSeq c n)) <= _
rw [lsum_cm, <- lsum_mul_r]
refine lsum_le _ _ _ (fun f _ => ?_)
rw [lsum_mapL]
exact inner f
have ih := forced_count c t forced loc uniq n
show lsum (fun f => bv (good t (n + 1) f)) (allSeq c (n + 1)) * c ^ (cntI forced n + bv (forced n)) <= c ^ n * c
cases ef : forced n with
| true =>
rw [ef] at hstep
have hstep' := Nat.le_trans hstep (Nat.le_of_eq (mul_one' _))
show lsum (fun f => bv (good t (n + 1) f)) (allSeq c (n + 1)) * (c ^ (cntI forced n) * c) <= c ^ n * c
rw [<- mul_assoc']
exact mul_le_r (Nat.le_trans (mul_le_r hstep' _) ih) c
| false =>
rw [ef] at hstep
have hstep' : lsum (fun f => bv (good t (n + 1) f)) (allSeq c (n + 1)) <=
lsum (fun f => bv (good t n f)) (allSeq c n) * c := hstep
show lsum (fun f => bv (good t (n + 1) f)) (allSeq c (n + 1)) * c ^ (cntI forced n) <= c ^ n * c
refine Nat.le_trans (mul_le_r hstep' _) ?_
rw [mul_assoc', Nat.mul_comm c, <- mul_assoc']
exact mul_le_r ih c
end Found.Pnp124
#print axioms Found.Pnp124.mem_rng
#print axioms Found.Pnp124.mem_rng_of
#print axioms Found.Pnp124.rng_dist
#print axioms Found.Pnp124.len_rng
#print axioms Found.Pnp124.beq_lt
#print axioms Found.Pnp124.extF_at
#print axioms Found.Pnp124.extF_lt
#print axioms Found.Pnp124.lsum_app
#print axioms Found.Pnp124.lsum_cm
#print axioms Found.Pnp124.lsum_mapL
#print axioms Found.Pnp124.lsum_mul_r
#print axioms Found.Pnp124.lsum_const_eq
#print axioms Found.Pnp124.len_cm
#print axioms Found.Pnp124.len_mapL
#print axioms Found.Pnp124.allSeq_len
#print axioms Found.Pnp124.allSeq_rng
#print axioms Found.Pnp124.le_one_unique
#print axioms Found.Pnp124.good_ext
#print axioms Found.Pnp124.forced_count
#print axioms: 19 theorems, no axioms.
Merging classes along a list of edges gives labels \(\mathrm {lab}(x)\le x\); colourings equal across every edge are constant on classes, and at most half of the end points are unforced.
found/Pnp125.lean
/-
T330 piece 125. found/Pnp125 -- merging labels along a list of edges (proof complexity
ladder, step 2: counting colourings that miss a correction set).
For a list E of edges (u, w) with u <> w, labs E gives every vertex a label: start with x
itself, and for each edge relabel the two classes of u and w to the smaller label. Then
labs_le: labs E x <= x;
labs_col: a colouring equal on the two ends of every edge is constant on each class,
col x = col (labs E x);
labs_root: every end point p of an edge has labs E p < p, or labs E p = p and another end
point w has label p.
A vertex is forced if its label is smaller than itself. Mapping every unforced end point
to such a w (found by search) is injective, so
endpoints_le: #end points below n <= 2 * #forced vertices below n.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp124
namespace Found.Pnp125
open Found.Pnp65 (bv selN cntI mn mn_le_l mn_le_r anyI anyI_true cntI_le or_true_r)
open Found.Pnp95 (inj_count)
open Found.Pnp115 (anyL anyL_true mem_cons_cases beq_eq beq_self and_true_l and_true_r)
open Found.Pnp116 (anyL_ex)
open Found.Pnp118 (ble_le le_ble)
def hit (lab : Nat -> Nat) (u w x : Nat) : Bool := Nat.beq (lab x) (lab u) || Nat.beq (lab x) (lab w)
def merge (lab : Nat -> Nat) (u w : Nat) (x : Nat) : Nat := selN (hit lab u w x) (mn (lab u) (lab w)) (lab x)
def labs : List (Nat × Nat) -> Nat -> Nat
| [] => fun x => x
| e :: E => merge (labs E) e.1 e.2
/-- p is an end point of an edge of E. -/
def inE (E : List (Nat × Nat)) (p : Nat) : Bool := anyL (fun e => Nat.beq p e.1 || Nat.beq p e.2) E
def fz (E : List (Nat × Nat)) (p : Nat) : Bool := Nat.blt (labs E p) p
theorem mn_cases (a b : Nat) : mn a b = a \/ mn a b = b := by
show selN (Nat.ble a b) a b = a \/ selN (Nat.ble a b) a b = b
cases Nat.ble a b with
| true => exact Or.inl rfl
| false => exact Or.inr rfl
theorem or_cases {a b : Bool} (h : (a || b) = true) : a = true \/ b = true := by
cases a with
| true => exact Or.inl rfl
| false => exact Or.inr h
theorem merge_hit (lab : Nat -> Nat) (u w x : Nat) (h : hit lab u w x = true) : merge lab u w x = mn (lab u) (lab w) := by
show selN (hit lab u w x) _ _ = _
rw [h]; rfl
theorem merge_miss (lab : Nat -> Nat) (u w x : Nat) (h : hit lab u w x = false) : merge lab u w x = lab x := by
show selN (hit lab u w x) _ _ = _
rw [h]; rfl
theorem hit_u (lab : Nat -> Nat) (u w : Nat) : hit lab u w u = true := by
show (Nat.beq (lab u) (lab u) || _) = true
rw [beq_self]; rfl
theorem hit_w (lab : Nat -> Nat) (u w : Nat) : hit lab u w w = true := by
show (_ || Nat.beq (lab w) (lab w)) = true
rw [beq_self]; exact or_true_r _
theorem labs_le : forall (E : List (Nat × Nat)) (x : Nat), labs E x <= x
| [], x => Nat.le_refl x
| e :: E, x => by
show merge (labs E) e.1 e.2 x <= x
cases eh : hit (labs E) e.1 e.2 x with
| false => rw [merge_miss _ _ _ _ eh]; exact labs_le E x
| true =>
rw [merge_hit _ _ _ _ eh]
cases or_cases eh with
| inl h => exact Nat.le_trans (mn_le_l _ _) (by rw [<- beq_eq _ _ h]; exact labs_le E x)
| inr h => exact Nat.le_trans (mn_le_r _ _) (by rw [<- beq_eq _ _ h]; exact labs_le E x)
theorem labs_col (col : Nat -> Nat) : forall E : List (Nat × Nat), (forall e, e ∈ E -> col e.1 = col e.2) ->
forall x, col x = col (labs E x)
| [], _, x => rfl
| e :: E, hE, x => by
have ih := labs_col col E (fun e' h' => hE e' (List.Mem.tail e h'))
have he := hE e (List.Mem.head E)
show col x = col (merge (labs E) e.1 e.2 x)
cases eh : hit (labs E) e.1 e.2 x with
| false => rw [merge_miss _ _ _ _ eh]; exact ih x
| true =>
rw [merge_hit _ _ _ _ eh]
-- every member of the merged class has the colour of e.1
have cu : col (labs E e.1) = col e.1 := (ih e.1).symm
have cw : col (labs E e.2) = col e.1 := ((ih e.2).symm).trans he.symm
have cm : col (mn (labs E e.1) (labs E e.2)) = col e.1 := by
cases mn_cases (labs E e.1) (labs E e.2) with
| inl q => rw [q]; exact cu
| inr q => rw [q]; exact cw
rw [cm]
cases or_cases eh with
| inl h => rw [ih x, beq_eq _ _ h]; exact cu
| inr h => rw [ih x, beq_eq _ _ h]; exact cw
theorem inE_cons (e : Nat × Nat) (E : List (Nat × Nat)) (p : Nat) :
inE (e :: E) p = ((Nat.beq p e.1 || Nat.beq p e.2) || inE E p) := rfl
theorem labs_root : forall E : List (Nat × Nat), (forall e, e ∈ E -> Not (e.1 = e.2)) ->
forall p, inE E p = true -> labs E p < p \/ (labs E p = p /\ Exists fun w => inE E w = true /\ Not (w = p) /\ labs E w = p)
| [], _, _, h => Bool.noConfusion h
| e :: E, hE, p, hp => by
have ih := labs_root E (fun e' h' => hE e' (List.Mem.tail e h'))
have hne := hE e (List.Mem.head E)
show merge (labs E) e.1 e.2 p < p \/ (merge (labs E) e.1 e.2 p = p /\
Exists fun w => inE (e :: E) w = true /\ Not (w = p) /\ merge (labs E) e.1 e.2 w = p)
cases eh : hit (labs E) e.1 e.2 p with
| true =>
rw [merge_hit _ _ _ _ eh]
cases Nat.lt_or_ge (mn (labs E e.1) (labs E e.2)) p with
| inl l => exact Or.inl l
| inr g =>
have hle : mn (labs E e.1) (labs E e.2) <= p := by
cases or_cases eh with
| inl h => exact Nat.le_trans (mn_le_l _ _) (by rw [<- beq_eq _ _ h]; exact labs_le E p)
| inr h => exact Nat.le_trans (mn_le_r _ _) (by rw [<- beq_eq _ _ h]; exact labs_le E p)
have eqm : mn (labs E e.1) (labs E e.2) = p := Nat.le_antisymm hle g
refine Or.inr (And.intro eqm ?_)
have i1 : inE (e :: E) e.1 = true := by rw [inE_cons, beq_self]; rfl
have i2 : inE (e :: E) e.2 = true := by
rw [inE_cons, beq_self]
show ((Nat.beq e.2 e.1 || true) || inE E e.2) = true
rw [or_true_r]; rfl
cases Nat.decEq e.1 p with
| isFalse ne1 =>
exact Exists.intro e.1 (And.intro i1 (And.intro ne1 (by rw [merge_hit _ _ _ _ (hit_u _ _ _)]; exact eqm)))
| isTrue e1 =>
refine Exists.intro e.2 (And.intro i2 (And.intro (fun q => hne (e1.trans q.symm)) ?_))
rw [merge_hit _ _ _ _ (hit_w _ _ _)]; exact eqm
| false =>
rw [merge_miss _ _ _ _ eh]
have hpE : inE E p = true := by
rw [inE_cons] at hp
cases or_cases hp with
| inr r => exact r
| inl l =>
cases or_cases l with
| inl h1 => rw [beq_eq _ _ h1, hit_u] at eh; exact Bool.noConfusion eh
| inr h2 => rw [beq_eq _ _ h2, hit_w] at eh; exact Bool.noConfusion eh
cases ih p hpE with
| inl l => exact Or.inl l
| inr r =>
refine Or.inr (And.intro r.1 ?_)
cases r.2 with
| intro w hw =>
refine Exists.intro w (And.intro ?_ (And.intro hw.2.1 ?_))
· rw [inE_cons, hw.1, or_true_r]
· have ehw : hit (labs E) e.1 e.2 w = false := by
show (Nat.beq (labs E w) (labs E e.1) || Nat.beq (labs E w) (labs E e.2)) = false
have ehp : (Nat.beq (labs E p) (labs E e.1) || Nat.beq (labs E p) (labs E e.2)) = false := eh
rw [hw.2.2]
rw [r.1] at ehp
exact ehp
rw [merge_miss _ _ _ _ ehw]
exact hw.2.2
/-! Counting end points against forced vertices. -/
def srch (g : Nat -> Bool) : Nat -> Nat
| 0 => 0
| k + 1 => selN (anyI g k) (srch g k) k
theorem srch_spec (g : Nat -> Bool) : forall k, anyI g k = true -> srch g k < k /\ g (srch g k) = true
| 0, h => Bool.noConfusion h
| k + 1, h => by
show selN (anyI g k) (srch g k) k < k + 1 /\ g (selN (anyI g k) (srch g k) k) = true
cases e : anyI g k with
| true =>
have t := srch_spec g k e
exact And.intro (Nat.lt_succ_of_lt t.1) t.2
| false =>
have h' : (anyI g k || g k) = true := h
rw [e] at h'
exact And.intro (Nat.lt_succ_self k) h'
theorem inE_lt (E : List (Nat × Nat)) (n : Nat) (hE : forall e, e ∈ E -> e.1 < n /\ e.2 < n) (p : Nat)
(h : inE E p = true) : p < n := by
cases anyL_ex _ E h with
| intro e he =>
cases or_cases he.2 with
| inl l => rw [beq_eq _ _ l]; exact (hE e he.1).1
| inr r => rw [beq_eq _ _ r]; exact (hE e he.1).2
theorem blt_of_lt (a b : Nat) (h : a < b) : Nat.blt a b = true := le_ble (a + 1) b h
theorem lt_of_blt (a b : Nat) (h : Nat.blt a b = true) : a < b := ble_le (a + 1) b h
theorem cntI_split2 (g h : Nat -> Bool) : forall k, cntI g k = cntI (fun p => g p && h p) k + cntI (fun p => g p && !(h p)) k
| 0 => rfl
| k + 1 => by
show cntI g k + bv (g k) = cntI (fun p => g p && h p) k + bv (g k && h k) + (cntI (fun p => g p && !(h p)) k + bv (g k && !(h k)))
rw [cntI_split2 g h k, Found.Pnp115.bv_split (g k) (h k), Found.Pnp93.add4]
def wg (E : List (Nat × Nat)) (p w : Nat) : Bool := inE E w && !(Nat.beq w p) && Nat.beq (labs E w) p
theorem endpoints_le (E : List (Nat × Nat)) (n : Nat) (hE : forall e, e ∈ E -> Not (e.1 = e.2) /\ e.1 < n /\ e.2 < n) :
cntI (inE E) n <= 2 * cntI (fz E) n := by
rw [cntI_split2 (inE E) (fz E) n, Nat.two_mul]
refine Nat.add_le_add (cntI_le _ _ n (fun j _ h => and_true_r h)) ?_
refine inj_count _ (fun p => srch (wg E p) n) n n (fz E) ?_ ?_
· intro x hx hA
have hin : inE E x = true := and_true_l hA
have hfz : fz E x = false := Found.Pnp91.not_true (and_true_r hA)
have hroot := labs_root E (fun e h => (hE e h).1) x hin
have hlx : labs E x = x := by
cases hroot with
| inl l =>
have hfz' : Nat.blt (labs E x) x = false := hfz
rw [blt_of_lt _ _ l] at hfz'; exact Bool.noConfusion hfz'
| inr r => exact r.1
cases hroot with
| inl l => rw [hlx] at l; exact absurd l (Nat.lt_irrefl x)
| inr r =>
cases r.2 with
| intro w hw =>
have hwn : w < n := inE_lt E n (fun e h => (hE e h).2) w hw.1
have hwg : wg E x w = true := by
show (inE E w && !(Nat.beq w x) && Nat.beq (labs E w) x) = true
rw [hw.1, hw.2.2, beq_self]
cases e : Nat.beq w x with
| true => exact absurd (beq_eq w x e) hw.2.1
| false => rfl
have sp := srch_spec (wg E x) n (anyI_true _ w hwg n hwn)
refine And.intro sp.1 ?_
have w' := sp.2
have l1 : labs E (srch (wg E x) n) = x := beq_eq _ _ (and_true_r w')
have l2 : Not (srch (wg E x) n = x) := by
intro q
have t := and_true_r (and_true_l w')
have t' : (!(Nat.beq (srch (wg E x) n) x)) = true := t
rw [q, beq_self] at t'
exact Bool.noConfusion t'
show Nat.blt (labs E (srch (wg E x) n)) (srch (wg E x) n) = true
rw [l1]
refine blt_of_lt _ _ ?_
have hle := labs_le E (srch (wg E x) n)
rw [l1] at hle
cases Nat.lt_or_ge x (srch (wg E x) n) with
| inl l => exact l
| inr g => exact absurd (Nat.le_antisymm hle g).symm l2
· intro x y hx hy hAx hAy he
have lx : forall z, z < n -> (inE E z && !(fz E z)) = true -> labs E (srch (wg E z) n) = z := by
intro z hz hA
have hin : inE E z = true := and_true_l hA
have hfz : fz E z = false := Found.Pnp91.not_true (and_true_r hA)
cases labs_root E (fun e h => (hE e h).1) z hin with
| inl l =>
have hfz' : Nat.blt (labs E z) z = false := hfz
rw [blt_of_lt _ _ l] at hfz'; exact Bool.noConfusion hfz'
| inr r =>
cases r.2 with
| intro w hw =>
have hwn : w < n := inE_lt E n (fun e h => (hE e h).2) w hw.1
have hwg : wg E z w = true := by
show (inE E w && !(Nat.beq w z) && Nat.beq (labs E w) z) = true
rw [hw.1, hw.2.2, beq_self]
cases e : Nat.beq w z with
| true => exact absurd (beq_eq w z e) hw.2.1
| false => rfl
exact beq_eq _ _ (and_true_r (srch_spec (wg E z) n (anyI_true _ w hwg n hwn)).2)
have ex := lx x hx hAx
have ey := lx y hy hAy
have he' : srch (wg E x) n = srch (wg E y) n := he
rw [he'] at ex
exact ex.symm.trans ey
end Found.Pnp125
#print axioms Found.Pnp125.mn_cases
#print axioms Found.Pnp125.or_cases
#print axioms Found.Pnp125.merge_hit
#print axioms Found.Pnp125.merge_miss
#print axioms Found.Pnp125.hit_u
#print axioms Found.Pnp125.hit_w
#print axioms Found.Pnp125.labs_le
#print axioms Found.Pnp125.labs_col
#print axioms Found.Pnp125.inE_cons
#print axioms Found.Pnp125.labs_root
#print axioms Found.Pnp125.srch_spec
#print axioms Found.Pnp125.inE_lt
#print axioms Found.Pnp125.blt_of_lt
#print axioms Found.Pnp125.lt_of_blt
#print axioms Found.Pnp125.cntI_split2
#print axioms Found.Pnp125.endpoints_le
#print axioms: 16 theorems, no axioms.
A list of pairwise different numbers with a property is no longer than the count of that property; edge variables with both ends in \(V\) number \(|V|^2\).
found/Pnp126.lean
/-
T330 piece 126. found/Pnp126 -- pigeonhole for lists of distinct numbers, and counting edge
variables over a vertex set (proof complexity ladder, step 2).
A list of pairwise different numbers below N, all with property P, has at most cntI P N
elements. The edge variables p(i, j) = i n + j with i, j in a vertex set V (i, j < n) number
exactly |V|^2 among the numbers below n * n. Hence a correction set of s different edge
variables with all end points in V has s <= |V|^2. Proved:
* pigeon: the pigeonhole bound;
* cntI_add, cntI_ext_lt, cntI_and_l: counting tools;
* pairB_eval, pairs_count: the edge variables over V.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp125
namespace Found.Pnp126
open Found.Pnp65 (bv cntI anyI anyI_true var var_inj)
open Found.Pnp96 (anyI_ex)
open Found.Pnp97 (cntI_sumI)
open Found.Pnp106 (sumI_ext_lt)
open Found.Pnp115 (Pw lsum filt mem_filt pw_filt lsum_filt lsum_split len_lsum lsum_le beq_eq beq_self and_true_l and_true_r one_mul')
open Found.Pnp123 (Dist)
open Found.Pnp124 (le_one_unique)
open Found.Pnp98 (add_mul')
open Found.Pnp104 (pv)
theorem pigeon (P : Nat -> Bool) : forall (N : Nat) (T : List Nat), Dist T -> (forall v, v ∈ T -> v < N /\ P v = true) ->
T.length <= cntI P N
| 0, [], _, _ => Nat.le_refl 0
| 0, v :: _, _, h => absurd (h v (List.Mem.head _)).1 (Nat.not_lt_zero v)
| N + 1, T, hd, h => by
show T.length <= cntI P N + bv (P N)
have sp : T.length = lsum (fun v => bv (true && !(Nat.beq v N))) T + lsum (fun v => bv (true && !(!(Nat.beq v N)))) T := by
rw [len_lsum T]
exact lsum_split (fun _ => true) (fun v => !(Nat.beq v N)) T
rw [sp]
refine Nat.add_le_add ?_ ?_
· rw [<- lsum_filt (fun v => true && !(Nat.beq v N)) T]
refine pigeon P N _ (pw_filt _ _ T hd) (fun v hv => ?_)
have m := mem_filt _ v T hv
have hvN := h v m.1
refine And.intro ?_ hvN.2
cases Found.Pnp115.lt_succ_cases hvN.1 with
| inl l => exact l
| inr e =>
have t : (true && !(Nat.beq v N)) = true := m.2
rw [e, beq_self] at t
exact Bool.noConfusion t
· cases eP : P N with
| true =>
refine le_one_unique (fun v => true && !(!(Nat.beq v N))) T hd (fun v w _ _ hv hw => ?_)
have hv' : (true && !(!(Nat.beq v N))) = true := hv
have hw' : (true && !(!(Nat.beq w N))) = true := hw
cases ev : Nat.beq v N with
| false => rw [ev] at hv'; exact Bool.noConfusion hv'
| true =>
cases ew : Nat.beq w N with
| false => rw [ew] at hw'; exact Bool.noConfusion hw'
| true => exact (beq_eq v N ev).trans (beq_eq w N ew).symm
| false =>
have z : lsum (fun v => bv (true && !(!(Nat.beq v N)))) T = 0 :=
Found.Pnp115.lsum_zero_of _ T (fun v hv => by
cases ev : Nat.beq v N with
| false => rfl
| true =>
have t := (h v hv).2
rw [beq_eq v N ev, eP] at t
exact Bool.noConfusion t)
rw [z]
exact Nat.le_refl 0
theorem cntI_add (g : Nat -> Bool) (a : Nat) : forall b, cntI g (a + b) = cntI g a + cntI (fun r => g (a + r)) b
| 0 => rfl
| b + 1 => by
show cntI g (a + b) + bv (g (a + b)) = cntI g a + (cntI (fun r => g (a + r)) b + bv (g (a + b)))
rw [cntI_add g a b, Nat.add_assoc]
theorem cntI_ext_lt (g h : Nat -> Bool) (k : Nat) (he : forall j, j < k -> g j = h j) : cntI g k = cntI h k := by
rw [cntI_sumI, cntI_sumI]
exact sumI_ext_lt _ _ k (fun j hj => congrArg bv (he j hj))
theorem cntI_and_l (b : Bool) (h : Nat -> Bool) : forall k, cntI (fun r => b && h r) k = bv b * cntI h k
| 0 => rfl
| k + 1 => by
show cntI (fun r => b && h r) k + bv (b && h k) = bv b * (cntI h k + bv (h k))
rw [cntI_and_l b h k, Found.Pnp98.mul_add']
cases b <;> cases h k <;> rfl
/-- p is an edge variable p(i, j) with both ends in V. -/
def pairB (n : Nat) (V : Nat -> Bool) (v : Nat) : Bool :=
anyI (fun i => anyI (fun j => Nat.beq v (pv n i j) && V i && V j) n) n
theorem pairB_eval (n : Nat) (V : Nat -> Bool) (q r : Nat) (hq : q < n) (hr : r < n) :
pairB n V (pv n q r) = (V q && V r) := by
cases e : (V q && V r) with
| true =>
refine anyI_true _ q ?_ n hq
refine anyI_true _ r ?_ n hr
show (Nat.beq (pv n q r) (pv n q r) && V q && V r) = true
rw [beq_self, Found.Pnp115.and_true_l e, Found.Pnp115.and_true_r e]
rfl
| false =>
cases eb : pairB n V (pv n q r) with
| false => rfl
| true =>
cases anyI_ex _ n eb with
| intro i hi =>
cases anyI_ex _ n hi.2 with
| intro j hj =>
have h3 := hj.2
have hb : Nat.beq (pv n q r) (pv n i j) = true := and_true_l (and_true_l h3)
have vi : V i = true := and_true_r (and_true_l h3)
have vj : V j = true := and_true_r h3
have inj := var_inj n q i r j hr hj.1 (beq_eq _ _ hb)
rw [inj.1, inj.2, vi, vj] at e
exact Bool.noConfusion e
theorem pairs_q (n : Nat) (V : Nat -> Bool) : forall q, q <= n -> cntI (pairB n V) (q * n) = cntI V q * cntI V n
| 0, _ => by
show cntI (pairB n V) (0 * n) = 0 * cntI V n
exact (congrArg (cntI (pairB n V)) (Nat.zero_mul n)).trans (Nat.zero_mul _).symm
| q + 1, hq => by
rw [add_mul', one_mul', cntI_add, pairs_q n V q (Nat.le_of_succ_le hq)]
have inner : cntI (fun r => pairB n V (q * n + r)) n = bv (V q) * cntI V n := by
rw [<- cntI_and_l (V q) V n]
exact cntI_ext_lt _ _ n (fun r hr => pairB_eval n V q r hq hr)
rw [inner]
show cntI V q * cntI V n + bv (V q) * cntI V n = (cntI V q + bv (V q)) * cntI V n
rw [add_mul']
theorem pairs_count (n : Nat) (V : Nat -> Bool) : cntI (pairB n V) (n * n) = cntI V n * cntI V n :=
pairs_q n V n (Nat.le_refl n)
/-- s different edge variables with all end points in V: s <= |V|^2. -/
theorem edges_le_sq (n : Nat) (V : Nat -> Bool) (T : List Nat) (hd : Dist T)
(h : forall v, v ∈ T -> Exists fun i => Exists fun j => i < n /\ j < n /\ v = pv n i j /\ V i = true /\ V j = true) :
T.length <= cntI V n * cntI V n := by
rw [<- pairs_count n V]
refine pigeon _ (n * n) T hd (fun v hv => ?_)
cases h v hv with
| intro i hi =>
cases hi with
| intro j hj =>
rw [hj.2.2.1]
refine And.intro (Found.Pnp104.lin_lt i j n n hj.1 hj.2.1) ?_
rw [pairB_eval n V i j hj.1 hj.2.1, hj.2.2.2.1, hj.2.2.2.2]
rfl
end Found.Pnp126
#print axioms Found.Pnp126.pigeon
#print axioms Found.Pnp126.cntI_add
#print axioms Found.Pnp126.cntI_ext_lt
#print axioms Found.Pnp126.cntI_and_l
#print axioms Found.Pnp126.pairB_eval
#print axioms Found.Pnp126.pairs_q
#print axioms Found.Pnp126.pairs_count
#print axioms Found.Pnp126.edges_le_sq
#print axioms: 8 theorems, no axioms.
For a correction set \(T\) of different off-diagonal edge variables there is \(F\) with \(|T|\le (2F)^2\) such that at most \(c^{\,n-F}\) colourings give graphs missing \(T\); every colouring colours its own graph.
found/Pnp127.lean
/-
T330 piece 127. found/Pnp127 -- colourings that miss a correction set (proof complexity
ladder, step 2: the negative side of the clique-colouring count).
The negative test input of a sequence col (a colouring of the n vertices with c colours) is
the graph xneg n col with the edges p(i, j) between differently coloured vertices. An
off-diagonal edge variable v decodes to its ends (srcV, dstV). For a correction set T of
pairwise different off-diagonal edge variables, let E be its list of edges and F the number
of forced vertices of the merging labels of E (Pnp125). Then
neg_bound: |T| <= (2F)^2 and #{col in allSeq c n : xneg n col misses T} * c^F <= c^n;
neg_colouring: col is a c-colouring of xneg n col (the hypothesis of Pnp112).
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp126
namespace Found.Pnp127
open Found.Pnp65 (bv selN cntI anyI anyI_true anyI_false var_inj)
open Found.Pnp90 (allI)
open Found.Pnp96 (anyI_ex)
open Found.Pnp99 (le_ex)
open Found.Pnp115 (anyL anyL_true lsum lsum_le mem_cons_cases beq_eq beq_self and_true_l and_true_r)
open Found.Pnp116 (anyL_ex)
open Found.Pnp120 (mapL mem_mapL)
open Found.Pnp122 (missB anyL_false_all)
open Found.Pnp123 (Dist offE)
open Found.Pnp124 (allSeq allSeq_rng extF extF_at extF_lt good forced_count)
open Found.Pnp125 (labs inE fz labs_le labs_col endpoints_le srch srch_spec lt_of_blt or_cases inE_cons)
open Found.Pnp126 (edges_le_sq)
open Found.Pnp104 (pv lin_lt)
open Found.Pnp112 (Colouring)
/-! Decoding edge variables. -/
theorem split_lt (n : Nat) : forall (q v : Nat), v < q * n -> Exists fun i => Exists fun j => i < q /\ j < n /\ v = pv n i j
| 0, v, h => by rw [Nat.zero_mul] at h; exact absurd h (Nat.not_lt_zero v)
| q + 1, v, h => by
rw [Found.Pnp98.add_mul', Found.Pnp115.one_mul'] at h
cases Nat.lt_or_ge v (q * n) with
| inl l =>
cases split_lt n q v l with
| intro i hi =>
cases hi with
| intro j hj => exact Exists.intro i (Exists.intro j (And.intro (Nat.lt_succ_of_lt hj.1) hj.2))
| inr g =>
cases le_ex v (q * n) g with
| intro d hd =>
refine Exists.intro q (Exists.intro d (And.intro (Nat.lt_succ_self q) (And.intro ?_ hd)))
rw [hd] at h
exact Nat.lt_of_add_lt_add_left h
def rowB (n v i : Nat) : Bool := anyI (fun j => Nat.beq v (pv n i j)) n
def srcV (n v : Nat) : Nat := srch (rowB n v) n
def dstV (n v : Nat) : Nat := srch (fun j => Nat.beq v (pv n (srcV n v) j)) n
theorem decode (n v : Nat) (h : offE n v = true) :
srcV n v < n /\ dstV n v < n /\ v = pv n (srcV n v) (dstV n v) /\ Not (srcV n v = dstV n v) := by
have hlt : v < n * n := Found.Pnp125.lt_of_blt v (n * n) (and_true_l h)
have hnd : anyI (fun i => Nat.beq v (pv n i i)) n = false := Found.Pnp91.not_true (and_true_r h)
cases split_lt n n v hlt with
| intro i hi =>
cases hi with
| intro j hj =>
have hrow : rowB n v i = true := anyI_true _ j (by rw [hj.2.2, beq_self]) n hj.2.1
have s1 := srch_spec (rowB n v) n (anyI_true _ i hrow n hj.1)
have s2 := srch_spec (fun j => Nat.beq v (pv n (srcV n v) j)) n s1.2
have ev : v = pv n (srcV n v) (dstV n v) := beq_eq _ _ s2.2
refine And.intro s1.1 (And.intro s2.1 (And.intro ev (fun e => ?_)))
have ev' : v = pv n (srcV n v) (srcV n v) := ev.trans (congrArg (pv n (srcV n v)) e.symm)
have t : Nat.beq v (pv n (srcV n v) (srcV n v)) = true := by
rw [<- ev']
exact beq_self v
have t' := anyI_false _ n hnd (srcV n v) s1.1
rw [t] at t'
exact Bool.noConfusion t'
/-! The negative test inputs. -/
def xneg (n : Nat) (col : Nat -> Nat) (v : Nat) : Bool :=
anyI (fun i => anyI (fun j => Nat.beq v (pv n i j) && !(Nat.beq (col i) (col j))) n) n
theorem xneg_false (n : Nat) (col : Nat -> Nat) (i j : Nat) (hi : i < n) (hj : j < n) (h : xneg n col (pv n i j) = false) :
col i = col j := by
have t := anyI_false _ n (anyI_false _ n h i hi) j hj
have t' : (Nat.beq (pv n i j) (pv n i j) && !(Nat.beq (col i) (col j))) = false := t
rw [beq_self] at t'
cases e : Nat.beq (col i) (col j) with
| true => exact beq_eq _ _ e
| false => rw [e] at t'; exact Bool.noConfusion t'
theorem neg_colouring (n c : Nat) (col : Nat -> Nat) (hc : col ∈ allSeq c n) : Colouring n c col (xneg n col) := by
refine Colouring.mk (fun i hi => allSeq_rng c n col hc i hi) (fun i j hi hj he hcol => ?_)
cases anyI_ex _ n he with
| intro i' hi' =>
cases anyI_ex _ n hi'.2 with
| intro j' hj' =>
have inj := var_inj n i i' j j' hj hj'.1 (beq_eq _ _ (and_true_l hj'.2))
have t := and_true_r hj'.2
rw [<- inj.1, <- inj.2, hcol, beq_self] at t
exact Bool.noConfusion t
def edgesOf (n : Nat) (T : List Nat) : List (Nat × Nat) := mapL (fun v => (srcV n v, dstV n v)) T
theorem mem_mapL_inv {α β : Type} (f : α -> β) (b : β) : forall L : List α, b ∈ mapL f L -> Exists fun a => a ∈ L /\ b = f a
| [], h => nomatch h
| a :: L, h => by
cases mem_cons_cases h with
| inl q => exact Exists.intro a (And.intro (List.Mem.head L) q)
| inr h2 =>
cases mem_mapL_inv f b L h2 with
| intro a' ha => exact Exists.intro a' (And.intro (List.Mem.tail a ha.1) ha.2)
theorem allI_of (g : Nat -> Bool) : forall k, (forall p, p < k -> g p = true) -> allI g k = true
| 0, _ => rfl
| k + 1, h => by
show (allI g k && g k) = true
rw [allI_of g k (fun p hp => h p (Nat.lt_succ_of_lt hp)), h k (Nat.lt_succ_self k)]
rfl
def tst (E : List (Nat × Nat)) (p : Nat) (f : Nat -> Nat) : Bool := !(fz E p) || Nat.beq (f p) (f (labs E p))
theorem neg_bound (n c : Nat) (T : List Nat) (hT : forall v, v ∈ T -> offE n v = true) (hd : Dist T) :
Exists fun F => T.length <= (2 * F) * (2 * F) /\
lsum (fun col => bv (missB (xneg n col) T)) (allSeq c n) * c ^ F <= c ^ n := by
let E := edgesOf n T
have hE : forall e, e ∈ E -> Not (e.1 = e.2) /\ e.1 < n /\ e.2 < n := by
intro e he
cases mem_mapL_inv _ e T he with
| intro v hv =>
have dv := decode n v (hT v hv.1)
rw [hv.2]
exact And.intro dv.2.2.2 (And.intro dv.1 dv.2.1)
refine Exists.intro (cntI (fz E) n) (And.intro ?_ ?_)
· have hV := endpoints_le E n hE
refine Nat.le_trans ?_ (Nat.mul_le_mul hV hV)
refine edges_le_sq n (inE E) T hd (fun v hv => ?_)
have dv := decode n v (hT v hv)
have hmem : (srcV n v, dstV n v) ∈ E := mem_mapL _ v T hv
refine Exists.intro (srcV n v) (Exists.intro (dstV n v) (And.intro dv.1 (And.intro dv.2.1 (And.intro dv.2.2.1 ?_))))
refine And.intro ?_ ?_
· refine anyL_true _ (srcV n v, dstV n v) ?_ E hmem
show (Nat.beq (srcV n v) (srcV n v) || Nat.beq (srcV n v) (dstV n v)) = true
rw [beq_self]; rfl
· refine anyL_true _ (srcV n v, dstV n v) ?_ E hmem
show (Nat.beq (dstV n v) (srcV n v) || Nat.beq (dstV n v) (dstV n v)) = true
rw [beq_self]; exact Found.Pnp65.or_true_r _
· have loc : forall p f f', (forall i, i <= p -> f i = f' i) -> tst E p f = tst E p f' := by
intro p f f' hf
show (!(fz E p) || Nat.beq (f p) (f (labs E p))) = (!(fz E p) || Nat.beq (f' p) (f' (labs E p)))
rw [hf p (Nat.le_refl p), hf (labs E p) (labs_le E p)]
have uniq : forall p f, fz E p = true -> forall v w, tst E p (extF f p v) = true -> tst E p (extF f p w) = true -> v = w := by
intro p f hp v w hv hw
have hl : labs E p < p := lt_of_blt _ _ hp
have hv' : (!(fz E p) || Nat.beq (extF f p v p) (extF f p v (labs E p))) = true := hv
have hw' : (!(fz E p) || Nat.beq (extF f p w p) (extF f p w (labs E p))) = true := hw
rw [hp, extF_at, extF_lt f p v _ hl] at hv'
rw [hp, extF_at, extF_lt f p w _ hl] at hw'
exact (beq_eq _ _ hv').trans (beq_eq _ _ hw').symm
have fc := forced_count c (tst E) (fz E) loc uniq n
refine Nat.le_trans (Found.Pnp98.mul_le_r ?_ _) fc
refine lsum_le _ _ _ (fun col _ => Found.Pnp65.bv_le _ _ (fun hm => ?_))
have hmiss : anyL (xneg n col) T = false := Found.Pnp91.not_true hm
have hedge : forall e, e ∈ E -> col e.1 = col e.2 := by
intro e he
cases mem_mapL_inv _ e T he with
| intro v hv =>
have dv := decode n v (hT v hv.1)
have xf := anyL_false_all _ T hmiss v hv.1
rw [dv.2.2.1] at xf
rw [hv.2]
exact xneg_false n col _ _ dv.1 dv.2.1 xf
refine allI_of _ n (fun p _ => ?_)
show (!(fz E p) || Nat.beq (col p) (col (labs E p))) = true
rw [<- labs_col col E hedge p, beq_self]
exact Found.Pnp65.or_true_r _
end Found.Pnp127
#print axioms Found.Pnp127.split_lt
#print axioms Found.Pnp127.decode
#print axioms Found.Pnp127.xneg_false
#print axioms Found.Pnp127.neg_colouring
#print axioms Found.Pnp127.mem_mapL_inv
#print axioms Found.Pnp127.allI_of
#print axioms Found.Pnp127.neg_bound
#print axioms: 7 theorems, no axioms.
For \(n=kb\) vertices in \(k\) blocks of \(b\), a sequence \(\sigma \in [b]^k\) picks one vertex per block; its clique \(x_\sigma \) is a \(k\)-clique, and a correction set \(T\) with end point set \(V\) is covered by few of them.
found/Pnp128.lean
/-
T330 piece 128. found/Pnp128 -- transversal cliques that cover a correction set (proof
complexity ladder, step 2: the positive side of the clique-colouring count).
The n = k b vertices form k blocks of b. A sequence sigma in allSeq b k chooses the vertex
l b + sigma l in block l; the positive test input xpos is the clique on the chosen vertices.
For a correction set T of different off-diagonal edge variables, with end point set V (of
size |V| = cntI V n), proved:
* pos_clique: the chosen vertices form a k-clique of xpos (the hypothesis of Pnp112);
* pos_cover: |T| <= |V|^2 and #{sigma : xpos sigma covers T} * b^|V| <= b^k (if two end
points share a block nothing covers T; otherwise every block of an end point is forced);
* pos_one: #{sigma : xpos sigma v = true} * b^2 <= b^k for an off-diagonal v.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp127
namespace Found.Pnp128
open Found.Pnp65 (bv selN cntI anyI anyI_true anyI_false var_inj or_true_r bv_le)
open Found.Pnp90 (allI)
open Found.Pnp95 (inj_count)
open Found.Pnp96 (anyI_ex)
open Found.Pnp115 (anyL anyL_true lsum lsum_le mem_cons_cases beq_eq beq_self and_true_l and_true_r)
open Found.Pnp116 (allL allL_mem anyL_ex)
open Found.Pnp120 (mapL mem_mapL)
open Found.Pnp123 (Dist offE)
open Found.Pnp124 (allSeq allSeq_rng extF extF_at extF_lt good forced_count)
open Found.Pnp125 (inE srch srch_spec or_cases)
open Found.Pnp126 (edges_le_sq pigeon)
open Found.Pnp127 (decode srcV dstV edgesOf mem_mapL_inv allI_of split_lt)
open Found.Pnp104 (pv lin_lt)
open Found.Pnp112 (Clique)
def xpos (n b k : Nat) (sg : Nat -> Nat) (v : Nat) : Bool :=
anyI (fun l => anyI (fun l' => Nat.beq v (pv n (l * b + sg l) (l' * b + sg l')) && !(Nat.beq l l')) k) k
theorem vtx_lt (b k l w : Nat) (hl : l < k) (hw : w < b) : l * b + w < k * b := lin_lt l w k b hl hw
theorem pos_clique (b k : Nat) (sg : Nat -> Nat) (hs : sg ∈ allSeq b k) :
Clique (k * b) k (fun l => l * b + sg l) (xpos (k * b) b k sg) := by
have rg := allSeq_rng b k sg hs
refine Clique.mk (fun l hl => vtx_lt b k l _ hl (rg l hl)) (fun l l' hl hl' he => (var_inj b l l' _ _ (rg l hl) (rg l' hl') he).1)
(fun l l' hll hl' => ?_)
have hl : l < k := Nat.lt_trans hll hl'
refine anyI_true _ l ?_ k hl
refine anyI_true _ l' ?_ k hl'
show (Nat.beq (pv (k * b) (l * b + sg l) (l' * b + sg l')) (pv (k * b) (l * b + sg l) (l' * b + sg l')) && !(Nat.beq l l')) = true
rw [beq_self]
cases e : Nat.beq l l' with
| true => rw [beq_eq l l' e] at hll; exact absurd hll (Nat.lt_irrefl l')
| false => rfl
/-- If xpos sigma has the edge p(i, j), both ends are chosen vertices. -/
theorem pos_ends (b k : Nat) (sg : Nat -> Nat) (i j : Nat) (_hi : i < k * b) (hj : j < k * b) (hs : sg ∈ allSeq b k)
(h : xpos (k * b) b k sg (pv (k * b) i j) = true) :
(Exists fun l => l < k /\ i = l * b + sg l) /\ (Exists fun l => l < k /\ j = l * b + sg l) := by
have rg := allSeq_rng b k sg hs
cases anyI_ex _ k h with
| intro l hl =>
cases anyI_ex _ k hl.2 with
| intro l' hl' =>
have inj := var_inj (k * b) i _ j _ hj (vtx_lt b k l' _ hl'.1 (rg l' hl'.1)) (beq_eq _ _ (and_true_l hl'.2))
exact And.intro (Exists.intro l (And.intro hl.1 inj.1)) (Exists.intro l' (And.intro hl'.1 inj.2))
/-! Covering a correction set. -/
def tP (b : Nat) (V : Nat -> Bool) (l : Nat) (sg : Nat -> Nat) : Bool :=
allI (fun w => !(V (l * b + w)) || Nat.beq (sg l) w) b
def forcedB (b : Nat) (V : Nat -> Bool) (l : Nat) : Bool := anyI (fun w => V (l * b + w)) b
def twoB (b k : Nat) (V : Nat -> Bool) : Bool :=
anyI (fun l => anyI (fun w => anyI (fun w' => V (l * b + w) && V (l * b + w') && !(Nat.beq w w')) b) b) k
theorem pow_le_exp (b : Nat) (hb : 1 <= b) : forall (a c : Nat), a <= c -> b ^ a <= b ^ c
| a, 0, h => by
cases a with
| zero => exact Nat.le_refl 1
| succ a => exact absurd h (Nat.not_succ_le_zero a)
| a, c + 1, h => by
cases Found.Pnp115.lt_succ_cases (Nat.lt_succ_of_le h) with
| inl l =>
have t := pow_le_exp b hb a c (Nat.le_of_lt_succ l)
show b ^ a <= b ^ c * b
exact Nat.le_trans t (Nat.le_trans (Nat.le_of_eq (Found.Pnp114.mul_one' _).symm) (Found.Pnp99.mul_le_l' hb _))
| inr e => rw [e]; exact Nat.le_refl _
theorem allI_mem (g : Nat -> Bool) : forall k, allI g k = true -> forall p, p < k -> g p = true
| 0, _, p, hp => absurd hp (Nat.not_lt_zero p)
| k + 1, h, p, hp => by
cases Found.Pnp115.lt_succ_cases hp with
| inl l => exact allI_mem g k (and_true_l h) p l
| inr e => rw [e]; exact and_true_r h
/-- The end point set V of T, and the covering count. -/
theorem pos_cover (b k : Nat) (hb : 1 <= b) (T : List Nat) (hT : forall v, v ∈ T -> offE (k * b) v = true) (hd : Dist T) :
T.length <= cntI (inE (edgesOf (k * b) T)) (k * b) * cntI (inE (edgesOf (k * b) T)) (k * b) /\
lsum (fun sg => bv (allL (xpos (k * b) b k sg) T)) (allSeq b k) * b ^ (cntI (inE (edgesOf (k * b) T)) (k * b)) <= b ^ k := by
let n := k * b
let E := edgesOf n T
let V := inE E
refine And.intro ?_ ?_
· refine edges_le_sq n V T hd (fun v hv => ?_)
have dv := decode n v (hT v hv)
have hmem : (srcV n v, dstV n v) ∈ E := mem_mapL _ v T hv
refine Exists.intro (srcV n v) (Exists.intro (dstV n v) (And.intro dv.1 (And.intro dv.2.1 (And.intro dv.2.2.1 (And.intro ?_ ?_)))))
· refine anyL_true _ (srcV n v, dstV n v) ?_ E hmem
show (Nat.beq (srcV n v) (srcV n v) || Nat.beq (srcV n v) (dstV n v)) = true
rw [beq_self]; rfl
· refine anyL_true _ (srcV n v, dstV n v) ?_ E hmem
show (Nat.beq (dstV n v) (srcV n v) || Nat.beq (dstV n v) (dstV n v)) = true
rw [beq_self]; exact or_true_r _
· -- every end point of a covered T is a chosen vertex
have chosen : forall sg, sg ∈ allSeq b k -> allL (xpos n b k sg) T = true -> forall u, V u = true ->
Exists fun l => l < k /\ u = l * b + sg l := by
intro sg hs hc u hu
cases anyL_ex _ E hu with
| intro e he =>
cases mem_mapL_inv _ e T he.1 with
| intro v hv =>
have dv := decode n v (hT v hv.1)
have xv := allL_mem _ T v hc hv.1
rw [dv.2.2.1] at xv
have ends := pos_ends b k sg _ _ dv.1 dv.2.1 hs xv
have he2 := he.2
rw [hv.2] at he2
cases or_cases he2 with
| inl q => rw [beq_eq _ _ q]; exact ends.1
| inr q => rw [beq_eq _ _ q]; exact ends.2
cases etw : twoB b k V with
| true =>
-- two end points in one block: nothing covers T
have z : lsum (fun sg => bv (allL (xpos n b k sg) T)) (allSeq b k) = 0 := by
refine Found.Pnp115.lsum_zero_of _ _ (fun sg hs => ?_)
cases ec : allL (xpos n b k sg) T with
| false => rfl
| true =>
have rg := allSeq_rng b k sg hs
cases anyI_ex _ k etw with
| intro l hl =>
cases anyI_ex _ b hl.2 with
| intro w hw =>
cases anyI_ex _ b hw.2 with
| intro w' hw' =>
have h3 := hw'.2
have c1 := chosen sg hs ec _ (and_true_l (and_true_l h3))
have c2 := chosen sg hs ec _ (and_true_r (and_true_l h3))
cases c1 with
| intro l1 h1 =>
cases c2 with
| intro l2 h2 =>
have i1 := var_inj b l l1 w (sg l1) hw.1 (rg l1 h1.1) h1.2
have i2 := var_inj b l l2 w' (sg l2) hw'.1 (rg l2 h2.1) h2.2
have eww : w = w' := by rw [i1.2, i2.2, <- i1.1, <- i2.1]
have t := and_true_r h3
rw [eww, beq_self] at t
exact Bool.noConfusion t
rw [z, Found.Pnp97.zero_mul']
exact Nat.zero_le _
| false =>
have loc : forall p f f', (forall i, i <= p -> f i = f' i) -> tP b V p f = tP b V p f' := by
intro p f f' hf
show allI (fun w => !(V (p * b + w)) || Nat.beq (f p) w) b = allI (fun w => !(V (p * b + w)) || Nat.beq (f' p) w) b
rw [hf p (Nat.le_refl p)]
have uniq : forall p f, forcedB b V p = true -> forall v w, tP b V p (extF f p v) = true -> tP b V p (extF f p w) = true -> v = w := by
intro p f hp v w hv hw
cases anyI_ex _ b hp with
| intro w0 hw0 =>
have a1 := allI_mem _ b hv w0 hw0.1
have a2 := allI_mem _ b hw w0 hw0.1
have a1' : (!(V (p * b + w0)) || Nat.beq (extF f p v p) w0) = true := a1
have a2' : (!(V (p * b + w0)) || Nat.beq (extF f p w p) w0) = true := a2
rw [hw0.2, extF_at] at a1'
rw [hw0.2, extF_at] at a2'
exact (beq_eq _ _ a1').trans (beq_eq _ _ a2').symm
have fc := forced_count b (tP b V) (forcedB b V) loc uniq k
-- end points inject into forced blocks
have hinj : cntI V n <= cntI (forcedB b V) k := by
let blk : Nat -> Nat := fun u => srch (fun l => anyI (fun w => Nat.beq u (l * b + w)) b) k
have dec : forall u, u < n -> Exists fun w => w < b /\ blk u < k /\ u = blk u * b + w := by
intro u hu
cases split_lt b k u hu with
| intro l hl =>
cases hl with
| intro w hw =>
have hrow : anyI (fun w' => Nat.beq u (l * b + w')) b = true := anyI_true _ w (by rw [hw.2.2]; exact beq_self _) b hw.2.1
have sp := srch_spec (fun l => anyI (fun w => Nat.beq u (l * b + w)) b) k (anyI_true _ l hrow k hw.1)
cases anyI_ex _ b sp.2 with
| intro w' hw' => exact Exists.intro w' (And.intro hw'.1 (And.intro sp.1 (beq_eq _ _ hw'.2)))
refine inj_count V blk k n (forcedB b V) (fun u hu hV => ?_) (fun u u' hu hu' hV hV' he => ?_)
· cases dec u hu with
| intro w hw =>
refine And.intro hw.2.1 (anyI_true _ w ?_ b hw.1)
rw [<- hw.2.2]; exact hV
· cases dec u hu with
| intro w hw =>
cases dec u' hu' with
| intro w' hw' =>
have hu2 := hw.2.2
have hu2' := hw'.2.2
rw [he] at hu2
cases Nat.decEq w w' with
| isTrue q => exact hu2.trans (by rw [q]; exact hu2'.symm)
| isFalse q =>
have t : twoB b k V = true := by
refine anyI_true _ (blk u') ?_ k hw'.2.1
refine anyI_true _ w ?_ b hw.1
refine anyI_true _ w' ?_ b hw'.1
rw [<- hu2, <- hu2', hV, hV']
cases e : Nat.beq w w' with
| true => exact absurd (beq_eq _ _ e) q
| false => rfl
rw [etw] at t
exact Bool.noConfusion t
refine Nat.le_trans (Found.Pnp99.mul_le_l' (pow_le_exp b hb _ _ hinj) _) ?_
refine Nat.le_trans (Found.Pnp98.mul_le_r ?_ _) fc
refine lsum_le _ _ _ (fun sg hs => bv_le _ _ (fun hc => ?_))
have rg := allSeq_rng b k sg hs
refine allI_of _ k (fun l hl => allI_of _ b (fun w hw => ?_))
show (!(V (l * b + w)) || Nat.beq (sg l) w) = true
cases eV : V (l * b + w) with
| false => rfl
| true =>
cases chosen sg hs hc _ eV with
| intro l' hl' =>
have inj := var_inj b l l' w (sg l') hw (rg l' hl'.1) hl'.2
rw [inj.1, inj.2, beq_self]
rfl
/-- One off-diagonal edge variable: at most b^(k-2) transversal cliques contain it. -/
theorem pos_one (b k : Nat) (hb : 1 <= b) (v : Nat) (hv : offE (k * b) v = true) :
lsum (fun sg => bv (xpos (k * b) b k sg v)) (allSeq b k) * (b * b) <= b ^ k := by
have pc := pos_cover b k hb [v] (fun w hw => by
cases mem_cons_cases hw with
| inl q => rw [q]; exact hv
| inr h2 => exact nomatch h2) (And.intro (fun _ h => nomatch h) True.intro)
have dv := decode (k * b) v hv
let V := inE (edgesOf (k * b) [v])
have two : 2 <= cntI V (k * b) := by
have p := pigeon V (k * b) [srcV (k * b) v, dstV (k * b) v]
(And.intro (fun w hw e => ?_) (And.intro (fun _ h => nomatch h) True.intro)) (fun w hw => ?_)
· exact p
· cases mem_cons_cases hw with
| inl q => exact dv.2.2.2 (e.trans q)
| inr h2 => exact nomatch h2
· cases mem_cons_cases hw with
| inl q =>
rw [q]
refine And.intro dv.1 ?_
show anyL (fun e => Nat.beq (srcV (k * b) v) e.1 || Nat.beq (srcV (k * b) v) e.2) [(srcV (k * b) v, dstV (k * b) v)] = true
refine anyL_true _ _ ?_ _ (List.Mem.head _)
show (Nat.beq (srcV (k * b) v) (srcV (k * b) v) || _) = true
rw [beq_self]; rfl
| inr h2 =>
cases mem_cons_cases h2 with
| inl q =>
rw [q]
refine And.intro dv.2.1 ?_
refine anyL_true _ _ ?_ _ (List.Mem.head _)
show (_ || Nat.beq (dstV (k * b) v) (dstV (k * b) v)) = true
rw [beq_self]; exact or_true_r _
| inr h3 => exact nomatch h3
have e1 : lsum (fun sg => bv (xpos (k * b) b k sg v)) (allSeq b k) =
lsum (fun sg => bv (allL (xpos (k * b) b k sg) [v])) (allSeq b k) := by
refine Nat.le_antisymm (lsum_le _ _ _ (fun sg _ => ?_)) (lsum_le _ _ _ (fun sg _ => ?_))
· show bv (xpos (k * b) b k sg v) <= bv (xpos (k * b) b k sg v && true)
cases xpos (k * b) b k sg v <;> exact Nat.le_refl _
· show bv (xpos (k * b) b k sg v && true) <= bv (xpos (k * b) b k sg v)
cases xpos (k * b) b k sg v <;> exact Nat.le_refl _
rw [e1]
refine Nat.le_trans (Found.Pnp99.mul_le_l' ?_ _) pc.2
refine Nat.le_trans (Nat.le_of_eq ?_) (pow_le_exp b hb 2 _ two)
show b * b = 1 * b * b
rw [Found.Pnp115.one_mul' b]
end Found.Pnp128
#print axioms Found.Pnp128.vtx_lt
#print axioms Found.Pnp128.pos_clique
#print axioms Found.Pnp128.pos_ends
#print axioms Found.Pnp128.pow_le_exp
#print axioms Found.Pnp128.allI_mem
#print axioms Found.Pnp128.pos_cover
#print axioms Found.Pnp128.pos_one
#print axioms: 7 theorems, no axioms.
A monotone real circuit reading only off-diagonal edge variables that outputs 1 on every transversal clique and 0 on every colouring graph satisfies one of two counting inequalities, for \(2\le s,r\) and \(f,g\) with \(s\le (2F)^2\Rightarrow f\le F\), \(r\le V^2\Rightarrow g\le V\).
found/Pnp129.lean
/-
T330 piece 129. found/Pnp129 -- the clique-colouring count plugged into the criterion
(proof complexity ladder, step 2: monotone real circuits on clique-colouring test inputs).
Let n = k b. A monotone real circuit C whose input gates read only off-diagonal edge
variables (InpOK (offE n) C), which outputs 1 on every transversal clique xpos sigma
(sigma in allSeq b k) and 0 on every colouring graph xneg col (col in allSeq c n), satisfies,
for 2 <= s, r and any f, g with s <= (2F)^2 -> f <= F and r <= V^2 -> g <= V:
c^f <= |C| (2r)^s or b^2 b^g <= s b^g + |C| (2s)^r b^2.
Proved:
* hit_le: a set q is hit at most as often as the sum of its single variables;
* combine: the arithmetic of the positive side, with atoms only;
* neg_side, pos_side: the two branches of Pnp122.criterion_count with Pnp127.neg_bound and
Pnp128.pos_cover / pos_one;
* mrc_bound: the bound above.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp128
namespace Found.Pnp129
open Found.Pnp65 (bv)
open Found.Pnp98 (mul_le_r add_mul')
open Found.Pnp99 (mul_le_l' pow_pos' mul_assoc')
open Found.Pnp101 (cancel_r)
open Found.Pnp115 (anyL lsum lsum_le lsum_const_le)
open Found.Pnp116 (allL)
open Found.Pnp120 (mapL apx nthA allKL allKR)
open Found.Pnp121 (circuit_sizes out_clauses)
open Found.Pnp122 (missB criterion_count lsum_swap)
open Found.Pnp123 (InpOK offE allKL_vars allKR_vars out_vars allKL_distinct allKR_distinct)
open Found.Pnp124 (allSeq allSeq_len lsum_mapL lsum_mul_r len_mapL)
open Found.Pnp127 (xneg neg_bound mem_mapL_inv)
open Found.Pnp128 (xpos pos_cover pos_one pow_le_exp)
open Found.Pnp113 (Gate outV)
open Found.Pnp109 (out)
theorem hit_le (x : Nat -> Bool) : forall q : List Nat, bv (anyL x q) <= lsum (fun v => bv (x v)) q
| [] => Nat.le_refl 0
| v :: q => by
show bv (x v || anyL x q) <= bv (x v) + lsum (fun v => bv (x v)) q
cases x v with
| true => exact Nat.le_add_right 1 _
| false => exact Nat.le_trans (hit_le x q) (Nat.le_of_eq (Nat.zero_add _).symm)
theorem swap23 (a x y : Nat) : (a * x) * y = (a * y) * x := by
rw [mul_assoc' a x y, mul_assoc' a y x, Nat.mul_comm x y]
/-- The arithmetic of the positive side. -/
theorem combine (P H S s M bb G : Nat) (hpos : 1 <= P) (hP : P <= H + S) (hH : H * bb <= s * P)
(hS : S * G <= M * P) : bb * G <= s * G + M * bb := by
have e1 : H * (bb * G) <= (s * G) * P :=
calc H * (bb * G) = (H * bb) * G := (mul_assoc' H bb G).symm
_ <= (s * P) * G := mul_le_r hH G
_ = (s * G) * P := swap23 s P G
have e2 : S * (bb * G) <= (M * bb) * P :=
calc S * (bb * G) = S * (G * bb) := congrArg (S * ·) (Nat.mul_comm bb G)
_ = (S * G) * bb := (mul_assoc' S G bb).symm
_ <= (M * P) * bb := mul_le_r hS bb
_ = (M * bb) * P := swap23 M P bb
refine cancel_r hpos ?_
calc (bb * G) * P = P * (bb * G) := Nat.mul_comm _ _
_ <= (H + S) * (bb * G) := mul_le_r hP _
_ = H * (bb * G) + S * (bb * G) := add_mul' _ _ _
_ <= (s * G) * P + (M * bb) * P := Nat.add_le_add e1 e2
_ = (s * G + M * bb) * P := (add_mul' _ _ _).symm
/-- The negative branch: every colouring misses some correction set. -/
theorem neg_side (n c s r f : Nat) (hc : 1 <= c) (hs : 2 <= s) (hr : 2 <= r) (C : List Gate)
(hC : InpOK (offE n) C) (X : List (Nat -> Bool))
(hf : forall F, s <= (2 * F) * (2 * F) -> f <= F)
(h : (mapL (xneg n) (allSeq c n)).length <=
lsum (fun T => lsum (fun x => bv (missB x T)) (mapL (xneg n) (allSeq c n))) (allKL (apx s r X C))) :
c ^ f <= C.length * (2 * r) ^ s := by
have cs := circuit_sizes s r hs hr X C
rw [len_mapL, allSeq_len] at h
have hT : forall T, T ∈ allKL (apx s r X C) ->
lsum (fun x => bv (missB x T)) (mapL (xneg n) (allSeq c n)) * c ^ f <= c ^ n := by
intro T hT
rw [lsum_mapL]
cases neg_bound n c T (allKL_vars (offE n) s r X C hC T hT) (allKL_distinct s r X C T hT) with
| intro F hF =>
have fF : f <= F := hf F (Nat.le_trans (Nat.le_of_eq (cs.2.2.1 T hT).symm) hF.1)
exact Nat.le_trans (mul_le_l' (pow_le_exp c hc f F fF) _) hF.2
have t1 := lsum_const_le (fun T => lsum (fun x => bv (missB x T)) (mapL (xneg n) (allSeq c n)) * c ^ f)
(c ^ n) (allKL (apx s r X C)) hT
have t2 : c ^ n * c ^ f <= (C.length * (2 * r) ^ s) * c ^ n :=
Nat.le_trans (mul_le_r h (c ^ f))
(Nat.le_trans (Nat.le_of_eq (lsum_mul_r (fun T => lsum (fun x => bv (missB x T)) (mapL (xneg n) (allSeq c n)))
(c ^ f) (allKL (apx s r X C))).symm) (Nat.le_trans t1 (mul_le_r cs.1 _)))
rw [Nat.mul_comm] at t2
exact cancel_r (pow_pos' hc n) t2
/-- The positive branch: every transversal clique hits q or covers some correction set. -/
theorem pos_side (b k s r g : Nat) (hb : 1 <= b) (hs : 2 <= s) (hr : 2 <= r) (C : List Gate)
(hC : InpOK (offE (k * b)) C) (X : List (Nat -> Bool))
(hg : forall V, r <= V * V -> g <= V)
(q : List Nat) (hq : q ∈ (nthA (apx s r X C) 0).L (0, 0))
(h : (mapL (xpos (k * b) b k) (allSeq b k)).length <=
lsum (fun x => bv (anyL x q)) (mapL (xpos (k * b) b k) (allSeq b k)) +
lsum (fun T => lsum (fun x => bv (allL x T)) (mapL (xpos (k * b) b k) (allSeq b k))) (allKR (apx s r X C))) :
(b * b) * b ^ g <= s * b ^ g + (C.length * (2 * s) ^ r) * (b * b) := by
have cs := circuit_sizes s r hs hr X C
rw [len_mapL, allSeq_len] at h
have hH : lsum (fun x => bv (anyL x q)) (mapL (xpos (k * b) b k) (allSeq b k)) * (b * b) <= s * b ^ k := by
rw [lsum_mapL]
have a1 : lsum (fun sg => bv (anyL (xpos (k * b) b k sg) q)) (allSeq b k) <=
lsum (fun v => lsum (fun sg => bv (xpos (k * b) b k sg v)) (allSeq b k)) q :=
Nat.le_trans
(lsum_le (fun sg => bv (anyL (xpos (k * b) b k sg) q))
(fun sg => lsum (fun v => bv (xpos (k * b) b k sg v)) q) (allSeq b k)
(fun sg _ => hit_le (xpos (k * b) b k sg) q))
(Nat.le_of_eq (lsum_swap (fun sg v => bv (xpos (k * b) b k sg v)) q (allSeq b k)))
refine Nat.le_trans (mul_le_r a1 (b * b)) ?_
refine Nat.le_trans (Nat.le_of_eq (lsum_mul_r (fun v => lsum (fun sg => bv (xpos (k * b) b k sg v)) (allSeq b k))
(b * b) q).symm) ?_
refine Nat.le_trans (lsum_const_le (fun v => lsum (fun sg => bv (xpos (k * b) b k sg v)) (allSeq b k) * (b * b))
(b ^ k) q (fun v hv => pos_one b k hb v (out_vars (offE (k * b)) s r X C hC q hq v hv))) ?_
exact mul_le_r (Nat.le_trans (Nat.le_succ _) (out_clauses s r hs hr X C q hq)) _
have hS : lsum (fun T => lsum (fun x => bv (allL x T)) (mapL (xpos (k * b) b k) (allSeq b k))) (allKR (apx s r X C)) * b ^ g
<= (C.length * (2 * s) ^ r) * b ^ k := by
refine Nat.le_trans (Nat.le_of_eq (lsum_mul_r (fun T => lsum (fun x => bv (allL x T)) (mapL (xpos (k * b) b k) (allSeq b k)))
(b ^ g) (allKR (apx s r X C))).symm) ?_
refine Nat.le_trans (lsum_const_le (fun T => lsum (fun x => bv (allL x T)) (mapL (xpos (k * b) b k) (allSeq b k)) * b ^ g)
(b ^ k) (allKR (apx s r X C)) (fun T hT => ?_)) (mul_le_r cs.2.1 _)
show lsum (fun x => bv (allL x T)) (mapL (xpos (k * b) b k) (allSeq b k)) * b ^ g <= b ^ k
rw [lsum_mapL]
have pc := pos_cover b k hb T (allKR_vars (offE (k * b)) s r X C hC T hT) (allKR_distinct s r X C T hT)
have gV := hg _ (Nat.le_trans (Nat.le_of_eq (cs.2.2.2 T hT).symm) pc.1)
exact Nat.le_trans (mul_le_l' (pow_le_exp b hb _ _ gV) _) pc.2
exact combine (b ^ k) _ _ s _ (b * b) (b ^ g) (pow_pos' hb k) h hH hS
/-- Monotone real circuits on clique-colouring test inputs. -/
theorem mrc_bound (k b c s r f g : Nat) (hb : 1 <= b) (hc : 1 <= c) (hs : 2 <= s) (hr : 2 <= r)
(C : List Gate) (hC : InpOK (offE (k * b)) C)
(hP : forall sg, sg ∈ allSeq b k -> out (outV (xpos (k * b) b k sg) C) = true)
(hN : forall col, col ∈ allSeq c (k * b) -> out (outV (xneg (k * b) col) C) = false)
(hf : forall F, s <= (2 * F) * (2 * F) -> f <= F)
(hg : forall V, r <= V * V -> g <= V) :
c ^ f <= C.length * (2 * r) ^ s \/
(b * b) * b ^ g <= s * b ^ g + (C.length * (2 * s) ^ r) * (b * b) := by
have hP' : forall x, x ∈ mapL (xpos (k * b) b k) (allSeq b k) -> out (outV x C) = true := by
intro x hx
cases mem_mapL_inv _ x _ hx with
| intro sg hsg => rw [hsg.2]; exact hP sg hsg.1
have hN' : forall x, x ∈ mapL (xneg (k * b)) (allSeq c (k * b)) -> out (outV x C) = false := by
intro x hx
cases mem_mapL_inv _ x _ hx with
| intro col hcol => rw [hcol.2]; exact hN col hcol.1
cases criterion_count s r C _ _ hP' hN' with
| inl h => exact Or.inl (neg_side (k * b) c s r f hc hs hr C hC _ hf h)
| inr h =>
cases h with
| intro q hq => exact Or.inr (pos_side b k s r g hb hs hr C hC _ hg q hq.1 hq.2)
end Found.Pnp129
#print axioms Found.Pnp129.hit_le
#print axioms Found.Pnp129.swap23
#print axioms Found.Pnp129.combine
#print axioms Found.Pnp129.neg_side
#print axioms Found.Pnp129.pos_side
#print axioms Found.Pnp129.mrc_bound
#print axioms: 6 theorems, no axioms.
With \(s=r=(2t)^2\), \(f=g=t\), \(D=2s\), \(b=c=D^{2(2t)+t}\), \(k=b+1\) and \(n=kb\), both inequalities give the same size bound.
found/Pnp130.lean
/-
T330 piece 130. found/Pnp130 -- concrete parameters for the monotone real circuit bound
(proof complexity ladder, step 2).
For t >= 1 put s = (2t)^2, D = 2 s, b = c = D^(2(2t) + t), k = b + 1 and n = k b. Then
s <= (2F)^2 forces t <= F, b^t = D^s D^(t t) and D <= b b, so Pnp129.mrc_bound with
r = s and f = g = t gives, for every monotone real circuit C reading only off-diagonal edge
variables that outputs 1 on every transversal k-clique and 0 on every b-colouring graph,
D^(t t) <= 2 |C|.
Since n <= D^(2 (2(2t) + t) + 1), this is |C| >= 2^(Omega((log n)^2 / (log log n)^2)).
Proved: sq_lt, sq_floor, sq_floor1, le_two_mul, pow_add', pow_pow, key_exp, b_pow, branch2, mrc_lower.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp129
namespace Found.Pnp130
open Found.Pnp98 (mul_le_r add_mul' mul_add')
open Found.Pnp99 (mul_le_l' pow_pos' mul_assoc')
open Found.Pnp101 (cancel_r two_mul' add_cancel_l')
open Found.Pnp114 (mul_one')
open Found.Pnp115 (one_mul')
open Found.Pnp123 (InpOK offE)
open Found.Pnp124 (allSeq)
open Found.Pnp127 (xneg)
open Found.Pnp128 (xpos pow_le_exp)
open Found.Pnp113 (Gate outV)
open Found.Pnp109 (out)
theorem sq_lt (u v : Nat) (h : u + 1 <= v) : u * u < v * v := by
have a : u * u <= (u + 1) * u := mul_le_r (Nat.le_succ u) u
have b : (u + 1) * u + 1 <= (u + 1) * (u + 1) :=
(Nat.add_le_add_left (Nat.succ_le_succ (Nat.zero_le u)) _ : (u + 1) * u + 1 <= (u + 1) * u + (u + 1))
have c : (u + 1) * (u + 1) <= v * v := Nat.le_trans (mul_le_r h (u + 1)) (mul_le_l' h v)
exact Nat.lt_of_lt_of_le (Nat.lt_of_le_of_lt a b) c
theorem sq_floor (t F : Nat) (h : (2 * t) * (2 * t) <= (2 * F) * (2 * F)) : t <= F := by
cases Nat.lt_or_ge F t with
| inr r => exact r
| inl l =>
have e : 2 * F + 1 <= 2 * t := Nat.le_trans (Nat.le_succ _) (mul_le_l' l 2)
exact absurd (Nat.lt_of_lt_of_le (sq_lt _ _ e) h) (Nat.lt_irrefl _)
theorem sq_floor1 (u V : Nat) (h : u * u <= V * V) : u <= V := by
cases Nat.lt_or_ge V u with
| inr r => exact r
| inl l => exact absurd (Nat.lt_of_lt_of_le (sq_lt _ _ l) h) (Nat.lt_irrefl _)
theorem le_two_mul (t : Nat) : t <= 2 * t :=
Nat.le_trans (Nat.le_of_eq (one_mul' t).symm) (mul_le_r (Nat.le_succ 1) t)
theorem pow_add' (D a : Nat) : forall m, D ^ (a + m) = D ^ a * D ^ m
| 0 => (mul_one' _).symm
| m + 1 => by
show D ^ (a + m) * D = D ^ a * (D ^ m * D)
rw [pow_add' D a m, mul_assoc']
theorem pow_pow (D a : Nat) : forall t, (D ^ a) ^ t = D ^ (a * t)
| 0 => rfl
| t + 1 => by
show (D ^ a) ^ t * D ^ a = D ^ (a * t + a)
rw [pow_pow D a t, pow_add']
theorem key_exp (t : Nat) : (2 * (2 * t)) * t = (2 * t) * (2 * t) := by
rw [mul_assoc' 2 (2 * t) t, mul_assoc' 2 t (2 * t), Nat.mul_comm (2 * t) t]
theorem b_pow (D t : Nat) : (D ^ (2 * (2 * t) + t)) ^ t = D ^ ((2 * t) * (2 * t)) * D ^ (t * t) := by
rw [pow_pow, add_mul', pow_add', key_exp]
/-- The arithmetic of the positive branch: if 2 s <= bb then bb B <= s B + M bb gives B <= 2 M. -/
theorem branch2 (bb B s M : Nat) (hbb : 1 <= bb) (hD : 2 * s <= bb) (h : bb * B <= s * B + M * bb) :
B <= 2 * M := by
have h2 : bb * B + bb * B <= bb * B + 2 * (M * bb) := by
rw [<- two_mul']
refine Nat.le_trans (mul_le_l' h 2) ?_
rw [mul_add']
refine Nat.add_le_add_right ?_ _
rw [<- mul_assoc']
exact mul_le_r hD B
have h3 : B * bb <= (2 * M) * bb := by
rw [Nat.mul_comm B bb, mul_assoc']
exact add_cancel_l' h2
exact cancel_r hbb h3
def sP (t : Nat) : Nat := (2 * t) * (2 * t)
def DP (t : Nat) : Nat := 2 * sP t
def bP (t : Nat) : Nat := DP t ^ (2 * (2 * t) + t)
theorem one_le_two_t (t : Nat) (ht : 1 <= t) : 1 <= 2 * t :=
Nat.le_trans ht (Nat.le_trans (Nat.le_of_eq (one_mul' t).symm) (mul_le_r (Nat.le_succ 1) t))
theorem two_le_s (t : Nat) (ht : 1 <= t) : 2 <= sP t := by
have u := one_le_two_t t ht
have a : 2 <= 2 * t := Nat.le_trans (Nat.le_of_eq (mul_one' 2).symm) (mul_le_l' ht 2)
exact Nat.le_trans a (Nat.le_trans (Nat.le_of_eq (mul_one' (2 * t)).symm) (mul_le_l' u (2 * t)))
theorem one_le_D (t : Nat) (ht : 1 <= t) : 1 <= DP t :=
Nat.le_trans (Nat.le_succ 1) (Nat.le_trans (two_le_s t ht) (Nat.le_trans (Nat.le_of_eq (one_mul' _).symm)
(mul_le_r (Nat.le_succ 1) (sP t))))
theorem D_le_bb (t : Nat) (ht : 1 <= t) : DP t <= bP t * bP t := by
have hD := one_le_D t ht
have e1 : 1 <= 2 * (2 * t) + t := Nat.le_trans ht (Nat.le_add_left t _)
have a : DP t <= bP t := by
refine Nat.le_trans (Nat.le_of_eq (one_mul' (DP t)).symm) ?_
exact pow_le_exp (DP t) hD 1 _ e1
have hb : 1 <= bP t := pow_pos' hD _
exact Nat.le_trans a (Nat.le_trans (Nat.le_of_eq (mul_one' (bP t)).symm) (mul_le_l' hb (bP t)))
/-- The size lower bound for monotone real circuits on the clique-colouring test inputs. -/
theorem mrc_lower (t : Nat) (ht : 1 <= t) (C : List Gate)
(hC : InpOK (offE ((bP t + 1) * bP t)) C)
(hP : forall sg, sg ∈ allSeq (bP t) (bP t + 1) ->
out (outV (xpos ((bP t + 1) * bP t) (bP t) (bP t + 1) sg) C) = true)
(hN : forall col, col ∈ allSeq (bP t) ((bP t + 1) * bP t) ->
out (outV (xneg ((bP t + 1) * bP t) col) C) = false) :
DP t ^ (t * t) <= 2 * C.length := by
have hD := one_le_D t ht
have hb : 1 <= bP t := pow_pos' hD _
have hs := two_le_s t ht
have bt : bP t ^ t = DP t ^ sP t * DP t ^ (t * t) := b_pow (DP t) t
have hsP : 1 <= DP t ^ sP t := pow_pos' hD _
cases Found.Pnp129.mrc_bound (bP t + 1) (bP t) (bP t) (sP t) (sP t) t t hb hb hs hs C hC hP hN
(fun F h => sq_floor t F h) (fun V h => Nat.le_trans (le_two_mul t) (sq_floor1 (2 * t) V h)) with
| inl h =>
rw [bt] at h
have h' : DP t ^ (t * t) * DP t ^ sP t <= C.length * DP t ^ sP t := by
rw [Nat.mul_comm (DP t ^ (t * t))]
exact h
exact Nat.le_trans (cancel_r hsP h') (Nat.le_trans (Nat.le_of_eq (one_mul' _).symm) (mul_le_r (Nat.le_succ 1) _))
| inr h =>
have hbb : 1 <= bP t * bP t := Nat.le_trans hb (Nat.le_trans (Nat.le_of_eq (mul_one' (bP t)).symm) (mul_le_l' hb (bP t)))
have b2 := branch2 (bP t * bP t) (bP t ^ t) (sP t) (C.length * (2 * sP t) ^ sP t) hbb (D_le_bb t ht) h
rw [bt] at b2
have h' : DP t ^ (t * t) * DP t ^ sP t <= (2 * C.length) * DP t ^ sP t := by
rw [Nat.mul_comm (DP t ^ (t * t)), mul_assoc']
exact b2
exact cancel_r hsP h'
end Found.Pnp130
#print axioms Found.Pnp130.sq_lt
#print axioms Found.Pnp130.sq_floor
#print axioms Found.Pnp130.sq_floor1
#print axioms Found.Pnp130.le_two_mul
#print axioms Found.Pnp130.pow_add'
#print axioms Found.Pnp130.pow_pow
#print axioms Found.Pnp130.key_exp
#print axioms Found.Pnp130.b_pow
#print axioms Found.Pnp130.branch2
#print axioms Found.Pnp130.one_le_two_t
#print axioms Found.Pnp130.two_le_s
#print axioms Found.Pnp130.one_le_D
#print axioms Found.Pnp130.D_le_bb
#print axioms Found.Pnp130.mrc_lower
#print axioms: 14 theorems, no axioms.
The interpolant circuit of a refutation, restricted to off-diagonal edge variables, separates transversal cliques from colouring graphs, so every tree-like cutting planes refutation \(p\) of \(\mathrm {CC}(n,k,c)\) with the parameters above is large (\(m\) the number of variables).
found/Pnp131.lean
/-
T330 piece 131. found/Pnp131 -- a size lower bound for tree-like cutting planes refutations
of the clique-colouring formulas (proof complexity ladder, step 2).
A cutting planes refutation p of CC n k c compiles to a monotone real circuit (Pnp114) whose
output is the interpolant; on graphs with a k-clique it outputs 1 and on c-colourable graphs
0 (Pnp112). Restricting its input gates to off-diagonal edge variables (Pnp123.restrict)
keeps both: a transversal clique only has off-diagonal edges, so masking does not change it,
and masking only lowers a colouring graph (monotonicity, Pnp113.outV_mono). With the
parameters of Pnp130 (n = (b + 1) b, k = b + 1, c = b, b = D^(2(2t) + t), D = 2 (2t)^2):
D^(t t) <= 2 (2 m + 1) size(p), m = mCC n k c (the number of variables).
Proved: anyI_none, offE_pv, xpos_off, circ_pos, circ_neg, cp_lower.
Written with ASCII forms only. CIC dependence: none.
-/
import found.Pnp130
namespace Found.Pnp131
open Found.Pnp65 (anyI var_inj)
open Found.Pnp96 (anyI_ex)
open Found.Pnp99 (mul_le_l' pow_pos')
open Found.Pnp102 (CPProof falseLine)
open Found.Pnp103 (linesOf)
open Found.Pnp104 (pv mCC CC lin_lt)
open Found.Pnp105 (zle zle_trans)
open Found.Pnp109 (out LeA)
open Found.Pnp110 (isAt)
open Found.Pnp111 (Qc Rc)
open Found.Pnp112 (separate_clique separate_colour)
open Found.Pnp113 (Gate outV outV_mono)
open Found.Pnp114 (compile compile_val compile_len)
open Found.Pnp115 (beq_eq and_true_l and_true_r)
open Found.Pnp120 (zle_out out_zle)
open Found.Pnp123 (offE restrict restrict_eval restrict_inp restrict_len maskX)
open Found.Pnp124 (allSeq allSeq_rng)
open Found.Pnp125 (blt_of_lt)
open Found.Pnp127 (xneg neg_colouring)
open Found.Pnp128 (xpos pos_clique vtx_lt)
open Found.Pnp130 (DP bP mrc_lower)
theorem anyI_none (g : Nat -> Bool) : forall k, (forall j, j < k -> g j = false) -> anyI g k = false
| 0, _ => rfl
| k + 1, h => by
have e1 := anyI_none g k (fun j hj => h j (Nat.lt_trans hj (Nat.lt_succ_self k)))
have e2 := h k (Nat.lt_succ_self k)
show (anyI g k || g k) = false
calc (anyI g k || g k) = (false || false) := by rw [e1, e2]
_ = false := rfl
theorem offE_pv (n i j : Nat) (hi : i < n) (hj : j < n) (hij : Not (i = j)) : offE n (pv n i j) = true := by
have a : Nat.blt (pv n i j) (n * n) = true := blt_of_lt _ _ (lin_lt i j n n hi hj)
have b : anyI (fun i' => Nat.beq (pv n i j) (pv n i' i')) n = false := anyI_none _ n (fun i' hi' => by
cases e : Nat.beq (pv n i j) (pv n i' i') with
| false => rfl
| true =>
have w := var_inj n i i' j i' hj hi' (beq_eq _ _ e)
exact absurd (w.1.trans w.2.symm) hij)
show (Nat.blt (pv n i j) (n * n) && !(anyI (fun i' => Nat.beq (pv n i j) (pv n i' i')) n)) = true
calc (Nat.blt (pv n i j) (n * n) && !(anyI (fun i' => Nat.beq (pv n i j) (pv n i' i')) n)) = (true && !false) := by rw [a, b]
_ = true := rfl
/-- Every edge of a transversal clique is off-diagonal, so masking keeps the clique. -/
theorem xpos_off (b k : Nat) (sg : Nat -> Nat) (hs : sg ∈ allSeq b k) :
LeA (xpos (k * b) b k sg) (maskX (k * b) (xpos (k * b) b k sg)) := by
intro v hv
have rg := allSeq_rng b k sg hs
cases anyI_ex _ k hv with
| intro l hl =>
cases anyI_ex _ k hl.2 with
| intro l' hl' =>
have ev : v = pv (k * b) (l * b + sg l) (l' * b + sg l') := beq_eq _ _ (and_true_l hl'.2)
have nl : Not (l = l') := fun e => by
have t := and_true_r hl'.2
rw [e, Found.Pnp115.beq_self] at t
exact Bool.noConfusion t
have off : offE (k * b) v = true := by
rw [ev]
refine offE_pv _ _ _ (vtx_lt b k l _ hl.1 (rg l hl.1)) (vtx_lt b k l' _ hl'.1 (rg l' hl'.1)) (fun e => ?_)
exact nl (var_inj b l l' _ _ (rg l hl.1) (rg l' hl'.1) e).1
show (offE (k * b) v && xpos (k * b) b k sg v) = true
calc (offE (k * b) v && xpos (k * b) b k sg v) = (true && true) := by rw [off, hv]
_ = true := rfl
/-- The restricted interpolant circuit of a refutation. -/
def circ (n k c : Nat) (p : CPProof (mCC n k c) (linesOf (CC n k c)) falseLine) : List Gate :=
restrict n (compile (mCC n k c) (linesOf (CC n k c)) (Qc n k) (isAt (mCC n k c) (Qc n k) (Rc n k)) p)
theorem circ_pos (b k c : Nat) (hc : 1 <= c) (p : CPProof (mCC (k * b) k c) (linesOf (CC (k * b) k c)) falseLine)
(sg : Nat -> Nat) (hs : sg ∈ allSeq b k) : out (outV (xpos (k * b) b k sg) (circ (k * b) k c p)) = true := by
let x := xpos (k * b) b k sg
let C0 := compile (mCC (k * b) k c) (linesOf (CC (k * b) k c)) (Qc (k * b) k)
(isAt (mCC (k * b) k c) (Qc (k * b) k) (Rc (k * b) k)) p
have h1 : out (outV x C0) = true := by
rw [compile_val]
exact separate_clique (k * b) k c hc p x (fun l => l * b + sg l) (pos_clique b k sg hs)
have mono := outV_mono x (maskX (k * b) x) (xpos_off b k sg hs) C0
show out (Found.Pnp113.nthV (Found.Pnp113.evalR x (restrict (k * b) C0)) 0) = true
rw [restrict_eval]
exact zle_out _ (zle_trans _ _ _ (out_zle _ h1) mono)
theorem circ_neg (n k c : Nat) (hc : 1 <= c) (p : CPProof (mCC n k c) (linesOf (CC n k c)) falseLine)
(col : Nat -> Nat) (hcol : col ∈ allSeq c n) : out (outV (xneg n col) (circ n k c p)) = false := by
let x := xneg n col
let C0 := compile (mCC n k c) (linesOf (CC n k c)) (Qc n k) (isAt (mCC n k c) (Qc n k) (Rc n k)) p
have h1 : out (outV x C0) = false := by
rw [compile_val]
exact separate_colour n k c hc p x col (neg_colouring n c col hcol)
have mono := outV_mono (maskX n x) x (fun v hv => and_true_r hv) C0
show out (Found.Pnp113.nthV (Found.Pnp113.evalR x (restrict n C0)) 0) = false
rw [restrict_eval]
cases e : out (Found.Pnp113.nthV (Found.Pnp113.evalR (maskX n x) C0) 0) with
| false => rfl
| true =>
have t := zle_out _ (zle_trans _ _ _ (out_zle _ e) mono)
rw [h1] at t
exact Bool.noConfusion t
/-- Tree-like cutting planes refutations of clique-colouring formulas are large. -/
theorem cp_lower (t : Nat) (ht : 1 <= t)
(p : CPProof (mCC ((bP t + 1) * bP t) (bP t + 1) (bP t)) (linesOf (CC ((bP t + 1) * bP t) (bP t + 1) (bP t))) falseLine) :
DP t ^ (t * t) <= 2 * ((2 * mCC ((bP t + 1) * bP t) (bP t + 1) (bP t) + 1) * p.size) := by
have hc : 1 <= bP t := pow_pos' (Found.Pnp130.one_le_D t ht) _
have lo := mrc_lower t ht (circ ((bP t + 1) * bP t) (bP t + 1) (bP t) p)
(restrict_inp _ _)
(fun sg hs => circ_pos (bP t) (bP t + 1) (bP t) hc p sg hs)
(fun col hcol => circ_neg _ _ (bP t) hc p col hcol)
refine Nat.le_trans lo (mul_le_l' ?_ 2)
show (restrict _ _).length <= _
rw [restrict_len]
exact compile_len _ _ _ _ _ p
end Found.Pnp131
#print axioms Found.Pnp131.anyI_none
#print axioms Found.Pnp131.offE_pv
#print axioms Found.Pnp131.xpos_off
#print axioms Found.Pnp131.circ_pos
#print axioms Found.Pnp131.circ_neg
#print axioms Found.Pnp131.cp_lower
#print axioms: 6 theorems, no axioms.
The main target and the open steps, stated as propositions with no proof and no axiom.
OPEN_LEMMAS_PNP.lean
/-
T330. Open lemmas of the P vs NP map (PNP_MAP.md). Each open lemma is a definition of a
proposition; nothing here is proved or assumed (no sorry, no axiom). A lemma moves out
of this file when a file under work/found proves it with no axioms.
The main target is O0 (or its negation); the chain O1 -> O0 and the barrier status of
every method are in PNP_MAP.md.
-/
import found.RootComp
import found.Pnp1
import found.Pnp4
import found.Owf1
import found.Owf4
namespace Pnp.Open
open Found.RootComp
open Found.Pnp1
/-- The language SAT on bit strings, for a fixed encoding of CNF formulas. -/
def SatLang (dec : List Bool -> Option CNF) (x : List Bool) : Prop :=
match dec x with
| some f => Sat f
| none => False
/-- O0 (main target): P differs from NP for the machine model M (some language in NP
is not in P). -/
def O0_P_ne_NP (M : Machine) : Prop :=
Exists fun L : List Bool -> Bool => InNP M L /\ (InP M L -> False)
/-- O0' (the other answer): P equals NP for M. -/
def O0_P_eq_NP (M : Machine) : Prop :=
forall L : List Bool -> Bool, InNP M L -> InP M L
/-- A family of formulas, one for each input length n, computing L on inputs of length n
(variables 0 .. n-1 read the input). -/
def Computes (L : List Bool -> Bool) (F : Nat -> Fm) : Prop :=
forall n (a : Nat -> Bool), ev a (F n) = L (read a 0 n)
/-- O1 (non-uniform route): some language in NP has no family of formulas of polynomial
size (NP not in NC1). A step only: O0 needs the circuit version of O1 together with the
tool "P has polynomial-size circuits" (missing, PNP_MAP.md). -/
def O1_formula_lower (M : Machine) : Prop :=
Exists fun L : List Bool -> Bool => InNP M L /\
forall F : Nat -> Fm, Computes L F -> forall c k : Nat,
Exists fun n => pbound c k n < gates (F n)
/-- O2 (tool, counting side): for every n some function of n bits needs at least
2^n / (4 (n+1)) - 1 gates in formulas (Shannon-Riordan counting). A tool lemma only: the property
"needs many gates" is large, so it is not used for the main conclusion (natural proofs). -/
def O2_counting (n : Nat) : Prop :=
Exists fun f : List Bool -> Bool => forall F : Fm,
(forall a, ev a F = f (read a 0 n)) -> pow2 n <= 4 * (n + 1) * (gates F + 1)
/-- O3 (tool, Cook-Levin): every language in NP maps to SAT by a length-polynomial map r.
The time bound of r needs a machine model that writes an output string; the model of
RootComp returns one bit; the model with output is found/Pnp3 (OM), and O3o below is the
same lemma there. -/
def O3_cook_levin (M : Machine) (satb : List Bool -> Bool) : Prop :=
forall L : List Bool -> Bool, InNP M L ->
Exists fun r : List Bool -> List Bool =>
(Exists fun c => Exists fun k => forall x, (r x).length <= pbound c k x.length) /\
forall x, L x = satb (r x)
/-- O0 over the machine model with output (found/Pnp3, Pnp4): some NP language is not in P.
By Pnp4.sep_hard this excludes every NP-hard language from P. -/
def O0o_P_ne_NP (M : Found.Pnp3.OM) : Prop :=
Exists fun L : List Bool -> Bool => Found.Pnp4.InNPo M L /\ (Found.Pnp3.InPo M L -> False)
/-- O3 over the machine model with output: the SAT language satb is NP-hard. With
Pnp4.hard_in_P and Pnp4.sep_hard: O0o holds iff satb is not in P (for satb in NP). -/
def O3o_cook_levin (M : Found.Pnp3.OM) (satb : List Bool -> Bool) : Prop :=
Found.Pnp4.Hard M satb
/-- O5 (T332, search-to-decision on a concrete model): if NP is inside P, every function
computed in polynomial time has a polynomial-time inverter. With Owf1.owf_p_ne_np a
one-way function (Owf1.OneWay2) then gives that NP is not inside P. -/
def O5_search_to_decision (M : Found.Pnp3.OM) : Prop := Found.Owf1.S2D M
/-- O5rm: search-to-decision on the register machine (Owf4.S2DRM). Closed: proved as
Found.Pnp86.s2drm (Pnp67-86), so a register-machine one-way function gives NP_RM not inside
P_RM (Found.Pnp86.owf_p_ne_np). Kept here as the statement. -/
def O5rm_search_to_decision : Prop := Found.Owf4.S2DRM
end Pnp.Open
#print axioms: no theorems (definitions only).