summaryrefslogtreecommitdiffstats
path: root/vendor/varisat/src/proof/map_step.rs
blob: 0c08e65105f56fd4220eaad21c3d8eab444a61c5 (plain)
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
//! Maps literals and hashes of clause steps between the solver and the checker.

use varisat_formula::{Lit, Var};

use super::{ClauseHash, ProofStep};

/// Maps literals and hashes of clause steps between the solver and the checker.
#[derive(Default)]
pub struct MapStep {
    lit_buf: Vec<Lit>,
    hash_buf: Vec<ClauseHash>,
    unit_buf: Vec<(Lit, ClauseHash)>,
}

impl MapStep {
    pub fn map_lits(&mut self, lits: &[Lit], map_var: impl Fn(Var) -> Var) -> &[Lit] {
        let map_var_ref = &map_var;
        self.lit_buf.clear();
        self.lit_buf
            .extend(lits.iter().map(|lit| lit.map_var(map_var_ref)));
        &self.lit_buf
    }

    pub fn map<'s, 'a, 'b>(
        &'a mut self,
        step: &ProofStep<'b>,
        map_var: impl Fn(Var) -> Var,
        map_hash: impl Fn(ClauseHash) -> ClauseHash,
    ) -> ProofStep<'s>
    where
        'a: 's,
        'b: 's,
    {
        let map_var_ref = &map_var;
        let map_lit = |lit: Lit| lit.map_var(map_var_ref);
        match *step {
            ProofStep::AddClause { clause } => {
                self.lit_buf.clear();
                self.lit_buf.extend(clause.iter().cloned().map(map_lit));
                ProofStep::AddClause {
                    clause: &self.lit_buf,
                }
            }

            ProofStep::AtClause {
                redundant,
                clause,
                propagation_hashes,
            } => {
                self.lit_buf.clear();
                self.lit_buf.extend(clause.iter().cloned().map(map_lit));
                self.hash_buf.clear();
                self.hash_buf
                    .extend(propagation_hashes.iter().cloned().map(map_hash));
                ProofStep::AtClause {
                    redundant,
                    clause: &self.lit_buf,
                    propagation_hashes: &self.hash_buf,
                }
            }

            ProofStep::UnitClauses { units } => {
                self.unit_buf.clear();
                self.unit_buf.extend(
                    units
                        .iter()
                        .map(|&(lit, hash)| (map_lit(lit), map_hash(hash))),
                );
                ProofStep::UnitClauses {
                    units: &self.unit_buf,
                }
            }

            ProofStep::DeleteClause { clause, proof } => {
                self.lit_buf.clear();
                self.lit_buf.extend(clause.iter().cloned().map(map_lit));
                ProofStep::DeleteClause {
                    clause: &self.lit_buf,
                    proof,
                }
            }

            ProofStep::Model { assignment } => {
                self.lit_buf.clear();
                self.lit_buf.extend(assignment.iter().cloned().map(map_lit));
                ProofStep::Model {
                    assignment: &self.lit_buf,
                }
            }

            ProofStep::Assumptions { assumptions } => {
                self.lit_buf.clear();
                self.lit_buf
                    .extend(assumptions.iter().cloned().map(map_lit));
                ProofStep::Assumptions {
                    assumptions: &self.lit_buf,
                }
            }

            ProofStep::FailedAssumptions {
                failed_core,
                propagation_hashes,
            } => {
                self.lit_buf.clear();
                self.lit_buf
                    .extend(failed_core.iter().cloned().map(map_lit));
                self.hash_buf.clear();
                self.hash_buf
                    .extend(propagation_hashes.iter().cloned().map(map_hash));
                ProofStep::FailedAssumptions {
                    failed_core: &self.lit_buf,
                    propagation_hashes: &self.hash_buf,
                }
            }

            ProofStep::ChangeHashBits { .. } | ProofStep::End => *step,

            ProofStep::SolverVarName { .. }
            | ProofStep::UserVarName { .. }
            | ProofStep::DeleteVar { .. }
            | ProofStep::ChangeSamplingMode { .. } => {
                // while these steps do contain variables, they are used to update the mapping, so
                // they shouldn't be mapped themselves.
                *step
            }
        }
    }
}