|
| 1 | +// SPDX-FileCopyrightText: 2026 ECHIDNA Project Team |
| 2 | +// SPDX-License-Identifier: PMPL-1.0-or-later |
| 3 | + |
| 4 | +//! Shared TPTP output parser for higher-order and first-order ATPs |
| 5 | +//! |
| 6 | +//! Parses SZS (Standard Zero Substitution) status lines produced by TPTP-compliant |
| 7 | +//! provers including Leo-III, Satallax, Lash, agsyHOL, Vampire, E-Prover, etc. |
| 8 | +
|
| 9 | +use anyhow::Result; |
| 10 | + |
| 11 | +/// Parse TPTP prover output for SZS status indicators |
| 12 | +/// |
| 13 | +/// Returns `Ok(true)` if status indicates theorem proved (Theorem) |
| 14 | +/// Returns `Ok(false)` if status indicates non-proof (CounterSatisfiable, Satisfiable, etc.) |
| 15 | +/// Returns `Err` if output cannot be parsed or prover error detected |
| 16 | +pub fn parse_szs_status(output: &str) -> Result<bool> { |
| 17 | + for line in output.lines() { |
| 18 | + let trimmed = line.trim(); |
| 19 | + |
| 20 | + // Check for theorem proven |
| 21 | + if contains_szs_status(trimmed, &["Theorem"]) { |
| 22 | + return Ok(true); |
| 23 | + } |
| 24 | + |
| 25 | + // Check for non-proof statuses |
| 26 | + if contains_szs_status( |
| 27 | + trimmed, |
| 28 | + &[ |
| 29 | + "CounterSatisfiable", |
| 30 | + "Satisfiable", |
| 31 | + "Unknown", |
| 32 | + "Timeout", |
| 33 | + "ResourceOut", |
| 34 | + "GaveUp", |
| 35 | + ], |
| 36 | + ) { |
| 37 | + return Ok(false); |
| 38 | + } |
| 39 | + |
| 40 | + // Errors and failures |
| 41 | + if contains_szs_status(trimmed, &["InternalError", "UserError", "OtherError"]) { |
| 42 | + return Err(anyhow::anyhow!("Prover error: {}", trimmed)); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + Ok(false) |
| 47 | +} |
| 48 | + |
| 49 | +/// Check if a line contains a specific SZS status |
| 50 | +fn contains_szs_status(line: &str, statuses: &[&str]) -> bool { |
| 51 | + for status in statuses { |
| 52 | + // Match both "SZS status <status>" and "% SZS status <status>" |
| 53 | + if line.contains(&format!("SZS status {}", status)) { |
| 54 | + return true; |
| 55 | + } |
| 56 | + } |
| 57 | + false |
| 58 | +} |
| 59 | + |
| 60 | +#[cfg(test)] |
| 61 | +mod tests { |
| 62 | + use super::*; |
| 63 | + |
| 64 | + #[test] |
| 65 | + fn test_parse_szs_theorem() { |
| 66 | + let output = "% SZS status Theorem\n"; |
| 67 | + assert!(parse_szs_status(output).unwrap()); |
| 68 | + } |
| 69 | + |
| 70 | + #[test] |
| 71 | + fn test_parse_szs_satisfiable() { |
| 72 | + let output = "% SZS status Satisfiable\n"; |
| 73 | + assert!(!parse_szs_status(output).unwrap()); |
| 74 | + } |
| 75 | + |
| 76 | + #[test] |
| 77 | + fn test_parse_szs_unknown() { |
| 78 | + let output = "% SZS status Unknown\n"; |
| 79 | + assert!(!parse_szs_status(output).unwrap()); |
| 80 | + } |
| 81 | + |
| 82 | + #[test] |
| 83 | + fn test_parse_szs_counter_satisfiable() { |
| 84 | + let output = "% SZS status CounterSatisfiable\n"; |
| 85 | + assert!(!parse_szs_status(output).unwrap()); |
| 86 | + } |
| 87 | + |
| 88 | + #[test] |
| 89 | + fn test_parse_szs_with_output() { |
| 90 | + let output = r#" |
| 91 | +% Some prover output |
| 92 | +% More output |
| 93 | +% SZS status Theorem |
| 94 | +% Additional info |
| 95 | +"#; |
| 96 | + assert!(parse_szs_status(output).unwrap()); |
| 97 | + } |
| 98 | + |
| 99 | + #[test] |
| 100 | + fn test_parse_szs_no_status() { |
| 101 | + let output = "some random output\nwithout status\n"; |
| 102 | + assert!(!parse_szs_status(output).unwrap()); |
| 103 | + } |
| 104 | + |
| 105 | + #[test] |
| 106 | + fn test_parse_szs_error() { |
| 107 | + let output = "% SZS status InternalError\n"; |
| 108 | + assert!(parse_szs_status(output).is_err()); |
| 109 | + } |
| 110 | +} |
0 commit comments