Skip to content

Commit

Permalink
Change formatting of negative numbers from -x to (- x) to be SMTL…
Browse files Browse the repository at this point in the history
…IB2 compliant
  • Loading branch information
oeb25 committed Sep 12, 2024
1 parent 2bd13db commit 787a671
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 32 deletions.
6 changes: 5 additions & 1 deletion lowlevel/src/lexicon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ use crate::parse::{ParseError, Parser, Token};
pub struct Numeral(pub String);
impl std::fmt::Display for Numeral {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
if let Some(rest) = self.0.strip_prefix('-') {
write!(f, "(- {rest})")
} else {
write!(f, "{}", self.0)
}
}
}
impl SmtlibParse for Numeral {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: lowlevel/src/tests.rs
expression: "d.exec(&Command::parse(r#\"(assert (< x 0))\"#)?)?"
---
Success
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: lowlevel/src/tests.rs
expression: "d.exec(&Command::parse(r#\"(check-sat)\"#)?)?"
---
SpecificSuccessResponse(CheckSatResponse(Sat))
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: lowlevel/src/tests.rs
expression: "d.exec(&Command::parse(r#\"(get-model)\"#)?)?"
---
SpecificSuccessResponse(GetModelResponse(GetModelResponse([
DefineFun(FunctionDef(Symbol("x"), [], Sort(Simple(Symbol("Int"))), Application(Identifier(Simple(Symbol("-"))), [
SpecConstant(Numeral(Numeral("2"))),
]))),
])))
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: lowlevel/src/tests.rs
expression: "d.exec(&Command::parse(r#\"(declare-const x Int)\"#)?)?"
---
Success
12 changes: 12 additions & 0 deletions lowlevel/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ mod z3 {

Ok(())
}

#[test]
fn negative_numbers() -> Result<(), Box<dyn std::error::Error>> {
let mut d = Driver::new(Z3Binary::new("z3")?)?;

cmd!(d, r#"(declare-const x Int)"#);
cmd!(d, r#"(assert (< x (- 1)))"#);
cmd!(d, r#"(check-sat)"#);
cmd!(d, r#"(get-model)"#);

Ok(())
}
}

#[cfg(feature = "z3-static")]
Expand Down
31 changes: 2 additions & 29 deletions smtlib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub mod funs;
mod solver;
pub mod sorts;
pub mod terms;
#[cfg(test)]
mod tests;
pub mod theories;

pub use solver::Solver;
Expand Down Expand Up @@ -187,32 +189,3 @@ impl Model {
Some(self.values.get(x.name().trim_matches('|'))?.clone().into())
}
}

#[cfg(test)]
mod tests {
use terms::StaticSorted;

use crate::terms::{forall, Sorted};

use super::*;

#[test]
fn int_math() {
let x = Int::new_const("x");
let y = Int::new_const("hello");
// let x_named = x.labeled();
let mut z = 12 + y * 4;
z += 3;
let w = x * x + z;
println!("{w}");
}

#[test]
fn quantifiers() {
let x = Int::new_const("x");
let y = Int::new_const("y");

let res = forall((x, y), (x + 2)._eq(y));
println!("{}", ast::Term::from(res));
}
}
37 changes: 37 additions & 0 deletions smtlib/src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use terms::StaticSorted;

use crate::terms::{forall, Sorted};

use super::*;

#[test]
fn int_math() {
let x = Int::new_const("x");
let y = Int::new_const("hello");
// let x_named = x.labeled();
let mut z = 12 + y * 4;
z += 3;
let w = x * x + z;
println!("{w}");
}

#[test]
fn quantifiers() {
let x = Int::new_const("x");
let y = Int::new_const("y");

let res = forall((x, y), (x + 2)._eq(y));
println!("{}", ast::Term::from(res));
}

#[test]
fn negative_numbers() {
let mut solver = Solver::new(crate::backend::z3_binary::Z3Binary::new("z3").unwrap()).unwrap();
let x = Int::new_const("x");
solver.assert(x.lt(-1)).unwrap();
let model = solver.check_sat_with_model().unwrap().expect_sat().unwrap();
match model.eval(x) {
Some(x) => println!("This is the value of x: {x}"),
None => panic!("Oh no! This should never happen, as x was part of an assert"),
}
}
7 changes: 5 additions & 2 deletions smtlib/src/theories/ints.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![doc = concat!("```ignore\n", include_str!("./Ints.smt2"), "```")]

use smtlib_lowlevel::ast::Term;
use smtlib_lowlevel::{ast::Term, lexicon::Numeral};

use crate::{
impl_op,
Expand Down Expand Up @@ -54,7 +54,10 @@ impl StaticSorted for Int {
}
impl From<i64> for Int {
fn from(i: i64) -> Self {
Term::Identifier(qual_ident(i.to_string(), None)).into()
Term::SpecConstant(smtlib_lowlevel::ast::SpecConstant::Numeral(Numeral(
i.to_string(),
)))
.into()
}
}
impl Int {
Expand Down

0 comments on commit 787a671

Please sign in to comment.