semi-persistent-egraph 0.3.0

Semi-persistent equality saturation engine with e-matching, rewrite scheduling, and proof logging
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//! Pair-mode bounded relaxation over the finite class-pair graph.
//!
//! E-classes are regular tree grammars. Their product is a finite AND/OR
//! hypergraph whose states are ordered class pairs. Cycles in that graph do
//! not require recursive evaluation: round `d` below is intended to compute
//! the best finite derivation of structural depth at most `d`, starting from
//! the always available terminal generalize action at depth zero. Finite
//! oracles exercise this invariant; it is not yet a machine-checked refinement.
//!
//! The implementation relies on the following cycle-erasure argument: a
//! minimum-size derivation does not repeat a pair on one root-to-leaf path. If
//! it did, the descendant occurrence would itself be a valid result for the
//! same pair, while the surrounding structural path contributes at least one
//! ordinary term node. Removing the path is therefore strictly smaller. With
//! `N` reachable pair states, the argument gives a depth-`N` bound, which the
//! loop asserts. This argument has finite regression coverage but is not yet a
//! machine-checked theorem; see design chapter 19, section 9.6.

use std::collections::HashMap;
use std::time::{Duration, Instant};

use crate::canon::{MSetCanon, VarCanon};
use crate::config::{AuIds, EGraphConfig};
use crate::literal::LitVal;

use super::ac_repr;
use super::actions::{ActionCache, generate_actions};
use super::egraph_api::{AuSnapshot, ClassOf};
use super::estimates::lb_pair;
use super::terms::{TermOp, TermPool, build_best_term, evaluate_generalize_action};
use super::transport::{Cell, TransportProblem, TransportSolution, solve_transport};

type Quality = (u32, u32);

struct StructuralAction<O> {
    op: O,
    children: Vec<(usize, u64)>,
}

struct TransportAction<O> {
    op: O,
    row_supply: Vec<u32>,
    col_demand: Vec<u32>,
    cells: Vec<Vec<usize>>,
}

struct PairNode<Cfg: EGraphConfig> {
    left: ClassOf<Cfg>,
    right: ClassOf<Cfg>,
    structural: Vec<StructuralAction<Cfg::O>>,
    transport: Vec<TransportAction<Cfg::O>>,
}

struct PairGraph<Cfg: EGraphConfig> {
    nodes: Vec<PairNode<Cfg>>,
    by_pair: HashMap<(ClassOf<Cfg>, ClassOf<Cfg>), usize>,
}

impl<Cfg: EGraphConfig> PairGraph<Cfg> {
    fn new(left: ClassOf<Cfg>, right: ClassOf<Cfg>) -> Self {
        let mut by_pair = HashMap::new();
        by_pair.insert((left, right), 0);
        PairGraph {
            nodes: vec![PairNode {
                left,
                right,
                structural: Vec::new(),
                transport: Vec::new(),
            }],
            by_pair,
        }
    }

    fn intern(&mut self, left: ClassOf<Cfg>, right: ClassOf<Cfg>) -> usize {
        if let Some(&index) = self.by_pair.get(&(left, right)) {
            return index;
        }
        let index = self.nodes.len();
        self.nodes.push(PairNode {
            left,
            right,
            structural: Vec::new(),
            transport: Vec::new(),
        });
        self.by_pair.insert((left, right), index);
        index
    }
}

pub(crate) struct FixedExact<T> {
    pub(crate) term: T,
    pub(crate) complete: bool,
    #[allow(dead_code)]
    pub(crate) states: usize,
    #[allow(dead_code)]
    pub(crate) rounds: usize,
}

#[inline]
fn expired(deadline: Option<Instant>) -> bool {
    deadline.is_some_and(|limit| Instant::now() >= limit)
}

fn narrow_counts<C>(monomial: &[(C, u64)]) -> Option<Vec<u32>> {
    monomial
        .iter()
        .map(|(_, count)| u32::try_from(*count).ok())
        .collect()
}

fn build_graph<Cfg: EGraphConfig, L: LitVal, const T: bool, const P: bool>(
    snap: &AuSnapshot<Cfg, L, T, P>,
    left: ClassOf<Cfg>,
    right: ClassOf<Cfg>,
    deadline: Option<Instant>,
) -> Option<PairGraph<Cfg>>
where
    MSetCanon: VarCanon<Cfg::G, Cfg::C>,
{
    let mut graph = PairGraph::new(left, right);
    let mut cache: ActionCache<Cfg::O, Cfg::Au, Cfg::M> =
        ActionCache::without_ac_actions(usize::MAX);
    let mut cursor = 0;

    while cursor < graph.nodes.len() {
        if expired(deadline) {
            return None;
        }
        let left = graph.nodes[cursor].left;
        let right = graph.nodes[cursor].right;
        if left == right {
            cursor += 1;
            continue;
        }

        generate_actions(snap, &mut cache, left, right);
        let actions = cache
            .get(left, right)
            .expect("action generation must populate the cache")
            .to_vec();
        let mut structural = Vec::with_capacity(actions.len());
        for action in actions {
            let children = action
                .pairs
                .iter()
                .map(|pair| {
                    (
                        graph.intern(pair.left, pair.right),
                        crate::multiplicity::MultiplicityLike::to_u64(pair.count),
                    )
                })
                .collect();
            structural.push(StructuralAction {
                op: action.op,
                children,
            });
        }

        let mut transport = Vec::new();
        for op in ac_repr::common_ac_ops(snap, left, right) {
            for (left_mono, right_mono, _) in ac_repr::representation_pairs(snap, left, right, op) {
                let Some(row_supply) = narrow_counts(&left_mono) else {
                    continue;
                };
                let Some(col_demand) = narrow_counts(&right_mono) else {
                    continue;
                };
                let cells = left_mono
                    .iter()
                    .map(|(left_child, _)| {
                        right_mono
                            .iter()
                            .map(|(right_child, _)| graph.intern(*left_child, *right_child))
                            .collect()
                    })
                    .collect();
                transport.push(TransportAction {
                    op,
                    row_supply,
                    col_demand,
                    cells,
                });
            }
        }

        graph.nodes[cursor].structural = structural;
        graph.nodes[cursor].transport = transport;
        cursor += 1;
    }
    Some(graph)
}

#[inline]
fn cap_quality(size: u128, variant_mass: u128) -> Quality {
    (
        size.min(u128::from(u32::MAX)) as u32,
        variant_mass.min(u128::from(u32::MAX)) as u32,
    )
}

fn structural_quality<O>(action: &StructuralAction<O>, qualities: &[Quality]) -> Quality {
    let mut size = 1u128;
    let mut variant_mass = 0u128;
    for &(child, count) in &action.children {
        size = size.saturating_add(u128::from(qualities[child].0) * u128::from(count));
        variant_mass =
            variant_mass.saturating_add(u128::from(qualities[child].1) * u128::from(count));
    }
    cap_quality(size, variant_mass)
}

fn structural_size_bound<Cfg: EGraphConfig, L: LitVal, const T: bool, const P: bool>(
    snap: &AuSnapshot<Cfg, L, T, P>,
    graph: &PairGraph<Cfg>,
    action: &StructuralAction<Cfg::O>,
) -> u128
where
    MSetCanon: VarCanon<Cfg::G, Cfg::C>,
{
    action
        .children
        .iter()
        .fold(1u128, |bound, &(child, count)| {
            let node = &graph.nodes[child];
            bound.saturating_add(
                u128::from(lb_pair(snap, node.left, node.right).0) * u128::from(count),
            )
        })
}

fn transport_solution<O>(
    action: &TransportAction<O>,
    qualities: &[Quality],
) -> Option<TransportSolution> {
    let cost = action
        .cells
        .iter()
        .map(|row| {
            row.iter()
                .map(|&child| Cell::Cost(qualities[child].0, qualities[child].1))
                .collect()
        })
        .collect();
    solve_transport(&TransportProblem {
        row_supply: action.row_supply.clone(),
        col_demand: action.col_demand.clone(),
        cost,
    })
}

fn transport_size_bound<Cfg: EGraphConfig, L: LitVal, const T: bool, const P: bool>(
    snap: &AuSnapshot<Cfg, L, T, P>,
    graph: &PairGraph<Cfg>,
    action: &TransportAction<Cfg::O>,
) -> Option<u128>
where
    MSetCanon: VarCanon<Cfg::G, Cfg::C>,
{
    let cost = action
        .cells
        .iter()
        .map(|row| {
            row.iter()
                .map(|&child| {
                    let node = &graph.nodes[child];
                    Cell::Cost(lb_pair(snap, node.left, node.right).0, 0)
                })
                .collect()
        })
        .collect();
    solve_transport(&TransportProblem {
        row_supply: action.row_supply.clone(),
        col_demand: action.col_demand.clone(),
        cost,
    })
    .map(|solution| solution.total.0.saturating_add(1))
}

fn solution_quality(solution: &TransportSolution) -> Quality {
    cap_quality(solution.total.0.saturating_add(1), solution.total.1)
}

fn structural_term<Cfg: EGraphConfig, L: LitVal, const T: bool, const P: bool>(
    snap: &AuSnapshot<Cfg, L, T, P>,
    pool: &mut TermPool<Cfg::O, Cfg::V, Cfg::Au>,
    action: &StructuralAction<Cfg::O>,
    terms: &[<Cfg::Au as AuIds>::Term],
) -> <Cfg::Au as AuIds>::Term
where
    MSetCanon: VarCanon<Cfg::G, Cfg::C>,
{
    let children: Vec<_> = action
        .children
        .iter()
        .map(|&(child, count)| (terms[child], count))
        .collect();
    pool.intern_action_result(
        TermOp::EGraph(action.op),
        &children,
        snap.op_is_commutative(action.op),
    )
}

fn transport_term<Cfg: EGraphConfig>(
    pool: &mut TermPool<Cfg::O, Cfg::V, Cfg::Au>,
    action: &TransportAction<Cfg::O>,
    solution: &TransportSolution,
    terms: &[<Cfg::Au as AuIds>::Term],
) -> <Cfg::Au as AuIds>::Term {
    let mut children = Vec::new();
    for (row, flow_row) in solution.flow.iter().enumerate() {
        for (col, &count) in flow_row.iter().enumerate() {
            if count > 0 {
                children.push((terms[action.cells[row][col]], u64::from(count)));
            }
        }
    }
    pool.intern_action_result(TermOp::EGraph(action.op), &children, true)
}

#[allow(clippy::too_many_arguments)]
pub(crate) fn run_in<Cfg: EGraphConfig, L: LitVal, const T: bool, const P: bool>(
    snap: &AuSnapshot<Cfg, L, T, P>,
    pool: &mut TermPool<Cfg::O, Cfg::V, Cfg::Au>,
    left: ClassOf<Cfg>,
    right: ClassOf<Cfg>,
    deadline: Option<Duration>,
    pruning: bool,
    warm_incumbent: Option<<Cfg::Au as AuIds>::Term>,
) -> Result<FixedExact<<Cfg::Au as AuIds>::Term>, super::AuError>
where
    MSetCanon: VarCanon<Cfg::G, Cfg::C>,
{
    snap.validate_finite_from(left)?;
    snap.validate_finite_from(right)?;
    let deadline = deadline.map(|duration| Instant::now() + duration);

    let root_fallback = if left == right {
        build_best_term(snap, pool, left)
    } else {
        evaluate_generalize_action(snap, pool, left, right)
    };
    let root_term = warm_incumbent
        .filter(|term| pool.quality(*term) < pool.quality(root_fallback))
        .unwrap_or(root_fallback);

    let Some(graph) = build_graph(snap, left, right, deadline) else {
        return Ok(FixedExact {
            term: root_term,
            complete: false,
            states: 1,
            rounds: 0,
        });
    };

    let mut qualities = Vec::with_capacity(graph.nodes.len());
    let mut terms = Vec::with_capacity(graph.nodes.len());
    for (index, node) in graph.nodes.iter().enumerate() {
        if expired(deadline) {
            return Ok(FixedExact {
                term: root_term,
                complete: false,
                states: graph.nodes.len(),
                rounds: 0,
            });
        }
        let term = if index == 0 {
            root_term
        } else if node.left == node.right {
            build_best_term(snap, pool, node.left)
        } else {
            evaluate_generalize_action(snap, pool, node.left, node.right)
        };
        qualities.push(pool.quality(term));
        terms.push(term);
    }

    let mut completed_rounds = 0;
    for round in 0..=graph.nodes.len() {
        if expired(deadline) {
            return Ok(FixedExact {
                term: terms[0],
                complete: false,
                states: graph.nodes.len(),
                rounds: completed_rounds,
            });
        }

        let mut next_qualities = qualities.clone();
        let mut next_terms = terms.clone();
        let mut changed = false;

        for (node_index, node) in graph.nodes.iter().enumerate() {
            if expired(deadline) {
                return Ok(FixedExact {
                    term: terms[0],
                    complete: false,
                    states: graph.nodes.len(),
                    rounds: completed_rounds,
                });
            }
            if node.left == node.right {
                continue;
            }
            let mut best_quality = qualities[node_index];
            let mut best_term = terms[node_index];

            for action in &node.structural {
                if pruning
                    && structural_size_bound(snap, &graph, action) > u128::from(best_quality.0)
                {
                    continue;
                }
                let candidate_quality = structural_quality(action, &qualities);
                if candidate_quality < best_quality {
                    let candidate = structural_term(snap, pool, action, &terms);
                    debug_assert_eq!(pool.quality(candidate), candidate_quality);
                    best_quality = candidate_quality;
                    best_term = candidate;
                }
            }

            for action in &node.transport {
                if pruning {
                    let Some(bound) = transport_size_bound(snap, &graph, action) else {
                        continue;
                    };
                    if bound > u128::from(best_quality.0) {
                        continue;
                    }
                }
                let Some(solution) = transport_solution(action, &qualities) else {
                    continue;
                };
                let candidate_quality = solution_quality(&solution);
                if candidate_quality < best_quality {
                    let candidate = transport_term::<Cfg>(pool, action, &solution, &terms);
                    debug_assert_eq!(pool.quality(candidate), candidate_quality);
                    best_quality = candidate_quality;
                    best_term = candidate;
                }
            }

            if best_quality < qualities[node_index] {
                changed = true;
                next_qualities[node_index] = best_quality;
                next_terms[node_index] = best_term;
            }
        }

        if !changed {
            return Ok(FixedExact {
                term: terms[0],
                complete: true,
                states: graph.nodes.len(),
                rounds: completed_rounds,
            });
        }
        assert!(
            round < graph.nodes.len(),
            "cycle-complete AU relaxation exceeded the pair-simple depth bound"
        );
        qualities = next_qualities;
        terms = next_terms;
        completed_rounds += 1;
    }
    unreachable!("the pair-depth loop either stabilizes or asserts")
}

pub(crate) fn run<Cfg: EGraphConfig, L: LitVal, const T: bool, const P: bool>(
    snap: &AuSnapshot<Cfg, L, T, P>,
    left: ClassOf<Cfg>,
    right: ClassOf<Cfg>,
    deadline: Option<Duration>,
    pruning: bool,
) -> Result<
    (
        FixedExact<<Cfg::Au as AuIds>::Term>,
        TermPool<Cfg::O, Cfg::V, Cfg::Au>,
    ),
    super::AuError,
>
where
    MSetCanon: VarCanon<Cfg::G, Cfg::C>,
{
    let mut pool = TermPool::new();
    let result = run_in(snap, &mut pool, left, right, deadline, pruning, None)?;
    Ok((result, pool))
}