File size: 12,696 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import OAI.Computability.MeanPayoff.ArithmeticBounds

namespace OAI

section

namespace MeanPayoff.WorkStack.RegisterCompile

theorem signed_adds (a b : Bool) (xs ys out : List Bool) (bits : ℕ → Bool) :
    RegisterBits.advance (program SignedAddition.program) bits
      (lift SignedAddition.program out (SignedAddition.state .start (a::xs) (b::ys) []))
      (6*(2*max xs.length ys.length+5)) =
      lift SignedAddition.program out (SignedAddition.stopped
        ((SignedBits.add a b false xs ys).sign::(SignedBits.add a b false xs ys).digits) []) := by
  rw [advance_six, SignedAddition.program_adds]
end MeanPayoff.WorkStack.RegisterCompile
namespace MeanPayoff.BinaryData

def boolData : Bool → Data
  | false => .nil
  | true => .pair .nil .nil

def listData : List Bool → Data
  | [] => .nil
  | b :: bs => .pair (boolData b) (listData bs)

def wordData (w : SignedBits.Word) : Data := listData (w.sign :: w.digits)

def consCode : Bool → List Bool
  | false => [true, false]
  | true => [true, true, false, false]

@[simp] theorem encode_list_nil : encode (listData []) = [false] := rfl
@[simp] theorem encode_list_cons (b : Bool) (bs : List Bool) :
    encode (listData (b :: bs)) = consCode b ++ encode (listData bs) := by cases b <;> rfl

@[simp] theorem consCode_length_le (b : Bool) : (consCode b).length ≤ 4 := by cases b <;> decide

def feedCost : List Bool → ℕ
  | [] => 0
  | b :: bs => (consCode b).length + 1 + feedCost bs

@[simp] theorem feedCost_append (a b : List Bool) : feedCost (a ++ b) = feedCost a + feedCost b := by
  induction a with
  | nil => simp [feedCost]
  | cons x xs ih => simp only [List.cons_append, feedCost, ih]; omega

@[simp] theorem feedCost_reverse (a : List Bool) : feedCost a.reverse = feedCost a := by
  induction a with
  | nil => rfl
  | cons b bs ih => simp only [List.reverse_cons, feedCost_append, ih, feedCost]; omega

theorem feedCost_le (a : List Bool) : feedCost a ≤ 5*a.length := by
  induction a with
  | nil => rfl
  | cons b bs ih => have := consCode_length_le b; simp only [feedCost, List.length_cons]; omega

theorem encode_list_length_le (a : List Bool) : (encode (listData a)).length ≤ 4*a.length+1 := by
  induction a with
  | nil => simp
  | cons b bs ih =>
    simp only [encode_list_cons, List.length_append, List.length_cons]
    have := consCode_length_le b; omega

namespace Unbox
open RegisterBits
open BinaryStack (applyOp)
open Take (config_ext advance_front advance_add)

inductive Label where
  | read | bit | skip0 | skip1 | push (b : Bool) | restore | put (b : Bool)
  deriving DecidableEq, Fintype
noncomputable def equiv : Label ≃ Fin (Fintype.card Label) := Fintype.equivFin _
noncomputable def enc (q : Label) := equiv q
@[simp] theorem decode (q : Label) : equiv.symm (enc q) = q := equiv.symm_apply_apply q

noncomputable def program : Program 3 where
  states := Fintype.card Label
  start := enc .read
  read q := match equiv.symm q with
    | .read | .bit | .skip0 | .skip1 => 0
    | .push _ | .restore => 2
    | .put _ => 1
  transition q top _ := match equiv.symm q with
    | .read => top.map fun b => ⟨enc (if b then .bit else .restore), .pop, none⟩
    | .bit => top.map fun b => ⟨enc (if b then .skip0 else .push false), .pop, none⟩
    | .skip0 => some ⟨enc .skip1, .pop, none⟩
    | .skip1 => some ⟨enc (.push true), .pop, none⟩
    | .push b => some ⟨enc .read, .push b, none⟩
    | .restore => top.map fun b => ⟨enc (.put b), .pop, none⟩
    | .put b => some ⟨enc .restore, .push b, none⟩

noncomputable def cfg (q : Label) (xs ys zs out : List Bool) : Config program :=
  ⟨some (enc q), ![xs,ys,zs], out⟩
def stopped (xs ys out : List Bool) : Config program := ⟨none, ![xs,ys,[]], out⟩

@[simp] theorem read_nil (xs ys zs out : List Bool) (b : Bool) :
    RegisterBits.step program (cfg .read (false::xs) ys zs out) b = cfg .restore xs ys zs out := by
  apply config_ext <;> simp [RegisterBits.step, cfg, program, applyOp]
  funext i; fin_cases i <;> rfl

theorem feed_one (b : Bool) (xs ys zs out : List Bool) (bits : ℕ → Bool) :
    advance program bits (cfg .read (consCode b ++ xs) ys zs out)
      ((consCode b).length + 1) = cfg .read xs ys (b::zs) out := by
  cases b <;>
    apply config_ext <;>
    simp [advance, RegisterBits.step, cfg, program, consCode, applyOp]
  all_goals funext i; fin_cases i <;> rfl

theorem feeds (bs xs ys zs out : List Bool) (bits : ℕ → Bool) :
    advance program bits (cfg .read (encode (listData bs) ++ xs) ys zs out)
      (feedCost bs) = cfg .read (false::xs) ys (bs.reverse ++ zs) out := by
  induction bs generalizing zs bits with
  | nil => rfl
  | cons b bs ih =>
    simp only [encode_list_cons, List.append_assoc, feedCost]
    rw [advance_add, feed_one, ih]
    simp [List.reverse_cons, List.append_assoc]

@[simp] theorem restore_read (b : Bool) (xs ys zs out : List Bool) (coin : Bool) :
    RegisterBits.step program (cfg .restore xs ys (b::zs) out) coin = cfg (.put b) xs ys zs out := by
  apply config_ext <;> simp [RegisterBits.step, cfg, program, applyOp]
  funext i; fin_cases i <;> rfl
@[simp] theorem restore_put (b : Bool) (xs ys zs out : List Bool) (coin : Bool) :
    RegisterBits.step program (cfg (.put b) xs ys zs out) coin = cfg .restore xs (b::ys) zs out := by
  apply config_ext <;> simp [RegisterBits.step, cfg, program, applyOp]
  funext i; fin_cases i <;> rfl
@[simp] theorem restore_done (xs ys out : List Bool) (coin : Bool) :
    RegisterBits.step program (cfg .restore xs ys [] out) coin = stopped xs ys out := by
  simp [RegisterBits.step, cfg, program, stopped]

theorem restores (zs xs ys out : List Bool) (bits : ℕ → Bool) :
    advance program bits (cfg .restore xs ys zs out) (2*zs.length+1) =
      stopped xs (zs.reverse ++ ys) out := by
  induction zs generalizing ys bits with
  | nil => simp [advance]
  | cons b zs ih =>
    rw [show 2*(b::zs).length+1 = (2*zs.length+1+1)+1 by simp; omega,
      Take.advance_front, restore_read, Take.advance_front, restore_put, ih]
    simp [List.reverse_cons, List.append_assoc]

theorem read_nil_advance (xs ys zs out : List Bool) (bits : ℕ → Bool) :
    advance program bits (cfg .read (false::xs) ys zs out) 1 = cfg .restore xs ys zs out := by
  simp only [advance, read_nil]

theorem unboxes (bs xs ys out : List Bool) (bits : ℕ → Bool) :
    advance program bits (cfg .read (encode (listData bs) ++ xs) ys [] out)
      (feedCost bs+1+(2*bs.length+1)) = stopped xs (bs++ys) out := by
  rw [advance_add _ _ _ (feedCost bs+1) (2*bs.length+1),
    advance_add _ _ _ (feedCost bs) 1, feeds]
  simp only [List.append_nil, read_nil_advance]
  rw [show bs.length = bs.reverse.length by simp, restores]
  simp

theorem unboxes_bound (bs : List Bool) : feedCost bs+1+(2*bs.length+1) ≤ 7*bs.length+2 := by
  have := feedCost_le bs; omega

end Unbox
end MeanPayoff.BinaryData
namespace MeanPayoff.BinaryData.Box
open RegisterBits
open BinaryStack (applyOp)
open Take (config_ext advance_add)

inductive Label where
  | reverse | revPut (b : Bool) | init | feed | zero | one0 | one1 | one2 | last
  deriving DecidableEq, Fintype
noncomputable def equiv : Label ≃ Fin (Fintype.card Label) := Fintype.equivFin _
noncomputable def enc (q : Label) := equiv q
@[simp] theorem decode (q : Label) : equiv.symm (enc q) = q := equiv.symm_apply_apply q

noncomputable def program : Program 3 where
  states := Fintype.card Label
  start := enc .reverse
  read q := match equiv.symm q with
    | .reverse => 0
    | .revPut _ | .feed => 2
    | _ => 1
  transition q top _ := match equiv.symm q with
    | .reverse => some (match top with
        | none => ⟨enc .init, .keep, none⟩
        | some b => ⟨enc (.revPut b), .pop, none⟩)
    | .revPut b => some ⟨enc .reverse, .push b, none⟩
    | .init => some ⟨enc .feed, .push false, none⟩
    | .feed => top.map fun b => ⟨enc (if b then .one0 else .zero), .pop, none⟩
    | .zero => some ⟨enc .last, .push false, none⟩
    | .one0 => some ⟨enc .one1, .push false, none⟩
    | .one1 => some ⟨enc .one2, .push false, none⟩
    | .one2 => some ⟨enc .last, .push true, none⟩
    | .last => some ⟨enc .feed, .push true, none⟩

noncomputable def cfg (q : Label) (xs ys zs out : List Bool) : Config program :=
  ⟨some (enc q), ![xs,ys,zs], out⟩
def stopped (ys out : List Bool) : Config program := ⟨none, ![[],ys,[]], out⟩

@[simp] theorem reverse_read (b : Bool) (xs ys zs out : List Bool) (coin : Bool) :
    RegisterBits.step program (cfg .reverse (b::xs) ys zs out) coin = cfg (.revPut b) xs ys zs out := by
  apply config_ext <;> simp [RegisterBits.step, cfg, program, applyOp]
  funext i; fin_cases i <;> rfl
@[simp] theorem reverse_put (b : Bool) (xs ys zs out : List Bool) (coin : Bool) :
    RegisterBits.step program (cfg (.revPut b) xs ys zs out) coin = cfg .reverse xs ys (b::zs) out := by
  apply config_ext <;> simp [RegisterBits.step, cfg, program, applyOp]
  funext i; fin_cases i <;> rfl

theorem reverses (bs ys zs out : List Bool) (bits : ℕ → Bool) :
    advance program bits (cfg .reverse bs ys zs out) (2*bs.length) =
      cfg .reverse [] ys (bs.reverse++zs) out := by
  induction bs generalizing zs bits with
  | nil => rfl
  | cons b bs ih =>
    rw [show 2*(b::bs).length = (2*bs.length+1)+1 by simp; omega,
      Take.advance_front, reverse_read, Take.advance_front, reverse_put, ih]
    simp [List.reverse_cons, List.append_assoc]

theorem initializes (ys zs out : List Bool) (bits : ℕ → Bool) :
    advance program bits (cfg .reverse [] ys zs out) 2 = cfg .feed [] (false::ys) zs out := by
  apply config_ext <;> simp [advance, RegisterBits.step, cfg, program, applyOp]
  funext i; fin_cases i <;> rfl

theorem feed_one (b : Bool) (xs ys zs out : List Bool) (bits : ℕ → Bool) :
    advance program bits (cfg .feed xs ys (b::zs) out) ((consCode b).length+1) =
      cfg .feed xs (consCode b++ys) zs out := by
  cases b <;>
    apply config_ext <;>
    simp [advance, RegisterBits.step, cfg, program, consCode, applyOp]
  all_goals funext i; fin_cases i <;> rfl

theorem feeds (bs acc xs ys out : List Bool) (bits : ℕ → Bool) :
    advance program bits (cfg .feed xs (encode (listData acc)++ys) bs out) (feedCost bs) =
      cfg .feed xs (encode (listData (bs.reverse++acc))++ys) [] out := by
  induction bs generalizing acc bits with
  | nil => rfl
  | cons b bs ih =>
    rw [feedCost, advance_add, feed_one]
    rw [← List.append_assoc, ← encode_list_cons, ih]
    simp [List.reverse_cons, List.append_assoc]

@[simp] theorem feed_done (ys out : List Bool) (coin : Bool) :
    RegisterBits.step program (cfg .feed [] ys [] out) coin = stopped ys out := by
  simp [RegisterBits.step, cfg, program, stopped]

theorem boxes (bs ys out : List Bool) (bits : ℕ → Bool) :
    advance program bits (cfg .reverse bs ys [] out)
      (2*bs.length+2+feedCost bs+1) = stopped (encode (listData bs)++ys) out := by
  rw [advance_add _ _ _ (2*bs.length+2+feedCost bs) 1,
    advance_add _ _ _ (2*bs.length+2) (feedCost bs),
    advance_add _ _ _ (2*bs.length) 2, reverses]
  simp only [List.append_nil]
  rw [initializes]
  change advance _ _ (advance _ _ (cfg .feed [] (encode (listData [])++ys) bs.reverse out) _) 1 = _
  rw [← feedCost_reverse bs, feeds]
  simp [advance]

theorem boxes_bound (bs : List Bool) : 2*bs.length+2+feedCost bs+1 ≤ 7*bs.length+3 := by
  have := feedCost_le bs; omega

end MeanPayoff.BinaryData.Box
namespace MeanPayoff.RegisterBits.Flow
open BinaryData.Take (advance_add)

theorem hop {K : ℕ} (P : Program K) (p q : Fin P.nodes) (s t : Fin K → List Bool)
    (out : List Bool) (bits : ℕ → Bool) (N : ℕ)
    (hm : advance (P.block p) bits ⟨some (P.block p).start, s, out⟩ N = ⟨none,t,out⟩)
    (hn : ∀ top bit, P.next p top bit = some ⟨q, .keep, none⟩) :
    ∃ d ≤ N+1, advance (compile P) bits (state P p s out) d = state P q t out := by
  obtain ⟨d, hd, he⟩ := macro_runs P p s out bits N (by rw [hm])
  rw [hm] at he
  refine ⟨d+1, by omega, ?_⟩
  simp only [advance, he, step_boundary, hn]
  simp [BinaryStack.applyOp]

theorem finish {K : ℕ} (P : Program K) (p : Fin P.nodes) (s t : Fin K → List Bool)
    (out : List Bool) (bits : ℕ → Bool) (N : ℕ)
    (hm : advance (P.block p) bits ⟨some (P.block p).start, s, out⟩ N = ⟨none,t,out⟩)
    (hn : ∀ top bit, P.next p top bit = none) :
    ∃ d ≤ N+1, advance (compile P) bits (state P p s out) d = ⟨none,t,out⟩ := by
  obtain ⟨d, hd, he⟩ := macro_runs P p s out bits N (by rw [hm])
  rw [hm] at he
  refine ⟨d+1, by omega, ?_⟩
  simp only [advance, he, step_boundary, hn]
end MeanPayoff.RegisterBits.Flow
end

end OAI