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
//! Tools for building a context.
use crate::{
context::GenericContext,
db::consequence_q::{self},
dispatch::{
library::report::{self, Report},
Dispatch,
},
structures::{
atom::Atom,
clause::{self, vClause, Clause},
literal::{abLiteral, Literal},
},
types::err::{self},
};
use std::{borrow::Borrow, io::BufRead};
/// Methods for building the context.
impl<R: rand::Rng + std::default::Default> GenericContext<R> {
/// Returns the internal representation an atom from a string, adding the atom to the context if required.
///
/// ```rust
/// # use otter_sat::context::Context;
/// # use otter_sat::config::Config;
/// #
/// let mut the_context = Context::from_config(Config::default(), None);
/// let mut atoms = vec!["p", "-q", "r", "-r"];
/// for atom in &atoms {
/// assert!(the_context.atom_from_string(&atom.to_string()).is_ok())
/// }
/// ```
pub fn atom_from_string(&mut self, string: &str) -> Result<Atom, err::Parse> {
match self.atom_db.internal_representation(string) {
Some(atom) => Ok(atom),
None => {
let the_id = self.atom_db.count() as Atom;
self.atom_db
.fresh_atom(string, self.rng.gen_bool(self.config.polarity_lean));
Ok(the_id)
}
}
}
/// Returns the internal representation of a literal from a string, adding an atom to the context if required.
/// ```rust
/// # use otter_sat::context::Context;
/// # use otter_sat::config::Config;
/// #
/// let mut the_context = Context::from_config(Config::default(), None);
/// let not_p = the_context.literal_from_string("-p").expect("p?");
/// ```
pub fn literal_from_string(&mut self, string: &str) -> Result<abLiteral, err::Parse> {
let trimmed_string = string.trim();
if trimmed_string.is_empty() {
return Err(err::Parse::Empty);
}
if trimmed_string == "-" {
return Err(err::Parse::Negation);
};
let polarity = !trimmed_string.starts_with('-');
let the_atom = match polarity {
true => trimmed_string,
false => &trimmed_string[1..],
};
// Safe, as atom_from_string takes any non-empty string, which has been established.
let the_atom = unsafe { self.atom_from_string(the_atom).unwrap_unchecked() };
Ok(abLiteral::fresh(the_atom, polarity))
}
/// Returns the internal representation a clause from a string, adding atoms to the context if required..
///
/// ```rust
/// # use otter_sat::context::Context;
/// # use otter_sat::config::Config;
/// # use otter_sat::dispatch::library::report::{self};
/// #
/// let mut the_context = Context::from_config(Config::default(), None);
///
/// assert!(the_context.clause_from_string("p -q -r s").is_ok());
/// ```
pub fn clause_from_string(&mut self, string: &str) -> Result<vClause, err::Build> {
let string_lterals = string.split_whitespace();
let mut the_clause = vec![];
for string_literal in string_lterals {
let the_literal = match self.literal_from_string(string_literal) {
Ok(literal) => literal,
Err(e) => return Err(err::Build::Parse(e)),
};
if !the_clause.iter().any(|l| *l == the_literal) {
the_clause.push(the_literal);
}
}
Ok(the_clause)
}
/// Adds a clause to the context.
///
/// ```rust
/// # use otter_sat::context::Context;
/// # use otter_sat::config::Config;
/// # use otter_sat::dispatch::library::report::{self};
/// #
/// let mut the_context = Context::from_config(Config::default(), None);
///
/// let a_clause = the_context.clause_from_string("p -q -r s").unwrap();
///
/// assert!(the_context.add_clause(a_clause).is_ok());
/// the_context.solve();
/// assert_eq!(the_context.report(), report::Solve::Satisfiable)
/// ```
///
/// - Empty clauses are rejected as these are equivalent to falsum, and so unsatisfiable.
/// - Unit clause (a literal) literal database.
/// - Clauses with two or more literals go to the clause database.
///
/// This handles the variations.
/*
TODO: Relax the constraints on adding a unit clause after a decision has been made.
If the decision conflicts with the current valuation, backtracking is required.
Otherwise, if the literal is not already recorded as a clause, it could be 'raised' to being a clause.
Though, a naive approach may cause some issues with FRAT proofs, and other features which rely on decision level information.
*/
pub fn add_clause(&mut self, clause: impl Clause) -> Result<(), err::Build> {
if clause.size() == 0 {
return Err(err::Build::ClauseDB(err::ClauseDB::EmptyClause));
}
let mut clause_vec = clause.canonical();
match self.preprocess_clause(&mut clause_vec)? {
PreprocessResult::Tautology => return Ok(()),
PreprocessResult::Contradiction => return Err(err::Build::Unsatisfiable),
_ => {}
};
match clause_vec.len() {
0 => panic!("!"),
1 => {
let literal = unsafe { *clause_vec.get_unchecked(0) };
match self.atom_db.value_of(literal.atom()) {
None => match self.q_literal(literal.borrow()) {
Ok(consequence_q::Ok::Qd) => {
self.record_clause(literal, clause::Source::Original);
Ok(())
}
_ => Err(err::Build::ClauseDB(err::ClauseDB::ImmediateConflict)),
},
Some(v) if v == literal.polarity() => {
// Must be at zero for an assumption, so there's nothing to do
if self.counters.total_decisions != 0 {
Err(err::Build::ClauseDB(err::ClauseDB::AddedUnitAfterDecision))
} else {
Ok(())
}
}
Some(_) => Err(err::Build::ClauseDB(err::ClauseDB::ImmediateConflict)),
};
Ok(())
}
_ => {
self.record_clause(clause_vec, clause::Source::Original)?;
Ok(())
}
}
}
/// Reads a DIMACS file into the context.
///
/// ```rust,ignore
/// context.read_dimacs(BufReader::new(&file))?;
/// ```
///
/// ```rust
/// # use otter_sat::context::Context;
/// # use otter_sat::config::Config;
/// # use std::io::Write;
/// let mut the_context = Context::from_config(Config::default(), None);
///
/// let mut dimacs = vec![];
/// let _ = dimacs.write(b"
/// p q 0
/// p -q 0
/// -p q 0
/// -p -q 0
/// p q r 0
/// -p q -r 0
/// r -s 0
/// ");
///
/// assert!(the_context.read_dimacs(dimacs.as_slice()).is_ok());
/// assert!(the_context.solve().is_ok());
/// ```
#[allow(clippy::manual_flatten, unused_labels)]
pub fn read_dimacs(&mut self, mut reader: impl BufRead) -> Result<(), err::Build> {
//
let mut buffer = String::with_capacity(1024);
let mut clause_buffer: vClause = Vec::default();
let mut line_counter = 0;
let mut clause_counter = 0;
// first phase, read until the formula begins
'preamble_loop: loop {
match reader.read_line(&mut buffer) {
Ok(0) => break,
Ok(_) => line_counter += 1,
Err(_) => return Err(err::Build::Parse(err::Parse::Line(line_counter))),
}
match buffer.chars().next() {
Some('c') => {
buffer.clear();
continue;
}
Some('p') => {
let mut problem_details = buffer.split_whitespace();
let atom_count: usize = match problem_details.nth(2) {
None => return Err(err::Build::Parse(err::Parse::ProblemSpecification)),
Some(string) => match string.parse() {
Err(_) => {
return Err(err::Build::Parse(err::Parse::ProblemSpecification))
}
Ok(count) => count,
},
};
let clause_count: usize = match problem_details.next() {
None => return Err(err::Build::Parse(err::Parse::ProblemSpecification)),
Some(string) => match string.parse() {
Err(_) => {
return Err(err::Build::Parse(err::Parse::ProblemSpecification))
}
Ok(count) => count,
},
};
buffer.clear();
if let Some(dispatcher) = &self.dispatcher {
let expectation = report::Parser::Expected(atom_count, clause_count);
dispatcher(Dispatch::Report(Report::Parser(expectation)));
}
break;
}
_ => break,
}
}
// second phase, read until the formula ends
'formula_loop: loop {
match reader.read_line(&mut buffer) {
Ok(0) => break,
Ok(_) => line_counter += 1,
Err(_) => return Err(err::Build::Parse(err::Parse::Line(line_counter))),
}
match buffer.chars().next() {
Some('%') => break 'formula_loop,
Some('c') => {}
// Some('p') => {
// return Err(err::Build::Parse(err::Parse::MisplacedProblem(line_counter)))
// }
_ => {
let split_buf = buffer.split_whitespace();
for item in split_buf {
match item {
"0" => {
let the_clause = std::mem::take(&mut clause_buffer);
match self.add_clause(the_clause) {
Ok(_) => clause_counter += 1,
Err(e) => return Err(e),
}
}
_ => {
let the_literal = match self.literal_from_string(item) {
Ok(literal) => literal,
Err(e) => return Err(err::Build::Parse(e)),
};
if !clause_buffer.iter().any(|l| *l == the_literal) {
clause_buffer.push(the_literal);
}
}
}
}
}
}
buffer.clear();
}
if let Some(dispatcher) = &self.dispatcher {
let counts = report::Parser::Counts(self.atom_db.count(), clause_counter);
dispatcher(Dispatch::Report(Report::Parser(counts)));
let report_clauses =
report::Parser::ContextClauses(self.clause_db.total_clause_count());
dispatcher(Dispatch::Report(Report::Parser(report_clauses)));
}
Ok(())
}
// todo: implement this again, sometime
// Aka. soft assumption
// This will hold until a restart happens
// pub fn believe(&mut self, literal: impl Borrow<Literal>) -> Result<(), err::Context> {
// if self.literal_db.decision_made() {
// return Err(err::Context::AssumptionAfterDecision);
// }
// match self.q_literal(literal.borrow()) {
// Ok(_) => {
// ???
// Ok(n())
// }
// Err(_) => Err(err::Context::AssumptionConflict),
// }
// }
}
/// Primarily to distinguish the case where preprocessing results in an empty clause.
#[derive(PartialEq, Eq)]
enum PreprocessResult {
Tautology,
Contradiction,
Clause,
}
impl<R: rand::Rng + std::default::Default> GenericContext<R> {
/// Preprocess a clause to remove proven literals and duplicate literals.
fn preprocess_clause(&self, clause: &mut vClause) -> Result<PreprocessResult, err::Build> {
let mut index = 0;
let mut max = clause.len();
loop {
if index == max {
break;
}
let this_l = clause[index];
let this_n = this_l.negate();
if clause.iter().any(|l| *l == this_n) {
return Ok(PreprocessResult::Tautology);
}
if self
.clause_db
.all_unit_clauses()
.any(|proven_literal| proven_literal.negate() == this_l)
{
clause.swap_remove(index);
max -= 1;
} else {
index += 1;
}
}
match clause.len() {
0 => Ok(PreprocessResult::Contradiction),
_ => Ok(PreprocessResult::Clause),
}
}
}