Skip to content

Commit 3c16859

Browse files
committed
chore: proving lemmas on associative lists
1 parent 82a00c6 commit 3c16859

File tree

2 files changed

+163
-9
lines changed

2 files changed

+163
-9
lines changed

src/Std/Data/DHashMap/Internal/RawLemmas.lean

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2704,7 +2704,12 @@ theorem contains_union_of_left [EquivBEq α] [LawfulHashable α] (h₁ : m₁.v
27042704
theorem contains_union_of_right [EquivBEq α] [LawfulHashable α] (h₁ : m₁.val.WF)
27052705
(h₂ : m₂.val.WF) {k : α} :
27062706
m₂.contains k → (m₁.union m₂).contains k := by
2707-
sorry
2707+
simp_to_model [contains, union]
2708+
simp only [List.containsKey_insertSmallerList]
2709+
intro h
2710+
simp only [Bool.or_eq_true]
2711+
apply Or.inr
2712+
exact h
27082713

27092714
@[simp]
27102715
theorem contains_union [EquivBEq α] [LawfulHashable α] (h₁ : m₁.val.WF)
@@ -2716,22 +2721,40 @@ theorem contains_union [EquivBEq α] [LawfulHashable α] (h₁ : m₁.val.WF)
27162721
theorem contains_union_iff [EquivBEq α] [LawfulHashable α] (h₁ : m₁.val.WF)
27172722
(h₂ : m₂.val.WF) {k : α} :
27182723
(m₁.union m₂).contains k ↔ m₁.contains k ∨ m₂.contains k := by
2719-
sorry
2724+
apply Iff.trans
2725+
rotate_left
2726+
. apply iff_of_eq
2727+
apply Bool.or_eq_true
2728+
. rw [Bool.coe_iff_coe]
2729+
apply contains_union h₁ h₂
27202730

27212731
theorem contains_of_contains_union_of_contains_right_eq_false [EquivBEq α]
27222732
[LawfulHashable α] (h₁ : m₁.val.WF) (h₂ : m₂.val.WF) {k : α} :
27232733
(m₁.union m₂).contains k → m₂.contains k = false → m₁.contains k := by
2724-
sorry
2734+
intro h₃ h₄
2735+
apply Or.elim <| (@contains_union_iff α β _ _ _ _ _ _ h₁ h₂ k).1 h₃
2736+
. simp only [imp_self]
2737+
. intro n
2738+
rw [n] at h₄
2739+
contradiction
27252740

27262741
theorem contains_of_contains_union_of_contains_left_eq_false [EquivBEq α]
27272742
[LawfulHashable α] (h₁ : m₁.val.WF) (h₂ : m₂.val.WF) {k : α} :
27282743
(m₁.union m₂).contains k → m₁.contains k = false → m₂.contains k := by
2729-
sorry
2744+
intro h₃ h₄
2745+
apply Or.elim <| (@contains_union_iff α β _ _ _ _ _ _ h₁ h₂ k).1 h₃
2746+
. intro n
2747+
rw [n] at h₄
2748+
contradiction
2749+
. simp only [imp_self]
27302750

27312751
theorem get?_union_of_contains_right_eq_false [LawfulBEq α] (h₁ : m₁.val.WF) (h₂ : m₂.val.WF)
27322752
{k : α} (contains_eq_false : m₂.contains k = false) :
27332753
(m₁.union m₂).get? k = m₁.get? k := by
2734-
sorry
2754+
revert contains_eq_false
2755+
simp_to_model [union, get?, contains]
2756+
intro contains_eq_false
2757+
27352758

27362759
theorem get_union_of_contains_right_eq_false [LawfulBEq α] (h₁ : m₁.val.WF) (h₂ : m₂.val.WF)
27372760
{k : α} (contains_eq_false : m₂.contains k = false) {h'} :
@@ -2823,21 +2846,21 @@ theorem size_union [EquivBEq α] [LawfulHashable α] (h₁ : m₁.val.WF)
28232846

28242847
theorem size_left_le_size_union [EquivBEq α] [LawfulHashable α] (h₁ : m₁.val.WF)
28252848
(h₂ : m₂.val.WF) : m₁.1.size ≤ (m₁.union m₂).1.size := by
2826-
sorry
2849+
simp_to_model [union, size] using length_left_le_length_insertSmallerList
28272850

28282851
theorem size_right_le_size_union [EquivBEq α] [LawfulHashable α] (h₁ : m₁.val.WF)
28292852
(h₂ : m₂.val.WF) : m₂.1.size ≤ (m₁.union m₂).1.size := by
2830-
sorry
2853+
simp_to_model [union, size] using length_right_le_length_insertSmallerList
28312854

28322855
theorem size_union_le_size_add_size [EquivBEq α] [LawfulHashable α]
28332856
(h₁ : m₁.val.WF) (h₂ : m₂.val.WF) :
28342857
(m₁.union m₂).1.size ≤ m₁.1.size + m₂.1.size := by
2835-
sorry
2858+
simp_to_model [union, size] using length_insertSmallerList_le
28362859

28372860
@[simp]
28382861
theorem isEmpty_union [EquivBEq α] [LawfulHashable α] (h₁ : m₁.val.WF) (h₂ : m₂.val.WF) :
28392862
(m₁.union m₂).1.isEmpty = (m₁.1.isEmpty && m₂.1.isEmpty) := by
2840-
sorry
2863+
simp_to_model [isEmpty, union] using isEmpty_insertSmallerList
28412864

28422865
end Union
28432866

src/Std/Data/Internal/List/Associative.lean

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3056,6 +3056,78 @@ theorem containsKey_insertSmallerList [BEq α] [PartialEquivBEq α] {l₁ l₂ :
30563056
· rw [containsKey_insertListIfNew, ← containsKey_eq_contains_map_fst, Bool.or_comm]
30573057
· rw [containsKey_insertList, ← containsKey_eq_contains_map_fst]
30583058

3059+
theorem isEmpty_insertListIfNew [BEq α]
3060+
{l toInsert : List ((a : α) × β a)} :
3061+
(List.insertListIfNew l toInsert).isEmpty = (l.isEmpty && toInsert.isEmpty) := by
3062+
induction toInsert generalizing l with
3063+
| nil => simp [insertListIfNew]
3064+
| cons hd tl ih =>
3065+
rw [insertListIfNew, List.isEmpty_cons, ih, isEmpty_insertEntryIfNew]
3066+
simp
3067+
3068+
theorem isEmpty_insertSmallerList [BEq α]
3069+
{l toInsert : List ((a : α) × β a)} :
3070+
(List.insertSmallerList l toInsert).isEmpty = (l.isEmpty && toInsert.isEmpty) := by
3071+
rw [insertSmallerList]
3072+
split
3073+
case isTrue =>
3074+
simp only [isEmpty_insertListIfNew, Bool.and_comm]
3075+
case isFalse =>
3076+
simp only [isEmpty_insertList]
3077+
3078+
theorem length_insertListIfNew_le [BEq α]
3079+
{l toInsert : List ((a : α) × β a)} :
3080+
(List.insertListIfNew l toInsert).length ≤ l.length + toInsert.length := by
3081+
induction toInsert generalizing l with
3082+
| nil => simp only [List.insertListIfNew, List.length_nil, Nat.add_zero, Nat.le_refl]
3083+
| cons hd tl ih =>
3084+
simp only [insertListIfNew, List.length_cons]
3085+
apply Nat.le_trans ih
3086+
rw [Nat.add_comm tl.length 1, ← Nat.add_assoc]
3087+
apply Nat.add_le_add _ (Nat.le_refl _)
3088+
apply length_insertEntryIfNew_le
3089+
3090+
theorem length_insertSmallerList_le [BEq α]
3091+
{l toInsert : List ((a : α) × β a)} :
3092+
(List.insertSmallerList l toInsert).length ≤ l.length + toInsert.length := by
3093+
unfold insertSmallerList
3094+
split
3095+
case isTrue =>
3096+
apply Nat.le_trans
3097+
. apply length_insertListIfNew_le
3098+
. simp only [Nat.add_comm, Nat.le_refl]
3099+
case isFalse => simp only [length_insertList_le]
3100+
3101+
theorem length_left_le_length_insertListIfNew [BEq α] [EquivBEq α]
3102+
{l toInsert : List ((a : α) × β a)} :
3103+
l.length ≤ (insertListIfNew l toInsert).length := by
3104+
induction toInsert generalizing l with
3105+
| nil => apply Nat.le_refl
3106+
| cons hd tl ih => exact Nat.le_trans length_le_length_insertEntryIfNew ih
3107+
3108+
theorem length_left_le_length_insertSmallerList [BEq α] [EquivBEq α]
3109+
{l toInsert : List ((a : α) × β a)} :
3110+
l.length ≤ (List.insertSmallerList l toInsert).length := by
3111+
unfold insertSmallerList
3112+
split
3113+
case isTrue h =>
3114+
apply Nat.le_trans
3115+
. exact h
3116+
. apply length_left_le_length_insertListIfNew
3117+
case isFalse => simp only [length_le_length_insertList]
3118+
3119+
theorem length_right_le_length_insertSmallerList [BEq α] [EquivBEq α]
3120+
{l toInsert : List ((a : α) × β a)} :
3121+
toInsert.length ≤ (List.insertSmallerList l toInsert).length := by
3122+
unfold insertSmallerList
3123+
split
3124+
case isTrue => apply length_left_le_length_insertListIfNew
3125+
case isFalse h =>
3126+
simp only [Nat.not_le] at h
3127+
apply Nat.le_trans
3128+
. apply le_of_lt h
3129+
. simp only [length_le_length_insertList]
3130+
30593131
section
30603132

30613133
variable {β : Type v}
@@ -3206,6 +3278,65 @@ theorem getValue?_insertListConst_of_contains_eq_false [BEq α] [PartialEquivBEq
32063278
rw [containsKey_eq_contains_map_fst]
32073279
simpa using not_contains
32083280

3281+
theorem getValue?_insertList_of_contains_eq_false [BEq α] [PartialEquivBEq α]
3282+
{l toInsert : List ((_ : α) × β)} {k : α}
3283+
(not_contains : (toInsert.map Sigma.fst).contains k = false) :
3284+
getValue? k (insertList l toInsert) = getValue? k l := by
3285+
rw [getValue?_eq_getEntry?, getValue?_eq_getEntry?, getEntry?_insertList_of_contains_eq_false]
3286+
rw [containsKey_eq_contains_map_fst]
3287+
simpa using not_contains
3288+
3289+
theorem getEntry?_insertListIfNew_of_contains_eq_false_right [BEq α] [PartialEquivBEq α]
3290+
{l toInsert : List ((_ : α) × β)} {k : α}
3291+
(not_contains : containsKey k l = false) :
3292+
getEntry? k (insertListIfNew l toInsert) = getEntry? k toInsert := by
3293+
induction toInsert generalizing l with
3294+
| nil => simpa [insertListIfNew]
3295+
| cons h t ih =>
3296+
unfold insertListIfNew
3297+
sorry
3298+
-- rw [containsKey_cons_eq_false] at not_contains
3299+
-- rw [ih not_contains.right, getEntry?_insertEntryIfNew]
3300+
-- simp [not_contains]
3301+
3302+
theorem getEntry?_insertListIfNew_of_contains_eq_false [BEq α] [PartialEquivBEq α]
3303+
{l toInsert : List ((_ : α) × β)} {k : α}
3304+
(not_contains : containsKey k toInsert = false) :
3305+
getEntry? k (insertListIfNew l toInsert) = getEntry? k l := by
3306+
induction toInsert generalizing l with
3307+
| nil => simp [insertListIfNew]
3308+
| cons h t ih =>
3309+
unfold insertListIfNew
3310+
rw [containsKey_cons_eq_false] at not_contains
3311+
rw [ih not_contains.right, getEntry?_insertEntryIfNew]
3312+
simp [not_contains]
3313+
3314+
theorem getValue?_insertListIfNew_of_contains_eq_false [BEq α] [PartialEquivBEq α]
3315+
{l toInsert : List ((_ : α) × β)} {k : α}
3316+
(not_contains : (toInsert.map Sigma.fst).contains k = false) :
3317+
getValue? k (insertListIfNew toInsert l) = getValue? k l := by
3318+
rw [getValue?_eq_getEntry?]
3319+
rw [getValue?_eq_getEntry?]
3320+
rw [getEntry?_insertListIfNew_of_contains_eq_false_right]
3321+
rw [containsKey_eq_contains_map_fst]
3322+
simpa using not_contains
3323+
3324+
theorem getValue?_insertSmallerList_of_contains_eq_false [BEq α] [PartialEquivBEq α] [LawfulBEq α]
3325+
{l toInsert : List ((a : α) × β)} {k : α}
3326+
(not_contains : containsKey k toInsert = false) :
3327+
getValueCast? k (insertSmallerList l toInsert) = getValueCast? k l := by
3328+
unfold insertSmallerList
3329+
split
3330+
case isTrue =>
3331+
simp only [containsKey_eq_contains_map_fst] at not_contains
3332+
simp only [←getValue?_eq_getValueCast?]
3333+
simp only [getValue?_insertListIfNew_of_contains_eq_false not_contains]
3334+
case isFalse =>
3335+
simp only [containsKey_eq_contains_map_fst] at not_contains
3336+
simp only [←getValue?_eq_getValueCast?]
3337+
simp [getValue?_insertList_of_contains_eq_false not_contains]
3338+
3339+
32093340
theorem getValue?_insertListConst_of_mem [BEq α] [EquivBEq α]
32103341
{l : List ((_ : α) × β)} {toInsert : List (α × β)}
32113342
(distinct_l : DistinctKeys l)

0 commit comments

Comments
 (0)