import Mathlib import OAI.Combinatorics.MatchingCount.Machines.CoinTM1 namespace OAI namespace MatchingFPRAS.SafeTrace open Turing MachineCost PhysicalStream variable {Γ Λ σ : Type} [Inhabited Γ] inductive Path (M : Λ → TM1.Stmt Γ Λ σ) (safe : Λ → Prop) : ℕ → TM1.Cfg Γ Λ σ → TM1.Cfg Γ Λ σ → Prop | refl (c) : Path M safe 0 c c | cons {n c d} (hc : ∀ l,c.l=some l→safe l) (rest : Path M safe n (advance (TM1.step M) c) d) : Path M safe (n+1) c d namespace Path variable {M : Λ → TM1.Stmt Γ Λ σ} {safe : Λ → Prop} lemma single (c : TM1.Cfg Γ Λ σ) (hc : ∀ l,c.l=some l→safe l) : Path M safe 1 c (advance (TM1.step M) c) := .cons hc (.refl _) lemma trans {n m : ℕ} {a b c : TM1.Cfg Γ Λ σ} (h : Path M safe n a b) (g : Path M safe m b c) : Path M safe (n+m) a c := by induction h with | refl a => simpa only [Nat.zero_add] using g | @cons n a b ha hr ih => simpa only [Nat.succ_add] using Path.cons ha (ih g) lemma run_eq {n : ℕ} {a b : TM1.Cfg Γ Λ σ} (h : Path M safe n a b) : MachineCost.run (TM1.step M) n a=b := by induction h with | refl a => rfl | cons ha hr ih => exact ih lemma monotone {n : ℕ} {a b : TM1.Cfg Γ Λ σ} (h : Path M safe n a b) {safe' : Λ → Prop} (hs : ∀ l,safe l→safe' l) : Path M safe' n a b := by induction h with | refl a => exact .refl _ | cons ha hr ih => exact .cons (fun l hl=>hs l (ha l hl)) ih lemma of_run (n : ℕ) (c : TM1.Cfg Γ Λ σ) (h : ∀ i,i exact .refl _ | succ n ih => refine .cons (h 0 (by omega)) (ih _ ?_) intro i hi l hl exact h (i+1) (by omega) l hl theorem physical {n : ℕ} {a b : TM1.Cfg Γ Λ σ} (h : Path M safe n a b) (coin : CoinTM1.Coins Λ) (hcoin : ∀ l,safe l→coin l=none) (C : ℕ) (hC : ∀ l,safe l→statementCost (M l)≤C) : ∃ k,k≤n*C ∧ ∀ bits : Stream, PhysicalPost.execute (CoinTM1.table M coin) k (CoinTM1.trCfg a) bits=CoinTM1.trCfg b := by induction h with | refl a => exact ⟨0,by simp,fun _=>rfl⟩ | @cons n c d hc hr ih => obtain ⟨k,hk,he⟩:=ih rcases c with ⟨l,v,T⟩ cases l with | none => refine ⟨k,le_trans hk ?_,he⟩ exact Nat.mul_le_mul_right C (by omega) | some l => have hs:=hc l rfl obtain ⟨j,hj,hjc,hje⟩:=CoinTM1.entry_ticks M coin l v T (hcoin l hs) refine ⟨j+k,?_,fun bits=>?_⟩ · have hb:=hC l hs calc j+k≤C+n*C := Nat.add_le_add (hjc.trans hb) hk _=(n+1)*C := by ring · rw [PhysicalPost.execute_add,hje] exact he (shift j bits) end Path end MatchingFPRAS.SafeTrace namespace MatchingFPRAS.SafeTrace.Path open Turing MachineCost variable {Γ Λ σ : Type} [Inhabited Γ] [Inhabited Λ] lemma with_support {M : Λ → TM1.Stmt Γ Λ σ} {safe : Λ → Prop} {S : Finset Λ} (hS : TM1.Supports M S) {n : ℕ} {a b : TM1.Cfg Γ Λ σ} (h : Path M safe n a b) (ha : ∀ l,a.l=some l→l∈S) : Path M (fun l=>safe l ∧ l∈S) n a b := by induction h with | refl c => exact .refl _ | @cons n c d hc hr ih => refine .cons (fun l hl=>⟨hc l hl,ha l hl⟩) (ih ?_) intro l hl rcases c with ⟨lc,v,T⟩ cases lc with | none => simp [advance,TM1.step] at hl | some lc => have hs:=TM1.step_supports M hS (c:=⟨some lc,v,T⟩) (c':=TM1.stepAux (M lc) v T) rfl (by simp only [Finset.some_mem_insertNone]; exact ha lc rfl) simp only [advance,TM1.step,Option.getD_some] at hl exact Finset.some_mem_insertNone.mp (hl ▸ hs) end MatchingFPRAS.SafeTrace.Path end OAI