Skip to main content

logicaffeine_proof/
error.rs

1//! Error types for proof search and unification.
2//!
3//! This module defines [`ProofError`], which captures all failure modes
4//! during proof construction. Errors are designed to be informative for
5//! debugging and educational feedback.
6//!
7//! # Error Categories
8//!
9//! | Category | Variants | Meaning |
10//! |----------|----------|---------|
11//! | Search | `NoProofFound`, `DepthExceeded` | Proof search terminated |
12//! | Unification | `OccursCheck`, `UnificationFailed`, `ExprUnificationFailed` | Terms cannot be unified |
13//! | Matching | `SymbolMismatch`, `ArityMismatch` | Structural incompatibility |
14//! | Higher-Order | `PatternNotDistinct`, `NotAPattern`, `ScopeViolation` | Miller pattern failures |
15//!
16//! # Example
17//!
18//! ```
19//! use logicaffeine_proof::ProofError;
20//!
21//! fn check_result(result: Result<(), ProofError>) {
22//!     match result {
23//!         Ok(()) => println!("Proof found"),
24//!         Err(ProofError::NoProofFound) => println!("No proof exists"),
25//!         Err(ProofError::DepthExceeded) => println!("Search too deep"),
26//!         Err(e) => println!("Error: {}", e),
27//!     }
28//! }
29//! ```
30
31use crate::{ProofExpr, ProofTerm};
32use std::fmt;
33
34/// Errors that can occur during proof search.
35#[derive(Debug, Clone)]
36pub enum ProofError {
37    /// No proof was found for the given goal.
38    NoProofFound,
39
40    /// The proof search exceeded the maximum depth limit.
41    DepthExceeded,
42
43    /// Unification failed due to occurs check.
44    /// This prevents infinite types like x = f(x).
45    OccursCheck { variable: String, term: ProofTerm },
46
47    /// Unification failed because terms could not be unified.
48    UnificationFailed { left: ProofTerm, right: ProofTerm },
49
50    /// Expression-level unification failed.
51    ExprUnificationFailed { left: ProofExpr, right: ProofExpr },
52
53    /// Symbol mismatch during unification.
54    SymbolMismatch { left: String, right: String },
55
56    /// Arity mismatch during unification.
57    ArityMismatch { expected: usize, found: usize },
58
59    // --- Higher-Order Pattern Unification ---
60    /// The pattern has non-distinct arguments (e.g., ?F(x, x))
61    /// Miller patterns require distinct bound variables.
62    PatternNotDistinct(String),
63
64    /// Expression is not a valid Miller pattern.
65    /// Pattern arguments must be Term(BoundVarRef(...)).
66    NotAPattern(ProofExpr),
67
68    /// RHS uses variables not in pattern scope.
69    /// In ?P(x) = Body, Body must only use variables that appear in the pattern.
70    ScopeViolation { var: String, allowed: Vec<String> },
71}
72
73impl fmt::Display for ProofError {
74    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75        match self {
76            ProofError::NoProofFound => {
77                write!(
78                    f,
79                    "No proof found: the goal could not be derived from the knowledge base"
80                )
81            }
82            ProofError::DepthExceeded => {
83                write!(f, "Proof search exceeded maximum depth limit")
84            }
85            ProofError::OccursCheck { variable, term } => {
86                write!(
87                    f,
88                    "Occurs check failed: variable '{}' appears in term '{}' (would create infinite type)",
89                    variable, term
90                )
91            }
92            ProofError::UnificationFailed { left, right } => {
93                write!(
94                    f,
95                    "Unification failed: cannot unify '{}' with '{}'",
96                    left, right
97                )
98            }
99            ProofError::ExprUnificationFailed { left, right } => {
100                write!(
101                    f,
102                    "Expression unification failed: cannot unify '{}' with '{}'",
103                    left, right
104                )
105            }
106            ProofError::SymbolMismatch { left, right } => {
107                write!(f, "Symbol mismatch: '{}' vs '{}'", left, right)
108            }
109            ProofError::ArityMismatch { expected, found } => {
110                write!(
111                    f,
112                    "Arity mismatch: expected {} arguments, found {}",
113                    expected, found
114                )
115            }
116            ProofError::PatternNotDistinct(var) => {
117                write!(f, "Pattern has duplicate variable '{}': Miller patterns require distinct bound variables", var)
118            }
119            ProofError::NotAPattern(expr) => {
120                write!(f, "Not a valid Miller pattern: '{}' (arguments must be bound variable references)", expr)
121            }
122            ProofError::ScopeViolation { var, allowed } => {
123                write!(
124                    f,
125                    "Scope violation: variable '{}' not in pattern scope {:?}",
126                    var, allowed
127                )
128            }
129        }
130    }
131}
132
133impl std::error::Error for ProofError {}
134
135/// Result type for proof operations.
136pub type ProofResult<T> = Result<T, ProofError>;