File size: 12,317 Bytes
37bcf52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import OAI.MathematicalPhysics.NavierStokes.ForcedComputation.Programs.BalancedRules

namespace OAI

/-! Finite sweeps in the balanced recorder.  The preparatory cycle restores
both work tapes and moves exactly one blank onto the history stack. -/

namespace ForcedComputation.Balanced

variable {Q A : Type*}
open ForcedComputation.Balanced.Symbol

abbrev push := @Radix.prepend (Symbol Q A)
abbrev word := @Radix.prependWord (Symbol Q A)

def triple (q : Control Q) (l r h : Stack Q A) : Configuration Q A :=
  ⟨q, ![l, r, h]⟩

theorem prefixConfiguration_three (q : Control Q)
    (a b c : List (Symbol Q A)) (l r h : Stack Q A) :
    prefixConfiguration q ![a, b, c] ![l, r, h] =
      triple q (word a l) (word b r) (word c h) := by
  change Configuration.mk q _ = Configuration.mk q _
  apply congrArg (Configuration.mk q)
  funext j
  fin_cases j <;> rfl

inductive Steps (M : Recorder.Machine Q A) (blank : A) :
    ℕ → Configuration Q A → Configuration Q A → Prop
  | refl (x) : Steps M blank 0 x x
  | next {n x y z} : Step M blank x y → Steps M blank n y z → Steps M blank (n + 1) x z

theorem Step.steps {M : Recorder.Machine Q A} {blank : A} {x y : Configuration Q A}
    (h : Step M blank x y) : Steps M blank 1 x y :=
  .next h (.refl y)

theorem Steps.trans {M : Recorder.Machine Q A} {blank : A}
    {n m : ℕ} {x y z : Configuration Q A}
    (hxy : Steps M blank n x y) (hyz : Steps M blank m y z) : Steps M blank (n + m) x z := by
  induction hxy with
  | refl => simpa only [Nat.zero_add] using hyz
  | next hxy hrest ih =>
    simpa only [Nat.add_assoc, Nat.add_comm 1 m] using Steps.next hxy (ih hyz)

theorem enter_step (M : Recorder.Machine Q A) (blank : A) (q : Q) (a : A)
    (hq : M.halting q = false) (l r h : Stack Q A) :
    Step M blank (triple (.checkpoint q) l (push (work a) r) h)
      (triple (.outward q) (push (marked a) l) r h) := by
  simpa only [Row.source, Row.target, Row.sourceControl, Row.targetControl,
    Row.sourcePrefixes, Row.targetPrefixes, prefixConfiguration_three,
    Radix.prependWord] using (Row.enter q a).step hq ![l, r, h]

theorem outward_step (M : Recorder.Machine Q A) (blank : A) (q : Q) (c : A)
    (l r h : Stack Q A) :
    Step M blank (triple (.outward q) l (push (work c) r) h)
      (triple (.outward q) (push (work c) l) r h) := by
  simpa only [Row.source, Row.target, Row.sourceControl, Row.targetControl,
    Row.sourcePrefixes, Row.targetPrefixes, prefixConfiguration_three,
    Radix.prependWord] using (Row.outward q c).step (M := M) (blank := blank) trivial ![l, r, h]

theorem supply_step (M : Recorder.Machine Q A) (blank : A) (q : Q)
    (l r h : Stack Q A) :
    Step M blank (triple (.outward q) l (push .boundary (push (work blank) r)) h)
      (triple (.inward q) l (push .boundary r) (push (work blank) h)) := by
  simpa only [Row.source, Row.target, Row.sourceControl, Row.targetControl,
    Row.sourcePrefixes, Row.targetPrefixes, prefixConfiguration_three,
    Radix.prependWord] using (Row.supply q).step (M := M) (blank := blank) trivial ![l, r, h]

theorem inward_step (M : Recorder.Machine Q A) (blank : A) (q : Q) (c : A)
    (l r h : Stack Q A) :
    Step M blank (triple (.inward q) (push (work c) l) r h)
      (triple (.inward q) l (push (work c) r) h) := by
  simpa only [Row.source, Row.target, Row.sourceControl, Row.targetControl,
    Row.sourcePrefixes, Row.targetPrefixes, prefixConfiguration_three,
    Radix.prependWord] using (Row.inward q c).step (M := M) (blank := blank) trivial ![l, r, h]

theorem restore_step (M : Recorder.Machine Q A) (blank : A) (q : Q) (a : A)
    (l r h : Stack Q A) :
    Step M blank (triple (.inward q) (push (marked a) l) r h)
      (triple (.dispatch q) l (push (work a) r) h) := by
  simpa only [Row.source, Row.target, Row.sourceControl, Row.targetControl,
    Row.sourcePrefixes, Row.targetPrefixes, prefixConfiguration_three,
    Radix.prependWord] using (Row.restore q a).step (M := M) (blank := blank) trivial ![l, r, h]

theorem word_append (a b : List (Symbol Q A)) (u : Stack Q A) :
    word (a ++ b) u = word a (word b u) := by
  induction a with
  | nil => rfl
  | cons c a ih => simp only [List.cons_append, Radix.prependWord, ih]

abbrev workWord (w : List A) : List (Symbol Q A) := w.map work

theorem outward_sweep (M : Recorder.Machine Q A) (blank : A) (q : Q)
    (w : List A) (l r h : Stack Q A) :
    Steps M blank w.length (triple (.outward q) l (word (workWord w) r) h)
      (triple (.outward q) (word (workWord w.reverse) l) r h) := by
  induction w generalizing l with
  | nil => exact .refl _
  | cons c w ih =>
    have hh := Steps.next (outward_step M blank q c l (word (workWord w) r) h)
      (ih (push (work c) l))
    simpa only [List.length_cons, workWord, List.map_cons, List.reverse_cons,
      List.map_append, List.map_singleton, List.map_nil, word_append, Radix.prependWord] using hh

theorem inward_sweep (M : Recorder.Machine Q A) (blank : A) (q : Q)
    (w : List A) (l r h : Stack Q A) :
    Steps M blank w.length (triple (.inward q) (word (workWord w) l) r h)
      (triple (.inward q) l (word (workWord w.reverse) r) h) := by
  induction w generalizing r with
  | nil => exact .refl _
  | cons c w ih =>
    have hh := Steps.next (inward_step M blank q c (word (workWord w) l) r h)
      (ih (push (work c) r))
    simpa only [List.length_cons, workWord, List.map_cons, List.reverse_cons,
      List.map_append, List.map_singleton, List.map_nil, word_append, Radix.prependWord] using hh

/-- On arbitrary independent tails, preparation removes one right-tail blank
and adds that same blank to history, leaving the finite work window unchanged. -/
theorem preparation (M : Recorder.Machine Q A) (blank : A) (q : Q) (a : A)
    (hq : M.halting q = false) (w : List A) (l r h : Stack Q A) :
    Steps M blank (2 * w.length + 3)
      (triple (.checkpoint q) l
        (push (work a) (word (workWord w) (push .boundary (push (work blank) r)))) h)
      (triple (.dispatch q) l
        (push (work a) (word (workWord w) (push .boundary r))) (push (work blank) h)) := by
  have h₁ := (enter_step M blank q a hq l
    (word (workWord w) (push .boundary (push (work blank) r))) h).steps
  have h₂ := outward_sweep M blank q w (push (marked a) l)
    (push .boundary (push (work blank) r)) h
  have h₃ := (supply_step M blank q (word (workWord w.reverse) (push (marked a) l)) r h).steps
  have h₄ := inward_sweep M blank q w.reverse (push (marked a) l)
    (push .boundary r) (push (work blank) h)
  have h₅ := (restore_step M blank q a l (word (workWord w) (push .boundary r))
    (push (work blank) h)).steps
  simp only [List.reverse_reverse, List.length_reverse] at h₄
  have hh := (((h₁.trans h₂).trans h₃).trans h₄).trans h₅
  convert hh using 1
  omega

theorem stay_step (M : Recorder.Machine Q A) (blank : A) (q : Q) (a : A)
    (hm : M.move q a = 0) (l r h : Stack Q A) :
    Step M blank (triple (.dispatch q) l (push (work a) r) (push (work blank) h))
      (triple (.checkpoint (M.next q a)) l (push (work (M.write q a)) r)
        (push (record (.stay q a)) h)) := by
  simpa only [Row.source, Row.target, Row.sourceControl, Row.targetControl,
    Row.sourcePrefixes, Row.targetPrefixes, prefixConfiguration_three,
    Radix.prependWord] using (Row.stay q a).step hm ![l, r, h]

theorem left_step (M : Recorder.Machine Q A) (blank : A) (q : Q) (a c : A)
    (hm : M.move q a = -1) (l r h : Stack Q A) :
    Step M blank
      (triple (.dispatch q) (push (work c) l) (push (work a) r) (push (work blank) h))
      (triple (.checkpoint (M.next q a)) l (push (work c) (push (work (M.write q a)) r))
        (push (record (.left q a c)) h)) := by
  simpa only [Row.source, Row.target, Row.sourceControl, Row.targetControl,
    Row.sourcePrefixes, Row.targetPrefixes, prefixConfiguration_three,
    Radix.prependWord] using (Row.left q a c).step hm ![l, r, h]

theorem right_step (M : Recorder.Machine Q A) (blank : A) (q : Q) (a c : A)
    (hm : M.move q a = 1) (l r h : Stack Q A) :
    Step M blank
      (triple (.dispatch q) l (push (work a) (push (work c) r)) (push (work blank) h))
      (triple (.checkpoint (M.next q a)) (push (work (M.write q a)) l) (push (work c) r)
        (push (record (.right q a c)) h)) := by
  simpa only [Row.source, Row.target, Row.sourceControl, Row.targetControl,
    Row.sourcePrefixes, Row.targetPrefixes, prefixConfiguration_three,
    Radix.prependWord] using (Row.right q a c).step hm ![l, r, h]

theorem boundary_step (M : Recorder.Machine Q A) (blank : A) (q : Q) (a : A)
    (hm : M.move q a = 1) (l r h : Stack Q A) :
    Step M blank (triple (.dispatch q) l
      (push (work a) (push .boundary (push (work blank) r))) (push (work blank) h))
      (triple (.checkpoint (M.next q a)) (push (work (M.write q a)) l)
        (push (work blank) (push .boundary r)) (push (record (.boundary q a)) h)) := by
  simpa only [Row.source, Row.target, Row.sourceControl, Row.targetControl,
    Row.sourcePrefixes, Row.targetPrefixes, prefixConfiguration_three,
    Radix.prependWord] using (Row.boundary q a).step hm ![l, r, h]

theorem stay_cycle (M : Recorder.Machine Q A) (blank : A) (q : Q) (a : A)
    (hq : M.halting q = false) (hm : M.move q a = 0) (w : List A) (l r h : Stack Q A) :
    Steps M blank (2 * (w.length + 1) + 2)
      (triple (.checkpoint q) l
        (push (work a) (word (workWord w) (push .boundary (push (work blank) r)))) h)
      (triple (.checkpoint (M.next q a)) l
        (push (work (M.write q a)) (word (workWord w) (push .boundary r)))
        (push (record (.stay q a)) h)) := by
  have hp := preparation M blank q a hq w l r h
  have hh := hp.trans (stay_step M blank q a hm l (word (workWord w) (push .boundary r)) h).steps
  convert hh using 1

theorem left_cycle (M : Recorder.Machine Q A) (blank : A) (q : Q) (a c : A)
    (hq : M.halting q = false) (hm : M.move q a = -1) (w : List A) (l r h : Stack Q A) :
    Steps M blank (2 * (w.length + 1) + 2)
      (triple (.checkpoint q) (push (work c) l)
        (push (work a) (word (workWord w) (push .boundary (push (work blank) r)))) h)
      (triple (.checkpoint (M.next q a)) l
        (push (work c) (push (work (M.write q a)) (word (workWord w) (push .boundary r))))
        (push (record (.left q a c)) h)) := by
  have hp := preparation M blank q a hq w (push (work c) l) r h
  have hh := hp.trans (left_step M blank q a c hm l (word (workWord w) (push .boundary r)) h).steps
  convert hh using 1

theorem right_cycle (M : Recorder.Machine Q A) (blank : A) (q : Q) (a c : A)
    (hq : M.halting q = false) (hm : M.move q a = 1) (w : List A) (l r h : Stack Q A) :
    Steps M blank (2 * (w.length + 2) + 2)
      (triple (.checkpoint q) l
        (push (work a) (push (work c)
          (word (workWord w) (push .boundary (push (work blank) r))))) h)
      (triple (.checkpoint (M.next q a)) (push (work (M.write q a)) l)
        (push (work c) (word (workWord w) (push .boundary r)))
        (push (record (.right q a c)) h)) := by
  have hp := preparation M blank q a hq (c :: w) l r h
  simp only [workWord, List.map_cons, Radix.prependWord] at hp
  have hh := hp.trans (right_step M blank q a c hm l
    (word (workWord w) (push .boundary r)) h).steps
  convert hh using 1

/-- A right move at the boundary creates a new blank work cell while retaining
one boundary and the same infinite blank filler. -/
theorem boundary_cycle (M : Recorder.Machine Q A) (blank : A) (q : Q) (a : A)
    (hq : M.halting q = false) (hm : M.move q a = 1) (l h : Stack Q A) :
    Steps M blank 4
      (triple (.checkpoint q) l
        (push (work a) (push .boundary (fun _ => work blank))) h)
      (triple (.checkpoint (M.next q a)) (push (work (M.write q a)) l)
        (push (work blank) (push .boundary (fun _ => work blank)))
        (push (record (.boundary q a)) h)) := by
  have hz : push (work blank) (fun _ : ℕ => work blank : Stack Q A) =
      (fun _ => work blank) := by
    funext n
    cases n <;> rfl
  have hp := preparation M blank q a hq [] l (fun _ => work blank) h
  simp only [workWord, List.map_nil, Radix.prependWord, List.length_nil,
    Nat.mul_zero, Nat.zero_add, hz] at hp
  have ht := boundary_step M blank q a hm l (fun _ => work blank) h
  rw [hz] at ht
  simpa only [Nat.reduceAdd] using hp.trans ht.steps

end ForcedComputation.Balanced

end OAI