1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Andrew Reynolds, Aina Niemetz |
4 |
|
* |
5 |
|
* This file is part of the cvc5 project. |
6 |
|
* |
7 |
|
* Copyright (c) 2009-2021 by the authors listed in the file AUTHORS |
8 |
|
* in the top-level source directory and their institutional affiliations. |
9 |
|
* All rights reserved. See the file COPYING in the top-level source |
10 |
|
* directory for licensing information. |
11 |
|
* **************************************************************************** |
12 |
|
* |
13 |
|
* The TheoryRewriteEq preprocessing pass. |
14 |
|
*/ |
15 |
|
|
16 |
|
#include "preprocessing/passes/theory_rewrite_eq.h" |
17 |
|
|
18 |
|
#include "preprocessing/assertion_pipeline.h" |
19 |
|
#include "preprocessing/preprocessing_pass_context.h" |
20 |
|
#include "theory/theory_engine.h" |
21 |
|
|
22 |
|
using namespace cvc5::theory; |
23 |
|
|
24 |
|
namespace cvc5 { |
25 |
|
namespace preprocessing { |
26 |
|
namespace passes { |
27 |
|
|
28 |
6278 |
TheoryRewriteEq::TheoryRewriteEq(PreprocessingPassContext* preprocContext) |
29 |
6278 |
: PreprocessingPass(preprocContext, "theory-rewrite-eq"){}; |
30 |
|
|
31 |
8548 |
PreprocessingPassResult TheoryRewriteEq::applyInternal( |
32 |
|
AssertionPipeline* assertions) |
33 |
|
{ |
34 |
|
// apply ppRewrite to all equalities in assertions |
35 |
72412 |
for (std::size_t i = 0, size = assertions->size(); i < size; ++i) |
36 |
|
{ |
37 |
127728 |
Node assertion = (*assertions)[i]; |
38 |
127728 |
TrustNode trn = rewriteAssertion(assertion); |
39 |
63864 |
if (!trn.isNull()) |
40 |
|
{ |
41 |
|
// replace based on the trust node |
42 |
901 |
assertions->replaceTrusted(i, trn); |
43 |
|
} |
44 |
|
} |
45 |
8548 |
return PreprocessingPassResult::NO_CONFLICT; |
46 |
|
} |
47 |
|
|
48 |
63864 |
TrustNode TheoryRewriteEq::rewriteAssertion(TNode n) |
49 |
|
{ |
50 |
63864 |
NodeManager* nm = NodeManager::currentNM(); |
51 |
63864 |
TheoryEngine* te = d_preprocContext->getTheoryEngine(); |
52 |
127728 |
std::unordered_map<TNode, Node> visited; |
53 |
63864 |
std::unordered_map<TNode, Node>::iterator it; |
54 |
127728 |
std::vector<TNode> visit; |
55 |
127728 |
TNode cur; |
56 |
63864 |
visit.push_back(n); |
57 |
3257081 |
do |
58 |
|
{ |
59 |
3320945 |
cur = visit.back(); |
60 |
3320945 |
visit.pop_back(); |
61 |
3320945 |
it = visited.find(cur); |
62 |
|
|
63 |
3320945 |
if (it == visited.end()) |
64 |
|
{ |
65 |
1130963 |
if (cur.getNumChildren()==0) |
66 |
|
{ |
67 |
256448 |
visited[cur] = cur; |
68 |
|
} |
69 |
|
else |
70 |
|
{ |
71 |
874515 |
visited[cur] = Node::null(); |
72 |
874515 |
visit.push_back(cur); |
73 |
874515 |
visit.insert(visit.end(), cur.begin(), cur.end()); |
74 |
|
} |
75 |
|
} |
76 |
2189982 |
else if (it->second.isNull()) |
77 |
|
{ |
78 |
1749030 |
Node ret = cur; |
79 |
874515 |
bool childChanged = false; |
80 |
1749030 |
std::vector<Node> children; |
81 |
874515 |
if (cur.getMetaKind() == kind::metakind::PARAMETERIZED) |
82 |
|
{ |
83 |
88352 |
children.push_back(cur.getOperator()); |
84 |
|
} |
85 |
3257081 |
for (const Node& cn : cur) |
86 |
|
{ |
87 |
2382566 |
it = visited.find(cn); |
88 |
2382566 |
Assert(it != visited.end()); |
89 |
2382566 |
Assert(!it->second.isNull()); |
90 |
2382566 |
childChanged = childChanged || cn != it->second; |
91 |
2382566 |
children.push_back(it->second); |
92 |
|
} |
93 |
874515 |
if (childChanged) |
94 |
|
{ |
95 |
9352 |
ret = nm->mkNode(cur.getKind(), children); |
96 |
|
} |
97 |
874515 |
if (ret.getKind() == kind::EQUAL && !ret[0].getType().isBoolean()) |
98 |
|
{ |
99 |
|
// For example, (= x y) ---> (and (>= x y) (<= x y)) |
100 |
185428 |
TrustNode trn = te->ppRewriteEquality(ret); |
101 |
|
// can make proof producing by using proof generator from trn |
102 |
92714 |
ret = trn.isNull() ? Node(ret) : trn.getNode(); |
103 |
|
} |
104 |
874515 |
visited[cur] = ret; |
105 |
|
} |
106 |
3320945 |
} while (!visit.empty()); |
107 |
63864 |
Assert(visited.find(n) != visited.end()); |
108 |
63864 |
Assert(!visited.find(n)->second.isNull()); |
109 |
127728 |
Node ret = visited[n]; |
110 |
63864 |
if (ret == n) |
111 |
|
{ |
112 |
62963 |
return TrustNode::null(); |
113 |
|
} |
114 |
|
// can make proof producing by providing a term conversion generator here |
115 |
901 |
return TrustNode::mkTrustRewrite(n, ret, nullptr); |
116 |
|
} |
117 |
|
|
118 |
|
} // namespace passes |
119 |
|
} // namespace preprocessing |
120 |
22755 |
} // namespace cvc5 |