File size: 7,489 Bytes
472a116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import Mathlib

namespace OAI

/-!
Finite table machines for the result.  X is the public input alphabet (all
uses for machines as space bounds have finite types); J indexes a fixed
number of other read-only tapes, on a binary alphabet.  Each read-only tape
is endmarked and has one confined head.  Work tapes for the simulator and
accessor are two-way, with unit moves. Terminal output in a finite set
may be specified in the transition table (equivalently transition to the
associated halt state). TM transitions with writes write before moving.

Space convention is visited work cells (including the origin of each work
tape), weighted by bits/symbol in the fixed alphabets, excluding read-only
tapes. Since visited sets are intervals, we use a window certificate including
all head positions during the run.
-/

namespace Fifths

inductive Dir
  | L | N | R
  deriving DecidableEq

instance : Fintype Dir := ⟨{.L, .N, .R}, by intro d; cases d <;> simp⟩

namespace Dir

@[simp] def delta : Dir → ℤ
  | L => -1
  | N => 0
  | R => 1

theorem bounds (d : Dir) : -1 ≤ d.delta ∧ d.delta ≤ 1 := by
  cases d <;> simp
end Dir

/-- Left endmarker at 0, then the word, then right endmarker. -/
inductive Mark (A : Type)
  | left
  | sym (a : A)
  | right
  deriving DecidableEq, Fintype

namespace Mark
variable {A : Type}
def see (w : List A) : ℕ → Mark A
  | 0 => left
  | p+1 =>
    match w[p]? with
    | some a => sym a
    | none => right

@[simp] theorem see_zero (w : List A) : see w 0 = left := rfl
@[simp] theorem see_left (w : List A) (p) : see w p = left ↔ p = 0 := by
  cases p with
  | zero => simp
  | succ p => cases e : w[p]? <;> simp [see, e]

@[simp] theorem see_right (w : List A) (p) : see w p = right ↔ w.length < p := by
  cases p with
  | zero => simp
  | succ p =>
    by_cases h : p < w.length
    · simp [see, h, Nat.not_lt.mpr (Nat.succ_le_of_lt h)]
    · simp only [see, getElem?_neg w p h, true_iff]
      omega

def walk (w : List A) (p : ℕ) : Dir → ℕ
  | .N => p
  | .L => p - 1
  | .R => if w.length < p then p else p + 1

theorem walk_bound (w : List A) (p : ℕ) (d : Dir) (h : p ≤ w.length + 1) :
    walk w p d ≤ w.length + 1 := by
  cases d
  · simp only [walk]
    omega
  · exact h
  · simp only [walk]
    split <;> omega

end Mark

/-- Deterministic halting runs. `f s = inl y` means the transition
specifies halt with that label (no more working moves). `P` holds
also at that final configuration. -/
inductive Runs {σ Y : Type} (f : σ → Y ⊕ σ) (P : σ → Prop) :
    σ → σ → Y → Prop
  | stop {s y} (ok : P s) (eqn : f s = Sum.inl y) : Runs f P s s y
  | step {s s' t y} (ok : P s) (eqn : f s = Sum.inr s')
      (tail : Runs f P s' t y) : Runs f P s t y

namespace Runs
variable {σ Y : Type} {f : σ → Y ⊕ σ} {P Q : σ → Prop} {s t : σ} {y : Y}
theorem mono (h : ∀ z, P z → Q z) (r : Runs f P s t y) : Runs f Q s t y := by
  induction r with
  | stop o e => exact .stop (h _ o) e
  | step o e r ih => exact .step (h _ o) e ih

theorem startOk (r : Runs f P s t y) : P s := by cases r <;> assumption
theorem endOk (r : Runs f P s t y) : P t := by induction r <;> assumption
theorem endStop (r : Runs f P s t y) : f t = Sum.inl y := by induction r <;> assumption

theorem unique (r : Runs f P s t y) {u v} (r' : Runs f Q s u v) : (t, y) = (u, v) := by
  induction r generalizing u v with
  | stop o e =>
    cases r' with
    | stop _ e' =>
      rw [e] at e'
      congr
      injection e'
    | step _ e' _ => rw [e] at e'; contradiction
  | step _ e _ ih =>
    cases r' with
    | stop _ e' => rw [e] at e'; contradiction
    | step _ e' tail =>
      rw [e] at e'
      obtain rfl : _ = _ := Sum.inr.inj e'
      exact ih tail
end Runs

/-- Common read-only inputs. -/
structure Inputs (X J : Type) where
  publicWord : List X
  arg : J → List Bool

structure Readers (J : Type) where
  px : ℕ := 0
  parg : J → ℕ := fun _ => 0

structure Reads (X J : Type) where
  sx : Mark X
  sarg : J → Mark Bool

structure Advances (J : Type) where
  dx : Dir := .N
  darg : J → Dir := fun _ => .N

namespace Readers
variable {X J : Type}
def home : Readers J := {}
def see (r : Readers J) (i : Inputs X J) : Reads X J :=
  ⟨Mark.see i.publicWord r.px, fun j => Mark.see (i.arg j) (r.parg j)⟩

def walk (r : Readers J) (i : Inputs X J) (a : Advances J) : Readers J :=
  ⟨Mark.walk i.publicWord r.px a.dx, fun j => Mark.walk (i.arg j) (r.parg j) (a.darg j)⟩

@[simp] theorem stationary (r : Readers J) (i : Inputs X J) : r.walk i {} = r := rfl
end Readers

structure Tape (A : Type) where
  pos : ℤ
  sym : ℤ → A

structure WriteMove (A : Type) where
  sym : A
  dir : Dir

namespace Tape
variable {A : Type}
def init (a : A) : Tape A := ⟨0, fun _ => a⟩
def see (w : Tape A) : A := w.sym w.pos
def perform (w : Tape A) (m : WriteMove A) : Tape A :=
  ⟨w.pos + m.dir.delta, Function.update w.sym w.pos m.sym⟩
end Tape

structure TAction (Q J K : Type) (W : K → Type) where
  next : Q
  advance : Advances J
  moves : ∀ k : K, WriteMove (W k)

/-- Finiteness of X, J and Y supplied in main theorem. Finite functions
in the table present no uniformity issue; all are fixed for the machine. -/
structure TM (X J Y : Type) where
  Q : Type
  [finiteQ : Finite Q]
  K : Type
  [tapes : Fintype K]
  W : K → Type
  [symbols : ∀ k, Fintype (W k)]
  blank : ∀ k, W k
  start : Q
  table : Q → Reads X J → ((k : K) → W k) → Y ⊕ TAction Q J K W

attribute [instance] TM.finiteQ TM.tapes TM.symbols

namespace TM
variable {X J Y : Type} (M : TM X J Y)

structure Cfg where
  state : M.Q
  ro : Readers J
  work : ∀ k, Tape (M.W k)

def initial : M.Cfg :=
  ⟨M.start, .home, fun k => .init (M.blank k)⟩

def step (i : Inputs X J) (s : M.Cfg) : Y ⊕ M.Cfg :=
  match M.table s.state (s.ro.see i) (fun k => (s.work k).see) with
  | Sum.inl y => .inl y
  | Sum.inr a =>
    .inr ⟨a.next, s.ro.walk i a.advance, fun k => (s.work k).perform (a.moves k)⟩

/-- Per tape visited positions contained in [-l,h], both nonnegative. -/
structure Window where
  l : M.K → ℕ
  h : M.K → ℕ

/-- Bits per symbol (at least one). -/
def width (k : M.K) : ℕ := max 1 (Fintype.card (M.W k) - 1).size

theorem width_pos (k : M.K) : 0 < M.width k :=
  Nat.succ_le_iff.mp (le_max_left ..)

namespace Window
variable {M}

def ok (w : M.Window) (s : M.Cfg) : Prop :=
  ∀ k, -(w.l k : ℤ) ≤ (s.work k).pos ∧ (s.work k).pos ≤ (w.h k : ℤ)

open BigOperators

def bits (w : M.Window) : ℕ :=
  ∑ k, (w.l k + w.h k + 1) * M.width k

theorem span_le (w : M.Window) (k : M.K) : w.l k + w.h k + 1 ≤ w.bits := calc
  _ ≤ (w.l k + w.h k + 1) * M.width k :=
    Nat.le_mul_of_pos_right _ (M.width_pos k)
  _ ≤ w.bits :=
    Finset.single_le_sum (f := fun k => (w.l k + w.h k + 1) * M.width k)
      (fun _ _ => Nat.zero_le _) (Finset.mem_univ k)
end Window

/-- `M` outputs `y` using at most `S` bits. -/
def Within (i : Inputs X J) (y : Y) (S : ℕ) : Prop :=
  ∃ w : M.Window, w.bits ≤ S ∧ ∃ t, Runs (M.step i) w.ok M.initial t y

theorem Within.mono {M : TM X J Y} {i y S R} (r : M.Within i y S) (h : S ≤ R) :
    M.Within i y R := by
  obtain ⟨w, hw, g⟩ := r
  exact ⟨w, hw.trans h, g⟩

theorem Within.unique {M : TM X J Y} {i y S z R} (r : M.Within i y S)
    (g : M.Within i z R) : y = z := by
  obtain ⟨_, -, _, r⟩ := r
  obtain ⟨_, -, _, g⟩ := g
  exact congrArg Prod.snd (r.unique g)
end TM

end Fifths

end OAI