Download data/lean/OAI/LinearAlgebra/MatrixMultiplication/Arithmetic/Programs.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 6.18 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/LinearAlgebra/MatrixMultiplication/Arithmetic/Programs.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/LinearAlgebra/MatrixMultiplication/Arithmetic/Programs.lean
-
curl -L -o Programs.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/LinearAlgebra/MatrixMultiplication/Arithmetic/Programs.lean
6.18 kB
| import OAI.LinearAlgebra.MatrixMultiplication.Arithmetic.Complexity | |
| import Lean.Elab.Tactic.Omega | |
| namespace OAI | |
| /-! Division-free arithmetic programs over a field and their operation counts. -/ | |
| noncomputable section | |
| namespace MatrixMultiplication.Arithmetic | |
| variable {F : Type*} [Field F] | |
| namespace Gate | |
| variable {Input R S : Type*} | |
| def mapRegister (f : R β S) : Gate F Input R β Gate F Input S | |
| | .constant z => .constant z | |
| | .input i => .input i | |
| | .add i j => .add (f i) (f j) | |
| | .sub i j => .sub (f i) (f j) | |
| | .mul i j => .mul (f i) (f j) | |
| theorem eval_mapRegister (g : Gate F Input R) (f : R β S) | |
| (inputs : Input β F) (registers : S β F) : | |
| (g.mapRegister f).eval inputs registers = g.eval inputs (fun i => registers (f i)) := by | |
| cases g <;> rfl | |
| omit [Field F] in | |
| @[simp] theorem cost_mapRegister (g : Gate F Input R) (f : R β S) : | |
| (g.mapRegister f).cost = g.cost := by | |
| cases g <;> rfl | |
| end Gate | |
| namespace Program | |
| variable {Input : Type*} | |
| def oldIndex (r s : β) (i : Fin r) : Fin (r + s) := β¨s + i.val, by omegaβ© | |
| def newIndex (r s : β) (i : Fin s) : Fin (r + s) := β¨i.val, by omegaβ© | |
| @[simp] theorem oldIndex_zero (r : β) (i : Fin r) : oldIndex r 0 i = i := by | |
| apply Fin.ext | |
| simp [oldIndex] | |
| @[simp] theorem oldIndex_succ (r s : β) (i : Fin r) : | |
| oldIndex r (s + 1) i = (oldIndex r s i).succ := by | |
| apply Fin.ext | |
| simp only [oldIndex, Fin.val_succ] | |
| omega | |
| @[simp] theorem newIndex_zero (r s : β) : newIndex r (s + 1) 0 = 0 := by | |
| apply Fin.ext | |
| rfl | |
| @[simp] theorem newIndex_succ (r s : β) (i : Fin s) : | |
| newIndex r (s + 1) i.succ = (newIndex r s i).succ := by | |
| apply Fin.ext | |
| rfl | |
| def append {r : β} (p : Program F Input r) : {s : β} β Program F Input s β Program F Input (r + s) | |
| | 0, .nil => p | |
| | s + 1, .step q g => (p.append q).step (g.mapRegister (newIndex r s)) | |
| theorem eval_append_new {r s : β} (p : Program F Input r) (q : Program F Input s) | |
| (inputs : Input β F) : | |
| β i : Fin s, (p.append q).eval inputs (newIndex r s i) = q.eval inputs i := by | |
| induction q with | |
| | nil => intro i; exact Fin.elim0 i | |
| | @step s q g ih => | |
| intro i | |
| refine Fin.cases ?_ (fun j => ?_) i | |
| Β· simp only [append, newIndex_zero, eval_step_zero, Gate.eval_mapRegister] | |
| rw [funext ih] | |
| Β· simpa only [append, newIndex_succ, eval_step_succ] using ih j | |
| theorem eval_append_old {r s : β} (p : Program F Input r) (q : Program F Input s) | |
| (inputs : Input β F) : | |
| β i : Fin r, (p.append q).eval inputs (oldIndex r s i) = p.eval inputs i := by | |
| induction q with | |
| | nil => intro i; simp [append] | |
| | @step s q g ih => | |
| intro i | |
| simpa only [append, oldIndex_succ, eval_step_succ] using ih i | |
| omit [Field F] in | |
| @[simp] theorem cost_append {r s : β} (p : Program F Input r) (q : Program F Input s) : | |
| (p.append q).cost = p.cost + q.cost := by | |
| induction q with | |
| | nil => simp [append, cost] | |
| | @step s q g ih => simp [append, ih, Nat.add_assoc] | |
| end Program | |
| inductive Expression (F Input : Type*) where | |
| | constant : F β Expression F Input | |
| | input : Input β Expression F Input | |
| | add : Expression F Input β Expression F Input β Expression F Input | |
| | sub : Expression F Input β Expression F Input β Expression F Input | |
| | mul : Expression F Input β Expression F Input β Expression F Input | |
| namespace Expression | |
| variable {Input : Type*} | |
| def eval (inputs : Input β F) : Expression F Input β F | |
| | .constant z => z | |
| | .input i => inputs i | |
| | .add e f => e.eval inputs + f.eval inputs | |
| | .sub e f => e.eval inputs - f.eval inputs | |
| | .mul e f => e.eval inputs * f.eval inputs | |
| def cost : Expression F Input β β | |
| | .constant _ => 0 | |
| | .input _ => 0 | |
| | .add e f => e.cost + f.cost + 1 | |
| | .sub e f => e.cost + f.cost + 1 | |
| | .mul e f => e.cost + f.cost + 1 | |
| structure Compiled (e : Expression F Input) where | |
| registers : β | |
| program : Program F Input registers | |
| output : Fin registers | |
| correct : β inputs, program.eval inputs output = e.eval inputs | |
| cost_eq : program.cost = e.cost | |
| def compile : (e : Expression F Input) β Compiled e | |
| | .constant z => | |
| { registers := 1 | |
| program := Program.nil.step (.constant z) | |
| output := 0 | |
| correct := fun _ => rfl | |
| cost_eq := rfl } | |
| | .input i => | |
| { registers := 1 | |
| program := Program.nil.step (.input i) | |
| output := 0 | |
| correct := fun _ => rfl | |
| cost_eq := rfl } | |
| | .add e f => | |
| let p := compile e | |
| let q := compile f | |
| { registers := p.registers + q.registers + 1 | |
| program := (p.program.append q.program).step | |
| (.add (Program.oldIndex _ _ p.output) (Program.newIndex _ _ q.output)) | |
| output := 0 | |
| correct := by | |
| intro inputs | |
| simp only [Program.eval_step_zero, Gate.eval, Program.eval_append_old, | |
| Program.eval_append_new, p.correct, q.correct, eval] | |
| cost_eq := by | |
| simp only [Program.cost_step, Program.cost_append, Gate.cost, p.cost_eq, q.cost_eq, cost] } | |
| | .sub e f => | |
| let p := compile e | |
| let q := compile f | |
| { registers := p.registers + q.registers + 1 | |
| program := (p.program.append q.program).step | |
| (.sub (Program.oldIndex _ _ p.output) (Program.newIndex _ _ q.output)) | |
| output := 0 | |
| correct := by | |
| intro inputs | |
| simp only [Program.eval_step_zero, Gate.eval, Program.eval_append_old, | |
| Program.eval_append_new, p.correct, q.correct, eval] | |
| cost_eq := by | |
| simp only [Program.cost_step, Program.cost_append, Gate.cost, p.cost_eq, q.cost_eq, cost] } | |
| | .mul e f => | |
| let p := compile e | |
| let q := compile f | |
| { registers := p.registers + q.registers + 1 | |
| program := (p.program.append q.program).step | |
| (.mul (Program.oldIndex _ _ p.output) (Program.newIndex _ _ q.output)) | |
| output := 0 | |
| correct := by | |
| intro inputs | |
| simp only [Program.eval_step_zero, Gate.eval, Program.eval_append_old, | |
| Program.eval_append_new, p.correct, q.correct, eval] | |
| cost_eq := by | |
| simp only [Program.cost_step, Program.cost_append, Gate.cost, p.cost_eq, q.cost_eq, cost] } | |
| end Expression | |
| end MatrixMultiplication.Arithmetic | |
| end | |
| end OAI | |