GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/datatypes/theory_datatypes.h Lines: 15 17 88.2 %
Date: 2021-05-22 Branches: 19 40 47.5 %

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
 * Theory of datatypes.
14
 */
15
16
#include "cvc5_private.h"
17
18
#ifndef CVC5__THEORY__DATATYPES__THEORY_DATATYPES_H
19
#define CVC5__THEORY__DATATYPES__THEORY_DATATYPES_H
20
21
#include <iostream>
22
#include <map>
23
24
#include "context/cdlist.h"
25
#include "expr/attribute.h"
26
#include "expr/node_trie.h"
27
#include "theory/datatypes/datatypes_rewriter.h"
28
#include "theory/datatypes/inference_manager.h"
29
#include "theory/datatypes/proof_checker.h"
30
#include "theory/datatypes/sygus_extension.h"
31
#include "theory/theory.h"
32
#include "theory/theory_eq_notify.h"
33
#include "theory/theory_state.h"
34
#include "theory/uf/equality_engine.h"
35
#include "util/hash.h"
36
37
namespace cvc5 {
38
namespace theory {
39
namespace datatypes {
40
41
class TheoryDatatypes : public Theory {
42
 private:
43
  typedef context::CDList<Node> NodeList;
44
  /** maps nodes to an index in a vector */
45
  typedef context::CDHashMap<Node, size_t> NodeUIntMap;
46
  typedef context::CDHashMap<Node, bool> BoolMap;
47
  typedef context::CDHashMap<Node, Node> NodeMap;
48
49
 private:
50
  //notification class for equality engine
51
9459
  class NotifyClass : public TheoryEqNotifyClass
52
  {
53
    TheoryDatatypes& d_dt;
54
  public:
55
9459
   NotifyClass(TheoryInferenceManager& im, TheoryDatatypes& dt)
56
9459
       : TheoryEqNotifyClass(im), d_dt(dt)
57
   {
58
9459
   }
59
282074
   void eqNotifyNewClass(TNode t) override
60
   {
61
282074
     Debug("dt") << "NotifyClass::eqNotifyNewClass(" << t << ")" << std::endl;
62
282074
     d_dt.eqNotifyNewClass(t);
63
282074
    }
64
2270412
    void eqNotifyMerge(TNode t1, TNode t2) override
65
    {
66
4540824
      Debug("dt") << "NotifyClass::eqNotifyMerge(" << t1 << ", " << t2 << ")"
67
2270412
                  << std::endl;
68
2270412
      d_dt.eqNotifyMerge(t1, t2);
69
2270412
    }
70
  };/* class TheoryDatatypes::NotifyClass */
71
private:
72
  /** equivalence class info
73
   * d_inst is whether the instantiate rule has been applied,
74
   * d_constructor is a node of kind APPLY_CONSTRUCTOR (if any) in this equivalence class,
75
   * d_selectors is whether a selector has been applied to this equivalence class.
76
   */
77
  class EqcInfo
78
  {
79
  public:
80
    EqcInfo( context::Context* c );
81
23862
    ~EqcInfo(){}
82
    //whether we have instantiatied this eqc
83
    context::CDO< bool > d_inst;
84
    //constructor equal to this eqc
85
    context::CDO< Node > d_constructor;
86
    //all selectors whose argument is this eqc
87
    context::CDO< bool > d_selectors;
88
  };
89
  /** does eqc of n have a label (do we know its constructor)? */
90
  bool hasLabel( EqcInfo* eqc, Node n );
91
  /** get the label associated to n */
92
  Node getLabel( Node n );
93
  /** get the index of the label associated to n */
94
  int getLabelIndex( EqcInfo* eqc, Node n );
95
  /** does eqc of n have any testers? */
96
  bool hasTester( Node n );
97
  /** get the possible constructors for n */
98
  void getPossibleCons( EqcInfo* eqc, Node n, std::vector< bool >& cons );
99
  /** skolems for terms */
100
  NodeMap d_term_sk;
101
  Node getTermSkolemFor( Node n );
102
private:
103
  /** information necessary for equivalence classes */
104
  std::map< Node, EqcInfo* > d_eqc_info;
105
  //---------------------------------labels
106
  /** labels for each equivalence class
107
   *
108
   * For each eqc r, d_labels[r] is testers that hold for this equivalence
109
   * class, either:
110
   * a list of equations of the form
111
   *   NOT is_[constructor_1]( t1 )...NOT is_[constructor_n]( tn ), each of
112
   *   which are unique testers, n is less than the number of possible
113
   *   constructors for t minus one,
114
   * or a list of equations of the form
115
   *   NOT is_[constructor_1]( t1 )...NOT is_[constructor_n]( tn ) followed by
116
   *   is_[constructor_(n+1)]( t{n+1} ), each of which is a unique tester.
117
   * In both cases, t1, ..., tn, t{n+1} are terms in the equivalence class of r.
118
   *
119
   * We store this list in a context-dependent way, using the four data
120
   * structures below. The three vectors d_labels_data, d_labels_args, and
121
   * d_labels_tindex store the tester applications, their arguments and the
122
   * tester index of the application. The map d_labels stores the number of
123
   * values in these vectors that is valid in the current context (this is an
124
   * optimization that ensures we don't need to pop data when changing SAT
125
   * contexts).
126
   */
127
  NodeUIntMap d_labels;
128
  /** the tester applications */
129
  std::map< Node, std::vector< Node > > d_labels_data;
130
  /** the argument of each node in d_labels_data */
131
  std::map<Node, std::vector<Node> > d_labels_args;
132
  /** the tester index of each node in d_labels_data */
133
  std::map<Node, std::vector<unsigned> > d_labels_tindex;
134
  //---------------------------------end labels
135
  /** selector apps for eqch equivalence class */
136
  NodeUIntMap d_selector_apps;
137
  std::map< Node, std::vector< Node > > d_selector_apps_data;
138
  /** The conflict node */
139
  Node d_conflictNode;
140
  /**
141
   * SAT-context dependent cache for which terms we have called
142
   * collectTerms(...) on.
143
   */
144
  BoolMap d_collectTermsCache;
145
  /**
146
   * User-context dependent cache for which terms we have called
147
   * collectTerms(...) on.
148
   */
149
  BoolMap d_collectTermsCacheU;
150
  /** All the function terms that the theory has seen */
151
  context::CDList<TNode> d_functionTerms;
152
  /** counter for forcing assignments (ensures fairness) */
153
  unsigned d_dtfCounter;
154
  /** uninterpreted constant to variable map */
155
  std::map< Node, Node > d_uc_to_fresh_var;
156
private:
157
  /** singleton lemmas (for degenerate co-datatype case) */
158
  std::map< TypeNode, Node > d_singleton_lemma[2];
159
  /** Cache for singleton equalities processed */
160
  BoolMap d_singleton_eq;
161
  /** list of all lemmas produced */
162
  BoolMap d_lemmas_produced_c;
163
private:
164
  /** assert fact */
165
  void assertFact( Node fact, Node exp );
166
167
  /** get or make eqc info */
168
  EqcInfo* getOrMakeEqcInfo( TNode n, bool doMake = false );
169
170
  /** has eqc info */
171
2416559
  bool hasEqcInfo( TNode n ) { return d_labels.find( n )!=d_labels.end(); }
172
173
  /** get eqc constructor */
174
  TNode getEqcConstructor( TNode r );
175
176
 protected:
177
  void addCarePairs(TNodeTrie* t1,
178
                    TNodeTrie* t2,
179
                    unsigned arity,
180
                    unsigned depth,
181
                    unsigned& n_pairs);
182
  /** compute care graph */
183
  void computeCareGraph() override;
184
185
 public:
186
  TheoryDatatypes(context::Context* c,
187
                  context::UserContext* u,
188
                  OutputChannel& out,
189
                  Valuation valuation,
190
                  const LogicInfo& logicInfo,
191
                  ProofNodeManager* pnm = nullptr);
192
  ~TheoryDatatypes();
193
194
  //--------------------------------- initialization
195
  /** get the official theory rewriter of this theory */
196
  TheoryRewriter* getTheoryRewriter() override;
197
  /** get the proof checker of this theory */
198
  ProofRuleChecker* getProofChecker() override;
199
  /**
200
   * Returns true if we need an equality engine. If so, we initialize the
201
   * information regarding how it should be setup. For details, see the
202
   * documentation in Theory::needsEqualityEngine.
203
   */
204
  bool needsEqualityEngine(EeSetupInfo& esi) override;
205
  /** finish initialization */
206
  void finishInit() override;
207
  //--------------------------------- end initialization
208
  /** propagate */
209
  bool propagateLit(TNode literal);
210
  /** Conflict when merging two constants */
211
  void conflict(TNode a, TNode b);
212
  /** explain */
213
  TrustNode explain(TNode literal) override;
214
  /** called when a new equivalance class is created */
215
  void eqNotifyNewClass(TNode t);
216
  /** called when two equivalance classes have merged */
217
  void eqNotifyMerge(TNode t1, TNode t2);
218
219
  //--------------------------------- standard check
220
  /** Do we need a check call at last call effort? */
221
  bool needsCheckLastEffort() override;
222
  /** Pre-check, called before the fact queue of the theory is processed. */
223
  bool preCheck(Effort level) override;
224
  /** Post-check, called after the fact queue of the theory is processed. */
225
  void postCheck(Effort level) override;
226
  /** Notify fact */
227
  void notifyFact(TNode atom, bool pol, TNode fact, bool isInternal) override;
228
  //--------------------------------- end standard check
229
  void preRegisterTerm(TNode n) override;
230
  TrustNode ppRewrite(TNode n, std::vector<SkolemLemma>& lems) override;
231
  EqualityStatus getEqualityStatus(TNode a, TNode b) override;
232
  std::string identify() const override
233
  {
234
    return std::string("TheoryDatatypes");
235
  }
236
  /** debug print */
237
  void printModelDebug( const char* c );
238
  /** entailment check */
239
  std::pair<bool, Node> entailmentCheck(TNode lit) override;
240
241
 private:
242
  /** add tester to equivalence class info */
243
  void addTester(unsigned ttindex, Node t, EqcInfo* eqc, Node n, Node t_arg);
244
  /** add selector to equivalence class info */
245
  void addSelector( Node s, EqcInfo* eqc, Node n, bool assertFacts = true );
246
  /** add constructor */
247
  void addConstructor( Node c, EqcInfo* eqc, Node n );
248
  /** merge the equivalence class info of t1 and t2 */
249
  void merge( Node t1, Node t2 );
250
  /** collapse selector, s is of the form sel( n ) where n = c */
251
  void collapseSelector( Node s, Node c );
252
  /** for checking if cycles exist */
253
  void checkCycles();
254
  Node searchForCycle(TNode n,
255
                      TNode on,
256
                      std::map<TNode, bool>& visited,
257
                      std::map<TNode, bool>& proc,
258
                      std::vector<Node>& explanation,
259
                      bool firstTime = true);
260
  /** for checking whether two codatatype terms must be equal */
261
  void separateBisimilar(std::vector<Node>& part,
262
                         std::vector<std::vector<Node> >& part_out,
263
                         std::vector<Node>& exp,
264
                         std::map<Node, Node>& cn,
265
                         std::map<Node, std::map<Node, int> >& dni,
266
                         int dniLvl,
267
                         bool mkExp);
268
  /** build model */
269
  Node getCodatatypesValue( Node n, std::map< Node, Node >& eqc_cons, std::map< Node, int >& vmap, int depth );
270
  /** get singleton lemma */
271
  Node getSingletonLemma( TypeNode tn, bool pol );
272
  /** collect terms */
273
  void collectTerms( Node n );
274
  /** get instantiate cons */
275
  Node getInstantiateCons(Node n, const DType& dt, int index);
276
  /** check instantiate */
277
  void instantiate( EqcInfo* eqc, Node n );
278
private:
279
  //equality queries
280
  bool hasTerm( TNode a );
281
  bool areEqual( TNode a, TNode b );
282
  bool areDisequal( TNode a, TNode b );
283
  bool areCareDisequal( TNode x, TNode y );
284
  TNode getRepresentative( TNode a );
285
286
  /** Collect model values in m based on the relevant terms given by termSet */
287
  bool collectModelValues(TheoryModel* m,
288
                          const std::set<Node>& termSet) override;
289
  /**
290
   * Compute relevant terms. This includes datatypes in non-singleton
291
   * equivalence classes.
292
   */
293
  void computeRelevantTerms(std::set<Node>& termSet) override;
294
  /** Commonly used terms */
295
  Node d_true;
296
  Node d_zero;
297
  /** sygus symmetry breaking utility */
298
  std::unique_ptr<SygusExtension> d_sygusExtension;
299
  /** The theory rewriter for this theory. */
300
  DatatypesRewriter d_rewriter;
301
  /** A (default) theory state object */
302
  TheoryState d_state;
303
  /** The inference manager */
304
  InferenceManager d_im;
305
  /** The notify class */
306
  NotifyClass d_notify;
307
  /** Proof checker for datatypes */
308
  DatatypesProofRuleChecker d_checker;
309
};/* class TheoryDatatypes */
310
311
}  // namespace datatypes
312
}  // namespace theory
313
}  // namespace cvc5
314
315
#endif /* CVC5__THEORY__DATATYPES__THEORY_DATATYPES_H */