fp_library/types/
additive.rs1#[fp_macros::document_module]
19mod inner {
20 use {
21 crate::classes::*,
22 fp_macros::*,
23 };
24
25 #[document_examples]
30 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
41 #[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
42 pub struct Additive<A>(
43 pub A,
45 );
46
47 #[document_type_parameters("The semiring type.")]
48 impl<A: Semiring> Semigroup for Additive<A> {
49 #[document_signature]
51 #[document_parameters("The first additive value.", "The second additive value.")]
53 #[document_returns("The sum wrapped in `Additive`.")]
55 #[document_examples]
56 fn append(
66 a: Self,
67 b: Self,
68 ) -> Self {
69 Additive(A::add(a.0, b.0))
70 }
71 }
72
73 #[document_type_parameters("The semiring type.")]
74 impl<A: Semiring> Monoid for Additive<A> {
75 #[document_signature]
77 #[document_returns("The additive identity wrapped in `Additive`.")]
79 #[document_examples]
80 fn empty() -> Self {
90 Additive(A::zero())
91 }
92 }
93}
94
95pub use inner::*;
96
97#[cfg(test)]
98mod tests {
99 use {
100 super::*,
101 crate::functions::*,
102 quickcheck_macros::quickcheck,
103 };
104
105 #[quickcheck]
106 fn semigroup_associativity(
107 a: i32,
108 b: i32,
109 c: i32,
110 ) -> bool {
111 let x = Additive(a);
112 let y = Additive(b);
113 let z = Additive(c);
114 append(x, append(y, z)) == append(append(x, y), z)
115 }
116
117 #[quickcheck]
118 fn monoid_left_identity(a: i32) -> bool {
119 let x = Additive(a);
120 append(empty::<Additive<i32>>(), x) == x
121 }
122
123 #[quickcheck]
124 fn monoid_right_identity(a: i32) -> bool {
125 let x = Additive(a);
126 append(x, empty::<Additive<i32>>()) == x
127 }
128}