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 |
9905 |
SkolemDefManager::SkolemDefManager(context::Context* context, |
24 |
9905 |
context::UserContext* userContext) |
25 |
9905 |
: d_skDefs(userContext), d_skActive(context) |
26 |
|
{ |
27 |
9905 |
} |
28 |
|
|
29 |
9905 |
SkolemDefManager::~SkolemDefManager() {} |
30 |
|
|
31 |
29893 |
void SkolemDefManager::notifySkolemDefinition(TNode skolem, Node def) |
32 |
|
{ |
33 |
|
// Notice that skolem may have kind SKOLEM or BOOLEAN_TERM_VARIABLE |
34 |
59786 |
Trace("sk-defs") << "notifySkolemDefinition: " << def << " for " << skolem |
35 |
29893 |
<< std::endl; |
36 |
|
// in very rare cases, a skolem may be generated twice for terms that are |
37 |
|
// equivalent up to purification |
38 |
29893 |
if (d_skDefs.find(skolem) == d_skDefs.end()) |
39 |
|
{ |
40 |
29880 |
d_skDefs.insert(skolem, def); |
41 |
|
} |
42 |
29893 |
} |
43 |
|
|
44 |
2408 |
TNode SkolemDefManager::getDefinitionForSkolem(TNode skolem) const |
45 |
|
{ |
46 |
2408 |
NodeNodeMap::const_iterator it = d_skDefs.find(skolem); |
47 |
2408 |
Assert(it != d_skDefs.end()) << "No skolem def for " << skolem; |
48 |
2408 |
return it->second; |
49 |
|
} |
50 |
|
|
51 |
6536698 |
void SkolemDefManager::notifyAsserted(TNode literal, |
52 |
|
std::vector<TNode>& activatedSkolems, |
53 |
|
bool useDefs) |
54 |
|
{ |
55 |
13073396 |
std::unordered_set<Node> skolems; |
56 |
6536698 |
getSkolems(literal, skolems); |
57 |
10351749 |
for (const Node& k : skolems) |
58 |
|
{ |
59 |
3815051 |
if (d_skActive.find(k) != d_skActive.end()) |
60 |
|
{ |
61 |
|
// already active |
62 |
3417864 |
continue; |
63 |
|
} |
64 |
397187 |
d_skActive.insert(k); |
65 |
397187 |
if (useDefs) |
66 |
|
{ |
67 |
|
// add its definition to the activated list |
68 |
397187 |
NodeNodeMap::const_iterator it = d_skDefs.find(k); |
69 |
397187 |
Assert(it != d_skDefs.end()); |
70 |
397187 |
activatedSkolems.push_back(it->second); |
71 |
|
} |
72 |
|
else |
73 |
|
{ |
74 |
|
// add to the activated list |
75 |
|
activatedSkolems.push_back(k); |
76 |
|
} |
77 |
|
} |
78 |
6536698 |
} |
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 |
20172280 |
bool SkolemDefManager::hasSkolems(TNode n) const |
92 |
|
{ |
93 |
40344560 |
std::unordered_set<TNode> visited; |
94 |
20172280 |
std::unordered_set<TNode>::iterator it; |
95 |
40344560 |
std::vector<TNode> visit; |
96 |
40344560 |
TNode cur; |
97 |
20172280 |
visit.push_back(n); |
98 |
2771513 |
do |
99 |
|
{ |
100 |
22943793 |
cur = visit.back(); |
101 |
43752066 |
if (cur.getAttribute(HasSkolemComputedAttr())) |
102 |
|
{ |
103 |
20808273 |
visit.pop_back(); |
104 |
|
// already computed |
105 |
20808273 |
continue; |
106 |
|
} |
107 |
2135520 |
it = visited.find(cur); |
108 |
2135520 |
if (it == visited.end()) |
109 |
|
{ |
110 |
1152944 |
visited.insert(cur); |
111 |
1152944 |
if (cur.getNumChildren() == 0) |
112 |
|
{ |
113 |
170368 |
visit.pop_back(); |
114 |
170368 |
bool hasSkolem = false; |
115 |
170368 |
if (cur.isVar()) |
116 |
|
{ |
117 |
125732 |
hasSkolem = (d_skDefs.find(cur) != d_skDefs.end()); |
118 |
|
} |
119 |
170368 |
cur.setAttribute(HasSkolemAttr(), hasSkolem); |
120 |
170368 |
cur.setAttribute(HasSkolemComputedAttr(), true); |
121 |
|
} |
122 |
|
else |
123 |
|
{ |
124 |
982576 |
visit.insert(visit.end(), cur.begin(), cur.end()); |
125 |
|
} |
126 |
|
} |
127 |
|
else |
128 |
|
{ |
129 |
982576 |
visit.pop_back(); |
130 |
982576 |
bool hasSkolem = false; |
131 |
2552447 |
for (TNode i : cur) |
132 |
|
{ |
133 |
1712798 |
Assert(i.getAttribute(HasSkolemComputedAttr())); |
134 |
1712798 |
if (i.getAttribute(HasSkolemAttr())) |
135 |
|
{ |
136 |
142927 |
hasSkolem = true; |
137 |
142927 |
break; |
138 |
|
} |
139 |
|
} |
140 |
982576 |
cur.setAttribute(HasSkolemAttr(), hasSkolem); |
141 |
982576 |
cur.setAttribute(HasSkolemComputedAttr(), true); |
142 |
|
} |
143 |
22943793 |
} while (!visit.empty()); |
144 |
20172280 |
Assert(n.getAttribute(HasSkolemComputedAttr())); |
145 |
40344560 |
return n.getAttribute(HasSkolemAttr()); |
146 |
|
} |
147 |
|
|
148 |
6539432 |
void SkolemDefManager::getSkolems(TNode n, |
149 |
|
std::unordered_set<Node>& skolems) const |
150 |
|
{ |
151 |
13078864 |
std::unordered_set<TNode> visited; |
152 |
6539432 |
std::unordered_set<TNode>::iterator it; |
153 |
13078864 |
std::vector<TNode> visit; |
154 |
13078864 |
TNode cur; |
155 |
6539432 |
visit.push_back(n); |
156 |
13632848 |
do |
157 |
|
{ |
158 |
20172280 |
cur = visit.back(); |
159 |
20172280 |
visit.pop_back(); |
160 |
20172280 |
if (!hasSkolems(cur)) |
161 |
|
{ |
162 |
|
// does not have skolems, continue |
163 |
7598328 |
continue; |
164 |
|
} |
165 |
12573952 |
it = visited.find(cur); |
166 |
12573952 |
if (it == visited.end()) |
167 |
|
{ |
168 |
12175620 |
visited.insert(cur); |
169 |
12175620 |
if (cur.isVar()) |
170 |
|
{ |
171 |
3817459 |
if (d_skDefs.find(cur) != d_skDefs.end()) |
172 |
|
{ |
173 |
3817459 |
skolems.insert(cur); |
174 |
|
} |
175 |
|
} |
176 |
|
else |
177 |
|
{ |
178 |
8358161 |
visit.insert(visit.end(), cur.begin(), cur.end()); |
179 |
|
} |
180 |
|
} |
181 |
20172280 |
} while (!visit.empty()); |
182 |
6539432 |
} |
183 |
|
|
184 |
|
} // namespace prop |
185 |
29280 |
} // namespace cvc5 |