import Mathlib import OAI.Combinatorics.KServer.Amplification import OAI.Combinatorics.KServer.Ranks namespace OAI noncomputable section open scoped BigOperators open Finset namespace KServer.FiniteExperiment attribute [local instance] Classical.propDecidable open Finset Episode PosteriorRanks universe u v variable {Y : Type u} [MetricSpace Y] [Fintype Y] {k H : ℕ} [NeZero k] /-- A fixed minimizing labeled trajectory for each complete input. -/ def optimalTrace (s : Configuration k Y) (σ : Fin H → Y) : List (Y × Fin k) := serviceTrace (List.ofFn σ) (optimalCost_attained s (List.ofFn σ)).choose omit [Fintype Y] in lemma optimalTrace_requests (s : Configuration k Y) (σ : Fin H → Y) : (optimalTrace s σ).map Prod.fst=List.ofFn σ := serviceTrace_requests _ _ omit [Fintype Y] in lemma optimalTrace_cost (s : Configuration k Y) (σ : Fin H → Y) : traceCost s (optimalTrace s σ)=optimalCost s (List.ofFn σ) := by rw [optimalTrace,serviceTrace_cost] exact (optimalCost_attained s (List.ofFn σ)).choose_spec omit [Fintype Y] in lemma optimalTrace_length (s : Configuration k Y) (σ : Fin H → Y) : (optimalTrace s σ).length=H := by simpa only [List.length_map,List.length_ofFn] using congrArg List.length (optimalTrace_requests s σ) /-- The genuine hidden configuration, frozen when the finite input ends. -/ def hidden (s : Configuration k Y) (σ : Fin H → Y) (t : ℕ) : Configuration k Y := configurationAfter s ((optimalTrace s σ).take t) omit [Fintype Y] in lemma hidden_zero (s : Configuration k Y) (σ : Fin H → Y) : hidden s σ 0=s := by simp [hidden,configurationAfter] /-- Current designated occurrence in the selected optimum; it is not observed. -/ def mover (s : Configuration k Y) (σ : Fin H → Y) (t : Fin H) : Fin k := ((optimalTrace s σ)[t.val]'(by rw [optimalTrace_length]; exact t.isLt)).2 omit [Fintype Y] in lemma optimalTrace_get_request (s : Configuration k Y) (σ : Fin H → Y) (t : Fin H) : ((optimalTrace s σ)[t.val]'(by rw [optimalTrace_length]; exact t.isLt)).1=σ t := by have he := congrArg (fun l : List Y => l[t.val]?) (optimalTrace_requests s σ) apply Option.some.inj simpa only [List.getElem?_map,List.getElem?_eq_getElem (show t.val<(optimalTrace s σ).length by rw [optimalTrace_length]; exact t.isLt), Option.map_some,List.getElem?_ofFn,Fin.isLt,dite_eq_left] using he omit [Fintype Y] in lemma hidden_step (s : Configuration k Y) (σ : Fin H → Y) (t : Fin H) : hidden s σ (t.val+1)=serve (hidden s σ t.val) (mover s σ t) (σ t) := by have ht : t.val<(optimalTrace s σ).length := by rw [optimalTrace_length]; exact t.isLt rw [hidden,List.take_succ_eq_append_getElem ht,configurationAfter_append] simp only [configurationAfter] rw [optimalTrace_get_request] rfl omit [Fintype Y] in lemma hidden_covers (s : Configuration k Y) (σ : Fin H → Y) (t : Fin H) : hidden s σ (t.val+1) (mover s σ t)=σ t := by rw [hidden_step] simp [serve] /-- A finite observation sufficient for exactly the request requestPrefix. -/ def requestPrefix (σ : Fin H → Y) (t : ℕ) : Fin H → Option Y := fun i => if i.val