File size: 7,875 Bytes
2f887db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import OAI.Computability.PerfectCompleteness.Reduction.SignedCompletionSchedule
import OAI.Computability.UniqueGames.Machines.MachineFiniteTable

namespace OAI


namespace PerfectCompleteness.DescriptorProjectionMachine

open Turing
open UniqueGamesTheorem.Foundations.Complexity
open SignedCompletionSchedule
open scoped Classical

noncomputable section

variable {branch : Nat → Nat} {n t q : Nat} {rows repeats : Nat → Nat}
  (hq : 0 < q)
  (large : CanonicalKeyEncoding.partitionWidth (TreeCanonical.locationCount branch n t) ≤ q)

def projection (descriptor : Descriptor (rows := rows) (repeats := repeats) hq large) :
    ProjectionTable q :=
  LocalCompletionSerialization.projection
    (FiniteConstraintMachine.result_correct hq large ⟨descriptor.1, descriptor.2.1⟩)
    descriptor.2.2

theorem projection_apply
    (descriptor : Descriptor (rows := rows) (repeats := repeats) hq large)
    (a : Fin (2 * q)) :
    (projection hq large descriptor).images[a] = descriptorTable hq large descriptor a :=
  (descriptorTable_eq_parsed hq large descriptor.1 descriptor.2 a).symm

def outputWords (descriptor : Descriptor (rows := rows) (repeats := repeats) hq large) :
    List Nat := Encoding.tableWords (projection hq large descriptor)

theorem outputWords_eq
    (descriptor : Descriptor (rows := rows) (repeats := repeats) hq large) :
    outputWords hq large descriptor =
      List.ofFn (fun a : Fin (2 * q) => (descriptorTable hq large descriptor a).val) :=
  (LocalCompletionSerialization.rawTableWords_eq
    (FiniteConstraintMachine.result_correct hq large ⟨descriptor.1, descriptor.2.1⟩)
    descriptor.2.2).symm

@[simp] theorem outputWords_length
    (descriptor : Descriptor (rows := rows) (repeats := repeats) hq large) :
    (outputWords hq large descriptor).length = 2 * q :=
  Encoding.tableWords_length _

def outputBits (descriptor : Descriptor (rows := rows) (repeats := repeats) hq large) :
    List Bool := encodeWords (outputWords hq large descriptor)

theorem outputBits_length_le
    (descriptor : Descriptor (rows := rows) (repeats := repeats) hq large) :
    (outputBits hq large descriptor).length ≤ (2 * q) * (q + 1) := by
  have hword : ∀ value ∈ outputWords hq large descriptor, value ≤ q := by
    intro value hvalue
    rw [outputWords_eq] at hvalue
    obtain ⟨a, ha⟩ := List.mem_ofFn.mp hvalue
    have hrange := (descriptorTable hq large descriptor a).isLt
    omega
  simpa only [outputBits, outputWords_length] using
    encodeWords_length_le (outputWords hq large descriptor) q hword

theorem outputBits_parse
    (descriptor : Descriptor (rows := rows) (repeats := repeats) hq large) :
    decodeWords (outputBits hq large descriptor) >>= Encoding.parseProjection q =
      some (projection hq large descriptor) := by
  rw [outputBits, decodeWords_encodeWords]
  exact Encoding.parseProjection_encoded _


variable {σ : Type} [Fintype σ] [DecidableEq σ]
  (select : σ → Descriptor (rows := rows) (repeats := repeats) hq large)

def keys (σ : Type) [Fintype σ] : List σ := (Finset.univ : Finset σ).toList

omit [DecidableEq σ] in
theorem keys_complete (state : σ) : state ∈ keys σ := by
  simp [keys]

def pushBound : Nat :=
  ((keys σ).map (fun state => (outputBits hq large (select state)).length)).sum

def statement {K Λ : Type} (dst : K) (exit : Λ) :
    TM2.Stmt (fun _ : K => Bool) Λ σ :=
  MachineFiniteTable.emit dst (fun state => outputBits hq large (select state)) (keys σ)
    (.goto (fun _ => exit))

theorem statement_trace {K Λ : Type} [DecidableEq K]
    (dst : K) (exit : Λ) (state : σ) (tapes : K → List Bool) :
    TM2.stepAux (statement hq large select dst exit) state tapes = {
      l := some exit
      var := state
      stk := Function.update tapes dst (outputBits hq large (select state) ++ tapes dst)
    } := by
  rw [statement, MachineFiniteTable.stepAux_emit dst _ (keys σ) (keys_complete)]
  rfl

def emitInTime {K Λ : Type} [DecidableEq K]
    (dst : K) (program : Λ → TM2.Stmt (fun _ : K => Bool) Λ σ)
    (label exit : Λ)
    (atLabel : program label = statement hq large select dst exit)
    (state : σ) (tapes : K → List Bool) :
    StateTransition.EvalsToInTime (TM2.step program)
      ⟨some label, state, tapes⟩
      (some ⟨some exit, state,
        Function.update tapes dst (outputBits hq large (select state) ++ tapes dst)⟩) 1 :=
  MachineFiniteTable.emitInTime (Γ := fun _ : K => Bool)
    dst (fun state => outputBits hq large (select state))
    (keys σ) keys_complete program label exit
    (by simpa only [statement] using atLabel) state tapes

omit [Fintype σ] [DecidableEq σ] in
theorem output_frame {K : Type} [DecidableEq K]
    (dst : K) (state : σ) (tapes : K → List Bool) (k : K) (hne : k ≠ dst) :
    (Function.update tapes dst (outputBits hq large (select state) ++ tapes dst)) k =
      tapes k :=
  MachineFiniteTable.output_frame dst (fun state => outputBits hq large (select state))
    state tapes k hne

theorem statement_pushBound {K Λ : Type} (dst : K) (exit : Λ) :
    Runtime.statementPushBound (statement hq large select dst exit) ≤
      pushBound hq large select := by
  have h := MachineFiniteTable.emit_push_bound (Γ := fun _ : K => Bool)
    dst (fun state => outputBits hq large (select state)) (keys σ)
    (.goto (fun _ => exit))
  simpa only [statement, pushBound, MachineFiniteTable.tableBound,
    Runtime.statementPushBound, Nat.add_zero] using h

def reverseStatement {K Λ : Type} (dst : K) (exit : Λ) :
    TM2.Stmt (fun _ : K => Bool) Λ σ :=
  MachineFiniteTable.emit dst (fun state => (outputBits hq large (select state)).reverse)
    (keys σ) (.goto (fun _ => exit))

theorem reverse_statement_trace {K Λ : Type} [DecidableEq K]
    (dst : K) (exit : Λ) (state : σ) (tapes : K → List Bool) :
    TM2.stepAux (reverseStatement hq large select dst exit) state tapes = {
      l := some exit
      var := state
      stk := Function.update tapes dst
        ((outputBits hq large (select state)).reverse ++ tapes dst)
    } := by
  rw [reverseStatement, MachineFiniteTable.stepAux_emit dst _ (keys σ) keys_complete]
  rfl

def reverseEmitInTime {K Λ : Type} [DecidableEq K]
    (dst : K) (program : Λ → TM2.Stmt (fun _ : K => Bool) Λ σ)
    (label exit : Λ)
    (atLabel : program label = reverseStatement hq large select dst exit)
    (state : σ) (tapes : K → List Bool) :
    StateTransition.EvalsToInTime (TM2.step program)
      ⟨some label, state, tapes⟩
      (some ⟨some exit, state,
        Function.update tapes dst
          ((outputBits hq large (select state)).reverse ++ tapes dst)⟩) 1 :=
  MachineFiniteTable.emitInTime (Γ := fun _ : K => Bool) dst
    (fun state => (outputBits hq large (select state)).reverse)
    (keys σ) keys_complete program label exit
    (by simpa only [reverseStatement] using atLabel) state tapes

omit [Fintype σ] [DecidableEq σ] in
theorem reverse_output_frame {K : Type} [DecidableEq K]
    (dst : K) (state : σ) (tapes : K → List Bool) (k : K) (hne : k ≠ dst) :
    (Function.update tapes dst
      ((outputBits hq large (select state)).reverse ++ tapes dst)) k = tapes k :=
  MachineFiniteTable.output_frame dst
    (fun state => (outputBits hq large (select state)).reverse) state tapes k hne

theorem reverse_statement_pushBound {K Λ : Type} (dst : K) (exit : Λ) :
    Runtime.statementPushBound (reverseStatement hq large select dst exit) ≤
      pushBound hq large select := by
  have h := MachineFiniteTable.emit_push_bound (Γ := fun _ : K => Bool)
    dst (fun state => (outputBits hq large (select state)).reverse) (keys σ)
    (.goto (fun _ => exit))
  simpa only [reverseStatement, pushBound, MachineFiniteTable.tableBound,
    List.length_reverse, Runtime.statementPushBound, Nat.add_zero] using h

end
end PerfectCompleteness.DescriptorProjectionMachine

end OAI