sicada 0.1.0

A weighted finite-state transducer (WFST) library, file-compatible with OpenFst
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
//! Moving weight, and labels, towards one end of an FST.
//!
//! Port of OpenFst's `push.h`. Pushing towards the initial state makes the sum
//! of the arcs leaving each non-initial state, together with its final weight,
//! come to [`Weight::one`], so a weight is committed as early on a path as it
//! can be, which is why a search over the result prunes well. Pushing
//! towards the final states does the same on the reversed machine.
//!
//! What the FST accepts does not change; only where along a path the weight
//! sits.

use crate::algorithms::arc_map::{FromGallicMapper, RmWeightMapper, ToGallicMapper, arc_map_to};
use crate::algorithms::factor_weight::{
    FactorIterator, FactorWeightOptions, GallicFactor, factor_weight,
};
use crate::algorithms::reweight::{reweight_to_final, reweight_to_initial};
use crate::algorithms::shortest_distance::{shortest_distance_forward, shortest_distance_reverse};
use crate::arc::{Arc, ArcStateId, GallicArc};
use crate::error::OpenFstError;
use crate::fst::{Fst, MutableFst};
use crate::fsts::vector_fst::VectorFst;
use crate::weight::{Divide, DivideType, LeftSemiring, RightSemiring, Weight};
use crate::weights::string_weight::{
    GallicLeft, GallicRight, GallicTypeMarker, GallicWeight, StringWeight,
};

/// Which end of the FST to move weight towards.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReweightType {
    /// Towards the initial state.
    ToInitial,
    /// Towards the final states.
    ToFinal,
}

/// Move the weights.
pub const PUSH_WEIGHTS: u8 = 0x01;
/// Move the labels.
pub const PUSH_LABELS: u8 = 0x02;
/// Divide the total weight out, so what is left sums to [`Weight::one`].
pub const PUSH_REMOVE_TOTAL_WEIGHT: u8 = 0x04;
/// Divide the common prefix or suffix out of the labels.
pub const PUSH_REMOVE_COMMON_AFFIX: u8 = 0x08;

/// The ⊕-sum of every accepting path, read off a distance vector already
/// computed.
///
/// Which end the distance was computed from decides how to read it: from the
/// final states it is the start state's entry, from the initial state it is the
/// sum over states of distance ⊗ final weight.
pub fn total_weight<A, F>(fst: &F, distance: &[A::Weight], reverse: bool) -> A::Weight
where
    A: Arc,
    F: Fst<A>,
{
    if reverse {
        return fst
            .start()
            .and_then(|start| distance.get(start.as_usize()).cloned())
            .unwrap_or_else(A::Weight::zero);
    }
    let mut sum = A::Weight::zero();
    for (index, weight) in distance.iter().enumerate() {
        sum = sum.plus(&weight.times(&fst.final_weight(A::StateId::from_usize(index))));
    }
    sum
}

/// Divides `weight` out of every accepting path, at the final states when
/// `at_final` and at the initial state otherwise.
pub fn remove_weight<A, F>(fst: &mut F, weight: &A::Weight, at_final: bool)
where
    A: Arc,
    A::Weight: Divide,
    F: MutableFst<A>,
{
    if *weight == A::Weight::one() || *weight == A::Weight::zero() {
        return;
    }
    if at_final {
        let states: Vec<A::StateId> = fst.states().collect();
        for state in states {
            let divided = fst.final_weight(state).divide(weight, DivideType::Right);
            fst.set_final(state, divided);
        }
        return;
    }
    let Some(start) = fst.start() else { return };
    fst.mutate_arcs(start, |arc| {
        let divided = arc.weight().divide(weight, DivideType::Left);
        *arc = A::new(arc.ilabel(), arc.olabel(), divided, arc.nextstate());
    });
    let divided = fst.final_weight(start).divide(weight, DivideType::Left);
    fst.set_final(start, divided);
}

/// Moves the weights of `fst` towards one end, in place.
///
/// The direction is chosen at run time, so the weight has to distribute on
/// both sides; a semiring with only one should use [`push_to_initial`] or
/// [`push_to_final`], which each ask for the side they actually divide on.
pub fn push_weights<A, F>(
    fst: &mut F,
    to: ReweightType,
    delta: f32,
    remove_total_weight: bool,
) -> Result<(), OpenFstError>
where
    A: Arc,
    A::Weight: Divide + LeftSemiring + RightSemiring,
    F: MutableFst<A>,
    <A::Weight as Weight>::ReverseWeight: Weight<ReverseWeight = A::Weight>,
{
    // Pushing towards the initial state needs to know what each state can still
    // reach, which is the distance computed backwards.
    let reverse = to == ReweightType::ToInitial;
    let distance = if reverse {
        shortest_distance_reverse(fst, delta)?
    } else {
        shortest_distance_forward(fst, delta)?
    };

    let total = remove_total_weight.then(|| total_weight(fst, &distance, reverse));
    match to {
        ReweightType::ToInitial => reweight_to_initial(fst, &distance),
        ReweightType::ToFinal => reweight_to_final(fst, &distance),
    }
    if let Some(total) = total {
        remove_weight(fst, &total, !reverse);
    }
    Ok(())
}

/// Moves the weights towards the initial state, in place.
///
/// Only left distributivity is asked for, since that is the only side this
/// direction divides on, which lets a weight that has one side but not the
/// other, such as the left gallic weight, be pushed.
pub fn push_weights_to_initial<A, F>(
    fst: &mut F,
    delta: f32,
    remove_total_weight: bool,
) -> Result<(), OpenFstError>
where
    A: Arc,
    A::Weight: Divide + LeftSemiring,
    F: MutableFst<A>,
    <A::Weight as Weight>::ReverseWeight: Weight<ReverseWeight = A::Weight>,
{
    let distance = shortest_distance_reverse(fst, delta)?;
    let total = remove_total_weight.then(|| total_weight(fst, &distance, true));
    reweight_to_initial(fst, &distance);
    if let Some(total) = total {
        remove_weight(fst, &total, false);
    }
    Ok(())
}

/// As [`push_weights_to_initial`], the other way.
pub fn push_weights_to_final<A, F>(
    fst: &mut F,
    delta: f32,
    remove_total_weight: bool,
) -> Result<(), OpenFstError>
where
    A: Arc,
    A::Weight: Divide + RightSemiring,
    F: MutableFst<A>,
    <A::Weight as Weight>::ReverseWeight: Weight<ReverseWeight = A::Weight>,
{
    let distance = shortest_distance_forward(fst, delta)?;
    let total = remove_total_weight.then(|| total_weight(fst, &distance, false));
    reweight_to_final(fst, &distance);
    if let Some(total) = total {
        remove_weight(fst, &total, true);
    }
    Ok(())
}

/// Moves the weights and labels of `ifst` towards the initial state.
///
/// `flags` is some combination of [`PUSH_WEIGHTS`], [`PUSH_LABELS`],
/// [`PUSH_REMOVE_TOTAL_WEIGHT`] and [`PUSH_REMOVE_COMMON_AFFIX`].
///
/// Labels travel through the **left** gallic semiring, whose string half keeps
/// the longest common prefix and so distributes on the left, which is the side
/// this direction divides on. That pairing is not a choice: upstream picks the
/// gallic type from the direction for the same reason, and here the bound makes
/// the wrong pairing fail to compile rather than to converge.
pub fn push_to_initial<A, F1, F2>(
    ifst: &F1,
    ofst: &mut F2,
    flags: u8,
    delta: f32,
) -> Result<(), OpenFstError>
where
    A: Arc,
    A::Weight: Divide + LeftSemiring + std::hash::Hash + Eq,
    F1: Fst<A>,
    F2: MutableFst<A>,
    <A::Weight as Weight>::ReverseWeight: Weight<ReverseWeight = A::Weight>,
    GallicWeight<A::Label, A::Weight, GallicLeft>: Divide + LeftSemiring,
    <GallicWeight<A::Label, A::Weight, GallicLeft> as Weight>::ReverseWeight:
        Weight<ReverseWeight = GallicWeight<A::Label, A::Weight, GallicLeft>>,
{
    push_impl::<A, GallicLeft, F1, F2>(
        ifst,
        ofst,
        ReweightType::ToInitial,
        flags,
        delta,
        reweight_to_initial,
        reweight_to_initial,
    )
}

/// Moves the weights and labels of `ifst` towards the final states.
///
/// Labels travel through the **right** gallic semiring, for the mirror of the
/// reason [`push_to_initial`] uses the left one.
pub fn push_to_final<A, F1, F2>(
    ifst: &F1,
    ofst: &mut F2,
    flags: u8,
    delta: f32,
) -> Result<(), OpenFstError>
where
    A: Arc,
    A::Weight: Divide + RightSemiring + std::hash::Hash + Eq,
    F1: Fst<A>,
    F2: MutableFst<A>,
    <A::Weight as Weight>::ReverseWeight: Weight<ReverseWeight = A::Weight>,
    GallicWeight<A::Label, A::Weight, GallicRight>: Divide + RightSemiring,
    <GallicWeight<A::Label, A::Weight, GallicRight> as Weight>::ReverseWeight:
        Weight<ReverseWeight = GallicWeight<A::Label, A::Weight, GallicRight>>,
{
    push_impl::<A, GallicRight, F1, F2>(
        ifst,
        ofst,
        ReweightType::ToFinal,
        flags,
        delta,
        reweight_to_final,
        reweight_to_final,
    )
}

/// What both directions do, with the two reweightings handed in.
///
/// SICADA-DIVERGE: upstream logs a warning and copies the input when asked to
/// push neither weights nor labels. Doing nothing is what was asked for, so it
/// is not a warning; the copy is made either way.
///
/// The reweightings are passed as function pointers rather than chosen here
/// because which one is used decides which side the semiring has to distribute
/// on, and that is a bound the caller carries. Choosing at run time would mean
/// demanding both sides of every weight.
#[allow(clippy::too_many_arguments)]
fn push_impl<A, G, F1, F2>(
    ifst: &F1,
    ofst: &mut F2,
    to: ReweightType,
    flags: u8,
    delta: f32,
    reweight_plain: fn(&mut F2, &[A::Weight]),
    reweight_gallic: fn(&mut VectorFst<GallicArc<A, G>>, &[GallicWeight<A::Label, A::Weight, G>]),
) -> Result<(), OpenFstError>
where
    A: Arc,
    A::Weight: Divide + std::hash::Hash + Eq,
    G: GallicTypeMarker,
    F1: Fst<A>,
    F2: MutableFst<A>,
    <A::Weight as Weight>::ReverseWeight: Weight<ReverseWeight = A::Weight>,
    GallicWeight<A::Label, A::Weight, G>: Divide,
    <GallicWeight<A::Label, A::Weight, G> as Weight>::ReverseWeight:
        Weight<ReverseWeight = GallicWeight<A::Label, A::Weight, G>>,
{
    let reverse = to == ReweightType::ToInitial;

    if flags & PUSH_LABELS == 0 {
        copy_fst(ifst, ofst);
        if flags & PUSH_WEIGHTS != 0 {
            let distance = distance_of(ofst, reverse, delta)?;
            let total = (flags & PUSH_REMOVE_TOTAL_WEIGHT != 0)
                .then(|| total_weight(ofst, &distance, reverse));
            reweight_plain(ofst, &distance);
            if let Some(total) = total {
                remove_weight(ofst, &total, !reverse);
            }
        }
        return Ok(());
    }

    // Labels are pushed by moving them into the weight, where the machinery for
    // weights already applies, and taking them back out again.
    let mut gfst: VectorFst<GallicArc<A, G>> = VectorFst::new();
    arc_map_to(ifst, &mut gfst, &mut ToGallicMapper::<G>::new())?;

    let gdistance = if flags & PUSH_WEIGHTS != 0 {
        distance_of(&gfst, reverse, delta)?
    } else {
        // Only the labels are being moved, so the distances have to be worked
        // out over the labels alone.
        let mut unweighted: VectorFst<A> = VectorFst::new();
        arc_map_to(ifst, &mut unweighted, &mut RmWeightMapper)?;
        let mut gunweighted: VectorFst<GallicArc<A, G>> = VectorFst::new();
        arc_map_to(
            &unweighted,
            &mut gunweighted,
            &mut ToGallicMapper::<G>::new(),
        )?;
        distance_of(&gunweighted, reverse, delta)?
    };

    let total = (flags & (PUSH_REMOVE_TOTAL_WEIGHT | PUSH_REMOVE_COMMON_AFFIX) != 0).then(|| {
        let total = total_weight(&gfst, &gdistance, reverse);
        // Only the halves that were asked for are divided out.
        GallicWeight::<A::Label, A::Weight, G>::from_parts(
            if flags & PUSH_REMOVE_COMMON_AFFIX != 0 {
                total.labels().clone()
            } else {
                StringWeight::one()
            },
            if flags & PUSH_REMOVE_TOTAL_WEIGHT != 0 {
                total.weight().clone()
            } else {
                A::Weight::one()
            },
        )
    });

    reweight_gallic(&mut gfst, &gdistance);
    if let Some(total) = total {
        remove_weight(&mut gfst, &total, to == ReweightType::ToFinal);
    }

    // A gallic weight may now hold several labels, which one arc cannot carry,
    // so it is spread over a chain of arcs before being taken apart.
    let mut factored: VectorFst<GallicArc<A, G>> = VectorFst::new();
    factor_weight(
        &gfst,
        &mut factored,
        GallicFactor::new,
        &FactorWeightOptions::<A::Label>::default(),
    );
    let mut mapper = FromGallicMapper::<A::Label, G>::new();
    arc_map_to(&factored, ofst, &mut mapper)?;
    if mapper.error() {
        return Err(OpenFstError::InvalidOperation(
            "Push: a weight came out that no single arc can carry".into(),
        ));
    }
    ofst.set_output_symbols(ifst.output_symbols());
    Ok(())
}

/// The distance from or to the final states, whichever pushing needs.
fn distance_of<A, F>(fst: &F, reverse: bool, delta: f32) -> Result<Vec<A::Weight>, OpenFstError>
where
    A: Arc,
    F: Fst<A>,
    <A::Weight as Weight>::ReverseWeight: Weight<ReverseWeight = A::Weight>,
{
    if reverse {
        shortest_distance_reverse(fst, delta)
    } else {
        shortest_distance_forward(fst, delta)
    }
}

/// Copies an FST state for state.
fn copy_fst<A, F1, F2>(ifst: &F1, ofst: &mut F2)
where
    A: Arc,
    F1: Fst<A>,
    F2: MutableFst<A>,
{
    ofst.delete_all_states();
    ofst.set_input_symbols(ifst.input_symbols());
    ofst.set_output_symbols(ifst.output_symbols());
    let mut nstates = 0usize;
    for state in ifst.states() {
        while nstates <= state.as_usize() {
            ofst.add_state();
            nstates += 1;
        }
    }
    if let Some(start) = ifst.start() {
        ofst.set_start(start);
    }
    for state in ifst.states() {
        ofst.set_final(state, ifst.final_weight(state));
        for arc in ifst.arcs(state) {
            ofst.add_arc(state, arc);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::algorithms::shortest_distance::SHORTEST_DELTA;
    use crate::algorithms::test_support::{Rng, paths, random_acyclic_fst, sorted};
    use crate::arc::StdArc;
    use crate::fst::ExpandedFst as _;
    use crate::fsts::vector_fst::StdVectorFst;
    use crate::properties::K_FST_PROPERTIES;
    use crate::weights::float_weight::TropicalWeight;

    /// The arcs and final weight leaving each non-initial state should come to
    /// One once the weight has been pushed to the front.
    fn leaving(fst: &StdVectorFst, state: i32) -> TropicalWeight {
        let mut sum = fst.final_weight(state);
        for arc in fst.arcs(state) {
            sum = sum.plus(arc.weight());
        }
        sum
    }

    /// Two branches costing 1 and 3, joining and then costing 2 more.
    fn branches() -> StdVectorFst {
        let mut fst = StdVectorFst::new();
        for _ in 0..4 {
            fst.add_state();
        }
        fst.set_start(0);
        fst.add_arc(0, StdArc::new(1, 1, TropicalWeight(1.0), 1));
        fst.add_arc(0, StdArc::new(2, 2, TropicalWeight(3.0), 2));
        fst.add_arc(1, StdArc::new(3, 3, TropicalWeight(2.0), 3));
        fst.add_arc(2, StdArc::new(4, 4, TropicalWeight(2.0), 3));
        fst.set_final(3, TropicalWeight::one());
        fst.properties(K_FST_PROPERTIES, true);
        fst
    }

    fn pushed(to: ReweightType, remove_total: bool) -> StdVectorFst {
        let mut fst = branches();
        push_weights(&mut fst, to, SHORTEST_DELTA, remove_total).unwrap();
        fst
    }

    /// Pushing to the front commits weight as early as it can, so every
    /// non-initial state's outgoing weight sums to One.
    #[test]
    fn pushing_to_the_initial_state_leaves_one_behind_every_state() {
        let fst = pushed(ReweightType::ToInitial, true);
        for state in 1..fst.num_states() as i32 {
            assert_eq!(leaving(&fst, state), TropicalWeight::one(), "state {state}");
        }
        // The lighter branch is now free, and the difference sits on the other.
        let from_start: Vec<f32> = fst.arcs(0).map(|a| a.weight().value()).collect();
        assert_eq!(from_start, vec![0.0, 2.0]);
    }

    /// Pushing does not change what the FST accepts, only where the weight
    /// sits along each path.
    #[test]
    fn pushing_does_not_change_the_paths() {
        let before = sorted(paths(&branches(), 12));
        assert_eq!(
            sorted(paths(&pushed(ReweightType::ToInitial, false), 12)),
            before
        );
        assert_eq!(
            sorted(paths(&pushed(ReweightType::ToFinal, false), 12)),
            before
        );
    }

    /// Dividing the total out makes the shortest path weigh One; the others
    /// weigh what they cost above it.
    #[test]
    fn removing_the_total_weight_makes_the_best_path_free() {
        for to in [ReweightType::ToInitial, ReweightType::ToFinal] {
            let fst = pushed(to, true);
            let mut weights: Vec<f32> = paths(&fst, 12)
                .into_iter()
                .map(|(_, _, weight)| weight.value())
                .collect();
            weights.sort_by(f32::total_cmp);
            assert_eq!(weights, vec![0.0, 2.0], "{to:?}");
        }
    }

    /// Whatever the FST, pushing changes only where the weight sits.
    #[test]
    fn pushing_never_changes_what_a_path_costs_in_total() {
        let mut rng = Rng::new(0x00F0_54ED_u64);
        for round in 0..200 {
            let fst = random_acyclic_fst(&mut rng, 6);
            let before = sorted(paths(&fst, 12));
            if before.is_empty() {
                continue;
            }
            for to in [ReweightType::ToInitial, ReweightType::ToFinal] {
                let mut copy = fst.clone();
                push_weights(&mut copy, to, SHORTEST_DELTA, false).unwrap();
                assert_eq!(sorted(paths(&copy, 12)), before, "round {round}, {to:?}");
            }
        }
    }

    /// Pushing labels moves the output side towards the front, leaving what the
    /// FST transduces unchanged.
    #[test]
    fn pushing_labels_moves_the_output_side_forward() {
        // 0 -a:eps-> 1 -b:xy-> 2, final.
        let mut fst = StdVectorFst::new();
        for _ in 0..3 {
            fst.add_state();
        }
        fst.set_start(0);
        fst.add_arc(0, StdArc::new(1, 0, TropicalWeight::one(), 1));
        fst.add_arc(1, StdArc::new(2, 7, TropicalWeight::one(), 2));
        fst.set_final(2, TropicalWeight::one());
        fst.properties(K_FST_PROPERTIES, true);

        let before = sorted(paths(&fst, 12));
        let mut out = StdVectorFst::new();
        push_to_initial(&fst, &mut out, PUSH_LABELS, SHORTEST_DELTA).unwrap();

        // The output label 7 has moved onto the first arc.
        let first: Vec<i32> = out.arcs(out.start().unwrap()).map(|a| a.olabel()).collect();
        assert_eq!(first, vec![7]);

        // And the transduction is the same, once epsilons are set aside.
        let visible =
            |paths: Vec<(Vec<i32>, Vec<i32>, String)>| -> Vec<(Vec<i32>, Vec<i32>, String)> {
                paths
                    .into_iter()
                    .map(|(i, o, w)| {
                        (
                            i.into_iter().filter(|l| *l != 0).collect(),
                            o.into_iter().filter(|l| *l != 0).collect(),
                            w,
                        )
                    })
                    .collect()
            };
        assert_eq!(visible(sorted(paths(&out, 12))), visible(before));
    }

    /// Pushing labels and weights together leaves the transduction alone.
    #[test]
    fn pushing_labels_and_weights_keeps_the_transduction() {
        let mut rng = Rng::new(0x001A_B315_u64);
        let visible =
            |paths: Vec<(Vec<i32>, Vec<i32>, String)>| -> Vec<(Vec<i32>, Vec<i32>, String)> {
                paths
                    .into_iter()
                    .map(|(i, o, w)| {
                        (
                            i.into_iter().filter(|l| *l != 0).collect(),
                            o.into_iter().filter(|l| *l != 0).collect(),
                            w,
                        )
                    })
                    .collect()
            };
        for round in 0..100 {
            let fst = random_acyclic_fst(&mut rng, 5);
            let before = visible(sorted(paths(&fst, 12)));
            if before.is_empty() {
                continue;
            }
            let mut out = StdVectorFst::new();
            push_to_initial(&fst, &mut out, PUSH_LABELS | PUSH_WEIGHTS, SHORTEST_DELTA).unwrap();
            assert_eq!(visible(sorted(paths(&out, 12))), before, "round {round}");
        }
    }

    /// Asking for neither weights nor labels copies the FST.
    #[test]
    fn pushing_nothing_copies_the_fst() {
        let fst = branches();
        let mut out = StdVectorFst::new();
        push_to_initial(&fst, &mut out, 0, SHORTEST_DELTA).unwrap();
        assert_eq!(sorted(paths(&out, 12)), sorted(paths(&fst, 12)));
        assert_eq!(out.num_states(), fst.num_states());
    }

    /// The total weight read off a distance vector is the sum over accepting
    /// paths, whichever end it was computed from.
    #[test]
    fn the_total_weight_is_the_same_from_either_end() {
        let fst = branches();
        let forward = shortest_distance_forward(&fst, SHORTEST_DELTA).unwrap();
        let backward = shortest_distance_reverse(&fst, SHORTEST_DELTA).unwrap();
        assert_eq!(
            total_weight(&fst, &forward, false),
            total_weight(&fst, &backward, true)
        );
        assert_eq!(total_weight(&fst, &forward, false), TropicalWeight(3.0));
    }
}