ipfrs-tensorlogic 0.2.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
//! Proof tree exporter — renders verified proof trees to multiple output formats.
//!
//! Supported formats:
//! - [`ExportFormat::Dot`] — Graphviz DOT language
//! - [`ExportFormat::Json`] — compact JSON array via serde_json
//! - [`ExportFormat::IndentedText`] — indented ASCII tree (2 spaces per depth)
//! - [`ExportFormat::EdgeList`] — `from_id -> to_id` one per line

use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};

// ---------------------------------------------------------------------------
// Data types
// ---------------------------------------------------------------------------

/// A single node in the exported proof tree.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ExportNode {
    /// Unique identifier for this node.
    pub node_id: u64,
    /// Identifier of the rule applied at this node.
    pub rule_id: String,
    /// The goal (predicate / formula) proved at this node.
    pub goal: String,
    /// Depth of this node from the root (root = 0).
    pub depth: usize,
    /// Ordered list of child node IDs.
    pub children: Vec<u64>,
}

// ---------------------------------------------------------------------------
// Export format
// ---------------------------------------------------------------------------

/// Output format for [`ProofTreeExporter`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ExportFormat {
    /// Graphviz DOT language.
    Dot,
    /// Compact JSON array.
    Json,
    /// Indented ASCII tree (2 spaces per depth level).
    IndentedText,
    /// One `from_id -> to_id` edge per line.
    EdgeList,
}

// ---------------------------------------------------------------------------
// Export configuration
// ---------------------------------------------------------------------------

/// Configuration for [`ProofTreeExporter`].
#[derive(Debug, Clone)]
pub struct ExportConfig {
    /// Target output format.
    pub format: ExportFormat,
    /// Maximum depth to include; deeper nodes are excluded when set.
    pub max_depth: Option<usize>,
    /// Whether to include the rule ID in rendered labels (default: `true`).
    pub include_rule_ids: bool,
    /// Maximum label length in characters; longer labels are truncated (default: 40).
    pub label_max_len: usize,
}

impl Default for ExportConfig {
    fn default() -> Self {
        Self {
            format: ExportFormat::IndentedText,
            max_depth: None,
            include_rule_ids: true,
            label_max_len: 40,
        }
    }
}

impl ExportConfig {
    /// Create a new config with all defaults plus the given format.
    pub fn new(format: ExportFormat) -> Self {
        Self {
            format,
            ..Self::default()
        }
    }
}

// ---------------------------------------------------------------------------
// Exporter
// ---------------------------------------------------------------------------

/// Exports verified proof trees to multiple formats.
pub struct ProofTreeExporter {
    /// Export configuration.
    pub config: ExportConfig,
}

impl ProofTreeExporter {
    /// Create a new exporter with the given configuration.
    pub fn new(config: ExportConfig) -> Self {
        Self { config }
    }

    // -----------------------------------------------------------------------
    // Public API
    // -----------------------------------------------------------------------

    /// Export `nodes` to the string format described by `self.config`.
    ///
    /// Returns `"(empty)"` when `nodes` is empty.
    pub fn export(&self, nodes: &[ExportNode]) -> String {
        if nodes.is_empty() {
            return "(empty)".to_string();
        }

        // Check whether any nodes survive the depth filter before dispatching.
        let would_be_empty = match self.config.max_depth {
            None => false,
            Some(max) => !nodes.iter().any(|n| n.depth <= max),
        };
        if would_be_empty {
            return "(empty)".to_string();
        }

        // The format methods each apply the depth filter internally.
        match self.config.format {
            ExportFormat::Dot => self.to_dot(nodes),
            ExportFormat::Json => self.to_json(nodes),
            ExportFormat::IndentedText => self.to_indented_text(nodes),
            ExportFormat::EdgeList => self.to_edge_list(nodes),
        }
    }

    /// Render nodes as Graphviz DOT.
    pub fn to_dot(&self, nodes: &[ExportNode]) -> String {
        let filtered_owned = self.owned_filtered(nodes);
        let nodes: &[ExportNode] = &filtered_owned;

        let mut out = String::from("digraph proof {\n");

        for node in nodes {
            let label = self.make_label(node);
            // Escape quotes inside the label for DOT safety.
            let escaped = label.replace('\\', "\\\\").replace('"', "\\\"");
            out.push_str(&format!("    {} [label=\"{}\"];\n", node.node_id, escaped));
        }

        // Build a set of node ids present in the filtered slice so we only
        // emit edges between present nodes.
        let id_set: HashSet<u64> = nodes.iter().map(|n| n.node_id).collect();

        for node in nodes {
            for &child_id in &node.children {
                if id_set.contains(&child_id) {
                    out.push_str(&format!("    {} -> {};\n", node.node_id, child_id));
                }
            }
        }

        out.push('}');
        out
    }

    /// Render nodes as a compact JSON array.
    pub fn to_json(&self, nodes: &[ExportNode]) -> String {
        let filtered_owned = self.owned_filtered(nodes);
        // serde_json::to_string returns Result; fall back to a minimal
        // representation if serialization fails (should never happen here).
        serde_json::to_string(&filtered_owned).unwrap_or_else(|_| "[]".to_string())
    }

    /// Render nodes as an indented ASCII tree (DFS from root).
    pub fn to_indented_text(&self, nodes: &[ExportNode]) -> String {
        let filtered_owned = self.owned_filtered(nodes);
        let nodes: &[ExportNode] = &filtered_owned;

        if nodes.is_empty() {
            return String::new();
        }

        let root_id = Self::find_root(nodes);
        let id_map: HashMap<u64, &ExportNode> = nodes.iter().map(|n| (n.node_id, n)).collect();

        let mut out = String::new();
        let mut stack: Vec<u64> = vec![root_id];

        while let Some(id) = stack.pop() {
            if let Some(node) = id_map.get(&id) {
                let indent = " ".repeat(2 * node.depth);
                if self.config.include_rule_ids {
                    out.push_str(&format!(
                        "{}{} [rule: {}]\n",
                        indent,
                        self.truncate(&node.goal),
                        node.rule_id
                    ));
                } else {
                    out.push_str(&format!("{}{}\n", indent, self.truncate(&node.goal)));
                }
                // Push children in reverse order so they are popped in order.
                for &child_id in node.children.iter().rev() {
                    stack.push(child_id);
                }
            }
        }

        // Remove trailing newline for consistent output.
        if out.ends_with('\n') {
            out.pop();
        }
        out
    }

    /// Render nodes as a flat edge list (`parent_id -> child_id`).
    pub fn to_edge_list(&self, nodes: &[ExportNode]) -> String {
        let filtered_owned = self.owned_filtered(nodes);
        let nodes: &[ExportNode] = &filtered_owned;

        let id_set: HashSet<u64> = nodes.iter().map(|n| n.node_id).collect();

        let mut lines: Vec<String> = Vec::new();
        for node in nodes {
            for &child_id in &node.children {
                if id_set.contains(&child_id) {
                    lines.push(format!("{} -> {}", node.node_id, child_id));
                }
            }
        }

        lines.join("\n")
    }

    /// Number of nodes that pass the `max_depth` filter.
    pub fn node_count(&self, nodes: &[ExportNode]) -> usize {
        self.owned_filtered(nodes).len()
    }

    /// Total number of child edges among nodes that pass the `max_depth` filter.
    pub fn edge_count(&self, nodes: &[ExportNode]) -> usize {
        let filtered = self.owned_filtered(nodes);
        let id_set: HashSet<u64> = filtered.iter().map(|n| n.node_id).collect();
        filtered
            .iter()
            .flat_map(|n| n.children.iter())
            .filter(|&&child_id| id_set.contains(&child_id))
            .count()
    }

    // -----------------------------------------------------------------------
    // Helpers
    // -----------------------------------------------------------------------

    /// Return owned clones of nodes that pass the `max_depth` filter.
    fn owned_filtered(&self, nodes: &[ExportNode]) -> Vec<ExportNode> {
        match self.config.max_depth {
            None => nodes.to_vec(),
            Some(max) => nodes.iter().filter(|n| n.depth <= max).cloned().collect(),
        }
    }

    /// Build the display label for a node.
    fn make_label(&self, node: &ExportNode) -> String {
        if self.config.include_rule_ids {
            let combined = format!("{} ({})", node.goal, node.rule_id);
            self.truncate(&combined)
        } else {
            self.truncate(&node.goal)
        }
    }

    /// Truncate a string to at most `label_max_len` characters, appending `…`
    /// when truncation occurs.
    fn truncate(&self, s: &str) -> String {
        let max = self.config.label_max_len;
        if s.chars().count() <= max {
            s.to_string()
        } else {
            let truncated: String = s.chars().take(max.saturating_sub(1)).collect();
            format!("{}", truncated)
        }
    }

    /// Find the root node: the node whose `node_id` does not appear in any
    /// other node's `children` list.  Falls back to `nodes[0]` when every
    /// node appears as a child (cyclic / degenerate tree).
    fn find_root(nodes: &[ExportNode]) -> u64 {
        let all_children: HashSet<u64> = nodes
            .iter()
            .flat_map(|n| n.children.iter().copied())
            .collect();

        nodes
            .iter()
            .find(|n| !all_children.contains(&n.node_id))
            .map(|n| n.node_id)
            .unwrap_or(nodes[0].node_id)
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    // ------------------------------------------------------------------
    // Helpers for building test fixtures
    // ------------------------------------------------------------------

    fn leaf(id: u64, rule: &str, goal: &str, depth: usize) -> ExportNode {
        ExportNode {
            node_id: id,
            rule_id: rule.to_string(),
            goal: goal.to_string(),
            depth,
            children: vec![],
        }
    }

    fn parent_node(
        id: u64,
        rule: &str,
        goal: &str,
        depth: usize,
        children: Vec<u64>,
    ) -> ExportNode {
        ExportNode {
            node_id: id,
            rule_id: rule.to_string(),
            goal: goal.to_string(),
            depth,
            children,
        }
    }

    /// Single-node tree.
    fn single() -> Vec<ExportNode> {
        vec![leaf(1, "R1", "fact(a)", 0)]
    }

    /// Two-level tree: root → child.
    fn two_level() -> Vec<ExportNode> {
        vec![
            parent_node(1, "R1", "root_goal", 0, vec![2]),
            leaf(2, "R2", "child_goal", 1),
        ]
    }

    /// Three-level tree: root → child → grandchild.
    fn three_level() -> Vec<ExportNode> {
        vec![
            parent_node(1, "R1", "root_goal", 0, vec![2]),
            parent_node(2, "R2", "child_goal", 1, vec![3]),
            leaf(3, "R3", "grandchild_goal", 2),
        ]
    }

    // ------------------------------------------------------------------
    // 1. new() with config
    // ------------------------------------------------------------------
    #[test]
    fn test_new_with_config() {
        let cfg = ExportConfig::new(ExportFormat::Dot);
        let exporter = ProofTreeExporter::new(cfg);
        assert_eq!(exporter.config.format, ExportFormat::Dot);
        assert!(exporter.config.include_rule_ids);
        assert_eq!(exporter.config.label_max_len, 40);
        assert!(exporter.config.max_depth.is_none());
    }

    // ------------------------------------------------------------------
    // 2. export() returns "(empty)" for empty input
    // ------------------------------------------------------------------
    #[test]
    fn test_export_empty_returns_empty_sentinel() {
        let exporter = ProofTreeExporter::new(ExportConfig::new(ExportFormat::Dot));
        assert_eq!(exporter.export(&[]), "(empty)");
    }

    // ------------------------------------------------------------------
    // 3. to_dot: single node, no edges
    // ------------------------------------------------------------------
    #[test]
    fn test_to_dot_single_node_no_edges() {
        let exporter = ProofTreeExporter::new(ExportConfig::new(ExportFormat::Dot));
        let nodes = single();
        let dot = exporter.to_dot(&nodes);
        assert!(dot.contains("digraph proof {"));
        assert!(dot.contains("1 [label="));
        // No edge lines
        assert!(!dot.contains("->"));
    }

    // ------------------------------------------------------------------
    // 4. to_dot: parent → child edge present
    // ------------------------------------------------------------------
    #[test]
    fn test_to_dot_edge_present() {
        let exporter = ProofTreeExporter::new(ExportConfig::new(ExportFormat::Dot));
        let nodes = two_level();
        let dot = exporter.to_dot(&nodes);
        assert!(dot.contains("1 -> 2;"));
    }

    // ------------------------------------------------------------------
    // 5. to_dot: label truncated at label_max_len
    // ------------------------------------------------------------------
    #[test]
    fn test_to_dot_label_truncated() {
        let mut cfg = ExportConfig::new(ExportFormat::Dot);
        cfg.label_max_len = 10;
        cfg.include_rule_ids = false;
        let exporter = ProofTreeExporter::new(cfg);
        let nodes = vec![leaf(1, "R1", "a_very_long_goal_that_exceeds_limit", 0)];
        let dot = exporter.to_dot(&nodes);
        // Label should be truncated (≤ 10 chars + ellipsis marker inside quotes)
        // The raw goal is 35 chars; we expect truncation.
        assert!(dot.contains('') || dot.contains("a_very_lo"));
        // Original full string should not appear
        assert!(!dot.contains("a_very_long_goal_that_exceeds_limit"));
    }

    // ------------------------------------------------------------------
    // 6. to_dot: rule_id omitted when include_rule_ids=false
    // ------------------------------------------------------------------
    #[test]
    fn test_to_dot_no_rule_ids() {
        let mut cfg = ExportConfig::new(ExportFormat::Dot);
        cfg.include_rule_ids = false;
        let exporter = ProofTreeExporter::new(cfg);
        let nodes = single();
        let dot = exporter.to_dot(&nodes);
        assert!(!dot.contains("R1"));
    }

    // ------------------------------------------------------------------
    // 7. to_json: round-trip via serde_json::from_str
    // ------------------------------------------------------------------
    #[test]
    fn test_to_json_round_trip() {
        let exporter = ProofTreeExporter::new(ExportConfig::new(ExportFormat::Json));
        let nodes = two_level();
        let json = exporter.to_json(&nodes);
        let recovered: Vec<ExportNode> = serde_json::from_str(&json).expect("valid JSON");
        assert_eq!(recovered.len(), 2);
        assert_eq!(recovered[0].node_id, 1);
        assert_eq!(recovered[1].node_id, 2);
    }

    // ------------------------------------------------------------------
    // 8. to_json: node_id and goal present in output string
    // ------------------------------------------------------------------
    #[test]
    fn test_to_json_contains_fields() {
        let exporter = ProofTreeExporter::new(ExportConfig::new(ExportFormat::Json));
        let nodes = single();
        let json = exporter.to_json(&nodes);
        assert!(json.contains("node_id"));
        assert!(json.contains("goal"));
        assert!(json.contains("fact(a)"));
    }

    // ------------------------------------------------------------------
    // 9. to_indented_text: root at indent 0
    // ------------------------------------------------------------------
    #[test]
    fn test_to_indented_text_root_no_indent() {
        let exporter = ProofTreeExporter::new(ExportConfig::new(ExportFormat::IndentedText));
        let nodes = two_level();
        let text = exporter.to_indented_text(&nodes);
        // First line must start with the root goal (no leading spaces)
        let first_line = text.lines().next().expect("at least one line");
        assert!(!first_line.starts_with(' '));
        assert!(first_line.contains("root_goal"));
    }

    // ------------------------------------------------------------------
    // 10. to_indented_text: child at indent 2
    // ------------------------------------------------------------------
    #[test]
    fn test_to_indented_text_child_indent_2() {
        let exporter = ProofTreeExporter::new(ExportConfig::new(ExportFormat::IndentedText));
        let nodes = two_level();
        let text = exporter.to_indented_text(&nodes);
        let child_line = text
            .lines()
            .find(|l| l.contains("child_goal"))
            .expect("child line present");
        assert!(
            child_line.starts_with("  "),
            "expected 2-space indent, got: {:?}",
            child_line
        );
    }

    // ------------------------------------------------------------------
    // 11. to_indented_text: grandchild at indent 4
    // ------------------------------------------------------------------
    #[test]
    fn test_to_indented_text_grandchild_indent_4() {
        let exporter = ProofTreeExporter::new(ExportConfig::new(ExportFormat::IndentedText));
        let nodes = three_level();
        let text = exporter.to_indented_text(&nodes);
        let gc_line = text
            .lines()
            .find(|l| l.contains("grandchild_goal"))
            .expect("grandchild line present");
        assert!(
            gc_line.starts_with("    "),
            "expected 4-space indent, got: {:?}",
            gc_line
        );
    }

    // ------------------------------------------------------------------
    // 12. to_edge_list: "X -> Y" format
    // ------------------------------------------------------------------
    #[test]
    fn test_to_edge_list_format() {
        let exporter = ProofTreeExporter::new(ExportConfig::new(ExportFormat::EdgeList));
        let nodes = two_level();
        let list = exporter.to_edge_list(&nodes);
        assert!(list.contains("1 -> 2"), "output was: {}", list);
    }

    // ------------------------------------------------------------------
    // 13. to_edge_list: empty for no-child nodes
    // ------------------------------------------------------------------
    #[test]
    fn test_to_edge_list_empty_for_leaf_only() {
        let exporter = ProofTreeExporter::new(ExportConfig::new(ExportFormat::EdgeList));
        let nodes = single();
        let list = exporter.to_edge_list(&nodes);
        assert!(list.is_empty(), "expected empty edge list, got: {:?}", list);
    }

    // ------------------------------------------------------------------
    // 14. max_depth filters out deep nodes
    // ------------------------------------------------------------------
    #[test]
    fn test_max_depth_filters_deep_nodes() {
        let mut cfg = ExportConfig::new(ExportFormat::IndentedText);
        cfg.max_depth = Some(1);
        let exporter = ProofTreeExporter::new(cfg);
        let nodes = three_level(); // depths 0, 1, 2
        let text = exporter.to_indented_text(&nodes);
        assert!(
            !text.contains("grandchild_goal"),
            "depth-2 node should be filtered"
        );
        assert!(text.contains("child_goal"), "depth-1 node should remain");
    }

    // ------------------------------------------------------------------
    // 15. node_count respects max_depth
    // ------------------------------------------------------------------
    #[test]
    fn test_node_count_with_max_depth() {
        let mut cfg = ExportConfig::new(ExportFormat::Dot);
        cfg.max_depth = Some(1);
        let exporter = ProofTreeExporter::new(cfg);
        let nodes = three_level(); // 3 nodes at depths 0, 1, 2
        assert_eq!(exporter.node_count(&nodes), 2); // only depths 0 and 1
    }

    // ------------------------------------------------------------------
    // 16. edge_count correct
    // ------------------------------------------------------------------
    #[test]
    fn test_edge_count_correct() {
        let exporter = ProofTreeExporter::new(ExportConfig::new(ExportFormat::Dot));
        let nodes = three_level(); // 2 edges: 1→2 and 2→3
        assert_eq!(exporter.edge_count(&nodes), 2);
    }

    // ------------------------------------------------------------------
    // Bonus: edge_count with max_depth cuts the deeper edge
    // ------------------------------------------------------------------
    #[test]
    fn test_edge_count_with_max_depth() {
        let mut cfg = ExportConfig::new(ExportFormat::Dot);
        cfg.max_depth = Some(1);
        let exporter = ProofTreeExporter::new(cfg);
        let nodes = three_level();
        // Node 3 (depth 2) filtered out → only 1→2 edge remains
        assert_eq!(exporter.edge_count(&nodes), 1);
    }

    // ------------------------------------------------------------------
    // Bonus: export dispatches to edge_list format correctly
    // ------------------------------------------------------------------
    #[test]
    fn test_export_dispatches_edge_list() {
        let exporter = ProofTreeExporter::new(ExportConfig::new(ExportFormat::EdgeList));
        let result = exporter.export(&two_level());
        assert!(result.contains("->"));
    }

    // ------------------------------------------------------------------
    // Bonus: export dispatches to json format correctly
    // ------------------------------------------------------------------
    #[test]
    fn test_export_dispatches_json() {
        let exporter = ProofTreeExporter::new(ExportConfig::new(ExportFormat::Json));
        let result = exporter.export(&single());
        assert!(result.starts_with('['));
    }
}