Datasets:
Tasks:
Text Generation
Modalities:
Text
Sub-tasks:
language-modeling
Languages:
English
Size:
100K - 1M
License:
/- | |
Copyright (c) 2014 Mario Carneiro. All rights reserved. | |
Released under Apache 2.0 license as described in the file LICENSE. | |
Authors: Mario Carneiro | |
-/ | |
import data.nat.basic | |
import data.nat.cast.defs | |
import algebra.group.pi | |
import tactic.pi_instances | |
import data.sum.basic | |
/-! | |
# Cast of natural numbers (additional theorems) | |
This file proves additional properties about the *canonical* homomorphism from | |
the natural numbers into an additive monoid with a one (`nat.cast`). | |
## Main declarations | |
* `cast_add_monoid_hom`: `cast` bundled as an `add_monoid_hom`. | |
* `cast_ring_hom`: `cast` bundled as a `ring_hom`. | |
-/ | |
namespace nat | |
variables {Ξ± : Type*} | |
/-- `coe : β β Ξ±` as an `add_monoid_hom`. -/ | |
def cast_add_monoid_hom (Ξ± : Type*) [add_monoid_with_one Ξ±] : β β+ Ξ± := | |
{ to_fun := coe, | |
map_add' := cast_add, | |
map_zero' := cast_zero } | |
@[simp] lemma coe_cast_add_monoid_hom [add_monoid_with_one Ξ±] : | |
(cast_add_monoid_hom Ξ± : β β Ξ±) = coe := rfl | |
@[simp, norm_cast] theorem cast_mul [non_assoc_semiring Ξ±] (m n : β) : | |
((m * n : β) : Ξ±) = m * n := | |
by induction n; simp [mul_succ, mul_add, *] | |
/-- `coe : β β Ξ±` as a `ring_hom` -/ | |
def cast_ring_hom (Ξ± : Type*) [non_assoc_semiring Ξ±] : β β+* Ξ± := | |
{ to_fun := coe, | |
map_one' := cast_one, | |
map_mul' := cast_mul, | |
.. cast_add_monoid_hom Ξ± } | |
@[simp] lemma coe_cast_ring_hom [non_assoc_semiring Ξ±] : (cast_ring_hom Ξ± : β β Ξ±) = coe := rfl | |
lemma cast_commute [non_assoc_semiring Ξ±] (n : β) (x : Ξ±) : commute βn x := | |
nat.rec_on n (by rw [cast_zero]; exact commute.zero_left x) $ | |
Ξ» n ihn, by rw [cast_succ]; exact ihn.add_left (commute.one_left x) | |
lemma cast_comm [non_assoc_semiring Ξ±] (n : β) (x : Ξ±) : (n : Ξ±) * x = x * n := | |
(cast_commute n x).eq | |
lemma commute_cast [non_assoc_semiring Ξ±] (x : Ξ±) (n : β) : commute x n := | |
(n.cast_commute x).symm | |
section | |
variables [ordered_semiring Ξ±] | |
@[mono] theorem mono_cast : monotone (coe : β β Ξ±) := | |
monotone_nat_of_le_succ $ Ξ» n, by rw [nat.cast_succ]; exact le_add_of_nonneg_right zero_le_one | |
@[simp] theorem cast_nonneg (n : β) : 0 β€ (n : Ξ±) := | |
@nat.cast_zero Ξ± _ βΈ mono_cast (nat.zero_le n) | |
variable [nontrivial Ξ±] | |
@[simp, norm_cast] theorem cast_le {m n : β} : | |
(m : Ξ±) β€ n β m β€ n := | |
strict_mono_cast.le_iff_le | |
@[simp, norm_cast, mono] theorem cast_lt {m n : β} : (m : Ξ±) < n β m < n := | |
strict_mono_cast.lt_iff_lt | |
@[simp] theorem cast_pos {n : β} : (0 : Ξ±) < n β 0 < n := | |
by rw [β cast_zero, cast_lt] | |
lemma cast_add_one_pos (n : β) : 0 < (n : Ξ±) + 1 := | |
add_pos_of_nonneg_of_pos n.cast_nonneg zero_lt_one | |
@[simp, norm_cast] theorem one_lt_cast {n : β} : 1 < (n : Ξ±) β 1 < n := | |
by rw [β cast_one, cast_lt] | |
@[simp, norm_cast] theorem one_le_cast {n : β} : 1 β€ (n : Ξ±) β 1 β€ n := | |
by rw [β cast_one, cast_le] | |
@[simp, norm_cast] theorem cast_lt_one {n : β} : (n : Ξ±) < 1 β n = 0 := | |
by rw [β cast_one, cast_lt, lt_succ_iff, le_zero_iff] | |
@[simp, norm_cast] theorem cast_le_one {n : β} : (n : Ξ±) β€ 1 β n β€ 1 := | |
by rw [β cast_one, cast_le] | |
end | |
@[simp, norm_cast] theorem cast_min [linear_ordered_semiring Ξ±] {a b : β} : | |
(β(min a b) : Ξ±) = min a b := | |
(@mono_cast Ξ± _).map_min | |
@[simp, norm_cast] theorem cast_max [linear_ordered_semiring Ξ±] {a b : β} : | |
(β(max a b) : Ξ±) = max a b := | |
(@mono_cast Ξ± _).map_max | |
@[simp, norm_cast] theorem abs_cast [linear_ordered_ring Ξ±] (a : β) : | |
|(a : Ξ±)| = a := | |
abs_of_nonneg (cast_nonneg a) | |
lemma coe_nat_dvd [semiring Ξ±] {m n : β} (h : m β£ n) : (m : Ξ±) β£ (n : Ξ±) := | |
map_dvd (nat.cast_ring_hom Ξ±) h | |
alias coe_nat_dvd β _root_.has_dvd.dvd.nat_cast | |
end nat | |
namespace prod | |
variables {Ξ± : Type*} {Ξ² : Type*} [add_monoid_with_one Ξ±] [add_monoid_with_one Ξ²] | |
instance : add_monoid_with_one (Ξ± Γ Ξ²) := | |
{ nat_cast := Ξ» n, (n, n), | |
nat_cast_zero := congr_arg2 prod.mk nat.cast_zero nat.cast_zero, | |
nat_cast_succ := Ξ» n, congr_arg2 prod.mk (nat.cast_succ _) (nat.cast_succ _), | |
.. prod.add_monoid, .. prod.has_one } | |
@[simp] lemma fst_nat_cast (n : β) : (n : Ξ± Γ Ξ²).fst = n := | |
by induction n; simp * | |
@[simp] lemma snd_nat_cast (n : β) : (n : Ξ± Γ Ξ²).snd = n := | |
by induction n; simp * | |
end prod | |
section add_monoid_hom_class | |
variables {A B F : Type*} [add_monoid_with_one B] | |
lemma ext_nat' [add_monoid A] [add_monoid_hom_class F β A] (f g : F) (h : f 1 = g 1) : f = g := | |
fun_like.ext f g $ begin | |
apply nat.rec, | |
{ simp only [nat.nat_zero_eq_zero, map_zero] }, | |
simp [nat.succ_eq_add_one, h] {contextual := tt} | |
end | |
@[ext] lemma add_monoid_hom.ext_nat [add_monoid A] : β {f g : β β+ A}, β h : f 1 = g 1, f = g := | |
ext_nat' | |
variable [add_monoid_with_one A] | |
-- these versions are primed so that the `ring_hom_class` versions aren't | |
lemma eq_nat_cast' [add_monoid_hom_class F β A] (f : F) (h1 : f 1 = 1) : | |
β n : β, f n = n | |
| 0 := by simp | |
| (n+1) := by rw [map_add, h1, eq_nat_cast' n, nat.cast_add_one] | |
lemma map_nat_cast' {A} [add_monoid_with_one A] [add_monoid_hom_class F A B] | |
(f : F) (h : f 1 = 1) : β (n : β), f n = n | |
| 0 := by simp | |
| (n+1) := by rw [nat.cast_add, map_add, nat.cast_add, map_nat_cast', nat.cast_one, h, nat.cast_one] | |
end add_monoid_hom_class | |
section monoid_with_zero_hom_class | |
variables {A F : Type*} [mul_zero_one_class A] | |
/-- If two `monoid_with_zero_hom`s agree on the positive naturals they are equal. -/ | |
theorem ext_nat'' [monoid_with_zero_hom_class F β A] (f g : F) | |
(h_pos : β {n : β}, 0 < n β f n = g n) : f = g := | |
begin | |
apply fun_like.ext, | |
rintro (_|n), | |
{ simp }, | |
exact h_pos n.succ_pos | |
end | |
@[ext] theorem monoid_with_zero_hom.ext_nat : | |
β {f g : β β*β A}, (β {n : β}, 0 < n β f n = g n) β f = g := ext_nat'' | |
end monoid_with_zero_hom_class | |
section ring_hom_class | |
variables {R S F : Type*} [non_assoc_semiring R] [non_assoc_semiring S] | |
@[simp] lemma eq_nat_cast [ring_hom_class F β R] (f : F) : β n, f n = n := | |
eq_nat_cast' f $ map_one f | |
@[simp] lemma map_nat_cast [ring_hom_class F R S] (f : F) : β n : β, f (n : R) = n := | |
map_nat_cast' f $ map_one f | |
lemma ext_nat [ring_hom_class F β R] (f g : F) : f = g := | |
ext_nat' f g $ by simp only [map_one] | |
end ring_hom_class | |
namespace ring_hom | |
/-- This is primed to match `ring_hom.eq_int_cast'`. -/ | |
lemma eq_nat_cast' {R} [non_assoc_semiring R] (f : β β+* R) : f = nat.cast_ring_hom R := | |
ring_hom.ext $ eq_nat_cast f | |
end ring_hom | |
@[simp, norm_cast] theorem nat.cast_id (n : β) : βn = n := | |
rfl | |
@[simp] lemma nat.cast_ring_hom_nat : nat.cast_ring_hom β = ring_hom.id β := rfl | |
@[simp] theorem nat.cast_with_bot (n : β) : | |
@coe β (with_bot β) (@coe_to_lift _ _ nat.cast_coe) n = n := rfl | |
-- I don't think `ring_hom_class` is good here, because of the `subsingleton` TC slowness | |
instance nat.unique_ring_hom {R : Type*} [non_assoc_semiring R] : unique (β β+* R) := | |
{ default := nat.cast_ring_hom R, uniq := ring_hom.eq_nat_cast' } | |
namespace mul_opposite | |
variables {Ξ± : Type*} [add_monoid_with_one Ξ±] | |
@[simp, norm_cast] lemma op_nat_cast (n : β) : op (n : Ξ±) = n := rfl | |
@[simp, norm_cast] lemma unop_nat_cast (n : β) : unop (n : Ξ±α΅α΅α΅) = n := rfl | |
end mul_opposite | |
namespace with_top | |
variables {Ξ± : Type*} | |
variables [add_monoid_with_one Ξ±] | |
@[simp, norm_cast] lemma coe_nat : β (n : β), ((n : Ξ±) : with_top Ξ±) = n | |
| 0 := rfl | |
| (n+1) := by { push_cast, rw [coe_nat n] } | |
@[simp] lemma nat_ne_top (n : nat) : (n : with_top Ξ±) β β€ := | |
by { rw [βcoe_nat n], apply coe_ne_top } | |
@[simp] lemma top_ne_nat (n : nat) : (β€ : with_top Ξ±) β n := | |
by { rw [βcoe_nat n], apply top_ne_coe } | |
lemma add_one_le_of_lt {i n : with_top β} (h : i < n) : i + 1 β€ n := | |
begin | |
cases n, { exact le_top }, | |
cases i, { exact (not_le_of_lt h le_top).elim }, | |
exact with_top.coe_le_coe.2 (with_top.coe_lt_coe.1 h) | |
end | |
lemma one_le_iff_pos {n : with_top β} : 1 β€ n β 0 < n := | |
β¨lt_of_lt_of_le (coe_lt_coe.mpr zero_lt_one), | |
Ξ» h, by simpa only [zero_add] using add_one_le_of_lt hβ© | |
@[elab_as_eliminator] | |
lemma nat_induction {P : with_top β β Prop} (a : with_top β) | |
(h0 : P 0) (hsuc : βn:β, P n β P n.succ) (htop : (βn : β, P n) β P β€) : P a := | |
begin | |
have A : βn:β, P n := Ξ» n, nat.rec_on n h0 hsuc, | |
cases a, | |
{ exact htop A }, | |
{ exact A a } | |
end | |
end with_top | |
namespace pi | |
variables {Ξ± : Type*} {Ξ² : Ξ± β Type*} [β a, has_nat_cast (Ξ² a)] | |
instance : has_nat_cast (β a, Ξ² a) := | |
by refine_struct { .. }; tactic.pi_instance_derive_field | |
lemma nat_apply (n : β) (a : Ξ±) : (n : β a, Ξ² a) a = n := rfl | |
@[simp] lemma coe_nat (n : β) : (n : β a, Ξ² a) = Ξ» _, n := rfl | |
end pi | |
lemma sum.elim_nat_cast_nat_cast {Ξ± Ξ² Ξ³ : Type*} [has_nat_cast Ξ³] (n : β) : | |
sum.elim (n : Ξ± β Ξ³) (n : Ξ² β Ξ³) = n := | |
@sum.elim_lam_const_lam_const Ξ± Ξ² Ξ³ n | |
namespace pi | |
variables {Ξ± : Type*} {Ξ² : Ξ± β Type*} [β a, add_monoid_with_one (Ξ² a)] | |
instance : add_monoid_with_one (β a, Ξ² a) := | |
by refine_struct { .. }; tactic.pi_instance_derive_field | |
end pi | |