1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//!
//! Algebraic _additive_ _monoid_ traits.
//!
//! An algebraic _additive_ _monoid_ is an _additive_ _semigroup_
//! `S`, with a unique additive _identity_ element, called _zero_,
//! and denoted `0`.
//!
//! # Axioms
//!
//! ```notrust
//! ∀x ∈ S
//!
//! Identity: ∃0 ∈ S: 0 + x = x + 0 = x.
//! ```
//!
//! # References
//!
//! See [references] for a formal definition of an additive monoid.
//!
#![doc(include = "../doc/references.md")]

use semigroup::add_semigroup::*;


///
/// An algebraic _additive monoid_.
///
pub trait AddMonoid: AddSemigroup {

  /// Unique zero (additive identity) element. Zero is ideally a
  /// `const` value, but the `const` rules make it too difficult to
  /// create `const` instances for many third party types.
  fn zero() -> Self;


  /// Test for the zero (additive identity) element.
  fn is_zero(&self) -> bool {
    *self == Self::zero()
  }


  /// Test the left additive identity axiom.
  fn axiom_left_add_identity(x: &Self) -> bool {
    Self::zero().add(x) == *x
  }


  /// Test the right additive identity axiom.
  fn axiom_right_add_identity(&self) -> bool {
    self.add(&Self::zero()) == Self::zero()
  }
}


///
/// A "numeric" algebraic _additive monoid_.
///
/// `NumAddMonoid` trait is for types that only form additive
/// monoids when "numeric" comparisons are used, e.g. floating point
/// types.
///
pub trait NumAddMonoid: NumAddSemigroup {

  /// Unique zero (additive identity) element. Zero is ideally a
  /// `const` value, but the `const` rules make it too difficult to
  /// create `const` instances for many third party types.
  fn zero() -> Self;


  /// Test for the zero (additive identity) element.
  fn is_zero(&self) -> bool {
    *self == Self::zero()
  }


  /// Numerically test the left additive identity axiom.
  fn axiom_left_add_identity(&self, eps: &Self::Error) -> bool {
    Self::zero().add(self).num_eq(&Self::zero(), eps)
  }


  /// Numerically test the right additive identity axiom.
  fn axiom_right_add_identity(&self, eps: &Self::Error) -> bool {
    self.add(&Self::zero()).num_eq(&Self::zero(), eps)
  }
}


///
/// Trait implementation macro for integer types.
///
/// A macro used to avoid writing repetitive, boilerplate
/// `AddMonoid` implementations for built-in integer types.
/// Probably not needed if Rust had an `Integer` super-trait.
///
macro_rules! integer_add_monoid {
  ($type:ty) => {
    impl AddMonoid for $type {

      /// Zero is just integer zero.
      fn zero() -> Self {
        0
      }
    }
  };

  ($type:ty, $($others:ty),+) => {
    integer_add_monoid! {$type}
    integer_add_monoid! {$($others),+}
  };
}


// Unsigned integer additive monoids.
integer_add_monoid! {
  u8, u16, u32, u64, u128, usize
}


// Signed integer additive monoids.
integer_add_monoid! {
  i8, i16, i32, i64, i128, isize
}


///
/// Trait implementation macro for floating point types.
///
/// A macro used to avoid writing repetitive, boilerplate
/// `NumAddMonoid` implementations for built-in floating point
/// types.  Probably not needed if Rust had a `Float` super-trait.
///
macro_rules! float_add_monoid {
  ($type:ty) => {
    impl NumAddMonoid for $type {

      /// Zero is just floating point zero.
      fn zero() -> Self {
        0.0
      }
    }
  };

  ($type:ty, $($others:ty),+) => {
    float_add_monoid! {$type}
    float_add_monoid! {$($others),+}
  };
}


// Floating point additive monoids.
float_add_monoid! {
  f32, f64
}


// Module unit tests are in a separate file.
#[cfg(test)]
#[path = "add_monoid_test.rs"]
mod add_monoid_test;