math / data /lean /OAI /Algorithms /SubexponentialSAT /DensePolynomialLoop.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
12.3 kB
import OAI.Algorithms.SubexponentialSAT.LeanCompat
import OAI.Algorithms.SubexponentialSAT.SubexpScanPrimitives
import OAI.Algorithms.SubexponentialSAT.SubexpQuarticRing
namespace OAI.Algorithms
/-! A fixed streaming array loop, parameterized by a finite word program
and an actual trace theorem for one word. The concrete modular-word
instantiation is supplied in DensePolynomialPrograms. -/
namespace SubexponentialSAT.DensePolynomialLoop
open BitProgram StructuredCalls SubexpScanPrimitives
open SubexpLiftRing SubexpQuarticRing
abbrev Data := State 8 20
def stackArray (x y modulus left right width counter tmp packed : List Bool)
(frame : Fin 6 β†’ List Bool) : Fin 20 β†’ List Bool :=
fun i => Fin.addCases (m := 14) (n := 6) (motive := fun _ => List Bool)
(![x,y,[],[],[],modulus,[],[],left,right,width,counter,tmp,packed] : Fin 14 β†’ List Bool) frame i
def state (regs : Fin 8 β†’ Bool)
(x y modulus left right width counter tmp packed : List Bool)
(frame : Fin 6 β†’ List Bool) : Data :=
⟨regs, stackArray x y modulus left right width counter tmp packed frame⟩
def ready (regs : Fin 8 β†’ Bool) (modulus left right width packed : List Bool)
(frame : Fin 6 β†’ List Bool) : Data :=
state regs [] [] modulus left right width [] [] packed frame
abbrev guard : Cmd 8 20 :=
.seq (.base (.atom (.peek 8 0 1)))
(.seq (.base (.atom (.peek 9 2 3))) (.base (.atom (.band 4 1 3))))
def guardRegs (regs : Fin 8 β†’ Bool) (left right : List Bool) : Fin 8 β†’ Bool :=
let r := readRegs (readRegs regs 0 1 left.head?) 2 3 right.head?
Function.update r 4 (r 1 && r 3)
@[simp] theorem guardRegs_test (regs : Fin 8 β†’ Bool) (left right : List Bool) :
guardRegs regs left right 4 = (left.head?.isSome && right.head?.isSome) := by
simp [guardRegs, readRegs]
theorem guard_eval (regs : Fin 8 β†’ Bool) (modulus left right width packed : List Bool)
(frame : Fin 6 β†’ List Bool) :
Eval guard 3 (ready regs modulus left right width packed frame)
(ready (guardRegs regs left right) modulus left right width packed frame) := by
let d := ready regs modulus left right width packed frame
have h₁ := Eval.base (StructuredProgram.Eval.atom (r := 8) (s := 20) (.peek 8 0 1) d)
have hβ‚‚ := Eval.base (StructuredProgram.Eval.atom (r := 8) (s := 20) (.peek 9 2 3)
((StructuredProgram.Atom.peek 8 0 1).eval d))
have h₃ := Eval.base (StructuredProgram.Eval.atom (r := 8) (s := 20) (.band 4 1 3)
((StructuredProgram.Atom.peek 9 2 3).eval ((StructuredProgram.Atom.peek 8 0 1).eval d)))
exact Eval.seq h₁ (Eval.seq hβ‚‚ h₃)
abbrev addLoop (step : Cmd 8 20) : Cmd 8 20 := .while 4 (.seq step guard)
abbrev flush : Cmd 8 20 :=
.seq (.call (StackRoutines.transferProgram 8 13 (0 : Fin 8) 1))
(.seq (.call (StackRoutines.transferProgram 9 13 (0 : Fin 8) 1))
(.call (StackRoutines.transferProgram 13 8 (0 : Fin 8) 1)))
abbrev addCmd (step : Cmd 8 20) : Cmd 8 20 := .seq guard (.seq (addLoop step) flush)
abbrev program (step : Cmd 8 20) : Program 8 20 (StructuredCalls.size (addCmd step) + 1) :=
StructuredCalls.compiled (addCmd step) 8 8
theorem flush_eval (regs : Fin 8 β†’ Bool) (modulus left right width packed : List Bool)
(frame : Fin 6 β†’ List Bool) :
Eval flush (6 * left.length + 6 * right.length + 3 * packed.length + 9)
(ready regs modulus left right width packed frame)
(ready (readRegs regs 0 1 none) modulus (packed.reverse ++ left ++ right) [] width [] frame) := by
let dβ‚€ := ready regs modulus left right width packed frame
let d₁ := ready (readRegs regs 0 1 none) modulus [] right width (left.reverse ++ packed) frame
let dβ‚‚ := ready (readRegs regs 0 1 none) modulus [] [] width
(right.reverse ++ left.reverse ++ packed) frame
have h₁ := transfer_eval dβ‚€ 8 13 0 1 (by decide) (by decide)
have e₁ : afterTransfer dβ‚€ 8 13 0 1 = d₁ := by
apply state_ext
Β· rfl
Β· funext i; fin_cases i <;>
simp [afterTransfer, dβ‚€, d₁, ready, state, stackArray, Fin.addCases, Fin.coe_ofNat_eq_mod,
StackRoutines.transferStacks]
have hβ‚‚ := transfer_eval d₁ 9 13 0 1 (by decide) (by decide)
have eβ‚‚ : afterTransfer d₁ 9 13 0 1 = dβ‚‚ := by
apply state_ext
Β· simp [afterTransfer, d₁, dβ‚‚, ready, state, StackRoutines.readRegs_reset]
Β· funext i; fin_cases i <;>
simp [afterTransfer, d₁, dβ‚‚, ready, state, stackArray, Fin.addCases, Fin.coe_ofNat_eq_mod,
StackRoutines.transferStacks,
List.append_assoc]
have h₃ := transfer_eval dβ‚‚ 13 8 0 1 (by decide) (by decide)
have e₃ : afterTransfer dβ‚‚ 13 8 0 1 =
ready (readRegs regs 0 1 none) modulus (packed.reverse ++ left ++ right) [] width [] frame := by
apply state_ext
Β· simp [afterTransfer, dβ‚‚, ready, state, StackRoutines.readRegs_reset]
Β· funext i; fin_cases i <;>
simp [afterTransfer, dβ‚‚, ready, state, stackArray, Fin.addCases, Fin.coe_ofNat_eq_mod,
StackRoutines.transferStacks,
List.reverse_append, List.append_assoc]
rw [e₁] at h₁
rw [eβ‚‚] at hβ‚‚
rw [e₃] at h₃
have h := Eval.seq h₁ (Eval.seq hβ‚‚ h₃)
convert h using 1
all_goals simp [ dβ‚€, d₁, dβ‚‚, ready, state, stackArray, Fin.addCases,
Fin.coe_ofNat_eq_mod, List.length_append]
omega
def flatten {B : β„•} (xs : List (Word B)) : List Bool := (xs.map Subtype.val).flatten
@[simp] theorem flatten_length {B : β„•} (xs : List (Word B)) :
(flatten xs).length = xs.length * B := by
induction xs with
| nil => simp [flatten]
| cons x xs ih =>
change (x.val ++ flatten xs).length = (xs.length + 1) * B
rw [List.length_append, x.property, ih]
ring
theorem flatten_cons_ne_nil {B : β„•} (hB : 0 < B) (x : Word B) (xs : List (Word B)) :
flatten (x :: xs) β‰  [] := by
intro h
have hh := congrArg List.length h
rw [flatten_length] at hh
simp only [List.length_cons, List.length_nil] at hh
nlinarith
theorem head_isSome_of_ne_nil {Ξ± : Type*} {xs : List Ξ±} (h : xs β‰  []) : xs.head?.isSome = true := by
cases xs with
| nil => exact (h rfl).elim
| cons a xs => rfl
structure WordStep (B : β„•) where
cmd : Cmd 8 20
modulus : List Bool
add : Word B β†’ Word B β†’ Word B
cost : β„•
run : βˆ€ (regs : Fin 8 β†’ Bool) (x y : Word B)
(left right width packed : List Bool) (frame : Fin 6 β†’ List Bool),
width.length = B β†’ βˆƒ n ≀ cost, βˆƒ regs',
Eval cmd n (ready regs modulus (x.val ++ left) (y.val ++ right) width packed frame)
(ready regs' modulus left right width ((add x y).val.reverse ++ packed) frame)
variable {B : β„•} (step : WordStep B)
def addWords : List (Word B) β†’ List (Word B) β†’ List (Word B)
| x :: xs, y :: ys => step.add x y :: addWords xs ys
| _, _ => []
theorem addWords_length (xs ys : List (Word (B))) :
(addWords step xs ys).length = min xs.length ys.length := by
induction xs generalizing ys with
| nil => simp [addWords]
| cons x xs ih => cases ys <;> simp [addWords, ih]
theorem addLoop_eval (hB : 0 < B) (regs : Fin 8 β†’ Bool)
(xs ys : List (Word (B)))
(width packed : List Bool) (frame : Fin 6 β†’ List Bool)
(hwidth : width.length = B) :
βˆƒ n ≀ min xs.length ys.length * (step.cost + 4) + 1, βˆƒ regs',
Eval (addLoop step.cmd) n
(ready (guardRegs regs (flatten xs) (flatten ys)) step.modulus
(flatten xs) (flatten ys) width packed frame)
(ready regs' step.modulus (flatten (xs.drop ys.length)) (flatten (ys.drop xs.length)) width
((flatten (addWords step xs ys)).reverse ++ packed) frame) := by
induction xs generalizing ys regs packed with
| nil =>
refine ⟨1, by simp, guardRegs regs [] (flatten ys), ?_⟩
simpa [addLoop, flatten, addWords] using
(Eval.while_false (test := (4 : Fin 8)) (body := .seq step.cmd guard)
(a := ready (guardRegs regs [] (flatten ys)) step.modulus [] (flatten ys) width packed frame)
(by simp [ready, state]))
| cons x xs ih =>
cases ys with
| nil =>
refine ⟨1, by simp, guardRegs regs (flatten (x :: xs)) [], ?_⟩
simpa [addLoop, flatten, addWords] using
(Eval.while_false (test := (4 : Fin 8)) (body := .seq step.cmd guard)
(a := ready (guardRegs regs (flatten (x :: xs)) []) step.modulus
(flatten (x :: xs)) [] width packed frame) (by simp [ready, state]))
| cons y ys =>
let rg := guardRegs regs (flatten (x :: xs)) (flatten (y :: ys))
have ht : (ready rg step.modulus (flatten (x :: xs)) (flatten (y :: ys)) width packed frame).regs 4 = true := by
simp only [ready, state, rg, guardRegs_test,
head_isSome_of_ne_nil (flatten_cons_ne_nil hB x xs),
head_isSome_of_ne_nil (flatten_cons_ne_nil hB y ys), Bool.and_self]
obtain ⟨n, hn, r₁, hβ‚βŸ© := step.run rg x y (flatten xs) (flatten ys)
width packed frame hwidth
let packed' := (step.add x y).val.reverse ++ packed
have hg := guard_eval r₁ step.modulus (flatten xs) (flatten ys) width packed' frame
obtain ⟨m, hm, rβ‚‚, hβ‚‚βŸ© := ih r₁ ys packed'
refine ⟨n + 3 + m + 1, ?_, rβ‚‚, ?_⟩
Β· simp only [List.length_cons]
rw [show min (xs.length + 1) (ys.length + 1) = min xs.length ys.length + 1 by omega]
calc
_ ≀ step.cost + 3 +
(min xs.length ys.length * (step.cost + 4) + 1) + 1 := by omega
_ = _ := by ring
Β· have h := Eval.while_true ht (Eval.seq h₁ hg) hβ‚‚
simpa [addLoop, flatten, addWords, rg, packed', List.reverse_append,
List.append_assoc, Nat.add_assoc] using h
def addWordsTotal : List (Word (B)) β†’ List (Word (B)) β†’
List (Word (B))
| [], ys => ys
| xs, [] => xs
| x :: xs, y :: ys => (step.add x y) :: addWordsTotal xs ys
theorem addWordsTotal_decompose (xs ys : List (Word (B))) :
addWordsTotal step xs ys = addWords step xs ys ++ xs.drop ys.length ++ ys.drop xs.length := by
induction xs generalizing ys with
| nil => simp [addWordsTotal, addWords]
| cons x xs ih => cases ys <;> simp [addWordsTotal, addWords, ih]
@[simp] theorem flatten_append {B : β„•} (xs ys : List (Word B)) :
flatten (xs ++ ys) = flatten xs ++ flatten ys := by simp [flatten]
theorem addCmd_eval (hB : 0 < B) (regs : Fin 8 β†’ Bool)
(xs ys : List (Word (B))) (width : List Bool) (frame : Fin 6 β†’ List Bool)
(hwidth : width.length = B) :
βˆƒ n ≀ (xs.length + ys.length + 1) * (step.cost + 15 * B + 20), βˆƒ regs',
Eval (addCmd step.cmd) n (ready regs step.modulus (flatten xs) (flatten ys) width [] frame)
(ready regs' step.modulus (flatten (addWordsTotal step xs ys)) [] width [] frame) := by
have hg := guard_eval regs step.modulus (flatten xs) (flatten ys) width [] frame
obtain ⟨n, hn, r₁, hl⟩ := addLoop_eval step hB regs xs ys width [] frame hwidth
simp only [List.append_nil] at hl
let left := flatten (xs.drop ys.length)
let right := flatten (ys.drop xs.length)
let packed := (flatten (addWords step xs ys)).reverse
have hf := flush_eval r₁ step.modulus left right width packed frame
have h := Eval.seq hg (Eval.seq hl hf)
have hleft : left.length ≀ (xs.length + ys.length) * B := by
simp only [left, flatten_length, List.length_drop]
exact Nat.mul_le_mul_right _ (by omega)
have hright : right.length ≀ (xs.length + ys.length) * B := by
simp only [right, flatten_length, List.length_drop]
exact Nat.mul_le_mul_right _ (by omega)
have hpacked : packed.length ≀ (xs.length + ys.length) * B := by
simp only [packed, List.length_reverse, flatten_length, addWords_length]
exact Nat.mul_le_mul_right _ (by omega)
have hloop : n ≀ (xs.length + ys.length) * (step.cost + 4) + 1 :=
hn.trans (by gcongr; omega)
refine ⟨3 + (n + (6 * left.length + 6 * right.length + 3 * packed.length + 9)), ?_,
readRegs r₁ 0 1 none, ?_⟩
Β· nlinarith
Β· simpa only [addCmd, List.append_nil, packed, left, right, List.reverse_reverse,
addWordsTotal_decompose, flatten_append] using h
end SubexponentialSAT.DensePolynomialLoop
end OAI.Algorithms