prove: restricted formula, program models and verified Lean backend
- formula.rs: the only math parser — fixed EBNF, typed Int/Prop AST, bounds, exact diagnostics, Lean target generation and counterexample evaluation - model.rs: complete whitelist validation of the finite exact-integer program subset and deterministic @[expose] Lean model mapping - lean.rs: pinned backend discovery, trusted generated search template, stock lean4export proof payload, independent con-leche kernel replay, expected-proposition regeneration and axiom audit - Replay.lean: verifier executable (type alpha-equivalence, axiom closure, metavariable rejection) without importing any proof olean - prove.rs/main.rs: single check/replay orchestration, reports, exit codes - real E2E: algebra/vector/arithmetic fixtures verify and publish bundles
This commit is contained in:
1 parent
3a529b923d
commit
4e02da95a4
20 files changed
+9077
-27
No files matched your search
@@ -0,0 +1,230 @@
|
||||
//! Contract tests for the restricted formula fragment (docs/DEVELOPMENT.md §4).
|
||||
//!
|
||||
//! These tests pin the *acceptance behaviour* of the only math parser in the
|
||||
//! tool: grammar, precedence, bounds, diagnostics for unsupported constructs,
|
||||
//! exact Lean target wording and exact normalized report wording.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use num_bigint::BigInt;
|
||||
|
||||
fn int(value: i64) -> BigInt {
|
||||
BigInt::from(value)
|
||||
}
|
||||
|
||||
use marqdo_prove::codes;
|
||||
use marqdo_prove::formula::{self, IntExpr, PropExpr, Rel};
|
||||
use marqdo_prove::Span;
|
||||
|
||||
fn parse(literal: &str) -> formula::Goal {
|
||||
formula::parse(literal, Span::new(0, literal.len())).expect(literal)
|
||||
}
|
||||
|
||||
fn lean(goal: &formula::Goal) -> String {
|
||||
goal.lean_prop(&BTreeMap::new()).expect("lean")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fixture_vector_proposition() {
|
||||
let goal = parse("\\forall x,y\\in\\mathbb{Z},\\quad x^2+y^2\\geq0\n");
|
||||
assert_eq!(goal.vars, vec!["x", "y"]);
|
||||
assert_eq!(
|
||||
lean(&goal),
|
||||
"∀ mqx0 mqx1 : Int, (((mqx0 ^ (2 : Nat)) + (mqx1 ^ (2 : Nat))) ≥ (0 : Int))"
|
||||
);
|
||||
assert_eq!(goal.normal(), "∀ x y : ℤ, x^2 + y^2 ≥ 0");
|
||||
assert!(goal.premises().is_empty());
|
||||
assert_eq!(goal.evaluate(&[int(3), int(-4)]), Some(true));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fixture_nonnegative_addition_preserves_premises() {
|
||||
let goal = parse("\\forall a,b\\in\\mathbb{Z},\\quad (a\\geq0\\land b\\geq0)\\Rightarrow a+b\\geq0");
|
||||
assert_eq!(goal.vars, vec!["a", "b"]);
|
||||
assert_eq!(
|
||||
lean(&goal),
|
||||
"∀ mqx0 mqx1 : Int, (((mqx0 ≥ (0 : Int)) ∧ (mqx1 ≥ (0 : Int))) → \
|
||||
((mqx0 + mqx1) ≥ (0 : Int)))"
|
||||
);
|
||||
assert_eq!(
|
||||
goal.normal(),
|
||||
"∀ a b : ℤ, (a ≥ 0 ∧ b ≥ 0) → a + b ≥ 0"
|
||||
);
|
||||
assert_eq!(goal.premises(), vec!["a ≥ 0 ∧ b ≥ 0"]);
|
||||
assert_eq!(goal.evaluate(&[int(-2), int(1)]), Some(true));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn precedence_unary_minus_and_power() {
|
||||
let goal = parse("\\forall x\\in\\mathbb{Z},\\quad -x^2\\leq0");
|
||||
assert_eq!(
|
||||
lean(&goal),
|
||||
"∀ mqx0 : Int, ((- (mqx0 ^ (2 : Nat))) ≤ (0 : Int))"
|
||||
);
|
||||
let goal = parse("\\forall x\\in\\mathbb{Z},\\quad (-x)^2\\geq0");
|
||||
assert_eq!(
|
||||
lean(&goal),
|
||||
"∀ mqx0 : Int, (((- mqx0) ^ (2 : Nat)) ≥ (0 : Int))"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn associativity_and_bracing() {
|
||||
let goal = parse("\\forall a,b,c\\in\\mathbb{Z},\\quad a-b-c=a-(b-c)");
|
||||
assert_eq!(
|
||||
lean(&goal),
|
||||
"∀ mqx0 mqx1 mqx2 : Int, (((mqx0 - mqx1) - mqx2) = (mqx0 - (mqx1 - mqx2)))"
|
||||
);
|
||||
let goal = parse(r"(1=1)\Rightarrow(2=2)\Rightarrow(3=3)");
|
||||
// Right-associative implication: a=1 → (b=2 → c=3).
|
||||
match &goal.body {
|
||||
PropExpr::Imp(_, right) => assert!(matches!(**right, PropExpr::Imp(_, _))),
|
||||
other => panic!("expected implication, got {other:?}"),
|
||||
}
|
||||
let goal = parse("\\forall a,b\\in\\mathbb{Z},\\quad (a=1)\\land(b=2)");
|
||||
match &goal.body {
|
||||
PropExpr::And(left, right) => {
|
||||
assert!(matches!(**left, PropExpr::Cmp(Rel::Eq, _, _)));
|
||||
assert!(matches!(**right, PropExpr::Cmp(Rel::Eq, _, _)));
|
||||
}
|
||||
other => panic!("expected conjunction, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exponent_rules() {
|
||||
let goal = parse("\\forall x\\in\\mathbb{Z},\\quad x^{12}\\geq0");
|
||||
assert_eq!(
|
||||
lean(&goal),
|
||||
"∀ mqx0 : Int, ((mqx0 ^ (12 : Nat)) ≥ (0 : Int))"
|
||||
);
|
||||
let error = formula::parse("\\forall x\\in\\mathbb{Z},\\quad x^12\\geq0", Span::new(0, 1))
|
||||
.unwrap_err();
|
||||
assert_eq!(error.code(), codes::FORMULA_SYNTAX, "{error:?}");
|
||||
let error = formula::parse("\\forall x\\in\\mathbb{Z},\\quad x^{17}\\geq0", Span::new(0, 1))
|
||||
.unwrap_err();
|
||||
assert_eq!(error.code(), codes::FORMULA_UNSUPPORTED, "{error:?}");
|
||||
let error = formula::parse("\\forall x\\in\\mathbb{Z},\\quad x^{999999999}\\geq0", Span::new(0, 1))
|
||||
.unwrap_err();
|
||||
assert_eq!(error.code(), codes::FORMULA_UNSUPPORTED, "{error:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsupported_and_invalid_inputs_are_rejected() {
|
||||
let cases: &[(&str, &str)] = &[
|
||||
("x^2\\geq0", codes::FORMULA_FREE_VARIABLE),
|
||||
("\\forall x\\in\\mathbb{R}, x^2\\geq0", codes::FORMULA_UNSUPPORTED),
|
||||
("\\forall x\\in\\mathbb{N}, x^2\\geq0", codes::FORMULA_UNSUPPORTED),
|
||||
("\\forall x\\in\\mathbb{Z}, 2x\\geq0", codes::FORMULA_SYNTAX),
|
||||
("\\forall x\\in\\mathbb{Z}, 0<x<1", codes::FORMULA_SYNTAX),
|
||||
("\\forall x\\in\\mathbb{Z}, x^2\\geq0\\ \\forall y, y\\geq0", codes::FORMULA_UNSUPPORTED),
|
||||
("\\forall x\\in\\mathbb{Z}, \\frac{1}{2}\\geq0", codes::FORMULA_UNSUPPORTED),
|
||||
("\\forall x\\in\\mathbb{Z}, \\exists y, y\\geq0", codes::FORMULA_UNSUPPORTED),
|
||||
("\\forall x\\in\\mathbb{Z}, \\neg x", codes::FORMULA_SYNTAX),
|
||||
("\\forall x\\in\\mathbb{Z}, x^2", codes::FORMULA_SYNTAX),
|
||||
("\\forall x\\in\\mathbb{Z} x^2\\geq0", codes::FORMULA_SYNTAX),
|
||||
("\\forall x,x\\in\\mathbb{Z}, x\\geq0", codes::FORMULA_SYNTAX),
|
||||
("\\forall x\\in\\mathbb{Z},", codes::FORMULA_SYNTAX),
|
||||
("", codes::FORMULA_SYNTAX),
|
||||
(" ", codes::FORMULA_SYNTAX),
|
||||
("\\forall x\\in\\mathbb{Z}, (x\\geq0", codes::FORMULA_SYNTAX),
|
||||
("\\forall x\\in\\mathbb{Z}, x\\geq0)", codes::FORMULA_SYNTAX),
|
||||
("\\forall x\\in\\mathbb{Z}, x\\geq0 y\\geq0", codes::FORMULA_SYNTAX),
|
||||
("\\forall x\\in\\mathbb{Z}, x\\neq y", codes::FORMULA_FREE_VARIABLE),
|
||||
("\\frac{1}{2}\\geq0", codes::FORMULA_UNSUPPORTED),
|
||||
("\\forall x\\in\\mathbb{Z}, \\operatorname{}(x)\\geq0", codes::FORMULA_SYNTAX),
|
||||
];
|
||||
for (literal, expected) in cases {
|
||||
let error = formula::parse(literal, Span::new(0, literal.len())).unwrap_err();
|
||||
assert_eq!(error.code(), *expected, "input {literal:?}: {error:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn variable_and_literal_budgets() {
|
||||
let mut vars = Vec::new();
|
||||
for index in 0..17 {
|
||||
vars.push(format!("x{index}"));
|
||||
}
|
||||
let literal = format!(
|
||||
"\\forall {}\\in\\mathbb{{Z}},\\quad {} \\geq 0",
|
||||
vars.join(","),
|
||||
vars[0]
|
||||
);
|
||||
let error = formula::parse(&literal, Span::new(0, 1)).unwrap_err();
|
||||
assert_eq!(error.code(), codes::FORMULA_SYNTAX, "{error:?}");
|
||||
let huge = "9".repeat(5000);
|
||||
let error = formula::parse(
|
||||
&format!("\\forall x\\in\\mathbb{{Z}},\\quad x\\geq {huge}"),
|
||||
Span::new(0, 1),
|
||||
)
|
||||
.unwrap_err();
|
||||
assert_eq!(error.code(), codes::FORMULA_SYNTAX, "{error:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn model_calls_require_explicit_alias_binding() {
|
||||
let goal = parse("\\forall x\\in\\mathbb{Z},\\quad \\operatorname{square}(x)\\geq0");
|
||||
assert_eq!(
|
||||
goal.used_aliases().into_iter().collect::<Vec<_>>(),
|
||||
vec!["square".to_string()]
|
||||
);
|
||||
let mut aliases = BTreeMap::new();
|
||||
aliases.insert("square".to_string(), "marqdoModel_ab12".to_string());
|
||||
assert_eq!(
|
||||
goal.lean_prop(&aliases).unwrap(),
|
||||
"∀ mqx0 : Int, ((marqdoModel_ab12 mqx0) ≥ (0 : Int))"
|
||||
);
|
||||
assert_eq!(goal.normal(), "∀ x : ℤ, square(x) ≥ 0");
|
||||
let error = formula::parse(
|
||||
"\\forall x\\in\\mathbb{Z},\\quad \\operatorname{f}(x,\\operatorname{g}(x))\\geq0",
|
||||
Span::new(0, 1),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(error.used_aliases().len(), 2);
|
||||
assert!(error.evaluate(&[int(1)]).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn evaluate_is_exact_arbitrary_precision() {
|
||||
let goal = parse("\\forall x\\in\\mathbb{Z},\\quad x\\cdot x\\geq0");
|
||||
assert_eq!(goal.evaluate(&[int(0)]), Some(true));
|
||||
assert_eq!(goal.evaluate(&[int(-123456789)]), Some(true));
|
||||
let goal = parse("\\forall x\\in\\mathbb{Z},\\quad x^2<0");
|
||||
assert_eq!(goal.evaluate(&[int(0)]), Some(false));
|
||||
assert_eq!(goal.evaluate(&["9".repeat(200).parse().unwrap()]), Some(false));
|
||||
let goal = parse("\\forall a,b\\in\\mathbb{Z},\\quad (a>0\\land b>0)\\Rightarrow a\\cdot b>0");
|
||||
assert_eq!(
|
||||
goal.evaluate(&[int(2), int(3)]),
|
||||
Some(true)
|
||||
);
|
||||
// If the antecedent fails, the conditional proposition is vacuously true;
|
||||
// this is exactly why reports preserve and display the whole premise.
|
||||
assert_eq!(goal.evaluate(&[int(0), int(3)]), Some(true));
|
||||
let implication = parse("\\forall a\\in\\mathbb{Z},\\quad a>0\\Rightarrow a>10");
|
||||
assert_eq!(implication.evaluate(&[int(5)]), Some(false));
|
||||
assert_eq!(implication.evaluate(&[int(0)]), Some(true));
|
||||
assert_eq!(goal.evaluate(&[int(1)]), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn typed_ast_is_public_and_structural() {
|
||||
let goal = parse("\\forall x\\in\\mathbb{Z},\\quad x\\geq0");
|
||||
match &goal.body {
|
||||
PropExpr::Cmp(Rel::Ge, left, right) => {
|
||||
assert!(matches!(left, IntExpr::Var(0)));
|
||||
assert!(matches!(right, IntExpr::Lit(value) if value == &0.into()));
|
||||
}
|
||||
other => panic!("unexpected AST {other:?}"),
|
||||
}
|
||||
assert!(goal.is_universally_quantified());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn closed_propositions_are_supported() {
|
||||
let goal = parse("(2+3)\\cdot4=20");
|
||||
assert!(goal.vars.is_empty());
|
||||
assert_eq!(lean(&goal), "((((2 : Int) + (3 : Int)) * (4 : Int)) = (20 : Int))");
|
||||
assert_eq!(goal.evaluate(&[]), Some(true));
|
||||
assert_eq!(goal.normal(), "(2 + 3) · 4 = 20");
|
||||
}
|
||||
Reference in new issue
Block a user