import Mathlib import OAI.Computability.MinUncut.Estimates.LiteralLocalTemplates import OAI.Computability.MinUncut.Estimates.ReversePrefixProgram import OAI.Computability.MinUncut.Encoding.RawGraphEncoding namespace OAI section open scoped BigOperators namespace MinUncut.PathRealization open MinUncut.FiniteProof MinUncutGames.Foundations.Hastad.SourceOccurrences lemma listFailure_map {V W : Type*} (f : V → W) (ds : List (Demand V)) (s : W → Bool) : listFailure (ds.map (Demand.map f)) s=listFailure ds (s ∘ f) := by simp only [listFailure,List.map_map,Function.comp_def,failure_map] def listOutput {N : ℕ} (ds : List (Demand (Fin N))) (k : ℕ) (hk : 1≤k) : Output := canonicalOutput (fun i : Fin ds.length => ds[i]) k hk lemma listOutput_yes {N : ℕ} (ds : List (Demand (Fin N))) (k : ℕ) (hk : 1≤k) (s : Fin N → Bool) (hs : listFailure ds s≤k) : (listOutput ds k hk).opt≤k := canonicalOutput_yes _ k hk s (by simpa only [← listFailure_eq] using hs) lemma listOutput_no {N : ℕ} (ds : List (Demand (Fin N))) (k : ℕ) (hk : 1≤k) (K : ℕ) (hs : ∀ s, K*k (List.finRange (N+ds.length*3)).map fun v=>canonicalAdjacent (fun d:Fin ds.length=>ds[d]) u v lemma rawRow_pack {N : ℕ} (ds : List (Demand (Fin N))) (u : Fin (N+ds.length*3)) : rawRow N ds.length (packDemands ds) u.val (N+ds.length*3)= ((List.finRange (N+ds.length*3)).map fun v=> canonicalAdjacent (fun d:Fin ds.length=>ds[d]) u v).map Bool.toNat := by rw [rawRow,←finRange_vals,List.map_map,List.map_map] congr 1 funext v exact rawCell_canonical ds u v lemma rawTable_pack {N : ℕ} (ds : List (Demand (Fin N))) : rawTable N ds.length (packDemands ds) (N+ds.length*3) (N+ds.length*3)= (canonicalTable ds).map Bool.toNat := by rw [rawTable,←finRange_vals,List.flatMap_map,canonicalTable,List.map_flatMap] congr 1 funext u exact rawRow_pack ds u lemma listOutput_bits {N : ℕ} (ds : List (Demand (Fin N))) (k : ℕ) (hk : 1≤k) : (listOutput ds k hk).bits=MinUncutGames.BinaryEncoding.nameBits (N+ds.length*3)++ canonicalTable ds++MinUncutGames.BinaryEncoding.nameBits k := rfl end MinUncut.Costed.GraphRegisters noncomputable section namespace MinUncut.Costed.SourceWords open MinUncut.Inner MinUncut.Outer MinUncut.Outer.LocalTemplate open MinUncut.PathRealization MinUncutGames.Reduction MinUncut.SourceBridge open MinUncutGames.Foundations.Hastad.SourceOccurrences variable {t k m n : ℕ} {g : MinUncut.FiniteGaussian.GridData} def demandExpressions (σ η : ℚ) (e : Index (Fin t) k m n g) : List AExpr := let T := indexQuery σ η e [leftExpr T e.2.2.1,rightExpr T e.2.2.1,signExpr T e.2.2.1] def localExpressions (E : Enumeration (Index (Fin t) k m n g)) (a : ℚ) (b : MinUncut.Outer.Test → ℚ) (σ η : ℚ) : List AExpr := E.values.flatMap (fun e=>(List.replicate (repetitions a b e) (demandExpressions σ η e)).flatten) def tupleDemands (input : SourceEncoding.Input) (u : Fin t → Fin input.equations.length) (E : Enumeration (Index (Fin t) k m n g)) (a : ℚ) (b : MinUncut.Outer.Test → ℚ) (σ η : ℚ) : List (Demand (Fin (paddedVariableEncoding input.«variables» t).size)) := E.values.flatMap (fun e=>List.replicate (repetitions a b e) ((allDemand (equations input) g σ η (occurrence (equations input) (u,e))).map variableCode)) lemma demandExpressions_eval (input : SourceEncoding.Input) (u : Fin t → Fin input.equations.length) (e : Index (Fin t) k m n g) (σ η : ℚ) (rest : List ℕ) : (demandExpressions σ η e).map (fun x=>x.eval (inputTuple input u++rest))= GraphRegisters.packDemand ((allDemand (equations input) g σ η (occurrence (equations input) (u,e))).map variableCode) := by change queryWords (indexQuery σ η e) e.2.2.1 (inputTuple input u++rest)=_ rw [queryWords_append,queryWords_eq,occurrence_demand] rfl lemma localExpressions_eval (input : SourceEncoding.Input) (u : Fin t → Fin input.equations.length) (E : Enumeration (Index (Fin t) k m n g)) (a : ℚ) (b : MinUncut.Outer.Test → ℚ) (σ η : ℚ) (rest : List ℕ) : (localExpressions E a b σ η).map (fun x=>x.eval (inputTuple input u++rest))= GraphRegisters.packDemands (tupleDemands input u E a b σ η) := by simp only [localExpressions,tupleDemands,GraphRegisters.packDemands,List.map_flatMap, List.map_flatten,List.flatMap_assoc] apply congrArg (List.flatMap · E.values) funext e simp only [List.map_replicate, demandExpressions_eval,List.flatMap_replicate] def tupleList (input : SourceEncoding.Input) (t : ℕ) : List (Fin t → Fin input.equations.length) := (List.finRange (input.equations.length^t)).map finFunctionFinEquiv.symm def generatedDemands (input : SourceEncoding.Input) (E : Enumeration (Index (Fin t) k m n g)) (a : ℚ) (b : MinUncut.Outer.Test → ℚ) (σ η : ℚ) : List (Demand (Fin (paddedVariableEncoding input.«variables» t).size)) := (tupleList input t).flatMap (fun u=>tupleDemands input u E a b σ η) lemma emitted_demands (input : SourceEncoding.Input) (E : Enumeration (Index (Fin t) k m n g)) (a : ℚ) (b : MinUncut.Outer.Test → ℚ) (σ η : ℚ) (rest : List ℕ) : Blocks.emitted (localExpressions E a b σ η) (input.equations.length^t) (SourceEncoding.inputWords input++rest)= GraphRegisters.packDemands (generatedDemands input E a b σ η) := by rw [Blocks.emitted,←GraphRegisters.finRange_vals] simp only [generatedDemands,tupleList,List.flatMap_map,GraphRegisters.packDemands, List.flatMap_assoc] apply congrArg (List.flatMap · (List.finRange (input.equations.length^t))) funext z have h := localExpressions_eval input (finFunctionFinEquiv.symm z) E a b σ η rest simpa only [Blocks.block,inputTuple,Equiv.apply_symm_apply,List.cons_append,GraphRegisters.packDemands] using h end MinUncut.Costed.SourceWords end end OAI