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
//! Gallic semiring: combining labels with weights for advanced FST composition.
//!
//! The Gallic semiring provides a sophisticated algebraic structure that pairs
//! label sequences with semiring weights. This enables advanced finite-state
//! transducer algorithms, particularly composition operations that need to track
//! output labels as part of the weight structure.
//!
//! # Mathematical Definition
//!
//! A Gallic weight $`(s, w)`$ combines:
//! - $`s \in \Sigma^*`$: A sequence of labels (output string)
//! - $`w \in K`$: A weight from an underlying semiring $`K`$
//!
//! | Operation | Definition |
//! |-----------|------------|
//! | $`(s_1, w_1) \oplus (s_2, w_2)`$ | Variant-dependent (see below) |
//! | $`(s_1, w_1) \otimes (s_2, w_2)`$ | $`(s_1 \cdot s_2, w_1 \otimes w_2)`$ |
//! | $`\bar{0}`$ | $`(\varepsilon, \bar{0}_K)`$ with zero weight |
//! | $`\bar{1}`$ | $`(\varepsilon, \bar{1}_K)`$ (empty string, one weight) |
//!
//! # Overview
//!
//! A Gallic weight $`(s, w)`$ combines:
//! - **s**: A sequence of labels (`Vec<Label>`)
//! - **w**: A weight from an arbitrary semiring W
//!
//! The Gallic semiring is parameterized by a **variant** that determines how
//! two weights are combined during addition ($`\oplus`$). Multiplication ($`\otimes`$) always
//! concatenates label sequences and multiplies weights.
//!
//! # Architecture
//!
//! This implementation leverages Rust's type system for beautiful, modular design:
//!
//! ```text
//! ┌─────────────────────────────────────────┐
//! │ GallicVariant Trait │
//! │ (Defines combination behavior) │
//! └─────────────────────────────────────────┘
//! ▲
//! │ implements
//! ┌───────┼───────┐
//! │ │ │
//! ┌────────┐ ┌────────┐ ┌────────┐
//! │ Left │ │ Right │ │ Min │ ...
//! │ Gallic │ │ Gallic │ │ Gallic │
//! └────────┘ └────────┘ └────────┘
//! │ │ │
//! └───────┼───────┘
//! ▼
//! ┌────────────────────────┐
//! │ GallicWeight<W, V> │
//! │ - labels: Vec<Label> │
//! │ - weight: W │
//! │ - variant: PhantomData<V> │
//! └────────────────────────┘
//! ```
//!
//! # Variants
//!
//! ## [`LeftGallic`]
//! Uses longest common prefix (LCP) for addition:
//! ```text
//! (s₁, w₁) ⊕ (s₂, w₂) = (lcp(s₁, s₂), w₁ ⊕ w₂)
//! ```
//! Mathematically: $`(s_1, w_1) \oplus (s_2, w_2) = (\mathrm{lcp}(s_1, s_2), w_1 \oplus w_2)`$
//!
//! ## [`RightGallic`]
//! Uses longest common suffix (LCS) for addition:
//! ```text
//! (s₁, w₁) ⊕ (s₂, w₂) = (lcs(s₁, s₂), w₁ ⊕ w₂)
//! ```
//! Mathematically: $`(s_1, w_1) \oplus (s_2, w_2) = (\mathrm{lcs}(s_1, s_2), w_1 \oplus w_2)`$
//!
//! ## [`RestrictGallic`]
//! Enforces functional transducer property (labels must match):
//! ```text
//! (s₁, w₁) ⊕ (s₂, w₂) = if s₁ == s₂ { (s₁, w₁ ⊕ w₂) } else { zero }
//! ```
//! Mathematically: $`(s_1, w_1) \oplus (s_2, w_2) = \begin{cases} (s_1, w_1 \oplus w_2) & \text{if } s_1 = s_2 \\ \bar{0} & \text{otherwise} \end{cases}`$
//!
//! ## [`MinGallic`]
//! Selects label sequence with minimum weight:
//! ```text
//! (s₁, w₁) ⊕ (s₂, w₂) = if w₁ ≤ w₂ { (s₁, w₁) } else { (s₂, w₂) }
//! ```
//! Mathematically: $`(s_1, w_1) \oplus (s_2, w_2) = \begin{cases} (s_1, w_1) & \text{if } w_1 \leq w_2 \\ (s_2, w_2) & \text{otherwise} \end{cases}`$
//!
//! ## [`UnionGallic`]
//! General union semantics (default):
//! ```text
//! (s₁, w₁) ⊕ (s₂, w₂) = (lcp(s₁, s₂), w₁ ⊕ w₂)
//! ```
//! Mathematically: $`(s_1, w_1) \oplus (s_2, w_2) = (\mathrm{lcp}(s_1, s_2), w_1 \oplus w_2)`$
//!
//! # Examples
//!
//! ## Basic Usage with LeftGallic
//!
//! ```rust
//! use arcweight::prelude::*;
//! use arcweight::semiring::gallic::{GallicWeight, LeftGallic};
//!
//! // Create Gallic weights with label sequences
//! let path1 = GallicWeight::<TropicalWeight, LeftGallic>::from_labels(
//! &[1, 2, 3],
//! TropicalWeight::new(1.0)
//! );
//! let path2 = GallicWeight::<TropicalWeight, LeftGallic>::from_labels(
//! &[1, 2, 4],
//! TropicalWeight::new(2.0)
//! );
//!
//! // Addition uses LCP
//! let combined = path1 + path2;
//! assert_eq!(combined.labels(), &[1, 2]); // Common prefix
//! assert_eq!(*combined.weight().value(), 1.0); // min(1.0, 2.0)
//! ```
//!
//! ## Path Selection with MinGallic
//!
//! ```rust
//! use arcweight::semiring::gallic::{GallicWeight, MinGallic};
//! use arcweight::semiring::{Semiring, TropicalWeight};
//!
//! type PathWeight = GallicWeight<TropicalWeight, MinGallic>;
//!
//! let path1 = PathWeight::from_labels(&[1, 2], TropicalWeight::new(5.0));
//! let path2 = PathWeight::from_labels(&[3, 4], TropicalWeight::new(3.0));
//!
//! // MinGallic selects minimum weight path
//! let shortest = path1 + path2;
//! assert_eq!(shortest.labels(), &[3, 4]); // Path with cost 3.0
//! assert_eq!(*shortest.weight().value(), 3.0);
//! ```
//!
//! ## Label Concatenation
//!
//! ```rust
//! use arcweight::semiring::gallic::{GallicWeight, UnionGallic};
//! use arcweight::semiring::{Semiring, TropicalWeight};
//!
//! let seg1 = GallicWeight::<TropicalWeight, UnionGallic>::from_labels(
//! &[1, 2],
//! TropicalWeight::new(1.0)
//! );
//! let seg2 = GallicWeight::<TropicalWeight, UnionGallic>::from_labels(
//! &[3, 4],
//! TropicalWeight::new(2.0)
//! );
//!
//! // Multiplication concatenates labels
//! let full_path = seg1 * seg2;
//! assert_eq!(full_path.labels(), &[1, 2, 3, 4]);
//! assert_eq!(*full_path.weight().value(), 3.0);
//! ```
//!
//! ## Functional Transducer Enforcement
//!
//! ```rust
//! use arcweight::semiring::gallic::{GallicWeight, RestrictGallic};
//! use arcweight::semiring::TropicalWeight;
//! use arcweight::semiring::Semiring;
//!
//! type FunctionalWeight = GallicWeight<TropicalWeight, RestrictGallic>;
//!
//! let w1 = FunctionalWeight::from_labels(&[1, 2], TropicalWeight::new(1.0));
//! let w2 = FunctionalWeight::from_labels(&[1, 2], TropicalWeight::new(2.0));
//! let w3 = FunctionalWeight::from_labels(&[3, 4], TropicalWeight::new(1.0));
//!
//! // Valid: identical labels
//! let valid = w1.clone() + w2;
//! assert!(!valid.weight().is_zero());
//!
//! // Invalid: different labels → zero weight
//! let invalid = w1 + w3;
//! assert!(invalid.weight().is_zero()); // Signals violation
//! ```
//!
//! ## Converting Between Variants
//!
//! ```rust
//! use arcweight::semiring::gallic::{GallicWeight, LeftGallic, MinGallic};
//! use arcweight::semiring::TropicalWeight;
//!
//! let left_weight = GallicWeight::<TropicalWeight, LeftGallic>::from_labels(
//! &[1, 2, 3],
//! TropicalWeight::new(5.0)
//! );
//!
//! // Convert to MinGallic variant
//! let min_weight: GallicWeight<TropicalWeight, MinGallic> =
//! left_weight.into_variant();
//! ```
//!
//! # Type Aliases
//!
//! Common variant combinations for convenience:
//!
//! ```rust
//! use arcweight::semiring::gallic::{GallicWeight, LeftGallic, MinGallic};
//! use arcweight::semiring::TropicalWeight;
//!
//! // Explicit type aliases
//! type LeftGallicWeight<W> = GallicWeight<W, LeftGallic>;
//! type MinGallicWeight<W> = GallicWeight<W, MinGallic>;
//!
//! // UnionGallic is the default
//! type DefaultGallic<W> = GallicWeight<W>; // UnionGallic implicit
//! ```
//!
//! # Use Cases
//!
//! ## FST Composition with Output Tracking
//! Track output labels as weights during composition operations
//!
//! ## Shortest Path with Label Sequences
//! Find optimal paths while preserving the label sequences
//!
//! ## Viterbi Decoding
//! Track state sequences alongside probabilities
//!
//! ## Machine Translation
//! Track word alignments with translation scores
//!
//! ## Speech Recognition
//! Maintain phone sequences with acoustic scores
//!
//! # Performance
//!
//! - **Plus**: Variant-dependent (O(1) for MinGallic, O(n) for LeftGallic)
//! - **Times**: O(|s₁| + |s₂|) for label concatenation
//! - **Memory**: Linear in total label count
//! - **Zero-cost variant selection**: Compile-time dispatch via PhantomData
//!
//! # Design Principles
//!
//! This implementation demonstrates:
//! 1. **Type-level programming**: Variants as zero-sized types
//! 2. **Trait-based modularity**: `GallicVariant` trait for extensibility
//! 3. **Zero-cost abstractions**: No runtime overhead for variant selection
//! 4. **Compile-time safety**: Type system prevents variant mixing
//! 5. **Idiomatic Rust**: Leverages generics, traits, and PhantomData
//!
//! # See Also
//!
//! - [`GallicVariant`] - Trait for implementing custom variants
//! - [`GallicWeight`] - Main weight type
//! - Individual variant types: [`LeftGallic`], [`MinGallic`], etc.
//!
//! # References
//!
//! - Mohri, M. (2002). Semiring frameworks and algorithms for shortest-distance problems.
//! *Journal of Automata, Languages and Combinatorics*, 7(3), 321-350.
//!
//! - Allauzen, C., & Mohri, M. (2009). Linear-space computation of the edit-distance between
//! a string and a finite automaton. In *Computing and Combinatorics* (pp. 103-114). Springer.
//!
//! - Mohri, M., Pereira, F., & Riley, M. (2002). Weighted finite-state transducers in
//! speech recognition. *Computer Speech & Language*, 16(1), 69-88.
pub use ;
pub use ;
pub use GallicWeight;
// Type aliases for common combinations
/// Left Gallic weight with specified base semiring
pub type LeftGallicWeight<W> = ;
/// Right Gallic weight with specified base semiring
pub type RightGallicWeight<W> = ;
/// Restricted Gallic weight with specified base semiring
pub type RestrictGallicWeight<W> = ;
/// Min Gallic weight with specified base semiring
pub type MinGallicWeight<W> = ;
/// Standard Gallic weight (UnionGallic variant)
pub type StandardGallicWeight<W> = ;