Expand description
Recursive Query Support with Tabling
This module implements advanced recursive query handling including:
- Tabling/tabulation for efficient recursive queries
- Stratified evaluation
- Support for left-recursive rules
- Fixpoint computation
§Tabling
Tabling (also called tabled resolution or SLG resolution) is a technique for evaluating logic programs that improves on standard SLD resolution by memoizing intermediate results and detecting loops.
§Example
use ipfrs_tensorlogic::{TabledInferenceEngine, KnowledgeBase, Predicate, Rule, Term, Constant};
let mut kb = KnowledgeBase::new();
// Define ancestor relation: ancestor(X, Y) :- parent(X, Y).
// ancestor(X, Z) :- parent(X, Y), ancestor(Y, Z).
// This is recursive and benefits from tabling
// Add parent facts
kb.add_fact(Predicate::new("parent".to_string(), vec![
Term::Const(Constant::String("alice".to_string())),
Term::Const(Constant::String("bob".to_string())),
]));
kb.add_fact(Predicate::new("parent".to_string(), vec![
Term::Const(Constant::String("bob".to_string())),
Term::Const(Constant::String("charlie".to_string())),
]));
// Add base rule: ancestor(X, Y) :- parent(X, Y)
kb.add_rule(Rule::new(
Predicate::new("ancestor".to_string(), vec![
Term::Var("X".to_string()),
Term::Var("Y".to_string()),
]),
vec![Predicate::new("parent".to_string(), vec![
Term::Var("X".to_string()),
Term::Var("Y".to_string()),
])],
));
// Add recursive rule: ancestor(X, Z) :- parent(X, Y), ancestor(Y, Z)
kb.add_rule(Rule::new(
Predicate::new("ancestor".to_string(), vec![
Term::Var("X".to_string()),
Term::Var("Z".to_string()),
]),
vec![
Predicate::new("parent".to_string(), vec![
Term::Var("X".to_string()),
Term::Var("Y".to_string()),
]),
Predicate::new("ancestor".to_string(), vec![
Term::Var("Y".to_string()),
Term::Var("Z".to_string()),
]),
],
));
// Create tabled engine
let engine = TabledInferenceEngine::new();
// Query for all ancestors of alice
let goal = Predicate::new("ancestor".to_string(), vec![
Term::Const(Constant::String("alice".to_string())),
Term::Var("Z".to_string()),
]);
let solutions = engine.query(&goal, &kb).unwrap();
// Should find at least bob as an ancestor
assert!(!solutions.is_empty());Structs§
- Fixpoint
Engine - Fixpoint computation for stratified programs
- Stratification
Analyzer - Stratification analysis for logic programs
- Table
Stats - Statistics about the tabling system
- Tabled
Inference Engine - Tabled inference engine using SLG resolution
Enums§
- Stratification
Result - Result of stratification analysis