arcweight 0.3.0

A high-performance, modular library for weighted finite state transducers with comprehensive examples and benchmarks
Documentation
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//! Boolean semiring implementation.
//!
//! This module provides the Boolean semiring $`(\{0, 1\}, \vee, \wedge, 0, 1)`$
//! for unweighted automata and recognition problems where addition is logical OR
//! and multiplication is logical AND.
//!
//! # References
//!
//! - Kuich, W., & Salomaa, A. (1986). *Semirings, Automata, Languages*. EATCS
//!   Monographs on Theoretical Computer Science, Vol. 5. Springer-Verlag.

use super::traits::*;
use core::fmt;
use core::ops::{Add, Mul};
use core::str::FromStr;
use num_traits::{One, Zero};

/// Boolean weight for recognition and reachability analysis.
///
/// The Boolean semiring provides the simplest non-trivial semiring structure,
/// corresponding to unweighted finite state automata and basic recognition problems.
/// In this semiring, "addition" implements logical OR (path existence), while
/// "multiplication" implements logical AND (path conjunction).
///
/// # Mathematical Definition
///
/// | Operation | Definition |
/// |-----------|------------|
/// | $`a \oplus b`$ | $`a \vee b`$ (logical OR) |
/// | $`a \otimes b`$ | $`a \wedge b`$ (logical AND) |
/// | $`\bar{0}`$ | $`\mathrm{false}`$ |
/// | $`\bar{1}`$ | $`\mathrm{true}`$ |
/// | $`a^*`$ | $`\mathrm{true}`$ |
///
/// # Algebraic Properties
///
/// - **Commutative:** Both $`\oplus`$ and $`\otimes`$ are commutative
/// - **Idempotent:** $`a \oplus a = a`$ and $`a \otimes a = a`$
/// - **Path:** $`a \oplus b \in \{a, b\}`$
/// - **Naturally Ordered:** Forms a complete Boolean lattice
/// - **Star Semiring:** Kleene closure is always $`\bar{1}`$
///
/// # Mathematical Semantics
///
/// - **Value Range:** Boolean values $`\{0, 1\} \cong \{\bot, \top\}`$
/// - **Addition ($`\oplus`$):** $`a \vee b`$ (logical OR) - combines alternative paths
/// - **Multiplication ($`\otimes`$):** $`a \wedge b`$ (logical AND) - requires all conditions
/// - **Zero ($`\bar{0}`$):** $`\mathrm{false}`$ - represents rejection/failure/no path
/// - **One ($`\bar{1}`$):** $`\mathrm{true}`$ - represents acceptance/success/valid path
///
/// # Use Cases
///
/// ## Regular Expression Matching
/// ```rust
/// use arcweight::prelude::*;
///
/// // Build pattern matcher FST
/// let mut fst = VectorFst::<BooleanWeight>::new();
/// let s0 = fst.add_state();
/// let s1 = fst.add_state();
///
/// fst.set_start(s0);
/// fst.set_final(s1, BooleanWeight::one());  // Accept state
///
/// // Add pattern transition: match 'a'
/// fst.add_arc(s0, Arc::new(
///     'a' as u32,
///     'a' as u32,
///     BooleanWeight::one(),  // Valid transition
///     s1
/// ));
///
/// // The FST now accepts strings containing 'a'
/// ```
///
/// ## Graph Reachability
/// ```rust
/// use arcweight::prelude::*;
///
/// // Check if path exists between nodes
/// let path_exists = BooleanWeight::new(true);
/// let no_path = BooleanWeight::new(false);
///
/// // Combine multiple path possibilities
/// let reachable = path_exists
///     .plus(&no_path)
///     .plus(&path_exists);  // true ∨ false ∨ true = true
///
/// // Check if all intermediate nodes are valid
/// let validᵢntermediate = BooleanWeight::new(true);
/// let path_validity = reachable.times(&validᵢntermediate);  // true ∧ true = true
/// ```
///
/// ## Set Membership Testing
/// ```rust
/// use arcweight::prelude::*;
///
/// // Dictionary lookup FST
/// let word_exists = BooleanWeight::new(true);   // Word found in dictionary
/// let word_missing = BooleanWeight::new(false); // Word not found
///
/// // Multiple dictionary checks
/// let found_anywhere = word_missing
///     .plus(&word_exists);  // false ∨ true = true (found in at least one)
///
/// // Require presence in multiple dictionaries
/// let found_everywhere = word_exists
///     .times(&word_exists); // true ∧ true = true (found in all)
/// ```
///
/// ## Constraint Satisfaction
/// ```rust
/// use arcweight::prelude::*;
///
/// // Grammar rule satisfaction
/// let rule1_satisfied = BooleanWeight::new(true);
/// let rule2_satisfied = BooleanWeight::new(false);
/// let rule3_satisfied = BooleanWeight::new(true);
///
/// // All rules must be satisfied (conjunction)
/// let all_rules = rule1_satisfied
///     .times(&rule2_satisfied)
///     .times(&rule3_satisfied);  // true ∧ false ∧ true = false
///
/// // At least one rule satisfied (disjunction)
/// let any_rule = rule1_satisfied
///     .plus(&rule2_satisfied)
///     .plus(&rule3_satisfied);   // true ∨ false ∨ true = true
/// ```
///
/// # Working with FSTs
///
/// ```rust
/// use arcweight::prelude::*;
///
/// let true_val = BooleanWeight::new(true);
/// let false_val = BooleanWeight::new(false);
///
/// // Addition is logical OR (alternative paths)
/// let or_result = true_val + false_val;
/// assert_eq!(or_result, BooleanWeight::new(true));
///
/// // Multiplication is logical AND (path conjunction)
/// let and_result = true_val * false_val;
/// assert_eq!(and_result, BooleanWeight::new(false));
///
/// // Idempotency properties
/// assert_eq!(true_val + true_val, true_val);   // true ∨ true = true
/// assert_eq!(false_val * false_val, false_val); // false ∧ false = false
///
/// // Identity elements
/// assert_eq!(BooleanWeight::zero(), BooleanWeight::new(false));  // Additive identity
/// assert_eq!(BooleanWeight::one(), BooleanWeight::new(true));    // Multiplicative identity
/// ```
///
/// # Algebraic Properties
///
/// The Boolean semiring forms a complete Boolean algebra with important properties:
///
/// - **Idempotent:** `a ⊕ a = a` and `a ⊗ a = a`
/// - **Commutative:** `a ⊕ b = b ⊕ a` and `a ⊗ b = b ⊗ a`
/// - **Associative:** `(a ⊕ b) ⊕ c = a ⊕ (b ⊕ c)`
/// - **Distributive:** `a ⊗ (b ⊕ c) = (a ⊗ b) ⊕ (a ⊗ c)`
/// - **Absorptive:** `a ⊕ (a ⊗ b) = a` and `a ⊗ (a ⊕ b) = a`
/// - **Complemented:** Each element has a complement (negation)
///
/// # Performance Characteristics
///
/// - **Arithmetic:** Both OR and AND operations are O(1) bit operations
/// - **Memory:** 1 byte per weight (single bool with padding)
/// - **Comparison:** Extremely fast integer comparison
/// - **Hash/Eq:** Optimal for hash maps and sets
/// - **Specialized:** Can be bit-packed for massive state spaces
///
/// # Advanced Usage
///
/// ## Kleene Star
/// ```rust
/// use arcweight::prelude::*;
///
/// let weight = BooleanWeight::new(true);
/// let star_result = weight.star();  // Always true for non-zero elements
/// assert_eq!(star_result, BooleanWeight::one());
/// ```
///
/// ## Logical Operations
/// ```rust
/// use arcweight::prelude::*;
///
/// // De Morgan's laws can be implemented using semiring operations
/// let a = BooleanWeight::new(true);
/// let b = BooleanWeight::new(false);
///
/// // ¬(a ∧ b) ≡ ¬a ∨ ¬b (requires external negation function)
/// // Boolean semiring provides the base operations for logical reasoning
/// ```
///
/// # Integration with FST Algorithms
///
/// Boolean weights work optimally with all FST algorithms:
/// - **Composition:** Combines recognizers and transducers
/// - **Union:** Creates alternatives in recognition
/// - **Determinization:** Produces minimal recognizers
/// - **Shortest Path:** Finds any accepting path (since all have same weight)
///
/// # See Also
///
/// - [Core Concepts - Boolean Semiring](../../docs/core-concepts/semirings.md#boolean-semiring) for mathematical background
/// - [`TropicalWeight`](crate::semiring::TropicalWeight) for optimization problems
/// - [`compose()`](crate::algorithms::compose) for building complex recognizers from Boolean FSTs
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BooleanWeight(bool);

impl BooleanWeight {
    /// Create a new boolean weight
    pub const fn new(value: bool) -> Self {
        Self(value)
    }
}

impl fmt::Display for BooleanWeight {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let value = self.0;
        write!(f, "{value}")
    }
}

impl Zero for BooleanWeight {
    fn zero() -> Self {
        Self::new(false)
    }

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

impl One for BooleanWeight {
    fn one() -> Self {
        Self::new(true)
    }
}

impl Add for BooleanWeight {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        Self(self.0 || rhs.0)
    }
}

impl Mul for BooleanWeight {
    type Output = Self;

    fn mul(self, rhs: Self) -> Self::Output {
        Self(self.0 && rhs.0)
    }
}

impl Semiring for BooleanWeight {
    type Value = bool;

    fn new(value: Self::Value) -> Self {
        Self::new(value)
    }

    fn value(&self) -> &Self::Value {
        &self.0
    }

    fn properties() -> SemiringProperties {
        SemiringProperties {
            left_semiring: true,
            right_semiring: true,
            commutative: true,
            idempotent: true,
            path: true,
        }
    }
}

impl NaturallyOrderedSemiring for BooleanWeight {}

impl StarSemiring for BooleanWeight {
    fn star(&self) -> Self {
        Self::one()
    }
}

impl FromStr for BooleanWeight {
    type Err = std::str::ParseBoolError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        s.parse::<bool>().map(Self::new)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use num_traits::{One, Zero};

    #[test]
    fn test_boolean_weight_creation() {
        let w_true = BooleanWeight::new(true);
        let w_false = BooleanWeight::new(false);

        assert!(*w_true.value());
        assert!(!*w_false.value());
    }

    #[test]
    fn test_boolean_zero_one() {
        let zero = BooleanWeight::zero();
        let one = BooleanWeight::one();

        assert!(Semiring::is_zero(&zero));
        assert!(Semiring::is_one(&one));
        assert!(!*zero.value());
        assert!(*one.value());
    }

    #[test]
    fn test_boolean_addition() {
        let w_true = BooleanWeight::new(true);
        let w_false = BooleanWeight::new(false);

        // OR operation
        assert!(*w_true.plus(&w_false).value());
        assert!(*w_false.plus(&w_true).value());
        assert!(!*w_false.plus(&w_false).value());
        assert!(*w_true.plus(&w_true).value());
    }

    #[test]
    fn test_boolean_multiplication() {
        let w_true = BooleanWeight::new(true);
        let w_false = BooleanWeight::new(false);

        // AND operation
        assert!(!*w_true.times(&w_false).value());
        assert!(!*w_false.times(&w_true).value());
        assert!(!*w_false.times(&w_false).value());
        assert!(*w_true.times(&w_true).value());
    }

    #[test]
    fn test_boolean_idempotence() {
        let w_true = BooleanWeight::new(true);
        let w_false = BooleanWeight::new(false);

        // w + w = w (idempotent)
        assert_eq!(w_true.plus(&w_true), w_true);
        assert_eq!(w_false.plus(&w_false), w_false);
    }

    #[test]
    fn test_boolean_display() {
        let w_true = BooleanWeight::new(true);
        let w_false = BooleanWeight::new(false);

        assert_eq!(format!("{w_true}"), "true");
        assert_eq!(format!("{w_false}"), "false");
    }

    #[test]
    fn test_boolean_star() {
        let w_true = BooleanWeight::new(true);
        let w_false = BooleanWeight::new(false);

        // Star is always one for boolean semiring
        assert_eq!(w_true.star(), BooleanWeight::one());
        assert_eq!(w_false.star(), BooleanWeight::one());
    }

    #[test]
    fn test_boolean_properties() {
        let props = BooleanWeight::properties();
        assert!(props.left_semiring);
        assert!(props.right_semiring);
        assert!(props.commutative);
        assert!(props.idempotent);
        assert!(props.path);
    }

    #[test]
    fn test_boolean_from_str() {
        assert_eq!(
            BooleanWeight::from_str("true").unwrap(),
            BooleanWeight::new(true)
        );
        assert_eq!(
            BooleanWeight::from_str("false").unwrap(),
            BooleanWeight::new(false)
        );
    }

    #[test]
    fn test_boolean_operator_overloads() {
        let w_true = BooleanWeight::new(true);
        let w_false = BooleanWeight::new(false);

        // Test + operator (OR)
        assert_eq!(w_true + w_false, BooleanWeight::new(true));
        assert_eq!(w_false + w_false, BooleanWeight::new(false));

        // Test * operator (AND)
        assert_eq!(w_true * w_false, BooleanWeight::new(false));
        assert_eq!(w_true * w_true, BooleanWeight::new(true));
    }

    #[test]
    fn test_boolean_identity_laws() {
        let w = BooleanWeight::new(true);
        let zero = BooleanWeight::zero();
        let one = BooleanWeight::one();

        // Additive identity
        assert_eq!(w + zero, w);
        assert_eq!(zero + w, w);

        // Multiplicative identity
        assert_eq!(w * one, w);
        assert_eq!(one * w, w);

        // Annihilation by zero
        assert!(Semiring::is_zero(&(w * zero)));
        assert!(Semiring::is_zero(&(zero * w)));
    }

    #[test]
    fn test_boolean_semiring_axioms() {
        let a = BooleanWeight::new(true);
        let b = BooleanWeight::new(false);
        let c = BooleanWeight::new(true);

        // Associativity of addition
        assert_eq!((a + b) + c, a + (b + c));

        // Associativity of multiplication
        assert_eq!((a * b) * c, a * (b * c));

        // Commutativity of addition
        assert_eq!(a + b, b + a);

        // Commutativity of multiplication
        assert_eq!(a * b, b * a);

        // Distributivity
        assert_eq!((a + b) * c, (a * c) + (b * c));
    }

    #[test]
    fn test_boolean_correctness() {
        // Test all combinations
        let t = BooleanWeight::new(true);
        let f = BooleanWeight::new(false);

        // OR truth table
        assert_eq!(f + f, f);
        assert_eq!(f + t, t);
        assert_eq!(t + f, t);
        assert_eq!(t + t, t);

        // AND truth table
        assert_eq!(f * f, f);
        assert_eq!(f * t, f);
        assert_eq!(t * f, f);
        assert_eq!(t * t, t);
    }
}