math / data /lean /OAI /Computability /MeanPayoff /EncodedArithmetic.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
17.9 kB
import OAI.Computability.MeanPayoff.EncodedPrograms
namespace OAI
section
namespace MeanPayoff.TreeDevice.Certified
open BinaryData Code
local infixl:80 " β‰«β‚š " => Code.comp
local infixr:70 " β‹β‚š " => Code.pair
theorem take_size {Ξ± : Type} [Encoding Ξ±] (xs : List Ξ±) (k : β„•) :
treeSize (encode (xs.take k)) ≀ treeSize (encode xs) := by
induction xs generalizing k with
| nil => simp
| cons x xs ih =>
cases k with
| zero => exact treeSize_pos _
| succ k =>
change treeSize (listTree encode ((x::xs).take (k+1))) ≀ treeSize (listTree encode (x::xs))
simp only [List.take_succ_cons,listTree,treeSize_pair]
have h := ih k
change treeSize (listTree encode (xs.take k)) ≀ treeSize (listTree encode xs) at h
omega
namespace Word
@[simp] theorem add_length (x y : W) : (add (x,y)).digits.length=Max.max x.digits.length y.digits.length+1 :=
SignedBits.add_length _ _ _ _ _
@[simp] theorem neg_length (x : W) : (neg x).digits.length=Max.max x.digits.length 1+1 := by
change (SignedBits.add _ _ _ _ _).digits.length=_
rw [SignedBits.add_length]
simp [SignedBits.Word.complement,Negate.one]
@[simp] theorem half_length (x : W) : (half x).digits.length=x.digits.length-1 := List.length_tail
@[simp] theorem double_length (x : W) : (double x).digits.length=x.digits.length+1 := Multiply.double_length _
theorem addFold_length (a : W) (xs : List W) :
(Fold.fold add.eval a xs).digits.length ≀ a.digits.length+(xs.map (fun x => x.digits.length+1)).sum := by
induction xs generalizing a with
| nil => simp [Fold.fold]
| cons x xs ih =>
have h := ih (add (a,x))
change (Fold.fold add.eval (add (a,x)) xs).digits.length ≀ _
simp only [List.map_cons,List.sum_cons]
rw [add_length] at h
omega
theorem digitSum_le_size (xs : List W) :
(xs.map (fun x => x.digits.length+1)).sum ≀ treeSize (encode xs) := by
induction xs with
| nil => simp []
| cons x xs ih =>
have h := digits_le_wordSize x
change _ ≀ treeSize (listTree wordData (x::xs))
rw [listTree,treeSize_pair]
simp only [List.map_cons,List.sum_cons]
change _ ≀ treeSize (listTree wordData xs) at ih
omega
noncomputable def addFold : Code (W Γ— List W) W := foldPrefix add 10 1 (by
intro a xs k hk
have hl := addFold_length a (xs.take k)
have hs := digitSum_le_size (xs.take k)
have ht : treeSize (encode (xs.take k)) ≀ treeSize (encode xs) := by
exact take_size xs k
have ha := digits_le_wordSize a
have hh := wordSize_bound (Fold.fold add.eval a (xs.take k))
simp only [pow_one]
change treeSize (wordData (Fold.fold add.eval a (xs.take k))) ≀ 10*(treeSize (product wordData (listTree wordData) (a,xs))+1)
rw [product,treeSize_pair]
change (List.map (fun x => x.digits.length+1) (xs.take k)).sum ≀ treeSize (listTree wordData (xs.take k)) at hs
change treeSize (listTree wordData (xs.take k)) ≀ treeSize (listTree wordData xs) at ht
dsimp only [Prod.fst,Prod.snd]
nlinarith)
noncomputable def sum : Code (List W) W := (const Median.zeroWord β‹β‚š Code.id) β‰«β‚š addFold
theorem addFold_val (a : W) (xs : List W) :
(addFold (a,xs)).val=a.val+(xs.map SignedBits.Word.val).sum := by
change (Fold.fold add.eval a xs).val=_
induction xs generalizing a with
| nil => simp [Fold.fold]
| cons x xs ih =>
change (Fold.fold add.eval (add (a,x)) xs).val=_
have h := ih (add (a,x))
simp only [List.map_cons,List.sum_cons] at *
rw [add_val] at h
omega
@[simp] theorem sum_val (xs : List W) : (sum xs).val=(xs.map SignedBits.Word.val).sum := by
change (addFold (Median.zeroWord,xs)).val=_
rw [addFold_val]
change 0+_=_
omega
noncomputable def shiftStep : Code (W Γ— Unit) W := fst β‰«β‚š half
noncomputable def shiftRight : Code (W Γ— Count) W := fold shiftStep (by
intro w u
change treeSize (wordData (Halve.halve w)) ≀ treeSize (wordData w)+treeSize (Median.unitData u)+1
cases w with
| mk s ds =>
cases ds with
| nil =>
simp only [Halve.halve,wordData,listData,treeSize_pair,List.tail_nil]
omega
| cons b bs =>
simp only [Halve.halve,wordData,listData,List.tail_cons,treeSize_pair]
omega)
theorem shiftRight_val (w : W) (c : Count) : (shiftRight (w,c)).val=w.val/(2^c.length) := by
change (Fold.fold shiftStep.eval w c).val=_
induction c generalizing w with
| nil => simp [Fold.fold]
| cons u cs ih =>
change (Fold.fold shiftStep.eval (half w) cs).val=_
rw [ih,half_val,Int.ediv_ediv]
simp only [List.length_cons,pow_succ]
norm_num
rw [mul_comm]
end Word
namespace Counter
noncomputable def inc : Code Count Count := (const () β‹β‚š Code.id) β‰«β‚š cons
noncomputable def step : Code (W Γ— Unit) W := (fst β‹β‚š const Negate.one) β‰«β‚š Word.add
noncomputable def word : Code Count W := map (const Negate.one) β‰«β‚š Word.sum
@[simp] theorem word_val (xs : Count) : (word xs).val=xs.length := by
simp [word,Code.const,List.sum_replicate]
noncomputable def add : Code (Count Γ— Count) Count := append
noncomputable def mul : Code (Count Γ— Count) Count := envMap (fst : Code (Count Γ— Unit) Count) β‰«β‚š flatten
@[simp] theorem add_length (a b : Count) : (add (a,b)).length=a.length+b.length := by simp [add]
@[simp] theorem mul_length (a b : Count) : (mul (a,b)).length=a.length*b.length := by
simp [mul,List.length_flatten,List.sum_replicate,Nat.mul_comm]
end Counter
end MeanPayoff.TreeDevice.Certified
namespace MeanPayoff.TreeDevice.Certified
open BinaryData Code
local infixl:80 " β‰«β‚š " => Code.comp
local infixr:70 " β‹β‚š " => Code.pair
def VectorRep {n : β„•} (v : Fin n β†’ β„€) (xs : List W) : Prop :=
βˆƒ ws : Fin n β†’ W, xs=List.ofFn ws ∧ βˆ€ i, (ws i).val=v i
def MaskRep {n : β„•} (H : Finset (Fin n)) (xs : List Bool) : Prop :=
xs=List.ofFn (fun i => decide (i∈H))
namespace Word
noncomputable def constant {Ξ± : Type} [Encoding Ξ±] (z : β„€) : Code Ξ± W := const (EdgeRead.encodedWord z)
@[simp] theorem constant_val {Ξ± : Type} [Encoding Ξ±] (z : β„€) (x : Ξ±) :
(constant z x).val=z := EdgeRead.encodedWord_val z
end Word
namespace Array
def zero : W := Median.zeroWord
noncomputable def add : Code (List W Γ— List W) (List W) := zip zero zero Word.add
noncomputable def sub : Code (List W Γ— List W) (List W) := zip zero zero Word.sub
noncomputable def min : Code (List W Γ— List W) (List W) := zip zero zero Word.min
noncomputable def max : Code (List W Γ— List W) (List W) := zip zero zero Word.max
noncomputable def average : Code (List W Γ— List W) (List W) := zip zero zero Word.average
noncomputable def mul : Code (List W Γ— List W) (List W) := zip zero zero Word.mul
noncomputable def lt : Code (List W Γ— List W) (List Bool) := zip zero zero Word.lt
noncomputable def le : Code (List W Γ— List W) (List Bool) := zip zero zero Word.le
noncomputable def scale : Code (W Γ— List W) (List W) := envMap Word.mul
noncomputable def translate : Code (W Γ— List W) (List W) := envMap Word.add
noncomputable def complement : Code (List Bool) (List Bool) := map Code.not
noncomputable def mask : Code (List W Γ— List Bool) (List W) :=
zip zero false (Code.branch snd fst (Word.constant 0))
noncomputable def doubleOn : Code (List W Γ— List Bool) (List W) :=
zip zero false (Code.branch snd (fst β‰«β‚š Word.double) fst)
theorem zip_ofFn {Ξ± Ξ² Ξ³ : Type} (f : Ξ± Γ— Ξ² β†’ Ξ³) (b : Ξ²) {n : β„•}
(as : Fin n β†’ Ξ±) (bs : Fin n β†’ Ξ²) :
Zip.zip f b (List.ofFn as) (List.ofFn bs)=List.ofFn (fun i => f (as i,bs i)) := by
induction n with
| zero => simp [List.ofFn_zero,Zip.zip]
| succ n ih => simp [List.ofFn_succ,Zip.zip,ih]
theorem zip_rep {n : β„•} (f : Code (W Γ— W) W) (g : β„€ β†’ β„€ β†’ β„€)
(hf : βˆ€ w v, (f (w,v)).val=g w.val v.val)
{x y : Fin n β†’ β„€} {xs ys : List W} (hx : VectorRep x xs) (hy : VectorRep y ys) :
VectorRep (fun i => g (x i) (y i)) (zip zero zero f (xs,ys)) := by
obtain ⟨ws,rfl,hw⟩ := hx
obtain ⟨vs,rfl,hv⟩ := hy
refine ⟨fun i => f (ws i,vs i),?_,?_⟩
Β· exact zip_ofFn f.eval zero ws vs
Β· intro i
rw [hf,hw,hv]
end Array
namespace RawBudget
abbrev Data := (W Γ— W) Γ— (W Γ— W) Γ— List W Γ— List W
noncomputable def k : Code Data (W Γ— W) := fst
noncomputable def m : Code Data (W Γ— W) := snd β‰«β‚š fst
noncomputable def alpha : Code Data (List W) := snd β‰«β‚š snd β‰«β‚š fst
noncomputable def beta : Code Data (List W) := snd β‰«β‚š snd β‰«β‚š snd
noncomputable def kn : Code Data W := k β‰«β‚š fst
noncomputable def kd : Code Data W := k β‰«β‚š snd
noncomputable def mn : Code Data W := m β‰«β‚š fst
noncomputable def md : Code Data W := m β‰«β‚š snd
noncomputable def times (r : β„€) : Code W W := (Code.id β‹β‚š Word.constant r) β‰«β‚š Word.mul
noncomputable def shrink (r : β„€) : Code (W Γ— W) (W Γ— W) :=
(fst β‰«β‚š times r) β‹β‚š (snd β‰«β‚š times 5)
noncomputable def reduceK : Code Data Data := (k β‰«β‚š shrink 4) β‹β‚š m β‹β‚š alpha β‹β‚š beta
noncomputable def reduceM : Code Data Data := k β‹β‚š (m β‰«β‚š shrink 4) β‹β‚š alpha β‹β‚š beta
noncomputable def reweightK : Code (Data Γ— List Bool) Data :=
(fst β‰«β‚š k β‰«β‚š shrink 3) β‹β‚š (fst β‰«β‚š m) β‹β‚š
(((fst β‰«β‚š alpha) β‹β‚š snd) β‰«β‚š Array.doubleOn) β‹β‚š (fst β‰«β‚š beta)
noncomputable def reweightM : Code (Data Γ— List Bool) Data :=
(fst β‰«β‚š k) β‹β‚š (fst β‰«β‚š m β‰«β‚š shrink 3) β‹β‚š (fst β‰«β‚š alpha) β‹β‚š
(((fst β‰«β‚š beta) β‹β‚š snd) β‰«β‚š Array.doubleOn)
noncomputable def density : Code Data (List W) := (alpha β‹β‚š beta) β‰«β‚š Array.mul
noncomputable def total : Code Data W := density β‰«β‚š Word.sum
noncomputable def parameterNumerator : Code Data W := (((kn β‹β‚š mn) β‰«β‚š Word.mul) β‹β‚š total) β‰«β‚š Word.mul
noncomputable def parameterDenominator : Code Data W := (kd β‹β‚š md) β‰«β‚š Word.mul
noncomputable def small : Code Data Bool := (parameterNumerator β‹β‚š parameterDenominator) β‰«β‚š Word.lt
noncomputable def measureLarge : Code (Data Γ— List Bool) Bool :=
((fst β‰«β‚š total) β‹β‚š ((((fst β‰«β‚š density) β‹β‚š snd) β‰«β‚š Array.mask β‰«β‚š Word.sum) β‰«β‚š Word.double)) β‰«β‚š Word.lt
noncomputable def smallMask : Code Data (List Bool) :=
(((mn β‹β‚š beta) β‰«β‚š Array.scale) β‹β‚š ((md β‹β‚š beta) β‰«β‚š envMap (fst : Code (W Γ— W) W))) β‰«β‚š Array.lt
structure Rep {n : β„•} (P : Budgets n) (p : Data) : Prop where
kn_pos : 0<p.1.1.val
kd_pos : 0<p.1.2.val
mn_pos : 0<p.2.1.1.val
md_pos : 0<p.2.1.2.val
k_eq : P.k=(p.1.1.val:β„š)/(p.1.2.val:β„š)
m_eq : P.m=(p.2.1.1.val:β„š)/(p.2.1.2.val:β„š)
coords : βˆƒ a b : Fin n β†’ W,
p.2.2.1=List.ofFn a ∧ p.2.2.2=List.ofFn b ∧
(βˆ€ i, 0<(a i).val) ∧ (βˆ€ i, 0<(b i).val) ∧
(βˆ€ i, P.alpha i=1/((a i).val:β„š)) ∧ (βˆ€ i, P.beta i=1/((b i).val:β„š))
@[simp] theorem times_val (r : β„€) (w : W) : (times r w).val=w.val*r := by simp [times]
theorem shrink_fst (r : β„€) (p : W Γ— W) : ((shrink r p).1).val=p.1.val*r := by simp [shrink]
theorem shrink_snd (r : β„€) (p : W Γ— W) : ((shrink r p).2).val=p.2.val*5 := by simp [shrink]
theorem reduceK_rep {n : β„•} {P : Budgets n} {p : Data} (h : Rep P p) :
Rep P.reduceK (reduceK p) := by
rcases h with ⟨hkn,hkd,hmn,hmd,hk,hm,hcoords⟩
refine ⟨?_,?_,hmn,hmd,?_,hm,hcoords⟩
Β· change 0<((shrink 4 p.1).1).val
rw [shrink_fst]
omega
Β· change 0<((shrink 4 p.1).2).val
rw [shrink_snd]
omega
Β· change (4/5:β„š)*P.k=(((shrink 4 p.1).1).val:β„š)/(((shrink 4 p.1).2).val:β„š)
rw [hk,shrink_fst,shrink_snd]
push_cast
ring
theorem reduceM_rep {n : β„•} {P : Budgets n} {p : Data} (h : Rep P p) :
Rep P.reduceM (reduceM p) := by
rcases h with ⟨hkn,hkd,hmn,hmd,hk,hm,hcoords⟩
refine ⟨hkn,hkd,?_,?_,hk,?_,hcoords⟩
Β· change 0<((shrink 4 p.2.1).1).val
rw [shrink_fst]
omega
Β· change 0<((shrink 4 p.2.1).2).val
rw [shrink_snd]
omega
Β· change (4/5:β„š)*P.m=(((shrink 4 p.2.1).1).val:β„š)/(((shrink 4 p.2.1).2).val:β„š)
rw [hm,shrink_fst,shrink_snd]
push_cast
ring
theorem reweightK_rep {n : β„•} {P : Budgets n} {p : Data} {H : Finset (Fin n)} {hs : List Bool}
(h : Rep P p) (hH : MaskRep H hs) : Rep (P.reweightK H) (reweightK (p,hs)) := by
rcases h with ⟨hkn,hkd,hmn,hmd,hk,hm,a,b,ha,hb,hap,hbp,hea,heb⟩
refine ⟨?_,?_,hmn,hmd,?_,hm,?_⟩
Β· change 0<((shrink 3 p.1).1).val
rw [shrink_fst]
omega
Β· change 0<((shrink 3 p.1).2).val
rw [shrink_snd]
omega
Β· change (3/5:β„š)*P.k=(((shrink 3 p.1).1).val:β„š)/(((shrink 3 p.1).2).val:β„š)
rw [hk,shrink_fst,shrink_snd]
push_cast
ring
Β· let a' : Fin n β†’ W := fun i => if i∈H then Word.double (a i) else a i
refine ⟨a',b,?_,hb,?_,hbp,?_,heb⟩
Β· change Zip.zip _ _ p.2.2.1 hs=List.ofFn a'
rw [ha,hH,Array.zip_ofFn]
congr 1
funext i
simp [a',Code.branch,Code.comp,Code.fst,Code.snd]
Β· intro i
dsimp [a']
split_ifs
Β· rw [Word.double_val]
have := hap i
omega
Β· exact hap i
Β· intro i
change halve P.alpha H i=1/((a' i).val:β„š)
unfold halve a'
split_ifs <;> simp [hea,Word.double_val] ; ring
theorem reweightM_rep {n : β„•} {P : Budgets n} {p : Data} {H : Finset (Fin n)} {hs : List Bool}
(h : Rep P p) (hH : MaskRep H hs) : Rep (P.reweightM H) (reweightM (p,hs)) := by
rcases h with ⟨hkn,hkd,hmn,hmd,hk,hm,a,b,ha,hb,hap,hbp,hea,heb⟩
refine ⟨hkn,hkd,?_,?_,hk,?_,?_⟩
Β· change 0<((shrink 3 p.2.1).1).val
rw [shrink_fst]
omega
Β· change 0<((shrink 3 p.2.1).2).val
rw [shrink_snd]
omega
Β· change (3/5:β„š)*P.m=(((shrink 3 p.2.1).1).val:β„š)/(((shrink 3 p.2.1).2).val:β„š)
rw [hm,shrink_fst,shrink_snd]
push_cast
ring
Β· let b' : Fin n β†’ W := fun i => if i∈H then Word.double (b i) else b i
refine ⟨a,b',ha,?_,hap,?_,hea,?_⟩
Β· change Zip.zip _ _ p.2.2.2 hs=List.ofFn b'
rw [hb,hH,Array.zip_ofFn]
congr 1
funext i
simp [b',Code.branch,Code.comp,Code.fst,Code.snd]
Β· intro i
dsimp [b']
split_ifs
Β· rw [Word.double_val]
have := hbp i
omega
Β· exact hbp i
Β· intro i
change halve P.beta H i=1/((b' i).val:β„š)
unfold halve b'
split_ifs <;> simp [heb,Word.double_val] ; ring
theorem density_cast {n : β„•} {P : Budgets n} {a b : Fin n β†’ W}
(ha : βˆ€ i, P.alpha i=1/((a i).val:β„š)) (hb : βˆ€ i, P.beta i=1/((b i).val:β„š)) (i : Fin n) :
P.density i=((a i).val:β„š)*((b i).val:β„š) := by
simp [Budgets.density,ha,hb,one_div,mul_inv_rev,mul_comm]
theorem total_eq {n : β„•} {P : Budgets n} {p : Data} (h : Rep P p) :
((total p).val:β„š)=P.totalDensity := by
obtain ⟨a,b,ha,hb,_,_,hea,heb⟩ := h.coords
change ((Word.sum (Array.mul (p.2.2.1,p.2.2.2))).val:β„š)=_
rw [ha,hb]
change ((Word.sum (Zip.zip _ _ (List.ofFn a) (List.ofFn b))).val:β„š)=_
rw [Array.zip_ofFn,Word.sum_val]
simp only [List.map_ofFn,List.sum_ofFn,Function.comp_def,Word.mul_val]
push_cast
unfold Budgets.totalDensity mass
apply Finset.sum_congr rfl
intro i _
exact (density_cast hea heb i).symm
theorem masked_eq {n : β„•} {P : Budgets n} {p : Data} {H : Finset (Fin n)} {hs : List Bool}
(h : Rep P p) (hH : MaskRep H hs) :
((Word.sum (Array.mask (density p,hs))).val:β„š)=mass P.density H := by
obtain ⟨a,b,ha,hb,_,_,hea,heb⟩ := h.coords
change ((Word.sum (Zip.zip _ _ (Zip.zip _ _ p.2.2.1 p.2.2.2) hs)).val:β„š)=_
rw [ha,hb,hH,Array.zip_ofFn,Array.zip_ofFn,Word.sum_val]
simp only [List.map_ofFn,List.sum_ofFn,Function.comp_def]
rw [Int.cast_sum]
unfold mass
calc
_ = βˆ‘ i : Fin n, if i∈H then P.density i else 0 := by
apply Finset.sum_congr rfl
intro i _
change ((if decide (i∈H) then Word.mul (a i,b i) else Word.constant 0 (Word.mul (a i,b i),decide (i∈H))).val:β„š)=_
by_cases hi : i∈H
Β· simp only [hi,decide_true,↓reduceIte,Word.mul_val,Int.cast_mul]
exact (density_cast hea heb i).symm
Β· simp [hi]
_ = _ := by simp []
theorem small_eq {n : β„•} {P : Budgets n} {p : Data} (h : Rep P p) :
small p=decide (P.parameter<1) := by
change Word.lt (parameterNumerator p,parameterDenominator p)=_
rw [Word.lt_eval]
apply decide_eq_decide.mpr
have hk : (((kn p).val:β„€):β„š)=(p.1.1.val:β„š) := rfl
have ht := total_eq h
have hd : (0:β„š)<p.1.2.val*p.2.1.2.val := by
exact mul_pos (by exact_mod_cast h.kd_pos) (by exact_mod_cast h.md_pos)
have he : P.parameter=
((parameterNumerator p).val:β„š)/((parameterDenominator p).val:β„š) := by
change P.k*P.m*P.totalDensity=_
simp only [parameterNumerator,parameterDenominator,comp_eval,pair_eval,Word.mul_val,Int.cast_mul]
rw [← ht,h.k_eq,h.m_eq]
change _=(p.1.1.val:β„š)*p.2.1.1.val*(total p).val/((p.1.2.val:β„š)*p.2.1.2.val)
ring
rw [he,div_lt_one (by
change (0:β„š)<((Word.mul (p.1.2,p.2.1.2)).val:β„š)
rw [Word.mul_val,Int.cast_mul]
exact hd)]
exact_mod_cast (Iff.rfl : (parameterNumerator p).val<(parameterDenominator p).val ↔ _)
theorem measureLarge_eq {n : β„•} {P : Budgets n} {p : Data} {H : Finset (Fin n)} {hs : List Bool}
(hn : 0<n) (h : Rep P p) (hH : MaskRep H hs) :
measureLarge (p,hs)=decide ((1/2:β„š)<P.measure H) := by
change Word.lt (total p,Word.double (Word.sum (Array.mask (density p,hs))))=_
rw [Word.lt_eval,Word.double_val]
apply decide_eq_decide.mpr
have ht := total_eq h
have hm := masked_eq h hH
have hp := P.totalDensity_pos hn
have he : (1/2:β„š)<P.measure H ↔ P.totalDensity<2*mass P.density H := by
unfold Budgets.measure
rw [lt_div_iffβ‚€ hp]
constructor <;> intro hh <;> linarith
rw [he,← ht,← hm]
exact_mod_cast (Iff.rfl : (total p).val<2*(Word.sum (Array.mask (density p,hs))).val ↔ _)
end RawBudget
end MeanPayoff.TreeDevice.Certified
end
end OAI