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 469 470 471 472 473 474
//! # C-API For RustSAT
//!
//! In the C-API, literals are represented as IPASIR literals.
//!
//! This is the C-API for RustSAT. Currently this API is very minimal and not
//! the focus of this project. For now, only the API of certain encodings is
//! available.
pub mod encodings {
//! # C-API For Encodings
use std::ffi::{c_int, c_void};
use crate::{
encodings::{self, CollectClauses},
instances::ManageVars,
types::{Clause, Var},
};
#[repr(C)]
pub enum MaybeError {
/// No error
Ok,
/// Encode was not called before using the encoding
NotEncoded,
/// The requested encoding is unsatisfiable
Unsat,
/// The encoding is in an invalid state to perform this action
InvalidState,
}
impl From<encodings::Error> for MaybeError {
fn from(value: encodings::Error) -> Self {
match value {
encodings::Error::NotEncoded => MaybeError::NotEncoded,
encodings::Error::Unsat => MaybeError::NotEncoded,
}
}
}
pub type CClauseCollector = extern "C" fn(lit: c_int, data: *mut c_void);
pub type CAssumpCollector = extern "C" fn(lit: c_int, data: *mut c_void);
struct ClauseCollector {
n_clauses: usize,
ccol: CClauseCollector,
cdata: *mut c_void,
}
impl ClauseCollector {
pub fn new(ccol: CClauseCollector, cdata: *mut c_void) -> Self {
Self {
n_clauses: 0,
ccol,
cdata,
}
}
}
impl CollectClauses for ClauseCollector {
fn n_clauses(&self) -> usize {
self.n_clauses
}
}
impl Extend<Clause> for ClauseCollector {
fn extend<T: IntoIterator<Item = Clause>>(&mut self, iter: T) {
iter.into_iter().for_each(|cl| {
cl.into_iter()
.for_each(|l| (self.ccol)(l.to_ipasir(), self.cdata));
(self.ccol)(0, self.cdata);
})
}
}
struct VarManager<'a> {
n_vars_used: &'a mut c_int,
}
impl<'a> VarManager<'a> {
pub fn new(n_vars_used: &'a mut c_int) -> Self {
Self { n_vars_used }
}
}
impl<'a> ManageVars for VarManager<'a> {
fn new_var(&mut self) -> Var {
let var = Var::new(*self.n_vars_used as u32);
*self.n_vars_used += 1;
var
}
fn max_var(&self) -> Option<Var> {
if *self.n_vars_used == 0 {
None
} else {
Some(Var::new(*self.n_vars_used as u32) - 1)
}
}
fn increase_next_free(&mut self, v: Var) -> bool {
let v_idx = v.idx32();
if v_idx > *self.n_vars_used as u32 {
*self.n_vars_used = v_idx as c_int;
return true;
}
false
}
fn combine(&mut self, _other: Self)
where
Self: Sized,
{
panic!("cannot combine this var manager")
}
fn n_used(&self) -> u32 {
*self.n_vars_used as u32
}
fn forget_from(&mut self, min_var: Var) {
*self.n_vars_used =
std::cmp::min(*self.n_vars_used, min_var.idx32().try_into().unwrap())
}
}
pub mod totalizer {
//! # Totalizer C-API
use std::ffi::{c_int, c_void};
use crate::{
encodings::card::{BoundUpper, BoundUpperIncremental, DbTotalizer},
types::Lit,
};
use super::{CClauseCollector, ClauseCollector, MaybeError, VarManager};
/// Creates a new [`DbTotalizer`] cardinality encoding
#[no_mangle]
pub extern "C" fn tot_new() -> *mut DbTotalizer {
Box::into_raw(Box::default())
}
/// Adds a new input literal to a [`DbTotalizer`]
///
/// # Safety
///
/// `tot` must be a return value of [`tot_new`] that [`tot_drop`] has
/// not yet been called on.
#[no_mangle]
pub unsafe extern "C" fn tot_add(tot: *mut DbTotalizer, lit: c_int) {
let mut boxed = unsafe { Box::from_raw(tot) };
boxed.extend([Lit::from_ipasir(lit).expect("invalid IPASIR literal")]);
Box::into_raw(boxed);
}
/// Lazily builds the _change in_ cardinality encoding to enable upper
/// bounds in a given range. A change might be added literals or changed
/// bounds.
///
/// The min and max bounds are inclusive. After a call to
/// [`tot_encode_ub`] with `min_bound=2` and `max_bound=4` bound
/// including `<= 2` and `<= 4` can be enforced.
///
/// A call to `var_manager` must yield a new variable. The
/// encoding will be returned via the given callback function as
/// 0-terminated clauses (in the same way as IPASIR's `add`).
///
/// # Safety
///
/// `tot` must be a return value of [`tot_new`] that [`tot_drop`] has
/// not yet been called on.
#[no_mangle]
pub unsafe extern "C" fn tot_encode_ub(
tot: *mut DbTotalizer,
min_bound: usize,
max_bound: usize,
n_vars_used: &mut c_int,
collector: CClauseCollector,
collector_data: *mut c_void,
) {
assert!(min_bound <= max_bound);
let mut collector = ClauseCollector::new(collector, collector_data);
let mut var_manager = VarManager::new(n_vars_used);
let mut boxed = unsafe { Box::from_raw(tot) };
boxed.encode_ub_change(min_bound..=max_bound, &mut collector, &mut var_manager);
Box::into_raw(boxed);
}
/// Returns an assumption/unit for enforcing an upper bound (`sum of
/// lits <= ub`). Make sure that [`tot_encode_ub`] has been called
/// adequately and nothing has been called afterwards, otherwise
/// [`MaybeError::NotEncoded`] will be returned.
///
/// # Safety
///
/// `tot` must be a return value of [`tot_new`] that [`tot_drop`] has
/// not yet been called on.
#[no_mangle]
pub unsafe extern "C" fn tot_enforce_ub(
tot: *mut DbTotalizer,
ub: usize,
assump: &mut c_int,
) -> MaybeError {
let boxed = unsafe { Box::from_raw(tot) };
let ret = match boxed.enforce_ub(ub) {
Ok(assumps) => {
debug_assert_eq!(assumps.len(), 1);
*assump = assumps[0].to_ipasir();
MaybeError::Ok
}
Err(err) => err.into(),
};
Box::into_raw(boxed);
ret
}
/// Frees the memory associated with a [`DbTotalizer`]
///
/// # Safety
///
/// `tot` must be a return value of [`tot_new`] and cannot be used
/// afterwards again.
#[no_mangle]
pub unsafe extern "C" fn tot_drop(tot: *mut DbTotalizer) {
drop(unsafe { Box::from_raw(tot) })
}
#[cfg(test)]
mod tests {
use inline_c::assert_c;
#[test]
fn new_drop() {
(assert_c! {
#include <assert.h>
#include "rustsat.h"
int main() {
DbTotalizer *tot = tot_new();
assert(tot != NULL);
tot_drop(tot);
return 0;
}
})
.success();
}
#[test]
fn basic() {
(assert_c! {
#include <assert.h>
#include "rustsat.h"
void clause_counter(int lit, void *data) {
if (!lit) {
int *cnt = (int *)data;
(*cnt)++;
}
}
int main() {
DbTotalizer *tot = tot_new();
tot_add(tot, 1);
tot_add(tot, 2);
tot_add(tot, 3);
tot_add(tot, 4);
int n_used = 4;
int n_clauses = 0;
tot_encode_ub(tot, 0, 4, &n_used, &clause_counter, &n_clauses);
tot_drop(tot);
assert(n_used == 12);
assert(n_clauses == 14);
return 0;
}
})
.success();
}
}
}
mod dpw {
//! # Dynamic Poly Watchdog C-API
use std::ffi::{c_int, c_void};
use crate::{
encodings::pb::{BoundUpper, BoundUpperIncremental, DynamicPolyWatchdog},
types::Lit,
};
use super::{CAssumpCollector, CClauseCollector, ClauseCollector, MaybeError, VarManager};
/// Creates a new [`DynamicPolyWatchdog`] cardinality encoding
#[no_mangle]
pub extern "C" fn dpw_new() -> *mut DynamicPolyWatchdog {
Box::into_raw(Box::default())
}
/// Adds a new input literal to a [`DynamicPolyWatchdog`]. Input
/// literals can only be added _before_ the encoding is built for the
/// first time. Otherwise [`MaybeError::InvalidState`] is returned.
///
/// # Safety
///
/// `dpw` must be a return value of [`dpw_new`] that [`dpw_drop`] has
/// not yet been called on.
#[no_mangle]
pub unsafe extern "C" fn dpw_add(
dpw: *mut DynamicPolyWatchdog,
lit: c_int,
weight: usize,
) -> MaybeError {
let mut boxed = unsafe { Box::from_raw(dpw) };
if boxed.structure.is_some() {
return MaybeError::InvalidState;
}
boxed.in_lits.insert(
Lit::from_ipasir(lit).expect("invalid IPASIR literal"),
weight,
);
boxed.weight_sum += weight;
Box::into_raw(boxed);
MaybeError::Ok
}
/// Lazily builds the _change in_ pseudo-boolean encoding to enable
/// upper bounds from within the range. A change might only be a change
/// in bounds, the [`DynamicPolyWatchdog`] does not support adding
/// literals at the moment.
///
/// The min and max bounds are inclusive. After a call to
/// [`dpw_encode_ub`] with `min_bound=2` and `max_bound=4` bound
/// including `<= 2` and `<= 4` can be enforced.
///
/// A call to `var_manager` must yield a new variable. The
/// encoding will be returned via the given callback function as
/// 0-terminated clauses (in the same way as IPASIR's `add`).
///
/// # Safety
///
/// `dpw` must be a return value of [`dpw_new`] that [`dpw_drop`] has
/// not yet been called on.
#[no_mangle]
pub unsafe extern "C" fn dpw_encode_ub(
dpw: *mut DynamicPolyWatchdog,
min_bound: usize,
max_bound: usize,
n_vars_used: &mut c_int,
collector: CClauseCollector,
collector_data: *mut c_void,
) {
assert!(min_bound <= max_bound);
let mut collector = ClauseCollector::new(collector, collector_data);
let mut var_manager = VarManager::new(n_vars_used);
let mut boxed = unsafe { Box::from_raw(dpw) };
boxed.encode_ub_change(min_bound..=max_bound, &mut collector, &mut var_manager);
Box::into_raw(boxed);
}
/// Returns assumptions/units for enforcing an upper bound (`sum of lits
/// <= ub`). Make sure that [`dpw_encode_ub`] has been called adequately
/// and nothing has been called afterwards, otherwise
/// [`MaybeError::NotEncoded`] will be returned.
///
/// Assumptions are returned via the collector callback. There is _no_
/// terminating zero, all assumptions are passed when [`dpw_enforce_ub`]
/// returns.
///
/// # Safety
///
/// `dpw` must be a return value of [`dpw_new`] that [`dpw_drop`] has
/// not yet been called on.
#[no_mangle]
pub unsafe extern "C" fn dpw_enforce_ub(
dpw: *mut DynamicPolyWatchdog,
ub: usize,
collector: CAssumpCollector,
collector_data: *mut c_void,
) -> MaybeError {
let boxed = unsafe { Box::from_raw(dpw) };
let ret = match boxed.enforce_ub(ub) {
Ok(assumps) => {
assumps.into_iter().for_each(|l| {
collector(l.to_ipasir(), collector_data);
});
MaybeError::Ok
}
Err(err) => err.into(),
};
Box::into_raw(boxed);
ret
}
/// Gets the next smaller upper bound value that can be encoded without
/// setting tares. This is used for coarse convergence.
///
/// # Safety
///
/// `dpw` must be a return value of [`dpw_new`] that [`dpw_drop`] has
/// not yet been called on.
#[no_mangle]
pub unsafe extern "C" fn dpw_coarse_ub(dpw: *mut DynamicPolyWatchdog, ub: usize) -> usize {
let boxed = unsafe { Box::from_raw(dpw) };
let ret = boxed.coarse_ub(ub);
Box::into_raw(boxed);
ret
}
/// Frees the memory associated with a [`DynamicPolyWatchdog`]
///
/// # Safety
///
/// `dpw` must be a return value of [`dpw_new`] and cannot be used
/// afterwards again.
#[no_mangle]
pub unsafe extern "C" fn dpw_drop(dpw: *mut DynamicPolyWatchdog) {
drop(unsafe { Box::from_raw(dpw) })
}
#[cfg(test)]
mod tests {
use inline_c::assert_c;
#[test]
fn new_drop() {
(assert_c! {
#include <assert.h>
#include "rustsat.h"
int main() {
DynamicPolyWatchdog *dpw = dpw_new();
assert(dpw != NULL);
dpw_drop(dpw);
return 0;
}
})
.success();
}
#[test]
fn basic() {
(assert_c! {
#include <assert.h>
#include "rustsat.h"
void clause_counter(int lit, void *data) {
if (!lit) {
int *cnt = (int *)data;
(*cnt)++;
}
}
int main() {
DynamicPolyWatchdog *dpw = dpw_new();
dpw_add(dpw, 1, 1);
dpw_add(dpw, 2, 1);
dpw_add(dpw, 3, 2);
dpw_add(dpw, 4, 2);
int n_used = 4;
int n_clauses = 0;
dpw_encode_ub(dpw, 0, 6, &n_used, &clause_counter, &n_clauses);
dpw_drop(dpw);
assert(n_used == 13);
assert(n_clauses == 13);
return 0;
}
})
.success();
}
}
}
}