logicaffeine_compile/codegen_sva/
verify_to_kernel.rs1use super::sva_to_verify::BoundedExpr;
10use logicaffeine_kernel::{Literal, Term};
11
12pub fn encode_bounded_expr(expr: &BoundedExpr) -> Term {
25 match expr {
26 BoundedExpr::Bool(true) => Term::Global("True".to_string()),
27 BoundedExpr::Bool(false) => Term::Global("False".to_string()),
28
29 BoundedExpr::Int(n) => Term::Lit(Literal::Int(*n)),
30
31 BoundedExpr::Var(name) => {
32 if let Some(at_pos) = name.find('@') {
33 let sig = &name[..at_pos];
34 let t: i64 = name[at_pos + 1..].parse().unwrap_or(0);
35 Term::App(
37 Box::new(Term::Global(sig.to_string())),
38 Box::new(Term::Lit(Literal::Int(t))),
39 )
40 } else {
41 Term::Var(name.clone())
42 }
43 }
44
45 BoundedExpr::And(left, right) => {
46 let l = encode_bounded_expr(left);
47 let r = encode_bounded_expr(right);
48 Term::App(
49 Box::new(Term::App(
50 Box::new(Term::Global("And".to_string())),
51 Box::new(l),
52 )),
53 Box::new(r),
54 )
55 }
56
57 BoundedExpr::Or(left, right) => {
58 let l = encode_bounded_expr(left);
59 let r = encode_bounded_expr(right);
60 Term::App(
61 Box::new(Term::App(
62 Box::new(Term::Global("Or".to_string())),
63 Box::new(l),
64 )),
65 Box::new(r),
66 )
67 }
68
69 BoundedExpr::Not(inner) => {
70 let e = encode_bounded_expr(inner);
71 Term::App(Box::new(Term::Global("Not".to_string())), Box::new(e))
72 }
73
74 BoundedExpr::Implies(left, right) => {
75 let l = encode_bounded_expr(left);
76 let r = encode_bounded_expr(right);
77 Term::Pi {
79 param: "_".to_string(),
80 param_type: Box::new(l),
81 body_type: Box::new(r),
82 }
83 }
84
85 BoundedExpr::Eq(left, right) => {
86 let l = encode_bounded_expr(left);
87 let r = encode_bounded_expr(right);
88 Term::App(
90 Box::new(Term::App(
91 Box::new(Term::App(
92 Box::new(Term::Global("Eq".to_string())),
93 Box::new(Term::Hole),
94 )),
95 Box::new(l),
96 )),
97 Box::new(r),
98 )
99 }
100
101 BoundedExpr::Lt(left, right) => {
102 let l = encode_bounded_expr(left);
103 let r = encode_bounded_expr(right);
104 Term::App(
105 Box::new(Term::App(
106 Box::new(Term::Global("lt".to_string())),
107 Box::new(l),
108 )),
109 Box::new(r),
110 )
111 }
112
113 BoundedExpr::Gt(left, right) => {
114 let l = encode_bounded_expr(left);
115 let r = encode_bounded_expr(right);
116 Term::App(
117 Box::new(Term::App(
118 Box::new(Term::Global("gt".to_string())),
119 Box::new(l),
120 )),
121 Box::new(r),
122 )
123 }
124
125 BoundedExpr::Lte(left, right) => {
126 let l = encode_bounded_expr(left);
127 let r = encode_bounded_expr(right);
128 Term::App(
129 Box::new(Term::App(
130 Box::new(Term::Global("le".to_string())),
131 Box::new(l),
132 )),
133 Box::new(r),
134 )
135 }
136
137 BoundedExpr::Gte(left, right) => {
138 let l = encode_bounded_expr(left);
139 let r = encode_bounded_expr(right);
140 Term::App(
141 Box::new(Term::App(
142 Box::new(Term::Global("ge".to_string())),
143 Box::new(l),
144 )),
145 Box::new(r),
146 )
147 }
148
149 BoundedExpr::Unsupported(msg) => {
150 let _ = msg;
152 Term::Global("False".to_string())
153 }
154
155 BoundedExpr::BitVecConst { value, .. } => Term::Lit(Literal::Int(*value as i64)),
157 BoundedExpr::BitVecVar(name, _) => Term::Var(name.clone()),
158 BoundedExpr::BitVecBinary { op, left, right } => {
159 let l = encode_bounded_expr(left);
160 let r = encode_bounded_expr(right);
161 let op_name = match op {
162 super::sva_to_verify::BitVecBoundedOp::And => "bit_and",
163 super::sva_to_verify::BitVecBoundedOp::Or => "bit_or",
164 super::sva_to_verify::BitVecBoundedOp::Xor => "bit_xor",
165 super::sva_to_verify::BitVecBoundedOp::Add => "bv_add",
166 super::sva_to_verify::BitVecBoundedOp::Sub => "bv_sub",
167 _ => "bv_op",
168 };
169 Term::App(
170 Box::new(Term::App(
171 Box::new(Term::Global(op_name.to_string())),
172 Box::new(l),
173 )),
174 Box::new(r),
175 )
176 }
177 BoundedExpr::BitVecExtract { operand, .. } => encode_bounded_expr(operand),
178 BoundedExpr::BitVecConcat(l, r) => {
179 let left = encode_bounded_expr(l);
180 let right = encode_bounded_expr(r);
181 Term::App(
182 Box::new(Term::App(
183 Box::new(Term::Global("bv_concat".to_string())),
184 Box::new(left),
185 )),
186 Box::new(right),
187 )
188 }
189 BoundedExpr::BitVecIte { cond, then, else_ } => Term::App(
193 Box::new(Term::App(
194 Box::new(Term::App(
195 Box::new(Term::Global("bv_ite".to_string())),
196 Box::new(encode_bounded_expr(cond)),
197 )),
198 Box::new(encode_bounded_expr(then)),
199 )),
200 Box::new(encode_bounded_expr(else_)),
201 ),
202 BoundedExpr::ArraySelect { array, index } => {
203 let a = encode_bounded_expr(array);
204 let i = encode_bounded_expr(index);
205 Term::App(
206 Box::new(Term::App(
207 Box::new(Term::Global("select".to_string())),
208 Box::new(a),
209 )),
210 Box::new(i),
211 )
212 }
213 BoundedExpr::ArrayStore {
214 array,
215 index,
216 value,
217 } => {
218 let a = encode_bounded_expr(array);
219 let i = encode_bounded_expr(index);
220 let v = encode_bounded_expr(value);
221 Term::App(
222 Box::new(Term::App(
223 Box::new(Term::App(
224 Box::new(Term::Global("store".to_string())),
225 Box::new(a),
226 )),
227 Box::new(i),
228 )),
229 Box::new(v),
230 )
231 }
232 BoundedExpr::IntBinary { op, left, right } => {
233 let l = encode_bounded_expr(left);
234 let r = encode_bounded_expr(right);
235 let op_name = match op {
236 super::sva_to_verify::ArithBoundedOp::Add => "plus",
237 super::sva_to_verify::ArithBoundedOp::Sub => "minus",
238 super::sva_to_verify::ArithBoundedOp::Mul => "mult",
239 super::sva_to_verify::ArithBoundedOp::Div => "div",
240 };
241 Term::App(
242 Box::new(Term::App(
243 Box::new(Term::Global(op_name.to_string())),
244 Box::new(l),
245 )),
246 Box::new(r),
247 )
248 }
249 BoundedExpr::Comparison { op, left, right } => {
250 let l = encode_bounded_expr(left);
251 let r = encode_bounded_expr(right);
252 let op_name = match op {
253 super::sva_to_verify::CmpBoundedOp::Gt => "gt",
254 super::sva_to_verify::CmpBoundedOp::Lt => "lt",
255 super::sva_to_verify::CmpBoundedOp::Gte => "ge",
256 super::sva_to_verify::CmpBoundedOp::Lte => "le",
257 };
258 Term::App(
259 Box::new(Term::App(
260 Box::new(Term::Global(op_name.to_string())),
261 Box::new(l),
262 )),
263 Box::new(r),
264 )
265 }
266 BoundedExpr::ForAll { var, body, .. } => {
267 let b = encode_bounded_expr(body);
268 Term::Pi {
269 param: var.clone(),
270 param_type: Box::new(Term::Hole),
271 body_type: Box::new(b),
272 }
273 }
274 BoundedExpr::Exists { var, body, .. } => {
275 let b = encode_bounded_expr(body);
276 Term::App(
277 Box::new(Term::Global("Ex".to_string())),
278 Box::new(Term::Lambda {
279 param: var.clone(),
280 param_type: Box::new(Term::Hole),
281 body: Box::new(b),
282 }),
283 )
284 }
285 BoundedExpr::Apply { name, args } => {
286 let mut term = Term::Global(name.clone());
288 for arg in args {
289 term = Term::App(Box::new(term), Box::new(encode_bounded_expr(arg)));
290 }
291 term
292 }
293 }
294}
295
296#[allow(dead_code)]
298fn nat_literal(n: usize) -> Term {
299 let mut term = Term::Global("Zero".to_string());
300 for _ in 0..n {
301 term = Term::App(Box::new(Term::Global("Succ".to_string())), Box::new(term));
302 }
303 term
304}