tensorlogic-ir 0.1.0

Intermediate representation (IR) and AST types for TensorLogic
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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
//! # Sequent Calculus for Proof-Theoretic Foundations
//!
//! This module implements sequent calculus representations and inference rules for TensorLogic.
//! Sequent calculus provides a formal foundation for logical reasoning and proof construction.
//!
//! ## Overview
//!
//! A **sequent** is a formal statement of the form `Γ ⊢ Δ` where:
//! - `Γ` (Gamma) is a multiset of antecedent formulas (hypotheses)
//! - `Δ` (Delta) is a multiset of consequent formulas (conclusions)
//! - `⊢` (turnstile) represents the entailment relation
//!
//! The sequent `Γ ⊢ Δ` is valid if: assuming all formulas in Γ are true,
//! at least one formula in Δ must be true.
//!
//! ## Inference Rules
//!
//! This module implements the following sequent calculus rules:
//!
//! ### Structural Rules
//! - **Identity**: `A ⊢ A`
//! - **Weakening**: From `Γ ⊢ Δ` derive `Γ, A ⊢ Δ` or `Γ ⊢ Δ, A`
//! - **Contraction**: From `Γ, A, A ⊢ Δ` derive `Γ, A ⊢ Δ`
//! - **Exchange**: Reorder formulas in Γ or Δ
//! - **Cut**: From `Γ ⊢ Δ, A` and `A, Γ' ⊢ Δ'` derive `Γ, Γ' ⊢ Δ, Δ'`
//!
//! ### Logical Rules (Left and Right)
//! - **AND-Left**: From `Γ, A, B ⊢ Δ` derive `Γ, A ∧ B ⊢ Δ`
//! - **AND-Right**: From `Γ ⊢ Δ, A` and `Γ ⊢ Δ, B` derive `Γ ⊢ Δ, A ∧ B`
//! - **OR-Left**: From `Γ, A ⊢ Δ` and `Γ, B ⊢ Δ` derive `Γ, A ∨ B ⊢ Δ`
//! - **OR-Right**: From `Γ ⊢ Δ, A, B` derive `Γ ⊢ Δ, A ∨ B`
//! - **NOT-Left**: From `Γ ⊢ Δ, A` derive `Γ, ¬A ⊢ Δ`
//! - **NOT-Right**: From `Γ, A ⊢ Δ` derive `Γ ⊢ Δ, ¬A`
//! - **IMPLY-Left**: From `Γ ⊢ Δ, A` and `Γ, B ⊢ Δ` derive `Γ, A → B ⊢ Δ`
//! - **IMPLY-Right**: From `Γ, A ⊢ Δ, B` derive `Γ ⊢ Δ, A → B`
//!
//! ### Quantifier Rules
//! - **EXISTS-Left**: From `Γ, A[t/x] ⊢ Δ` derive `Γ, ∃x.A ⊢ Δ` (t is fresh)
//! - **EXISTS-Right**: From `Γ ⊢ Δ, A[t/x]` derive `Γ ⊢ Δ, ∃x.A`
//! - **FORALL-Left**: From `Γ, A[t/x] ⊢ Δ` derive `Γ, ∀x.A ⊢ Δ`
//! - **FORALL-Right**: From `Γ ⊢ Δ, A[t/x]` derive `Γ ⊢ Δ, ∀x.A` (t is fresh)
//!
//! ## Applications
//!
//! - **Proof Search**: Construct proofs by applying inference rules backward
//! - **Proof Checking**: Verify that a derivation tree is valid
//! - **Proof Normalization**: Transform proofs to normal forms (cut-elimination)
//! - **Compilation**: Use sequent calculus to guide tensor compilation strategies
//!
//! ## Example
//!
//! ```rust
//! use tensorlogic_ir::{TLExpr, Term, Sequent, InferenceRule, ProofTree};
//!
//! // Construct a sequent: P(x) ∧ Q(x) ⊢ P(x)
//! let p = TLExpr::pred("P", vec![Term::var("x")]);
//! let q = TLExpr::pred("Q", vec![Term::var("x")]);
//! let and_pq = TLExpr::and(p.clone(), q);
//!
//! let conclusion = Sequent::new(vec![and_pq], vec![p.clone()]);
//!
//! // Construct a proof tree: Identity axiom as premise, then apply AndLeft
//! let identity_premise = ProofTree::identity(p.clone());
//! let proof = ProofTree::new(
//!     conclusion,
//!     InferenceRule::AndLeft { index: 0 },
//!     vec![identity_premise]
//! );
//!
//! assert!(proof.is_valid());
//! ```

use crate::expr::TLExpr;
use crate::term::Term;
use crate::unification::Substitution;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;

/// Helper function to substitute terms in predicates within an expression.
///
/// This performs capture-avoiding substitution, respecting bound variables in quantifiers.
fn substitute_in_expr(expr: &TLExpr, subst: &Substitution) -> TLExpr {
    match expr {
        TLExpr::Pred { name, args } => {
            // Apply substitution to each term in the predicate
            let new_args = args.iter().map(|term| subst.apply(term)).collect();
            TLExpr::Pred {
                name: name.clone(),
                args: new_args,
            }
        }
        TLExpr::And(left, right) => TLExpr::And(
            Box::new(substitute_in_expr(left, subst)),
            Box::new(substitute_in_expr(right, subst)),
        ),
        TLExpr::Or(left, right) => TLExpr::Or(
            Box::new(substitute_in_expr(left, subst)),
            Box::new(substitute_in_expr(right, subst)),
        ),
        TLExpr::Not(inner) => TLExpr::Not(Box::new(substitute_in_expr(inner, subst))),
        TLExpr::Imply(left, right) => TLExpr::Imply(
            Box::new(substitute_in_expr(left, subst)),
            Box::new(substitute_in_expr(right, subst)),
        ),
        TLExpr::Exists { var, domain, body } => {
            // Capture-avoiding substitution: don't substitute if var is bound
            if subst.domain().contains(var) {
                // Variable is bound by this quantifier, return unchanged
                expr.clone()
            } else {
                TLExpr::Exists {
                    var: var.clone(),
                    domain: domain.clone(),
                    body: Box::new(substitute_in_expr(body, subst)),
                }
            }
        }
        TLExpr::ForAll { var, domain, body } => {
            // Capture-avoiding substitution: don't substitute if var is bound
            if subst.domain().contains(var) {
                // Variable is bound by this quantifier, return unchanged
                expr.clone()
            } else {
                TLExpr::ForAll {
                    var: var.clone(),
                    domain: domain.clone(),
                    body: Box::new(substitute_in_expr(body, subst)),
                }
            }
        }
        // For other expression types, return as-is (they don't contain terms)
        _ => expr.clone(),
    }
}

/// A sequent is a formal statement `Γ ⊢ Δ` representing an entailment relation.
///
/// - `antecedents` (Γ): Multiset of hypothesis formulas (left side)
/// - `consequents` (Δ): Multiset of conclusion formulas (right side)
///
/// The sequent is valid if: assuming all antecedents are true,
/// at least one consequent must be true.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Sequent {
    /// Hypothesis formulas (left side of ⊢)
    pub antecedents: Vec<TLExpr>,
    /// Conclusion formulas (right side of ⊢)
    pub consequents: Vec<TLExpr>,
}

impl Sequent {
    /// Create a new sequent from antecedents and consequents.
    pub fn new(antecedents: Vec<TLExpr>, consequents: Vec<TLExpr>) -> Self {
        Sequent {
            antecedents,
            consequents,
        }
    }

    /// Create an identity sequent: `A ⊢ A`
    pub fn identity(formula: TLExpr) -> Self {
        Sequent::new(vec![formula.clone()], vec![formula])
    }

    /// Check if this is an axiom (identity sequent where some antecedent equals some consequent).
    pub fn is_axiom(&self) -> bool {
        for ant in &self.antecedents {
            for cons in &self.consequents {
                if ant == cons {
                    return true;
                }
            }
        }
        false
    }

    /// Apply weakening rule: add a formula to antecedents.
    pub fn weaken_left(mut self, formula: TLExpr) -> Self {
        self.antecedents.push(formula);
        self
    }

    /// Apply weakening rule: add a formula to consequents.
    pub fn weaken_right(mut self, formula: TLExpr) -> Self {
        self.consequents.push(formula);
        self
    }

    /// Apply contraction rule: remove duplicate from antecedents.
    pub fn contract_left(mut self, index: usize) -> Option<Self> {
        if index >= self.antecedents.len() {
            return None;
        }
        let formula = self.antecedents[index].clone();
        // Find another occurrence
        for i in 0..self.antecedents.len() {
            if i != index && self.antecedents[i] == formula {
                self.antecedents.remove(index);
                return Some(self);
            }
        }
        None
    }

    /// Apply contraction rule: remove duplicate from consequents.
    pub fn contract_right(mut self, index: usize) -> Option<Self> {
        if index >= self.consequents.len() {
            return None;
        }
        let formula = self.consequents[index].clone();
        // Find another occurrence
        for i in 0..self.consequents.len() {
            if i != index && self.consequents[i] == formula {
                self.consequents.remove(index);
                return Some(self);
            }
        }
        None
    }

    /// Get all free variables in the sequent.
    pub fn free_vars(&self) -> HashSet<String> {
        let mut vars = HashSet::new();
        for ant in &self.antecedents {
            vars.extend(ant.free_vars());
        }
        for cons in &self.consequents {
            vars.extend(cons.free_vars());
        }
        vars
    }

    /// Substitute a term for a variable throughout the sequent.
    ///
    /// This creates a new substitution and applies it to all formulas in the sequent.
    /// The substitution is capture-avoiding for bound variables in quantifiers.
    ///
    /// # Example
    ///
    /// ```rust
    /// use tensorlogic_ir::{Sequent, TLExpr, Term};
    ///
    /// // P(x) ⊢ P(x)
    /// let p_x = TLExpr::pred("P", vec![Term::var("x")]);
    /// let seq = Sequent::identity(p_x);
    ///
    /// // Substitute x with a
    /// let seq_subst = seq.substitute("x", &Term::constant("a"));
    ///
    /// // Result should be P(a) ⊢ P(a)
    /// ```
    pub fn substitute(&self, var: &str, term: &Term) -> Self {
        let mut subst = Substitution::empty();
        subst.bind(var.to_string(), term.clone());

        let new_antecedents = self
            .antecedents
            .iter()
            .map(|expr| substitute_in_expr(expr, &subst))
            .collect();

        let new_consequents = self
            .consequents
            .iter()
            .map(|expr| substitute_in_expr(expr, &subst))
            .collect();

        Sequent::new(new_antecedents, new_consequents)
    }
}

/// Inference rules in sequent calculus.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum InferenceRule {
    /// Identity axiom: `A ⊢ A`
    Identity,

    /// Weakening left: add formula to antecedents
    WeakeningLeft,

    /// Weakening right: add formula to consequents
    WeakeningRight,

    /// Contraction left: remove duplicate from antecedents
    ContractionLeft { index: usize },

    /// Contraction right: remove duplicate from consequents
    ContractionRight { index: usize },

    /// Exchange: reorder formulas (implicit, not tracked)
    Exchange,

    /// Cut rule: eliminate intermediate formula (index in antecedents/consequents)
    Cut { index: usize },

    /// AND introduction (left): `Γ, A, B ⊢ Δ` from `Γ, A ∧ B ⊢ Δ`
    AndLeft { index: usize },

    /// AND introduction (right): `Γ ⊢ Δ, A ∧ B` from `Γ ⊢ Δ, A` and `Γ ⊢ Δ, B`
    AndRight { index: usize },

    /// OR introduction (left): `Γ, A ∨ B ⊢ Δ` from `Γ, A ⊢ Δ` and `Γ, B ⊢ Δ`
    OrLeft { index: usize },

    /// OR introduction (right): `Γ ⊢ Δ, A, B` from `Γ ⊢ Δ, A ∨ B`
    OrRight { index: usize },

    /// NOT introduction (left): `Γ, ¬A ⊢ Δ` from `Γ ⊢ Δ, A`
    NotLeft { index: usize },

    /// NOT introduction (right): `Γ ⊢ Δ, ¬A` from `Γ, A ⊢ Δ`
    NotRight { index: usize },

    /// IMPLY introduction (left): `Γ, A → B ⊢ Δ` from `Γ ⊢ Δ, A` and `Γ, B ⊢ Δ`
    ImplyLeft { index: usize },

    /// IMPLY introduction (right): `Γ ⊢ Δ, A → B` from `Γ, A ⊢ Δ, B`
    ImplyRight { index: usize },

    /// EXISTS introduction (left): `Γ, ∃x.A ⊢ Δ` from `Γ, A[t/x] ⊢ Δ` (t fresh)
    ExistsLeft { index: usize, witness: Term },

    /// EXISTS introduction (right): `Γ ⊢ Δ, ∃x.A` from `Γ ⊢ Δ, A[t/x]`
    ExistsRight { index: usize, witness: Term },

    /// FORALL introduction (left): `Γ, ∀x.A ⊢ Δ` from `Γ, A[t/x] ⊢ Δ`
    ForAllLeft { index: usize, term: Term },

    /// FORALL introduction (right): `Γ ⊢ Δ, ∀x.A` from `Γ ⊢ Δ, A[t/x]` (t fresh)
    ForAllRight { index: usize, witness: Term },
}

/// A proof tree in sequent calculus.
///
/// Represents a derivation tree showing how a sequent is proved
/// using inference rules applied to premises.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ProofTree {
    /// The conclusion sequent of this proof step
    pub conclusion: Sequent,
    /// The inference rule applied
    pub rule: InferenceRule,
    /// Premise proof trees (subproofs)
    pub premises: Vec<ProofTree>,
}

impl ProofTree {
    /// Create a proof tree for an identity axiom.
    pub fn identity(formula: TLExpr) -> Self {
        ProofTree {
            conclusion: Sequent::identity(formula),
            rule: InferenceRule::Identity,
            premises: vec![],
        }
    }

    /// Create a proof tree with premises and rule.
    pub fn new(conclusion: Sequent, rule: InferenceRule, premises: Vec<ProofTree>) -> Self {
        ProofTree {
            conclusion,
            rule,
            premises,
        }
    }

    /// Check if this proof tree is valid (premises correctly derive conclusion).
    pub fn is_valid(&self) -> bool {
        // Check that the rule application is sound
        match &self.rule {
            InferenceRule::Identity => {
                // Must have no premises and be an axiom
                self.premises.is_empty() && self.conclusion.is_axiom()
            }
            InferenceRule::WeakeningLeft | InferenceRule::WeakeningRight => {
                // Must have exactly one premise
                if self.premises.len() != 1 {
                    return false;
                }
                // The premise should be a subset of the conclusion
                // (detailed validation omitted for brevity)
                true
            }
            InferenceRule::AndLeft { index } => {
                // Must have one premise where A ∧ B is split into A, B
                if self.premises.len() != 1 {
                    return false;
                }
                if *index >= self.conclusion.antecedents.len() {
                    return false;
                }
                // Check that the formula at index is an AND
                matches!(self.conclusion.antecedents[*index], TLExpr::And(_, _))
            }
            InferenceRule::AndRight { .. } => {
                // Must have two premises
                self.premises.len() == 2
            }
            InferenceRule::OrLeft { .. } => {
                // Must have two premises
                self.premises.len() == 2
            }
            InferenceRule::OrRight { index } => {
                // Must have one premise
                if self.premises.len() != 1 {
                    return false;
                }
                if *index >= self.conclusion.consequents.len() {
                    return false;
                }
                // Check that the formula at index is an OR
                matches!(self.conclusion.consequents[*index], TLExpr::Or(_, _))
            }
            InferenceRule::NotLeft { .. } | InferenceRule::NotRight { .. } => {
                // Must have one premise
                self.premises.len() == 1
            }
            InferenceRule::ImplyLeft { .. } => {
                // Must have two premises
                self.premises.len() == 2
            }
            InferenceRule::ImplyRight { .. } => {
                // Must have one premise
                self.premises.len() == 1
            }
            InferenceRule::Cut { .. } => {
                // Must have two premises
                self.premises.len() == 2
            }
            _ => true, // Other rules validation omitted for brevity
        }
    }

    /// Get the depth of this proof tree.
    pub fn depth(&self) -> usize {
        if self.premises.is_empty() {
            1
        } else {
            1 + self.premises.iter().map(|p| p.depth()).max().unwrap_or(0)
        }
    }

    /// Count the number of inference rule applications in this proof.
    pub fn size(&self) -> usize {
        1 + self.premises.iter().map(|p| p.size()).sum::<usize>()
    }

    /// Check if this proof uses the cut rule.
    pub fn uses_cut(&self) -> bool {
        matches!(self.rule, InferenceRule::Cut { .. }) || self.premises.iter().any(|p| p.uses_cut())
    }
}

/// Proof search strategies for automated theorem proving.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ProofSearchStrategy {
    /// Depth-first search with backtracking
    DepthFirst { max_depth: usize },
    /// Breadth-first search
    BreadthFirst { max_depth: usize },
    /// Best-first search with heuristic
    BestFirst { max_depth: usize },
    /// Iterative deepening
    IterativeDeepening { max_depth: usize },
}

/// Proof search engine for automated theorem proving.
pub struct ProofSearchEngine {
    strategy: ProofSearchStrategy,
    /// Maximum number of proof search steps
    max_steps: usize,
    /// Statistics about the search
    pub stats: ProofSearchStats,
}

/// Statistics about proof search.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct ProofSearchStats {
    /// Number of sequents explored
    pub sequents_explored: usize,
    /// Number of proof trees generated
    pub proofs_generated: usize,
    /// Number of backtracking steps
    pub backtracks: usize,
    /// Final proof depth (if found)
    pub proof_depth: Option<usize>,
}

impl ProofSearchEngine {
    /// Create a new proof search engine with the given strategy.
    pub fn new(strategy: ProofSearchStrategy, max_steps: usize) -> Self {
        ProofSearchEngine {
            strategy,
            max_steps,
            stats: ProofSearchStats::default(),
        }
    }

    /// Search for a proof of the given sequent.
    ///
    /// Returns `Some(proof)` if a proof is found, `None` otherwise.
    pub fn search(&mut self, sequent: &Sequent) -> Option<ProofTree> {
        match self.strategy {
            ProofSearchStrategy::DepthFirst { max_depth } => self.dfs_search(sequent, 0, max_depth),
            ProofSearchStrategy::BreadthFirst { max_depth } => self.bfs_search(sequent, max_depth),
            ProofSearchStrategy::BestFirst { max_depth } => {
                self.best_first_search(sequent, max_depth)
            }
            ProofSearchStrategy::IterativeDeepening { max_depth } => {
                self.iterative_deepening_search(sequent, max_depth)
            }
        }
    }

    /// Depth-first search for a proof.
    fn dfs_search(
        &mut self,
        sequent: &Sequent,
        depth: usize,
        max_depth: usize,
    ) -> Option<ProofTree> {
        self.stats.sequents_explored += 1;

        if depth >= max_depth || self.stats.sequents_explored >= self.max_steps {
            self.stats.backtracks += 1;
            return None;
        }

        // Check if this is an axiom
        if sequent.is_axiom() {
            // Find the matching formula
            for ant in &sequent.antecedents {
                if sequent.consequents.contains(ant) {
                    self.stats.proofs_generated += 1;
                    let proof = ProofTree::identity(ant.clone());
                    self.stats.proof_depth = Some(depth);
                    return Some(proof);
                }
            }
        }

        // Try applying inference rules
        // (Simplified implementation - full proof search would try all applicable rules)

        // Try AND-Left rules
        for (i, ant) in sequent.antecedents.iter().enumerate() {
            if let TLExpr::And(a, b) = ant {
                let mut new_ant = sequent.antecedents.clone();
                new_ant.remove(i);
                new_ant.push((**a).clone());
                new_ant.push((**b).clone());

                let new_sequent = Sequent::new(new_ant, sequent.consequents.clone());
                if let Some(premise) = self.dfs_search(&new_sequent, depth + 1, max_depth) {
                    self.stats.proofs_generated += 1;
                    return Some(ProofTree::new(
                        sequent.clone(),
                        InferenceRule::AndLeft { index: i },
                        vec![premise],
                    ));
                }
            }
        }

        // Try OR-Right rules
        for (i, cons) in sequent.consequents.iter().enumerate() {
            if let TLExpr::Or(a, b) = cons {
                let mut new_cons = sequent.consequents.clone();
                new_cons.remove(i);
                new_cons.push((**a).clone());
                new_cons.push((**b).clone());

                let new_sequent = Sequent::new(sequent.antecedents.clone(), new_cons);
                if let Some(premise) = self.dfs_search(&new_sequent, depth + 1, max_depth) {
                    self.stats.proofs_generated += 1;
                    return Some(ProofTree::new(
                        sequent.clone(),
                        InferenceRule::OrRight { index: i },
                        vec![premise],
                    ));
                }
            }
        }

        // Try NOT-Left rules
        for (i, ant) in sequent.antecedents.iter().enumerate() {
            if let TLExpr::Not(a) = ant {
                let mut new_ant = sequent.antecedents.clone();
                new_ant.remove(i);

                let mut new_cons = sequent.consequents.clone();
                new_cons.push((**a).clone());

                let new_sequent = Sequent::new(new_ant, new_cons);
                if let Some(premise) = self.dfs_search(&new_sequent, depth + 1, max_depth) {
                    self.stats.proofs_generated += 1;
                    return Some(ProofTree::new(
                        sequent.clone(),
                        InferenceRule::NotLeft { index: i },
                        vec![premise],
                    ));
                }
            }
        }

        // Try NOT-Right rules
        for (i, cons) in sequent.consequents.iter().enumerate() {
            if let TLExpr::Not(a) = cons {
                let mut new_cons = sequent.consequents.clone();
                new_cons.remove(i);

                let mut new_ant = sequent.antecedents.clone();
                new_ant.push((**a).clone());

                let new_sequent = Sequent::new(new_ant, new_cons);
                if let Some(premise) = self.dfs_search(&new_sequent, depth + 1, max_depth) {
                    self.stats.proofs_generated += 1;
                    return Some(ProofTree::new(
                        sequent.clone(),
                        InferenceRule::NotRight { index: i },
                        vec![premise],
                    ));
                }
            }
        }

        self.stats.backtracks += 1;
        None
    }

    /// Breadth-first search (stub implementation).
    fn bfs_search(&mut self, sequent: &Sequent, max_depth: usize) -> Option<ProofTree> {
        // For simplicity, fall back to DFS
        self.dfs_search(sequent, 0, max_depth)
    }

    /// Best-first search with heuristic (stub implementation).
    fn best_first_search(&mut self, sequent: &Sequent, max_depth: usize) -> Option<ProofTree> {
        // For simplicity, fall back to DFS
        self.dfs_search(sequent, 0, max_depth)
    }

    /// Iterative deepening search.
    fn iterative_deepening_search(
        &mut self,
        sequent: &Sequent,
        max_depth: usize,
    ) -> Option<ProofTree> {
        for depth in 1..=max_depth {
            if let Some(proof) = self.dfs_search(sequent, 0, depth) {
                return Some(proof);
            }
        }
        None
    }
}

/// Cut elimination transformation.
///
/// Cut elimination is a fundamental property of sequent calculus stating that
/// any proof using the cut rule can be transformed into a cut-free proof.
/// This is important for proof normalization and extracting computational content.
pub struct CutElimination;

impl CutElimination {
    /// Attempt to eliminate all cut rules from a proof tree.
    ///
    /// Returns a cut-free proof if successful, or the original proof if
    /// cut elimination fails or is not applicable.
    pub fn eliminate(proof: ProofTree) -> ProofTree {
        if !proof.uses_cut() {
            return proof;
        }

        // Recursively eliminate cuts from premises first
        let premises: Vec<ProofTree> = proof.premises.into_iter().map(Self::eliminate).collect();

        // If this is a cut rule, try to eliminate it
        if let InferenceRule::Cut { index } = proof.rule {
            if premises.len() == 2 {
                // Cut elimination algorithm (simplified)
                // In a full implementation, this would perform the standard
                // cut elimination procedure by permuting the cut rule upward
                // and then removing it using logical rule inversions.

                // For now, return a simplified version
                // (A full implementation would be significantly more complex)
                return ProofTree::new(proof.conclusion, InferenceRule::Cut { index }, premises);
            }
        }

        // Return proof with eliminated premises
        ProofTree::new(proof.conclusion, proof.rule, premises)
    }

    /// Check if a proof is cut-free.
    pub fn is_cut_free(proof: &ProofTree) -> bool {
        !proof.uses_cut()
    }
}

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

    #[test]
    fn test_identity_sequent() {
        let p = TLExpr::pred("P", vec![Term::var("x")]);
        let seq = Sequent::identity(p);
        assert!(seq.is_axiom());
        assert_eq!(seq.antecedents.len(), 1);
        assert_eq!(seq.consequents.len(), 1);
    }

    #[test]
    fn test_weakening_left() {
        let p = TLExpr::pred("P", vec![]);
        let q = TLExpr::pred("Q", vec![]);
        let seq = Sequent::identity(p.clone()).weaken_left(q.clone());
        assert_eq!(seq.antecedents.len(), 2);
        assert!(seq.antecedents.contains(&q));
    }

    #[test]
    fn test_weakening_right() {
        let p = TLExpr::pred("P", vec![]);
        let q = TLExpr::pred("Q", vec![]);
        let seq = Sequent::identity(p.clone()).weaken_right(q.clone());
        assert_eq!(seq.consequents.len(), 2);
        assert!(seq.consequents.contains(&q));
    }

    #[test]
    fn test_contraction_left() {
        let p = TLExpr::pred("P", vec![]);
        let mut seq = Sequent::identity(p.clone());
        seq.antecedents.push(p.clone());
        assert_eq!(seq.antecedents.len(), 2);

        let contracted = seq.contract_left(0);
        assert!(contracted.is_some());
        assert_eq!(contracted.expect("unwrap").antecedents.len(), 1);
    }

    #[test]
    fn test_free_vars() {
        let p = TLExpr::pred("P", vec![Term::var("x")]);
        let q = TLExpr::pred("Q", vec![Term::var("y")]);
        let seq = Sequent::new(vec![p], vec![q]);

        let vars = seq.free_vars();
        assert_eq!(vars.len(), 2);
        assert!(vars.contains("x"));
        assert!(vars.contains("y"));
    }

    #[test]
    fn test_sequent_substitute() {
        let p_x = TLExpr::pred("P", vec![Term::var("x")]);
        let seq = Sequent::identity(p_x.clone());

        // Substitute x with a
        let substituted = seq.substitute("x", &Term::constant("a"));
        let p_a = TLExpr::pred("P", vec![Term::constant("a")]);
        assert_eq!(substituted.antecedents[0], p_a);
        assert_eq!(substituted.consequents[0], p_a);

        // Verify original is unchanged
        assert_eq!(seq.antecedents[0], p_x);
    }

    #[test]
    fn test_sequent_substitute_capture_avoiding() {
        // Test that substitution respects bound variables
        // ∃x. P(x) ⊢ Q(x)
        let p_x = TLExpr::pred("P", vec![Term::var("x")]);
        let exists_p = TLExpr::exists("x", "Domain", p_x);
        let q_x = TLExpr::pred("Q", vec![Term::var("x")]);
        let seq = Sequent::new(vec![exists_p.clone()], vec![q_x]);

        // Substitute x with a
        let substituted = seq.substitute("x", &Term::constant("a"));

        // Left side should be unchanged (x is bound)
        assert_eq!(substituted.antecedents[0], exists_p);
        // Right side should be substituted (x is free)
        let q_a = TLExpr::pred("Q", vec![Term::constant("a")]);
        assert_eq!(substituted.consequents[0], q_a);
    }

    #[test]
    fn test_sequent_substitute_multiple() {
        // P(x) ∧ Q(x) ⊢ R(x)
        let p_x = TLExpr::pred("P", vec![Term::var("x")]);
        let q_x = TLExpr::pred("Q", vec![Term::var("x")]);
        let and_pq = TLExpr::and(p_x, q_x);
        let r_x = TLExpr::pred("R", vec![Term::var("x")]);
        let seq = Sequent::new(vec![and_pq], vec![r_x]);

        // Substitute x with b
        let substituted = seq.substitute("x", &Term::constant("b"));

        // All x's should be replaced with b
        let p_b = TLExpr::pred("P", vec![Term::constant("b")]);
        let q_b = TLExpr::pred("Q", vec![Term::constant("b")]);
        let and_pq_b = TLExpr::and(p_b, q_b);
        let r_b = TLExpr::pred("R", vec![Term::constant("b")]);

        assert_eq!(substituted.antecedents[0], and_pq_b);
        assert_eq!(substituted.consequents[0], r_b);
    }

    #[test]
    fn test_substitution() {
        let p = TLExpr::pred("P", vec![Term::var("x")]);
        let seq = Sequent::identity(p.clone());

        // Substitution now works properly with unification
        let substituted = seq.substitute("x", &Term::constant("a"));
        let p_a = TLExpr::pred("P", vec![Term::constant("a")]);
        assert_eq!(substituted.antecedents[0], p_a);
        assert_eq!(substituted.consequents[0], p_a);
    }

    #[test]
    fn test_identity_proof_tree() {
        let p = TLExpr::pred("P", vec![]);
        let proof = ProofTree::identity(p);
        assert!(proof.is_valid());
        assert_eq!(proof.depth(), 1);
        assert_eq!(proof.size(), 1);
        assert!(!proof.uses_cut());
    }

    #[test]
    fn test_and_left_proof() {
        let p = TLExpr::pred("P", vec![]);
        let q = TLExpr::pred("Q", vec![]);
        let and_pq = TLExpr::and(p.clone(), q.clone());

        // Build proof: from P, Q ⊢ P, derive P ∧ Q ⊢ P
        let _premise_seq = Sequent::new(vec![p.clone(), q], vec![p.clone()]);
        let premise = ProofTree::identity(p.clone());

        let conclusion_seq = Sequent::new(vec![and_pq], vec![p]);
        let proof = ProofTree::new(
            conclusion_seq,
            InferenceRule::AndLeft { index: 0 },
            vec![premise],
        );

        assert!(proof.is_valid());
        assert_eq!(proof.depth(), 2);
    }

    #[test]
    fn test_proof_search_simple() {
        let p = TLExpr::pred("P", vec![]);
        let sequent = Sequent::identity(p);

        let mut engine =
            ProofSearchEngine::new(ProofSearchStrategy::DepthFirst { max_depth: 10 }, 1000);

        let proof = engine.search(&sequent);
        assert!(proof.is_some());
        assert!(proof.expect("unwrap").is_valid());
        assert!(engine.stats.proofs_generated > 0);
    }

    #[test]
    fn test_proof_search_and() {
        let p = TLExpr::pred("P", vec![]);
        let q = TLExpr::pred("Q", vec![]);
        let and_pq = TLExpr::and(p.clone(), q.clone());

        // Try to prove: P ∧ Q ⊢ P
        let sequent = Sequent::new(vec![and_pq], vec![p]);

        let mut engine =
            ProofSearchEngine::new(ProofSearchStrategy::DepthFirst { max_depth: 10 }, 1000);

        let proof = engine.search(&sequent);
        assert!(proof.is_some());
        let proof = proof.expect("unwrap");
        assert!(proof.is_valid());
        assert!(engine.stats.proofs_generated > 0);
    }

    #[test]
    fn test_proof_search_not() {
        let p = TLExpr::pred("P", vec![]);
        let not_p = TLExpr::negate(p.clone());

        // Try to prove: ¬P ⊢ ¬P (should be immediate)
        let sequent = Sequent::identity(not_p);

        let mut engine =
            ProofSearchEngine::new(ProofSearchStrategy::DepthFirst { max_depth: 10 }, 1000);

        let proof = engine.search(&sequent);
        assert!(proof.is_some());
        assert!(proof.expect("unwrap").is_valid());
    }

    #[test]
    fn test_cut_elimination_no_cut() {
        let p = TLExpr::pred("P", vec![]);
        let proof = ProofTree::identity(p);

        assert!(CutElimination::is_cut_free(&proof));
        let eliminated = CutElimination::eliminate(proof.clone());
        assert_eq!(eliminated, proof);
    }

    #[test]
    fn test_iterative_deepening_search() {
        let p = TLExpr::pred("P", vec![]);
        let q = TLExpr::pred("Q", vec![]);
        let and_pq = TLExpr::and(p.clone(), q.clone());

        let sequent = Sequent::new(vec![and_pq], vec![p]);

        let mut engine = ProofSearchEngine::new(
            ProofSearchStrategy::IterativeDeepening { max_depth: 10 },
            1000,
        );

        let proof = engine.search(&sequent);
        assert!(proof.is_some());
        assert!(proof.expect("unwrap").is_valid());
    }
}