import Mathlib import OAI.Combinatorics.IndependentSets.PCP.GraphTables import OAI.Combinatorics.IndependentSets.PCP.Enumeration namespace OAI namespace IndependentSetsGames.Foundations.PCP.AlphabetTable.Queries open IndependentSetsGames.Foundations.Hastad abbrev RawQuery (q : Nat) := (Bool × Cube (Fin q)) ⊕ Cube (Fin q × Fin q) abbrev LocalEvent (q : Nat) := AlphabetGraph.LocalEvent (Fin q) def mask {q : Nat} (P : Fin q → Fin q → Bool) (f : Cube (Fin q × Fin q)) : Cube (Fin q × Fin q) := fun p => if P p.1 p.2 then f p else false def xorTape {A : Type*} (f g : Cube A) : Cube A := fun a => f a ^^ g a def andTape {A : Type*} (f g : Cube A) : Cube A := fun a => f a && g a def inputPairTape {q : Nat} (k : Bool × Cube (Fin q)) : Cube (Fin q × Fin q) := fun p => if k.1 then k.2 p.2 else k.2 p.1 def query {q : Nat} (P : Fin q → Fin q → Bool) (event : LocalEvent q) (slot : Fin 6) : RawQuery q := match event with | (kind, k, f, g, r₀, r₁, r₂) => if kind = 0 then if slot = 0 then .inr (mask P f) else if slot = 1 then .inr (mask P g) else .inr (mask P (xorTape f g)) else if kind = 1 then if slot = 0 then .inr (mask P r₀) else if slot = 1 then .inr (mask P (xorTape r₀ (andTape f g))) else if slot = 2 then .inr (mask P r₁) else if slot = 3 then .inr (mask P (xorTape r₁ f)) else if slot = 4 then .inr (mask P r₂) else .inr (mask P (xorTape r₂ g)) else if kind = 2 then if slot = 0 then .inr (mask P r₀) else .inr (mask P (xorTape r₀ (fun _ => true))) else if slot = 0 then .inl k else if slot = 1 then .inr (mask P r₀) else .inr (mask P (xorTape r₀ (inputPairTape k))) def globalize {q : Nat} {V E : Type*} (tail head : V) (edge : E) : RawQuery q → AlphabetGraph.Address V E (Fin q) | .inl (side, tape) => .inl ((if side then head else tail), tape) | .inr tape => .inr (edge, tape) @[simp] theorem mask_idempotent {q : Nat} (P : Fin q → Fin q → Bool) (f : Cube (Fin q × Fin q)) : mask P (mask P f) = mask P f := by funext p cases hp : P p.1 p.2 <;> simp [mask, hp] theorem mask_of_empty {q : Nat} (P : Fin q → Fin q → Bool) (empty : ∀ a b, P a b = false) (f : Cube (Fin q × Fin q)) : mask P f = fun _ => false := by funext p simp [mask, empty] theorem query_is_masked {q : Nat} (P : Fin q → Fin q → Bool) (event : LocalEvent q) (slot : Fin 6) : match query P event slot with | .inl _ => True | .inr tape => mask P tape = tape := by rcases event with ⟨kind, k, f, g, r₀, r₁, r₂⟩ by_cases hk₀ : kind = 0 <;> by_cases hk₁ : kind = 1 <;> by_cases hk₂ : kind = 2 <;> simp only [query, hk₀, hk₁, hk₂, ite_true, ite_false] <;> split_ifs <;> simp def extendQuery {q : Nat} (P : Fin q → Fin q → Bool) : AlphabetReduction.InputCoordinate (Fin q) ⊕ Cube (AlphabetReduction.LegalPair P) → RawQuery q | .inl k => .inl k | .inr tape => .inr (UniformRestriction.extend (fun p : Fin q × Fin q => P p.1 p.2 = true) tape) theorem query_eq_extend_eventQueries {q : Nat} (P : Fin q → Fin q → Bool) (kind : Fin 4) (k : AlphabetReduction.InputCoordinate (Fin q)) (f g r₀ r₁ r₂ : Cube (Fin q × Fin q)) (slot : Fin 6) : query P (kind, k, f, g, r₀, r₁, r₂) slot = extendQuery P (AssignmentTester.eventQueries (AlphabetReduction.pairEncoding P) kind k (UniformRestriction.restrict (fun p : Fin q × Fin q => P p.1 p.2 = true) f) (UniformRestriction.restrict (fun p : Fin q × Fin q => P p.1 p.2 = true) g) (UniformRestriction.restrict (fun p : Fin q × Fin q => P p.1 p.2 = true) r₀) (UniformRestriction.restrict (fun p : Fin q × Fin q => P p.1 p.2 = true) r₁) (UniformRestriction.restrict (fun p : Fin q × Fin q => P p.1 p.2 = true) r₂) slot) := by by_cases hk₀ : kind = 0 <;> by_cases hk₁ : kind = 1 <;> by_cases hk₂ : kind = 2 <;> simp only [query, AssignmentTester.eventQueries, hk₀, hk₁, hk₂, ite_true, ite_false] all_goals split_ifs <;> simp_all only [extendQuery] all_goals rfl theorem globalize_extendQuery {q : Nat} {V E : Type*} (G : ConstraintGraph V E (Fin q)) (e : E) (x : AlphabetReduction.InputCoordinate (Fin q) ⊕ Cube (AlphabetReduction.LegalPair (G.accepts e))) : globalize (G.tail e) (G.head e) e (extendQuery (G.accepts e) x) = AlphabetGraph.globalizeQuery G e x := by cases x with | inl k => cases k; rfl | inr tape => rfl theorem globalize_query {q : Nat} {V E : Type*} (G : ConstraintGraph V E (Fin q)) (e : E) (event : LocalEvent q) (slot : Fin 6) : globalize (G.tail e) (G.head e) e (query (G.accepts e) event slot) = (AlphabetGraph.verifier G).query (e, event) slot := by rcases event with ⟨kind, k, f, g, r₀, r₁, r₂⟩ rw [query_eq_extend_eventQueries] exact globalize_extendQuery G e _ end IndependentSetsGames.Foundations.PCP.AlphabetTable.Queries namespace IndependentSetsGames.Foundations.PCP.AlphabetTable.Relations open Hastad open Queries variable {q : Nat} def sameAddress (selfLoop : Bool) : RawQuery q → RawQuery q → Bool | .inl (side, f), .inl (side', g) => ((side == side') || selfLoop) && decide (f = g) | .inr f, .inr g => decide (f = g) | _, _ => false @[simp] theorem sameAddress_refl (selfLoop : Bool) (x : RawQuery q) : sameAddress selfLoop x x = true := by rcases x with ⟨side, f⟩ | f <;> simp [sameAddress] theorem globalize_eq_iff {V E : Type*} [DecidableEq V] (tail head : V) (edge : E) (selfLoop : Bool) (hloop : selfLoop = decide (tail = head)) (x y : RawQuery q) : globalize tail head edge x = globalize tail head edge y ↔ sameAddress selfLoop x y = true := by rcases x with ⟨side, f⟩ | f <;> rcases y with ⟨side', g⟩ | g · cases side <;> cases side' · simp [globalize, sameAddress] · simp [globalize, sameAddress, hloop] · simp [globalize, sameAddress, hloop, show head = tail ↔ tail = head from eq_comm] · simp [globalize, sameAddress] · simp [globalize, sameAddress] · simp [globalize, sameAddress] · simp [globalize, sameAddress] def eqProfile (selfLoop : Bool) (queries : Fin 6 → RawQuery q) (i j : Fin 6) : Bool := sameAddress selfLoop (queries i) (queries j) @[simp] theorem eqProfile_refl (selfLoop : Bool) (queries : Fin 6 → RawQuery q) (i : Fin 6) : eqProfile selfLoop queries i i = true := sameAddress_refl _ _ def canonicalSlot (selfLoop : Bool) (queries : Fin 6 → RawQuery q) (i : Fin 6) : Fin 6 := Fin.find (fun j => eqProfile selfLoop queries j i = true) ⟨i, eqProfile_refl _ _ _⟩ theorem canonicalSlot_profile (selfLoop : Bool) (queries : Fin 6 → RawQuery q) (i : Fin 6) : eqProfile selfLoop queries (canonicalSlot selfLoop queries i) i = true := by unfold canonicalSlot exact Fin.find_spec (p := fun j => eqProfile selfLoop queries j i = true) ⟨i, eqProfile_refl _ _ _⟩ theorem canonicalSlot_le (selfLoop : Bool) (queries : Fin 6 → RawQuery q) (i j : Fin 6) (h : eqProfile selfLoop queries j i = true) : canonicalSlot selfLoop queries i ≤ j := Fin.find_le_of_pos ⟨i, eqProfile_refl _ _ _⟩ h def RepeatedConsistent (selfLoop : Bool) (queries : Fin 6 → RawQuery q) (label : QueryIncidence.Label 6) : Prop := ∀ i j, eqProfile selfLoop queries i j = true → label i = label j instance repeatedConsistentDecidable (selfLoop : Bool) (queries : Fin 6 → RawQuery q) (label : QueryIncidence.Label 6) : Decidable (RepeatedConsistent selfLoop queries label) := by unfold RepeatedConsistent infer_instance def incidenceAccept (selfLoop : Bool) (queries : Fin 6 → RawQuery q) (accept : QueryIncidence.Label 6 → Bool) (slot : Fin 6) (left right : QueryIncidence.Label 6) : Bool := decide (accept left = true ∧ RepeatedConsistent selfLoop queries left) && decide (left (canonicalSlot selfLoop queries slot) = right 0) def predicate (selfLoop : Bool) (queries : Fin 6 → RawQuery q) (accept : QueryIncidence.Label 6 → Bool) (slot : Fin 6) (orientation : Bool) (a b : QueryIncidence.Label 6) : Bool := if orientation then incidenceAccept selfLoop queries accept slot b a else incidenceAccept selfLoop queries accept slot a b def relation (P : Fin q → Fin q → Bool) (selfLoop : Bool) (event : AlphabetGraph.LocalEvent (Fin q)) (slot : Fin 6) (orientation : Bool) : GraphTables.RelationTable := GraphTables.relationOf fun a b => predicate selfLoop (query P event) (AssignmentTester.eventAccepts event.1) slot orientation (Enumeration.labelEquiv.symm a) (Enumeration.labelEquiv.symm b) @[simp] theorem relationAt_relation (P : Fin q → Fin q → Bool) (selfLoop : Bool) (event : AlphabetGraph.LocalEvent (Fin q)) (slot : Fin 6) (orientation : Bool) (a b : GraphTables.Label) : GraphTables.relationAt (relation P selfLoop event slot orientation) a b = predicate selfLoop (query P event) (AssignmentTester.eventAccepts event.1) slot orientation (Enumeration.labelEquiv.symm a) (Enumeration.labelEquiv.symm b) := by exact GraphTables.relationAt_relationOf _ _ _ theorem relation_reverse (P : Fin q → Fin q → Bool) (selfLoop : Bool) (event : AlphabetGraph.LocalEvent (Fin q)) (slot : Fin 6) (orientation : Bool) (a b : GraphTables.Label) : GraphTables.relationAt (relation P selfLoop event slot (!orientation)) b a = GraphTables.relationAt (relation P selfLoop event slot orientation) a b := by simp only [relationAt_relation] cases orientation <;> rfl def oldPredicate (old : Vector Bool (q * q)) (a b : Fin q) : Bool := old[(finProdFinEquiv (a, b) : Fin (q * q))] def relationFromTable (old : Vector Bool (q * q)) (selfLoop : Bool) (event : AlphabetGraph.LocalEvent (Fin q)) (slot : Fin 6) (orientation : Bool) : GraphTables.RelationTable := relation (oldPredicate old) selfLoop event slot orientation @[simp] theorem oldPredicate_relationAt (old : GraphTables.RelationTable) (a b : GraphTables.Label) : oldPredicate old a b = GraphTables.relationAt old a b := rfl theorem relationFromTable_eq (old : GraphTables.RelationTable) (selfLoop : Bool) (event : AlphabetGraph.LocalEvent (Fin 64)) (slot : Fin 6) (orientation : Bool) : relationFromTable old selfLoop event slot orientation = relation (GraphTables.relationAt old) selfLoop event slot orientation := rfl end IndependentSetsGames.Foundations.PCP.AlphabetTable.Relations end OAI