cfahlgren1's picture
download
raw
7.78 kB
import Mathlib
import OAI.Analysis.MatrixSigning.Complexity.PolynomialRoutine
import OAI.Analysis.MatrixSigning.Realization.NaturalRealization
namespace OAI
section
namespace CurrentKS.Realization
open CleanRoutine KS.W23
local notation "X" => NaturalPolynomial.variable
variable {α β γ : Type*} {ea : α → Word} {eb : β → Word}
lemma len_listCode (ea : α → Word) (xs : List α) :
(listCode ea xs).length=(xs.map (fun x => 2*(ea x).length+1)).sum := by
induction xs with
| nil => rfl
| cons x xs ih => simp only [listCode_cons,prodCode_length,List.map_cons,List.sum_cons,ih]
lemma sum_map_bound (xs : List α) (f : α → ℕ) (B : ℕ) (h : ∀ x∈xs,f x≤B) :
(xs.map f).sum≤xs.length*B := by
induction xs with
| nil => simp
| cons x xs ih =>
have hx := h x (by simp)
have ht := ih (fun y hy => h y (by simp [hy]))
simp only [List.map_cons,List.sum_cons,List.length_cons,Nat.add_mul,Nat.one_mul]
omega
lemma listCode_length_bound (xs : List α) (B : ℕ) (h : ∀ x∈xs,(ea x).length≤B) :
(listCode ea xs).length≤xs.length*(2*B+1) := by
rw [len_listCode]
apply sum_map_bound
intro x hx
have := h x hx
omega
lemma list_length_le_code (ea : α → Word) (xs : List α) : xs.length≤(listCode ea xs).length := by
induction xs with
| nil => rfl
| cons x xs ih => simp only [listCode_cons,prodCode_length,List.length_cons];omega
lemma mem_length_le_code (xs : List α) {x : α} (hx : x∈xs) :
(ea x).length≤(listCode ea xs).length := by
induction xs with
| nil => simp at hx
| cons y ys ih =>
simp only [List.mem_cons] at hx
rw [listCode_cons,prodCode_length]
dsimp only
rcases hx with rfl|hx
· omega
· have := ih hx;omega
lemma listCode_reverse_length (ea : α → Word) (xs : List α) :
(listCode ea xs.reverse).length=(listCode ea xs).length := by
simp only [len_listCode,List.map_reverse,List.sum_reverse]
instance poly_map {f : α → β} (r : Realizes ea eb f) [PolyTime r] : PolyTime r.map := by
obtain ⟨p,hp⟩ := PolyTime.bound (r:=r)
let Q := X+p
refine ⟨⟨2*X+X*(3*X+p+6*Q+8)+X*(2*Q+1)+4,?_⟩⟩
intro xs
let L := (listCode ea xs).length
have hn : xs.length≤L := list_length_le_code ea xs
have ho : ∀ x∈xs,(eb (f x)).length≤L+p.eval L := by
intro x hx
have hl : (ea x).length≤L := mem_length_le_code xs hx
exact (r.output_length x).trans (Nat.add_le_add hl ((hp x).trans (p.eval_mono hl)))
have hs := sum_map_bound xs (fun x => FramedMapRoutine.bodyTicks ea (fun x => eb (f x)) r.cost x+1)
(3*L+p.eval L+6*(L+p.eval L)+8) (by
intro x hx
have hl : (ea x).length≤L := mem_length_le_code xs hx
have hc := (hp x).trans (p.eval_mono hl)
have hout := ho x hx
simp only [FramedMapRoutine.bodyTicks,KS.W01.length_frame]
omega)
have ht := listCode_length_bound (ea:=fun x => eb (f x)) xs (L+p.eval L) ho
have hs' := hs.trans (Nat.mul_le_mul_right _ hn)
have ht' := ht.trans (Nat.mul_le_mul_right _ hn)
change FramedMapRoutine.ticks ea (fun x => eb (f x)) r.cost xs≤_
simp only [FramedMapRoutine.ticks,FramedMapRoutine.scanTicks]
change 2*L+2+((xs.map _).sum+1)+(listCode (fun x => eb (f x)) xs).length+1≤_
change _≤2*L+L*(3*L+p.eval L+6*(L+p.eval L)+8)+L*(2*(L+p.eval L)+1)+4
omega
lemma fold_scan_bound (F : α → β → α) (cost : α×β → ℕ) (s : α) (xs : List β) (S E C : ℕ)
(hs : ∀ ys zs,xs=ys++zs → (ea (ys.foldl F s)).length≤S)
(he : ∀ x∈xs,(eb x).length≤E)
(hc : ∀ a x,(ea a).length≤S → (eb x).length≤E → cost (a,x)≤C) :
FoldRoutine.scanTicks ea eb F cost s xs≤xs.length*(3*E+10*S+C+12)+1 := by
induction xs generalizing s with
| nil => simp [FoldRoutine.scanTicks]
| cons x xs ih =>
have h0 := hs [] (x::xs) rfl
have h1 := hs [x] xs rfl
have hx := he x (by simp)
have hc0 := hc s x h0 hx
have ht := ih (F s x) (by
intro ys zs hz
have h := hs (x::ys) zs (by simp only [List.cons_append];rw [hz])
simpa only [List.foldl_cons] using h) (fun y hy => he y (by simp [hy]))
simp only [FoldRoutine.scanTicks,FoldRoutine.bodyTicks,KS.W01.length_frame,List.length_cons,Nat.add_mul,Nat.one_mul]
simp only [List.foldl_nil,List.foldl_cons] at h0 h1
omega
class FoldSize {f : α×β → α} (r : Realizes (prodCode ea eb) ea f) : Prop where
bound : ∃ p : NaturalPolynomial,∀ s xs ys zs,xs=ys++zs →
(ea (ys.foldl (fun a b => f (a,b)) s)).length ≤ p.eval (prodCode ea (listCode eb) (s,xs)).length
instance poly_fold {f : α×β → α} (r : Realizes (prodCode ea eb) ea f) [PolyTime r] [FoldSize r] :
PolyTime r.fold := by
obtain ⟨p,hp⟩ := PolyTime.bound (r:=r)
obtain ⟨q,hq⟩ := FoldSize.bound (r:=r)
refine ⟨⟨5*q+2*X+X*(3*X+10*q+p.comp (2*q+1+X)+12)+7,?_⟩⟩
intro u
let L := (prodCode ea (listCode eb) u).length
let S := q.eval L
have he : (listCode eb u.2).length≤L := le_prodCode_right u
have hn := (list_length_le_code eb u.2).trans he
have hs := hq u.1 u.2
have h0 : (ea u.1).length≤S := hs [] u.2 rfl
have hN : (ea (u.2.foldl (fun a b => f (a,b)) u.1)).length≤S := hs u.2 [] (by simp)
have hc : ∀ a x,(ea a).length≤S → (eb x).length≤L → r.cost (a,x)≤p.eval (2*S+1+L) := by
intro a x ha hx
apply (hp (a,x)).trans (p.eval_mono ?_)
rw [prodCode_length]
dsimp only
omega
have hscan := fold_scan_bound (ea:=ea) (eb:=eb) (fun a b => f (a,b)) r.cost u.1 u.2 S L
(p.eval (2*S+1+L)) hs (fun x hx => (mem_length_le_code u.2 hx).trans he) hc
have hscan' := hscan.trans (Nat.add_le_add_right (Nat.mul_le_mul_right _ hn) 1)
change FoldRoutine.ticks ea eb (fun a b => f (a,b)) r.cost u.1 u.2≤_
simp only [FoldRoutine.ticks]
simp only [NaturalPolynomial.eval_add,NaturalPolynomial.eval_mul,NaturalPolynomial.eval_variable,NaturalPolynomial.eval_comp]
change _≤5*S+2*L+L*(3*L+10*S+p.eval (2*S+1+L)+12)+7
change _≤_ at hscan'
change (FramedMapRoutine.tape eb u.2).length≤L at he
omega
lemma iterate_ticks_bound {f : α → α} (r : Realizes ea ea f) (x : α) (n C : ℕ)
(h : ∀ i,i<n → r.cost (f^[i] x)≤C) :
IterateRoutine.loopTicks r.cost f n x≤n*(C+2)+1 := by
induction n generalizing x with
| zero => simp [IterateRoutine.loopTicks]
| succ n ih =>
have h0 := h 0 (by omega)
have ht := ih (f x) (by
intro i hi
simpa only [←Function.iterate_succ_apply] using h (i+1) (by omega))
simp only [Function.iterate_zero, id_eq] at h0
simp only [IterateRoutine.loopTicks,Nat.add_mul,Nat.one_mul]
omega
class IterateSize {f : α → α} (r : Realizes ea ea f) : Prop where
bound : ∃ p : NaturalPolynomial,∀ x n,(ea (f^[n] x)).length≤p.eval ((ea x).length+n)
instance poly_iterate {f : α → α} (r : Realizes ea ea f) [PolyTime r] [IterateSize r] :
PolyTime r.iterate := by
obtain ⟨p,hp⟩ := PolyTime.bound (r:=r)
obtain ⟨q,hq⟩ := IterateSize.bound (r:=r)
refine ⟨⟨3*X+X*(p.comp q+2)+3,?_⟩⟩
intro u
let L := (prodCode natCode ea u).length
have hi : (ea u.2).length+u.1≤L := by simp only [L,prodCode_length,natCode,List.length_replicate];omega
have hn : u.1≤L := by omega
have ht := iterate_ticks_bound r u.2 u.1 (p.eval (q.eval L)) (by
intro i hi'
exact (hp _).trans (p.eval_mono ((hq u.2 i).trans (q.eval_mono (by omega)))))
have ht' := ht.trans (Nat.add_le_add_right (Nat.mul_le_mul_right _ hn) 1)
simp only [NaturalPolynomial.eval_add,NaturalPolynomial.eval_mul,NaturalPolynomial.eval_variable,NaturalPolynomial.eval_comp]
change 3*u.1+2+IterateRoutine.loopTicks r.cost f u.1 u.2≤3*L+L*(p.eval (q.eval L)+2)+3
omega
end CurrentKS.Realization
end
end OAI

Xet Storage Details

Size:
7.78 kB
·
Xet hash:
159cf0e62a10a8448f7d926c5b58865816483b413122d9aa9c7a2f07cecf463d

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