semitone 0.0.21

SeMiTONE is a Satisfiability Modulo TheOries NEtwork, allowing the creation of variables and constraints in different underlying theories.
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
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
#![doc = include_str!("../README.md")]

//! # API overview
//! [`SeMiTONE`] owns all solver state. Create expressions with the types in
//! [`ast`], assert a [`ast::BoolExpr`], then call [`SeMiTONE::propagate`] after
//! each assertion or decision. Applications that need a complete SAT/SMT search
//! loop can use the decision, trail, and clause APIs exposed by [`SeMiTONE`].

pub mod ast;
mod dl_theory;
mod enum_theory;
mod euf_theory;
mod lra_theory;
#[cfg(feature = "parser")]
pub mod parser;
mod proxy;
pub mod rational;
mod sat_solver;
#[cfg(feature = "solver")]
pub mod solver;

use crate::{
    ast::{ArithExpr, BoolExpr, BoolVar, DlVar, EnumExpr, EnumVar, EufExpr, EufNode, Expr, FuncId, IntVar, RealVar},
    dl_theory::DlTheory,
    enum_theory::EnumTheory,
    euf_theory::EufTheory,
    lra_theory::{LraTheory, SparseRow, tighten_int_bound},
    proxy::{ProxyRegistry, TheoryConstraint},
    rational::{InfRational, Rational},
    sat_solver::SatSolver,
};
use rug::Assign;
pub use sat_solver::Lit;
use tracing::trace;

/// Main solver entry point for propositional, linear arithmetic, and enum constraints.
///
/// The solver combines a SAT core with theory propagation for linear rational
/// arithmetic and finite-domain enum reasoning.
pub struct SeMiTONE {
    registry: ProxyRegistry,
    sat_solver: SatSolver,
    lra_theory: LraTheory,
    enum_theory: EnumTheory,
    dl_theory: DlTheory,
    euf_theory: EufTheory,
    notified_len: usize,
    user_scopes: Vec<(usize, usize)>,
}

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

impl SeMiTONE {
    /// Creates a new empty solver instance.
    pub fn new() -> Self {
        Self {
            registry: ProxyRegistry::new(),
            sat_solver: SatSolver::new(),
            lra_theory: LraTheory::new(),
            enum_theory: EnumTheory::new(),
            dl_theory: DlTheory::new(),
            euf_theory: EufTheory::new(),
            notified_len: 0,
            user_scopes: Vec::new(),
        }
    }

    /// Allocates a new SAT literal.
    pub fn new_lit(&mut self) -> Lit {
        Lit::new(self.sat_solver.mk_var(), false)
    }

    /// Allocates a new Boolean variable.
    pub fn new_bool(&mut self) -> BoolExpr {
        BoolExpr::Var(BoolVar(self.sat_solver.mk_var()))
    }

    /// Allocates a new integer variable.
    pub fn new_int(&mut self) -> ArithExpr {
        ArithExpr::IntVar(IntVar(self.lra_theory.mk_int()))
    }

    /// Allocates a new real variable.
    pub fn new_real(&mut self) -> ArithExpr {
        ArithExpr::RealVar(RealVar(self.lra_theory.mk_real()))
    }

    /// Allocates a new enum variable with the given finite domain.
    pub fn new_enum(&mut self, domain: impl IntoIterator<Item = i32>) -> EnumExpr {
        EnumExpr::Var(EnumVar(self.enum_theory.mk_var(domain.into_iter().collect())))
    }

    /// Allocates a new difference logic variable.
    pub fn new_dl_var(&mut self) -> DlVar {
        DlVar(self.dl_theory.new_var())
    }

    /// Allocates a new EUF variable.
    pub fn new_euf_var(&mut self) -> EufExpr {
        EufExpr::Var(EufNode(self.euf_theory.new_var()))
    }

    /// Allocates a new uninterpreted function symbol.
    pub fn new_euf_func(&mut self) -> FuncId {
        FuncId(self.euf_theory.new_func_id())
    }

    /// Allocates a new EUF function application with the given function ID and arguments.
    pub fn new_euf_app(&mut self, func_id: FuncId, args: Vec<Expr>) -> EufExpr {
        let mut internal_args = Vec::with_capacity(args.len());
        for arg in &args {
            if let Expr::Euf(euf_arg) = arg {
                let node_id = match euf_arg {
                    EufExpr::Var(node) => node.0,
                    EufExpr::App(node, _, _) => node.0,
                };
                internal_args.push(node_id);
            } else {
                panic!("EUF application arguments must be EUF expressions, but got: {:?}", arg);
            }
        }

        let node_id = self.euf_theory.new_app(func_id.0, internal_args);
        EufExpr::App(EufNode(node_id), func_id, args)
    }

    /// Returns the current number of SAT variables allocated in the solver core.
    ///
    /// This includes user-visible Boolean variables and internal proxy variables
    /// introduced while encoding compound constraints.
    pub fn num_vars(&self) -> usize {
        self.sat_solver.num_vars()
    }

    /// Adds a clause directly to the SAT core.
    ///
    /// Returns `Ok(())` when the clause is accepted, or `Err(conflict_clause)` if
    /// the clause is immediately contradictory at the current root context.
    ///
    /// This is useful for integrating external search/learning loops that produce
    /// learned no-goods.
    pub fn add_clause(&mut self, clause: impl IntoIterator<Item = Lit>) -> Result<(), Vec<Lit>> {
        self.sat_solver.add_clause(clause)
    }

    /// Adds a Boolean constraint to the current solver context.
    ///
    /// Returns `true` if the assertion was successfully added, or `false` if it
    /// led to an immediate, trivial conflict while translating/asserting.
    ///
    /// Note that `true` does not imply global feasibility: call [`SeMiTONE::propagate`]
    /// to detect conflicts that emerge after propagation through SAT/theory state.
    pub fn assert(&mut self, expr: impl AsRef<BoolExpr>) -> bool {
        self.assert_internal(expr.as_ref(), true)
    }

    fn assert_internal(&mut self, expr: &BoolExpr, polarity: bool) -> bool {
        match (expr, polarity) {
            (BoolExpr::Not(inner), _) => self.assert_internal(inner, !polarity),
            (BoolExpr::And(args), true) | (BoolExpr::Or(args), false) => {
                for arg in args {
                    if !self.assert_internal(arg, polarity) {
                        return false;
                    }
                }
                true
            }
            (BoolExpr::Or(args), true) | (BoolExpr::And(args), false) => {
                let mut clause = Vec::with_capacity(args.len());
                for arg in args {
                    let mut lit = self.encode_bool(arg);
                    if !polarity {
                        lit = !lit;
                    }
                    clause.push(lit);
                }
                self.sat_solver.add_clause(clause).is_ok()
            }
            (BoolExpr::Lt(e1, e2), _) | (BoolExpr::Le(e1, e2), _) | (BoolExpr::Ge(e1, e2), _) | (BoolExpr::Gt(e1, e2), _) => {
                let (vars, const_term) = self.diff(e1, e2);
                if vars.is_empty() {
                    let is_sat = match expr {
                        BoolExpr::Lt(_, _) => const_term.is_negative(),
                        BoolExpr::Le(_, _) => const_term.is_negative() || const_term.is_zero(),
                        BoolExpr::Ge(_, _) => !const_term.is_negative(),
                        BoolExpr::Gt(_, _) => const_term.is_positive(),
                        _ => unreachable!(),
                    };
                    return if polarity { is_sat } else { !is_sat };
                }

                let (is_upper_bound, is_strict) = match (expr, polarity) {
                    (BoolExpr::Lt(_, _), true) => (true, true),
                    (BoolExpr::Lt(_, _), false) => (false, false),
                    (BoolExpr::Le(_, _), true) => (true, false),
                    (BoolExpr::Le(_, _), false) => (false, true),
                    (BoolExpr::Ge(_, _), true) => (false, false),
                    (BoolExpr::Ge(_, _), false) => (true, true),
                    (BoolExpr::Gt(_, _), true) => (false, true),
                    (BoolExpr::Gt(_, _), false) => (true, false),
                    _ => unreachable!(),
                };

                if vars.len() == 1 {
                    let (var, coeff) = vars.iter().next().unwrap();
                    let is_int = self.lra_theory.is_int_var(*var);
                    let is_upper_for_var = is_upper_bound == coeff.is_positive();

                    let bound = if is_int {
                        let val = -const_term / coeff.clone();
                        let tight_val = tighten_int_bound(val, is_upper_for_var, is_strict);
                        InfRational::new(Rational::Finite(tight_val), rug::Rational::from(0))
                    } else {
                        let eps_shift = if is_strict { if is_upper_bound { rug::Rational::from(-1) } else { rug::Rational::from(1) } } else { rug::Rational::from(0) };
                        InfRational::new(Rational::Finite(-const_term / coeff.clone()), eps_shift / coeff)
                    };

                    if is_upper_for_var { self.lra_theory.set_ub(None, *var, bound).is_ok() } else { self.lra_theory.set_lb(None, *var, bound).is_ok() }
                } else {
                    let slack = self.lra_theory.get_or_create_slack(vars);
                    let is_int = self.lra_theory.is_int_var(slack);

                    let bound = if is_int {
                        let tight_val = tighten_int_bound(-const_term, is_upper_bound, is_strict);
                        InfRational::new(Rational::Finite(tight_val), rug::Rational::from(0))
                    } else {
                        let eps_shift = if is_strict { if is_upper_bound { rug::Rational::from(-1) } else { rug::Rational::from(1) } } else { rug::Rational::from(0) };
                        InfRational::new(Rational::Finite(-const_term), eps_shift)
                    };

                    if is_upper_bound { self.lra_theory.set_ub(None, slack, bound).is_ok() } else { self.lra_theory.set_lb(None, slack, bound).is_ok() }
                }
            }
            (BoolExpr::Eq(e1, e2), _) => {
                if let (Expr::Arith(a1), Expr::Arith(a2)) = (&**e1, &**e2) {
                    let (vars, const_term) = self.diff(a1, a2);

                    if vars.is_empty() {
                        let is_sat = const_term.is_zero();
                        return if polarity { is_sat } else { !is_sat };
                    }

                    if polarity {
                        let bound = InfRational::new(Rational::Finite(-const_term), rug::Rational::from(0));

                        if vars.len() == 1 {
                            let (var, coeff) = vars.iter().next().unwrap();
                            let final_bound = bound / coeff.clone();
                            self.lra_theory.set_lb(None, *var, final_bound.clone()).is_ok() && self.lra_theory.set_ub(None, *var, final_bound).is_ok()
                        } else {
                            let slack = self.lra_theory.get_or_create_slack(vars);
                            self.lra_theory.set_lb(None, slack, bound.clone()).is_ok() && self.lra_theory.set_ub(None, slack, bound).is_ok()
                        }
                    } else {
                        let lt_lit = self.mk_le(a1, a2, true);
                        let gt_lit = self.mk_ge(a1, a2, true);
                        self.sat_solver.add_clause([lt_lit, gt_lit]).is_ok()
                    }
                } else {
                    let mut lit = self.encode_eq(e1, e2);
                    if !polarity {
                        lit = !lit;
                    }
                    self.sat_solver.add_clause([lit]).is_ok()
                }
            }
            _ => {
                let mut lit = self.encode_bool(expr);
                if !polarity {
                    lit = !lit;
                }
                self.sat_solver.add_clause([lit]).is_ok()
            }
        }
    }

    /// Encodes a Boolean expression and returns its equivalent SAT literal.
    ///
    /// The returned literal may refer to an internal proxy variable. Encoding
    /// a compound expression can therefore add auxiliary variables and
    /// clauses to the solver. The literal preserves the expression's polarity
    /// and can be inspected with [`SeMiTONE::get_lit_val`] or used with
    /// [`SeMiTONE::decide`]. This method does not assert the expression.
    pub fn track_expr(&mut self, expr: impl AsRef<BoolExpr>) -> Lit {
        self.encode_bool(expr.as_ref())
    }

    fn encode_bool(&mut self, expr: &BoolExpr) -> Lit {
        match expr {
            BoolExpr::True => Lit::TRUE,
            BoolExpr::False => Lit::FALSE,
            BoolExpr::Var(v) => Lit::new(v.0, false),
            BoolExpr::Not(inner) => match inner.as_ref() {
                BoolExpr::Lt(a1, a2) => self.mk_ge(a1, a2, false),
                BoolExpr::Le(a1, a2) => self.mk_ge(a1, a2, true),
                BoolExpr::Ge(a1, a2) => self.mk_le(a1, a2, true),
                BoolExpr::Gt(a1, a2) => self.mk_le(a1, a2, false),
                BoolExpr::DlLt(f, t, b) => self.mk_dl_ge(f.0, t.0, b.clone(), false), // !(x < y) => x >= y
                BoolExpr::DlLe(f, t, b) => self.mk_dl_ge(f.0, t.0, b.clone(), true),  // !(x <= y) => x > y
                BoolExpr::DlGe(f, t, b) => self.mk_dl_le(f.0, t.0, b.clone(), true),  // !(x >= y) => x < y
                BoolExpr::DlGt(f, t, b) => self.mk_dl_le(f.0, t.0, b.clone(), false), // !(x > y) => x <= y
                _ => !self.encode_bool(inner),
            },
            BoolExpr::And(terms) => {
                let mut lits = Vec::with_capacity(terms.len());
                for term in terms {
                    lits.push(self.encode_bool(term));
                }

                let proxy_var = self.sat_solver.mk_var();
                let proxy_lit = Lit::new(proxy_var, true);

                for &lit in &lits {
                    self.sat_solver.add_clause([!proxy_lit, lit]).expect("Failed to add clause");
                }

                let mut big_clause: Vec<Lit> = lits.into_iter().map(|l| !l).collect();
                big_clause.push(proxy_lit);
                self.sat_solver.add_clause(big_clause).expect("Failed to add clause");

                proxy_lit
            }
            BoolExpr::Or(terms) => {
                let mut lits = Vec::with_capacity(terms.len());
                for term in terms {
                    lits.push(self.encode_bool(term));
                }

                let proxy_var = self.sat_solver.mk_var();
                let proxy_lit = Lit::new(proxy_var, true);

                for &lit in &lits {
                    self.sat_solver.add_clause([!lit, proxy_lit]).expect("Failed to add clause");
                }

                let mut big_clause = lits;
                big_clause.push(!proxy_lit);
                self.sat_solver.add_clause(big_clause).expect("Failed to add clause");

                proxy_lit
            }
            BoolExpr::Lt(e1, e2) => self.mk_le(e1, e2, true),
            BoolExpr::Le(e1, e2) => self.mk_le(e1, e2, false),
            BoolExpr::Ge(e1, e2) => self.mk_ge(e1, e2, false),
            BoolExpr::Gt(e1, e2) => self.mk_ge(e1, e2, true),
            BoolExpr::Eq(e1, e2) => self.encode_eq(e1, e2),
            BoolExpr::DlLt(from, to, b) => self.mk_dl_le(from.0, to.0, b.clone(), true),
            BoolExpr::DlLe(from, to, b) => self.mk_dl_le(from.0, to.0, b.clone(), false),
            BoolExpr::DlGt(from, to, b) => self.mk_dl_ge(from.0, to.0, b.clone(), true),
            BoolExpr::DlGe(from, to, b) => self.mk_dl_ge(from.0, to.0, b.clone(), false),
            BoolExpr::DlEq(from, to, b) => self.mk_dl_eq(from.0, to.0, b.clone()),
        }
    }

    fn encode_eq(&mut self, expr1: &Expr, expr2: &Expr) -> Lit {
        match (expr1, expr2) {
            (Expr::Arith(a1), Expr::Arith(a2)) => self.mk_arith_eq(a1, a2),
            (Expr::Bool(b1), Expr::Bool(b2)) => {
                let l1 = self.encode_bool(b1);
                let l2 = self.encode_bool(b2);
                let proxy_var = self.sat_solver.mk_var();
                let p = Lit::new(proxy_var, true);

                self.sat_solver.add_clause([!p, l1, !l2]).expect("Failed to add clause");
                self.sat_solver.add_clause([!p, !l1, l2]).expect("Failed to add clause");
                self.sat_solver.add_clause([p, l1, l2]).expect("Failed to add clause");
                self.sat_solver.add_clause([p, !l1, !l2]).expect("Failed to add clause");

                p
            }
            (Expr::Enum(e1), Expr::Enum(e2)) => self.mk_enum_eq(e1, e2),
            (Expr::Euf(u1), Expr::Euf(u2)) => {
                let id1 = match u1 {
                    crate::ast::EufExpr::Var(n) => n.0,
                    crate::ast::EufExpr::App(n, _, _) => n.0,
                };
                let id2 = match u2 {
                    crate::ast::EufExpr::Var(n) => n.0,
                    crate::ast::EufExpr::App(n, _, _) => n.0,
                };

                if id1 == id2 {
                    return Lit::TRUE;
                }

                let (min_id, max_id) = if id1 < id2 { (id1, id2) } else { (id2, id1) };

                self.get_or_create_proxy(TheoryConstraint::EufEq(min_id, max_id))
            }
            _ => panic!("Type mismatch in Eq: cannot compare different domains.\nLeft: {:?}\nRight: {:?}", expr1, expr2),
        }
    }

    fn mk_enum_eq(&mut self, e1: &EnumExpr, e2: &EnumExpr) -> Lit {
        match (e1, e2) {
            (EnumExpr::Const(c1), EnumExpr::Const(c2)) => {
                if c1 == c2 {
                    Lit::TRUE
                } else {
                    Lit::FALSE
                }
            }
            (EnumExpr::Var(v), EnumExpr::Const(c)) | (EnumExpr::Const(c), EnumExpr::Var(v)) => self.get_or_create_proxy(TheoryConstraint::EnumEq(v.0, *c)),
            (EnumExpr::Var(v1), EnumExpr::Var(v2)) => {
                if v1 == v2 {
                    return Lit::TRUE;
                }

                let domain1 = self.enum_theory.initial_domains[v1.0].clone();
                let domain2 = self.enum_theory.initial_domains[v2.0].clone();
                let common: Vec<i32> = domain1.intersection(&domain2).copied().collect();

                if common.is_empty() {
                    return Lit::FALSE;
                }

                let mut lits = Vec::with_capacity(common.len());
                for val in common {
                    let p1 = self.get_or_create_proxy(TheoryConstraint::EnumEq(v1.0, val));
                    let p2 = self.get_or_create_proxy(TheoryConstraint::EnumEq(v2.0, val));

                    let and_proxy_var = self.sat_solver.mk_var();
                    let and_proxy = Lit::new(and_proxy_var, false);

                    self.sat_solver.add_clause([!and_proxy, p1]).unwrap();
                    self.sat_solver.add_clause([!and_proxy, p2]).unwrap();
                    self.sat_solver.add_clause([!p1, !p2, and_proxy]).unwrap();

                    lits.push(and_proxy);
                }

                let or_proxy_var = self.sat_solver.mk_var();
                let or_proxy = Lit::new(or_proxy_var, false);

                for &lit in &lits {
                    self.sat_solver.add_clause([!lit, or_proxy]).unwrap();
                }

                let mut big_clause = lits;
                big_clause.push(!or_proxy);
                self.sat_solver.add_clause(big_clause).unwrap();

                or_proxy
            }
        }
    }

    fn mk_le(&mut self, e1: &ArithExpr, e2: &ArithExpr, strict: bool) -> Lit {
        let (vars, const_term) = self.diff(e1, e2);

        match vars.len() {
            0 => {
                if if strict { const_term.is_negative() } else { const_term.is_negative() || const_term.is_zero() } {
                    Lit::TRUE
                } else {
                    Lit::FALSE
                }
            }
            1 => {
                let (var, coeff) = vars.iter().next().unwrap();
                let is_int = self.lra_theory.is_int_var(*var);
                let is_upper_bound = coeff.is_positive();

                let bound = if is_int {
                    let val = -const_term / coeff.clone();
                    let tight_val = tighten_int_bound(val, is_upper_bound, strict);
                    InfRational::new(Rational::Finite(tight_val), rug::Rational::from(0))
                } else {
                    let eps_shift = if strict { rug::Rational::from(-1) } else { rug::Rational::from(0) };
                    InfRational::new(Rational::Finite(-const_term / coeff.clone()), eps_shift / coeff)
                };

                if self.sat_solver.decision_level() == 0 {
                    if is_upper_bound {
                        if self.lra_theory.ub(*var) <= &bound {
                            return Lit::TRUE;
                        }
                        if self.lra_theory.lb(*var) > &bound {
                            return Lit::FALSE;
                        }
                    } else {
                        if self.lra_theory.lb(*var) >= &bound {
                            return Lit::TRUE;
                        }
                        if self.lra_theory.ub(*var) < &bound {
                            return Lit::FALSE;
                        }
                    }
                }

                let bound = if is_upper_bound { TheoryConstraint::LraUb(*var, bound) } else { TheoryConstraint::LraLb(*var, bound) };
                self.get_or_create_proxy(bound)
            }
            _ => {
                let slack = self.lra_theory.get_or_create_slack(vars);
                let is_int = self.lra_theory.is_int_var(slack);

                let bound = if is_int {
                    let tight_val = tighten_int_bound(-const_term, true, strict);
                    InfRational::new(Rational::Finite(tight_val), rug::Rational::from(0))
                } else {
                    let eps_shift = if strict { rug::Rational::from(-1) } else { rug::Rational::from(0) };
                    InfRational::new(Rational::Finite(-const_term), eps_shift)
                };

                if self.sat_solver.decision_level() == 0 {
                    if self.lra_theory.ub(slack) <= &bound {
                        return Lit::TRUE;
                    }
                    if self.lra_theory.lb(slack) > &bound {
                        return Lit::FALSE;
                    }
                }

                let bound = TheoryConstraint::LraUb(slack, bound);
                self.get_or_create_proxy(bound)
            }
        }
    }

    fn mk_arith_eq(&mut self, e1: &ArithExpr, e2: &ArithExpr) -> Lit {
        if e1 == e2 {
            return Lit::TRUE;
        }

        let le_lit = self.mk_le(e1, e2, false);
        let ge_lit = self.mk_ge(e1, e2, false);
        if le_lit == Lit::TRUE && ge_lit == Lit::TRUE {
            return Lit::TRUE;
        }
        if le_lit == Lit::FALSE || ge_lit == Lit::FALSE {
            return Lit::FALSE;
        }

        let proxy_var = self.sat_solver.mk_var();
        let p = Lit::new(proxy_var, false);

        // p -> (x <= y)
        self.sat_solver.add_clause([!p, le_lit]).expect("Failed to add clause");
        // p -> (x >= y)
        self.sat_solver.add_clause([!p, ge_lit]).expect("Failed to add clause");
        // (x <= y) ∧ (x >= y) -> p
        self.sat_solver.add_clause([!le_lit, !ge_lit, p]).expect("Failed to add clause");

        p
    }

    fn mk_ge(&mut self, e1: &ArithExpr, e2: &ArithExpr, strict: bool) -> Lit {
        let (vars, const_term) = self.diff(e1, e2);

        match vars.len() {
            0 => {
                if if strict { const_term.is_positive() } else { const_term.is_positive() || const_term.is_zero() } {
                    Lit::TRUE
                } else {
                    Lit::FALSE
                }
            }
            1 => {
                let (var, coeff) = vars.iter().next().unwrap();
                let is_int = self.lra_theory.is_int_var(*var);

                let is_upper_bound = !coeff.is_positive();

                let bound = if is_int {
                    let val = -const_term / coeff.clone();
                    let tight_val = tighten_int_bound(val, is_upper_bound, strict);
                    InfRational::new(Rational::Finite(tight_val), rug::Rational::from(0))
                } else {
                    let eps_shift = if strict { rug::Rational::from(1) } else { rug::Rational::from(0) };
                    InfRational::new(Rational::Finite(-const_term / coeff.clone()), eps_shift / coeff)
                };

                if self.sat_solver.decision_level() == 0 {
                    if is_upper_bound {
                        if self.lra_theory.ub(*var) <= &bound {
                            return Lit::TRUE;
                        }
                        if self.lra_theory.lb(*var) > &bound {
                            return Lit::FALSE;
                        }
                    } else {
                        if self.lra_theory.lb(*var) >= &bound {
                            return Lit::TRUE;
                        }
                        if self.lra_theory.ub(*var) < &bound {
                            return Lit::FALSE;
                        }
                    }
                }

                let bound = if is_upper_bound { TheoryConstraint::LraUb(*var, bound) } else { TheoryConstraint::LraLb(*var, bound) };
                self.get_or_create_proxy(bound)
            }
            _ => {
                let slack = self.lra_theory.get_or_create_slack(vars);
                let is_int = self.lra_theory.is_int_var(slack);

                let bound = if is_int {
                    let tight_val = tighten_int_bound(-const_term, false, strict);
                    InfRational::new(Rational::Finite(tight_val), rug::Rational::from(0))
                } else {
                    let eps_shift = if strict { rug::Rational::from(1) } else { rug::Rational::from(0) };
                    InfRational::new(Rational::Finite(-const_term), eps_shift)
                };

                if self.sat_solver.decision_level() == 0 {
                    if self.lra_theory.lb(slack) >= &bound {
                        return Lit::TRUE;
                    }
                    if self.lra_theory.ub(slack) < &bound {
                        return Lit::FALSE;
                    }
                }

                let bound = TheoryConstraint::LraLb(slack, bound);
                self.get_or_create_proxy(bound)
            }
        }
    }

    fn mk_dl_le(&mut self, from: usize, to: usize, bound: rug::Rational, strict: bool) -> Lit {
        let delta = if strict { rug::Rational::from(-1) } else { rug::Rational::from(0) };
        let inf_bound = InfRational::new(Rational::Finite(bound), delta);

        let constraint = TheoryConstraint::DlLeq(from, to, inf_bound);
        self.get_or_create_proxy(constraint)
    }

    fn mk_dl_ge(&mut self, from: usize, to: usize, bound: rug::Rational, strict: bool) -> Lit {
        self.mk_dl_le(to, from, -bound, strict)
    }

    fn mk_dl_eq(&mut self, from: usize, to: usize, bound: rug::Rational) -> Lit {
        let le_lit = self.mk_dl_le(from, to, bound.clone(), false);
        let ge_lit = self.mk_dl_ge(from, to, bound, false);

        let proxy_var = self.sat_solver.mk_var();
        let p = Lit::new(proxy_var, false);

        self.sat_solver.add_clause([!p, le_lit]).expect("Failed to add clause");
        self.sat_solver.add_clause([!p, ge_lit]).expect("Failed to add clause");
        self.sat_solver.add_clause([!le_lit, !ge_lit, p]).expect("Failed to add clause");

        p
    }

    fn diff(&self, e1: &ArithExpr, e2: &ArithExpr) -> (SparseRow, rug::Rational) {
        let mut vars = SparseRow::new();
        let mut const_term = rug::Rational::from(0);

        let pos_one = rug::Rational::from(1);
        let neg_one = rug::Rational::from(-1);

        let mut temp = rug::Rational::new();

        self.accumulate_expr(e1, &pos_one, &mut vars, &mut const_term, &mut temp);
        self.accumulate_expr(e2, &neg_one, &mut vars, &mut const_term, &mut temp);

        vars.retain(|_, c| *c != 0);

        (vars, const_term)
    }

    fn accumulate_expr(&self, expr: &ArithExpr, scale: &rug::Rational, vars: &mut SparseRow, const_term: &mut rug::Rational, temp: &mut rug::Rational) {
        match expr {
            ArithExpr::Const(c) => {
                temp.assign(c * scale);
                *const_term += &*temp;
            }
            ArithExpr::IntVar(var) => {
                self.accumulate_var(var.0, scale, vars, temp);
            }
            ArithExpr::RealVar(var) => {
                self.accumulate_var(var.0, scale, vars, temp);
            }
            ArithExpr::Add(terms) => {
                for term in terms {
                    self.accumulate_expr(term, scale, vars, const_term, temp);
                }
            }
            ArithExpr::Mul(terms) => {
                if terms.len() != 2 {
                    panic!("Only binary multiplication is supported in linear arithmetic");
                }
                let (first, second) = (&terms[0], &terms[1]);

                // Smart recursion: accept (Constant * SubExpression)
                match (first, second) {
                    (ArithExpr::Const(c), sub_expr) | (sub_expr, ArithExpr::Const(c)) => {
                        let mut new_scale = rug::Rational::new();
                        new_scale.assign(c * scale);
                        self.accumulate_expr(sub_expr, &new_scale, vars, const_term, temp);
                    }
                    _ => {
                        panic!("Non-linear arithmetic: multiplication between two non-constant expressions is not supported");
                    }
                }
            }
            ArithExpr::Div(numerator, denominator) => {
                // The denominator MUST be a constant to preserve linearity
                if let ArithExpr::Const(c) = &**denominator {
                    if c.is_zero() {
                        panic!("Division by zero detected in AST");
                    }
                    let mut div_scale = rug::Rational::new();
                    div_scale.assign(scale / c);
                    self.accumulate_expr(numerator, &div_scale, vars, const_term, temp);
                } else {
                    panic!("Non-linear arithmetic: division by a non-constant expression is not supported");
                }
            }
            ArithExpr::Neg(sub_expr) => {
                let mut neg_scale = rug::Rational::new();
                neg_scale.assign(scale * -1);
                self.accumulate_expr(sub_expr, &neg_scale, vars, const_term, temp);
            }
        }
    }

    fn accumulate_var(&self, var: usize, scale: &rug::Rational, vars: &mut SparseRow, temp: &mut rug::Rational) {
        if let Some(basic_row) = self.lra_theory.tableau.get(&var) {
            for (sub_var, sub_coeff) in basic_row.iter() {
                temp.assign(sub_coeff * scale);
                vars.add_coeff(*sub_var, temp);
            }
        } else {
            vars.add_coeff(var, scale);
        }
    }

    fn get_or_create_proxy(&mut self, constraint: TheoryConstraint) -> Lit {
        if let Some(sat_var) = self.registry.get_proxy(&constraint) {
            Lit::new(sat_var, false)
        } else {
            let sat_var = self.sat_solver.mk_var();
            self.registry.register(constraint, sat_var);
            Lit::new(sat_var, false)
        }
    }

    /// Adds a decision literal to the current search branch.
    ///
    /// This opens a new internal decision level and enqueues `lit` as a decision.
    /// Returns `false` only if the literal is immediately inconsistent with the
    /// current assignment.
    ///
    /// After a successful decision, call [`SeMiTONE::propagate`] to derive its
    /// consequences and detect theory conflicts.
    pub fn decide(&mut self, lit: Lit) -> bool {
        self.sat_solver.push();
        self.lra_theory.push();
        self.enum_theory.push();
        self.dl_theory.push();
        self.euf_theory.push();
        self.sat_solver.enqueue_decision(lit)
    }

    /// Decides that an enum variable takes a specific value in the current branch.
    ///
    /// Convenience wrapper around [`SeMiTONE::decide`]. Returns `false` if the
    /// decision cannot be enqueued consistently.
    pub fn decide_enum(&mut self, expr: impl AsRef<EnumExpr>, value: i32) -> bool {
        let var = match expr.as_ref() {
            EnumExpr::Var(v) => *v,
            EnumExpr::Const(_) => panic!("Cannot decide on a constant value"),
        };

        let lit = self.get_or_create_proxy(TheoryConstraint::EnumEq(var.0, value));
        self.decide(lit)
    }

    /// Returns the current internal decision level.
    ///
    /// Level `0` corresponds to the root level (no user decision applied).
    pub fn decision_level(&self) -> usize {
        self.sat_solver.decision_level()
    }

    /// Cancels all decisions and theory updates at levels deeper than `level`.
    ///
    /// This restores SAT/theory state to the requested level and drops pending
    /// notifications beyond the restored trail.
    pub fn cancel_until(&mut self, level: usize) {
        self.sat_solver.cancel_until(level);
        self.lra_theory.cancel_until(level);
        self.enum_theory.cancel_until(level);
        self.dl_theory.cancel_until(level);
        self.euf_theory.cancel_until(level);
        self.notified_len = self.sat_solver.trail.len();
    }

    /// Returns the trail suffix starting at `from_index`.
    ///
    /// Useful for external heuristics that need to inspect newly assigned literals.
    pub fn get_trail_delta(&self, from_index: usize) -> &[Lit] {
        &self.sat_solver.trail[from_index..]
    }

    /// Returns the current size of the SAT assignment trail.
    pub fn current_trail_len(&self) -> usize {
        self.sat_solver.trail.len()
    }

    /// Returns the trail length recorded at a specific decision level.
    ///
    /// If `level` is beyond the current number of levels, returns the current
    /// trail length.
    pub fn get_trail_len_at_level(&self, level: usize) -> usize {
        if level < self.sat_solver.trail_lim.len() { self.sat_solver.trail_lim[level] } else { self.sat_solver.trail.len() }
    }

    /// Returns a slice of the trail between `start` (inclusive) and `end` (exclusive).
    pub fn get_trail_slice(&self, start: usize, end: usize) -> &[Lit] {
        &self.sat_solver.trail[start..end]
    }

    /// Returns the number of currently active user scopes (`push`/`pop`).
    pub fn user_scopes_len(&self) -> usize {
        self.user_scopes.len()
    }

    /// Runs SAT + theory propagation until a fixed point or a conflict.
    ///
    /// Returns:
    /// - `Ok(())` if the current branch is feasible after propagation.
    /// - `Err((backtrack_level, no_good))` if a conflict is found.
    ///
    /// The returned `backtrack_level` is the suggested non-chronological level to
    /// backtrack to, and `no_good` is a conflict explanation suitable for learning.
    pub fn propagate(&mut self) -> Result<(), (usize, Vec<Lit>)> {
        let base_level = self.user_scopes.last().map(|&(lvl, _)| lvl).unwrap_or(0);
        if let Err((bt_level, conflict)) = self.sat_solver.propagate() {
            return Err((bt_level.max(base_level), conflict));
        }

        while self.notified_len < self.sat_solver.trail.len() {
            let lit = self.sat_solver.trail[self.notified_len];

            if let Some(constraint) = self.registry.get_constraint(lit.var()) {
                let theory_result = match (constraint, lit.sign()) {
                    (TheoryConstraint::LraLb(var, bound), false) => self.lra_theory.set_lb(Some(lit), *var, bound.clone()),
                    (TheoryConstraint::LraLb(var, bound), true) => {
                        let new_bound = if self.lra_theory.is_int_var(*var) {
                            if let Rational::Finite(r) = bound.rational_part() {
                                let tight_val = tighten_int_bound(r.clone(), true, true);
                                InfRational::new(Rational::Finite(tight_val), rug::Rational::from(0))
                            } else {
                                unreachable!()
                            }
                        } else {
                            InfRational::new(bound.rational_part().clone(), if bound.infinitesimal_part().is_positive() { rug::Rational::from(0) } else { rug::Rational::from(-1) })
                        };
                        self.lra_theory.set_ub(Some(lit), *var, new_bound)
                    }
                    (TheoryConstraint::LraUb(var, bound), false) => self.lra_theory.set_ub(Some(lit), *var, bound.clone()),
                    (TheoryConstraint::LraUb(var, bound), true) => {
                        let new_bound = if self.lra_theory.is_int_var(*var) {
                            if let Rational::Finite(r) = bound.rational_part() {
                                let tight_val = tighten_int_bound(r.clone(), false, true);
                                InfRational::new(Rational::Finite(tight_val), rug::Rational::from(0))
                            } else {
                                unreachable!()
                            }
                        } else {
                            InfRational::new(bound.rational_part().clone(), if bound.infinitesimal_part().is_negative() { rug::Rational::from(0) } else { rug::Rational::from(1) })
                        };
                        self.lra_theory.set_lb(Some(lit), *var, new_bound)
                    }
                    (TheoryConstraint::EnumEq(var, val), false) => self.enum_theory.set_eq(Some(lit), *var, *val),
                    (TheoryConstraint::EnumEq(var, val), true) => self.enum_theory.set_neq(Some(lit), *var, *val),
                    (TheoryConstraint::DlLeq(from, to, bound), false) => self.dl_theory.assert_edge(*from, *to, bound.clone(), lit).map_err(|cycle| cycle.into_iter().map(|l| !l).collect()),
                    (TheoryConstraint::DlLeq(from, to, bound), true) => {
                        let rat_part = bound.rational_part().clone();
                        let neg_rat = match rat_part {
                            Rational::Finite(val) => Rational::Finite(-val),
                            Rational::PositiveInf => Rational::NegativeInf,
                            Rational::NegativeInf => Rational::PositiveInf,
                        };

                        let neg_inf = -bound.infinitesimal_part().clone() - rug::Rational::from(1);
                        let neg_bound = InfRational::new(neg_rat, neg_inf);

                        self.dl_theory.assert_edge(*to, *from, neg_bound, lit).map_err(|cycle| cycle.into_iter().map(|l| !l).collect())
                    }
                    (TheoryConstraint::EufEq(t1, t2), false) => {
                        self.euf_theory.merge(*t1, *t2, Some(lit));
                        self.euf_theory.propagate_congruences();
                        self.euf_theory.check_disequalities()
                    }
                    (TheoryConstraint::EufEq(t1, t2), true) => {
                        self.euf_theory.propagate_congruences();
                        self.euf_theory.assert_disequality(*t1, *t2, lit)
                    }
                };

                if let Err(lemma) = theory_result {
                    return Err((self.compute_backtrack_level(&lemma, base_level), lemma));
                }
            }
            self.notified_len += 1;
        }

        if let Err(conflict) = self.lra_theory.check() {
            return Err((self.compute_backtrack_level(&conflict, base_level), conflict));
        }
        Ok(())
    }

    /// Returns the assigned value of a literal in the current trail.
    pub fn get_lit_val(&self, lit: Lit) -> Option<bool> {
        self.sat_solver.lit_value(lit)
    }

    /// Returns the assigned value of a Boolean expression when it is fully determined.
    pub fn get_bool_val(&self, expr: &BoolExpr) -> Option<bool> {
        match expr {
            BoolExpr::True => Some(true),
            BoolExpr::False => Some(false),
            BoolExpr::Var(v) => *self.sat_solver.value(v.0),
            BoolExpr::Not(inner) => self.get_bool_val(inner).map(|val| !val),
            BoolExpr::And(args) => {
                let mut result = Some(true);
                for arg in args {
                    match self.get_bool_val(arg) {
                        Some(val) => {
                            if !val {
                                return Some(false);
                            }
                        }
                        None => result = None,
                    }
                }
                result
            }
            BoolExpr::Or(args) => {
                let mut result = Some(false);
                for arg in args {
                    match self.get_bool_val(arg) {
                        Some(val) => {
                            if val {
                                return Some(true);
                            }
                        }
                        None => result = None,
                    }
                }
                result
            }
            BoolExpr::Lt(e1, e2) => {
                let (lb1, ub1) = self.get_arith_bounds(e1)?;
                let (lb2, ub2) = self.get_arith_bounds(e2)?;
                if ub1 < lb2 {
                    Some(true)
                } else if lb1 >= ub2 {
                    Some(false)
                } else {
                    None
                }
            }
            BoolExpr::Le(e1, e2) => {
                let (lb1, ub1) = self.get_arith_bounds(e1)?;
                let (lb2, ub2) = self.get_arith_bounds(e2)?;
                if ub1 <= lb2 {
                    Some(true)
                } else if lb1 > ub2 {
                    Some(false)
                } else {
                    None
                }
            }
            BoolExpr::Ge(e1, e2) => {
                let (lb1, ub1) = self.get_arith_bounds(e1)?;
                let (lb2, ub2) = self.get_arith_bounds(e2)?;
                if lb1 >= ub2 {
                    Some(true)
                } else if ub1 < lb2 {
                    Some(false)
                } else {
                    None
                }
            }
            BoolExpr::Gt(e1, e2) => {
                let (lb1, ub1) = self.get_arith_bounds(e1)?;
                let (lb2, ub2) = self.get_arith_bounds(e2)?;
                if lb1 > ub2 {
                    Some(true)
                } else if ub1 <= lb2 {
                    Some(false)
                } else {
                    None
                }
            }
            BoolExpr::DlLt(from, to, bound) => {
                let (lb_from, ub_from) = self.get_dl_bounds(*from);
                let (lb_to, ub_to) = self.get_dl_bounds(*to);
                let bound_inf = InfRational::new(Rational::Finite(bound.clone()), rug::Rational::from(0));

                // Max possible distance: ub_to - lb_from
                // Min possible distance: lb_to - ub_from
                let max_dist = ub_to.clone() - lb_from.clone();
                let min_dist = lb_to - ub_from;

                if max_dist < bound_inf {
                    Some(true)
                } else if min_dist >= bound_inf {
                    Some(false)
                } else {
                    None
                }
            }
            BoolExpr::DlLe(from, to, bound) => {
                let (lb_from, ub_from) = self.get_dl_bounds(*from);
                let (lb_to, ub_to) = self.get_dl_bounds(*to);
                let bound_inf = InfRational::new(Rational::Finite(bound.clone()), rug::Rational::from(0));

                let max_dist = ub_to.clone() - lb_from.clone();
                let min_dist = lb_to - ub_from;

                if max_dist <= bound_inf {
                    Some(true)
                } else if min_dist > bound_inf {
                    Some(false)
                } else {
                    None
                }
            }
            BoolExpr::DlGt(from, to, bound) => {
                let (lb_from, ub_from) = self.get_dl_bounds(*from);
                let (lb_to, ub_to) = self.get_dl_bounds(*to);
                let bound_inf = InfRational::new(Rational::Finite(bound.clone()), rug::Rational::from(0));

                let max_dist = ub_to.clone() - lb_from.clone();
                let min_dist = lb_to - ub_from;

                if min_dist > bound_inf {
                    Some(true)
                } else if max_dist <= bound_inf {
                    Some(false)
                } else {
                    None
                }
            }
            BoolExpr::DlGe(from, to, bound) => {
                let (lb_from, ub_from) = self.get_dl_bounds(*from);
                let (lb_to, ub_to) = self.get_dl_bounds(*to);
                let bound_inf = InfRational::new(Rational::Finite(bound.clone()), rug::Rational::from(0));

                let max_dist = ub_to.clone() - lb_from.clone();
                let min_dist = lb_to - ub_from;

                if min_dist >= bound_inf {
                    Some(true)
                } else if max_dist < bound_inf {
                    Some(false)
                } else {
                    None
                }
            }
            BoolExpr::DlEq(from, to, bound) => {
                let (lb_from, ub_from) = self.get_dl_bounds(*from);
                let (lb_to, ub_to) = self.get_dl_bounds(*to);
                let bound_inf = InfRational::new(Rational::Finite(bound.clone()), rug::Rational::from(0));

                let max_dist = ub_to.clone() - lb_from.clone();
                let min_dist = lb_to - ub_from;

                // Exact equality means max_dist and min_dist are both exactly equal to bound
                if max_dist == bound_inf && min_dist == bound_inf {
                    Some(true)
                } else if max_dist < bound_inf || min_dist > bound_inf {
                    Some(false) // It's impossible to satisfy the equality
                } else {
                    None
                }
            }
            BoolExpr::Eq(e1, e2) => match (e1.as_ref(), e2.as_ref()) {
                (Expr::Arith(a1), Expr::Arith(a2)) => {
                    let (lb1, ub1) = self.get_arith_bounds(a1)?;
                    let (lb2, ub2) = self.get_arith_bounds(a2)?;
                    if ub1 < lb2 || ub2 < lb1 {
                        Some(false)
                    } else if lb1 == ub1 && lb2 == ub2 && lb1 == lb2 {
                        Some(true)
                    } else {
                        None
                    }
                }
                (Expr::Bool(b1), Expr::Bool(b2)) => {
                    let val1 = self.get_bool_val(b1);
                    let val2 = self.get_bool_val(b2);
                    match (val1, val2) {
                        (Some(v1), Some(v2)) => Some(v1 == v2),
                        _ => None,
                    }
                }
                (Expr::Enum(e1), Expr::Enum(e2)) => {
                    let val1 = self.get_enum_val(e1);
                    let val2 = self.get_enum_val(e2);
                    match (val1, val2) {
                        (Some(v1), Some(v2)) => Some(v1 == v2),
                        _ => None,
                    }
                }
                _ => None,
            },
        }
    }

    /// Returns the current value of an arithmetic expression when it is fixed by the model.
    pub fn get_arith_val(&self, expr: &ArithExpr) -> Option<InfRational> {
        match expr {
            ArithExpr::Const(c) => Some(InfRational::new(Rational::Finite(c.clone()), rug::Rational::from(0))),
            ArithExpr::IntVar(v) => Some(self.lra_theory.value(v.0).clone()),
            ArithExpr::RealVar(v) => Some(self.lra_theory.value(v.0).clone()),
            ArithExpr::Add(terms) => {
                let mut sum = InfRational::new(Rational::Finite(rug::Rational::from(0)), rug::Rational::from(0));
                for term in terms {
                    sum += self.get_arith_val(term)?;
                }
                Some(sum)
            }
            ArithExpr::Neg(term) => self.get_arith_val(term).map(|val| -val),
            _ => None,
        }
    }

    /// Returns the current lower and upper bounds of an arithmetic expression when they are fixed by the model.
    pub fn get_arith_bounds(&self, expr: &ArithExpr) -> Option<(InfRational, InfRational)> {
        match expr {
            ArithExpr::Const(c) => {
                let val = InfRational::new(Rational::Finite(c.clone()), rug::Rational::from(0));
                Some((val.clone(), val))
            }
            ArithExpr::IntVar(v) => Some((self.lra_theory.lb(v.0).clone(), self.lra_theory.ub(v.0).clone())),
            ArithExpr::RealVar(v) => Some((self.lra_theory.lb(v.0).clone(), self.lra_theory.ub(v.0).clone())),
            ArithExpr::Add(terms) => {
                let mut sum_lb = InfRational::new(Rational::Finite(rug::Rational::from(0)), rug::Rational::from(0));
                let mut sum_ub = sum_lb.clone();
                for term in terms {
                    let (lb, ub) = self.get_arith_bounds(term)?;
                    sum_lb += lb;
                    sum_ub += ub;
                }
                Some((sum_lb, sum_ub))
            }
            ArithExpr::Neg(term) => {
                let (lb, ub) = self.get_arith_bounds(term)?;
                Some((-ub, -lb))
            }
            _ => None,
        }
    }

    /// Returns the concrete value of an enum variable if it has been fully assigned.
    pub fn get_enum_val(&self, expr: &EnumExpr) -> Option<i32> {
        match expr {
            EnumExpr::Const(c) => Some(*c),
            EnumExpr::Var(v) => {
                let domain = &self.enum_theory.active_domains[v.0];
                if domain.len() == 1 { domain.iter().next().copied() } else { None }
            }
        }
    }

    /// Returns the current value of a difference logic variable.
    pub fn get_dl_val(&self, var: DlVar) -> InfRational {
        self.dl_theory.value(var.0)
    }

    /// Returns the current lower and upper bounds of a difference logic variable.
    pub fn get_dl_bounds(&self, var: DlVar) -> (InfRational, InfRational) {
        (self.dl_theory.lb(var.0), self.dl_theory.ub(var.0))
    }

    /// Opens a new incremental user scope for assertions and decisions.
    ///
    /// All constraints/decisions added after this call can be discarded by
    /// [`SeMiTONE::pop`].
    pub fn push(&mut self) {
        let clauses_len = self.sat_solver.clauses.len();

        self.sat_solver.push();
        self.lra_theory.push();
        self.enum_theory.push();
        self.dl_theory.push();

        let current_level = self.sat_solver.decision_level();
        self.user_scopes.push((current_level, clauses_len));
    }

    /// Restores the previous user scope and discards assertions added in it.
    ///
    /// If no user scope is open, this is a no-op.
    pub fn pop(&mut self) {
        if let Some((saved_level, saved_clauses_len)) = self.user_scopes.pop() {
            self.cancel_until(saved_level - 1);
            for watch_list in self.sat_solver.watches.iter_mut() {
                watch_list.retain(|&clause_idx| clause_idx < saved_clauses_len);
            }
            self.sat_solver.clauses.truncate(saved_clauses_len);
        }
    }

    fn compute_backtrack_level(&self, lemma: &[Lit], root_level: usize) -> usize {
        if lemma.len() <= 1 {
            return root_level;
        }

        let mut levels: Vec<usize> = lemma.iter().map(|lit| self.sat_solver.level(lit.var()).expect("literal should have a decision level")).collect();

        levels.sort_unstable_by(|a, b| b.cmp(a));

        let max_level = levels[0];

        if max_level <= root_level {
            return root_level;
        }

        if levels.iter().filter(|&&l| l == max_level).count() == 1 { levels[1].max(root_level) } else { (max_level - 1).max(root_level) }
    }

    /// Checks integrality of integer variables under the current rational model.
    ///
    /// Returns `Ok(())` if all integer variables are integral.
    /// Otherwise returns `Err((backtrack_level, lemma))` with either a Gomory
    /// cut or a branching disjunction for branch-and-bound.
    pub fn check_ints(&mut self) -> Result<(), (usize, Vec<Lit>)> {
        if let Err((var, val)) = self.lra_theory.check_ints() {
            trace!("Integer variable {} has fractional value {}", var, val);

            if let Some((cut_row, cut_rhs, explanation_bounds)) = self.lra_theory.new_gomory_cut() {
                let cut_slack = self.lra_theory.get_or_create_slack(cut_row);
                let bound = InfRational::new(Rational::Finite(cut_rhs), rug::Rational::from(0));
                let cut_lit = self.get_or_create_proxy(TheoryConstraint::LraLb(cut_slack, bound));

                let mut lemma = Vec::with_capacity(explanation_bounds.len() + 1);
                for constraint in explanation_bounds {
                    let proxy = self.get_or_create_proxy(constraint);
                    lemma.push(!proxy);
                }
                lemma.push(cut_lit);

                trace!("Generated global Gomory cut with {} dependencies!", lemma.len() - 1);

                if self.sat_solver.lit_value(cut_lit) != Some(true) {
                    let backtrack_level = if lemma.len() == 1 { 0 } else { self.sat_solver.decision_level() };
                    return Err((backtrack_level, lemma));
                }
            }

            let Rational::Finite(inner_frac) = val.rational_part() else {
                unreachable!("Fractional variable in Branch & Bound must be finite");
            };

            let mut floor_val = inner_frac.clone();
            floor_val.floor_mut();
            let mut ceil_val = inner_frac.clone();
            ceil_val.ceil_mut();

            if floor_val == ceil_val {
                let inf_part = val.infinitesimal_part();
                if inf_part > &rug::Rational::from(0) {
                    ceil_val += rug::Rational::from(1);
                } else if inf_part < &rug::Rational::from(0) {
                    floor_val -= rug::Rational::from(1);
                }
            }

            let ub = InfRational::new(Rational::Finite(floor_val), rug::Rational::from(0));
            let lb = InfRational::new(Rational::Finite(ceil_val), rug::Rational::from(0));
            trace!("Branching on variable {}: x <= {} or x >= {}", var, ub, lb);

            let lit_ub = self.get_or_create_proxy(TheoryConstraint::LraUb(var, ub));
            let lit_lb = self.get_or_create_proxy(TheoryConstraint::LraLb(var, lb));
            let lemma = vec![lit_ub, lit_lb];

            let current_level = self.sat_solver.decision_level();
            return Err((current_level, lemma));
        }
        Ok(())
    }
}

#[cold]
#[inline(never)]
pub(crate) fn out_of_bounds(var: usize) -> ! {
    panic!("variable index out of bounds: {}", var);
}

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

    #[test]
    fn test_pure_sat_resolution() {
        let mut solver = SeMiTONE::new();

        let a = solver.new_bool();
        let b = solver.new_bool();
        let c = solver.new_bool();

        // (A ∨ B) ∧ (¬B ∨ C) ∧ (¬B ∨ ¬C) ∧ (¬A)
        // With ¬A, clause (A ∨ B) forces B; then B forces both C and ¬C.
        let expr = (&a | &b) & (&!&b | &c) & (!&b | !&c) & !&a;

        let _ = solver.assert(expr);
        assert!(solver.propagate().is_err(), "The solver should detect that the system is unsatisfiable");
    }

    #[test]
    fn test_early_bounding_unsat() {
        let mut solver = SeMiTONE::new();
        let x = solver.new_real();

        // x > 10 ∧ x < 5
        let expr = (x.clone().gt(10)) & (x.lt(5));

        let result = solver.assert(expr);
        assert!(!result, "The solver should detect that the system is unsatisfiable");
    }

    #[test]
    fn test_equality_mutually_exclusive() {
        let mut solver = SeMiTONE::new();
        let x = solver.new_real();

        // x == 5 ∧ x > 6
        let expr = (x.clone().eq(5)) & (x.gt(6));

        let result = solver.assert(expr);
        assert!(!result, "The solver should detect that the system is unsatisfiable");
    }

    #[test]
    fn test_simplex_system_unsat() {
        let mut solver = SeMiTONE::new();
        let x = solver.new_real();
        let y = solver.new_real();

        // x + y == 10
        let eq_expr = (x.clone() + y.clone()).eq(10);
        // x > 6
        let gt_x = x.clone().gt(6);
        // y > 6
        let gt_y = y.clone().gt(6);

        let expr = eq_expr & gt_x & gt_y;

        let _ = solver.assert(expr);
        assert!(solver.propagate().is_err(), "The solver should detect that the system is unsatisfiable");
    }

    #[test]
    fn test_constant_arithmetic_evaluations() {
        let mut solver = SeMiTONE::new();

        assert!(solver.assert(ArithExpr::from(5).lt(10)));
        assert!(solver.assert(ArithExpr::from(5).le(5)));
        assert!(solver.assert(ArithExpr::from(10).ge(5)));
        assert!(solver.assert(ArithExpr::from(10).gt(5)));

        assert!(!solver.assert(ArithExpr::from(10).lt(5)));
        assert!(!solver.assert(ArithExpr::from(10).le(5)));
        assert!(!solver.assert(ArithExpr::from(5).ge(10)));
        assert!(!solver.assert(ArithExpr::from(5).gt(10)));

        assert!(solver.assert(!ArithExpr::from(10).lt(5)));
        assert!(!solver.assert(!ArithExpr::from(5).lt(10)));
    }

    #[test]
    fn test_negated_variable_inequalities() {
        let mut solver = SeMiTONE::new();
        let x = solver.new_real();

        solver.push();
        assert!(solver.assert(!x.clone().lt(5)));
        assert!(!solver.assert(x.clone().lt(4)));
        solver.pop();

        solver.push();
        assert!(solver.assert(!x.clone().le(5)));
        assert!(!solver.assert(x.clone().le(5)));
        solver.pop();

        solver.push();
        assert!(solver.assert(!x.clone().ge(5)));
        assert!(!solver.assert(x.clone().ge(5)));
        solver.pop();

        solver.push();
        assert!(solver.assert(!x.clone().gt(5)));
        assert!(!solver.assert(x.clone().gt(5)));
        solver.pop();
    }

    #[test]
    #[should_panic(expected = "Type mismatch in Eq")]
    fn test_encode_eq_type_mismatch_panic() {
        let mut solver = SeMiTONE::new();
        let a = solver.new_bool();
        let x = solver.new_real();

        let bad_eq = BoolExpr::Eq(Box::new(Expr::Bool(a)), Box::new(Expr::Arith(x)));

        let _ = solver.assert(bad_eq);
    }

    #[test]
    fn test_enum_out_of_domain_unsat() {
        let mut solver = SeMiTONE::new();
        let e = solver.new_enum(vec![1, 2]);

        let expr = e.eq(3);

        let _ = solver.assert(expr);
        assert!(solver.propagate().is_err(), "The solver should detect that the enum variable cannot take a value outside its domain");
    }

    #[test]
    fn test_enum_exhaustive_denial_integration() {
        let mut solver = SeMiTONE::new();
        let e = solver.new_enum(vec![1, 2]);

        // (e != 1) AND (e != 2)
        let expr = !(e.clone().eq(1)) & !(e.clone().eq(2));

        let _ = solver.assert(expr);
        assert!(solver.propagate().is_err(), "The solver should detect that the enum variable cannot take a value outside its domain");
    }

    #[test]
    fn test_dl_chain_upper_bounds() {
        let mut solver = SeMiTONE::new();
        let zero = solver.new_dl_var(); // first difference logic variable created = reference 0 in the DL theory
        let t1 = solver.new_dl_var();
        let t2 = solver.new_dl_var();

        let _ = solver.assert(BoolExpr::DlLe(zero, t1, rug::Rational::from(10))); // t1 - zero <= 10
        let _ = solver.assert(BoolExpr::DlLe(t1, t2, rug::Rational::from(5))); // t2 - t1 <= 5

        assert!(solver.propagate().is_ok());

        assert_eq!(solver.get_dl_bounds(t1).1, InfRational::from(10));
        assert_eq!(solver.get_dl_bounds(t2).1, InfRational::from(15));
        assert_eq!(solver.get_dl_bounds(t1).0, InfRational::from(Rational::NegativeInf), "no lower bound imposed");
    }

    #[test]
    fn test_dl_lower_and_upper_bound_interval() {
        let mut solver = SeMiTONE::new();
        let zero = solver.new_dl_var();
        let t1 = solver.new_dl_var();

        let _ = solver.assert(BoolExpr::DlLe(zero, t1, rug::Rational::from(8))); // t1 <= 8
        let _ = solver.assert(BoolExpr::DlGe(zero, t1, rug::Rational::from(3))); // t1 >= 3

        assert!(solver.propagate().is_ok());
        assert_eq!(solver.get_dl_bounds(t1), (InfRational::from(3), InfRational::from(8)));
    }

    #[test]
    fn test_dl_eq_pins_value() {
        let mut solver = SeMiTONE::new();
        let zero = solver.new_dl_var();
        let t1 = solver.new_dl_var();

        let _ = solver.assert(BoolExpr::DlEq(zero, t1, rug::Rational::from(7)));
        assert!(solver.propagate().is_ok());

        assert_eq!(solver.get_dl_bounds(t1), (InfRational::from(7), InfRational::from(7)));
    }

    #[test]
    fn test_dl_negative_cycle_conflict() {
        let mut solver = SeMiTONE::new();
        let a = solver.new_dl_var();
        let b = solver.new_dl_var();

        // b - a <= 5 and b - a >= 10 (via a - b <= -10): irreconcilable
        let _ = solver.assert(BoolExpr::DlLe(a, b, rug::Rational::from(5)));
        let _ = solver.assert(BoolExpr::DlLe(b, a, rug::Rational::from(-10)));

        assert!(solver.propagate().is_err(), "the negative cycle must produce a conflict");
    }

    #[test]
    fn test_dl_strict_boundary_conflict() {
        let mut solver = SeMiTONE::new();
        let a = solver.new_dl_var();
        let b = solver.new_dl_var();

        // b - a <= 0 (non-strict) and a - b < 0, i.e. b - a > 0 (strict):
        // the weights sum to 0 but the conflict must be detected via the infinitesimal trick
        let _ = solver.assert(BoolExpr::DlLe(a, b, rug::Rational::from(0)));
        let _ = solver.assert(BoolExpr::DlLt(b, a, rug::Rational::from(0)));

        assert!(solver.propagate().is_err());
    }

    #[test]
    fn test_dl_non_strict_boundary_is_sat() {
        let mut solver = SeMiTONE::new();
        let a = solver.new_dl_var();
        let b = solver.new_dl_var();

        let _ = solver.assert(BoolExpr::DlLe(a, b, rug::Rational::from(0)));
        let _ = solver.assert(BoolExpr::DlLe(b, a, rug::Rational::from(0)));

        assert!(solver.propagate().is_ok());
        assert_eq!(solver.get_dl_bounds(b), solver.get_dl_bounds(a), "b == a == 0");
    }

    #[test]
    fn test_dl_backtracking_via_push_pop() {
        let mut solver = SeMiTONE::new();
        let zero = solver.new_dl_var();
        let t1 = solver.new_dl_var();

        solver.push();
        let _ = solver.assert(BoolExpr::DlLe(zero, t1, rug::Rational::from(4)));
        assert!(solver.propagate().is_ok());
        assert_eq!(solver.get_dl_bounds(t1).1, InfRational::from(4));
        solver.pop();

        assert_eq!(solver.get_dl_bounds(t1).1, InfRational::from(Rational::PositiveInf), "the constraint must disappear after the pop");
    }

    #[test]
    fn test_dl_negated_le_forces_strict_gt() {
        // `assert(!expr)` goes through the catch-all branch of `assert_internal`, which negates
        // at the SAT level the literal already registered for DlLe (it does not call mk_dl_ge).
        // This exercises the `(DlLeq(..), false)` branch of `propagate()`.
        let mut solver = SeMiTONE::new();
        let a = solver.new_dl_var();
        let b = solver.new_dl_var();

        let neg_le = BoolExpr::Not(Box::new(BoolExpr::DlLe(a, b, rug::Rational::from(4))));
        let _ = solver.assert(neg_le);
        assert!(solver.propagate().is_ok());

        let (lb_b, _) = solver.get_dl_bounds(b);
        assert!(lb_b > InfRational::from(4), "!(b - a <= 4) must imply b - a > 4, found lb(b) = {:?}", lb_b);
    }
}