GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/strings/extf_solver.h Lines: 4 4 100.0 %
Date: 2021-08-16 Branches: 1 2 50.0 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Andres Noetzli, Tim King
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
 * Solver for extended functions of theory of strings.
14
 */
15
16
#include "cvc5_private.h"
17
18
#ifndef CVC5__THEORY__STRINGS__EXTF_SOLVER_H
19
#define CVC5__THEORY__STRINGS__EXTF_SOLVER_H
20
21
#include <map>
22
#include <vector>
23
24
#include "context/cdo.h"
25
#include "expr/node.h"
26
#include "theory/ext_theory.h"
27
#include "theory/strings/base_solver.h"
28
#include "theory/strings/core_solver.h"
29
#include "theory/strings/inference_manager.h"
30
#include "theory/strings/sequences_stats.h"
31
#include "theory/strings/skolem_cache.h"
32
#include "theory/strings/solver_state.h"
33
#include "theory/strings/strings_rewriter.h"
34
#include "theory/strings/theory_strings_preprocess.h"
35
36
namespace cvc5 {
37
namespace theory {
38
namespace strings {
39
40
/**
41
 * Non-static information about an extended function t. This information is
42
 * constructed and used during the check extended function evaluation
43
 * inference schema.
44
 *
45
 * In the following, we refer to the "context-dependent simplified form"
46
 * of a term t to be the result of rewriting t * sigma, where sigma is a
47
 * derivable substitution in the current context. For example, the
48
 * context-depdendent simplified form of contains( x++y, "a" ) given
49
 * sigma = { x -> "" } is contains(y,"a").
50
 */
51
405143
class ExtfInfoTmp
52
{
53
 public:
54
405143
  ExtfInfoTmp() : d_modelActive(true) {}
55
  /**
56
   * If s is in d_ctn[true] (resp. d_ctn[false]), then contains( t, s )
57
   * (resp. ~contains( t, s )) holds in the current context. The vector
58
   * d_ctnFrom is the explanation for why this holds. For example,
59
   * if d_ctn[false][i] is "A", then d_ctnFrom[false][i] might be
60
   * t = x ++ y AND x = "" AND y = "B".
61
   */
62
  std::map<bool, std::vector<Node> > d_ctn;
63
  std::map<bool, std::vector<Node> > d_ctnFrom;
64
  /**
65
   * The constant that t is entailed to be equal to, or null if none exist.
66
   */
67
  Node d_const;
68
  /**
69
   * The explanation for why t is equal to its context-dependent simplified
70
   * form.
71
   */
72
  std::vector<Node> d_exp;
73
  /** This flag is false if t is reduced in the model. */
74
  bool d_modelActive;
75
};
76
77
/**
78
 * Extended function solver for the theory of strings. This manages extended
79
 * functions for the theory of strings using a combination of context-dependent
80
 * simplification (Reynolds et al CAV 2017) and lazy reductions.
81
 */
82
class ExtfSolver
83
{
84
  typedef context::CDHashSet<Node> NodeSet;
85
86
 public:
87
  ExtfSolver(SolverState& s,
88
             InferenceManager& im,
89
             TermRegistry& tr,
90
             StringsRewriter& rewriter,
91
             BaseSolver& bs,
92
             CoreSolver& cs,
93
             ExtTheory& et,
94
             SequencesStatistics& statistics);
95
  ~ExtfSolver();
96
  /** check extended functions evaluation
97
   *
98
   * This applies "context-dependent simplification" for all active extended
99
   * function terms in this SAT context. This infers facts of the form:
100
   *   x = c => f( t1 ... tn ) = c'
101
   * where the rewritten form of f( t1...tn ) { x |-> c } is c', and x = c
102
   * is a (tuple of) equalities that are asserted in this SAT context, and
103
   * f( t1 ... tn ) is a term from this SAT context.
104
   *
105
   * For more details, this is steps 4 when effort=0 and step 6 when
106
   * effort=1 from Strategy 1 in Reynolds et al, "Scaling up DPLL(T) String
107
   * Solvers using Context-Dependent Simplification", CAV 2017. When called with
108
   * effort=3, we apply context-dependent simplification based on model values.
109
   */
110
  void checkExtfEval(int effort);
111
  /** check extended function reductions
112
   *
113
   * This adds "reduction" lemmas for each active extended function in this SAT
114
   * context. These are generally lemmas of the form:
115
   *   F[t1...tn,k] ^ f( t1 ... tn ) = k
116
   * where f( t1 ... tn ) is an active extended function, k is a fresh constant
117
   * and F is a formula that constrains k based on the definition of f.
118
   *
119
   * For more details, this is step 7 from Strategy 1 in Reynolds et al,
120
   * CAV 2017. We stratify this in practice, where calling this with effort=1
121
   * reduces some of the "easier" extended functions, and effort=2 reduces
122
   * the rest.
123
   */
124
  void checkExtfReductions(int effort);
125
  /** get preprocess module */
126
  StringsPreprocess* getPreprocess() { return &d_preproc; }
127
128
  /**
129
   * Get the current substitution for term n.
130
   *
131
   * This method returns a term that n is currently equal to in the current
132
   * context. It updates exp to contain an explanation of why it is currently
133
   * equal to that term.
134
   *
135
   * The argument effort determines what kind of term to return, either
136
   * a constant in the equivalence class of n (effort=0), the normal form of
137
   * n (effort=1,2) or the model value of n (effort>=3). The latter is only
138
   * valid at LAST_CALL effort. If a term of the above form cannot be returned,
139
   * then n itself is returned.
140
   */
141
  Node getCurrentSubstitutionFor(int effort, Node n, std::vector<Node>& exp);
142
  /** get mapping from extended functions to their information
143
   *
144
   * This information is valid during a full effort check after a call to
145
   * checkExtfEval.
146
   */
147
  const std::map<Node, ExtfInfoTmp>& getInfo() const;
148
  //---------------------------------- information about ExtTheory
149
  /** Are there any active extended functions? */
150
  bool hasExtendedFunctions() const;
151
  /**
152
   * Get the function applications of kind k that are active in the current
153
   * context (see ExtTheory::getActive).
154
   */
155
  std::vector<Node> getActive(Kind k) const;
156
  //---------------------------------- end information about ExtTheory
157
  /**
158
   * Print the relevant information regarding why we have a model, return as a
159
   * string.
160
   */
161
  std::string debugPrintModel();
162
163
 private:
164
  /** do reduction
165
   *
166
   * This is called when an extended function application n is not able to be
167
   * simplified by context-depdendent simplification, and we are resorting to
168
   * expanding n to its full semantics via a reduction. This method returns
169
   * true if it successfully reduced n by some reduction. If so, it either
170
   * caches that the reduction lemma was sent, or marks n as reduced in this
171
   * SAT-context. The argument effort has the same meaning as in
172
   * checkExtfReductions.
173
   */
174
  bool doReduction(int effort, Node n);
175
  /** check extended function inferences
176
   *
177
   * This function makes additional inferences for n that do not contribute
178
   * to its reduction, but may help show a refutation.
179
   *
180
   * This function is called when the context-depdendent simplified form of
181
   * n is nr. The argument "in" is the information object for n. The argument
182
   * "effort" has the same meaning as the effort argument of checkExtfEval.
183
   */
184
  void checkExtfInference(Node n, Node nr, ExtfInfoTmp& in, int effort);
185
  /** The solver state object */
186
  SolverState& d_state;
187
  /** The (custom) output channel of the theory of strings */
188
  InferenceManager& d_im;
189
  /** Reference to the term registry of theory of strings */
190
  TermRegistry& d_termReg;
191
  /** The theory rewriter for this theory. */
192
  StringsRewriter& d_rewriter;
193
  /** reference to the base solver, used for certain queries */
194
  BaseSolver& d_bsolver;
195
  /** reference to the core solver, used for certain queries */
196
  CoreSolver& d_csolver;
197
  /** the extended theory object for the theory of strings */
198
  ExtTheory& d_extt;
199
  /** Reference to the statistics for the theory of strings/sequences. */
200
  SequencesStatistics& d_statistics;
201
  /** preprocessing utility, for performing strings reductions */
202
  StringsPreprocess d_preproc;
203
  /** Common constants */
204
  Node d_true;
205
  Node d_false;
206
  /** Empty vector */
207
  std::vector<Node> d_emptyVec;
208
  /** map extended functions to the above information */
209
  std::map<Node, ExtfInfoTmp> d_extfInfoTmp;
210
  /** any non-reduced extended functions exist? */
211
  context::CDO<bool> d_hasExtf;
212
  /** extended functions inferences cache */
213
  NodeSet d_extfInferCache;
214
  /** The set of extended functions we have sent reduction lemmas for */
215
  NodeSet d_reduced;
216
};
217
218
/** An extended theory callback */
219
9853
class StringsExtfCallback : public ExtTheoryCallback
220
{
221
 public:
222
9853
  StringsExtfCallback() : d_esolver(nullptr) {}
223
  /**
224
   * Get current substitution based on the underlying extended function
225
   * solver.
226
   */
227
  bool getCurrentSubstitution(int effort,
228
                              const std::vector<Node>& vars,
229
                              std::vector<Node>& subs,
230
                              std::map<Node, std::vector<Node> >& exp) override;
231
  /** The extended function solver */
232
  ExtfSolver* d_esolver;
233
};
234
235
}  // namespace strings
236
}  // namespace theory
237
}  // namespace cvc5
238
239
#endif /* CVC5__THEORY__STRINGS__EXTF_SOLVER_H */