Branch data Line data Source code
1 : : /******************************************************************************
2 : : * This file is part of the cvc5 project.
3 : : *
4 : : * Copyright (c) 2009-2026 by the authors listed in the file AUTHORS
5 : : * in the top-level source directory and their institutional affiliations.
6 : : * All rights reserved. See the file COPYING in the top-level source
7 : : * directory for licensing information.
8 : : * ****************************************************************************
9 : : *
10 : : * Implementation of proof step and proof step buffer utilities.
11 : : */
12 : :
13 : : #include "proof/proof_step_buffer.h"
14 : :
15 : : #include "proof/proof.h"
16 : : #include "proof/proof_checker.h"
17 : :
18 : : using namespace cvc5::internal::kind;
19 : :
20 : : namespace cvc5::internal {
21 : :
22 : 45005 : ProofStep::ProofStep() : d_rule(ProofRule::UNKNOWN) {}
23 : 16768814 : ProofStep::ProofStep(ProofRule r,
24 : : const std::vector<Node>& children,
25 : 16768814 : const std::vector<Node>& args)
26 : 16768814 : : d_rule(r), d_children(children), d_args(args)
27 : : {
28 : 16768814 : }
29 : 0 : std::ostream& operator<<(std::ostream& out, ProofStep step)
30 : : {
31 : 0 : out << "(step " << step.d_rule;
32 [ - - ]: 0 : for (const Node& c : step.d_children)
33 : : {
34 : 0 : out << " " << c;
35 : : }
36 [ - - ]: 0 : if (!step.d_args.empty())
37 : : {
38 : 0 : out << " :args";
39 [ - - ]: 0 : for (const Node& a : step.d_args)
40 : : {
41 : 0 : out << " " << a;
42 : : }
43 : : }
44 : 0 : out << ")";
45 : 0 : return out;
46 : : }
47 : :
48 : 3914135 : ProofStepBuffer::ProofStepBuffer(ProofChecker* pc,
49 : : bool ensureUnique,
50 : 3914135 : bool autoSym)
51 : 3914135 : : d_autoSym(autoSym), d_checker(pc), d_ensureUnique(ensureUnique)
52 : : {
53 : 3914135 : }
54 : :
55 : 425966 : Node ProofStepBuffer::tryStep(ProofRule id,
56 : : const std::vector<Node>& children,
57 : : const std::vector<Node>& args,
58 : : Node expected)
59 : : {
60 : : bool added;
61 : 425966 : return tryStep(added, id, children, args, expected);
62 : : }
63 : :
64 : 476405 : Node ProofStepBuffer::tryStep(bool& added,
65 : : ProofRule id,
66 : : const std::vector<Node>& children,
67 : : const std::vector<Node>& args,
68 : : Node expected)
69 : : {
70 [ - + ]: 476405 : if (d_checker == nullptr)
71 : : {
72 : 0 : added = false;
73 : 0 : DebugUnhandled() << "ProofStepBuffer::ProofStepBuffer: no proof checker.";
74 : : return Node::null();
75 : : }
76 : : Node res =
77 : 476405 : d_checker->checkDebug(id, children, args, expected, "pf-step-buffer");
78 [ + + ]: 476405 : if (!res.isNull())
79 : : {
80 : : // add proof step
81 : 456134 : added = addStep(id, children, args, res);
82 : : }
83 : : else
84 : : {
85 : 20271 : added = false;
86 : : }
87 : 476405 : return res;
88 : 476405 : }
89 : :
90 : 16505192 : bool ProofStepBuffer::addStep(ProofRule id,
91 : : const std::vector<Node>& children,
92 : : const std::vector<Node>& args,
93 : : Node expected)
94 : : {
95 [ + + ]: 16505192 : if (d_ensureUnique)
96 : : {
97 [ + + ]: 64321 : if (d_allSteps.find(expected) != d_allSteps.end())
98 : : {
99 [ + - ]: 10206 : Trace("psb-debug") << "Discard " << expected << " from " << id
100 : 5103 : << std::endl;
101 : 5103 : return false;
102 : : }
103 : 59218 : d_allSteps.insert(expected);
104 : : // if we are automatically considering symmetry, we also add the symmetric
105 : : // fact here
106 [ + - ]: 59218 : if (d_autoSym)
107 : : {
108 : 59218 : Node sexpected = CDProof::getSymmFact(expected);
109 [ + + ]: 59218 : if (!sexpected.isNull())
110 : : {
111 : 49434 : d_allSteps.insert(sexpected);
112 : : }
113 : 59218 : }
114 [ + - ]: 59218 : Trace("psb-debug") << "Add " << expected << " from " << id << std::endl;
115 : : }
116 : 16500089 : d_steps.push_back(
117 : 33000178 : std::pair<Node, ProofStep>(expected, ProofStep(id, children, args)));
118 : 16500089 : return true;
119 : : }
120 : 69 : bool ProofStepBuffer::addTrustedStep(TrustId id,
121 : : const std::vector<Node>& children,
122 : : const std::vector<Node>& args,
123 : : Node conc)
124 : : {
125 : 69 : std::vector<Node> sargs;
126 : 69 : sargs.push_back(mkTrustId(conc.getNodeManager(), id));
127 : 69 : sargs.push_back(conc);
128 : 69 : sargs.insert(sargs.end(), args.begin(), args.end());
129 : 138 : return addStep(ProofRule::TRUST, children, sargs, conc);
130 : 69 : }
131 : :
132 : 0 : void ProofStepBuffer::addSteps(ProofStepBuffer& psb)
133 : : {
134 : 0 : const std::vector<std::pair<Node, ProofStep>>& steps = psb.getSteps();
135 [ - - ]: 0 : for (const std::pair<Node, ProofStep>& step : steps)
136 : : {
137 : 0 : addStep(step.second.d_rule,
138 : 0 : step.second.d_children,
139 : 0 : step.second.d_args,
140 : 0 : step.first);
141 : : }
142 : 0 : }
143 : :
144 : 0 : void ProofStepBuffer::popStep()
145 : : {
146 : 0 : Assert(!d_steps.empty());
147 [ - - ]: 0 : if (!d_steps.empty())
148 : : {
149 [ - - ]: 0 : if (d_ensureUnique)
150 : : {
151 : 0 : d_allSteps.erase(d_steps.back().first);
152 : : }
153 : 0 : d_steps.pop_back();
154 : : }
155 : 0 : }
156 : :
157 : 0 : size_t ProofStepBuffer::getNumSteps() const { return d_steps.size(); }
158 : :
159 : 3901614 : const std::vector<std::pair<Node, ProofStep>>& ProofStepBuffer::getSteps() const
160 : : {
161 : 3901614 : return d_steps;
162 : : }
163 : :
164 : 59569 : void ProofStepBuffer::clear()
165 : : {
166 : 59569 : d_steps.clear();
167 : 59569 : d_allSteps.clear();
168 : 59569 : }
169 : :
170 : : } // namespace cvc5::internal
|