lemmaZipped.move {w z A} (h : Zipped w z A) (b : Bool) (d : Direction) :
Zipped (Function.update w z b) (d.move z) (A.act b d) :=by
proof · 37
lines
obtain ⟨hl,hr⟩ := h
cases d with
| stay =>
dsimp [Zipped,Direction.move,Zip.act]
constructor
· intro j; rw [hl, upd_ne w b z _ _ (by omega) rfl]
· intro j
cases j with
| zero => simp [bit]
| succ j => rw [bit_push,← bit,hr,upd_ne w b z _ _ (by omega) rfl]
| right =>
dsimp [Zipped,Direction.move,Zip.act]
constructor
· intro j
cases j with
| zero => simp [bit]
| succ j =>
rw [bit_push,hl,upd_ne w b z _ (z-1-j) (by omega) (by omega)]
· intro j; rw [← bit,hr,upd_ne w b z _ _ (by omega) (show z+1+j=z+(↑(j+1):ℤ) by omega)]
| left =>
dsimp [Zipped,Direction.move,Zip.act]
constructor
· intro j
rw [← bit,hl,upd_ne w b z _ _ (by omega)
(show z-1-1-j=z-1-↑(j+1) by omega)]
· intro j
cases j with
| zero =>have e := hl 0
simp only [bit,Nat.cast_zero,sub_zero] at e
simp [bit,e]
| succ j =>
cases j with
| zero => simp [bit]
| succ j =>
rw [bit_push,bit_push, ← bit,hr,upd_ne w b z _ _ (by omega)
(show z-1+↑(j+1+1)=z+↑(j+1) by omega)]
lemmatrunc_bits (m n : ℕ) (h : ∀ j, m≤j → bit n j=false) : n<2^m :=by
proof · 13
lines
induction m generalizing n with
| zero =>
induction n usingNat.strong_induction_on with
| h n ih =>by_cases e : n=0
· simp [e]
have he := ih (n/2) (by omega) (fun j _=> h (j+1) (by omega))
have ht := h 0 (by omega)
simp [bit,front] at ht; simp at he; simp; omega
| succ m ih =>have he := ih (n/2) (fun j e=> h (j+1) (by omega))
rw [pow_succ]; omega
lemmaZip.unique {AA' : Zip} {b b' : Bool} {d : Direction}
(h : front A.2=front A'.2)
(e : A.act b d=A'.act b' d) : A=A':=by
proof · 11
lines
haveH : ∀ (z w : ℕ), front z=front w → z/2=w/2→ z=w :=by
intro z w h h'
rw [← push_front z,h,h',push_front]
cases d <;> simp only [Zip.act,Prod.mk.injEq] at e
·
apply Prod.ext
· exact H _ _ (by simpa using congrArg front e.2) e.1
· apply H _ _ h
simpa using congrArg (funa : ℕ=> a/2/2) e.2
· exact Prod.ext e.1 (H _ _ h (by simpa using congrArg (funa : ℕ=>a/2) e.2))
· exact Prod.ext (by simpa using congrArg (funa : ℕ=>a/2) e.1) (H _ _ h e.2)
Record of numeric registers (time separate from rest).
defstep (A : R w h) (x : Word) (b : Bool) : R w h :=
body · 5
lines
let u :=A.action M x b
{ tm :=A.tm+1
st := u.nextState
inp :=fun j=> minput x (A.inp j) (u.inputMove j)
tapes :=fun k=> (A.tapes k).act (u.write k) (u.workMove k) }
lemmaOK.state {x : Word} {t A} {c : Configuration q w h x.length} (hc : OK t A c) :
A.state q = c.state :=by ext; simp [R.state,hc.st,Nat.mod_eq_of_lt c.state.isLt]
lemmaruns_small (x : Word) (coins : CoinTape) {cs k e : ℕ}
(ht : ∀ t, M.spaceThrough x coins t ≤ cs * Nat.clog 2 (x.length+2))
{t : ℕ} (he : t ≤ polynomialClock k e x.length) :
(runs M x coins t).Small (radix q cs k e x.length) :=by
proof · 16
lines
let n := x.length
have ok := runs_OK M x coins t
simp only [Small, ok.tm, ok.st, ok.inp, radix]
refine ⟨by omega, byhave:= (M.run x coins t).state.isLt; omega, ?_, ?_⟩
· intro i; have:= ((M.run x coins t).inputPos i).isLt; omega
· intro i
let s := cs * Nat.clog 2 (n+2)
have hv := @Zipped.bound ( (M.run x coins t).work i) _ _ s (ok.tapes i)
(le_trans (M.workPos_abs_le_space x coins t i)
(show (M.spaceThrough x coins t : ℤ) ≤ (s:ℤ) by exact_mod_cast ht t))
(M.work_eq_false_outside ht t i)
havehz : 2^(2*s+1) ≤2*(2*(x.length+2))^(2*cs) :=byhavee : 2*s=Nat.clog 2 (n+2)*(2*cs) :=by dsimp [s]; ring
rw [e,pow_succ,pow_mul,Nat.mul_comm _ 2]
exact Nat.mul_le_mul_left 2 (Nat.pow_le_pow_left (two_pow_clog_le _) _)
omega
lemmastep_inj (x : Word) (AA' : R w h) (b b' : Bool)
(hk : A.step M x b=A'.step M x b') (hh : A.hint q b=A'.hint q b')
(h : A.st<q+1) (h' : A'.st<q+1) : A=A':=by
proof · 19
lines
simp only [hint,Prod.mk.injEq] at hh
obtain ⟨rfl,hst,hin,hq⟩ := hh
havehe : A.inp=A'.inp :=by
funext i
have hv := congrFun (congrArg R.inp hk) i
have hm := congrArg Fin.val (congrFun hin i)
simp only [step] at hv
have hs := minput_range x (A.inp i) ((A.action M x b).inputMove i)
have ht := minput_range x (A'.inp i) ((A'.action M x b).inputMove i)
dsimp only at hm
omega
haveha : A.action M x b=A'.action M x b :=by rw [action,action,out,out,hst,he,hq]
refine R.ext ?_ ?_ he ?_
· haveH:= congrArg R.tm hk; simp [step] at H; exact H
· simpa [R.state,Nat.mod_eq_of_lt h,Nat.mod_eq_of_lt h'] using congrArg Fin.val hst
· funext i
have hv := congrFun (congrArg tapes hk) i
simp only [step,ha] at hv
exact Zip.unique (congrFun hq i) hv
lemmarecov_id {A : R w h} {m} (he : A.Small m) : recov w h m (A.id m)=A:=by
proof · 6
lines
obtain ⟨_,hs,hi,ht⟩ := he
have hl (z : ℕ) := dec_enc m z (fun i=> (A.tapes i).1) (fun i=> (ht i).1)
have hr (z : ℕ) := dec_enc m z (fun i=> (A.tapes i).2) (fun i=> (ht i).2)
have hin (z : ℕ) := dec_enc m z A.inp hi
simp only [id, recov,(hr _).2,(hl _).2,(hin _).2,(hr _).1,(hl _).1,
(hin _).1,wrap_hi hs,wrap_lo hs]
Source machines with supplied polynomial clock parameters (need not
be positive for building the graph) and controller space coefficient.
Semantics will justify these externally.
lemmasteps_to {x n b y} (h : P.next x n b = some y) :
P.legal x (P.row x n) ∧ (P.row x n).tm<P.clock x ∧P.legal x (P.row x y) ∧
(P.row x n).step P.mach x b=P.row x y :=by
proof · 6
lines
dsimp only [next] at h; split at h
· rename_i he
obtain rfl :=Option.some.inj h
unfold row at he
simp only [row,ident, R.recov_id he.2.2.1]; exact ⟨he.1,he.2.1,he.2.2,trivial⟩
· simp at h
lemmaavg_S (x n) (f) :
P.avg x n f = ∑ j ∈P.dom x, P.S x n j * f j :=by
proof · 8
lines
let term (b : Bool) j : ℚ:=ifP.next x n b= some j then f j/2else0have he (b : Bool) : payoff f (P.next x n b)/2=∑ j∈P.dom x,term b j :=bycases h : P.next x n b with
| none => simp [term,payoff,h]
| some j => simp [term,payoff,h,P.mem (P.steps_to h).2.2.1]
have hk (j) : P.S x n j * f j= term false j + term true j :=by
unfold S edge term; split <;> split <;> norm_num <;> ring
simp [avg,add_div,he,hk,sum_add_distrib]
lemmain_degree (x y) :
((P.dom x).filter (fun n=>0<P.S x n y)).card ≤Fintype.card (R.Hint q w h) :=by
proof · 16
lines
classical
let v := (P.dom x).filter (fun n=>0<P.S x n y)
change v.card ≤ _
letb n : Bool:=ifP.next x n true=some y thentrueelsefalsehave hs (n) (hn : n∈v) : P.next x n (b n)=some y :=byhave hv :=P.S_pos.1 (Finset.mem_filter.1 hn).2
dsimp only [b]; split <;> grind
let code (n : ↥v) := (P.row x n.val).hint q (b n.val)
havehi : Function.Injective code :=by
intro n m he
have hn :=P.steps_to (hs n n.prop)
have hm :=P.steps_to (hs m m.prop)
have e :=R.step_inj P.mach x _ _ _ _ (hn.2.2.2.trans hm.2.2.2.symm) he hn.1.2 hm.1.2
apply Subtype.ext
simpa only [row, ident,R.id_recov] using congrArg (P.ident x) e
simpa usingFintype.card_le_of_injective _ hi
lemmabounded (x k n) : 0≤P.potential x k n ∧P.potential x k n ≤1:=by
proof · 13
lines
induction k generalizing n with
| zero => unfold potential reward; split <;> norm_num
| succ k ih =>cases h : P.fin x n with
| true=> simp [potential,reward,h,avg,P.dead h,payoff]
| false=>haveH (b) : 0≤ payoff (P.potential x k) (P.next x n b) ∧
payoff (P.potential x k) (P.next x n b) ≤1:=by
cases P.next x n b <;> simp [payoff, ih]
have ht :=Htruehave hf :=Hfalse
simp [potential,reward,h,avg]
constructor <;> linarith
lemmastable (x n k) (h : P.clock x ≤ (P.row x n).tm+k) :
P.potential x k n=P.potential x (k+1) n :=by
proof · 17
lines
induction k generalizing n with
| zero =>have he (b) : P.next x n b=none :=by
simp only [Nat.add_zero] at h; simp [next,not_lt.2 h]
simp [potential,avg,he,payoff]
| succ k ih =>have he (b) : payoff (P.potential x k) (P.next x n b) =
payoff (P.potential x (k+1)) (P.next x n b) :=bycases hb : P.next x n b with
| none => rfl
| some j =>
apply ih
haveH:= congrArg R.tm (P.steps_to hb).2.2.2
simp only [R.step] at H
omega
change P.reward x n+ _=P.reward x n+_
unfold avg; rw [he,he]
lemmavisited (e : SpaceP) (x : Word) (coins t) (h : t≤P.clock x) :
P.row x (P.walker x coins t) = R.runs P.mach x coins t ∧P.legal x (R.runs P.mach x coins t) ∧ (R.runs P.mach x coins t).tm=t :=by
proof · 3
lines
haveH:= @R.runs_small q w _ P.mach x coins P.cs P.c P.k (e x coins) t h
haveI:=R.runs_OK P.mach x coins t
exact ⟨R.recov_id H,⟨H,I.st ▸ ((P.mach.run x coins t).state).isLt⟩,I.tm⟩
lemmastep_true (e : SpaceP) (x coins t) (h : t<P.clock x) :
P.next x (P.walker x coins t) (coins t) =
some (P.walker x coins (t+1)) ∧P.fin x (P.walker x coins t) = false:=by
proof · 5
lines
obtain ⟨H,G,S⟩ :=P.visited e x coins t (le_of_lt h)
obtain ⟨_,T,_⟩ :=P.visited e x coins (t+1) h
simp only [R.runs] at T
simp only [next,fin,H,G,S,h,T,true_and,ite_true]
simp [walker,R.runs,Nat.ne_of_lt h]
lemmapr_end (e : SpaceP) (x coins) :
P.pr x (P.walker x coins (P.clock x)) =
ifP.mach.output (P.mach.run x coins (P.clock x)).state = some truethen1else0:=by
proof · 3
lines
obtain ⟨H,G,S⟩ :=P.visited e x coins (P.clock x) le_rfl
rw [pr_eq]
simp [avg,reward,fin,next,H,G,S,payoff,R.runs_out]
lemmafair (e : SpaceP) (x : Word) :
P.pr x (P.first x)=P.mach.acceptanceProbability x (P.clock x) :=by
proof · 56
lines
let k :=P.clock x
let f (b : Tapes k) (t) :=P.walker x (Machine.extendCoins b) t
let g (t) (b : Tapes k) :=P.pr x (f b t)
haveHH (t) (ht : t<k) (b : Tapes k) :
g t b = (g (t+1) b + g (t+1) (Toggle ⟨t,ht⟩ b))/2:=byleti : Fin k := ⟨t,ht⟩
let l :=Toggle i b
let w :=Machine.extendCoins
(t:=k)
havehv : f b t=f l t :=by
apply P.same
intro j hj
havehk : j<k :=by omega
havehne : i.val≠j :=by dsimp [i]; omega
havehne' : i ≠ ⟨j,hk⟩ :=fun h=>hne (congrArg Fin.val h)
simp [Machine.extendCoins, l, Toggle,hk,hne']
obtain ⟨hn,Hp⟩ :=P.step_true e x (w b) t ht
obtain ⟨hn',-⟩ :=P.step_true e x (w l) t ht
haveh1 : w b t=b i :=Machine.extendCoins_apply b i
haveh2 : w l t= (!b i) :=
(Machine.extendCoins_apply l i).trans (by simp [l,Toggle])
rw [h1] at hn
rw [h2] at hn'
change P.next x (f b t) (b i)=some (f b (t+1)) at hn
change P.next x (f l t) (!b i)=some (f l (t+1)) at hn'
change P.fin x (f b t)=false at Hp
rw [← hv] at hn'
change P.pr x (f b t) = (P.pr x (f b (t+1)) + P.pr x (f l (t+1))) / 2
rw [pr_eq]
cases h : b i <;> simp only [h, Bool.not_true,Bool.not_false] at hn hn' <;>
simp [reward,Hp,avg,payoff,hn,hn',add_comm]
have hs (t) (ht : t≤k) : ∑ b, g 0 b= ∑ b, g t b :=by
induction t with
| zero => rfl
| succ t ih =>havehlt : t<k := ht
rw [ih (by omega)]
calc
_ = ∑ b, (g (t+1) b+g (t+1) (Toggle ⟨t,hlt⟩ b))/2:=Finset.sum_congr rfl (fun b _=>HH t hlt b)
_ = _ :=by
rw [←Finset.sum_div, Finset.sum_add_distrib,Fintype.sum_equiv
(Toggle ⟨t,hlt⟩) _ (g (t+1)) (fun b=>rfl)]
ring
let p (b : Tapes k) :=P.mach.output (P.mach.run x (Machine.extendCoins b) k).state = some truelet : DecidablePred p :=fun i => inferInstanceAs (Decidable (_ = some true))
have hv (b) : g k b = if p b then1else0:=P.pr_end e x _
have hz (b) : g 0 b = P.pr x (P.first x) := rfl
have he := hs k le_rfl
simp only [hz,hv] at he
rw [Finset.sum_const, Finset.card_univ, Fintype.card_fun, Fintype.card_bool, Fintype.card_fin] at he
unfold Machine.acceptanceProbability
rw [eq_div_iff (by positivity),mul_comm]
change (2:ℚ)^k * P.pr x (P.first x) = _
rw [Finset.card_filter]
simpa using he
lemmadec_E {m n : NE} (hm : E m) (hn : E n) (d : ℕ) :
E (fun x a=>upper (m x a) d (n x a)) ∧∀ i, E (fun x a=>dec (m x a) d (n x a) i) :=by
proof · 7
lines
induction d generalizing n with
| zero => exact ⟨hn,fun i=>Fin.elim0 i⟩
| succ d ih =>
obtain ⟨h,h'⟩ := ih (hn.div hm)
refine ⟨h,fun i=>Fin.cases ?_ ?_ i⟩
· simpa only [dec,Fin.cons_zero] using (hn.mod hm)
· intro i; simpa only [dec,Fin.cons_succ] using h' i
lemmaPickAND {p q : Word→Store→Prop} [∀ x, DecidablePred (p x)] [∀ x, DecidablePred (q x)]
(h : Pick (fun x a=>decide (p x a))) (k : Pick (fun x a => decide (q x a))) :
Pick (fun x a => decide (p x a ∧ q x a)) :=by
tim : Efun x a=> (f x a).tm
st : Efun x a=> (f x a).st
inp (i : Fin h) : Efun x a=> (f x a).inp i
ll (i : Fin w) : Efun x a=> ((f x a).tapes i).1
rr (i : Fin w) : Efun x a=> ((f x a).tapes i).2
lemmachoose (hf : RE f) (M : Machine q w h) (b : Bool) :
Pick (fun x a => (f x a).action M x b) :=by
proof · 10
lines
let v x a :=fun i =>Counter.scan x ((f x a).inp i)
let z x a :=fun i=> front ((f x a).tapes i).2havehv : Pick v :=Pick.tuple (fun i=>Pick.read (hf.inp i))
havehz : Pick z :=Pick.tuple (fun i=> front_E (hf.rr i))
let doIt :=fun (s : Fin (q+1)) (u : Fin h →InputSymbol) (n : Fin w →Bool) =>matchM.output s with
| none=>M.transition s u n b
| some _=> ⟨s,n,fun _=> .stay,fun _=>.stay⟩
exact (hf.stPick q).bind (g:=fun s x a=>doIt s (v x a) (z x a))
(fun s=> hv.bind (g:=fun u x a => doIt s u (z x a)) (fun u => hz.map (doIt s u)))
lemmastep (hf : RE f) (M : Machine q w h) (b : Bool) :
RE (fun x a => (f x a).step M x b) :=by
proof · 25
lines
have he := hf.choose M b
constructor
· exact hf.tim.succ
· exact he (fun z _ _=>z.nextState.val) (fun _=>E.const _)
· intro i
refine (he.map (fun z=>z.inputMove i))
(fun d x a=>R.minput x ((f x a).inp i) d) (fun d=>?_ )
cases d
· exact (hf.inp i).pred
· exact hf.inp i
· exact E.ite_lt (hf.inp i) E.length.succ (hf.inp i).succ (hf.inp i)
· intro i
apply he (fun z x a => (((f x a).tapes i).act (z.write i) (z.workMove i)).1)
intro z
cases H : z.workMove i with
| stay => exact hf.ll i
| right => exact push_E (hf.ll i) (Pick.const _)
| left => exact (hf.ll i).div (E.const 2)
· intro i
apply he (fun z x a => (((f x a).tapes i).act (z.write i) (z.workMove i)).2)
intro z
cases H : z.workMove i with
| stay => exact push_E ((hf.rr i).div (E.const 2)) (Pick.const _)
| right => exact (hf.rr i).div (E.const 2)
| left => exact push_E (push_E ((hf.rr i).div (E.const 2)) (Pick.const _)) (front_E (hf.ll i))
lemmaeWeight {f g : NE} (hf : E f) (hg : E g) :
E (fun x a=>P.edge x (f x a) (g x a)) :=by
proof · 11
lines
let v x a :=P.row x (f x a)
havehk : RE v :=RE.recover P.eRad hf
have he (b : Bool) : E (fun x a=>ifP.next x (f x a) b=some (g x a) then1else0) :=bylet z x a :=R.step P.mach (v x a) x b
havehz : RE z := hk.step P.mach b
have hc := hz.code P.eRad
have ht :=PickAND (P.eLegal hk) (PickAND (Pick.test_lt hk.tim P.eClock) (P.eLegal hz))
convert ht.ite (hc.ite_eq hg (E.const 1) (E.const 0)) (E.const 0) using1
ext x a
simp [next,v,z,ident] ; grind
exact (he false).add (he true)