math / data /lean /OAI /Computability /BinPacking /Arithmetic /PackingRegisterFrame.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
11.1 kB
import Mathlib.Data.Fin.VecNotation
import Mathlib.Data.Fintype.Fin
import Mathlib.Data.Nat.Bits
import Mathlib.Logic.Embedding.Basic
import Mathlib.Tactic.FinCases
namespace OAI
namespace BinPackingGap.BinaryRegisterProgram
structure Operands (Reg : Type*) where
destination : Reg
left : Reg
right : Reg
left_ne_right : left β‰  right
left_ne_destination : left β‰  destination
right_ne_destination : right β‰  destination
variable {Reg K : Type*}
def addSlots (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) : Fin 7 β†ͺ K where
toFun i := slot (![Sum.inl op.left, Sum.inl op.right, Sum.inr 0, Sum.inr 1,
Sum.inr 2, Sum.inl op.destination, Sum.inr 3] i)
inj' := by
intro i j hij
have heq := slot.injective hij
fin_cases i <;> fin_cases j <;>
simp_all [op.left_ne_right, op.left_ne_destination, op.right_ne_destination,
Ne.symm op.left_ne_right, Ne.symm op.left_ne_destination,
Ne.symm op.right_ne_destination]
@[simp] theorem addSlots_zero (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) :
addSlots slot op 0 = slot (Sum.inl op.left) := rfl
@[simp] theorem addSlots_one (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) :
addSlots slot op 1 = slot (Sum.inl op.right) := rfl
@[simp] theorem addSlots_two (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) :
addSlots slot op 2 = slot (Sum.inr 0) := rfl
@[simp] theorem addSlots_three (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) :
addSlots slot op 3 = slot (Sum.inr 1) := rfl
@[simp] theorem addSlots_four (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) :
addSlots slot op 4 = slot (Sum.inr 2) := rfl
@[simp] theorem addSlots_five (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) :
addSlots slot op 5 = slot (Sum.inl op.destination) := rfl
@[simp] theorem addSlots_six (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) :
addSlots slot op 6 = slot (Sum.inr 3) := rfl
def mulSlots (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) : Fin 9 β†ͺ K where
toFun i := slot (![Sum.inl op.left, Sum.inl op.right, Sum.inl op.destination,
Sum.inr 0, Sum.inr 1, Sum.inr 2, Sum.inr 3, Sum.inr 4, Sum.inr 5] i)
inj' := by
intro i j hij
have heq := slot.injective hij
fin_cases i <;> fin_cases j <;>
simp_all [op.left_ne_right, op.left_ne_destination, op.right_ne_destination,
Ne.symm op.left_ne_right, Ne.symm op.left_ne_destination,
Ne.symm op.right_ne_destination]
@[simp] theorem mulSlots_zero (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) :
mulSlots slot op 0 = slot (Sum.inl op.left) := rfl
@[simp] theorem mulSlots_one (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) :
mulSlots slot op 1 = slot (Sum.inl op.right) := rfl
@[simp] theorem mulSlots_two (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) :
mulSlots slot op 2 = slot (Sum.inl op.destination) := rfl
@[simp] theorem mulSlots_three (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) :
mulSlots slot op 3 = slot (Sum.inr 0) := rfl
@[simp] theorem mulSlots_four (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) :
mulSlots slot op 4 = slot (Sum.inr 1) := rfl
@[simp] theorem mulSlots_five (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) :
mulSlots slot op 5 = slot (Sum.inr 2) := rfl
@[simp] theorem mulSlots_six (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) :
mulSlots slot op 6 = slot (Sum.inr 3) := rfl
@[simp] theorem mulSlots_seven (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) :
mulSlots slot op 7 = slot (Sum.inr 4) := rfl
@[simp] theorem mulSlots_eight (slot : (Reg βŠ• Fin 6) β†ͺ K) (op : Operands Reg) :
mulSlots slot op 8 = slot (Sum.inr 5) := rfl
def registerWords (values : Reg β†’ β„•) : Reg βŠ• Fin 6 β†’ List Bool
| Sum.inl r => (values r).bits
| Sum.inr _ => []
@[simp] theorem registerWords_reg (values : Reg β†’ β„•) (r : Reg) :
registerWords values (Sum.inl r) = (values r).bits := rfl
@[simp] theorem registerWords_scratch (values : Reg β†’ β„•) (i : Fin 6) :
registerWords values (Sum.inr i) = [] := rfl
noncomputable def registerTapes (slot : (Reg βŠ• Fin 6) β†ͺ K)
(base : K β†’ List Bool) (values : Reg β†’ β„•) : K β†’ List Bool := by
classical
exact fun k =>
if h : βˆƒ i, slot i = k then registerWords values (Classical.choose h) else base k
@[simp] theorem registerTapes_slot (slot : (Reg βŠ• Fin 6) β†ͺ K)
(base : K β†’ List Bool) (values : Reg β†’ β„•) (i : Reg βŠ• Fin 6) :
registerTapes slot base values (slot i) = registerWords values i := by
unfold registerTapes
rw [dite_eq_left ⟨i, rfl⟩]
congr 1
exact slot.injective (Classical.choose_spec (show βˆƒ j, slot j = slot i from ⟨i, rfl⟩))
@[simp] theorem registerTapes_reg (slot : (Reg βŠ• Fin 6) β†ͺ K)
(base : K β†’ List Bool) (values : Reg β†’ β„•) (r : Reg) :
registerTapes slot base values (slot (Sum.inl r)) = (values r).bits :=
registerTapes_slot slot base values (Sum.inl r)
@[simp] theorem registerTapes_scratch (slot : (Reg βŠ• Fin 6) β†ͺ K)
(base : K β†’ List Bool) (values : Reg β†’ β„•) (i : Fin 6) :
registerTapes slot base values (slot (Sum.inr i)) = [] :=
registerTapes_slot slot base values (Sum.inr i)
theorem registerTapes_other (slot : (Reg βŠ• Fin 6) β†ͺ K)
(base : K β†’ List Bool) (values : Reg β†’ β„•) (k : K)
(hk : Β¬ βˆƒ i, slot i = k) : registerTapes slot base values k = base k := by
simp [registerTapes, hk]
theorem registerTapes_update [DecidableEq Reg] [DecidableEq K]
(slot : (Reg βŠ• Fin 6) β†ͺ K) (base : K β†’ List Bool) (values : Reg β†’ β„•)
(d : Reg) (v : β„•) :
Function.update (registerTapes slot base values) (slot (Sum.inl d)) v.bits =
registerTapes slot base (Function.update values d v) := by
funext k
by_cases hk : βˆƒ j, slot j = k
· obtain ⟨j, rfl⟩ := hk
cases j with
| inl r =>
by_cases hrd : r = d
Β· subst r; simp
Β· have hslot : slot (Sum.inl r) β‰  slot (Sum.inl d) :=
slot.injective.ne (by simpa only [ne_eq, Sum.inl.injEq] using hrd)
simp [Function.update_of_ne, hrd, hslot]
| inr i =>
have hslot : slot (Sum.inr i) β‰  slot (Sum.inl d) := slot.injective.ne (by simp)
simp [Function.update_of_ne, hslot]
Β· have hslot : k β‰  slot (Sum.inl d) := fun h => hk ⟨Sum.inl d, h.symm⟩
simp [Function.update_of_ne hslot, registerTapes_other slot base _ k hk]
theorem registerTapes_mul_workspace (slot : (Reg βŠ• Fin 6) β†ͺ K)
(op : Operands Reg) (base : K β†’ List Bool) (values : Reg β†’ β„•)
(i : Fin 9) (hi : 3 ≀ i.val) :
registerTapes slot base values (mulSlots slot op i) = [] := by
fin_cases i <;> simp_all
theorem registerTapes_add_workspace (slot : (Reg βŠ• Fin 6) β†ͺ K)
(op : Operands Reg) (base : K β†’ List Bool) (values : Reg β†’ β„•)
(i : Fin 7) (hi : 2 ≀ i.val) (hout : i β‰  5) :
registerTapes slot base values (addSlots slot op i) = [] := by
fin_cases i <;> simp_all
end BinPackingGap.BinaryRegisterProgram
noncomputable section
namespace BinPackingGap.PackingRegisterFrame
open BinaryRegisterProgram
variable {Local Global K : Type}
def withScratch (register : Local β†ͺ Global) : (Local βŠ• Fin 6) β†ͺ (Global βŠ• Fin 6) where
toFun
| .inl r => .inl (register r)
| .inr i => .inr i
inj' := by
intro a b h
cases a <;> cases b <;> simp_all only [Sum.inl.injEq, Sum.inr.injEq,
Sum.inl_ne_inr, Sum.inr_ne_inl]
exact register.injective h
def localSlots (slot : (Global βŠ• Fin 6) β†ͺ K) (register : Local β†ͺ Global) :
(Local βŠ• Fin 6) β†ͺ K := (withScratch register).trans slot
@[simp] theorem localSlots_reg (slot : (Global βŠ• Fin 6) β†ͺ K)
(register : Local β†ͺ Global) (r : Local) :
localSlots slot register (.inl r) = slot (.inl (register r)) := rfl
@[simp] theorem localSlots_scratch (slot : (Global βŠ• Fin 6) β†ͺ K)
(register : Local β†ͺ Global) (i : Fin 6) :
localSlots slot register (.inr i) = slot (.inr i) := rfl
theorem registerTapes_eq_self (slot : (Local βŠ• Fin 6) β†ͺ K)
(base : K β†’ List Bool) (values : Local β†’ Nat)
(shape : βˆ€ i, base (slot i) = registerWords values i) :
registerTapes slot base values = base := by
funext k
by_cases hk : βˆƒ i, slot i = k
· obtain ⟨i, rfl⟩ := hk
rw [registerTapes_slot, shape]
Β· exact registerTapes_other slot base values k hk
theorem restrict (slot : (Global βŠ• Fin 6) β†ͺ K) (register : Local β†ͺ Global)
(base : K β†’ List Bool) (values : Global β†’ Nat) :
registerTapes (localSlots slot register) (registerTapes slot base values)
(values ∘ register) = registerTapes slot base values := by
apply registerTapes_eq_self
intro i
cases i <;> simp [Function.comp_def]
theorem replace (slot : (Global βŠ• Fin 6) β†ͺ K) (register : Local β†ͺ Global)
(base : K β†’ List Bool) (before after : Global β†’ Nat) (localAfter : Local β†’ Nat)
(inside : βˆ€ r, after (register r) = localAfter r)
(outside : βˆ€ r, (Β¬ βˆƒ l, register l = r) β†’ after r = before r) :
registerTapes (localSlots slot register) (registerTapes slot base before) localAfter =
registerTapes slot base after := by
funext k
by_cases hk : βˆƒ i, slot i = k
· obtain ⟨i, rfl⟩ := hk
cases i with
| inl r =>
by_cases hr : βˆƒ l, register l = r
· obtain ⟨l, rfl⟩ := hr
change registerTapes (localSlots slot register) _ localAfter
(localSlots slot register (.inl l)) = _
simp only [registerTapes_reg, inside]
Β· have hlocal : Β¬ βˆƒ i, localSlots slot register i = slot (.inl r) := by
rintro ⟨i, hi⟩
have h := slot.injective hi
cases i with
| inl l => exact hr ⟨l, Sum.inl.inj h⟩
| inr j => cases h
rw [registerTapes_other _ _ _ _ hlocal, registerTapes_reg,
registerTapes_reg, outside r hr]
| inr j =>
change registerTapes (localSlots slot register) _ localAfter
(localSlots slot register (.inr j)) = _
simp only [registerTapes_scratch]
Β· have hlocal : Β¬ βˆƒ i, localSlots slot register i = k := by
rintro ⟨i, hi⟩
exact hk ⟨withScratch register i, hi⟩
rw [registerTapes_other _ _ _ _ hlocal, registerTapes_other _ _ _ _ hk,
registerTapes_other _ _ _ _ hk]
theorem external_update [DecidableEq K] (slot : (Global βŠ• Fin 6) β†ͺ K) (base : K β†’ List Bool)
(values : Global β†’ Nat) (output : K) (outside : βˆ€ i, slot i β‰  output)
(word : List Bool) :
registerTapes slot (Function.update base output word) values =
Function.update (registerTapes slot base values) output word := by
funext k
by_cases hk : k = output
Β· subst k
have hn : Β¬ βˆƒ i, slot i = output := by rintro ⟨i, hi⟩; exact outside i hi
simp [registerTapes_other _ _ _ _ hn]
Β· by_cases hs : βˆƒ i, slot i = k
· obtain ⟨i, rfl⟩ := hs
simp [outside i]
Β· simp [registerTapes_other _ _ _ _ hs, hk]
end BinPackingGap.PackingRegisterFrame
end
end OAI