GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/theory_model.h Lines: 4 4 100.0 %
Date: 2021-05-22 Branches: 1 2 50.0 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Clark Barrett, 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
 * Model class.
14
 */
15
16
#include "cvc5_private.h"
17
18
#ifndef CVC5__THEORY__THEORY_MODEL_H
19
#define CVC5__THEORY__THEORY_MODEL_H
20
21
#include <unordered_map>
22
#include <unordered_set>
23
24
#include "theory/ee_setup_info.h"
25
#include "theory/rep_set.h"
26
#include "theory/type_enumerator.h"
27
#include "theory/type_set.h"
28
#include "theory/uf/equality_engine.h"
29
30
namespace cvc5 {
31
32
class Env;
33
34
namespace theory {
35
36
/** Theory Model class.
37
 *
38
 * This class represents a model produced by the TheoryEngine.
39
 * The data structures used to represent a model are:
40
 * (1) d_equalityEngine : an equality engine object, which stores
41
 *     an equivalence relation over all terms that exist in
42
 *     the current set of assertions.
43
 * (2) d_reps : a map from equivalence class representatives of
44
 *     the equality engine to the (constant) representatives
45
 *     assigned to that equivalence class.
46
 * (3) d_uf_models : a map from uninterpreted functions to their
47
 *     lambda representation.
48
 * (4) d_rep_set : a data structure that allows interpretations
49
 *     for types to be represented as terms. This is useful for
50
 *     finite model finding.
51
 * Additionally, models are dependent on top-level substitutions stored in the
52
 * d_env class.
53
 *
54
 * These data structures are built after a full effort check with
55
 * no lemmas sent, within a call to:
56
 *    TheoryEngineModelBuilder::buildModel(...)
57
 * which includes subcalls to TheoryX::collectModelInfo(...) calls.
58
 *
59
 * These calls may modify the model object using the interface
60
 * functions below, including:
61
 * - assertEquality, assertPredicate, assertSkeleton,
62
 *   assertEqualityEngine.
63
 * - assignFunctionDefinition
64
 *
65
 * This class provides several interface functions:
66
 * - hasTerm, getRepresentative, areEqual, areDisequal
67
 * - getEqualityEngine
68
 * - getRepSet
69
 * - hasAssignedFunctionDefinition, getFunctionsToAssign
70
 * - getValue
71
 *
72
 * The above functions can be used for a model m after it has been
73
 * successfully built, i.e. when m->isBuiltSuccess() returns true.
74
 *
75
 * Additionally, all of the above functions, with the exception of getValue,
76
 * can be used during step (5) of TheoryEngineModelBuilder::buildModel, as
77
 * documented in theory_model_builder.h. In particular, we make calls to the
78
 * above functions such as getRepresentative() when assigning total
79
 * interpretations for uninterpreted functions.
80
 */
81
class TheoryModel
82
{
83
  friend class TheoryEngineModelBuilder;
84
85
 public:
86
  TheoryModel(Env& env, std::string name, bool enableFuncModels);
87
  virtual ~TheoryModel();
88
  /**
89
   * Finish init, where ee is the equality engine the model should use.
90
   */
91
  void finishInit(eq::EqualityEngine* ee);
92
93
  /** reset the model */
94
  virtual void reset();
95
  //---------------------------- for building the model
96
  /** assert equality holds in the model
97
   *
98
   * This method returns true if and only if the equality engine of this model
99
   * is consistent after asserting the equality to this model.
100
   */
101
  bool assertEquality(TNode a, TNode b, bool polarity);
102
  /** assert predicate holds in the model
103
   *
104
   * This method returns true if and only if the equality engine of this model
105
   * is consistent after asserting the predicate to this model.
106
   */
107
  bool assertPredicate(TNode a, bool polarity);
108
  /** assert all equalities/predicates in equality engine hold in the model
109
   *
110
   * This method returns true if and only if the equality engine of this model
111
   * is consistent after asserting the equality engine to this model.
112
   */
113
  bool assertEqualityEngine(const eq::EqualityEngine* ee,
114
                            const std::set<Node>* termSet = NULL);
115
  /** assert skeleton
116
   *
117
   * This method gives a "skeleton" for the model value of the equivalence
118
   * class containing n. This should be an application of interpreted function
119
   * (e.g. datatype constructor, array store, set union chain). The subterms of
120
   * this term that are variables or terms that belong to other theories will
121
   * be filled in with model values.
122
   *
123
   * For example, if we call assertSkeleton on (C x y) where C is a datatype
124
   * constructor and x and y are variables, then the equivalence class of
125
   * (C x y) will be interpreted in m as (C x^m y^m) where
126
   * x^m = m->getValue( x ) and y^m = m->getValue( y ).
127
   *
128
   * It should be called during model generation, before final representatives
129
   * are chosen. In the case of TheoryEngineModelBuilder, it should be called
130
   * during Theory's collectModelInfo( ... ) functions.
131
   */
132
  void assertSkeleton(TNode n);
133
  /** set assignment exclusion set
134
   *
135
   * This method sets the "assignment exclusion set" for term n. This is a
136
   * set of terms whose value n must be distinct from in the model.
137
   *
138
   * This method should be used sparingly, and in a way such that model
139
   * building is still guaranteed to succeed. Term n is intended to be an
140
   * assignable term, typically of finite type. Thus, for example, this method
141
   * should not be called with a vector eset that is greater than the
142
   * cardinality of the type of n. Additionally, this method should not be
143
   * called in a way that introduces cyclic dependencies on the assignment order
144
   * of terms in the model. For example, providing { y } as the assignment
145
   * exclusion set of x and { x } as the assignment exclusion set of y will
146
   * cause model building to fail.
147
   *
148
   * The vector eset should contain only terms that occur in the model, or
149
   * are constants.
150
   *
151
   * Additionally, we (currently) require that an assignment exclusion set
152
   * should not be set for two terms in the same equivalence class, or to
153
   * equivalence classes with an assignable term. Otherwise an
154
   * assertion will be thrown by TheoryEngineModelBuilder during model building.
155
   */
156
  void setAssignmentExclusionSet(TNode n, const std::vector<Node>& eset);
157
  /** set assignment exclusion set group
158
   *
159
   * Given group = { x_1, ..., x_n }, this is semantically equivalent to calling
160
   * the above method on the following pairs of arguments:
161
   *   x1, eset
162
   *   x2, eset + { x_1 }
163
   *   ...
164
   *   xn, eset + { x_1, ..., x_{n-1} }
165
   * Similar restrictions should be considered as above when applying this
166
   * method to ensure that model building will succeed. Notice that for
167
   * efficiency, the implementation of how the above information is stored
168
   * may avoid constructing n copies of eset.
169
   */
170
  void setAssignmentExclusionSetGroup(const std::vector<TNode>& group,
171
                                      const std::vector<Node>& eset);
172
  /** get assignment exclusion set for term n
173
   *
174
   * If n has been given an assignment exclusion set, then this method returns
175
   * true and the set is added to eset. Otherwise, the method returns false.
176
   *
177
   * Additionally, if n was assigned an assignment exclusion set via a call to
178
   * setAssignmentExclusionSetGroup, it adds all members that were passed
179
   * in the first argument of that call to the vector group. Otherwise, it
180
   * adds n itself to group.
181
   */
182
  bool getAssignmentExclusionSet(TNode n,
183
                                 std::vector<Node>& group,
184
                                 std::vector<Node>& eset);
185
  /** have any assignment exclusion sets been created? */
186
  bool hasAssignmentExclusionSets() const;
187
  /** record approximation
188
   *
189
   * This notifies this model that the value of n was approximated in this
190
   * model such that the predicate pred (involving n) holds. For example,
191
   * for transcendental functions, we may determine an error bound on the
192
   * value of a transcendental function, say c-e <= y <= c+e where
193
   * c and e are constants. We call this function with n set to sin( x ) and
194
   * pred set to c-e <= sin( x ) <= c+e.
195
   *
196
   * If recordApproximation is called at least once during the model
197
   * construction process, then check-model is not guaranteed to succeed.
198
   * However, there are cases where we can establish the input is satisfiable
199
   * without constructing an exact model. For example, if x=.77, sin(x)=.7, and
200
   * say we have computed c=.7 and e=.01 as an approximation in the above
201
   * example, then we may reason that the set of assertions { sin(x)>.6 } is
202
   * satisfiable, albiet without establishing an exact (irrational) value for
203
   * sin(x).
204
   *
205
   * This function is simply for bookkeeping, it does not affect the model
206
   * construction process.
207
   */
208
  void recordApproximation(TNode n, TNode pred);
209
  /**
210
   * Same as above, but with a witness constant. This ensures that the
211
   * approximation predicate is of the form (or (= n witness) pred). This
212
   * is useful if the user wants to know a possible concrete value in
213
   * the range of the predicate.
214
   */
215
  void recordApproximation(TNode n, TNode pred, Node witness);
216
  /** set unevaluate/semi-evaluated kind
217
   *
218
   * This informs this model how it should interpret applications of terms with
219
   * kind k in getModelValue. We distinguish four categories of kinds:
220
   *
221
   * [1] "Evaluated"
222
   * This includes (standard) interpreted symbols like NOT, PLUS, UNION, etc.
223
   * These operators can be characterized by the invariant that they are
224
   * "evaluatable". That is, if they are applied to only constants, the rewriter
225
   * is guaranteed to rewrite the application to a constant. When getting
226
   * the model value of <k>( t1...tn ) where k is a kind of this category, we
227
   * compute the (constant) value of t1...tn, say this returns c1...cn, we
228
   * return the (constant) result of rewriting <k>( c1...cn ).
229
   *
230
   * [2] "Unevaluated"
231
   * This includes interpreted symbols like FORALL, EXISTS,
232
   * CARDINALITY_CONSTRAINT, that are not evaluatable. When getting a model
233
   * value for a term <k>( t1...tn ) where k is a kind of this category, we
234
   * check whether <k>( t1...tn ) exists in the equality engine of this model.
235
   * If it does, we return its representative, otherwise we return the term
236
   * itself.
237
   *
238
   * [3] "Semi-evaluated"
239
   * This includes kinds like BITVECTOR_ACKERMANNIZE_UDIV and others, typically
240
   * those that correspond to abstractions. Like unevaluated kinds, these
241
   * kinds do not have an evaluator. In contrast to unevaluated kinds, we
242
   * interpret a term <k>( t1...tn ) not appearing in the equality engine as an
243
   * arbitrary value instead of the term itself.
244
   *
245
   * [4] APPLY_UF, where getting the model value depends on an internally
246
   * constructed representation of a lambda model value (d_uf_models).
247
   * It is optional whether this kind is "evaluated" or "semi-evaluated".
248
   * In the case that it is "evaluated", get model rewrites the application
249
   * of the lambda model value of its operator to its evaluated arguments.
250
   *
251
   * By default, all kinds are considered "evaluated". The following methods
252
   * change the interpretation of various (non-APPLY_UF) kinds to one of the
253
   * above categories and should be called by the theories that own the kind
254
   * during Theory::finishInit. We set APPLY_UF to be semi-interpreted when
255
   * this model does not enabled function values (this is the case for the model
256
   * of TheoryEngine when the option assignFunctionValues is set to false).
257
   */
258
  void setUnevaluatedKind(Kind k);
259
  void setSemiEvaluatedKind(Kind k);
260
  /**
261
   * Set irrelevant kind. These kinds do not impact model generation, that is,
262
   * registered terms in theories of this kind do not need to be sent to
263
   * the model. An example is APPLY_TESTER.
264
   */
265
  void setIrrelevantKind(Kind k);
266
  /**
267
   * Get the set of irrelevant kinds that have been registered by the above
268
   * method.
269
   */
270
  const std::set<Kind>& getIrrelevantKinds() const;
271
  /** is legal elimination
272
   *
273
   * Returns true if x -> val is a legal elimination of variable x.
274
   * In particular, this ensures that val does not have any subterms that
275
   * are of unevaluated kinds.
276
   */
277
  bool isLegalElimination(TNode x, TNode val);
278
  //---------------------------- end building the model
279
280
  // ------------------- general equality queries
281
  /** does the equality engine of this model have term a? */
282
  bool hasTerm(TNode a);
283
  /** get the representative of a in the equality engine of this model */
284
  Node getRepresentative(TNode a);
285
  /** are a and b equal in the equality engine of this model? */
286
  bool areEqual(TNode a, TNode b);
287
  /** are a and b disequal in the equality engine of this model? */
288
  bool areDisequal(TNode a, TNode b);
289
  /** get the equality engine for this model */
290
8302
  eq::EqualityEngine* getEqualityEngine() { return d_equalityEngine; }
291
  // ------------------- end general equality queries
292
293
  /** Get value function.
294
   * This should be called only after a ModelBuilder
295
   * has called buildModel(...) on this model.
296
   */
297
  Node getValue(TNode n) const;
298
  /** get comments */
299
  void getComments(std::ostream& out) const;
300
301
  //---------------------------- separation logic
302
  /** set the heap and value sep.nil is equal to */
303
  void setHeapModel(Node h, Node neq);
304
  /** get the heap and value sep.nil is equal to */
305
  bool getHeapModel(Node& h, Node& neq) const;
306
  //---------------------------- end separation logic
307
308
  /** is the list of approximations non-empty? */
309
  bool hasApproximations() const;
310
  /** get approximations */
311
  std::vector<std::pair<Node, Node> > getApproximations() const;
312
  /** get domain elements for uninterpreted sort t */
313
  std::vector<Node> getDomainElements(TypeNode t) const;
314
  /** get the representative set object */
315
202393
  const RepSet* getRepSet() const { return &d_rep_set; }
316
  /** get the representative set object (FIXME: remove this, see #1199) */
317
24667
  RepSet* getRepSetPtr() { return &d_rep_set; }
318
319
  //---------------------------- model cores
320
  /** set using model core */
321
  void setUsingModelCore();
322
  /** record model core symbol */
323
  void recordModelCoreSymbol(Node sym);
324
  /** Return whether symbol expr is in the model core. */
325
  bool isModelCoreSymbol(Node sym) const;
326
  //---------------------------- end model cores
327
328
  /** get cardinality for sort */
329
  Cardinality getCardinality(TypeNode t) const;
330
331
  //---------------------------- function values
332
  /** Does this model have terms for the given uninterpreted function? */
333
  bool hasUfTerms(Node f) const;
334
  /** Get the terms for uninterpreted function f */
335
  const std::vector<Node>& getUfTerms(Node f) const;
336
  /** are function values enabled? */
337
  bool areFunctionValuesEnabled() const;
338
  /** assign function value f to definition f_def */
339
  void assignFunctionDefinition( Node f, Node f_def );
340
  /** have we assigned function f? */
341
13977
  bool hasAssignedFunctionDefinition( Node f ) const { return d_uf_models.find( f )!=d_uf_models.end(); }
342
  /** get the list of functions to assign.
343
  * This list will contain all terms of function type that are terms in d_equalityEngine.
344
  * If higher-order is enabled, we ensure that this list is sorted by type size.
345
  * This allows us to assign functions T -> T before ( T x T ) -> T and before ( T -> T ) -> T,
346
  * which is required for "dag form" model construction (see TheoryModelBuilder::assignHoFunction).
347
  */
348
  std::vector< Node > getFunctionsToAssign();
349
  //---------------------------- end function values
350
  /** Get the name of this model */
351
  const std::string& getName() const;
352
  /**
353
   * For debugging, print the equivalence classes of the underlying equality
354
   * engine.
355
   */
356
  std::string debugPrintModelEqc() const;
357
358
 protected:
359
  /** Reference to the environmanet */
360
  Env& d_env;
361
  /** Unique name of this model */
362
  std::string d_name;
363
  /** equality engine containing all known equalities/disequalities */
364
  eq::EqualityEngine* d_equalityEngine;
365
  /** approximations (see recordApproximation) */
366
  std::map<Node, Node> d_approximations;
367
  /** list of all approximations */
368
  std::vector<std::pair<Node, Node> > d_approx_list;
369
  /** a set of kinds that are unevaluated */
370
  std::unordered_set<Kind, kind::KindHashFunction> d_unevaluated_kinds;
371
  /** a set of kinds that are semi-evaluated */
372
  std::unordered_set<Kind, kind::KindHashFunction> d_semi_evaluated_kinds;
373
  /** The set of irrelevant kinds */
374
  std::set<Kind> d_irrKinds;
375
  /**
376
   * Map of representatives of equality engine to used representatives in
377
   * representative set
378
   */
379
  std::map<Node, Node> d_reps;
380
  /** Map of terms to their assignment exclusion set. */
381
  std::map<Node, std::vector<Node> > d_assignExcSet;
382
  /**
383
   * Map of terms to their "assignment exclusion set master". After a call to
384
   * setAssignmentExclusionSetGroup, the master of each term in group
385
   * (except group[0]) is set to group[0], which stores the assignment
386
   * exclusion set for that group in the above map.
387
   */
388
  std::map<Node, Node> d_aesMaster;
389
  /** Reverse of the above map */
390
  std::map<Node, std::vector<Node> > d_aesSlaves;
391
  /** stores set of representatives for each type */
392
  RepSet d_rep_set;
393
  /** true/false nodes */
394
  Node d_true;
395
  Node d_false;
396
  /** comment stream to include in printing */
397
  std::stringstream d_comment_str;
398
  /** are we using model cores? */
399
  bool d_using_model_core;
400
  /** symbols that are in the model core */
401
  std::unordered_set<Node> d_model_core;
402
  /** Get model value function.
403
   *
404
   * This function is a helper function for getValue.
405
   */
406
  Node getModelValue(TNode n) const;
407
  /** add term internal
408
   *
409
   * This will do any model-specific processing necessary for n,
410
   * such as constraining the interpretation of uninterpreted functions.
411
   * This is called once for all terms in the equality engine, just before
412
   * a model builder constructs this model.
413
   */
414
  virtual void addTermInternal(TNode n);
415
 private:
416
  /** cache for getModelValue */
417
  mutable std::unordered_map<Node, Node> d_modelCache;
418
419
  //---------------------------- separation logic
420
  /** the value of the heap */
421
  Node d_sep_heap;
422
  /** the value of the nil element */
423
  Node d_sep_nil_eq;
424
  //---------------------------- end separation logic
425
426
  //---------------------------- function values
427
  /** a map from functions f to a list of all APPLY_UF terms with operator f */
428
  std::map<Node, std::vector<Node> > d_uf_terms;
429
  /** a map from functions f to a list of all HO_APPLY terms with first argument
430
   * f */
431
  std::map<Node, std::vector<Node> > d_ho_uf_terms;
432
  /** whether function models are enabled */
433
  bool d_enableFuncModels;
434
  /** map from function terms to the (lambda) definitions
435
  * After the model is built, the domain of this map is all terms of function
436
  * type that appear as terms in d_equalityEngine.
437
  */
438
  std::map<Node, Node> d_uf_models;
439
  //---------------------------- end function values
440
};/* class TheoryModel */
441
442
}  // namespace theory
443
}  // namespace cvc5
444
445
#endif /* CVC5__THEORY__THEORY_MODEL_H */