Download lean/OAI/Computability/BinPacking/Packing/FiniteHall.lean from roseking/openAI-math-dataset: direct link, hf CLI and curl.
- Browser
- Download file 8.49 kB
-
https://huggingface.co/datasets/roseking/openAI-math-dataset/resolve/main/lean/OAI/Computability/BinPacking/Packing/FiniteHall.lean
- Command line
-
hf download hf://datasets/roseking/openAI-math-dataset/lean/OAI/Computability/BinPacking/Packing/FiniteHall.lean
-
curl -L -o FiniteHall.lean https://huggingface.co/datasets/roseking/openAI-math-dataset/resolve/main/lean/OAI/Computability/BinPacking/Packing/FiniteHall.lean
8.49 kB
| /- | |
| This file has been adapted from an external source. | |
| The source copyright header and license information are preserved below. | |
| -/ | |
| /- | |
| Copyright (c) 2021 Alena Gusakov, Bhavik Mehta, Kyle Miller. All rights reserved. | |
| Released under Apache 2.0 license as described in the file LICENSE. | |
| Authors: Alena Gusakov, Bhavik Mehta, Kyle Miller | |
| This file is adapted from Mathlib.Combinatorics.Hall.Finite. | |
| Source: https://github.com/leanprover-community/mathlib4/blob/d13f23b723b8a846827a245b89c10fc7d3f11612/Mathlib/Combinatorics/Hall/Finite.lean | |
| This code has been modified. | |
| -/ | |
| import Mathlib.Data.Fintype.Basic | |
| import Mathlib.Data.Fintype.Powerset | |
| import Mathlib.Data.Set.Finite.Basic | |
| namespace OAI | |
| open Finset | |
| namespace BinPackingGap.FiniteHall | |
| universe u v | |
| namespace HallMarriageTheorem | |
| variable {ι : Type u} {α : Type v} [DecidableEq α] {t : ι → Finset α} | |
| section Fintype | |
| variable [Fintype ι] | |
| theorem hall_cond_of_erase {x : ι} (a : α) | |
| (ha : ∀ s : Finset ι, s.Nonempty → s ≠ univ → #s < #(s.biUnion t)) | |
| (s' : Finset { x' : ι | x' ≠ x }) : #s' ≤ #(s'.biUnion fun x' => (t x').erase a) := by | |
| have := Classical.decEq ι | |
| specialize ha (s'.image fun z => z.1) | |
| rw [image_nonempty, Finset.card_image_of_injective s' Subtype.coe_injective] at ha | |
| by_cases! he : s'.Nonempty | |
| · have ha' : #s' < #(s'.biUnion fun x => t x) := by | |
| convert! ha he fun h => by simpa [← h] using mem_univ x using 2 | |
| ext x | |
| simp only [mem_image, mem_biUnion, SetCoe.exists, exists_and_right, | |
| exists_eq_right] | |
| rw [← erase_biUnion] | |
| by_cases hb : a ∈ s'.biUnion fun x => t x | |
| · rw [card_erase_of_mem hb] | |
| exact Nat.le_sub_one_of_lt ha' | |
| · rw [erase_eq_of_notMem hb] | |
| exact Nat.le_of_lt ha' | |
| · subst s' | |
| simp | |
| theorem hall_hard_inductive_step_A {n : ℕ} (hn : Fintype.card ι = n + 1) | |
| (ht : ∀ s : Finset ι, #s ≤ #(s.biUnion t)) | |
| (ih : | |
| ∀ {ι' : Type u} [Fintype ι'] (t' : ι' → Finset α), | |
| Fintype.card ι' ≤ n → | |
| (∀ s' : Finset ι', #s' ≤ #(s'.biUnion t')) → | |
| ∃ f : ι' → α, Function.Injective f ∧ ∀ x, f x ∈ t' x) | |
| (ha : ∀ s : Finset ι, s.Nonempty → s ≠ univ → #s < #(s.biUnion t)) : | |
| ∃ f : ι → α, Function.Injective f ∧ ∀ x, f x ∈ t x := by | |
| have : Nonempty ι := Fintype.card_pos_iff.mp (hn.symm ▸ Nat.succ_pos _) | |
| have := Classical.decEq ι | |
| let x := Classical.arbitrary ι | |
| have tx_ne : (t x).Nonempty := by | |
| rw [← Finset.card_pos] | |
| calc | |
| 0 < 1 := Nat.one_pos | |
| _ ≤ #(.biUnion {x} t) := ht {x} | |
| _ = (t x).card := by rw [Finset.singleton_biUnion] | |
| choose y hy using tx_ne | |
| let ι' := { x' : ι | x' ≠ x } | |
| let t' : ι' → Finset α := fun x' => (t x').erase y | |
| have card_ι' : Fintype.card ι' = n := | |
| calc | |
| Fintype.card ι' = Fintype.card ι - 1 := Set.card_ne_eq _ | |
| _ = n := by rw [hn, Nat.add_succ_sub_one, add_zero] | |
| rcases ih t' card_ι'.le (hall_cond_of_erase y ha) with ⟨f', hfinj, hfr⟩ | |
| refine ⟨fun z => if h : z = x then y else f' ⟨z, h⟩, ?_, ?_⟩ | |
| · rintro z₁ z₂ | |
| have key : ∀ {x}, y ≠ f' x := by | |
| intro x h | |
| simpa [t', ← h] using hfr x | |
| by_cases h₁ : z₁ = x <;> by_cases h₂ : z₂ = x <;> | |
| simp [h₁, h₂, hfinj.eq_iff, key, key.symm] | |
| · intro z | |
| simp only | |
| split_ifs with hz | |
| · rwa [hz] | |
| · specialize hfr ⟨z, hz⟩ | |
| rw [mem_erase] at hfr | |
| exact hfr.2 | |
| theorem hall_cond_of_restrict {ι : Type u} {t : ι → Finset α} {s : Finset ι} | |
| (ht : ∀ s : Finset ι, #s ≤ #(s.biUnion t)) (s' : Finset (s : Set ι)) : | |
| #s' ≤ #(s'.biUnion fun a' => t a') := by | |
| classical | |
| rw [← card_image_of_injective s' Subtype.coe_injective] | |
| convert! ht (s'.image fun z => z.1) using 1 | |
| apply congr_arg | |
| ext y | |
| simp | |
| theorem hall_cond_of_compl {ι : Type u} {t : ι → Finset α} {s : Finset ι} | |
| (hus : #s = #(s.biUnion t)) (ht : ∀ s : Finset ι, #s ≤ #(s.biUnion t)) | |
| (s' : Finset (sᶜ : Set ι)) : #s' ≤ #(s'.biUnion fun x' => t x' \ s.biUnion t) := by | |
| have := Classical.decEq ι | |
| have disj : Disjoint s (s'.image fun z => z.1) := by | |
| simp only [disjoint_left, not_exists, mem_image, SetCoe.exists, exists_and_right, | |
| exists_eq_right] | |
| intro x hx hc _ | |
| exact absurd hx hc | |
| have : #s' = #(s ∪ s'.image fun z => z.1) - #s := by | |
| simp [disj, card_image_of_injective _ Subtype.coe_injective, Nat.add_sub_cancel_left] | |
| rw [this, hus] | |
| refine (Nat.sub_le_sub_right (ht _) _).trans ?_ | |
| rw [← card_sdiff_of_subset] | |
| · gcongr | |
| intro t | |
| simp only [mem_biUnion, mem_sdiff, not_exists, mem_image, and_imp, mem_union, | |
| exists_imp] | |
| rintro x (hx | ⟨x', hx', rfl⟩) rat hs | |
| · exact False.elim <| (hs x) <| And.intro hx rat | |
| · use x', hx', rat, hs | |
| · apply biUnion_subset_biUnion_of_subset_left | |
| apply subset_union_left | |
| theorem hall_hard_inductive_step_B {n : ℕ} (hn : Fintype.card ι = n + 1) | |
| (ht : ∀ s : Finset ι, #s ≤ #(s.biUnion t)) | |
| (ih : | |
| ∀ {ι' : Type u} [Fintype ι'] (t' : ι' → Finset α), | |
| Fintype.card ι' ≤ n → | |
| (∀ s' : Finset ι', #s' ≤ #(s'.biUnion t')) → | |
| ∃ f : ι' → α, Function.Injective f ∧ ∀ x, f x ∈ t' x) | |
| (s : Finset ι) (hs : s.Nonempty) (hns : s ≠ univ) (hus : #s = #(s.biUnion t)) : | |
| ∃ f : ι → α, Function.Injective f ∧ ∀ x, f x ∈ t x := by | |
| have := Classical.decEq ι | |
| rw [Nat.add_one] at hn | |
| have card_ι'_le : Fintype.card s ≤ n := by | |
| apply Nat.le_of_lt_succ | |
| calc | |
| Fintype.card s = #s := Fintype.card_coe _ | |
| _ < Fintype.card ι := (card_lt_iff_ne_univ _).mpr hns | |
| _ = n.succ := hn | |
| let t' : s → Finset α := fun x' => t x' | |
| rcases ih t' card_ι'_le (hall_cond_of_restrict ht) with ⟨f', hf', hsf'⟩ | |
| set ι'' := (s : Set ι)ᶜ | |
| let t'' : ι'' → Finset α := fun a'' => t a'' \ s.biUnion t | |
| have card_ι''_le : Fintype.card ι'' ≤ n := by | |
| simp_rw [ι'', ← Nat.lt_succ_iff, ← hn, ← Finset.coe_compl, coe_sort_coe] | |
| rwa [Fintype.card_coe, card_compl_lt_iff_nonempty] | |
| rcases ih t'' card_ι''_le (hall_cond_of_compl hus ht) with ⟨f'', hf'', hsf''⟩ | |
| have f''_notMem_biUnion : ∀ (x'') (hx'' : x'' ∉ s), f'' ⟨x'', hx''⟩ ∉ s.biUnion t := by | |
| intro x'' hx'' | |
| have h := hsf'' ⟨x'', hx''⟩ | |
| rw [mem_sdiff] at h | |
| exact h.2 | |
| have im_disj : | |
| ∀ (x' x'' : ι) (hx' : x' ∈ s) (hx'' : x'' ∉ s), f' ⟨x', hx'⟩ ≠ f'' ⟨x'', hx''⟩ := by | |
| grind | |
| refine ⟨fun x => if h : x ∈ s then f' ⟨x, h⟩ else f'' ⟨x, h⟩, ?_, ?_⟩ | |
| · refine hf'.dite _ hf'' (@fun x x' => im_disj x x' _ _) | |
| · intro x | |
| simp only | |
| split_ifs with h | |
| · exact hsf' ⟨x, h⟩ | |
| · exact sdiff_subset (hsf'' ⟨x, h⟩) | |
| end Fintype | |
| variable [Finite ι] | |
| theorem hall_hard_inductive (ht : ∀ s : Finset ι, #s ≤ #(s.biUnion t)) : | |
| ∃ f : ι → α, Function.Injective f ∧ ∀ x, f x ∈ t x := by | |
| cases nonempty_fintype ι | |
| generalize hn : Fintype.card ι = m | |
| induction m using Nat.strongRecOn generalizing ι with | ind n ih => _ | |
| rcases n with (_ | n) | |
| · rw [Fintype.card_eq_zero_iff] at hn | |
| exact ⟨isEmptyElim, isEmptyElim, isEmptyElim⟩ | |
| · have ih' : ∀ (ι' : Type u) [Fintype ι'] (t' : ι' → Finset α), Fintype.card ι' ≤ n → | |
| (∀ s' : Finset ι', #s' ≤ #(s'.biUnion t')) → | |
| ∃ f : ι' → α, Function.Injective f ∧ ∀ x, f x ∈ t' x := by | |
| intro ι' _ _ hι' ht' | |
| exact ih _ (Nat.lt_succ_of_le hι') ht' _ rfl | |
| by_cases! h : ∀ s : Finset ι, s.Nonempty → s ≠ univ → #s < #(s.biUnion t) | |
| · refine hall_hard_inductive_step_A hn ht (@fun ι' => ih' ι') h | |
| · rcases h with ⟨s, sne, snu, sle⟩ | |
| exact hall_hard_inductive_step_B hn ht (@fun ι' => ih' ι') | |
| s sne snu (Nat.le_antisymm (ht _) sle) | |
| end HallMarriageTheorem | |
| theorem all_card_le_biUnion_card_iff_existsInjective {ι α : Type*} [Finite ι] | |
| [DecidableEq α] (t : ι → Finset α) : | |
| (∀ s : Finset ι, #s ≤ #(s.biUnion t)) ↔ | |
| ∃ f : ι → α, Function.Injective f ∧ ∀ x, f x ∈ t x := by | |
| constructor | |
| · exact HallMarriageTheorem.hall_hard_inductive | |
| · rintro ⟨f, hf₁, hf₂⟩ s | |
| rw [← card_image_of_injective s hf₁] | |
| apply card_le_card | |
| intro | |
| rw [mem_image, mem_biUnion] | |
| rintro ⟨x, hx, rfl⟩ | |
| exact ⟨x, hx, hf₂ x⟩ | |
| end BinPackingGap.FiniteHall | |
| end OAI | |