1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Andrew Reynolds, Mathias Preiner |
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 |
|
* extended rewriting class |
14 |
|
*/ |
15 |
|
|
16 |
|
#include "cvc5_private.h" |
17 |
|
|
18 |
|
#ifndef CVC5__THEORY__QUANTIFIERS__EXTENDED_REWRITE_H |
19 |
|
#define CVC5__THEORY__QUANTIFIERS__EXTENDED_REWRITE_H |
20 |
|
|
21 |
|
#include <unordered_map> |
22 |
|
|
23 |
|
#include "expr/node.h" |
24 |
|
|
25 |
|
namespace cvc5 { |
26 |
|
namespace theory { |
27 |
|
namespace quantifiers { |
28 |
|
|
29 |
|
/** Extended rewriter |
30 |
|
* |
31 |
|
* This class is used for all rewriting that is not necessarily |
32 |
|
* helpful for quantifier-free solving, but is helpful |
33 |
|
* in other use cases. An example of this is SyGuS, where rewriting |
34 |
|
* can be used for generalizing refinement lemmas, and hence |
35 |
|
* should be highly aggressive. |
36 |
|
* |
37 |
|
* This class extended the standard techniques for rewriting |
38 |
|
* with techniques, including but not limited to: |
39 |
|
* - Redundant child elimination, |
40 |
|
* - Sorting children of commutative operators, |
41 |
|
* - Boolean constraint propagation, |
42 |
|
* - Equality chain normalization, |
43 |
|
* - Negation normal form, |
44 |
|
* - Simple ITE pulling, |
45 |
|
* - ITE conditional variable elimination, |
46 |
|
* - ITE condition subsumption. |
47 |
|
*/ |
48 |
|
class ExtendedRewriter |
49 |
|
{ |
50 |
|
public: |
51 |
|
ExtendedRewriter(bool aggr = true); |
52 |
11438 |
~ExtendedRewriter() {} |
53 |
|
/** return the extended rewritten form of n */ |
54 |
|
Node extendedRewrite(Node n) const; |
55 |
|
|
56 |
|
private: |
57 |
|
/** |
58 |
|
* Whether this extended rewriter applies aggressive rewriting techniques, |
59 |
|
* which are more expensive. Examples of aggressive rewriting include: |
60 |
|
* - conditional rewriting, |
61 |
|
* - condition merging, |
62 |
|
* - sorting childing of commutative operators with more than 5 children. |
63 |
|
* |
64 |
|
* Aggressive rewriting is applied for SyGuS, whereas non-aggressive rewriting |
65 |
|
* may be applied as a preprocessing step. |
66 |
|
*/ |
67 |
|
bool d_aggr; |
68 |
|
/** true/false nodes */ |
69 |
|
Node d_true; |
70 |
|
Node d_false; |
71 |
|
/** cache that the extended rewritten form of n is ret */ |
72 |
|
void setCache(Node n, Node ret) const; |
73 |
|
/** get the cache for n */ |
74 |
|
Node getCache(Node n) const; |
75 |
|
/** add to children |
76 |
|
* |
77 |
|
* Adds nc to the vector of children, if dropDup is true, we do not add |
78 |
|
* nc if it already occurs in children. This method returns false in this |
79 |
|
* case, otherwise it returns true. |
80 |
|
*/ |
81 |
|
bool addToChildren(Node nc, std::vector<Node>& children, bool dropDup) const; |
82 |
|
|
83 |
|
//--------------------------------------generic utilities |
84 |
|
/** Rewrite ITE, for example: |
85 |
|
* |
86 |
|
* ite( ~C, s, t ) ---> ite( C, t, s ) |
87 |
|
* ite( A or B, s, t ) ---> ite( ~A and ~B, t, s ) |
88 |
|
* ite( x = c, x, t ) --> ite( x = c, c, t ) |
89 |
|
* t * { x -> c } = s => ite( x = c, s, t ) ---> t |
90 |
|
* |
91 |
|
* The parameter "full" indicates an effort level that this rewrite will |
92 |
|
* take. If full is false, then we do only perform rewrites that |
93 |
|
* strictly decrease the term size of n. |
94 |
|
*/ |
95 |
|
Node extendedRewriteIte(Kind itek, Node n, bool full = true) const; |
96 |
|
/** Rewrite AND/OR |
97 |
|
* |
98 |
|
* This implements BCP, factoring, and equality resolution for the Boolean |
99 |
|
* term n whose top symbolic is AND/OR. |
100 |
|
*/ |
101 |
|
Node extendedRewriteAndOr(Node n) const; |
102 |
|
/** Pull ITE, for example: |
103 |
|
* |
104 |
|
* D=C2 ---> false |
105 |
|
* implies |
106 |
|
* D=ite( C, C1, C2 ) ---> C ^ D=C1 |
107 |
|
* |
108 |
|
* f(t,t1) --> s and f(t,t2)---> s |
109 |
|
* implies |
110 |
|
* f(t,ite(C,t1,t2)) ---> s |
111 |
|
* |
112 |
|
* If this function returns a non-null node ret, then n ---> ret. |
113 |
|
*/ |
114 |
|
Node extendedRewritePullIte(Kind itek, Node n) const; |
115 |
|
/** Negation Normal Form (NNF), for example: |
116 |
|
* |
117 |
|
* ~( A & B ) ---> ( ~ A | ~B ) |
118 |
|
* ~( ite( A, B, C ) ---> ite( A, ~B, ~C ) |
119 |
|
* |
120 |
|
* If this function returns a non-null node ret, then n ---> ret. |
121 |
|
*/ |
122 |
|
Node extendedRewriteNnf(Node n) const; |
123 |
|
/** (type-independent) Boolean constraint propagation, for example: |
124 |
|
* |
125 |
|
* ~A & ( B V A ) ---> ~A & B |
126 |
|
* A & ( B = ( A V C ) ) ---> A & B |
127 |
|
* |
128 |
|
* This function takes as arguments the kinds that specify AND, OR, and NOT. |
129 |
|
* It additionally takes as argument a map bcp_kinds. If this map is |
130 |
|
* non-empty, then all terms that have a Kind that is *not* in this map should |
131 |
|
* be treated as immutable. This is for instance to prevent propagation |
132 |
|
* beneath illegal terms. As an example: |
133 |
|
* (bvand A (bvor A B)) is equivalent to (bvand A (bvor 1...1 B)), but |
134 |
|
* (bvand A (bvadd A B)) is not equivalent to (bvand A (bvadd 1..1 B)), |
135 |
|
* hence, when using this function to do BCP for bit-vectors, we have that |
136 |
|
* BITVECTOR_AND is a bcp_kind, but BITVECTOR_ADD is not. |
137 |
|
* |
138 |
|
* If this function returns a non-null node ret, then n ---> ret. |
139 |
|
*/ |
140 |
|
Node extendedRewriteBcp(Kind andk, |
141 |
|
Kind ork, |
142 |
|
Kind notk, |
143 |
|
std::map<Kind, bool>& bcp_kinds, |
144 |
|
Node n) const; |
145 |
|
/** (type-independent) factoring, for example: |
146 |
|
* |
147 |
|
* ( A V B ) ^ ( A V C ) ----> A V ( B ^ C ) |
148 |
|
* ( A ^ B ) V ( A ^ C ) ----> A ^ ( B V C ) |
149 |
|
* |
150 |
|
* This function takes as arguments the kinds that specify AND, OR, NOT. |
151 |
|
* We assume that the children of n do not contain duplicates. |
152 |
|
*/ |
153 |
|
Node extendedRewriteFactoring(Kind andk, Kind ork, Kind notk, Node n) const; |
154 |
|
/** (type-independent) equality resolution, for example: |
155 |
|
* |
156 |
|
* ( A V C ) & ( A = B ) ---> ( B V C ) & ( A = B ) |
157 |
|
* ( A V ~B ) & ( A = B ) ----> ( A = B ) |
158 |
|
* ( A V B ) & ( A xor B ) ----> ( A xor B ) |
159 |
|
* ( A & B ) V ( A xor B ) ----> B V ( A xor B ) |
160 |
|
* |
161 |
|
* This function takes as arguments the kinds that specify AND, OR, EQUAL, |
162 |
|
* and NOT. The equal kind eqk is interpreted as XOR if isXor is true. |
163 |
|
* It additionally takes as argument a map bcp_kinds, which |
164 |
|
* serves the same purpose as the above function. |
165 |
|
* If this function returns a non-null node ret, then n ---> ret. |
166 |
|
*/ |
167 |
|
Node extendedRewriteEqRes(Kind andk, |
168 |
|
Kind ork, |
169 |
|
Kind eqk, |
170 |
|
Kind notk, |
171 |
|
std::map<Kind, bool>& bcp_kinds, |
172 |
|
Node n, |
173 |
|
bool isXor = false) const; |
174 |
|
/** (type-independent) Equality chain rewriting, for example: |
175 |
|
* |
176 |
|
* A = ( A = B ) ---> B |
177 |
|
* ( A = D ) = ( C = B ) ---> A = ( B = ( C = D ) ) |
178 |
|
* A = ( A & B ) ---> ~A | B |
179 |
|
* |
180 |
|
* If this function returns a non-null node ret, then n ---> ret. |
181 |
|
* This function takes as arguments the kinds that specify EQUAL, AND, OR, |
182 |
|
* and NOT. If the flag isXor is true, the eqk is treated as XOR. |
183 |
|
*/ |
184 |
|
Node extendedRewriteEqChain(Kind eqk, |
185 |
|
Kind andk, |
186 |
|
Kind ork, |
187 |
|
Kind notk, |
188 |
|
Node n, |
189 |
|
bool isXor = false) const; |
190 |
|
/** extended rewrite aggressive |
191 |
|
* |
192 |
|
* All aggressive rewriting techniques (those that should be prioritized |
193 |
|
* at a lower level) go in this function. |
194 |
|
*/ |
195 |
|
Node extendedRewriteAggr(Node n) const; |
196 |
|
/** Decompose right associative chain |
197 |
|
* |
198 |
|
* For term f( ... f( f( base, tn ), t{n-1} ) ... t1 ), returns term base, and |
199 |
|
* appends t1...tn to children. |
200 |
|
*/ |
201 |
|
Node decomposeRightAssocChain(Kind k, |
202 |
|
Node n, |
203 |
|
std::vector<Node>& children) const; |
204 |
|
/** Make right associative chain |
205 |
|
* |
206 |
|
* Sorts children to obtain list { tn...t1 }, and returns the term |
207 |
|
* f( ... f( f( base, tn ), t{n-1} ) ... t1 ). |
208 |
|
*/ |
209 |
|
Node mkRightAssocChain(Kind k, Node base, std::vector<Node>& children) const; |
210 |
|
/** Partial substitute |
211 |
|
* |
212 |
|
* Applies the substitution specified by assign to n, recursing only beneath |
213 |
|
* terms whose Kind appears in rkinds (when rkinds is empty), and additionally |
214 |
|
* never recursing beneath WITNESS. |
215 |
|
*/ |
216 |
|
Node partialSubstitute(Node n, |
217 |
|
const std::map<Node, Node>& assign, |
218 |
|
const std::map<Kind, bool>& rkinds) const; |
219 |
|
/** same as above, with vectors */ |
220 |
|
Node partialSubstitute(Node n, |
221 |
|
const std::vector<Node>& vars, |
222 |
|
const std::vector<Node>& subs, |
223 |
|
const std::map<Kind, bool>& rkinds) const; |
224 |
|
/** solve equality |
225 |
|
* |
226 |
|
* If this function returns a non-null node n', then n' is equivalent to n |
227 |
|
* and is of the form that can be used by inferSubstitution below. |
228 |
|
*/ |
229 |
|
Node solveEquality(Node n) const; |
230 |
|
/** infer substitution |
231 |
|
* |
232 |
|
* If n is an equality of the form x = t, where t is either: |
233 |
|
* (1) a constant, or |
234 |
|
* (2) a variable y such that x < y based on an ordering, |
235 |
|
* then this method adds x to vars and y to subs and return true, otherwise |
236 |
|
* it returns false. |
237 |
|
* If usePred is true, we may additionally add n -> true, or n[0] -> false |
238 |
|
* is n is a negation. |
239 |
|
*/ |
240 |
|
bool inferSubstitution(Node n, |
241 |
|
std::vector<Node>& vars, |
242 |
|
std::vector<Node>& subs, |
243 |
|
bool usePred = false) const; |
244 |
|
/** extended rewrite |
245 |
|
* |
246 |
|
* Prints debug information, indicating the rewrite n ---> ret was found. |
247 |
|
*/ |
248 |
|
void debugExtendedRewrite(Node n, Node ret, const char* c) const; |
249 |
|
//--------------------------------------end generic utilities |
250 |
|
|
251 |
|
//--------------------------------------theory-specific top-level calls |
252 |
|
/** extended rewrite strings |
253 |
|
* |
254 |
|
* If this method returns a non-null node ret', then ret is equivalent to |
255 |
|
* ret'. |
256 |
|
*/ |
257 |
|
Node extendedRewriteStrings(Node ret) const; |
258 |
|
//--------------------------------------end theory-specific top-level calls |
259 |
|
}; |
260 |
|
|
261 |
|
} // namespace quantifiers |
262 |
|
} // namespace theory |
263 |
|
} // namespace cvc5 |
264 |
|
|
265 |
|
#endif /* CVC5__THEORY__QUANTIFIERS__EXTENDED_REWRITE_H */ |