File size: 3,743 Bytes
e5a97c6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | import Mathlib
import OAI.Combinatorics.MatchingCount.Complexity.GuardMachineBase
namespace OAI
namespace MatchingFPRAS.AlphabetCost
open Turing Turing.TM1 Turing.TM1to1 MachineCost
variable {Γ Λ σ : Type*} [Inhabited Γ]
def writesCount : TM1.Stmt Γ Λ σ → ℕ
| .move _ q => writesCount q
| .write _ q => writesCount q+1
| .load _ q => writesCount q
| .branch _ q r => max (writesCount q) (writesCount r)
| .goto _ => 0
| .halt => 0
variable {n : ℕ} (enc : Γ → List.Vector Bool n) (dec : List.Vector Bool n → Γ)
(enc0 : enc default = List.Vector.replicate n false)
(encdec : ∀ a,dec (enc a)=a) (M : Λ → TM1.Stmt Γ Λ σ)
include encdec
theorem statement_time (q : TM1.Stmt Γ Λ σ) (v : σ) (L R : ListBlank Γ) :
∃ t : ℕ, t≤writesCount q ∧
MachineCost.run (TM1.step (tr enc dec M)) t
(stepAux (trNormal dec q) v (trTape' enc0 L R)) =
trCfg enc enc0 (stepAux q v (Tape.mk' L R)) := by
induction q generalizing v L R with
| move d q ih =>
cases d <;>
simp only [trNormal,stepAux_move,stepAux,Tape.move_left_mk',
trTape'_move_left enc0,trTape'_move_right enc0,writesCount] <;>
apply ih
| write f q ih =>
simp only [trNormal,stepAux_read dec enc0 encdec,stepAux,writesCount]
obtain ⟨a,R,rfl⟩:=R.exists_cons
obtain ⟨t,ht,he⟩:=ih v L (R.cons (f a v))
refine ⟨t+1,by omega,?_⟩
rw [MachineCost.run]
simp only [advance,TM1.step,tr,Option.getD_some,Tape.mk'_head,
ListBlank.head_cons]
rw [stepAux_write enc0,stepAux_move,trTape'_move_left enc0]
simpa using he
| load f q ih =>
simp only [trNormal,stepAux_read dec enc0 encdec,stepAux,writesCount]
exact ih _ _ _
| branch p q r ihq ihr =>
simp only [trNormal,stepAux_read dec enc0 encdec,stepAux,Tape.mk'_head,writesCount]
cases h:p R.head v
· simp only [Bool.cond_false]
obtain ⟨t,ht,he⟩:=ihr v L R
exact ⟨t,ht.trans (le_max_right _ _),he⟩
· simp only [Bool.cond_true]
obtain ⟨t,ht,he⟩:=ihq v L R
exact ⟨t,ht.trans (le_max_left _ _),he⟩
| goto l =>
refine ⟨0,le_rfl,?_⟩
simp only [trNormal,stepAux_read dec enc0 encdec,stepAux,trCfg,trTape_mk',
MachineCost.run,Tape.mk'_head,Option.map_some]
| halt =>
refine ⟨0,le_rfl,?_⟩
simp only [trNormal,stepAux,trCfg,trTape_mk',MachineCost.run,Option.map_none]
lemma one_step_time (W : ℕ) (hW : ∀ l,writesCount (M l)≤W) (c : TM1.Cfg Γ Λ σ) :
∃ t : ℕ,t≤W+1 ∧
MachineCost.run (TM1.step (tr enc dec M)) t (trCfg enc enc0 c)=
trCfg enc enc0 (advance (TM1.step M) c) := by
rcases c with ⟨l,v,T⟩
obtain ⟨L,R,rfl⟩:=T.exists_mk'
cases l with
| none => exact ⟨0,Nat.zero_le _,rfl⟩
| some l =>
obtain ⟨t,ht,he⟩:=statement_time enc dec enc0 encdec M (M l) v L R
refine ⟨t+1,by have:=hW l;omega,?_⟩
simpa only [MachineCost.run,advance,TM1.step,Option.map_some,trCfg,tr,
Option.getD_some,trTape_mk'] using he
theorem execution_time (W : ℕ) (hW : ∀ l,writesCount (M l)≤W)
(c : TM1.Cfg Γ Λ σ) (k : ℕ) :
∃ t : ℕ,t≤k*(W+1) ∧
MachineCost.run (TM1.step (tr enc dec M)) t (trCfg enc enc0 c)=
trCfg enc enc0 (MachineCost.run (TM1.step M) k c) := by
induction k generalizing c with
| zero => exact ⟨0,by simp,rfl⟩
| succ k ih =>
obtain ⟨t₁,ht₁,he₁⟩:=one_step_time enc dec enc0 encdec M W hW c
obtain ⟨t₂,ht₂,he₂⟩:=ih (advance (TM1.step M) c)
refine ⟨t₁+t₂,?_,?_⟩
· calc
t₁+t₂≤(W+1)+k*(W+1) := Nat.add_le_add ht₁ ht₂
_=(k+1)*(W+1) := by ring
· rw [run_add,he₁,he₂]; rfl
end MatchingFPRAS.AlphabetCost
end OAI
|