File size: 10,922 Bytes
ea76aac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import Mathlib

namespace OAI

/-!
Finite weak transition components and finite-scan instruction semantics.
-/

namespace FiniteAlgebraicEnvelopes.Compiler
open Relation

variable {α : Type*} {r : α → α → Prop}

/-- An internal terminal configuration has no forward transition. -/
def Terminal (r : α → α → Prop) (a : α) : Prop := ∀ b, ¬ r a b

/-- Determinism makes forward-reachability joins an equivalence relation. -/
theorem deterministic_join (hr : Relator.RightUnique r) :
    Equivalence (Join (ReflTransGen r)) := by
  apply equivalence_join_reflTransGen
  intro a b c hab hac
  have hbc := hr hab hac
  subst c
  exact ⟨b, .refl, .refl⟩

/-- Undirected internal connectivity may be converted to a common forward
    descendant; no reversibility of the simulated machine is required. -/
theorem weak_component_joins (hr : Relator.RightUnique r) {a b : α}
    (h : EqvGen r a b) : Join (ReflTransGen r) a b := by
  let : IsEquiv α (Join (ReflTransGen r)) := (deterministic_join hr).isEquiv
  exact EqvGen.eqvGen_le (fun _ y hxy => ⟨y, .single hxy, .refl⟩) _ _ h

theorem terminal_eq_of_reaches {a b : α} (ha : Terminal r a)
    (hab : ReflTransGen r a b) : a=b := by
  rcases hab.cases_head with h | ⟨c, hac, _⟩
  · exact h
  · exact False.elim (ha c hac)

/-- Every configuration in a terminal's weak component reaches that terminal.
    This is the graph-theoretic core of source Lemma compiler-components,
    and holds even without the finite-component assumption. -/
theorem reaches_terminal (hr : Relator.RightUnique r) {a b : α}
    (hb : Terminal r b) (hab : EqvGen r a b) : ReflTransGen r a b := by
  obtain ⟨c, hac, hbc⟩ := weak_component_joins hr hab
  have h := terminal_eq_of_reaches hb hbc
  exact h ▸ hac

/-- In particular there cannot be two successful terminal sectors in the same
    weak component, even with arbitrary reverse branching on dirty data. -/
theorem terminal_unique (hr : Relator.RightUnique r) {a b : α}
    (ha : Terminal r a) (hb : Terminal r b) (hab : EqvGen r a b) : a=b :=
  terminal_eq_of_reaches ha (reaches_terminal hr hb hab)

/-- Fixed ordered bases, a head gap, and finite tags are the finite search data
    of the finite compiler. Workspace quota insertion is NOT an internal step. -/
structure Configuration (Head Tag : Type*) (k : ℕ) where
  head : Head
  gap : Fin (k+1)
  tags : Fin k → Tag
  deriving DecidableEq, Fintype

@[simp] theorem configuration_card (Head Tag : Type*) [Fintype Head] [Fintype Tag]
    (k : ℕ) : Fintype.card (Configuration Head Tag k) =
      (k+1) * Fintype.card Head * Fintype.card Tag ^ k := by
  let e : Configuration Head Tag k ≃ Head × Fin (k+1) × (Fin k → Tag) :=
    { toFun := fun c => (c.head, c.gap, c.tags)
      invFun := fun c => ⟨c.1, c.2.1, c.2.2⟩
      left_inv := fun _ => rfl
      right_inv := fun _ => rfl }
  rw [Fintype.card_congr e]
  simp [Nat.mul_comm, Nat.mul_left_comm]

section FiniteSearch
variable [Fintype α] [DecidableEq α] (r : α → α → Prop) [DecidableRel r]

/-- One effective expansion by undirected internal edges. -/
def expand (s : Finset α) : Finset α :=
  s ∪ Finset.univ.filter (fun b => ∃ a ∈ s, r a b ∨ r b a)

@[simp] theorem mem_expand (s : Finset α) (b : α) :
    b ∈ expand r s ↔ b ∈ s ∨ ∃ a ∈ s, r a b ∨ r b a := by
  simp [expand]

theorem subset_expand (s : Finset α) : s ⊆ expand r s :=
  fun _ h => (mem_expand r _ _).mpr (Or.inl h)

/-- This finite algorithm never calls the simulated partial function. -/
def search (a : α) : ℕ → Finset α
  | 0 => {a}
  | n+1 => expand r (search a n)

theorem search_mono (a : α) : Monotone (search r a) := by
  apply monotone_nat_of_le_succ
  intro n
  exact subset_expand r _

theorem search_stabilizes (a : α) :
    search r a (Fintype.card α + 1) = search r a (Fintype.card α) := by
  have hs : ∀ n, (search r a n).card ≤ Fintype.card α :=
    fun _ => Finset.card_le_univ _
  have hm : Monotone (fun n => (search r a n).card) :=
    fun _ _ h => Finset.card_le_card (search_mono r a h)
  have hstep : ∀ n, (search r a n).card = (search r a (n+1)).card →
      (search r a (n+1)).card = (search r a (n+2)).card := by
    intro n hn
    have he : search r a n = search r a (n+1) :=
      Finset.eq_of_subset_of_card_le (search_mono r a (Nat.le_succ n)) hn.ge
    exact congrArg Finset.card (congrArg (expand r) he)
  exact (Finset.eq_of_subset_of_card_le
    (search_mono r a (Nat.le_succ (Fintype.card α)))
    (Nat.stabilises_of_monotone hm hs hstep (Nat.le_succ _)).le).symm

/-- Entire weak component, computed by at most the known number of vertices
    many finite expansions. Unlike running the machine, this always terminates. -/
def component (a : α) : Finset α := search r a (Fintype.card α)

theorem self_mem_component (a : α) : a ∈ component r a :=
  search_mono r a (Nat.zero_le _) (by simp [search])

theorem component_saturated (a b c : α) (hb : b ∈ component r a)
    (hbc : r b c ∨ r c b) : c ∈ component r a := by
  have h := (mem_expand r (component r a) c).mpr (Or.inr ⟨b, hb, hbc⟩)
  change c ∈ search r a (Fintype.card α + 1) at h
  rw [search_stabilizes] at h
  exact h

theorem mem_search_connected (a b : α) (n : ℕ) (h : b ∈ search r a n) :
    EqvGen r a b := by
  induction n generalizing b with
  | zero =>
    have : b=a := by simpa [search] using h
    subst b
    exact .refl a
  | succ n ih =>
    rcases (mem_expand r (search r a n) b).mp h with h | ⟨c, hc, hcb⟩
    · exact ih b h
    · refine EqvGen.trans _ _ _ (ih c hc) ?_
      rcases hcb with h | h
      · exact .rel _ _ h
      · exact .symm _ _ (.rel _ _ h)

/-- Exactness of the finite component search on malformed as well as genuine
    configurations. -/
theorem mem_component_iff (a b : α) : b ∈ component r a ↔ EqvGen r a b := by
  constructor
  · exact mem_search_connected r a b _
  · intro h
    have h' : ReflTransGen (SymmGen r) a b := by
      rw [EqvGen.reflTransGen_symmGen]
      exact h
    clear h
    induction h' with
    | refl => exact self_mem_component r a
    | @tail b c _ hbc ih => exact component_saturated r a b c ih hbc

/-- Consequently internal weak connectivity has a decision procedure that uses
    only the table and finite configuration data, not a halting oracle. -/
def decideConnected (a b : α) : Bool := decide (b ∈ component r a)

@[simp] theorem decideConnected_eq_true (a b : α) :
    decideConnected r a b = true ↔ EqvGen r a b := by
  simp [decideConnected, mem_component_iff]
end FiniteSearch

/- The actual finite-scan instruction semantics of the finite compiler.
   The immutable bases are not part of what an instruction may update. -/
inductive Base (S : Type*)
  | anchor : S → Base S
  | cutC : Base S
  | cutD : Base S
  | workspace : Base S
  deriving DecidableEq, Fintype

structure Tag (S Γ : Type*) where
  anchorRead : Bool
  workspaceFilled : Bool
  work : Γ
  outputRead : Bool
  output : Option S
  deriving DecidableEq, Fintype

/-- The same clean tag is used on every base, including its unused fields. -/
def clean {S Γ : Type*} (blank : Γ) : Tag S Γ :=
  ⟨false, false, blank, false, none⟩

inductive Direction
  | left | right
  deriving DecidableEq

instance : Fintype Direction := ⟨{.left, .right}, by intro x; cases x <;> simp⟩

structure Instruction (Head Tag : Type*) where
  head : Head
  tag : Tag
  cross : Bool
  deriving DecidableEq, Fintype

/-- Each state designates one reading side. A cell test has at most one result;
    boundary tests only change the state. A failed (or absent-boundary) test
    has no result. This is exactly the source's disjoint-case convention. -/
structure Table (Head Base Tag : Type*) where
  direction : Head → Direction
  cell : Head → Base → Tag → Option (Instruction Head Tag)
  boundary : Head → Option Head

/-- The finite state transition on sectors with fixed ordered base sequence.
    The two flags record whether alpha and omega are present. There is no
    insertion or deletion among internal steps. -/
def step {Head Base Tag : Type*} (table : Table Head Base Tag)
    {k : ℕ} (bases : Fin k → Base) (leftBoundary rightBoundary : Bool)
    (c : Configuration Head Tag k) : Option (Configuration Head Tag k) :=
  match table.direction c.head with
  | .right =>
    if hg : c.gap.val < k then
      let i : Fin k := ⟨c.gap.val, hg⟩
      (table.cell c.head (bases i) (c.tags i)).map fun a =>
        { head := a.head
          gap := if a.cross then ⟨c.gap.val+1, by omega⟩ else c.gap
          tags := Function.update c.tags i a.tag }
    else if rightBoundary then
      (table.boundary c.head).map fun q => { c with head := q }
    else none
  | .left =>
    if hg : 0 < c.gap.val then
      let i : Fin k := ⟨c.gap.val-1, by have := c.gap.isLt; omega⟩
      (table.cell c.head (bases i) (c.tags i)).map fun a =>
        { head := a.head
          gap := if a.cross then ⟨c.gap.val-1, by have := c.gap.isLt; omega⟩ else c.gap
          tags := Function.update c.tags i a.tag }
    else if leftBoundary then
      (table.boundary c.head).map fun q => { c with head := q }
    else none

/-- The relation used for component search; the equation is used both ways in
    the presentation, but only this direction in the deterministic analysis. -/
def InternalStep {Head Base Tag : Type*} (table : Table Head Base Tag)
    {k : ℕ} (bases : Fin k → Base) (leftBoundary rightBoundary : Bool)
    (c d : Configuration Head Tag k) : Prop :=
  step table bases leftBoundary rightBoundary c = some d

instance {Head Base Tag : Type*} [DecidableEq Head] [DecidableEq Tag]
    (table : Table Head Base Tag) {k : ℕ} (bases : Fin k → Base)
    (leftBoundary rightBoundary : Bool) :
    DecidableRel (InternalStep table bases leftBoundary rightBoundary) :=
  fun _ _ => inferInstanceAs (Decidable (_ = _))

/-- Determinism is proved on ALL data, not assumed for clean initial sectors. -/
theorem internalStep_deterministic {Head Base Tag : Type*} (table : Table Head Base Tag)
    {k : ℕ} (bases : Fin k → Base) (leftBoundary rightBoundary : Bool) :
    Relator.RightUnique (InternalStep table bases leftBoundary rightBoundary) := by
  intro a b c hab hac
  exact Option.some.inj (hab.symm.trans hac)

/-- The success-terminal assertion applies directly to the finite scan semantics. -/
theorem internal_terminal_reached {Head Base Tag : Type*} (table : Table Head Base Tag)
    {k : ℕ} (bases : Fin k → Base) (l r : Bool) {a b : Configuration Head Tag k}
    (hb : step table bases l r b = none)
    (hab : EqvGen (InternalStep table bases l r) a b) :
    ReflTransGen (InternalStep table bases l r) a b := by
  apply reaches_terminal (internalStep_deterministic table bases l r) _ hab
  intro c hc
  change step table bases l r b = some c at hc
  rw [hb] at hc
  cases hc

end FiniteAlgebraicEnvelopes.Compiler

end OAI