oxiz-spacer 0.2.0

Property Directed Reachability (PDR/IC3) engine for OxiZ - Horn clause solving
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
//! Non-linear CHC constraint handling.
//!
//! This module provides support for non-linear Constrained Horn Clauses,
//! where rules can have multiple recursive predicate applications in the body.
//!
//! ## Non-linear CHCs
//!
//! Non-linear CHCs have the form:
//! ```text
//! P1(x1) ∧ P2(x2) ∧ ... ∧ Pn(xn) ∧ φ => P(x)
//! ```
//!
//! Where multiple predicate applications appear in the body. This is more
//! general than linear CHCs and can express problems like:
//! - Tree invariants (left child ∧ right child => parent)
//! - Mutual recursion
//! - Complex data structure verification
//!
//! ## Solving Approach
//!
//! Non-linear CHCs require different solving strategies:
//! 1. **Tree interpolation**: Generate interpolants for tree-like derivations
//! 2. **Product construction**: Build product automata for multiple predicates
//! 3. **Lemma combination**: Combine lemmas from different predicates
//!
//! Reference: Non-linear CHC solving techniques from literature

use crate::chc::{ChcSystem, PredId, PredicateApp, Rule};
use crate::frames::LemmaId;
use crate::pdr::SpacerError;
use oxiz_core::{TermId, TermManager};
use smallvec::SmallVec;
use std::collections::{HashMap, HashSet};
use thiserror::Error;

/// Errors specific to non-linear CHC handling
#[derive(Error, Debug)]
pub enum NonLinearError {
    /// Non-linear rule not supported by current strategy
    #[error("non-linear rule not supported: {0}")]
    Unsupported(String),
    /// Interpolation failed
    #[error("interpolation failed: {0}")]
    InterpolationFailed(String),
    /// Product construction failed
    #[error("product construction failed: {0}")]
    ProductConstructionFailed(String),
    /// Spacer error
    #[error("spacer error: {0}")]
    Spacer(#[from] SpacerError),
}

/// Result type for non-linear operations
pub type NonLinearResult<T> = Result<T, NonLinearError>;

/// Analysis of a non-linear rule
#[derive(Debug, Clone)]
pub struct NonLinearRuleAnalysis {
    /// The rule being analyzed
    pub rule_id: usize,
    /// Number of predicate applications in body
    pub body_pred_count: usize,
    /// Predicates in the body
    pub body_preds: SmallVec<[PredId; 4]>,
    /// Head predicate
    pub head_pred: PredId,
    /// Degree of non-linearity (0 = linear, 1 = binary, 2+ = higher-order)
    pub nonlinearity_degree: usize,
    /// Whether this forms a cycle in the dependency graph
    pub is_cyclic: bool,
}

impl NonLinearRuleAnalysis {
    /// Analyze a rule for non-linearity
    pub fn analyze(rule: &Rule) -> Self {
        let body_preds: SmallVec<[PredId; 4]> =
            rule.body.predicates.iter().map(|app| app.pred).collect();

        let body_pred_count = body_preds.len();
        let nonlinearity_degree = body_pred_count.saturating_sub(1);

        let head_pred = rule.head_predicate().unwrap_or(PredId(0)); // Use 0 for queries

        Self {
            rule_id: 0, // Will be set by caller
            body_pred_count,
            body_preds,
            head_pred,
            nonlinearity_degree,
            is_cyclic: false, // Will be determined by dependency analysis
        }
    }

    /// Check if this rule is linear
    pub fn is_linear(&self) -> bool {
        self.body_pred_count <= 1
    }

    /// Check if this rule is binary non-linear
    pub fn is_binary_nonlinear(&self) -> bool {
        self.body_pred_count == 2
    }
}

/// Non-linear CHC analyzer
pub struct NonLinearAnalyzer {
    /// Analyses for each rule
    rule_analyses: Vec<NonLinearRuleAnalysis>,
    /// Dependency graph (pred -> dependent preds)
    dependencies: HashMap<PredId, HashSet<PredId>>,
    /// Strongly connected components
    sccs: Vec<HashSet<PredId>>,
}

impl NonLinearAnalyzer {
    /// Create a new non-linear analyzer
    pub fn new() -> Self {
        Self {
            rule_analyses: Vec::new(),
            dependencies: HashMap::new(),
            sccs: Vec::new(),
        }
    }

    /// Analyze a CHC system for non-linearity
    pub fn analyze(&mut self, system: &ChcSystem) -> NonLinearResult<()> {
        // Analyze each rule
        self.rule_analyses.clear();
        for (idx, rule) in system.rules().enumerate() {
            let mut analysis = NonLinearRuleAnalysis::analyze(rule);
            analysis.rule_id = idx;
            self.rule_analyses.push(analysis);
        }

        // Build dependency graph
        self.build_dependency_graph(system);

        // Compute SCCs to detect cycles
        self.compute_sccs();

        // Mark cyclic rules
        self.mark_cyclic_rules();

        Ok(())
    }

    /// Get statistics about non-linearity
    pub fn statistics(&self) -> NonLinearStats {
        let total_rules = self.rule_analyses.len();
        let linear_rules = self.rule_analyses.iter().filter(|a| a.is_linear()).count();
        let binary_nonlinear = self
            .rule_analyses
            .iter()
            .filter(|a| a.is_binary_nonlinear())
            .count();
        let higher_order_nonlinear = self
            .rule_analyses
            .iter()
            .filter(|a| a.nonlinearity_degree > 1)
            .count();

        let max_body_preds = self
            .rule_analyses
            .iter()
            .map(|a| a.body_pred_count)
            .max()
            .unwrap_or(0);

        NonLinearStats {
            total_rules,
            linear_rules,
            binary_nonlinear,
            higher_order_nonlinear,
            max_body_preds,
            num_sccs: self.sccs.len(),
        }
    }

    /// Build dependency graph from rules
    fn build_dependency_graph(&mut self, system: &ChcSystem) {
        self.dependencies.clear();
        for rule in system.rules() {
            if let Some(head_pred) = rule.head_predicate() {
                for app in &rule.body.predicates {
                    self.dependencies
                        .entry(head_pred)
                        .or_default()
                        .insert(app.pred);
                }
            }
        }
    }

    /// Compute strongly connected components (Tarjan's algorithm)
    fn compute_sccs(&mut self) {
        self.sccs.clear();

        // Get all predicates in the dependency graph
        let mut all_preds: HashSet<PredId> = self.dependencies.keys().copied().collect();
        for deps in self.dependencies.values() {
            all_preds.extend(deps);
        }

        if all_preds.is_empty() {
            return;
        }

        // Tarjan's algorithm state
        let mut index = 0u32;
        let mut stack = Vec::new();
        let mut indices: HashMap<PredId, u32> = HashMap::new();
        let mut lowlinks: HashMap<PredId, u32> = HashMap::new();
        let mut on_stack: HashSet<PredId> = HashSet::new();

        // Run Tarjan's algorithm for each unvisited predicate
        for &pred in &all_preds {
            if !indices.contains_key(&pred) {
                self.tarjan_strongconnect(
                    pred,
                    &mut index,
                    &mut stack,
                    &mut indices,
                    &mut lowlinks,
                    &mut on_stack,
                );
            }
        }
    }

    /// Tarjan's strongconnect procedure
    fn tarjan_strongconnect(
        &mut self,
        pred: PredId,
        index: &mut u32,
        stack: &mut Vec<PredId>,
        indices: &mut HashMap<PredId, u32>,
        lowlinks: &mut HashMap<PredId, u32>,
        on_stack: &mut HashSet<PredId>,
    ) {
        // Set the depth index for pred to the smallest unused index
        indices.insert(pred, *index);
        lowlinks.insert(pred, *index);
        *index += 1;
        stack.push(pred);
        on_stack.insert(pred);

        // Consider successors of pred
        // Collect successors to avoid borrow checker issues
        let successors: Vec<PredId> = self
            .dependencies
            .get(&pred)
            .map(|s| s.iter().copied().collect())
            .unwrap_or_default();

        for succ in successors {
            if !indices.contains_key(&succ) {
                // Successor has not yet been visited; recurse on it
                self.tarjan_strongconnect(succ, index, stack, indices, lowlinks, on_stack);
                let succ_lowlink = *lowlinks
                    .get(&succ)
                    .expect("lowlink exists for visited node");
                let pred_lowlink = lowlinks.get(&pred).expect("lowlink exists for predecessor");
                lowlinks.insert(pred, (*pred_lowlink).min(succ_lowlink));
            } else if on_stack.contains(&succ) {
                // Successor is in stack and hence in the current SCC
                let succ_index = *indices.get(&succ).expect("index exists for visited node");
                let pred_lowlink = lowlinks.get(&pred).expect("lowlink exists for predecessor");
                lowlinks.insert(pred, (*pred_lowlink).min(succ_index));
            }
        }

        // If pred is a root node, pop the stack and create an SCC
        if lowlinks.get(&pred) == indices.get(&pred) {
            let mut scc = HashSet::new();
            loop {
                let w = stack.pop().expect("collection validated to be non-empty");
                on_stack.remove(&w);
                scc.insert(w);
                if w == pred {
                    break;
                }
            }
            self.sccs.push(scc);
        }
    }

    /// Mark rules that are cyclic based on SCC analysis
    fn mark_cyclic_rules(&mut self) {
        // A rule is cyclic if its head and any body predicate are in the same SCC
        for analysis in &mut self.rule_analyses {
            analysis.is_cyclic = self.sccs.iter().any(|scc| {
                scc.contains(&analysis.head_pred)
                    && analysis.body_preds.iter().any(|p| scc.contains(p))
            });
        }
    }

    /// Get analysis for a specific rule
    pub fn get_analysis(&self, rule_id: usize) -> Option<&NonLinearRuleAnalysis> {
        self.rule_analyses.get(rule_id)
    }

    /// Check if the system is fully linear
    pub fn is_linear(&self) -> bool {
        self.rule_analyses.iter().all(|a| a.is_linear())
    }
}

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

/// Statistics about non-linearity in a CHC system
#[derive(Debug, Clone)]
pub struct NonLinearStats {
    /// Total number of rules
    pub total_rules: usize,
    /// Number of linear rules
    pub linear_rules: usize,
    /// Number of binary non-linear rules (2 preds in body)
    pub binary_nonlinear: usize,
    /// Number of higher-order non-linear rules (3+ preds in body)
    pub higher_order_nonlinear: usize,
    /// Maximum number of predicates in any rule body
    pub max_body_preds: usize,
    /// Number of strongly connected components
    pub num_sccs: usize,
}

/// Tree interpolant for non-linear rules
///
/// When a rule has multiple predicates in the body, we need to compute
/// a tree of interpolants rather than a single interpolant.
#[derive(Debug, Clone)]
pub struct TreeInterpolant {
    /// Interpolants for each body predicate
    pub body_interpolants: Vec<TermId>,
    /// Combined interpolant for the rule
    pub combined: TermId,
    /// Structure of the interpolation tree
    pub tree_structure: InterpolantTree,
}

/// Structure of an interpolation tree
#[derive(Debug, Clone)]
pub enum InterpolantTree {
    /// Leaf node (single predicate)
    Leaf { pred: PredId, interpolant: TermId },
    /// Binary node (two children)
    Binary {
        left: Box<InterpolantTree>,
        right: Box<InterpolantTree>,
        combined: TermId,
    },
}

/// Non-linear lemma combiner
///
/// Combines lemmas from multiple predicates in a non-linear rule body.
pub struct NonLinearLemmaCombiner;

impl NonLinearLemmaCombiner {
    /// Combine lemmas from multiple predicates
    ///
    /// Given lemmas for P1(x1), P2(x2), ..., combine them to learn a lemma for P(x)
    /// where the rule is: P1(x1) ∧ P2(x2) ∧ φ => P(x)
    pub fn combine_lemmas(
        _terms: &mut TermManager,
        _predicates: &[PredId],
        _lemmas: &[LemmaId],
        constraint: TermId,
    ) -> NonLinearResult<TermId> {
        // Combine lemmas from multiple branches of a non-linear rule
        // For a rule: P1(x1) ∧ P2(x2) ∧ φ => P(x)
        // Given lemmas for P1 and P2, combine them to learn a lemma for P

        // Basic implementation: use the constraint as the combined lemma
        // A full implementation would:
        // 1. Get the formulas for each lemma
        // 2. Combine them using conjunction
        // 3. Project out variables not in the head
        // 4. Simplify the result

        // For now, return the constraint as a conservative approximation
        Ok(constraint)
    }

    /// Compute tree interpolant for a non-linear derivation
    pub fn compute_tree_interpolant(
        terms: &mut TermManager,
        body_apps: &[PredicateApp],
        constraint: TermId,
    ) -> NonLinearResult<TreeInterpolant> {
        // Tree interpolation for non-linear rules
        // For a rule: P1(x1) ∧ P2(x2) ∧ ... ∧ Pn(xn) ∧ C => Q(y)
        // We need to compute interpolants between different partitions

        if body_apps.is_empty() {
            // No body predicates - degenerate case
            return Ok(TreeInterpolant {
                body_interpolants: Vec::new(),
                combined: terms.mk_true(),
                tree_structure: InterpolantTree::Leaf {
                    pred: PredId(0),
                    interpolant: terms.mk_true(),
                },
            });
        }

        if body_apps.len() == 1 {
            // Single body predicate - also degenerate
            // The interpolant is just the constraint
            return Ok(TreeInterpolant {
                body_interpolants: vec![constraint],
                combined: constraint,
                tree_structure: InterpolantTree::Leaf {
                    pred: body_apps[0].pred,
                    interpolant: constraint,
                },
            });
        }

        // For multiple body predicates, build a binary tree
        // Split the predicates into left and right partitions
        let mid = body_apps.len() / 2;
        let left_apps = &body_apps[..mid];
        let right_apps = &body_apps[mid..];

        // Recursively compute interpolants for left and right partitions
        let left_tree = Self::compute_tree_interpolant(terms, left_apps, constraint)?;
        let right_tree = Self::compute_tree_interpolant(terms, right_apps, constraint)?;

        // Compute interpolant between left and right partitions
        // For now, use a simple heuristic: conjoin the constraint
        let combined_interpolant = constraint;

        // Collect all body interpolants
        let mut body_interpolants = Vec::new();
        body_interpolants.extend(left_tree.body_interpolants);
        body_interpolants.extend(right_tree.body_interpolants);

        Ok(TreeInterpolant {
            body_interpolants,
            combined: combined_interpolant,
            tree_structure: InterpolantTree::Binary {
                left: Box::new(left_tree.tree_structure),
                right: Box::new(right_tree.tree_structure),
                combined: combined_interpolant,
            },
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::chc::{ChcSystem, PredicateApp, RuleBody, RuleHead};
    use oxiz_core::TermManager;

    #[test]
    fn test_linear_rule_analysis() {
        let mut terms = TermManager::new();
        let mut system = ChcSystem::new();

        // Declare predicates
        let p = system.declare_predicate("P", [terms.sorts.int_sort]);

        // Linear rule: x = 0 => P(x)
        let x = terms.mk_var("x", terms.sorts.int_sort);
        let zero = terms.mk_int(0);
        let constraint = terms.mk_eq(x, zero);

        let rule = Rule {
            id: crate::chc::RuleId(0),
            vars: vec![("x".to_string(), terms.sorts.int_sort)].into(),
            body: RuleBody::init(constraint),
            head: RuleHead::Predicate(PredicateApp::new(p, [x])),
            name: None,
        };

        let analysis = NonLinearRuleAnalysis::analyze(&rule);
        assert!(analysis.is_linear());
        assert_eq!(analysis.nonlinearity_degree, 0);
    }

    #[test]
    fn test_binary_nonlinear_rule_analysis() {
        let mut terms = TermManager::new();
        let mut system = ChcSystem::new();

        // Declare predicates
        let p = system.declare_predicate("P", [terms.sorts.int_sort]);

        // Binary non-linear rule: P(x1) ∧ P(x2) => P(x1 + x2)
        let x1 = terms.mk_var("x1", terms.sorts.int_sort);
        let x2 = terms.mk_var("x2", terms.sorts.int_sort);

        let rule = Rule {
            id: crate::chc::RuleId(0),
            vars: vec![
                ("x1".to_string(), terms.sorts.int_sort),
                ("x2".to_string(), terms.sorts.int_sort),
            ]
            .into(),
            body: RuleBody::new(
                vec![PredicateApp::new(p, [x1]), PredicateApp::new(p, [x2])],
                terms.mk_true(),
            ),
            head: RuleHead::Predicate(PredicateApp::new(p, [terms.mk_add([x1, x2])])),
            name: None,
        };

        let analysis = NonLinearRuleAnalysis::analyze(&rule);
        assert!(!analysis.is_linear());
        assert!(analysis.is_binary_nonlinear());
        assert_eq!(analysis.nonlinearity_degree, 1);
        assert_eq!(analysis.body_pred_count, 2);
    }

    #[test]
    fn test_nonlinear_analyzer() {
        let mut terms = TermManager::new();
        let mut system = ChcSystem::new();
        let p = system.declare_predicate("P", [terms.sorts.int_sort]);

        // Add a linear rule
        let x = terms.mk_var("x", terms.sorts.int_sort);
        let zero = terms.mk_int(0);
        system.add_rule(
            vec![("x".to_string(), terms.sorts.int_sort)],
            RuleBody::init(terms.mk_eq(x, zero)),
            RuleHead::Predicate(PredicateApp::new(p, [x])),
            None,
        );

        // Add a binary non-linear rule
        let x1 = terms.mk_var("x1", terms.sorts.int_sort);
        let x2 = terms.mk_var("x2", terms.sorts.int_sort);
        system.add_rule(
            vec![
                ("x1".to_string(), terms.sorts.int_sort),
                ("x2".to_string(), terms.sorts.int_sort),
            ],
            RuleBody::new(
                vec![PredicateApp::new(p, [x1]), PredicateApp::new(p, [x2])],
                terms.mk_true(),
            ),
            RuleHead::Predicate(PredicateApp::new(p, [terms.mk_add([x1, x2])])),
            None,
        );

        let mut analyzer = NonLinearAnalyzer::new();
        analyzer
            .analyze(&system)
            .expect("test operation should succeed");

        let stats = analyzer.statistics();
        assert_eq!(stats.total_rules, 2);
        assert_eq!(stats.linear_rules, 1);
        assert_eq!(stats.binary_nonlinear, 1);
        assert_eq!(stats.max_body_preds, 2);
        assert!(!analyzer.is_linear());
    }
}