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 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 intro h; simp only [Nat.add_zero] at h; subst i; simpa using (Eval.loopFalse hno) | succ t ih => intro h have hi : i 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 if base≤a ∧ a 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 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 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