Cast of natural numbers #
This file defines the canonical homomorphism from the natural numbers into an
AddMonoid
with a one. In additive monoids with one, there exists a unique
such homomorphism and we store it in the natCast : ℕ → R
field.
Preferentially, the homomorphism is written as the coercion Nat.cast
.
Main declarations #
NatCast
: Type class forNat.cast
.AddMonoidWithOne
: Type class for whichNat.cast
is a canonical monoid homomorphism fromℕ
.Nat.cast
: Canonical homomorphismℕ → R
.
The numeral ((0+1)+⋯)+1
.
Equations
- Nat.unaryCast 0 = 0
- Nat.unaryCast (Nat.succ n) = Nat.unaryCast n + 1
Instances For
Recognize numeric literals which are at least 2
as terms of R
via Nat.cast
. This
instance is what makes things like 37 : R
type check. Note that 0
and 1
are not needed
because they are recognized as terms of R
(at least when R
is an AddMonoidWithOne
) through
Zero
and One
, respectively.
Equations
- instOfNatAtLeastTwo = { ofNat := ↑n }
Additive monoids with one #
An AddMonoidWithOne
is an AddMonoid
with a 1
.
It also contains data for the unique homomorphism ℕ → R
.
- natCast : ℕ → R
- add : R → R → R
- zero : R
- nsmul : ℕ → R → R
- nsmul_zero : ∀ (x : R), AddMonoid.nsmul 0 x = 0
- nsmul_succ : ∀ (n : ℕ) (x : R), AddMonoid.nsmul (n + 1) x = AddMonoid.nsmul n x + x
- one : R
- natCast_zero : NatCast.natCast 0 = 0
The canonical map
ℕ → R
sends0 : ℕ
to0 : R
. - natCast_succ : ∀ (n : ℕ), NatCast.natCast (n + 1) = NatCast.natCast n + 1
The canonical map
ℕ → R
is a homomorphism.
Instances
An AddCommMonoidWithOne
is an AddMonoidWithOne
satisfying a + b = b + a
.
- natCast : ℕ → R
- add : R → R → R
- zero : R
- nsmul : ℕ → R → R
- nsmul_zero : ∀ (x : R), AddMonoid.nsmul 0 x = 0
- nsmul_succ : ∀ (n : ℕ) (x : R), AddMonoid.nsmul (n + 1) x = AddMonoid.nsmul n x + x
- one : R
- natCast_zero : NatCast.natCast 0 = 0
- natCast_succ : ∀ (n : ℕ), NatCast.natCast (n + 1) = NatCast.natCast n + 1
Addition is commutative in an commutative additive magma.
Instances
Computationally friendlier cast than Nat.unaryCast
, using binary representation.
Equations
- Nat.binCast 0 = 0
- Nat.binCast (Nat.succ n) = if (n + 1) % 2 = 0 then Nat.binCast ((n + 1) / 2) + Nat.binCast ((n + 1) / 2) else Nat.binCast ((n + 1) / 2) + Nat.binCast ((n + 1) / 2) + 1
Instances For
AddMonoidWithOne
implementation using unary recursion.
Equations
- AddMonoidWithOne.unary = let __src := inst; let __src_1 := inst✝; AddMonoidWithOne.mk ⋯ ⋯
Instances For
AddMonoidWithOne
implementation using binary recursion.
Equations
- AddMonoidWithOne.binary = let __src := inst; let __src_1 := inst✝; AddMonoidWithOne.mk ⋯ ⋯