GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/quant_conflict_find.h Lines: 18 18 100.0 %
Date: 2021-11-07 Branches: 19 36 52.8 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Tim King, 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
 * Quantifiers conflict find class.
14
 */
15
16
#include "cvc5_private.h"
17
18
#ifndef QUANT_CONFLICT_FIND
19
#define QUANT_CONFLICT_FIND
20
21
#include <ostream>
22
#include <vector>
23
24
#include "context/cdhashmap.h"
25
#include "context/cdlist.h"
26
#include "expr/node_trie.h"
27
#include "theory/quantifiers/quant_module.h"
28
29
namespace cvc5 {
30
namespace theory {
31
namespace quantifiers {
32
33
class QuantConflictFind;
34
class QuantInfo;
35
36
//match generator
37
362685
class MatchGen {
38
  friend class QuantInfo;
39
private:
40
  //current children information
41
  int d_child_counter;
42
  bool d_use_children;
43
  //children of this object
44
  std::vector< int > d_children_order;
45
469132
  unsigned getNumChildren() { return d_children.size(); }
46
1932949
  MatchGen * getChild( int i ) { return &d_children[d_children_order[i]]; }
47
  //MatchGen * getChild( int i ) { return &d_children[i]; }
48
  //current matching information
49
  std::vector<TNodeTrie*> d_qn;
50
  std::vector<std::map<TNode, TNodeTrie>::iterator> d_qni;
51
  bool doMatching( QuantConflictFind * p, QuantInfo * qi );
52
  //for matching : each index is either a variable or a ground term
53
  unsigned d_qni_size;
54
  std::map< int, int > d_qni_var_num;
55
  std::map< int, TNode > d_qni_gterm;
56
  std::map< int, int > d_qni_bound;
57
  std::vector< int > d_qni_bound_except;
58
  std::map< int, TNode > d_qni_bound_cons;
59
  std::map< int, int > d_qni_bound_cons_var;
60
  std::map< int, int >::iterator d_binding_it;
61
  //std::vector< int > d_independent;
62
  bool d_matched_basis;
63
  bool d_binding;
64
  //int getVarBindingVar();
65
  std::map< int, Node > d_ground_eval;
66
  //determine variable order
67
  void determineVariableOrder( QuantInfo * qi, std::vector< int >& bvars );
68
  void collectBoundVar( QuantInfo * qi, Node n, std::vector< int >& cbvars, std::map< Node, bool >& visited, bool& hasNested );
69
public:
70
  //type of the match generator
71
  enum {
72
    typ_invalid,
73
    typ_ground,
74
    typ_pred,
75
    typ_eq,
76
    typ_formula,
77
    typ_var,
78
    typ_bool_var,
79
    typ_tconstraint,
80
    typ_tsym,
81
  };
82
  void debugPrintType( const char * c, short typ, bool isTrace = false );
83
public:
84
  MatchGen();
85
  MatchGen( QuantInfo * qi, Node n, bool isVar = false );
86
  bool d_tgt;
87
  bool d_tgt_orig;
88
  bool d_wasSet;
89
  Node d_n;
90
  std::vector< MatchGen > d_children;
91
  short d_type;
92
  bool d_type_not;
93
  /** reset round
94
   *
95
   * Called once at the beginning of each full/last-call effort, prior to
96
   * processing this match generator. This method returns false if the reset
97
   * failed, e.g. if a conflict was encountered during term indexing.
98
   */
99
  bool reset_round(QuantConflictFind* p);
100
  void reset( QuantConflictFind * p, bool tgt, QuantInfo * qi );
101
  bool getNextMatch( QuantConflictFind * p, QuantInfo * qi );
102
452718
  bool isValid() { return d_type!=typ_invalid; }
103
  void setInvalid();
104
105
  // is this term treated as UF application?
106
  static bool isHandledBoolConnective( TNode n );
107
  static bool isHandledUfTerm( TNode n );
108
  static Node getMatchOperator( QuantConflictFind * p, Node n );
109
  //can this node be handled by the algorithm
110
  static bool isHandled( TNode n );
111
};
112
113
//info for quantifiers
114
class QuantInfo {
115
private:
116
  void registerNode( Node n, bool hasPol, bool pol, bool beneathQuant = false );
117
  void flatten( Node n, bool beneathQuant );
118
private: //for completing match
119
  std::vector< int > d_unassigned;
120
  std::vector< TypeNode > d_unassigned_tn;
121
  int d_unassigned_nvar;
122
  int d_una_index;
123
  std::vector< int > d_una_eqc_count;
124
  //optimization: track which arguments variables appear under UF terms in
125
  std::map< int, std::map< TNode, std::vector< unsigned > > > d_var_rel_dom;
126
  void getPropagateVars( QuantConflictFind * p, std::vector< TNode >& vars, TNode n, bool pol, std::map< TNode, bool >& visited );
127
  //optimization: number of variables set, to track when we can stop
128
  std::map< int, bool > d_vars_set;
129
  std::vector< Node > d_extra_var;
130
public:
131
  bool isBaseMatchComplete();
132
public:
133
  QuantInfo();
134
  ~QuantInfo();
135
  std::vector< TNode > d_vars;
136
  std::vector< TypeNode > d_var_types;
137
  std::map< TNode, int > d_var_num;
138
  std::vector< int > d_tsym_vars;
139
  std::map< TNode, bool > d_inMatchConstraint;
140
8244581
  int getVarNum( TNode v ) { return d_var_num.find( v )!=d_var_num.end() ? d_var_num[v] : -1; }
141
440396
  bool isVar( TNode v ) { return d_var_num.find( v )!=d_var_num.end(); }
142
3153931
  int getNumVars() { return (int)d_vars.size(); }
143
426
  TNode getVar( int i ) { return d_vars[i]; }
144
145
  typedef std::map< int, MatchGen * > VarMgMap;
146
 private:
147
  /** The parent who owns this class */
148
  QuantConflictFind* d_parent;
149
  MatchGen * d_mg;
150
  VarMgMap d_var_mg;
151
 public:
152
5123589
  VarMgMap::const_iterator var_mg_find(int i) const { return d_var_mg.find(i); }
153
4601128
  VarMgMap::const_iterator var_mg_end() const { return d_var_mg.end(); }
154
1738542
  bool containsVarMg(int i) const { return var_mg_find(i) != var_mg_end(); }
155
156
228197
  bool matchGeneratorIsValid() const { return d_mg->isValid(); }
157
452918
  bool getNextMatch( QuantConflictFind * p ) {
158
452918
    return d_mg->getNextMatch(p, this);
159
  }
160
161
  Node d_q;
162
  bool reset_round( QuantConflictFind * p );
163
public:
164
  //initialize
165
  void initialize( QuantConflictFind * p, Node q, Node qn );
166
  //current constraints
167
  std::vector< TNode > d_match;
168
  std::vector< TNode > d_match_term;
169
  std::map< int, std::map< TNode, int > > d_curr_var_deq;
170
  std::map< Node, bool > d_tconstraints;
171
  int getCurrentRepVar( int v );
172
  TNode getCurrentValue( TNode n );
173
  TNode getCurrentExpValue( TNode n );
174
  bool getCurrentCanBeEqual( QuantConflictFind * p, int v, TNode n, bool chDiseq = false );
175
  int addConstraint( QuantConflictFind * p, int v, TNode n, bool polarity );
176
  int addConstraint( QuantConflictFind * p, int v, TNode n, int vn, bool polarity, bool doRemove );
177
  bool setMatch( QuantConflictFind * p, int v, TNode n, bool isGroundRep, bool isGround );
178
  void unsetMatch( QuantConflictFind * p, int v );
179
  bool isMatchSpurious( QuantConflictFind * p );
180
  bool isTConstraintSpurious( QuantConflictFind * p, std::vector< Node >& terms );
181
  bool entailmentTest( QuantConflictFind * p, Node lit, bool chEnt = true );
182
  bool completeMatch( QuantConflictFind * p, std::vector< int >& assigned, bool doContinue = false );
183
  void revertMatch( QuantConflictFind * p, std::vector< int >& assigned );
184
  void debugPrintMatch( const char * c );
185
  bool isConstrainedVar( int v );
186
public:
187
  void getMatch( std::vector< Node >& terms );
188
};
189
190
18486
class QuantConflictFind : public QuantifiersModule
191
{
192
  friend class MatchGen;
193
  friend class QuantInfo;
194
  typedef context::CDHashMap<Node, bool> NodeBoolMap;
195
196
 private:
197
  context::CDO< bool > d_conflict;
198
  std::map< Kind, Node > d_zero;
199
  //for storing nodes created during t-constraint solving (prevents memory leaks)
200
  std::vector< Node > d_tempCache;
201
  //optimization: list of quantifiers that depend on ground function applications
202
  std::map< TNode, std::vector< Node > > d_func_rel_dom;
203
  std::map< TNode, bool > d_irr_func;
204
  std::map< Node, bool > d_irr_quant;
205
  void setIrrelevantFunction( TNode f );
206
private:
207
  std::map< Node, Node > d_op_node;
208
  std::map< Node, int > d_fid;
209
public:  //for ground terms
210
  Node d_true;
211
  Node d_false;
212
  TNode getZero( Kind k );
213
private:
214
  std::map< Node, QuantInfo > d_qinfo;
215
private:  //for equivalence classes
216
  // type -> list(eqc)
217
  std::map< TypeNode, std::vector< TNode > > d_eqcs;
218
219
 public:
220
  enum Effort : unsigned {
221
    EFFORT_CONFLICT,
222
    EFFORT_PROP_EQ,
223
    EFFORT_INVALID,
224
  };
225
  void setEffort(Effort e) { d_effort = e; }
226
227
1923802
  inline bool atConflictEffort() const {
228
1923802
    return d_effort == QuantConflictFind::EFFORT_CONFLICT;
229
  }
230
231
 private:
232
  Effort d_effort;
233
234
 public:
235
  bool areMatchEqual( TNode n1, TNode n2 );
236
  bool areMatchDisequal( TNode n1, TNode n2 );
237
238
 public:
239
  QuantConflictFind(Env& env,
240
                    QuantifiersState& qs,
241
                    QuantifiersInferenceManager& qim,
242
                    QuantifiersRegistry& qr,
243
                    TermRegistry& tr);
244
245
  /** register quantifier */
246
  void registerQuantifier(Node q) override;
247
248
 public:
249
  /** needs check */
250
  bool needsCheck(Theory::Effort level) override;
251
  /** reset round */
252
  void reset_round(Theory::Effort level) override;
253
  /** check
254
   *
255
   * This method attempts to construct a conflicting or propagating instance.
256
   * If such an instance exists, then it makes a call to
257
   * Instantiation::addInstantiation.
258
   */
259
  void check(Theory::Effort level, QEffort quant_e) override;
260
261
 private:
262
  /** check quantified formula
263
   *
264
   * This method is called by the above check method for each quantified
265
   * formula q. It attempts to find a conflicting or propagating instance for
266
   * q, depending on the effort level (d_effort).
267
   *
268
   * isConflict: this is set to true if we discovered a conflicting instance.
269
   * This flag may be set instead of d_conflict if --qcf-all-conflict is true,
270
   * in which we continuing adding all conflicts.
271
   * addedLemmas: tracks the total number of lemmas added, and is incremented by
272
   * this method when applicable.
273
   */
274
  void checkQuantifiedFormula(Node q, bool& isConflict, unsigned& addedLemmas);
275
276
 private:
277
  void debugPrint( const char * c );
278
  //for debugging
279
  std::vector< Node > d_quants;
280
  std::map< Node, int > d_quant_id;
281
  void debugPrintQuant( const char * c, Node q );
282
  void debugPrintQuantBody( const char * c, Node q, Node n, bool doVarNum = true );
283
public:
284
  /** statistics class */
285
  class Statistics {
286
  public:
287
    IntStat d_inst_rounds;
288
    IntStat d_entailment_checks;
289
    Statistics();
290
  };
291
  Statistics d_statistics;
292
  /** Identify this module */
293
157823
  std::string identify() const override { return "QcfEngine"; }
294
  /** is n a propagating instance?
295
   *
296
   * A propagating instance is any formula that consists of Boolean connectives,
297
   * equality, quantified formulas, and terms that exist in the current
298
   * context (those in the master equality engine).
299
   *
300
   * Notice the distinction that quantified formulas that do not appear in the
301
   * current context are considered to be legal in propagating instances. This
302
   * choice is significant for TPTP, where a net of ~200 benchmarks are gained
303
   * due to this decision.
304
   *
305
   * Propagating instances are the second most useful kind of instantiation
306
   * after conflicting instances and are used as a second effort in the
307
   * algorithm performed by this class.
308
   */
309
  bool isPropagatingInstance(Node n) const;
310
};
311
312
std::ostream& operator<<(std::ostream& os, const QuantConflictFind::Effort& e);
313
314
}  // namespace quantifiers
315
}  // namespace theory
316
}  // namespace cvc5
317
318
#endif