File size: 14,513 Bytes
b224933
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import OAI.ModelTheory.Choiceless.Counters

namespace OAI

namespace CPTSeparation.TableMachine

open CounterLang

inductive Index where
 | t | y | a | x | z
 deriving DecidableEq

instance : Fintype Index := ⟨{.t,.y,.a,.x,.z}, by intro i; cases i <;> simp⟩

inductive Reg where
 | size | width
 | idx : Index → Reg
 | rem : Index → Reg
 | addr | prod | tmp | cmp
 deriving DecidableEq, Fintype

abbrev CState (V : Type) := CounterLang.State Reg (Bool × V)

abbrev Prog (V : Type) := CounterLang.Program Reg (Bool × V)

lemma tableInput_length {N} (S : OrderedQuery.Data N) : (tableInput S).length = 8*(N*N) := by
 simp [tableInput]

lemma tableInput_lookup {N} (S : OrderedQuery.Data N) (r : Symbol) (a b : Fin N) :
 (tableInput S)[((OrderedQuery.symbolIndex r).val*N+a.val)*N+b.val]? =
   some (bitLetter (S.rel r a b)) := by
 have hi : ((OrderedQuery.symbolIndex r).val*N+a.val)*N+b.val =
     (OrderedQuery.cellIndex r a b).val := by
   simp [OrderedQuery.cellIndex,finProdFinEquiv]
   ring
 rw [hi]
 simp [tableInput,OrderedQuery.tableBits,OrderedQuery.cellValue_index]

def readCell {V} (k : ℕ) (i j : Index) (f : V → Option Letter → V) : Prog V :=
 .seq (.atom (.zero .addr))
 (.seq (repeatAdd .size .addr (by decide) k)
 (.seq (.atom (.add (.idx i) .addr (by simp)))
 (.seq (multiply .addr .size .prod .tmp (by decide) (by decide))
 (.seq (.atom (.add (.idx j) .prod (by simp)))
 (.seq (.atom (.lookup .prod (fun c a => (c.1,f c.2 a))))
 (.seq (.atom (.zero .addr)) (.atom (.zero .prod))))))))

lemma readCell_runs {V} (s : CState V) (k : ℕ) (i j : Index) (f : V → Option Letter → V)
   (haddr : s.nums .addr = 0) (hprod : s.nums .prod = 0) (htmp : s.nums .tmp = 0) :
 Runs (readCell k i j f) s
   {s with ctl := (false,f s.ctl.2 s.input[((k*s.nums .size+s.nums (.idx i))*s.nums .size+s.nums (.idx j))]?)}
   (16*k*(s.nums .size)^2+16*s.nums (.idx i)*s.nums .size +
     6*k*s.nums .size+8*k+16*s.nums .size+6*s.nums (.idx i)+16*s.nums (.idx j)+10*s.input.length+101) := by
 let s₁ := (Atom.zero .addr).eval s
 let s₂ : CState V := {s₁ with nums := Function.update s₁.nums .addr (s₁.nums .addr+k*s₁.nums .size)}
 let s₃ := (Atom.add (.idx i) .addr (by simp)).eval s₂
 let s₄ : CState V := {s₃ with ctl := (false,s₃.ctl.2), nums := Function.update (Function.update s₃.nums .prod (s₃.nums .addr*s₃.nums .size)) .tmp 0}
 let s₅ := (Atom.add (.idx j) .prod (by simp)).eval s₄
 let s₆ := (Atom.lookup .prod (fun c a => (c.1,f c.2 a))).eval s₅
 let s₇ := (Atom.zero .addr).eval s₆
 let s₈ := (Atom.zero .prod).eval s₇
 have hm := multiply_runs .addr .size .prod .tmp (by decide) (by decide) (by decide) (by decide) (by decide) s₃
 have h := (Runs.atom (.zero .addr) s).seq
   ((repeatAdd_runs .size .addr (by decide) k s₁).seq
   ((Runs.atom (.add (.idx i) .addr (by simp)) s₂).seq
   (hm.seq ((Runs.atom (.add (.idx j) .prod (by simp)) s₄).seq
   ((Runs.atom (.lookup .prod (fun c a => (c.1,f c.2 a))) s₅).seq
   ((Runs.atom (.zero .addr) s₆).seq (Runs.atom (.zero .prod) s₇)))))))
 have he : s₈ = {s with ctl := (false,f s.ctl.2 s.input[((k*s.nums .size+s.nums (.idx i))*s.nums .size+s.nums (.idx j))]?)} := by
   apply CounterLang.State.ext
   · simp [s₈,s₇,s₆,s₅,s₄,s₃,s₂,s₁,Atom.eval,Function.update,Nat.add_comm]
   · funext r; cases r <;> simp [s₈,s₇,s₆,s₅,s₄,s₃,s₂,s₁,Atom.eval,Function.update,haddr,hprod,htmp]
   · rfl
   · rfl
 change Runs (readCell k i j f) s s₈ _ at h
 rw [he] at h
 apply h.mono
 simp only [s₇,s₆,s₅,s₄,s₃,s₂,s₁,Atom.eval,Atom.cost]
 simp [Function.update,haddr,hprod,htmp]
 ; nlinarith

end CPTSeparation.TableMachine

namespace CPTSeparation.Bounded

section

open CounterLang TableMachine

inductive Program (V : Type) where
 | load (f : V → V)
 | emit (f : V → Letter)
 | cell (r : Symbol) (i j : Index) (f : V → Bool → V)
 | equal (i j : Index) (f : V → Bool → V)
 | seq (a b : Program V)
 | loop (i : Index) (body : Program V)

variable {V : Type}

def isOne (a : Option Letter) : Bool := a == some (some 1)

def Program.WF : Program V → Finset Index → Prop
 | .load _,_ => True
 | .emit _,_ => True
 | .cell _ _ _ _,_ => True
 | .equal _ _ _,_ => True
 | .seq a b,S => a.WF S ∧ b.WF S
 | .loop i b,S => i ∉ S ∧ b.WF (insert i S)

def Program.eval (N : ℕ) (table : List Letter) :
    Program V → (Index → ℕ) → V × List Letter → V × List Letter
 | .load f,_,s => (f s.1,s.2)
 | .emit f,_,s => (s.1,f s.1::s.2)
 | .cell r i j f,e,s => (f s.1 (isOne table[((OrderedQuery.symbolIndex r).val*N+e i)*N+e j]?),s.2)
 | .equal i j f,e,s => (f s.1 (e i == e j),s.2)
 | .seq a b,e,s => b.eval N table e (a.eval N table e s)
 | .loop i b,e,s => (List.range N).foldl (fun s k => b.eval N table (Function.update e i k) s) s

def equalCode (i j : Index) (f : V → Bool → V) : Prog V :=
 .seq (CounterLang.compare (.idx i) (.idx j) .addr .prod (by simp) (by simp) f)
   (.seq (.atom (.zero .addr)) (.atom (.zero .prod)))

def Program.compile : Program V → Prog V
 | .load f => .atom (.load (fun c => (false,f c.2)))
 | .emit f => .seq (.atom (.emit (fun c => f c.2))) (.atom (.load (fun c => (false,c.2))))
 | .cell r i j f => readCell (OrderedQuery.symbolIndex r).val i j (fun v a => f v (isOne a))
 | .equal i j f => equalCode i j f
 | .seq a b => .seq a.compile b.compile
 | .loop i b => .seq (.atom (.load (fun c => (false,c.2))))
   (forRange .size (.idx i) (.rem i) (by simp) b.compile)

def Program.time (N L : ℕ) : Program V → ℕ
 | .load _ => 1
 | .emit _ => 3
 | .cell _ _ _ _ => 1000*(N+1)^2+10*L
 | .equal _ _ _ => 100*(N+1)
 | .seq a b => 1+(a.time N L+b.time N L)
 | .loop _ b => (b.time N L+13)*N+22

def env (s : CState V) : Index → ℕ := fun i => s.nums (.idx i)

def result (s : CState V) (v : V × List Letter) : CState V :=
 {s with ctl := (false,v.1), output := v.2}

def Ready (S : Finset Index) (N : ℕ) (s : CState V) : Prop :=
 s.nums .size = N ∧ s.nums .addr = 0 ∧ s.nums .prod = 0 ∧ s.nums .tmp = 0 ∧
 (∀ i, s.nums (.idx i) ≤ N) ∧ (∀ i ∉ S, s.nums (.idx i) = 0 ∧ s.nums (.rem i) = 0)

@[simp] lemma ready_result {S N} (s : CState V) (v) : Ready S N (result s v) ↔ Ready S N s := Iff.rfl

@[simp] lemma env_result (s : CState V) (v) : env (result s v) = env s := rfl

@[simp] lemma result_result (s : CState V) (v w) : result (result s v) w = result s w := rfl

lemma ready_index {S N} (s : CState V) (hs : Ready S N s) (i : Index) (k : ℕ) (hk : k≤N) :
 Ready (insert i S) N (indexState (.idx i) (.rem i) N k s) := by
 rcases hs with ⟨hN,ha,hp,ht,hidx,hr⟩
 refine ⟨?_,?_,?_,?_,?_,?_⟩
 · simpa [indexState,Function.update] using hN
 · simpa [indexState,Function.update] using ha
 · simpa [indexState,Function.update] using hp
 · simpa [indexState,Function.update] using ht
 · intro j
   by_cases hji : j=i
   · subst j; simpa [indexState,Function.update] using hk
   · simpa [indexState,Function.update,hji] using hidx j
 · intro j hj
   have hh : j≠i ∧ j∉S := by simpa using hj
   simpa [indexState,Function.update,hh.1] using hr j hh.2

@[simp] lemma env_index (s : CState V) (i : Index) (N k) :
 env (indexState (.idx i) (.rem i) N k s) = Function.update (env s) i k := by
 funext j; by_cases h : j=i <;> simp [env,indexState,Function.update,h]

lemma equalCode_runs (i j : Index) (f : V → Bool → V) (s : CState V)
    (ha : s.nums .addr=0) (hp : s.nums .prod=0) (N : ℕ)
    (hi : s.nums (.idx i)≤N) (hj : s.nums (.idx j)≤N) :
 Runs (equalCode i j f) s (result s (f s.ctl.2 (s.nums (.idx i)==s.nums (.idx j)),s.output)) (100*(N+1)) := by
 let s₁ : CState V := {s with ctl := (false,f s.ctl.2 (decide (s.nums (.idx i)=s.nums (.idx j)))), nums := Function.update (Function.update s.nums .addr (s.nums (.idx i)-s.nums (.idx j))) .prod (s.nums (.idx j)-s.nums (.idx i))}
 let s₂ := (Atom.zero .addr).eval s₁
 have hc := compare_runs (.idx i) (.idx j) .addr .prod (by simp) (by simp) (by simp) (by decide) f s
 change Runs _ s s₁ _ at hc
 have h := hc.seq ((Runs.atom (.zero .addr) s₁).seq (Runs.atom (.zero .prod) s₂))
 have he : (Atom.zero .prod).eval s₂ = result s (f s.ctl.2 (s.nums (.idx i)==s.nums (.idx j)),s.output) := by
   apply CounterLang.State.ext
   · rfl
   · funext r; cases r <;> simp [s₂,s₁,Atom.eval,result,Function.update,ha,hp]
   · rfl
   · rfl
 rw [he] at h
 apply h.mono
 simp [s₂,s₁,Atom.cost,Atom.eval,Function.update,ha,hp]
 omega

lemma cell_runs (r : Symbol) (i j : Index) (f : V → Bool → V)
    (s : CState V) {S N} (hs : Ready S N s) :
 Runs (Program.compile (.cell r i j f)) s
   (result s ((Program.cell r i j f).eval N s.input (env s) (s.ctl.2,s.output)))
   (1000*(N+1)^2+10*s.input.length) := by
 have h := readCell_runs s (OrderedQuery.symbolIndex r).val i j (fun v a => f v (isOne a)) hs.2.1 hs.2.2.1 hs.2.2.2.1
 have he : s.nums .size=N := hs.1
 simp only [he] at h
 apply h.mono
 have hk : (OrderedQuery.symbolIndex r).val ≤ 8 := (OrderedQuery.symbolIndex r).isLt.le
 have hi := hs.2.2.2.2.1 i
 have hj := hs.2.2.2.2.1 j
 calc
  _ ≤ 16*8*N^2+16*N*N+6*8*N+8*8+16*N+6*N+16*N+10*s.input.length+101 := by gcongr
  _ ≤ _ := by nlinarith

lemma compile_runs (p : Program V) (S : Finset Index) (hw : p.WF S)
    (N : ℕ) (s : CState V) (hs : Ready S N s) :
 Runs p.compile s (result s (p.eval N s.input (env s) (s.ctl.2,s.output)))
   (p.time N s.input.length) := by
 induction p generalizing S s with
 | load f => exact Runs.atom (.load (fun c => (false,f c.2))) s
 | emit f =>
   exact (Runs.atom (.emit (fun c => f c.2)) s).seq
     (Runs.atom (.load (fun c => (false,c.2))) ((Atom.emit (fun c => f c.2)).eval s))
 | cell r i j f => exact cell_runs r i j f s hs
 | equal i j f => exact equalCode_runs i j f s hs.2.1 hs.2.2.1 N (hs.2.2.2.2.1 i) (hs.2.2.2.2.1 j)
 | seq a b iha ihb =>
   have ha := iha S hw.1 s hs
   have hb := ihb S hw.2 (result s (a.eval N s.input (env s) (s.ctl.2,s.output))) hs
   exact ha.seq hb
 | loop i b ih =>
   let e := env s
   let step := fun v k => b.eval N s.input (Function.update e i k) v
   let pref := fun k => (List.range k).foldl step (s.ctl.2,s.output)
   let st := fun k => result s (pref k)
   have hstep (k : ℕ) : pref (k+1) = step (pref k) k := by simp [pref,List.range_succ,step]
   have hbody (k : ℕ) (hk : k<N) :
       Runs b.compile (indexState (.idx i) (.rem i) N k (st k))
         (flag (indexState (.idx i) (.rem i) N k (st (k+1))) false) (b.time N s.input.length) := by
     have hr : Ready (insert i S) N (indexState (.idx i) (.rem i) N k (st k)) :=
       ready_index (st k) hs i k hk.le
     have h := ih (insert i S) hw.2 (indexState (.idx i) (.rem i) N k (st k)) hr
     rw [env_index] at h
     change Runs _ _ (result (indexState (.idx i) (.rem i) N k (st k)) (step (pref k) k)) _ at h
     rw [← hstep k] at h
     exact h
   have hi := hs.2.2.2.2.2 i hw.1
   have h := forRange_runs TableMachine.Reg.size (.idx i) (.rem i) (by simp) (by simp)
     b.compile st N (b.time N s.input.length)
     (by simpa [st,result] using hs.1)
     (by simpa [st,result] using hi.1) (by simpa [st,result] using hi.2)
     (by simpa [st,result] using hi.1) (by simpa [st,result] using hi.2)
     rfl hbody
   have hin : Runs (.atom (.load (fun c : Bool × V => (false,c.2)))) s (st 0) 1 := by
     simpa [st,pref,result,Atom.eval,Atom.cost] using Runs.atom (.load (fun c : Bool × V => (false,c.2))) s
   have total := hin.seq h
   convert total using 1 <;> first | rfl | (simp only [Program.time]; omega)

end

open CounterLang TableMachine

variable {V : Type}

abbrev Relation := Symbol → ℕ → ℕ → Bool

def readTable (N : ℕ) (w : List Letter) : Relation := fun r a b =>
 isOne w[((OrderedQuery.symbolIndex r).val*N+a)*N+b]?

def Program.interpret (N : ℕ) (rel : Relation) :
    Program V → (Index → ℕ) → V × List Letter → V × List Letter
 | .load f,_,s => (f s.1,s.2)
 | .emit f,_,s => (s.1,f s.1::s.2)
 | .cell r i j f,e,s => (f s.1 (rel r (e i) (e j)),s.2)
 | .equal i j f,e,s => (f s.1 (e i == e j),s.2)
 | .seq a b,e,s => b.interpret N rel e (a.interpret N rel e s)
 | .loop i b,e,s => (List.range N).foldl (fun s k => b.interpret N rel (Function.update e i k) s) s

lemma Program.eval_interpret (p : Program V) (N : ℕ) (w : List Letter) (e) (s) :
 p.eval N w e s = p.interpret N (readTable N w) e s := by
 induction p generalizing e s with
 | load _ => rfl
 | emit _ => rfl
 | cell _ _ _ _ => rfl
 | equal _ _ _ => rfl
 | seq a b iha ihb => simp only [Program.eval,Program.interpret,iha,ihb]
 | loop i b ih =>
   simp only [Program.eval,Program.interpret]
   congr 1
   funext s k
   exact ih _ _

lemma readTable_input {N} (S : OrderedQuery.Data N) (r : Symbol) (a b : Fin N) :
 readTable N (tableInput S) r a b = S.rel r a b := by
 rw [readTable,tableInput_lookup]
 cases h : S.rel r a b <;> decide +revert

lemma fold_emits {α : Type} (ls : List α) (step : V × List Letter → α → V × List Letter)
    (v : V) (chunk : α → List Letter)
    (h : ∀ a ∈ ls, ∀ out, step (v,out) a = (v,(chunk a).reverse++out)) (out) :
 ls.foldl step (v,out) = (v,(ls.flatMap chunk).reverse++out) := by
 induction ls generalizing out with
 | nil => rfl
 | cons a ls ih =>
   rw [List.foldl_cons,h a (by simp),ih (by intro b hb; exact h b (by simp [hb]))]
   simp [List.reverse_append,List.append_assoc]

lemma loop_emits (N : ℕ) (rel : Relation) (body : Program V) (i : Index)
    (e : Index → ℕ) (v : V) (chunk : ℕ → List Letter)
    (h : ∀ k<N, ∀ out, body.interpret N rel (Function.update e i k) (v,out) = (v,(chunk k).reverse++out))
    (out) :
 (Program.loop i body).interpret N rel e (v,out) =
   (v,((List.range N).flatMap chunk).reverse++out) := by
 exact fold_emits _ _ v chunk (by intro k hk; exact h k (List.mem_range.mp hk)) out

lemma Program.time_polynomial (p : Program V) :
 ∃ q : Polynomial ℕ, ∀ N, p.time N (8*(N*N)) = q.eval N := by
 induction p with
 | load f => exact ⟨1,by simp [Program.time]⟩
 | emit f => exact ⟨3,by simp [Program.time]⟩
 | cell r i j f => exact ⟨1000*(Polynomial.X+1)^2+80*Polynomial.X^2,by intro N; simp [Program.time]; ring⟩
 | equal i j f => exact ⟨100*(Polynomial.X+1),by simp [Program.time]⟩
 | seq a b iha ihb =>
   obtain ⟨p,hp⟩ := iha
   obtain ⟨q,hq⟩ := ihb
   exact ⟨1+(p+q),by intro N; simp [Program.time,hp,hq]⟩
 | loop i b ih =>
   obtain ⟨p,hp⟩ := ih
   exact ⟨(p+13)*Polynomial.X+22,by intro N; simp [Program.time,hp]⟩

end CPTSeparation.Bounded

end OAI