File size: 6,394 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
import OAI.Computability.SpaceSimulation.StackMachine

namespace OAI

/-! Sliding representation of a tape/configuration for emulating a
multi-work-tape program with stacks. -/
namespace Fifths

structure WorkSplit (A : Type) where
  center : A
  lo : List A
  hi : List A

namespace WorkSplit
variable {A : Type}

def init (b : A) : WorkSplit A := ⟨b, [], []⟩
/-- Full-tape agreement (padding by blank), plus inclusion of both
lists in the allowed tape window. -/
structure Good (z : WorkSplit A) (t : Tape A) (b : A) (l r : ℕ) : Prop where
  see : t.see = z.center
  down : ∀ n : ℕ, t.sym (t.pos - (n+1:ℕ)) = (z.lo[n]?).getD b
  up : ∀ n : ℕ, t.sym (t.pos + (n+1:ℕ)) = (z.hi[n]?).getD b
  lb : -(l:ℤ) ≤ t.pos - z.lo.length
  hb : t.pos + z.hi.length ≤ (r:ℤ)

theorem init_good (b : A) (l r) : (init b).Good (Tape.init b) b l r := by
  constructor
  · rfl
  · simp [Tape.init,init]
  · simp [Tape.init,init]
  · simp [Tape.init,init]
  · simp [Tape.init,init]

theorem good_lengths {z : WorkSplit A} {t b l r} (g : z.Good t b l r) :
    z.lo.length ≤ l+r ∧ z.hi.length ≤ l+r := by
  have a := g.lb
  have c := g.hb
  omega

def slide (z : WorkSplit A) (m : WriteMove A) (b : A) : WorkSplit A :=
  match m.dir with
  | .N => ⟨m.sym,z.lo,z.hi⟩
  | .R => ⟨z.hi.head?.getD b, m.sym :: z.lo, z.hi.tail⟩
  | .L => ⟨z.lo.head?.getD b, z.lo.tail, m.sym :: z.hi⟩

theorem good_slide {z : WorkSplit A} {t b l r} (m : WriteMove A)
    (g : z.Good t b l r)
    (hl : -(l:ℤ) ≤ (t.perform m).pos)
    (hr : (t.perform m).pos ≤ (r:ℤ)) :
    (z.slide m b).Good (t.perform m) b l r := by
  obtain ⟨s,d⟩ := m
  rcases g with ⟨e,a,c,u,v⟩
  cases d
  case N =>
    constructor
    · simp [slide,Tape.perform,Tape.see]
    · intro n
      simp only [Tape.perform,Dir.delta, add_zero, slide]
      rw [Function.update_of_ne (by omega)]
      exact a n
    · intro n
      simp only [Tape.perform,Dir.delta, add_zero, slide]
      rw [Function.update_of_ne (by omega)]
      exact c n
    · simpa [Tape.perform,slide] using u
    · simpa [Tape.perform,slide] using v
  case R =>
    constructor
    · simp only [slide,Tape.perform,Tape.see,Dir.delta]
      rw [Function.update_of_ne (by omega)]
      specialize c 0
      cases h : z.hi <;> simpa [h] using c
    · intro n; cases n with
      | zero => simp [Tape.perform,slide]
      | succ n =>
        simp only [Tape.perform,slide,Dir.delta,List.getElem?_cons_succ]
        rw [show t.pos + 1 - (((n+1+1:ℕ)):ℤ) = t.pos - (((n+1:ℕ)):ℤ) by omega,
          Function.update_of_ne (by omega)]
        exact a n
    · intro n
      simp only [Tape.perform,slide,Dir.delta]
      rw [show t.pos + 1 + (((n+1:ℕ)):ℤ) = t.pos + (((n+1+1:ℕ)):ℤ) by omega,
        Function.update_of_ne (by omega)]
      specialize c (n+1)
      cases h : z.hi <;> simpa [h] using c
    · simp only [Tape.perform, slide, Dir.delta, List.length_cons]
      omega
    · cases h : z.hi with
      | nil => simpa [Tape.perform,slide,h] using hr
      | cons a m =>
        simp only [Tape.perform,slide,Dir.delta,h,List.tail_cons]
        rw [h, List.length_cons] at v
        omega
  case L =>
    constructor
    · simp only [slide,Tape.perform,Tape.see,Dir.delta]
      rw [Function.update_of_ne (by omega)]
      specialize a 0
      cases h : z.lo <;> simpa [h,sub_eq_add_neg] using a
    · intro n
      simp only [Tape.perform,slide,Dir.delta]
      rw [show t.pos + -1 - (((n+1:ℕ)):ℤ) = t.pos - (((n+1+1:ℕ)):ℤ) by omega,
        Function.update_of_ne (by omega)]
      specialize a (n+1)
      cases h : z.lo <;> simpa [h] using a
    · intro n; cases n with
      | zero => simp [Tape.perform,slide]
      | succ n =>
        simp only [Tape.perform,slide,Dir.delta,List.getElem?_cons_succ]
        rw [show t.pos + -1 + (((n+1+1:ℕ)):ℤ) = t.pos + (((n+1:ℕ)):ℤ) by omega,
          Function.update_of_ne (by omega)]
        exact c n
    · cases h : z.lo with
      | nil => simpa [Tape.perform,slide,h] using hl
      | cons a m =>
        simp only [Tape.perform,slide,Dir.delta,h,List.tail_cons]
        rw [h, List.length_cons] at u
        omega
    · simp only [Tape.perform,slide,Dir.delta,List.length_cons]
      omega
end WorkSplit

/-- Unlike the work splits, never push either marker. -/
structure ArgSplit where
  center : Mark Bool
  lo : List Bool
  hi : List Bool

namespace ArgSplit
def init (w : List Bool) : ArgSplit := ⟨.left, [], w⟩

inductive Good : ArgSplit → List Bool → ℕ → Prop
  | left (h) : Good ⟨.left, [], h⟩ h 0
  | right (l : List Bool) : Good ⟨.right, l, []⟩ l.reverse (l.length+1)
  | sym (b l h) : Good ⟨.sym b, l, h⟩ (l.reverse ++ b::h) (l.length+1)

theorem good_see {z : ArgSplit} {w p} (g : z.Good w p) :
    Mark.see w p = z.center := by
  cases g with
  | left => rfl
  | right l =>
    simp [Mark.see]
  | sym b l h =>
    simp [Mark.see]

theorem good_lengths {z : ArgSplit} {w p} (g : z.Good w p) :
    z.lo.length ≤ w.length ∧ z.hi.length ≤ w.length := by
  cases g with
  | left => simp
  | right l => simp
  | sym b l h => simp; omega

def slide (z : ArgSplit) (d : Dir) : ArgSplit :=
  match d, z.center with
  | .N, _ | .R, .right | .L, .left => z
  | .R, c =>
    ⟨(z.hi.head?.map Mark.sym).getD .right,
     match c with | .sym b => b :: z.lo | _ => z.lo, z.hi.tail⟩
  | .L, c =>
    ⟨(z.lo.head?.map Mark.sym).getD .left,
     z.lo.tail, match c with | .sym b => b :: z.hi | _ => z.hi⟩

theorem good_slide {z : ArgSplit} {w p} (g : z.Good w p) (d) :
    (z.slide d).Good w (Mark.walk w p d) := by
  cases d with
  | N => exact g
  | R =>
    cases g with
    | left =>
      cases w with
      | nil => exact Good.right []
      | cons b h => exact Good.sym b [] h
    | right l => simpa [Mark.walk,slide] using Good.right l
    | sym b l h =>
      cases h with
      | nil =>
        simpa [Mark.walk,slide] using Good.right (b::l)
      | cons a h =>
        simpa [Mark.walk,slide] using Good.sym a (b::l) h
  | L =>
    cases g with
    | left => exact Good.left w
    | right l =>
      cases l with
      | nil => exact Good.left []
      | cons b l => simpa [Mark.walk,slide] using Good.sym b l []
    | sym b l h =>
      cases l with
      | nil => exact Good.left (b::h)
      | cons a l => simpa [Mark.walk,slide] using Good.sym a l (b::h)
end ArgSplit
end Fifths

end OAI