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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//! # SAT Solvers
//!
//! A unified Rust interface to multiple SAT solvers with automatic source compilation.
//!
//! This crate provides a simple way to use popular SAT solvers (CaDiCaL, MiniSat, Glucose,
//! Lingeling, Kissat) without requiring pre-installed binaries. The solvers are compiled
//! from source during `cargo build`, ensuring reproducible builds across different systems.
//!
//! ## Features
//!
//! - **Self-contained**: Solvers are built from bundled source code during compilation
//! - **Unified interface**: All solvers implement the [`SolverExecutor`] trait
//! - **Feature-gated**: Only compile the solvers you need
//! - **Timeout support**: Built-in timeout handling for long-running problems
//! - **DIMACS format**: Standard CNF input format supported by all solvers
//!
//! ## Quick Start
//!
//! Add `sat-solvers` to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! sat-solvers = "0.1"
//! ```
//!
//! By default, CaDiCaL and MiniSat are enabled. To use other solvers, enable their features:
//!
//! ```toml
//! [dependencies]
//! sat-solvers = { version = "0.1", features = ["cadical", "glucose", "kissat"] }
//! ```
//!
//! ## Example
//!
//! Solve a simple SAT problem using CaDiCaL:
//!
//! ```no_run
//! use sat_solvers::solvers::SolverExecutor;
//! use sat_solvers::SATResult;
//! use std::time::Duration;
//!
//! # #[cfg(feature = "cadical")]
//! # fn main() {
//! use sat_solvers::solvers::cadical::CaDiCaLExecutor;
//!
//! // Define a simple CNF formula in DIMACS format:
//! // (x1 OR NOT x2) AND (x2 OR x3)
//! let dimacs = "p cnf 3 2\n1 -2 0\n2 3 0\n";
//!
//! let executor = CaDiCaLExecutor;
//! let result = executor.execute(dimacs, Some(Duration::from_secs(30))).unwrap();
//!
//! match result {
//! SATResult::Satisfiable(assignment) => {
//! println!("Satisfiable! Assignment: {:?}", assignment);
//! }
//! SATResult::Unsatisfiable => {
//! println!("Unsatisfiable");
//! }
//! SATResult::Unknown => {
//! println!("Unknown (timeout or resource limit)");
//! }
//! }
//! # }
//! # #[cfg(not(feature = "cadical"))]
//! # fn main() {}
//! ```
//!
//! ## Available Solvers
//!
//! | Feature | Solver | Description |
//! |-------------|-----------|--------------------------------------------------|
//! | `cadical` | CaDiCaL | State-of-the-art CDCL solver, SAT Competition winner |
//! | `minisat` | MiniSat | Classic, widely-used SAT solver |
//! | `glucose` | Glucose | MiniSat derivative with improved learning |
//! | `lingeling` | Lingeling | High-performance solver by Armin Biere |
//! | `kissat` | Kissat | Successor to CaDiCaL, SAT Competition 2020+ winner |
//!
//! ## Build Requirements
//!
//! Since solvers are compiled from source, you need:
//!
//! - A C++ compiler (g++ or clang++)
//! - `make`
//! - `cmake` (for Glucose)
//!
//! ## DIMACS CNF Format
//!
//! The solvers accept input in DIMACS CNF format:
//!
//! ```text
//! p cnf <num_variables> <num_clauses>
//! <literal1> <literal2> ... 0
//! <literal1> <literal2> ... 0
//! ...
//! ```
//!
//! - Variables are positive integers (1, 2, 3, ...)
//! - Negative literals represent negation (-1 means NOT x1)
//! - Each clause ends with 0
use Error;
use fmt;
use PathBuf;
use Command;
use Output;
pub use SolverExecutor;
/// Identifies which SAT solver to use.
///
/// Each variant corresponds to a different SAT solver implementation.
/// The availability of each solver depends on the enabled feature flags.
///
/// # Example
///
/// ```
/// use sat_solvers::SATSolverKind;
///
/// let solver = SATSolverKind::CaDiCaL;
/// println!("Using solver: {}", solver);
/// ```
/// The result of a SAT solver execution.
///
/// A SAT problem can have three possible outcomes:
/// - [`Satisfiable`](SATResult::Satisfiable): A satisfying assignment exists
/// - [`Unsatisfiable`](SATResult::Unsatisfiable): No satisfying assignment exists
/// - [`Unknown`](SATResult::Unknown): The solver couldn't determine satisfiability
/// (e.g., due to timeout or resource limits)
///
/// # Example
///
/// ```
/// use sat_solvers::SATResult;
///
/// let result = SATResult::Satisfiable(vec![1, -2, 3]);
///
/// if result.is_satisfiable() {
/// if let SATResult::Satisfiable(assignment) = result {
/// // assignment contains the satisfying variable assignments
/// // Positive values mean the variable is true, negative means false
/// assert!(assignment.contains(&1)); // x1 = true
/// assert!(assignment.contains(&-2)); // x2 = false
/// }
/// }
/// ```
/// Errors that can occur when working with SAT solvers.
///
/// This enum represents all possible errors that can be returned by
/// the crate's public API.
type Result<T> = Result;
/// Returns the path to the MiniSat binary.
///
/// The path can be overridden by setting the `MINISAT_BINARY_PATH` environment variable.
/// Otherwise, the path determined at build time is used.
///
/// # Panics
///
/// Panics if the `minisat` feature is not enabled.
/// Returns the path to the Glucose binary.
///
/// The path can be overridden by setting the `GLUCOSE_BINARY_PATH` environment variable.
/// Otherwise, the path determined at build time is used.
///
/// # Panics
///
/// Panics if the `glucose` feature is not enabled.
/// Returns the path to the CaDiCaL binary.
///
/// The path can be overridden by setting the `CADICAL_BINARY_PATH` environment variable.
/// Otherwise, the path determined at build time is used.
///
/// # Panics
///
/// Panics if the `cadical` feature is not enabled.
/// Returns the path to the Kissat binary.
///
/// The path can be overridden by setting the `KISSAT_BINARY_PATH` environment variable.
/// Otherwise, the path determined at build time is used.
///
/// # Panics
///
/// Panics if the `kissat` feature is not enabled.
/// Returns the path to the Lingeling binary.
///
/// The path can be overridden by setting the `LINGELING_BINARY_PATH` environment variable.
/// Otherwise, the path determined at build time is used.
///
/// # Panics
///
/// Panics if the `lingeling` feature is not enabled.
/// Returns the path to the specified solver's binary.
///
/// # Arguments
///
/// * `solver` - The name of the solver (case-insensitive). Valid values are:
/// `"minisat"`, `"glucose"`, `"cadical"`, `"kissat"`, `"lingeling"`
///
/// # Errors
///
/// Returns [`SATSolversError::SolverNotAvailable`] if the solver is not
/// enabled via feature flags or if the solver name is not recognized.
///
/// # Example
///
/// ```no_run
/// use sat_solvers::solver_path;
///
/// # #[cfg(feature = "cadical")]
/// let path = solver_path("cadical").unwrap();
/// ```
/// Runs the specified solver with the given command-line arguments.
///
/// This is a low-level function that executes the solver binary directly.
/// For most use cases, prefer using the [`SolverExecutor`] trait instead.
///
/// # Arguments
///
/// * `solver` - The name of the solver (case-insensitive)
/// * `args` - Command-line arguments to pass to the solver
///
/// # Errors
///
/// Returns [`SATSolversError::SolverNotAvailable`] if the solver is not enabled,
/// or [`SATSolversError::IoError`] if execution fails.
///
/// # Example
///
/// ```no_run
/// use sat_solvers::run_solver;
///
/// # #[cfg(feature = "cadical")]
/// let output = run_solver("cadical", &["--help"]).unwrap();
/// ```
/// Ensures that the specified solver binary exists at its expected path.
///
/// # Arguments
///
/// * `solver` - The name of the solver (case-insensitive)
///
/// # Errors
///
/// Returns [`SATSolversError::SolverNotAvailable`] if the solver is not enabled,
/// or [`SATSolversError::SolverBinaryNotFound`] if the binary doesn't exist.
///
/// # Example
///
/// ```no_run
/// use sat_solvers::ensure_solver_exists;
///
/// # #[cfg(feature = "cadical")]
/// match ensure_solver_exists("cadical") {
/// Ok(path) => println!("CaDiCaL found at: {}", path.display()),
/// Err(e) => eprintln!("Error: {}", e),
/// }
/// ```