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
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
//! Rewriting every arc of an FST through a mapper.
//!
//! Port of OpenFst's `arc-map.h`. A great many operations amount to "do the
//! same thing to each arc": invert the weights, drop them, quantize them, make
//! the input side epsilon. This is the shape they all share. What varies
//! between them is a [`ArcMapper`], and what makes the pattern non-trivial is
//! the final weights: some mappings turn a final weight into something an FST
//! cannot store as a final weight, and then a superfinal state is needed.

use std::marker::PhantomData;

use crate::arc::{Arc, ArcLabel, ArcStateId, GallicArc};
use crate::error::OpenFstError;
use crate::fst::{Fst, MutableFst};
use crate::properties::{
    K_ADD_SUPER_FINAL_PROPERTIES, K_COPY_PROPERTIES, K_ERROR, K_FST_PROPERTIES,
    K_O_LABEL_INVARIANT_PROPERTIES, K_WEIGHT_INVARIANT_PROPERTIES, project_properties,
};
use crate::weight::{Divide, DivideType, Weight};
use crate::weights::string_weight::{
    GallicTypeMarker, GallicWeight, StringWeight, StringWeightValue,
};

/// What a mapper needs done about final weights.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MapFinalAction {
    /// The mapped final weight is still a final weight. A mapper that could
    /// produce labels here is a mistake.
    NoSuperfinal,
    /// The mapped final weight may come out as an arc, in which case it goes
    /// to a superfinal state, added only if some state actually needs it.
    AllowSuperfinal,
    /// The mapped final weight always goes to a superfinal state, which is
    /// therefore always added.
    RequireSuperfinal,
}

/// What a mapper needs done about symbol tables.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MapSymbolsAction {
    /// The mapping changes what the labels mean, so the tables are dropped.
    Clear,
    /// The labels keep their meanings, so the tables carry over.
    Copy,
    /// The mapper sets them itself.
    Noop,
}

/// Turns one arc into another.
///
/// A final weight is offered as an arc `(epsilon, epsilon, weight, no_state)`,
/// and what the mapper does with it is governed by
/// [`final_action`](Self::final_action).
pub trait ArcMapper<From: Arc, To: Arc> {
    /// Maps one arc.
    fn map(&mut self, arc: &From) -> To;

    /// What to do about final weights.
    fn final_action(&self) -> MapFinalAction;

    /// What to do about the input symbol table.
    fn input_symbols_action(&self) -> MapSymbolsAction {
        MapSymbolsAction::Copy
    }

    /// What to do about the output symbol table.
    fn output_symbols_action(&self) -> MapSymbolsAction {
        MapSymbolsAction::Copy
    }

    /// The properties the result has, given those of the input.
    fn properties(&self, props: u64) -> u64;
}

/// A final weight, offered to a mapper in the shape of an arc.
fn final_as_arc<From: Arc>(weight: From::Weight) -> From {
    From::new(
        From::Label::epsilon(),
        From::Label::epsilon(),
        weight,
        From::StateId::no_state(),
    )
}

/// Whether a mapped final weight came back as something that has to become an
/// arc rather than a final weight.
fn became_an_arc<To: Arc>(arc: &To) -> bool {
    arc.ilabel() != To::Label::epsilon() || arc.olabel() != To::Label::epsilon()
}

/// Rewrites every arc of `fst` in place.
///
/// SICADA-DIVERGE: upstream reports a mapper that produces labels where it
/// promised not to by setting `K_ERROR` and carrying on to the next state, so
/// the FST ends up half mapped and marked broken. Here it is an error.
pub fn arc_map<A, F, M>(fst: &mut F, mapper: &mut M) -> Result<(), OpenFstError>
where
    A: Arc,
    F: MutableFst<A>,
    M: ArcMapper<A, A>,
{
    if mapper.input_symbols_action() == MapSymbolsAction::Clear {
        fst.set_input_symbols(None);
    }
    if mapper.output_symbols_action() == MapSymbolsAction::Clear {
        fst.set_output_symbols(None);
    }
    if fst.start().is_none() {
        return Ok(());
    }

    let props = fst.properties(K_FST_PROPERTIES, false);
    let final_action = mapper.final_action();
    let zero = A::Weight::zero();

    let mut superfinal = None;
    if final_action == MapFinalAction::RequireSuperfinal {
        let s = fst.add_state();
        fst.set_final(s, A::Weight::one());
        superfinal = Some(s);
    }

    let states: Vec<A::StateId> = fst.states().collect();
    for state in states {
        // The mapper is borrowed by the closure, so the error it may raise is
        // collected rather than returned from inside.
        let mut label_error = false;
        fst.mutate_arcs(state, |arc| *arc = mapper.map(arc));

        if Some(state) == superfinal {
            continue;
        }
        let mapped = mapper.map(&final_as_arc::<A>(fst.final_weight(state)));
        match final_action {
            MapFinalAction::NoSuperfinal => {
                if became_an_arc(&mapped) {
                    label_error = true;
                } else {
                    fst.set_final(state, mapped.weight().clone());
                }
            }
            MapFinalAction::AllowSuperfinal => {
                if became_an_arc(&mapped) {
                    let target = match superfinal {
                        Some(s) => s,
                        None => {
                            let s = fst.add_state();
                            fst.set_final(s, A::Weight::one());
                            superfinal = Some(s);
                            s
                        }
                    };
                    fst.add_arc(
                        state,
                        A::new(
                            mapped.ilabel(),
                            mapped.olabel(),
                            mapped.weight().clone(),
                            target,
                        ),
                    );
                    fst.set_final(state, zero.clone());
                } else {
                    fst.set_final(state, mapped.weight().clone());
                }
            }
            MapFinalAction::RequireSuperfinal => {
                let target = superfinal.expect("a superfinal state was added above");
                if became_an_arc(&mapped) || *mapped.weight() != zero {
                    fst.add_arc(
                        state,
                        A::new(
                            mapped.ilabel(),
                            mapped.olabel(),
                            mapped.weight().clone(),
                            target,
                        ),
                    );
                }
                fst.set_final(state, zero.clone());
            }
        }
        if label_error {
            return Err(OpenFstError::InvalidOperation(
                "ArcMap: the mapper produced a labelled arc from a final weight, but said it \
                 would not need a superfinal state"
                    .to_string(),
            ));
        }
    }

    fst.set_properties(mapper.properties(props), K_FST_PROPERTIES);
    Ok(())
}

/// Rewrites every arc of `ifst` into `ofst`, which may have a different arc
/// type.
pub fn arc_map_to<From, To, F1, F2, M>(
    ifst: &F1,
    ofst: &mut F2,
    mapper: &mut M,
) -> Result<(), OpenFstError>
where
    From: Arc,
    To: Arc<StateId = From::StateId>,
    F1: Fst<From>,
    F2: MutableFst<To>,
    M: ArcMapper<From, To>,
{
    ofst.delete_all_states();
    match mapper.input_symbols_action() {
        MapSymbolsAction::Copy => ofst.set_input_symbols(ifst.input_symbols()),
        MapSymbolsAction::Clear => ofst.set_input_symbols(None),
        MapSymbolsAction::Noop => {}
    }
    match mapper.output_symbols_action() {
        MapSymbolsAction::Copy => ofst.set_output_symbols(ifst.output_symbols()),
        MapSymbolsAction::Clear => ofst.set_output_symbols(None),
        MapSymbolsAction::Noop => {}
    }

    let iprops = ifst.properties(K_COPY_PROPERTIES, false);
    let Some(start) = ifst.start() else {
        return Ok(());
    };

    let final_action = mapper.final_action();
    let zero = To::Weight::zero();
    if let Some(num_states) = ifst.num_states_if_known() {
        ofst.reserve_states(num_states + usize::from(final_action != MapFinalAction::NoSuperfinal));
    }
    for _ in ifst.states() {
        ofst.add_state();
    }
    let mut superfinal = None;
    if final_action == MapFinalAction::RequireSuperfinal {
        let s = ofst.add_state();
        ofst.set_final(s, To::Weight::one());
        superfinal = Some(s);
    }

    for state in ifst.states() {
        if state == start {
            ofst.set_start(state);
        }
        ofst.reserve_arcs(
            state,
            ifst.num_arcs(state) + usize::from(final_action != MapFinalAction::NoSuperfinal),
        );
        for arc in ifst.arcs(state) {
            let mapped = mapper.map(&arc);
            ofst.add_arc(state, mapped);
        }

        let mapped = mapper.map(&final_as_arc::<From>(ifst.final_weight(state)));
        match final_action {
            MapFinalAction::NoSuperfinal => {
                if became_an_arc(&mapped) {
                    return Err(OpenFstError::InvalidOperation(
                        "ArcMap: the mapper produced a labelled arc from a final weight, but \
                         said it would not need a superfinal state"
                            .to_string(),
                    ));
                }
                ofst.set_final(state, mapped.weight().clone());
            }
            MapFinalAction::AllowSuperfinal => {
                if became_an_arc(&mapped) {
                    let target = match superfinal {
                        Some(s) => s,
                        None => {
                            let s = ofst.add_state();
                            ofst.set_final(s, To::Weight::one());
                            superfinal = Some(s);
                            s
                        }
                    };
                    ofst.add_arc(
                        state,
                        To::new(
                            mapped.ilabel(),
                            mapped.olabel(),
                            mapped.weight().clone(),
                            target,
                        ),
                    );
                    ofst.set_final(state, zero.clone());
                } else {
                    ofst.set_final(state, mapped.weight().clone());
                }
            }
            MapFinalAction::RequireSuperfinal => {
                let target = superfinal.expect("a superfinal state was added above");
                if became_an_arc(&mapped) || *mapped.weight() != zero {
                    ofst.add_arc(
                        state,
                        To::new(
                            mapped.ilabel(),
                            mapped.olabel(),
                            mapped.weight().clone(),
                            target,
                        ),
                    );
                }
                ofst.set_final(state, zero.clone());
            }
        }
    }

    let oprops = ofst.properties(K_FST_PROPERTIES, false);
    ofst.set_properties(mapper.properties(iprops) | oprops, K_FST_PROPERTIES);
    Ok(())
}

/// Leaves every arc as it is.
#[derive(Debug, Clone, Copy, Default)]
pub struct IdentityArcMapper;

impl<A: Arc> ArcMapper<A, A> for IdentityArcMapper {
    #[inline]
    fn map(&mut self, arc: &A) -> A {
        arc.clone()
    }

    fn final_action(&self) -> MapFinalAction {
        MapFinalAction::NoSuperfinal
    }

    fn properties(&self, props: u64) -> u64 {
        props
    }
}

/// Makes the input side of every arc epsilon.
#[derive(Debug, Clone, Copy, Default)]
pub struct InputEpsilonMapper;

impl<A: Arc> ArcMapper<A, A> for InputEpsilonMapper {
    #[inline]
    fn map(&mut self, arc: &A) -> A {
        A::new(
            A::Label::epsilon(),
            arc.olabel(),
            arc.weight().clone(),
            arc.nextstate(),
        )
    }

    fn final_action(&self) -> MapFinalAction {
        MapFinalAction::NoSuperfinal
    }

    fn input_symbols_action(&self) -> MapSymbolsAction {
        MapSymbolsAction::Clear
    }

    fn properties(&self, props: u64) -> u64 {
        crate::properties::project_properties(props, false)
            & crate::properties::K_SET_ARC_PROPERTIES
            | crate::properties::K_I_EPSILONS
            | crate::properties::K_I_LABEL_SORTED
    }
}

/// Makes the output side of every arc epsilon.
#[derive(Debug, Clone, Copy, Default)]
pub struct OutputEpsilonMapper;

impl<A: Arc> ArcMapper<A, A> for OutputEpsilonMapper {
    #[inline]
    fn map(&mut self, arc: &A) -> A {
        A::new(
            arc.ilabel(),
            A::Label::epsilon(),
            arc.weight().clone(),
            arc.nextstate(),
        )
    }

    fn final_action(&self) -> MapFinalAction {
        MapFinalAction::NoSuperfinal
    }

    fn output_symbols_action(&self) -> MapSymbolsAction {
        MapSymbolsAction::Clear
    }

    fn properties(&self, props: u64) -> u64 {
        crate::properties::project_properties(props, true) & crate::properties::K_SET_ARC_PROPERTIES
            | crate::properties::K_O_EPSILONS
            | crate::properties::K_O_LABEL_SORTED
    }
}

/// Leaves arcs alone but forces every final weight through a superfinal state.
///
/// The result has exactly one final state, as an operation wanting a single
/// accepting point, such as concatenation or closure, requires.
#[derive(Debug, Clone, Copy, Default)]
pub struct SuperFinalMapper;

impl<A: Arc> ArcMapper<A, A> for SuperFinalMapper {
    #[inline]
    fn map(&mut self, arc: &A) -> A {
        arc.clone()
    }

    fn final_action(&self) -> MapFinalAction {
        MapFinalAction::RequireSuperfinal
    }

    fn properties(&self, props: u64) -> u64 {
        props & crate::properties::K_ADD_SUPER_FINAL_PROPERTIES
    }
}

/// Multiplies a weight onto every arc and final weight, on the left.
#[derive(Debug, Clone)]
pub struct TimesMapper<W> {
    weight: W,
}

impl<W: Weight> TimesMapper<W> {
    /// Multiplies by `weight`.
    pub fn new(weight: W) -> Self {
        Self { weight }
    }
}

impl<A: Arc> ArcMapper<A, A> for TimesMapper<A::Weight> {
    #[inline]
    fn map(&mut self, arc: &A) -> A {
        A::new(
            arc.ilabel(),
            arc.olabel(),
            self.weight.times(arc.weight()),
            arc.nextstate(),
        )
    }

    fn final_action(&self) -> MapFinalAction {
        MapFinalAction::NoSuperfinal
    }

    fn properties(&self, props: u64) -> u64 {
        props & crate::properties::K_WEIGHT_INVARIANT_PROPERTIES
    }
}

/// Adds a weight to every arc and final weight.
#[derive(Debug, Clone)]
pub struct PlusMapper<W> {
    weight: W,
}

impl<W: Weight> PlusMapper<W> {
    /// Adds `weight`.
    pub fn new(weight: W) -> Self {
        Self { weight }
    }
}

impl<A: Arc> ArcMapper<A, A> for PlusMapper<A::Weight> {
    #[inline]
    fn map(&mut self, arc: &A) -> A {
        A::new(
            arc.ilabel(),
            arc.olabel(),
            self.weight.plus(arc.weight()),
            arc.nextstate(),
        )
    }

    fn final_action(&self) -> MapFinalAction {
        MapFinalAction::NoSuperfinal
    }

    fn properties(&self, props: u64) -> u64 {
        props & crate::properties::K_WEIGHT_INVARIANT_PROPERTIES
    }
}

/// Raises every weight to a power.
#[derive(Debug, Clone, Copy)]
pub struct PowerMapper {
    power: f64,
}

impl PowerMapper {
    /// Raises to `power`.
    pub fn new(power: f64) -> Self {
        Self { power }
    }
}

impl<A: Arc> ArcMapper<A, A> for PowerMapper
where
    A::Weight: Weight,
{
    fn map(&mut self, arc: &A) -> A {
        A::new(
            arc.ilabel(),
            arc.olabel(),
            power(arc.weight(), self.power),
            arc.nextstate(),
        )
    }

    fn final_action(&self) -> MapFinalAction {
        MapFinalAction::NoSuperfinal
    }

    fn properties(&self, props: u64) -> u64 {
        props & crate::properties::K_WEIGHT_INVARIANT_PROPERTIES
    }
}

/// A weight multiplied by itself `n` times.
///
/// SICADA-DIVERGE: upstream's `Power` is a free function over `size_t`
/// exponents. A fractional power is only meaningful for the float weights,
/// where it is a multiplication in the log domain; the general case is repeated
/// multiplication, which is what this does once the exponent is rounded.
fn power<W: Weight>(weight: &W, n: f64) -> W {
    let n = n.max(0.0).round() as u64;
    let mut result = W::one();
    for _ in 0..n {
        result = result.times(weight);
    }
    result
}

/// Replaces every weight by its inverse.
#[derive(Debug, Clone, Copy, Default)]
pub struct InvertWeightMapper;

impl<A: Arc> ArcMapper<A, A> for InvertWeightMapper
where
    A::Weight: Divide,
{
    #[inline]
    fn map(&mut self, arc: &A) -> A {
        A::new(
            arc.ilabel(),
            arc.olabel(),
            A::Weight::one().divide(arc.weight(), DivideType::Any),
            arc.nextstate(),
        )
    }

    fn final_action(&self) -> MapFinalAction {
        MapFinalAction::NoSuperfinal
    }

    fn properties(&self, props: u64) -> u64 {
        props & crate::properties::K_WEIGHT_INVARIANT_PROPERTIES
    }
}

/// Drops the weights, leaving an unweighted FST.
#[derive(Debug, Clone, Copy, Default)]
pub struct RmWeightMapper;

impl<A: Arc> ArcMapper<A, A> for RmWeightMapper {
    #[inline]
    fn map(&mut self, arc: &A) -> A {
        let weight = if *arc.weight() == A::Weight::zero() {
            A::Weight::zero()
        } else {
            A::Weight::one()
        };
        A::new(arc.ilabel(), arc.olabel(), weight, arc.nextstate())
    }

    fn final_action(&self) -> MapFinalAction {
        MapFinalAction::NoSuperfinal
    }

    fn properties(&self, props: u64) -> u64 {
        (props & crate::properties::K_WEIGHT_INVARIANT_PROPERTIES) | crate::properties::K_UNWEIGHTED
    }
}

/// Rounds every weight to a multiple of `delta`.
#[derive(Debug, Clone, Copy)]
pub struct QuantizeMapper {
    delta: f32,
}

impl Default for QuantizeMapper {
    fn default() -> Self {
        Self {
            delta: crate::weight::DELTA,
        }
    }
}

impl QuantizeMapper {
    /// Rounds to a multiple of `delta`.
    pub fn new(delta: f32) -> Self {
        Self { delta }
    }
}

impl<A: Arc> ArcMapper<A, A> for QuantizeMapper {
    #[inline]
    fn map(&mut self, arc: &A) -> A {
        A::new(
            arc.ilabel(),
            arc.olabel(),
            arc.weight().quantize(self.delta),
            arc.nextstate(),
        )
    }

    fn final_action(&self) -> MapFinalAction {
        MapFinalAction::NoSuperfinal
    }

    fn properties(&self, props: u64) -> u64 {
        props & crate::properties::K_WEIGHT_INVARIANT_PROPERTIES
    }
}

/// Replaces every weight by its reverse, and swaps the arc's sides.
///
/// The reverse of a weight lives in a different semiring in general, which is
/// why this maps between arc types.
#[derive(Debug, Clone, Copy, Default)]
pub struct ReverseWeightMapper;

impl<From, To> ArcMapper<From, To> for ReverseWeightMapper
where
    From: Arc,
    To: Arc<
            Label = From::Label,
            StateId = From::StateId,
            Weight = <From::Weight as Weight>::ReverseWeight,
        >,
{
    #[inline]
    fn map(&mut self, arc: &From) -> To {
        To::new(
            arc.ilabel(),
            arc.olabel(),
            arc.weight().reverse(),
            arc.nextstate(),
        )
    }

    fn final_action(&self) -> MapFinalAction {
        MapFinalAction::NoSuperfinal
    }

    fn properties(&self, props: u64) -> u64 {
        props
    }
}

/// Keeps the arcs and copies the symbol tables, for a mapper that only changes
/// the weight type.
#[derive(Debug, Clone, Copy, Default)]
/// SICADA-DIVERGE: upstream's `WeightConvert` is a template that fails to
/// compile unless specialized, which is how it reports "these two weights are
/// unrelated". `From` says the same thing, the compiler's message names the two
/// types rather than a template, and every pair `weights/float_weight.rs`
/// already relates through `impl From` is usable here without a second trait
/// having to repeat them.
pub struct WeightConvertMapper<To> {
    _marker: std::marker::PhantomData<To>,
}

impl<To> WeightConvertMapper<To> {
    /// Converts weights into `To`'s weight type.
    pub fn new() -> Self {
        Self {
            _marker: std::marker::PhantomData,
        }
    }
}

impl<A1, A2> ArcMapper<A1, A2> for WeightConvertMapper<A2>
where
    A1: Arc,
    A2: Arc<Label = A1::Label, StateId = A1::StateId>,
    A2::Weight: From<A1::Weight>,
{
    #[inline]
    fn map(&mut self, arc: &A1) -> A2 {
        A2::new(
            arc.ilabel(),
            arc.olabel(),
            A2::Weight::from(arc.weight().clone()),
            arc.nextstate(),
        )
    }

    fn final_action(&self) -> MapFinalAction {
        MapFinalAction::NoSuperfinal
    }

    fn properties(&self, props: u64) -> u64 {
        props
    }
}

/// Moves an arc's output label into its weight, making an acceptor over the
/// gallic semiring.
///
/// The gallic semiring pairs a label sequence with a weight, so an arc's output
/// side becomes part of what a shortest-distance computation sums over. That is
/// what lets labels be pushed the way weights are.
#[derive(Debug, Clone, Copy, Default)]
pub struct ToGallicMapper<G> {
    _marker: PhantomData<G>,
}

impl<G> ToGallicMapper<G> {
    /// Creates the mapper.
    pub fn new() -> Self {
        Self {
            _marker: PhantomData,
        }
    }
}

impl<A, G> ArcMapper<A, GallicArc<A, G>> for ToGallicMapper<G>
where
    A: Arc,
    G: GallicTypeMarker,
{
    fn map(&mut self, arc: &A) -> GallicArc<A, G> {
        let epsilon = A::Label::epsilon();
        let no_state = A::StateId::no_state();
        let gallic = |labels: StringWeight<A::Label, G::StringType>, weight: A::Weight| {
            GallicWeight::<A::Label, A::Weight, G>::from_parts(labels, weight)
        };
        if arc.nextstate() == no_state {
            // A final weight, offered as an arc. A state that is not final has
            // nothing to carry across.
            return if *arc.weight() == A::Weight::zero() {
                GallicArc::new(epsilon, epsilon, GallicWeight::zero(), no_state)
            } else {
                GallicArc::new(
                    epsilon,
                    epsilon,
                    gallic(StringWeight::one(), arc.weight().clone()),
                    no_state,
                )
            };
        }
        // The result is an acceptor over the input side; the output label moves
        // into the weight, and an epsilon output contributes no label at all.
        let labels = if arc.olabel() == epsilon {
            StringWeight::one()
        } else {
            StringWeight::new(vec![arc.olabel()])
        };
        GallicArc::new(
            arc.ilabel(),
            arc.ilabel(),
            gallic(labels, arc.weight().clone()),
            arc.nextstate(),
        )
    }

    fn final_action(&self) -> MapFinalAction {
        MapFinalAction::NoSuperfinal
    }

    fn input_symbols_action(&self) -> MapSymbolsAction {
        MapSymbolsAction::Copy
    }

    fn output_symbols_action(&self) -> MapSymbolsAction {
        // The output labels are now inside the weights, so a table keyed on
        // them no longer describes anything the FST has.
        MapSymbolsAction::Clear
    }

    fn properties(&self, props: u64) -> u64 {
        project_properties(props, true) & K_WEIGHT_INVARIANT_PROPERTIES
    }
}

/// Moves the label out of a gallic weight and back onto the arc's output side.
///
/// SICADA-DIVERGE: upstream reports a weight carrying more than one label, which
/// no single arc can represent, by setting a `mutable bool error_` from a
/// `const` member function and returning an arc built from the values it just
/// failed to extract. The failure is carried here and turned into an error by
/// [`arc_map`], which is the only thing that can act on it.
#[derive(Debug, Clone)]
pub struct FromGallicMapper<L, G> {
    /// The input label to give an arc that came from a final weight carrying a
    /// label.
    superfinal_label: L,
    /// Whether a weight was met that no arc can carry.
    error: bool,
    _marker: PhantomData<G>,
}

impl<L: ArcLabel, G> FromGallicMapper<L, G> {
    /// Uses epsilon as the label of an arc made from a labelled final weight.
    pub fn new() -> Self {
        Self::with_superfinal_label(L::epsilon())
    }

    /// Uses `superfinal_label` for such an arc.
    pub fn with_superfinal_label(superfinal_label: L) -> Self {
        Self {
            superfinal_label,
            error: false,
            _marker: PhantomData,
        }
    }

    /// Whether a weight was met that no single arc can carry.
    pub fn error(&self) -> bool {
        self.error
    }
}

impl<L: ArcLabel, G> Default for FromGallicMapper<L, G> {
    fn default() -> Self {
        Self::new()
    }
}

impl<A, G> ArcMapper<GallicArc<A, G>, A> for FromGallicMapper<A::Label, G>
where
    A: Arc,
    G: GallicTypeMarker,
{
    fn map(&mut self, arc: &GallicArc<A, G>) -> A {
        let epsilon = A::Label::epsilon();
        let no_state = A::StateId::no_state();
        if arc.nextstate() == no_state && *arc.weight() == GallicWeight::zero() {
            // A state that was not final stays not final.
            return A::new(arc.ilabel(), epsilon, A::Weight::zero(), no_state);
        }
        // A gallic weight an arc can carry holds at most one label.
        let (label, weight) = match &arc.weight().labels().value {
            StringWeightValue::Labels(labels) if labels.len() <= 1 => (
                labels.first().copied().unwrap_or(epsilon),
                arc.weight().weight().clone(),
            ),
            _ => {
                self.error = true;
                (epsilon, A::Weight::zero())
            }
        };
        if arc.ilabel() != arc.olabel() {
            self.error = true;
        }
        if arc.ilabel() == epsilon && label != epsilon && arc.nextstate() == no_state {
            // A final weight carrying a label has to become a real arc, and
            // needs an input label to go on it.
            A::new(self.superfinal_label, label, weight, arc.nextstate())
        } else {
            A::new(arc.ilabel(), label, weight, arc.nextstate())
        }
    }

    fn final_action(&self) -> MapFinalAction {
        MapFinalAction::AllowSuperfinal
    }

    fn input_symbols_action(&self) -> MapSymbolsAction {
        MapSymbolsAction::Copy
    }

    fn output_symbols_action(&self) -> MapSymbolsAction {
        MapSymbolsAction::Clear
    }

    fn properties(&self, props: u64) -> u64 {
        let out = props
            & K_O_LABEL_INVARIANT_PROPERTIES
            & K_WEIGHT_INVARIANT_PROPERTIES
            & K_ADD_SUPER_FINAL_PROPERTIES;
        if self.error { out | K_ERROR } else { out }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::AtomicRc;
    use crate::arc::StdArc;
    use crate::fst::ExpandedFst as _;
    use crate::fsts::vector_fst::StdVectorFst;
    use crate::properties::{K_ACCEPTOR, K_NOT_ACCEPTOR, K_UNWEIGHTED};
    use crate::symbol_table::SymbolTable;
    use crate::weights::float_weight::TropicalWeight;

    fn fst() -> StdVectorFst {
        let mut fst = StdVectorFst::new();
        for _ in 0..3 {
            fst.add_state();
        }
        fst.set_start(0);
        fst.add_arc(0, StdArc::new(1, 2, TropicalWeight(1.0), 1));
        fst.add_arc(1, StdArc::new(3, 4, TropicalWeight(2.0), 2));
        fst.set_final(2, TropicalWeight(3.0));
        fst
    }

    fn arcs(fst: &StdVectorFst) -> Vec<(i32, i32, f32, i32)> {
        (0..fst.num_states() as i32)
            .flat_map(|s| {
                fst.arcs(s)
                    .map(|a| (a.ilabel(), a.olabel(), a.weight().value(), a.nextstate()))
                    .collect::<Vec<_>>()
            })
            .collect()
    }

    #[test]
    fn the_identity_mapper_changes_nothing() {
        let mut fst = fst();
        let before = arcs(&fst);
        arc_map(&mut fst, &mut IdentityArcMapper).unwrap();
        assert_eq!(arcs(&fst), before);
        assert_eq!(fst.final_weight(2), TropicalWeight(3.0));
    }

    #[test]
    fn the_epsilon_mappers_blank_one_side_and_drop_its_table() {
        let mut table = SymbolTable::new("input".to_string());
        table.add_symbol("<eps>", 0);

        let mut with_tables = fst();
        with_tables.set_input_symbols(Some(AtomicRc::new(table.clone())));
        with_tables.set_output_symbols(Some(AtomicRc::new(table)));

        arc_map(&mut with_tables, &mut InputEpsilonMapper).unwrap();
        assert_eq!(
            arcs(&with_tables),
            vec![(0, 2, 1.0, 1), (0, 4, 2.0, 2)],
            "the input side is blanked"
        );
        assert!(
            with_tables.input_symbols().is_none(),
            "its table went with it"
        );
        assert!(with_tables.output_symbols().is_some());

        let mut other = fst();
        arc_map(&mut other, &mut OutputEpsilonMapper).unwrap();
        assert_eq!(arcs(&other), vec![(1, 0, 1.0, 1), (3, 0, 2.0, 2)]);
    }

    #[test]
    fn removing_weights_leaves_an_unweighted_fst() {
        let mut fst = fst();
        arc_map(&mut fst, &mut RmWeightMapper).unwrap();
        assert_eq!(arcs(&fst), vec![(1, 2, 0.0, 1), (3, 4, 0.0, 2)]);
        assert_eq!(fst.final_weight(2), TropicalWeight::one());
        assert_ne!(fst.properties(K_UNWEIGHTED, false) & K_UNWEIGHTED, 0);
    }

    /// In the tropical semiring times is addition, so this adds a constant to
    /// every arc, and to the final weights, which is the point of the final
    /// action.
    #[test]
    fn multiplying_reaches_the_final_weights_too() {
        let mut fst = fst();
        arc_map(&mut fst, &mut TimesMapper::new(TropicalWeight(10.0))).unwrap();
        assert_eq!(arcs(&fst), vec![(1, 2, 11.0, 1), (3, 4, 12.0, 2)]);
        assert_eq!(fst.final_weight(2), TropicalWeight(13.0));
        // A non-final state stays non-final: Zero times anything is Zero.
        assert_eq!(fst.final_weight(0), TropicalWeight::zero());
    }

    /// Tropical plus is min, so this caps every weight.
    #[test]
    fn adding_caps_every_weight() {
        let mut fst = fst();
        arc_map(&mut fst, &mut PlusMapper::new(TropicalWeight(1.5))).unwrap();
        assert_eq!(arcs(&fst), vec![(1, 2, 1.0, 1), (3, 4, 1.5, 2)]);
        assert_eq!(fst.final_weight(2), TropicalWeight(1.5));
    }

    #[test]
    fn quantizing_rounds_every_weight() {
        let mut fst = StdVectorFst::new();
        fst.add_state();
        fst.set_start(0);
        fst.add_arc(0, StdArc::new(1, 1, TropicalWeight(1.234_567), 0));
        fst.set_final(0, TropicalWeight(2.765_432));

        arc_map(&mut fst, &mut QuantizeMapper::new(0.5)).unwrap();
        assert_eq!(fst.arcs(0).next().unwrap().weight().value(), 1.0);
        assert_eq!(fst.final_weight(0).value(), 3.0);
    }

    /// Requiring a superfinal state moves every final weight onto an arc, so
    /// the result has exactly one final state.
    #[test]
    fn the_superfinal_mapper_leaves_exactly_one_final_state() {
        let mut fst = fst();
        fst.set_final(1, TropicalWeight(5.0));

        arc_map(&mut fst, &mut SuperFinalMapper).unwrap();

        let finals: Vec<i32> = (0..fst.num_states() as i32)
            .filter(|&s| fst.final_weight(s) != TropicalWeight::zero())
            .collect();
        assert_eq!(finals.len(), 1);
        let superfinal = finals[0];
        assert_eq!(fst.final_weight(superfinal), TropicalWeight::one());

        // The states that were final now reach it, carrying what they weighed.
        let to_superfinal: Vec<f32> = (0..fst.num_states() as i32)
            .flat_map(|s| {
                fst.arcs(s)
                    .filter(|a| a.nextstate() == superfinal)
                    .map(|a| a.weight().value())
                    .collect::<Vec<_>>()
            })
            .collect();
        assert_eq!(to_superfinal.len(), 2);
        assert!(to_superfinal.contains(&5.0));
        assert!(to_superfinal.contains(&3.0));
    }

    /// Mapping into a second FST leaves the input alone and reproduces its
    /// shape in the output.
    #[test]
    fn mapping_into_another_fst_leaves_the_input_alone() {
        let ifst = fst();
        let mut ofst = StdVectorFst::new();
        ofst.add_state();
        arc_map_to(&ifst, &mut ofst, &mut RmWeightMapper).unwrap();

        assert_eq!(ofst.num_states(), 3, "the output starts from nothing");
        assert_eq!(ofst.start(), Some(0));
        assert_eq!(arcs(&ofst), vec![(1, 2, 0.0, 1), (3, 4, 0.0, 2)]);
        assert_eq!(ofst.final_weight(2), TropicalWeight::one());
        // The input is untouched.
        assert_eq!(arcs(&ifst), vec![(1, 2, 1.0, 1), (3, 4, 2.0, 2)]);
    }

    /// A tropical weight is its own reverse, so this changes nothing, but it
    /// exercises the path that maps between arc types.
    #[test]
    fn reversing_weights_maps_between_arc_types() {
        let ifst = fst();
        let mut ofst = StdVectorFst::new();
        arc_map_to(&ifst, &mut ofst, &mut ReverseWeightMapper).unwrap();
        assert_eq!(arcs(&ofst), arcs(&ifst));
        assert_eq!(ofst.final_weight(2), TropicalWeight(3.0));
    }

    #[test]
    fn an_fst_with_no_start_state_is_left_alone() {
        let mut fst = StdVectorFst::new();
        fst.add_state();
        arc_map(&mut fst, &mut RmWeightMapper).unwrap();
        assert_eq!(fst.num_states(), 1);

        let ifst = StdVectorFst::new();
        let mut ofst = StdVectorFst::new();
        ofst.add_state();
        arc_map_to(&ifst, &mut ofst, &mut IdentityArcMapper).unwrap();
        assert_eq!(ofst.num_states(), 0);
    }

    /// The acceptor bit cannot survive a mapping that blanks one side.
    #[test]
    fn label_properties_are_given_up_when_labels_change() {
        let mut fst = StdVectorFst::new();
        for _ in 0..2 {
            fst.add_state();
        }
        fst.set_start(0);
        fst.set_final(1, TropicalWeight::one());
        fst.add_arc(0, StdArc::new(5, 5, TropicalWeight::one(), 1));
        assert_ne!(fst.properties(K_ACCEPTOR, true) & K_ACCEPTOR, 0);

        arc_map(&mut fst, &mut InputEpsilonMapper).unwrap();
        assert_eq!(
            fst.properties(K_ACCEPTOR, false) & K_ACCEPTOR,
            0,
            "it is no longer an acceptor and must not say it is"
        );
        // Blanking one side made it a transducer, which a scan confirms.
        assert_ne!(fst.properties(K_NOT_ACCEPTOR, true) & K_NOT_ACCEPTOR, 0);
    }
}