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
use super::*;

///
///A [monoid](MulMonoid) constructed from free multiplication of elements of a set
///
///Concretely, given a set `C`, we construct the free-monoid of `C` as the set of all finite lists
///of members of `C` where multiplication is given by concatenation. In other words, it's basically
///[`Vec<C>`](Vec) but with `a*b == {a.append(&mut b); a}`.
///
///# Examples
///```
///use maths_traits::algebra::One;
///use free_algebra::FreeMonoid;
///
///let s1 = FreeMonoid::one() * 'a' * 'b' * 'c';
///let s2 = s1.clone().reverse();
///
///assert_eq!(s1, ['a','b','c']);
///assert_eq!(s2, ['c','b','a']);
///
///let mul = s1 * s2;
///
///assert_eq!(mul, ['a','b','c','c','b','a']);
///
///```
///
pub type FreeMonoid<C> = MonoidalString<C,()>;

impl<C> AssociativeMonoidRule<C> for () {}
impl<C> MonoidRule<C> for () {
    fn apply(mut string: Vec<C>, letter: C) -> Vec<C> {string.push(letter); string}
    fn apply_many(mut string1: Vec<C>, mut string2: Vec<C>) -> Vec<C> {
        string1.append(&mut string2); string1
    }
    fn apply_iter<I:Iterator<Item=C>>(mut string: Vec<C>, letters: I) -> Vec<C> {
        string.extend(letters); string
    }
}