File size: 6,186 Bytes
cdec422 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | import Mathlib
import OAI.LinearAlgebra.MatrixFields.Arithmetic.ArithmeticComplexity
namespace OAI
namespace MatrixAllFields
open scoped BigOperators Topology Polynomial
section
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
end MatrixAllFields
end OAI
|