File size: 9,941 Bytes
4a56cd6 | 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 196 197 | import OAI.Algorithms.SubexponentialSAT.LeanCompat
import OAI.Algorithms.SubexponentialSAT.ArrayUpdatePrograms
import OAI.Algorithms.SubexponentialSAT.Subexp.TupleProgram
namespace OAI.Algorithms
/-!
# A temporary candidate assignment with fixed control
The current assignment, candidate word, processed-position counter, and width
are preserved. The program copies the assignment and replaces precisely the
processed word in that copy. No restore of the original assignment is needed.
-/
namespace SubexponentialSAT.Subexp.CandidateArrayProgram
open BitProgram StructuredCalls StackRoutines
abbrev Data := State 2 10
abbrev state (regs : Fin 2 → Bool) (original candidate index width work indexWork : List Bool) : Data :=
⟨regs, ![original, candidate, index, width, [], [], [], [], work, indexWork]⟩
def replacePorts : Fin 8 → Fin 10 := ![8, 1, 9, 3, 4, 5, 6, 7]
theorem replacePorts_injective : Function.Injective replacePorts := by
intro i j h
fin_cases i <;> fin_cases j <;> simp_all [replacePorts]
abbrev replace : Cmd 2 10 := .call (ProgramLinking.liftProgram
(ArrayUpdatePrograms.replaceProgram (0 : Fin 2) 1) id replacePorts)
abbrev command : Cmd 2 10 :=
.seq (.call (copyProgram 0 8 4 (0 : Fin 2) 1))
(.seq (.call (copyProgram 2 9 4 (0 : Fin 2) 1)) replace)
abbrev program : Program 2 10 (size command + 1) := compiled command 0 8
private theorem state_ext {a b : Data} (hr : a.regs = b.regs)
(hs : a.stacks = b.stacks) : a = b := by
cases a; cases b; cases hr; cases hs; rfl
private theorem embed_replace_eq (frame target : Data) (inner : State 2 8)
(hr : ∀ i, target.regs i = inner.regs i)
(hs : ∀ i, target.stacks (replacePorts i) = inner.stacks i)
(hf : ∀ j, (¬ ∃ i, replacePorts i = j) → target.stacks j = frame.stacks j) :
embedState id replacePorts frame inner = target := by
apply state_ext
· funext j
exact (show (embedState id replacePorts frame inner).regs j = inner.regs j by
simpa only [id_eq] using
embedState_register id replacePorts frame inner Function.injective_id j).trans (hr j).symm
· funext j
by_cases hj : ∃ i, replacePorts i = j
· obtain ⟨i, rfl⟩ := hj
exact (embedState_stack id replacePorts frame inner replacePorts_injective i).trans (hs i).symm
· exact (embedState_stack_frame id replacePorts frame inner j hj).trans (hf j hj).symm
theorem copy_array_eval (regs : Fin 2 → Bool)
(original candidate index width : List Bool) :
Eval (.call (copyProgram 0 8 4 (0 : Fin 2) 1)) (7 * original.length + 5)
(state regs original candidate index width [] [])
(state (readRegs regs 0 1 none) original candidate index width original []) := by
have h := TupleProgram.copy_eval (0 : Fin 10) 8 4 (0 : Fin 2) 1
(by decide) (by decide) (by decide) (by decide)
(state regs original candidate index width [] []) (by rfl)
have he : (⟨readRegs (state regs original candidate index width [] []).regs 0 1 none,
copyStacks 0 8 (state regs original candidate index width [] []).stacks⟩ : Data) =
state (readRegs regs 0 1 none) original candidate index width original [] := by
apply state_ext
· rfl
· funext j; fin_cases j <;> simp [ copyStacks]
rw [he] at h
simpa [state] using h
theorem copy_index_eval (regs : Fin 2 → Bool)
(original candidate index width work : List Bool) :
Eval (.call (copyProgram 2 9 4 (0 : Fin 2) 1)) (7 * index.length + 5)
(state regs original candidate index width work [])
(state (readRegs regs 0 1 none) original candidate index width work index) := by
have h := TupleProgram.copy_eval (2 : Fin 10) 9 4 (0 : Fin 2) 1
(by decide) (by decide) (by decide) (by decide)
(state regs original candidate index width work []) (by rfl)
have he : (⟨readRegs (state regs original candidate index width work []).regs 0 1 none,
copyStacks 2 9 (state regs original candidate index width work []).stacks⟩ : Data) =
state (readRegs regs 0 1 none) original candidate index width work index := by
apply state_ext
· rfl
· funext j; fin_cases j <;> simp [ copyStacks]
rw [he] at h
simpa [state] using h
theorem replace_eval (regs : Fin 2 → Bool) (original : List Bool)
(before : List (List Bool)) (old candidate rest index width : List Bool)
(hi : index.length = before.length)
(hrows : ∀ row ∈ before, row.length = width.length)
(ho : old.length = width.length) (hc : candidate.length = width.length) :
Eval replace (before.length * (14 * width.length + 10) + 20 * width.length + 21)
(state regs original candidate index width (before.flatten ++ (old ++ rest)) index)
(state (readRegs regs 0 1 none) original candidate index width
(before.flatten ++ (candidate ++ rest)) []) := by
let inner : State 2 8 := ⟨regs,
![before.flatten ++ (old ++ rest), candidate, index, width, [], [], [], []]⟩
let base := state regs original candidate index width (before.flatten ++ (old ++ rest)) index
have ht := ArrayUpdatePrograms.replaceProgram_trace (0 : Fin 2) 1 (by decide)
before old candidate rest width index regs inner.stacks
rfl rfl rfl rfl hi hrows ho hc rfl rfl rfl rfl
change Trace (ArrayUpdatePrograms.replaceProgram (0 : Fin 2) 1) _
(inner.withPC (some (ArrayUpdatePrograms.replaceProgram (0 : Fin 2) 1).entry))
((⟨readRegs regs 0 1 none,
ArrayUpdatePrograms.replaceStacks before candidate rest inner.stacks⟩ : State 2 8).withPC none) at ht
have he := eval_call_lifted (ArrayUpdatePrograms.replaceProgram (0 : Fin 2) 1)
id replacePorts base Function.injective_id replacePorts_injective ht
have hin : embedState id replacePorts base inner = base := by
apply embed_replace_eq
· intro j; rfl
· intro j; fin_cases j <;> rfl
· intro j hj; rfl
have hout : embedState id replacePorts base
(⟨readRegs regs 0 1 none,
ArrayUpdatePrograms.replaceStacks before candidate rest inner.stacks⟩ : State 2 8) =
state (readRegs regs 0 1 none) original candidate index width
(before.flatten ++ (candidate ++ rest)) [] := by
apply embed_replace_eq
· intro j; rfl
· intro j; fin_cases j <;> simp [replacePorts, inner, ArrayUpdatePrograms.replaceStacks]
· intro j hj
fin_cases j <;> simp_all [replacePorts, base, inner, state, Fin.exists_fin_succ]
simpa only [hin, hout, replace, inner, base] using he
def sourceCost (originalSize position width : ℕ) : ℕ :=
7 * originalSize + 7 * position + position * (14 * width + 10) + 20 * width + 32
theorem command_eval (regs : Fin 2 → Bool) (before : List (List Bool))
(old candidate rest index width : List Bool)
(hi : index.length = before.length)
(hrows : ∀ row ∈ before, row.length = width.length)
(ho : old.length = width.length) (hc : candidate.length = width.length) :
let original := before.flatten ++ (old ++ rest)
Eval command (sourceCost original.length before.length width.length - 1)
(state regs original candidate index width [] [])
(state (readRegs regs 0 1 none) original candidate index width
(before.flatten ++ (candidate ++ rest)) []) := by
dsimp only
have h := (copy_array_eval regs (before.flatten ++ (old ++ rest)) candidate index width).seq
((copy_index_eval (readRegs regs 0 1 none)
(before.flatten ++ (old ++ rest)) candidate index width
(before.flatten ++ (old ++ rest))).seq
(replace_eval (readRegs (readRegs regs 0 1 none) 0 1 none)
(before.flatten ++ (old ++ rest)) before old candidate rest index width hi hrows ho hc))
convert h using 1 <;> simp [ sourceCost, hi, readRegs_reset] ; omega
theorem program_trace (regs : Fin 2 → Bool) (before : List (List Bool))
(old candidate rest index width : List Bool)
(hi : index.length = before.length)
(hrows : ∀ row ∈ before, row.length = width.length)
(ho : old.length = width.length) (hc : candidate.length = width.length) :
let original := before.flatten ++ (old ++ rest)
Trace program (sourceCost original.length before.length width.length)
((state regs original candidate index width [] []).withPC (some program.entry))
((state (readRegs regs 0 1 none) original candidate index width
(before.flatten ++ (candidate ++ rest)) []).withPC none) := by
dsimp only
have ht := compiled_halt_trace
(command_eval regs before old candidate rest index width hi hrows ho hc) 0 8
have hp : 0 < sourceCost (before.flatten ++ (old ++ rest)).length before.length width.length := by
simp [sourceCost]
have htime : sourceCost (before.flatten ++ (old ++ rest)).length
before.length width.length - 1 + 1 =
sourceCost (before.flatten ++ (old ++ rest)).length before.length width.length :=
Nat.sub_add_cancel hp
rw [htime] at ht
exact ht
theorem sourceCost_polynomial (D w : ℕ) :
sourceCost (D * w) D w ≤ 40 * (D + 1) * (w + 1) := by
unfold sourceCost
nlinarith
theorem program_primitive (regs : Fin 2 → Bool) (before : List (List Bool))
(old candidate rest index width : List Bool)
(hi : index.length = before.length)
(hrows : ∀ row ∈ before, row.length = width.length)
(ho : old.length = width.length) (hc : candidate.length = width.length) :
let original := before.flatten ++ (old ++ rest)
Computation.RunsIn (primitive program).step
(primitiveOverhead program * sourceCost original.length before.length width.length)
(primitiveCfg program ((state regs original candidate index width [] []).withPC
(some program.entry)))
(primitiveCfg program ((state (readRegs regs 0 1 none) original candidate index width
(before.flatten ++ (candidate ++ rest)) []).withPC none)) := by
exact primitive_trace program (program_trace regs before old candidate rest index width hi hrows ho hc)
end SubexponentialSAT.Subexp.CandidateArrayProgram
end OAI.Algorithms
|