Skip to content

Commit 6a93f55

Browse files
committed
Move up to Raw and free lemmas
1 parent 55d2f7d commit 6a93f55

File tree

5 files changed

+149
-33
lines changed

5 files changed

+149
-33
lines changed

src/Std/Data/DHashMap/Basic.lean

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,8 @@ section Unverified
366366

367367
@[inline, inherit_doc Raw.partition] def partition (f : (a : α) → β a → Bool)
368368
(m : DHashMap α β) : DHashMap α β × DHashMap α β :=
369-
⟨⟨(m.1.partition f).1, Raw.WF.fst_partition m.2⟩, ⟨(m.1.partition f).2, Raw.WF.snd_partition m.2⟩⟩
369+
⟨⟨(Raw₀.partition f ⟨m.1, m.2.size_buckets_pos⟩).1, Raw.WF.fst_partition₀⟩,
370+
⟨(Raw₀.partition f ⟨m.1, m.2.size_buckets_pos⟩).2, Raw.WF.snd_partition₀⟩⟩
370371

371372
@[inline, inherit_doc Raw.values] def values {β : Type v}
372373
(m : DHashMap α (fun _ => β)) : List β :=

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

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,20 @@ theorem distinct_keys_toList [EquivBEq α] [LawfulHashable α] (h : m.1.WF) :
10321032
m.1.toList.Pairwise (fun a b => (a.1 == b.1) = false) := by
10331033
simp_to_model [toList] using List.pairwise_fst_eq_false
10341034

1035+
theorem nodup_toList [EquivBEq α] [LawfulHashable α] (h : m.1.WF) :
1036+
m.1.toList.Nodup := by
1037+
simp_to_model [toList] using List.nodup_of_distinctKeys
1038+
1039+
theorem mem_toList_insert_of_contains_eq_false [EquivBEq α] [LawfulHashable α] (h : m.1.WF)
1040+
{k : α} {v : β k} {x : (a : α) × β a} (h' : m.contains k = false) :
1041+
x ∈ (m.insert k v).1.toList ↔ x = ⟨k, v⟩ ∨ x ∈ m.1.toList := by
1042+
rw [contains_eq_containsₘ] at h'
1043+
simp [Raw.toList, insert_eq_insertₘ, insertₘ, h', Bool.false_eq_true, ↓reduceIte,
1044+
Raw.foldRev_cons, List.append_nil]
1045+
rw [List.Perm.mem_iff (toListModel_expandIfNecessary (consₘ m k v))]
1046+
rw [List.Perm.mem_iff (toListModel_consₘ m (Raw.WF.out h) k v)]
1047+
simp
1048+
10351049
namespace Const
10361050

10371051
variable {β : Type v} (m : Raw₀ α (fun _ => β))
@@ -1081,6 +1095,28 @@ theorem distinct_keys_toList [EquivBEq α] [LawfulHashable α] (h : m.1.WF) :
10811095
(Raw.Const.toList m.1).Pairwise (fun a b => (a.1 == b.1) = false) := by
10821096
simp_to_model [Const.toList] using List.pairwise_fst_eq_false_map_toProd
10831097

1098+
theorem nodup_toList [EquivBEq α] [LawfulHashable α] (h : m.1.WF) :
1099+
(Raw.Const.toList m.1).Nodup := by
1100+
simp_to_model [Const.toList] using List.nodup_map_of_distinctKeys
1101+
1102+
theorem mem_toList_insert_of_contains_eq_false [EquivBEq α] [LawfulHashable α] (h : m.1.WF)
1103+
{k : α} {v : β} {x : α × β} (h' : m.contains k = false) :
1104+
x ∈ (Raw.Const.toList (m.insert k v).1) ↔ x = ⟨k, v⟩ ∨ x ∈ Raw.Const.toList m.1 := by
1105+
rw [contains_eq_containsₘ] at h'
1106+
simp only [Raw.Const.toList, insert_eq_insertₘ, insertₘ, h', Bool.false_eq_true, ↓reduceIte,
1107+
Raw.foldRev_cons_mk, List.append_nil]
1108+
rw [← List.mem_map_toProd_iff_mem, ← List.mem_map_toProd_iff_mem]
1109+
rw [List.Perm.mem_iff (toListModel_expandIfNecessary (consₘ m k v))]
1110+
rw [List.Perm.mem_iff (toListModel_consₘ m (Raw.WF.out h) k v)]
1111+
simp only [List.mem_cons, Sigma.mk.injEq, heq_eq_eq]
1112+
have : x.fst = k ∧ x.snd = v ↔ x = (k,v) := by
1113+
constructor
1114+
· intro h
1115+
simp [← h.1, ← h.2]
1116+
· intro h
1117+
simp [h]
1118+
rw [this]
1119+
10841120
end Const
10851121

10861122
omit [Hashable α] [BEq α] in
@@ -5184,16 +5220,6 @@ theorem snd_partition_not_eq_fst_partition [EquivBEq α] [LawfulHashable α]
51845220
rw [← fst_partition_not_eq_snd_partition]
51855221
simp
51865222

5187-
private theorem mem_toList_insert_of_contains_eq_false [EquivBEq α] [LawfulHashable α] (h : m.1.WF)
5188-
{k : α} {v : β k} {x : (a : α) × β a} (h' : m.contains k = false) :
5189-
x ∈ (m.insert k v).1.toList ↔ x ∈ m.1.toList ∨ x = ⟨k, v⟩ := by
5190-
rw [contains_eq_containsₘ] at h'
5191-
simp only [Raw.toList, insert_eq_insertₘ, insertₘ, h', Bool.false_eq_true, ↓reduceIte,
5192-
Raw.foldRev_cons, List.append_nil]
5193-
rw [List.Perm.mem_iff (toListModel_expandIfNecessary (consₘ m k v))]
5194-
rw [List.Perm.mem_iff (toListModel_consₘ m (Raw.WF.out h) k v)]
5195-
simp [Or.comm]
5196-
51975223
private theorem mem_toList_fst_partition [EquivBEq α] [LawfulHashable α] (h : m.1.WF)
51985224
{p : (a : α) → β a → Bool} (x : (a : α) × β a) :
51995225
x ∈ (m.partition p).1.1.toList ↔ x ∈ m.1.toList ∧ p x.1 x.2 = true := by
@@ -5228,10 +5254,10 @@ private theorem mem_toList_fst_partition [EquivBEq α] [LawfulHashable α] (h :
52285254
| inr h =>
52295255
cases h with
52305256
| inl h =>
5231-
apply Or.inr h
5232-
| inr h =>
52335257
rw [h]
52345258
simp [hhd]
5259+
| inr h =>
5260+
apply Or.inr h
52355261
· intro h
52365262
cases h with
52375263
| inl h =>
@@ -5278,23 +5304,6 @@ private theorem mem_toList_fst_partition [EquivBEq α] [LawfulHashable α] (h :
52785304
· simp only [List.pairwise_cons] at h₄
52795305
apply h₄.2
52805306

5281-
private theorem nodup_toList [EquivBEq α] [LawfulHashable α] (h : m.1.WF) : m.1.toList.Nodup := by
5282-
simp only [List.Nodup, ne_eq]
5283-
suffices ∀ (l : List ((a :α) × β a)) (h₁ : List.Pairwise (fun a b => (a.1 == b.1) = false) l),
5284-
List.Pairwise (fun a b => a ≠ b) l from this m.1.toList (distinct_keys_toList _ h)
5285-
intro l
5286-
induction l with
5287-
| nil => simp
5288-
| cons hd tl ih =>
5289-
simp
5290-
intro h₁ h₂
5291-
refine And.intro ?_ (ih h₂)
5292-
intro a ha
5293-
false_or_by_contra
5294-
rename_i h'
5295-
specialize h₁ a ha
5296-
simp [h'] at h₁
5297-
52985307
theorem fst_partition_equiv_filter [EquivBEq α] [LawfulHashable α]
52995308
{p : (a : α) → β a → Bool} (h : m.1.WF) :
53005309
(m.partition p).1.1.Equiv (m.filter p).1 := by

src/Std/Data/DHashMap/Lemmas.lean

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,17 @@ theorem distinct_keys_toList [EquivBEq α] [LawfulHashable α] :
12471247
m.toList.Pairwise (fun a b => (a.1 == b.1) = false) :=
12481248
Raw₀.distinct_keys_toList ⟨m.1, m.2.size_buckets_pos⟩ m.2
12491249

1250+
theorem nodup_toList [EquivBEq α] [LawfulHashable α] :
1251+
m.toList.Nodup :=
1252+
Raw₀.nodup_toList ⟨m.1, m.2.size_buckets_pos⟩ m.2
1253+
1254+
theorem mem_toList_insert_of_not_mem [EquivBEq α] [LawfulHashable α]
1255+
{k : α} {v : β k} {x : (a : α) × (β a)} (h' : ¬ k ∈ m) :
1256+
x ∈ (m.insert k v).toList ↔ x = ⟨k, v⟩ ∨ x ∈ m.toList := by
1257+
apply Raw₀.mem_toList_insert_of_contains_eq_false ⟨m.1, m.2.size_buckets_pos⟩ m.2
1258+
rw [mem_iff_contains, Bool.not_eq_true] at h'
1259+
apply h'
1260+
12501261
namespace Const
12511262

12521263
variable {β : Type v} {m : DHashMap α (fun _ => β)}
@@ -1306,6 +1317,17 @@ theorem distinct_keys_toList [EquivBEq α] [LawfulHashable α] :
13061317
(toList m).Pairwise (fun a b => (a.1 == b.1) = false) :=
13071318
Raw₀.Const.distinct_keys_toList ⟨m.1, m.2.size_buckets_pos⟩ m.2
13081319

1320+
theorem nodup_toList [EquivBEq α] [LawfulHashable α] :
1321+
(Const.toList m).Nodup :=
1322+
Raw₀.Const.nodup_toList ⟨m.1, m.2.size_buckets_pos⟩ m.2
1323+
1324+
theorem mem_toList_insert_of_not_mem [EquivBEq α] [LawfulHashable α]
1325+
{k : α} {v : β} {x : α × β} (h' : ¬ k ∈ m) :
1326+
x ∈ (Const.toList (m.insert k v)) ↔ x = ⟨k, v⟩ ∨ x ∈ Const.toList m := by
1327+
apply Raw₀.Const.mem_toList_insert_of_contains_eq_false ⟨m.1, m.2.size_buckets_pos⟩ m.2
1328+
rw [mem_iff_contains, Bool.not_eq_true] at h'
1329+
apply h'
1330+
13091331
end Const
13101332

13111333
@[simp]
@@ -5312,5 +5334,34 @@ theorem toList_map {m : DHashMap α fun _ => β}
53125334
end Const
53135335

53145336
end map
5337+
5338+
@[simp]
5339+
theorem size_fst_partition_add_size_snd_partition_eq_size [EquivBEq α] [LawfulHashable α]
5340+
{f : (a : α) → β a → Bool} :
5341+
(m.partition f).1.size + (m.partition f).2.size = m.size :=
5342+
Raw₀.size_partition ⟨m.1, m.2.size_buckets_pos⟩ m.2
5343+
5344+
@[simp]
5345+
theorem fst_partition_not_eq_snd_partition [EquivBEq α] [LawfulHashable α]
5346+
{f : (a : α) → β a → Bool} :
5347+
(m.partition (fun a b => ! f a b)).fst = (m.partition f).snd := by
5348+
simp [partition, Raw₀.fst_partition_not_eq_snd_partition ⟨m.1, m.2.size_buckets_pos⟩ (p:= f)]
5349+
5350+
@[simp]
5351+
theorem snd_partition_not_eq_fst_partition [EquivBEq α] [LawfulHashable α]
5352+
{f : (a : α) → β a → Bool} :
5353+
(m.partition (fun a b => ! f a b)).snd = (m.partition f).fst := by
5354+
simp [partition, Raw₀.snd_partition_not_eq_fst_partition ⟨m.1, m.2.size_buckets_pos⟩ (p:= f)]
5355+
5356+
theorem fst_partition_equiv_filter [EquivBEq α] [LawfulHashable α]
5357+
{f : (a : α) → β a → Bool} :
5358+
(m.partition f).fst ~m m.filter f :=
5359+
⟨Raw₀.fst_partition_equiv_filter ⟨m.1, m.2.size_buckets_pos⟩ m.2
5360+
5361+
theorem snd_partition_equiv_filter_not [EquivBEq α] [LawfulHashable α]
5362+
{f : (a : α) → β a → Bool} :
5363+
(m.partition f).snd ~m m.filter (fun a b => ! f a b) :=
5364+
⟨Raw₀.snd_partition_equiv_filter_not ⟨m.1, m.2.size_buckets_pos⟩ m.2
5365+
53155366
attribute [simp] contains_eq_false_iff_not_mem
53165367
end Std.DHashMap

src/Std/Data/DHashMap/RawLemmas.lean

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,6 +1318,21 @@ theorem distinct_keys_toList [EquivBEq α] [LawfulHashable α] (h : m.WF) :
13181318
m.toList.Pairwise (fun a b => (a.1 == b.1) = false) := by
13191319
apply Raw₀.distinct_keys_toList ⟨m, h.size_buckets_pos⟩ h
13201320

1321+
theorem nodup_toList [EquivBEq α] [LawfulHashable α] (h : m.WF) :
1322+
m.toList.Nodup := by
1323+
apply Raw₀.nodup_toList ⟨m, h.size_buckets_pos⟩ h
1324+
1325+
theorem mem_toList_insert_of_contains_eq_false [EquivBEq α] [LawfulHashable α] (h : m.WF)
1326+
{k : α} {v : β k} {x : (a : α) × β a} : m.contains k = false →
1327+
(x ∈ (m.insert k v).toList ↔ x = ⟨k, v⟩ ∨ x ∈ m.toList) := by
1328+
simp_to_raw using Raw₀.mem_toList_insert_of_contains_eq_false ⟨m, h.size_buckets_pos⟩ h
1329+
1330+
theorem mem_toList_insert_of_not_mem [EquivBEq α] [LawfulHashable α] (h : m.WF)
1331+
{k : α} {v : β k} {x : (a : α) × β a} : ¬ k ∈ m →
1332+
(x ∈ (m.insert k v).toList ↔ x = ⟨k, v⟩ ∨ x ∈ m.toList) := by
1333+
rw [mem_iff_contains, Bool.not_eq_true]
1334+
apply mem_toList_insert_of_contains_eq_false h
1335+
13211336
namespace Const
13221337

13231338
variable {β : Type v} {m : Raw α (fun _ => β)}
@@ -1380,6 +1395,21 @@ theorem distinct_keys_toList [EquivBEq α] [LawfulHashable α] (h : m.WF) :
13801395
(Raw.Const.toList m).Pairwise (fun a b => (a.1 == b.1) = false) := by
13811396
apply Raw₀.Const.distinct_keys_toList ⟨m, h.size_buckets_pos⟩ h
13821397

1398+
theorem nodup_toList [EquivBEq α] [LawfulHashable α] (h : m.WF) :
1399+
(Raw.Const.toList m).Nodup := by
1400+
apply Raw₀.Const.nodup_toList ⟨m, h.size_buckets_pos⟩ h
1401+
1402+
theorem mem_toList_insert_of_contains_eq_false [EquivBEq α] [LawfulHashable α] (h : m.WF)
1403+
{k : α} {v : β} {x : α × β} : m.contains k = false →
1404+
(x ∈ (Raw.Const.toList (m.insert k v)) ↔ x = ⟨k, v⟩ ∨ x ∈ Raw.Const.toList m) := by
1405+
simp_to_raw using Raw₀.Const.mem_toList_insert_of_contains_eq_false ⟨m, h.size_buckets_pos⟩ h
1406+
1407+
theorem mem_toList_insert_of_not_mem [EquivBEq α] [LawfulHashable α] (h : m.WF)
1408+
{k : α} {v : β} {x : α × β} : ¬ k ∈ m →
1409+
(x ∈ (Raw.Const.toList (m.insert k v)) ↔ x = ⟨k, v⟩ ∨ x ∈ Raw.Const.toList m) := by
1410+
rw [mem_iff_contains, Bool.not_eq_true]
1411+
apply mem_toList_insert_of_contains_eq_false h
1412+
13831413
end Const
13841414

13851415
@[simp]
@@ -5994,20 +6024,21 @@ end Const
59946024

59956025
end map
59966026

6027+
@[simp]
59976028
theorem size_fst_partition_add_size_snd_partition_eq_size [EquivBEq α] [LawfulHashable α]
59986029
{p : (a : α) → β a → Bool} (h : m.WF) :
59996030
(m.partition p).1.size + (m.partition p).2.size = m.size := by
60006031
simp_to_raw using Raw₀.size_partition
60016032

60026033
@[simp]
6003-
theorem fst_partition_not_eq_partition_snd [EquivBEq α] [LawfulHashable α]
6034+
theorem fst_partition_not_eq_snd_partition [EquivBEq α] [LawfulHashable α]
60046035
{p : (a : α) → β a → Bool} (h : m.WF) :
60056036
(m.partition (fun a b => ! p a b)).fst = (m.partition p).snd := by
60066037
simp_to_raw
60076038
rw [Raw₀.fst_partition_not_eq_snd_partition]
60086039

60096040
@[simp]
6010-
theorem snd_partition_not_eq_partition_fst [EquivBEq α] [LawfulHashable α]
6041+
theorem snd_partition_not_eq_fst_partition [EquivBEq α] [LawfulHashable α]
60116042
{p : (a : α) → β a → Bool} (h : m.WF) :
60126043
(m.partition (fun a b => ! p a b)).snd = (m.partition p).fst := by
60136044
simp_to_raw

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2585,6 +2585,18 @@ theorem pairwise_fst_eq_false [BEq α] {l : List ((a : α) × β a)} (h : Distin
25852585
rw [DistinctKeys.def] at h
25862586
assumption
25872587

2588+
theorem nodup_of_distinctKeys [BEq α] [ReflBEq α] {l : List ((a : α) × β a)} (h : DistinctKeys l) :
2589+
l.Nodup := by
2590+
rw [DistinctKeys.def] at h
2591+
induction l with
2592+
| nil => simp
2593+
| cons hd tl ih =>
2594+
simp only [pairwise_cons, List.nodup_cons] at h ⊢
2595+
refine And.intro ?_ (ih h.2)
2596+
intro h'
2597+
have := h.1 hd h'
2598+
simp at this
2599+
25882600
theorem map_fst_map_toProd_eq_keys {β : Type v} {l : List ((_ : α) × β)} :
25892601
List.map Prod.fst (List.map (fun x => (x.fst, x.snd)) l) = List.keys l := by
25902602
induction l with
@@ -2663,7 +2675,6 @@ theorem getValue?_eq_some_iff_exists_beq_and_mem_toList {β : Type v} [BEq α] [
26632675
exists ⟨k', v⟩
26642676
simp only [and_true, getEntry?_congr k_k', h']
26652677

2666-
26672678
theorem mem_map_toProd_iff_getKey?_eq_some_and_getValue?_eq_some [BEq α] [EquivBEq α]
26682679
{β : Type v} {k: α} {v : β} {l : List ((_ : α) × β)} (h : DistinctKeys l) :
26692680
(k, v) ∈ l.map (fun x => (x.fst, x.snd)) ↔ getKey? k l = some k ∧ getValue? k l = some v := by
@@ -2677,6 +2688,19 @@ theorem pairwise_fst_eq_false_map_toProd [BEq α] {β : Type v}
26772688
simp [List.pairwise_map]
26782689
assumption
26792690

2691+
theorem nodup_map_of_distinctKeys [BEq α] [ReflBEq α] {β : Type v} {l : List ((_ : α) × β)} (h : DistinctKeys l) :
2692+
(l.map (fun x => (x.fst, x.snd))).Nodup := by
2693+
rw [DistinctKeys.def] at h
2694+
induction l with
2695+
| nil => simp
2696+
| cons hd tl ih =>
2697+
simp only [pairwise_cons, List.map_cons, List.nodup_cons, List.mem_map, Prod.mk.injEq,
2698+
not_exists, not_and] at h ⊢
2699+
refine And.intro ?_ (ih h.2)
2700+
intro x h' hx₁ _
2701+
have := h.1 x h'
2702+
simp [hx₁] at this
2703+
26802704
theorem foldlM_eq_foldlM_toProd {β : Type v} {δ : Type w} {m' : Type w → Type w'} [Monad m']
26812705
[LawfulMonad m'] {l : List ((_ : α) × β)} {f : δ → (a : α) → β → m' δ} {init : δ} :
26822706
l.foldlM (fun a b => f a b.fst b.snd) init =

0 commit comments

Comments
 (0)