cfahlgren1/math / lean /OAI /Algorithms /SubexponentialSAT /ExecBranchControllerSupport.lean
cfahlgren1's picture
download
raw
17 kB
17
abbrev Regs := Fin 15 → Bool
18
abbrev Front := Fin 7 → List Bool
19
abbrev Bank := Fin 16 → List Bool
20
abbrev Data := State 15 23
22
def frontPort (i : Fin 7) : Fin 23 := Fin.castAdd 16 i
23
def bankPort (i : Fin 16) : Fin 23 := Fin.natAdd 7 i
25
@[simp] theorem frontPort_inj {i j : Fin 7} : frontPort i = frontPort j ↔ i = j :=
proof · 1 line
  Fin.castAdd_inj
27
@[simp] theorem bankPort_inj {i j : Fin 16} : bankPort i = bankPort j ↔ i = j := by
proof · 1 line
  simp [bankPort, Fin.ext_iff]
30
theorem bankPort_injective : Function.Injective bankPort := fun _ _ h => bankPort_inj.mp h
32
@[simp] theorem front_ne_bank (i : Fin 7) (j : Fin 16) : frontPort i ≠ bankPort j := by
proof · 4 lines
  intro h
  have he := congrArg Fin.val h
  change i.val = 7 + j.val at he
  omega
38
@[simp] theorem bank_ne_front (i : Fin 16) (j : Fin 7) : bankPort i ≠ frontPort j :=
proof · 1 line
  (front_ne_bank j i).symm
41
def state (regs : Regs) (front : Front) (bank : Bank) : Data :=
body · 1 line
  ⟨regs, Fin.addCases (m := 7) (n := 16) (motive := fun _ => List Bool) front bank⟩
44
@[simp] theorem state_regs (regs : Regs) (front : Front) (bank : Bank) :
    (state regs front bank).regs = regs := rfl
47
theorem state_stacks (regs regs' : Regs) (front : Front) (bank : Bank) :
    (state regs front bank).stacks = (state regs' front bank).stacks := rfl
50
def emptyBank : Bank := fun _ => []
51
def idle (regs : Regs) (front : Front) : Data := state regs front emptyBank
52
def wordBank (port : Fin 16) (bits : List Bool) : Bank := Function.update emptyBank port bits
54
@[simp] theorem state_front (regs : Regs) (front : Front) (bank : Bank) (i : Fin 7) :
    (state regs front bank).stacks (frontPort i) = front i := by
proof · 1 line
  simp [state, frontPort]
58
@[simp] theorem state_bank (regs : Regs) (front : Front) (bank : Bank) (i : Fin 16) :
    (state regs front bank).stacks (bankPort i) = bank i := by
proof · 1 line
  simp [state, bankPort]
62
theorem state_update_bank (regs : Regs) (front : Front) (bank : Bank)
    (i : Fin 16) (bits : List Bool) :
    state regs front (Function.update bank i bits) =
      ⟨regs, Function.update (state regs front bank).stacks (bankPort i) bits⟩ := by
proof · 9 lines
  apply congrArg₂ StructuredProgram.State.mk rfl
  funext j
  refine Fin.addCases (m := 7) (n := 16) (fun k => ?_) (fun k => ?_) j
  · change (state regs front (Function.update bank i bits)).stacks (frontPort k) =
      Function.update (state regs front bank).stacks (bankPort i) bits (frontPort k)
    simp
  · change (state regs front (Function.update bank i bits)).stacks (bankPort k) =
      Function.update (state regs front bank).stacks (bankPort i) bits (bankPort k)
    simp [Function.update_apply]
76
theorem state_update_front (regs : Regs) (front : Front) (bank : Bank)
    (i : Fin 7) (bits : List Bool) :
    state regs (Function.update front i bits) bank =
      ⟨regs, Function.update (state regs front bank).stacks (frontPort i) bits⟩ := by
proof · 9 lines
  apply congrArg₂ StructuredProgram.State.mk rfl
  funext j
  refine Fin.addCases (m := 7) (n := 16) (fun k => ?_) (fun k => ?_) j
  · change (state regs (Function.update front i bits) bank).stacks (frontPort k) =
      Function.update (state regs front bank).stacks (frontPort i) bits (frontPort k)
    simp [Function.update_apply]
  · change (state regs (Function.update front i bits) bank).stacks (bankPort k) =
      Function.update (state regs front bank).stacks (frontPort i) bits (bankPort k)
    simp []
90
def reset (regs : Regs) : Regs := readRegs regs 0 1 none
92
@[simp] theorem reset_reset (regs : Regs) : reset (reset regs) = reset regs :=
proof · 1 line
  StackRoutines.readRegs_reset _ _ _ _
95
abbrev copyInto (src : Fin 7) (dst tmp : Fin 16) : Cmd 15 23 :=
body · 1 line
  .call (StackRoutines.copyProgram (frontPort src) (bankPort dst) (bankPort tmp) 0 1)
98
theorem copyInto_eval (src : Fin 7) (dst tmp : Fin 16) (regs : Regs)
    (front : Front) (bank : Bank) (hne : tmp ≠ dst)
    (hdst : bank dst = []) (htmp : bank tmp = []) :
    Eval (copyInto src dst tmp) (7 * (front src).length + 5)
      (state regs front bank)
      (state (reset regs) front (Function.update bank dst (front src))) := by
proof · 9 lines
  have h := ListRegisterProgram.copy_run (frontPort src) (bankPort dst) (bankPort tmp)
    (0 : Fin 15) 1 (front_ne_bank _ _) (front_ne_bank _ _)
    (fun h => hne (bankPort_inj.mp h)) (by decide)
    regs (state regs front bank).stacks (by simpa using htmp)
  rw [state_update_bank]
  apply Eval.call
  simpa [ListRegisterProgram.Run, StructuredProgram.State.withPC,
    StackRoutines.copyStacks, hdst, reset, state_front, state_bank,
    state_stacks regs (reset regs)] using h
114
abbrev load : Cmd 15 23 :=
body · 1 line
  .seq (copyInto 5 9 0) (.seq (copyInto 6 12 0) (copyInto 4 13 0))
117
def loadCost (front : Front) : ℕ :=
body · 1 line
  7 * ((front 4).length + (front 5).length + (front 6).length) + 15
120
def loaded (regs : Regs) (front : Front) : State 15 16 :=
body · 1 line
  ExecClauseFiltersProgram.state regs (front 5) [] (front 6) (front 4) [] []
123
theorem load_eval (regs : Regs) (front : Front) :
    Eval load (loadCost front) (idle regs front)
      (state (reset regs) front (loaded (reset regs) front).stacks) := by
proof · 17 lines
  let b1 : Bank := Function.update emptyBank 9 (front 5)
  let b2 : Bank := Function.update b1 12 (front 6)
  let b3 : Bank := Function.update b2 13 (front 4)
  have h1 := copyInto_eval 5 9 0 regs front emptyBank (by decide) rfl rfl
  have h2 := copyInto_eval 6 12 0 (reset regs) front b1 (by decide)
    (by simp [b1, emptyBank]) (by simp [b1, emptyBank])
  have h3 := copyInto_eval 4 13 0 (reset (reset regs)) front b2 (by decide)
    (by simp [b2, b1, emptyBank]) (by simp [b2, b1, emptyBank])
  have hb : b3 = (loaded (reset regs) front).stacks := by
    funext i
    fin_cases i <;> simp [b3, b2, b1, emptyBank, loaded, ExecClauseFiltersProgram.state]
  have h := h1.seq (h2.seq h3)
  change Eval load _ (idle regs front) (state _ front b3) at h
  rw [hb, reset_reset, reset_reset] at h
  convert h using 1
  unfold loadCost
  omega
144
theorem embed_child (frameRegs : Regs) (front : Front) (frameBank : Bank)
    (child : State 15 16) :
    embedState id bankPort (state frameRegs front frameBank) child =
      state child.regs front child.stacks := by
proof · 9 lines
  apply embedState_eq_of_ports id bankPort _ _ _ Function.injective_id bankPort_injective
  · intro i; rfl
  · intro i hi; exact (hi ⟨i, rfl⟩).elim
  · intro i; simp
  · intro i
    refine Fin.addCases (m := 7) (n := 16) (fun j hi => ?_) (fun j hi => ?_) i
    · exact (state_front child.regs front child.stacks j).trans
        (state_front frameRegs front frameBank j).symm
    · exact (hi ⟨j, rfl⟩).elim
158
abbrev callChild {l : ℕ} (p : Program 15 16 l) : Cmd 15 23 :=
body · 1 line
  .call (ProgramLinking.liftProgram p id bankPort)
161
theorem callChild_eval {l : ℕ} (p : Program 15 16 l) (front : Front)
    {n : ℕ} {a b : State 15 16}
    (h : Trace p n (a.withPC (some p.entry)) (b.withPC none)) :
    Eval (callChild p) n (state a.regs front a.stacks) (state b.regs front b.stacks) := by
proof · 3 lines
  have he := eval_call_lifted p id bankPort (state a.regs front a.stacks)
    Function.injective_id bankPort_injective h
  simpa only [embed_child, callChild] using he
169
def outputPort (i : Fin 2) : Fin 7 := ⟨2 + i.val, by omega⟩
171
@[simp] theorem outputPort_ne_zero (i : Fin 2) : outputPort i ≠ 0 := by
proof · 4 lines
  intro h
  have he := congrArg Fin.val h
  change 2 + i.val = 0 at he
  omega
176
@[simp] theorem outputPort_ne_one (i : Fin 2) : outputPort i ≠ 1 := by
proof · 4 lines
  intro h
  have he := congrArg Fin.val h
  change 2 + i.val = 1 at he
  omega
182
def emitReg (i : Fin 3) : Fin 15 := Fin.castAdd 12 i
183
def emitPort (i : Fin 2) : Fin 4 → Fin 23 :=
body · 1 line
  ![frontPort 0, frontPort 1, frontPort (outputPort i), bankPort 0]
186
theorem emitReg_injective : Function.Injective emitReg := Fin.castAdd_injective 3 12
187
theorem emitPort_injective (i : Fin 2) : Function.Injective (emitPort i) := by
proof · 1 line
  fin_cases i <;> decide
190
abbrev emitInto (i : Fin 2) : Cmd 15 23 :=
body · 1 line
  .call (ProgramLinking.liftProgram SubexpNormalizeProgram.emitRecordProgram emitReg (emitPort i))
193
def emittedRegs (regs : Regs) : Regs := Function.update (Function.update regs 0 true) 1 false
195
def emitAfter (i : Fin 2) (bits : List Bool) (d : Data) : Data :=
body · 2 lines
  ⟨emittedRegs d.regs, Function.update (Function.update d.stacks (bankPort 0) [])
    (frontPort (outputPort i)) (ExecWorklistProgram.record bits ++ d.stacks (frontPort (outputPort i)))⟩
199
theorem emitInto_eval (i : Fin 2) (bits : List Bool) (d : Data)
    (hpayload : d.stacks (bankPort 0) = bits.reverse) :
    Eval (emitInto i) (5 * bits.length + 7) d (emitAfter i bits d) := by
proof · 37 lines
  let a := SubexpNormalizeProgram.filterState (d.regs 0) (d.regs 1) (d.regs 2)
    (d.stacks (frontPort 0)) (d.stacks (frontPort 1))
    (d.stacks (frontPort (outputPort i))) bits.reverse
  let b := SubexpNormalizeProgram.filterState true false (d.regs 2)
    (d.stacks (frontPort 0)) (d.stacks (frontPort 1))
    (ExecWorklistProgram.record bits ++ d.stacks (frontPort (outputPort i))) []
  have ha : embedState emitReg (emitPort i) d a = d := by
    apply embedState_eq_of_ports _ _ _ _ _ emitReg_injective (emitPort_injective i)
    · intro j; fin_cases j <;> rfl
    · intro j hj; rfl
    · intro j; fin_cases j <;> simp [a, emitPort, SubexpNormalizeProgram.filterState,
        SubexpPostfixControl.quadruple, hpayload]
    · intro j hj; rfl
  have hb : embedState emitReg (emitPort i) d b = emitAfter i bits d := by
    apply embedState_eq_of_ports _ _ _ _ _ emitReg_injective (emitPort_injective i)
    · intro j
      fin_cases j <;> simp [b, emitReg, emitAfter, emittedRegs, SubexpNormalizeProgram.filterState,
        SubexpPostfixControl.triple]
    · intro j hj
      have h0 : j ≠ 0 := fun h => hj ⟨0, h.symm⟩
      have h1 : j ≠ 1 := fun h => hj ⟨1, h.symm⟩
      simp [emitAfter, emittedRegs, h0, h1]
    · intro j
      fin_cases j <;> simp [b, emitPort, emitAfter, SubexpNormalizeProgram.filterState,
        SubexpPostfixControl.quadruple, Ne.symm (outputPort_ne_zero i), Ne.symm (outputPort_ne_one i)]
    · intro j hj
      have ho : j ≠ frontPort (outputPort i) := fun h => hj ⟨2, h.symm⟩
      have hp : j ≠ bankPort 0 := fun h => hj ⟨3, h.symm⟩
      simp [emitAfter, ho, hp]
  have ht := SubexpNormalizeProgram.emitRecord_trace bits
    (d.stacks (frontPort 0)) (d.stacks (frontPort 1))
    (d.stacks (frontPort (outputPort i))) (d.regs 0) (d.regs 1) (d.regs 2)
  have he := eval_call_lifted SubexpNormalizeProgram.emitRecordProgram emitReg (emitPort i) d
    emitReg_injective (emitPort_injective i) ht
  change Eval (emitInto i) _ (embedState emitReg (emitPort i) d a)
    (embedState emitReg (emitPort i) d b) at he
  simpa only [ha, hb] using he
240
abbrev toPayload : Cmd 15 23 :=
body · 1 line
  .call (StackRoutines.transferProgram (bankPort 13) (bankPort 0) 0 1)
243
theorem toPayload_eval (regs : Regs) (front : Front) (bits : List Bool) :
    Eval toPayload (3 * bits.length + 3)
      (state regs front (wordBank 13 bits))
      (state (reset regs) front (wordBank 0 bits.reverse)) := by
proof · 18 lines
  let d := state regs front (wordBank 13 bits)
  have hs : StackRoutines.transferStacks (bankPort 13) (bankPort 0) d.stacks =
      (state (reset regs) front (wordBank 0 bits.reverse)).stacks := by
    funext j
    refine Fin.addCases (m := 7) (n := 16) (fun k => ?_) (fun k => ?_) j
    · change StackRoutines.transferStacks (bankPort 13) (bankPort 0) d.stacks (frontPort k) =
        (state (reset regs) front (wordBank 0 bits.reverse)).stacks (frontPort k)
      simp [StackRoutines.transferStacks, d, wordBank, emptyBank]
    · change StackRoutines.transferStacks (bankPort 13) (bankPort 0) d.stacks (bankPort k) =
        (state (reset regs) front (wordBank 0 bits.reverse)).stacks (bankPort k)
      fin_cases k <;> simp [StackRoutines.transferStacks, d, wordBank, emptyBank]
  have h := ListRegisterProgram.transfer_run (bankPort 13) (bankPort 0) (0 : Fin 15) 1
    (by decide) (by decide) regs d.stacks
  rw [state_bank] at h
  rw [hs] at h
  apply Eval.call
  simpa [ListRegisterProgram.Run, StructuredProgram.State.withPC, d, wordBank,
    emptyBank, reset, state_regs] using h
266
abbrev frameChild (i : Fin 2) : Cmd 15 23 := .seq toPayload (emitInto i)
268
theorem frameChild_eval (i : Fin 2) (regs : Regs) (front : Front) (bits : List Bool) :
    Eval (frameChild i) (8 * bits.length + 10)
      (state regs front (wordBank 13 bits))
      (idle (emittedRegs (reset regs)) (Function.update front (outputPort i)
        (ExecWorklistProgram.record bits ++ front (outputPort i)))) := by
proof · 20 lines
  let d := state (reset regs) front (wordBank 0 bits.reverse)
  have he := emitInto_eval i bits d (by simp [d, wordBank])
  have hfinal : emitAfter i bits d =
      idle (emittedRegs (reset regs)) (Function.update front (outputPort i)
        (ExecWorklistProgram.record bits ++ front (outputPort i))) := by
    apply congrArg₂ StructuredProgram.State.mk rfl
    funext j
    refine Fin.addCases (m := 7) (n := 16) (fun k => ?_) (fun k => ?_) j
    · change (emitAfter i bits d).stacks (frontPort k) =
        (idle (emittedRegs (reset regs)) (Function.update front (outputPort i)
          (ExecWorklistProgram.record bits ++ front (outputPort i)))).stacks (frontPort k)
      simp [emitAfter, d, idle, Function.update_apply]
    · change (emitAfter i bits d).stacks (bankPort k) =
        (idle (emittedRegs (reset regs)) (Function.update front (outputPort i)
          (ExecWorklistProgram.record bits ++ front (outputPort i)))).stacks (bankPort k)
      by_cases hk : k = 0 <;> simp [emitAfter, d, idle, wordBank, emptyBank, hk]
  rw [hfinal] at he
  have h := (toPayload_eval regs front bits).seq he
  convert h using 1
  omega
294
abbrev buildChild {l : ℕ} (p : Program 15 16 l) (i : Fin 2) : Cmd 15 23 :=
body · 1 line
  .seq load (.seq (callChild p) (frameChild i))

Compose a measured execution of a concrete child table with the actual input copies and frame writer. This lemma only links real instruction traces.

299
theorem buildChild_eval {l : ℕ} (p : Program 15 16 l) (i : Fin 2)
    (regs : Regs) (front : Front) (bits : List Bool) (rf : Regs) {n : ℕ}
    (h : Trace p n ((loaded (reset regs) front).withPC (some p.entry))
      ((ExecClauseFiltersProgram.state rf [] [] [] bits [] []).withPC none)) :
    Eval (buildChild p i) (loadCost front + (n + (8 * bits.length + 10)))
      (idle regs front)
      (idle (emittedRegs (reset rf)) (Function.update front (outputPort i)
        (ExecWorklistProgram.record bits ++ front (outputPort i)))) := by
proof · 7 lines
  have hc := callChild_eval p front h
  have hbank : (ExecClauseFiltersProgram.state rf [] [] [] bits [] []).stacks =
      wordBank 13 bits := by
    funext j
    fin_cases j <;> simp [ExecClauseFiltersProgram.state, wordBank, emptyBank]
  rw [hbank] at hc
  exact (load_eval regs front).seq (hc.seq (frameChild_eval i rf front bits))
315
abbrev clearFront (i : Fin 7) : Cmd 15 23 :=
body · 1 line
  .call (StackRoutines.clearProgram (frontPort i) 0 1)
318
theorem clearFront_eval (i : Fin 7) (regs : Regs) (front : Front) :
    Eval (clearFront i) (2 * (front i).length + 3) (idle regs front)
      (idle (reset regs) (Function.update front i [])) := by
proof · 7 lines
  have h := ListRegisterProgram.clear_run (frontPort i) (0 : Fin 15) 1 regs (idle regs front).stacks
  rw [show (idle regs front).stacks (frontPort i) = front i by simp [idle]] at h
  change Eval _ _ _ (state (reset regs) (Function.update front i []) emptyBank)
  rw [state_update_front]
  apply Eval.call
  simpa [ListRegisterProgram.Run, StructuredProgram.State.withPC, StackRoutines.clearStacks,
    reset, idle, state_regs, state_stacks regs (reset regs)] using h
329
abbrev cleanInputs : Cmd 15 23 := .seq (clearFront 4) (.seq (clearFront 5) (clearFront 6))
330
def cleanFront (front : Front) : Front :=
body · 1 line
  Function.update (Function.update (Function.update front 4 []) 5 []) 6 []
333
theorem cleanInputs_eval (regs : Regs) (front : Front) :
    Eval cleanInputs (2 * ((front 4).length + (front 5).length + (front 6).length) + 9)
      (idle regs front) (idle (reset regs) (cleanFront front)) := by
proof · 10 lines
  have h1 := clearFront_eval 4 regs front
  have h2 := clearFront_eval 5 (reset regs) (Function.update front 4 [])
  have h3 := clearFront_eval 6 (reset (reset regs)) (Function.update (Function.update front 4 []) 5 [])
  have h := h1.seq (h2.seq h3)
  simp only [Function.update_of_ne (by decide : (5 : Fin 7) ≠ 4),
    Function.update_of_ne (by decide : (6 : Fin 7) ≠ 5),
    Function.update_of_ne (by decide : (6 : Fin 7) ≠ 4), reset_reset] at h
  convert h using 1
  · omega
  · rfl

Xet Storage Details

Size:
17 kB
·
Xet hash:
0d45865bf96a82aa656e45da39ad97b35c17e8fb3a89efd7375dd5b55af7724b

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.