File size: 14,294 Bytes
8df23f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import OAI.Computability.BinPacking.CookLevin.InitializationMachineAt
import OAI.Computability.BinPacking.CookLevin.ValidityComposition

namespace OAI

namespace BinPackingGames.Foundations.Complexity.CookLevin.PayloadPresenceMachine

open Turing MachineComposition PostfixModel InitializationTemplate
open ClashMachine (State clean emitted)

variable {K Λ σ : Type} [DecidableEq K]

abbrev Alphabet (_ : K) := Bool
abbrev Ports (K : Type) := Fin 9 ↪ K

def presenceMap : Fin 6 ↪ Fin 9 := ⟨![2, 3, 4, 5, 7, 8], by decide⟩

omit [DecidableEq K] in
theorem closing_distinct (slots : Ports K) (j : Fin 6) : slots 6 ≠ (presenceMap.trans slots) j := by
  apply slots.injective.ne
  fin_cases j <;> decide

def closurePorts (slots : Ports K) : OrClosure.Ports K :=
  InitializationMachineAt.closurePorts (presenceMap.trans slots) (slots 6) (closing_distinct slots)

inductive CopyContext where
  | remaining | threshold | closing
  deriving DecidableEq

protected abbrev CopyContext.enumList : List CopyContext := [.remaining, .threshold, .closing]

protected theorem CopyContext.enumList_getElem?_ctorIdx_eq (x : CopyContext) :
    CopyContext.enumList[x.ctorIdx]? = some x := by
  cases x <;> rfl

protected theorem CopyContext.enumList_nodup : CopyContext.enumList.Nodup := by decide

instance : Fintype CopyContext where
  elems := ⟨CopyContext.enumList, CopyContext.enumList_nodup⟩
  complete x := by cases x <;> decide

inductive Label where
  | initialize
  | copy (context : CopyContext) (l : MachineUnaryAffineAt.Label)
  | presence (l : InitializationMachine.Label)
  | orLoop | cleanup | drain
  deriving DecidableEq, Fintype

def presenceLabels : InitializationMachine.Label ↪ Label :=
  ⟨Label.presence, fun _ _ h => by cases h; rfl⟩

def copySource (slots : Ports K) : CopyContext → K
  | .remaining | .closing => slots 0
  | .threshold => slots 1

def copyTarget (slots : Ports K) : CopyContext → K
  | .remaining => slots 2
  | .threshold => slots 4
  | .closing => slots 6

def copyExit (labels : Label ↪ Λ) : CopyContext → Λ
  | .remaining => labels (.copy .threshold .seed)
  | .threshold => labels (.copy .closing .seed)
  | .closing => labels (.presence .guard)

def statement (slots : Ports K) (labels : Label ↪ Λ) (exit : Option Λ) :
    Label → TM2.Stmt (Alphabet (K := K)) Λ (State σ)
  | .initialize => .push (slots 3) (fun _ => false) (.goto fun _ => labels (.copy .remaining .seed))
  | .copy context l => ValidityMachine.Nonempty.copyStatement (copySource slots context) (slots 5)
      (copyTarget slots context) (fun l => labels (.copy context l)) (some (copyExit labels context)) l
  | .presence .done => OrClosure.seed (closurePorts slots) (labels .orLoop)
  | .presence l => InitializationMachineAt.statement (presenceMap.trans slots)
      (presenceLabels.trans labels) l
  | .orLoop => OrClosure.loop (closurePorts slots) (labels .orLoop) (some (labels .cleanup))
  | .cleanup => .pop (slots 2) (fun s _ => s)
      (.pop (slots 4) (fun s _ => s) (.pop (slots 6) (fun s _ => s) (.goto fun _ => labels .drain)))
  | .drain => MachineDrain.drain (slots 3) (labels .drain) exit

structure Ready (slots : Ports K) (base : K → List Bool) (q i : Nat) : Prop where
  boundWord : base (slots 0) = encodeWord q
  indexWord : base (slots 1) = encodeWord i
  empty : ∀ j : Fin 9, j ≠ 0 → j ≠ 1 → j ≠ 7 → j ≠ 8 → base (slots j) = []

theorem Ready.emitted {slots : Ports K} {base : K → List Bool} {q i : Nat}
    (h : Ready slots base q i) (tokens : List Token) :
    Ready slots (emitted (slots 7) (slots 8) base tokens) q i := by
  constructor
  · simpa [ClashMachine.emitted, slots.injective.eq_iff] using h.boundWord
  · simpa [ClashMachine.emitted, slots.injective.eq_iff] using h.indexWord
  · intro j h₀ h₁ h₇ h₈
    simpa [ClashMachine.emitted, slots.injective.eq_iff, h₇, h₈] using h.empty j h₀ h₁ h₇ h₈

def working (slots : Ports K) (base : K → List Bool) (r j d c : Nat) : K → List Bool :=
  Function.update (Function.update (Function.update (Function.update base
    (slots 2) (encodeWord r)) (slots 3) (encodeWord j)) (slots 4) (encodeWord d))
      (slots 6) (encodeWord c)

def steps (q i : Nat) : Nat :=
  1 + (2 * (q + 1) + 1) + (2 * (i + 1) + 1) + (2 * (q + 1) + 1) +
    (InitializationMachine.selectorSteps 0 (q + 1) + (q + 1 + 2)) + 1 + (q + 3)

private theorem chain {A : Type*} {f : A → A} {x y z : A} {n m : Nat}
    (first : f^[n] x = y) (second : f^[m] y = z) : f^[n + m] x = z := by
  rw [Nat.add_comm n m, Function.iterate_add_apply, first, second]

theorem copyTrace (slots : Ports K) (labels : Label ↪ Λ) (exit : Option Λ)
    (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State σ))
    (atLabels : ∀ l, program (labels l) = statement slots labels exit l)
    (context : CopyContext) (base : K → List Bool) (n : Nat)
    (sourceWord : base (copySource slots context) = encodeWord n)
    (scratchEmpty : base (slots 5) = []) (targetEmpty : base (copyTarget slots context) = [])
    (ambient : σ) :
    (advance (TM2.step program))^[2 * (n + 1) + 1]
      (some ⟨some (labels (.copy context .seed)), clean ambient, base⟩) =
      some ⟨some (copyExit labels context), clean ambient,
        Function.update base (copyTarget slots context) (encodeWord (n + 1))⟩ := by
  have hs : copySource slots context ≠ slots 5 := by
    cases context <;> exact slots.injective.ne (by decide)
  have hd : copySource slots context ≠ copyTarget slots context := by
    cases context <;> exact slots.injective.ne (by decide)
  have hsd : slots 5 ≠ copyTarget slots context := by
    cases context <;> exact slots.injective.ne (by decide)
  have h := MachineUnaryAffineAt.seededAffineTrace (copySource slots context) (slots 5)
    (copyTarget slots context) hs hd hsd 1 1
    (labels (.copy context .seed)) (labels (.copy context .scan)) (labels (.copy context .restore))
    (some (copyExit labels context)) program
    (atLabels (.copy context .seed)) (atLabels (.copy context .scan)) (atLabels (.copy context .restore))
    base n [] (by simpa using sourceWord) scratchEmpty (ambient, false) none
  simpa only [Nat.one_mul, targetEmpty, List.append_nil, clean] using h

theorem trace (slots : Ports K) (labels : Label ↪ Λ) (exit : Option Λ)
    (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State σ))
    (atLabels : ∀ l, program (labels l) = statement slots labels exit l)
    (base : K → List Bool) (q i : Nat) (hi : i < q)
    (ready : Ready slots base q i) (ambient : σ) :
    (advance (TM2.step program))^[steps q i]
      (some ⟨some (labels .initialize), clean ambient, base⟩) =
      some ⟨exit, clean ambient, emitted (slots 7) (slots 8) base (presenceTokens q i)⟩ := by
  have e₂ := ready.empty 2 (by decide) (by decide) (by decide) (by decide)
  have e₃ := ready.empty 3 (by decide) (by decide) (by decide) (by decide)
  have e₄ := ready.empty 4 (by decide) (by decide) (by decide) (by decide)
  have e₅ := ready.empty 5 (by decide) (by decide) (by decide) (by decide)
  have e₆ := ready.empty 6 (by decide) (by decide) (by decide) (by decide)
  let b₀ := Function.update base (slots 3) (encodeWord 0)
  let b₁ := Function.update b₀ (slots 2) (encodeWord (q + 1))
  let b₂ := Function.update b₁ (slots 4) (encodeWord (i + 1))
  let b₃ := Function.update b₂ (slots 6) (encodeWord (q + 1))
  have hinit : (advance (TM2.step program))^[1]
      (some ⟨some (labels .initialize), clean ambient, base⟩) =
      some ⟨some (labels (.copy .remaining .seed)), clean ambient, b₀⟩ := by
    change some (TM2.stepAux (program (labels .initialize)) _ _) = _
    rw [atLabels, statement]
    simp [TM2.stepAux, e₃, b₀, encodeWord]
  have hr := copyTrace slots labels exit program atLabels .remaining b₀ q
    (by simpa [b₀, copySource, slots.injective.eq_iff] using ready.boundWord)
    (by simpa [b₀, slots.injective.eq_iff] using e₅)
    (by simpa [b₀, copyTarget, slots.injective.eq_iff] using e₂) ambient
  have ht := copyTrace slots labels exit program atLabels .threshold b₁ i
    (by simpa [b₁, b₀, copySource, slots.injective.eq_iff] using ready.indexWord)
    (by simpa [b₁, b₀, slots.injective.eq_iff] using e₅)
    (by simpa [b₁, b₀, copyTarget, slots.injective.eq_iff] using e₄) ambient
  have hc := copyTrace slots labels exit program atLabels .closing b₂ q
    (by simpa [b₂, b₁, b₀, copySource, slots.injective.eq_iff] using ready.boundWord)
    (by simpa [b₂, b₁, b₀, slots.injective.eq_iff] using e₅)
    (by simpa [b₂, b₁, b₀, copyTarget, slots.injective.eq_iff] using e₆) ambient
  let loopBase := Function.update base (slots 6) (encodeWord (q + 1))
  have hprepared : b₃ = InitializationMachineAt.tapes (presenceMap.trans slots)
      loopBase (q + 1) 0 (i + 1) (base (slots 7)) (base (slots 8)) := by
    apply ClashMachine.Full.ext_slots slots
    · intro j
      fin_cases j <;> simp [b₃, b₂, b₁, b₀, loopBase, InitializationMachineAt.tapes,
        InitializationMachineAt.frame, InitializationMachineAt.contents, presenceMap,
        slots.injective.eq_iff, e₅]
    · intro k hk
      simp [b₃, b₂, b₁, b₀, loopBase, InitializationMachineAt.tapes,
        InitializationMachineAt.frame, presenceMap, hk]
  have agrees : InitializationMachineAt.Agrees (presenceMap.trans slots)
      (presenceLabels.trans labels) program := by
    intro l hl
    change program (labels (.presence l)) =
      InitializationMachineAt.statement (presenceMap.trans slots) (presenceLabels.trans labels) l
    have h := atLabels (.presence l)
    cases l <;> try contradiction
    all_goals simpa only [statement] using h
  have hp := InitializationMachineAt.presenceTrace (presenceMap.trans slots) (presenceLabels.trans labels)
    (slots 6) (closing_distinct slots) (labels .orLoop) (some (labels .cleanup)) program agrees
    (atLabels (.presence .done)) (atLabels .orLoop) loopBase q i []
    (base (slots 7)) (base (slots 8)) (by simp [loopBase]) ambient
  dsimp only at hp
  let selected := InitializationMachineAt.tapes (presenceMap.trans slots) loopBase
    0 (q + 1) (i + 1 - (q + 1))
    ((tokenBits (selectorLoopTokens (q + 1) 0 (i + 1))).reverse ++ base (slots 7))
    (List.replicate (3 * (q + 1)) true ++ base (slots 8))
  let result := emitted (slots 7) (slots 8) base (presenceTokens q i)
  let produced := working slots result 0 (q + 1) 0 0
  have hsub : i + 1 - (q + 1) = 0 := by omega
  have hproduced : OrClosure.frame (closurePorts slots) selected (encodeWord 0 ++ [])
      ((tokenBits (presenceTokens q i)).reverse ++ base (slots 7))
      (List.replicate (4 * (q + 1) + 1) true ++ base (slots 8)) = produced := by
    apply ClashMachine.Full.ext_slots slots
    · intro j
      fin_cases j <;> simp [produced, working, result, emitted, OrClosure.frame,
        selected, closurePorts, InitializationMachineAt.closurePorts,
        InitializationMachineAt.tapes, InitializationMachineAt.frame,
        InitializationMachineAt.contents, presenceMap, loopBase, hsub,
        slots.injective.eq_iff, presenceTokens_length, e₅]
    · intro k hk
      simp [produced, working, result, emitted, OrClosure.frame,
        selected, closurePorts, InitializationMachineAt.closurePorts,
        InitializationMachineAt.tapes, InitializationMachineAt.frame, presenceMap, loopBase, hk]
  change (advance (TM2.step program))^[InitializationMachine.selectorSteps 0 (q + 1) + (q + 1 + 2)]
    (some ⟨some (labels (.presence .guard)), clean ambient,
      InitializationMachineAt.tapes (presenceMap.trans slots) loopBase
        (q + 1) 0 (i + 1) (base (slots 7)) (base (slots 8))⟩) =
    some ⟨some (labels .cleanup), clean ambient,
      OrClosure.frame (closurePorts slots) selected (encodeWord 0 ++ [])
        ((tokenBits (presenceTokens q i)).reverse ++ base (slots 7))
        (List.replicate (4 * (q + 1) + 1) true ++ base (slots 8))⟩ at hp
  rw [← hprepared, hproduced] at hp
  let cleared := Function.update (Function.update (Function.update produced (slots 2) [])
    (slots 4) []) (slots 6) []
  have hcleanup : (advance (TM2.step program))^[1]
      (some ⟨some (labels .cleanup), clean ambient, produced⟩) =
      some ⟨some (labels .drain), clean ambient, cleared⟩ := by
    change some (TM2.stepAux (program (labels .cleanup)) _ _) = _
    rw [atLabels, statement]
    simp [TM2.stepAux, produced, working, encodeWord, cleared, slots.injective.eq_iff]
  have hcurrent : cleared (slots 3) = encodeWord (q + 1) := by
    simp [cleared, produced, working, slots.injective.eq_iff]
  have hd := MachineDrain.drainTrace (slots 3) (labels .drain) exit program (atLabels .drain)
    cleared (encodeWord (q + 1)) (ambient, false) none
  rw [← hcurrent, Function.update_eq_self] at hd
  rw [hcurrent, encodeWord_length] at hd
  have hfinish : Function.update cleared (slots 3) [] = result := by
    apply ClashMachine.Full.ext_slots slots
    · intro j
      fin_cases j <;> simp [cleared, produced, working, result, emitted,
        slots.injective.eq_iff, e₂, e₃, e₄, e₆]
    · intro k hk
      simp [cleared, produced, working, hk]
  rw [hfinish] at hd
  exact chain (chain (chain (chain (chain (chain hinit hr) ht) hc) hp) hcleanup) hd

theorem steps_le (q i : Nat) (hi : i < q) : steps q i ≤ 30 * (q + 2) ^ 2 := by
  have h := InitializationMachineAt.presenceSteps_le q
  unfold steps
  nlinarith

def inTime (slots : Ports K) (labels : Label ↪ Λ) (exit : Option Λ)
    (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State σ))
    (atLabels : ∀ l, program (labels l) = statement slots labels exit l)
    (base : K → List Bool) (q i : Nat) (hi : i < q)
    (ready : Ready slots base q i) (ambient : σ) :
    StateTransition.EvalsToInTime (TM2.step program)
      ⟨some (labels .initialize), clean ambient, base⟩
      (some ⟨exit, clean ambient, emitted (slots 7) (slots 8) base (presenceTokens q i)⟩)
      (30 * (q + 2) ^ 2) where
  steps := steps q i
  evals_in_steps := by
    change (advance (TM2.step program))^[steps q i] _ = _
    exact trace slots labels exit program atLabels base q i hi ready ambient
  steps_le_m := steps_le q i hi

end BinPackingGames.Foundations.Complexity.CookLevin.PayloadPresenceMachine

end OAI