sim-lib-serial-core 0.1.0

Finite symbolic alphabets, aggregate rules, and certified total series transforms for SIM.
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
//! Aggregate policies and validation evidence.

use crate::alphabet::{validate_alphabet, validate_stable_id};
use crate::{AggregateRuleError, AlphabetId, SerialAlphabet};
use std::collections::{BTreeMap, BTreeSet};
use std::fmt::{Display, Formatter};

/// One alphabet symbol paired with its declared aggregate count.
pub type SymbolCount<S> = (S, usize);

/// Stable identity of one class in a projected aggregate.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ProjectionId(String);

impl ProjectionId {
    /// Validates and constructs a projected-class id.
    pub fn try_new(value: impl Into<String>) -> Result<Self, AggregateRuleError> {
        let value = value.into();
        validate_stable_id(&value).map_err(|reason| AggregateRuleError::InvalidProjectionId {
            value: value.clone(),
            reason,
        })?;
        Ok(Self(value))
    }

    /// Returns the stable text identity.
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl Display for ProjectionId {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
        Display::fmt(&self.0, formatter)
    }
}

/// Symbol-based declaration of one projected aggregate class.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ProjectedClassSpec<S> {
    /// Stable class identity.
    pub id: ProjectionId,
    /// Source-alphabet symbols that project to this class.
    pub symbols: Vec<S>,
    /// Required number of class occurrences in the series.
    pub multiplicity: usize,
}

impl<S> ProjectedClassSpec<S> {
    /// Constructs a class specification. Full membership is validated by the rule constructor.
    pub fn new(id: ProjectionId, symbols: Vec<S>, multiplicity: usize) -> Self {
        Self {
            id,
            symbols,
            multiplicity,
        }
    }
}

/// Public category of an [`AggregateRule`].
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum AggregateRuleKind {
    /// Every alphabet symbol occurs exactly once.
    ExhaustiveExactlyOnce,
    /// Symbols may be omitted but no symbol may repeat.
    NoRepeat,
    /// Every symbol has an explicitly declared positive multiplicity.
    DeclaredMultiplicity,
    /// Every non-omitted symbol occurs exactly once.
    DeclaredOmissions,
    /// Occurrence requirements apply to declared projection classes.
    ProjectedAggregate,
    /// Any finite order of alphabet members is accepted, including repeats.
    FreeOrder,
}

/// Aggregate rule data compiled from symbols into private canonical positions.
/// Callers never provide raw ordinals.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum AggregateRule {
    /// Every alphabet symbol occurs exactly once.
    ExhaustiveExactlyOnce,
    /// Symbols may be omitted but no symbol may repeat.
    NoRepeat,
    /// Explicit per-symbol multiplicity data.
    DeclaredMultiplicity(DeclaredCounts),
    /// Explicit set of omitted symbols; every other symbol occurs once.
    DeclaredOmissions(DeclaredCounts),
    /// Explicit projection classes and their required multiplicities.
    ProjectedAggregate(ProjectedRule),
    /// Any finite order of alphabet members, including repeats.
    FreeOrder,
}

/// Alphabet-bound expected counts retained by a declared aggregate rule.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DeclaredCounts {
    alphabet_id: AlphabetId,
    expected: Vec<usize>,
}

/// One compiled projected class.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ProjectedClassRule {
    id: ProjectionId,
    members: Vec<usize>,
    multiplicity: usize,
}

/// Alphabet-bound projected aggregate data.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ProjectedRule {
    alphabet_id: AlphabetId,
    cardinality: usize,
    classes: Vec<ProjectedClassRule>,
    class_by_position: Vec<usize>,
}

impl AggregateRule {
    /// Constructs an exhaustive exactly-once rule.
    pub const fn exhaustive_exactly_once() -> Self {
        Self::ExhaustiveExactlyOnce
    }

    /// Constructs a no-repeat rule.
    pub const fn no_repeat() -> Self {
        Self::NoRepeat
    }

    /// Constructs a free-order rule.
    pub const fn free_order() -> Self {
        Self::FreeOrder
    }

    /// Compiles an explicit positive multiplicity for every alphabet symbol.
    pub fn declared_multiplicity<A, I>(
        alphabet: &A,
        declarations: I,
    ) -> Result<Self, AggregateRuleError>
    where
        A: SerialAlphabet,
        I: IntoIterator<Item = (A::Symbol, usize)>,
    {
        let positions = validate_alphabet(alphabet)?;
        let mut expected = vec![None; alphabet.symbols().len()];
        for (symbol, multiplicity) in declarations {
            let Some(&position) = positions.get(&symbol) else {
                return Err(AggregateRuleError::ForeignSymbol {
                    alphabet_id: alphabet.id().clone(),
                });
            };
            if expected[position].is_some() {
                return Err(AggregateRuleError::DuplicateDeclaration { position });
            }
            if multiplicity == 0 {
                return Err(AggregateRuleError::ZeroMultiplicity { position });
            }
            expected[position] = Some(multiplicity);
        }
        let expected = expected
            .into_iter()
            .enumerate()
            .map(|(position, count)| {
                count.ok_or(AggregateRuleError::MissingDeclaration { position })
            })
            .collect::<Result<Vec<_>, _>>()?;
        checked_total(&expected)?;
        Ok(Self::DeclaredMultiplicity(DeclaredCounts {
            alphabet_id: alphabet.id().clone(),
            expected,
        }))
    }

    /// Compiles a set of symbols to omit; every remaining symbol is required once.
    pub fn declared_omissions<A, I>(alphabet: &A, omissions: I) -> Result<Self, AggregateRuleError>
    where
        A: SerialAlphabet,
        I: IntoIterator<Item = A::Symbol>,
    {
        let positions = validate_alphabet(alphabet)?;
        let mut expected = vec![1; alphabet.symbols().len()];
        let mut omitted = BTreeSet::new();
        for symbol in omissions {
            let Some(&position) = positions.get(&symbol) else {
                return Err(AggregateRuleError::ForeignSymbol {
                    alphabet_id: alphabet.id().clone(),
                });
            };
            if !omitted.insert(position) {
                return Err(AggregateRuleError::DuplicateDeclaration { position });
            }
            expected[position] = 0;
        }
        if omitted.is_empty() {
            return Err(AggregateRuleError::NoOmissions);
        }
        if omitted.len() == alphabet.symbols().len() {
            return Err(AggregateRuleError::OmitsEverything(alphabet.id().clone()));
        }
        Ok(Self::DeclaredOmissions(DeclaredCounts {
            alphabet_id: alphabet.id().clone(),
            expected,
        }))
    }

    /// Compiles a complete, disjoint projection of alphabet symbols into classes.
    pub fn projected_aggregate<A, I>(alphabet: &A, classes: I) -> Result<Self, AggregateRuleError>
    where
        A: SerialAlphabet,
        I: IntoIterator<Item = ProjectedClassSpec<A::Symbol>>,
    {
        let positions = validate_alphabet(alphabet)?;
        let mut class_ids = BTreeSet::new();
        let mut class_by_position = vec![None; alphabet.symbols().len()];
        let mut compiled = Vec::new();
        let mut total = 0usize;
        for spec in classes {
            if !class_ids.insert(spec.id.clone()) {
                return Err(AggregateRuleError::DuplicateProjectionId(spec.id));
            }
            if spec.symbols.is_empty() {
                return Err(AggregateRuleError::EmptyProjectionClass(spec.id));
            }
            let class_index = compiled.len();
            let mut members = Vec::with_capacity(spec.symbols.len());
            for symbol in spec.symbols {
                let Some(&position) = positions.get(&symbol) else {
                    return Err(AggregateRuleError::ForeignSymbol {
                        alphabet_id: alphabet.id().clone(),
                    });
                };
                if class_by_position[position].replace(class_index).is_some() {
                    return Err(AggregateRuleError::DuplicateProjectionMember { position });
                }
                members.push(position);
            }
            total = total
                .checked_add(spec.multiplicity)
                .ok_or(AggregateRuleError::MultiplicityOverflow)?;
            compiled.push(ProjectedClassRule {
                id: spec.id,
                members,
                multiplicity: spec.multiplicity,
            });
        }
        for (position, class) in class_by_position.iter().enumerate() {
            if class.is_none() {
                return Err(AggregateRuleError::MissingProjectionMember { position });
            }
        }
        if total == 0 {
            return Err(AggregateRuleError::OmitsEverything(alphabet.id().clone()));
        }
        Ok(Self::ProjectedAggregate(ProjectedRule {
            alphabet_id: alphabet.id().clone(),
            cardinality: alphabet.symbols().len(),
            classes: compiled,
            class_by_position: class_by_position.into_iter().flatten().collect(),
        }))
    }

    /// Returns the public category of this rule.
    pub const fn kind(&self) -> AggregateRuleKind {
        match self {
            Self::ExhaustiveExactlyOnce => AggregateRuleKind::ExhaustiveExactlyOnce,
            Self::NoRepeat => AggregateRuleKind::NoRepeat,
            Self::DeclaredMultiplicity(_) => AggregateRuleKind::DeclaredMultiplicity,
            Self::DeclaredOmissions(_) => AggregateRuleKind::DeclaredOmissions,
            Self::ProjectedAggregate(_) => AggregateRuleKind::ProjectedAggregate,
            Self::FreeOrder => AggregateRuleKind::FreeOrder,
        }
    }

    /// Returns symbol/count declarations for a declared multiplicity or omission rule.
    pub fn declared_counts<A>(
        &self,
        alphabet: &A,
    ) -> Result<Option<Vec<SymbolCount<A::Symbol>>>, AggregateRuleError>
    where
        A: SerialAlphabet,
    {
        let counts = match self {
            Self::DeclaredMultiplicity(counts) | Self::DeclaredOmissions(counts) => counts,
            _ => return Ok(None),
        };
        counts.validate_for(alphabet)?;
        Ok(Some(
            alphabet
                .symbols()
                .iter()
                .cloned()
                .zip(counts.expected.iter().copied())
                .collect(),
        ))
    }

    /// Returns symbolic projected-class declarations for a projected rule.
    pub fn projected_classes<A>(
        &self,
        alphabet: &A,
    ) -> Result<Option<Vec<ProjectedClassSpec<A::Symbol>>>, AggregateRuleError>
    where
        A: SerialAlphabet,
    {
        let Self::ProjectedAggregate(rule) = self else {
            return Ok(None);
        };
        rule.validate_for(alphabet)?;
        Ok(Some(
            rule.classes
                .iter()
                .map(|class| {
                    ProjectedClassSpec::new(
                        class.id.clone(),
                        class
                            .members
                            .iter()
                            .map(|&position| alphabet.symbols()[position].clone())
                            .collect(),
                        class.multiplicity,
                    )
                })
                .collect(),
        ))
    }

    pub(crate) fn declared(&self) -> Option<&DeclaredCounts> {
        match self {
            Self::DeclaredMultiplicity(counts) | Self::DeclaredOmissions(counts) => Some(counts),
            _ => None,
        }
    }

    pub(crate) fn projected(&self) -> Option<&ProjectedRule> {
        match self {
            Self::ProjectedAggregate(rule) => Some(rule),
            _ => None,
        }
    }
}

impl DeclaredCounts {
    pub(crate) fn validate_for<A: SerialAlphabet>(
        &self,
        alphabet: &A,
    ) -> Result<(), AggregateRuleError> {
        validate_binding(&self.alphabet_id, self.expected.len(), alphabet)
    }

    pub(crate) fn expected(&self) -> &[usize] {
        &self.expected
    }
}

impl ProjectedRule {
    pub(crate) fn validate_for<A: SerialAlphabet>(
        &self,
        alphabet: &A,
    ) -> Result<(), AggregateRuleError> {
        validate_binding(&self.alphabet_id, self.cardinality, alphabet)
    }

    pub(crate) fn required_len(&self) -> Result<usize, AggregateRuleError> {
        checked_total(
            &self
                .classes
                .iter()
                .map(|class| class.multiplicity)
                .collect::<Vec<_>>(),
        )
    }

    pub(crate) fn class_by_position(&self) -> &[usize] {
        &self.class_by_position
    }

    pub(crate) fn classes(&self) -> &[ProjectedClassRule] {
        &self.classes
    }
}

impl ProjectedClassRule {
    pub(crate) fn id(&self) -> &ProjectionId {
        &self.id
    }

    pub(crate) fn multiplicity(&self) -> usize {
        self.multiplicity
    }
}

/// Observed and expected counts for one projected class.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ProjectedClassEvidence {
    /// Stable projected-class id.
    pub id: ProjectionId,
    /// Required number of occurrences.
    pub expected: usize,
    /// Observed number of occurrences.
    pub observed: usize,
}

/// Construction evidence retained by a valid [`crate::Series`].
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AggregateLedger<S>
where
    S: Clone + Eq + Ord + std::fmt::Debug,
{
    pub(crate) alphabet_id: AlphabetId,
    pub(crate) rule: AggregateRuleKind,
    pub(crate) series_len: usize,
    pub(crate) observed: BTreeMap<S, usize>,
    pub(crate) expected: Option<BTreeMap<S, usize>>,
    pub(crate) omitted: Vec<S>,
    pub(crate) repeated: Vec<S>,
    pub(crate) projected: Vec<ProjectedClassEvidence>,
}

impl<S> AggregateLedger<S>
where
    S: Clone + Eq + Ord + std::fmt::Debug,
{
    /// Stable alphabet identity validated by this ledger.
    pub fn alphabet_id(&self) -> &AlphabetId {
        &self.alphabet_id
    }

    /// Aggregate rule category applied during validation.
    pub fn rule(&self) -> AggregateRuleKind {
        self.rule
    }

    /// Number of ordered positions validated.
    pub fn series_len(&self) -> usize {
        self.series_len
    }

    /// Observed count of `symbol`, or `None` when it is outside the alphabet.
    pub fn observed_count(&self, symbol: &S) -> Option<usize> {
        self.observed.get(symbol).copied()
    }

    /// Declared expected count of `symbol` when this rule has per-symbol expectations.
    pub fn expected_count(&self, symbol: &S) -> Option<usize> {
        self.expected
            .as_ref()
            .and_then(|counts| counts.get(symbol).copied())
    }

    /// Alphabet symbols absent from the supplied series.
    pub fn omitted_symbols(&self) -> &[S] {
        &self.omitted
    }

    /// Alphabet symbols occurring more than once.
    pub fn repeated_symbols(&self) -> &[S] {
        &self.repeated
    }

    /// Projected-class count evidence, empty for non-projected rules.
    pub fn projected_classes(&self) -> &[ProjectedClassEvidence] {
        &self.projected
    }

    /// Returns true when every alphabet symbol occurred exactly once.
    pub fn is_exhaustive_exactly_once(&self) -> bool {
        self.omitted.is_empty()
            && self.repeated.is_empty()
            && self.observed.values().all(|count| *count == 1)
    }
}

fn validate_binding<A: SerialAlphabet>(
    rule_id: &AlphabetId,
    cardinality: usize,
    alphabet: &A,
) -> Result<(), AggregateRuleError> {
    validate_alphabet(alphabet)?;
    if rule_id != alphabet.id() {
        return Err(AggregateRuleError::AlphabetMismatch {
            rule_id: rule_id.clone(),
            series_id: alphabet.id().clone(),
        });
    }
    if cardinality != alphabet.symbols().len() {
        return Err(AggregateRuleError::CardinalityMismatch {
            expected: cardinality,
            found: alphabet.symbols().len(),
        });
    }
    Ok(())
}

fn checked_total(counts: &[usize]) -> Result<usize, AggregateRuleError> {
    counts.iter().try_fold(0usize, |total, &count| {
        total
            .checked_add(count)
            .ok_or(AggregateRuleError::MultiplicityOverflow)
    })
}