File size: 15,660 Bytes
760f90e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import OAI.Computability.Scheduling.StateBounds

namespace OAI

universe u1 u2 u3 u4 u5 u6 u7

section
namespace ThreeMachine.StackCompiler.Uniform
abbrev StatesInput (n : ℕ) := Universe n × (Matrix n ×
    (List (Finset (Fin n)) × (List (Finset (Fin n)) × Finset (Fin n))))
def statesC : Uniform (fun n (x : (Finset (Fin n) × Finset (Fin n)) ×
    (Universe n × Finset (Fin n))) => Algorithm.State.candidate x.2.2 x.1.1 x.1.2) :=
  ((snd.comp fst).pair ((snd.comp snd).pair ((fst.comp fst).pair (fst.comp snd)))).comp stateCandidate
def statesF : Uniform (fun n (x : StatesInput n) => x.2.2.1) := snd.comp (snd.comp fst)
def statesZ : Uniform (fun n (x : StatesInput n) => x.2.2.2.1) := snd.comp (snd.comp (snd.comp fst))
def statesW : Uniform (fun n (x : StatesInput n) => x.2.2.2.2) := snd.comp (snd.comp (snd.comp snd))
def statesPairs := (statesF.pair statesZ).comp product
def statesS := (statesPairs.pair (fst.pair statesW)).comp statesC.map
def statesP : Uniform (fun n (x : Algorithm.State (Fin n) ×
    (Universe n × (Matrix n × Finset (Fin n)))) =>
    decide (x.1.Compatible (matrixRel x.2.2.1) x.2.2.2)) :=
  ((snd.comp fst).pair ((snd.comp (snd.comp fst)).pair ((snd.comp (snd.comp snd)).pair fst))).comp stateCompatible
theorem time_states (n : ℕ) (x : StatesInput n) :
    states.time n x =
    ((statesS.pair (fst.pair ((snd.comp fst).pair statesW))).comp statesP.filter).time n x := rfl
end ThreeMachine.StackCompiler.Uniform
namespace ThreeMachine.StackCompiler.Costs
theorem statesC : Poly (fun x : Σ n, (Finset (Fin n) × Finset (Fin n)) ×
    (Universe n × Finset (Fin n)) => x.1)
    (fun x => Uniform.statesC.time x.1 x.2) 5 := by poly_auto
theorem statesP : Poly (fun x : Σ n, Algorithm.State (Fin n) ×
    (Universe n × (Matrix n × Finset (Fin n))) => x.1)
    (fun x => Uniform.statesP.time x.1 x.2) 5 := by poly_auto
end ThreeMachine.StackCompiler.Costs
end

section
namespace ThreeMachine.StackCompiler.Costs
variable {J : Type} (n : J → ℕ) (U : ∀ j, Universe (n j)) (M : ∀ j, Matrix (n j))
variable (fs zs : ∀ j, List (Finset (Fin (n j)))) (W : ∀ j, Finset (Fin (n j)))
theorem statesPairs
    (hf : Poly n (fun j => (fs j).length) 30000) (hz : Poly n (fun j => (zs j).length) 3) :
    Poly n (fun j => Uniform.statesPairs.time (n j) (U j,(M j,(fs j,(zs j,W j))))) 60020 := by
  have hfv := Poly.volumeSetList fs (Poly.size n) hf
  have hzv := Poly.volumeSetList zs (Poly.size n) hz
  poly_auto

theorem statesS
    (hf : Poly n (fun j => (fs j).length) 30000) (hz : Poly n (fun j => (zs j).length) 3) :
    Poly n (fun j => Uniform.statesS.time (n j) (U j,(M j,(fs j,(zs j,W j))))) 60020 := by
  have hp := statesPairs n U M fs zs W hf hz
  have hfv := Poly.volumeSetList fs (Poly.size n) hf
  have hzv := Poly.volumeSetList zs (Poly.size n) hz
  poly_auto

theorem states
    (hf : Poly n (fun j => (fs j).length) 30000) (hz : Poly n (fun j => (zs j).length) 3) :
    Poly n (fun j => Uniform.states.time (n j) (U j,(M j,(fs j,(zs j,W j))))) 120008 := by
  have hS := statesS n U M fs zs W hf hz
  have hfv := Poly.volumeSetList fs (Poly.size n) hf
  have hzv := Poly.volumeSetList zs (Poly.size n) hz
  poly_auto
end ThreeMachine.StackCompiler.Costs
end

section
namespace ThreeMachine.StackCompiler.Uniform
abbrev BlockAppendCellInput (n : ℕ) := Fin n ×
  (Finset (Fin n) × (Algorithm.Block (Fin n) × Algorithm.Block (Fin n)))
def appendCellS : Uniform (fun n (x : BlockAppendCellInput n) => x.2.2.1) := snd.comp (snd.comp fst)
def appendCellT : Uniform (fun n (x : BlockAppendCellInput n) => x.2.2.2) := snd.comp (snd.comp snd)
def appendCellTest : Uniform (fun n (x : BlockAppendCellInput n) => decide (x.1 ∈ x.2.1)) :=
  (fst.pair (snd.comp fst)).comp setMem
def appendCellLeft : Uniform (fun n (x : BlockAppendCellInput n) => x.2.2.1.time x.1) :=
  (((appendCellS.comp blockTime).pair fst).comp functionGet)
def appendCellRight : Uniform (fun n (x : BlockAppendCellInput n) => x.2.2.1.length+x.2.2.2.time x.1) :=
  (((appendCellS.comp blockLength).pair (((appendCellT.comp blockTime).pair fst).comp functionGet)).comp
    (Realizer.add.uniform ℕ))
def appendCell : Uniform (fun n (x : BlockAppendCellInput n) =>
    if x.1 ∈ x.2.1 then x.2.2.1.time x.1 else x.2.2.1.length+x.2.2.2.time x.1) :=
  (appendCellTest.ite appendCellLeft appendCellRight).congr (fun _ _ => by simp)
abbrev BlockAppendInput (n : ℕ) := Universe n ×
    (Finset (Fin n) × (Algorithm.Block (Fin n) × Algorithm.Block (Fin n)))
def appendLength : Uniform (fun n (x : BlockAppendInput n) => x.2.2.1.length+x.2.2.2.length) :=
  ((((snd.comp (snd.comp fst)).comp blockLength).pair
      ((snd.comp (snd.comp snd)).comp blockLength)).comp (Realizer.add.uniform ℕ))
def blockAppendNamed : Uniform (fun n (x : BlockAppendInput n) =>
    Algorithm.Block.append x.2.1 x.2.2.1 x.2.2.2) :=
  (appendLength.pair appendCell.vectorMap).comp blockMake
theorem time_blockAppend (n : ℕ) (x : BlockAppendInput n) :
    blockAppend.time n x = blockAppendNamed.time n x := rfl
end ThreeMachine.StackCompiler.Uniform
namespace ThreeMachine.StackCompiler.Costs
variable {J : Type u1} (n : J → ℕ) (x : ∀ j, Uniform.BlockAppendCellInput (n j))
theorem appendCellLeft
    (hb : Poly n (fun j => volume (x j).2.2.1) 2)
    (hc : Poly n (fun j => volume (x j).2.2.2) 2) :
    Poly n (fun j => Uniform.appendCellLeft.time (n j) (x j)) 4 := by poly_auto

theorem appendCellRight
    (hb : Poly n (fun j => volume (x j).2.2.1) 2)
    (hc : Poly n (fun j => volume (x j).2.2.2) 2) :
    Poly n (fun j => Uniform.appendCellRight.time (n j) (x j)) 4 := by poly_auto

theorem appendCell
    (hb : Poly n (fun j => volume (x j).2.2.1) 2)
    (hc : Poly n (fun j => volume (x j).2.2.2) 2) :
    Poly n (fun j => Uniform.appendCell.time (n j) (x j)) 4 := by
  have hL := appendCellLeft n x hb hc
  have hR := appendCellRight n x hb hc
  poly_auto
end ThreeMachine.StackCompiler.Costs
end

section
namespace ThreeMachine.StackCompiler.Uniform
def appendVector := appendCell.vectorMap
theorem time_blockAppendNamed (n : ℕ) (x : BlockAppendInput n) :
    blockAppendNamed.time n x = ((appendLength.pair appendVector).comp blockMake).time n x := rfl
end ThreeMachine.StackCompiler.Uniform
namespace ThreeMachine.StackCompiler.Costs
variable {J : Type u2} (n : J → ℕ)
variable (U : ∀ j, Universe (n j)) (W : ∀ j, Finset (Fin (n j)))
variable (b c : ∀ j, Algorithm.Block (Fin (n j)))

theorem appendLength
    (hb : Poly n (fun j => volume (b j)) 2)
    (hc : Poly n (fun j => volume (c j)) 2) :
    Poly n (fun j => Uniform.appendLength.time (n j) (U j,(W j,(b j,c j)))) 4 := by
  poly_auto

theorem appendVector
    (hb : Poly n (fun j => volume (b j)) 2)
    (hc : Poly n (fun j => volume (c j)) 2) :
    Poly n (fun j => Uniform.appendVector.time (n j) (U j,(W j,(b j,c j)))) 5 := by
  have hcell := appendCell (fun p : Poly.ListPool (fun j => List.finRange (n j)) => n p.1)
    (fun p => (p.2.1,(W p.1,(b p.1,c p.1)))) (hb.precomp Sigma.fst) (hc.precomp Sigma.fst)
  poly_auto

theorem blockAppend
    (hb : Poly n (fun j => volume (b j)) 2)
    (hc : Poly n (fun j => volume (c j)) 2) :
    Poly n (fun j => Uniform.blockAppend.time (n j) (U j,(W j,(b j,c j)))) 5 := by
  have hL := appendLength n U W b c hb hc
  have hV := appendVector n U W b c hb hc
  poly_auto
end ThreeMachine.StackCompiler.Costs
end

section
namespace ThreeMachine.StackCompiler.Uniform
abbrev AdvanceInput (n : ℕ) := Universe n ×
    (Algorithm.State (Fin n) × (Algorithm.Block (Fin n) × Algorithm.Block (Fin n)))
def advanceU : Uniform (fun n (x : AdvanceInput n) => x.1) := fst
def advanceV : Uniform (fun n (x : AdvanceInput n) => x.2.1) := snd.comp fst
def advanceS : Uniform (fun n (x : AdvanceInput n) => x.2.2.1) := snd.comp (snd.comp fst)
def advanceT : Uniform (fun n (x : AdvanceInput n) => x.2.2.2) := snd.comp (snd.comp snd)
def advanceB : Uniform (fun n (x : AdvanceInput n) =>
    Algorithm.Block.append x.2.1.left x.2.2.1 Algorithm.Block.triple) :=
  (advanceU.pair ((advanceV.comp stateLeft).pair
    (advanceS.pair (advanceU.comp blockTriple)))).comp blockAppend
theorem time_stateAdvance (n : ℕ) (x : AdvanceInput n) :
    stateAdvance.time n x =
      ((advanceU.pair (((advanceU.pair advanceV).comp stateUpto).pair
        (advanceB.pair advanceT))).comp blockAppend).time n x := rfl
end ThreeMachine.StackCompiler.Uniform
namespace ThreeMachine.StackCompiler.Poly
variable {J : Type} {s n : J → ℕ} {d : ℕ}
theorem volumeAppendTriple (W : ∀ j, Finset (Fin (n j))) (b : ∀ j, Algorithm.Block (Fin (n j)))
    (hn : Poly s n d) (hb : ∀ j, BlockSmall (b j)) :
    Poly s (fun j => volume (Algorithm.Block.append (W j) (b j) Algorithm.Block.triple)) (2*d) := by
  apply of_le (g := fun j => 4*(n j+1)*(n j+2))
  · intro j
    apply volume_block_bound _ (n j+1)
    · change (b j).length+1 ≤ n j+1
      exact Nat.add_le_add_right (hb j).1 1
    · intro a
      simp only [Algorithm.Block.append,Algorithm.Block.triple]
      have := (hb j).1; have := (hb j).2 a
      split_ifs <;> omega
  · poly_bound
end ThreeMachine.StackCompiler.Poly
namespace ThreeMachine.StackCompiler.Costs
variable {J : Type} (n : J → ℕ) (U : ∀ j, Universe (n j))
variable (v : ∀ j, Algorithm.State (Fin (n j))) (b c : ∀ j, Algorithm.Block (Fin (n j)))
theorem advanceB (hb : Poly n (fun j => volume (b j)) 2)
    (hc : Poly n (fun j => volume (c j)) 2) :
    Poly n (fun j => Uniform.advanceB.time (n j) (U j,(v j,(b j,c j)))) 5 := by
  have htriple : Poly n (fun j => volume (Algorithm.Block.triple : Algorithm.Block (Fin (n j)))) 2 := by poly_auto
  have h := blockAppend n U (fun j => (v j).left) b (fun _ => Algorithm.Block.triple) hb htriple
  poly_auto

theorem stateAdvance (hb : ∀ j, BlockSmall (b j)) (hc : ∀ j, BlockSmall (c j)) :
    Poly n (fun j => Uniform.stateAdvance.time (n j) (U j,(v j,(b j,c j)))) 7 := by
  have hbV := Poly.volumeSmallBlock b (Poly.size n) hb
  have hcV := Poly.volumeSmallBlock c (Poly.size n) hc
  have hB := advanceB n U v b c hbV hcV
  have hBVol := Poly.volumeAppendTriple (fun j => (v j).left) b (Poly.size n) hb
  have hA := blockAppend n U (fun j => (v j).upto)
    (fun j => Algorithm.Block.append (v j).left (b j) Algorithm.Block.triple) c hBVol hcV
  poly_auto
end ThreeMachine.StackCompiler.Costs
end

section
namespace ThreeMachine.StackCompiler

theorem volume_option_mem_le {α : Type u3} [Coding α] {o : Option α} {a : α} (ha : a ∈ o) :
    volume a ≤ volume o := by cases o <;> simp_all [volume_some]

theorem volume_find_le {α : Type u4} [Coding α] (xs : List α) (p : α → Bool) :
    volume (xs.find? p) ≤ volume xs+2 := volume_option_le _ _ (fun _a ha =>
      volume_mem_le (List.mem_of_find?_eq_some ha))

theorem volume_lookup_le {K V : Type} [Coding K] [Coding V] [DecidableEq K]
    (xs : List (K × V)) (k : K) : volume (Algorithm.lookup xs k) ≤ volume xs+2 :=
  volume_option_le _ _ (fun v hv => by
    have h := volume_mem_le (Algorithm.lookup_mem hv)
    rw [volume_pair] at h; omega)

namespace Uniform
variable {I : Type u5} {α : I → Type u6} {β : I → Type u7} [∀ i, Coding (α i)] [∀ i, Coding (β i)]

theorem time_mapSndOption_le (i : I) (o : Option (α i × β i)) :
    (snd : Uniform (fun i (x : α i × β i) => x.2)).mapOption.time i o ≤
      1000000000*(volume o+1) := by
  let R : Uniform (fun i (x : (α i × β i) × Unit) => x.1.2) := fst.comp snd
  have hm := time_optionMap R i o ()
    (100*(volume o+1)) (volume o) (by
      intro a ha
      have hv := volume_option_mem_le ha
      simp only [R,time_comp,time_fst,time_snd,volume_prod,volume_unit]
      rw [volume_pair] at hv
      omega) (by
      intro a ha
      have hv := volume_option_mem_le ha
      rw [volume_prod] at hv
      dsimp only
      omega)
  simp only [mapOption,time_congr,time_comp,time_pair,time_id,time_unit,Function.comp_apply,volume_pair,volume_unit]
  dsimp only [R] at hm
  simp only [volume_unit] at hm
  nlinarith [volume_pos o]

theorem time_lookup_le [∀ i, DecidableEq (α i)]
    (E : Uniform (fun i (x : α i × α i) => decide (x.1 = x.2)))
    (i : I) (xs : List (α i × β i)) (k : α i) (T : ℕ)
    (hT : ∀ a ∈ xs, E.time i (a.1,k) ≤ T) :
    (lookup E).time i (xs,k) ≤ 100000000000*(xs.length+1)*(T+volume xs+volume k+1) := by
  let P := (((fst.comp fst).pair snd).comp E : Uniform (fun i (x : (α i × β i) × α i) => decide (x.1.1=x.2)))
  have hp : ∀ a ∈ xs, P.time i (a,k) ≤ T+100*(volume xs+volume k+1) := by
    intro a ha
    have h := hT a ha
    have hv := volume_mem_le ha
    simp only [P,time_comp,time_pair,time_fst,time_snd,Function.comp_apply,volume_prod]
    rw [volume_pair] at hv
    omega
  have hf := time_find? P i xs k (T+100*(volume xs+volume k+1)) hp
  have hm := time_mapSndOption_le i (xs.find? (fun a => decide (a.1=k)))
  have hv := volume_find_le xs (fun a => decide (a.1=k))
  simp only [lookup,time_congr,time_comp,Function.comp_apply]
  change P.find?.time i (xs,k)+_+10*(volume (xs.find? (fun a => decide (a.1=k)))+1) ≤ _
  nlinarith [volume_pos xs,volume_pos k]

end Uniform
end ThreeMachine.StackCompiler
end

section
namespace ThreeMachine.StackCompiler.Uniform
theorem time_lookup_setEq_le {n : ℕ}
    (xs : List (Finset (Fin n) × Algorithm.Block (Fin n))) (k : Finset (Fin n)) :
    (lookupA (β := fun n => Algorithm.Block (Fin n)) setEq).time n (xs,k) ≤ 100000000000*(xs.length+1)*
      (Classical.choose Costs.setEq*(n+2)^4+volume xs+volume k+1) := by
  apply time_lookup_le setEq
  intro a _ha
  exact Classical.choose_spec Costs.setEq ⟨n,(a.1,k)⟩
theorem time_lookup_stateEq_le {n : ℕ}
    (xs : List (Algorithm.State (Fin n) × Algorithm.Block (Fin n))) (k : Algorithm.State (Fin n)) :
    (lookupA (β := fun n => Algorithm.Block (Fin n)) stateEq).time n (xs,k) ≤ 100000000000*(xs.length+1)*
      (Classical.choose Costs.stateEq*(n+2)^4+volume xs+volume k+1) := by
  apply time_lookup_le stateEq
  intro a _ha
  exact Classical.choose_spec Costs.stateEq ⟨n,(a.1,k)⟩
end ThreeMachine.StackCompiler.Uniform
end

section
namespace ThreeMachine.StackCompiler.Uniform
abbrev TryInput (n : ℕ) := Universe n ×
    (Table n × ((Algorithm.State (Fin n) × Algorithm.Block (Fin n)) × Algorithm.State (Fin n)))
def tryU : Uniform (fun n (x : TryInput n) => x.1) := fst
def tryT : Uniform (fun n (x : TryInput n) => x.2.1) := snd.comp fst
def tryA : Uniform (fun n (x : TryInput n) => x.2.2.1) := snd.comp (snd.comp fst)
def tryV : Uniform (fun n (x : TryInput n) => x.2.2.2) := snd.comp (snd.comp snd)
def tryW := (tryU.pair (tryA.comp fst)).comp stateUpto
def tryL := tryV.comp stateLeft
def tryP := (tryW.pair tryL).comp setSubset
def tryDiff := (tryL.pair tryW).comp setDiff
def tryO := (tryT.pair tryDiff).comp (lookupA setEq)
def tryWork := (tryO.pair (tryU.pair tryA)).comp advanceLast.optionMap
theorem time_tryAdvance (n : ℕ) (x : TryInput n) :
    tryAdvance.time n x = (tryP.ite tryWork none).time n x := rfl
end ThreeMachine.StackCompiler.Uniform
namespace ThreeMachine.StackCompiler.Costs
variable {J : Type} (n : J → ℕ) (U : ∀ j, Universe (n j))
variable (u : ∀ j, Algorithm.State (Fin (n j)) × Algorithm.Block (Fin (n j)))
variable (c : ∀ j, Algorithm.Block (Fin (n j)))
theorem advanceLast (hb : ∀ j, BlockSmall (u j).2) (hc : ∀ j, BlockSmall (c j)) :
    Poly n (fun j => Uniform.advanceLast.time (n j) (c j,(U j,u j))) 7 := by
  have h := stateAdvance n U (fun j => (u j).1) (fun j => (u j).2) c hb hc
  have hvC := Poly.volumeSmallBlock c (Poly.size n) hc
  have hvB := Poly.volumeSmallBlock (fun j => (u j).2) (Poly.size n) hb
  poly_auto
end ThreeMachine.StackCompiler.Costs
end

end OAI