use std::sync::Arc;
use crate::algebra::Algebra;
use crate::approx::left_approximation;
use crate::basic::{
AddClosureWitness, BasicDecomposition, ProjectiveSupport, SupportPairIsoOutcome,
SupportPairObstruction, pair_iso,
};
use crate::context::VerificationContext;
use crate::hom::{cokernel, hom};
use crate::module::Module;
use crate::profile::{Site, hit};
use crate::supporttau::{
AlmostCompleteClassification, AlmostCompletePair, SupportTauTiltingClassification,
SupportTauTiltingPair,
};
use crate::taurigid::TauCache;
use super::build::build_target;
use super::common::{support, trace_dims};
use super::error::{Endpoint, MutationDefect, MutationError, defect};
use super::fac::FacWitness;
use super::witness::{ExchangeShape, MutationWitness};
#[derive(Debug)]
pub struct Mutation {
pub(super) slot: usize,
pub(super) target: SupportTauTiltingPair,
pub(super) witness: MutationWitness,
}
impl Mutation {
accessor_methods! {
pub slot() -> usize = |this| this.slot;
pub target() -> &SupportTauTiltingPair = |this| &this.target;
}
#[inline]
pub fn into_target(self) -> SupportTauTiltingPair {
self.target
}
accessor_methods! {
pub witness() -> &MutationWitness = |this| &this.witness;
pub shape() -> &ExchangeShape = |this| this.witness.shape();
}
verify_methods!(pub(crate), { hit(Site::MutationVerify); },
|self, context| {
self.slot == self.witness.slot()
&& self
.target
.module()
.module()
.ptr_eq(self.witness.target_module())
&& self.target.projective().vertices() == self.witness.target_projective()
&& self.witness.verify_with_context(context)
&& self.target.verify_with_context(context)
});
}
#[derive(Debug)]
pub enum SlotOutcome {
NoLeftMutation(FacWitness),
LeftMutation(Box<Mutation>),
}
impl SlotOutcome {
binary_outcome_accessors!(
LeftMutation,
NoLeftMutation,
mutation -> Mutation = |value| value.as_ref(),
fac_witness -> FacWitness = |value| value,
is_left_mutation,
into_mutation -> Mutation = |value| *value;
flag = "Whether the slot admits a left mutation.";
positive = "The mutation, or `None` when the slot admits no left mutation.";
negative = "The `Fac` witness, or `None` when the slot admits a left mutation.";
into = "The mutation by value, or `None` when the slot admits no left mutation.";
);
}
pub fn mutate_at(pair: &SupportTauTiltingPair, slot: usize) -> Result<SlotOutcome, MutationError> {
let count = pair.module().len();
let indices: Vec<usize> = (0..count).collect();
mutate_at_with_cache(pair, slot, &indices, count, None)
}
struct MutationInput {
algebra: Arc<Algebra>,
x: Module,
u_dec: BasicDecomposition,
u: Module,
u_indices: Vec<usize>,
u_summands: Vec<Module>,
}
fn prepare_input(
pair: &SupportTauTiltingPair,
slot: usize,
summand_indices: &[usize],
) -> Result<MutationInput, MutationError> {
let m = pair.module();
if slot >= m.len() {
return Err(MutationError::SlotOutOfRange {
slot,
summands: m.len(),
});
}
if summand_indices.len() != m.len() {
return Err(MutationError::SummandIndexCount {
indices: summand_indices.len(),
summands: m.len(),
});
}
let algebra = m.module().algebra().clone();
let x = m.summands()[slot].module().clone();
let u_dec = m.without(slot).expect("the slot is a summand position");
let u = u_dec.module().clone();
let u_indices = summand_indices
.iter()
.enumerate()
.filter(|(index, _)| *index != slot)
.map(|(_, &index)| index)
.collect();
let u_summands = u_dec
.summands()
.iter()
.map(|summand| summand.module().clone())
.collect();
Ok(MutationInput {
algebra,
x,
u_dec,
u,
u_indices,
u_summands,
})
}
fn classify_target(
input: &MutationInput,
decomposition: BasicDecomposition,
vertices: &[u32],
replacement: bool,
fresh_index: usize,
cache: &mut TauCache,
) -> Result<SupportTauTiltingPair, MutationError> {
let mut target_indices = input.u_indices.clone();
target_indices.extend(replacement.then_some(fresh_index));
let target_proj = ProjectiveSupport::new(&input.algebra, vertices)?;
match SupportTauTiltingPair::classify_with_cache(
decomposition,
target_proj,
&target_indices,
Some(&mut *cache),
)? {
SupportTauTiltingClassification::Pair(target) => Ok(target),
SupportTauTiltingClassification::Rejected(rejection) => {
Err(defect(MutationDefect::TargetRejected(rejection)))
}
}
}
fn endpoint_data(
source: &SupportTauTiltingPair,
almost_complete: &AlmostCompletePair,
target: &SupportTauTiltingPair,
) -> Result<(AddClosureWitness, AddClosureWitness, SupportPairObstruction), MutationError> {
let Some(source_extension) = AddClosureWitness::new(almost_complete.module(), source.module())?
else {
return Err(defect(MutationDefect::AddClosureMissing {
endpoint: Endpoint::Source,
}));
};
let Some(target_extension) = AddClosureWitness::new(almost_complete.module(), target.module())?
else {
return Err(defect(MutationDefect::AddClosureMissing {
endpoint: Endpoint::Target,
}));
};
if almost_complete.projective().vertices() != source.projective().vertices() {
return Err(defect(MutationDefect::ProjectiveSupportNotExtended {
endpoint: Endpoint::Source,
}));
}
if !almost_complete
.projective()
.vertices()
.iter()
.all(|&vertex| target.projective().contains(vertex))
{
return Err(defect(MutationDefect::ProjectiveSupportNotExtended {
endpoint: Endpoint::Target,
}));
}
let distinct = match pair_iso(
source.module(),
&source.projective(),
target.module(),
&target.projective(),
)? {
SupportPairIsoOutcome::NotIsomorphic(obstruction) => obstruction,
SupportPairIsoOutcome::Isomorphic(_) => {
return Err(defect(MutationDefect::EndpointsIsomorphic));
}
};
Ok((source_extension, target_extension, distinct))
}
fn prepare_mutation(
pair: &SupportTauTiltingPair,
slot: usize,
input: MutationInput,
fresh_index: usize,
cache: &mut TauCache,
) -> Result<SlotOutcome, MutationError> {
let projective = ProjectiveSupport::new(&input.algebra, pair.projective().vertices())?;
let almost_complete = match AlmostCompletePair::classify_with_cache(
input.u_dec.clone(),
projective,
&input.u_indices,
Some(&mut *cache),
)? {
AlmostCompleteClassification::Pair(almost) => almost,
AlmostCompleteClassification::Rejected(rejection) => {
return Err(defect(MutationDefect::AlmostCompleteRejected(rejection)));
}
};
let approximation =
left_approximation(&input.x, &input.u_summands).map_err(MutationError::Approx)?;
let (y, exchange) = cokernel(approximation.map());
let dropped: Vec<u32> = support(pair.module().module())
.into_iter()
.filter(|&vertex| input.u.dim_at(vertex) == 0)
.collect();
let built = build_target(pair, &input.u_dec, &y, &dropped)?;
let super::build::BuiltTarget {
decomposition,
vertices,
shape,
replacement,
} = built;
let target = classify_target(
&input,
decomposition,
&vertices,
replacement.is_some(),
fresh_index,
cache,
)?;
let (source_extension, target_extension, distinct) =
endpoint_data(pair, &almost_complete, &target)?;
let witness = MutationWitness {
slot,
exchanged: input.x,
almost_complete,
source_module: pair.module().module().clone(),
source_projective: pair.projective().vertices().to_vec(),
source_extension,
target_module: target.module().module().clone(),
target_projective: target.projective().vertices().to_vec(),
target_extension,
distinct,
approximation,
exchange,
shape,
replacement,
};
Ok(SlotOutcome::LeftMutation(Box::new(Mutation {
slot,
target,
witness,
})))
}
pub fn mutate_at_with_cache(
pair: &SupportTauTiltingPair,
slot: usize,
summand_indices: &[usize],
fresh_index: usize,
cache: Option<&mut TauCache>,
) -> Result<SlotOutcome, MutationError> {
let input = prepare_input(pair, slot, summand_indices)?;
let maps = hom(&input.u, &input.x)?;
if trace_dims(&maps, &input.x) == input.x.dim_vector() {
return Ok(SlotOutcome::NoLeftMutation(FacWitness {
module: input.u,
summands: input
.u_dec
.summands()
.iter()
.map(|summand| summand.module().clone())
.collect(),
summand: input.x,
maps,
}));
}
let mut owned = TauCache::new();
let cache = cache.unwrap_or(&mut owned);
prepare_mutation(pair, slot, input, fresh_index, cache)
}