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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
//! # Monoid Trait
//!
//! This module provides the Monoid trait, which extends `Semigroup` to add an identity element.
//!
//! A monoid extends a semigroup by providing an identity element that, when combined with any other
//! element, returns that element unchanged. This makes monoids particularly useful for operations
//! like addition (identity: 0), multiplication (identity: 1), and string concatenation (identity: empty string).
//!
//! ## Example
//!
//! ```rust
//! use rustica::traits::monoid::Monoid;
//! use rustica::traits::semigroup::Semigroup;
//!
//! // String monoid under concatenation
//! let s1 = String::from("Hello, ");
//! let s2 = String::from("world!");
//! let s3 = s1.clone().combine_owned(s2.clone());
//! assert_eq!(s3, "Hello, world!");
//!
//! // The empty string is the identity element
//! let empty = String::empty();
//! assert_eq!(s1.clone().combine_owned(empty.clone()), s1.clone());
//! assert_eq!(empty.combine_owned(s2.clone()), s2);
//! ```
//!
//! ## Extension Trait
//!
//! The `MonoidExt` trait adds extension methods to all types implementing Monoid.
use crateSemigroup;
/// A Monoid is a Semigroup with an identity element.
///
/// # Mathematical Definition
///
/// A monoid is an algebraic structure consisting of:
/// - A set `M`
/// - An associative binary operation `combine: M × M → M`
/// - An identity element `empty() ∈ M`
///
/// # Laws
///
/// For any value `x` of type implementing Monoid:
/// ```text
/// x.combine(&Self::empty()) = x // Right identity
/// Self::empty().combine(&x) = x // Left identity
/// ```
///
/// Additionally, since Monoid extends `Semigroup`, the associativity law must hold:
/// ```text
/// (a.combine(&b)).combine(&c) = a.combine(&b.combine(&c))
/// ```
///
/// # Examples
///
/// ```rust
/// use rustica::traits::monoid::Monoid;
/// use rustica::traits::semigroup::Semigroup;
///
/// // String monoid under concatenation
/// let hello = String::from("Hello");
/// let empty_string = String::empty();
///
/// // Owned value identity laws
/// assert_eq!(hello.clone().combine_owned(empty_string.clone()), hello.clone()); // Right identity
/// assert_eq!(String::empty().combine_owned(hello.clone()), hello.clone()); // Left identity
///
/// // Reference identity laws
/// assert_eq!(hello.combine(&empty_string), hello.clone()); // Right identity
/// assert_eq!(empty_string.combine(&hello), hello); // Left identity
///
/// // Vec monoid under concatenation
/// let numbers = vec![1, 2, 3];
/// let empty_vec = Vec::<i32>::empty();
///
/// // Owned value identity laws
/// assert_eq!(numbers.clone().combine_owned(empty_vec.clone()), numbers.clone()); // Right identity
/// assert_eq!(Vec::<i32>::empty().combine_owned(numbers.clone()), numbers.clone()); // Left identity
///
/// // Reference identity laws
/// assert_eq!(numbers.combine(&empty_vec), numbers.clone()); // Right identity
/// assert_eq!(empty_vec.combine(&numbers), numbers); // Left identity
/// ```
///
/// # Common Use Cases
///
/// Monoids are particularly useful in scenarios where:
/// - You need to combine elements with a neutral element
/// - You're implementing data structures that require an empty state
/// - You're working with collection types
/// - You need to perform parallel or distributed computations
///
/// Some common monoids include:
/// - Strings under concatenation (empty string as identity)
/// - Lists/Vectors under concatenation (empty list as identity)
/// - Numbers under addition (0 as identity)
/// - Numbers under multiplication (1 as identity)
/// - Booleans under conjunction (true as identity)
/// - Booleans under disjunction (false as identity)
// Implement Monoid for Vec
// Implement Monoid for String
/// Utility function to combine all monoid values with the identity element
///
/// This function takes an iterator of monoid values and combines them all,
/// returning the identity element if the iterator is empty.
///
/// # Type Parameters
///
/// * `M` - A type implementing Monoid
/// * `I` - An iterator type yielding values of type `M`
///
/// # Returns
///
/// The combined result of all values or the identity element if the iterator is empty
///
/// # Examples
///
/// ```rust
/// use rustica::traits::monoid::{self, Monoid};
/// use rustica::traits::semigroup::Semigroup;
///
/// // Combining strings
/// let strings = vec![String::from("Hello"), String::from(" "), String::from("World")];
/// let result = monoid::combine_all(strings);
/// assert_eq!(result, String::from("Hello World"));
///
/// // Empty iterator returns the identity element
/// let empty: Vec<String> = vec![];
/// let result = monoid::combine_all(empty);
/// assert_eq!(result, String::empty());
/// ```
/// A trait providing extension methods for monoid operations
///
/// This trait is automatically implemented for all types that implement Monoid.
/// Creates a monoid by repeating an element a specified number of times.
///
/// This function creates a new monoid value by combining n copies of the provided value.
/// If n is 0, it returns the identity element.
/// If n is 1, returns the value itself.
/// For exponents > 1, combines the value with itself that many times.
///
/// # Type Parameters
///
/// * `M` - A type implementing Monoid
///
/// # Arguments
///
/// * `value` - The value to repeat
/// * `n` - Number of times to repeat the value
///
/// # Returns
///
/// A new monoid value consisting of n copies of the input combined together
///
/// # Examples
///
/// ```rust
/// use rustica::traits::monoid::{self, Monoid};
/// use rustica::traits::semigroup::Semigroup;
///
/// // Repeating a string
/// let hello = String::from("Hello");
/// let repeated = monoid::repeat(hello, 3);
/// assert_eq!(repeated, "HelloHelloHello");
///
/// // Zero repetitions returns the identity
/// let zero_repeat = monoid::repeat(String::from("World"), 0);
/// assert_eq!(zero_repeat, String::empty());
/// ```
/// Combines a slice of monoid values.
///
/// This function takes a slice of monoid values and combines them all,
/// returning the identity element if the slice is empty.
///
/// # Type Parameters
///
/// * `M` - A type implementing Monoid
///
/// # Arguments
///
/// * `values` - A slice of monoid values to combine
///
/// # Returns
///
/// The combined result of all values in the slice or the identity element if the slice is empty
///
/// # Examples
///
/// ```rust
/// use rustica::traits::monoid::{self, Monoid};
/// use rustica::traits::semigroup::Semigroup;
///
/// // Combining strings
/// let strings = [
/// String::from("Hello"),
/// String::from(" "),
/// String::from("World")
/// ];
/// let result = monoid::mconcat(&strings);
/// assert_eq!(result, "Hello World");
///
/// // Empty slice returns the identity element
/// let empty: [String; 0] = [];
/// let result = monoid::mconcat(&empty);
/// assert_eq!(result, String::empty());
/// ```
/// Creates a monoid that is combined with itself a specified number of times (raised to a power).
///
/// If the exponent is 0, returns the identity element.
/// If the exponent is 1, returns the value itself.
/// For exponents > 1, combines the value with itself that many times.
///
/// # Type Parameters
///
/// * `M` - A type implementing Monoid
///
/// # Arguments
///
/// * `value` - The base value
/// * `exponent` - The power to raise the value to
///
/// # Returns
///
/// The value combined with itself `exponent` times
///
/// # Examples
///
/// ```rust
/// use rustica::traits::monoid::{self, Monoid};
/// use rustica::traits::semigroup::Semigroup;
///
/// // String power
/// let base = String::from("ab");
/// assert_eq!(monoid::power(base.clone(), 0), String::empty());
/// assert_eq!(monoid::power(base.clone(), 1), "ab");
/// assert_eq!(monoid::power(base.clone(), 3), "ababab");
///
/// // Vec power
/// let nums = vec![1, 2];
/// assert_eq!(monoid::power(nums.clone(), 0), Vec::<i32>::empty());
/// assert_eq!(monoid::power(nums.clone(), 1), vec![1, 2]);
/// assert_eq!(monoid::power(nums.clone(), 3), vec![1, 2, 1, 2, 1, 2]);
/// ```