File size: 20,359 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import Mathlib
import OAI.Computability.DeterministicSum.Machine

namespace OAI

/-! Structured assembly, instruction-exact semantics, initialized-memory frames and loops. -/

namespace DeterministicThreeSum

lemma run_add (w : ℕ) (p : Program) (t u : ℕ) (s : State) :
    run w p (t+u) s = (run w p t s).bind (run w p u) := by
  induction t generalizing s with
  | zero => simp [run]
  | succ t ih =>
    rw [Nat.succ_add,run,run]
    cases step w p s <;> simp only [Option.bind_none,Option.bind_some,ih]

namespace Structured

structure Data where
  registers : ℕ → ℕ
  memory : ℕ → Option ℕ

def state (s : Data) (pc : ℕ) : State := ⟨pc,s.registers,s.memory,none⟩

def operand (w : ℕ) (s : Data) (x : Operand) := readOperand w (state s 0) x

def put (s : Data) (dst value : ℕ) : Data :=
  {s with registers:=Function.update s.registers dst value}

inductive Atom where
  | assign (dst : ℕ) (src : Operand)
  | binary (dst : ℕ) (op : BinOp) (left right : Operand)
  | load (dst : ℕ) (address : Operand)
  | store (address value : Operand)

def Atom.instruction : Atom → Instruction
  | .assign d a => .assign d a
  | .binary d op a b => .binary d op a b
  | .load d a => .load d a
  | .store a b => .store a b

def Atom.eval (w : ℕ) (s : Data) : Atom → Option Data
  | .assign d a => some (put s d (operand w s a))
  | .binary d op a b => (evalBinOp w op (operand w s a) (operand w s b)).map (put s d)
  | .load d a => (s.memory (operand w s a)).map (put s d)
  | .store a b => some {s with memory:=Function.update s.memory (operand w s a) (some (operand w s b))}

def test (w : ℕ) (s : Data) (op : Test) (a b : Operand) : Bool :=
  evalTest op (operand w s a) (operand w s b)

inductive Command where
  | skip
  | atom (a : Atom)
  | seq (a b : Command)
  | ite (op : Test) (a b : Operand) (yes no : Command)
  | loop (op : Test) (a b : Operand) (body : Command)

namespace Command

def size : Command → ℕ
  | .skip => 0
  | .atom _ => 1
  | .seq a b => size a+size b
  | .ite _ _ _ yes no => 2+size yes+size no
  | .loop _ _ _ body => size body+2

def compile : Command → ℕ → List Instruction
  | .skip,_ => []
  | .atom a,_ => [a.instruction]
  | .seq a b,pc => a.compile pc ++ b.compile (pc+a.size)
  | .ite op a b yes no,pc =>
    Instruction.branch op a b (pc+1) (pc+2+yes.size) ::
      (yes.compile (pc+1) ++ [.jump (pc+size (.ite op a b yes no))] ++ no.compile (pc+2+yes.size))
  | .loop op a b body,pc =>
    Instruction.branch op a b (pc+1) (pc+size (.loop op a b body)) ::
      (body.compile (pc+1) ++ [.jump pc])

lemma length_compile (c : Command) (pc : ℕ) : (c.compile pc).length=c.size := by
  induction c generalizing pc <;> simp_all [compile,size]
  omega

inductive Eval (w : ℕ) : Command → Data → ℕ → Data → Prop
  | skip (s) : Eval w .skip s 0 s
  | atom {s t a} (h : a.eval w s=some t) : Eval w (.atom a) s 1 t
  | seq {a b s t u i j} : Eval w a s i t → Eval w b t j u → Eval w (.seq a b) s (i+j) u
  | iteTrue {op a b yes no s t i} : test w s op a b=true → Eval w yes s i t →
      Eval w (.ite op a b yes no) s (1+i+1) t
  | iteFalse {op a b yes no s t i} : test w s op a b=false → Eval w no s i t →
      Eval w (.ite op a b yes no) s (1+i) t
  | loopFalse {op a b body s} : test w s op a b=false → Eval w (.loop op a b body) s 1 s
  | loopTrue {op a b body s t u i j} : test w s op a b=true → Eval w body s i t →
      Eval w (.loop op a b body) t j u → Eval w (.loop op a b body) s (1+i+1+j) u

end Command

def Contains (p : Program) (base : ℕ) (code : List Instruction) : Prop :=
  ∀ i, i<code.length → p[base+i]?=code[i]?

lemma contains_self (p : Program) : Contains p 0 p := by simp [Contains]
lemma Contains.head {p : Program} {pc : ℕ} {a : Instruction} {l : List Instruction}
    (h : Contains p pc (a::l)) : p[pc]?=some a := by
  simpa using h 0 (by simp)
lemma Contains.tail {p : Program} {pc : ℕ} {a : Instruction} {l : List Instruction}
    (h : Contains p pc (a::l)) : Contains p (pc+1) l := by
  intro i hi
  simpa only [List.getElem?_cons_succ,Nat.add_assoc,Nat.add_comm 1 i] using h (i+1) (by simpa using hi)
lemma Contains.left {p : Program} {pc : ℕ} {a b : List Instruction}
    (h : Contains p pc (a++b)) : Contains p pc a := by
  intro i hi
  simpa only [List.getElem?_append_left hi] using h i (hi.trans_le (by simp))
lemma Contains.right {p : Program} {pc : ℕ} {a b : List Instruction}
    (h : Contains p pc (a++b)) : Contains p (pc+a.length) b := by
  intro i hi
  have hh := h (a.length+i) (by simp only [List.length_append]; omega)
  simpa only [List.getElem?_append_right (Nat.le_add_right _ _),Nat.add_sub_cancel_left,Nat.add_assoc] using hh

lemma step_atom {w : ℕ} {p : Program} {pc : ℕ} {a : Atom} {s t : Data}
    (hc : p[pc]?=some a.instruction) (he : a.eval w s=some t) :
    step w p (state s pc)=some (state t (pc+1)) := by
  cases a <;> simp only [Atom.eval] at he
  · cases he
    simp [step,state,hc,operand,put,putRegister,Atom.instruction,readOperand]
  · simp only [Option.map_eq_some_iff] at he
    obtain ⟨v,hv,rfl⟩ := he
    simp [step,state,hc,operand,put,putRegister,Atom.instruction,readOperand] at *
    simp [hv]
  · simp only [Option.map_eq_some_iff] at he
    obtain ⟨v,hv,rfl⟩ := he
    simp [step,state,hc,operand,put,putRegister,Atom.instruction,readOperand] at *
    simp [hv]
  · cases he
    simp [step,state,hc,operand,Atom.instruction,readOperand]

lemma step_jump {w : ℕ} {p : Program} {pc target : ℕ} (s : Data)
    (hc : p[pc]?=some (.jump target)) :
    step w p (state s pc)=some (state s target) := by simp [step,state,hc]

lemma step_branch {w : ℕ} {p : Program} {pc yes no : ℕ} (s : Data)
    (op : Test) (a b : Operand) (hc : p[pc]?=some (.branch op a b yes no)) :
    step w p (state s pc)=some (state s (if test w s op a b then yes else no)) := by
  simp [step,state,hc,test,operand,readOperand]
  rfl

end Structured
end DeterministicThreeSum

namespace DeterministicThreeSum
lemma run_one_of_step {w : ℕ} {p : Program} {s t : State}
    (h : step w p s=some t) : run w p 1 s=some t := by simp [run,h]
lemma run_comp {w : ℕ} {p : Program} {s t u : State} {i j : ℕ}
    (h : run w p i s=some t) (h' : run w p j t=some u) :
    run w p (i+j) s=some u := by rw [run_add,h]; exact h'

namespace Structured.Command

theorem compile_correct {w : ℕ} {c : Command} {s t : Data} {i : ℕ}
    (he : Eval w c s i t) (p : Program) (pc : ℕ) (hc : Contains p pc (c.compile pc)) :
    run w p i (state s pc)=some (state t (pc+c.size)) := by
  induction he generalizing pc with
  | skip s => simp [run,size]
  | @atom s t a h =>
    simpa only [size] using run_one_of_step (step_atom (hc.head) h)
  | @seq a b s t u i j h h' ih ih' =>
    have hleft : Contains p pc (a.compile pc) := hc.left
    have hright : Contains p (pc+a.size) (b.compile (pc+a.size)) := by
      simpa only [length_compile] using hc.right
    simpa only [size,Nat.add_assoc] using run_comp (ih pc hleft) (ih' (pc+a.size) hright)
  | @iteTrue op a b yes no s t i ht hy ih =>
    have hyes : Contains p (pc+1) (yes.compile (pc+1)) := hc.tail.left.left
    have hj : p[pc+1+yes.size]?=some (.jump (pc+(.ite op a b yes no : Command).size)) := by
      simpa only [length_compile] using hc.tail.left.right.head
    have hbr := step_branch (w:=w) s op a b hc.head
    rw [ht] at hbr
    simp only [↓reduceIte] at hbr
    exact run_comp (run_comp (run_one_of_step hbr) (ih (pc+1) hyes))
      (run_one_of_step (step_jump t hj))
  | @iteFalse op a b yes no s t i ht hn ih =>
    have hno : Contains p (pc+2+yes.size) (no.compile (pc+2+yes.size)) := by
      have hh := hc.tail.right
      simpa only [List.length_append,length_compile,List.length_singleton,Nat.add_assoc,Nat.add_left_comm,Nat.add_comm,show (1:ℕ)+1=2 from rfl,←Nat.add_assoc] using hh
    have hbr := step_branch (w:=w) s op a b hc.head
    rw [ht] at hbr
    simp only [Bool.false_eq_true,↓reduceIte] at hbr
    simpa only [size,Nat.add_assoc] using run_comp (run_one_of_step hbr) (ih (pc+2+yes.size) hno)
  | @loopFalse op a b body s ht =>
    have hbr := step_branch (w:=w) s op a b hc.head
    rw [ht] at hbr
    simp only [Bool.false_eq_true,↓reduceIte] at hbr
    exact run_one_of_step hbr
  | @loopTrue op a b body s t u i j ht hb hl ih ih' =>
    have hbody : Contains p (pc+1) (body.compile (pc+1)) := hc.tail.left
    have hj : p[pc+1+body.size]?=some (.jump pc) := by
      simpa only [length_compile] using hc.tail.right.head
    have hbr := step_branch (w:=w) s op a b hc.head
    rw [ht] at hbr
    simp only [↓reduceIte] at hbr
    exact run_comp (run_comp (run_comp (run_one_of_step hbr) (ih (pc+1) hbody))
      (run_one_of_step (step_jump t hj))) (ih' pc hc)

def decisionProgram (c : Command) (result : Operand) : Program :=
  c.compile 0 ++ [.branch .eq result (.literal 0) (c.size+2) (c.size+1),.halt true,.halt false]

lemma decision_compiles {w : ℕ} {c : Command} {s t : Data} {i : ℕ}
    (he : Eval w c s i t) (result : Operand) (_hword : 1<wordModulus w) :
    ∃ final : State,
      run w (c.decisionProgram result) (i+2) (state s 0)=some final ∧
      final.answer=some (decide (operand w t result≠0)) := by
  let p := c.decisionProgram result
  have hc : Contains p 0 (c.compile 0) := (contains_self p).left
  have hh := compile_correct he p 0 hc
  simp only [Nat.zero_add] at hh
  have hbr : p[c.size]?=some (.branch .eq result (.literal 0) (c.size+2) (c.size+1)) := by
    simpa only [length_compile,Nat.zero_add] using (contains_self p).right.head
  have ht : p[c.size+1]?=some (.halt true) := by
    simpa only [length_compile,Nat.zero_add] using (contains_self p).right.tail.head
  have hf : p[c.size+2]?=some (.halt false) := by
    simpa only [length_compile,Nat.zero_add,Nat.add_assoc] using (contains_self p).right.tail.tail.head
  have hbranch := step_branch (w:=w) t .eq result (.literal 0) hbr
  have hz : test w t .eq result (.literal 0)=decide (operand w t result=0) := by
    simp [test,operand,readOperand,state,evalTest]
    rfl
  rw [hz] at hbranch
  by_cases hv : operand w t result=0
  · simp only [hv,decide_true,↓reduceIte] at hbranch
    let final : State := {state t (c.size+2) with answer:=some false}
    have hhalt : step w p (state t (c.size+2))=some final := by simp [step,state,hf,final]
    refine ⟨final,run_comp hh (run_comp (run_one_of_step hbranch) (run_one_of_step hhalt)),?_⟩
    simp [final,hv]
  · simp only [hv,decide_false,Bool.false_eq_true,↓reduceIte] at hbranch
    let final : State := {state t (c.size+1) with answer:=some true}
    have hhalt : step w p (state t (c.size+1))=some final := by simp [step,state,ht,final]
    refine ⟨final,run_comp hh (run_comp (run_one_of_step hbranch) (run_one_of_step hhalt)),?_⟩
    simp [final,hv]
end Structured.Command
end DeterministicThreeSum
namespace DeterministicThreeSum.Structured
open Command

@[ext] lemma Data.ext {s t : Data} (hr : s.registers=t.registers) (hm : s.memory=t.memory) : s=t := by
  cases s; cases t; simp_all

lemma operand_register (w : ℕ) (s : Data) (r : ℕ) :
    operand w s (.register r)=s.registers r % wordModulus w := rfl
lemma operand_literal (w : ℕ) (s : Data) (v : ℕ) :
    operand w s (.literal v)=v % wordModulus w := rfl

lemma loop_iterations {w N : ℕ} {op : Test} {a b : Operand} {body : Command}
    (s : ℕ → Data) (cost : ℕ → ℕ)
    (hyes : ∀ k, k<N → test w (s k) op a b=true)
    (hno : test w (s N) op a b=false)
    (hbody : ∀ k, k<N → Eval w body (s k) (cost k) (s (k+1))) :
    Eval w (.loop op a b body) (s 0) ((∑ k ∈ Finset.range N, (cost k+2))+1) (s N) := by
  have aux : ∀ i t, i+t=N →
      Eval w (.loop op a b body) (s i) ((∑ k ∈ Finset.range t, (cost (i+k)+2))+1) (s N) := by
    intro i t
    induction t generalizing i with
    | zero => intro h; simp only [Nat.add_zero] at h; subst i; simpa using (Eval.loopFalse hno)
    | succ t ih =>
      intro h
      have hi : i<N := by omega
      have hn : i+1+t=N := by omega
      have he := Eval.loopTrue (hyes i hi) (hbody i hi) (ih (i+1) hn)
      have heq : (fun k => cost (i+(k+1))+2) = (fun k => cost (i+1+k)+2) := by
        funext k
        congr 2
        omega
      rw [Finset.sum_range_succ',heq]
      convert he using 1
      simp only [Nat.add_zero]
      omega
  simpa using aux 0 N (by omega)

lemma eval_increment {w r : ℕ} {s : Data} (hr : s.registers r+1<wordModulus w) :
    Eval w (.atom (.binary r .add (.register r) (.literal 1))) s 1
      (put s r (s.registers r+1)) := by
  apply Eval.atom
  have h1 : 1<wordModulus w := by omega
  simp [Atom.eval,operand_register,operand_literal,evalBinOp,
    Nat.mod_eq_of_lt hr,Nat.mod_eq_of_lt (by omega : s.registers r<wordModulus w),Nat.mod_eq_of_lt h1]

def fillCommand (ptr stop value : ℕ) : Command :=
  .loop .lt (.register ptr) (.register stop)
    (.seq (.atom (.store (.register ptr) (.register value)))
      (.atom (.binary ptr .add (.register ptr) (.literal 1))))

def filled (s : Data) (ptr base count v : ℕ) : Data where
  registers := Function.update s.registers ptr (base+count)
  memory := fun a => if base≤a ∧ a<base+count then some v else s.memory a

lemma filled_zero (s : Data) (ptr base v : ℕ) (h : s.registers ptr=base) :
    filled s ptr base 0 v=s := by
  apply Data.ext
  · simp [filled,←h]
  · funext a; simp only [filled,Nat.add_zero]; split <;> rename_i ha
    · omega
    · rfl

theorem fillCommand_correct {w ptr stop value base N v : ℕ} (s : Data)
    (hptr : s.registers ptr=base) (hstop : s.registers stop=base+N)
    (hvalue : s.registers value=v) (hs : stop≠ptr) (hv : value≠ptr)
    (hbound : base+N<wordModulus w) (hvbound : v<wordModulus w) :
    Eval w (fillCommand ptr stop value) s (4*N+1) (filled s ptr base N v) := by
  have hstates (k : ℕ) (hk : k≤N) :
      (filled s ptr base k v).registers ptr=base+k ∧
      (filled s ptr base k v).registers stop=base+N ∧
      (filled s ptr base k v).registers value=v := by
    simp [filled,Function.update_of_ne hs,Function.update_of_ne hv,hstop,hvalue]
  have hyes : ∀ k, k<N → test w (filled s ptr base k v) .lt (.register ptr) (.register stop)=true := by
    intro k hk
    obtain ⟨ha,hb,hc⟩ := hstates k (by omega)
    simp [test,evalTest,operand_register,ha,hb,Nat.mod_eq_of_lt hbound,
      Nat.mod_eq_of_lt (by omega : base+k<wordModulus w)]
    omega
  have hno : test w (filled s ptr base N v) .lt (.register ptr) (.register stop)=false := by
    obtain ⟨ha,hb,hc⟩ := hstates N le_rfl
    simp [test,evalTest,operand_register,ha,hb]
  have hbody : ∀ k, k<N → Eval w
      (.seq (.atom (.store (.register ptr) (.register value)))
       (.atom (.binary ptr .add (.register ptr) (.literal 1))))
      (filled s ptr base k v) 2 (filled s ptr base (k+1) v) := by
    intro k hk
    obtain ⟨ha,hb,hc⟩ := hstates k (by omega)
    let t : Data := {filled s ptr base k v with
      memory:=Function.update (filled s ptr base k v).memory (base+k) (some v)}
    have hs' : Eval w (.atom (.store (.register ptr) (.register value)))
        (filled s ptr base k v) 1 t := by
      apply Eval.atom
      simp [Atom.eval,operand_register,ha,hc,Nat.mod_eq_of_lt hvbound,
        Nat.mod_eq_of_lt (by omega : base+k<wordModulus w),t]
    have ht : t.registers ptr+1<wordModulus w := by change (filled s ptr base k v).registers ptr+1<wordModulus w; rw [ha]; omega
    have hi := eval_increment ht
    have heq : put t ptr (t.registers ptr+1)=filled s ptr base (k+1) v := by
      apply Data.ext
      · simp [put,t,filled,Function.update_idem,Nat.add_assoc]
      · funext a
        simp only [put,t,filled]
        by_cases ha : a=base+k
        · subst a; simp
        · rw [Function.update_of_ne ha]
          by_cases hl : base≤a
          · by_cases hh : a<base+k
            · simp [hl,hh,show a<base+(k+1) by omega]
            · simp [hl,hh,show ¬a<base+(k+1) by omega]
          · simp [hl]
    rw [heq] at hi
    exact Eval.seq hs' hi
  have he := loop_iterations (N:=N) (fun k => filled s ptr base k v) (fun _ => 2) hyes hno hbody
  rw [filled_zero s ptr base v hptr] at he
  simpa [fillCommand,Finset.sum_const,Nat.mul_comm] using he
end DeterministicThreeSum.Structured
namespace DeterministicThreeSum.Structured
open Command

def Atom.writes : Atom → Finset ℕ
  | .assign d _ | .binary d _ _ _ | .load d _ => {d}
  | .store _ _ => ∅

def Command.writes : Command → Finset ℕ
  | .skip => ∅
  | .atom a => a.writes
  | .seq a b => a.writes∪b.writes
  | .ite _ _ _ a b => a.writes∪b.writes
  | .loop _ _ _ body => body.writes

def Atom.noStore : Atom → Prop
  | .store _ _ => False
  | _ => True

def Command.noStore : Command → Prop
  | .skip => True
  | .atom a => a.noStore
  | .seq a b | .ite _ _ _ a b => a.noStore ∧ b.noStore
  | .loop _ _ _ body => body.noStore

lemma atom_register_frame {w r : ℕ} {s t : Data} {a : Atom}
    (he : a.eval w s=some t) (hr : r ∉ a.writes) : t.registers r=s.registers r := by
  cases a <;> simp only [Atom.eval,Option.map_eq_some_iff] at he
  · cases he; simp [put, Function.update_of_ne (by simpa [Atom.writes] using hr)]
  · obtain ⟨v,_,rfl⟩ := he
    simp [put,Function.update_of_ne (by simpa [Atom.writes] using hr)]
  · obtain ⟨v,_,rfl⟩ := he
    simp [put,Function.update_of_ne (by simpa [Atom.writes] using hr)]
  · cases he; rfl

lemma register_frame {w r cost : ℕ} {s t : Data} {c : Command}
    (he : Eval w c s cost t) (hr : r ∉ c.writes) : t.registers r=s.registers r := by
  induction he with
  | skip => rfl
  | atom h => exact atom_register_frame h hr
  | seq h h' ih ih' =>
    simp only [Command.writes,Finset.mem_union,not_or] at hr
    exact (ih' hr.2).trans (ih hr.1)
  | iteTrue _ _ ih => exact ih (by simp only [Command.writes,Finset.mem_union,not_or] at hr; exact hr.1)
  | iteFalse _ _ ih => exact ih (by simp only [Command.writes,Finset.mem_union,not_or] at hr; exact hr.2)
  | loopFalse => rfl
  | loopTrue _ _ _ ih ih' => exact (ih' hr).trans (ih hr)

lemma atom_memory_frame {w : ℕ} {s t : Data} {a : Atom}
    (he : a.eval w s=some t) (hstore : a.noStore) : t.memory=s.memory := by
  cases a <;> simp only [Atom.eval,Option.map_eq_some_iff] at he
  · cases he; rfl
  · obtain ⟨v,_,rfl⟩ := he; rfl
  · obtain ⟨v,_,rfl⟩ := he; rfl
  · exact False.elim hstore

lemma memory_frame {w cost : ℕ} {s t : Data} {c : Command}
    (he : Eval w c s cost t) (hstore : c.noStore) : t.memory=s.memory := by
  induction he with
  | skip => rfl
  | atom h => exact atom_memory_frame h hstore
  | seq h h' ih ih' => exact (ih' hstore.2).trans (ih hstore.1)
  | iteTrue _ _ ih => exact ih hstore.1
  | iteFalse _ _ ih => exact ih hstore.2
  | loopFalse => rfl
  | loopTrue _ _ _ ih ih' => exact (ih' hstore).trans (ih hstore)

theorem bounded_loop {w N B : ℕ} {op : Test} {a b : Operand} {body : Command}
    (Inv : ℕ → Data → Prop)
    (hyes : ∀ k s, k<N → Inv k s → test w s op a b=true)
    (hno : ∀ s, Inv N s → test w s op a b=false)
    (hbody : ∀ k s, k<N → Inv k s → ∃ cost t, Eval w body s cost t ∧ cost≤B ∧ Inv (k+1) t)
    {i : ℕ} (hi : i≤N) {s : Data} (hs : Inv i s) :
    ∃ cost t, Eval w (.loop op a b body) s cost t ∧ cost≤(B+2)*(N-i)+1 ∧ Inv N t := by
  have aux : ∀ fuel i, i+fuel=N → ∀ s, Inv i s →
      ∃ cost t, Eval w (.loop op a b body) s cost t ∧ cost≤(B+2)*fuel+1 ∧ Inv N t := by
    intro fuel
    induction fuel with
    | zero =>
      intro i he s hs
      have hi : i=N := by omega
      subst i
      exact ⟨1,s,Eval.loopFalse (hno s hs),by simp,hs⟩
    | succ fuel ih =>
      intro i he s hs
      have hlt : i<N := by omega
      obtain ⟨c,t,ht,hc,hInv⟩ := hbody i s hlt hs
      obtain ⟨c',u,hu,hc',hInv'⟩ := ih (i+1) (by omega) t hInv
      exact ⟨1+c+1+c',u,Eval.loopTrue (hyes i s hlt hs) ht hu,by nlinarith,hInv'⟩
  exact aux (N-i) i (by omega) s hs

lemma eval_assign {w : ℕ} (s : Data) (r : ℕ) (a : Operand) :
    Eval w (.atom (.assign r a)) s 1 (put s r (operand w s a)) := Eval.atom rfl

lemma eval_load {w r a v : ℕ} {s : Data} (ha : a<wordModulus w) (hv : s.memory a=some v) :
    Eval w (.atom (.load r (.literal a))) s 1 (put s r v) := by
  apply Eval.atom
  simp [Atom.eval,operand_literal,Nat.mod_eq_of_lt ha,hv]
end DeterministicThreeSum.Structured

end OAI