Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit fb7dfa1

Browse files
avigadmergify[bot]
authored andcommitted
feat(data/{nat,int,zmod,finset}): add a few useful facts (#1220)
* feat(data/finset): add a few useful facts * feat(data/zmod/basic): express neg in terms of residues * feat(data/{nat,int}): add theorem 'mod_mod_of_dvd'
1 parent 3d36966 commit fb7dfa1

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

src/data/finset.lean

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,9 @@ ext.2 $ λ a, by simpa only [mem_sdiff, mem_union, or_comm,
508508
@[simp] theorem union_sdiff_of_subset {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁ ∪ (s₂ \ s₁) = s₂ :=
509509
(union_comm _ _).trans (sdiff_union_of_subset h)
510510

511+
theorem inter_sdiff (s t u : finset α) : s ∩ (t \ u) = s ∩ t \ u :=
512+
by { ext x, simp [and_assoc] }
513+
511514
@[simp] theorem inter_sdiff_self (s₁ s₂ : finset α) : s₁ ∩ (s₂ \ s₁) = ∅ :=
512515
eq_empty_of_forall_not_mem $
513516
by simp only [mem_inter, mem_sdiff]; rintro x ⟨h, _, hn⟩; exact hn h
@@ -599,6 +602,19 @@ theorem filter_union_right (p q : α → Prop) [decidable_pred p] [decidable_pre
599602
s.filter p ∪ s.filter q = s.filter (λx, p x ∨ q x) :=
600603
ext.2 $ λ x, by simp only [mem_filter, mem_union, and_or_distrib_left.symm]
601604

605+
theorem filter_inter {s t : finset α} : filter p s ∩ t = filter p (s ∩ t) :=
606+
by {ext, simp [and_assoc], rw [and.left_comm] }
607+
608+
theorem inter_filter {s t : finset α} : s ∩ filter p t = filter p (s ∩ t) :=
609+
by rw [inter_comm, filter_inter, inter_comm]
610+
611+
theorem filter_insert (a : α) (s : finset α) :
612+
filter p (insert a s) = if p a then insert a (filter p s) else (filter p s) :=
613+
by { ext x, simp, split_ifs with h; by_cases h' : x = a; simp [h, h'] }
614+
615+
theorem filter_singleton (a : α) : filter p (singleton a) = if p a then singleton a else ∅ :=
616+
by { ext x, simp, split_ifs with h; by_cases h' : x = a; simp [h, h'] }
617+
602618
theorem filter_or (s : finset α) : s.filter (λ a, p a ∨ q a) = s.filter p ∪ s.filter q :=
603619
ext.2 $ λ _, by simp only [mem_filter, mem_union, and_or_distrib_left]
604620

@@ -1124,6 +1140,14 @@ ext.2 $ λ x, by simp only [mem_bind, exists_prop, mem_union, mem_insert,
11241140
@[simp] lemma singleton_bind [decidable_eq α] {a : α} : (singleton a).bind t = t a :=
11251141
show (insert a ∅ : finset α).bind t = t a, from bind_insert.trans $ union_empty _
11261142

1143+
theorem bind_inter (s : finset α) (f : α → finset β) (t : finset β) :
1144+
s.bind f ∩ t = s.bind (λ x, f x ∩ t) :=
1145+
by { ext x, simp, exact ⟨λ ⟨xt, y, ys, xf⟩, ⟨y, ys, xt, xf⟩, λ ⟨y, ys, xt, xf⟩, ⟨xt, y, ys, xf⟩⟩ }
1146+
1147+
theorem inter_bind (t : finset β) (s : finset α) (f : α → finset β) :
1148+
t ∩ s.bind f = s.bind (λ x, t ∩ f x) :=
1149+
by rw [inter_comm, bind_inter]; simp
1150+
11271151
theorem image_bind [decidable_eq γ] {f : α → β} {s : finset α} {t : β → finset γ} :
11281152
(s.image f).bind t = s.bind (λa, t (f a)) :=
11291153
by haveI := classical.dec_eq α; exact

src/data/int/basic.lean

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,15 @@ by rw [mul_comm, mul_mod_left]
404404
@[simp] theorem mod_self {a : ℤ} : a % a = 0 :=
405405
by have := mul_mod_left 1 a; rwa one_mul at this
406406

407-
@[simp] lemma mod_mod (a b : ℤ) : a % b % b = a % b :=
407+
@[simp] theorem mod_mod (a b : ℤ) : a % b % b = a % b :=
408408
by conv {to_rhs, rw [← mod_add_div a b, add_mul_mod_self_left]}
409409

410+
@[simp] theorem mod_mod_of_dvd (n : int) {m k : int} (h : m ∣ k) : n % k % m = n % m :=
411+
begin
412+
conv { to_rhs, rw ←mod_add_div n k },
413+
rcases h with ⟨t, rfl⟩, rw [mul_assoc, add_mul_mod_self_left]
414+
end
415+
410416
/- properties of / and % -/
411417

412418
@[simp] theorem mul_div_mul_of_pos {a : ℤ} (b c : ℤ) (H : a > 0) : a * b / (a * c) = b / c :=

src/data/nat/basic.lean

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,12 @@ exists_congr $ λ d, by rw [mul_right_comm, nat.mul_right_inj hc]
469469
(λ n0, by simp [n0])
470470
(λ npos, mod_eq_of_lt (mod_lt _ npos))
471471

472+
@[simp] theorem mod_mod_of_dvd (n : nat) {m k : nat} (h : m ∣ k) : n % k % m = n % m :=
473+
begin
474+
conv { to_rhs, rw ←mod_add_div n k },
475+
rcases h with ⟨t, rfl⟩, rw [mul_assoc, add_mul_mod_self_left]
476+
end
477+
472478
theorem add_pos_left {m : ℕ} (h : m > 0) (n : ℕ) : m + n > 0 :=
473479
calc
474480
m + n > 0 + n : nat.add_lt_add_right h n

src/data/zmod/basic.lean

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,20 @@ begin
103103
exact nat.modeq.modeq_add (nat.mod_mod a n) (nat.mod_mod 1 n) }
104104
end
105105

106+
lemma neg_val' {m : pnat} (n : zmod m) : (-n).val = (m - n.val) % m :=
107+
have ((-n).val + n.val) % m = (m - n.val + n.val) % m,
108+
by { rw [←add_val, add_left_neg, nat.sub_add_cancel (le_of_lt n.is_lt), nat.mod_self], refl },
109+
(nat.mod_eq_of_lt (fin.is_lt _)).symm.trans (nat.modeq.modeq_add_cancel_right rfl this)
110+
111+
lemma neg_val {m : pnat} (n : zmod m) : (-n).val = if n = 0 then 0 else m - n.val :=
112+
begin
113+
rw neg_val',
114+
by_cases h : n = 0; simp [h],
115+
cases n with n nlt; cases n; dsimp, { contradiction },
116+
rw nat.mod_eq_of_lt,
117+
apply nat.sub_lt m.2 (nat.succ_pos _),
118+
end
119+
106120
lemma mk_eq_cast {n : ℕ+} {a : ℕ} (h : a < n) : (⟨a, h⟩ : zmod n) = (a : zmod n) :=
107121
fin.eq_of_veq (by rw [val_cast_nat, nat.mod_eq_of_lt h])
108122

0 commit comments

Comments
 (0)