1use crate::ir::{BitVecOp, VerifyExpr, VerifyOp, VerifyType};
6use std::collections::HashSet;
7
8pub fn to_smtlib2(expr: &VerifyExpr, declarations: &[(&str, VerifyType)]) -> String {
10 let mut output = String::new();
11 output.push_str("(set-logic ALL)\n");
12
13 for (name, ty) in declarations {
15 let sort = type_to_smtlib(ty);
16 output.push_str(&format!("(declare-fun {} () {})\n", name, sort));
17 }
18
19 let mut vars = HashSet::new();
21 crate::equivalence::collect_vars_pub(expr, &mut vars);
22 let declared: HashSet<String> = declarations.iter().map(|(n, _)| n.to_string()).collect();
23 for var in &vars {
24 if !declared.contains(var) {
25 output.push_str(&format!("(declare-fun {} () Int)\n", var));
26 }
27 }
28
29 output.push_str(&format!("(assert {})\n", expr_to_smtlib(expr)));
30 output.push_str("(check-sat)\n");
31 output.push_str("(get-model)\n");
32 output
33}
34
35pub fn equivalence_to_smtlib2(a: &VerifyExpr, b: &VerifyExpr) -> String {
37 let mut output = String::new();
38 output.push_str("(set-logic ALL)\n");
39
40 let mut vars = HashSet::new();
41 crate::equivalence::collect_vars_pub(a, &mut vars);
42 crate::equivalence::collect_vars_pub(b, &mut vars);
43 for var in &vars {
44 output.push_str(&format!("(declare-fun {} () Int)\n", var));
45 }
46
47 let a_smt = expr_to_smtlib(a);
48 let b_smt = expr_to_smtlib(b);
49 output.push_str(&format!("(assert (not (= {} {})))\n", a_smt, b_smt));
50 output.push_str("(check-sat)\n");
51 output
52}
53
54fn type_to_smtlib(ty: &VerifyType) -> String {
55 match ty {
56 VerifyType::Int => "Int".into(),
57 VerifyType::Bool => "Bool".into(),
58 VerifyType::Object => "Int".into(),
59 VerifyType::Real => "Real".into(),
60 VerifyType::BitVector(w) => format!("(_ BitVec {})", w),
61 VerifyType::Array(idx, elem) => {
62 format!("(Array {} {})", type_to_smtlib(idx), type_to_smtlib(elem))
63 }
64 }
65}
66
67fn expr_to_smtlib(expr: &VerifyExpr) -> String {
68 match expr {
69 VerifyExpr::Bool(true) => "true".into(),
70 VerifyExpr::Bool(false) => "false".into(),
71 VerifyExpr::Int(n) => {
72 if *n < 0 {
73 format!("(- {})", -n)
74 } else {
75 n.to_string()
76 }
77 }
78 VerifyExpr::Var(name) => name.clone(),
79 VerifyExpr::Binary { op, left, right } => {
80 let l = expr_to_smtlib(left);
81 let r = expr_to_smtlib(right);
82 let op_str = match op {
83 VerifyOp::Add => "+",
84 VerifyOp::Sub => "-",
85 VerifyOp::Mul => "*",
86 VerifyOp::Div => "div",
87 VerifyOp::FloorDiv => {
89 return format!("(to_int (/ (to_real {}) (to_real {})))", l, r)
90 }
91 VerifyOp::Eq => "=",
92 VerifyOp::Neq => return format!("(not (= {} {}))", l, r),
93 VerifyOp::Gt => ">",
94 VerifyOp::Lt => "<",
95 VerifyOp::Gte => ">=",
96 VerifyOp::Lte => "<=",
97 VerifyOp::And => "and",
98 VerifyOp::Or => "or",
99 VerifyOp::Implies => "=>",
100 };
101 format!("({} {} {})", op_str, l, r)
102 }
103 VerifyExpr::Not(inner) => format!("(not {})", expr_to_smtlib(inner)),
104 VerifyExpr::Iff(l, r) => format!("(= {} {})", expr_to_smtlib(l), expr_to_smtlib(r)),
105 VerifyExpr::ForAll { vars, body } => {
106 let bindings: Vec<String> = vars
107 .iter()
108 .map(|(n, t)| format!("({} {})", n, type_to_smtlib(t)))
109 .collect();
110 format!("(forall ({}) {})", bindings.join(" "), expr_to_smtlib(body))
111 }
112 VerifyExpr::Exists { vars, body } => {
113 let bindings: Vec<String> = vars
114 .iter()
115 .map(|(n, t)| format!("({} {})", n, type_to_smtlib(t)))
116 .collect();
117 format!("(exists ({}) {})", bindings.join(" "), expr_to_smtlib(body))
118 }
119 VerifyExpr::Apply { name, args } | VerifyExpr::ApplyInt { name, args } => {
120 let arg_strs: Vec<String> = args.iter().map(expr_to_smtlib).collect();
121 if arg_strs.is_empty() {
122 name.clone()
123 } else {
124 format!("({} {})", name, arg_strs.join(" "))
125 }
126 }
127 VerifyExpr::BitVecConst { width, value } => {
128 format!("#x{:0>width$X}", value, width = ((*width + 3) / 4) as usize)
129 }
130 VerifyExpr::BitVecBinary { op, left, right } => {
131 let l = expr_to_smtlib(left);
132 let r = expr_to_smtlib(right);
133 let op_str = match op {
134 BitVecOp::And => "bvand",
135 BitVecOp::Or => "bvor",
136 BitVecOp::Xor => "bvxor",
137 BitVecOp::Not => return format!("(bvnot {})", l),
138 BitVecOp::Shl => "bvshl",
139 BitVecOp::Shr => "bvlshr",
140 BitVecOp::AShr => "bvashr",
141 BitVecOp::Add => "bvadd",
142 BitVecOp::Sub => "bvsub",
143 BitVecOp::Mul => "bvmul",
144 BitVecOp::SDiv => "bvsdiv",
145 BitVecOp::SRem => "bvsrem",
146 BitVecOp::ULt => "bvult",
147 BitVecOp::SLt => "bvslt",
148 BitVecOp::ULe => "bvule",
149 BitVecOp::SLe => "bvsle",
150 BitVecOp::Eq => return format!("(= {} {})", l, r),
151 };
152 format!("({} {} {})", op_str, l, r)
153 }
154 VerifyExpr::BitVecExtract { high, low, operand } => {
155 format!("((_ extract {} {}) {})", high, low, expr_to_smtlib(operand))
156 }
157 VerifyExpr::BitVecConcat(l, r) => {
158 format!("(concat {} {})", expr_to_smtlib(l), expr_to_smtlib(r))
159 }
160 VerifyExpr::Select { array, index } => {
161 format!(
162 "(select {} {})",
163 expr_to_smtlib(array),
164 expr_to_smtlib(index)
165 )
166 }
167 VerifyExpr::Store {
168 array,
169 index,
170 value,
171 } => {
172 format!(
173 "(store {} {} {})",
174 expr_to_smtlib(array),
175 expr_to_smtlib(index),
176 expr_to_smtlib(value)
177 )
178 }
179 VerifyExpr::AtState { expr, .. } => expr_to_smtlib(expr),
180 VerifyExpr::Transition { from, to } => {
181 format!("(and {} {})", expr_to_smtlib(from), expr_to_smtlib(to))
182 }
183 }
184}