Download lean/OAI/Computability/QuantumFactoring/BitStackDivision.lean from roseking/openAI-math-dataset: direct link, hf CLI and curl.
- Browser
- Download file 6.12 kB
-
https://huggingface.co/datasets/roseking/openAI-math-dataset/resolve/main/lean/OAI/Computability/QuantumFactoring/BitStackDivision.lean
- Command line
-
hf download hf://datasets/roseking/openAI-math-dataset/lean/OAI/Computability/QuantumFactoring/BitStackDivision.lean
-
curl -L -o BitStackDivision.lean https://huggingface.co/datasets/roseking/openAI-math-dataset/resolve/main/lean/OAI/Computability/QuantumFactoring/BitStackDivision.lean
6.12 kB
| import Mathlib | |
| import OAI.Computability.QuantumFactoring.BitStackMultiplier | |
| import OAI.Computability.QuantumFactoring.Procedure | |
| namespace OAI | |
| section | |
| namespace ExactQuantumFactoring.BitStackProgram | |
| namespace Procedure | |
| variable {α β : Type} {ea : α→List Bool} {eb : β→List Bool} | |
| {c : α→Bool} {f g : α→β} | |
| noncomputable def conditional (test : Procedure ea boolCode c) | |
| (yes : Procedure ea eb f) (no : Procedure ea eb g) : | |
| Procedure ea eb (fun x=>if c x then f x else g x) := | |
| (choose no yes).comp (test.pair (identity ea)) | |
| noncomputable def binaryZero : Procedure Nat.bits boolCode (fun n=>decide (n=0)) := | |
| (isEmpty.precompose Nat.bits).congrFun (by | |
| intro n | |
| by_cases hn : n=0 | |
| · subst n;rfl | |
| · have hh : n.bits≠[]:=by | |
| intro h | |
| have hy:=binaryValue_bits n | |
| rw [h] at hy | |
| exact hn hy.symm | |
| simp [hh,hn]) | |
| end Procedure | |
| abbrev DivState := ℕ×ℕ×ℕ | |
| abbrev divStateCode := prodCode Nat.bits (prodCode Nat.bits Nat.bits) | |
| def divStep (b : Bool) (x : DivState) : DivState := | |
| let t:=2*x.2.2+b.toNat | |
| if x.1≤t then (x.1,2*x.2.1+1,t-x.1) else (x.1,2*x.2.1,t) | |
| lemma divStep_fst (b : Bool) (x : DivState) : (divStep b x).1=x.1 := by | |
| dsimp only [divStep];split <;> rfl | |
| lemma divStep_size (b : Bool) (x : DivState) : | |
| (divStateCode (divStep b x)).length≤(divStateCode x).length+3 := by | |
| have hq : (2*x.2.1+1).bits.length≤x.2.1.bits.length+1:=doubleOne_bits_length _ | |
| have hq0:=double_bits_length x.2.1 | |
| have hr : (2*x.2.2+b.toNat).bits.length≤x.2.2.bits.length+1:=by | |
| cases b | |
| · simpa using double_bits_length x.2.2 | |
| · exact doubleOne_bits_length x.2.2 | |
| have hs : (2*x.2.2+b.toNat-x.1).bits.length≤(2*x.2.2+b.toNat).bits.length:= | |
| bits_length_mono (Nat.sub_le _ _) | |
| simp only [divStateCode,divStep,prodCode,pairBits_length] | |
| split <;> dsimp only <;> omega | |
| lemma divStep_spec (b : Bool) (x : DivState) (hr : x.2.2<x.1) : | |
| (divStep b x).2.2<x.1 ∧ | |
| (divStep b x).2.1*x.1+(divStep b x).2.2= | |
| 2*(x.2.1*x.1+x.2.2)+b.toNat := by | |
| have hb : b.toNat≤1:=by cases b <;> decide | |
| dsimp only [divStep] | |
| split | |
| · rename_i h | |
| have ht:=Nat.sub_add_cancel h | |
| dsimp only | |
| constructor <;> nlinarith | |
| · rename_i h | |
| dsimp only | |
| constructor <;> nlinarith | |
| lemma div_fold_spec (xs : List Bool) (x : DivState) (hr : x.2.2<x.1) : | |
| let y:=xs.foldl (bitAction (divStep false) (divStep true)) x | |
| y.1=x.1 ∧ y.2.2<x.1 ∧ | |
| y.2.1*x.1+y.2.2=binaryValue xs.reverse+2^xs.length*(x.2.1*x.1+x.2.2) := by | |
| induction xs generalizing x with | |
| | nil=>simp [binaryValue,hr] | |
| | cons b bs ih=> | |
| have he : bitAction (divStep false) (divStep true) x b=divStep b x:=by cases b <;> rfl | |
| rw [List.foldl_cons,he] | |
| have H:=divStep_spec b x hr | |
| have hi:=ih (divStep b x) (by rw [divStep_fst];exact H.1) | |
| simp only [divStep_fst] at hi | |
| refine ⟨hi.1,hi.2.1,?_⟩ | |
| rw [hi.2.2,H.2] | |
| simp only [List.reverse_cons,binaryValue_append,List.length_reverse,List.length_cons, | |
| binaryValue,pow_succ] | |
| ring | |
| def divideRaw (x : List Bool×ℕ) : ℕ×ℕ := | |
| (x.1.reverse.foldl (bitAction (divStep false) (divStep true)) (x.2,0,0)).2 | |
| lemma divideRaw_spec (x : List Bool×ℕ) (hp : 0<x.2) : | |
| divideRaw x=(binaryValue x.1/x.2,binaryValue x.1%x.2) := by | |
| have hh:=div_fold_spec x.1.reverse (x.2,0,0) hp | |
| simp only [List.reverse_reverse,zero_mul,mul_zero,Nat.add_zero] at hh | |
| have hq : binaryValue x.1/x.2=(divideRaw x).1:=by | |
| apply Nat.div_eq_of_lt_le <;> dsimp [divideRaw] <;> nlinarith [hh.2.1,hh.2.2] | |
| have hm:=Nat.mod_add_div (binaryValue x.1) x.2 | |
| rw [hq] at hm | |
| apply Prod.ext | |
| · exact hq.symm | |
| · dsimp [divideRaw] at hm ⊢;nlinarith [hh.2.2] | |
| namespace Procedure | |
| noncomputable def divStepP (b : Bool) : Procedure divStateCode divStateCode (divStep b) := by | |
| let d:=first Nat.bits (prodCode Nat.bits Nat.bits) | |
| let qr:=second Nat.bits (prodCode Nat.bits Nat.bits) | |
| let q:=(first Nat.bits Nat.bits).comp qr | |
| let r:=(second Nat.bits Nat.bits).comp qr | |
| let db : Procedure Nat.bits Nat.bits (fun n=>2*n+b.toNat):=by | |
| cases b | |
| · exact double.congrFun (by intro n;simp) | |
| · exact doubleOne | |
| let t:=db.comp r | |
| let test:=binaryLe.comp (d.pair t) | |
| let yes:=d.pair ((doubleOne.comp q).pair (binarySub.comp (t.pair d))) | |
| let no:=d.pair ((double.comp q).pair t) | |
| exact (conditional test yes no).congrFun (by intro x;simp only [Function.comp_apply,divStep,decide_eq_true_eq]) | |
| noncomputable def divideRawP : Procedure (prodCode (id : List Bool→List Bool) Nat.bits) | |
| (prodCode Nat.bits Nat.bits) divideRaw := by | |
| let x:=first (id : List Bool→List Bool) Nat.bits | |
| let d:=second (id : List Bool→List Bool) Nat.bits | |
| let zero:=constant (prodCode (id : List Bool→List Bool) Nat.bits) Nat.bits 0 | |
| let start:=(reverse.comp x).pair (d.pair (zero.pair zero)) | |
| let fold:=foldBitsLinear (divStepP false) (divStepP true) 3 (divStep_size false) (divStep_size true) | |
| exact ((second Nat.bits (prodCode Nat.bits Nat.bits)).comp (fold.comp start)).congrFun (by intro x;rfl) | |
| noncomputable def binaryDivMod : Procedure (prodCode Nat.bits Nat.bits) | |
| (prodCode Nat.bits Nat.bits) (fun x=>(x.1/x.2,x.1%x.2)) := by | |
| let raw:=(divideRawP.precompose (fun x : ℕ×ℕ=>(x.1.bits,x.2))) | |
| let test:=binaryZero.comp (second Nat.bits Nat.bits) | |
| let zerocase:=(constant (prodCode Nat.bits Nat.bits) Nat.bits 0).pair (first Nat.bits Nat.bits) | |
| exact (conditional test zerocase raw).congrFun (by | |
| intro x | |
| change (if decide (x.2=0) then (0,x.1) else divideRaw (x.1.bits,x.2))=(x.1/x.2,x.1%x.2) | |
| by_cases hz : x.2=0 | |
| · simp [hz] | |
| · simp only [hz,decide_false,Bool.false_eq_true,ite_false] | |
| simpa only [binaryValue_bits] using divideRaw_spec (x.1.bits,x.2) (by omega)) | |
| noncomputable def binaryDiv : Procedure (prodCode Nat.bits Nat.bits) Nat.bits | |
| (fun x=>x.1/x.2) := (first Nat.bits Nat.bits).comp binaryDivMod | |
| noncomputable def binaryMod : Procedure (prodCode Nat.bits Nat.bits) Nat.bits | |
| (fun x=>x.1%x.2) := (second Nat.bits Nat.bits).comp binaryDivMod | |
| end Procedure | |
| end ExactQuantumFactoring.BitStackProgram | |
| end | |
| end OAI | |