Datasets:

Modalities:
Text
Languages:
English
Libraries:
Datasets
License:
Zhangir Azerbayev
squashed?
4365a98
raw
history blame
9.08 kB
/-
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