import Mathlib import OAI.Probability.Contingency.DenseWeightBounds namespace OAI /-! Dense Dyadic. -/ noncomputable section open scoped BigOperators namespace Contingency.Dense.BinProblem noncomputable section open Contingency.Grid Contingency.FiniteChain variable {a c q K : ℕ} (D : BinProblem a c q K) attribute [local instance] tableFintype tableNonempty local instance : DecidableRel (@Adj q K) := Classical.decRel _ def exponent (v : Vertex q K) : ℕ := (⌊binExponent D.e (fun i => (D.R i:ℝ)) (fun j => (D.P j:ℝ)) D.widths D.d (realCorner v)⌋:ℤ).toNat theorem floor_nonneg (v : Vertex q K) : 0≤(⌊binExponent D.e (fun i => (D.R i:ℝ)) (fun j => (D.P j:ℝ)) D.widths D.d (realCorner v)⌋:ℤ) := by exact Int.floor_nonneg.mpr (mul_nonneg (by positivity) (penalty_nonneg _ _ _ _ _ _)) theorem weight_exponent (v : Vertex q K) : D.weight v=1/(2:ℝ)^D.exponent v := by change (2:ℝ)^(-(⌊binExponent D.e _ _ D.widths D.d (realCorner v)⌋:ℝ))=_ rw [← Int.toNat_of_nonneg (D.floor_nonneg v)] simp only [Int.cast_natCast,Real.rpow_neg (by norm_num : (0:ℝ)≤2),Real.rpow_natCast,one_div,exponent] theorem exponent_bound (d : ℕ) (hD : D.d=d) (hK : (K:ℝ)=4*D.d^8) (v : Vertex q K) : D.exponent v≤8*d^5 := by apply Int.toNat_le.mpr have hh := Int.floor_le (binExponent D.e (fun i => (D.R i:ℝ)) (fun j => (D.P j:ℝ)) D.widths D.d (realCorner v)) have hb := mul_le_mul_of_nonneg_left (D.corner_penalty_bound hK v) (show 0≤D.d^4 by positivity) have hle : (⌊binExponent D.e (fun i => (D.R i:ℝ)) (fun j => (D.P j:ℝ)) D.widths D.d (realCorner v)⌋:ℝ)≤8*(d:ℝ)^5 := by unfold binExponent at hh ⊢ rw [hD] at hh hb ⊢ nlinarith only [hh,hb,show 0≤(d:ℝ)^5 by positivity] exact_mod_cast hle theorem neighbor_bound (d : ℕ) (hD : D.d=d) (v : Vertex q K) : (Finset.univ.filter (Adj v)).card≤2^Nat.clog 2 (8*d) := by classical have he : (Finset.univ.filter (Adj v)).card=Nat.card {w : Vertex q K // Adj v w} := by rw [Nat.card_eq_fintype_card,Fintype.card_subtype] rw [he] have hq : q≤d := by have hq' := D.hq rw [hD] at hq' exact_mod_cast hq' exact (neighbor_card v).trans ((by omega : 2*q≤8*d).trans (Nat.le_pow_clog (by norm_num) (8*d))) def bitKernel (d : ℕ) (hD : D.d=d) (v : Vertex q K) : DyadicLaw (Vertex q K) := by classical exact (DyadicLaw.neighborProposal Adj v (Nat.clog 2 (8*d)) (adj_irrefl v) (D.neighbor_bound d hD v)).accept D.exponent (8*d^5) v theorem bitKernel_bits (d : ℕ) (hD : D.d=d) (v : Vertex q K) : (D.bitKernel d hD v).bits=Nat.clog 2 (8*d)+8*d^5 := rfl theorem bitKernel_law (d : ℕ) (hD : D.d=d) (hK : (K:ℝ)=4*D.d^8) (hp0 : 0≤proposalRate (8*d)) (hp : proposalRate (8*d)≤1/(8*D.d)) (v : Vertex q K) : (D.bitKernel d hD v).toLaw=(D.chain (proposalRate (8*d)) hp0 hp).transition v := by classical apply DyadicLaw.accepted_neighbor_kernel Adj D.exponent (8*d^5) (Nat.clog 2 (8*d)) (D.exponent_bound d hD hK) v intro w hvw change (if v=w then _ else _) = _ rw [ite_eq_right hvw] change proposalRate (8*d)*edgeWeight D.weight v w/D.weight v=_ simp only [edgeWeight,D.weight_exponent,proposalRate] def bitOffsets (h d : ℕ) : DyadicLaw (Offsets D.w) := DyadicLaw.pi (fun i => offsetLaw (D.w i) (D.hw i) (Nat.clog 2 (D.w i+1)+4*(h+d))) theorem bitOffsets_law (h d : ℕ) : (D.bitOffsets h d).toLaw=D.implementedOffsets h d := by exact DyadicLaw.toLaw_pi _ def bitTrial (h d : ℕ) (hD : D.d=d) (t : ℕ) : DyadicLaw (Option (Tables D.R D.P)) := by classical let walk := DyadicLaw.walk (D.bitKernel d hD) (Nat.clog 2 (8*d)+8*d^5) (D.bitKernel_bits d hD) (fun _ => ⟨0,D.Kpos⟩) t exact (walk.bind (fun v => (D.bitOffsets h d).map (fun β => (v,β))) (D.bitOffsets h d).bits (fun _ => DyadicLaw.map_bits _ _)).map D.decode theorem bitTrial_law (h d : ℕ) (hD : D.d=d) (hK : (K:ℝ)=4*D.d^8) (hp0 : 0≤proposalRate (8*d)) (hp : proposalRate (8*d)≤1/(8*D.d)) (t : ℕ) : (D.bitTrial h d hD t).toLaw=D.implementedTrial (proposalRate (8*d)) hp0 hp h d t := by classical have hw : (DyadicLaw.walk (D.bitKernel d hD) (Nat.clog 2 (8*d)+8*d^5) (D.bitKernel_bits d hD) (fun _ => ⟨0,D.Kpos⟩) t).toLaw=(D.chain (proposalRate (8*d)) hp0 hp).walk (fun _ => ⟨0,D.Kpos⟩) t := by induction t with | zero => exact DyadicLaw.toLaw_pure _ _ | succ t ih => simp only [DyadicLaw.walk,DyadicLaw.toLaw_bind,Chain.walk] rw [ih] congr 1 funext x exact D.bitKernel_law d hD hK hp0 hp x simp only [bitTrial,DyadicLaw.toLaw_map,DyadicLaw.toLaw_bind,D.bitOffsets_law] rw [hw] congr 1 apply Law.ext; funext s rcases s with ⟨v,β⟩ simp only [Law.bind,Law.map,Law.pure,Law.prod] simp [Prod.mk.injEq,ite_and,Finset.sum_ite_irrel,mul_ite] def bitDraw (h d : ℕ) (hD : D.d=d) (fallback : Tables D.R D.P) : DyadicLaw (Tables D.R D.P) := by classical exact stoppedLaw (D.bitTrial h d hD (d^50*(h+1)^2)) fallback (d^4*(h+1)) theorem bitDraw_tv (d : ℕ) (hd : 14≤d) (hD : D.d=d) (hK : (K:ℝ)=4*D.d^8) (h : ℕ) (hh : 1≤h) (fallback : Tables D.R D.P) : (D.bitDraw h d hD fallback).toLaw.tv (Law.uniform (Tables D.R D.P))≤(2:ℝ)^(-(h:ℤ)) := by classical have hb := binProposal_bounds d (by omega) have hp : proposalRate (8*d)≤1/(8*D.d) := by simpa only [hD] using hb.2 have hp0 : 0≤proposalRate (8*d) := by unfold proposalRate; positivity rw [bitDraw,stoppedLaw_toLaw,D.bitTrial_law h d hD hK hp0 hp] exact D.draw_tv d hd hD hK _ hp0 hp (by simpa only [hD] using hb.1) h hh fallback end end Contingency.Dense.BinProblem namespace Contingency namespace Schedule def h (d b k : ℕ) := d^4*(k+b)^2 def T (d b k : ℕ) := d^200*(k+b)^2 def Jo (d k : ℕ) := d^4*(k+1) def Q (d : ℕ) := Nat.clog 2 (8*d)+8*d^5 def qs (d : ℕ) := Nat.clog 2 (32*d^2) def rmax (d b k : ℕ) := b+1+4*(h d b k+d) def H (d b k : ℕ) := d^50*(h d b k+1)^2 def JD (d b k : ℕ) := d^4*(h d b k+1) def R1 (d b k : ℕ) := H d b k*Q d+d*rmax d b k def RD (d b k : ℕ) := JD d b k*R1 d b k def RT (d b k : ℕ) := T d b k*(RD d b k+qs d)+RD d b k def R (d b k : ℕ) := Jo d k*RT d b k theorem clog_self_bound (x : ℕ) : Nat.clog 2 x≤x := Nat.clog_le_of_le_pow (Nat.lt_two_pow_self (n:=x)).le theorem qs_bound (d : ℕ) : qs d≤2*d+5 := by apply Nat.clog_le_of_le_pow calc 32*d^2 ≤ 32*(2^d)^2 := Nat.mul_le_mul_left _ (Nat.pow_le_pow_left (Nat.lt_two_pow_self (n:=d)).le 2) _ = 2^(2*d+5) := by rw [← pow_mul,Nat.mul_comm d 2,pow_add]; norm_num; ring theorem bounds (d b k : ℕ) (hd : 14≤d) (hb : d≤b) (hk : 1≤k) : let q := d+b+k+2 h d b k≤q^6 ∧ h d b k+1≤q^7 ∧ Q d≤q^6 ∧ rmax d b k≤q^7 ∧ H d b k≤q^64 ∧ R1 d b k≤q^71 ∧ RD d b k≤q^82 ∧ RT d b k≤q^285 ∧ R d b k≤q^290 ∧ R d b k≤q^300 := by dsimp only set q := d+b+k+2 have hq : 31≤q := by dsimp [q]; omega have hq0 : 0 R (marginOrder R i) abbrev sortedP := fun j => P (marginOrder P j) def literalWidth (i : Fin (a*c)) : ℕ := min (sortedR R ((finProdFinEquiv.symm i).1.castSucc)) (sortedP P ((finProdFinEquiv.symm i).2.castSucc))/(d^8) include hd in theorem padding_scale : 2*d^8 ≤ d^12 := by have hh : 2 ≤ d^4 := (by norm_num : 2 ≤ 14^4).trans (Nat.pow_le_pow_left hd _) nlinarith only [Nat.mul_le_mul_left (d^8) hh,show d^12=d^8*d^4 by ring] abbrev literalProblem : BinProblem a c (a*c) (4*d^8) where e := finProdFinEquiv.symm R := sortedR R P := sortedP P w := literalWidth d R P d := d hd := by exact_mod_cast (show 4 ≤ d by omega) hq := by exact_mod_cast (Nat.mul_le_mul (Nat.le_add_right a 1) (Nat.le_add_right c 1)).trans hac ha := by exact_mod_cast ha hc := by exact_mod_cast hc hK := by push_cast have hp : (0:ℝ) < (d:ℝ)^8 := by positivity linarith hR i := (pow_pos (by omega : 0 < d) 12).trans_le (hR _) hP j := (pow_pos (by omega : 0 < d) 12).trans_le (hP _) heq := by rw [(marginOrder R).sum_comp R,(marginOrder P).sum_comp P]; exact heq hRmax := marginOrder_max R hPmax := marginOrder_max P hw i := (floor_width_bounds _ (d^8) (by positivity) ((padding_scale d hd).trans (le_min (hR _) (hP _)))).1 hwlo i := by have hh := (floor_width_bounds (min (sortedR R (finProdFinEquiv.symm i).1.castSucc) (sortedP P (finProdFinEquiv.symm i).2.castSucc)) (d^8) (by positivity) ((padding_scale d hd).trans (le_min (hR _) (hP _)))).2.1 simpa only [scale,literalWidth,Nat.cast_min,Nat.cast_pow] using hh hwhi i := by have hh := (floor_width_bounds (min (sortedR R (finProdFinEquiv.symm i).1.castSucc) (sortedP P (finProdFinEquiv.symm i).2.castSucc)) (d^8) (by positivity) ((padding_scale d hd).trans (le_min (hR _) (hP _)))).2.2 simpa only [scale,literalWidth,Nat.cast_min,Nat.cast_pow] using hh theorem literalWidth_bound (_hd : 14 ≤ d) (b : ℕ) (hRb : ∀ i,R i ≤ 2^b) (i : Fin (a*c)) : literalWidth d R P i ≤ 2^b := by exact (Nat.div_le_self _ _).trans ((min_le_left _ _).trans (hRb _)) attribute [local instance] BinProblem.tableFintype def literalDraw (b k : ℕ) (hRb : ∀ i,R i ≤ 2^b) (fallback : Tables R P) : DyadicLaw (Tables R P) := by classical let e := permuteTableEquiv R P (marginOrder R) (marginOrder P) let D := literalProblem d R P hd ha hc hac hR hP heq exact (D.reservedDraw d b k rfl (literalWidth_bound d R P hd b hRb) (e fallback)).map e.symm theorem literalDraw_bits (b k : ℕ) (hRb : ∀ i,R i ≤ 2^b) (fallback : Tables R P) : (literalDraw d R P hd ha hc hac hR hP heq b k hRb fallback).bits=Schedule.RD d b k := by classical simp only [literalDraw,DyadicLaw.map_bits,BinProblem.reservedDraw_bits] theorem literalDraw_tv (b k : ℕ) (hk : 1 ≤ k) (hRb : ∀ i,R i ≤ 2^b) (fallback : Tables R P) : letI := tables_nonempty R P heq (literalDraw d R P hd ha hc hac hR hP heq b k hRb fallback).toLaw.tv (Law.uniform (Tables R P)) ≤ (2:ℝ)^(-(Schedule.h d b k:ℤ)) := by classical let := tables_nonempty R P heq let e := permuteTableEquiv R P (marginOrder R) (marginOrder P) let D := literalProblem d R P hd ha hc hac hR hP heq let := tables_nonempty D.R D.P D.heq have hK : ((4*d^8:ℕ):ℝ)=4*D.d^8 := by simp [D] have hh := D.reservedDraw_tv d b k hd hk rfl hK (literalWidth_bound d R P hd b hRb) (e fallback) change ((D.reservedDraw d b k rfl _ (e fallback)).map e.symm).toLaw.tv _ ≤ _ rw [DyadicLaw.toLaw_map,← Law.uniform_map_equiv e.symm] exact (Law.tv_map e.symm _ _).trans hh end end Contingency.Dense open scoped BigOperators namespace Contingency namespace PaddedGraph variable {m n : ℕ} def IsLarge (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) (s : Cell m n) : Prop := U ≤ r s.1 ∧ U ≤ c s.2 def IsSmall (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) (s : Cell m n) : Prop := ¬IsLarge U r c s instance (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) (s : Cell m n) : Decidable (IsLarge U r c s) := inferInstanceAs (Decidable (_ ∧ _)) instance (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) (s : Cell m n) : Decidable (IsSmall U r c s) := inferInstanceAs (Decidable (¬_)) structure State (m n : ℕ) where x : Cell m n → ℤ q : Cell m n → ℤ deriving DecidableEq def rowSum (x : Cell m n → ℤ) (i : Fin m) : ℤ := ∑ j, x (i,j) def colSum (x : Cell m n → ℤ) (j : Fin n) : ℤ := ∑ i, x (i,j) def Feasible (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) (X : State m n) : Prop := (∀ s, 0 ≤ X.x s ∧ X.x s ≤ U ∧ 0 ≤ X.q s ∧ X.q s ≤ U) ∧ (∀ s, IsLarge U r c s → X.x s = 0 ∧ X.q s = 0) ∧ (∀ i, rowSum X.x i ≤ r i ∧ ((∀ j, ¬IsLarge U r c (i,j)) → rowSum X.x i = r i)) ∧ (∀ j, colSum X.q j ≤ c j ∧ ((∀ i, ¬IsLarge U r c (i,j)) → colSum X.q j = c j)) def Transversal (X : State m n) : Prop := X.q = X.x def unit (s : Cell m n) : Cell m n → ℤ := fun v => if v = s then 1 else 0 def DefectAt (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) (s t : Cell m n) (X : State m n) : Prop := IsSmall U r c s ∧ IsSmall U r c t ∧ s ≠ t ∧ X.q = fun v => X.x v - unit t v + unit s v def receiver (s t : Cell m n) : Cell m n := (t.1,s.2) noncomputable def repair (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) (s t : Cell m n) (X : State m n) : State m n := let x' := fun w => X.x w - unit t w + if IsSmall U r c (receiver s t) then unit (receiver s t) w else 0 ⟨x',x'⟩ @[simp] theorem repair_transversal (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) (s t : Cell m n) (X : State m n) : Transversal (repair U r c s t X) := rfl @[simp] theorem rowSum_unit (s : Cell m n) (i : Fin m) : rowSum (unit s) i = if i = s.1 then 1 else 0 := by classical rcases s with ⟨a,b⟩ unfold rowSum unit by_cases hi : i = a · subst i; simp · simp [hi] @[simp] theorem colSum_unit (s : Cell m n) (j : Fin n) : colSum (unit s) j = if j = s.2 then 1 else 0 := by classical rcases s with ⟨a,b⟩ unfold colSum unit by_cases hj : j = b · subst j; simp · simp [hj] theorem rowSum_sub_add (x y z : Cell m n → ℤ) (i : Fin m) : rowSum (fun v => x v-y v+z v) i = rowSum x i-rowSum y i+rowSum z i := by simp [rowSum,Finset.sum_add_distrib,Finset.sum_sub_distrib] theorem colSum_sub_add (x y z : Cell m n → ℤ) (j : Fin n) : colSum (fun v => x v-y v+z v) j = colSum x j-colSum y j+colSum z j := by simp [colSum,Finset.sum_add_distrib,Finset.sum_sub_distrib] theorem donor_positive (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) (s t : Cell m n) (X : State m n) (hx : Feasible U r c X) (hd : DefectAt U r c s t X) : 1 ≤ X.x t := by have h := congrFun hd.2.2.2 t have hq := (hx.1 t).2.2.1 change X.q t = X.x t - (if t = t then 1 else 0) + (if t = s then 1 else 0) at h simp [Ne.symm hd.2.2.1] at h omega theorem repair_nonneg (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) (s t : Cell m n) (X : State m n) (hx : Feasible U r c X) (hd : DefectAt U r c s t X) : ∀ w, 0 ≤ (repair U r c s t X).x w := by intro w have h0 := (hx.1 w).1 have ht := donor_positive U r c s t X hx hd dsimp [repair,unit] by_cases hw : w = t · subst w; split_ifs <;> omega · simp only [ite_eq_right hw,sub_zero] split_ifs <;> omega theorem repair_support (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) (s t : Cell m n) (X : State m n) (hx : Feasible U r c X) (hd : DefectAt U r c s t X) : ∀ w, IsLarge U r c w → (repair U r c s t X).x w = 0 := by intro w hw have hwt : w ≠ t := by intro he; exact hd.2.1 (he ▸ hw) have hx0 := (hx.2.1 w hw).1 dsimp [repair,unit] rw [hx0,ite_eq_right hwt] by_cases hv : IsSmall U r c (receiver s t) · rw [ite_eq_left hv] have hwv : w ≠ receiver s t := by intro he; exact hv (he ▸ hw) rw [ite_eq_right hwv] ring · rw [ite_eq_right hv] ring theorem repair_rowSum (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) (s t : Cell m n) (X : State m n) (i : Fin m) : rowSum (repair U r c s t X).x i = rowSum X.x i - if IsLarge U r c (receiver s t) ∧ i = t.1 then 1 else 0 := by by_cases hv : IsSmall U r c (receiver s t) · change rowSum (fun w => X.x w - unit t w + if IsSmall U r c (receiver s t) then unit (receiver s t) w else 0) i = _ simp only [ite_eq_left hv,rowSum_sub_add,rowSum_unit] have hv' : ¬IsLarge U r c (receiver s t) := hv have hnot : ¬(IsLarge U r c (receiver s t) ∧ i = t.1) := fun h => hv' h.1 rw [ite_eq_right hnot] dsimp [receiver] omega · have hv' : IsLarge U r c (receiver s t) := Classical.not_not.mp hv simp only [repair,ite_eq_right hv] change rowSum (fun w => X.x w-unit t w+0) i = _ rw [rowSum_sub_add,rowSum_unit] simp [rowSum,hv'] theorem repair_q_identity (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) (s t : Cell m n) (X : State m n) (hd : DefectAt U r c s t X) : (repair U r c s t X).q = fun w => X.q w - unit s w + if IsSmall U r c (receiver s t) then unit (receiver s t) w else 0 := by funext w have h := congrFun hd.2.2.2 w dsimp [repair] rw [h] ring theorem repair_colSum (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) (s t : Cell m n) (X : State m n) (hd : DefectAt U r c s t X) (j : Fin n) : colSum (repair U r c s t X).q j = colSum X.q j - if IsLarge U r c (receiver s t) ∧ j = s.2 then 1 else 0 := by rw [repair_q_identity U r c s t X hd] by_cases hv : IsSmall U r c (receiver s t) · simp only [ite_eq_left hv,colSum_sub_add,colSum_unit] have hv' : ¬IsLarge U r c (receiver s t) := hv have hnot : ¬(IsLarge U r c (receiver s t) ∧ j = s.2) := fun h => hv' h.1 rw [ite_eq_right hnot] dsimp [receiver] omega · have hv' : IsLarge U r c (receiver s t) := Classical.not_not.mp hv simp only [ite_eq_right hv] rw [colSum_sub_add,colSum_unit] simp [colSum,hv'] theorem coordinate_le_rowSum (x : Cell m n → ℤ) (hx : ∀ v, 0 ≤ x v) (s : Cell m n) : x s ≤ rowSum x s.1 := by apply Finset.single_le_sum (fun j _ => hx (s.1,j)) (Finset.mem_univ s.2) theorem coordinate_le_colSum (x : Cell m n → ℤ) (hx : ∀ v, 0 ≤ x v) (s : Cell m n) : x s ≤ colSum x s.2 := by apply Finset.single_le_sum (fun i _ => hx (i,s.2)) (Finset.mem_univ s.1) theorem repair_feasible (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) (s t : Cell m n) (X : State m n) (hx : Feasible U r c X) (hd : DefectAt U r c s t X) : Feasible U r c (repair U r c s t X) := by have hnn := repair_nonneg U r c s t X hx hd have hsup := repair_support U r c s t X hx hd have hr (i) : rowSum (repair U r c s t X).x i ≤ r i ∧ ((∀ j, ¬IsLarge U r c (i,j)) → rowSum (repair U r c s t X).x i = r i) := by rw [repair_rowSum] constructor · have h := (hx.2.2.1 i).1 split_ifs <;> omega · intro hi have h := (hx.2.2.1 i).2 hi have hn : ¬(IsLarge U r c (receiver s t) ∧ i = t.1) := by rintro ⟨hl, rfl⟩ exact hi s.2 hl rw [ite_eq_right hn,h] omega have hc (j) : colSum (repair U r c s t X).q j ≤ c j ∧ ((∀ i, ¬IsLarge U r c (i,j)) → colSum (repair U r c s t X).q j = c j) := by rw [repair_colSum U r c s t X hd] constructor · have h := (hx.2.2.2 j).1 split_ifs <;> omega · intro hj have h := (hx.2.2.2 j).2 hj have hn : ¬(IsLarge U r c (receiver s t) ∧ j = s.2) := by rintro ⟨hl, rfl⟩ exact hj t.1 hl rw [ite_eq_right hn,h] omega have hcap (w) : (repair U r c s t X).x w ≤ U := by by_cases hw : IsLarge U r c w · rw [hsup w hw]; positivity · have hw' : ¬(U ≤ r w.1 ∧ U ≤ c w.2) := hw have h1 := coordinate_le_rowSum _ hnn w have h2 := coordinate_le_colSum _ hnn w have h3 := (hr w.1).1 have h4 := (hc w.2).1 change colSum (repair U r c s t X).x w.2 ≤ c w.2 at h4 omega exact ⟨fun w => ⟨hnn w,hcap w,hnn w,hcap w⟩, fun w hw => ⟨hsup w hw,hsup w hw⟩,hr,hc⟩ theorem repair_injective (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) (s t : Cell m n) (X Y : State m n) (hX : DefectAt U r c s t X) (hY : DefectAt U r c s t Y) (he : repair U r c s t X = repair U r c s t Y) : X = Y := by have hx : X.x = Y.x := by funext w have h := congrArg (fun Z : State m n => Z.x w) he dsimp [repair] at h linarith have hq : X.q = Y.q := by rw [hX.2.2.2,hY.2.2.2,hx] cases X; cases Y simp_all def Before (s t : Cell m n) : Prop := s.1 < t.1 ∨ s.1 = t.1 ∧ s.2 < t.2 theorem before_ne (s t : Cell m n) (h : Before s t) : s ≠ t := by intro he subst t rcases h with h | h · exact (lt_irrefl _ h) · exact (lt_irrefl _ h.2) theorem before_trans (s t v : Cell m n) (hst : Before s t) (htv : Before t v) : Before s v := by rcases hst with hst | ⟨hst,hst'⟩ <;> rcases htv with htv | ⟨htv,htv'⟩ · exact Or.inl (lt_trans hst htv) · exact Or.inl (htv ▸ hst) · exact Or.inl (hst ▸ htv) · exact Or.inr ⟨hst.trans htv,lt_trans hst' htv'⟩ theorem receiver_order (s t : Cell m n) (hst : Before s t) : receiver s t = s ∨ Before s (receiver s t) := by rcases hst with hst | ⟨hst,_⟩ · exact Or.inr (Or.inl hst) · exact Or.inl (Prod.ext hst.symm rfl) theorem repair_prefix (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) (s t : Cell m n) (X : State m n) (hd : DefectAt U r c s t X) (hst : Before s t) (w : Cell m n) (hws : Before w s) : (repair U r c s t X).x w = X.x w ∧ (repair U r c s t X).q w = X.q w := by have hwt := before_ne w t (before_trans w s t hws hst) have hws' := before_ne w s hws have hwv : w ≠ receiver s t := by rcases receiver_order s t hst with he | he · rwa [he] · exact before_ne w _ (before_trans w s _ hws he) constructor · simp [repair,unit,hwt,hwv] · rw [repair_q_identity U r c s t X hd] simp [unit,hws',hwv] theorem repair_special (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) (s t : Cell m n) (X : State m n) (hd : DefectAt U r c s t X) (l : ℤ) (hl : X.x s = l-1) : (repair U r c s t X).x s = if t.1 = s.1 then l else l-1 := by have hst := hd.2.2.1 by_cases hi : t.1=s.1 · have hv : receiver s t = s := Prod.ext hi rfl simp [repair,unit,hst,hi,hv,hd.1,hl] · have hv : s ≠ receiver s t := by intro he exact hi (congrArg Prod.fst he).symm simp [repair,unit,hst,hi,hv,hl] end PaddedGraph end Contingency end end OAI