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
//! Semiring implementations for weighted finite-state transducers.
//!
//! This module provides algebraic structures called *semirings* that define the weight
//! systems for weighted FSTs. A semiring generalizes the notions of addition and
//! multiplication while relaxing some requirements of traditional arithmetic, enabling
//! a unified framework for diverse computational problems.
//!
//! # Mathematical Foundation
//!
//! A **semiring** is an algebraic structure $`(K, \oplus, \otimes, \bar{0}, \bar{1})`$ where:
//!
//! - $`K`$ is a set of elements (weights)
//! - $`\oplus : K \times K \to K`$ is the **addition** operation (combining alternatives)
//! - $`\otimes : K \times K \to K`$ is the **multiplication** operation (extending paths)
//! - $`\bar{0} \in K`$ is the **additive identity** (represents "no path" or impossibility)
//! - $`\bar{1} \in K`$ is the **multiplicative identity** (represents "free" or zero-cost)
//!
//! ## Semiring Axioms
//!
//! For all $`a, b, c \in K`$:
//!
//! 1. **$(K, \oplus, \bar{0})$ is a commutative monoid:**
//! - Associativity: $`(a \oplus b) \oplus c = a \oplus (b \oplus c)`$
//! - Commutativity: $`a \oplus b = b \oplus a`$
//! - Identity: $`a \oplus \bar{0} = \bar{0} \oplus a = a`$
//!
//! 2. **$(K, \otimes, \bar{1})$ is a monoid:**
//! - Associativity: $`(a \otimes b) \otimes c = a \otimes (b \otimes c)`$
//! - Identity: $`a \otimes \bar{1} = \bar{1} \otimes a = a`$
//!
//! 3. **$\otimes$ distributes over $\oplus$:**
//! - Left: $`a \otimes (b \oplus c) = (a \otimes b) \oplus (a \otimes c)`$
//! - Right: $`(a \oplus b) \otimes c = (a \otimes c) \oplus (b \otimes c)`$
//!
//! 4. **$\bar{0}$ is an annihilator for $\otimes$:**
//! - $`a \otimes \bar{0} = \bar{0} \otimes a = \bar{0}`$
//!
//! # Available Semirings
//!
//! ## [`TropicalWeight`] — Tropical Semiring $`(\mathbb{R} \cup \{+\infty\}, \min, +, +\infty, 0)`$
//!
//! | Operation | Definition |
//! |-----------|------------|
//! | $`a \oplus b`$ | $`\min(a, b)`$ |
//! | $`a \otimes b`$ | $`a + b`$ |
//! | $`\bar{0}`$ | $`+\infty`$ |
//! | $`\bar{1}`$ | $`0`$ |
//!
//! **Use cases:** Shortest path, Viterbi decoding, edit distance, cost minimization
//!
//! ## [`LogWeight`] — Log Semiring $`(\mathbb{R} \cup \{+\infty\}, \oplus_{\log}, +, +\infty, 0)`$
//!
//! | Operation | Definition |
//! |-----------|------------|
//! | $`a \oplus b`$ | $`-\log(e^{-a} + e^{-b})`$ |
//! | $`a \otimes b`$ | $`a + b`$ |
//! | $`\bar{0}`$ | $`+\infty`$ |
//! | $`\bar{1}`$ | $`0`$ |
//!
//! **Use cases:** Numerically stable probability computation, speech recognition, machine translation
//!
//! ## [`ProbabilityWeight`] — Probability Semiring $`([0, 1], +, \times, 0, 1)`$
//!
//! | Operation | Definition |
//! |-----------|------------|
//! | $`a \oplus b`$ | $`a + b`$ |
//! | $`a \otimes b`$ | $`a \times b`$ |
//! | $`\bar{0}`$ | $`0`$ |
//! | $`\bar{1}`$ | $`1`$ |
//!
//! **Use cases:** Direct probability computation, language models, Bayesian inference
//!
//! ## [`RealWeight`] — Real Semiring $`(\mathbb{R}, +, \times, 0, 1)`$
//!
//! | Operation | Definition |
//! |-----------|------------|
//! | $`a \oplus b`$ | $`a + b`$ |
//! | $`a \otimes b`$ | $`a \times b`$ |
//! | $`\bar{0}`$ | $`0`$ |
//! | $`\bar{1}`$ | $`1`$ |
//!
//! **Use cases:** Linear algebra, normalization, matrix operations, signal processing
//!
//! ## [`BooleanWeight`] — Boolean Semiring $`(\{0, 1\}, \vee, \wedge, 0, 1)`$
//!
//! | Operation | Definition |
//! |-----------|------------|
//! | $`a \oplus b`$ | $`a \vee b`$ (OR) |
//! | $`a \otimes b`$ | $`a \wedge b`$ (AND) |
//! | $`\bar{0}`$ | $`\mathrm{false}`$ |
//! | $`\bar{1}`$ | $`\mathrm{true}`$ |
//!
//! **Use cases:** Unweighted automata, membership testing, regular expressions
//!
//! ## [`IntegerWeight`] — Integer Semiring $`(\mathbb{Z}, +, \times, 0, 1)`$
//!
//! | Operation | Definition |
//! |-----------|------------|
//! | $`a \oplus b`$ | $`a + b`$ |
//! | $`a \otimes b`$ | $`a \times b`$ |
//! | $`\bar{0}`$ | $`0`$ |
//! | $`\bar{1}`$ | $`1`$ |
//!
//! **Use cases:** Counting paths, exact arithmetic, combinatorial applications
//!
//! ## [`StringWeight`] — String Semiring $`(\Sigma^*, \mathrm{lcp}, \cdot, \bot, \varepsilon)`$
//!
//! | Operation | Definition |
//! |-----------|------------|
//! | $`a \oplus b`$ | longest common prefix |
//! | $`a \otimes b`$ | string concatenation |
//! | $`\bar{0}`$ | special marker |
//! | $`\bar{1}`$ | empty string $`\varepsilon`$ |
//!
//! **Use cases:** String operations, edit sequences, sequence alignment
//!
//! ## [`MinWeight`] / [`MaxWeight`] — MinMax Semirings
//!
//! Lattice-based semirings for optimization problems.
//!
//! ## [`ProductWeight`] — Product Semiring $`(K_1 \times K_2, \oplus, \otimes)`$
//!
//! Combines two semirings into a Cartesian product with component-wise operations.
//!
//! **Use cases:** Multi-objective optimization, simultaneous cost/probability tracking
//!
//! ## [`GallicWeight`] — Gallic Semiring
//!
//! Combines label sequences with weights for FST composition and output tracking.
//! Available variants: [`LeftGallic`], [`RightGallic`], [`MinGallic`], [`RestrictGallic`], [`UnionGallic`].
//!
//! # Choosing a Semiring
//!
//! ```
//! use arcweight::prelude::*;
//!
//! // For shortest path / minimum cost
//! let tropical = TropicalWeight::new(0.5);
//!
//! // For probabilistic models (log domain)
//! let log_prob = LogWeight::new(-2.3); // log probability
//!
//! // For direct probabilities
//! let prob = ProbabilityWeight::new(0.8);
//!
//! // For unweighted automata
//! let boolean = BooleanWeight::new(true);
//!
//! // Arithmetic follows semiring properties
//! let sum = tropical + TropicalWeight::new(0.3); // min(0.5, 0.3) = 0.3
//! let product = tropical * TropicalWeight::new(0.2); // 0.5 + 0.2 = 0.7
//! ```
//!
//! # Specialized Properties
//!
//! Some semirings provide additional structure enabling specialized algorithms:
//!
//! - [`DivisibleSemiring`]: Supports division operation for weight pushing
//! - [`StarSemiring`]: Supports Kleene closure $`w^* = \bigoplus_{i=0}^{\infty} w^i`$
//! - [`NaturallyOrderedSemiring`]: Has a compatible partial order
//! - [`InvertibleSemiring`]: All non-zero elements have multiplicative inverses
//!
//! # References
//!
//! - Kuich, W., & Salomaa, A. (1986). *Semirings, Automata, Languages*. EATCS Monographs
//! on Theoretical Computer Science, Vol. 5. Springer-Verlag.
//!
//! - Mohri, M. (2002). Semiring frameworks and algorithms for shortest-distance problems.
//! *Journal of Automata, Languages and Combinatorics*, 7(3), 321–350.
//!
//! - Mohri, M., Pereira, F., & Riley, M. (2002). Weighted finite-state transducers in
//! speech recognition. *Computer Speech & Language*, 16(1), 69–88.
//!
//! - Goodman, J. (1999). Semiring parsing. *Computational Linguistics*, 25(4), 573–605.
pub use BooleanWeight;
pub use IntegerWeight;
pub use LogWeight;
pub use ;
pub use ProbabilityWeight;
pub use ProductWeight;
pub use RealWeight;
pub use StringWeight;
pub use *;
pub use TropicalWeight;
// Re-export gallic types for convenience
pub use ;