ipfrs-tensorlogic 0.1.0

Zero-copy tensor operations and logic programming for content-addressed storage
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
//! 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());
//! ```

use crate::ir::{KnowledgeBase, Predicate, Rule};
use crate::reasoning::{apply_subst_predicate, unify_predicates, Substitution};
use ipfrs_core::error::Result;
use std::collections::{HashMap, HashSet};

/// Table entry for memoized subgoals
#[derive(Debug, Clone)]
struct TableEntry {
    /// The subgoal being solved
    #[allow(dead_code)]
    goal: Predicate,
    /// Solutions found so far
    solutions: Vec<Substitution>,
    /// Whether this entry is complete
    complete: bool,
    /// Depth at which this was tabled
    #[allow(dead_code)]
    depth: usize,
}

/// Tabled inference engine using SLG resolution
pub struct TabledInferenceEngine {
    /// Table for memoizing subgoals
    table: HashMap<String, TableEntry>,
    /// Maximum depth
    max_depth: usize,
    /// Maximum solutions per subgoal
    max_solutions: usize,
}

impl TabledInferenceEngine {
    /// Create a new tabled inference engine
    pub fn new() -> Self {
        Self {
            table: HashMap::new(),
            max_depth: 100,
            max_solutions: 1000,
        }
    }

    /// Create with custom limits
    pub fn with_limits(max_depth: usize, max_solutions: usize) -> Self {
        Self {
            table: HashMap::new(),
            max_depth,
            max_solutions,
        }
    }

    /// Query with tabling
    pub fn query(&self, goal: &Predicate, kb: &KnowledgeBase) -> Result<Vec<Substitution>> {
        let mut engine = Self {
            table: HashMap::new(),
            max_depth: self.max_depth,
            max_solutions: self.max_solutions,
        };

        engine.solve_tabled(goal, &Substitution::new(), kb, 0)
    }

    /// Solve a goal with tabling
    fn solve_tabled(
        &mut self,
        goal: &Predicate,
        subst: &Substitution,
        kb: &KnowledgeBase,
        depth: usize,
    ) -> Result<Vec<Substitution>> {
        // Check depth limit
        if depth > self.max_depth {
            return Ok(Vec::new());
        }

        // Apply substitution to goal
        let goal = apply_subst_predicate(goal, subst);

        // Create table key
        let key = self.goal_key(&goal);

        // Check if goal is already tabled
        if let Some(entry) = self.table.get(&key) {
            // If complete, return cached solutions
            if entry.complete {
                return Ok(entry.solutions.clone());
            }
            // If incomplete, we have a loop - return empty for now
            return Ok(Vec::new());
        }

        // Create new table entry
        let mut entry = TableEntry {
            goal: goal.clone(),
            solutions: Vec::new(),
            complete: false,
            depth,
        };

        // Insert incomplete entry to detect loops
        self.table.insert(key.clone(), entry.clone());

        // Solve using standard backward chaining
        let mut solutions = Vec::new();

        // Try facts
        for fact in kb.get_predicates(&goal.name) {
            if let Some(new_subst) = unify_predicates(&goal, fact, &Substitution::new()) {
                solutions.push(new_subst);
                if solutions.len() >= self.max_solutions {
                    break;
                }
            }
        }

        // Try rules
        for rule in kb.get_rules(&goal.name) {
            if solutions.len() >= self.max_solutions {
                break;
            }

            // Rename variables in rule
            let renamed_rule = self.rename_rule(rule, depth);

            // Try to unify with rule head
            if let Some(new_subst) =
                unify_predicates(&goal, &renamed_rule.head, &Substitution::new())
            {
                // Solve rule body
                let body_solutions =
                    self.solve_conjunction(&renamed_rule.body, &new_subst, kb, depth + 1)?;
                solutions.extend(body_solutions);
            }
        }

        // Mark entry as complete and update solutions
        entry.solutions = solutions.clone();
        entry.complete = true;
        self.table.insert(key, entry);

        Ok(solutions)
    }

    /// Solve a conjunction of goals
    fn solve_conjunction(
        &mut self,
        goals: &[Predicate],
        subst: &Substitution,
        kb: &KnowledgeBase,
        depth: usize,
    ) -> Result<Vec<Substitution>> {
        if goals.is_empty() {
            return Ok(vec![subst.clone()]);
        }

        let first = &goals[0];
        let rest = &goals[1..];

        let first_solutions = self.solve_tabled(first, subst, kb, depth)?;

        let mut all_solutions = Vec::new();
        for first_subst in first_solutions {
            let rest_solutions = self.solve_conjunction(rest, &first_subst, kb, depth)?;
            all_solutions.extend(rest_solutions);

            if all_solutions.len() >= self.max_solutions {
                break;
            }
        }

        Ok(all_solutions)
    }

    /// Generate a unique key for a goal
    fn goal_key(&self, goal: &Predicate) -> String {
        format!("{}({})", goal.name, goal.args.len())
    }

    /// Rename variables in a rule
    fn rename_rule(&self, rule: &Rule, suffix: usize) -> Rule {
        let var_map: HashMap<String, String> = rule
            .variables()
            .into_iter()
            .map(|v| (v.clone(), format!("{}_{}", v, suffix)))
            .collect();

        let rename_subst: Substitution = var_map
            .into_iter()
            .map(|(old, new)| (old, crate::ir::Term::Var(new)))
            .collect();

        Rule {
            head: apply_subst_predicate(&rule.head, &rename_subst),
            body: rule
                .body
                .iter()
                .map(|p| apply_subst_predicate(p, &rename_subst))
                .collect(),
        }
    }

    /// Get table statistics
    pub fn table_stats(&self) -> TableStats {
        TableStats {
            entries: self.table.len(),
            complete_entries: self.table.values().filter(|e| e.complete).count(),
            total_solutions: self.table.values().map(|e| e.solutions.len()).sum(),
        }
    }

    /// Clear the table
    pub fn clear_table(&mut self) {
        self.table.clear();
    }
}

impl Default for TabledInferenceEngine {
    fn default() -> Self {
        Self::new()
    }
}

/// Statistics about the tabling system
#[derive(Debug, Clone)]
pub struct TableStats {
    /// Number of table entries
    pub entries: usize,
    /// Number of complete entries
    pub complete_entries: usize,
    /// Total solutions across all entries
    pub total_solutions: usize,
}

/// Fixpoint computation for stratified programs
pub struct FixpointEngine {
    /// Maximum iterations for fixpoint
    max_iterations: usize,
}

impl FixpointEngine {
    /// Create a new fixpoint engine
    pub fn new() -> Self {
        Self {
            max_iterations: 100,
        }
    }

    /// Create with custom iteration limit
    pub fn with_max_iterations(max_iterations: usize) -> Self {
        Self { max_iterations }
    }

    /// Compute fixpoint for a set of rules
    pub fn compute_fixpoint(&self, kb: &KnowledgeBase) -> Result<KnowledgeBase> {
        let mut current_kb = kb.clone();
        let mut iteration = 0;

        loop {
            iteration += 1;
            if iteration > self.max_iterations {
                break;
            }

            let mut new_facts = Vec::new();
            let mut changed = false;

            // Apply all rules to derive new facts
            // Collect unique predicate names from rules
            let predicate_names: std::collections::HashSet<String> = current_kb
                .rules
                .iter()
                .map(|r| r.head.name.clone())
                .collect();

            for predicate_name in predicate_names {
                for rule in current_kb.get_rules(&predicate_name) {
                    let derived = self.derive_facts_from_rule(rule, &current_kb)?;
                    for fact in derived {
                        // Check if fact already exists
                        if !current_kb.facts.contains(&fact) {
                            new_facts.push(fact);
                            changed = true;
                        }
                    }
                }
            }

            // Add new facts to KB
            for fact in new_facts {
                current_kb.add_fact(fact);
            }

            // If no new facts, we've reached fixpoint
            if !changed {
                break;
            }
        }

        Ok(current_kb)
    }

    /// Derive facts from a single rule
    fn derive_facts_from_rule(&self, _rule: &Rule, _kb: &KnowledgeBase) -> Result<Vec<Predicate>> {
        let derived = Vec::new();

        // This is a simplified implementation
        // A full implementation would do proper unification and substitution

        // For now, just return empty
        // TODO: Implement full derivation

        Ok(derived)
    }
}

impl Default for FixpointEngine {
    fn default() -> Self {
        Self::new()
    }
}

/// Stratification analysis for logic programs
pub struct StratificationAnalyzer {
    /// Dependency graph between predicates
    dependencies: HashMap<String, HashSet<String>>,
}

impl StratificationAnalyzer {
    /// Create a new stratification analyzer
    pub fn new() -> Self {
        Self {
            dependencies: HashMap::new(),
        }
    }

    /// Analyze a knowledge base for stratification
    pub fn analyze(&mut self, kb: &KnowledgeBase) -> StratificationResult {
        self.build_dependency_graph(kb);

        // Check for cycles
        if self.has_cycles() {
            StratificationResult::NonStratifiable
        } else {
            // Compute stratification levels
            let strata = self.compute_strata();
            StratificationResult::Stratifiable(strata)
        }
    }

    /// Build dependency graph from KB
    fn build_dependency_graph(&mut self, kb: &KnowledgeBase) {
        // Collect unique predicate names from rules
        let predicate_names: HashSet<String> =
            kb.rules.iter().map(|r| r.head.name.clone()).collect();

        for predicate_name in predicate_names {
            for rule in kb.get_rules(&predicate_name) {
                let head = &rule.head.name;
                let deps: HashSet<String> = rule.body.iter().map(|p| p.name.clone()).collect();

                self.dependencies
                    .entry(head.clone())
                    .or_default()
                    .extend(deps);
            }
        }
    }

    /// Check if dependency graph has cycles
    fn has_cycles(&self) -> bool {
        let mut visited = HashSet::new();
        let mut rec_stack = HashSet::new();

        for node in self.dependencies.keys() {
            if self.has_cycle_util(node, &mut visited, &mut rec_stack) {
                return true;
            }
        }

        false
    }

    /// Utility for cycle detection (DFS)
    fn has_cycle_util(
        &self,
        node: &str,
        visited: &mut HashSet<String>,
        rec_stack: &mut HashSet<String>,
    ) -> bool {
        if rec_stack.contains(node) {
            return true;
        }

        if visited.contains(node) {
            return false;
        }

        visited.insert(node.to_string());
        rec_stack.insert(node.to_string());

        if let Some(neighbors) = self.dependencies.get(node) {
            for neighbor in neighbors {
                if self.has_cycle_util(neighbor, visited, rec_stack) {
                    return true;
                }
            }
        }

        rec_stack.remove(node);
        false
    }

    /// Compute stratification levels
    fn compute_strata(&self) -> Vec<Vec<String>> {
        let mut strata = Vec::new();
        let mut remaining: HashSet<String> = self.dependencies.keys().cloned().collect();

        while !remaining.is_empty() {
            // Find predicates with no dependencies on remaining predicates
            let mut current_stratum = Vec::new();

            for pred in &remaining {
                let has_remaining_deps = self
                    .dependencies
                    .get(pred)
                    .map(|deps| deps.iter().any(|d| remaining.contains(d)))
                    .unwrap_or(false);

                if !has_remaining_deps {
                    current_stratum.push(pred.clone());
                }
            }

            if current_stratum.is_empty() {
                // Shouldn't happen if no cycles, but break to avoid infinite loop
                break;
            }

            for pred in &current_stratum {
                remaining.remove(pred);
            }

            strata.push(current_stratum);
        }

        strata
    }
}

impl Default for StratificationAnalyzer {
    fn default() -> Self {
        Self::new()
    }
}

/// Result of stratification analysis
#[derive(Debug, Clone)]
pub enum StratificationResult {
    /// Program is stratifiable with given strata
    Stratifiable(Vec<Vec<String>>),
    /// Program contains unstratifiable recursion
    NonStratifiable,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ir::{Constant, Term};

    #[test]
    fn test_tabled_inference_basic() {
        let mut kb = KnowledgeBase::new();

        // Add 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 recursive 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())],
                ),
            ],
        ));

        let engine = TabledInferenceEngine::new();

        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();
        assert!(!solutions.is_empty());
    }

    #[test]
    fn test_table_stats() {
        let engine = TabledInferenceEngine::new();
        let stats = engine.table_stats();
        assert_eq!(stats.entries, 0);
        assert_eq!(stats.complete_entries, 0);
    }

    #[test]
    fn test_stratification_no_cycles() {
        let mut kb = KnowledgeBase::new();

        // Add non-recursive rule: grandparent(X, Z) :- parent(X, Y), parent(Y, Z)
        kb.add_rule(Rule::new(
            Predicate::new(
                "grandparent".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(
                    "parent".to_string(),
                    vec![Term::Var("Y".to_string()), Term::Var("Z".to_string())],
                ),
            ],
        ));

        let mut analyzer = StratificationAnalyzer::new();
        let result = analyzer.analyze(&kb);

        match result {
            StratificationResult::Stratifiable(strata) => {
                assert!(!strata.is_empty());
            }
            StratificationResult::NonStratifiable => {
                // Should be stratifiable
                panic!("Expected stratifiable result");
            }
        }
    }

    #[test]
    fn test_fixpoint_engine() {
        let engine = FixpointEngine::new();
        let kb = KnowledgeBase::new();

        // Compute fixpoint (should return same KB for empty KB)
        let result = engine.compute_fixpoint(&kb).unwrap();
        assert_eq!(result.facts.len(), kb.facts.len());
    }
}