import Mathlib.Data.Fin.Tuple.Basic import Mathlib.Data.Fintype.BigOperators import Mathlib.Data.Fintype.Pi import Mathlib.Data.Fintype.Prod import Mathlib.Data.Fintype.Sum import OAI.Computability.BinPacking.PCP.BinaryFormula import OAI.Computability.BinPacking.PCP.CloudRounding namespace OAI namespace BinPackingGames.Foundations.PCP.QueryIncidence structure Verifier (E X : Type*) (q : Nat) where query : E → Fin q → X accepts : E → (Fin q → Bool) → Bool abbrev Label (q : Nat) := Fin q → Bool abbrev Vertex (E X : Type*) := E ⊕ X abbrev Dart (E : Type*) (q : Nat) := (E × Fin q) × Bool variable {E X : Type*} {q : Nat} def response (T : Verifier E X q) (assignment : X → Bool) (event : E) : Label q := fun i => assignment (T.query event i) def zeroSlot (positive : 0 < q) : Fin q := ⟨0, positive⟩ def decodeRight (positive : 0 < q) (label : Label q) : Bool := label (zeroSlot positive) def encodeRight (bit : Bool) : Label q := fun _ => bit @[simp] theorem decode_encodeRight (positive : 0 < q) (bit : Bool) : decodeRight positive (encodeRight bit) = bit := rfl def canonicalSlot [DecidableEq X] (T : Verifier E X q) (event : E) (i : Fin q) : Fin q := Fin.find (fun j => T.query event j = T.query event i) ⟨i, rfl⟩ theorem canonicalSlot_query [DecidableEq X] (T : Verifier E X q) (event : E) (i : Fin q) : T.query event (canonicalSlot T event i) = T.query event i := Fin.find_spec (p := fun j => T.query event j = T.query event i) ⟨i, rfl⟩ def RepeatedConsistent (T : Verifier E X q) (event : E) (label : Label q) : Prop := ∀ i j, T.query event i = T.query event j → label i = label j instance repeatedConsistentDecidable [DecidableEq X] (T : Verifier E X q) (event : E) (label : Label q) : Decidable (RepeatedConsistent T event label) := by unfold RepeatedConsistent infer_instance def LeftValid (T : Verifier E X q) (event : E) (label : Label q) : Prop := T.accepts event label = true ∧ RepeatedConsistent T event label instance leftValidDecidable [DecidableEq X] (T : Verifier E X q) (event : E) (label : Label q) : Decidable (LeftValid T event label) := by unfold LeftValid infer_instance def incidenceAccept [DecidableEq X] (T : Verifier E X q) (positive : 0 < q) (event : E) (slot : Fin q) (left right : Label q) : Bool := decide (LeftValid T event left) && decide (left (canonicalSlot T event slot) = decodeRight positive right) def reverse : Dart E q → Dart E q := fun d => (d.1, !d.2) theorem reverse_involutive : Function.Involutive (reverse : Dart E q → Dart E q) := by rintro ⟨⟨event, slot⟩, orientation⟩ cases orientation <;> rfl def reverseEquiv : Dart E q ≃ Dart E q where toFun := reverse invFun := reverse left_inv := reverse_involutive right_inv := reverse_involutive def tail (T : Verifier E X q) : Dart E q → Vertex E X | ((event, _), false) => .inl event | ((event, slot), true) => .inr (T.query event slot) def predicate [DecidableEq X] (T : Verifier E X q) (positive : 0 < q) : Dart E q → Label q → Label q → Bool | ((event, slot), false), a, b => incidenceAccept T positive event slot a b | ((event, slot), true), a, b => incidenceAccept T positive event slot b a def graph [DecidableEq X] (T : Verifier E X q) (positive : 0 < q) : ConstraintGraph (Vertex E X) (Dart E q) (Label q) where reverse := reverseEquiv reverse_involutive := reverse_involutive tail := tail T accepts := predicate T positive reverse_accepts := by rintro ⟨⟨event, slot⟩, orientation⟩ a b cases orientation <;> rfl theorem edgeSatisfied_eq_incidence [DecidableEq X] (T : Verifier E X q) (positive : 0 < q) (labeling : Vertex E X → Label q) (event : E) (slot : Fin q) (orientation : Bool) : (graph T positive).edgeSatisfied labeling ((event, slot), orientation) = incidenceAccept T positive event slot (labeling (.inl event)) (labeling (.inr (T.query event slot))) := by cases orientation <;> rfl def honestLabeling (T : Verifier E X q) (assignment : X → Bool) : Vertex E X → Label q | .inl event => response T assignment event | .inr address => encodeRight (assignment address) theorem honest_leftValid (T : Verifier E X q) (assignment : X → Bool) (event : E) (accepted : T.accepts event (response T assignment event) = true) : LeftValid T event (response T assignment event) := by refine ⟨accepted, ?_⟩ intro i j hij exact congrArg assignment hij theorem honest_incidence [DecidableEq X] (T : Verifier E X q) (positive : 0 < q) (assignment : X → Bool) (event : E) (accepted : T.accepts event (response T assignment event) = true) (slot : Fin q) : incidenceAccept T positive event slot (honestLabeling T assignment (.inl event)) (honestLabeling T assignment (.inr (T.query event slot))) = true := by simp only [incidenceAccept, Bool.and_eq_true, honestLabeling, decode_encodeRight] refine ⟨_root_.decide_eq_true (honest_leftValid T assignment event accepted), ?_⟩ exact _root_.decide_eq_true (congrArg assignment (canonicalSlot_query T event slot)) theorem perfect_completeness [DecidableEq X] (T : Verifier E X q) (positive : 0 < q) (assignment : X → Bool) (accepted : ∀ event, T.accepts event (response T assignment event) = true) : ∀ dart, (graph T positive).edgeSatisfied (honestLabeling T assignment) dart = true := by rintro ⟨⟨event, slot⟩, orientation⟩ rw [edgeSatisfied_eq_incidence] exact honest_incidence T positive assignment event (accepted event) slot theorem satisfiable_of_verifier_satisfiable [DecidableEq X] (T : Verifier E X q) (positive : 0 < q) (sat : ∃ assignment : X → Bool, ∀ event, T.accepts event (response T assignment event) = true) : (graph T positive).Satisfiable := by obtain ⟨assignment, h⟩ := sat exact ⟨honestLabeling T assignment, perfect_completeness T positive assignment h⟩ def decodedAssignment (positive : 0 < q) (labeling : Vertex E X → Label q) : X → Bool := fun address => decodeRight positive (labeling (.inr address)) theorem incidenceAccept_true [DecidableEq X] (T : Verifier E X q) (positive : 0 < q) (event : E) (slot : Fin q) (left right : Label q) (accepted : incidenceAccept T positive event slot left right = true) : T.accepts event left = true ∧ RepeatedConsistent T event left ∧ left (canonicalSlot T event slot) = decodeRight positive right := by simp only [incidenceAccept, Bool.and_eq_true] at accepted have hvalid : LeftValid T event left := _root_.of_decide_eq_true accepted.1 exact ⟨hvalid.1, hvalid.2, _root_.of_decide_eq_true accepted.2⟩ theorem all_incidences_imply_event [DecidableEq X] (T : Verifier E X q) (positive : 0 < q) (labeling : Vertex E X → Label q) (event : E) (accepted : ∀ slot, incidenceAccept T positive event slot (labeling (.inl event)) (labeling (.inr (T.query event slot))) = true) : T.accepts event (response T (decodedAssignment positive labeling) event) = true := by have first := incidenceAccept_true T positive event (zeroSlot positive) _ _ (accepted (zeroSlot positive)) have labels_equal : labeling (.inl event) = response T (decodedAssignment positive labeling) event := by funext slot have info := incidenceAccept_true T positive event slot _ _ (accepted slot) exact (first.2.1 slot (canonicalSlot T event slot) (canonicalSlot_query T event slot).symm).trans info.2.2 rw [← labels_equal] exact first.1 theorem rejected_event_forces_slot [DecidableEq X] (T : Verifier E X q) (positive : 0 < q) (labeling : Vertex E X → Label q) (event : E) (rejected : T.accepts event (response T (decodedAssignment positive labeling) event) = false) : ∃ slot, incidenceAccept T positive event slot (labeling (.inl event)) (labeling (.inr (T.query event slot))) = false := by classical by_contra none have all : ∀ slot, incidenceAccept T positive event slot (labeling (.inl event)) (labeling (.inr (T.query event slot))) = true := by intro slot cases h : incidenceAccept T positive event slot (labeling (.inl event)) (labeling (.inr (T.query event slot))) with | false => exact False.elim (none ⟨slot, h⟩) | true => rfl have h := all_incidences_imply_event T positive labeling event all rw [rejected] at h cases h def rejectedEvents [Fintype E] (T : Verifier E X q) (assignment : X → Bool) : Finset E := Finset.univ.filter (fun event => T.accepts event (response T assignment event) = false) def verifierRejectionCount [Fintype E] (T : Verifier E X q) (assignment : X → Bool) : Nat := (rejectedEvents T assignment).card theorem rejection_count_bound [Fintype E] [DecidableEq X] (T : Verifier E X q) (positive : 0 < q) (labeling : Vertex E X → Label q) : 2 * verifierRejectionCount T (decodedAssignment positive labeling) ≤ (graph T positive).rejectionCount labeling := by classical let failed := rejectedEvents T (decodedAssignment positive labeling) have witnesses : ∀ event, event ∈ failed → ∃ slot, incidenceAccept T positive event slot (labeling (.inl event)) (labeling (.inr (T.query event slot))) = false := by intro event he apply rejected_event_forces_slot T positive labeling event simpa [failed, rejectedEvents] using he let chosen := fun event he => Classical.choose (witnesses event he) let inject : (failed.product (Finset.univ : Finset Bool)) → (graph T positive).rejectedDarts labeling := fun p => ⟨((p.val.1, chosen p.val.1 (Finset.mem_product.mp p.property).1), p.val.2), by apply (ConstraintGraph.mem_rejectedDarts _ _ _).mpr rw [edgeSatisfied_eq_incidence] exact Classical.choose_spec (witnesses p.val.1 (Finset.mem_product.mp p.property).1)⟩ have injective : Function.Injective inject := by intro a b h apply Subtype.ext apply Prod.ext · exact congrArg (fun d => d.val.1.1) h · exact congrArg (fun d => d.val.2) h have bound := Finset.card_le_card_of_injective injective simpa [failed, verifierRejectionCount, ConstraintGraph.rejectionCount, Finset.card_product, Nat.mul_comm] using bound @[simp] theorem card_label (q : Nat) : Fintype.card (Label q) = 2 ^ q := by simp [Label] @[simp] theorem card_vertex [Fintype E] [Fintype X] : Fintype.card (Vertex E X) = Fintype.card E + Fintype.card X := by simp [Vertex, Fintype.card_sum] @[simp] theorem card_dart [Fintype E] : Fintype.card (Dart E q) = 2 * q * Fintype.card E := by simp [Dart, Fintype.card_prod, Nat.mul_comm, Nat.mul_left_comm, Nat.mul_assoc] theorem gap_transfer [Fintype E] [DecidableEq X] (T : Verifier E X q) (positive : 0 < q) (a b : Nat) (sourceGap : ∀ assignment : X → Bool, a * Fintype.card E ≤ b * verifierRejectionCount T assignment) (labeling : Vertex E X → Label q) : a * Fintype.card (Dart E q) ≤ (b * q) * (graph T positive).rejectionCount labeling := by have source := Nat.mul_le_mul_left (2 * q) (sourceGap (decodedAssignment positive labeling)) have incidences := Nat.mul_le_mul_left (b * q) (rejection_count_bound T positive labeling) rw [card_dart] calc a * (2 * q * Fintype.card E) = (2 * q) * (a * Fintype.card E) := by ac_rfl _ ≤ (2 * q) * (b * verifierRejectionCount T (decodedAssignment positive labeling)) := source _ = (b * q) * (2 * verifierRejectionCount T (decodedAssignment positive labeling)) := by ac_rfl _ ≤ (b * q) * (graph T positive).rejectionCount labeling := incidences theorem six_query_alphabet : Fintype.card (Label 6) = 64 := by simp end BinPackingGames.Foundations.PCP.QueryIncidence namespace BinPackingGames.Foundations.PCP.InitialGraph open Target abbrev Label := Bool × Bool × Bool abbrev Vertex (F : Formula) := (Fin F.variables ⊕ Fin F.clauses.length) ⊕ Unit abbrev Dart (F : Formula) := (RandomEvent F × Bool) ⊕ Unit instance slotFintype : Fintype Slot where elems := {Slot.first, Slot.second, Slot.third} complete := by intro s; cases s <;> simp @[simp] theorem card_slot : Fintype.card Slot = 3 := by decide @[simp] theorem card_label : Fintype.card Label = 8 := by simp [Label, Fintype.card_prod] def variableVertex (F : Formula) (v : Fin F.variables) : Vertex F := .inl (.inl v) def clauseVertex (F : Formula) (i : Fin F.clauses.length) : Vertex F := .inl (.inr i) def dummyVertex (F : Formula) : Vertex F := .inr () def dummyDart (F : Formula) : Dart F := .inr () instance vertexNonempty (F : Formula) : Nonempty (Vertex F) := ⟨dummyVertex F⟩ instance dartNonempty (F : Formula) : Nonempty (Dart F) := ⟨dummyDart F⟩ def reverse (F : Formula) : Dart F → Dart F | .inl (event, orientation) => .inl (event, !orientation) | .inr _ => .inr () theorem reverse_involutive (F : Formula) : Function.Involutive (reverse F) := by intro d cases d with | inl pair => rcases pair with ⟨e, b⟩; cases b <;> rfl | inr u => cases u; rfl def reverseEquiv (F : Formula) : Dart F ≃ Dart F where toFun := reverse F invFun := reverse F left_inv := reverse_involutive F right_inv := reverse_involutive F def tail (F : Formula) : Dart F → Vertex F | .inl (event, false) => clauseVertex F event.1 | .inl (event, true) => variableVertex F (nameAt (clauseAt F event.1) event.2) | .inr _ => dummyVertex F def toClauseAnswer (label : Label) : ClauseAnswer := ⟨label.1, label.2.1, label.2.2⟩ def fromClauseAnswer (answer : ClauseAnswer) : Label := (answer.first, answer.second, answer.third) def variableLabel (value : Bool) : Label := (value, false, false) @[simp] theorem to_fromClauseAnswer (answer : ClauseAnswer) : toClauseAnswer (fromClauseAnswer answer) = answer := by cases answer; rfl def variableValid (label : Label) : Bool := decide (label.2.1 = false ∧ label.2.2 = false) def incidenceAccept (F : Formula) (event : RandomEvent F) (clauseLabel variableAnswer : Label) : Bool := variableValid variableAnswer && (localSatisfies (clauseAt F event.1) (toClauseAnswer clauseLabel) && decide (answerAt (toClauseAnswer clauseLabel) event.2 = variableAnswer.1)) def predicate (F : Formula) : Dart F → Label → Label → Bool | .inl (event, false), a, b => incidenceAccept F event a b | .inl (event, true), a, b => incidenceAccept F event b a | .inr _, _, _ => true def raw (F : Formula) : ConstraintGraph (Vertex F) (Dart F) Label where reverse := reverseEquiv F reverse_involutive := reverse_involutive F tail := tail F accepts := predicate F reverse_accepts := by intro d a b cases d with | inl pair => rcases pair with ⟨event, orientation⟩; cases orientation <;> rfl | inr u => rfl theorem raw_forward (F : Formula) (labeling : Vertex F → Label) (event : RandomEvent F) : (raw F).edgeSatisfied labeling (.inl (event, false)) = incidenceAccept F event (labeling (clauseVertex F event.1)) (labeling (variableVertex F (nameAt (clauseAt F event.1) event.2))) := rfl theorem incidenceAccept_true (F : Formula) (event : RandomEvent F) (a b : Label) (h : incidenceAccept F event a b = true) : localSatisfies (clauseAt F event.1) (toClauseAnswer a) = true ∧ answerAt (toClauseAnswer a) event.2 = b.1 := by simp only [incidenceAccept, Bool.and_eq_true, decide_eq_true_eq] at h exact h.2 def honestLabeling (F : Formula) (A : Fin F.variables → Bool) : Vertex F → Label | .inl (.inl v) => variableLabel (A v) | .inl (.inr i) => fromClauseAnswer (honestAnswer (clauseAt F i) A) | .inr _ => variableLabel false theorem incidence_honest (F : Formula) (A : Fin F.variables → Bool) (sat : ∀ c ∈ F.clauses, c.eval A = true) (event : RandomEvent F) : incidenceAccept F event (honestLabeling F A (clauseVertex F event.1)) (honestLabeling F A (variableVertex F (nameAt (clauseAt F event.1) event.2))) = true := by have hc : (clauseAt F event.1).eval A = true := sat _ (List.getElem_mem _) simp [incidenceAccept, honestLabeling, clauseVertex, variableVertex, variableLabel, variableValid, honest_satisfies, honest_answerAt, hc] theorem raw_completeness (F : Formula) (sat : F.Satisfiable) : (raw F).Satisfiable := by rcases sat with ⟨A, hA⟩ refine ⟨honestLabeling F A, fun d => ?_⟩ cases d with | inl pair => rcases pair with ⟨event, orientation⟩ cases orientation <;> exact incidence_honest F A hA event | inr u => rfl theorem clauseAnswer_eq_of_slots (a b : ClauseAnswer) (h : ∀ s, answerAt a s = answerAt b s) : a = b := by rcases a with ⟨a₀, a₁, a₂⟩ rcases b with ⟨b₀, b₁, b₂⟩ have h₀ : a₀ = b₀ := h .first have h₁ : a₁ = b₁ := h .second have h₂ : a₂ = b₂ := h .third cases h₀; cases h₁; cases h₂; rfl theorem raw_reflects (F : Formula) (sat : (raw F).Satisfiable) : F.Satisfiable := by rcases sat with ⟨labeling, hlabeling⟩ let A : Fin F.variables → Bool := fun v => (labeling (variableVertex F v)).1 have slot_correct (i : Fin F.clauses.length) (s : Slot) : localSatisfies (clauseAt F i) (toClauseAnswer (labeling (clauseVertex F i))) = true ∧ answerAt (toClauseAnswer (labeling (clauseVertex F i))) s = A (nameAt (clauseAt F i) s) := by exact incidenceAccept_true F (i, s) _ _ (hlabeling (.inl ((i, s), false))) have clause_correct (i : Fin F.clauses.length) : (clauseAt F i).eval A = true := by have equal : toClauseAnswer (labeling (clauseVertex F i)) = honestAnswer (clauseAt F i) A := clauseAnswer_eq_of_slots _ _ (fun s => by simpa only [honest_answerAt] using (slot_correct i s).2) have satisfied := (slot_correct i .first).1 rw [equal, honest_satisfies] at satisfied exact satisfied refine ⟨A, fun c hc => ?_⟩ obtain ⟨i, hi, heq⟩ := List.getElem_of_mem hc simpa only [clauseAt, heq] using clause_correct ⟨i, hi⟩ theorem raw_satisfiable_iff (F : Formula) : (raw F).Satisfiable ↔ F.Satisfiable := ⟨raw_reflects F, raw_completeness F⟩ @[simp] theorem card_vertex (F : Formula) : Fintype.card (Vertex F) = F.variables + F.clauses.length + 1 := by simp [Vertex, Fintype.card_sum] @[simp] theorem card_dart (F : Formula) : Fintype.card (Dart F) = 6 * F.clauses.length + 1 := by simp [Dart, RandomEvent, Fintype.card_sum, Fintype.card_prod] omega abbrev CompactVertex (F : Formula) := Vertex (NameCompaction.compact F) abbrev CompactDart (F : Formula) := Dart (NameCompaction.compact F) def build (F : Formula) : ConstraintGraph (CompactVertex F) (CompactDart F) Label := raw (NameCompaction.compact F) theorem build_satisfiable_iff (F : Formula) : (build F).Satisfiable ↔ F.Satisfiable := by exact (raw_satisfiable_iff (NameCompaction.compact F)).trans (NameCompaction.compact_satisfiable_iff F) theorem build_vertex_bound (F : Formula) : Fintype.card (CompactVertex F) ≤ 4 * F.clauses.length + 1 := by have h := NameCompaction.compact_active_bound F simp only [CompactVertex, card_vertex, NameCompaction.compact_clause_count] omega theorem build_dart_count (F : Formula) : Fintype.card (CompactDart F) = 6 * F.clauses.length + 1 := by simp only [CompactDart, card_dart, NameCompaction.compact_clause_count] theorem build_initial_gap (F : Formula) (unsat : ¬ F.Satisfiable) (labeling : CompactVertex F → Label) : 1 ≤ (build F).rejectionCount labeling := by apply ConstraintGraph.rejectionCount_positive intro h exact unsat ((build_satisfiable_iff F).mp h) theorem build_inverse_size_gap (F : Formula) (unsat : ¬ F.Satisfiable) (labeling : CompactVertex F → Label) : Fintype.card (CompactDart F) ≤ (6 * F.clauses.length + 1) * (build F).rejectionCount labeling := by rw [build_dart_count] simpa only [Nat.mul_one] using Nat.mul_le_mul_left (6 * F.clauses.length + 1) (build_initial_gap F unsat labeling) end BinPackingGames.Foundations.PCP.InitialGraph namespace BinPackingGames.Foundations.PCP.AlphabetRetraction variable {V E A B : Type*} def pullback (G : ConstraintGraph V E A) (decode : B → A) : ConstraintGraph V E B where reverse := G.reverse reverse_involutive := G.reverse_involutive tail := G.tail accepts := fun e b c => G.accepts e (decode b) (decode c) reverse_accepts := fun e b c => G.reverse_accepts e (decode b) (decode c) @[simp] theorem edgeSatisfied_pullback (G : ConstraintGraph V E A) (decode : B → A) (labeling : V → B) (e : E) : (pullback G decode).edgeSatisfied labeling e = G.edgeSatisfied (decode ∘ labeling) e := rfl @[simp] theorem rejectionCount_pullback [Fintype E] (G : ConstraintGraph V E A) (decode : B → A) (labeling : V → B) : (pullback G decode).rejectionCount labeling = G.rejectionCount (decode ∘ labeling) := rfl theorem satisfiable_pullback_iff (G : ConstraintGraph V E A) (decode : B → A) (encode : A → B) (sectionLaw : ∀ a, decode (encode a) = a) : (pullback G decode).Satisfiable ↔ G.Satisfiable := by constructor · rintro ⟨labeling, h⟩ exact ⟨decode ∘ labeling, h⟩ · rintro ⟨labeling, h⟩ refine ⟨encode ∘ labeling, fun e => ?_⟩ change G.accepts e (decode (encode (labeling (G.tail e)))) (decode (encode (labeling (G.head e)))) = true simpa only [ConstraintGraph.edgeSatisfied, sectionLaw] using h e abbrev Label64 := QueryIncidence.Label 6 def decode64 (label : Label64) : InitialGraph.Label := (label 0, label 1, label 2) def encode64 (label : InitialGraph.Label) : Label64 := fun i => if i = 0 then label.1 else if i = 1 then label.2.1 else label.2.2 @[simp] theorem decode_encode64 (label : InitialGraph.Label) : decode64 (encode64 label) = label := by rcases label with ⟨a, b, c⟩ rfl def initial64 (F : BinPackingGames.Foundations.Target.Formula) : ConstraintGraph (InitialGraph.CompactVertex F) (InitialGraph.CompactDart F) Label64 := pullback (InitialGraph.build F) decode64 theorem initial64_satisfiable_iff (F : BinPackingGames.Foundations.Target.Formula) : (initial64 F).Satisfiable ↔ F.Satisfiable := (satisfiable_pullback_iff (InitialGraph.build F) decode64 encode64 decode_encode64).trans (InitialGraph.build_satisfiable_iff F) theorem initial64_alphabet : Fintype.card Label64 = 64 := QueryIncidence.six_query_alphabet theorem initial64_inverse_size_gap (F : BinPackingGames.Foundations.Target.Formula) (unsat : ¬ F.Satisfiable) (labeling : InitialGraph.CompactVertex F → Label64) : Fintype.card (InitialGraph.CompactDart F) ≤ (6 * F.clauses.length + 1) * (initial64 F).rejectionCount labeling := InitialGraph.build_inverse_size_gap F unsat (decode64 ∘ labeling) end BinPackingGames.Foundations.PCP.AlphabetRetraction end OAI