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

Commit 7a0513d

Browse files
committed
feat(data/nat/*): generalize typeclass assumptions (#13260)
1 parent f5ee47b commit 7a0513d

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

src/data/nat/cast.lean

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ end prod
264264

265265
section add_monoid_hom_class
266266

267-
variables {A B F : Type*} [add_monoid A] [add_monoid B] [has_one B]
267+
variables {A B F : Type*} [add_zero_class A] [add_monoid B] [has_one B]
268268

269269
lemma ext_nat' [add_monoid_hom_class F ℕ A] (f g : F) (h : f 1 = g 1) : f = g :=
270270
fun_like.ext f g $ begin
@@ -283,15 +283,16 @@ lemma eq_nat_cast' [add_monoid_hom_class F ℕ A] (f : F) (h1 : f 1 = 1) :
283283
| 0 := by simp
284284
| (n+1) := by rw [map_add, h1, eq_nat_cast' n, nat.cast_add_one]
285285

286-
lemma map_nat_cast' [add_monoid_hom_class F A B] (f : F) (h : f 1 = 1) : ∀ (n : ℕ), f n = n
286+
lemma map_nat_cast' {A} [add_monoid A] [has_one A] [add_monoid_hom_class F A B]
287+
(f : F) (h : f 1 = 1) : ∀ (n : ℕ), f n = n
287288
| 0 := by simp
288289
| (n+1) := by rw [nat.cast_add, map_add, nat.cast_add, map_nat_cast', nat.cast_one, h, nat.cast_one]
289290

290291
end add_monoid_hom_class
291292

292293
section monoid_with_zero_hom_class
293294

294-
variables {A F : Type*} [monoid_with_zero A]
295+
variables {A F : Type*} [mul_zero_one_class A]
295296

296297
/-- If two `monoid_with_zero_hom`s agree on the positive naturals they are equal. -/
297298
theorem ext_nat'' [monoid_with_zero_hom_class F ℕ A] (f g : F)

src/data/nat/parity.lean

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,15 +194,9 @@ end
194194
lemma even_sub_one_of_prime_ne_two {p : ℕ} (hp : prime p) (hodd : p ≠ 2) : even (p - 1) :=
195195
odd.sub_odd (odd_iff.2 $ hp.eq_two_or_odd.resolve_left hodd) (odd_iff.2 rfl)
196196

197-
variables {R : Type*} [ring R]
197+
section distrib_neg_monoid
198198

199-
theorem neg_one_pow_eq_one_iff_even (h1 : (-1 : R) ≠ 1) : (-1 : R) ^ n = 1 ↔ even n :=
200-
begin
201-
rcases n.even_or_odd' with ⟨n, rfl | rfl⟩,
202-
{ simp [neg_one_pow_eq_pow_mod_two, pow_zero] },
203-
{ rw [← not_iff_not, neg_one_pow_eq_pow_mod_two, not_even_iff, add_mod],
204-
simp only [h1, mul_mod_right, one_mod, pow_one, not_false_iff, eq_self_iff_true] }
205-
end
199+
variables {R : Type*} [monoid R] [has_distrib_neg R]
206200

207201
@[simp] theorem neg_one_sq : (-1 : R) ^ 2 = 1 := by simp
208202

@@ -229,6 +223,15 @@ by { convert nat.div_add_mod' n 2, rw odd_iff.mp h }
229223
lemma one_add_div_two_mul_two_of_odd (h : odd n) : 1 + n / 2 * 2 = n :=
230224
by { rw add_comm, convert nat.div_add_mod' n 2, rw odd_iff.mp h }
231225

226+
theorem neg_one_pow_eq_one_iff_even (h1 : (-1 : R) ≠ 1) : (-1 : R) ^ n = 1 ↔ even n :=
227+
begin
228+
refine ⟨λ h, _, neg_one_pow_of_even⟩,
229+
contrapose! h1,
230+
exact (neg_one_pow_of_odd $ odd_iff_not_even.mpr h1).symm.trans h
231+
end
232+
233+
end distrib_neg_monoid
234+
232235
-- Here are examples of how `parity_simps` can be used with `nat`.
233236

234237
example (m n : ℕ) (h : even m) : ¬ even (n + 3) ↔ even (m^2 + m + n) :=

0 commit comments

Comments
 (0)