File size: 14,340 Bytes
ea76aac | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | import OAI.Computability.MeanPayoff.DriverExecution
namespace OAI
namespace MeanPayoff.TreeDevice.Certified
open BinaryData Code
namespace Size
theorem count (xs : Count) : treeSize (encode xs)=2*xs.length+1 := by
induction xs with
| nil => rfl
| cons x xs ih =>
change treeSize (Data.pair .nil (encode xs))=_
rw [treeSize_pair,treeSize_nil,ih,List.length_cons]
omega
end Size
end MeanPayoff.TreeDevice.Certified
section
namespace MeanPayoff.TreeDevice.Certified
open BinaryData Code
local infixl:80 " ≫ₚ " => Code.comp
local infixr:70 " ⋏ₚ " => Code.pair
namespace Word
noncomputable def digitCount : Code W Count := by
let F := Map.device (TreeDevice.constant boolData Median.unitData ())
let D : Code Digits Count := by
refine ⟨fun d => d.bits.map (fun _ => ()), F.recode encode encode Digits.bits
(fun d => d.bits.map (fun _ => ())) (fun _ => rfl) (fun _ => rfl), ?_⟩
obtain ⟨K,d,h⟩ := PolynomialCost.map (LinearCost.constant boolData Median.unitData ()).polynomial
exact ⟨K,d,fun z => h z.bits⟩
exact digits ≫ₚ D
@[simp] theorem digitCount_length (w : W) : (digitCount w).length=w.digits.length := by
change (w.digits.map (fun _ => ())).length=w.digits.length
simp only [List.length_map]
theorem half_size (w : W) : treeSize (encode (half w)) ≤ treeSize (encode w) := by
change treeSize (wordData (Halve.halve w)) ≤ treeSize (wordData w)
rcases w with ⟨s,ds⟩
cases ds with
| nil => exact le_rfl
| cons b bs =>
simp only [Halve.halve,wordData,listData,List.tail_cons,treeSize_pair]
omega
end Word
namespace NaturalLog
abbrev State := W×Count
noncomputable def positive : Code State Bool := (Word.constant 0 ⋏ₚ fst) ≫ₚ Word.lt
noncomputable def step : Code (State×Unit) State :=
Code.branch (fst ≫ₚ positive) (((fst ≫ₚ fst ≫ₚ Word.half) ⋏ₚ (fst ≫ₚ snd ≫ₚ Counter.inc))) fst
@[simp] theorem step_eval (s : State) (u : Unit) : step (s,u)=
if 0<s.1.val then (Word.half s.1,()::s.2) else s := by
simp [step,positive,Counter.inc]
noncomputable def loop : Code (State×Count) State := fold step (by
rintro ⟨w,c⟩ u
rw [step_eval]
dsimp only
split
· rw [Size.pair,Size.pair,Size.count,Size.count,List.length_cons]
have := Word.half_size w
change _ ≤ _+1+1
omega
· omega)
noncomputable def length : Code W Count :=
(((Code.id ⋏ₚ const []) ⋏ₚ Word.digitCount) ≫ₚ loop) ≫ₚ snd
noncomputable def ceil : Code W Count :=
((Code.id ⋏ₚ Word.constant 1) ≫ₚ Word.sub) ≫ₚ length
theorem recurrence (n : ℕ) (h : 0<n) : Nat.clog 2 (n+1)=Nat.clog 2 (n/2+1)+1 := by
rw [Nat.clog_of_one_lt (by decide : 1<2) (by omega : 1<n+1)]
congr 2
omega
theorem fold_stopped (w : W) (hw : w.val ≤ 0) (c xs : Count) :
Fold.fold step.eval (w,c) xs = (w,c) := by
induction xs with
| nil => rfl
| cons u us ih =>
change Fold.fold step.eval (step ((w,c),u)) us = _
rw [step_eval]
dsimp only
rw [ite_eq_right (by omega)]
exact ih
theorem fold_length (w : W) (hw : 0 ≤ w.val) (c xs : Count) (hf : w.digits.length ≤ xs.length) :
(Fold.fold step.eval (w,c) xs).2.length = c.length+Nat.clog 2 (w.val.toNat+1) := by
induction xs generalizing w c with
| nil =>
have hd : w.digits=[] := List.length_eq_zero_iff.mp (by simpa using hf)
have hs : w.sign=false := Bool.eq_false_iff.mpr (fun h => (not_lt_of_ge hw) ((SignedBits.val_negative w).mpr h))
have hz : w.val=0 := by simp [SignedBits.Word.val,hd,hs,BinaryStack.Addition.value]
simp [Fold.fold,hz]
| cons u us ih =>
by_cases hp : 0<w.val
· have hd : w.digits ≠ [] := by
intro hd
have hb : w.val ≤ 0 := by
cases hs : w.sign <;> simp [SignedBits.Word.val,hd,hs,BinaryStack.Addition.value]
omega
have hw' : 0 ≤ (Word.half w).val := by rw [Word.half_val]; omega
have hf' : (Word.half w).digits.length ≤ us.length := by
rw [Word.half_length]
have : 0<w.digits.length := by simpa only [List.length_pos_iff] using hd
simp only [List.length_cons] at hf
omega
change (Fold.fold step.eval (step ((w,c),u)) us).2.length = _
rw [step_eval,ite_eq_left hp,ih (Word.half w) hw' (()::c) hf',List.length_cons]
have hn : 0<w.val.toNat := by omega
have he : (Word.half w).val.toNat=w.val.toNat/2 := by rw [Word.half_val]; omega
rw [he,recurrence _ hn]
omega
· have hz : w.val=0 := by omega
rw [fold_stopped w (by omega)]
simp [hz]
theorem length_exact (w : W) (hw : 0 ≤ w.val) : (length w).length=Nat.clog 2 (w.val.toNat+1) := by
change (Fold.fold step.eval (w,[]) (Word.digitCount w)).2.length=_
simpa only [List.length_nil,Nat.zero_add] using fold_length w hw [] (Word.digitCount w) (by rw [Word.digitCount_length])
theorem ceil_exact (w : W) (hw : 0 < w.val) : (ceil w).length=Nat.clog 2 w.val.toNat := by
change (length (Word.sub (w,Word.constant 1 w))).length=_
rw [length_exact _ (by simp; omega)]
congr 1
simp only [Word.sub_val,Word.constant_val]
omega
end NaturalLog
end MeanPayoff.TreeDevice.Certified
end
section
namespace MeanPayoff.TreeDevice.Certified
open BinaryData Code
local infixl:80 " ≫ₚ " => Code.comp
local infixr:70 " ⋏ₚ " => Code.pair
namespace Word
noncomputable def powerStep : Code (W×Unit) W := fst ≫ₚ double
noncomputable def powerFold : Code (W×Count) W := fold powerStep (by
intro w u
change treeSize (wordData (Multiply.double w)) ≤ treeSize (wordData w)+1+1
simp only [Multiply.double,SignedBits.cons,wordData,listData,treeSize_pair,boolData,treeSize_nil]
omega)
noncomputable def power : Code Count W := (Word.constant 1 ⋏ₚ Code.id) ≫ₚ powerFold
theorem powerFold_val (w : W) (cs : Count) : (powerFold (w,cs)).val=2^cs.length*w.val := by
change (Fold.fold powerStep.eval w cs).val=_
induction cs generalizing w with
| nil => simp [Fold.fold]
| cons u us ih =>
change (Fold.fold powerStep.eval (double w) us).val=_
rw [ih,Word.double_val,List.length_cons,pow_succ]
ring
@[simp] theorem power_val (cs : Count) : (power cs).val=2^cs.length := by
change (powerFold (Word.constant 1 cs,cs)).val=_
rw [powerFold_val,Word.constant_val,mul_one]
end Word
namespace Counter
theorem false_data (cs : Count) : listTree boolData (cs.map (fun _ => false))=encode cs := by
induction cs with
| nil => rfl
| cons u us ih =>
change Data.pair .nil (listTree boolData (us.map (fun _ => false))) = Data.pair .nil (encode us)
rw [ih]
noncomputable def bounded : Code (W×Count) Count := by
refine ⟨fun z => Address.read (z.1,z.2.map (fun _ => false)),
Address.device.recode encode encode (fun z : W×Count => (z.1,z.2.map (fun _ => false)))
(fun z => Address.read (z.1,z.2.map (fun _ => false))) ?_ (fun _ => rfl), ?_⟩
· intro z
change Data.pair (encode z.1) (listTree boolData (z.2.map (fun _ => false)))=Data.pair (encode z.1) (encode z.2)
rw [false_data]
· obtain ⟨K,d,h⟩ := Address.polynomial
refine ⟨K,d,fun z => ?_⟩
have hh := h (z.1,z.2.map (fun _ => false))
change Address.device.limit (z.1,z.2.map (fun _ => false)) ≤ K*(treeSize (product wordData (listTree Median.unitData) z)+1)^d
change Address.device.limit (z.1,z.2.map (fun _ => false)) ≤ K*(treeSize (Data.pair (wordData z.1) (listTree boolData (z.2.map (fun _ => false))))+1)^d at hh
rw [false_data] at hh
exact hh
theorem bounded_exact (w : W) (n : ℕ) (hw : w.val=n) (cap : Count) (hc : n≤cap.length) :
bounded (w,cap)=List.replicate n () :=
Address.exact n w hw _ (by simpa only [List.length_map] using hc)
noncomputable def power : Code (Count×Count) Count := ((fst ≫ₚ Word.power) ⋏ₚ snd) ≫ₚ bounded
theorem power_exact (e cap : Count) (hc : 2^e.length ≤ cap.length) :
power (e,cap)=List.replicate (2^e.length) () := by
exact bounded_exact (Word.power e) _ (by simp) cap hc
theorem pow_clog_le (n : ℕ) (hn : 0<n) : 2^Nat.clog 2 n ≤ 2*n := by
by_cases h : n≤1
· have he : n=1 := by omega
subst n
norm_num
· have hl : 0<Nat.clog 2 n := Nat.clog_pos (by decide) (by omega)
have hh := Nat.pow_pred_clog_lt_self (by decide : 1<2) (show 1<n by omega)
have he : Nat.clog 2 n=(Nat.clog 2 n).pred+1 := (Nat.succ_pred_eq_of_pos hl).symm
rw [he,pow_succ]
omega
end Counter
end MeanPayoff.TreeDevice.Certified
end
section
namespace MeanPayoff.TreeDevice.Certified
open BinaryData Code
local infixl:80 " ≫ₚ " => Code.comp
local infixr:70 " ⋏ₚ " => Code.pair
structure Parsed where
raw : Normalize.Arena
instance : Encoding Parsed := ⟨fun p => Normalize.arenaData p.raw⟩
namespace InputCode
noncomputable def parse : Code Digits Parsed := by
refine ⟨fun d => ⟨Normalize.read d.bits⟩, Normalize.device.recode encode encode Digits.bits
(fun d => (⟨Normalize.read d.bits⟩ : Parsed)) (fun _ => rfl) (fun _ => rfl), ?_⟩
obtain ⟨K,d,h⟩ := Normalize.polynomial
exact ⟨K,d,fun z => h z.bits⟩
noncomputable def n : Code Parsed Count := by
refine ⟨fun p => p.raw.1, Normalize.nDevice.recode encode encode Parsed.raw (fun p => p.raw.1)
(fun _ => rfl) (fun _ => rfl), ?_⟩
obtain ⟨K,d,h⟩ := (LinearCost.first Normalize.countData (product Normalize.ownersData Normalize.edgesData)).polynomial
exact ⟨K,d,fun p => h p.raw⟩
noncomputable def edges : Code Parsed (List EdgeRead.Edge) := by
refine ⟨fun p => p.raw.2.2, Normalize.edgesDevice.recode encode encode Parsed.raw (fun p => p.raw.2.2)
(fun _ => rfl) (fun _ => rfl), ?_⟩
have hc : PolynomialCost Normalize.edgesDevice :=
(LinearCost.compose (LinearCost.second _ _) (LinearCost.second _ _)).polynomial
obtain ⟨K,d,h⟩ := hc
exact ⟨K,d,fun p => h p.raw⟩
noncomputable def rows : Code Parsed RawBasic.Arena := by
refine ⟨fun p => Rows.build p.raw, Rows.buildDevice.recode encode encode Parsed.raw (fun p => Rows.build p.raw)
(fun _ => rfl) (fun _ => rfl), ?_⟩
obtain ⟨K,d,h⟩ := Rows.build_polynomial
exact ⟨K,d,fun p => h p.raw⟩
def game (G : Game) : Parsed := ⟨(List.replicate G.n (),(List.ofFn G.maxOwner,List.ofFn (Normalize.encodedEdge G)))⟩
@[simp] theorem parse_game (G : Game) : parse ⟨input G⟩=game G := by
change Parsed.mk (Normalize.read (input G))=_
rw [Normalize.exact]
rfl
@[simp] theorem n_game (G : Game) : n (game G)=List.replicate G.n () := rfl
@[simp] theorem rows_game (G : Game) : rows (game G)=Graph.codeVertices G (Normalize.word G) := Rows.build_exact _
noncomputable def absolute : Code W W := (Code.id ⋏ₚ Word.neg) ≫ₚ Word.max
@[simp] theorem absolute_val (w : W) : (absolute w).val=|w.val| := by simp [absolute,abs_eq_max_neg]
noncomputable def maximumFold : Code (W×List W) W := fold Word.max (by
intro w x
change treeSize (wordData (Order.high (w,x))) ≤ treeSize (wordData w)+treeSize (wordData x)+1
unfold Order.high
split <;> dsimp only [Prod.fst,Prod.snd] <;> omega)
noncomputable def maximum : Code (List W) W := (Word.constant 1 ⋏ₚ Code.id) ≫ₚ maximumFold
noncomputable def bound : Code Parsed W := edges ≫ₚ map (snd ≫ₚ snd ≫ₚ absolute) ≫ₚ maximum
noncomputable def width : Code Parsed W := (((n ≫ₚ Counter.word) ⋏ₚ bound) ≫ₚ Word.mul) ≫ₚ RawBudget.times 8
theorem fold_upper (a : W) (xs : List W) (M : ℤ) (ha : a.val ≤ M) (hx : ∀ x∈xs, x.val ≤ M) :
(maximumFold (a,xs)).val ≤ M := by
change (Fold.fold Word.max.eval a xs).val ≤ M
induction xs generalizing a with
| nil => exact ha
| cons x xs ih =>
change (Fold.fold Word.max.eval (Word.max (a,x)) xs).val ≤ M
exact ih _ (by simp only [Word.max_val]; exact max_le ha (hx _ (by simp))) (by intro y hy; exact hx y (by simp [hy]))
theorem fold_lower (a : W) (xs : List W) : a.val ≤ (maximumFold (a,xs)).val := by
change a.val ≤ (Fold.fold Word.max.eval a xs).val
induction xs generalizing a with
| nil => exact le_rfl
| cons x xs ih =>
exact le_trans (show a.val ≤ (Word.max (a,x)).val by rw [Word.max_val]; exact le_max_left _ _) (ih (Word.max (a,x)))
theorem fold_mem (a : W) (xs : List W) (x : W) (hx : x∈xs) : x.val ≤ (maximumFold (a,xs)).val := by
change x.val ≤ (Fold.fold Word.max.eval a xs).val
induction xs generalizing a with
| nil => simp at hx
| cons y ys ih =>
rcases List.mem_cons.mp hx with rfl | hx
· exact le_trans (show x.val ≤ (Word.max (a,x)).val by rw [Word.max_val]; exact le_max_right _ _) (fold_lower (Word.max (a,x)) ys)
· exact ih (Word.max (a,y)) hx
theorem bound_val (G : Game) : (bound (game G)).val=weightBound G := by
let xs := (List.ofFn (Normalize.encodedEdge G)).map (fun e => absolute e.2.2)
change (maximumFold (Word.constant 1 xs,xs)).val=weightBound G
apply le_antisymm
· apply fold_upper
· rw [Word.constant_val]
exact le_max_left _ _
· intro x hx
obtain ⟨e,he,rfl⟩ := List.mem_map.mp hx
obtain ⟨i,hi⟩ := List.mem_ofFn.mp he
subst e
simp only [Normalize.encodedEdge,absolute_val,Normalize.word_val]
exact transformed_le_bound G i
· have h1 : 1 ≤ (maximumFold (Word.constant 1 xs,xs)).val := by simpa only [Word.constant_val] using fold_lower (Word.constant 1 xs) xs
apply max_le h1
have hsup : Finset.univ.sup (fun e : Fin G.m => (transformed G e).natAbs) ≤ (maximumFold (Word.constant 1 xs,xs)).val.toNat := by
apply Finset.sup_le
intro i _
have hmem : absolute (Normalize.word G i) ∈ xs := by
apply List.mem_map.mpr
exact ⟨Normalize.encodedEdge G i,List.mem_ofFn.mpr ⟨i,rfl⟩,rfl⟩
have hh := fold_mem (Word.constant 1 xs) xs _ hmem
rw [absolute_val,Normalize.word_val] at hh
exact_mod_cast (show ((transformed G i).natAbs : ℤ) ≤ ((maximumFold (Word.constant 1 xs,xs)).val.toNat : ℤ) by
rw [Int.natCast_natAbs,Int.toNat_of_nonneg (by omega)]; exact hh)
have hh := Int.ofNat_le.mpr hsup
rwa [Int.toNat_of_nonneg (by omega)] at hh
@[simp] theorem width_val (G : Game) : (width (game G)).val=topWidth G := by
simp only [width,comp_eval,pair_eval,RawBudget.times_val,Word.mul_val,Counter.word_val,n_game,List.length_replicate,bound_val]
unfold topWidth
ring
end InputCode
end MeanPayoff.TreeDevice.Certified
end
end OAI
|