GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/ext_theory.h Lines: 7 7 100.0 %
Date: 2021-08-01 Branches: 1 2 50.0 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Tim King, 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 theory interface.
14
 *
15
 * This implements a generic module, used by theory solvers, for performing
16
 * "context-dependent simplification", as described in Reynolds et al
17
 * "Designing Theory Solvers with Extensions", FroCoS 2017.
18
 *
19
 * At a high level, this technique implements a generic inference scheme based
20
 * on the combination of SAT-context-dependent equality reasoning and
21
 * SAT-context-indepedent rewriting.
22
 *
23
 * As a simple example, say
24
 * (1) TheoryStrings tells us that the following facts hold in the SAT context:
25
 *     x = "A" ^ str.contains( str.++( x, z ), "B" ) = true.
26
 * (2) The Rewriter tells us that:
27
 *     str.contains( str.++( "A", z ), "B" ) ----> str.contains( z, "B" ).
28
 * From this, this class may infer that the following lemma is T-valid:
29
 *   x = "A" ^ str.contains( str.++( x, z ), "B" ) => str.contains( z, "B" )
30
 */
31
32
#include "cvc5_private.h"
33
34
#ifndef CVC5__THEORY__EXT_THEORY_H
35
#define CVC5__THEORY__EXT_THEORY_H
36
37
#include <map>
38
39
#include "context/cdhashmap.h"
40
#include "context/cdhashset.h"
41
#include "context/cdo.h"
42
#include "context/context.h"
43
#include "expr/node.h"
44
45
namespace cvc5 {
46
namespace theory {
47
48
class OutputChannel;
49
50
/** Reasons for why a term was marked reduced */
51
enum class ExtReducedId
52
{
53
  UNKNOWN,
54
  // the extended function substitutes+rewrites to a constant
55
  SR_CONST,
56
  // the extended function was reduced by the callback
57
  REDUCTION,
58
  // the extended function substitutes+rewrites to zero
59
  ARITH_SR_ZERO,
60
  // the extended function substitutes+rewrites to a linear (non-zero) term
61
  ARITH_SR_LINEAR,
62
  // an extended string function substitutes+rewrites to a constant
63
  STRINGS_SR_CONST,
64
  // a negative str.contains was reduced to a disequality
65
  STRINGS_NEG_CTN_DEQ,
66
  // a positive str.contains was reduced to an equality
67
  STRINGS_POS_CTN,
68
  // a str.contains was subsumed by another based on a decomposition
69
  STRINGS_CTN_DECOMPOSE,
70
  // reduced via an intersection inference
71
  STRINGS_REGEXP_INTER,
72
  // subsumed due to intersection reasoning
73
  STRINGS_REGEXP_INTER_SUBSUME,
74
  // subsumed due to RE inclusion reasoning for positive memberships
75
  STRINGS_REGEXP_INCLUDE,
76
  // subsumed due to RE inclusion reasoning for negative memberships
77
  STRINGS_REGEXP_INCLUDE_NEG,
78
};
79
/**
80
 * Converts an ext reduced identifier to a string.
81
 *
82
 * @param id The ext reduced identifier
83
 * @return The name of the ext reduced identifier
84
 */
85
const char* toString(ExtReducedId id);
86
87
/**
88
 * Writes an ext reduced identifier to a stream.
89
 *
90
 * @param out The stream to write to
91
 * @param id The ext reduced identifier to write to the stream
92
 * @return The stream
93
 */
94
std::ostream& operator<<(std::ostream& out, ExtReducedId id);
95
96
/**
97
 * A callback class for ExtTheory below. This class is responsible for
98
 * determining how to apply context-dependent simplification.
99
 */
100
14972
class ExtTheoryCallback
101
{
102
 public:
103
14972
  virtual ~ExtTheoryCallback() {}
104
  /*
105
   * Get current substitution at an effort
106
   * @param effort The effort identifier
107
   * @param vars The variables to get a substitution for
108
   * @param subs The terms to substitute for variables, in order. This vector
109
   * should be updated to one the same size as vars.
110
   * @param exp The map containing the explanation for each variable. Together
111
   * with subs, we have that:
112
   *   ( exp[vars[i]] => vars[i] = subs[i] ) holds for all i
113
   * @return true if any (non-identity) substitution was added to subs.
114
   */
115
  virtual bool getCurrentSubstitution(int effort,
116
                                      const std::vector<Node>& vars,
117
                                      std::vector<Node>& subs,
118
                                      std::map<Node, std::vector<Node> >& exp);
119
120
  /*
121
   * Is extended function n reduced? This returns true if n is reduced to a
122
   * form that requires no further interaction from the theory.
123
   *
124
   * @param effort The effort identifier
125
   * @param n The term to reduce
126
   * @param on The original form of n, before substitution
127
   * @param exp The explanation of on = n
128
   * @param id If this method returns true, then id is updated to the reason
129
   * why n was reduced.
130
   * @return true if n is reduced.
131
   */
132
  virtual bool isExtfReduced(
133
      int effort, Node n, Node on, std::vector<Node>& exp, ExtReducedId& id);
134
135
  /**
136
   * Get reduction for node n.
137
   * If return value is true, then n is reduced.
138
   * If satDep is updated to false, then n is reduced independent of the
139
   * SAT context (e.g. by a lemma that persists at this
140
   * user-context level).
141
   * If nr is non-null, then ( n = nr ) should be added as a lemma by caller,
142
   * and return value of this method should be true.
143
   */
144
  virtual bool getReduction(int effort, Node n, Node& nr, bool& satDep);
145
};
146
147
/** Extended theory class
148
 *
149
 * This class is used for constructing generic extensions to theory solvers.
150
 * In particular, it provides methods for "context-dependent simplification"
151
 * and "model-based refinement". For details, see Reynolds et al "Design
152
 * Theory Solvers with Extensions", FroCoS 2017.
153
 *
154
 * It maintains:
155
 * (1) A set of "extended function" kinds (d_extf_kind),
156
 * (2) A database of active/inactive extended function terms (d_ext_func_terms)
157
 * that have been registered to the class.
158
 *
159
 * This class has methods doInferences/doReductions, which send out lemmas
160
 * based on the current set of active extended function terms. The former
161
 * is based on context-dependent simplification, where this class asks the
162
 * underlying theory for a "derivable substitution", whereby extended functions
163
 * may be reducable.
164
 */
165
class ExtTheory
166
{
167
  using NodeBoolMap = context::CDHashMap<Node, bool>;
168
  using NodeExtReducedIdMap = context::CDHashMap<Node, ExtReducedId>;
169
  using NodeSet = context::CDHashSet<Node>;
170
171
 public:
172
  /** constructor
173
   *
174
   * If cacheEnabled is false, we do not cache results of getSubstitutedTerm.
175
   */
176
  ExtTheory(ExtTheoryCallback& p,
177
            context::Context* c,
178
            context::UserContext* u,
179
            OutputChannel& out);
180
14972
  virtual ~ExtTheory() {}
181
  /** Tells this class to treat terms with Kind k as extended functions */
182
217726
  void addFunctionKind(Kind k) { d_extf_kind[k] = true; }
183
  /** Is kind k treated as an extended function by this class? */
184
64
  bool hasFunctionKind(Kind k) const
185
  {
186
64
    return d_extf_kind.find(k) != d_extf_kind.end();
187
  }
188
  /** register term
189
   *
190
   * Registers term n with this class. Adds n to the set of extended function
191
   * terms known by this class (d_ext_func_terms) if n is an extended function,
192
   * that is, if addFunctionKind( n.getKind() ) was called.
193
   */
194
  void registerTerm(Node n);
195
  /** set n as reduced/inactive
196
   *
197
   * If satDep = false, then n remains inactive in the duration of this
198
   * user-context level
199
   */
200
  void markReduced(Node n, ExtReducedId rid, bool satDep = true);
201
  /** getSubstitutedTerms
202
   *
203
   *  input : effort, terms
204
   *  output : sterms, exp, where ( exp[i] => terms[i] = sterms[i] ) for all i.
205
   *
206
   * For each i, sterms[i] = term[i] * sigma for some "derivable substitution"
207
   * sigma. We obtain derivable substitutions and their explanations via calls
208
   * to the underlying theory's Theory::getCurrentSubstitution method.
209
   */
210
  void getSubstitutedTerms(int effort,
211
                           const std::vector<Node>& terms,
212
                           std::vector<Node>& sterms,
213
                           std::vector<std::vector<Node> >& exp);
214
  /**
215
   * Same as above, but for a single term. We return the substituted form of
216
   * term and add its explanation to exp.
217
   */
218
  Node getSubstitutedTerm(int effort, Node term, std::vector<Node>& exp);
219
  /** doInferences
220
   *
221
   * This function performs "context-dependent simplification". The method takes
222
   * as input:
223
   *  effort: an identifier used to determine which terms we reduce and the
224
   *          form of the derivable substitution we will use,
225
   *  terms: the set of terms to simplify,
226
   *  batch: if this flag is true, we send lemmas for all terms; if it is false
227
   *         we send a lemma for the first applicable term.
228
   *
229
   * Sends rewriting lemmas of the form ( exp => t = c ) where t is in terms
230
   * and c is a constant, c = rewrite( t*sigma ) where exp |= sigma. These
231
   * lemmas are determined by asking the underlying theory for a derivable
232
   * substitution (through getCurrentSubstitution with getSubstitutedTerm)
233
   * above, applying this substitution to terms in terms, rewriting
234
   * the result and checking with the theory whether the resulting term is
235
   * in reduced form (isExtfReduced).
236
   *
237
   * This method adds the extended terms that are still active to nred, and
238
   * returns true if and only if a lemma of the above form was sent.
239
   */
240
  bool doInferences(int effort,
241
                    const std::vector<Node>& terms,
242
                    std::vector<Node>& nred,
243
                    bool batch = true);
244
  /**
245
   * Calls the above function, where terms is getActive(), the set of currently
246
   * active terms.
247
   */
248
  bool doInferences(int effort, std::vector<Node>& nred, bool batch = true);
249
  /** doReductions
250
   *
251
   * This method has the same interface as doInferences. In contrast to
252
   * doInfereces, this method will send reduction lemmas of the form ( t = t' )
253
   * where t is in terms and t' is an equivalent reduced term.
254
   */
255
  bool doReductions(int effort,
256
                    const std::vector<Node>& terms,
257
                    std::vector<Node>& nred,
258
                    bool batch = true);
259
  bool doReductions(int effort, std::vector<Node>& nred, bool batch = true);
260
261
  /** get the set of all extended function terms from d_ext_func_terms */
262
  void getTerms(std::vector<Node>& terms);
263
  /** is there at least one active extended function term? */
264
  bool hasActiveTerm() const;
265
  /** is n an active extended function term? */
266
  bool isActive(Node n) const;
267
  /**
268
   * Same as above, but rid is updated to the reason if this method returns
269
   * false.
270
   */
271
  bool isActive(Node n, ExtReducedId& rid) const;
272
  /** get the set of active terms from d_ext_func_terms */
273
  std::vector<Node> getActive() const;
274
  /** get the set of active terms from d_ext_func_terms of kind k */
275
  std::vector<Node> getActive(Kind k) const;
276
277
 private:
278
  /** returns the set of variable subterms of n */
279
  static std::vector<Node> collectVars(Node n);
280
  /** is n context dependent inactive? */
281
  bool isContextIndependentInactive(Node n) const;
282
  /**
283
   * Same as above, but rid is updated to the reason if this method returns
284
   * true.
285
   */
286
  bool isContextIndependentInactive(Node n, ExtReducedId& rid) const;
287
  /** do inferences internal */
288
  bool doInferencesInternal(int effort,
289
                            const std::vector<Node>& terms,
290
                            std::vector<Node>& nred,
291
                            bool batch,
292
                            bool isRed);
293
  /** send lemma on the output channel */
294
  bool sendLemma(Node lem, bool preprocess = false);
295
  /** reference to the callback */
296
  ExtTheoryCallback& d_parent;
297
  /** Reference to the output channel we are using */
298
  OutputChannel& d_out;
299
  /** the true node */
300
  Node d_true;
301
  /** extended function terms, map to whether they are active */
302
  NodeBoolMap d_ext_func_terms;
303
  /** mapping to why extended function terms are inactive */
304
  NodeExtReducedIdMap d_extfExtReducedIdMap;
305
  /**
306
   * The set of terms from d_ext_func_terms that are SAT-context-independent
307
   * inactive. For instance, a term t is SAT-context-independent inactive if
308
   * a reduction lemma of the form t = t' was added in this user-context level.
309
   */
310
  NodeExtReducedIdMap d_ci_inactive;
311
  /**
312
   * Watched term for checking if any non-reduced extended functions exist.
313
   * This is an arbitrary active member of d_ext_func_terms.
314
   */
315
  context::CDO<Node> d_has_extf;
316
  /** the set of kinds we are treated as extended functions */
317
  std::map<Kind, bool> d_extf_kind;
318
  /** information for each term in d_ext_func_terms */
319
19396
  class ExtfInfo
320
  {
321
   public:
322
    /** all variables in this term */
323
    std::vector<Node> d_vars;
324
  };
325
  std::map<Node, ExtfInfo> d_extf_info;
326
327
  // cache of all lemmas sent
328
  NodeSet d_lemmas;
329
  NodeSet d_pp_lemmas;
330
};
331
332
}  // namespace theory
333
}  // namespace cvc5
334
335
#endif /* CVC5__THEORY__EXT_THEORY_H */