logicaffeine-proof 0.9.16

Backward-chaining proof engine with Socratic hints
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
//! Z3 Oracle integration for proof search fallback.
//!
//! When the structural backward chainer cannot derive a proof, the oracle
//! delegates to Z3, an SMT solver, for arithmetic, comparisons, and
//! uninterpreted function reasoning.
//!
//! # Architecture
//!
//! 1. Convert [`ProofExpr`] → [`VerifyExpr`](logicaffeine_verify::ir::VerifyExpr)
//! 2. Add assumptions from the knowledge base
//! 3. Ask Z3 to verify the goal
//! 4. Return [`DerivationTree`] with `OracleVerification` rule if successful
//!
//! # Limitations
//!
//! The oracle cannot reason about:
//! - Inductive constructs (`Ctor`, `Match`, `Fixpoint`, `TypedVar`)
//! - Lambda calculus (`Lambda`, `App`)
//! - Event semantics (`NeoEvent`)
//!
//! These constructs are detected by [`contains_inductive_constructs`] and cause
//! the oracle to return `Ok(None)` rather than attempting verification.
//!
//! # Feature Gate
//!
//! This module requires the `verification` feature flag:
//!
//! ```toml
//! [dependencies]
//! logicaffeine_proof = { version = "...", features = ["verification"] }
//! ```
//!
//! # Example
//!
//! ```ignore
//! use logicaffeine_proof::{ProofGoal, ProofExpr};
//! use logicaffeine_proof::oracle::try_oracle;
//!
//! let goal = ProofGoal::new(ProofExpr::Atom("P".into()));
//! let kb = vec![ProofExpr::Atom("P".into())];
//!
//! match try_oracle(&goal, &kb) {
//!     Ok(Some(tree)) => println!("Z3 verified: {}", tree),
//!     Ok(None) => println!("Z3 cannot verify"),
//!     Err(e) => println!("Error: {}", e),
//! }
//! ```

use crate::error::ProofResult;
use crate::{DerivationTree, InferenceRule, ProofExpr, ProofGoal, ProofTerm};

use logicaffeine_verify::ir::{VerifyExpr, VerifyOp, VerifyType};
use logicaffeine_verify::solver::VerificationSession;

// =============================================================================
// INDUCTIVE CONSTRUCT DETECTION
// =============================================================================

/// Check if an expression contains inductive constructs (Ctor, TypedVar, etc.)
/// that Z3 cannot handle without explicit axioms.
fn contains_inductive_constructs(expr: &ProofExpr) -> bool {
    match expr {
        // Inductive constructors - Z3 doesn't understand these
        ProofExpr::Ctor { .. } => true,
        ProofExpr::TypedVar { .. } => true,
        ProofExpr::Match { .. } => true,
        ProofExpr::Fixpoint { .. } => true,

        // Check sub-expressions
        ProofExpr::And(l, r)
        | ProofExpr::Or(l, r)
        | ProofExpr::Implies(l, r)
        | ProofExpr::Iff(l, r) => {
            contains_inductive_constructs(l) || contains_inductive_constructs(r)
        }

        ProofExpr::Not(inner) => contains_inductive_constructs(inner),

        ProofExpr::ForAll { body, .. } | ProofExpr::Exists { body, .. } => {
            contains_inductive_constructs(body)
        }

        ProofExpr::Identity(l, r) => {
            contains_inductive_constructs_term(l) || contains_inductive_constructs_term(r)
        }

        ProofExpr::Predicate { args, .. } => {
            args.iter().any(contains_inductive_constructs_term)
        }

        // Other expressions are fine
        _ => false,
    }
}

/// Check if a term contains inductive constructs.
fn contains_inductive_constructs_term(term: &ProofTerm) -> bool {
    match term {
        ProofTerm::Function(name, args) => {
            // Check for known inductive constructors
            matches!(name.as_str(), "Zero" | "Succ" | "Nil" | "Cons")
                || args.iter().any(contains_inductive_constructs_term)
        }
        ProofTerm::Variable(v) | ProofTerm::BoundVarRef(v) => {
            // Check for TypedVar pattern "name:Type"
            v.contains(':')
        }
        ProofTerm::Group(terms) => terms.iter().any(contains_inductive_constructs_term),
        ProofTerm::Constant(_) => false,
    }
}

// =============================================================================
// PUBLIC API
// =============================================================================

/// Attempt to prove a goal using Z3 as an oracle.
///
/// This is the fallback when structural backward chaining fails. Z3 will verify
/// arithmetic, comparisons, and uninterpreted function reasoning.
///
/// # Arguments
///
/// * `goal` - The proof goal to verify
/// * `knowledge_base` - Facts and rules available as assumptions
///
/// # Returns
///
/// * `Ok(Some(tree))` - Z3 verified the goal; returns a [`DerivationTree`] with
///   [`InferenceRule::OracleVerification`]
/// * `Ok(None)` - Z3 cannot verify (unknown, unsat, or unsupported constructs)
/// * `Err(e)` - Internal error during verification
///
/// # Behavior
///
/// The function performs these steps:
/// 1. Check for inductive constructs (returns `None` if found)
/// 2. Infer types for all variables in goal and KB
/// 3. Declare variables in Z3 session
/// 4. Add context and KB as assumptions
/// 5. Convert goal to [`VerifyExpr`](logicaffeine_verify::ir::VerifyExpr)
/// 6. Ask Z3 to verify
///
/// # See Also
///
/// * [`proof_expr_to_verify_expr`] - Conversion from proof to verification expressions
/// * [`BackwardChainer`](crate::BackwardChainer) - The main proof engine that calls this
pub fn try_oracle(
    goal: &ProofGoal,
    knowledge_base: &[ProofExpr],
) -> ProofResult<Option<DerivationTree>> {
    // Skip oracle for goals containing inductive constructs
    // Z3 cannot reason about Peano arithmetic without explicit axioms
    if contains_inductive_constructs(&goal.target) {
        return Ok(None);
    }

    // Also skip if KB contains inductive constructs (they would corrupt Z3 context)
    for kb_expr in knowledge_base {
        if contains_inductive_constructs(kb_expr) {
            return Ok(None);
        }
    }
    // Collect all variables and their types
    let mut session = VerificationSession::new();
    let mut types = TypeInference::new();

    // Infer types from goal
    types.infer_from_expr(&goal.target);

    // Infer types from context and KB
    for ctx_expr in &goal.context {
        types.infer_from_expr(ctx_expr);
    }
    for kb_expr in knowledge_base {
        types.infer_from_expr(kb_expr);
    }

    // Declare all inferred variables
    for (name, ty) in types.variables.iter() {
        session.declare(name, ty.clone());
    }

    // Add context assumptions
    for ctx_expr in &goal.context {
        if let Some(verify_expr) = proof_expr_to_verify_expr(ctx_expr) {
            session.assume(&verify_expr);
        }
    }

    // Add KB as assumptions (simplified - in full version, would be more selective)
    for kb_expr in knowledge_base {
        if let Some(verify_expr) = proof_expr_to_verify_expr(kb_expr) {
            session.assume(&verify_expr);
        }
    }

    // Convert goal to VerifyExpr
    let goal_expr = match proof_expr_to_verify_expr(&goal.target) {
        Some(e) => e,
        None => return Ok(None), // Cannot convert, oracle can't help
    };

    // Ask Z3 to verify the goal
    match session.verify(&goal_expr) {
        Ok(()) => {
            // Z3 verified it!
            let tree = DerivationTree::leaf(
                goal.target.clone(),
                InferenceRule::OracleVerification("Verified by Z3".into()),
            );
            Ok(Some(tree))
        }
        Err(_) => {
            // Z3 could not verify (either invalid or unknown)
            Ok(None)
        }
    }
}

// =============================================================================
// TYPE INFERENCE
// =============================================================================

/// Simple type inference for Z3 variable declaration.
struct TypeInference {
    variables: std::collections::HashMap<String, VerifyType>,
}

impl TypeInference {
    fn new() -> Self {
        Self {
            variables: std::collections::HashMap::new(),
        }
    }

    /// Infer types from a ProofExpr.
    fn infer_from_expr(&mut self, expr: &ProofExpr) {
        match expr {
            ProofExpr::Predicate { args, .. } => {
                for arg in args {
                    self.infer_from_term(arg, VerifyType::Int);
                }
            }

            ProofExpr::Identity(left, right) => {
                self.infer_from_term(left, VerifyType::Int);
                self.infer_from_term(right, VerifyType::Int);
            }

            ProofExpr::Atom(name) => {
                // Atoms are boolean propositions
                self.variables.insert(name.clone(), VerifyType::Bool);
            }

            ProofExpr::And(left, right)
            | ProofExpr::Or(left, right)
            | ProofExpr::Implies(left, right)
            | ProofExpr::Iff(left, right) => {
                self.infer_from_expr(left);
                self.infer_from_expr(right);
            }

            ProofExpr::Not(inner) => {
                self.infer_from_expr(inner);
            }

            ProofExpr::ForAll { body, .. } | ProofExpr::Exists { body, .. } => {
                self.infer_from_expr(body);
            }

            _ => {}
        }
    }

    /// Infer type of a term.
    fn infer_from_term(&mut self, term: &ProofTerm, context_type: VerifyType) {
        match term {
            ProofTerm::Variable(name) | ProofTerm::BoundVarRef(name) => {
                // Use context type if not already declared
                if !self.variables.contains_key(name) {
                    self.variables.insert(name.clone(), context_type);
                }
            }

            ProofTerm::Function(_, args) => {
                for arg in args {
                    self.infer_from_term(arg, VerifyType::Int);
                }
            }

            ProofTerm::Group(terms) => {
                for t in terms {
                    self.infer_from_term(t, VerifyType::Int);
                }
            }

            ProofTerm::Constant(_) => {
                // Constants don't need declaration
            }
        }
    }
}

// =============================================================================
// CONVERSION: ProofExpr → VerifyExpr
// =============================================================================

/// Convert a [`ProofExpr`] to [`VerifyExpr`](logicaffeine_verify::ir::VerifyExpr) for Z3 verification.
///
/// Transforms proof-level expressions into the verification IR that Z3 understands.
///
/// # Returns
///
/// * `Some(expr)` - Successfully converted expression
/// * `None` - Expression contains unsupported constructs
///
/// # Supported Constructs
///
/// | ProofExpr | VerifyExpr |
/// |-----------|------------|
/// | `Atom(name)` | `Var(name)` |
/// | `Predicate { Gt, [x, y] }` | `Binary(Gt, x, y)` |
/// | `And(l, r)` | `And(l, r)` |
/// | `Implies(l, r)` | `Implies(l, r)` |
/// | `ForAll { var, body }` | `ForAll([(var, Int)], body)` |
/// | `Identity(l, r)` | `Eq(l, r)` |
///
/// # Unsupported (returns `None`)
///
/// * `Lambda`, `App` - Higher-order functions
/// * `Ctor`, `Match`, `Fixpoint` - Inductive types
/// * `NeoEvent` - Event semantics
/// * `Hole`, `Term`, `Unsupported` - Meta-constructs
pub fn proof_expr_to_verify_expr(expr: &ProofExpr) -> Option<VerifyExpr> {
    match expr {
        ProofExpr::Atom(name) => Some(VerifyExpr::var(name)),

        ProofExpr::Predicate { name, args, .. } => {
            // Check for built-in comparison predicates
            if args.len() == 2 {
                let left = proof_term_to_verify_expr(&args[0])?;
                let right = proof_term_to_verify_expr(&args[1])?;

                match name.as_str() {
                    "Gt" => return Some(VerifyExpr::gt(left, right)),
                    "Lt" => return Some(VerifyExpr::lt(left, right)),
                    "Gte" => return Some(VerifyExpr::gte(left, right)),
                    "Lte" => return Some(VerifyExpr::lte(left, right)),
                    "Eq" => return Some(VerifyExpr::eq(left, right)),
                    "Neq" => return Some(VerifyExpr::neq(left, right)),
                    _ => {}
                }
            }

            // General predicate → uninterpreted function
            let verify_args: Vec<VerifyExpr> = args
                .iter()
                .filter_map(proof_term_to_verify_expr)
                .collect();
            Some(VerifyExpr::apply(name, verify_args))
        }

        ProofExpr::Identity(left, right) => {
            let l = proof_term_to_verify_expr(left)?;
            let r = proof_term_to_verify_expr(right)?;
            Some(VerifyExpr::eq(l, r))
        }

        ProofExpr::And(left, right) => {
            let l = proof_expr_to_verify_expr(left)?;
            let r = proof_expr_to_verify_expr(right)?;
            Some(VerifyExpr::and(l, r))
        }

        ProofExpr::Or(left, right) => {
            let l = proof_expr_to_verify_expr(left)?;
            let r = proof_expr_to_verify_expr(right)?;
            Some(VerifyExpr::or(l, r))
        }

        ProofExpr::Implies(left, right) => {
            let l = proof_expr_to_verify_expr(left)?;
            let r = proof_expr_to_verify_expr(right)?;
            Some(VerifyExpr::implies(l, r))
        }

        ProofExpr::Iff(left, right) => {
            // A ↔ B is (A → B) ∧ (B → A)
            let l = proof_expr_to_verify_expr(left)?;
            let r = proof_expr_to_verify_expr(right)?;
            Some(VerifyExpr::and(
                VerifyExpr::implies(l.clone(), r.clone()),
                VerifyExpr::implies(r, l),
            ))
        }

        ProofExpr::Not(inner) => {
            let i = proof_expr_to_verify_expr(inner)?;
            Some(VerifyExpr::not(i))
        }

        ProofExpr::ForAll { variable, body } => {
            let b = proof_expr_to_verify_expr(body)?;
            Some(VerifyExpr::forall(
                vec![(variable.clone(), VerifyType::Int)],
                b,
            ))
        }

        ProofExpr::Exists { variable, body } => {
            let b = proof_expr_to_verify_expr(body)?;
            Some(VerifyExpr::exists(
                vec![(variable.clone(), VerifyType::Int)],
                b,
            ))
        }

        // Modal and Temporal become uninterpreted functions
        ProofExpr::Modal { flavor, body, .. } => {
            let b = proof_expr_to_verify_expr(body)?;
            Some(VerifyExpr::apply(flavor, vec![b]))
        }

        ProofExpr::Temporal { operator, body } => {
            let b = proof_expr_to_verify_expr(body)?;
            Some(VerifyExpr::apply(operator, vec![b]))
        }

        ProofExpr::TemporalBinary { operator, left, right } => {
            let l = proof_expr_to_verify_expr(left)?;
            let r = proof_expr_to_verify_expr(right)?;
            Some(VerifyExpr::apply(operator, vec![l, r]))
        }

        // Inductive types - unsupported for now
        ProofExpr::Ctor { .. }
        | ProofExpr::Match { .. }
        | ProofExpr::Fixpoint { .. }
        | ProofExpr::TypedVar { .. } => None,

        // Others - not representable in Z3
        ProofExpr::Lambda { .. }
        | ProofExpr::App(_, _)
        | ProofExpr::NeoEvent { .. }
        | ProofExpr::Hole(_)
        | ProofExpr::Term(_)
        | ProofExpr::Unsupported(_) => None,
    }
}

/// Convert a [`ProofTerm`] to [`VerifyExpr`](logicaffeine_verify::ir::VerifyExpr).
///
/// Transforms proof-level terms into verification expressions for Z3.
///
/// # Conversion Rules
///
/// | ProofTerm | VerifyExpr |
/// |-----------|------------|
/// | `Constant("42")` | `Int(42)` |
/// | `Constant("foo")` | `Var("foo")` |
/// | `Variable(name)` | `Var(name)` |
/// | `BoundVarRef(name)` | `Var(name)` |
/// | `Function("Add", [x, y])` | `Binary(Add, x, y)` |
/// | `Function(name, args)` | `Apply(name, args)` |
///
/// Numeric string constants are parsed as integers; non-numeric become variables.
/// Arithmetic functions (`Add`, `Sub`, `Mul`, `Div`) are converted to binary operations.
pub fn proof_term_to_verify_expr(term: &ProofTerm) -> Option<VerifyExpr> {
    match term {
        ProofTerm::Constant(s) => {
            // Try to parse as integer
            if let Ok(n) = s.parse::<i64>() {
                Some(VerifyExpr::int(n))
            } else {
                // Non-numeric constant becomes a variable
                Some(VerifyExpr::var(s))
            }
        }

        ProofTerm::Variable(name) | ProofTerm::BoundVarRef(name) => Some(VerifyExpr::var(name)),

        ProofTerm::Function(name, args) => {
            // Check for built-in arithmetic functions
            if args.len() == 2 {
                let left = proof_term_to_verify_expr(&args[0])?;
                let right = proof_term_to_verify_expr(&args[1])?;

                match name.as_str() {
                    "Add" => {
                        return Some(VerifyExpr::binary(VerifyOp::Add, left, right))
                    }
                    "Sub" => {
                        return Some(VerifyExpr::binary(VerifyOp::Sub, left, right))
                    }
                    "Mul" => {
                        return Some(VerifyExpr::binary(VerifyOp::Mul, left, right))
                    }
                    "Div" => {
                        return Some(VerifyExpr::binary(VerifyOp::Div, left, right))
                    }
                    _ => {}
                }
            }

            // General function → Apply
            let verify_args: Vec<VerifyExpr> = args
                .iter()
                .filter_map(proof_term_to_verify_expr)
                .collect();
            Some(VerifyExpr::apply(name, verify_args))
        }

        ProofTerm::Group(terms) => {
            // Group of terms - convert each (used for tuple-like structures)
            if terms.len() == 1 {
                proof_term_to_verify_expr(&terms[0])
            } else {
                // Multi-term group not directly supported
                None
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_convert_atom() {
        let expr = ProofExpr::Atom("P".into());
        let result = proof_expr_to_verify_expr(&expr);
        assert!(matches!(result, Some(VerifyExpr::Var(s)) if s == "P"));
    }

    #[test]
    fn test_convert_gt_predicate() {
        let expr = ProofExpr::Predicate {
            name: "Gt".into(),
            args: vec![
                ProofTerm::Variable("x".into()),
                ProofTerm::Constant("10".into()),
            ],
            world: None,
        };
        let result = proof_expr_to_verify_expr(&expr);
        assert!(matches!(
            result,
            Some(VerifyExpr::Binary {
                op: VerifyOp::Gt,
                ..
            })
        ));
    }

    #[test]
    fn test_convert_implication() {
        let expr = ProofExpr::Implies(
            Box::new(ProofExpr::Atom("P".into())),
            Box::new(ProofExpr::Atom("Q".into())),
        );
        let result = proof_expr_to_verify_expr(&expr);
        assert!(matches!(
            result,
            Some(VerifyExpr::Binary {
                op: VerifyOp::Implies,
                ..
            })
        ));
    }

    #[test]
    fn test_convert_arithmetic_function() {
        let term = ProofTerm::Function(
            "Add".into(),
            vec![
                ProofTerm::Variable("x".into()),
                ProofTerm::Constant("5".into()),
            ],
        );
        let result = proof_term_to_verify_expr(&term);
        assert!(matches!(
            result,
            Some(VerifyExpr::Binary {
                op: VerifyOp::Add,
                ..
            })
        ));
    }
}