import Mathlib import OAI.Combinatorics.IndependentSets.Fourier.Folding namespace OAI namespace IndependentSetsGames.Foundations.Hastad.SourceOccurrences open scoped BigOperators structure Encoding (α : Type) where size : ℕ code : α ≃ Fin size namespace Encoding def fin (n : ℕ) : Encoding (Fin n) := ⟨n, Equiv.refl _⟩ def bool : Encoding Bool := ⟨2, finTwoEquiv.symm⟩ def unit : Encoding Unit := ⟨1, finOneEquiv.symm⟩ def prod {α β : Type} (a : Encoding α) (b : Encoding β) : Encoding (α × β) := ⟨a.size * b.size, (Equiv.prodCongr a.code b.code).trans finProdFinEquiv⟩ def sum {α β : Type} (a : Encoding α) (b : Encoding β) : Encoding (α ⊕ β) := ⟨a.size + b.size, (Equiv.sumCongr a.code b.code).trans finSumFinEquiv⟩ def function {α β : Type} (a : Encoding α) (b : Encoding β) : Encoding (α → β) where size := b.size ^ a.size code := (show (α → β) ≃ (Fin a.size → Fin b.size) from { toFun := fun f i => b.code (f (a.code.symm i)) invFun := fun f x => b.code.symm (f (a.code x)) left_inv := by intro f; funext x; simp right_inv := by intro f; funext i; simp }).trans finFunctionFinEquiv def enumerate {α : Type} (a : Encoding α) : List α := List.ofFn a.code.symm @[simp] theorem length_enumerate {α : Type} (a : Encoding α) : a.enumerate.length = a.size := by simp [enumerate] theorem mem_enumerate {α : Type} (a : Encoding α) (x : α) : x ∈ a.enumerate := by rw [enumerate, List.mem_ofFn'] exact ⟨a.code x, a.code.symm_apply_apply x⟩ theorem nodup_enumerate {α : Type} (a : Encoding α) : a.enumerate.Nodup := List.nodup_ofFn_ofInjective a.code.symm.injective theorem card_eq_size {α : Type} [Fintype α] (a : Encoding α) : Fintype.card α = a.size := by simpa only [Fintype.card_fin] using Fintype.card_congr a.code end Encoding abbrev GlobalKey (V C I J : Type) := (V × Cube I) ⊕ ((C × Cube J) ⊕ Unit) def globalEncoding {V C I J : Type} (v : Encoding V) (c : Encoding C) (i : Encoding I) (j : Encoding J) : Encoding (GlobalKey V C I J) := (v.prod (i.function Encoding.bool)).sum ((c.prod (j.function Encoding.bool)).sum Encoding.unit) @[simp] theorem globalEncoding_size {V C I J : Type} (v : Encoding V) (c : Encoding C) (i : Encoding I) (j : Encoding J) : (globalEncoding v c i j).size = v.size * 2 ^ i.size + (c.size * 2 ^ j.size + 1) := rfl def extendRestricted {J : Type} (valid : J → Bool) (f : Cube {j : J // valid j = true}) : Cube J := fun j => if h : valid j = true then f ⟨j, h⟩ else false @[simp] theorem extendRestricted_valid {J : Type} (valid : J → Bool) (f : Cube {j : J // valid j = true}) (j : {j : J // valid j = true}) : extendRestricted valid f j.val = f j := by simp [extendRestricted, j.property] theorem extendRestricted_injective {J : Type} (valid : J → Bool) : Function.Injective (extendRestricted valid) := by intro f g h funext j have := congrFun h j.val simpa only [extendRestricted_valid] using this @[simp] theorem restrictQuery_extendRestricted {J : Type} (valid : J → Bool) (f : Cube {j : J // valid j = true}) : restrictQuery valid (extendRestricted valid f) = f := by funext j exact extendRestricted_valid valid f j @[simp] theorem canonicalInput_half {I : Type} [Fintype I] [DecidableEq I] (i₀ : I) (f : HalfCube i₀) : canonicalInput i₀ f.val = f := by apply Subtype.ext simp [canonicalInput, representative, f.property] def assignmentOfKey {V C I J : Type} (v : Encoding V) (c : Encoding C) (i : Encoding I) (j : Encoding J) (bits : GlobalKey V C I J → Bool) : Fin (globalEncoding v c i j).size → Bool := fun q => bits ((globalEncoding v c i j).code.symm q) @[simp] theorem assignmentOfKey_code {V C I J : Type} (v : Encoding V) (c : Encoding C) (i : Encoding I) (j : Encoding J) (bits : GlobalKey V C I J → Bool) (key : GlobalKey V C I J) : assignmentOfKey v c i j bits ((globalEncoding v c i j).code key) = bits key := by exact congrArg bits ((globalEncoding v c i j).code.symm_apply_apply key) theorem assignmentOfKey_surjective {V C I J : Type} (v : Encoding V) (c : Encoding C) (i : Encoding I) (j : Encoding J) : Function.Surjective (assignmentOfKey v c i j) := by intro bits refine ⟨fun key => bits ((globalEncoding v c i j).code key), ?_⟩ funext q exact congrArg bits ((globalEncoding v c i j).code.apply_symm_apply q) end IndependentSetsGames.Foundations.Hastad.SourceOccurrences end OAI