1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Andres Noetzli, Andrew Reynolds, Dejan Jovanovic |
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 Rewriter class. |
14 |
|
*/ |
15 |
|
|
16 |
|
#include "cvc5_private.h" |
17 |
|
|
18 |
|
#pragma once |
19 |
|
|
20 |
|
#include "expr/node.h" |
21 |
|
#include "theory/theory_rewriter.h" |
22 |
|
|
23 |
|
namespace cvc5 { |
24 |
|
|
25 |
|
class TConvProofGenerator; |
26 |
|
class ProofNodeManager; |
27 |
|
|
28 |
|
namespace theory { |
29 |
|
|
30 |
|
class TrustNode; |
31 |
|
|
32 |
|
namespace builtin { |
33 |
|
class BuiltinProofRuleChecker; |
34 |
|
} |
35 |
|
|
36 |
|
/** |
37 |
|
* The rewrite environment holds everything that the individual rewrites have |
38 |
|
* access to. |
39 |
|
*/ |
40 |
|
class RewriteEnvironment |
41 |
|
{ |
42 |
|
}; |
43 |
|
|
44 |
|
/** |
45 |
|
* The identity rewrite just returns the original node. |
46 |
|
* |
47 |
|
* @param re The rewrite environment |
48 |
|
* @param n The node to rewrite |
49 |
|
* @return The original node |
50 |
|
*/ |
51 |
|
RewriteResponse identityRewrite(RewriteEnvironment* re, TNode n); |
52 |
|
|
53 |
|
/** |
54 |
|
* The main rewriter class. |
55 |
|
*/ |
56 |
9587 |
class Rewriter { |
57 |
|
friend builtin::BuiltinProofRuleChecker; |
58 |
|
|
59 |
|
public: |
60 |
|
Rewriter(); |
61 |
|
|
62 |
|
/** |
63 |
|
* Rewrites the node using theoryOf() to determine which rewriter to |
64 |
|
* use on the node. |
65 |
|
*/ |
66 |
|
static Node rewrite(TNode node); |
67 |
|
|
68 |
|
/** |
69 |
|
* Rewrites the equality node using theoryOf() to determine which rewriter to |
70 |
|
* use on the node corresponding to an equality s = t. |
71 |
|
* |
72 |
|
* Specifically, this method performs rewrites whose conclusion is not |
73 |
|
* necessarily one of { s = t, t = s, true, false }, which is an invariant |
74 |
|
* guaranted by the above method. This invariant is motivated by theory |
75 |
|
* combination, which needs to guarantee that equalities between terms |
76 |
|
* can be communicated for all pairs of terms. |
77 |
|
*/ |
78 |
|
static Node rewriteEqualityExt(TNode node); |
79 |
|
|
80 |
|
/** |
81 |
|
* Rewrite with proof production, which is managed by the term conversion |
82 |
|
* proof generator managed by this class (d_tpg). This method requires a call |
83 |
|
* to setProofNodeManager prior to this call. |
84 |
|
* |
85 |
|
* @param node The node to rewrite. |
86 |
|
* @param isExtEq Whether node is an equality which we are applying |
87 |
|
* rewriteEqualityExt on. |
88 |
|
* @return The trust node of kind TrustNodeKind::REWRITE that contains the |
89 |
|
* rewritten form of node. |
90 |
|
*/ |
91 |
|
TrustNode rewriteWithProof(TNode node, |
92 |
|
bool isExtEq = false); |
93 |
|
|
94 |
|
/** Set proof node manager */ |
95 |
|
void setProofNodeManager(ProofNodeManager* pnm); |
96 |
|
|
97 |
|
/** |
98 |
|
* Garbage collects the rewrite caches. |
99 |
|
*/ |
100 |
|
static void clearCaches(); |
101 |
|
|
102 |
|
/** |
103 |
|
* Registers a theory rewriter with this rewriter. The rewriter does not own |
104 |
|
* the theory rewriters. |
105 |
|
* |
106 |
|
* @param tid The theory that the theory rewriter should be associated with. |
107 |
|
* @param trew The theory rewriter to register. |
108 |
|
*/ |
109 |
|
static void registerTheoryRewriter(theory::TheoryId tid, |
110 |
|
TheoryRewriter* trew); |
111 |
|
|
112 |
|
/** |
113 |
|
* Register a prerewrite for a given kind. |
114 |
|
* |
115 |
|
* @param k The kind to register a rewrite for. |
116 |
|
* @param fn The function that performs the rewrite. |
117 |
|
*/ |
118 |
|
void registerPreRewrite( |
119 |
|
Kind k, std::function<RewriteResponse(RewriteEnvironment*, TNode)> fn); |
120 |
|
|
121 |
|
/** |
122 |
|
* Register a postrewrite for a given kind. |
123 |
|
* |
124 |
|
* @param k The kind to register a rewrite for. |
125 |
|
* @param fn The function that performs the rewrite. |
126 |
|
*/ |
127 |
|
void registerPostRewrite( |
128 |
|
Kind k, std::function<RewriteResponse(RewriteEnvironment*, TNode)> fn); |
129 |
|
|
130 |
|
/** |
131 |
|
* Register a prerewrite for equalities belonging to a given theory. |
132 |
|
* |
133 |
|
* @param tid The theory to register a rewrite for. |
134 |
|
* @param fn The function that performs the rewrite. |
135 |
|
*/ |
136 |
|
void registerPreRewriteEqual( |
137 |
|
theory::TheoryId tid, |
138 |
|
std::function<RewriteResponse(RewriteEnvironment*, TNode)> fn); |
139 |
|
|
140 |
|
/** |
141 |
|
* Register a postrewrite for equalities belonging to a given theory. |
142 |
|
* |
143 |
|
* @param tid The theory to register a rewrite for. |
144 |
|
* @param fn The function that performs the rewrite. |
145 |
|
*/ |
146 |
|
void registerPostRewriteEqual( |
147 |
|
theory::TheoryId tid, |
148 |
|
std::function<RewriteResponse(RewriteEnvironment*, TNode)> fn); |
149 |
|
|
150 |
|
/** Get the theory rewriter for the given id */ |
151 |
|
TheoryRewriter* getTheoryRewriter(theory::TheoryId theoryId); |
152 |
|
|
153 |
|
private: |
154 |
|
/** |
155 |
|
* Get the rewriter associated with the SmtEngine in scope. |
156 |
|
* |
157 |
|
* TODO(#3468): Get rid of this function (it relies on there being an |
158 |
|
* singleton with the current SmtEngine in scope) |
159 |
|
*/ |
160 |
|
static Rewriter* getInstance(); |
161 |
|
|
162 |
|
/** Returns the appropriate cache for a node */ |
163 |
|
Node getPreRewriteCache(theory::TheoryId theoryId, TNode node); |
164 |
|
|
165 |
|
/** Returns the appropriate cache for a node */ |
166 |
|
Node getPostRewriteCache(theory::TheoryId theoryId, TNode node); |
167 |
|
|
168 |
|
/** Sets the appropriate cache for a node */ |
169 |
|
void setPreRewriteCache(theory::TheoryId theoryId, TNode node, TNode cache); |
170 |
|
|
171 |
|
/** Sets the appropriate cache for a node */ |
172 |
|
void setPostRewriteCache(theory::TheoryId theoryId, TNode node, TNode cache); |
173 |
|
|
174 |
|
/** |
175 |
|
* Rewrites the node using the given theory rewriter. |
176 |
|
*/ |
177 |
|
Node rewriteTo(theory::TheoryId theoryId, |
178 |
|
Node node, |
179 |
|
TConvProofGenerator* tcpg = nullptr); |
180 |
|
|
181 |
|
/** Calls the pre-rewriter for the given theory */ |
182 |
|
RewriteResponse preRewrite(theory::TheoryId theoryId, |
183 |
|
TNode n, |
184 |
|
TConvProofGenerator* tcpg = nullptr); |
185 |
|
|
186 |
|
/** Calls the post-rewriter for the given theory */ |
187 |
|
RewriteResponse postRewrite(theory::TheoryId theoryId, |
188 |
|
TNode n, |
189 |
|
TConvProofGenerator* tcpg = nullptr); |
190 |
|
/** processes a trust rewrite response */ |
191 |
|
RewriteResponse processTrustRewriteResponse( |
192 |
|
theory::TheoryId theoryId, |
193 |
|
const TrustRewriteResponse& tresponse, |
194 |
|
bool isPre, |
195 |
|
TConvProofGenerator* tcpg); |
196 |
|
|
197 |
|
/** |
198 |
|
* Calls the equality-rewriter for the given theory. |
199 |
|
*/ |
200 |
|
Node callRewriteEquality(theory::TheoryId theoryId, TNode equality); |
201 |
|
|
202 |
|
void clearCachesInternal(); |
203 |
|
|
204 |
|
/** Theory rewriters used by this rewriter instance */ |
205 |
|
TheoryRewriter* d_theoryRewriters[theory::THEORY_LAST]; |
206 |
|
|
207 |
|
/** Rewriter table for prewrites. Maps kinds to rewriter function. */ |
208 |
|
std::function<RewriteResponse(RewriteEnvironment*, TNode)> |
209 |
|
d_preRewriters[kind::LAST_KIND]; |
210 |
|
/** Rewriter table for postrewrites. Maps kinds to rewriter function. */ |
211 |
|
std::function<RewriteResponse(RewriteEnvironment*, TNode)> |
212 |
|
d_postRewriters[kind::LAST_KIND]; |
213 |
|
/** |
214 |
|
* Rewriter table for prerewrites of equalities. Maps theory to rewriter |
215 |
|
* function. |
216 |
|
*/ |
217 |
|
std::function<RewriteResponse(RewriteEnvironment*, TNode)> |
218 |
|
d_preRewritersEqual[theory::THEORY_LAST]; |
219 |
|
/** |
220 |
|
* Rewriter table for postrewrites of equalities. Maps theory to rewriter |
221 |
|
* function. |
222 |
|
*/ |
223 |
|
std::function<RewriteResponse(RewriteEnvironment*, TNode)> |
224 |
|
d_postRewritersEqual[theory::THEORY_LAST]; |
225 |
|
|
226 |
|
RewriteEnvironment d_re; |
227 |
|
|
228 |
|
/** The proof generator */ |
229 |
|
std::unique_ptr<TConvProofGenerator> d_tpg; |
230 |
|
#ifdef CVC5_ASSERTIONS |
231 |
|
std::unique_ptr<std::unordered_set<Node>> d_rewriteStack = nullptr; |
232 |
|
#endif /* CVC5_ASSERTIONS */ |
233 |
|
};/* class Rewriter */ |
234 |
|
|
235 |
|
} // namespace theory |
236 |
|
} // namespace cvc5 |