Skip to main content

fp_library/classes/
commutative_ring.rs

1//! Types where multiplication is commutative.
2//!
3//! ### Examples
4//!
5//! ```
6//! use fp_library::classes::Semiring;
7//!
8//! // CommutativeRing guarantees: multiply(a, b) = multiply(b, a)
9//! assert_eq!(i32::multiply(3, 7), i32::multiply(7, 3));
10//! ```
11
12#[fp_macros::document_module]
13mod inner {
14	use {
15		crate::classes::*,
16		fp_macros::*,
17	};
18
19	/// A marker trait for [`Ring`] types where multiplication is commutative.
20	///
21	/// ### Laws
22	///
23	/// * Commutativity: `multiply(a, b) = multiply(b, a)`
24	#[document_examples]
25	///
26	/// ```
27	/// use fp_library::classes::Semiring;
28	///
29	/// assert_eq!(i32::multiply(3, 7), i32::multiply(7, 3));
30	/// ```
31	pub trait CommutativeRing: Ring {}
32
33	macro_rules! impl_commutative_ring {
34		($($t:ty),+) => {
35			$(impl CommutativeRing for $t {})+
36		};
37	}
38
39	impl_commutative_ring!(i8, i16, i32, i64, i128, isize, f32, f64);
40}
41
42pub use inner::*;