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
//! # Natural Numbers
//!
//! `natural_numbers` provides natural numbers and operations on them, based on the successor operation.

/// Trait for types implementing natural numbers.
pub trait Nat
where
    Self: Sized + Clone,
{
    /// Converts a value of type Nat into the corresponding value of type u32.
    /// # Examples
    /// ```
    /// use natural_numbers::{Nat,U32Nat};
    ///
    /// let zero = U32Nat::zero();
    /// assert_eq!(zero.value(), 0);
    /// ```
    fn value(&self) -> u32;

    /// Converts a value of type u32 into the corresponding value of type Nat.
    /// # Examples
    /// ```
    /// use natural_numbers::{Nat,U32Nat};
    ///
    /// assert_eq!(U32Nat::wrap(0), U32Nat::zero());
    /// ```
    fn wrap(n: u32) -> Self;

    fn zero() -> Self;

    fn is_zero(&self) -> bool;

    /// The successor of a value.
    /// # Examples
    /// ```
    /// use natural_numbers::{Nat,U32Nat};
    ///
    /// assert_eq!(U32Nat::wrap(1), U32Nat::zero().succ());
    /// ```
    fn succ(&self) -> Self;

    /// The value of which this is a successor, or None if the value is zero.
    /// # Examples
    /// ```
    /// use natural_numbers::{Nat,U32Nat};
    ///
    /// let one = U32Nat::zero().succ();
    /// assert_eq!(one.pred().unwrap(), U32Nat::zero());
    /// ```
    fn pred(&self) -> Option<Self> {
        match self.value() {
            0 => None,
            n => Some(Self::wrap(n - 1)),
        }
    }

    /// The sum of two values.
    /// # Examples
    /// ```
    /// use natural_numbers::{Nat,U32Nat};
    ///
    /// let two = U32Nat::wrap(2);
    /// let three = U32Nat::wrap(3);
    /// let five = U32Nat::wrap(5);
    /// assert_eq!(two.add(&three), five);
    fn add(&self, n: &Self) -> Self {
        match n.pred() {
            None => self.clone(),
            Some(m) => self.add(&m).succ(),
        }
    }
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct U32Nat(u32);

// impl Ord for U32Nat {
//     fn cmp(&self, other: &Self) -> Ordering {
//         match other.pred() {
//             None => match self.pred() {
//                 None => Ordering::Equal,
//                 Some(_) => Ordering::Greater,
//             },
//             Some(n) => match self.pred() {
//                 None => Ordering::Less,
//                 Some(m) => m.cmp(&n),
//             },
//         }
//     }
// }

impl Nat for U32Nat {
    fn value(&self) -> u32 {
        self.0
    }

    fn wrap(n: u32) -> Self {
        U32Nat(n)
    }

    fn zero() -> Self {
        U32Nat(0)
    }

    fn is_zero(&self) -> bool {
        self.0 == 0
    }

    fn succ(&self) -> Self {
        U32Nat(self.0 + 1)
    }
}