cfahlgren1's picture
download
raw
6.46 kB
import Mathlib
import OAI.Analysis.MatrixSigning.Complexity.PolynomialLoops
import OAI.Analysis.MatrixSigning.Realization.ArrayRealization
namespace OAI
namespace CurrentKS.Realization
open CleanRoutine KS.W23
local notation "X" => NaturalPolynomial.variable
variable {α β γ : Type*} {ea : α → Word} {eb : β → Word} {ec : γ → Word}
lemma prefix_code_length (ea : α → Word) (xs ys zs : List α) (h : xs=ys++zs) :
(listCode ea ys).length≤(listCode ea xs).length := by
rw [h,listCode_append,List.length_append];omega
lemma prefix_length (xs ys zs : List α) (h : xs=ys++zs) : ys.length≤xs.length := by
rw [h,List.length_append];omega
instance poly_consList (ea : α → Word) : PolyTime (consList ea) := by
unfold consList
apply poly_changeOutput
instance poly_appendList (ea : α → Word) : PolyTime (appendList ea) := by
refine ⟨⟨5*X+4,?_⟩⟩
intro p
exact Nat.add_le_add_right (Nat.mul_le_mul_left 5 (le_prodCode_left p)) 4
instance poly_reverseBody (ea : α → Word) : PolyTime (reverseBody ea) := by
unfold reverseBody;infer_instance
instance size_reverseBody (ea : α → Word) : FoldSize (reverseBody ea) := by
refine ⟨⟨X,?_⟩⟩
intro s xs ys zs h
rw [fold_cons,listCode_append,List.length_append,listCode_reverse_length]
have hp := prefix_code_length ea xs ys zs h
change _≤(prodCode (listCode ea) (listCode ea) (s,xs)).length
rw [prodCode_length]
try dsimp only
omega
instance poly_reverseList (ea : α → Word) : PolyTime (reverseList ea) := by
unfold reverseList;infer_instance
instance poly_mapCtxBody {f : γ×α → β} (r : Realizes (prodCode ec ea) eb f) [PolyTime r] :
PolyTime (mapCtxBody ec ea eb r) := by
dsimp only [mapCtxBody]
apply poly_both
instance size_mapCtxBody {f : γ×α → β} (r : Realizes (prodCode ec ea) eb f) [PolyTime r] :
FoldSize (mapCtxBody ec ea eb r) := by
obtain ⟨p,hp⟩ := PolyTime.bound (r:=r)
let U := 3*X+1+p.comp (3*X+1)
refine ⟨⟨3*X+1+X*(2*U+1),?_⟩⟩
intro s xs ys zs h
rw [fold_mapCtx]
let L := (prodCode (prodCode ec (listCode eb)) (listCode ea) (s,xs)).length
have hc : (ec s.1).length≤L := (le_prodCode_left s).trans (le_prodCode_left (s,xs))
have ha : (listCode eb s.2).length≤L := (le_prodCode_right s).trans (le_prodCode_left (s,xs))
have he : (listCode ea ys).length≤L := (prefix_code_length ea xs ys zs h).trans (le_prodCode_right (s,xs))
have hn : ys.length≤L := (list_length_le_code ea ys).trans he
have ho : ∀ x∈ys,(eb (f (s.1,x))).length≤3*L+1+p.eval (3*L+1) := by
intro x hx
have hl : (prodCode ec ea (s.1,x)).length≤3*L+1 := by
have he' := (mem_length_le_code ys hx).trans he
simp only [prodCode_length]
try dsimp only
omega
exact (r.output_length (s.1,x)).trans (Nat.add_le_add hl ((hp _).trans (p.eval_mono hl)))
have hm := listCode_length_bound (ea:=fun x => eb (f (s.1,x))) ys (3*L+1+p.eval (3*L+1)) ho
have hm' := hm.trans (Nat.mul_le_mul_right _ hn)
have heq : listCode eb (ys.map (fun x => f (s.1,x)))=listCode (fun x => eb (f (s.1,x))) ys := by
simp only [listCode,FramedMapRoutine.tape,List.flatMap_map]
rw [prodCode_length]
dsimp only
rw [listCode_append,List.length_append,listCode_reverse_length,heq]
change 2*(ec s.1).length+1+((listCode (fun x => eb (f (s.1,x))) ys).length+(listCode eb s.2).length)≤_
simp only [NaturalPolynomial.eval_add,NaturalPolynomial.eval_mul,NaturalPolynomial.eval_variable,NaturalPolynomial.eval_comp,U]
change _≤3*L+1+L*(2*(3*L+1+p.eval (3*L+1))+1)
omega
instance poly_mapCtx {f : γ×α → β} (r : Realizes (prodCode ec ea) eb f) [PolyTime r] :
PolyTime r.mapCtx := by
unfold Realizes.mapCtx
infer_instance
instance poly_natSucc : PolyTime natSucc := ⟨⟨1,fun _ => le_rfl⟩⟩
instance poly_natPred : PolyTime natPred := ⟨⟨1,fun _ => le_rfl⟩⟩
instance poly_natAdd : PolyTime natAdd := by
refine ⟨⟨5*X+4,?_⟩⟩
intro p
have h := le_prodCode_left (ea:=natCode) (eb:=natCode) p
simp only [natCode,List.length_replicate] at h
change 5*p.1+4≤5*(prodCode natCode natCode p).length+4
omega
instance poly_natIsZero : PolyTime natIsZero := by
refine ⟨⟨X+6,?_⟩⟩
intro n
change n+6≤(X+6).eval (natCode n).length
simp only [natCode,List.length_replicate]
exact Nat.le_refl _
instance poly_rangeStep : PolyTime rangeStepRealizes := by dsimp only [rangeStepRealizes];apply poly_both
lemma range_iterate_size (s : ℕ×List ℕ) (k : ℕ) :
(rangeStep^[k] s).1=s.1+k ∧
(listCode natCode (rangeStep^[k] s).2).length≤(listCode natCode s.2).length+k*(2*(s.1+k)+1) := by
induction k with
| zero => simp
| succ k ih =>
rw [Function.iterate_succ_apply']
change (rangeStep^[k] s).1+1=s.1+(k+1) ∧ _
constructor
· omega
· change (listCode natCode ((rangeStep^[k] s).1::(rangeStep^[k] s).2)).length≤_
rw [listCode_cons,prodCode_length]
simp only [natCode,List.length_replicate]
try dsimp only
nlinarith [ih.2]
instance size_rangeStep : IterateSize rangeStepRealizes := by
refine ⟨⟨10*(X+1)^2,?_⟩⟩
intro s k
have h := range_iterate_size s k
simp only [prodCode_length,natCode,List.length_replicate]
change 2*(rangeStep^[k] s).1+1+(listCode natCode (rangeStep^[k] s).2).length≤10*(2*s.1+1+(listCode natCode s.2).length+k+1)^2
rw [h.1]
nlinarith [h.2]
instance poly_range : PolyTime range := by unfold range;infer_instance
instance poly_lengthBody (ea : α → Word) : PolyTime ((first natCode ea).then natSucc) := by infer_instance
instance size_lengthBody (ea : α → Word) : FoldSize ((first natCode ea).then natSucc) := by
refine ⟨⟨2*X,?_⟩⟩
intro s xs ys zs h
change (natCode (ys.foldl (fun k _ => k+1) s)).length≤_
rw [fold_length]
have hs := le_prodCode_left (ea:=natCode) (eb:=listCode ea) (s,xs)
have hn := ((prefix_length xs ys zs h).trans (list_length_le_code ea xs)).trans (le_prodCode_right (ea:=natCode) (s,xs))
simp only [natCode,List.length_replicate] at hs ⊢
change s+ys.length≤2*(prodCode natCode (listCode ea) (s,xs)).length
omega
instance poly_lengthList (ea : α → Word) : PolyTime (lengthList ea) := by unfold lengthList;infer_instance
instance poly_tabulate {f : γ×ℕ → α} (r : Realizes (prodCode ec natCode) ea f) [PolyTime r] :
PolyTime r.tabulate := by unfold Realizes.tabulate;infer_instance
end CurrentKS.Realization
end OAI

Xet Storage Details

Size:
6.46 kB
·
Xet hash:
de18bb0d2bd60042698b4054baa7bce729cc8ae296debc53bef6125e76b5a78c

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