Basics for the Rational Numbers #
Summary #
We define a rational number q
as a structure { num, denom, pos, cop }
, where
num
is the numerator ofq
,denom
is the denominator ofq
,pos
is a proof thatdenom > 0
, andcop
is a proofnum
anddenom
are coprime.
We then define the expected (discrete) field structure on ℚ
and prove basic lemmas about it.
Moreoever, we provide the expected casts from ℕ
and ℤ
into ℚ
, i.e. (↑n : ℚ) = n / 1
.
Main Definitions #
Notations #
/.
is infix notation forrat.mk
.
Tags #
rat, rationals, field, ℚ, numerator, denominator, num, denom
rat
, or ℚ
, is the type of rational numbers. It is defined
as the set of pairs ⟨n, d⟩ of integers such that d
is positive and n
and
d
are coprime. This representation is preferred to the quotient
because without periodic reduction, the numerator and denominator can grow
exponentially (for example, adding 1/2 to itself repeatedly).
Equations
- rat.has_repr = {repr := rat.repr}
Equations
Equations
- rat.encodable = encodable.of_equiv (Σ (n : ℤ), {d // 0 < d ∧ n.nat_abs.coprime d}) {to_fun := λ (_x : ℚ), rat.encodable._match_1 _x, inv_fun := λ (_x : Σ (n : ℤ), {d // 0 < d ∧ n.nat_abs.coprime d}), rat.encodable._match_2 _x, left_inv := rat.encodable._proof_1, right_inv := rat.encodable._proof_2}
- rat.encodable._match_1 {num := a, denom := b, pos := c, cop := d} = ⟨a, ⟨b, _⟩⟩
- rat.encodable._match_2 ⟨a, ⟨b, _⟩⟩ = {num := a, denom := b, pos := c, cop := d}
Embed an integer as a rational number
Equations
- rat.of_int n = {num := n, denom := 1, pos := nat.one_pos, cop := _}
Equations
- rat.has_zero = {zero := rat.of_int 0}
Equations
- rat.has_one = {one := rat.of_int 1}
Equations
- rat.inhabited = {default := 0}
Form the quotient n / d
where n:ℤ
and d:ℕ
. In the case d = 0
, we
define n / 0 = 0
by convention.
Equations
- rat.mk_nat n d = dite (d = 0) (λ (d0 : d = 0), 0) (λ (d0 : ¬d = 0), rat.mk_pnat n ⟨d, _⟩)
Define a (dependent) function or prove ∀ r : ℚ, p r
by dealing with rational
numbers of the form n /. d
with d ≠ 0
.
Equations
- a.num_denom_cases_on' H = a.num_denom_cases_on (λ (n : ℤ) (d : ℕ) (h : 0 < d) (c : n.nat_abs.coprime d), H n d _)
Inverse rational number. Use r⁻¹
instead.
Equations
- rat.decidable_eq = id (λ (_v : ℚ), _v.cases_on (λ (num : ℤ) (denom : ℕ) (pos : 0 < denom) (cop : num.nat_abs.coprime denom) (w : ℚ), w.cases_on (λ (w_num : ℤ) (w_denom : ℕ) (w_pos : 0 < w_denom) (w_cop : w_num.nat_abs.coprime w_denom), decidable.by_cases (λ (ᾰ : num = w_num), eq.rec (λ (w_cop : num.nat_abs.coprime w_denom), decidable.by_cases (λ (ᾰ : denom = w_denom), eq.rec (λ (w_pos : 0 < denom) (w_cop : num.nat_abs.coprime denom), is_true _) ᾰ w_pos w_cop) (λ (ᾰ : ¬denom = w_denom), is_false _)) ᾰ w_cop) (λ (ᾰ : ¬num = w_num), is_false _))))
Equations
- rat.field = {add := rat.add, add_assoc := rat.add_assoc, zero := 0, zero_add := rat.zero_add, add_zero := rat.add_zero, nsmul := comm_ring.nsmul._default 0 rat.add rat.zero_add rat.add_zero, nsmul_zero' := rat.field._proof_1, nsmul_succ' := rat.field._proof_2, neg := rat.neg, sub := comm_ring.sub._default rat.add rat.add_assoc 0 rat.zero_add rat.add_zero (comm_ring.nsmul._default 0 rat.add rat.zero_add rat.add_zero) rat.field._proof_3 rat.field._proof_4 rat.neg, sub_eq_add_neg := rat.field._proof_5, zsmul := comm_ring.zsmul._default rat.add rat.add_assoc 0 rat.zero_add rat.add_zero (comm_ring.nsmul._default 0 rat.add rat.zero_add rat.add_zero) rat.field._proof_6 rat.field._proof_7 rat.neg, zsmul_zero' := rat.field._proof_8, zsmul_succ' := rat.field._proof_9, zsmul_neg' := rat.field._proof_10, add_left_neg := rat.add_left_neg, add_comm := rat.add_comm, mul := rat.mul, mul_assoc := rat.mul_assoc, one := 1, one_mul := rat.one_mul, mul_one := rat.mul_one, npow := comm_ring.npow._default 1 rat.mul rat.one_mul rat.mul_one, npow_zero' := rat.field._proof_11, npow_succ' := rat.field._proof_12, left_distrib := rat.mul_add, right_distrib := rat.add_mul, mul_comm := rat.mul_comm, inv := rat.inv, div := div_inv_monoid.div._default rat.mul rat.mul_assoc 1 rat.one_mul rat.mul_one (comm_ring.npow._default 1 rat.mul rat.one_mul rat.mul_one) rat.field._proof_13 rat.field._proof_14 rat.inv, div_eq_mul_inv := rat.field._proof_15, zpow := div_inv_monoid.zpow._default rat.mul rat.mul_assoc 1 rat.one_mul rat.mul_one (comm_ring.npow._default 1 rat.mul rat.one_mul rat.mul_one) rat.field._proof_16 rat.field._proof_17 rat.inv, zpow_zero' := rat.field._proof_18, zpow_succ' := rat.field._proof_19, zpow_neg' := rat.field._proof_20, exists_pair_ne := rat.field._proof_21, mul_inv_cancel := rat.mul_inv_cancel, inv_zero := rat.field._proof_22}