1 /********************* */
4 ** Top contributors (to current version):
5 ** Andres Noetzli, Andrew Reynolds, Dejan Jovanovic
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2020 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.\endverbatim
12 ** \brief The Rewriter class
14 ** The Rewriter class.
17 #include "cvc4_private.h"
21 #include "expr/node.h"
22 #include "expr/term_conversion_proof_generator.h"
23 #include "theory/theory_rewriter.h"
24 #include "theory/trust_node.h"
25 #include "util/unsafe_interrupt_exception.h"
31 class BuiltinProofRuleChecker
;
34 class RewriterInitializer
;
37 * The rewrite environment holds everything that the individual rewrites have
40 class RewriteEnvironment
45 * The identity rewrite just returns the original node.
47 * @param re The rewrite environment
48 * @param n The node to rewrite
49 * @return The original node
51 RewriteResponse
identityRewrite(RewriteEnvironment
* re
, TNode n
);
54 * The main rewriter class.
57 friend builtin::BuiltinProofRuleChecker
;
63 * Rewrites the node using theoryOf() to determine which rewriter to
66 static Node
rewrite(TNode node
);
69 * Rewrites the equality node using theoryOf() to determine which rewriter to
70 * use on the node corresponding to an equality s = t.
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.
78 static Node
rewriteEqualityExt(TNode node
);
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.
85 * @param node The node to rewrite.
86 * @param elimTheoryRewrite Whether we also want fine-grained proofs for
87 * THEORY_REWRITE steps.
88 * @param isExtEq Whether node is an equality which we are applying
89 * rewriteEqualityExt on.
90 * @return The trust node of kind TrustNodeKind::REWRITE that contains the
91 * rewritten form of node.
93 TrustNode
rewriteWithProof(TNode node
,
94 bool elimTheoryRewrite
= false,
95 bool isExtEq
= false);
97 /** Set proof node manager */
98 void setProofNodeManager(ProofNodeManager
* pnm
);
101 * Garbage collects the rewrite caches.
103 static void clearCaches();
106 * Registers a theory rewriter with this rewriter. The rewriter does not own
107 * the theory rewriters.
109 * @param tid The theory that the theory rewriter should be associated with.
110 * @param trew The theory rewriter to register.
112 static void registerTheoryRewriter(theory::TheoryId tid
,
113 TheoryRewriter
* trew
);
116 * Register a prerewrite for a given kind.
118 * @param k The kind to register a rewrite for.
119 * @param fn The function that performs the rewrite.
121 void registerPreRewrite(
122 Kind k
, std::function
<RewriteResponse(RewriteEnvironment
*, TNode
)> fn
);
125 * Register a postrewrite for a given kind.
127 * @param k The kind to register a rewrite for.
128 * @param fn The function that performs the rewrite.
130 void registerPostRewrite(
131 Kind k
, std::function
<RewriteResponse(RewriteEnvironment
*, TNode
)> fn
);
134 * Register a prerewrite for equalities belonging to a given theory.
136 * @param tid The theory to register a rewrite for.
137 * @param fn The function that performs the rewrite.
139 void registerPreRewriteEqual(
140 theory::TheoryId tid
,
141 std::function
<RewriteResponse(RewriteEnvironment
*, TNode
)> fn
);
144 * Register a postrewrite for equalities belonging to a given theory.
146 * @param tid The theory to register a rewrite for.
147 * @param fn The function that performs the rewrite.
149 void registerPostRewriteEqual(
150 theory::TheoryId tid
,
151 std::function
<RewriteResponse(RewriteEnvironment
*, TNode
)> fn
);
155 * Get the rewriter associated with the SmtEngine in scope.
157 * TODO(#3468): Get rid of this function (it relies on there being an
158 * singleton with the current SmtEngine in scope)
160 static Rewriter
* getInstance();
162 /** Returns the appropriate cache for a node */
163 Node
getPreRewriteCache(theory::TheoryId theoryId
, TNode node
);
165 /** Returns the appropriate cache for a node */
166 Node
getPostRewriteCache(theory::TheoryId theoryId
, TNode node
);
168 /** Sets the appropriate cache for a node */
169 void setPreRewriteCache(theory::TheoryId theoryId
, TNode node
, TNode cache
);
171 /** Sets the appropriate cache for a node */
172 void setPostRewriteCache(theory::TheoryId theoryId
, TNode node
, TNode cache
);
175 * Rewrites the node using the given theory rewriter.
177 Node
rewriteTo(theory::TheoryId theoryId
,
179 TConvProofGenerator
* tcpg
= nullptr);
181 /** Calls the pre-rewriter for the given theory */
182 RewriteResponse
preRewrite(theory::TheoryId theoryId
,
184 TConvProofGenerator
* tcpg
= nullptr);
186 /** Calls the post-rewriter for the given theory */
187 RewriteResponse
postRewrite(theory::TheoryId theoryId
,
189 TConvProofGenerator
* tcpg
= nullptr);
190 /** processes a trust rewrite response */
191 RewriteResponse
processTrustRewriteResponse(
192 theory::TheoryId theoryId
,
193 const TrustRewriteResponse
& tresponse
,
195 TConvProofGenerator
* tcpg
);
198 * Calls the equality-rewriter for the given theory.
200 Node
callRewriteEquality(theory::TheoryId theoryId
, TNode equality
);
202 void clearCachesInternal();
204 /** Theory rewriters used by this rewriter instance */
205 TheoryRewriter
* d_theoryRewriters
[theory::THEORY_LAST
];
207 unsigned long d_iterationCount
= 0;
209 /** Rewriter table for prewrites. Maps kinds to rewriter function. */
210 std::function
<RewriteResponse(RewriteEnvironment
*, TNode
)>
211 d_preRewriters
[kind::LAST_KIND
];
212 /** Rewriter table for postrewrites. Maps kinds to rewriter function. */
213 std::function
<RewriteResponse(RewriteEnvironment
*, TNode
)>
214 d_postRewriters
[kind::LAST_KIND
];
216 * Rewriter table for prerewrites of equalities. Maps theory to rewriter
219 std::function
<RewriteResponse(RewriteEnvironment
*, TNode
)>
220 d_preRewritersEqual
[theory::THEORY_LAST
];
222 * Rewriter table for postrewrites of equalities. Maps theory to rewriter
225 std::function
<RewriteResponse(RewriteEnvironment
*, TNode
)>
226 d_postRewritersEqual
[theory::THEORY_LAST
];
228 RewriteEnvironment d_re
;
230 /** The proof generator */
231 std::unique_ptr
<TConvProofGenerator
> d_tpg
;
232 #ifdef CVC4_ASSERTIONS
233 std::unique_ptr
<std::unordered_set
<Node
, NodeHashFunction
>> d_rewriteStack
=
235 #endif /* CVC4_ASSERTIONS */
236 };/* class Rewriter */
238 }/* CVC4::theory namespace */
239 }/* CVC4 namespace */