GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/extended_rewrite.h Lines: 1 1 100.0 %
Date: 2021-09-07 Branches: 0 0 0.0 %

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