use std::sync::Arc;
use crate::basic::{
AddClosureWitness, BasicDecomposition, ProjectiveSupport, SupportPairIsoOutcome,
SupportPairObstruction,
};
use crate::context::VerificationContext;
use crate::decompose::Certificate;
use crate::hom::Morphism;
use crate::indec::IndecomposableModule;
use crate::iso::indecomposable_iso;
use crate::module::Module;
use crate::profile::{Site, hit};
use crate::supporttau::{AlmostCompletePair, SupportTauTiltingPair};
use super::common::is_cokernel;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ExchangeShape {
MovesToProjective {
vertex: u32,
},
ReplacedByModule {
multiplicity: usize,
},
}
#[derive(Debug)]
pub struct MutationWitness {
pub(super) slot: usize,
pub(super) exchanged: Module,
pub(super) almost_complete: AlmostCompletePair,
pub(super) source_module: Module,
pub(super) source_projective: Vec<u32>,
pub(super) source_extension: AddClosureWitness,
pub(super) target_module: Module,
pub(super) target_projective: Vec<u32>,
pub(super) target_extension: AddClosureWitness,
pub(super) distinct: SupportPairObstruction,
pub(super) approximation: crate::approx::MinimalLeftApproximation,
pub(super) exchange: Morphism,
pub(super) shape: ExchangeShape,
pub(super) replacement: Option<Module>,
}
impl MutationWitness {
accessor_methods! {
pub slot() -> usize = |this| this.slot;
pub exchanged() -> &Module = |this| &this.exchanged;
pub almost_complete() -> &AlmostCompletePair = |this| &this.almost_complete;
pub source_extension() -> &AddClosureWitness = |this| &this.source_extension;
pub target_extension() -> &AddClosureWitness = |this| &this.target_extension;
pub distinct() -> &SupportPairObstruction = |this| &this.distinct;
pub approximation() -> &crate::approx::MinimalLeftApproximation = |this| &this.approximation;
pub exchange() -> &Morphism = |this| &this.exchange;
pub shape() -> &ExchangeShape = |this| &this.shape;
pub replacement() -> Option<&Module> = |this| this.replacement.as_ref();
pub source_module() -> &Module = |this| &this.source_module;
pub source_projective() -> &[u32] = |this| &this.source_projective;
pub target_module() -> &Module = |this| &this.target_module;
pub target_projective() -> &[u32] = |this| &this.target_projective;
}
verify_methods!(pub(crate), { hit(Site::MutationWitnessVerify); },
|self, context| {
verify_guard!(self.almost_complete.verify_with_context(context));
let u = self.almost_complete.module();
let algebra = u.module().algebra();
verify_guard!(Arc::ptr_eq(algebra, self.source_module.algebra())
&& Arc::ptr_eq(algebra, self.target_module.algebra()));
let_or_false!((Ok(source_dec), Ok(target_dec)) = (
BasicDecomposition::new_with_context(&self.source_module, context),
BasicDecomposition::new_with_context(&self.target_module, context),
));
let_or_false!((Ok(source_proj), Ok(target_proj)) = (
ProjectiveSupport::new(algebra, &self.source_projective),
ProjectiveSupport::new(algebra, &self.target_projective),
));
verify_guard!(self.almost_complete.projective().vertices() == source_proj.vertices()
&& extends(u, &source_dec, &self.source_extension));
verify_guard!(self
.almost_complete
.projective()
.vertices()
.iter()
.all(|&v| target_proj.contains(v))
&& extends(u, &target_dec, &self.target_extension));
verify_guard!(matches!(
context.pair_iso_for(&source_dec, &source_proj, &target_dec, &target_proj),
Ok(outcome)
if matches!(outcome.as_ref(), SupportPairIsoOutcome::NotIsomorphic(found) if found == &self.distinct)
));
let_or_false!((Ok(fresh_dec), Ok(fresh_proj)) = (
BasicDecomposition::new_with_context(&self.target_module, context),
ProjectiveSupport::new(algebra, &self.target_projective),
));
matches!(
SupportTauTiltingPair::classify_with_context(fresh_dec, fresh_proj, context),
Ok(classification) if classification.is_pair()
) && self.exchange_holds(
u,
&source_dec,
&target_dec,
&source_proj,
&target_proj,
context,
)
});
fn exchange_holds(
&self,
u: &BasicDecomposition,
source_dec: &BasicDecomposition,
target_dec: &BasicDecomposition,
source_proj: &ProjectiveSupport,
target_proj: &ProjectiveSupport,
context: &VerificationContext,
) -> bool {
let f = self.approximation.map();
verify_guard!(f.source().ptr_eq(&self.exchanged));
verify_guard!(
self.approximation.summands().len() == u.len()
&& self
.approximation
.summands()
.iter()
.zip(u.summands())
.all(|(a, b)| a.module().ptr_eq(b.module()))
);
verify_guard!(
self.approximation.verify_with_context(context) && is_cokernel(f, &self.exchange)
);
let_or_false!(Some(slot_summand) = source_dec.summands().get(self.slot));
verify_guard!(
indecomposable_iso(slot_summand.module(), &self.exchanged, slot_summand.endo())
.is_some()
);
let y = self.exchange.target();
match &self.shape {
ExchangeShape::MovesToProjective { vertex } => {
let mut expected = source_proj.vertices().to_vec();
expected.push(*vertex);
expected.sort_unstable();
y.is_zero()
&& self.replacement.is_none()
&& !source_proj.contains(*vertex)
&& target_proj.contains(*vertex)
&& expected == target_proj.vertices()
&& target_dec.len() == u.len()
}
ExchangeShape::ReplacedByModule { multiplicity } => {
let_or_false!(Some(replacement) = &self.replacement);
verify_guard!(!y.is_zero() && source_proj.vertices() == target_proj.vertices());
verify_guard!(target_dec.len() == u.len() + 1);
let split = context.decompose_for(y);
verify_guard!(
split.summands().len() == *multiplicity
&& !split
.certificates()
.iter()
.any(|c| *c != Certificate::Indecomposable)
);
let_or_false!(
Ok(y1) = IndecomposableModule::new_with_context(replacement, context)
);
verify_guard!(
split
.summands()
.iter()
.all(|s| indecomposable_iso(y1.module(), s, y1.endo()).is_some())
);
verify_guard!(
!source_dec.summands().iter().any(|s| indecomposable_iso(
y1.module(),
s.module(),
y1.endo()
)
.is_some())
);
target_dec
.summands()
.iter()
.any(|s| indecomposable_iso(y1.module(), s.module(), y1.endo()).is_some())
}
}
}
}
pub(super) fn extends(
u: &BasicDecomposition,
whole: &BasicDecomposition,
stored: &AddClosureWitness,
) -> bool {
let_or_false!(Ok(Some(live)) = AddClosureWitness::new(u, whole));
verify_guard!(
live.matches().len() == stored.matches().len()
&& stored.module().ptr_eq(u.module())
&& stored.target().ptr_eq(whole.module())
);
let mut seen: Vec<usize> = stored.matches().iter().map(|m| m.target_index()).collect();
seen.sort_unstable();
let distinct = seen.windows(2).all(|w| w[0] != w[1]);
distinct && stored.verify()
}