Cast of natural numbers #
THIS FILE IS SYNCHRONIZED WITH MATHLIB4. Any changes to this file require a corresponding PR to mathlib4.
This file defines the canonical homomorphism from the natural numbers into an
add_monoid
with a one. In additive monoids with one, there exists a unique
such homomorphism and we store it in the nat_cast : ℕ → R
field.
Preferentially, the homomorphism is written as a coercion.
Main declarations #
add_monoid_with_one
: Type class fornat.cast
.nat.cast
: Canonical homomorphismℕ → R
.
The numeral ((0+1)+⋯)+1
.
Equations
- (n + 1).unary_cast = n.unary_cast + 1
- 0.unary_cast = 0
Type class for the canonical homomorphism ℕ → R
.
Instances of this typeclass
- add_monoid_with_one.to_has_nat_cast
- mul_opposite.has_nat_cast
- add_opposite.has_nat_cast
- pi.has_nat_cast
- order_dual.has_nat_cast
- lex.has_nat_cast
- ulift.has_nat_cast
- cardinal.has_nat_cast
- self_adjoint.has_nat_cast
- ring_con.quotient.has_nat_cast
- cau_seq.completion.Cauchy.has_nat_cast
- real.has_nat_cast
- polynomial.has_nat_cast
Instances of other typeclasses for has_nat_cast
- has_nat_cast.has_sizeof_inst
- nat_cast : ℕ → R
- add : R → R → R
- add_assoc : ∀ (a b c : R), a + b + c = a + (b + c)
- zero : R
- zero_add : ∀ (a : R), 0 + a = a
- add_zero : ∀ (a : R), a + 0 = a
- nsmul : ℕ → R → R
- nsmul_zero' : (∀ (x : R), add_monoid_with_one.nsmul 0 x = 0) . "try_refl_tac"
- nsmul_succ' : (∀ (n : ℕ) (x : R), add_monoid_with_one.nsmul n.succ x = x + add_monoid_with_one.nsmul n x) . "try_refl_tac"
- one : R
- nat_cast_zero : add_monoid_with_one.nat_cast 0 = 0 . "control_laws_tac"
- nat_cast_succ : (∀ (n : ℕ), add_monoid_with_one.nat_cast (n + 1) = add_monoid_with_one.nat_cast n + 1) . "control_laws_tac"
An add_monoid_with_one
is an add_monoid
with a 1
.
It also contains data for the unique homomorphism ℕ → R
.
Instances of this typeclass
- add_submonoid_with_one_class.to_add_monoid_with_one
- add_comm_monoid_with_one.to_add_monoid_with_one
- add_group_with_one.to_add_monoid_with_one
- mul_opposite.add_monoid_with_one
- with_zero.add_monoid_with_one
- with_top.add_monoid_with_one
- with_bot.add_monoid_with_one
- pi.add_monoid_with_one
- order_dual.add_monoid_with_one
- lex.add_monoid_with_one
- fin.add_monoid_with_one
- ulift.add_monoid_with_one
- prod.add_monoid_with_one
- ordinal.add_monoid_with_one
- nonneg.add_monoid_with_one
- algebra.tensor_product.tensor_product.add_monoid_with_one
Instances of other typeclasses for add_monoid_with_one
- add_monoid_with_one.has_sizeof_inst
Canonical homomorphism from ℕ
to a additive monoid R
with a 1
.
Equations
- nat_cast : ℕ → R
- add : R → R → R
- add_assoc : ∀ (a b c : R), a + b + c = a + (b + c)
- zero : R
- zero_add : ∀ (a : R), 0 + a = a
- add_zero : ∀ (a : R), a + 0 = a
- nsmul : ℕ → R → R
- nsmul_zero' : (∀ (x : R), add_comm_monoid_with_one.nsmul 0 x = 0) . "try_refl_tac"
- nsmul_succ' : (∀ (n : ℕ) (x : R), add_comm_monoid_with_one.nsmul n.succ x = x + add_comm_monoid_with_one.nsmul n x) . "try_refl_tac"
- one : R
- nat_cast_zero : add_comm_monoid_with_one.nat_cast 0 = 0 . "control_laws_tac"
- nat_cast_succ : (∀ (n : ℕ), add_comm_monoid_with_one.nat_cast (n + 1) = add_comm_monoid_with_one.nat_cast n + 1) . "control_laws_tac"
- add_comm : ∀ (a b : R), a + b = b + a
An add_comm_monoid_with_one
is an add_monoid_with_one
satisfying a + b = b + a
.
Instances of this typeclass
- add_comm_group_with_one.to_add_comm_monoid_with_one
- non_assoc_semiring.to_add_comm_monoid_with_one
- mul_opposite.add_comm_monoid_with_one
- add_opposite.add_comm_monoid_with_one
- with_top.add_comm_monoid_with_one
- with_bot.add_comm_monoid_with_one
- order_dual.add_comm_monoid_with_one
- lex.add_comm_monoid_with_one
- ulift.add_comm_monoid_with_one
- enat.add_comm_monoid_with_one
- part_enat.add_comm_monoid_with_one
- ennreal.add_comm_monoid_with_one
Instances of other typeclasses for add_comm_monoid_with_one
- add_comm_monoid_with_one.has_sizeof_inst
Coercions such as nat.cast_coe
that go from a concrete structure such as
ℕ
to an arbitrary ring R
should be set up as follows:
@[priority 900] instance : has_coe_t ℕ R := ⟨...⟩
It needs to be has_coe_t
instead of has_coe
because otherwise type-class
inference would loop when constructing the transitive coercion ℕ → ℕ → ℕ → ...
.
The reduced priority is necessary so that it doesn't conflict with instances
such as has_coe_t R (option R)
.
For this to work, we reduce the priority of the coe_base
and coe_trans
instances because we want the instances for has_coe_t
to be tried in the
following order:
has_coe_t
instances declared in mathlib (such ashas_coe_t R (with_top R)
, etc.)coe_base
, which contains instances such ashas_coe (fin n) n
nat.cast_coe : has_coe_t ℕ R
etc.coe_trans
If coe_trans
is tried first, then nat.cast_coe
doesn't get a chance to apply.
Equations
- nat.cast_coe = {coe := nat.cast _inst_2}
Computationally friendlier cast than nat.unary_cast
, using binary representation.
add_monoid_with_one
implementation using unary recursion.
Equations
- add_monoid_with_one.unary = {nat_cast := nat.unary_cast (add_zero_class.to_has_add R), add := add_monoid.add _inst_1, add_assoc := _, zero := add_monoid.zero _inst_1, zero_add := _, add_zero := _, nsmul := add_monoid.nsmul _inst_1, nsmul_zero' := _, nsmul_succ' := _, one := 1, nat_cast_zero := _, nat_cast_succ := _}
add_monoid_with_one
implementation using binary recursion.
Equations
- add_monoid_with_one.binary = {nat_cast := nat.bin_cast (add_zero_class.to_has_add R), add := add_monoid.add _inst_1, add_assoc := _, zero := add_monoid.zero _inst_1, zero_add := _, add_zero := _, nsmul := add_monoid.nsmul _inst_1, nsmul_zero' := _, nsmul_succ' := _, one := 1, nat_cast_zero := _, nat_cast_succ := _}