summaryrefslogtreecommitdiff
path: root/src/theory/quantifiers/inst_propagator.h
blob: 1ba359228339694524f1825b71e10215bac59d46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*********************                                                        */
/*! \file inst_propagator.h
 ** \verbatim
 ** Top contributors (to current version):
 **   Andrew Reynolds, Mathias Preiner
 ** This file is part of the CVC4 project.
 ** Copyright (c) 2009-2018 by the authors listed in the file AUTHORS
 ** in the top-level source directory) and their institutional affiliations.
 ** All rights reserved.  See the file COPYING in the top-level source
 ** directory for licensing information.\endverbatim
 **
 ** \brief Propagate mechanism for instantiations
 **/

#include "cvc4_private.h"

#ifndef __CVC4__QUANTIFIERS_INST_PROPAGATOR_H
#define __CVC4__QUANTIFIERS_INST_PROPAGATOR_H

#include <iostream>
#include <map>
#include <string>
#include <vector>
#include "expr/node.h"
#include "expr/node_trie.h"
#include "expr/type_node.h"
#include "theory/quantifiers/instantiate.h"
#include "theory/quantifiers_engine.h"

namespace CVC4 {
namespace theory {
namespace quantifiers {

class EqualityQueryInstProp : public EqualityQuery {
private:
  /** pointer to quantifiers engine */
  QuantifiersEngine* d_qe;
public:
  EqualityQueryInstProp( QuantifiersEngine* qe );
  ~EqualityQueryInstProp(){};
  /** reset */
  bool reset(Theory::Effort e) override;
  /* Called for new quantifiers */
  void registerQuantifier(Node q) override {}
  /** identify */
  std::string identify() const override { return "EqualityQueryInstProp"; }
  /** extends engine */
  bool extendsEngine() override { return true; }
  /** contains term */
  bool hasTerm(Node a) override;
  /** get the representative of the equivalence class of a */
  Node getRepresentative(Node a) override;
  /** returns true if a and b are equal in the current context */
  bool areEqual(Node a, Node b) override;
  /** returns true is a and b are disequal in the current context */
  bool areDisequal(Node a, Node b) override;
  /** get the equality engine associated with this query */
  eq::EqualityEngine* getEngine() override;
  /** get the equivalence class of a */
  void getEquivalenceClass(Node a, std::vector<Node>& eqc) override;
  /** get congruent term */
  TNode getCongruentTerm(Node f, std::vector<TNode>& args) override;

 public:
  /** get the representative of the equivalence class of a, with explanation */
  Node getRepresentativeExp( Node a, std::vector< Node >& exp );
  /** returns true if a and b are equal in the current context */
  bool areEqualExp( Node a, Node b, std::vector< Node >& exp );
  /** returns true is a and b are disequal in the current context */
  bool areDisequalExp( Node a, Node b, std::vector< Node >& exp );
  /** get congruent term */
  TNode getCongruentTermExp( Node f, std::vector< TNode >& args, std::vector< Node >& exp );
private:
  /** term index */
 std::map<Node, TNodeTrie> d_uf_func_map_trie;
 /** union find for terms beyond what is stored in equality engine */
 std::map<Node, Node> d_uf;
 std::map<Node, std::vector<Node> > d_uf_exp;
 Node getUfRepresentative(Node a, std::vector<Node>& exp);
 /** disequality list, stores explanations */
 std::map<Node, std::map<Node, std::vector<Node> > > d_diseq_list;
 /** add arg */
 void addArgument(Node n,
                  std::vector<Node>& args,
                  std::vector<Node>& watch,
                  bool is_watch);
 /** register term */
 void registerUfTerm(TNode n);

public:
  enum {
    STATUS_CONFLICT,
    STATUS_MERGED_KNOWN,
    STATUS_MERGED_UNKNOWN,
    STATUS_NONE,
  };
  /** set equal */
  int setEqual( Node& a, Node& b, bool pol, std::vector< Node >& reason );
  Node d_true;
  Node d_false;
public:
  //for explanations
  static void merge_exp( std::vector< Node >& v, std::vector< Node >& v_to_merge, int up_to_size = -1 );
  //for watch list
  static void setWatchList( Node n, std::vector< Node >& watch, std::map< Node, std::vector< Node > >& watch_list_out );
  static void collectWatchList( Node n, std::map< Node, std::vector< Node > >& watch_list_out, std::vector< Node >& watch_list );

  Node evaluateTermExp( Node n, std::vector< Node >& exp, std::map< int, std::map< Node, Node > >& visited,
                        bool hasPol, bool pol, std::map< Node, std::vector< Node > >& watch_list_out, std::vector< Node >& props );
  bool isPropagateLiteral( Node n );
};

class InstPropagator : public QuantifiersUtil {
private:
  /** pointer to quantifiers engine */
  QuantifiersEngine* d_qe;
  /** notify class */
  class InstantiationNotifyInstPropagator : public InstantiationNotify {
    InstPropagator& d_ip;
  public:
    InstantiationNotifyInstPropagator(InstPropagator& ip): d_ip(ip) {}
    bool notifyInstantiation(QuantifiersModule::QEffort quant_e,
                             Node q,
                             Node lem,
                             std::vector<Node>& terms,
                             Node body) override
    {
      return d_ip.notifyInstantiation( quant_e, q, lem, terms, body );
    }
    void filterInstantiations() override { d_ip.filterInstantiations(); }
  };
  InstantiationNotifyInstPropagator d_notify;
  /** notify instantiation method */
  bool notifyInstantiation(QuantifiersModule::QEffort quant_e,
                           Node q,
                           Node lem,
                           std::vector<Node>& terms,
                           Node body);
  /** remove instance ids */
  void filterInstantiations();  
  /** allocate instantiation */
  unsigned allocateInstantiation( Node q, Node lem, std::vector< Node >& terms, Node body );
  /** equality query */
  EqualityQueryInstProp d_qy;
  class InstInfo {
  public:
    bool d_active;
    Node d_q;
    Node d_lem;
    std::vector< Node > d_terms;
    // the current entailed body
    Node d_curr;
    //explanation for current entailed body
    std::vector< Node > d_curr_exp;
    void init( Node q, Node lem, std::vector< Node >& terms, Node body );
  };
  /** instantiation count/info */
  unsigned d_icount;
  std::map< unsigned, InstInfo > d_ii;
  std::map< Node, unsigned > d_conc_to_id[2];
  /** are we in conflict */
  bool d_conflict;
  /** watch list */
  std::map< Node, std::map< unsigned, bool > > d_watch_list;
  /** update list */
  std::vector< unsigned > d_update_list;
  /** relevant instances */
  std::map< unsigned, bool > d_relevant_inst;
  bool d_has_relevant_inst;
private:
  bool update( unsigned id, InstInfo& i, bool firstTime = false );
  void propagate( Node a, Node b, bool pol, std::vector< Node >& exp );
  void conflict( std::vector< Node >& exp );
  bool cacheConclusion( unsigned id, Node body, int prop_index = 0 );
  void addRelevantInstances( std::vector< Node >& exp, const char * c );
  void debugPrintExplanation( std::vector< Node >& exp, const char * c );
public:
  InstPropagator( QuantifiersEngine* qe );
  ~InstPropagator(){}
  /** reset */
  bool reset(Theory::Effort e) override;
  /* Called for new quantifiers */
  void registerQuantifier(Node q) override {}
  /** identify */
  std::string identify() const override { return "InstPropagator"; }
  /** get the notify mechanism */
  InstantiationNotify* getInstantiationNotify() { return &d_notify; }
};

}
}
}

#endif
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback