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