GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/extended_rewrite.h Lines: 1 1 100.0 %
Date: 2021-05-22 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
11108
  ~ExtendedRewriter() {}
53
  /** return the extended rewritten form of n */
54
  Node extendedRewrite(Node n);
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);
73
  /** get the cache for n */
74
  Node getCache(Node n);
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);
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);
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);
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);
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);
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(
141
      Kind andk, Kind ork, Kind notk, std::map<Kind, bool>& bcp_kinds, Node n);
142
  /** (type-independent) factoring, for example:
143
   *
144
   *   ( A V B ) ^ ( A V C ) ----> A V ( B ^ C )
145
   *   ( A ^ B ) V ( A ^ C ) ----> A ^ ( B V C )
146
   *
147
   * This function takes as arguments the kinds that specify AND, OR, NOT.
148
   * We assume that the children of n do not contain duplicates.
149
   */
150
  Node extendedRewriteFactoring(Kind andk, Kind ork, Kind notk, Node n);
151
  /** (type-independent) equality resolution, for example:
152
   *
153
   *   ( A V C ) & ( A = B ) ---> ( B V C ) & ( A = B )
154
   *   ( A V ~B ) & ( A = B ) ----> ( A = B )
155
   *   ( A V B ) & ( A xor B ) ----> ( A xor B )
156
   *   ( A & B ) V ( A xor B ) ----> B V ( A xor B )
157
   *
158
   * This function takes as arguments the kinds that specify AND, OR, EQUAL,
159
   * and NOT. The equal kind eqk is interpreted as XOR if isXor is true.
160
   * It additionally takes as argument a map bcp_kinds, which
161
   * serves the same purpose as the above function.
162
   * If this function returns a non-null node ret, then n ---> ret.
163
   */
164
  Node extendedRewriteEqRes(Kind andk,
165
                            Kind ork,
166
                            Kind eqk,
167
                            Kind notk,
168
                            std::map<Kind, bool>& bcp_kinds,
169
                            Node n,
170
                            bool isXor = false);
171
  /** (type-independent) Equality chain rewriting, for example:
172
   *
173
   *   A = ( A = B ) ---> B
174
   *   ( A = D ) = ( C = B ) ---> A = ( B = ( C = D ) )
175
   *   A = ( A & B ) ---> ~A | B
176
   *
177
   * If this function returns a non-null node ret, then n ---> ret.
178
   * This function takes as arguments the kinds that specify EQUAL, AND, OR,
179
   * and NOT. If the flag isXor is true, the eqk is treated as XOR.
180
   */
181
  Node extendedRewriteEqChain(
182
      Kind eqk, Kind andk, Kind ork, Kind notk, Node n, bool isXor = false);
183
  /** extended rewrite aggressive
184
   *
185
   * All aggressive rewriting techniques (those that should be prioritized
186
   * at a lower level) go in this function.
187
   */
188
  Node extendedRewriteAggr(Node n);
189
  /** Decompose right associative chain
190
   *
191
   * For term f( ... f( f( base, tn ), t{n-1} ) ... t1 ), returns term base, and
192
   * appends t1...tn to children.
193
   */
194
  Node decomposeRightAssocChain(Kind k, Node n, std::vector<Node>& children);
195
  /** Make right associative chain
196
   *
197
   * Sorts children to obtain list { tn...t1 }, and returns the term
198
   * f( ... f( f( base, tn ), t{n-1} ) ... t1 ).
199
   */
200
  Node mkRightAssocChain(Kind k, Node base, std::vector<Node>& children);
201
  /** Partial substitute
202
   *
203
   * Applies the substitution specified by assign to n, recursing only beneath
204
   * terms whose Kind appears in rkinds (when rkinds is empty), and additionally
205
   * never recursing beneath WITNESS.
206
   */
207
  Node partialSubstitute(Node n,
208
                         const std::map<Node, Node>& assign,
209
                         const std::map<Kind, bool>& rkinds);
210
  /** same as above, with vectors */
211
  Node partialSubstitute(Node n,
212
                         const std::vector<Node>& vars,
213
                         const std::vector<Node>& subs,
214
                         const std::map<Kind, bool>& rkinds);
215
  /** solve equality
216
   *
217
   * If this function returns a non-null node n', then n' is equivalent to n
218
   * and is of the form that can be used by inferSubstitution below.
219
   */
220
  Node solveEquality(Node n);
221
  /** infer substitution
222
   *
223
   * If n is an equality of the form x = t, where t is either:
224
   * (1) a constant, or
225
   * (2) a variable y such that x < y based on an ordering,
226
   * then this method adds x to vars and y to subs and return true, otherwise
227
   * it returns false.
228
   * If usePred is true, we may additionally add n -> true, or n[0] -> false
229
   * is n is a negation.
230
   */
231
  bool inferSubstitution(Node n,
232
                         std::vector<Node>& vars,
233
                         std::vector<Node>& subs,
234
                         bool usePred = false);
235
  /** extended rewrite
236
   *
237
   * Prints debug information, indicating the rewrite n ---> ret was found.
238
   */
239
  inline void debugExtendedRewrite(Node n, Node ret, const char* c) const;
240
  //--------------------------------------end generic utilities
241
242
  //--------------------------------------theory-specific top-level calls
243
  /** extended rewrite strings
244
   *
245
   * If this method returns a non-null node ret', then ret is equivalent to
246
   * ret'.
247
   */
248
  Node extendedRewriteStrings(Node ret);
249
  //--------------------------------------end theory-specific top-level calls
250
};
251
252
}  // namespace quantifiers
253
}  // namespace theory
254
}  // namespace cvc5
255
256
#endif /* CVC5__THEORY__QUANTIFIERS__EXTENDED_REWRITE_H */