A rational number $\frac{\text{num}}{\text{den}}$ in canonical form.
Invariants:
- $\text{den} > 0$ (denominator is a positive natural number)
- $\gcd(|\text{num}|, \text{den}) = 1$ (fully reduced)
The sign is carried solely by num; den is always positive.
These invariants make structural equality correct:
two Ratio values are equal iff they represent the same rational number.
- num : Int
The numerator (carries the sign).
- den : Nat
The denominator (always positive).
Proof that the denominator is positive: $\text{den} > 0$.
Proof of coprimality: $\gcd(|\text{num}|, \text{den}) = 1$.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- Data.instReprRatio = { reprPrec := Data.instReprRatio.repr }
The rational number $0 = \frac{0}{1}$.
Equations
- Data.Ratio.zero = { num := 0, den := 1, den_pos := Nat.one_pos, coprime := Data.Ratio.coprime_one✝ }
Instances For
The rational number $1 = \frac{1}{1}$.
Equations
- Data.Ratio.one = { num := 1, den := 1, den_pos := Nat.one_pos, coprime := Data.Ratio.one._proof_1 }
Instances For
Equations
- Data.Ratio.instOfNatOfNatNat = { ofNat := Data.Ratio.zero }
Equations
- Data.Ratio.instOfNatOfNatNat_1 = { ofNat := Data.Ratio.one }
Equations
- Data.Ratio.instInhabited = { default := Data.Ratio.zero }
Smart constructor: normalize $\frac{n}{d}$ to canonical form.
Given integers $n$ and $d \neq 0$:
- Compute $g = \gcd(|n|, |d|)$
- Adjust sign so denominator is positive
- Divide both by $g$
$$\text{mk'}(n, d) = \frac{n / g}{d / g} \quad \text{where } g = \gcd(|n|, |d|)$$
Equations
- One or more equations did not get rendered due to their size.
Instances For
Create a ratio from an integer: $n \mapsto \frac{n}{1}$.
Equations
- Data.Ratio.fromInt n = { num := n, den := 1, den_pos := Nat.one_pos, coprime := ⋯ }
Instances For
Create a ratio from a natural number: $n \mapsto \frac{n}{1}$.
Equations
Instances For
Equations
- Data.Ratio.instAdd = { add := Data.Ratio.add }
Equations
- Data.Ratio.instSub = { sub := Data.Ratio.sub }
Equations
- Data.Ratio.instMul = { mul := Data.Ratio.mul }
Equations
- Data.Ratio.instNeg = { neg := Data.Ratio.neg }