auslander 0.8.0

Computational representation theory of finite-dimensional bound quiver algebras
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
use super::portable_encode::{canonical_without_fingerprint, fingerprint};
use super::portable_parser;
use super::portable_types::{
    HomologicalStreamBudget, HomologicalStreamConfig, HomologicalStreamCutReason,
    HomologicalStreamParseLimits, HomologicalStreamPortableError, HomologicalStreamPortableStatus,
    HomologicalStreamStep, HomologicalStreamVerifyLimits, VerifiedHomologicalStream,
};
use super::{
    HomologicalBatchStreamChunk, HomologicalBatchStreamLimits, HomologicalBatchStreamWork,
};
use crate::batch::{HomologicalBatch, HomologicalBatchLimits};
use crate::census::{CensusOutcome, CensusPortableStatus, VerifiedCensus};
use crate::control::{CancellationToken, ComputationControl};
use crate::field::Fp;
use crate::linalg::DenseMat;
use crate::module::Module;
use crate::quiver::ArrowId;

/// A canonical homological self-pair stream checkpoint.
#[derive(Clone, Debug)]
pub struct HomologicalStreamPortable {
    pub(super) census: String,
    pub(super) census_fingerprint: String,
    pub(super) max_degree: usize,
    pub(super) config: HomologicalStreamConfig,
    pub(super) next_source: usize,
    pub(super) rows: Vec<super::HomologicalBatchStreamRow>,
    pub(super) work: HomologicalBatchStreamWork,
    pub(super) chunk_sizes: Vec<usize>,
    pub(super) status: HomologicalStreamPortableStatus,
    pub(super) fingerprint: String,
}

impl HomologicalStreamPortable {
    /// Starts an active checkpoint over a verified complete census.
    pub fn from_verified_census(
        census: &VerifiedCensus,
        max_degree: usize,
        config: HomologicalStreamConfig,
    ) -> Result<Self, HomologicalStreamPortableError> {
        validate_config(max_degree, config.chunk_limits)?;
        require_complete_census(census)?;
        let mut portable = Self {
            census: census.portable().to_canonical_json(),
            census_fingerprint: census.portable().fingerprint().to_string(),
            max_degree,
            config,
            next_source: 0,
            rows: Vec::new(),
            work: HomologicalBatchStreamWork::default(),
            chunk_sizes: Vec::new(),
            status: HomologicalStreamPortableStatus::Active,
            fingerprint: String::new(),
        };
        portable.fingerprint = fingerprint(&canonical_without_fingerprint(&portable));
        Ok(portable)
    }

    accessor_methods! {
        /// The embedded complete census JSON.
        pub census() -> &str = |this| &this.census;
        /// The fingerprint of the embedded complete census.
        pub census_fingerprint() -> &str = |this| &this.census_fingerprint;
        /// The largest Ext degree in each stored row.
        pub max_degree() -> usize = |this| this.max_degree;
        /// The fixed chunk limits and absolute ceilings.
        pub config() -> HomologicalStreamConfig = |this| this.config;
        /// The first representative without a stored row.
        pub next_source() -> usize = |this| this.next_source;
        /// The exact completed rows in representative order.
        pub rows() -> &[super::HomologicalBatchStreamRow] = |this| &this.rows;
        /// The cumulative exact batch work.
        pub work() -> HomologicalBatchStreamWork = |this| this.work;
        /// The source count of each committed chunk, in order.
        pub chunk_sizes() -> &[usize] = |this| &this.chunk_sizes;
        /// The active, complete, or cut checkpoint status.
        pub status() -> &HomologicalStreamPortableStatus = |this| &this.status;
        /// The canonical FNV-1a fingerprint of the preceding fields.
        pub fingerprint() -> &str = |this| &this.fingerprint;
    }

    /// Serializes this checkpoint to byte-exact canonical JSON.
    pub fn to_canonical_json(&self) -> String {
        let mut output = canonical_without_fingerprint(self);
        output.push_str(",\"fingerprint\":\"");
        output.push_str(&self.fingerprint);
        output.push_str("\"}");
        output
    }

    /// Parses one canonical checkpoint under explicit limits.
    pub fn from_json(
        text: &str,
        limits: HomologicalStreamParseLimits,
    ) -> Result<Self, HomologicalStreamPortableError> {
        let raw = portable_parser::parse(text, limits)?;
        let census =
            String::from_utf8(raw.census).map_err(|_| HomologicalStreamPortableError::Syntax {
                byte: 0,
                message: "embedded census bytes are not UTF-8".to_string(),
            })?;
        validate_fingerprint(&raw.census_fingerprint)?;
        validate_fingerprint(&raw.fingerprint)?;
        let portable = Self {
            census,
            census_fingerprint: raw.census_fingerprint,
            max_degree: raw.max_degree,
            config: raw.config,
            next_source: raw.next_source,
            rows: raw.rows,
            work: raw.work,
            chunk_sizes: raw.chunk_sizes,
            status: raw.status,
            fingerprint: raw.fingerprint,
        };
        if portable.to_canonical_json() != text {
            return Err(HomologicalStreamPortableError::NonCanonical);
        }
        if !portable.has_valid_fingerprint() {
            return Err(HomologicalStreamPortableError::FingerprintMismatch);
        }
        Ok(portable)
    }

    /// Whether the outer fingerprint matches the canonical preceding fields.
    pub fn has_valid_fingerprint(&self) -> bool {
        self.fingerprint == fingerprint(&canonical_without_fingerprint(self))
    }

    /// Independently reconstructs the census and replays every stored row.
    pub fn verify(
        &self,
        limits: HomologicalStreamVerifyLimits,
    ) -> Result<VerifiedHomologicalStream, HomologicalStreamPortableError> {
        super::portable_verify::verify_portable(self, limits)
    }
}

/// Builds a resumable homological stream from a verified complete census.
pub fn homological_stream_from_census(
    census: &VerifiedCensus,
    max_degree: usize,
    config: HomologicalStreamConfig,
    control: Option<&ComputationControl>,
) -> Result<HomologicalSelfPairCheckpointStream, HomologicalStreamPortableError> {
    HomologicalSelfPairCheckpointStream::from_verified_census(census, max_degree, config, control)
}

/// A bounded-memory stream that commits one complete chunk at a time.
pub struct HomologicalSelfPairCheckpointStream {
    census: VerifiedCensus,
    portable: HomologicalStreamPortable,
    cancellation: Option<CancellationToken>,
}

impl HomologicalSelfPairCheckpointStream {
    pub(super) fn from_verified_census(
        census: &VerifiedCensus,
        max_degree: usize,
        config: HomologicalStreamConfig,
        control: Option<&ComputationControl>,
    ) -> Result<Self, HomologicalStreamPortableError> {
        let portable = HomologicalStreamPortable::from_verified_census(census, max_degree, config)?;
        Ok(Self {
            census: census.clone(),
            portable,
            cancellation: control.map(ComputationControl::cancellation_token),
        })
    }

    pub(super) fn from_verified_checkpoint(
        verified: &VerifiedHomologicalStream,
        budget: HomologicalStreamBudget,
        control: Option<&ComputationControl>,
    ) -> Result<Self, HomologicalStreamPortableError> {
        let mut portable = verified.portable.clone();
        let committed_work = primitive_work_units(portable.work)?;
        if budget.max_sources < portable.next_source {
            return Err(HomologicalStreamPortableError::ResumeBudget {
                field: "max_sources",
                committed: portable.next_source,
                limit: budget.max_sources,
            });
        }
        if budget.max_work_units < committed_work {
            return Err(HomologicalStreamPortableError::ResumeBudget {
                field: "max_work_units",
                committed: committed_work,
                limit: budget.max_work_units,
            });
        }
        portable.config.budget = budget;
        let cancellation = control.map(ComputationControl::cancellation_token);
        portable.status = HomologicalStreamPortableStatus::Active;
        portable.fingerprint = fingerprint(&canonical_without_fingerprint(&portable));
        let mut stream = Self {
            census: verified.census.clone(),
            portable,
            cancellation,
        };
        if let Some(HomologicalStreamStep::Failed { error, .. }) = stream.boundary_step() {
            return Err(error);
        }
        Ok(stream)
    }

    accessor_methods! {
        /// The first representative without a stored row.
        pub next_source() -> usize = |this| this.portable.next_source();
        /// The cumulative exact batch work.
        pub work() -> HomologicalBatchStreamWork = |this| this.portable.work();
        /// The current active, complete, or cut status.
        pub status() -> &HomologicalStreamPortableStatus = |this| this.portable.status();
        /// The fixed chunk limits and current absolute ceilings.
        pub config() -> HomologicalStreamConfig = |this| this.portable.config();
    }

    /// Returns the latest checkpoint, which contains only committed chunks.
    pub fn checkpoint(&self) -> HomologicalStreamPortable {
        self.portable.clone()
    }

    /// Computes and commits the next complete chunk.
    pub fn next_chunk(&mut self) -> HomologicalStreamStep {
        if !matches!(
            self.portable.status,
            HomologicalStreamPortableStatus::Active
        ) {
            return self.terminal_step();
        }
        if let Some(step) = self.boundary_step() {
            return step;
        }
        self.compute_chunk()
    }

    fn boundary_step(&mut self) -> Option<HomologicalStreamStep> {
        if self.cancelled() {
            self.portable.status =
                HomologicalStreamPortableStatus::Cut(HomologicalStreamCutReason::Cancelled);
            self.refresh_fingerprint();
            return Some(self.terminal_step());
        }
        let total = source_count(&self.census);
        if self.portable.next_source >= total {
            self.portable.status = HomologicalStreamPortableStatus::Complete;
            self.refresh_fingerprint();
            return Some(self.terminal_step());
        }
        if self.portable.next_source >= self.portable.config.budget.max_sources {
            self.cut(HomologicalStreamCutReason::SourceLimit {
                limit: self.portable.config.budget.max_sources,
            });
            return Some(self.terminal_step());
        }
        let current_units = match primitive_work_units(self.portable.work) {
            Ok(units) => units,
            Err(error) => return Some(self.failed(error)),
        };
        if current_units >= self.portable.config.budget.max_work_units {
            self.cut(HomologicalStreamCutReason::WorkLimit {
                limit: self.portable.config.budget.max_work_units,
            });
            return Some(self.terminal_step());
        }
        None
    }

    fn compute_chunk(&mut self) -> HomologicalStreamStep {
        let total = source_count(&self.census);
        let capacity = self.chunk_capacity();
        let remaining_budget = self
            .portable
            .config
            .budget
            .max_sources
            .saturating_sub(self.portable.next_source);
        let count = capacity
            .min(total - self.portable.next_source)
            .min(remaining_budget);
        let chunk = match build_chunk(
            &self.census,
            self.portable.next_source,
            count,
            self.portable.max_degree,
            self.portable.config.chunk_limits,
        ) {
            Ok(chunk) => chunk,
            Err(error) => return self.failed(error),
        };
        let mut next_work = self.portable.work;
        if let Err(error) = next_work.add_chunk(chunk.work(), count) {
            return self.failed(error.into());
        }
        let next_units = match primitive_work_units(next_work) {
            Ok(units) => units,
            Err(error) => return self.failed(error),
        };
        if next_units > self.portable.config.budget.max_work_units {
            self.cut(HomologicalStreamCutReason::WorkLimit {
                limit: self.portable.config.budget.max_work_units,
            });
            return self.terminal_step();
        }
        self.commit_chunk(&chunk, next_work);
        self.set_boundary_status(total, next_units);
        HomologicalStreamStep::Chunk(chunk)
    }

    fn commit_chunk(
        &mut self,
        chunk: &HomologicalBatchStreamChunk,
        work: HomologicalBatchStreamWork,
    ) {
        self.portable.rows.extend_from_slice(chunk.rows());
        self.portable.work = work;
        self.portable.chunk_sizes.push(chunk.source_count());
        self.portable.next_source = self
            .portable
            .next_source
            .checked_add(chunk.source_count())
            .expect("verified census source count cannot overflow usize");
        self.refresh_fingerprint();
    }

    fn set_boundary_status(&mut self, total: usize, units: usize) {
        if self.portable.next_source == total {
            self.portable.status = HomologicalStreamPortableStatus::Complete;
        } else if self.portable.next_source >= self.portable.config.budget.max_sources {
            self.cut(HomologicalStreamCutReason::SourceLimit {
                limit: self.portable.config.budget.max_sources,
            });
        } else if units >= self.portable.config.budget.max_work_units {
            self.cut(HomologicalStreamCutReason::WorkLimit {
                limit: self.portable.config.budget.max_work_units,
            });
        }
        self.refresh_fingerprint();
    }

    fn cut(&mut self, reason: HomologicalStreamCutReason) {
        self.portable.status = HomologicalStreamPortableStatus::Cut(reason);
        self.refresh_fingerprint();
    }

    fn failed(&mut self, error: HomologicalStreamPortableError) -> HomologicalStreamStep {
        HomologicalStreamStep::Failed {
            source: self.portable.next_source,
            error,
            work: self.portable.work,
        }
    }

    fn refresh_fingerprint(&mut self) {
        self.portable.fingerprint = fingerprint(&canonical_without_fingerprint(&self.portable));
    }

    fn cancelled(&self) -> bool {
        self.cancellation
            .as_ref()
            .is_some_and(CancellationToken::is_cancelled)
    }

    fn chunk_capacity(&self) -> usize {
        let degree_steps = self
            .portable
            .max_degree
            .checked_add(1)
            .expect("stream configuration validates degree overflow");
        self.portable
            .config
            .chunk_limits
            .max_live_sources
            .min(self.portable.config.chunk_limits.max_pairs)
            .min(self.portable.config.chunk_limits.max_ext_cells / degree_steps)
    }

    fn terminal_step(&self) -> HomologicalStreamStep {
        match &self.portable.status {
            HomologicalStreamPortableStatus::Active => {
                unreachable!("active checkpoint has no terminal step")
            }
            HomologicalStreamPortableStatus::Complete => HomologicalStreamStep::Complete {
                next_source: self.portable.next_source,
                work: self.portable.work,
            },
            HomologicalStreamPortableStatus::Cut(reason) => HomologicalStreamStep::Cut {
                next_source: self.portable.next_source,
                reason: *reason,
                work: self.portable.work,
            },
        }
    }
}

fn require_complete_census(census: &VerifiedCensus) -> Result<(), HomologicalStreamPortableError> {
    if matches!(census.portable().status(), CensusPortableStatus::Complete)
        && matches!(census.outcome(), CensusOutcome::Complete(_))
    {
        Ok(())
    } else {
        Err(HomologicalStreamPortableError::CensusNotComplete)
    }
}

pub(super) fn validate_config(
    max_degree: usize,
    limits: HomologicalBatchStreamLimits,
) -> Result<(), HomologicalStreamPortableError> {
    let degree_steps = max_degree.checked_add(1).ok_or_else(|| {
        HomologicalStreamPortableError::InvalidConfig(
            "max_degree has no representable successor".to_string(),
        )
    })?;
    if limits.max_live_sources == 0 {
        return Err(HomologicalStreamPortableError::InvalidConfig(
            "max_live_sources must be positive".to_string(),
        ));
    }
    if limits.max_pairs == 0 || limits.max_ext_cells / degree_steps == 0 {
        return Err(HomologicalStreamPortableError::InvalidConfig(
            "chunk limits cannot fit one source".to_string(),
        ));
    }
    Ok(())
}

pub(super) fn source_count(census: &VerifiedCensus) -> usize {
    census.portable().representatives().len()
}

pub(super) fn build_chunk(
    census: &VerifiedCensus,
    first_source: usize,
    count: usize,
    max_degree: usize,
    limits: HomologicalBatchStreamLimits,
) -> Result<HomologicalBatchStreamChunk, HomologicalStreamPortableError> {
    let modules = (first_source..first_source + count)
        .map(|index| representative_module(census, index))
        .collect::<Result<Vec<_>, _>>()?;
    let batch = HomologicalBatch::self_pairs(
        modules,
        max_degree,
        HomologicalBatchLimits {
            max_pairs: limits.max_pairs,
            max_ext_cells: limits.max_ext_cells,
        },
    )
    .map_err(HomologicalStreamPortableError::Batch)?;
    super::stream::make_chunk(first_source, batch).map_err(HomologicalStreamPortableError::Stream)
}

fn representative_module(
    census: &VerifiedCensus,
    index: usize,
) -> Result<Module, HomologicalStreamPortableError> {
    let domain = census.census().domain();
    let representative = census
        .portable()
        .representatives()
        .get(index)
        .ok_or_else(|| HomologicalStreamPortableError::CountMismatch {
            field: "representative index".to_string(),
        })?;
    let coordinates = representative.coordinates();
    if coordinates.len() != domain.coordinate_count() {
        return Err(HomologicalStreamPortableError::CountMismatch {
            field: "representative coordinate count".to_string(),
        });
    }
    let algebra = domain.algebra();
    let field = algebra.field();
    let mut maps = Vec::with_capacity(algebra.quiver().num_arrows());
    let mut offset = 0usize;
    for arrow_index in 0..algebra.quiver().num_arrows() {
        let arrow = ArrowId(arrow_index as u32);
        let rows = domain.dimensions()[algebra.quiver().source(arrow) as usize];
        let columns = domain.dimensions()[algebra.quiver().target(arrow) as usize];
        let entries = rows.checked_mul(columns).ok_or_else(|| {
            HomologicalStreamPortableError::DomainMismatch {
                field: format!("arrow {arrow_index} coordinate count"),
            }
        })?;
        let end = offset.checked_add(entries).ok_or_else(|| {
            HomologicalStreamPortableError::DomainMismatch {
                field: "representative coordinate layout".to_string(),
            }
        })?;
        let values = coordinates.get(offset..end).ok_or_else(|| {
            HomologicalStreamPortableError::CountMismatch {
                field: "representative coordinate layout".to_string(),
            }
        })?;
        if let Some((row, &_value)) = values
            .iter()
            .enumerate()
            .find(|(_, value)| **value >= field.modulus())
        {
            return Err(HomologicalStreamPortableError::Module(
                crate::module::ModuleError::NonCanonicalEntry {
                    arrow,
                    row: row / columns,
                    col: row % columns,
                },
            ));
        }
        let values: Vec<Fp> = values
            .iter()
            .map(|&value| field.elem(value as i64))
            .collect();
        maps.push(DenseMat::from_flat(rows, columns, &values));
        offset = end;
    }
    Module::new(algebra.clone(), domain.dimensions().to_vec(), maps)
        .map_err(HomologicalStreamPortableError::Module)
}

pub(super) fn primitive_work_units(
    work: HomologicalBatchStreamWork,
) -> Result<usize, HomologicalStreamPortableError> {
    [
        work.resolutions,
        work.target_covers,
        work.hom_spaces,
        work.projective_factor_spaces,
        work.ext_tables,
    ]
    .into_iter()
    .try_fold(0usize, |total, value| {
        total
            .checked_add(value)
            .ok_or(HomologicalStreamPortableError::CounterOverflow {
                field: "primitive work units",
            })
    })
}

fn validate_fingerprint(value: &str) -> Result<(), HomologicalStreamPortableError> {
    if value.len() == 16
        && value
            .bytes()
            .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
    {
        Ok(())
    } else {
        Err(HomologicalStreamPortableError::FingerprintShape)
    }
}