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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*++
Copyright (c) 2025 Microsoft Corporation
Module Name:
search_tree.h
Abstract:
A binary search tree for managing the search space of a DPLL(T) solver.
It supports splitting on atoms, backtracking on conflicts, and activating nodes.
Nodes can be in one of three states: open, closed, or active.
- Closed nodes are fully explored (both children are closed).
- Active nodes have no children and are currently being explored.
- Open nodes either have children that are open or are leaves.
A node can be split if it is active. After splitting, it becomes open and has two open children.
Backtracking on a conflict closes all nodes below the last node whose atom is in the conflict set.
Activation searches an open node closest to a seed node.
Author:
Ilana Shapiro 2025-9-06
--*/
#include "util/util.h"
#include "util/vector.h"
#pragma once
namespace search_tree {
enum class status { open, closed, active };
template <typename Config> class node {
typedef typename Config::literal literal;
literal m_literal;
node *m_left = nullptr, *m_right = nullptr, *m_parent = nullptr;
status m_status;
vector<literal> m_core;
public:
node(literal const &l, node *parent) : m_literal(l), m_parent(parent), m_status(status::open) {}
~node() {
dealloc(m_left);
dealloc(m_right);
}
status get_status() const {
return m_status;
}
void set_status(status s) {
m_status = s;
}
literal const &get_literal() const {
return m_literal;
}
bool literal_is_null() const {
return Config::is_null(m_literal);
}
void split(literal const &a, literal const &b) {
SASSERT(!Config::literal_is_null(a));
SASSERT(!Config::literal_is_null(b));
if (m_status != status::active)
return;
SASSERT(!m_left);
SASSERT(!m_right);
m_left = alloc(node<Config>, a, this);
m_right = alloc(node<Config>, b, this);
m_status = status::open;
}
node* left() const { return m_left; }
node* right() const { return m_right; }
node* parent() const { return m_parent; }
unsigned depth() const {
unsigned d = 0;
node* p = m_parent;
while (p) {
++d;
p = p->parent();
}
return d;
}
node *find_active_node() {
if (m_status == status::active)
return this;
if (m_status == status::closed)
return nullptr;
node *nodes[2] = {m_left, m_right};
for (unsigned i = 0; i < 2; ++i) {
auto res = nodes[i] ? nodes[i]->find_active_node() : nullptr;
if (res)
return res;
}
if (m_left->get_status() == status::closed && m_right->get_status() == status::closed)
m_status = status::closed;
return nullptr;
}
void display(std::ostream &out, unsigned indent) const {
for (unsigned i = 0; i < indent; ++i)
out << " ";
Config::display_literal(out, m_literal);
out << (get_status() == status::open ? " (o)" : get_status() == status::closed ? " (c)" : " (a)");
out << "\n";
if (m_left)
m_left->display(out, indent + 2);
if (m_right)
m_right->display(out, indent + 2);
}
void set_core(vector<literal> const &core) {
m_core = core;
}
vector<literal> const &get_core() const {
return m_core;
}
void clear_core() {
m_core.clear();
}
};
template <typename Config> class tree {
typedef typename Config::literal literal;
scoped_ptr<node<Config>> m_root = nullptr;
literal m_null_literal;
random_gen m_rand;
// return an active node in the subtree rooted at n, or nullptr if there is none
node<Config> *activate_from_root(node<Config> *n) {
if (!n)
return nullptr;
if (n->get_status() != status::open)
return nullptr;
auto left = n->left();
auto right = n->right();
if (!left && !right) {
n->set_status(status::active);
return n;
}
node<Config> *nodes[2] = {left, right};
unsigned index = m_rand(2);
auto child = activate_from_root(nodes[index]);
if (child)
return child;
child = activate_from_root(nodes[1 - index]);
if (child)
return child;
return nullptr;
}
// Bubble to the highest ancestor where ALL literals in the resolvent
// are present somewhere on the path from that ancestor to root
node<Config>* find_highest_attach(node<Config>* p, vector<literal> const& resolvent) {
node<Config>* candidate = p;
node<Config>* attach_here = p;
while (candidate) {
bool all_found = true;
for (auto const& r : resolvent) {
bool found = false;
for (node<Config>* q = candidate; q; q = q->parent()) {
if (q->get_literal() == r) {
found = true;
break;
}
}
if (!found) {
all_found = false;
break;
}
}
if (all_found) {
attach_here = candidate; // bubble up to this node
}
candidate = candidate->parent();
}
return attach_here;
}
// Propagate closure upward via sibling resolution starting at node `cur`.
// Returns true iff global UNSAT was detected.
bool propagate_closure_upward(node<Config>* cur) {
while (true) {
node<Config>* parent = cur->parent();
if (!parent)
return false;
auto left = parent->left();
auto right = parent->right();
if (!left || !right)
return false;
if (left->get_status() != status::closed ||
right->get_status() != status::closed)
return false;
if (left->get_core().empty() ||
right->get_core().empty())
return false;
auto res = compute_sibling_resolvent(left, right);
if (res.empty()) {
close(m_root.get(), res); // global UNSAT
return true;
}
close(parent, res);
cur = parent; // keep bubbling
}
}
void close(node<Config> *n, vector<literal> const &C) {
if (!n || n->get_status() == status::closed)
return;
n->set_status(status::closed);
n->set_core(C);
close(n->left(), C);
close(n->right(), C);
}
// Invariants:
// Cores labeling nodes are subsets of the literals on the path to the node and the (external) assumption
// literals. If a parent is open, then the one of the children is open.
void close_with_core(node<Config> *n, vector<literal> const &C) {
if (!n)
return;
// If the node is closed AND has a stronger or equal core, we are done.
// Otherwise, closed nodes may still accept a different (stronger) core to enable pruning/resolution higher in the tree.
auto subseteq = [](vector<literal> const& A, vector<literal> const& B) {
return all_of(A, [&](auto const &a) { return B.contains(a); });
};
if (n->get_status() == status::closed && subseteq(n->get_core(), C))
return;
node<Config> *p = n->parent();
// The conflict does NOT depend on the decision literal at node n, so n’s split literal is irrelevant to this conflict
// thus the entire subtree under n is closed regardless of the split, so the conflict should be attached higher, at the nearest ancestor that does participate
if (p && all_of(C, [n](auto const &l) { return l != n->get_literal(); })) {
close_with_core(p, C);
return;
}
// Close descendants WITHOUT resolving
close(n, C);
if (!p)
return;
auto left = p->left();
auto right = p->right();
if (!left || !right)
return;
// only attempt when both children are closed and each has a *non-empty* core
if (left->get_status() != status::closed || right->get_status() != status::closed) return;
if (left->get_core().empty() || right->get_core().empty()) return;
auto resolvent = compute_sibling_resolvent(left, right);
if (resolvent.empty()) { // empty resolvent => global UNSAT
close(m_root.get(), resolvent);
return;
}
auto attach = find_highest_attach(p, resolvent);
close(attach, resolvent);
// try to propagate the highest attach node upward *with sibling resolution*
// this handles the case when non-chronological backjumping takes us to a node whose sibling was closed by another thread
node<Config>* cur = attach;
propagate_closure_upward(cur);
}
// Given complementary sibling nodes for literals x and ¬x, sibling resolvent = (core_left ∪ core_right) \ {x,
// ¬x}
vector<literal> compute_sibling_resolvent(node<Config> *left, node<Config> *right) {
vector<literal> res;
auto &core_l = left->get_core();
auto &core_r = right->get_core();
if (core_l.empty() || core_r.empty() || left->parent() != right->parent())
return res;
auto lit_l = left->get_literal();
auto lit_r = right->get_literal();
for (auto const &lit : core_l)
if (lit != lit_l && !res.contains(lit))
res.push_back(lit);
for (auto const &lit : core_r)
if (lit != lit_r && !res.contains(lit))
res.push_back(lit);
return res;
}
public:
tree(literal const &null_literal) : m_null_literal(null_literal) {
reset();
}
void set_seed(unsigned seed) {
m_rand.set_seed(seed);
}
void reset() {
m_root = alloc(node<Config>, m_null_literal, nullptr);
m_root->set_status(status::active);
}
// Split current node if it is active.
// After the call, n is open and has two children.
void split(node<Config> *n, literal const &a, literal const &b) {
n->split(a, b);
}
// conflict is given by a set of literals.
// they are subsets of the literals on the path from root to n AND the external assumption literals
void backtrack(node<Config> *n, vector<literal> const &conflict) {
if (conflict.empty()) {
close_with_core(m_root.get(), conflict);
return;
}
SASSERT(n != m_root.get());
// all literals in conflict are on the path from root to n
// remove assumptions from conflict to ensure this.
DEBUG_CODE(auto on_path =
[&](literal const &a) {
node<Config> *p = n;
while (p) {
if (p->get_literal() == a)
return true;
p = p->parent();
}
return false;
};
SASSERT(all_of(conflict, [&](auto const &a) { return on_path(a); })););
// Walk upward to find the nearest ancestor whose decision participates in the conflict
while (n) {
if (any_of(conflict, [&](auto const &a) { return a == n->get_literal(); })) {
// close the subtree under n (preserves core attached to n), and attempt to resolve upwards
close_with_core(n, conflict);
return;
}
n = n->parent();
}
UNREACHABLE();
}
// return an active node in the tree, or nullptr if there is none
// first check if there is a node to activate under n,
// if not, go up the tree and try to activate a sibling subtree
node<Config> *activate_node(node<Config> *n) {
if (!n) {
if (m_root->get_status() == status::active)
return m_root.get();
n = m_root.get();
}
auto res = activate_from_root(n);
if (res)
return res;
auto p = n->parent();
while (p) {
if (p->left() && p->left()->get_status() == status::closed &&
p->right() && p->right()->get_status() == status::closed) {
if (p->get_status() != status::closed)
return nullptr; // inconsistent state
n = p;
p = n->parent();
continue;
}
if (n == p->left()) {
res = activate_from_root(p->right());
if (res)
return res;
}
else {
VERIFY(n == p->right());
res = activate_from_root(p->left());
if (res)
return res;
}
n = p;
p = n->parent();
}
return nullptr;
}
node<Config> *find_active_node() {
return m_root->find_active_node();
}
vector<literal> const &get_core_from_root() const {
return m_root->get_core();
}
bool is_closed() const {
return m_root->get_status() == status::closed;
}
std::ostream &display(std::ostream &out) const {
m_root->display(out, 0);
return out;
}
};
} // namespace search_tree