ordofp 0.1.0

OrdoFP: Functional programming in Rust with HList, Disiunctio, GAT traits, Optics, and Monad Transformers
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
//! Shared workload core for the end-to-end perf drivers (`e2e_workload` timed
//! driver, `e2e_allocs` allocation-count driver). Not auto-discovered as an
//! example (lives in a subdirectory); included via `#[path]` by both drivers.
//!
//! Models one term-end grade-processing batch shaped like a production LMS
//! application, with surface proportions measured from that real workload:
//!
//! 1. `validate` — per-grade `Probatum` multi-error validation (`map2` chain),
//!    the #1 downstream surface; validators are the faithful copies used by
//!    `benches/grade_validation.rs`.
//! 2. `route` — per-student error routing: `NonEmpty` error lists in a
//!    `Disiunctio` coproduct, `Display`-rendered at the boundary (the cold /
//!    error path; scales with `--error-pct`).
//! 3. `aggregate` — per-student and per-course `Compositio`/`Unitas` folds of
//!    `GradeStatistics` / `WeightedGrade` over `rust_decimal::Decimal`, the
//!    faithful copies used by `benches/grade_aggregation.rs`.
//! 4. `optics` — per-enrollment nested read through a composed cloning
//!    `Aspectus` (the pattern the production consumer imports).
//! 5. `pfds` — a small persistent `OrdMap` course index: per-course inserts,
//!    per-student `&str` lookups (the Borrow-generic path).
//!
//! Phases run phase-major (not request-major) so each phase can be timed with
//! two `Instant` calls per rep; the per-grade work is identical either way.
//!
//! Determinism: xorshift64* with a fixed seed. Each rep recomputes the same
//! checksum from value-bearing results (counts, rendered-error bytes, Decimal
//! mantissae, name lengths); drivers assert rep checksums are identical, and
//! the checksum must stay IDENTICAL across optimization commits
//! (behavior-preservation evidence).

use std::fmt::Write as _;
use std::time::Instant;

use ordofp::validated::Probatum;
use ordofp_core::disiunctio::Disiunctio;
use ordofp_core::nonempty::NonEmpty;
use ordofp_core::optics::aspectus;
use ordofp_core::pfds::OrdMap;
use ordofp_core::typeclasses::{Compositio, Unitas};
use rust_decimal::Decimal;

// ─── configuration ───────────────────────────────────────────────────────────

#[derive(Clone, Debug)]
pub struct Config {
    pub courses: usize,
    pub students: usize,
    pub assignments: usize,
    pub reps: usize,
    /// Percentage (0–100) of grade cells generated invalid.
    pub error_pct: u64,
    pub seed: u64,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            courses: 40,
            students: 150,
            assignments: 20,
            reps: 60,
            error_pct: 3,
            seed: 0xC0FFEE0D0F,
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Mode {
    /// Realistic steady-state mix (~3% invalid input).
    Steady,
    /// Error-path stress (50% invalid input unless --error-pct overrides).
    ErrorHeavy,
    /// Parse args and exit immediately (process/start-up cost floor).
    Startup,
}

fn usage(msg: &str) -> ! {
    eprintln!("error: {msg}");
    eprintln!(
        "usage: --mode steady|error-heavy|startup --courses N --students N \
         --assignments N --reps N --error-pct N --seed N"
    );
    std::process::exit(2)
}

fn num(args: &mut impl Iterator<Item = String>, flag: &str) -> u64 {
    let v = args
        .next()
        .unwrap_or_else(|| usage(&format!("{flag} needs a value")));
    v.parse()
        .unwrap_or_else(|_| usage(&format!("{flag} needs a number, got {v}")))
}

pub fn parse_args() -> (Config, Mode) {
    let mut cfg = Config::default();
    let mut mode = Mode::Steady;
    let mut error_pct_set = false;
    let mut args = std::env::args().skip(1);
    while let Some(a) = args.next() {
        match a.as_str() {
            "--mode" => {
                let v = args.next().unwrap_or_else(|| usage("--mode needs a value"));
                mode = match v.as_str() {
                    "steady" => Mode::Steady,
                    "error-heavy" => Mode::ErrorHeavy,
                    "startup" => Mode::Startup,
                    other => usage(&format!("unknown mode {other}")),
                };
            }
            "--courses" => cfg.courses = num(&mut args, "--courses") as usize,
            "--students" => cfg.students = num(&mut args, "--students") as usize,
            "--assignments" => cfg.assignments = num(&mut args, "--assignments") as usize,
            "--reps" => cfg.reps = num(&mut args, "--reps") as usize,
            "--error-pct" => {
                cfg.error_pct = num(&mut args, "--error-pct").min(100);
                error_pct_set = true;
            }
            "--seed" => cfg.seed = num(&mut args, "--seed"),
            other => usage(&format!("unknown flag {other}")),
        }
    }
    if mode == Mode::ErrorHeavy && !error_pct_set {
        cfg.error_pct = 50;
    }
    (cfg, mode)
}

// ─── deterministic rng (xorshift64*) ─────────────────────────────────────────

pub struct Rng(u64);

impl Rng {
    pub fn new(seed: u64) -> Self {
        Self(seed.max(1))
    }

    fn next(&mut self) -> u64 {
        let mut x = self.0;
        x ^= x >> 12;
        x ^= x << 25;
        x ^= x >> 27;
        self.0 = x;
        x.wrapping_mul(0x2545_F491_4F6C_DD1D)
    }

    fn below(&mut self, n: u64) -> u64 {
        self.next() % n
    }
}

// ─── grade validation (faithful copy of benches/grade_validation.rs) ─────────

#[derive(Clone, Debug)]
pub enum GradeInputError {
    InvalidPointsPossible(String),
    ScoreOutOfRange(String),
    MissingGradingScheme,
}

impl core::fmt::Display for GradeInputError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::InvalidPointsPossible(msg) | Self::ScoreOutOfRange(msg) => f.write_str(msg),
            Self::MissingGradingScheme => f.write_str("missing grading scheme"),
        }
    }
}

fn validate_input_not_empty(input: &str) -> Probatum<GradeInputError, ()> {
    // Empty / special-case inputs are acceptable (cleared grade, "ex", "mi").
    let _ = input.trim();
    Probatum::valid(())
}

fn validate_points_possible(pp: Option<Decimal>) -> Probatum<GradeInputError, ()> {
    if let Some(pp) = pp
        && pp <= Decimal::ZERO
    {
        return Probatum::invalid(GradeInputError::InvalidPointsPossible(format!(
            "Points possible must be positive, got {pp}"
        )));
    }
    Probatum::valid(())
}

fn validate_score_range(score: Decimal, pp: Option<Decimal>) -> Probatum<GradeInputError, ()> {
    if score < Decimal::ZERO {
        return Probatum::invalid(GradeInputError::ScoreOutOfRange(format!(
            "Score cannot be negative: {score}"
        )));
    }
    if let Some(pp) = pp
        && score > pp
        && pp > Decimal::ZERO
    {
        return Probatum::invalid(GradeInputError::ScoreOutOfRange(format!(
            "Score {score} exceeds points possible {pp}"
        )));
    }
    Probatum::valid(())
}

fn validate_entry_mode(has_scheme: bool, needs_scheme: bool) -> Probatum<GradeInputError, ()> {
    if needs_scheme && !has_scheme {
        return Probatum::invalid(GradeInputError::MissingGradingScheme);
    }
    Probatum::valid(())
}

/// The real combine-and-accumulate pattern (map2 chain → collects ALL errors).
fn validate_all(
    input: &str,
    score: Decimal,
    pp: Option<Decimal>,
    has_scheme: bool,
    needs_scheme: bool,
) -> Probatum<GradeInputError, ()> {
    Probatum::valid(())
        .map2(validate_input_not_empty(input), |(), ()| ())
        .map2(validate_points_possible(pp), |(), ()| ())
        .map2(validate_score_range(score, pp), |(), ()| ())
        .map2(validate_entry_mode(has_scheme, needs_scheme), |(), ()| ())
}

// ─── grade aggregation (faithful copy of benches/grade_aggregation.rs) ───────

#[derive(Debug, Clone, PartialEq)]
pub struct GradeStatistics {
    pub count: u64,
    pub sum: Decimal,
    pub sum_of_squares: Decimal,
    pub min: Option<Decimal>,
    pub max: Option<Decimal>,
}

impl GradeStatistics {
    fn from_score(score: Decimal) -> Self {
        Self {
            count: 1,
            sum: score,
            sum_of_squares: score * score,
            min: Some(score),
            max: Some(score),
        }
    }
}

impl Default for GradeStatistics {
    fn default() -> Self {
        Self {
            count: 0,
            sum: Decimal::ZERO,
            sum_of_squares: Decimal::ZERO,
            min: None,
            max: None,
        }
    }
}

impl Compositio for GradeStatistics {
    fn combine(&self, other: &Self) -> Self {
        Self {
            count: self.count + other.count,
            sum: self.sum + other.sum,
            sum_of_squares: self.sum_of_squares + other.sum_of_squares,
            min: match (self.min, other.min) {
                (Some(a), Some(b)) => Some(a.min(b)),
                (Some(a), None) => Some(a),
                (None, Some(b)) => Some(b),
                (None, None) => None,
            },
            max: match (self.max, other.max) {
                (Some(a), Some(b)) => Some(a.max(b)),
                (Some(a), None) => Some(a),
                (None, Some(b)) => Some(b),
                (None, None) => None,
            },
        }
    }
}

impl Unitas for GradeStatistics {
    fn empty() -> Self {
        Self::default()
    }
}

#[derive(Debug, Clone, PartialEq)]
struct WeightedGrade {
    weighted_sum: Decimal,
    weight_sum: Decimal,
}

impl WeightedGrade {
    fn new(score: Decimal, weight: Decimal) -> Self {
        Self {
            weighted_sum: score * weight,
            weight_sum: weight,
        }
    }
    fn weighted_average(&self) -> Option<Decimal> {
        if self.weight_sum == Decimal::ZERO {
            None
        } else {
            Some(self.weighted_sum / self.weight_sum)
        }
    }
}

impl Default for WeightedGrade {
    fn default() -> Self {
        Self {
            weighted_sum: Decimal::ZERO,
            weight_sum: Decimal::ZERO,
        }
    }
}

impl Compositio for WeightedGrade {
    fn combine(&self, other: &Self) -> Self {
        Self {
            weighted_sum: self.weighted_sum + other.weighted_sum,
            weight_sum: self.weight_sum + other.weight_sum,
        }
    }
}

impl Unitas for WeightedGrade {
    fn empty() -> Self {
        Self::default()
    }
}

/// Real consumer helper (the production `calculate_statistics`).
fn calculate_statistics(scores: &[Decimal]) -> GradeStatistics {
    scores
        .iter()
        .map(|s| GradeStatistics::from_score(*s))
        .fold(GradeStatistics::empty(), |acc, s| acc.combine(&s))
}

/// Real consumer helper (the production `weighted_average`).
fn weighted_average(grades: &[(Decimal, Decimal)]) -> Option<Decimal> {
    grades
        .iter()
        .map(|(score, weight)| WeightedGrade::new(*score, *weight))
        .fold(WeightedGrade::empty(), |acc, g| acc.combine(&g))
        .weighted_average()
}

// ─── optics structs (shapes from benches/optics_get.rs) ──────────────────────

#[derive(Clone)]
pub struct Course {
    pub id: u64,
    pub name: String,
    pub description: String,
    pub tags: Vec<String>,
}

#[derive(Clone)]
pub struct Enrollment {
    pub student: String,
    pub course: Course,
    pub grade: f64,
}

const DESCRIPTION: &str = "A fairly long course description that makes the \
                           intermediate Course struct non-trivial to clone.";
const TAGS: [&str; 4] = ["rust", "fp", "monads", "optics"];

// ─── workload data + per-rep state ───────────────────────────────────────────

#[derive(Clone, Copy)]
pub struct GradeCell {
    score: Decimal,
    pp: Option<Decimal>,
    has_scheme: bool,
    needs_scheme: bool,
}

pub struct CourseData {
    pub name: String,
    /// Raw input strings, one per assignment (validated per student).
    inputs: Vec<String>,
    /// Per-assignment weights for the weighted-average fold.
    weights: Vec<Decimal>,
    /// Row-major `[student][assignment]` grade cells.
    cells: Vec<GradeCell>,
    pub enrollments: Vec<Enrollment>,
}

pub struct WorkloadData {
    pub courses: Vec<CourseData>,
    pub students: usize,
    pub assignments: usize,
}

fn gen_cell(rng: &mut Rng, error_pct: u64) -> GradeCell {
    let bad = rng.below(100) < error_pct;
    if !bad {
        return GradeCell {
            score: Decimal::new(rng.below(10_001) as i64, 2),
            pp: if rng.below(10) == 0 {
                None
            } else {
                Some(Decimal::new(10_000, 2))
            },
            has_scheme: true,
            needs_scheme: rng.below(4) == 0,
        };
    }
    // Invalid cell: a non-zero 3-bit mask picks 1–3 simultaneous error causes,
    // exercising Probatum's multi-error accumulation.
    let kinds = 1 + rng.below(7);
    let mut score = Decimal::new(rng.below(10_001) as i64, 2);
    let mut pp = Some(Decimal::new(10_000, 2));
    let mut has_scheme = true;
    let mut needs_scheme = false;
    if kinds & 1 != 0 {
        pp = Some(Decimal::ZERO); // InvalidPointsPossible
    }
    if kinds & 2 != 0 {
        score = Decimal::new(-(rng.below(5_000) as i64 + 1), 2); // ScoreOutOfRange
    }
    if kinds & 4 != 0 {
        has_scheme = false; // MissingGradingScheme
        needs_scheme = true;
    }
    GradeCell {
        score,
        pp,
        has_scheme,
        needs_scheme,
    }
}

pub fn generate(cfg: &Config) -> WorkloadData {
    let mut rng = Rng::new(cfg.seed);
    let courses = (0..cfg.courses)
        .map(|ci| {
            let name = format!("course_{ci:04}");
            let inputs: Vec<String> = (0..cfg.assignments)
                .map(|ai| format!("{}", 50 + (ai * 7) % 51))
                .collect();
            let weights: Vec<Decimal> = (0..cfg.assignments)
                .map(|_| Decimal::new((rng.below(5) + 1) as i64, 0))
                .collect();
            let cells: Vec<GradeCell> = (0..cfg.students * cfg.assignments)
                .map(|_| gen_cell(&mut rng, cfg.error_pct))
                .collect();
            let enrollments: Vec<Enrollment> = (0..cfg.students)
                .map(|si| Enrollment {
                    student: format!("student_{si:05}"),
                    course: Course {
                        id: ci as u64,
                        name: name.clone(),
                        description: DESCRIPTION.to_string(),
                        tags: TAGS.iter().map(|t| (*t).to_string()).collect(),
                    },
                    grade: 0.0,
                })
                .collect();
            CourseData {
                name,
                inputs,
                weights,
                cells,
                enrollments,
            }
        })
        .collect();
    WorkloadData {
        courses,
        students: cfg.students,
        assignments: cfg.assignments,
    }
}

/// Reused per-rep buffers, preallocated so the steady state adds no
/// driver-side allocations on the hot (valid) path.
pub struct State {
    /// Per-(course,student) valid scores.
    scores: Vec<Vec<Decimal>>,
    /// Per-(course,student) (score, weight) pairs for the weighted fold.
    pairs: Vec<Vec<(Decimal, Decimal)>>,
    /// Per-(course,student) accumulated validation errors.
    errs: Vec<Vec<GradeInputError>>,
    /// Per-course (course index, average) collected by the aggregate phase.
    course_avgs: Vec<(usize, Decimal)>,
    /// Reused boundary-rendering buffer for error messages.
    render_buf: String,
}

impl State {
    pub fn new(cfg: &Config) -> Self {
        let slots = cfg.courses * cfg.students;
        Self {
            scores: (0..slots)
                .map(|_| Vec::with_capacity(cfg.assignments))
                .collect(),
            pairs: (0..slots)
                .map(|_| Vec::with_capacity(cfg.assignments))
                .collect(),
            errs: (0..slots).map(|_| Vec::new()).collect(),
            course_avgs: Vec::with_capacity(cfg.courses),
            render_buf: String::new(),
        }
    }
}

// ─── the measured pipeline ───────────────────────────────────────────────────

pub const PHASES: [&str; 5] = ["validate", "route", "aggregate", "optics", "pfds"];

pub struct RepOutcome {
    pub checksum: u64,
    pub phase_ns: [u64; 5],
    pub valid_grades: u64,
    pub errored_students: u64,
    pub errors: u64,
}

pub fn run_rep(data: &WorkloadData, st: &mut State) -> RepOutcome {
    let s_count = data.students;
    let a_count = data.assignments;
    let mut cs: u64 = 0xcbf2_9ce4_8422_2325;
    let mut phase_ns = [0u64; 5];
    let mut valid_grades = 0u64;
    let mut errored_students = 0u64;
    let mut errors = 0u64;
    st.course_avgs.clear();

    // Phase 1: validate — per-grade Probatum map2 accumulation (#1 surface).
    let t = Instant::now();
    for (ci, course) in data.courses.iter().enumerate() {
        for si in 0..s_count {
            let slot = ci * s_count + si;
            st.scores[slot].clear();
            st.pairs[slot].clear();
            st.errs[slot].clear();
            for ai in 0..a_count {
                let cell = &course.cells[si * a_count + ai];
                let v = validate_all(
                    &course.inputs[ai],
                    cell.score,
                    cell.pp,
                    cell.has_scheme,
                    cell.needs_scheme,
                );
                match v {
                    Probatum::Valid(()) => {
                        valid_grades += 1;
                        st.scores[slot].push(cell.score);
                        st.pairs[slot].push((cell.score, course.weights[ai]));
                    }
                    Probatum::Invalid(es) => st.errs[slot].extend(es),
                }
            }
        }
    }
    phase_ns[0] = t.elapsed().as_nanos() as u64;

    // Phase 2: route — per-student NonEmpty/Disiunctio error coproduct +
    // boundary Display rendering (the explicit error/cold path).
    let t = Instant::now();
    for ci in 0..data.courses.len() {
        for si in 0..s_count {
            let slot = ci * s_count + si;
            let routed: Disiunctio<NonEmpty<GradeInputError>, usize> = if st.errs[slot].is_empty() {
                Disiunctio::Dexter(st.scores[slot].len())
            } else {
                let mut drained = st.errs[slot].drain(..);
                let head = drained.next().expect("checked non-empty");
                let tail: Vec<GradeInputError> = drained.collect();
                Disiunctio::Sinister(NonEmpty::new(head, tail))
            };
            match routed {
                Disiunctio::Dexter(n) => cs = cs.wrapping_add(n as u64),
                Disiunctio::Sinister(ne) => {
                    errored_students += 1;
                    errors += ne.len() as u64;
                    st.render_buf.clear();
                    for e in ne.iter() {
                        let _ = write!(st.render_buf, "{e}; ");
                    }
                    cs = cs.wrapping_add(st.render_buf.len() as u64).rotate_left(7);
                }
            }
        }
    }
    phase_ns[1] = t.elapsed().as_nanos() as u64;

    // Phase 3: aggregate — Semigroup/Monoid folds (per-student stats fold,
    // per-course combine, per-student weighted average).
    let t = Instant::now();
    for ci in 0..data.courses.len() {
        let mut course_stats = GradeStatistics::empty();
        for si in 0..s_count {
            let slot = ci * s_count + si;
            let student_stats = calculate_statistics(&st.scores[slot]);
            course_stats = course_stats.combine(&student_stats);
            if let Some(avg) = weighted_average(&st.pairs[slot]) {
                cs = cs.wrapping_add(avg.mantissa() as u64);
            }
        }
        if course_stats.count > 0 {
            let avg = course_stats.sum / Decimal::from(course_stats.count);
            st.course_avgs.push((ci, avg));
            cs = cs
                .wrapping_add(course_stats.count)
                .wrapping_add(course_stats.sum.mantissa() as u64);
        }
    }
    phase_ns[2] = t.elapsed().as_nanos() as u64;

    // Phase 4: optics — per-enrollment nested read via composed cloning
    // Aspectus (the consumer's import).
    let t = Instant::now();
    let course_lens = aspectus(
        |e: &Enrollment| e.course.clone(),
        |e: &Enrollment, course: Course| Enrollment {
            course,
            student: e.student.clone(),
            grade: e.grade,
        },
    );
    let name_lens = aspectus(
        |c: &Course| c.name.clone(),
        |c: &Course, name: String| Course {
            name,
            id: c.id,
            description: c.description.clone(),
            tags: c.tags.clone(),
        },
    );
    let course_name = course_lens.compose(&name_lens);
    for course in &data.courses {
        for e in &course.enrollments {
            let name = course_name.get(e);
            cs = cs.wrapping_add(name.len() as u64);
        }
    }
    phase_ns[3] = t.elapsed().as_nanos() as u64;

    // Phase 5: pfds — persistent OrdMap course index: per-course insert
    // (owned String key), per-student &str lookup (Borrow-generic path).
    let t = Instant::now();
    let mut index: OrdMap<String, Decimal> = OrdMap::new();
    for (ci, avg) in &st.course_avgs {
        index = index.insert(data.courses[*ci].name.clone(), *avg);
    }
    for course in &data.courses {
        for _ in 0..s_count {
            if let Some(avg) = index.get(course.name.as_str()) {
                cs = cs.wrapping_add(avg.mantissa() as u64 & 0xFFFF);
            }
        }
    }
    phase_ns[4] = t.elapsed().as_nanos() as u64;

    RepOutcome {
        checksum: cs,
        phase_ns,
        valid_grades,
        errored_students,
        errors,
    }
}