]> Repositorios git - scryer-prolog.git/commitdiff
LeafAnswer docs and success checking methods
authorbakaq <[email protected]>
Mon, 30 Sep 2024 02:21:18 +0000 (23:21 -0300)
committerbakaq <[email protected]>
Sun, 8 Dec 2024 23:18:06 +0000 (20:18 -0300)
src/machine/parsed_results.rs

index 3688bc97ea236beff691f27635c441b7739c4c62..ec3d469362cc4dc79fc12498277b8ed6c5c4fdd9 100644 (file)
@@ -14,15 +14,47 @@ use super::{HeapCellValue, Number};
 /// Represents a leaf answer from a query.
 #[derive(Debug, Clone, PartialEq, Eq)]
 pub enum LeafAnswer {
+    /// A `true` leaf answer.
     True,
+    /// A `false` leaf answer.
+    ///
+    /// This means that there are no more answers for the query.
     False,
+    /// An exception leaf answer.
     Exception(PrologTerm),
+    /// A leaf answer with bindings and residual goals.
+    ///
+    /// Both bindings and residual goals can be empty.
     LeafAnswer {
         bindings: BTreeMap<String, PrologTerm>,
         residual_goals: Vec<PrologTerm>,
     },
 }
 
+impl LeafAnswer {
+    /// True if leaf answer failed.
+    ///
+    /// This gives [`false`] for exceptions.
+    pub fn failed(&self) -> bool {
+        match self {
+            LeafAnswer::False => true,
+            _ => false,
+        }
+    }
+
+    /// True if leaf answer may have succeeded.
+    ///
+    /// When a leaf answer has residual goals the success is conditional on the satisfiability of
+    /// the contraints they represent. This gives [`false`] for exceptions.
+    pub fn maybe_succeeded(&self) -> bool {
+        match self {
+            LeafAnswer::True => true,
+            LeafAnswer::LeafAnswer { .. } => true,
+            _ => false,
+        }
+    }
+}
+
 /// Represents a Prolog term.
 #[non_exhaustive]
 #[derive(Debug, Clone, PartialEq, Eq)]