math / data /lean /OAI /Computability /MeanPayoff /RawLabels.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
15.7 kB
import OAI.Computability.MeanPayoff.RawMass
namespace OAI
namespace MeanPayoff.TreeDevice.Certified
open BinaryData Code
local infixl:80 " β‰«β‚š " => Code.comp
local infixr:70 " β‹β‚š " => Code.pair
namespace Code
@[simp] theorem any_ofFn {Ξ± : Type} [Encoding Ξ±] {n : β„•} (f : Code Ξ± Bool) (xs : Fin n β†’ Ξ±) :
any f (List.ofFn xs) = decide (βˆƒ i, f (xs i)=true) := by
simp only [any,comp_eval,filter_eval,empty_eval,not_eval]
apply Bool.eq_iff_iff.mpr
simp [List.filter_eq_nil_iff]
end Code
namespace DetRaw
abbrev Input := (W Γ— List W) Γ— DetMass.Pair
noncomputable def width : Code Input W := fst β‰«β‚š fst
noncomputable def lower : Code Input (List W) := fst β‰«β‚š snd
noncomputable def masses : Code Input DetMass.Pair := snd
noncomputable def a : Code Input DetMass.Data := masses β‰«β‚š fst
noncomputable def b : Code Input DetMass.Data := masses β‰«β‚š snd
noncomputable def box : Code Input RawBox.Data :=
width β‹β‚š lower β‹β‚š ((width β‹β‚š lower) β‰«β‚š Array.translate)
noncomputable def small : Code Input Bool := (width β‹β‚š Word.constant 4096) β‰«β‚š Word.lt
noncomputable def base : Code Input Bool := masses β‰«β‚š DetMass.base
noncomputable def baseLabels : Code Input (List Bool) := b β‰«β‚š DetMass.labels
noncomputable def piece (k : β„•) : Code Input W :=
(width β‹β‚š const (List.replicate k ())) β‰«β‚š Word.shiftRight
noncomputable def amount (r : β„€) : Code Input W := piece 12 β‰«β‚š RawBudget.times r
noncomputable def center : Code Input (List W) := ((piece 1) β‹β‚š lower) β‰«β‚š Array.translate
noncomputable def child (up : Bool) : Code (Input Γ— List W) Input :=
((fst β‰«β‚š piece 7) β‹β‚š (((fst β‰«β‚š piece 8 β‰«β‚š Word.neg) β‹β‚š snd) β‰«β‚š Array.translate)) β‹β‚š
(if up then ((fst β‰«β‚š a β‰«β‚š DetMass.scale 4 3) β‹β‚š (fst β‰«β‚š b))
else ((fst β‰«β‚š a) β‹β‚š (fst β‰«β‚š b β‰«β‚š DetMass.scale 4 3)))
noncomputable def provisional : Code (Input Γ— List W) Input :=
((fst β‰«β‚š piece 1) β‹β‚š (((fst β‰«β‚š piece 2 β‰«β‚š Word.neg) β‹β‚š snd) β‰«β‚š Array.translate)) β‹β‚š
(fst β‰«β‚š masses)
noncomputable def continuation : Code (Input Γ— List Bool) Input :=
(fst β‰«β‚š fst) β‹β‚š
((((fst β‰«β‚š a) β‹β‚š snd) β‰«β‚š DetMass.reweight) β‹β‚š
(((fst β‰«β‚š b) β‹β‚š (snd β‰«β‚š Array.complement)) β‰«β‚š DetMass.reweight))
noncomputable def lowerCap : Code Input (List W) := (amount 1536 β‹β‚š lower) β‰«β‚š Array.translate
noncomputable def upperCap : Code Input (List W) := (amount 2560 β‹β‚š lower) β‰«β‚š Array.translate
noncomputable def minus : Code (Input Γ— List W) (List W) :=
((((fst β‰«β‚š piece 12 β‰«β‚š Word.neg) β‹β‚š snd) β‰«β‚š Array.translate) β‹β‚š (fst β‰«β‚š lowerCap)) β‰«β‚š Array.max
noncomputable def plus : Code (Input Γ— List W) (List W) :=
((((fst β‰«β‚š piece 12) β‹β‚š snd) β‰«β‚š Array.translate) β‹β‚š (fst β‰«β‚š upperCap)) β‰«β‚š Array.min
abbrev Update := Input Γ— List W Γ— List Bool
noncomputable def down : Code Update (List W) :=
((snd β‰«β‚š snd) β‹β‚š (snd β‰«β‚š fst) β‹β‚š ((fst β‹β‚š (snd β‰«β‚š fst)) β‰«β‚š minus)) β‰«β‚š Array.choose
noncomputable def up : Code Update (List W) :=
((snd β‰«β‚š snd) β‹β‚š ((fst β‹β‚š (snd β‰«β‚š fst)) β‰«β‚š plus) β‹β‚š (snd β‰«β‚š fst)) β‰«β‚š Array.choose
noncomputable def equalWord : Code (W Γ— W) Bool := (Word.le β‹β‚š (swap β‰«β‚š Word.le)) β‰«β‚š Code.and
noncomputable def different : Code (List W Γ— List W) Bool :=
zip Array.zero Array.zero (equalWord β‰«β‚š Code.not) β‰«β‚š any Code.id
noncomputable def changed (u : Bool) : Code Update Bool :=
((if u then up else down) β‹β‚š (snd β‰«β‚š fst)) β‰«β‚š different
noncomputable def lowMark : Code (Input Γ— List W) (List Bool) :=
(snd β‹β‚š (((fst β‰«β‚š amount 1920) β‹β‚š (fst β‰«β‚š lower)) β‰«β‚š Array.translate)) β‰«β‚š Array.lt
noncomputable def highMark : Code (Input Γ— List W) (List Bool) :=
((((fst β‰«β‚š amount 2176) β‹β‚š (fst β‰«β‚š lower)) β‰«β‚š Array.translate) β‹β‚š snd) β‰«β‚š Array.lt
noncomputable def selectBool : Code (Bool Γ— Bool Γ— Bool) Bool := Code.branch fst (snd β‰«β‚š fst) (snd β‰«β‚š snd)
noncomputable def chooseBool : Code (List Bool Γ— List Bool Γ— List Bool) (List Bool) :=
(fst β‹β‚š (snd β‰«β‚š zip false false (fst β‹β‚š snd))) β‰«β‚š zip false (false,false) selectBool
abbrev Finish := Input Γ— List W Γ— List W Γ— List Bool
noncomputable def highFinish : Code Finish (List Bool) :=
(((fst β‹β‚š (snd β‰«β‚š snd β‰«β‚š fst)) β‰«β‚š highMark) β‹β‚š
((snd β‰«β‚š snd β‰«β‚š snd) β‰«β‚š map (const true)) β‹β‚š (snd β‰«β‚š snd β‰«β‚š snd)) β‰«β‚š chooseBool
noncomputable def finish : Code Finish (List Bool) :=
(((fst β‹β‚š (snd β‰«β‚š fst)) β‰«β‚š lowMark) β‹β‚š
((snd β‰«β‚š snd β‰«β‚š snd) β‰«β‚š map (const false)) β‹β‚š highFinish) β‰«β‚š chooseBool
noncomputable def widthLabels : Code (Input Γ— List W) (List Bool) :=
((((fst β‰«β‚š width) β‹β‚š (fst β‰«β‚š lower β‰«β‚š map Word.double)) β‰«β‚š Array.translate) β‹β‚š
(snd β‰«β‚š map Word.double)) β‰«β‚š Array.lt
noncomputable def basicCount : Code Input Count :=
(const (List.replicate 4096 ()) β‹β‚š (lower β‰«β‚š count)) β‰«β‚š Counter.mul
noncomputable def basic : Code (RawBasic.Arena Γ— Input) (List Bool) :=
(snd β‹β‚š (((fst β‹β‚š (snd β‰«β‚š box)) β‹β‚š (snd β‰«β‚š basicCount)) β‰«β‚š RawBasic.solve)) β‰«β‚š widthLabels
structure Rep {n : β„•} (A : Fin n β†’ β„€) (d : β„•) (a b : Fin n β†’ β„š) (x : Input) : Prop where
width : x.1.1.val=(2:β„€)^d
lower : VectorRep A x.1.2
a : DetMass.Rep a x.2.1
b : DetMass.Rep b x.2.2
variable {n : β„•} {A : Fin n β†’ β„€} {d : β„•} {a b : Fin n β†’ β„š} {x : Input}
@[simp] theorem equalWord_eq (p : W Γ— W) : equalWord p=decide (p.1.val=p.2.val) := by
change (if Word.le p then Word.le (p.2,p.1) else false) = _
rw [Word.le_eval,Word.le_eval]
by_cases h : p.1.val ≀ p.2.val
Β· by_cases h' : p.2.val ≀ p.1.val
Β· simp [le_antisymm h h']
Β· have hn : p.1.val β‰  p.2.val := fun he => h' (by rw [he])
simp [h,h',hn]
Β· have hn : p.1.val β‰  p.2.val := fun he => h (by rw [he])
simp [h,hn]
theorem different_eq {v w : Fin n β†’ β„€} {vs ws : List W}
(hv : VectorRep v vs) (hw : VectorRep w ws) :
different (vs,ws)=decide (v≠w) := by
obtain ⟨as,rfl,ha⟩ := hv
obtain ⟨bs,rfl,hb⟩ := hw
simp only [different,Code.comp_eval,Code.zip_eval,Array.zip_ofFn,Code.any_ofFn,Code.id_eval,
equalWord_eq,Code.not_eval,←decide_not,ha,hb,decide_eq_true_eq]
exact decide_eq_decide.mpr Function.ne_iff.symm
theorem base_eq (h : Rep A d a b x) : base x=decide (βˆ€ i, 1<a i*b i) :=
DetMass.base_eq h.a h.b
theorem baseLabels_eq (h : Rep A d a b x) : baseLabels x=List.ofFn (Label.massLabels b) :=
DetMass.labels_eq h.b
theorem small_eq (h : Rep A d a b x) : small x=decide (d<12) := by
change Word.lt (x.1.1,_) = _
rw [Word.lt_eval]
have hc : (Word.constant 4096 x).val = (4096:β„€) := Word.constant_val 4096 x
rw [hc,h.width]
apply decide_eq_decide.mpr
have he : (4096:β„€)=(2:β„€)^12 := by norm_num
rw [he]
exact pow_lt_pow_iff_rightβ‚€ (by norm_num)
theorem piece_val (h : Rep A d a b x) (k : β„•) (hk : k≀d) :
(piece k x).val=(2:β„€)^(d-k) := by
change (Word.shiftRight (x.1.1,List.replicate k ())).val=_
rw [Word.shiftRight_val,List.length_replicate,h.width]
rw [show d=(d-k)+k by omega,pow_add,Int.mul_ediv_cancel _ (by positivity : (2:℀)^k≠0)]
congr 1
omega
theorem amount_val (h : Rep A d a b x) (hd : 12≀d) (c : β„€) :
(amount c x).val=c*(2:β„€)^(d-12) := by
simp only [amount,Code.comp_eval,RawBudget.times,Code.pair_eval,Code.id_eval,Word.mul_val,Word.constant_val]
rw [piece_val h 12 hd,mul_comm]
theorem box_rep (h : Rep A d a b x) :
RawBox.Rep (show SolveBox n (2^d) from
{ a:=A, b:=shift A ((2:β„€)^d), width:=2^d,
ordered:=fun i => le_add_of_nonneg_right (by positivity),
positive:=by positivity, bounded:=le_rfl,
width_bound:=fun i => by simp [shift] }) (box x) := by
refine ⟨?_,h.lower,?_⟩
Β· exact_mod_cast h.width
Β· convert Array.translate_rep h.lower x.1.1 using 1 <;> try rfl
funext i
simp only [shift,h.width,add_comm]
theorem center_rep (h : Rep A d a b x) (hd : 12≀d) :
VectorRep (Label.gridPivot A ((2:β„€)^(d-12)) Label.gridCenter) (center x) := by
rw [Label.gridPivot_center]
convert Array.translate_rep h.lower (piece 1 x) using 1 <;> try rfl
funext i
rw [piece_val h 1 (by omega),(Label.dyadic_splits hd).2.2]
ring
theorem child_rep (h : Rep A d a b x) (hd : 12≀d) (u : Bool)
{v : Fin n β†’ β„€} {vs : List W} (hv : VectorRep v vs) :
Rep (fun i => v i-16*(2:β„€)^(d-12)) (d-7)
(if u then fun i => 4/3*a i else a) (if u then b else fun i => 4/3*b i)
(child u (x,vs)) := by
have hlow : VectorRep (fun i => v i-16*(2:β„€)^(d-12))
(Array.translate (Word.neg (piece 8 x),vs)) := by
convert Array.translate_rep hv (Word.neg (piece 8 x)) using 1
funext i
rw [Word.neg_val,piece_val h 8 (by omega)]
have he : (2:β„€)^(d-8)=16*(2:β„€)^(d-12) := by
rw [show d-8=(d-12)+4 by omega,pow_add]
norm_num
ring
rw [he]
ring
cases u
· exact ⟨piece_val h 7 (by omega),hlow,h.a,DetMass.scale_rep h.b (by norm_num) (by norm_num)⟩
· exact ⟨piece_val h 7 (by omega),hlow,DetMass.scale_rep h.a (by norm_num) (by norm_num),h.b⟩
theorem provisional_rep (h : Rep A d a b x) (hd : 12≀d)
{v : Fin n β†’ β„€} {vs : List W} (hv : VectorRep v vs) :
Rep (fun i => v i-1024*(2:β„€)^(d-12)) (d-1) a b (provisional (x,vs)) := by
refine ⟨piece_val h 1 (by omega),?_,h.a,h.b⟩
convert Array.translate_rep hv (Word.neg (piece 2 x)) using 1 <;> try rfl
funext i
rw [Word.neg_val,piece_val h 2 (by omega)]
have he : (2:β„€)^(d-2)=1024*(2:β„€)^(d-12) := by
rw [show d-2=(d-12)+10 by omega,pow_add]
norm_num
ring
rw [he]
ring
theorem continuation_rep (h : Rep A d a b x) (ls : Fin n β†’ Bool) :
Rep A d (Label.reweight a ls) (Label.reweight b (fun i => !(ls i)))
(continuation (x,List.ofFn ls)) := by
refine ⟨h.width,h.lower,DetMass.reweight_rep h.a ls,?_⟩
have hc : Array.complement (List.ofFn ls)=List.ofFn (fun i => !(ls i)) := by
simp [Array.complement,Code.not,List.map_ofFn,Function.comp_def]
change DetMass.Rep _ (DetMass.reweight (x.2.2,Array.complement (List.ofFn ls)))
rw [hc]
exact DetMass.reweight_rep h.b _
theorem minus_rep (h : Rep A d a b x) (hd : 12≀d)
{v : Fin n β†’ β„€} {vs : List W} (hv : VectorRep v vs) :
VectorRep (fun i => max (v i-(2:β„€)^(d-12)) (A i+1536*(2:β„€)^(d-12))) (minus (x,vs)) := by
convert Array.zip_rep Word.max Max.max (fun _ _ => Word.max_val _ _)
(Array.translate_rep hv (Word.neg (piece 12 x)))
(Array.translate_rep h.lower (amount 1536 x)) using 1 <;> try rfl
funext i
rw [Word.neg_val,piece_val h 12 hd,amount_val h hd]
congr 1 <;> ring
theorem plus_rep (h : Rep A d a b x) (hd : 12≀d)
{v : Fin n β†’ β„€} {vs : List W} (hv : VectorRep v vs) :
VectorRep (fun i => min (v i+(2:β„€)^(d-12)) (A i+2560*(2:β„€)^(d-12))) (plus (x,vs)) := by
convert Array.zip_rep Word.min Min.min (fun _ _ => Word.min_val _ _)
(Array.translate_rep hv (piece 12 x))
(Array.translate_rep h.lower (amount 2560 x)) using 1 <;> try rfl
funext i
rw [piece_val h 12 hd,amount_val h hd]
congr 1 <;> ring
theorem choose_rep_bool {v w : Fin n β†’ β„€} {vs ws : List W}
(hv : VectorRep v vs) (hw : VectorRep w ws) (ls : Fin n β†’ Bool) :
VectorRep (fun i => if ls i then v i else w i) (Array.choose (List.ofFn ls,vs,ws)) := by
have hm : MaskRep (Finset.univ.filter (fun i => ls i=true)) (List.ofFn ls) := by
simp [MaskRep]
simpa using Array.choose_rep hm hv hw
theorem down_rep (h : Rep A d a b x) (hd : 12≀d)
{v : Fin n β†’ β„€} {vs : List W} (hv : VectorRep v vs) (ls : Fin n β†’ Bool) :
VectorRep (Label.downStep A ((2:β„€)^(d-12)) v ls) (down (x,vs,List.ofFn ls)) := by
convert choose_rep_bool hv (minus_rep h hd hv) ls using 1 <;> try rfl
funext i
simp only [Label.downStep,max_comm]
theorem up_rep (h : Rep A d a b x) (hd : 12≀d)
{v : Fin n β†’ β„€} {vs : List W} (hv : VectorRep v vs) (ls : Fin n β†’ Bool) :
VectorRep (Label.upStep A ((2:β„€)^(d-12)) v ls) (up (x,vs,List.ofFn ls)) := by
convert choose_rep_bool (plus_rep h hd hv) hv ls using 1 <;> try rfl
funext i
simp only [Label.upStep,min_comm]
theorem lt_eq {v w : Fin n β†’ β„€} {vs ws : List W}
(hv : VectorRep v vs) (hw : VectorRep w ws) :
Array.lt (vs,ws)=List.ofFn (fun i => decide (v i<w i)) := by
simpa [MaskRep] using Array.lt_rep hv hw
theorem lowMark_eq (h : Rep A d a b x) (hd : 12≀d)
{v : Fin n β†’ β„€} {vs : List W} (hv : VectorRep v vs) :
lowMark (x,vs)=List.ofFn (fun i => decide (v i<A i+1920*(2:β„€)^(d-12))) := by
convert lt_eq hv (Array.translate_rep h.lower (amount 1920 x)) using 1 <;> try rfl
congr 1
funext i
rw [amount_val h hd,add_comm]
theorem highMark_eq (h : Rep A d a b x) (hd : 12≀d)
{v : Fin n β†’ β„€} {vs : List W} (hv : VectorRep v vs) :
highMark (x,vs)=List.ofFn (fun i => decide (A i+2176*(2:β„€)^(d-12)<v i)) := by
convert lt_eq (Array.translate_rep h.lower (amount 2176 x)) hv using 1 <;> try rfl
congr 1
funext i
rw [amount_val h hd,add_comm]
@[simp] theorem chooseBool_eq (m p q : Fin n β†’ Bool) :
chooseBool (List.ofFn m,List.ofFn p,List.ofFn q)=
List.ofFn (fun i => if m i then p i else q i) := by
change Zip.zip _ _ (List.ofFn _) (Zip.zip _ _ (List.ofFn _) (List.ofFn _)) = _
rw [Array.zip_ofFn,Array.zip_ofFn]
rfl
theorem finish_eq (h : Rep A d a b x) (hd : 12≀d)
{vd vu : Fin n β†’ β„€} {ds us : List W} (hdv : VectorRep vd ds) (huv : VectorRep vu us)
(ls : Fin n β†’ Bool) : finish (x,ds,us,List.ofFn ls)=
List.ofFn (Label.override A vd vu ((2:β„€)^(d-12)) ls) := by
change chooseBool (lowMark (x,ds),List.map (fun _ => false) (List.ofFn ls),
chooseBool (highMark (x,us),List.map (fun _ => true) (List.ofFn ls),List.ofFn ls)) = _
rw [lowMark_eq h hd hdv,highMark_eq h hd huv,List.map_ofFn,List.map_ofFn,chooseBool_eq,chooseBool_eq]
congr 1
funext i
simp only [decide_eq_true_eq,Function.comp_apply,Label.override]
theorem widthLabels_eq (h : Rep A d a b x)
{v : Fin n β†’ β„€} {vs : List W} (hv : VectorRep v vs) :
widthLabels (x,vs)=List.ofFn (Label.widthLabels A v ((2:β„€)^d)) := by
convert lt_eq (Array.translate_rep (Array.map_rep Word.double (2*Β·) Word.double_val h.lower) x.1.1)
(Array.map_rep Word.double (2*Β·) Word.double_val hv) using 1 <;> try rfl
congr 1
funext i
simp only [Label.widthLabels,h.width,add_comm]
theorem basic_eq (G : Game) {A : Fin G.n β†’ β„€} {d : β„•} {a b : Fin G.n β†’ β„š} {x : Input}
(h : Rep A d a b x) (hd : d<12) (w : Fin G.m β†’ W)
(hw : βˆ€ e, (w e).val=transformed G e) :
basic (Graph.codeVertices G w,x)=List.ofFn (Label.widthLabels A
(MeanPayoff.basic G A (shift A ((2:β„€)^d)) (fun i => by
dsimp [shift]; exact le_add_of_nonneg_right (by positivity))).1 ((2:β„€)^d)) := by
apply widthLabels_eq h
apply RawBasic.solve_rep G (box_rep h) w hw
have hn : x.1.2.length=G.n := by obtain ⟨ws,he,_⟩ := h.lower; simp [he]
change G.n*2^d ≀ (Counter.mul (List.replicate 4096 (), Code.count x.1.2)).length
rw [Counter.mul_length]
simp only [List.length_replicate,Code.count,Code.map_eval,List.length_map,hn]
have hd' : 2^d ≀ 4096 := by
have hh := Nat.pow_le_pow_right (by omega : 1≀2) (show d≀12 by omega)
norm_num at hh ⊒
exact hh
nlinarith
end DetRaw
end MeanPayoff.TreeDevice.Certified
end OAI