import Mathlib import OAI.Combinatorics.MatchingCount.Arithmetic.TreeMultiply import OAI.Combinatorics.MatchingCount.Arithmetic.TreeCompare import OAI.Combinatorics.MatchingCount.Arithmetic.TreeSubtract import OAI.Combinatorics.MatchingCount.Arithmetic.BinaryDivision namespace OAI namespace MatchingFPRAS.TreeDivision open TreeData TreeMachine RegisterProc open StackAddition (value value_bits) open BinaryDivision (low nextQ nextR) open RegisterControl (Effect) abbrev Reg := Fin 7 inductive Point where | loop | split | shift | copyR | copyD | cmp | test | copyD2 | sub | one | zero | pushQ | halt deriving DecidableEq, Inhabited instance : Fintype Point := ⟨{.loop,.split,.shift,.copyR,.copyD,.cmp,.test,.copyD2,.sub,.one,.zero,.pushQ,.halt}, by intro p; cases p <;> simp⟩ def program : Point → RegisterControl.Instr Reg Point | .loop => .branch 0 .halt .split | .split => .call (procedure (TreeMachine.Instr.split (Λ:=Unit) 4 0 (by decide) ())) .shift | .shift => .call (procedure (TreeMachine.Instr.cons (Λ:=Unit) 4 2 (by decide) ())) .copyR | .copyR => .call (procedure (TreeMachine.Instr.copy (Λ:=Unit) 2 4 (by decide) ())) .copyD | .copyD => .call (procedure (TreeMachine.Instr.copy (Λ:=Unit) 1 5 (by decide) ())) .cmp | .cmp => .call (TreeArithmetic.less 4 5 (by decide)) .test | .test => .branch 4 .copyD2 .zero | .copyD2 => .call (procedure (TreeMachine.Instr.copy (Λ:=Unit) 1 5 (by decide) ())) .sub | .sub => .call (TreeArithmetic.sub 2 5 (by decide)) .one | .one => .call (procedure (TreeMachine.Instr.cons (Λ:=Unit) 5 4 (by decide) ())) .pushQ | .zero => .call (procedure (TreeMachine.Instr.nil (Λ:=Unit) 4 ())) .pushQ | .pushQ => .call (procedure (TreeMachine.Instr.cons (Λ:=Unit) 4 3 (by decide) ())) .loop | .halt => .halt def splitState (E : Env Reg) (b : Bool) (A : List Bool) := putPair E 4 0 (bit b) (bits A) def shiftState (E : Env Reg) (b : Bool) (A C : List Bool) := putPair (splitState E b A) 4 2 .nil (bits (b::C)) def copyState (E : Env Reg) (b : Bool) (A B C : List Bool) := putPair (putPair (shiftState E b A C) 2 4 (bits (b::C)) (bits (b::C))) 1 5 (bits B) (bits B) def tested (E : Env Reg) (b : Bool) (A B C : List Bool) := putPair (copyState E b A B C) 4 5 (bit (low b B C)) .nil def reduced (E : Env Reg) (b : Bool) (A B C : List Bool) := putPair (putPair (tested E b A B C) 1 5 (bits B) (bits B)) 2 5 (nat (value (b::C)-value B)) .nil def digitState (E : Env Reg) (b : Bool) (A B C : List Bool) : Env Reg := if low b B C then Function.update (tested E b A B C) 4 .nil else putPair (reduced E b A B C) 5 4 .nil (bit true) def next (E : Env Reg) (b : Bool) (A B Q C : List Bool) : Env Reg := putPair (digitState E b A B C) 4 3 .nil (bits (nextQ b B Q C)) def result (E : Env Reg) : List Bool → List Bool → List Bool → List Bool → Env Reg | [],_,_,_ => E | b::A,B,Q,C => result (next E b A B Q C) A B (nextQ b B Q C) (nextR b B C) lemma next_zero (E : Env Reg) (b : Bool) (A B Q C : List Bool) : next E b A B Q C 0=bits A := by unfold next digitState split_ifs <;> simp [tested,copyState,shiftState,splitState,reduced,putPair] lemma next_one (E : Env Reg) (b : Bool) (A B Q C : List Bool) : next E b A B Q C 1=bits B := by unfold next digitState split_ifs <;> simp [tested,copyState,reduced,putPair] lemma next_two (E : Env Reg) (b : Bool) (A B Q C : List Bool) : next E b A B Q C 2=bits (nextR b B C) := by unfold next digitState nextR split_ifs <;> simp [tested,copyState,shiftState,splitState,reduced,putPair,nat,bits] lemma next_three (E : Env Reg) (b : Bool) (A B Q C : List Bool) : next E b A B Q C 3=bits (nextQ b B Q C) := by simp [next,putPair] lemma next_four (E : Env Reg) (b : Bool) (A B Q C : List Bool) : next E b A B Q C 4=.nil := by simp [next,putPair] lemma next_five (E : Env Reg) (b : Bool) (A B Q C : List Bool) : next E b A B Q C 5=.nil := by unfold next digitState split_ifs <;> simp [tested,reduced,putPair] lemma next_six (E : Env Reg) (b : Bool) (A B Q C : List Bool) : next E b A B Q C 6=E 6 := by unfold next digitState split_ifs <;> simp [tested,copyState,shiftState,splitState,reduced,putPair] lemma outputs (E : Env Reg) (A B Q C : List Bool) (h2 : E 2=bits C) (h3 : E 3=bits Q) : result E A B Q C 2=bits (BinaryDivision.run A B Q C).2 ∧ result E A B Q C 3=bits (BinaryDivision.run A B Q C).1 := by induction A generalizing E Q C with | nil => exact ⟨h2,h3⟩ | cons b A ih => exact ih _ _ _ (next_two E b A B Q C) (next_three E b A B Q C) open RegisterControl (Runs) lemma cycle_runs (E F : Env Reg) (b : Bool) (A B Q C : List Bool) (D n : ℕ) (h0 : E 0=bits (b::A)) (h1 : E 1=bits B) (h2 : E 2=bits C) (h3 : E 3=bits Q) (h4 : E 4=.nil) (h5 : E 5=.nil) (hB : B.length ≤ D) (hC : C.length ≤ D) (rest : Runs program (some Point.loop) (next E b A B Q C) F n) : Runs program (some Point.loop) E F (100*(D+2)+n) := by have hf4 : digitState E b A B C 4=bit (!low b B C) := by cases hl : low b B C <;> simp [digitState,hl,putPair,bit] have hf3 : digitState E b A B C 3=bits Q := by unfold digitState split_ifs <;> simp [tested,reduced,copyState,shiftState,splitState,putPair,h3] have hpush := cons_exec (digitState E b A B C) 4 3 (by decide) () rw [hf4,hf3] at hpush change Exec _ _ (encodeEnv (next E b A B Q C)) _ at hpush have hpush' := hpush.mono (show 2*(encode (bit (!low b B C))).length+3 ≤ 9 by have := TreeMultiply.bit_length (!low b B C); omega) have tail : Runs program (some Point.pushQ) (digitState E b A B C) F (10+n) := Runs.step Point.pushQ _ _ _ _ _ 10 (Effect.call _ _ _ _ 9 hpush') rest have ht : Runs program (some Point.test) (tested E b A B C) F (31*D+60+n) := by cases hl : low b B C with | true => have hx : tested E b A B C 4=.cons .nil .nil := by simp [tested,putPair,hl,bit] have hzero := nil_exec (tested E b A B C) 4 () rw [hx] at hzero change Exec _ _ (encodeEnv (Function.update (tested E b A B C) 4 .nil)) 5 at hzero simp only [digitState,hl,↓reduceIte] at tail have hz : Runs program (some Point.zero) (tested E b A B C) F (6+(10+n)) := Runs.step Point.zero _ _ _ _ _ 6 (Effect.call _ _ _ _ 5 hzero) tail have htest : Runs program (some Point.test) (tested E b A B C) F (1+(6+(10+n))) := Runs.step Point.test _ _ _ _ _ 1 (Effect.consBranch _ 4 _ _ .nil .nil hx) hz exact Runs.weaken _ _ _ _ _ htest (by omega) | false => have hx : tested E b A B C 4=.nil := by simp [tested,putPair,hl,bit] have hlower : value B ≤ value (b::C) := by have hn : ¬ value (b::C) have hh : Runs program (some Point.halt) E E 1 := Runs.step Point.halt _ _ _ _ 0 1 (Effect.halt _) (Runs.done _) have hl : Runs program (some Point.loop) E E 2 := Runs.step Point.loop _ _ _ _ 1 1 (Effect.nilBranch _ 0 _ _ h0) hh exact Runs.weaken _ _ _ _ _ hl (by simp only [List.length_nil]; omega) | cons b A ih => have hr := ih (next E b A B Q C) (nextQ b B Q C) (nextR b B C) (next_zero E b A B Q C) (next_one E b A B Q C) (next_two E b A B Q C) (next_three E b A B Q C) (next_four E b A B Q C) (next_five E b A B Q C) (by have := BinaryDivision.nextR_length b B C; simp only [List.length_cons] at hC; omega) have hh := cycle_runs E _ b A B Q C D _ h0 h1 h2 h3 h4 h5 hB (by simp only [List.length_cons] at hC; omega) hr convert hh using 1 <;> simp only [result,List.length_cons] ring def divide : Proc (K Reg) := RegisterControl.compile program theorem exec (E : Env Reg) (A B Q C : List Bool) (D : ℕ) (h0 : E 0=bits A) (h1 : E 1=bits B) (h2 : E 2=bits C) (h3 : E 3=bits Q) (h4 : E 4=.nil) (h5 : E 5=.nil) (hB : B.length ≤ D) (hC : C.length+A.length ≤ D) : Exec divide (encodeEnv E) (encodeEnv (result E A B Q C)) (100*(A.length+1)*(D+2)) := RegisterControl.compile_exec program _ E _ (runs E A B Q C D h0 h1 h2 h3 h4 h5 hB hC) end MatchingFPRAS.TreeDivision end OAI