semi-persistent-egraph 0.3.0

Semi-persistent equality saturation engine with e-matching, rewrite scheduling, and proof logging
Documentation
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//! Canonical AC/ACI monomial representations, shared by the exact solver's and
//! MCGS's min-cost transport paths (§3.4.4).
//!
//! A class viewed under an AC/ACI operator has one monomial representation per
//! member with that operator (the member's canonical child multiset), plus, when
//! the operator declares an identity, the virtual singleton monomial `{class¹}`
//! for classes with no such member (the e-graph canonizes one-child applications
//! to the bare child, so the member disappears while the algebraic reading
//! `op(x) = x` remains). Centralizing this here keeps the exact and MCGS paths
//! from drifting on identity, padding, and dedup semantics.

use crate::canon::{MSetCanon, VarCanon};
use crate::config::EGraphConfig;
use crate::containers::DenseId;
use crate::id::ENodeKind;
use crate::literal::LitVal;
use crate::multiplicity::MultiplicityLike;

use super::egraph_api::{AuSnapshot, ClassOf};

/// One canonical monomial: sorted, deduplicated `(child class, multiplicity)`
/// pairs with all multiplicities positive.
///
/// Multiplicities are held at the *surface* width (`u64`), not at the e-graph's
/// configured [`EGraphConfig::M`]. Two reasons: every supported configured width
/// widens into `u64` totally and losslessly, so reading a member's child
/// multiset never needs a fallible narrowing here; and a monomial's own
/// arithmetic — [`total`], identity padding — sums *across* entries, a quantity
/// legitimately wider than any single entry. Monomials are transient solver
/// scratch, never stored in the e-graph, so the width costs nothing per e-node.
pub type Monomial<C> = Vec<(C, u64)>;

/// Overflow message shared by the monomial summation sites. A wrapped
/// multiplicity would silently shrink a monomial (possibly to zero, breaking the
/// positivity invariant) and make the transport problem's supply and demand
/// disagree with the multisets they came from.
const MONO_OVERFLOW: &str = "AU monomial multiplicity exceeds u64";

fn canonize<C: DenseId>(mut m: Monomial<C>) -> Monomial<C> {
    m.sort_unstable_by_key(|(c, _)| c.to_usize());
    // Merge duplicate classes (defensive; canonical members are already merged).
    let mut out: Monomial<C> = Vec::with_capacity(m.len());
    for (c, k) in m {
        if k == 0 {
            continue;
        }
        match out.last_mut() {
            Some((lc, lk)) if *lc == c => *lk = lk.checked_add(k).expect(MONO_OVERFLOW),
            _ => out.push((c, k)),
        }
    }
    out
}

/// All canonical monomial representations of `class` under `op`.
/// `op` must have canon class MSet or Set.
pub fn monomials_of<Cfg: EGraphConfig, L: LitVal, const T: bool, const P: bool>(
    snap: &AuSnapshot<Cfg, L, T, P>,
    class: ClassOf<Cfg>,
    op: Cfg::O,
) -> Vec<Monomial<ClassOf<Cfg>>>
where
    MSetCanon: VarCanon<Cfg::G, Cfg::C>,
{
    let eg = snap.egraph();
    let kind = eg.ops().info(op).canon_class();
    debug_assert!(matches!(kind, ENodeKind::MSet | ENodeKind::Set));

    let mut reprs: Vec<Monomial<ClassOf<Cfg>>> = Vec::new();

    for &(member_op, member_id) in snap.members(class) {
        if member_op != op {
            continue;
        }
        let mono: Monomial<ClassOf<Cfg>> = if kind == ENodeKind::MSet {
            let mut buf: Vec<(Cfg::G, Cfg::M)> = Vec::new();
            eg.mset_children(member_id, &mut buf);
            canonize(
                buf.iter()
                    .map(|(g, m)| (snap.class_of(*g).unwrap(), m.to_u64()))
                    .collect(),
            )
        } else {
            let mut children: Monomial<ClassOf<Cfg>> = Vec::new();
            eg.for_each_child(member_id, |child, _| {
                children.push((snap.class_of(child).unwrap(), 1));
            });
            canonize(children)
        };
        if !reprs.contains(&mono) {
            reprs.push(mono);
        }
    }

    // Virtual singleton: with an identity, any class is also the monomial
    // {class¹} (algebraically op(x) = x via the identity collapse). This
    // reading is valid regardless of whether explicit op-members exist: a
    // class containing combine(a,b) is still itself a valid singleton
    // argument to combine, reading as combine(class, identity) = class.
    if snap.op_identity_class(op).is_some() {
        let singleton = vec![(class, 1)];
        if !reprs.contains(&singleton) {
            reprs.push(singleton);
        }
    }

    reprs
}

/// Total multiplicity of a monomial: the number of operator arguments it stands
/// for, i.e. a sum across entries rather than a single child's multiplicity.
///
/// Summation is checked. Padding derives a deficit from the difference of two
/// totals, so a wrapped total would produce a bogus deficit and pad the wrong
/// monomial by the wrong amount.
pub fn total<C: DenseId>(m: &Monomial<C>) -> u64 {
    m.iter()
        .try_fold(0u64, |acc, (_, k)| acc.checked_add(*k))
        .expect(MONO_OVERFLOW)
}

/// A padded representation pair. `pad_identity` records the identity class
/// when padding injected it into either side, `None` when the totals were
/// already equal. Consumers deriving cycle contexts need the provenance: the
/// injected identity class is not a structural child of any member, so
/// reachability-based context derivation records nothing for it, and only
/// this annotation lets contextual Exact keep its rank invariant (the OR key
/// of a padding-created cell must differ from its ancestors'). Root Exact has
/// bare-pair states and does not need the annotation for termination.
pub type PaddedPair<C> = (Monomial<C>, Monomial<C>, Option<C>);

/// Pad the smaller of two monomials with identity copies so totals are equal.
/// Returns `None` when totals differ and the operator has no identity.
pub fn pad_pair<Cfg: EGraphConfig, L: LitVal, const T: bool, const P: bool>(
    snap: &AuSnapshot<Cfg, L, T, P>,
    op: Cfg::O,
    left: &Monomial<ClassOf<Cfg>>,
    right: &Monomial<ClassOf<Cfg>>,
) -> Option<PaddedPair<ClassOf<Cfg>>>
where
    MSetCanon: VarCanon<Cfg::G, Cfg::C>,
{
    let lt = total(left);
    let rt = total(right);
    if lt == rt {
        return Some((left.clone(), right.clone(), None));
    }
    let id_class = snap.op_identity_class(op)?;
    let mut l = left.clone();
    let mut r = right.clone();
    if lt < rt {
        add_identity(&mut l, id_class, rt - lt);
    } else {
        add_identity(&mut r, id_class, lt - rt);
    }
    Some((canonize(l), canonize(r), Some(id_class)))
}

fn add_identity<C: DenseId>(m: &mut Monomial<C>, id_class: C, deficit: u64) {
    if let Some(entry) = m.iter_mut().find(|(c, _)| *c == id_class) {
        entry.1 = entry.1.checked_add(deficit).expect(MONO_OVERFLOW);
    } else {
        m.push((id_class, deficit));
    }
}

/// All padded representation pairs of classes `l` and `r` under `op`, ready for
/// matrix enumeration or transport solving. Pairs with unequal totals and no
/// identity are skipped. Deduplicated on the full triple: the same monomial
/// pair reached both with and without padding stays as two entries, because
/// the padding annotation changes how consumers derive cycle contexts and the
/// unpadded entry must keep its unrestricted subtree.
pub fn representation_pairs<Cfg: EGraphConfig, L: LitVal, const T: bool, const P: bool>(
    snap: &AuSnapshot<Cfg, L, T, P>,
    l: ClassOf<Cfg>,
    r: ClassOf<Cfg>,
    op: Cfg::O,
) -> Vec<PaddedPair<ClassOf<Cfg>>>
where
    MSetCanon: VarCanon<Cfg::G, Cfg::C>,
{
    let l_reprs = monomials_of(snap, l, op);
    let r_reprs = monomials_of(snap, r, op);
    // A pair of virtual singletons ({l¹} vs {r¹}) degenerates to AU(l, r)
    // itself and would recurse unproductively; require at least one real
    // op-member representation in the pair. A representation is virtual
    // exactly when it is the singleton of its own class.
    let is_virtual = |m: &Monomial<ClassOf<Cfg>>, class: ClassOf<Cfg>| {
        m.len() == 1 && m[0].0 == class && m[0].1 == 1
    };

    let mut out: Vec<PaddedPair<ClassOf<Cfg>>> = Vec::new();
    for lm in &l_reprs {
        for rm in &r_reprs {
            if is_virtual(lm, l) && is_virtual(rm, r) {
                continue;
            }
            if let Some(pair) = pad_pair(snap, op, lm, rm)
                && !out.contains(&pair)
            {
                out.push(pair);
            }
        }
    }
    out
}

/// The distinct AC/ACI operators relevant to the class pair `(l, r)`: any MSet
/// or Set operator appearing in either side's members. (Operators appearing on
/// one side only still matter through the virtual singleton of the other side.)
pub fn common_ac_ops<Cfg: EGraphConfig, L: LitVal, const T: bool, const P: bool>(
    snap: &AuSnapshot<Cfg, L, T, P>,
    l: ClassOf<Cfg>,
    r: ClassOf<Cfg>,
) -> Vec<Cfg::O>
where
    MSetCanon: VarCanon<Cfg::G, Cfg::C>,
{
    let eg = snap.egraph();
    let mut ops: Vec<Cfg::O> = Vec::new();
    for &(op, _) in snap.members(l).iter().chain(snap.members(r).iter()) {
        let kind = eg.ops().info(op).canon_class();
        if matches!(kind, ENodeKind::MSet | ENodeKind::Set) && !ops.contains(&op) {
            ops.push(op);
        }
    }
    ops.sort_unstable_by_key(|o| o.to_usize());
    ops
}