harness_algebra/modules/trivial.rs
1//! # Trivial Module
2//!
3//! This module provides an implementation of a trivial module over an arbitrary ring.
4//!
5//! ## Mathematical Background
6//!
7//! In abstract algebra, a trivial module is a module that contains exactly one element:
8//! the zero element. All operations on this element return the zero element itself:
9//!
10//! - Addition: $0 + 0 = 0$
11//! - Negation: $-0 = 0$
12//! - Scalar multiplication: $r \cdot 0 = 0$ for any ring element $r$
13//!
14//! ## Properties
15//!
16//! The trivial module satisfies all module axioms in the simplest possible way:
17//!
18//! - It forms an abelian group under addition (with only one element)
19//! - Scalar multiplication is distributive over addition
20//! - Scalar multiplication is compatible with ring multiplication
21//!
22//! ## Use Cases
23//!
24//! The trivial module serves several purposes:
25//!
26//! - As a base case in recursive constructions and mathematical proofs
27//! - To represent the kernel or image of certain module homomorphisms
28//! - For testing module-related algorithms with the simplest possible input
29//! - As a terminal object in the category of R-modules
30//!
31//! ## Example
32//!
33//! ```
34//! use harness_algebra::{modules::trivial::TrivialModule, prelude::*};
35//!
36//! // Create a trivial module over the integers
37//! let m1: TrivialModule<i32> = TrivialModule::zero();
38//! let m2: TrivialModule<i32> = TrivialModule::zero();
39//!
40//! // All operations return the same element
41//! assert_eq!(m1 + m2, m1);
42//! assert_eq!(m1 * 42, m1);
43//! assert_eq!(-m1, m1);
44//! ```
45
46use super::*;
47
48/// A trivial module over a ring.
49///
50/// This is a simple implementation of a module that has only one element.
51/// It's useful as a base case or for testing purposes.
52#[derive(Clone, Copy, Default, Eq, PartialEq, Debug)]
53pub struct TrivialModule<R> {
54 pub(crate) _r: PhantomData<R>,
55}
56
57impl<R> Add for TrivialModule<R> {
58 type Output = Self;
59
60 fn add(self, _: Self) -> Self::Output { Self { _r: PhantomData } }
61}
62
63impl<R> AddAssign for TrivialModule<R> {
64 fn add_assign(&mut self, _: Self) {}
65}
66
67impl<R> Sub for TrivialModule<R> {
68 type Output = Self;
69
70 fn sub(self, _: Self) -> Self::Output { Self { _r: PhantomData } }
71}
72
73impl<R> SubAssign for TrivialModule<R> {
74 fn sub_assign(&mut self, _: Self) {}
75}
76
77impl<R> Neg for TrivialModule<R> {
78 type Output = Self;
79
80 fn neg(self) -> Self::Output { Self { _r: PhantomData } }
81}
82
83impl<R> Mul<R> for TrivialModule<R> {
84 type Output = Self;
85
86 fn mul(self, _: R) -> Self::Output { Self { _r: PhantomData } }
87}
88
89impl<R> Zero for TrivialModule<R> {
90 fn zero() -> Self { Self { _r: PhantomData } }
91
92 fn is_zero(&self) -> bool { true }
93}
94
95impl<R: Ring> Additive for TrivialModule<R> {}
96
97impl<R> Group for TrivialModule<R> {
98 fn identity() -> Self { Self { _r: PhantomData } }
99
100 fn inverse(&self) -> Self { Self { _r: PhantomData } }
101}
102
103impl<R: Ring> AbelianGroup for TrivialModule<R> {}
104impl<R: Ring + Mul<Self>> LeftModule for TrivialModule<R> {
105 type Ring = R;
106}
107
108impl<R: Ring + Mul<Self>> RightModule for TrivialModule<R> {
109 type Ring = R;
110}
111
112impl<R: Ring + Mul<Self>> TwoSidedModule for TrivialModule<R> {
113 type Ring = R;
114}