use std::sync::Arc;
use crate::algebra::Algebra;
use crate::ar::TauError;
use crate::basic::{BasicError, SupportPairIsoWitness};
use crate::indec::IndecError;
use crate::mutation::{FacWitness, Mutation, MutationError};
use crate::supporttau::{SupportTauError, SupportTauTiltingPair};
use crate::taurigid::TauRigidError;
use super::verify::ClosureWitness;
#[derive(Clone, Debug)]
pub enum GraphError {
Basic(BasicError),
SupportTau(SupportTauError),
Mutation(MutationError),
Defect {
reason: String,
},
}
display_error! { GraphError {
Self::Basic(error) => "basic layer: {error}";
Self::SupportTau(error) => "support tau-tilting layer: {error}";
Self::Mutation(error) => "mutation layer: {error}";
Self::Defect { reason } => "internal cross-check failed: {reason}";
} }
error_source!(GraphError {
Self::Basic(error) => Some(error),
Self::SupportTau(error) => Some(error),
Self::Mutation(error) => Some(error),
Self::Defect { .. } => None,
});
from_variants!(GraphError {
BasicError => Basic,
SupportTauError => SupportTau,
MutationError => Mutation,
});
pub(super) fn defect(reason: String) -> GraphError {
GraphError::Defect { reason }
}
pub(super) fn basic_blocker(error: &BasicError) -> Option<String> {
match error {
BasicError::CertificationBlocked { reason } => Some(reason.clone()),
_ => None,
}
}
pub(super) fn support_blocker(error: &SupportTauError) -> Option<String> {
match error {
SupportTauError::Basic(inner) => basic_blocker(inner),
SupportTauError::TauRigid(TauRigidError::Tau(TauError::AgreementUnknown {
reason,
..
})) => Some(format!("the two tau routes stayed undecided: {reason}")),
_ => None,
}
}
pub(super) fn mutation_blocker(error: &MutationError) -> Option<String> {
match error {
MutationError::Basic(inner) => basic_blocker(inner),
MutationError::SupportTau(inner) => support_blocker(inner),
MutationError::Indec(IndecError::Undetermined { attempts }) => Some(format!(
"a cokernel summand stayed undetermined after {attempts} split attempts"
)),
_ => None,
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct MutationGraphLimits {
pub max_vertices: usize,
pub max_directed_mutations: usize,
pub max_work_units: u64,
pub max_matrix_entries: usize,
}
impl Default for MutationGraphLimits {
fn default() -> MutationGraphLimits {
MutationGraphLimits {
max_vertices: 10_000,
max_directed_mutations: 100_000,
max_work_units: 50_000_000,
max_matrix_entries: 4_000_000,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum GraphLimit {
Vertices,
DirectedMutations,
WorkUnits,
MatrixEntries,
}
display_error! { GraphLimit {
Self::Vertices => "max_vertices";
Self::DirectedMutations => "max_directed_mutations";
Self::WorkUnits => "max_work_units";
Self::MatrixEntries => "max_matrix_entries";
} }
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct GraphBudgetDiagnostics {
pub(super) vertices_found: usize,
pub(super) verified_slots: usize,
pub(super) new_vertices: usize,
pub(super) repeated_endpoints: usize,
pub(super) frontier: usize,
pub(super) vertex: usize,
pub(super) slot: Option<usize>,
pub(super) open_slots: usize,
pub(super) work_units: u64,
pub(super) limit: GraphLimit,
}
impl GraphBudgetDiagnostics {
accessor_methods! {
pub vertices_found() -> usize = |this| this.vertices_found;
pub verified_slots() -> usize = |this| this.verified_slots;
pub new_vertices() -> usize = |this| this.new_vertices;
pub repeated_endpoints() -> usize = |this| this.repeated_endpoints;
pub frontier() -> usize = |this| this.frontier;
pub vertex() -> usize = |this| this.vertex;
pub slot() -> Option<usize> = |this| this.slot;
pub open_slots() -> usize = |this| this.open_slots;
pub work_units() -> u64 = |this| this.work_units;
pub limit() -> GraphLimit = |this| this.limit;
}
}
display_error! { GraphBudgetDiagnostics {
Self { limit, vertex, slot, vertices_found, verified_slots, open_slots, frontier, work_units, .. } => "{} ran out at vertex {} slot {:?}: {} vertices, {} slots decided, {} open, frontier {}, {} work units", limit, vertex, slot, vertices_found, verified_slots, open_slots, frontier, work_units;
} }
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CertificationBlocker {
pub(super) vertex: usize,
pub(super) slot: Option<usize>,
pub(super) reason: String,
pub(super) vertices_found: usize,
pub(super) work_units: u64,
}
impl CertificationBlocker {
accessor_methods! {
pub vertex() -> usize = |this| this.vertex;
pub slot() -> Option<usize> = |this| this.slot;
pub reason() -> &str = |this| &this.reason;
pub vertices_found() -> usize = |this| this.vertices_found;
pub work_units() -> u64 = |this| this.work_units;
}
}
display_error! { CertificationBlocker {
Self { vertex, slot, reason, .. } => "certification blocked at vertex {} slot {:?}: {}", vertex, slot, reason;
} }
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum IncompleteReason {
BudgetExhausted(GraphBudgetDiagnostics),
CertificationBlocked(CertificationBlocker),
}
display_error! { IncompleteReason {
Self::BudgetExhausted(diagnostics) => "{diagnostics}";
Self::CertificationBlocked(blocker) => "{blocker}";
} }
#[derive(Debug)]
pub enum SlotRecord {
LeftMutation {
mutation: usize,
},
NoLeftMutation(FacWitness),
}
impl SlotRecord {
optional_accessors! {
pub mutation() -> usize = Self::LeftMutation { mutation } => *mutation;
pub fac_witness() -> &FacWitness = Self::NoLeftMutation(witness) => witness;
}
}
#[derive(Debug)]
pub struct GraphVertex {
pub(super) pair: SupportTauTiltingPair,
pub(super) slots: Vec<SlotRecord>,
}
impl GraphVertex {
accessor_methods! {
pub pair() -> &SupportTauTiltingPair = |this| &this.pair;
pub slots() -> &[SlotRecord] = |this| &this.slots;
}
}
#[derive(Debug)]
pub struct VerifiedMutation {
pub(super) source: usize,
pub(super) slot: usize,
pub(super) target: usize,
pub(super) mutation: Mutation,
pub(super) endpoint: SupportPairIsoWitness,
}
impl VerifiedMutation {
accessor_methods! {
pub source() -> usize = |this| this.source;
pub slot() -> usize = |this| this.slot;
pub target() -> usize = |this| this.target;
pub mutation() -> &Mutation = |this| &this.mutation;
pub endpoint() -> &SupportPairIsoWitness = |this| &this.endpoint;
}
}
#[derive(Debug)]
pub struct ClosedSupportTauTiltingGraph {
pub(super) witness: ClosureWitness,
pub(super) work_units: u64,
}
impl ClosedSupportTauTiltingGraph {
accessor_methods! {
pub algebra() -> &Arc<Algebra> = |this| this.witness.algebra();
pub pairs() -> impl ExactSizeIterator<Item = &SupportTauTiltingPair> =
|this| this.witness.vertices().iter().map(GraphVertex::pair);
pub vertices() -> &[GraphVertex] = |this| this.witness.vertices();
pub mutations() -> &[VerifiedMutation] = |this| this.witness.mutations();
pub len() -> usize = |this| this.witness.vertices().len();
pub is_empty() -> bool = |this| this.witness.vertices().is_empty();
}
pub fn histogram(&self) -> Vec<usize> {
let n = self.algebra().quiver().num_vertices() as usize;
let mut out = vec![0; n + 1];
for vertex in self.witness.vertices() {
out[vertex.pair.module().len()] += 1;
}
out
}
accessor_methods! {
pub witness() -> &ClosureWitness = |this| &this.witness;
pub work_units() -> u64 = |this| this.work_units;
pub verify() -> bool = |this| this.witness.verify();
}
}
#[derive(Debug)]
pub struct IncompleteSupportTauTiltingGraph {
pub(super) algebra: Arc<Algebra>,
pub(super) vertices: Vec<GraphVertex>,
pub(super) mutations: Vec<VerifiedMutation>,
pub(super) reason: IncompleteReason,
pub(super) work_units: u64,
}
impl IncompleteSupportTauTiltingGraph {
accessor_methods! {
pub algebra() -> &Arc<Algebra> = |this| &this.algebra;
pub vertices_found() -> &[GraphVertex] = |this| &this.vertices;
pub verified_mutations() -> &[VerifiedMutation] = |this| &this.mutations;
pub reason() -> &IncompleteReason = |this| &this.reason;
pub work_units() -> u64 = |this| this.work_units;
pub verify_parts() -> bool = |this| this.vertices.iter().all(|v| {
Arc::ptr_eq(v.pair.module().module().algebra(), &this.algebra) && v.pair.verify()
}) && this
.mutations
.iter()
.all(|edge| edge.mutation.verify() && edge.endpoint.verify());
}
}
#[derive(Debug)]
pub enum SupportTauTiltingGraphOutcome {
Closed(ClosedSupportTauTiltingGraph),
Incomplete(IncompleteSupportTauTiltingGraph),
}
impl SupportTauTiltingGraphOutcome {
binary_outcome_accessors!(
Closed,
Incomplete,
closed -> ClosedSupportTauTiltingGraph = |value| value,
incomplete -> IncompleteSupportTauTiltingGraph = |value| value,
is_closed,
into_closed -> ClosedSupportTauTiltingGraph = |value| value;
flag = "Whether the walk closed.";
positive = "The closed graph, or `None` when the walk stopped short.";
negative = "The partial graph, or `None` when the walk closed.";
into = "The closed graph by value, or `None` when the walk stopped short.";
);
}