File size: 4,757 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import Mathlib
import OAI.Combinatorics.MatchingCount.Complexity.AlphabetCompilerCost

namespace OAI

namespace MatchingFPRAS.SupportedCompiler
open Turing MachineCost
open scoped Classical

variable {Γ Λ σ : Type*} [Inhabited Γ] [Inhabited Λ] [Inhabited σ]
variable (M : Λ → TM1.Stmt Γ Λ σ) (S : Finset Λ) (hS : TM1.Supports M S)
noncomputable def cost : ℕ := S.sup (fun l=>statementCost (M l))
omit [Inhabited Γ] [Inhabited Λ] [Inhabited σ] in
lemma cost_le {l : Λ} (hl : l ∈ S) : statementCost (M l) ≤ cost M S := Finset.le_sup (f:=fun l=>statementCost (M l)) hl
include hS
omit [Inhabited σ] in
lemma advance_supported {c : TM1.Cfg Γ Λ σ} (hc : c.l ∈ S.insertNone) :
    (advance (TM1.step M) c).l ∈ S.insertNone := by
  cases he:TM1.step M c with
  | none => simpa only [advance,he,Option.getD_none] using hc
  | some c' =>
    simpa only [advance,he,Option.getD_some] using TM1.step_supports M hS he hc
omit [Inhabited σ] in
lemma run_supported {c : TM1.Cfg Γ Λ σ} (hc : c.l ∈ S.insertNone) (n : ℕ) :
    (MachineCost.run (TM1.step M) n c).l ∈ S.insertNone := by
  induction n generalizing c with
  | zero => exact hc
  | succ n ih => exact ih (advance_supported M S hS hc)

theorem post_time {c : TM1.Cfg Γ Λ σ} (hc : c.l ∈ S.insertNone) (n : ℕ) :
    ∃ k ≤ cost M S*n,
      MachineCost.run (TM0.step (TM1to0.tr M)) k (TM1to0.trCfg M c)=
        TM1to0.trCfg M (MachineCost.run (TM1.step M) n c) := by
  induction n generalizing c with
  | zero => exact ⟨0,by simp,rfl⟩
  | succ n ih =>
    rcases c with ⟨l,v,T⟩
    cases l with
    | none =>
      have he : ∀ j,MachineCost.run (TM1.step M) j ⟨none,v,T⟩=⟨none,v,T⟩ := by
        intro j
        induction j with
        | zero => rfl
        | succ j hj => exact hj
      exact ⟨0,Nat.zero_le _,by rw [MachineCost.run,he]⟩
    | some l =>
      have hl : l ∈ S:=Finset.some_mem_insertNone.mp hc
      obtain ⟨j,hj,hjc,hje⟩:=statement_translation_time M (M l) v T
      have hc' : (TM1.stepAux (M l) v T).l ∈ S.insertNone :=
        advance_supported M S hS hc
      obtain ⟨k,hk,hke⟩:=ih hc'
      refine ⟨j+k,?_,?_⟩
      · have hj' := hjc.trans (cost_le M S hl)
        calc
          j+k ≤ cost M S+cost M S*n:=Nat.add_le_add hj' hk
          _=cost M S*(n+1):=by ring
      · rw [run_add]
        change MachineCost.run (TM0.step (TM1to0.tr M)) k
          (MachineCost.run (TM0.step (TM1to0.tr M)) j ⟨(some (M l),v),T⟩)=_
        rw [hje,hke]
        rfl
end MatchingFPRAS.SupportedCompiler

namespace MatchingFPRAS.AlphabetCost
open Turing MachineCost SupportedCompiler
open scoped Classical

variable {Γ Λ σ : Type*} [Inhabited Γ] [Inhabited Λ] [Inhabited σ]
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 Γ Λ σ)
  (S : Finset Λ) (hS : TM1.Supports M S)
include encdec hS
noncomputable def supportedWrites : ℕ := S.sup (fun l=>writesCount (M l))
omit [Inhabited σ] hS in
lemma one_step_supported (_hS : TM1.Supports M S) {c : TM1.Cfg Γ Λ σ} (hc : c.l ∈ S.insertNone) :
    ∃ t : ℕ,t ≤ supportedWrites M S+1 ∧
      MachineCost.run (TM1.step (TM1to1.tr enc dec M)) t (TM1to1.trCfg enc enc0 c)=
      TM1to1.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
    have hh : writesCount (M l) ≤ supportedWrites M S:=
      Finset.le_sup (f:=fun l=>writesCount (M l)) (Finset.some_mem_insertNone.mp hc)
    refine ⟨t+1,by omega,?_⟩
    simpa only [MachineCost.run,advance,TM1.step,Option.map_some,TM1to1.trCfg,TM1to1.tr,
      Option.getD_some,TM1to1.trTape_mk'] using he

omit [Inhabited σ] in
theorem execution_supported {c : TM1.Cfg Γ Λ σ} (hc : c.l ∈ S.insertNone) (k : ℕ) :
    ∃ t : ℕ,t ≤ k*(supportedWrites M S+1) ∧
      MachineCost.run (TM1.step (TM1to1.tr enc dec M)) t (TM1to1.trCfg enc enc0 c)=
      TM1to1.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_supported enc dec enc0 encdec M S hS hc
    obtain ⟨t₂,ht₂,he₂⟩:=ih (advance_supported M S hS hc)
    refine ⟨t₁+t₂,?_,?_⟩
    · calc
        t₁+t₂ ≤ (supportedWrites M S+1)+k*(supportedWrites M S+1) := Nat.add_le_add ht₁ ht₂
        _=(k+1)*(supportedWrites M S+1) := by ring
    · rw [run_add,he₁,he₂]; rfl
end MatchingFPRAS.AlphabetCost

end OAI