Check that a Lean-side Expr matches a zero Expr structurally.
( lean_expr: &lean::Expr, zero_expr: &KExpr<Anon>, nr: &NameResolver, )
| 60 | |
| 61 | /// Check that a Lean-side Expr matches a zero Expr<Anon> structurally. |
| 62 | pub fn expr_congruent( |
| 63 | lean_expr: &lean::Expr, |
| 64 | zero_expr: &KExpr<Anon>, |
| 65 | nr: &NameResolver, |
| 66 | ) -> Result<(), String> { |
| 67 | use lean::ExprData as LE; |
| 68 | match (lean_expr.as_data(), zero_expr.data()) { |
| 69 | (LE::Bvar(n, _), ExprData::Var(m, _, _)) => { |
| 70 | let n = n.to_u64().unwrap_or(u64::MAX); |
| 71 | if n == *m { |
| 72 | Ok(()) |
| 73 | } else { |
| 74 | Err(format!("var mismatch: lean={n} vs zero={m}")) |
| 75 | } |
| 76 | }, |
| 77 | |
| 78 | (LE::Sort(l, _), ExprData::Sort(u, _)) => level_congruent(l, u, nr), |
| 79 | |
| 80 | (LE::Const(name, levels, _), ExprData::Const(id, univs, _)) => { |
| 81 | match nr.resolve(name) { |
| 82 | Some(expected) if expected == &id.addr => {}, |
| 83 | Some(expected) => { |
| 84 | return Err(format!( |
| 85 | "const address mismatch for {name}: expected {}, got {}", |
| 86 | expected.hex(), |
| 87 | id.addr.hex() |
| 88 | )); |
| 89 | }, |
| 90 | None => { |
| 91 | return Err(format!("const name not found in resolver: {name}")); |
| 92 | }, |
| 93 | } |
| 94 | if levels.len() != univs.len() { |
| 95 | return Err(format!( |
| 96 | "const {name}: level count mismatch: {} vs {}", |
| 97 | levels.len(), |
| 98 | univs.len() |
| 99 | )); |
| 100 | } |
| 101 | for (l, u) in levels.iter().zip(univs.iter()) { |
| 102 | level_congruent(l, u, nr)?; |
| 103 | } |
| 104 | Ok(()) |
| 105 | }, |
| 106 | |
| 107 | (LE::App(f1, a1, _), ExprData::App(f2, a2, _)) => { |
| 108 | expr_congruent(f1, f2, nr)?; |
| 109 | expr_congruent(a1, a2, nr) |
| 110 | }, |
| 111 | |
| 112 | (LE::Lam(_, ty1, body1, _, _), ExprData::Lam(_, _, ty2, body2, _)) |
| 113 | | (LE::ForallE(_, ty1, body1, _, _), ExprData::All(_, _, ty2, body2, _)) => |
| 114 | { |
| 115 | expr_congruent(ty1, ty2, nr)?; |
| 116 | expr_congruent(body1, body2, nr) |
| 117 | }, |
| 118 | |
| 119 | ( |