Skip to content

Commit

Permalink
chore(data/multiset/basic): consistently use singleton notation (#8786)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben-VandeVelde committed Aug 21, 2021
1 parent 252cb02 commit d3e20b4
Show file tree
Hide file tree
Showing 29 changed files with 172 additions and 151 deletions.
16 changes: 8 additions & 8 deletions src/algebra/big_operators/basic.lean
Expand Up @@ -98,8 +98,8 @@ theorem prod_eq_fold [comm_monoid β] (s : finset α) (f : α → β) :
rfl

@[simp] lemma sum_multiset_singleton (s : finset α) :
s.sum (λ x, x ::ₘ 0) = s.val :=
by simp [sum_eq_multiset_sum]
s.sum (λ x, {x}) = s.val :=
by simp only [sum_eq_multiset_sum, multiset.sum_map_singleton]

end finset

Expand Down Expand Up @@ -1408,12 +1408,12 @@ lemma count_sum' {s : finset β} {a : α} {f : β → multiset α} :
by { dunfold finset.sum, rw count_sum }

@[simp] lemma to_finset_sum_count_nsmul_eq (s : multiset α) :
(∑ a in s.to_finset, s.count a • (a ::ₘ 0)) = s :=
(∑ a in s.to_finset, s.count a • {a}) = s :=
begin
apply ext', intro b,
rw count_sum',
have h : count b s = count b (count b s • (b ::ₘ 0)),
{ rw [singleton_coe, count_nsmul, ← singleton_coe, count_singleton, mul_one] },
have h : count b s = count b (count b s • {b}),
{ rw [count_nsmul, count_singleton_self, mul_one] },
rw h, clear h,
apply finset.sum_eq_single b,
{ intros c h hcb, rw count_nsmul, convert mul_zero (count c s),
Expand All @@ -1425,9 +1425,9 @@ theorem exists_smul_of_dvd_count (s : multiset α) {k : ℕ}
(h : ∀ (a : α), a ∈ s → k ∣ multiset.count a s) :
∃ (u : multiset α), s = k • u :=
begin
use ∑ a in s.to_finset, (s.count a / k) • (a ::ₘ 0),
have h₂ : ∑ (x : α) in s.to_finset, k • (count x s / k) • (x ::ₘ 0) =
∑ (x : α) in s.to_finset, count x s • (x ::ₘ 0),
use ∑ a in s.to_finset, (s.count a / k) • {a},
have h₂ : ∑ (x : α) in s.to_finset, k • (count x s / k) • ({x} : multiset α) =
∑ (x : α) in s.to_finset, count x s • {x},
{ apply finset.sum_congr rfl,
intros x hx,
rw [← mul_nsmul, nat.mul_div_cancel' (h x (mem_to_finset.mp hx))] },
Expand Down
6 changes: 4 additions & 2 deletions src/algebra/gcd_monoid/multiset.lean
Expand Up @@ -39,7 +39,8 @@ fold_zero _ _
(a ::ₘ s).lcm = gcd_monoid.lcm a s.lcm :=
fold_cons_left _ _ _ _

@[simp] lemma lcm_singleton {a : α} : (a ::ₘ 0).lcm = normalize a := by simp
@[simp] lemma lcm_singleton {a : α} : ({a} : multiset α).lcm = normalize a :=
(fold_singleton _ _ _).trans $ lcm_one_right _

@[simp] lemma lcm_add (s₁ s₂ : multiset α) : (s₁ + s₂).lcm = gcd_monoid.lcm s₁.lcm s₂.lcm :=
eq.trans (by simp [lcm]) (fold_add _ _ _ _ _)
Expand Down Expand Up @@ -94,7 +95,8 @@ fold_zero _ _
(a ::ₘ s).gcd = gcd_monoid.gcd a s.gcd :=
fold_cons_left _ _ _ _

@[simp] lemma gcd_singleton {a : α} : (a ::ₘ 0).gcd = normalize a := by simp
@[simp] lemma gcd_singleton {a : α} : ({a} : multiset α).gcd = normalize a :=
(fold_singleton _ _ _).trans $ gcd_zero_right _

@[simp] lemma gcd_add (s₁ s₂ : multiset α) : (s₁ + s₂).gcd = gcd_monoid.gcd s₁.gcd s₂.gcd :=
eq.trans (by simp [gcd]) (fold_add _ _ _ _ _)
Expand Down
6 changes: 5 additions & 1 deletion src/data/finset/basic.lean
Expand Up @@ -382,7 +382,7 @@ This differs from `insert a ∅` in that it does not require a `decidable_eq` in
-/
instance : has_singleton α (finset α) := ⟨λ a, ⟨{a}, nodup_singleton a⟩⟩

@[simp] theorem singleton_val (a : α) : ({a} : finset α).1 = a ::ₘ 0 := rfl
@[simp] theorem singleton_val (a : α) : ({a} : finset α).1 = {a} := rfl

@[simp] theorem mem_singleton {a b : α} : b ∈ ({a} : finset α) ↔ b = a := mem_singleton

Expand Down Expand Up @@ -1654,6 +1654,10 @@ rfl
to_finset (a ::ₘ s) = insert a (to_finset s) :=
finset.eq_of_veq erase_dup_cons

@[simp] lemma to_finset_singleton (a : α) :
to_finset ({a} : multiset α) = {a} :=
by rw [singleton_eq_cons, to_finset_cons, to_finset_zero, is_lawful_singleton.insert_emptyc_eq]

@[simp] lemma to_finset_add (s t : multiset α) :
to_finset (s + t) = to_finset s ∪ to_finset t :=
finset.ext $ by simp
Expand Down
2 changes: 1 addition & 1 deletion src/data/finset/noncomm_prod.lean
Expand Up @@ -215,7 +215,7 @@ by simp [noncomm_prod, insert_val_of_not_mem ha, multiset.noncomm_prod_cons']
@[simp, to_additive] lemma noncomm_prod_singleton (a : α) (f : α → β) :
noncomm_prod ({a} : finset α) f
(λ x hx y hy, by by rw [mem_singleton.mp hx, mem_singleton.mp hy]) = f a :=
by simp [noncomm_prod]
by simp [noncomm_prod, multiset.singleton_eq_cons]

@[to_additive] lemma noncomm_prod_eq_prod {β : Type*} [comm_monoid β] (s : finset α) (f : α → β) :
noncomm_prod s f (λ _ _ _ _, commute.all _ _) = s.prod f :=
Expand Down
30 changes: 13 additions & 17 deletions src/data/finsupp/basic.lean
Expand Up @@ -1866,11 +1866,12 @@ section multiset
def to_multiset : (α →₀ ℕ) ≃+ multiset α :=
{ to_fun := λ f, f.sum (λa n, n • {a}),
inv_fun := λ s, ⟨s.to_finset, λ a, s.count a, λ a, by simp⟩,
left_inv := λ f, ext $ λ a,
suffices (if f a = 0 then 0 else f a) = f a,
by simpa [finsupp.sum, multiset.count_sum', multiset.count_cons],
by split_ifs with h; [rw h, refl],
right_inv := λ s, by simp [finsupp.sum],
left_inv := λ f, ext $ λ a, by {
simp only [sum, multiset.count_sum', multiset.count_singleton, mul_boole, coe_mk,
multiset.mem_to_finset, iff_self, not_not, mem_support_iff, ite_eq_left_iff, ne.def,
multiset.count_eq_zero, multiset.count_nsmul, finset.sum_ite_eq, ite_not],
exact eq.symm },
right_inv := λ s, by simp only [sum, coe_mk, multiset.to_finset_sum_count_nsmul_eq],
map_add' := λ f g, sum_add_index (λ a, zero_nsmul _) (λ a, add_nsmul _) }

lemma to_multiset_zero : (0 : α →₀ ℕ).to_multiset = 0 :=
Expand All @@ -1896,8 +1897,7 @@ add_equiv.map_sum _ _ _

lemma to_multiset_sum_single {ι : Type*} (s : finset ι) (n : ℕ) :
finsupp.to_multiset (∑ i in s, single i n) = n • s.val :=
by simp_rw [to_multiset_sum, finsupp.to_multiset_single, multiset.singleton_eq_singleton,
sum_nsmul, sum_multiset_singleton]
by simp_rw [to_multiset_sum, finsupp.to_multiset_single, sum_nsmul, sum_multiset_singleton]

lemma card_to_multiset (f : α →₀ ℕ) : f.to_multiset.card = f.sum (λa, id) :=
by simp [to_multiset_apply, add_monoid_hom.map_finsupp_sum, function.id_def]
Expand All @@ -1921,8 +1921,7 @@ begin
{ rw [to_multiset_zero, multiset.prod_zero, finsupp.prod_zero_index] },
{ assume a n f _ _ ih,
rw [to_multiset_add, multiset.prod_add, ih, to_multiset_single, finsupp.prod_add_index,
finsupp.prod_single_index, multiset.prod_nsmul, multiset.singleton_eq_singleton,
multiset.prod_singleton],
finsupp.prod_single_index, multiset.prod_nsmul, multiset.prod_singleton],
{ exact pow_zero a },
{ exact pow_zero },
{ exact pow_add } }
Expand All @@ -1935,9 +1934,7 @@ begin
{ rw [to_multiset_zero, multiset.to_finset_zero, support_zero] },
{ assume a n f ha hn ih,
rw [to_multiset_add, multiset.to_finset_add, ih, to_multiset_single, support_add_eq,
support_single_ne_zero hn, multiset.to_finset_nsmul _ _ hn,
multiset.singleton_eq_singleton, multiset.to_finset_cons, multiset.to_finset_zero],
refl,
support_single_ne_zero hn, multiset.to_finset_nsmul _ _ hn, multiset.to_finset_singleton],
refine disjoint.mono_left support_single_subset _,
rwa [finset.singleton_disjoint] }
end
Expand All @@ -1947,11 +1944,10 @@ end
calc f.to_multiset.count a = f.sum (λx n, (n • {x} : multiset α).count a) :
(multiset.count_add_monoid_hom a).map_sum _ f.support
... = f.sum (λx n, n * ({x} : multiset α).count a) : by simp only [multiset.count_nsmul]
... = f.sum (λx n, n * (x ::ₘ 0 : multiset α).count a) : rfl
... = f a * (a ::ₘ 0 : multiset α).count a : sum_eq_single _
(λ a' _ H, by simp only [multiset.count_cons_of_ne (ne.symm H), multiset.count_zero, mul_zero])
... = f a * ({a} : multiset α).count a : sum_eq_single _
(λ a' _ H, by simp only [multiset.count_singleton, if_false, H.symm, mul_zero])
(λ H, by simp only [not_mem_support_iff.1 H, zero_mul])
... = f a : by simp only [multiset.count_singleton, mul_one]
... = f a : by rw [multiset.count_singleton_self, mul_one]

lemma mem_support_multiset_sum [add_comm_monoid M]
{s : multiset (α →₀ M)} (a : α) :
Expand Down Expand Up @@ -2547,7 +2543,7 @@ lemma to_finsupp_add (s t : multiset α) :
to_finsupp (s + t) = to_finsupp s + to_finsupp t :=
to_finsupp.map_add s t

@[simp] lemma to_finsupp_singleton (a : α) : to_finsupp (a ::ₘ 0) = finsupp.single a 1 :=
@[simp] lemma to_finsupp_singleton (a : α) : to_finsupp ({a} : multiset α) = finsupp.single a 1 :=
finsupp.to_multiset.symm_apply_eq.2 $ by simp

@[simp] lemma to_finsupp_to_multiset (s : multiset α) :
Expand Down
2 changes: 1 addition & 1 deletion src/data/multiset/antidiagonal.lean
Expand Up @@ -53,7 +53,7 @@ by simp [powerset_aux']
quotient.induction_on s $ λ l,
by simp [powerset_aux']

@[simp] theorem antidiagonal_zero : @antidiagonal α 0 = (0, 0) ::ₘ 0 := rfl
@[simp] theorem antidiagonal_zero : @antidiagonal α 0 = {(0, 0)} := rfl

@[simp] theorem antidiagonal_cons (a : α) (s) : antidiagonal (a ::ₘ s) =
map (prod.map id (cons a)) (antidiagonal s) +
Expand Down

0 comments on commit d3e20b4

Please sign in to comment.