deep_causality_num_rational 0.1.8

Exact rational number type (the field of fractions of an integral domain) for deep_causality.
Documentation
/*
 * SPDX-License-Identifier: MIT
 * Copyright (c) 2023 - 2026. The DeepCausality Authors and Contributors. All Rights Reserved.
 */

//! Where ℚ sits in the algebra tower.
//!
//! `Rational<T>` is a **field**, and these four markers plus `Invertible` are what carry it
//! there: the tower derives `Ring`, `CommutativeRing`, and `Field` automatically from them
//! together with the structural `Add`/`Mul`/`Div`/`Zero`/`One` impls.
//!
//! Each is a promise the compiler cannot check, so each is stated deliberately:
//!
//! | Law | Why it holds for `a/b` |
//! | :--- | :--- |
//! | `Commutative`  | `(a/b)·(c/d) = ac/bd = ca/db = (c/d)·(a/b)`, inherited from `T` |
//! | `Associative`  | likewise, from associativity of `T` |
//! | `Distributive` | `(a/b)·((c/d)+(e/f))` expands to the same fraction either way |
//! | `AbelianGroup` | additive inverse `-a/b`, identity `0/1` |
//! | `Annihilating` | `0/1 · a/b = 0/b`, which reduces to `0/1` |
//! | `Invertible`   | every non-zero `a/b` has `b/a`, which is the field axiom |
//!
//! `Invertible` is the one that distinguishes ℚ from ℤ. The integers reach `CommutativeRing` and
//! stop there, because integer `/` truncates rather than inverts. Constructing ℚ as the field of
//! fractions is precisely the act of supplying the missing inverses, so ℚ earns the marker that
//! ℤ cannot have.
//!
//! Deliberately absent: [`Real`](deep_causality_algebra::Real). ℚ is not closed under the
//! analytic operations — there is no rational `sqrt(2)`, `exp(1)`, or `ln(2)` — so claiming the
//! analytic axis would be false. ℚ is a field without being analytic, the mirror of `Dual<T>`,
//! which is analytic without being a field.

use super::{Rational, RationalScalar};
use crate::{Annihilating, Associative, Commutative, Distributive, Invertible};
use deep_causality_algebra::IntegralDomain;
use deep_causality_algebra::{Additive, Multiplicative};
use deep_causality_algebra::{Characteristic, DivisibleByIntegers};

impl<T: RationalScalar> Commutative<Multiplicative> for Rational<T> {}
impl<T: RationalScalar> Associative<Multiplicative> for Rational<T> {}
// Addition carries the integer parts separately and adds the fractions over their least
// common denominator. Both additive laws follow from the ring laws of `T`.
impl<T: RationalScalar> Associative<Additive> for Rational<T> {}
impl<T: RationalScalar> Commutative<Additive> for Rational<T> {}
impl<T: RationalScalar> Distributive for Rational<T> {}
// Reached through the `AbelianGroup` blanket now that the additive markers are present.
// `0/1 · a/b = 0/b`, which reduces to `0/1`. In a ring this is a theorem, but `Semiring` takes it
// as an axiom — the derivation needs an additive inverse — so it is stated rather than assumed.
impl<T: RationalScalar> Annihilating for Rational<T> {}
impl<T: RationalScalar> Invertible for Rational<T> {}

// ℚ is a field, so it has no zero divisors: `a·b = 0` with `a ≠ 0` gives `b = a⁻¹·a·b = 0`.
impl<T: RationalScalar> IntegralDomain for Rational<T> {}

// ℚ has characteristic zero: `n · 1` is the fraction `n/1`, which is zero only for `n = 0`.
// Stated per type rather than blanket-implemented, because it is a promise the compiler cannot
// check.
//
// This is what admits `Rational<T>` to the operations that divide by an integer, and what keeps 𝔽₂
// out of them. See `deep_causality_algebra::DivisibleByIntegers`.
impl<T: RationalScalar> Characteristic for Rational<T> {
    const CHARACTERISTIC: u32 = 0;
}

impl<T: RationalScalar> DivisibleByIntegers for Rational<T> {}