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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
use super::*;
use super::literal::*;
use super::short_free_group_term::ShortFreeGroupTerm;
use std::ops::{Mul, MulAssign};

mod parse_free_group_term;

/// An element of the group algebra.
/// 
/// # Examples
/// To use this, import
/// ```
/// use l_group_formulas::free_group_term::*;
/// use l_group_formulas::literal::*;
/// use l_group_formulas::*;
/// ```
/// Constructing a free group term goes like this.
/// ```
/// # use l_group_formulas::free_group_term::*;
/// # use l_group_formulas::literal::*;
/// let term = Literal::from('x') * Literal::from('y');
/// ```
/// In this case, `term` encodes the element xy of a free group.
/// 
/// Terms constructed using `FreeGrouPTerm::new` automatically get reduced:
/// ```
/// # use l_group_formulas::free_group_term::*;
/// # use l_group_formulas::literal::*;
/// # use l_group_formulas::*;
/// let term = Literal::from('x') * Literal::from('x').inverse();
/// assert_eq!(String::from("e"), term.to_string());
/// ```
/// 
/// Can also be constructed from strings:
/// `FreeGroupTerm::from(&str)` parses a free group term from a string.
/// 
/// This ignores all non-alphanumeric characters, such as `*`. Perhaps dangerously,
/// this also ignores symbols like `^`, and treats `v` as the name of a symbol.
/// The input is parsed by 
/// 
/// # Examples
/// Basic usage:
/// ```
/// use l_group_formulas::literal::Literal;
/// use l_group_formulas::free_group_term::FreeGroupTerm;
/// // this is equivalent to: 
/// // let string = "X31yz39";
/// let string = "X3 1*yz39 ";
/// let x = Literal::new('x', 31, true);
/// let y = Literal::new('y', 0, false);
/// let z = Literal::new('z', 39, false);
/// let term = FreeGroupTerm::new(vec![x, y, z]);
/// assert_eq!(term, FreeGroupTerm::from(string));
/// ```
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct FreeGroupTerm {
    pub literals: Vec<literal::Literal>
}

impl FreeGroupTerm {
    pub fn new(literals: Vec<literal::Literal>) -> FreeGroupTerm {
        FreeGroupTerm { literals: literals }.reduced()
    }
}

pub const FREE_GROUP_IDENTITY: FreeGroupTerm = FreeGroupTerm { literals: vec![] };

impl From<Literal> for FreeGroupTerm {
    fn from(x: Literal) -> FreeGroupTerm {
        FreeGroupTerm::new(vec![x])
    }
}

impl From<char> for FreeGroupTerm {
    fn from(c: char) -> FreeGroupTerm {
        FreeGroupTerm::from(Literal::from(c))
    }
}

impl From<&str> for FreeGroupTerm {
    fn from(s: &str) -> FreeGroupTerm {
        let result = parse_free_group_term::parse(s);
        match result {
            Ok(t) => t,
            Err(e) => panic!(e)
        }
    }
}

impl From<ShortFreeGroupTerm> for FreeGroupTerm {
    fn from(t: ShortFreeGroupTerm) -> FreeGroupTerm {
        match (t.left, t.mid, t.right) {
            (None, None, None) => FreeGroupTerm::new(Vec::new()),
            (Some(x), None, None) => FreeGroupTerm::from(x),
            (Some(x), Some(y), None) => x * y,
            (Some(x), Some(y), Some(z)) => x * y * z,
            _ => panic!("Invalid ShortFreeGroupTerm")
        }
    }
}

impl Term for FreeGroupTerm {
    fn inverse(&self) -> FreeGroupTerm {
        let mut result = Vec::new();
        for x in &self.literals {
            result.push(x.inverse())
        }
        result.reverse();
        FreeGroupTerm {
            literals: result
        }
    }
}

impl Mul for FreeGroupTerm {
    type Output = FreeGroupTerm;
    
    fn mul(self, other: FreeGroupTerm) -> FreeGroupTerm {
        if self.literals.len() == 0 { other } 
        else if other.literals.len() == 0 { self } 
        else {
            FreeGroupTerm::new([&self.literals[..], &other.literals[..]].concat())
        }
    }
}

impl Mul<Literal> for FreeGroupTerm {
    type Output = FreeGroupTerm;

    fn mul(self, other: Literal) -> FreeGroupTerm {
        self * FreeGroupTerm::from(other)
    }
}

impl MulAssign for FreeGroupTerm {
    fn mul_assign(&mut self, rhs: FreeGroupTerm) {
        for x in rhs.literals {
            self.literals.push(x);
        }
        *self = FreeGroupTerm::new(self.literals.clone());
    }
}

impl ToString for FreeGroupTerm {
    fn to_string(&self) -> String {
        let mut result = String::from("");
        for l in &self.literals {
            result.push_str(&l.to_string());
        }
        if result == "" {
            return String::from("e");
        }
        return result;
    }
}


impl Reducable for FreeGroupTerm {
    fn reduced(self) -> FreeGroupTerm {
        let mut index: usize = 0;
        let mut literals = self.literals.clone();
        
        enum ReducingState {
            ReducedInBeginning,
            ReducedElsewhere,
            DidNotReduce
        }

        while literals.len() > 0 && index < literals.len() - 1 {
            let mut reducing_state = ReducingState::DidNotReduce;
            if literals[index] == literals[index + 1].inverse() {
                literals.remove(index);
                literals.remove(index);
                match index {
                    0 | 1 => reducing_state = ReducingState::ReducedInBeginning,
                    _     => reducing_state = ReducingState::ReducedElsewhere
                };
            }
            index = match reducing_state {
                ReducingState::ReducedInBeginning => 0,
                ReducingState::ReducedElsewhere   => index - 1,
                ReducingState::DidNotReduce       => index + 1
            };
        }
        return FreeGroupTerm { literals: (literals).to_vec() }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

     #[test]
    fn test_reduce() {
        let x = Literal::from('x');
        let x_inv = Literal::from('x').inverse();
        let y = Literal::from('y');
        
        let result = FreeGroupTerm { literals: vec![x, x_inv, y] };
        assert_eq!(FreeGroupTerm { literals: vec![y] }, result.reduced());

        let x = Literal::from('x');
        let y = Literal::from('y');
        let z = Literal::from('z');
        let result = FreeGroupTerm { literals: vec![x, y, z, z.inverse(), y.inverse(), x.inverse()]};
        assert_eq!(FreeGroupTerm::new(vec![]), result.reduced());
    }

    #[test]
    fn test_to_string() {
        let term = FreeGroupTerm::new(vec![Literal::from('x'), Literal::from('y'), Literal::from('z')]);
        assert_eq!("xyz", term.to_string());
    }

    #[test]
    fn test_inverse() {
        let x = Literal::from('x');
        let y = Literal::from('y');
        let z = Literal::from('z');
        let term = FreeGroupTerm { literals: vec![x,y,z] };
        let other_term = FreeGroupTerm { literals: vec![z.inverse(), y.inverse(), x.inverse()] };
        assert_eq!(other_term, term.inverse())
    }

    #[test]
    fn test_mul() {
        let x = FreeGroupTerm::new(vec![Literal::from('x')]);
        let x_inv = FreeGroupTerm::new(vec![Literal::from('x').inverse()]);
        assert_eq!(FREE_GROUP_IDENTITY, x*x_inv);
    }

    #[test]
    fn test_mul_assign() {
        let mut term = FreeGroupTerm::from(Literal::from('x').inverse());
        term *= FreeGroupTerm::from(Literal::from('x'));
        assert_eq!(FreeGroupTerm::new(Vec::new()), term);
    }
}