GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/first_order_model.h Lines: 4 6 66.7 %
Date: 2021-09-10 Branches: 9 22 40.9 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Paul Meng, Morgan Deters
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 extended classes.
14
 */
15
16
#include "cvc5_private.h"
17
18
#ifndef CVC5__FIRST_ORDER_MODEL_H
19
#define CVC5__FIRST_ORDER_MODEL_H
20
21
#include "context/cdlist.h"
22
#include "theory/quantifiers/equality_query.h"
23
#include "theory/theory_model.h"
24
#include "theory/uf/theory_uf_model.h"
25
26
namespace cvc5 {
27
namespace theory {
28
29
class TheoryModel;
30
class RepSet;
31
32
namespace quantifiers {
33
34
class QuantifiersState;
35
class TermRegistry;
36
class QuantifiersRegistry;
37
38
// TODO (#1301) : document and refactor this class
39
class FirstOrderModel
40
{
41
 public:
42
  FirstOrderModel(Env& env,
43
                  QuantifiersState& qs,
44
                  QuantifiersRegistry& qr,
45
                  TermRegistry& tr);
46
18358
  virtual ~FirstOrderModel() {}
47
48
  /** finish init */
49
  void finishInit(TheoryModel* m);
50
  //---------------------------------- access functions for underlying model
51
  /** Get value in the underlying theory model */
52
  Node getValue(TNode n) const;
53
  /** does the equality engine of this model have term a? */
54
  bool hasTerm(TNode a);
55
  /** get the representative of a in the equality engine of this model */
56
  Node getRepresentative(TNode a);
57
  /** are a and b equal in the equality engine of this model? */
58
  bool areEqual(TNode a, TNode b);
59
  /** are a and b disequal in the equality engine of this model? */
60
  bool areDisequal(TNode a, TNode b);
61
  /** get the equality engine for this model */
62
  eq::EqualityEngine* getEqualityEngine();
63
  /** get the representative set object */
64
  const RepSet* getRepSet() const;
65
  /** get the representative set object */
66
  RepSet* getRepSetPtr();
67
  /** get the entire theory model */
68
  TheoryModel* getTheoryModel();
69
  //---------------------------------- end access functions for underlying model
70
  /** get internal representative
71
   *
72
   * Choose a term that is equivalent to a in the current context that is the
73
   * best term for instantiating the index^th variable of quantified formula q.
74
   * If no legal term can be found, we return null. This can occur if:
75
   * - a's type is not a subtype of the type of the index^th variable of q,
76
   * - a is in an equivalent class with all terms that are restricted not to
77
   * appear in instantiations of q, e.g. INST_CONSTANT terms for counterexample
78
   * guided instantiation.
79
   */
80
  Node getInternalRepresentative(Node a, Node q, size_t index);
81
82
  /** assert quantifier */
83
  void assertQuantifier( Node n );
84
  /** get number of asserted quantifiers */
85
  size_t getNumAssertedQuantifiers() const;
86
  /** get asserted quantifier */
87
  Node getAssertedQuantifier( unsigned i, bool ordered = false );
88
  /** initialize model for term */
89
  void initializeModelForTerm( Node n, std::map< Node, bool >& visited );
90
  // initialize the model
91
  void initialize();
92
  /** get variable id */
93
49994
  int getVariableId(TNode q, TNode n) {
94
49994
    return d_quant_var_id.find( q )!=d_quant_var_id.end() ? d_quant_var_id[q][n] : -1;
95
  }
96
  /** do we need to do any work? */
97
  bool checkNeeded();
98
  /** reset round */
99
  void reset_round();
100
  /** mark quantified formula relevant */
101
  void markRelevant( Node q );
102
  /** set quantified formula active/inactive
103
   *
104
   * This indicates that quantified formula is "inactive", that is, it need
105
   * not be considered during this instantiation round.
106
   *
107
   * A quantified formula may be set inactive if for instance:
108
   *   - It is entailed by other quantified formulas, or
109
   *   - All of its instances are known to be true in the current model.
110
   *
111
   * This method should be called after a call to FirstOrderModel::reset_round,
112
   * and before calls to QuantifiersModule check calls. A common place to call
113
   * this method is during QuantifiersModule reset_round calls.
114
   */
115
  void setQuantifierActive( TNode q, bool active );
116
  /** is quantified formula active?
117
   *
118
   * Returns false if there has been a call to setQuantifierActive( q, false )
119
   * during this instantiation round.
120
   */
121
  bool isQuantifierActive(TNode q) const;
122
  /** is quantified formula asserted */
123
  bool isQuantifierAsserted(TNode q) const;
124
  /** get model basis term */
125
  Node getModelBasisTerm(TypeNode tn);
126
  /** is model basis term */
127
  bool isModelBasisTerm(Node n);
128
  /** get model basis term for op */
129
  Node getModelBasisOpTerm(Node op);
130
  /** get model basis */
131
  Node getModelBasis(Node q, Node n);
132
  /** get model basis arg */
133
  unsigned getModelBasisArg(Node n);
134
  /** get some domain element */
135
  Node getSomeDomainElement(TypeNode tn);
136
  /** initialize representative set for type
137
   *
138
   * This ensures that TheoryModel::d_rep_set
139
   * is initialized for type tn. In particular:
140
   * (1) If tn is an uninitialized (unconstrained)
141
   * uninterpreted sort, then we interpret it
142
   * as a set of size one,
143
   * (2) If tn is a "small" enumerable finite type,
144
   * then we ensure that all its values are in
145
   * TheoryModel::d_rep_set.
146
   *
147
   * Returns true if the initialization was complete,
148
   * in that the set for tn in TheoryModel::d_rep_set
149
   * has all representatives of type tn.
150
   */
151
  bool initializeRepresentativesForType(TypeNode tn);
152
  /**
153
   * Has the term been marked as a model basis term?
154
   */
155
  static bool isModelBasis(TNode n);
156
  /** Get the equality query */
157
  EqualityQuery* getEqualityQuery();
158
159
 protected:
160
  /** Pointer to the underyling theory model */
161
  TheoryModel* d_model;
162
  /** The quantifiers registry */
163
  QuantifiersRegistry& d_qreg;
164
  /** Reference to the term registry */
165
  TermRegistry& d_treg;
166
  /** equality query class */
167
  EqualityQuery d_eq_query;
168
  /** list of quantifiers asserted in the current context */
169
  context::CDList<Node> d_forall_asserts;
170
  /**
171
   * The (ordered) list of quantified formulas marked as relevant using
172
   * markRelevant, where the quantified formula q in the most recent
173
   * call to markRelevant comes last in the list.
174
   */
175
  std::vector<Node> d_forall_rlv_vec;
176
  /** The last quantified formula marked as relevant, if one exists. */
177
  Node d_last_forall_rlv;
178
  /**
179
   * The list of asserted quantified formulas, ordered by relevance.
180
   * Relevance is a dynamic partial ordering where q1 < q2 if there has been
181
   * a call to markRelevant( q1 ) after the last call to markRelevant( q2 )
182
   * (or no call to markRelevant( q2 ) has been made).
183
   *
184
   * This list is used primarily as an optimization for conflict-based
185
   * instantiation so that quantifed formulas that have been instantiated
186
   * most recently are processed first, since these are (statistically) more
187
   * likely to have conflicting instantiations.
188
   */
189
  std::vector<Node> d_forall_rlv_assert;
190
  /**
191
   * Whether the above list has been computed. This flag is updated during
192
   * reset_round and is valid within a full effort check.
193
   */
194
  bool d_forallRlvComputed;
195
  /** get variable id */
196
  std::map<Node, std::map<Node, int> > d_quant_var_id;
197
  /** process initialize model for term */
198
  virtual void processInitializeModelForTerm(Node n) {}
199
  /** process initialize quantifier */
200
9121
  virtual void processInitializeQuantifier(Node q) {}
201
  /** process initialize */
202
  virtual void processInitialize(bool ispre) {}
203
204
 private:
205
  // list of inactive quantified formulas
206
  std::map<TNode, bool> d_quant_active;
207
  /** map from types to model basis terms */
208
  std::map<TypeNode, Node> d_model_basis_term;
209
  /** map from ops to model basis terms */
210
  std::map<Node, Node> d_model_basis_op_term;
211
  /** map from instantiation terms to their model basis equivalent */
212
  std::map<Node, Node> d_model_basis_body;
213
  /** map from universal quantifiers to model basis terms */
214
  std::map<Node, std::vector<Node> > d_model_basis_terms;
215
  /** compute model basis arg */
216
  void computeModelBasisArgAttribute(Node n);
217
};/* class FirstOrderModel */
218
219
}  // namespace quantifiers
220
}  // namespace theory
221
}  // namespace cvc5
222
223
#endif /* CVC5__FIRST_ORDER_MODEL_H */