use sail_api::runtime::Limits;
use sail_smtlib::{
lex::TokenKind,
sexpr::{SExpr, SExprKind},
};
pub(crate) trait Backend: Sync + std::fmt::Debug {
fn name(&self) -> &'static str;
fn label(&self) -> &'static str;
fn pin(&self, pins: &toml::Value, host: &str) -> Result<Option<(String, String)>, ()>;
fn version_matches(&self, stdout: &str, version: &str) -> bool;
fn args(&self, limits: &Limits) -> Vec<String>;
fn key_policy(&self) -> [&'static str; 2];
fn option_admitted(&self, key: &[u8], value: &[u8], limit: u64) -> bool;
fn reason_unknown_value(&self, value: &SExpr) -> bool;
}
fn numeral(value: &[u8]) -> Option<u64> {
std::str::from_utf8(value).ok()?.parse().ok()
}
#[derive(Debug)]
pub(crate) struct Z3;
impl Backend for Z3 {
fn name(&self) -> &'static str {
"z3"
}
fn label(&self) -> &'static str {
"bundled Z3"
}
fn pin(&self, pins: &toml::Value, host: &str) -> Result<Option<(String, String)>, ()> {
let version = pins
.get("z3")
.and_then(|z3| z3.get("version"))
.and_then(toml::Value::as_str);
let digest = pins
.get("assets")
.and_then(|a| a.get(host))
.and_then(|a| a.get("z3_sha256"))
.and_then(toml::Value::as_str);
match (version, digest) {
(Some(version), Some(digest)) if digest.len() == 64 => {
Ok(Some((version.into(), digest.into())))
}
_ => Err(()),
}
}
fn version_matches(&self, stdout: &str, version: &str) -> bool {
stdout
.split_whitespace()
.take(3)
.eq(["Z3", "version", version])
}
fn args(&self, limits: &Limits) -> Vec<String> {
vec![
"-smt2".into(),
"-in".into(),
"smt.random_seed=0".into(),
"sat.random_seed=0".into(),
format!("rlimit={}", limits.resource_limit),
]
}
fn key_policy(&self) -> [&'static str; 2] {
["smt.random_seed=0", "sat.random_seed=0"]
}
fn option_admitted(&self, key: &[u8], value: &[u8], limit: u64) -> bool {
match key {
b":print-success" => value == b"false",
b":produce-models" | b":produce-unsat-cores" => matches!(value, b"true" | b"false"),
b":smt.random_seed" | b":sat.random_seed" => value == b"0",
b":rlimit" => numeral(value) == Some(limit),
_ => false,
}
}
fn reason_unknown_value(&self, value: &SExpr) -> bool {
matches!(value.kind, SExprKind::Atom(TokenKind::String))
}
}
#[derive(Debug)]
pub(crate) struct Cvc5;
impl Backend for Cvc5 {
fn name(&self) -> &'static str {
"cvc5"
}
fn label(&self) -> &'static str {
"pinned cvc5"
}
fn pin(&self, pins: &toml::Value, host: &str) -> Result<Option<(String, String)>, ()> {
let Some(cvc5) = pins.get("cvc5") else {
return Ok(None);
};
let version = cvc5.get("version").and_then(toml::Value::as_str);
let Some(asset) = cvc5.get("assets").and_then(|a| a.get(host)) else {
return Ok(None);
};
let digest = asset.get("bin_sha256").and_then(toml::Value::as_str);
match (version, digest) {
(Some(version), Some(digest)) if digest.len() == 64 => {
Ok(Some((version.into(), digest.into())))
}
_ => Err(()),
}
}
fn version_matches(&self, stdout: &str, version: &str) -> bool {
stdout.split_whitespace().take(2).eq(["cvc5", version])
}
fn args(&self, limits: &Limits) -> Vec<String> {
vec![
"--lang=smt2".into(),
"-q".into(),
"--incremental".into(),
"--seed=0".into(),
"--sat-random-seed=0".into(),
format!("--rlimit-per={}", limits.resource_limit),
]
}
fn key_policy(&self) -> [&'static str; 2] {
[
"cvc5-argv-v1:--lang=smt2 -q --incremental --rlimit-per",
"--seed=0 --sat-random-seed=0",
]
}
fn option_admitted(&self, key: &[u8], value: &[u8], limit: u64) -> bool {
match key {
b":print-success" => value == b"false",
b":produce-models" | b":produce-unsat-cores" => matches!(value, b"true" | b"false"),
b":seed" | b":random-seed" | b":sat-random-seed" => value == b"0",
b":rlimit-per" | b":reproducible-resource-limit" => numeral(value) == Some(limit),
_ => false,
}
}
fn reason_unknown_value(&self, value: &SExpr) -> bool {
matches!(
value.kind,
SExprKind::Atom(TokenKind::Symbol | TokenKind::String)
)
}
}
pub(crate) const ALL: [&dyn Backend; 2] = [&Z3, &Cvc5];
pub(crate) fn named(name: &str) -> Option<&'static dyn Backend> {
ALL.into_iter().find(|backend| backend.name() == name)
}