cfahlgren1/math / lean /OAI /Computability /Logspace /FiniteFieldPrograms.lean
cfahlgren1's picture
download
raw
43 kB
24
def tube {α} (f:PT (α×ℕ) Bool) (x:Word)(a:α)(n:ℕ):ℕ :=
body · 1 line
    forNat (fun j v=>if f x (a,j) then j+1 else v) 0 n
26
lemma tube_hi {α}(f:PT (α×ℕ) Bool) (x a n):tube f x a n≤n:=by
proof · 3 lines
   induction n <;> simp only [tube,forNat] at *
   · exact le_rfl
   split <;> omega
31
lemma unique_small {α} {f:PT (α×ℕ) Bool} {x a n} :
    tube f x a n=0 ↔ ∀ j < n, f x (a,j)=false :=by
proof · 12 lines
   induction n with
   | zero=> simp [tube,forNat]
   | succ n ih=>
     have he : (∀ j<n+1,f x (a,j)=false) ↔ f x (a,n)=false ∧ (∀ j<n,f x (a,j)=false) :=by
        constructor
        · exact fun h=>⟨h n (by omega),fun i he=> h i (by omega)⟩
        intro h i hi
        rcases Nat.eq_or_lt_of_le (Nat.le_of_lt_succ hi) with he|he
        · simpa [he] using h.1
        · exact h.2 i he
     rw [he]; change (if f x (a,n) then n+1 else _)=0 ↔ _
     cases h:f x (a,n) <;> simp [← ih,tube]
45
lemma uniquely {α} {f:PT (α×ℕ) Bool} {x a n i}
    (h:tube f x a n=i+1) : i < n ∧ f x (a,i)=true:=by
proof · 8 lines
   induction n with
   | zero=> simp [tube,forNat] at h
   | succ n ih=>
     change (if f x (a,n) then n+1 else tube f x a n)=i+1 at h
     split at h
     · have he:i=n:=by omega
       subst i; exact ⟨by omega,by assumption⟩
     obtain ⟨ht,hh⟩:=ih h; exact ⟨by omega,hh⟩
56
lemma tubeK {α} [Wire α] {f:PT (α×ℕ) Bool}{n:PT α ℕ}(h:Kn f)(he:Kn n):
     Kn (fun x a=>tube f x a (n x a)):=
proof · 3 lines
   Kn.stroll (fun x (b:α×ℕ×ℕ)=>if f x (b.1,b.2.1) then b.2.1+1 else b.2.2)
       n (fun _ _=>0) n ((h.comp (Kn.first.tuple Kn.next.left)).cond Kn.next.left.succ Kn.next.right)
         he (Kn.fix 0) he (fun _ _ _ hv=> le_trans (tube_hi ..) hv)
62
def test (_:Word)(a:ℕ×ℕ):Bool :=
body · 1 line
    if a.2 < 2 then false else decide (a.1%a.2=0)
65
def primex (x:Word)(k:ℕ):Bool :=
body · 1 line
    if k<2 then false else decide (tube test x k k=0)
68
lemma divK : Kn test:= Kn.next.cond_lt (Kn.fix _) (Kn.fix _) ((Kn.first.mod Kn.next).eq (Kn.fix _))
69
lemma pxK : Kn primex:= Kn.identity.cond_lt (Kn.fix _) (Kn.fix _)
proof · 1 line
            ((tubeK divK Kn.identity).eq (Kn.fix _))
72
lemma px_eq (x n): primex x n=true↔n.Prime:=by
proof · 22 lines
   unfold primex
   by_cases ht:n<2
   · simp only [ht,ite_true,Bool.false_eq_true,false_iff]
     intro h; have he:=h.two_le; omega
   rw [ite_eq_right ht,decide_eq_true_eq,unique_small]
   constructor
   · intro h
     rw [Nat.prime_def_lt]
     refine ⟨by omega,?_⟩
     intro m hm he
     have hs:= h _ hm
     have h0 : m≠0:= by rintro rfl; simp at he; omega
     by_cases hb:m<2
     · omega
     simp [test,hb,Nat.mod_eq_zero_of_dvd he] at hs
   intro h m hm
   unfold test; split
   · rfl
   next hh=>
      by_contra! hv
      have hz:m∣n:= Nat.dvd_of_mod_eq_zero (by simpa using hv)
      have he:= (Nat.dvd_prime h).1 hz; omega
96
def atp (x:Word) (a:ℕ×ℕ):Bool :=
body · 1 line
    if a.1 < a.2 then primex x a.2 else false
98
def last (x:Word) (n:ℕ) := tube atp x n (2*n+1)-1
99
lemma last_prime (x n)(h:0<n) : (last x n).Prime ∧ n < last x n ∧ last x n≤ 2*n:= by
proof · 11 lines
    obtain ⟨p,hp,ht,hk⟩:= Nat.bertrand n (by omega)
    have hl: atp x (n,p)=true:=by rw [atp]; simp [ht,(px_eq ..).2 hp]
    unfold last
    cases hh:tube atp x n (2*n+1) with
    | zero=> have he:=unique_small.1 hh p (by omega); simp_all
    | succ i=>
      have he:=uniquely hh
      simp only [Nat.add_sub_cancel]
      unfold atp at *; split at he
      · exact ⟨(px_eq ..).1 he.2,by assumption,by omega⟩
      · grind
112
lemma lastK : Kn last:=(tubeK (Kn.first.cond_lt Kn.next (pxK.comp Kn.next) (Kn.fix _))
proof · 1 line
                     (((Kn.fix 2).times Kn.identity).succ)).pred
124
structure Rock (q w h:ℕ) where
body · 3 lines
   P:Base.Source q w h
   w:Wares
   pad:ℕ
131
def B (x:Word):ℕ:= p.pad+lb (p.P.size x)+ lb (x.length+2)
132
def lamp (x:Word):ℕ:=2^(p.w.ceiling (p.B x))+p.w.p.M
133
def n (x:Word):ℕ:=Prime.last x (p.lamp x)
134
instance primed (x:Word) : Fact (p.n x).Prime:=⟨(Prime.last_prime x (p.lamp x)
body · 1 line
        (by unfold lamp; positivity)).1⟩
136
lemma nf (x:Word): p.w.p.M<p.n x ∧ 2^(p.w.ceiling (p.B x))≤ p.n x:=by
proof · 2 lines
    have he:p.lamp x < p.n x:=(Prime.last_prime x (p.lamp x) (by unfold lamp; positivity)).2.1
    unfold lamp at he; constructor <;> first|exact le_trans (Nat.le_add_left _ _) he|exact (Nat.le_add_right _ _).trans he.le
139
noncomputable def matter0 (x:Word):Matter q w h (p.n x):=⟨p.P,p.w,x,p.B x⟩
140
lemma prompts (e:p.w.Proof) (h:6≤p.pad) (ht:e.T.bank ≤ Est.box p.pad) (x:Word) :
      (p.matter0 x).ready e := by
proof · 3 lines
    have hp:p.pad≤p.B x:=by unfold B; omega
    exact ⟨h.trans hp, (below (p.P.size x)).le.trans (Nat.pow_le_pow_right Nat.zero_lt_two (by change lb (p.P.size x) ≤ p.B x; unfold B; omega)),
           ht.trans (Est.box_mono hp), (p.nf x).1,(p.nf x).2⟩
146
lemma sizeE :E (fun x _=>p.B x):= ((E.const p.pad).add (lbE p.P.eSize)).add
proof · 1 line
                  (lbE (E.length.add (E.const 2)))
148
lemma codeE : E (fun x _=>2^p.B x):= by
proof · 3 lines
    have hh:= ((E.const (2^p.pad)).mul (digitsE p.P.eSize)).mul
                       (digitsE (E.length.add (E.const 2)))
    simpa only [B,pow_add] using hh
155
lemma BKn:Kn (fun x (_:α)=>p.B x):=Kn.word _ p.sizeE
156
lemma codeKn:Kn (fun x (_:α)=> (2:ℕ)^p.B x):=Kn.word _ p.codeE
158
abbrev linE (a b:ℕ) (x:Word) := a + b*p.B x
159
lemma exprKn (i j):Kn (fun x (_:α)=>(2:ℕ)^p.linE i j x):=by
proof · 2 lines
    simpa only [linE, mul_comm j, pow_add,pow_mul] using
                  (Kn.fix (α:=α) (2^i)).times (Kn.power j p.codeKn)
162
lemma affine :∃ a b,∀ x,p.w.ceiling (p.B x)=p.linE a b x:=by
proof · 4 lines
   let m:=p.w.p.M
   let g:=p.w.A0
   use 8*g+1,8*(4+g*(Net.lag p.w.p)+(1+m)+2)+1+(1+m)
   intro x; unfold Wares.ceiling Wares.top Wares.tolls Wares.density linE Net.coreR m g; ring
167
lemma lampK:Kn (fun x (_:α)=>p.lamp x):=by
proof · 3 lines
   obtain ⟨a,b,h⟩:=p.affine
   unfold lamp; simp only [h]
   exact (p.exprKn a b).add (Kn.fix _)
171
lemma nKn :Kn (fun x (_:α)=> p.n x):= Prime.lastK.comp p.lampK
184
abbrev Page := Four ℕ
185
abbrev Stamp:= Page×Page
186
abbrev Issue:=Stamp×Stamp×Stamp
193
def ncard (x:V n):Page := vmap (fun i=> i.val) x
194
def den (n:ℕ)(x:Page) : V n := vmap (fun (i:ℕ)=> (i: ZMod n)) x
195
def badge (s:Channel n):Stamp :=(ncard s.1.val,ncard s.2)
196
def issued (s:Env n 0):Issue:=(badge s.1,badge s.2.1,badge s.2.2.1)
197
lemma nr (x:V n):den n (ncard x)=x:= by simp [den,ncard,vmap]
198
lemma xi : Function.Injective (@badge n) := by
proof · 4 lines
   intro a b h
   have he1 : a.1.val=b.1.val:=by rw [← nr a.1.val,← nr b.1.val, show ncard a.1.val=ncard b.1.val from
                                                           congrArg Prod.fst h]
   exact Prod.ext (Subtype.ext he1) (by rw [← nr a.2,← nr b.2,show ncard a.2=ncard b.2 from congrArg Prod.snd h])
203
instance : Subsingleton (Salt 0):=inferInstanceAs (Subsingleton (V 1))
204
lemma xu : Function.Injective (@issued n) := by
proof · 3 lines
   rintro ⟨v,w,t,r⟩ ⟨v',w',t',r'⟩ h
   unfold issued at h; simp only [Prod.mk.injEq] at h ⊢
   exact ⟨xi h.1,xi h.2.1,xi h.2.2,Subsingleton.elim ..⟩
209
def barr (a ℓ:ℕ×ℕ):Prop:=a.1<ℓ.1∧a.2<ℓ.2
210
def brief (a:Page)(n:ℕ):Prop:=barr a.1 (n,n) ∧ barr a.2 (n,n)
211
def on (a:Stamp)(n:ℕ):Prop:= brief a.1 n ∧ a.1≠0 ∧ brief a.2 n
212
def revealed (a:Issue)(n:ℕ):Prop:=on a.1 n ∧ on a.2.1 n ∧ on a.2.2 n
213
instance (a:Page)(n):Decidable (brief a n):=by unfold brief barr;infer_instance
214
instance (a:Stamp)(n):Decidable (on a n):=by unfold on;infer_instance
215
instance (a:Issue)(n):Decidable (revealed a n):=by unfold revealed;infer_instance
216
lemma nsmall (x:V n): brief (ncard x) n := by simp [ncard,vmap,brief,barr,ZMod.val_lt]
218
lemma rn (x:Page)(h:brief x n): ncard (den n x)=x:= by
proof · 2 lines
   obtain ⟨⟨ht,hf⟩,hq,hz⟩:=h
   simp [ncard,den,vmap,ZMod.val_natCast,Nat.mod_eq_of_lt,ht,hf,hq,hz]
221
lemma valid (s:Channel n):on (badge s) n:= by
proof · 5 lines
   refine ⟨nsmall _,?_,nsmall _⟩
   change ncard s.1.val≠_
   intro he
   apply s.1.prop
   rw [← nr s.1.val,he]; simp [den,vmap,Prod.ext_iff]
227
lemma built (x:Stamp)(h:on x n):∃ s:Channel n,badge s=x:=by
proof · 4 lines
   obtain ⟨ha,hb,hk⟩:=h
   have hp : den n x.1 ≠0:= by
      intro he; apply hb; rw [← rn x.1 ha,he]; simp [ncard,vmap,Prod.ext_iff]
   exact ⟨(⟨den n x.1,hp⟩,den n x.2),by simp [badge,rn x.1 ha,rn x.2 hk]⟩
232
lemma constit (s:Env n 0):revealed (issued s) n :=⟨valid _,valid _,valid _⟩
233
lemma fill (x:Issue)(h:revealed x n): ∃ s:Env n 0,issued s=x:=by
proof · 5 lines
   rcases x with ⟨a,b,c⟩
   obtain ⟨u,rfl⟩:= built a h.1
   obtain ⟨v,rfl⟩:= built b h.2.1
   obtain ⟨w,rfl⟩:= built c h.2.2
   exact ⟨(u,v,w,(0:Salt 0)),rfl⟩
240
def wallP (n:ℕ):Page:=((n,n),(n,n))
241
def ink (n:ℕ):Stamp:=(wallP n,wallP n)
242
def pages (n:ℕ):ℕ:=enc ((ink n,ink n,ink n):Issue)+1
243
lemma boxbound (s:Env n 0):enc (issued s)< pages n:=by
proof · 5 lines
   have he(q:V n):enc (ncard q)≤enc (wallP n):= by
      obtain ⟨⟨ha,hb⟩,hc,hd⟩:=nsmall q
      exact bead_mono (pair_mono ha.le hb.le) (pair_mono hc.le hd.le)
   have hp(x:Channel n): enc (badge x) ≤ enc (ink n):= bead_mono (he _) (he _)
   exact Nat.lt_succ_of_le (bead_mono (hp _) (bead_mono (hp _) (hp _)))
250
def publish (n v:ℕ) : Bool:= decide (revealed (dec v:Issue) n ∧ enc (dec v:Issue)=v)
251
def spine (n:ℕ) : Finset ℕ:= (range (pages n)).filter (fun i=> publish n i)
252
lemma entry (s:Env n 0) : enc (issued s)∈spine n:= by
proof · 2 lines
    rw [spine,mem_filter]; exact ⟨mem_range.2 (boxbound _),
                                   by simp [publish,constit]⟩
255
lemma owner {x}(h:x∈spine n): ∃ s:Env n 0, enc (issued s)=x:=by
proof · 4 lines
   obtain ⟨ht,he⟩:=mem_filter.1 h
   obtain ⟨hi,hf⟩:= of_decide_eq_true he
   obtain ⟨s,h⟩:= fill (dec x) hi
   exact ⟨s,by rw [h,hf]⟩
260
lemma columns {n:ℕ} [Fact n.Prime] (f:ℕ→ℝ):
   ∑ i∈spine n,f i=∑ s:Env n 0, f (enc (issued s)):=by
proof · 5 lines
   classical
   symm
   refine sum_nbij (fun s:Env n 0=>enc (issued s)) (fun x _=>entry x) (fun s _ t _ h=>?_)
               (fun x hx=>by obtain ⟨s,h⟩:=owner hx; exact ⟨s,mem_univ _,h⟩) fun _ _=>rfl
   exact xu (by simpa using congrArg (fun a=> (dec a:Issue)) h)
271
lemma both {f g:PT α Bool}(h:Kn f)(k:Kn g):Kn (fun x a=> f x a&&g x a):=by
proof · 2 lines
   convert h.cond k (Kn.fix false) using 1
   ext x a; cases hx:f x a <;> simp []
274
lemma bytesK {f:PT α Page}{g:PT α ℕ}(h:Kn f)(k:Kn g):
    Kn (fun x a=> decide (brief (f x a) (g x a))):=by
proof · 6 lines
   convert both (both (h.left.left.lt k) (h.left.right.lt k)) (both (h.right.left.lt k) (h.right.right.lt k))
       using 1
   ext x a
   rw [Bool.eq_iff_iff]
   simp only [decide_eq_true_eq, Bool.and_eq_true]
   rfl
282
lemma onK {f:PT α Stamp}{g:PT α ℕ}(h:Kn f)(k:Kn g):
    Kn (fun x a=>decide (on (f x a) (g x a))):=by
proof · 10 lines
   let t:=h.left
   have he:Kn (fun x a=> decide ((f x a).1≠0)):=by
      convert bneg _ ((Kn.wash.comp h.left).eq (Kn.fix (enc (0:Page)))) using 1
      ext x a; rw [Bool.eq_iff_iff]; simp; exact ⟨fun e h=>e (by
                              simpa using congrArg (fun n=>(dec n:Page)) h), fun e h=>e (by rw [h])⟩
   convert both (bytesK h.left k) (both he (bytesK h.right k)) using 1
   ext x a
   rw [Bool.eq_iff_iff]
   simp only [decide_eq_true_eq, Bool.and_eq_true]
   rfl
294
lemma pagesK {f:PT α ℕ}(h:Kn f):
    Kn (fun x a=>pages (f x a)):= by
proof · 2 lines
   have ha:=h.tuple h; have hb:=ha.tuple ha; have ht:=hb.tuple hb
   exact (Kn.wash.comp (ht.tuple (ht.tuple ht))).succ
298
lemma pubK {f g:PT α ℕ}(h:Kn f)(k:Kn g):
    Kn (fun x a=>publish (f x a) (g x a)):=by
proof · 7 lines
   have he:=(Kn.unpack (α:=Issue)).comp k
   have hz:=both (onK he.left h) (both (onK he.right.left h) (onK he.right.right h))
   convert both hz ((Kn.wash.comp he).eq k) using 1
   ext x a
   rw [Bool.eq_iff_iff]
   simp only [publish, decide_eq_true_eq, Bool.and_eq_true]
   rfl
320
def ms (n i x y:ℕ) := (x+i*y)%n
321
def zi (n i:ℕ ) (x y:ℕ×ℕ) := (ms n i x.1 y.1,ms n i x.2 y.2)
322
def mix (n i:ℕ ) (a b:Page):Page:=(zi n i a.1 b.1,zi n i a.2 b.2)
325
lemma valms (i:ℕ ) (x y:ZMod n): (x+(i:ZMod n)*y).val=ms n i x.val y.val:=by
proof · 2 lines
    have he:(x+(i:ZMod n)*y)= ↑(x.val+i*y.val):=by simp
    rw [he,ZMod.val_natCast]; rfl
328
lemma nadd (i:ℕ ) (x y:V n): ncard (x+(i:ZMod n)•y)=mix n i (ncard x) (ncard y) :=
proof · 1 line
    Prod.ext (Prod.ext (valms ..) (valms ..)) (Prod.ext (valms ..) (valms ..))
331
def shoot (n k x:ℕ ) (p:Stamp):Bool:=decide (brief (mix n x p.2 p.1 ) (side n k))
332
lemma shoots (a:Channel n) (k i):
    shoot n k i (badge a)=true ↔ hit k (i:ZMod n) a := by
proof · 1 line
   unfold shoot badge hit vEval; dsimp only; rw [← nadd,decide_eq_true_eq]; simp [ncard,brief,barr,vmap,restr,each,and_assoc]
336
def stretch (n s t:ℕ) (p:Stamp):Stamp:=(mix n s 0 p.1,mix n t p.2 p.1)
337
lemma stretchy (a:Channel n ) (s t:ℕ) (h:(s:ZMod n)≠0) :
   stretch n s t (badge a)=badge (affineChannel (↑s:ZMod n) ↑t h a) := by
proof · 7 lines
   let w:= a.1.val
   unfold stretch badge
   change (_,_)= (ncard ((s:ZMod n)•w), ncard (a.2+(t:ZMod n)•w))
   have he: ncard (0:V n)=0:=by simp [ncard,vmap,Prod.ext_iff]
   apply Prod.ext _ (nadd t a.2 w).symm
   rw [← zero_add ((s:ZMod n) • w)]
   rw [nadd,he]
350
lemma asideK (h:Kn x ) (e:Kn k):Kn (fun w a=>side (x w a ) (k w a)):= by
proof · 5 lines
    convert (((h.pred.div (Kn.cap h.succ (Kn.fix 2) e)).succ)) using 1
    ext w a; unfold side
    by_cases he :2^(k w a) ≤ x w a+1
    · rw [min_eq_right he]
    rw [min_eq_left (by omega),Nat.div_eq_of_lt (by omega),Nat.div_eq_of_lt (by omega)]
357
lemma msK (hr:Kn p) (ht:Kn k ) (h:Kn x ) (e:Kn y):Kn (fun w a=>ms (p w a) (k w a) (x w a) (y w a)):=
proof · 1 line
    (h.add (ht.times e)).mod hr
359
lemma ziK (hr:Kn p ) (ht:Kn k){u v:PT α (ℕ×ℕ)}(h:Kn u ) (e:Kn v):
    Kn (fun w a=>zi (p w a ) (k w a ) (u w a ) (v w a)):=
proof · 1 line
    (msK hr ht h.left e.left).tuple (msK hr ht h.right e.right)
362
lemma mixK (hr:Kn p ) (ht:Kn k){u v:PT α Page}(h:Kn u ) (e:Kn v):
    Kn (fun w a=>mix (p w a ) (k w a ) (u w a ) (v w a)):=
proof · 1 line
       (ziK hr ht h.left e.left).tuple (ziK hr ht h.right e.right)
365
lemma shootK(hr:Kn p ) (ht:Kn k ) (h:Kn x){f:PT α Stamp}(e:Kn f):
    Kn (fun w a=>shoot (p w a ) (k w a ) (x w a ) (f w a)):=bytesK (mixK hr h e.right e.left) (asideK hr ht)
367
lemma stretchK (hr:Kn p ) (ht:Kn k ) (h:Kn x){f:PT α Stamp}(e:Kn f):
    Kn (fun w a=> stretch (p w a ) (k w a ) (x w a ) (f w a)) :=
proof · 1 line
       (mixK hr ht (Kn.fix _) e.left).tuple (mixK hr h e.right e.left)
385
def camp (x:Word):ℕ:= p.w.p.M^p.B x*p.P.first x
386
lemma camps (x) : p.camp x=(p.matter0 x).i:=by
proof · 8 lines
   let z:=p.matter0 x
   have he (n): z.s.H.child z.x0 n=p.w.p.M^n*p.P.first x:=by
       induction n with
       | zero=> change p.P.first x=_; simp
       | succ n ih=> rw [Prize.child,ih]
                     unfold radix; change _+p.w.p.M*_=_
                     ring
   exact (he _).symm
395
lemma campK {α} [Wire α]:Kn (fun x (_:α)=>p.camp x):=by
proof · 8 lines
   let k:=p.w.p.M
   have hf:Kn (fun x (_:α)=>(k)^p.B x):=by
      convert (Kn.cap (p.exprKn 0 k) (Kn.fix k) p.BKn) using 1
      ext x a; symm; apply min_eq_right
      rw [linE,Nat.zero_add,pow_mul]
      have hh:= growing k
      gcongr; omega
   exact hf.times (Kn.word _ p.P.eFirst)
405
def qualify (x:Word) (a:ℕ) : Bool :=
body · 1 line
     Print.publish (p.n x) a && Print.shoot (p.n x) (p.B x) (p.camp x) (dec a:Issue).1
408
abbrev printid (x:Word)(s:Pool (p.n x)) := enc (Print.issued s)
409
lemma verified (x:Word) (s:Pool (p.n x)):
    p.qualify x (p.printid x s)= true ↔ (p.matter0 x).polls s:=by
proof · 9 lines
    have he:= (Print.entry s)
    have hk:= (mem_filter.1 he).2
    change Print.publish (p.n x) (p.printid x s)=true at hk
    rw [qualify, Bool.and_eq_true,hk]
    simp only [true_and]
    unfold printid; rw [Wire.eq]
    change _=true ↔ hit (p.B x) (↑(p.matter0 x).i:ZMod (p.n x)) s.1
    rw [← camps]
    exact Print.shoots ..
420
lemma qualifyK:Kn p.qualify:=
proof · 1 line
    Print.both (Print.pubK p.nKn Kn.identity) (Print.shootK p.nKn p.BKn p.campK (Kn.unpack (α:=Issue)).left)
423
lemma numbers (x) (f:ℕ→ℕ):
   (↑(∑ i∈range (Print.pages (p.n x)),if p.qualify x i then f i else 0):ℝ)=
        ∑ s∈ univ.filter (fun s:Pool (p.n x)=> (p.matter0 x).polls s),(f (p.printid x s):ℝ) := by
proof · 19 lines
   classical
   let n:=p.n x
   let : Fact n.Prime:=p.primed x
   let F i:= (↑(if p.qualify x i then f i else 0):ℝ)
   push_cast
   calc
      _ = ∑ i∈Print.spine n,F i := by
             rw [Print.spine,sum_filter]
             refine sum_congr rfl fun i _=> ?_
             unfold F qualify
             by_cases h: Print.publish (p.n x) i=true
             · simp [h,n]
             simp [h,n]
      _ = _:=by
          rw [Print.columns (n:=n),sum_filter]
          refine sum_congr rfl fun i hi=>?_
          unfold F
          change (↑(if p.qualify x (p.printid x i) then f (p.printid x i) else 0):ℝ)=_
          split <;> have ht:= p.verified x i <;> grind
446
def sumtop (x:Word)(f:ℕ→ℕ) :ℕ:=
body · 1 line
       ∑ i∈range (Print.pages (p.n x)), if p.qualify x i then min 64 (f i) else 0
448
lemma totalK (f:PT ℕ ℕ)(h:Kn f){α}[Wire α]: Kn (fun x (_:α)=> p.sumtop x (f x)):=by
proof · 16 lines
   let g:PT (α × ℕ × ℕ) ℕ:= fun x a=> a.2.2+
                            (if p.qualify x a.2.1 then min 64 (f x a.2.1) else 0)
   have hg:Kn g:= Kn.next.right.add ((p.qualifyK.comp Kn.next.left).cond
                                        ((Kn.fix _).small (h.comp Kn.next.left)) (Kn.fix _))
   have hp (x a j):forNat (fun k v=>g x (a,k,v)) 0 j=
                   ∑ i∈range j, (if p.qualify x i then min 64 (f x i) else 0):=by
       induction j with
       | zero=> simp [forNat]
       | succ i ih=> rw [sum_range_succ,← ih]; rfl
   have ht:Kn (fun x (_:α)=> Print.pages (p.n x)):=Print.pagesK p.nKn
   simpa only [hp,sumtop] using Kn.stroll g _ (fun _ _=>0) _ hg ht (Kn.fix 0) (ht.times (Kn.fix 64)) (by
      intro x a B he
      rw [hp]
      apply le_trans (b:=∑ i∈range B,(64:ℕ))
      · exact sum_le_sum (fun i hi=>by split <;> omega)
      · simpa using Nat.mul_le_mul_right 64 he)
466
def census (x:Word):ℕ:=p.sumtop x (fun _=>1)
467
lemma witness0 (x): (p.census x:ℝ) = (univ.filter (fun s:Pool (p.n x)=> (p.matter0 x).polls s)).card:= by
proof · 1 line
   simpa only [census,sumtop,min_eq_right (show 1≤(64:ℕ) by omega),sum_const,nsmul_eq_mul,Nat.cast_one,mul_one] using p.numbers x (fun _=>1)
469
lemma positive0 (x):0 < p.census x:=by
proof · 5 lines
   have h0:= (p.matter0 x).quorum
   have hz:= p.witness0 x
   have ht:p.census x=(univ.filter (fun s:Pool (p.n x)=>(p.matter0 x).polls s)).card:=by
        exact_mod_cast hz
   rwa [ht]
476
def survey (f:PT ℕ ℕ)(x)(s:Pool (p.n x)):ℝ:= (↑(min 64 (f x (p.printid x s))):ℝ) /64
477
lemma countok (f:PT ℕ ℕ)(x) :
    p.sumtop x (f x)=p.sumtop x (f x) ∧
       ((p.sumtop x (f x):ℝ)/64) = (p.census x:ℝ)*(p.matter0 x).evidence (p.survey f x):=by
proof · 6 lines
   refine ⟨rfl,?_⟩
   rw [p.witness0,← Matter.tally_eq]
   unfold survey
   rw [← sum_div]
   congr 1
   exact p.numbers x (fun v=> min 64 (f x v))
487
def answer (f:PT ℕ ℕ) (x:Word) :ℕ :=
body · 1 line
   if p.census x*32 < p.sumtop x (f x) then 1 else 0
489
lemma zenK {n:PT ℕ ℕ} (h:Kn n) (x : Word → ℕ) (ht : n=fun w _=>x w):
   E (fun y _=> x y):= by
proof · 2 lines
   rw [ht] at h
   exact h (E.const 0)
493
lemma censusK : E (fun x _=> p.census x):=
proof · 1 line
   zenK (p.totalK (α:=ℕ) _ (Kn.fix 1)) _ (by rfl)
495
lemma tallyK (f:PT ℕ ℕ)(h:Kn f) : E (fun x _=> p.sumtop x (f x)) :=
proof · 1 line
   zenK (p.totalK (α:=ℕ) _ h) _ rfl
497
lemma annK (f:PT ℕ ℕ)(h: Kn f): E (fun x _=> p.answer f x):=
proof · 1 line
        (p.censusK.mul (E.const 32)).ite_lt (p.tallyK f h) (E.const 1) (E.const 0)
500
lemma scale_cnv {t:ℚ}: (t ≤ (1:ℚ)/3 → (t:ℝ) ≤1/3) ∧ ((2:ℚ)/3≤ t → (2:ℝ)/3≤ t):= by
proof · 5 lines
    constructor <;> intro h
    · have hp:(↑t:ℝ)≤ ↑((1:ℚ)/3):= Rat.cast_le.mpr h
      push_cast at hp; exact hp
    have hp:(↑((2:ℚ)/3):ℝ) ≤ (t:ℝ):= Rat.cast_le.mpr h
    push_cast at hp; exact hp
506
lemma certificate {t v:ℝ}{a b:ℕ} {h:Prop} (hp: -(1/8:ℝ)< t-v ∧ t-v<1/8)
    (h0:0<(a:ℝ)) (hx:(b:ℝ)/64=a*v) (ht:¬h→ t≤ 1/3) (hu:h → 2/3≤ t):
     a*32 < b ↔ h:=by
proof · 10 lines
       constructor
       · intro hs
         by_contra hv
         have hr:=ht hv
         have he:(a:ℝ)*32 < b:=by exact_mod_cast hs
         nlinarith [hp.1,hp.2]
       intro hs
       have hz:=hu hs
       have hc:(a:ℝ)*32 < b:=by nlinarith [hp.1,hp.2]
       exact_mod_cast hc
519
theorem convince {A:Language}(hS:p.P.Space)(p1: p.w.Proof)(hv:6≤p.pad)
    (hm:p1.T.bank≤Est.box p.pad)
    (fo:PT ℕ ℕ)(he:Kn fo)
    (ha:∀ x (s:Pool (p.n x)),
       let m:= p.matter0 x
       m.polls s → |p.survey fo x s-m.oracleV s m.i|≤ 1/32)
    (hs:∀ x∈A, (2/3:ℚ) ≤ p.P.mach.acceptanceProbability x (p.P.clock x))
    (ht:∀ x, x∉A → p.P.mach.acceptanceProbability x (p.P.clock x) ≤ (1/3:ℚ)):
    A∈ L :=by
proof · 18 lines
    apply (p.annK fo he).gives_L _
    intro x
    have hx:=(p.countok fo x).2
    have hp :=Matter.probes (p:=p1) (p.matter0 x) hS (p.prompts p1 hv hm x) (p.survey fo x) (ha x)
    let t:ℝ:=p.P.mach.acceptanceProbability x (p.P.clock x)
    let v:ℝ:=(p.matter0 x).evidence (p.survey fo x)
    change |t-v|<_ at hp
    rw [abs_lt] at hp
    let a:=p.census x
    let b:=p.sumtop x (fo x)
    have h1 :0<(a:ℝ):=by exact_mod_cast p.positive0 x
    change (b:ℝ)/_=↑a*v at hx
    change p.answer fo x≠0↔ _
    unfold answer; change (if a*32<b then _ else _)≠_ ↔ _
    suffices hh:a*32 < b ↔x ∈ A by simpa using hh
    apply certificate hp h1 hx ?_ ?_
    · intro hy; exact scale_cnv.1 (ht x hy)
    intro hy; exact scale_cnv.2 (hs x hy)
559
abbrev Z4:=Four ℤ
561
def zd (a:Page):Z4:=vmap (fun i:ℕ=>(i:ℤ)) a
562
def red (n:ℕ ) (a:Z4) :Page:=vmap (fun i:ℤ=>(i%(n:ℤ)).toNat) a
564
def change0 (n:ℕ ) (g:Gen ) (v:Page):Page:=red n (pol false g (zd v))
565
def change1 (n:ℕ ) (g:Gen ) (v:Page ) (k:ℕ ) (w:W):Page:=
body · 1 line
    red n (pol true g (zd v)+addin (side n k) w)
567
def bare (n:ℕ ) (g:Gen ) (s:Stamp):Stamp:=(change0 n g s.1,red n (pol true g (zd s.2)))
568
def walkstep (n:ℕ ) (g:Gen ) (k:ℕ ) (w:W ) (s:Stamp):Stamp:=
body · 1 line
         (change0 n g s.1,change1 n g s.2 k w)
571
def reducep (a:Page ) (m:ℕ):Page:= vmap (· % m) a
572
def fetch (n k i:ℕ ) (g:Gen ) (s:Stamp):W:=
body · 1 line
    guess (side n k) (pol true g (zd (reducep (mix n i s.2 s.1) (side n k))))
577
lemma readout (v:V m) : zd (ncard v)=raiseV v:= rfl
578
lemma redCast (v:Z4):red m v=ncard (reduceV m v):=by
proof · 3 lines
    have h(i:ℤ):(i%(m:ℤ)).toNat=(i:ZMod m).val:=by
       rw [← ZMod.val_intCast]; omega
    simp only [red,ncard,vmap,reduceV,h]
583
lemma reducePol_any (b g) (t: Z4) : reduceV m (pol b g t)=pol b g (reduceV m t) := by
proof · 11 lines
   cases b
   · unfold pol; split
     all_goals
       unfold reduceV lower vmap
       cases he:g.2 with
       | inl u=>
          cases u with
          | inl b=> rcases b with ⟨a,b⟩; cases a <;> simp
          | inr b=>cases b <;> simp
       | inr u=> rcases u with ⟨a,b⟩; cases a <;> cases b <;> simp
   · exact reducePol g t
596
lemma prints_any (v:V m ) (b g):red m (pol b g (zd (ncard v)))=ncard (act b g v):=by
proof · 1 line
    rw [readout,redCast,reducePol_any,reraise,act_formula]
598
lemma recast (g ) (s:Channel n):
    bare n g (badge s)=badge (gens n g s):=by
proof · 2 lines
   let a:=s.1.val; let b:=s.2
   exact Prod.ext (prints_any a false g) (prints_any b true g)
602
lemma zoom (g k w ) (s:Channel n):
    walkstep n g k w (badge s)=badge (Variant k g w s):=by
proof · 4 lines
    let a:=s.1.val; let b:=s.2
    have he:change1 n g (ncard b) k w=ncard (act true g b+reduceV n (addin (side n k) w)):=by
        rw [change1,redCast,reduce_add,readout,reducePol,reraise,act_formula]
    exact Prod.ext (prints_any a false g) he
609
lemma fetching (k i:ℕ ) (g ) (s:Channel n):
    fetch n k i g (badge s)=guessP k g (closeP k (↑i:ZMod n) s):= by
proof · 6 lines
     let v:=vEval s (↑i:ZMod n)
     have he:mix n i (badge s).2 (badge s).1=ncard v:=(nadd ..).symm
     rw [fetch,he]
     have ht: reducep (ncard v) (side n k)=ncard (vshort k v):= by
        simp only [reducep,ncard,vshort,vmap,ZMod.val_natCast]
     rw [ht,readout]; rfl
619
lemma quads {s:PT α (Four β)} (h:Kn s ) (f:PT (α×β) γ ) (e:Kn f):
    Kn (fun x a=>vmap (fun i=>f x (a,i)) (s x a)):=by
proof · 4 lines
    exact ((e.comp (Kn.identity.tuple h.left.left)).tuple
                (e.comp (Kn.identity.tuple h.left.right))).tuple
             ((e.comp (Kn.identity.tuple h.right.left)).tuple
                (e.comp (Kn.identity.tuple h.right.right)))
625
lemma zdK {s:PT α Page}(h:Kn s):Kn (fun x a=>zd (s x a)):=quads h (fun _ (a:α×ℕ)=>(a.2:ℤ)) Kn.next.ZofN
627
lemma holeK {x:PT α ℤ} {m:PT α ℕ} (h:Kn x ) (e:Kn m) :
    Kn (fun w a=> x w a%((m w a:ℕ):ℤ)):= by
proof · 4 lines
    convert e.cond_eq (Kn.fix 0) h (h.modulus e.pred) using 1
    ext w a; split
    · simp_all
    · next hz=> simp [Nat.sub_add_cancel (show 1 ≤ m w a by omega)]
634
lemma redK {f:PT α Z4} {m:PT α ℕ}(h:Kn f ) (e:Kn m):
    Kn (fun w a=> red (m w a ) (f w a)) :=
proof · 1 line
       quads h (fun w (a:α×ℤ)=>(a.2%(m w a.1)).toNat) ((holeK Kn.next (e.comp Kn.first)).topNat)
637
lemma plow (b:Bool ) (g:Affine.Cmd) {s t:PT α (ℤ×ℤ)} (h:Kn s ) (e:Kn t):
    Kn (fun x a => lower b g (s x a) (t x a)):= by
proof · 19 lines
    cases g with
    | inl c=>
      cases c with
      | inl c=>
        rcases c with ⟨w,z⟩; cases w
        · exact h.left.tuple (h.right.plusZ ((Kn.fix (-(amount z:ℤ))).prodZ h.left))
        exact (h.left.plusZ ((Kn.fix (-(amount z:ℤ))).prodZ h.right)).tuple h.right
      | inr c=>
        cases b
        · exact h
        cases c
        · exact h.left.tuple (h.right.plusZ (Kn.fix _))
        exact (h.left.plusZ (Kn.fix _)).tuple h.right
    | inr c=>
      rcases c with ⟨i,j⟩; cases i <;> cases j
      · exact h.left.tuple (h.right.plusZ e.right)
      · exact h.left.tuple (h.right.plusZ e.left)
      · exact (h.left.plusZ e.right).tuple h.right
      exact (h.left.plusZ e.left).tuple h.right
659
lemma polK {f:PT α Z4}(h:Kn f ) (b:Bool ) (g:Gen):Kn (fun w a=> pol b g (f w a)):= by
proof · 3 lines
   rcases g with ⟨i,j⟩; cases i
   · exact (plow b j h.left h.right).tuple h.right
   exact h.left.tuple (plow b j h.right h.left)
664
lemma plusK {f g:PT α Z4}(h:Kn f ) (e:Kn g): Kn (fun w a=> f w a+g w a):=
proof · 2 lines
    ((h.left.left.plusZ e.left.left).tuple (h.left.right.plusZ e.left.right)).tuple
               ((h.right.left.plusZ e.right.left).tuple (h.right.right.plusZ e.right.right))
668
lemma addK {f:PT α ℕ}(h:Kn f ) (v:W):Kn (fun w a=> addin (f w a) v):=by
proof · 2 lines
    have he(i:Fin 11):Kn (fun w a=> (f w a:ℤ)*((i.val:ℤ)-5)) := h.ZofN.prodZ (Kn.fix _)
    exact ((he _).tuple (he _)).tuple ((he _).tuple (he _))
672
lemma bareK {f:PT α Stamp}(h:Kn f ) (g:Gen){p:PT α ℕ}(he:Kn p):
    Kn (fun x a=> bare (p x a) g (f x a)) :=
proof · 1 line
    (redK (polK (zdK h.left) false g) he).tuple (redK (polK (zdK h.right) true g) he)
675
lemma zoomK {f:PT α Stamp}(h:Kn f ) (g:Gen){p k:PT α ℕ}(hp:Kn p ) (hk:Kn k ) (v:W):
    Kn (fun x a=>walkstep (p x a) g (k x a) v (f x a)) :=
proof · 2 lines
   (redK (polK (zdK h.left) false g) hp).tuple
     (redK (plusK (polK (zdK h.right) true g) (addK (asideK hp hk) v)) hp)
680
lemma downK {x m:PT α ℕ}(h:Kn x ) (e:Kn m): Kn (fun w a=> x w a/m w a):= h.div e
681
lemma dropK {x:PT α ℤ} {m:PT α ℕ}(h:Kn x ) (e:Kn m):Kn (fun w a=> x w a/(m w a:ℕ)):=by
proof · 4 lines
   convert e.cond_eq (Kn.fix 0) (Kn.fix 0) (h.chop e.pred) using 1
   ext w a; split
   · next hz=> simp [show m w a=0 from hz]
   next hz=> simp [Nat.sub_add_cancel (show 1 ≤ m w a by omega)]
686
lemma pickK {f:PT α Z4}(h:Kn f) {m:PT α ℕ}(e:Kn m):
    Kn (fun w a=> vmap (fun i:Fin 11=>i.val) (guess (m w a ) (f w a))):=by
proof · 4 lines
    have hp {s:PT α ℤ}(hs: Kn s):Kn (fun w a=>(chooseOne (m w a) (s w a)).val) :=
        (Kn.fix 10).small (((Kn.fix 5).plusZ ((dropK hs e).negZ)).topNat)
    exact ((hp h.left.left).tuple (hp h.left.right)).tuple
          ((hp h.right.left).tuple (hp h.right.right))
692
lemma fetchK0 {p k z:PT α ℕ}{f:PT α Stamp}(h:Kn f ) (g:Gen ) (hp:Kn p ) (hk:Kn k ) (hz:Kn z):
    Kn (fun x a=>vmap (fun i:Fin 11=>i.val)
                       (fetch (p x a ) (k x a) (z x a) g (f x a))):=by
proof · 5 lines
   have he:= asideK hp hk
   let u (x a):=mix (p x a ) (z x a) ((f x a).2) ((f x a).1)
   have ht:Kn u:= mixK hp hz h.right h.left
   have hv := quads ht _ (Kn.next.mod (he.comp Kn.first))
   exact pickK (polK (zdK hv) true g) he
700
lemma fetchK {p k z:PT α ℕ}{f:PT α Stamp}(h:Kn f ) (g:Gen ) (hp:Kn p ) (hk:Kn k ) (hz:Kn z ) (v):
    Kn (fun x a=>decide (fetch (p x a ) (k x a ) (z x a) g (f x a)=v)):=by
proof · 11 lines
   let glyph (v:W):=enc (vmap (fun i:Fin 11=>i.val) v)
   have he : Function.Injective glyph:=by
     intro a b hx
     have hp:=congrArg (fun n=>(dec n:Page)) hx; unfold glyph at hp
     simp [vmap,Prod.ext_iff] at *
     exact ⟨⟨Fin.ext hp.1.1,Fin.ext hp.1.2⟩,Fin.ext hp.2.1,Fin.ext hp.2.2⟩
   let s x a:=fetch (p x a ) (k x a ) (z x a) g (f x a)
   convert (Kn.wash.comp (fetchK0 h g hp hk hz)).eq (Kn.fix (glyph v)) using 1
   ext x a
   change decide (s x a=v)=decide (glyph (s x a)=glyph v)
   simp [he.eq_iff]
727
abbrev Token:= Atom×Roller
729
def checkS (v:Token)(n k _ x:ℕ)(s:Issue):Bool :=
body · 5 lines
   if v.1.1 then match v.1.2 with
     | .inl i=> decide (fetch n k x i s.1=v.2)
     | .inr (.inl _)=>true
     | _=> decide (v.2=0)
   else decide (v.2=0)
735
def checkD (v:Token)(n _ j x:ℕ)(s:Issue):Bool :=
body · 4 lines
   if v.1.1 then match v.1.2 with
     | .inr (.inl i)=> decide (fetch n j x i s.2.1=v.2)
     | _=>true
   else true
740
def arch (v:Token)(n k j:ℕ)(s:Issue):Issue:=
body · 6 lines
   if v.1.1 then match v.1.2 with
     | .inl i=> (walkstep n i k v.2 s.1,s.2)
     | .inr (.inl i)=> (s.1,walkstep n i j v.2 s.2.1,s.2.2)
     | .inr (.inr (.inl i))=> (s.1,s.2.1,bare n i s.2.2)
     | _=>s
   else s
747
abbrev Bundle:=(ℕ×ℕ×ℕ)×ℕ×Issue
748
def tossR (b:Bool)(v:Token)(_ :Word)(a:Bundle):Bool×Issue:=
body · 3 lines
    let (n,k,j):=a.1
    let (x,s):=a.2
    (if b then checkS v n k j x s else checkD v n k j x s, arch v n k j s)
754
lemma rose (v:Token)(k j)(s:Env n 0):arch v n k j (issued s)=
                   issued (rol k j (d:=0) (w:=0) le_rfl v.1 v.2 s):=by
proof · 2 lines
    rcases v with ⟨⟨b,i|i|i|i⟩,v⟩ <;> cases b <;>
     rcases s with ⟨s,t,u,w⟩ <;> simp [issued,arch,rol,rollG,unionG,zoom,recast]
759
lemma landlord (v:Token)(k j x y:ℕ)(s:Env n 0):
    v.2=winner k j (↑x:ZMod n) (↑y:ZMod n) v.1 s ↔
          checkS v n k j x (issued s)=true ∧ checkD v n k j y (issued s)=true :=by
proof · 2 lines
   rcases s with ⟨s,t,u,w⟩; rcases v with ⟨⟨b,i|i|i|i⟩,v⟩ <;>
     cases b <;> simp [issued,winner,checkS,checkD,fetching,eq_comm]
765
lemma S_K(v:Token):
   Kn (fun (_:Word)(a:Bundle)=>checkS v a.1.1 a.1.2.1 a.1.2.2 a.2.1 a.2.2):=by
proof · 3 lines
    rcases v with ⟨⟨b,i|i|i|i⟩,v⟩ <;> cases b
    all_goals first| exact Kn.fix _ |
        exact fetchK Kn.next.right.left i Kn.first.left Kn.first.right.left Kn.next.left v
770
lemma D_K(v:Token):
   Kn (fun (_:Word)(a:Bundle)=>checkD v a.1.1 a.1.2.1 a.1.2.2 a.2.1 a.2.2):=by
proof · 2 lines
    rcases v with ⟨⟨b,i|i|i|i⟩,v⟩ <;> cases b <;> first| exact Kn.fix _ |
       exact fetchK Kn.next.right.right.left i Kn.first.left Kn.first.right.right Kn.next.left v
775
lemma A_K(v:Token):
   Kn (fun (_:Word)(a:Bundle)=> arch v a.1.1 a.1.2.1 a.1.2.2 a.2.2):=by
proof · 11 lines
   have hz:Kn (fun (_:Word)(a:Bundle)=>a.2.2):=Kn.next.right
   rcases v with ⟨⟨b,i|i|i|i⟩,v⟩ <;> cases b
   · exact hz
   · exact (zoomK hz.left i Kn.first.left Kn.first.right.left v).tuple hz.right
   · exact hz
   · exact hz.left.tuple
                     ((zoomK hz.right.left i Kn.first.left Kn.first.right.right v).tuple hz.right.right)
   · exact hz
   · exact hz.left.tuple (hz.right.left.tuple (bareK hz.right.right i Kn.first.left))
   · exact hz
   · exact hz
788
lemma tossed(b:Bool)(v) : Kn (tossR b v):= by
proof · 3 lines
   cases b
   · exact Kn.tuple (D_K v) (A_K v)
   · exact Kn.tuple (S_K v) (A_K v)
793
abbrev rad:=Fintype.card Token
794
lemma rad_pos:0<rad:= Fintype.card_pos
795
def number : Fin rad≃Token := (Fintype.equivFin Token).symm
796
def mark (i:ℕ):Token:=number ⟨i%rad,Nat.mod_lt _ rad_pos⟩
798
lemma mintK {α β} [Wire α][Wire β](f:Token→ PT α β) (h:∀ v,Kn (f v))
   (k:PT α ℕ)(hp:Kn k):Kn (fun x a=> f (mark (k x a)) x a):= by
proof · 26 lines
  let u(v:Fin rad):= v.val
  let m:Fin rad := ⟨0,rad_pos⟩
  classical
  have he (s: ℕ):
    Kn (fun x a=> let n:= (k x a)%rad
                  if n < s then f (mark n) x a else f (mark 0) x a):=by
     induction s with
     | zero=> simpa using h (mark 0)
     | succ s ih =>
       convert ((hp.mod (Kn.fix rad)).cond_eq (Kn.fix s) (h (mark s)) ih) using 1
       ext x a
       dsimp only
       generalize k x a % rad=n
       by_cases he:n=s
       · subst s
         rw [ite_eq_left (by omega),ite_eq_left rfl]
       rw [ite_eq_right he]
       by_cases ht:n<s
       · rw [ite_eq_left (by omega),ite_eq_left ht]
       rw [ite_eq_right (by omega),ite_eq_right ht]
  have ht (v):v%rad<rad:=Nat.mod_lt v rad_pos
  convert he rad using 1
  ext x a
  dsimp only
  rw [ite_eq_left (ht _)]
  simp only [mark,Nat.mod_mod]
827
def queue : (l c:ℕ) → Paths Atom l×Paths Roller l
body · 5 lines
   | 0,_=>((),())
   | l+1,c=>
     let u:=mark c
     let v:=queue l (c/rad)
     ((u.1,v.1),(u.2,v.2))
834
lemma width (r:ℕ)(_:0< r)(n:ℕ)(f:ℕ→ℝ):
   ∑ i∈range (r*n),f i = ∑ i∈range r, ∑ j∈range n,f (i+j*r):=by
proof · 7 lines
   induction n with
   | zero=> simp
   | succ n ih=>
     conv_rhs => arg 2; ext; rw [sum_range_succ]
     rw [mul_add,mul_one, sum_add_distrib,← ih]
     have ht := sum_range_add f (r*n) r
     rw [ht]; simp [Nat.add_comm,mul_comm]
844
lemma emblem (v:Token→ℝ):
    ∑ i ∈ range rad, v (mark i)= ∑ i:Token,v i:=by
proof · 4 lines
   rw [← Fin.sum_univ_eq_sum_range (fun i => v (mark i))]
   exact Fintype.sum_equiv number _ _ (fun i=>by
       have hp: mark (i:Fin rad).val=number i := congrArg number (Fin.ext (Nat.mod_eq_of_lt i.isLt))
       simp only [hp])
851
lemma touring (l:ℕ)(f:Paths Atom l → Paths Roller l → ℝ):
    ∑ i ∈ range (rad^l), f (queue l i).1 (queue l i).2 =∑ p,∑ g,f p g:=by
proof · 16 lines
    induction l with
    | zero=> simp [queue]
    | succ l ih=>
      rw [pow_succ',width rad rad_pos]
      let v (s:Token):= ∑ a,∑ b, f (s.1,a) (s.2,b)
      have he (i) (hi:i<rad): ∑ j∈range (rad^l),f (queue _ (i+j*rad)).1 (queue _ (i+j*rad)).2=v (mark i):=by
          convert ih (fun a b=> f ((mark i).1,a) ((mark i).2,b)) using 1
          apply sum_congr rfl; intro j hj
          simp only [queue,Nat.add_mul_div_right _ _ rad_pos,Nat.div_eq_of_lt hi,zero_add,
                  mark,Nat.add_mul_mod_self_right]
      have hx:= sum_congr rfl (fun i (e:i∈range rad)=>he i (mem_range.1 e))
      rw [hx,emblem]
      rw [Fintype.sum_prod_type]; rw [pathFold Atom l]
      apply sum_congr rfl; intro a _
      simp_rw [pathFold Roller l]
      unfold v; rw [sum_comm]
883
abbrev Guide:= (ℕ×ℕ×ℕ)×ℕ
884
abbrev MapState:=ℕ×Bool×Issue
886
def goon (b:Bool ) (x:Word ) (a:Guide × MapState):MapState :=
body · 2 lines
   let t:= tossR b (mark a.2.1) x (a.1.1,a.1.2,a.2.2.2)
   (a.2.1/rad, a.2.2.1 && t.1, t.2)
890
lemma goonK (b):Kn (goon b):=by
proof · 6 lines
   let f (v:Token ) (x:Word ) (a:Guide×MapState):=
       tossR b v x (a.1.1,a.1.2,a.2.2.2)
   have hp(v):Kn (f v):= (tossed b v).comp (Kn.first.left.tuple
                                       (Kn.first.right.tuple Kn.next.right.right))
   have he:=mintK f hp _ Kn.next.left
   exact (Kn.next.left.div (Kn.fix rad)).tuple ((both Kn.next.right.left he.left).tuple he.right)
898
abbrev Lap:=Guide × MapState × ℕ
899
def capz (a:Lap) :ℕ:=
body · 1 line
   bead a.2.1.1 (bead 1 (pages a.1.1.1))
901
lemma capzK:Kn (fun (_:Word ) (a:Lap)=>capz a):=by
proof · 4 lines
   have h1:= (Kn.first (α:=Guide ) (β:=MapState×ℕ)).left.left
   have he:= pagesK h1
   have hz:= Kn.wash.comp ((Kn.fix (1:ℕ)).tuple he)
   exact Kn.wash.comp (Kn.next.left.left.tuple hz)
906
def spin (b:Bool ) (x:Word ) (a:Lap):MapState:=
body · 3 lines
   let L:=capz a
   dec (forNat (fun _ (v:ℕ)=> min L (enc (goon b x (a.1,dec v))))
        (min L (enc a.2.1)) a.2.2)
911
lemma spinK (b):Kn (spin b):=by
proof · 8 lines
   let f:PT (Lap×ℕ×ℕ) ℕ:=fun x a=>min (capz a.1) (enc (goon b x (a.1.1,dec a.2.2)))
   have ht := Kn.stroll f _ _ _ ((capzK.comp Kn.first).small
       (Kn.wash.comp ((goonK b).comp (Kn.first.left.tuple (Kn.unpack.comp Kn.next.right)))))
          (Kn.next (α:=Guide ) (β:=MapState×ℕ)).right
          (capzK.small (Kn.wash.comp Kn.next.left)) capzK (by
             intro x a j _
             cases j <;> exact min_le_left _ _)
   exact Kn.unpack.comp ht
922
lemma limits {v c k j x m H} (h:v≤c) (s:Env n 0 ) (b r:Bool) :
   enc ((v,b,issued s):MapState)≤ capz (((n,k,j),x),(c,r,m),H) :=
proof · 1 line
   bead_mono h (bead_mono (show enc b≤1 by cases b <;> decide) (boxbound s).le)
926
def scoreQ (b:Bool ) (k j x:ℕ) :
       {l:ℕ}→Paths Atom l → Paths Roller l→ Env n 0→ Bool
body · 4 lines
   | 0,_,_,_=>true
   | _+1,(t,a),(v,w),s=>
      (if b then checkS (t,v) n k j x (issued s) else checkD (t,v) n k j x (issued s)) &&
                   scoreQ b k j x a w (rol k j (d:=0 ) (w:=0) le_rfl t v s)
933
lemma wellQ (k j x y:ℕ){l:ℕ}(a:Paths Atom l ) (b:Paths Roller l ) (s:Env n 0):
   live k j (↑x:ZMod n) (↑y:ZMod n) (d:=0 ) (w:=0) le_rfl a b s=true ↔
       scoreQ true k j x a b s=true ∧ scoreQ false k j y a b s=true:=by
proof · 13 lines
   induction l generalizing s with
   | zero=> simp [live,scoreQ]
   | succ l ih=>
     cases a with | mk t a=>
      cases b with | mk v b=>
       unfold live scoreQ
       let u:Token :=(t,v)
       have hx:= landlord u k j x y s
       dsimp only [u] at hx
       simp only [Bool.and_eq_true,ite_true,Bool.false_eq_true,ite_false]
       split
       · next hz=> have hp:=hx.1 hz; simp [hp,ih]
       next hz=> simp; grind
950
theorem flowed (b:Bool ) (k j i l c:ℕ ) (x:Word ) (s:Env n 0 ) (v:Bool) :
   spin b x (((n,k,j),i),(c,v,issued s),l) =
        let P:=queue l c
proof · 32 lines
        (c/rad^l, v && scoreQ b k j i P.1 P.2 s,
                         issued (go k j (d:=0 ) (w:=0) le_rfl P.1 P.2 s)) :=by
    let q:Guide:=((n,k,j),i)
    let p:Lap:=(q,(c,v,issued s),l)
    let M:=capz p
    let f (z:ℕ):= min M (enc (goon b x (q,dec z)))
    have he(l':ℕ ) (c':ℕ ) (hv:c'≤c ) (v':Bool ) (s':Env n 0):
      forNat (fun _=> f) (enc ((c',v',issued s'):MapState)) l' =
        let P:=queue l' c'
        enc ((c'/rad^l',v'&&scoreQ b k j i P.1 P.2 s',
              issued (go k j (d:=0 ) (w:=0) le_rfl P.1 P.2 s')):MapState):=by
        induction l' generalizing c' v' s' with
        | zero=> simp [forNat,go,scoreQ]
        | succ l' ih=>
          let c0:=c'/rad
          have ht:c0≤c:=le_trans (Nat.div_le_self ..) hv
          let u:=mark c'
          let s0:=rol k j (d:=0 ) (w:=0) le_rfl u.1 u.2 s'
          let b0:=v' && (tossR b u x (q.1,q.2,issued s')).1
          have hh:f (enc ((c',v',issued s'):MapState))=enc ((c0,b0,issued s0):MapState):=by
               dsimp only [f]
               rw [dec_round]; unfold goon
               change min M (enc ((c0,b0,arch u n k j (issued s')):MapState))=_
               rw [rose]; exact min_eq_right (limits ht s0 b0 v)
          rw [forNat_cons,hh,ih c0 ht]
          simp only [c0,b0,q,s0,u, queue, tossR,scoreQ,go,Equiv.trans_apply,Nat.div_div_eq_div_mul,
                          pow_succ',Bool.and_assoc]
    unfold spin
    change dec (forNat _ (min M _) _)=_
    rw [min_eq_right (limits ..),he l c le_rfl]
    · dsimp only; simp only [Wire.eq]
    exact le_rfl
986
def stepout (n k j l a:ℕ ) (s:Issue):Issue:= (spin true [] (((n,k,j),0),(a,true,s),l)).2.2
987
def touched (b:Bool ) (n k j i l a:ℕ ) (s:Issue):Bool:=
body · 1 line
                         (spin b [] (((n,k,j),i),(a,true,s),l)).2.1
990
lemma spin_null (b:Bool ) (x:Word ) (a:Lap):spin b x a=spin b [] a:=rfl
992
lemma stepK {α} [Wire α]{n k j l c :PT α ℕ}{s:PT α Issue}
   (hn:Kn n ) (hk:Kn k ) (hj:Kn j ) (hl:Kn l ) (hc:Kn c ) (hs:Kn s):
   Kn (fun x a=>stepout (n x a ) (k x a ) (j x a ) (l x a ) (c x a ) (s x a)) :=
proof · 2 lines
   ((spinK true).comp (((hn.tuple (hk.tuple hj)).tuple (Kn.fix 0)).tuple
                                      ((hc.tuple ((Kn.fix _).tuple hs)).tuple hl))).right.right
997
lemma touchedK {α} [Wire α]{n k j i l c:PT α ℕ}{s:PT α Issue}
   (b ) (hn:Kn n ) (hk:Kn k ) (hj:Kn j ) (hi:Kn i ) (hl:Kn l ) (hc:Kn c ) (hs:Kn s):
   Kn (fun x a=> touched b (n x a ) (k x a ) (j x a ) (i x a ) (l x a ) (c x a ) (s x a)):=
proof · 2 lines
   ((spinK b).comp (((hn.tuple (hk.tuple hj)).tuple hi).tuple
                                      ((hc.tuple ((Kn.fix _).tuple hs)).tuple hl))).right.left
1003
lemma unpacked (k j l a ) (s:Env n 0):
   stepout n k j l a (issued s)=issued
                 (go k j (d:=0 ) (w:=0) le_rfl (queue l a).1 (queue l a).2 s):=by
proof · 1 line
   rw [stepout,flowed]
1007
lemma exactHit (k j x y l a:ℕ ) (s:Env n 0):
   live k j (↑x:ZMod n) (↑y:ZMod n) (d:=0 ) (w:=0) le_rfl (queue l a).1 (queue l a).2 s=true ↔
     touched true n k j x l a (issued s)=true ∧ touched false n k j y l a (issued s)=true := by
proof · 1 line
   simp only [touched,flowed, Bool.true_and]; exact wellQ k j x y ..

Xet Storage Details

Size:
43 kB
·
Xet hash:
5e2cf4b1efb47d4eabc2bd8aad0fd721c7a1df94b06fddf91ee0b2400b7cdde0

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