Download data/lean/OAI/Computability/BinPacking/Trees/IntegerTreeGeometry.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 29.9 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Computability/BinPacking/Trees/IntegerTreeGeometry.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Computability/BinPacking/Trees/IntegerTreeGeometry.lean
-
curl -L -o IntegerTreeGeometry.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Computability/BinPacking/Trees/IntegerTreeGeometry.lean
29.9 kB
| import Mathlib.Data.Finset.Dedup | |
| import Mathlib.Data.Finset.Disjoint | |
| import Mathlib.Data.Finset.Lattice.Fold | |
| import Mathlib.Data.Finset.Union | |
| import Mathlib.Data.Fintype.Card | |
| import Mathlib.Data.List.Infix | |
| import Mathlib.Order.Interval.Set.Basic | |
| import OAI.Computability.BinPacking.Machines.IntegerPackingArithmetic | |
| namespace OAI | |
| namespace BinPackingGap | |
| structure UniformTree where | |
| height : β | |
| leaves : Finset (List β) | |
| leaves_nonempty : leaves.Nonempty | |
| length_leaf : β l β leaves, l.length = height | |
| positive_labels : β l β leaves, β a β l, 0 < a | |
| namespace UniformTree | |
| def prefixes (l : List β) : Finset (List β) := | |
| (Finset.range (l.length + 1)).image (fun k => l.take k) | |
| @[simp] theorem mem_prefixes_iff {w l : List β} : | |
| w β prefixes l β w.IsPrefix l := by | |
| constructor | |
| Β· intro hw | |
| obtain β¨k, _, rflβ© := Finset.mem_image.mp hw | |
| exact List.take_prefix k l | |
| Β· intro h | |
| exact Finset.mem_image.mpr | |
| β¨w.length, Finset.mem_range.mpr (Nat.lt_succ_of_le h.length_le), | |
| (List.prefix_iff_eq_take.mp h).symmβ© | |
| def vertices (T : UniformTree) : Finset (List β) := | |
| T.leaves.biUnion prefixes | |
| theorem mem_vertices_iff (T : UniformTree) {w : List β} : | |
| w β T.vertices β β l β T.leaves, w.IsPrefix l := by | |
| simp only [vertices, Finset.mem_biUnion, mem_prefixes_iff] | |
| theorem leaf_mem_vertices (T : UniformTree) {l : List β} (hl : l β T.leaves) : | |
| l β T.vertices := | |
| (T.mem_vertices_iff).mpr β¨l, hl, List.prefix_refl lβ© | |
| theorem nil_mem_vertices (T : UniformTree) : [] β T.vertices := by | |
| obtain β¨l, hlβ© := T.leaves_nonempty | |
| exact (T.mem_vertices_iff).mpr β¨l, hl, List.nil_prefixβ© | |
| theorem vertices_nonempty (T : UniformTree) : T.vertices.Nonempty := | |
| β¨[], T.nil_mem_verticesβ© | |
| theorem prefix_mem_vertices (T : UniformTree) {u v : List β} | |
| (hv : v β T.vertices) (h : u.IsPrefix v) : u β T.vertices := by | |
| obtain β¨l, hl, hvlβ© := T.mem_vertices_iff.mp hv | |
| exact T.mem_vertices_iff.mpr β¨l, hl, h.trans hvlβ© | |
| theorem take_mem_vertices (T : UniformTree) {w : List β} | |
| (hw : w β T.vertices) (k : β) : w.take k β T.vertices := | |
| T.prefix_mem_vertices hw (List.take_prefix k w) | |
| theorem length_le_height (T : UniformTree) {w : List β} | |
| (hw : w β T.vertices) : w.length β€ T.height := by | |
| obtain β¨l, hl, hβ© := T.mem_vertices_iff.mp hw | |
| exact (T.length_leaf l hl) βΈ h.length_le | |
| theorem mem_leaves_iff_length_eq_height (T : UniformTree) {w : List β} | |
| (hw : w β T.vertices) : w β T.leaves β w.length = T.height := by | |
| constructor | |
| Β· exact T.length_leaf w | |
| Β· intro hlen | |
| obtain β¨l, hl, hβ© := T.mem_vertices_iff.mp hw | |
| have heq : w = l := h.eq_of_length (hlen.trans (T.length_leaf l hl).symm) | |
| simpa only [heq] using hl | |
| abbrev Node (T : UniformTree) := {w : List β // w β T.vertices} | |
| instance nodeFintype (T : UniformTree) : Fintype T.Node := | |
| Finset.fintypeCoeSort T.vertices | |
| def root (T : UniformTree) : T.Node := β¨[], T.nil_mem_verticesβ© | |
| instance nodeNonempty (T : UniformTree) : Nonempty T.Node := β¨T.rootβ© | |
| @[simp] theorem root_val (T : UniformTree) : T.root.val = [] := rfl | |
| @[simp] theorem node_card (T : UniformTree) : | |
| Fintype.card T.Node = T.vertices.card := Fintype.card_coe T.vertices | |
| theorem node_card_pos (T : UniformTree) : 0 < Fintype.card T.Node := | |
| Fintype.card_pos | |
| def depth {T : UniformTree} (v : T.Node) : β := v.val.length | |
| def ancestor {T : UniformTree} (u v : T.Node) : Prop := u.val.IsPrefix v.val | |
| def leaf {T : UniformTree} (v : T.Node) : Prop := v.val β T.leaves | |
| def child {T : UniformTree} (u v : T.Node) : Prop := | |
| β a : β, v.val = u.val ++ [a] | |
| @[simp] theorem depth_root (T : UniformTree) : depth T.root = 0 := rfl | |
| theorem depth_le_height {T : UniformTree} (v : T.Node) : depth v β€ T.height := | |
| T.length_le_height v.property | |
| theorem leaf_iff_depth_eq_height {T : UniformTree} (v : T.Node) : | |
| leaf v β depth v = T.height := | |
| T.mem_leaves_iff_length_eq_height v.property | |
| @[simp] theorem ancestor_refl {T : UniformTree} (v : T.Node) : ancestor v v := | |
| List.prefix_refl v.val | |
| theorem ancestor_trans {T : UniformTree} {u v w : T.Node} | |
| (huv : ancestor u v) (hvw : ancestor v w) : ancestor u w := huv.trans hvw | |
| theorem ancestor_depth_le {T : UniformTree} {u v : T.Node} | |
| (h : ancestor u v) : depth u β€ depth v := h.length_le | |
| theorem eq_of_ancestor_of_depth_eq {T : UniformTree} {u v : T.Node} | |
| (h : ancestor u v) (hlen : depth u = depth v) : u = v := | |
| Subtype.ext (h.eq_of_length hlen) | |
| @[simp] theorem root_ancestor {T : UniformTree} (v : T.Node) : ancestor T.root v := | |
| List.nil_prefix | |
| theorem eq_root_iff_depth_eq_zero {T : UniformTree} (v : T.Node) : | |
| v = T.root β depth v = 0 := by | |
| constructor | |
| Β· rintro rfl | |
| rfl | |
| Β· intro h | |
| apply Subtype.ext | |
| exact List.length_eq_zero_iff.mp h | |
| def ancestorAt {T : UniformTree} (v : T.Node) (k : β) : T.Node := | |
| β¨v.val.take k, T.take_mem_vertices v.property kβ© | |
| @[simp] theorem ancestorAt_val {T : UniformTree} (v : T.Node) (k : β) : | |
| (ancestorAt v k).val = v.val.take k := rfl | |
| theorem ancestorAt_ancestor {T : UniformTree} (v : T.Node) (k : β) : | |
| ancestor (ancestorAt v k) v := List.take_prefix k v.val | |
| theorem depth_ancestorAt {T : UniformTree} (v : T.Node) {k : β} | |
| (hk : k β€ depth v) : depth (ancestorAt v k) = k := by | |
| exact List.length_take_of_le hk | |
| theorem ancestor_eq_ancestorAt {T : UniformTree} {u v : T.Node} | |
| (h : ancestor u v) : u = ancestorAt v (depth u) := | |
| Subtype.ext (List.prefix_iff_eq_take.mp h) | |
| def branchBound (T : UniformTree) : β := | |
| max 1 (T.leaves.sup (fun l => l.toFinset.sup id)) | |
| theorem one_le_branchBound (T : UniformTree) : 1 β€ T.branchBound := | |
| le_max_left _ _ | |
| theorem branchBound_pos (T : UniformTree) : 0 < T.branchBound := | |
| Nat.lt_of_lt_of_le Nat.zero_lt_one T.one_le_branchBound | |
| theorem label_pos {T : UniformTree} (v : T.Node) {a : β} (ha : a β v.val) : | |
| 0 < a := by | |
| obtain β¨l, hl, hβ© := T.mem_vertices_iff.mp v.property | |
| exact T.positive_labels l hl a (h.subset ha) | |
| theorem label_le_branchBound {T : UniformTree} (v : T.Node) {a : β} | |
| (ha : a β v.val) : a β€ T.branchBound := by | |
| obtain β¨l, hl, hβ© := T.mem_vertices_iff.mp v.property | |
| calc | |
| a β€ l.toFinset.sup id := | |
| Finset.le_sup (f := id) (List.mem_toFinset.mpr (h.subset ha)) | |
| _ β€ T.leaves.sup (fun l => l.toFinset.sup id) := | |
| Finset.le_sup (f := fun l : List β => l.toFinset.sup id) hl | |
| _ β€ T.branchBound := le_max_right _ _ | |
| theorem exists_child_word {T : UniformTree} (v : T.Node) (hv : depth v < T.height) : | |
| β a : β, v.val ++ [a] β T.vertices := by | |
| obtain β¨l, hl, tail, htailβ© := T.mem_vertices_iff.mp v.property | |
| cases tail with | |
| | nil => | |
| have heq : v.val = l := by simpa using htail | |
| have hlen : depth v = T.height := by | |
| change v.val.length = T.height | |
| rw [heq, T.length_leaf l hl] | |
| exact (Nat.ne_of_lt hv hlen).elim | |
| | cons a tail => | |
| refine β¨a, T.mem_vertices_iff.mpr β¨l, hl, tail, ?_β©β© | |
| simpa [List.append_assoc] using htail | |
| theorem exists_child {T : UniformTree} (v : T.Node) (hv : depth v < T.height) : | |
| β u : T.Node, child v u := by | |
| obtain β¨a, haβ© := exists_child_word v hv | |
| exact β¨β¨v.val ++ [a], haβ©, a, rflβ© | |
| theorem child_depth {T : UniformTree} {u v : T.Node} (h : child u v) : | |
| depth v = depth u + 1 := by | |
| obtain β¨a, haβ© := h | |
| simp only [depth, ha, List.length_append, List.length_singleton] | |
| theorem child_ancestor {T : UniformTree} {u v : T.Node} (h : child u v) : | |
| ancestor u v := by | |
| obtain β¨a, haβ© := h | |
| exact β¨[a], ha.symmβ© | |
| theorem child_label_bounds {T : UniformTree} {u v : T.Node} {a : β} | |
| (h : v.val = u.val ++ [a]) : 0 < a β§ a β€ T.branchBound := by | |
| have ha : a β v.val := by simp [h] | |
| exact β¨label_pos v ha, label_le_branchBound v haβ© | |
| theorem leaf_iff_no_child {T : UniformTree} (v : T.Node) : | |
| leaf v β Β¬ β u : T.Node, child v u := by | |
| constructor | |
| Β· intro hv β¨u, huβ© | |
| have heq := (leaf_iff_depth_eq_height v).mp hv | |
| have hle := depth_le_height u | |
| have hdepth := child_depth hu | |
| omega | |
| Β· intro h | |
| apply (leaf_iff_depth_eq_height v).mpr | |
| have hle := depth_le_height v | |
| by_contra hne | |
| exact h (exists_child v (lt_of_le_of_ne hle hne)) | |
| end UniformTree | |
| def levelCount (marks : Finset (List β)) (depth : β) : β := | |
| (marks.filter (fun w => w.length = depth)).card | |
| def colorCount (marks : Finset (List β)) (leaf : List β) : β := | |
| (marks.filter (fun w => w.IsPrefix leaf)).card | |
| theorem colorCount_eq_card_inter_prefixes (marks : Finset (List β)) | |
| (leaf : List β) : | |
| colorCount marks leaf = (marks β© UniformTree.prefixes leaf).card := by | |
| unfold colorCount | |
| congr 1 | |
| ext w | |
| simp only [Finset.mem_filter, Finset.mem_inter, | |
| UniformTree.mem_prefixes_iff] | |
| def RespectsTreeQuota (T : UniformTree) (quota : β β β) | |
| (marks : Finset (List β)) : Prop := | |
| [] β marks β§ β depth, 1 β€ depth β depth β€ T.height β | |
| levelCount marks depth β€ quota depth | |
| structure AuxiliaryTree (phases : β) where | |
| tree : UniformTree | |
| quota : β β β | |
| quota_pos : β depth, 1 β€ depth β depth β€ tree.height β 0 < quota depth | |
| red : Finset (List β) | |
| blue : Finset (List β) | |
| red_subset : red β tree.vertices | |
| blue_subset : blue β tree.vertices | |
| root_not_red : [] β red | |
| root_not_blue : [] β blue | |
| red_blue_disjoint : Disjoint red blue | |
| red_level : β depth, 1 β€ depth β depth β€ tree.height β | |
| levelCount red depth β€ quota depth | |
| blue_level : β depth, 1 β€ depth β depth β€ tree.height β | |
| levelCount blue depth β€ quota depth | |
| total_count : β leaf β tree.leaves, | |
| colorCount red leaf + colorCount blue leaf = phases | |
| red_witness : β leaf β tree.leaves, colorCount red leaf = phases | |
| blue_witness : β leaf β tree.leaves, colorCount blue leaf = phases | |
| avoids : β marks : Finset (List β), | |
| RespectsTreeQuota tree quota marks β | |
| β leaf β tree.leaves, Disjoint marks (UniformTree.prefixes leaf) | |
| theorem card_hit_le {ΞΉ Ξ± : Type*} [DecidableEq ΞΉ] [DecidableEq Ξ±] | |
| (I : Finset ΞΉ) (A : Finset Ξ±) (P : ΞΉ β Finset Ξ±) | |
| (hP : (I : Set ΞΉ).PairwiseDisjoint P) : | |
| (I.filter (fun i => Β¬ Disjoint A (P i))).card β€ A.card := by | |
| classical | |
| let hit := I.filter (fun i => Β¬ Disjoint A (P i)) | |
| have hx (i : {i // i β hit}) : β x : {x // x β A}, x.val β P i.val := by | |
| obtain β¨x, hxA, hxPβ© := | |
| Finset.not_disjoint_iff.mp (Finset.mem_filter.mp i.property).2 | |
| exact β¨β¨x, hxAβ©, hxPβ© | |
| let f (i : {i // i β hit}) : {x // x β A} := (hx i).choose | |
| have hf (i : {i // i β hit}) : (f i).val β P i.val := (hx i).choose_spec | |
| have hinj : Function.Injective f := by | |
| intro i j hij | |
| apply Subtype.ext | |
| by_contra hne | |
| apply Finset.disjoint_left.mp | |
| (hP (Finset.mem_filter.mp i.property).1 | |
| (Finset.mem_filter.mp j.property).1 hne) (hf i) | |
| rw [hij] | |
| exact hf j | |
| simpa only [Fintype.card_coe, hit] using Fintype.card_le_of_injective f hinj | |
| theorem exists_disjoint_of_card_lt {ΞΉ Ξ± : Type*} [DecidableEq ΞΉ] [DecidableEq Ξ±] | |
| (I : Finset ΞΉ) (A : Finset Ξ±) (P : ΞΉ β Finset Ξ±) | |
| (hP : (I : Set ΞΉ).PairwiseDisjoint P) (hcard : A.card < I.card) : | |
| β i β I, Disjoint A (P i) := by | |
| classical | |
| by_contra h | |
| have hhit : β i β I, Β¬ Disjoint A (P i) := fun i hi hd => h β¨i, hi, hdβ© | |
| have hfilter : I.filter (fun i => Β¬ Disjoint A (P i)) = I := | |
| Finset.filter_eq_self.mpr hhit | |
| have hle := card_hit_le I A P hP | |
| rw [hfilter] at hle | |
| exact (Nat.not_le_of_lt hcard) hle | |
| def prefixesAfter (stem word : List β) : Finset (List β) := | |
| ((UniformTree.prefixes word).erase []).image (fun v => stem ++ v) | |
| @[simp] | |
| theorem mem_prefixesAfter_iff {stem word w : List β} : | |
| w β prefixesAfter stem word β | |
| β v, v β [] β§ v.IsPrefix word β§ stem ++ v = w := by | |
| simp [prefixesAfter, Finset.mem_image, and_assoc] | |
| theorem stem_not_mem_prefixesAfter (stem word : List β) : | |
| stem β prefixesAfter stem word := by | |
| intro h | |
| obtain β¨v, hv, _, heqβ© := mem_prefixesAfter_iff.mp h | |
| apply hv | |
| apply List.append_cancel_left (as := stem) | |
| simpa only [List.append_nil] using heq | |
| theorem prefixes_append_eq (stem word : List β) : | |
| UniformTree.prefixes (stem ++ word) = | |
| UniformTree.prefixes stem βͺ prefixesAfter stem word := by | |
| ext w | |
| simp only [Finset.mem_union, UniformTree.mem_prefixes_iff] | |
| constructor | |
| Β· intro hw | |
| rcases List.prefix_or_prefix_of_prefix hw (List.prefix_append stem word) with h | h | |
| Β· exact Or.inl h | |
| Β· obtain β¨v, rflβ© := h | |
| by_cases hv : v = [] | |
| Β· subst v | |
| exact Or.inl (by simpa only [List.append_nil] using List.prefix_refl stem) | |
| Β· exact Or.inr (mem_prefixesAfter_iff.mpr | |
| β¨v, hv, (List.prefix_append_right_inj stem).mp hw, rflβ©) | |
| Β· rintro (hw | hw) | |
| Β· exact hw.trans (List.prefix_append stem word) | |
| Β· obtain β¨v, _, hv, rflβ© := mem_prefixesAfter_iff.mp hw | |
| exact (List.prefix_append_right_inj stem).mpr hv | |
| theorem disjoint_positive_prefixes_cons {a b : β} (u v : List β) (hab : a β b) : | |
| Disjoint ((UniformTree.prefixes (a :: u)).erase []) | |
| ((UniformTree.prefixes (b :: v)).erase []) := by | |
| apply Finset.disjoint_left.mpr | |
| intro w hwa hwb | |
| have hpa := UniformTree.mem_prefixes_iff.mp (Finset.mem_erase.mp hwa).2 | |
| have hpb := UniformTree.mem_prefixes_iff.mp (Finset.mem_erase.mp hwb).2 | |
| cases w with | |
| | nil => exact (Finset.mem_erase.mp hwa).1 rfl | |
| | cons x xs => | |
| exact hab ((List.cons_prefix_cons.mp hpa).1.symm.trans | |
| (List.cons_prefix_cons.mp hpb).1) | |
| theorem disjoint_prefixesAfter_cons (stem : List β) {a b : β} | |
| (u v : List β) (hab : a β b) : | |
| Disjoint (prefixesAfter stem (a :: u)) (prefixesAfter stem (b :: v)) := by | |
| apply Finset.disjoint_left.mpr | |
| intro w hwa hwb | |
| obtain β¨x, hx, hxeqβ© := Finset.mem_image.mp hwa | |
| obtain β¨y, hy, hyeqβ© := Finset.mem_image.mp hwb | |
| have hxy : x = y := List.append_cancel_left (hxeq.trans hyeq.symm) | |
| exact Finset.disjoint_left.mp (disjoint_positive_prefixes_cons u v hab) | |
| hx (hxy.symm βΈ hy) | |
| theorem disjoint_positive_spine_prefixes {a b n m : β} (hab : a β b) : | |
| Disjoint ((UniformTree.prefixes ((a + 1) :: List.replicate n 1)).erase []) | |
| ((UniformTree.prefixes ((b + 1) :: List.replicate m 1)).erase []) := by | |
| apply disjoint_positive_prefixes_cons | |
| exact fun h => hab (Nat.add_right_cancel h) | |
| theorem disjoint_spine_prefixesAfter (stem : List β) {a b n m : β} (hab : a β b) : | |
| Disjoint (prefixesAfter stem ((a + 1) :: List.replicate n 1)) | |
| (prefixesAfter stem ((b + 1) :: List.replicate m 1)) := by | |
| apply disjoint_prefixesAfter_cons | |
| exact fun h => hab (Nat.add_right_cancel h) | |
| end BinPackingGap | |
| namespace BinPackingGap.TreeGeometry | |
| open _root_.OAI.BinPackingGap.Geometry UniformTree | |
| inductive Side where | |
| | plus | |
| | minus | |
| deriving DecidableEq | |
| instance : Fintype Side where | |
| elems := {.plus, .minus} | |
| complete s := by cases s <;> simp | |
| def rootOffset : Side β β | |
| | .plus => 0 | |
| | .minus => 4 | |
| def pathSum (m R D : β) : β β List β β β | |
| | _, [] => 0 | |
| | k, a :: w => 4 * (a : β) * lambda m R D (k + 1) + pathSum m R D (k + 1) w | |
| def left (m R D : β) (s : Side) (w : List β) : β := | |
| rootOffset s + pathSum m R D 0 w | |
| def width (m R D : β) (w : List β) : β := | |
| if w = [] then 1 else lambda m R D w.length | |
| def right (m R D : β) (s : Side) (w : List β) : β := | |
| left m R D s w + width m R D w | |
| def cell (m R D : β) (s : Side) (w : List β) : Set β := | |
| Set.Icc (left m R D s w) (right m R D s w) | |
| def BoundedAddress (D : β) (w : List β) : Prop := β a β w, a β€ D | |
| theorem boundedAddress_node {T : UniformTree} (v : T.Node) {D : β} | |
| (hD : T.branchBound β€ D) : BoundedAddress D v.val := | |
| fun _ ha => (label_le_branchBound v ha).trans hD | |
| @[simp] theorem left_nil (m R D : β) (s : Side) : | |
| left m R D s [] = rootOffset s := by simp [left, pathSum] | |
| @[simp] theorem width_nil (m R D : β) : width m R D [] = 1 := by simp [width] | |
| @[simp] theorem right_nil (m R D : β) (s : Side) : | |
| right m R D s [] = rootOffset s + 1 := by simp [right] | |
| theorem width_of_ne_nil (m R D : β) {w : List β} (hw : w β []) : | |
| width m R D w = lambda m R D w.length := by simp [width, hw] | |
| theorem width_pos (m R D : β) (w : List β) : 0 < width m R D w := by | |
| by_cases hw : w = [] | |
| Β· simp [width, hw] | |
| Β· simpa [width, hw] using lambda_pos m R D w.length | |
| theorem left_lt_right (m R D : β) (s : Side) (w : List β) : | |
| left m R D s w < right m R D s w := by | |
| exact lt_add_of_pos_right _ (width_pos m R D w) | |
| theorem cell_nonempty (m R D : β) (s : Side) (w : List β) : | |
| (cell m R D s w).Nonempty := | |
| β¨left m R D s w, le_rfl, (left_lt_right m R D s w).leβ© | |
| theorem pathSum_append (m R D k : β) (u v : List β) : | |
| pathSum m R D k (u ++ v) = | |
| pathSum m R D k u + pathSum m R D (k + u.length) v := by | |
| induction u generalizing k with | |
| | nil => simp [pathSum] | |
| | cons a u ih => | |
| simp only [List.cons_append, pathSum, ih, List.length_cons] | |
| rw [show k + 1 + u.length = k + (u.length + 1) by omega] | |
| ring | |
| theorem left_append_singleton (m R D : β) (s : Side) (w : List β) (a : β) : | |
| left m R D s (w ++ [a]) = | |
| left m R D s w + 4 * (a : β) * lambda m R D (w.length + 1) := by | |
| simp only [left, pathSum_append, Nat.zero_add, pathSum, add_zero] | |
| ring | |
| theorem right_append_singleton (m R D : β) (s : Side) (w : List β) (a : β) : | |
| right m R D s (w ++ [a]) = | |
| left m R D s w + (4 * (a : β) + 1) * lambda m R D (w.length + 1) := by | |
| have hn : w ++ [a] β [] := by simp | |
| rw [right, left_append_singleton, width_of_ne_nil m R D hn] | |
| simp only [List.length_append, List.length_singleton] | |
| ring | |
| theorem child_endpoints (m R D : β) (s : Side) (w : List β) {a : β} | |
| (ha : a β€ D) : | |
| left m R D s w β€ left m R D s (w ++ [a]) β§ | |
| right m R D s (w ++ [a]) β€ right m R D s w := by | |
| have hscale := lambda_pos m R D (w.length + 1) | |
| have hcast : (a : β) β€ D := by exact_mod_cast ha | |
| have ha0 : (0 : β) β€ a := Nat.cast_nonneg a | |
| constructor | |
| Β· rw [left_append_singleton] | |
| nlinarith | |
| Β· rw [right_append_singleton, right] | |
| have hmul : (4 * (a : β) + 1) * lambda m R D (w.length + 1) β€ | |
| (4 * (D : β) + 1) * lambda m R D (w.length + 1) := by | |
| exact mul_le_mul_of_nonneg_right (by linarith) hscale.le | |
| by_cases hw : w = [] | |
| Β· subst w | |
| have hfirst := first_children_lt_tenth m R D | |
| simp only [List.length_nil, Nat.zero_add, width_nil] at * | |
| linarith | |
| Β· rw [width_of_ne_nil m R D hw] | |
| have hchild := children_succ_lt_parent m R D w.length | |
| linarith | |
| theorem append_endpoints (m R D : β) (s : Side) (u v : List β) | |
| (hv : BoundedAddress D v) : | |
| left m R D s u β€ left m R D s (u ++ v) β§ | |
| right m R D s (u ++ v) β€ right m R D s u := by | |
| induction v generalizing u with | |
| | nil => simp | |
| | cons a v ih => | |
| have hc := child_endpoints m R D s u (hv a (by simp)) | |
| have ht := ih (u ++ [a]) (fun b hb => hv b (by simp [hb])) | |
| have heq : (u ++ [a]) ++ v = u ++ a :: v := by simp [List.append_assoc] | |
| rw [heq] at ht | |
| exact β¨hc.1.trans ht.1, ht.2.trans hc.2β© | |
| theorem prefix_endpoints (m R D : β) (s : Side) {u v : List β} | |
| (h : u.IsPrefix v) (hv : BoundedAddress D v) : | |
| left m R D s u β€ left m R D s v β§ right m R D s v β€ right m R D s u := by | |
| obtain β¨t, rflβ© := h | |
| exact append_endpoints m R D s u t (fun a ha => hv a (by simp [ha])) | |
| theorem cell_subset_of_prefix (m R D : β) (s : Side) {u v : List β} | |
| (h : u.IsPrefix v) (hv : BoundedAddress D v) : | |
| cell m R D s v β cell m R D s u := by | |
| have hb := prefix_endpoints m R D s h hv | |
| intro x hx | |
| exact β¨hb.1.trans hx.1, hx.2.trans hb.2β© | |
| theorem nonroot_endpoints (m R D : β) (s : Side) {w : List β} | |
| (hw : w β []) (hb : BoundedAddress D w) : | |
| rootOffset s β€ left m R D s w β§ right m R D s w < rootOffset s + 1 / 10 := by | |
| cases w with | |
| | nil => exact (hw rfl).elim | |
| | cons a w => | |
| have ha := hb a (by simp) | |
| have hroot := prefix_endpoints m R D s (u := []) List.nil_prefix hb | |
| have hfirst := prefix_endpoints m R D s (u := [a]) β¨w, rflβ© hb | |
| simp only [List.singleton_append] at hfirst | |
| have hm : (4 * (a : β) + 1) * lambda m R D 1 β€ | |
| (4 * (D : β) + 1) * lambda m R D 1 := by | |
| have hcast : (a : β) β€ D := by exact_mod_cast ha | |
| exact mul_le_mul_of_nonneg_right (by linarith) (lambda_nonneg m R D 1) | |
| have hf := first_children_lt_tenth m R D | |
| have hr := right_append_singleton m R D s [] a | |
| simp only [List.nil_append, List.length_nil, Nat.zero_add, left_nil] at hr | |
| exact β¨by simpa only [left_nil] using hroot.1, by linarith [hfirst.2]β© | |
| private theorem exists_snoc_of_length_succ {w : List β} {ell : β} | |
| (h : w.length = ell + 1) : | |
| β u a, w = u ++ [a] β§ u.length = ell := by | |
| induction w using List.reverseRecOn with | |
| | nil => simp at h | |
| | append_singleton u a _ => | |
| exact β¨u, a, rfl, by simpa using hβ© | |
| theorem same_depth_gap (m R D ell : β) {s t : Side} {u v : List β} | |
| (hu : u.length = ell) (hv : v.length = ell) | |
| (hbu : BoundedAddress D u) (hbv : BoundedAddress D v) | |
| (hne : (s, u) β (t, v)) : | |
| right m R D s u + 3 * lambda m R D ell β€ left m R D t v β¨ | |
| right m R D t v + 3 * lambda m R D ell β€ left m R D s u := by | |
| induction ell generalizing s t u v with | |
| | zero => | |
| have hu0 : u = [] := List.length_eq_zero_iff.mp hu | |
| have hv0 : v = [] := List.length_eq_zero_iff.mp hv | |
| subst u | |
| subst v | |
| have hscale := lambda_le_one m R D 0 | |
| cases s <;> cases t <;> simp only [left_nil, right_nil, rootOffset] at * | |
| Β· exact (hne rfl).elim | |
| Β· exact Or.inl (by linarith) | |
| Β· exact Or.inr (by linarith) | |
| Β· exact (hne rfl).elim | |
| | succ ell ih => | |
| obtain β¨u, a, rfl, hupβ© := exists_snoc_of_length_succ hu | |
| obtain β¨v, b, rfl, hvpβ© := exists_snoc_of_length_succ hv | |
| have hau : a β€ D := hbu a (by simp) | |
| have hbv' : b β€ D := hbv b (by simp) | |
| have hpu : BoundedAddress D u := fun c hc => hbu c (by simp [hc]) | |
| have hpv : BoundedAddress D v := fun c hc => hbv c (by simp [hc]) | |
| by_cases hp : (s, u) = (t, v) | |
| Β· have hs : s = t := congrArg Prod.fst hp | |
| have huv : u = v := congrArg Prod.snd hp | |
| subst t | |
| subst v | |
| have hab : a β b := by | |
| intro heq | |
| exact hne (by simp [heq]) | |
| have hscale := lambda_nonneg m R D (ell + 1) | |
| rw [right_append_singleton, right_append_singleton, | |
| left_append_singleton, left_append_singleton, hup] | |
| rcases lt_or_gt_of_ne hab with hab | hba | |
| Β· left | |
| have hc : (a : β) + 1 β€ b := by exact_mod_cast (Nat.succ_le_iff.mpr hab) | |
| nlinarith | |
| Β· right | |
| have hc : (b : β) + 1 β€ a := by exact_mod_cast (Nat.succ_le_iff.mpr hba) | |
| nlinarith | |
| Β· have hgap := ih hup hvp hpu hpv hp | |
| have hcu := child_endpoints m R D s u hau | |
| have hcv := child_endpoints m R D t v hbv' | |
| have hscale := lambda_antitone m R D (Nat.le_succ ell) | |
| rcases hgap with hgap | hgap | |
| Β· left | |
| linarith [hcu.2, hcv.1] | |
| Β· right | |
| linarith [hcv.2, hcu.1] | |
| theorem eq_of_short_intersections (m R D : β) {ell L : β} | |
| (hell : ell β€ L) {s t : Side} {u v : List β} {a b : β} | |
| (hu : u.length = ell) (hv : v.length = ell) | |
| (hbu : BoundedAddress D u) (hbv : BoundedAddress D v) | |
| (hlen : b - a β€ lambda m R D ell + delta m R D L) | |
| (hiu : (Set.Icc a b β© cell m R D s u).Nonempty) | |
| (hiv : (Set.Icc a b β© cell m R D t v).Nonempty) : (s, u) = (t, v) := by | |
| by_contra hne | |
| have hgap := same_depth_gap m R D ell hu hv hbu hbv hne | |
| obtain β¨x, hx, hxuβ© := hiu | |
| obtain β¨y, hy, hyvβ© := hiv | |
| have hshort := lambda_add_delta_lt_three m R D hell | |
| rcases hgap with hgap | hgap | |
| Β· linarith [hx.1, hx.2, hy.1, hy.2, hxu.1, hxu.2, hyv.1, hyv.2] | |
| Β· linarith [hx.1, hx.2, hy.1, hy.2, hxu.1, hxu.2, hyv.1, hyv.2] | |
| def nodeCell (m R D : β) (s : Side) {T : UniformTree} (v : T.Node) : Set β := | |
| cell m R D s v.val | |
| theorem nodeCell_subset_of_ancestor (m R D : β) (s : Side) {T : UniformTree} | |
| (hD : T.branchBound β€ D) {u v : T.Node} (h : ancestor u v) : | |
| nodeCell m R D s v β nodeCell m R D s u := | |
| cell_subset_of_prefix m R D s h (boundedAddress_node v hD) | |
| theorem nodeCell_subset_root (m R D : β) (s : Side) {T : UniformTree} | |
| (hD : T.branchBound β€ D) (v : T.Node) : | |
| nodeCell m R D s v β Set.Icc (rootOffset s) (rootOffset s + 1) := by | |
| simpa [nodeCell, cell] using | |
| nodeCell_subset_of_ancestor m R D s hD (root_ancestor v) | |
| theorem nonroot_nodeCell_first_tenth (m R D : β) (s : Side) {T : UniformTree} | |
| (hD : T.branchBound β€ D) {v : T.Node} (hv : v β T.root) : | |
| nodeCell m R D s v β Set.Ico (rootOffset s) (rootOffset s + 1 / 10) := by | |
| have hn : v.val β [] := by | |
| intro heq | |
| exact hv (Subtype.ext heq) | |
| have he := nonroot_endpoints m R D s hn (boundedAddress_node v hD) | |
| intro x hx | |
| exact β¨he.1.trans hx.1, hx.2.trans_lt he.2β© | |
| theorem mem_ancestor_cell (m R D : β) (s : Side) {T : UniformTree} | |
| (hD : T.branchBound β€ D) (v : T.Node) (ell : β) {x : β} | |
| (hx : x β nodeCell m R D s v) : | |
| x β nodeCell m R D s (ancestorAt v ell) := | |
| nodeCell_subset_of_ancestor m R D s hD (ancestorAt_ancestor v ell) hx | |
| theorem leaf_intersection_is_ancestor (m R D : β) {L ell : β} | |
| (hell : ell β€ L) {T U : UniformTree} (hT : T.height = L) | |
| (hDT : T.branchBound β€ D) (hDU : U.branchBound β€ D) | |
| {s t : Side} (v : T.Node) (hv : leaf v) (w : U.Node) | |
| (hw : depth w = ell) {a b x : β} | |
| (hlen : b - a β€ lambda m R D ell + delta m R D L) | |
| (hx : x β Set.Icc a b) (hxv : x β nodeCell m R D s v) | |
| (hwI : (Set.Icc a b β© nodeCell m R D t w).Nonempty) : | |
| (t, w.val) = (s, (ancestorAt v ell).val) := by | |
| have hdepth : depth v = L := ((leaf_iff_depth_eq_height v).mp hv).trans hT | |
| have hanc : depth (ancestorAt v ell) = ell := | |
| depth_ancestorAt v (by simpa only [hdepth] using hell) | |
| exact eq_of_short_intersections m R D hell hw hanc | |
| (boundedAddress_node w hDU) (boundedAddress_node (ancestorAt v ell) hDT) | |
| hlen hwI β¨x, hx, mem_ancestor_cell m R D s hDT v ell hxvβ© | |
| def commonBranchBound (plus minus : UniformTree) : β := | |
| max plus.branchBound minus.branchBound | |
| theorem plus_branch_le (plus minus : UniformTree) : | |
| plus.branchBound β€ commonBranchBound plus minus := le_max_left _ _ | |
| theorem minus_branch_le (plus minus : UniformTree) : | |
| minus.branchBound β€ commonBranchBound plus minus := le_max_right _ _ | |
| end BinPackingGap.TreeGeometry | |
| namespace BinPackingGap.IntegerPackingArithmetic | |
| def pathNumerator (D L : β) : β β List β β β | |
| | _, [] => 0 | |
| | k, a :: w => 40 * a * widthRadix D ^ (L - (k + 1)) + | |
| pathNumerator D L (k + 1) w | |
| def rootOffsetNumerator : TreeGeometry.Side β β | |
| | .plus => 0 | |
| | .minus => 4 | |
| def treeLeftNumerator (m R D L : β) (side : TreeGeometry.Side) | |
| (w : List β) : β := | |
| rootOffsetNumerator side * geomDenominator m R D L + pathNumerator D L 0 w | |
| def treeRightNumerator (m R D L : β) (side : TreeGeometry.Side) | |
| (w : List β) : β := | |
| treeLeftNumerator m R D L side w + | |
| if w = [] then geomDenominator m R D L else localNumerator D L w.length | |
| theorem geomDenominator_mul_pathSum (m R D L k : β) (w : List β) | |
| (hdepth : k + w.length β€ L) : | |
| (geomDenominator m R D L : β) * TreeGeometry.pathSum m R D k w = | |
| (pathNumerator D L k w : β) := by | |
| induction w generalizing k with | |
| | nil => simp [TreeGeometry.pathSum, pathNumerator] | |
| | cons a w ih => | |
| have hdepth' : k + (w.length + 1) β€ L := hdepth | |
| have hfirst : k + 1 β€ L := by omega | |
| have htail : (k + 1) + w.length β€ L := by | |
| omega | |
| have hlocal := geomDenominator_mul_lambda m R D L (k + 1) hfirst | |
| have hrest := ih (k + 1) htail | |
| simp only [TreeGeometry.pathSum, pathNumerator, Nat.cast_add, Nat.cast_mul, | |
| Nat.cast_ofNat, Nat.cast_pow] | |
| calc | |
| _ = 4 * (a : β) * ((geomDenominator m R D L : β) * | |
| Geometry.lambda m R D (k + 1)) + | |
| (geomDenominator m R D L : β) * TreeGeometry.pathSum m R D (k + 1) w := by ring | |
| _ = _ := by | |
| rw [hlocal, hrest] | |
| simp only [localNumerator, Nat.cast_mul, Nat.cast_ofNat, Nat.cast_pow] | |
| ring | |
| theorem rootOffsetNumerator_cast (side : TreeGeometry.Side) : | |
| (rootOffsetNumerator side : β) = TreeGeometry.rootOffset side := by | |
| cases side <;> rfl | |
| theorem geomDenominator_mul_treeLeft (m R D L : β) | |
| (side : TreeGeometry.Side) (w : List β) (hdepth : w.length β€ L) : | |
| (geomDenominator m R D L : β) * TreeGeometry.left m R D side w = | |
| (treeLeftNumerator m R D L side w : β) := by | |
| have hpath := geomDenominator_mul_pathSum m R D L 0 w (by simpa using hdepth) | |
| simp only [TreeGeometry.left, treeLeftNumerator, Nat.cast_add, Nat.cast_mul, | |
| rootOffsetNumerator_cast] | |
| rw [mul_add, hpath] | |
| ring | |
| theorem geomDenominator_mul_treeRight (m R D L : β) | |
| (side : TreeGeometry.Side) (w : List β) (hdepth : w.length β€ L) : | |
| (geomDenominator m R D L : β) * TreeGeometry.right m R D side w = | |
| (treeRightNumerator m R D L side w : β) := by | |
| have hleft := geomDenominator_mul_treeLeft m R D L side w hdepth | |
| simp only [TreeGeometry.right, mul_add, hleft, treeRightNumerator, Nat.cast_add] | |
| by_cases hw : w = [] | |
| Β· simp [TreeGeometry.width, hw] | |
| Β· simp only [TreeGeometry.width, hw, ite_false] | |
| rw [geomDenominator_mul_lambda m R D L w.length hdepth] | |
| end BinPackingGap.IntegerPackingArithmetic | |
| end OAI | |