/- BooleanReduction.lean Theorem: Any Boolean predicate on `n` Boolean variables can be reduced to its global existential truth value in `n` iterations. This file provides a self-contained Lean 4 proof (no external dependencies, no `sorry`) across four canonical `n`-iteration reduction mechanisms, showing that all four yield the exact global existential truth value and that `n` iterations are tight for every `n ≥ 1`: 1. Single-operator iteration (`shiftElim`): a fixed local binary-OR operator `BoolPred n → BoolPred n` iterated `n` times produces the constant predicate equal to the global existential truth value. 2. Arity-reducing variable elimination (`elimVar` / `reduceByElim`): eliminating one Boolean variable per iteration reduces `BoolPred n` to `BoolPred 0 ≃ Bool`. 3. Coordinate-wise existential projection (`elimCoord` / `reduceCoords`): eliminating each of the `n` coordinates in `n` iterations (hypercube all-reduce / butterfly reduction) yields the constant existential predicate. 4. Hypercube neighborhood dilation (`dilate`): iterating 1-hop Hamming-ball expansion `n` times (the diameter of `{0, 1}^n`) yields the constant existential predicate. -/ /- Simplified: -- Each recursive call eliminates one variable. def Bits : Nat → Type | 0 => Unit | n + 1 => Bool × Bits n def reduce : (n : Nat) → (Bits n → Bool) → Bool | 0, P => P () | n + 1, P => reduce n (fun x => P (false, x) || P (true, x)) theorem correct (n : Nat) (P : Bits n → Bool) : reduce n P = true ↔ ∃ x, P x = true := by induction n with | zero => exact ⟨fun h => ⟨(), h⟩, fun ⟨(), h⟩ => h⟩ | succ n ih => simp [reduce, ih, Bits, Prod.exists, Bool.exists_bool, exists_or] -/ /- import Mathlib.Data.Fin.Tuple.Basic def R : ∀ n, ((Fin n → Bool) → Bool) → Bool | 0, p => p Fin.elim0 | n+1, p => R n fun x => p (Fin.cons false x) || p (Fin.cons true x) lemma T n p : R n p = true ↔ ∃ x, p x = true := by induction n <;> simp [R, *, Fin.exists_fin_succ_pi, exists_or]; rfl -/ /- def reduce : ∀ {n} (P : (Fin n → Bool) → Bool), { b : Bool // b = true ↔ ∃ x, P x = true } | 0, P => ⟨P Fin.elim0, ⟨(⟨Fin.elim0, ·⟩), fun ⟨x, h⟩ => by rwa [show x = Fin.elim0 from funext fun i => i.elim0] at h⟩⟩ | _ + 1, P => let ⟨b, h⟩ := reduce fun x => P (Fin.cases false x) || P (Fin.cases true x); ⟨b, h.trans ⟨fun ⟨_, h⟩ => (Bool.or_eq_true _ _ |>.mp h).elim (⟨_, ·⟩) (⟨_, ·⟩), fun ⟨y, h⟩ => ⟨y ∘ Fin.succ, by cases h0 : y 0 <;> simp [← h0, show Fin.cases (y 0) (y ∘ Fin.succ) = y from funext (Fin.cases rfl fun _ => rfl), h]⟩⟩⟩ -/ /- -- #0: program synthesis + Right-associated Nat.repeat def R : ∀ {n} (P : Nat.repeat (· × Bool) n Unit → Bool), { b // b = true ↔ ∃ x, P x = true } | 0, _ => ⟨_, ⟨(⟨(), ·⟩), fun ⟨(), h⟩ => h⟩⟩ | _ + 1, P => ⟨_, by rw [(R fun x => P (x, false) || P (x, true)).2]; simp [Nat.repeat]⟩ -- #1: Decidable directly (3 lines, 280 bytes, no rfl recurrence for decide) def D : ∀ {n} (P : Nat.repeat (· × Bool) n Unit → Bool), Decidable (∃ x, P x = true) | 0, P => decidable_of_iff (P () = true) ⟨(⟨(), ·⟩), fun ⟨(), h⟩ => h⟩ | _ + 1, P => @decidable_of_iff _ _ (by simp [Nat.repeat]) (D fun x => P (x, false) || P (x, true)) -- #3: -- Exact (Fin n → Bool) → Bool with #3 synthesis (3 lines, 514 bytes, rfl recurrence) def R : ∀ {n} (P : (Fin n → Bool) → Bool), { b // b = true ↔ ∃ x, P x = true } | 0, P => ⟨P Fin.elim0, ⟨(⟨_, ·⟩), fun ⟨x, h⟩ => (funext (·.elim0) : x = Fin.elim0) ▸ h⟩⟩ | _ + 1, P => ⟨_, by rw [(R fun x => P (Fin.cases false x) || P (Fin.cases true x)).2]; simp; exact ⟨fun ⟨_, h⟩ => h.elim (⟨_, ·⟩) (⟨_, ·⟩), fun ⟨y, h⟩ => ⟨y ∘ Fin.succ, by cases e : y 0 <;> simp [(funext (Fin.cases e.symm fun _ => rfl) : Fin.cases _ (y ∘ Fin.succ) = y), h]⟩⟩⟩ -/ /- def R : ∀ n (P : Nat.repeat (· × Bool) n Unit → Bool), {b : Bool // b ↔ ∃ x, P x} := Nat.rec (fun _ => ⟨_, ⟨(⟨(), ·⟩), fun ⟨(), h⟩ => h⟩⟩) fun _ r P => ⟨_, by rw [(r fun x => P (x, false) || P (x, true)).2]; simp [Nat.repeat]⟩ -/ namespace BooleanReduction /-- An `n`-variable Boolean predicate `{0, 1}^n → {0, 1}`. -/ abbrev BoolPred (n : Nat) := (Fin n → Bool) → Bool /-- Standard `k`-fold iteration of an endomorphism `f : α → α`. -/ def iterate {α : Type u} (f : α → α) : Nat → α → α | 0, a => a | k + 1, a => f (iterate f k a) /-! ### Helper operations on Boolean assignments `Fin n → Bool` -/ /-- Prepend a Boolean value to the front of a length-`k` assignment. -/ def cons {k : Nat} (b : Bool) (x : Fin k → Bool) : Fin (k + 1) → Bool | ⟨0, _⟩ => b | ⟨i + 1, h⟩ => x ⟨i, Nat.lt_of_succ_lt_succ h⟩ /-- Drop the first entry of a length-`(k + 1)` assignment. -/ def tail {k : Nat} (x : Fin (k + 1) → Bool) : Fin k → Bool := fun i => x i.succ /-- Append a Boolean value to the end of a length-`k` assignment. -/ def snoc {k : Nat} (x : Fin k → Bool) (b : Bool) : Fin (k + 1) → Bool := fun i => if h : i.val < k then x ⟨i.val, h⟩ else b /-- Flip coordinate `i` of an assignment `x`. -/ def flipBit {n : Nat} (i : Fin n) (x : Fin n → Bool) : Fin n → Bool := fun j => if j = i then !(x j) else x j theorem cons_head_tail {k : Nat} (x : Fin (k + 1) → Bool) : cons (x ⟨0, Nat.zero_lt_succ k⟩) (tail x) = x := by funext ⟨i, hi⟩ cases i <;> rfl theorem flipBit_self {n : Nat} (i : Fin n) (x : Fin n → Bool) : flipBit i x i = !(x i) := by simp [flipBit] theorem flipBit_ne {n : Nat} {i j : Fin n} (h : j ≠ i) (x : Fin n → Bool) : flipBit i x j = x j := by simp [flipBit, h] theorem flipBit_involutive {n : Nat} (i : Fin n) (x : Fin n → Bool) : flipBit i (flipBit i x) = x := by funext j by_cases h : j = i · subst h simp [flipBit] · simp [flipBit, h] theorem flipBit_comm {n : Nat} (i j : Fin n) (x : Fin n → Bool) : flipBit i (flipBit j x) = flipBit j (flipBit i x) := by funext k by_cases hki : k = i <;> by_cases hkj : k = j <;> simp_all [flipBit] theorem eq_not_of_ne_bool {a b : Bool} (h : a ≠ b) : a = !b := by cases a <;> cases b <;> simp_all /-! ### 1. Arity-Reducing Existential Elimination (`n` steps: `BoolPred n → BoolPred 0`) -/ /-- Eliminate one Boolean variable by existential projection (disjunction over `false` and `true`). -/ def elimVar {k : Nat} (P : BoolPred (k + 1)) : BoolPred k := fun x => P (cons false x) || P (cons true x) /-- Reduce an `n`-variable Boolean predicate to a `0`-variable predicate in `n` iterations. -/ def reduceByElim : {n : Nat} → BoolPred n → BoolPred 0 | 0, P => P | _ + 1, P => reduceByElim (elimVar P) /-- The global existential truth value of `P` as a `Bool`. -/ def globalExists {n : Nat} (P : BoolPred n) : Bool := reduceByElim P Fin.elim0 theorem reduceByElim_eq_true_iff : ∀ {n : Nat} (P : BoolPred n), reduceByElim P Fin.elim0 = true ↔ ∃ x : Fin n → Bool, P x = true | 0, P => by constructor · intro h exact ⟨Fin.elim0, h⟩ · rintro ⟨x, hx⟩ have hxeq : x = Fin.elim0 := by funext i exact i.elim0 simpa [reduceByElim, hxeq] using hx | k + 1, P => by rw [reduceByElim, reduceByElim_eq_true_iff (elimVar P)] constructor · rintro ⟨x, hx⟩ simp only [elimVar, Bool.or_eq_true] at hx rcases hx with h | h · exact ⟨cons false x, h⟩ · exact ⟨cons true x, h⟩ · rintro ⟨y, hy⟩ refine ⟨tail y, ?_⟩ simp only [elimVar, Bool.or_eq_true] have hc := cons_head_tail y cases h0 : y ⟨0, Nat.zero_lt_succ k⟩ · left rw [h0] at hc rwa [hc] · right rw [h0] at hc rwa [hc] theorem globalExists_eq_true_iff {n : Nat} (P : BoolPred n) : globalExists P = true ↔ ∃ x : Fin n → Bool, P x = true := reduceByElim_eq_true_iff P theorem globalExists_eq_false_iff {n : Nat} (P : BoolPred n) : globalExists P = false ↔ ∀ x : Fin n → Bool, P x = false := by constructor · intro h x cases hx : P x · rfl · have ht : globalExists P = true := (globalExists_eq_true_iff P).mpr ⟨x, hx⟩ rw [h] at ht contradiction · intro h cases hg : globalExists P · rfl · rcases (globalExists_eq_true_iff P).mp hg with ⟨x, hx⟩ rw [h x] at hx contradiction /-! ### 2. Single Fixed Endomorphism Iterated `n` Times (`shiftElim`) -/ /-- A single fixed local reduction operator on `BoolPred n`. Each iteration shifts inputs left by one coordinate and existentially projects the vacated coordinate using a single binary `||`. -/ def shiftElim : {n : Nat} → BoolPred n → BoolPred n | 0, P => P | _ + 1, P => fun x => P (snoc (tail x) false) || P (snoc (tail x) true) /-- Loop invariant: after `k ≤ n` iterations, the last `k` coordinates have been existentially quantified and the remaining `n - k` coordinates are shifted by `k`. -/ theorem iterate_shiftElim_invariant {n : Nat} (P : BoolPred n) : ∀ (k : Nat), k ≤ n → ∀ (x : Fin n → Bool), iterate shiftElim k P x = true ↔ ∃ y : Fin n → Bool, (∀ (i : Nat) (hi : i + k < n), y ⟨i, Nat.lt_of_le_of_lt (Nat.le_add_right i k) hi⟩ = x ⟨i + k, hi⟩) ∧ P y = true | 0, _, x => by simp only [iterate] constructor · intro h exact ⟨x, fun _ _ => rfl, h⟩ · rintro ⟨y, hy, hPy⟩ have hxy : y = x := by funext ⟨i, hi⟩ exact hy i hi rwa [← hxy] | k + 1, hk, x => by cases n with | zero => exact absurd hk (Nat.not_succ_le_zero k) | succ m => simp only [iterate, shiftElim, Bool.or_eq_true] have hk' : k ≤ m + 1 := Nat.le_of_succ_le hk rw [iterate_shiftElim_invariant P k hk' (snoc (tail x) false), iterate_shiftElim_invariant P k hk' (snoc (tail x) true)] constructor · intro h have step : ∀ b : Bool, (∃ y : Fin (m + 1) → Bool, (∀ (i : Nat) (hi : i + k < m + 1), y ⟨i, Nat.lt_of_le_of_lt (Nat.le_add_right i k) hi⟩ = snoc (tail x) b ⟨i + k, hi⟩) ∧ P y = true) → ∃ y : Fin (m + 1) → Bool, (∀ (i : Nat) (hi : i + (k + 1) < m + 1), y ⟨i, Nat.lt_of_le_of_lt (Nat.le_add_right i (k + 1)) hi⟩ = x ⟨i + (k + 1), hi⟩) ∧ P y = true := by rintro b ⟨y, hy, hPy⟩ refine ⟨y, ?_, hPy⟩ intro i hi have hik_m : i + k < m := Nat.lt_of_succ_lt_succ hi have hik : i + k < m + 1 := Nat.lt_succ_of_lt hik_m have hval := hy i hik simpa [snoc, tail, hik_m, Nat.add_assoc] using hval rcases h with h | h · exact step false h · exact step true h · rintro ⟨y, hy, hPy⟩ have hmk : m - k < m + 1 := Nat.lt_succ_of_le (Nat.sub_le m k) have claim : ∀ b : Bool, y ⟨m - k, hmk⟩ = b → ∃ y' : Fin (m + 1) → Bool, (∀ (i : Nat) (hi : i + k < m + 1), y' ⟨i, Nat.lt_of_le_of_lt (Nat.le_add_right i k) hi⟩ = snoc (tail x) b ⟨i + k, hi⟩) ∧ P y' = true := by intro b hb refine ⟨y, ?_, hPy⟩ intro i hi by_cases hik_m : i + k < m · have hi' : i + (k + 1) < m + 1 := Nat.succ_lt_succ hik_m have hval := hy i hi' simpa [snoc, tail, hik_m, Nat.add_assoc] using hval · have hik_eq : i + k = m := by omega have hi_eq : i = m - k := by omega simp only [snoc, hik_m, ↓reduceDIte] rw [← hb] congr 1 exact Fin.ext hi_eq cases hb : y ⟨m - k, hmk⟩ · exact Or.inl (claim false hb) · exact Or.inr (claim true hb) /-- Main theorem (fixed operator): `n` iterations of `shiftElim` reduce any `n`-variable Boolean predicate to its global existential truth value. -/ theorem iterate_shiftElim_eq_true_iff {n : Nat} (P : BoolPred n) (x : Fin n → Bool) : iterate shiftElim n P x = true ↔ ∃ y : Fin n → Bool, P y = true := by rw [iterate_shiftElim_invariant P n (Nat.le_refl n) x] constructor · rintro ⟨y, _, hPy⟩ exact ⟨y, hPy⟩ · rintro ⟨y, hPy⟩ refine ⟨y, ?_, hPy⟩ intro i hi exact absurd hi (Nat.not_lt_of_ge (Nat.le_add_left n i)) /-- After `n` iterations of `shiftElim`, `P` becomes the constant predicate `fun _ => globalExists P`. -/ theorem iterate_shiftElim_eq_const {n : Nat} (P : BoolPred n) : iterate shiftElim n P = fun _ => globalExists P := by funext x cases h : globalExists P · cases hs : iterate shiftElim n P x · rfl · have hex := (iterate_shiftElim_eq_true_iff P x).mp hs have hgt := (globalExists_eq_true_iff P).mpr hex rw [h] at hgt contradiction · exact (iterate_shiftElim_eq_true_iff P x).mpr ((globalExists_eq_true_iff P).mp h) /-- Tightness: for every `n ≥ 1`, `n - 1` iterations of `shiftElim` do not suffice. -/ theorem iterate_shiftElim_sharp (m : Nat) : ∃ (P : BoolPred (m + 1)) (x : Fin (m + 1) → Bool), iterate shiftElim m P x = false ∧ ∃ y : Fin (m + 1) → Bool, P y = true := by refine ⟨fun y => y ⟨0, Nat.zero_lt_succ m⟩, fun _ => false, ?_, ⟨fun _ => true, rfl⟩⟩ cases h : iterate shiftElim m (fun y => y ⟨0, Nat.zero_lt_succ m⟩) (fun _ => false) · rfl · rcases (iterate_shiftElim_invariant (fun y : Fin (m + 1) → Bool => y ⟨0, Nat.zero_lt_succ m⟩) m (Nat.le_succ m) (fun _ => false)).mp h with ⟨y, hy, hPy⟩ have h0 : y ⟨0, Nat.zero_lt_succ m⟩ = false := hy 0 (by omega) rw [h0] at hPy contradiction /-! ### 3. Coordinate-Wise Existential Closure / Hypercube All-Reduce (`elimCoord`) -/ /-- Existential projection along coordinate `i` (butterfly / hypercube fold step). -/ def elimCoord {n : Nat} (i : Fin n) (P : BoolPred n) : BoolPred n := fun x => P x || P (flipBit i x) theorem elimCoord_comm {n : Nat} (i j : Fin n) (P : BoolPred n) : elimCoord i (elimCoord j P) = elimCoord j (elimCoord i P) := by funext x simp only [elimCoord, flipBit_comm] cases P x <;> cases P (flipBit j x) <;> cases P (flipBit i x) <;> cases P (flipBit j (flipBit i x)) <;> rfl theorem elimCoord_idem {n : Nat} (i : Fin n) (P : BoolPred n) : elimCoord i (elimCoord i P) = elimCoord i P := by funext x simp only [elimCoord, flipBit_involutive] cases P x <;> cases P (flipBit i x) <;> rfl /-- Apply `elimCoord` across the first `k ≤ n` coordinates in `k` iterations. -/ def reduceCoordsAux {n : Nat} (P : BoolPred n) : (k : Nat) → k ≤ n → BoolPred n | 0, _ => P | k + 1, hk => elimCoord ⟨k, hk⟩ (reduceCoordsAux P k (Nat.le_of_succ_le hk)) /-- Reduce `P` in `n` iterations by eliminating coordinates `0, 1, …, n - 1`. -/ def reduceCoords {n : Nat} (P : BoolPred n) : BoolPred n := reduceCoordsAux P n (Nat.le_refl n) theorem reduceCoordsAux_invariant {n : Nat} (P : BoolPred n) : ∀ (k : Nat) (hk : k ≤ n) (x : Fin n → Bool), reduceCoordsAux P k hk x = true ↔ ∃ y : Fin n → Bool, (∀ j : Fin n, k ≤ j.val → y j = x j) ∧ P y = true | 0, _, x => by simp only [reduceCoordsAux] constructor · intro h exact ⟨x, fun _ _ => rfl, h⟩ · rintro ⟨y, hy, hPy⟩ have hxy : y = x := by funext j exact hy j (Nat.zero_le j.val) rwa [← hxy] | k + 1, hk, x => by simp only [reduceCoordsAux, elimCoord, Bool.or_eq_true] have hk' : k ≤ n := Nat.le_of_succ_le hk rw [reduceCoordsAux_invariant P k hk' x, reduceCoordsAux_invariant P k hk' (flipBit ⟨k, hk⟩ x)] constructor · rintro (⟨y, hy, hPy⟩ | ⟨y, hy, hPy⟩) · exact ⟨y, fun j hj => hy j (Nat.le_of_succ_le hj), hPy⟩ · refine ⟨y, ?_, hPy⟩ intro j hj have hne : j ≠ ⟨k, hk⟩ := fun heq => by subst heq exact Nat.not_succ_le_self k hj rw [hy j (Nat.le_of_succ_le hj), flipBit_ne hne] · rintro ⟨y, hy, hPy⟩ by_cases hEq : y ⟨k, hk⟩ = x ⟨k, hk⟩ · left refine ⟨y, ?_, hPy⟩ intro j hj by_cases hjk : j.val = k · have hjeq : j = ⟨k, hk⟩ := Fin.ext hjk subst hjeq exact hEq · exact hy j (by omega) · right refine ⟨y, ?_, hPy⟩ intro j hj by_cases hjk : j.val = k · have hjeq : j = ⟨k, hk⟩ := Fin.ext hjk subst hjeq rw [flipBit_self] exact eq_not_of_ne_bool hEq · have hne : j ≠ ⟨k, hk⟩ := fun heq => hjk (congrArg Fin.val heq) rw [flipBit_ne hne] exact hy j (by omega) /-- Main theorem (coordinate-wise all-reduce): `n` iterations of `elimCoord` reduce any `P : BoolPred n` to its global existential truth value. -/ theorem reduceCoords_eq_true_iff {n : Nat} (P : BoolPred n) (x : Fin n → Bool) : reduceCoords P x = true ↔ ∃ y : Fin n → Bool, P y = true := by rw [reduceCoords, reduceCoordsAux_invariant P n (Nat.le_refl n) x] constructor · rintro ⟨y, _, hPy⟩ exact ⟨y, hPy⟩ · rintro ⟨y, hPy⟩ refine ⟨y, ?_, hPy⟩ intro j hj exact absurd hj (Nat.not_le_of_gt j.isLt) theorem reduceCoords_eq_const {n : Nat} (P : BoolPred n) : reduceCoords P = fun _ => globalExists P := by funext x cases h : globalExists P · cases hr : reduceCoords P x · rfl · have hex := (reduceCoords_eq_true_iff P x).mp hr have hgt := (globalExists_eq_true_iff P).mpr hex rw [h] at hgt contradiction · exact (reduceCoords_eq_true_iff P x).mpr ((globalExists_eq_true_iff P).mp h) /-! ### 4. Hypercube Neighborhood Dilation (`dilate`, Hamming-ball expansion) -/ /-- Boolean disjunction of `f : Fin n → Bool` over all indices in `Fin n`. -/ def anyFin : (n : Nat) → (Fin n → Bool) → Bool | 0, _ => false | n + 1, f => f ⟨n, Nat.lt_succ_self n⟩ || anyFin n (fun i => f i.castSucc) theorem anyFin_eq_true_iff : ∀ (n : Nat) (f : Fin n → Bool), anyFin n f = true ↔ ∃ i : Fin n, f i = true | 0, _ => by simp [anyFin] | n + 1, f => by simp only [anyFin, Bool.or_eq_true, anyFin_eq_true_iff n] constructor · rintro (h | ⟨i, hi⟩) · exact ⟨⟨n, Nat.lt_succ_self n⟩, h⟩ · exact ⟨i.castSucc, hi⟩ · rintro ⟨j, hj⟩ by_cases hlt : j.val < n · right exact ⟨⟨j.val, hlt⟩, by simpa [Fin.castSucc] using hj⟩ · left have hjeq : j = ⟨n, Nat.lt_succ_self n⟩ := Fin.ext (show j.val = n by have := j.isLt; omega) rwa [← hjeq] /-- One-hop Hamming neighborhood dilation on the `n`-dimensional Boolean hypercube. -/ def dilate {n : Nat} (P : BoolPred n) : BoolPred n := fun x => P x || anyFin n (fun i => P (flipBit i x)) /-- Number of coordinates `< m` where `x` and `y` disagree. -/ def hammingDistPrefix {n : Nat} (x y : Fin n → Bool) : (m : Nat) → m ≤ n → Nat | 0, _ => 0 | m + 1, hm => hammingDistPrefix x y m (Nat.le_of_succ_le hm) + (if x ⟨m, hm⟩ = y ⟨m, hm⟩ then 0 else 1) /-- Hamming distance between two points of `{0, 1}^n`. -/ def hammingDist {n : Nat} (x y : Fin n → Bool) : Nat := hammingDistPrefix x y n (Nat.le_refl n) theorem hammingDistPrefix_le {n : Nat} (x y : Fin n → Bool) : ∀ (m : Nat) (hm : m ≤ n), hammingDistPrefix x y m hm ≤ m | 0, _ => Nat.le_refl 0 | m + 1, hm => by have ih := hammingDistPrefix_le x y m (Nat.le_of_succ_le hm) simp only [hammingDistPrefix] split <;> omega theorem hammingDist_le {n : Nat} (x y : Fin n → Bool) : hammingDist x y ≤ n := hammingDistPrefix_le x y n (Nat.le_refl n) theorem hammingDistPrefix_eq_zero_iff {n : Nat} (x y : Fin n → Bool) : ∀ (m : Nat) (hm : m ≤ n), hammingDistPrefix x y m hm = 0 ↔ ∀ j : Fin n, j.val < m → x j = y j | 0, _ => by simp [hammingDistPrefix] | m + 1, hm => by have ih := hammingDistPrefix_eq_zero_iff x y m (Nat.le_of_succ_le hm) simp only [hammingDistPrefix] constructor · intro h j hj by_cases hjm : j.val < m · have hprev : hammingDistPrefix x y m (Nat.le_of_succ_le hm) = 0 := by omega exact ih.mp hprev j hjm · have hjeq : j = ⟨m, hm⟩ := Fin.ext (show j.val = m by omega) subst hjeq by_cases hxy : x ⟨m, hm⟩ = y ⟨m, hm⟩ · exact hxy · simp only [hxy, ↓reduceIte] at h omega · intro h have hprev : hammingDistPrefix x y m (Nat.le_of_succ_le hm) = 0 := ih.mpr (fun j hj => h j (Nat.lt_succ_of_lt hj)) have hlast : x ⟨m, hm⟩ = y ⟨m, hm⟩ := h ⟨m, hm⟩ (Nat.lt_succ_self m) simp [hprev, hlast] theorem hammingDist_eq_zero_iff {n : Nat} (x y : Fin n → Bool) : hammingDist x y = 0 ↔ x = y := by rw [hammingDist, hammingDistPrefix_eq_zero_iff] constructor · intro h funext j exact h j j.isLt · intro h j _ rw [h] theorem exists_ne_of_hammingDistPrefix_pos {n : Nat} (x y : Fin n → Bool) : ∀ (m : Nat) (hm : m ≤ n), 0 < hammingDistPrefix x y m hm → ∃ i : Fin n, x i ≠ y i | 0, _, h => absurd h (Nat.lt_irrefl 0) | m + 1, hm, h => by simp only [hammingDistPrefix] at h by_cases hxy : x ⟨m, hm⟩ = y ⟨m, hm⟩ · simp only [hxy, ↓reduceIte] at h exact exists_ne_of_hammingDistPrefix_pos x y m (Nat.le_of_succ_le hm) (by omega) · exact ⟨⟨m, hm⟩, hxy⟩ theorem exists_ne_of_hammingDist_pos {n : Nat} {x y : Fin n → Bool} (h : 0 < hammingDist x y) : ∃ i : Fin n, x i ≠ y i := exists_ne_of_hammingDistPrefix_pos x y n (Nat.le_refl n) h theorem hammingDistPrefix_flipBit_of_le {n : Nat} (i : Fin n) (x y : Fin n → Bool) : ∀ (m : Nat) (hm : m ≤ n), m ≤ i.val → hammingDistPrefix (flipBit i x) y m hm = hammingDistPrefix x y m hm | 0, _, _ => rfl | m + 1, hm, hmi => by have ih := hammingDistPrefix_flipBit_of_le i x y m (Nat.le_of_succ_le hm) (Nat.le_of_succ_le hmi) have hne : (⟨m, hm⟩ : Fin n) ≠ i := fun heq => by have : m = i.val := congrArg Fin.val heq omega simp only [hammingDistPrefix, ih, flipBit_ne hne] theorem hammingDistPrefix_flipBit_of_ne {n : Nat} (i : Fin n) (x y : Fin n → Bool) (hxy : x i ≠ y i) : ∀ (m : Nat) (hm : m ≤ n), i.val < m → hammingDistPrefix (flipBit i x) y m hm + 1 = hammingDistPrefix x y m hm | 0, _, him => absurd him (Nat.not_lt_zero _) | m + 1, hm, him => by by_cases hlt : i.val < m · have ih := hammingDistPrefix_flipBit_of_ne i x y hxy m (Nat.le_of_succ_le hm) hlt have hne : (⟨m, hm⟩ : Fin n) ≠ i := fun heq => by have : m = i.val := congrArg Fin.val heq omega simp only [hammingDistPrefix, flipBit_ne hne] omega · have hmeq : m = i.val := by omega have hieq : (⟨m, hm⟩ : Fin n) = i := Fin.ext hmeq have hprev := hammingDistPrefix_flipBit_of_le i x y m (Nat.le_of_succ_le hm) (Nat.le_of_eq hmeq) simp only [hammingDistPrefix, hprev, hieq, flipBit_self] cases hx : x i <;> cases hy : y i <;> simp_all theorem hammingDistPrefix_flipBit_of_eq {n : Nat} (i : Fin n) (x y : Fin n → Bool) (hxy : x i = y i) : ∀ (m : Nat) (hm : m ≤ n), i.val < m → hammingDistPrefix (flipBit i x) y m hm = hammingDistPrefix x y m hm + 1 | 0, _, him => absurd him (Nat.not_lt_zero _) | m + 1, hm, him => by by_cases hlt : i.val < m · have ih := hammingDistPrefix_flipBit_of_eq i x y hxy m (Nat.le_of_succ_le hm) hlt have hne : (⟨m, hm⟩ : Fin n) ≠ i := fun heq => by have : m = i.val := congrArg Fin.val heq omega simp only [hammingDistPrefix, flipBit_ne hne] omega · have hmeq : m = i.val := by omega have hieq : (⟨m, hm⟩ : Fin n) = i := Fin.ext hmeq have hprev := hammingDistPrefix_flipBit_of_le i x y m (Nat.le_of_succ_le hm) (Nat.le_of_eq hmeq) simp only [hammingDistPrefix, hprev, hieq, flipBit_self] cases hx : x i <;> cases hy : y i <;> simp_all theorem hammingDist_flipBit_of_ne {n : Nat} {i : Fin n} {x y : Fin n → Bool} (hxy : x i ≠ y i) : hammingDist (flipBit i x) y + 1 = hammingDist x y := hammingDistPrefix_flipBit_of_ne i x y hxy n (Nat.le_refl n) i.isLt theorem hammingDist_le_flipBit_succ {n : Nat} (i : Fin n) (x y : Fin n → Bool) : hammingDist x y ≤ hammingDist (flipBit i x) y + 1 := by by_cases hxy : x i = y i · have h := hammingDistPrefix_flipBit_of_eq i x y hxy n (Nat.le_refl n) i.isLt change hammingDist (flipBit i x) y = hammingDist x y + 1 at h omega · have h := hammingDist_flipBit_of_ne hxy omega /-- After `k` iterations of `dilate`, `(iterate dilate k P) x` holds iff some satisfying assignment `y` lies within Hamming distance `k` of `x`. -/ theorem iterate_dilate_invariant {n : Nat} (P : BoolPred n) : ∀ (k : Nat) (x : Fin n → Bool), iterate dilate k P x = true ↔ ∃ y : Fin n → Bool, hammingDist x y ≤ k ∧ P y = true | 0, x => by simp only [iterate] constructor · intro h exact ⟨x, by rw [(hammingDist_eq_zero_iff x x).mpr rfl]; exact Nat.le_refl 0, h⟩ · rintro ⟨y, hdist, hPy⟩ have h0 : hammingDist x y = 0 := Nat.eq_zero_of_le_zero hdist have hxy : x = y := (hammingDist_eq_zero_iff x y).mp h0 rwa [hxy] | k + 1, x => by simp only [iterate, dilate, Bool.or_eq_true, anyFin_eq_true_iff] rw [iterate_dilate_invariant P k x] constructor · rintro (⟨y, hdist, hPy⟩ | ⟨i, hi⟩) · exact ⟨y, Nat.le_succ_of_le hdist, hPy⟩ · rcases (iterate_dilate_invariant P k (flipBit i x)).mp hi with ⟨y, hdist, hPy⟩ have hstep := hammingDist_le_flipBit_succ i x y exact ⟨y, by omega, hPy⟩ · rintro ⟨y, hdist, hPy⟩ by_cases hle : hammingDist x y ≤ k · exact Or.inl ⟨y, hle, hPy⟩ · right have hpos : 0 < hammingDist x y := by omega rcases exists_ne_of_hammingDist_pos hpos with ⟨i, hne⟩ refine ⟨i, (iterate_dilate_invariant P k (flipBit i x)).mpr ⟨y, ?_, hPy⟩⟩ have hdec := hammingDist_flipBit_of_ne hne omega /-- Main theorem (hypercube dilation): `n` iterations of `dilate` reduce any `n`-variable Boolean predicate to its global existential truth value. -/ theorem iterate_dilate_eq_true_iff {n : Nat} (P : BoolPred n) (x : Fin n → Bool) : iterate dilate n P x = true ↔ ∃ y : Fin n → Bool, P y = true := by rw [iterate_dilate_invariant P n x] constructor · rintro ⟨y, _, hPy⟩ exact ⟨y, hPy⟩ · rintro ⟨y, hPy⟩ exact ⟨y, hammingDist_le x y, hPy⟩ theorem iterate_dilate_eq_const {n : Nat} (P : BoolPred n) : iterate dilate n P = fun _ => globalExists P := by funext x cases h : globalExists P · cases hd : iterate dilate n P x · rfl · have hex := (iterate_dilate_eq_true_iff P x).mp hd have hgt := (globalExists_eq_true_iff P).mpr hex rw [h] at hgt contradiction · exact (iterate_dilate_eq_true_iff P x).mpr ((globalExists_eq_true_iff P).mp h) theorem hammingDistPrefix_false_true {n : Nat} : ∀ (m : Nat) (hm : m ≤ n), hammingDistPrefix (fun _ : Fin n => false) (fun _ : Fin n => true) m hm = m | 0, _ => rfl | m + 1, hm => by simp [hammingDistPrefix, hammingDistPrefix_false_true m (Nat.le_of_succ_le hm)] /-- Tightness for dilation: for every `n ≥ 1`, `n - 1` iterations of `dilate` do not suffice. -/ theorem iterate_dilate_sharp (m : Nat) : ∃ (P : BoolPred (m + 1)) (x : Fin (m + 1) → Bool), iterate dilate m P x = false ∧ ∃ y : Fin (m + 1) → Bool, P y = true := by let P : BoolPred (m + 1) := fun y => hammingDist y (fun _ => true) == 0 refine ⟨P, fun _ => false, ?_, ⟨fun _ => true, ?_⟩⟩ · cases h : iterate dilate m P (fun _ => false) · rfl · rcases (iterate_dilate_invariant P m (fun _ => false)).mp h with ⟨y, hdist, hPy⟩ have h0 : hammingDist y (fun _ => true) = 0 := Nat.eq_of_beq_eq_true hPy have hy : y = fun _ => true := (hammingDist_eq_zero_iff y (fun _ => true)).mp h0 rw [hy, hammingDist, hammingDistPrefix_false_true] at hdist omega · show (hammingDist (fun _ : Fin (m + 1) => true) (fun _ => true) == 0) = true rw [(hammingDist_eq_zero_iff (fun _ => true) (fun _ => true)).mpr rfl] rfl /-- Tightness for coordinate-wise projection: `n - 1` coordinate steps do not suffice. -/ theorem reduceCoords_sharp (m : Nat) : ∃ (P : BoolPred (m + 1)) (x : Fin (m + 1) → Bool), reduceCoordsAux P m (Nat.le_succ m) x = false ∧ ∃ y : Fin (m + 1) → Bool, P y = true := by refine ⟨fun y => y ⟨m, Nat.lt_succ_self m⟩, fun _ => false, ?_, ⟨fun _ => true, rfl⟩⟩ cases h : reduceCoordsAux (fun y : Fin (m + 1) → Bool => y ⟨m, Nat.lt_succ_self m⟩) m (Nat.le_succ m) (fun _ => false) · rfl · rcases (reduceCoordsAux_invariant (fun y : Fin (m + 1) → Bool => y ⟨m, Nat.lt_succ_self m⟩) m (Nat.le_succ m) (fun _ => false)).mp h with ⟨y, hy, hPy⟩ have hm : y ⟨m, Nat.lt_succ_self m⟩ = false := hy ⟨m, Nat.lt_succ_self m⟩ (Nat.le_refl m) rw [hm] at hPy contradiction /-! ### 5. Unified Summary: Equality with the Decidable Global Existential Truth Value -/ /-- The existential truth value of any `n`-variable Boolean predicate is decidable. -/ instance decidableExistsBoolPred {n : Nat} (P : BoolPred n) : Decidable (∃ x : Fin n → Bool, P x = true) := if h : globalExists P = true then isTrue ((globalExists_eq_true_iff P).mp h) else isFalse (fun hex => h ((globalExists_eq_true_iff P).mpr hex)) theorem globalExists_eq_decide {n : Nat} (P : BoolPred n) : globalExists P = decide (∃ x : Fin n → Bool, P x = true) := by cases h : globalExists P · have hnot : ¬ ∃ x : Fin n → Bool, P x = true := by intro hex have ht := (globalExists_eq_true_iff P).mpr hex rw [h] at ht contradiction simp [hnot] · have hex : ∃ x : Fin n → Bool, P x = true := (globalExists_eq_true_iff P).mp h simp [hex] /-- Summary theorem: every `n`-variable Boolean predicate is reduced in `n` iterations to its global existential truth value `decide (∃ y, P y = true)`. -/ theorem reduce_to_global_existential_truth_value {n : Nat} (P : BoolPred n) (x : Fin n → Bool) : reduceByElim P Fin.elim0 = decide (∃ y : Fin n → Bool, P y = true) ∧ iterate shiftElim n P x = decide (∃ y : Fin n → Bool, P y = true) ∧ reduceCoords P x = decide (∃ y : Fin n → Bool, P y = true) ∧ iterate dilate n P x = decide (∃ y : Fin n → Bool, P y = true) := by have h0 : globalExists P = decide (∃ y : Fin n → Bool, P y = true) := globalExists_eq_decide P refine ⟨h0, ?_, ?_, ?_⟩ · rw [iterate_shiftElim_eq_const, h0] · rw [reduceCoords_eq_const, h0] · rw [iterate_dilate_eq_const, h0] end BooleanReduction