import Mathlib.Tacticnamespace Equalizingabbrev State := ℕ × ℕ/-- False increments the first coordinate and doubles the second. True does the reverse. -/def run : List Bool → State → State| [], p => p| false::w, (x,y) => run w (x+1,2*y)| true::w, (x,y) => run w (2*x,y+1)private theorem run_append (w v : List Bool) (p : State) : run (w++v) p = run v (run w p) := byinduction w generalizing p with| nil => rfl| cons b w ih => rcases p with ⟨x,y⟩;cases b <;> apply ihprivate theorem left_run (i x y : ℕ) : run (List.replicate i false) (x,y)=(x+i,2^i*y) := byinduction i generalizing x y with| zero => simp [run]| succ i ih => rw [List.replicate_succ,run,ih,pow_succ];congr 1 <;> ringprivate theorem lower_count (w : List Bool) (x y : ℕ) : 2^(w.count true)*x ≤ (run w (x,y)).1 := byinduction w generalizing x y with| nil => simp [run]| cons b w ih =>cases b· simpa [run] using (show 2^w.count true*x≤_ from (Nat.mul_le_mul_left _ (Nat.le_succ x)).trans (ih (x+1) (2*y)))· simp [run,pow_succ,Nat.mul_assoc] at *exact ih _ _private def singleR (i c : ℕ) : List Bool := List.replicate i false ++ true::List.replicate c falseprivate theorem classify (w : List Bool) :w=List.replicate w.length false ∨ (∃ i c, w=singleR i c) ∨ 2≤w.count true := byinduction w with| nil => exact Or.inl rfl| cons b w ih =>cases b· rcases ih with e|⟨i,c,e⟩|h· left;simpa [List.replicate_succ] using congrArg (false::·) e· right;left;exact ⟨i+1,c,by rw [e];simp [singleR,List.replicate_succ]⟩· exact Or.inr (Or.inr (by simpa using h))· rcases ih with e|⟨i,c,e⟩|h· exact Or.inr (Or.inl ⟨0,w.length,by simpa [singleR] using congrArg (true::·) e⟩)· right;right;rw [e];simp [singleR,List.count_append];omega· right;right;simp only [List.count_cons_self];omegaprivate theorem single_run (n i c : ℕ) : run (singleR i c) (n,0)=(2*(n+i)+c,2^c) := byrw [singleR,run_append,left_run]simp [run,left_run]/-- t starts at zero here. The number of final left moves is a positive multiple of four. -/def block (t : ℕ) := 4*t+4def scale (t : ℕ) := 4*16^tdef start (t : ℕ) := 5*scale t-t-2def shortWord (t : ℕ) := [true,false,false,true] ++ List.replicate (block t) falsedef longWord (t : ℕ) := singleR (3*scale t-(t+1)) (block t+2)private theorem scale_lower (t : ℕ) : 4*(t+1) ≤ scale t := byinduction t with| zero => decide| succ t ih => simp only [scale,pow_succ] at *;omegaprivate theorem pow_block (t : ℕ) : 2^(block t)=4*scale t := bysimp [block,scale,pow_add,pow_mul];ringtheorem short_route (t : ℕ) : (shortWord t).length=block t+4 ∧run (shortWord t) (start t,0)=(20*scale t,20*scale t) := byconstructor· simp [shortWord]· rw [shortWord,run_append]simp only [run,left_run,pow_block]have := scale_lower tunfold start blockcongr 1 <;> omegatheorem long_route (t : ℕ) :run (longWord t) (start t,0)=(16*scale t,16*scale t) := byrw [longWord,single_run,pow_add,pow_block]have := scale_lower tunfold start blocknorm_numcongr 1 <;> omegatheorem short_lower (t m : ℕ) (w : List Bool) (len : w.length ≤ block t+4)(diag : run w (start t,0) = (m,m)) : 4*start t ≤ m := byhave hk := scale_lower trcases classify w with eqn|⟨i,c,rfl⟩|h· rw [eqn,left_run] at diagsimp at diagunfold start at *;omega· rw [single_run] at diagsimp only [Prod.mk.injEq] at diagsimp [singleR] at lenhave ub : 2^(block t+1) = 8*scale t := by rw [pow_succ,pow_block];ringhave low : block t+2 ≤ c := byby_contra! smallhave hh := Nat.pow_le_pow_right (by decide : 0<2) (show c≤block t+1 by omega)rw [ub] at hhunfold start at *;omegahave lb : 2^(block t+2) = 16*scale t := by rw [pow_add,pow_block];ringhave hh := Nat.pow_le_pow_right (by decide : 0<2) lowrw [lb] at hhunfold start block at *;omega· have hh := lower_count w (start t) 0rw [diag] at hhexact (Nat.mul_le_mul_right _ (Nat.pow_le_pow_right (by decide : 0<2) h)).trans hhdef Shortest (p : State) (w : List Bool) (m : ℕ) : Prop :=run w p=(m,m) ∧ ∀ v z, run v p=(z,z) → w.length≤v.lengththeorem family (t : ℕ) : (∃ w m, Shortest (start t,0) w m) ∧(∀ w m, Shortest (start t,0) w m → 16*scale t + 12*t + 8 ≤ m) ∧run (longWord t) (start t,0)=(16*scale t,16*scale t) := byclassicalobtain ⟨hs,hd⟩ := short_route thave possible : ∃ l, ∃ w m, w.length=l ∧ run w (start t,0)=(m,m) :=⟨_,shortWord t,_,rfl,hd⟩obtain ⟨w,m,hl,hm⟩ := Nat.find_spec possiblerefine ⟨⟨w,m,hm,fun v z hv => hl.trans_le (Nat.find_min' possible ⟨v,z,rfl,hv⟩)⟩,?_,long_route t⟩intro v z hvhave ub := (hv.2 _ _ hd).trans_eq hshave lb := short_lower t z v ub hv.1have hk := scale_lower tunfold start at lbomega/-- Original table minimizes the endpoint among the walks of minimum length. -/def TableEntry (p : State) (m : ℕ) : Prop :=(∃ w, Shortest p w m) ∧ ∀ v z, Shortest p v z → m≤ztheorem refutes_table_conjecture : ¬ ∀ n k : ℕ, k≤n →∃ m, TableEntry (n,k) m ∧ ∀ v z, run v (n,k)=(z,z) → m≤z := byintro allegedobtain ⟨m,h,global⟩ := alleged (start 0) 0 (Nat.zero_le _)obtain ⟨w,hw⟩ := h.1have lb := (family 0).2.1 w m hwhave ub := global _ _ (long_route 0)omega#print axioms family#print axioms refutes_table_conjectureend Equalizing