1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Andrew Reynolds |
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 |
|
* Skolem definition manager. |
14 |
|
*/ |
15 |
|
|
16 |
|
#include "prop/skolem_def_manager.h" |
17 |
|
|
18 |
|
#include "expr/attribute.h" |
19 |
|
|
20 |
|
namespace cvc5 { |
21 |
|
namespace prop { |
22 |
|
|
23 |
6328 |
SkolemDefManager::SkolemDefManager(context::Context* context, |
24 |
6328 |
context::UserContext* userContext) |
25 |
6328 |
: d_skDefs(userContext), d_skActive(context) |
26 |
|
{ |
27 |
6328 |
} |
28 |
|
|
29 |
6325 |
SkolemDefManager::~SkolemDefManager() {} |
30 |
|
|
31 |
14805 |
void SkolemDefManager::notifySkolemDefinition(TNode skolem, Node def) |
32 |
|
{ |
33 |
|
// Notice that skolem may have kind SKOLEM or BOOLEAN_TERM_VARIABLE |
34 |
29610 |
Trace("sk-defs") << "notifySkolemDefinition: " << def << " for " << skolem |
35 |
14805 |
<< std::endl; |
36 |
|
// in very rare cases, a skolem may be generated twice for terms that are |
37 |
|
// equivalent up to purification |
38 |
14805 |
if (d_skDefs.find(skolem) == d_skDefs.end()) |
39 |
|
{ |
40 |
14802 |
d_skDefs.insert(skolem, def); |
41 |
|
} |
42 |
14805 |
} |
43 |
|
|
44 |
1434 |
TNode SkolemDefManager::getDefinitionForSkolem(TNode skolem) const |
45 |
|
{ |
46 |
1434 |
NodeNodeMap::const_iterator it = d_skDefs.find(skolem); |
47 |
1434 |
Assert(it != d_skDefs.end()) << "No skolem def for " << skolem; |
48 |
1434 |
return it->second; |
49 |
|
} |
50 |
|
|
51 |
3640985 |
void SkolemDefManager::notifyAsserted(TNode literal, |
52 |
|
std::vector<TNode>& activatedSkolems, |
53 |
|
bool useDefs) |
54 |
|
{ |
55 |
7281970 |
std::unordered_set<Node> skolems; |
56 |
3640985 |
getSkolems(literal, skolems); |
57 |
5296116 |
for (const Node& k : skolems) |
58 |
|
{ |
59 |
1655131 |
if (d_skActive.find(k) != d_skActive.end()) |
60 |
|
{ |
61 |
|
// already active |
62 |
1505889 |
continue; |
63 |
|
} |
64 |
149242 |
d_skActive.insert(k); |
65 |
149242 |
if (useDefs) |
66 |
|
{ |
67 |
|
// add its definition to the activated list |
68 |
149242 |
NodeNodeMap::const_iterator it = d_skDefs.find(k); |
69 |
149242 |
Assert(it != d_skDefs.end()); |
70 |
149242 |
activatedSkolems.push_back(it->second); |
71 |
|
} |
72 |
|
else |
73 |
|
{ |
74 |
|
// add to the activated list |
75 |
|
activatedSkolems.push_back(k); |
76 |
|
} |
77 |
|
} |
78 |
3640985 |
} |
79 |
|
|
80 |
|
struct HasSkolemTag |
81 |
|
{ |
82 |
|
}; |
83 |
|
struct HasSkolemComputedTag |
84 |
|
{ |
85 |
|
}; |
86 |
|
/** Attribute true for nodes with skolems in them */ |
87 |
|
typedef expr::Attribute<HasSkolemTag, bool> HasSkolemAttr; |
88 |
|
/** Attribute true for nodes where we have computed the above attribute */ |
89 |
|
typedef expr::Attribute<HasSkolemComputedTag, bool> HasSkolemComputedAttr; |
90 |
|
|
91 |
9931450 |
bool SkolemDefManager::hasSkolems(TNode n) const |
92 |
|
{ |
93 |
19862900 |
std::unordered_set<TNode> visited; |
94 |
9931450 |
std::unordered_set<TNode>::iterator it; |
95 |
19862900 |
std::vector<TNode> visit; |
96 |
19862900 |
TNode cur; |
97 |
9931450 |
visit.push_back(n); |
98 |
1441088 |
do |
99 |
|
{ |
100 |
11372538 |
cur = visit.back(); |
101 |
21634378 |
if (cur.getAttribute(HasSkolemComputedAttr())) |
102 |
|
{ |
103 |
10261840 |
visit.pop_back(); |
104 |
|
// already computed |
105 |
10261840 |
continue; |
106 |
|
} |
107 |
1110698 |
it = visited.find(cur); |
108 |
1110698 |
if (it == visited.end()) |
109 |
|
{ |
110 |
601147 |
visited.insert(cur); |
111 |
601147 |
if (cur.getNumChildren() == 0) |
112 |
|
{ |
113 |
91596 |
visit.pop_back(); |
114 |
91596 |
bool hasSkolem = false; |
115 |
91596 |
if (cur.isVar()) |
116 |
|
{ |
117 |
63410 |
hasSkolem = (d_skDefs.find(cur) != d_skDefs.end()); |
118 |
|
} |
119 |
91596 |
cur.setAttribute(HasSkolemAttr(), hasSkolem); |
120 |
91596 |
cur.setAttribute(HasSkolemComputedAttr(), true); |
121 |
|
} |
122 |
|
else |
123 |
|
{ |
124 |
509551 |
visit.insert(visit.end(), cur.begin(), cur.end()); |
125 |
|
} |
126 |
|
} |
127 |
|
else |
128 |
|
{ |
129 |
509551 |
visit.pop_back(); |
130 |
509551 |
bool hasSkolem = false; |
131 |
1316336 |
for (TNode i : cur) |
132 |
|
{ |
133 |
883048 |
Assert(i.getAttribute(HasSkolemComputedAttr())); |
134 |
883048 |
if (i.getAttribute(HasSkolemAttr())) |
135 |
|
{ |
136 |
76263 |
hasSkolem = true; |
137 |
76263 |
break; |
138 |
|
} |
139 |
|
} |
140 |
509551 |
cur.setAttribute(HasSkolemAttr(), hasSkolem); |
141 |
509551 |
cur.setAttribute(HasSkolemComputedAttr(), true); |
142 |
|
} |
143 |
11372538 |
} while (!visit.empty()); |
144 |
9931450 |
Assert(n.getAttribute(HasSkolemComputedAttr())); |
145 |
19862900 |
return n.getAttribute(HasSkolemAttr()); |
146 |
|
} |
147 |
|
|
148 |
3642913 |
void SkolemDefManager::getSkolems(TNode n, |
149 |
|
std::unordered_set<Node>& skolems) const |
150 |
|
{ |
151 |
7285826 |
std::unordered_set<TNode> visited; |
152 |
3642913 |
std::unordered_set<TNode>::iterator it; |
153 |
7285826 |
std::vector<TNode> visit; |
154 |
7285826 |
TNode cur; |
155 |
3642913 |
visit.push_back(n); |
156 |
6288537 |
do |
157 |
|
{ |
158 |
9931450 |
cur = visit.back(); |
159 |
9931450 |
visit.pop_back(); |
160 |
9931450 |
if (!hasSkolems(cur)) |
161 |
|
{ |
162 |
|
// does not have skolems, continue |
163 |
4249150 |
continue; |
164 |
|
} |
165 |
5682300 |
it = visited.find(cur); |
166 |
5682300 |
if (it == visited.end()) |
167 |
|
{ |
168 |
5288537 |
visited.insert(cur); |
169 |
5288537 |
if (cur.isVar()) |
170 |
|
{ |
171 |
1656565 |
if (d_skDefs.find(cur) != d_skDefs.end()) |
172 |
|
{ |
173 |
1656565 |
skolems.insert(cur); |
174 |
|
} |
175 |
|
} |
176 |
|
else |
177 |
|
{ |
178 |
3631972 |
visit.insert(visit.end(), cur.begin(), cur.end()); |
179 |
|
} |
180 |
|
} |
181 |
9931450 |
} while (!visit.empty()); |
182 |
3642913 |
} |
183 |
|
|
184 |
|
} // namespace prop |
185 |
22746 |
} // namespace cvc5 |