import OAI.NumberTheory.SingleFold.Power namespace OAI namespace SingleFold.Binary def Bits (x y : ℕ) : Prop := ∀ i, x/2^i%2 ≤ y/2^i%2 lemma split (x y : ℕ) : Bits x y ↔ x%2 ≤ y%2 ∧ Bits (x/2) (y/2) := by constructor · intro h refine ⟨by simpa using h 0, ?_⟩ intro i simpa [Nat.div_div_eq_div_mul, pow_succ', mul_comm] using h (i+1) · rintro ⟨h,h'⟩ (_|i) · simpa using h · simpa [Nat.div_div_eq_div_mul, pow_succ', mul_comm] using h' i lemma zero_right (x : ℕ) : Bits x 0 ↔ x=0 := by constructor · induction x using Nat.strong_induction_on with | h x ih => intro hx by_cases h : x=0 · exact h have he := (split x 0).mp hx have hh := ih (x/2) (Nat.div_lt_self (by omega) (by omega)) (by simpa using he.2) simp only [Nat.zero_mod] at he omega · rintro rfl; intro i; simp lemma choose_odd_iff (x y : ℕ) : y.choose x % 2=1 ↔ Bits x y := by induction y using Nat.strong_induction_on generalizing x with | h y ih => by_cases hy : y=0 · subst y rw [zero_right] cases x <;> simp have hl := Choose.choose_modEq_choose_mod_mul_choose_div_nat (n:=y) (k:=x) (p:=2) change y.choose x%2 = (y%2).choose (x%2)*(y/2).choose (x/2)%2 at hl rw [hl,Nat.mul_mod,split, ← ih (y/2) (Nat.div_lt_self (by omega) (by omega))] have hx := Nat.mod_lt x (by omega : 0<2) have hy' := Nat.mod_lt y (by omega : 0<2) have hz := Nat.mod_lt ((y/2).choose (x/2)) (by omega : 0<2) interval_cases hxm : x%2 <;> interval_cases hym : y%2 <;> simp_all lemma le {x y : ℕ} (h : Bits x y) : x≤y := by have hh := (choose_odd_iff x y).mpr h by_contra hl rw [Nat.choose_eq_zero_of_lt (by omega)] at hh omega lemma ofDigits_digit {B : ℕ} (hB : 0 hl a (List.mem_of_mem_drop ha) have he : l[i]?=(l.drop i)[0]? := by simp rw [he] generalize l.drop i = v at * cases v with | nil => simp | cons a v => simp only [Nat.ofDigits_cons, Nat.add_mul_mod_self_left, List.getElem?_cons_zero, Option.getD_some] exact Nat.mod_eq_of_lt (hd a List.mem_cons_self) lemma ofDigits_map_range (B n : ℕ) (f : ℕ → ℕ) : Nat.ofDigits B ((List.range n).map f) = ∑ i∈Finset.range n, f i*B^i := by induction n with | zero => simp | succ n ih => rw [List.range_succ,List.map_append,Nat.ofDigits_append] simp [ih,Finset.sum_range_succ, mul_comm] lemma choose_digit {x y B : ℕ} (hxy : x≤y) (hB : 2^y assumption lemma complete {x y : ℕ} (h : Bits x y) : ∃ w, System x y w := by let B:=2^(y+1) let H:=B^x let N:=(B+1)^y let d:=N/H%B have hB : 0 f i z) := by exact ((MapSF.proj (fun i => (i,()))).comp (MapSF.all h)).congr (by intros; rfl) end SingleFold.Compiler.MapSF namespace SingleFold.Compiler.ScalarSF variable {α : Type} {f g h : (α → ℕ) → ℕ} lemma powerRel (hf : ScalarSF f) (hg : ScalarSF g) (hh : ScalarSF h) : SF (fun z => 2≤f z ∧ h z=f z^g z) := by have hv : MapSF (fun z => ![f z,g z,h z]) := MapSF.vector (by intro i; fin_cases i <;> assumption) exact hv.pullback PowerCompilation.power end SingleFold.Compiler.ScalarSF namespace SingleFold.Binary open Compiler lemma system_sf : SF (fun z : Fin 2⊕Fin 7 → ℕ => System (z (.inl 0)) (z (.inl 1)) (z ∘ Sum.inr)) := by let α:=Fin 2⊕Fin 7 let a (i : α) : ScalarSF (fun z : α → ℕ => z i) := ScalarSF.proj i let x:=a (.inl 0); let y:=a (.inl 1) let B:=a (.inr 0); let H:=a (.inr 1); let N:=a (.inr 2) let s:=a (.inr 3); let d:=a (.inr 4); let r:=a (.inr 5); let t:=a (.inr 6) let k (j : ℕ) : ScalarSF (fun _ : α → ℕ => j) := ScalarSF.const j have h := (x.le y).and (((k 2).powerRel (y.add (k 1)) B).and ((B.powerRel x H).and (((B.add (k 1)).powerRel y N).and ((N.eq ((((s.mul B).mul H).add (d.mul H)).add r)).and ((d.lt B).and ((r.lt H).and (d.eq (((k 2).mul t).add (k 1))))))))) refine h.congr ?_ intro z constructor · rintro ⟨hxy,⟨_,hB⟩,⟨_,hH⟩,⟨_,hN⟩,he⟩ exact ⟨hxy,hB,hH,hN,he⟩ · rintro ⟨hxy,hB,hH,hN,he⟩ have hb : 2≤z (.inr 0) := by change 2≤(z ∘ Sum.inr) 0 rw [hB,pow_succ] have := Nat.two_pow_pos (z (.inl 1)) omega exact ⟨hxy,⟨by omega,hB⟩,⟨hb,hH⟩,⟨by omega,hN⟩,he⟩ theorem bits : SF (fun z : Fin 2 → ℕ => Bits (z 0) (z 1)) := by have h := system_sf.ex (by intro z w w' hw hw'; exact unique hw hw') refine h.congr ?_ intro z exact ⟨fun ⟨w,hw⟩ => sound hw,complete⟩ end SingleFold.Binary end OAI