semi-persistent-egraph 0.2.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
//! AC congruence completion: superposition + inter-reduction during rebuild.
//!
//! This implements the completion machinery intended to recover AC congruence
//! consequences that canonization alone cannot provide: recanonicalizing an AC
//! node substitutes equal *atoms* but never equal *sub-sums*, so equalities like
//! `+(a,b)=c ∧ +(b,d)=e ⊨ +(c,d)=+(a,e)` are otherwise missed. The code-to-paper
//! correspondence is tested on finite fixtures, but an end-to-end completeness
//! theorem for this implementation remains open. See
//! `doc/design/ac-congruence-completeness.md` for the theory and
//! `doc/design/ac-completion-spec.md` for the engine-level spec.
//!
//! This file contains the representation-agnostic per-round snapshot used by
//! completion diagnostics and conformance tests. The completion round itself
//! lives in `egraph.rs::cc_round` and finds partners through class use-lists.
//! The snapshot must cover both MSet and Set nodes to agree with
//! `completion_node_ids` semantics.

use crate::canon::{MSetCanon, VarCanon};
use crate::config::EGraphConfig;
use crate::egraph::EGraph;
use crate::literal::LitVal;
use std::collections::HashMap;

/// Same rationale as `index::FastMap`: `(op, child_repr)` keys are internal
/// dense ids, so SipHash's collision resistance buys nothing here.
type FastMap<K, V> = HashMap<K, V, foldhash::fast::RandomState>;

/// A per-round search index over the live AC nodes, restricted to AC ops.
///
/// This is the narrow slice of [`crate::index::IndexStore`] that the completion
/// search needs: the spec's candidate set `⋃_{x ∈ distinct(M)} by_contains[x] ∩
/// by_op[f]`. We pre-intersect with `by_op[f]` by keying on
/// `(op, child_repr)`, so a lookup directly yields the AC nodes of op `f` that
/// contain child class `x` — no separate `by_op` intersection at use time.
///
/// Unlike `IndexStore`, this diagnostic index covers only AC ops and carries no
/// `by_repr`/`by_child_pos`. Production completion currently finds candidates
/// through class use-lists instead. Subsumed nodes are skipped, matching
/// `IndexStore`.
///
/// Node ids in each bucket are sorted and deduplicated.
pub struct CcSnapshot<Cfg: EGraphConfig> {
    /// `(op, child_repr) → sorted, deduped AC node ids of that op containing the child`.
    by_op_contains: FastMap<(Cfg::O, Cfg::G), Vec<Cfg::G>>,
    /// All live (non-subsumed) AC node ids, sorted — the set of completion targets/rules.
    completion_nodes: Vec<Cfg::G>,
}

impl<Cfg: EGraphConfig> CcSnapshot<Cfg>
where
    MSetCanon: VarCanon<Cfg::G, Cfg::C>,
{
    /// Build the snapshot from the current e-graph state. Walks every node once,
    /// keeping only AC nodes (`ops.is_mset`), skipping subsumed nodes.
    pub fn build<L: LitVal, const TRACK: bool, const PROOFS: bool>(
        eg: &EGraph<Cfg, L, TRACK, PROOFS>,
    ) -> Self {
        let mut by_op_contains: FastMap<(Cfg::O, Cfg::G), Vec<Cfg::G>> = FastMap::default();
        let mut completion_nodes: Vec<Cfg::G> = Vec::new();

        // Active completion nodes only: skip user-subsumed (not matchable) and AC-collapsed
        // (reducible by a smaller rule) — neither is a completion rule. See §6b. Both
        // completion partitions are covered: MSet (plain AC / nilpotent) and Set (ACI).
        let inactive = crate::node_types::FLAG_SUBSUMED | crate::node_types::FLAG_AC_COLLAPSED;
        for gid in eg.node_ids() {
            if eg.node_flags(gid) & inactive != 0 {
                continue;
            }
            let op = eg.node_op(gid);
            if !eg.ops().is_mset(op) && !eg.ops().is_set(op) {
                continue;
            }
            completion_nodes.push(gid);

            // Bucket this node under each distinct child class it contains.
            let mut seen: Vec<Cfg::G> = Vec::new();
            eg.for_each_child(gid, |child, _mult| {
                let cr = eg.class_repr(child);
                if !seen.contains(&cr) {
                    seen.push(cr);
                    by_op_contains.entry((op, cr)).or_default().push(gid);
                }
            });
        }

        for v in by_op_contains.values_mut() {
            v.sort_unstable();
            v.dedup();
        }
        completion_nodes.sort_unstable();
        completion_nodes.dedup();

        Self {
            by_op_contains,
            completion_nodes,
        }
    }

    /// All live AC node ids (sorted). These are the completion targets — each is
    /// both a node `+M = d` to complete and a rule `+M → d` to complete against.
    pub fn completion_nodes(&self) -> &[Cfg::G] {
        &self.completion_nodes
    }

    /// AC nodes of op `op` that contain child class `child_repr` (sorted, deduped).
    /// This is the spec's `by_contains[child_repr] ∩ by_op[op]`, pre-intersected.
    pub fn partners(&self, op: Cfg::O, child_repr: Cfg::G) -> &[Cfg::G] {
        self.by_op_contains
            .get(&(op, child_repr))
            .map(|v| v.as_slice())
            .unwrap_or(&[])
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::egraph::EGraph31;
    use crate::literal::NiraLitVal;

    /// Brute-force reference: AC nodes of `op` whose `mset_children` contain `cr`.
    fn ref_partners(
        eg: &EGraph31<NiraLitVal, false, false>,
        op: crate::id::OpId,
        cr: crate::id::ENodeId,
    ) -> Vec<crate::id::ENodeId> {
        let mut out = Vec::new();
        let mut buf = Vec::new();
        let inactive = crate::node_types::FLAG_SUBSUMED | crate::node_types::FLAG_AC_COLLAPSED;
        for i in 0..eg.node_count() {
            let gid = crate::id::ENodeId::new(i as u32);
            if eg.node_flags(gid) & inactive != 0 {
                continue;
            }
            if eg.node_op(gid) != op || !(eg.ops().is_mset(op) || eg.ops().is_set(op)) {
                continue;
            }
            buf.clear();
            eg.for_each_child(gid, |c, m| buf.push((c, m)));
            if buf.iter().any(|&(c, _)| eg.class_repr(c) == cr) {
                out.push(gid);
            }
        }
        out.sort_unstable();
        out.dedup();
        out
    }

    #[test]
    fn snapshot_matches_by_contains_variadic_fixture() {
        // Mirror of index.rs `by_contains_variadic`, but through the AC snapshot.
        let mut eg = EGraph31::<NiraLitVal, false, false>::new();
        let int = eg.intern_sort("Int");
        let plus = eg.register_mset("plus", int, int);
        let x_op = eg.register_op0("x", int);
        let y_op = eg.register_op0("y", int);
        let z_op = eg.register_op0("z", int);

        let x = eg.add(x_op, &[]);
        let y = eg.add(y_op, &[]);
        let z = eg.add(z_op, &[]);
        let pxy = eg.add(plus, &[x, y]);
        let pxz = eg.add(plus, &[x, z]);

        let snap = CcSnapshot::build(&eg);

        // x is contained in both pxy and pxz.
        let mut cx = snap.partners(plus, x).to_vec();
        cx.sort_unstable();
        assert_eq!(cx, vec![pxy.min(pxz), pxy.max(pxz)]);

        // y is contained only in pxy.
        assert_eq!(snap.partners(plus, y), &[pxy]);

        // completion_nodes lists exactly the two AC sums (not the op0 leaves).
        assert_eq!(snap.completion_nodes(), &[pxy.min(pxz), pxy.max(pxz)]);
    }

    #[test]
    fn snapshot_only_indexes_ac_ops() {
        let mut eg = EGraph31::<NiraLitVal, false, false>::new();
        let int = eg.intern_sort("Int");
        let plus = eg.register_mset("plus", int, int);
        let f = eg.register_op1("f", int, int); // Normal — must not appear
        let x_op = eg.register_op0("x", int);

        let x = eg.add(x_op, &[]);
        let fx = eg.add(f, &[x]);
        let pfx = eg.add(plus, &[x, fx]);

        let snap = CcSnapshot::build(&eg);

        // Only the AC node is a completion target.
        assert_eq!(snap.completion_nodes(), &[pfx]);
        // The plain node f(x) is not bucketed even though it contains x.
        assert!(snap.partners(f, eg.class_repr(x)).is_empty());
        // The AC sum is found under both its children.
        assert_eq!(snap.partners(plus, eg.class_repr(x)), &[pfx]);
        assert_eq!(snap.partners(plus, eg.class_repr(fx)), &[pfx]);
    }

    #[test]
    fn snapshot_agrees_with_brute_force_after_merge() {
        // Multiplicities + a merge that changes class reprs.
        let mut eg = EGraph31::<NiraLitVal, false, false>::new();
        let int = eg.intern_sort("Int");
        let plus = eg.register_mset("plus", int, int);
        let a_op = eg.register_op0("a", int);
        let b_op = eg.register_op0("b", int);
        let d_op = eg.register_op0("d", int);

        let a = eg.add(a_op, &[]);
        let b = eg.add(b_op, &[]);
        let d = eg.add(d_op, &[]);
        let _ab = eg.add(plus, &[a, b]);
        let _bd = eg.add(plus, &[b, d]);
        let _abb = eg.add(plus, &[a, b, b]); // multiplicity > 1 on b

        // Merge a and d, then rebuild so reprs settle.
        eg.merge(a, d);
        eg.rebuild();

        let snap = CcSnapshot::build(&eg);

        // For every (AC op, child class) the snapshot must equal the brute-force set.
        for x in [a, b, d] {
            let cr = eg.class_repr(x);
            let mut got = snap.partners(plus, cr).to_vec();
            got.sort_unstable();
            assert_eq!(
                got,
                ref_partners(&eg, plus, cr),
                "mismatch for child {cr:?}"
            );
        }
    }

    #[test]
    fn cc_collapsed_leaves_completion_set_but_stays_matchable() {
        // FLAG_AC_COLLAPSED and FLAG_SUBSUMED are distinct (design §6b): a collapsed node
        // drops out of completion's active set but remains visible to the matcher's index.
        use crate::index::IndexStore;
        let mut eg = EGraph31::<NiraLitVal, false, false>::new();
        let int = eg.intern_sort("Int");
        let plus = eg.register_mset("plus", int, int);
        let a_op = eg.register_op0("a", int);
        let b_op = eg.register_op0("b", int);
        let a = eg.add(a_op, &[]);
        let b = eg.add(b_op, &[]);
        let pab = eg.add(plus, &[a, b]);

        // Baseline: pab is both a completion candidate and in the matcher index.
        assert_eq!(CcSnapshot::build(&eg).completion_nodes(), &[pab]);
        assert!(IndexStore::build(&eg).nodes_by_op(plus).contains(&pab));

        // Collapse it (the completion-internal retirement).
        eg.set_cc_collapsed(pab);

        // Gone from completion's active set...
        assert!(
            CcSnapshot::build(&eg).completion_nodes().is_empty(),
            "AC-collapsed node must leave the completion active set"
        );
        // ...but still matchable: present in the index and still in its class.
        assert!(
            IndexStore::build(&eg).nodes_by_op(plus).contains(&pab),
            "AC-collapsed node must stay visible to the matcher (not subsumed)"
        );
        assert_eq!(eg.class_repr(pab), eg.class_repr(pab));

        // Contrast: user subsume DOES hide it from the matcher.
        eg.subsume(pab);
        assert!(
            !IndexStore::build(&eg).nodes_by_op(plus).contains(&pab),
            "subsumed node must be hidden from the matcher index"
        );
    }
}