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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
//! Lazy Composition FST - On-the-fly composition without materializing the full result.
//!
//! This module provides [`LazyComposeFst`], which computes the composition of two FSTs
//! on demand without materializing the full composed automaton. This approach is based
//! on the composition algorithm described by Mohri et al., adapted for lazy evaluation.
//!
//! # Composition Algorithm
//!
//! For two WFSTs $`T_1`$ and $`T_2`$, the composition $`T_1 \circ T_2`$ produces a
//! transducer that maps input strings of $`T_1`$ to output strings of $`T_2`$ by
//! matching the output labels of $`T_1`$ with the input labels of $`T_2`$.
//!
//! The lazy implementation uses a state-pair encoding where each state in the
//! composed FST corresponds to a pair $`(q_1, q_2)`$ of states from the input FSTs.
//! States are computed and cached on first access.
//!
//! # Complexity
//!
//! Let $`n_1, n_2`$ be the state counts and $`m_1, m_2`$ be the arc counts of the
//! input FSTs. For lazy composition:
//!
//! - **Time per state expansion**: $`O(d_1 \cdot d_2)`$ where $`d_i`$ is the out-degree
//! - **Space for accessed states**: $`O(\text{accessed state pairs})`$
//! - **Total potential states**: $`O(n_1 \cdot n_2)`$ (but rarely all accessed)
//!
//! # Performance Benefits
//!
//! - **Memory Efficient**: Only stores computed state pairs
//! - **Sparse Access**: Ideal for shortest path through composed FST (10-100x faster)
//! - **Caching**: Previously computed arcs cached for repeated access
//!
//! # Examples
//!
//! ```rust
//! use arcweight::prelude::*;
//! use arcweight::fst::LazyComposeFst;
//!
//! // Create two FSTs
//! let mut fst1 = VectorFst::<TropicalWeight>::new();
//! let s0 = fst1.add_state();
//! let s1 = fst1.add_state();
//! fst1.set_start(s0);
//! fst1.set_final(s1, TropicalWeight::one());
//! fst1.add_arc(s0, Arc::new(1, 2, TropicalWeight::new(0.5), s1));
//!
//! let mut fst2 = VectorFst::<TropicalWeight>::new();
//! let t0 = fst2.add_state();
//! let t1 = fst2.add_state();
//! fst2.set_start(t0);
//! fst2.set_final(t1, TropicalWeight::one());
//! fst2.add_arc(t0, Arc::new(2, 3, TropicalWeight::new(0.3), t1));
//!
//! // Create lazy composition - no computation happens yet
//! let lazy = LazyComposeFst::new(&fst1, &fst2);
//!
//! // Arcs are computed on demand
//! if let Some(start) = lazy.start() {
//!     for arc in lazy.arcs(start) {
//!         println!("Lazy arc: {} -> {}", arc.ilabel, arc.olabel);
//!     }
//! }
//! ```
//!
//! # References
//!
//! - Mohri, M., Pereira, F., & Riley, M. (1996). Weighted Automata in Text and
//!   Speech Processing. In *Proc. ECAI-96 Workshop*, pp. 46-50.
//!
//! - Mohri, M., Pereira, F., & Riley, M. (2002). Weighted Finite-State Transducers
//!   in Speech Recognition. *Computer Speech & Language*, 16(1), 69-88.
//!
//! - Allauzen, C., & Mohri, M. (2008). 3-Way Composition of Weighted Finite-State
//!   Transducers. In *Proc. CIAA 2008*, LNCS 5148, pp. 262-273. Springer.

use crate::arc::{Arc, ArcIterator};
use crate::fst::{Fst, Label, StateId};
use crate::semiring::Semiring;

use rustc_hash::FxHashMap;
use std::sync::RwLock;

/// Epsilon label constant
const EPSILON: Label = 0;

/// Cached state data for lazy composition
#[derive(Debug, Clone)]
struct CachedComposedState<W: Semiring> {
    /// Final weight of this state pair (None if non-final)
    final_weight: Option<W>,
    /// Cached arcs from this state pair
    arcs: Vec<Arc<W>>,
}

/// Lazy Composition FST with on-demand state computation.
///
/// Computes the composition of two FSTs $`T_1 \circ T_2`$ on-the-fly, caching results
/// for repeated access. This is particularly efficient for operations like shortest
/// path where only a small subset of the composed FST is explored.
///
/// # Type Parameters
///
/// - `'a`: Lifetime bound ensuring input FSTs outlive this composition
/// - `W`: Semiring weight type (must match both input FSTs)
/// - `F1`: Type of the first (left) FST
/// - `F2`: Type of the second (right) FST
///
/// # State Encoding
///
/// States in the composed FST are identified by pairs $`(q_1, q_2)`$ where $`q_1`$
/// is a state in $`F_1`$ and $`q_2`$ is a state in $`F_2`$. The start state is
/// $`(I_1, I_2)`$ where $`I_i`$ is the start state of FST $`i`$.
///
/// # References
///
/// - Mohri, M., Pereira, F., & Riley, M. (2002). Weighted Finite-State Transducers
///   in Speech Recognition. *Computer Speech & Language*, 16(1), 69-88.
pub struct LazyComposeFst<'a, W, F1, F2>
where
    W: Semiring,
    F1: Fst<W>,
    F2: Fst<W>,
{
    /// First FST (provides input labels)
    fst1: &'a F1,
    /// Second FST (provides output labels)
    fst2: &'a F2,
    /// Cache of state pair to composed state ID
    state_map: RwLock<FxHashMap<(StateId, StateId), StateId>>,
    /// Reverse map from composed state ID to state pair
    state_pairs: RwLock<Vec<(StateId, StateId)>>,
    /// Cached state data
    state_cache: RwLock<Vec<Option<CachedComposedState<W>>>>,
    /// Start state of composed FST
    start: Option<StateId>,
}

impl<'a, W, F1, F2> LazyComposeFst<'a, W, F1, F2>
where
    W: Semiring,
    F1: Fst<W>,
    F2: Fst<W>,
{
    /// Create a new lazy composition FST
    pub fn new(fst1: &'a F1, fst2: &'a F2) -> Self {
        // Determine start state
        let start = match (fst1.start(), fst2.start()) {
            (Some(s1), Some(s2)) => {
                let mut state_map = FxHashMap::default();
                state_map.insert((s1, s2), 0);
                Some(0)
            }
            _ => None,
        };

        let state_pairs = if let (Some(s1), Some(s2)) = (fst1.start(), fst2.start()) {
            vec![(s1, s2)]
        } else {
            Vec::new()
        };

        let state_cache = if start.is_some() {
            vec![None]
        } else {
            Vec::new()
        };

        let mut state_map_init = FxHashMap::default();
        if let (Some(s1), Some(s2)) = (fst1.start(), fst2.start()) {
            state_map_init.insert((s1, s2), 0);
        }

        Self {
            fst1,
            fst2,
            state_map: RwLock::new(state_map_init),
            state_pairs: RwLock::new(state_pairs),
            state_cache: RwLock::new(state_cache),
            start,
        }
    }

    /// Get or create a state for the given state pair
    fn get_or_create_state(&self, s1: StateId, s2: StateId) -> StateId {
        // Check if state already exists (read lock)
        {
            let state_map = self.state_map.read().unwrap();
            if let Some(&state) = state_map.get(&(s1, s2)) {
                return state;
            }
        }

        // Create new state (write lock)
        let mut state_map = self.state_map.write().unwrap();
        // Double-check after acquiring write lock
        if let Some(&state) = state_map.get(&(s1, s2)) {
            return state;
        }

        let mut state_pairs = self.state_pairs.write().unwrap();
        let mut state_cache = self.state_cache.write().unwrap();

        let new_state = state_pairs.len() as StateId;
        state_map.insert((s1, s2), new_state);
        state_pairs.push((s1, s2));
        state_cache.push(None);

        new_state
    }

    /// Compute and cache the arcs for a state
    fn compute_arcs(&self, state: StateId) -> CachedComposedState<W> {
        let state_pairs = self.state_pairs.read().unwrap();
        let (s1, s2) = state_pairs[state as usize];
        drop(state_pairs);

        let mut arcs = Vec::new();

        // Get arcs from both FSTs
        let arcs1: Vec<Arc<W>> = self.fst1.arcs(s1).collect();
        let arcs2: Vec<Arc<W>> = self.fst2.arcs(s2).collect();

        // Sort arcs by labels for two-pointer merge (O(E1 + E2) instead of O(E1 * E2))
        let mut sorted_arcs1 = arcs1.clone();
        let mut sorted_arcs2 = arcs2.clone();
        sorted_arcs1.sort_by_key(|a| a.olabel);
        sorted_arcs2.sort_by_key(|a| a.ilabel);

        // Handle epsilon arcs from FST1 (advance FST1 only)
        for arc1 in arcs1.iter().filter(|a| a.olabel == EPSILON) {
            let next_state = self.get_or_create_state(arc1.nextstate, s2);
            arcs.push(Arc::new(
                arc1.ilabel,
                EPSILON,
                arc1.weight.clone(),
                next_state,
            ));
        }

        // Handle epsilon arcs from FST2 (advance FST2 only)
        for arc2 in arcs2.iter().filter(|a| a.ilabel == EPSILON) {
            let next_state = self.get_or_create_state(s1, arc2.nextstate);
            arcs.push(Arc::new(
                EPSILON,
                arc2.olabel,
                arc2.weight.clone(),
                next_state,
            ));
        }

        // Two-pointer merge for matching non-epsilon labels
        let non_eps1: Vec<_> = sorted_arcs1
            .into_iter()
            .filter(|a| a.olabel != EPSILON)
            .collect();
        let non_eps2: Vec<_> = sorted_arcs2
            .into_iter()
            .filter(|a| a.ilabel != EPSILON)
            .collect();

        let mut i = 0;
        let mut j = 0;

        while i < non_eps1.len() && j < non_eps2.len() {
            let arc1 = &non_eps1[i];
            let arc2 = &non_eps2[j];

            match arc1.olabel.cmp(&arc2.ilabel) {
                std::cmp::Ordering::Less => {
                    i += 1;
                }
                std::cmp::Ordering::Greater => {
                    j += 1;
                }
                std::cmp::Ordering::Equal => {
                    // Find all arcs with the same label
                    let label = arc1.olabel;
                    let mut end_i = i;
                    let mut end_j = j;

                    while end_i < non_eps1.len() && non_eps1[end_i].olabel == label {
                        end_i += 1;
                    }
                    while end_j < non_eps2.len() && non_eps2[end_j].ilabel == label {
                        end_j += 1;
                    }

                    // Match all pairs with this label
                    for a1 in non_eps1.iter().take(end_i).skip(i) {
                        for a2 in non_eps2.iter().take(end_j).skip(j) {
                            let next_state = self.get_or_create_state(a1.nextstate, a2.nextstate);
                            arcs.push(Arc::new(
                                a1.ilabel,
                                a2.olabel,
                                a1.weight.times(&a2.weight),
                                next_state,
                            ));
                        }
                    }

                    i = end_i;
                    j = end_j;
                }
            }
        }

        // Compute final weight
        let final_weight = match (self.fst1.final_weight(s1), self.fst2.final_weight(s2)) {
            (Some(w1), Some(w2)) => Some(w1.times(w2)),
            _ => None,
        };

        CachedComposedState { final_weight, arcs }
    }

    /// Get cached state data, computing if necessary
    fn get_state_data(&self, state: StateId) -> CachedComposedState<W> {
        // Check cache first (read lock)
        {
            let cache = self.state_cache.read().unwrap();
            if let Some(Some(data)) = cache.get(state as usize) {
                return data.clone();
            }
        }

        // Compute arcs
        let data = self.compute_arcs(state);

        // Cache result (write lock)
        {
            let mut cache = self.state_cache.write().unwrap();
            if let Some(slot) = cache.get_mut(state as usize) {
                *slot = Some(data.clone());
            }
        }

        data
    }

    /// Get the number of computed states
    pub fn num_computed_states(&self) -> usize {
        self.state_pairs.read().unwrap().len()
    }

    /// Get the number of cached states
    pub fn num_cached_states(&self) -> usize {
        self.state_cache
            .read()
            .unwrap()
            .iter()
            .filter(|s| s.is_some())
            .count()
    }

    /// Get the state pair for a composed state
    pub fn state_pair(&self, state: StateId) -> Option<(StateId, StateId)> {
        self.state_pairs
            .read()
            .unwrap()
            .get(state as usize)
            .copied()
    }

    /// Clear the arc cache (keeps state mapping)
    pub fn clear_arc_cache(&self) {
        let mut cache = self.state_cache.write().unwrap();
        for slot in cache.iter_mut() {
            *slot = None;
        }
    }
}

impl<'a, W, F1, F2> std::fmt::Debug for LazyComposeFst<'a, W, F1, F2>
where
    W: Semiring,
    F1: Fst<W>,
    F2: Fst<W>,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("LazyComposeFst")
            .field("num_computed_states", &self.num_computed_states())
            .field("num_cached_states", &self.num_cached_states())
            .field("start", &self.start)
            .finish()
    }
}

/// Arc iterator for lazy composition FST
#[derive(Debug)]
pub struct LazyComposeArcIterator<W: Semiring> {
    arcs: Vec<Arc<W>>,
    index: usize,
}

impl<W: Semiring> Iterator for LazyComposeArcIterator<W> {
    type Item = Arc<W>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.index < self.arcs.len() {
            let arc = self.arcs[self.index].clone();
            self.index += 1;
            Some(arc)
        } else {
            None
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let remaining = self.arcs.len() - self.index;
        (remaining, Some(remaining))
    }
}

impl<W: Semiring> ExactSizeIterator for LazyComposeArcIterator<W> {}

impl<W: Semiring> ArcIterator<W> for LazyComposeArcIterator<W> {
    fn reset(&mut self) {
        self.index = 0;
    }
}

// Implement Fst trait for LazyComposeFst
impl<'a, W, F1, F2> Fst<W> for LazyComposeFst<'a, W, F1, F2>
where
    W: Semiring,
    F1: Fst<W>,
    F2: Fst<W>,
{
    type ArcIter<'b>
        = LazyComposeArcIterator<W>
    where
        Self: 'b;

    fn start(&self) -> Option<StateId> {
        self.start
    }

    fn final_weight(&self, _state: StateId) -> Option<&W> {
        // We can't return a reference to cached data, so return None
        // Use is_final() and get_final_weight() for lazy access
        None
    }

    fn num_arcs(&self, state: StateId) -> usize {
        self.get_state_data(state).arcs.len()
    }

    fn num_states(&self) -> usize {
        self.state_pairs.read().unwrap().len()
    }

    fn properties(&self) -> crate::properties::FstProperties {
        crate::properties::FstProperties::default()
    }

    fn arcs(&self, state: StateId) -> Self::ArcIter<'_> {
        let data = self.get_state_data(state);
        LazyComposeArcIterator {
            arcs: data.arcs,
            index: 0,
        }
    }

    fn is_final(&self, state: StateId) -> bool {
        self.get_state_data(state).final_weight.is_some()
    }
}

// Thread safety
unsafe impl<'a, W, F1, F2> Send for LazyComposeFst<'a, W, F1, F2>
where
    W: Semiring + Send,
    F1: Fst<W> + Sync,
    F2: Fst<W> + Sync,
{
}

unsafe impl<'a, W, F1, F2> Sync for LazyComposeFst<'a, W, F1, F2>
where
    W: Semiring + Send + Sync,
    F1: Fst<W> + Sync,
    F2: Fst<W> + Sync,
{
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::fst::{MutableFst, VectorFst};
    use crate::semiring::TropicalWeight;
    use num_traits::One;

    #[test]
    fn test_lazy_compose_basic() {
        // FST1: 0 --(1:2/0.5)--> 1
        let mut fst1 = VectorFst::<TropicalWeight>::new();
        let s0 = fst1.add_state();
        let s1 = fst1.add_state();
        fst1.set_start(s0);
        fst1.set_final(s1, TropicalWeight::one());
        fst1.add_arc(s0, Arc::new(1, 2, TropicalWeight::new(0.5), s1));

        // FST2: 0 --(2:3/0.3)--> 1
        let mut fst2 = VectorFst::<TropicalWeight>::new();
        let t0 = fst2.add_state();
        let t1 = fst2.add_state();
        fst2.set_start(t0);
        fst2.set_final(t1, TropicalWeight::one());
        fst2.add_arc(t0, Arc::new(2, 3, TropicalWeight::new(0.3), t1));

        let lazy = LazyComposeFst::new(&fst1, &fst2);

        assert_eq!(lazy.start(), Some(0));
        assert_eq!(lazy.num_computed_states(), 1);

        // Access arcs to trigger computation
        let arcs: Vec<_> = lazy.arcs(0).collect();
        assert_eq!(arcs.len(), 1);
        assert_eq!(arcs[0].ilabel, 1);
        assert_eq!(arcs[0].olabel, 3);

        // Final state should be computed
        assert_eq!(lazy.num_computed_states(), 2);
        assert!(lazy.is_final(arcs[0].nextstate));
    }

    #[test]
    fn test_lazy_compose_no_match() {
        // FST1: 0 --(1:2)--> 1
        let mut fst1 = VectorFst::<TropicalWeight>::new();
        let s0 = fst1.add_state();
        let s1 = fst1.add_state();
        fst1.set_start(s0);
        fst1.set_final(s1, TropicalWeight::one());
        fst1.add_arc(s0, Arc::new(1, 2, TropicalWeight::one(), s1));

        // FST2: 0 --(3:4)--> 1 (no matching labels)
        let mut fst2 = VectorFst::<TropicalWeight>::new();
        let t0 = fst2.add_state();
        let t1 = fst2.add_state();
        fst2.set_start(t0);
        fst2.set_final(t1, TropicalWeight::one());
        fst2.add_arc(t0, Arc::new(3, 4, TropicalWeight::one(), t1));

        let lazy = LazyComposeFst::new(&fst1, &fst2);

        let arcs: Vec<_> = lazy.arcs(0).collect();
        assert_eq!(arcs.len(), 0); // No matching labels
    }

    #[test]
    fn test_lazy_compose_epsilon_handling() {
        // FST1: 0 --(1:eps)--> 1 --(eps:2)--> 2
        let mut fst1 = VectorFst::<TropicalWeight>::new();
        let s0 = fst1.add_state();
        let s1 = fst1.add_state();
        let s2 = fst1.add_state();
        fst1.set_start(s0);
        fst1.set_final(s2, TropicalWeight::one());
        fst1.add_arc(s0, Arc::new(1, 0, TropicalWeight::one(), s1)); // eps output
        fst1.add_arc(s1, Arc::new(0, 2, TropicalWeight::one(), s2)); // eps input

        // FST2: 0 --(2:3)--> 1
        let mut fst2 = VectorFst::<TropicalWeight>::new();
        let t0 = fst2.add_state();
        let t1 = fst2.add_state();
        fst2.set_start(t0);
        fst2.set_final(t1, TropicalWeight::one());
        fst2.add_arc(t0, Arc::new(2, 3, TropicalWeight::one(), t1));

        let lazy = LazyComposeFst::new(&fst1, &fst2);

        // Should have epsilon transition from (0,0) to (1,0)
        let arcs: Vec<_> = lazy.arcs(0).collect();
        assert!(!arcs.is_empty());
    }

    #[test]
    fn test_lazy_compose_multiple_matches() {
        // FST1: 0 --(1:2)--> 1, 0 --(1:2)--> 2
        let mut fst1 = VectorFst::<TropicalWeight>::new();
        let s0 = fst1.add_state();
        let s1 = fst1.add_state();
        let s2 = fst1.add_state();
        fst1.set_start(s0);
        fst1.set_final(s1, TropicalWeight::one());
        fst1.set_final(s2, TropicalWeight::one());
        fst1.add_arc(s0, Arc::new(1, 2, TropicalWeight::new(0.5), s1));
        fst1.add_arc(s0, Arc::new(1, 2, TropicalWeight::new(0.7), s2));

        // FST2: 0 --(2:3)--> 1, 0 --(2:4)--> 2
        let mut fst2 = VectorFst::<TropicalWeight>::new();
        let t0 = fst2.add_state();
        let t1 = fst2.add_state();
        let t2 = fst2.add_state();
        fst2.set_start(t0);
        fst2.set_final(t1, TropicalWeight::one());
        fst2.set_final(t2, TropicalWeight::one());
        fst2.add_arc(t0, Arc::new(2, 3, TropicalWeight::new(0.3), t1));
        fst2.add_arc(t0, Arc::new(2, 4, TropicalWeight::new(0.4), t2));

        let lazy = LazyComposeFst::new(&fst1, &fst2);

        // Should have 4 matching arcs (2x2 combinations)
        let arcs: Vec<_> = lazy.arcs(0).collect();
        assert_eq!(arcs.len(), 4);
    }

    #[test]
    fn test_lazy_compose_state_pair() {
        let mut fst1 = VectorFst::<TropicalWeight>::new();
        let s0 = fst1.add_state();
        let s1 = fst1.add_state();
        fst1.set_start(s0);
        fst1.set_final(s1, TropicalWeight::one());
        fst1.add_arc(s0, Arc::new(1, 2, TropicalWeight::one(), s1));

        let mut fst2 = VectorFst::<TropicalWeight>::new();
        let t0 = fst2.add_state();
        let t1 = fst2.add_state();
        fst2.set_start(t0);
        fst2.set_final(t1, TropicalWeight::one());
        fst2.add_arc(t0, Arc::new(2, 3, TropicalWeight::one(), t1));

        let lazy = LazyComposeFst::new(&fst1, &fst2);

        // Start state should be (0, 0)
        assert_eq!(lazy.state_pair(0), Some((0, 0)));

        // Trigger computation
        let arcs: Vec<_> = lazy.arcs(0).collect();
        let next_state = arcs[0].nextstate;

        // Next state should be (1, 1)
        assert_eq!(lazy.state_pair(next_state), Some((1, 1)));
    }
}