1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Andrew Reynolds, Paul Meng, Tim King |
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 |
|
* Extension to Sets theory. |
14 |
|
*/ |
15 |
|
|
16 |
|
#ifndef SRC_THEORY_SETS_THEORY_SETS_RELS_H_ |
17 |
|
#define SRC_THEORY_SETS_THEORY_SETS_RELS_H_ |
18 |
|
|
19 |
|
#include <unordered_set> |
20 |
|
|
21 |
|
#include "context/cdhashset.h" |
22 |
|
#include "context/cdlist.h" |
23 |
|
#include "theory/sets/inference_manager.h" |
24 |
|
#include "theory/sets/rels_utils.h" |
25 |
|
#include "theory/sets/solver_state.h" |
26 |
|
#include "theory/sets/term_registry.h" |
27 |
|
#include "theory/theory.h" |
28 |
|
#include "theory/uf/equality_engine.h" |
29 |
|
|
30 |
|
namespace cvc5 { |
31 |
|
namespace theory { |
32 |
|
namespace sets { |
33 |
|
|
34 |
|
class TheorySetsPrivate; |
35 |
|
|
36 |
52608 |
class TupleTrie { |
37 |
|
public: |
38 |
|
/** the data */ |
39 |
|
std::map< Node, TupleTrie > d_data; |
40 |
|
public: |
41 |
|
std::vector<Node> findTerms( std::vector< Node >& reps, int argIndex = 0 ); |
42 |
|
std::vector<Node> findSuccessors( std::vector< Node >& reps, int argIndex = 0 ); |
43 |
|
Node existsTerm( std::vector< Node >& reps, int argIndex = 0 ); |
44 |
|
bool addTerm( Node n, std::vector< Node >& reps, int argIndex = 0 ); |
45 |
|
void debugPrint( const char * c, Node n, unsigned depth = 0 ); |
46 |
9290 |
void clear() { d_data.clear(); } |
47 |
|
};/* class TupleTrie */ |
48 |
|
|
49 |
|
/** The relations extension of the theory of sets |
50 |
|
* |
51 |
|
* This class implements inference schemes described in Meng et al. CADE 2017 |
52 |
|
* for handling quantifier-free constraints in the theory of relations. |
53 |
|
* |
54 |
|
* In cvc5, relations are represented as sets of tuples. The theory of |
55 |
|
* relations includes constraints over operators, e.g. TRANSPOSE, JOIN and so |
56 |
|
* on, which apply to sets of tuples. |
57 |
|
* |
58 |
|
* Since relations are a special case of sets, this class is implemented as an |
59 |
|
* extension of the theory of sets. That is, it shares many components of the |
60 |
|
* TheorySets object which owns it. |
61 |
|
*/ |
62 |
|
class TheorySetsRels { |
63 |
|
typedef context::CDList<Node> NodeList; |
64 |
|
typedef context::CDHashSet<Node> NodeSet; |
65 |
|
typedef context::CDHashMap<Node, Node> NodeMap; |
66 |
|
|
67 |
|
public: |
68 |
|
TheorySetsRels(SolverState& s, |
69 |
|
InferenceManager& im, |
70 |
|
SkolemCache& skc, |
71 |
|
TermRegistry& treg); |
72 |
|
|
73 |
|
~TheorySetsRels(); |
74 |
|
/** |
75 |
|
* Invoke the check method with effort level e. At a high level, this class |
76 |
|
* will make calls to TheorySetsPrivate::processInference to assert facts, |
77 |
|
* lemmas, and conflicts. If this class makes no such call, then the current |
78 |
|
* set of assertions is satisfiable with respect to relations. |
79 |
|
*/ |
80 |
|
void check(Theory::Effort e); |
81 |
|
/** Is kind k a kind that belongs to the relation theory? */ |
82 |
|
static bool isRelationKind(Kind k); |
83 |
|
|
84 |
|
private: |
85 |
|
/** True and false constant nodes */ |
86 |
|
Node d_trueNode; |
87 |
|
Node d_falseNode; |
88 |
|
|
89 |
|
/** Reference to the state object for the theory of sets */ |
90 |
|
SolverState& d_state; |
91 |
|
/** Reference to the inference manager for the theory of sets */ |
92 |
|
InferenceManager& d_im; |
93 |
|
/** Reference to the skolem cache */ |
94 |
|
SkolemCache& d_skCache; |
95 |
|
/** Reference to the term registry */ |
96 |
|
TermRegistry& d_treg; |
97 |
|
NodeSet d_shared_terms; |
98 |
|
|
99 |
|
std::unordered_set<Node> d_rel_nodes; |
100 |
|
std::map< Node, std::vector<Node> > d_tuple_reps; |
101 |
|
std::map< Node, TupleTrie > d_membership_trie; |
102 |
|
|
103 |
|
/** Symbolic tuple variables that has been reduced to concrete ones */ |
104 |
|
std::unordered_set<Node> d_symbolic_tuples; |
105 |
|
|
106 |
|
/** Mapping between relation and its member representatives */ |
107 |
|
std::map< Node, std::vector< Node > > d_rReps_memberReps_cache; |
108 |
|
|
109 |
|
/** Mapping between relation and its member representatives explanation */ |
110 |
|
std::map< Node, std::vector< Node > > d_rReps_memberReps_exp_cache; |
111 |
|
|
112 |
|
/** Mapping between a relation representative and its equivalent relations involving relational operators */ |
113 |
|
std::map< Node, std::map<kind::Kind_t, std::vector<Node> > > d_terms_cache; |
114 |
|
|
115 |
|
/** Mapping between transitive closure relation TC(r) and its TC graph constructed based on the members of r*/ |
116 |
|
std::map<Node, std::map<Node, std::unordered_set<Node> > > d_rRep_tcGraph; |
117 |
|
std::map<Node, std::map<Node, std::unordered_set<Node> > > d_tcr_tcGraph; |
118 |
|
std::map< Node, std::map< Node, Node > > d_tcr_tcGraph_exps; |
119 |
|
|
120 |
|
private: |
121 |
|
/** Send infer |
122 |
|
* |
123 |
|
* Called when we have inferred fact from explanation reason, where the |
124 |
|
* latter should be a conjunction of facts that hold in the current context. |
125 |
|
* |
126 |
|
* This method adds the node (=> reason exp) to the pending vector d_pending. |
127 |
|
*/ |
128 |
|
void sendInfer(Node fact, InferenceId id, Node reason); |
129 |
|
/** |
130 |
|
* This method flushes the inferences in the pending vector d_pending to |
131 |
|
* theory of sets, which may process them as lemmas or as facts to assert to |
132 |
|
* the equality engine. |
133 |
|
*/ |
134 |
|
void doPendingInfers(); |
135 |
|
/** Process inference |
136 |
|
* |
137 |
|
* A wrapper around d_im.assertInference that ensures that we do not send |
138 |
|
* inferences with explanations that are not entailed. |
139 |
|
*/ |
140 |
|
void processInference(Node conc, InferenceId id, Node exp); |
141 |
|
|
142 |
|
/** Methods used in full effort */ |
143 |
|
void check(); |
144 |
|
void collectRelsInfo(); |
145 |
|
void applyTransposeRule( std::vector<Node> tp_terms ); |
146 |
|
void applyTransposeRule( Node rel, Node rel_rep, Node exp ); |
147 |
|
void applyProductRule( Node rel, Node rel_rep, Node exp ); |
148 |
|
void applyJoinRule( Node rel, Node rel_rep, Node exp); |
149 |
|
void applyJoinImageRule( Node mem_rep, Node rel_rep, Node exp); |
150 |
|
void applyIdenRule( Node mem_rep, Node rel_rep, Node exp); |
151 |
|
void applyTCRule( Node mem, Node rel, Node rel_rep, Node exp); |
152 |
|
void buildTCGraphForRel( Node tc_rel ); |
153 |
|
void doTCInference(); |
154 |
|
void doTCInference(std::map<Node, std::unordered_set<Node> > rel_tc_graph, |
155 |
|
std::map<Node, Node> rel_tc_graph_exps, |
156 |
|
Node tc_rel); |
157 |
|
void doTCInference(Node tc_rel, |
158 |
|
std::vector<Node> reasons, |
159 |
|
std::map<Node, std::unordered_set<Node> >& tc_graph, |
160 |
|
std::map<Node, Node>& rel_tc_graph_exps, |
161 |
|
Node start_node_rep, |
162 |
|
Node cur_node_rep, |
163 |
|
std::unordered_set<Node>& seen); |
164 |
|
|
165 |
|
void composeMembersForRels( Node ); |
166 |
|
void computeMembersForBinOpRel( Node ); |
167 |
|
void computeMembersForIdenTerm( Node ); |
168 |
|
void computeMembersForUnaryOpRel( Node ); |
169 |
|
void computeMembersForJoinImageTerm( Node ); |
170 |
|
|
171 |
|
bool isTCReachable( Node mem_rep, Node tc_rel ); |
172 |
|
void isTCReachable(Node start, |
173 |
|
Node dest, |
174 |
|
std::unordered_set<Node>& hasSeen, |
175 |
|
std::map<Node, std::unordered_set<Node> >& tc_graph, |
176 |
|
bool& isReachable); |
177 |
|
|
178 |
|
/** Helper functions */ |
179 |
|
bool hasTerm( Node a ); |
180 |
|
void makeSharedTerm(Node, TypeNode t); |
181 |
|
void reduceTupleVar( Node ); |
182 |
|
bool hasMember( Node, Node ); |
183 |
|
void computeTupleReps( Node ); |
184 |
|
bool areEqual( Node a, Node b ); |
185 |
|
Node getRepresentative( Node t ); |
186 |
|
inline void addToMembershipDB( Node, Node, Node ); |
187 |
|
inline Node constructPair(Node tc_rep, Node a, Node b); |
188 |
|
bool safelyAddToMap( std::map< Node, std::vector<Node> >&, Node, Node ); |
189 |
|
bool isRel( Node n ) {return n.getType().isSet() && n.getType().getSetElementType().isTuple();} |
190 |
|
}; |
191 |
|
|
192 |
|
} // namespace sets |
193 |
|
} // namespace theory |
194 |
|
} // namespace cvc5 |
195 |
|
|
196 |
|
#endif /* SRC_THEORY_SETS_THEORY_SETS_RELS_H_ */ |