Documentation

Hale.Base.Data.Ratio

structure Data.Ratio :

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).

  • den_pos : self.den > 0

    Proof that the denominator is positive: $\text{den} > 0$.

  • coprime : self.num.natAbs.Coprime self.den

    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
      @[implicit_reducible]
      Equations

      The rational number $0 = \frac{0}{1}$.

      Equations
      Instances For

        The rational number $1 = \frac{1}{1}$.

        Equations
        Instances For
          @[implicit_reducible]
          Equations
          def Data.Ratio.mk' (n d : Int) (hd : d 0) :

          Smart constructor: normalize $\frac{n}{d}$ to canonical form.

          Given integers $n$ and $d \neq 0$:

          1. Compute $g = \gcd(|n|, |d|)$
          2. Adjust sign so denominator is positive
          3. 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
            @[inline]

            Create a ratio from an integer: $n \mapsto \frac{n}{1}$.

            Equations
            Instances For
              @[inline]

              Create a ratio from a natural number: $n \mapsto \frac{n}{1}$.

              Equations
              Instances For

                Negation: $-\frac{p}{q} = \frac{-p}{q}$. Preserves coprimality since $\gcd(|-p|, q) = \gcd(|p|, q)$.

                Equations
                • r.neg = { num := -r.num, den := r.den, den_pos := , coprime := }
                Instances For

                  Absolute value: $\left|\frac{p}{q}\right| = \frac{|p|}{q}$.

                  Equations
                  Instances For

                    Addition of rationals: $$\frac{a}{b} + \frac{c}{d} = \frac{ad + bc}{bd}$$ (normalized to canonical form).

                    Equations
                    Instances For

                      Subtraction: $\frac{a}{b} - \frac{c}{d} = \frac{a}{b} + \frac{-c}{d}$.

                      Equations
                      Instances For

                        Multiplication: $$\frac{a}{b} \times \frac{c}{d} = \frac{ac}{bd}$$ (normalized to canonical form).

                        Equations
                        Instances For
                          def Data.Ratio.inv (r : Ratio) (h : r.num 0) :

                          Reciprocal: $\frac{q}{p}$ for $p \neq 0$. Swaps numerator and denominator, adjusting sign.

                          Equations
                          Instances For
                            def Data.Ratio.div (r s : Ratio) (h : s.num 0) :

                            Division: $\frac{a}{b} \div \frac{c}{d} = \frac{a}{b} \times \frac{d}{c}$. Requires $c/d \neq 0$.

                            Equations
                            Instances For
                              @[implicit_reducible]

                              Comparison: $\frac{a}{b} \leq \frac{c}{d} \iff ad \leq bc$ (since $b, d > 0$).

                              Equations
                              @[implicit_reducible]
                              Equations
                              @[implicit_reducible]
                              Equations
                              @[implicit_reducible]
                              Equations
                              @[implicit_reducible]
                              Equations
                              @[implicit_reducible]
                              Equations
                              @[implicit_reducible]
                              Equations
                              @[implicit_reducible]
                              Equations
                              @[implicit_reducible]
                              Equations

                              Floor: the greatest integer $\leq r$. $$\lfloor r \rfloor = \lfloor \text{num} / \text{den} \rfloor$$

                              Equations
                              Instances For

                                Ceiling: the least integer $\geq r$. $$\lceil r \rceil = -\lfloor -r \rfloor$$

                                Equations
                                Instances For

                                  Round to the nearest integer (half rounds away from zero). $$\text{round}(r) = \lfloor r + 1/2 \rfloor$$

                                  Equations
                                  Instances For