alkahest-cas 3.12.0

High-performance computer algebra kernel: symbolic expressions, polynomials, Gröbner bases, JIT, and Arb ball arithmetic.
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
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
//! First-order formulas over symbolic reals (V3-3 / FOFormula).
//!
//! - [`Formula`] is the algebraic view over kernel predicates and quantifiers.
//! - [`satisfiable`] decides two disjoint quantifier-free fragments and answers
//!   [`Satisfiability::Unknown`] for everything else:
//!   1. **Purely propositional** — every leaf is a bare [`ExprData::Symbol`] or
//!      a `True`/`False` constant, combined with `And` / `Or` / `Not`.  This is
//!      decided *completely* (Tseitin + a budgeted DPLL): every such
//!      formula gets `Sat`/`Unsat`, never `Unknown`, up to the search budget.
//!   2. **Single-variable interval arithmetic** — conjunctions of comparisons
//!      between **one** real symbol and a rational constant, plus `Or` / `Not`
//!      (via NNF on relations).  Incomplete; `Unknown` is common here.
//!
//!   A formula that *mixes* the two (a Boolean symbol and an arithmetic
//!   relation in the same formula) is deliberately `Unknown`: deciding it needs
//!   a Boolean-plus-theory combination this module does not implement, and the
//!   only cheap alternative — abstracting each relation to a fresh proposition —
//!   is sound for `Unsat` only and would report `x > 0 ∧ x < 0` as *sat*.  Route
//!   those to [`crate::real::decide`] (CAD) or to `alkahest.smt`.
//! - [`smtlib`] exports a [`Formula`] as SMT-LIB 2 text for an external solver
//!   (P2-3).  `alkahest.smt` drives the solver process from Python.

pub mod smtlib;

use crate::kernel::expr::PredicateKind;
use crate::kernel::{ExprData, ExprId, ExprPool};
use std::collections::HashMap;
use std::fmt;

// ---------------------------------------------------------------------------
// Errors
// ---------------------------------------------------------------------------

/// Failure to interpret an expression as a boolean formula.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LogicError {
    /// The expression node is not supported in [`formula_from_expr`].
    UnsupportedExpr(&'static str),
}

impl fmt::Display for LogicError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            LogicError::UnsupportedExpr(s) => write!(f, "{s}"),
        }
    }
}

impl std::error::Error for LogicError {}

impl crate::errors::AlkahestError for LogicError {
    fn code(&self) -> &'static str {
        "E-LOGIC-001"
    }
}

// ---------------------------------------------------------------------------
// Formula
// ---------------------------------------------------------------------------

/// First-order formula; [`Formula::Atom`] wraps kernel [`PredicateKind`] + operands.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Formula {
    Atom {
        kind: PredicateKind,
        args: Vec<ExprId>,
    },
    And(Box<Formula>, Box<Formula>),
    Or(Box<Formula>, Box<Formula>),
    Not(Box<Formula>),
    True,
    False,
    Forall {
        var: ExprId,
        body: Box<Formula>,
    },
    Exists {
        var: ExprId,
        body: Box<Formula>,
    },
}

impl Formula {
    pub fn and(a: Formula, b: Formula) -> Self {
        Formula::And(Box::new(a), Box::new(b))
    }

    pub fn or(a: Formula, b: Formula) -> Self {
        Formula::Or(Box::new(a), Box::new(b))
    }

    #[allow(clippy::should_implement_trait)] // `Not::not` would force unary `!` semantics at call sites we avoid for clarity.
    pub fn not(a: Formula) -> Self {
        Formula::Not(Box::new(a))
    }

    /// Intern this formula as [`ExprData`] nodes.
    pub fn to_expr(&self, pool: &ExprPool) -> ExprId {
        match self {
            Formula::True => pool.pred_true(),
            Formula::False => pool.pred_false(),
            Formula::Atom { kind, args } => pool.predicate(kind.clone(), args.clone()),
            Formula::And(l, r) => pool.pred_and(vec![l.to_expr(pool), r.to_expr(pool)]),
            Formula::Or(l, r) => pool.pred_or(vec![l.to_expr(pool), r.to_expr(pool)]),
            Formula::Not(x) => pool.pred_not(x.to_expr(pool)),
            Formula::Forall { var, body } => pool.forall(*var, body.to_expr(pool)),
            Formula::Exists { var, body } => pool.exists(*var, body.to_expr(pool)),
        }
    }
}

/// Lift a predicate (or quantified) `ExprId` into a structured [`Formula`].
pub fn formula_from_expr(expr: ExprId, pool: &ExprPool) -> Result<Formula, LogicError> {
    match pool.get(expr) {
        ExprData::Predicate { kind, args } => match kind {
            PredicateKind::True => Ok(Formula::True),
            PredicateKind::False => Ok(Formula::False),
            PredicateKind::And => {
                if args.is_empty() {
                    Ok(Formula::True)
                } else {
                    let mut it = args.into_iter();
                    let first = formula_from_expr(it.next().unwrap(), pool)?;
                    it.try_fold(first, |acc, e| {
                        Ok(Formula::and(acc, formula_from_expr(e, pool)?))
                    })
                }
            }
            PredicateKind::Or => {
                if args.is_empty() {
                    Ok(Formula::False)
                } else {
                    let mut it = args.into_iter();
                    let first = formula_from_expr(it.next().unwrap(), pool)?;
                    it.try_fold(first, |acc, e| {
                        Ok(Formula::or(acc, formula_from_expr(e, pool)?))
                    })
                }
            }
            PredicateKind::Not => {
                if args.len() != 1 {
                    return Err(LogicError::UnsupportedExpr("Not predicate arity must be 1"));
                }
                Ok(Formula::not(formula_from_expr(args[0], pool)?))
            }
            PredicateKind::Lt
            | PredicateKind::Le
            | PredicateKind::Gt
            | PredicateKind::Ge
            | PredicateKind::Eq
            | PredicateKind::Ne => {
                if args.len() != 2 {
                    return Err(LogicError::UnsupportedExpr("relation arity must be 2"));
                }
                Ok(Formula::Atom { kind, args })
            }
        },
        ExprData::Forall { var, body } => Ok(Formula::Forall {
            var,
            body: Box::new(formula_from_expr(body, pool)?),
        }),
        ExprData::Exists { var, body } => Ok(Formula::Exists {
            var,
            body: Box::new(formula_from_expr(body, pool)?),
        }),
        _ => Err(LogicError::UnsupportedExpr(
            "expression is not a predicate or quantified formula",
        )),
    }
}

// ---------------------------------------------------------------------------
// Intervals (one variable)
// ---------------------------------------------------------------------------

#[derive(Clone, Debug)]
enum Bound {
    Lower { val: rug::Rational, strict: bool },
    Upper { val: rug::Rational, strict: bool },
}

#[derive(Clone, Debug, Default)]
struct VarInterval {
    lower: Option<Bound>,
    upper: Option<Bound>,
}

impl VarInterval {
    fn is_empty(&self) -> bool {
        match (&self.lower, &self.upper) {
            (
                Some(Bound::Lower {
                    val: lo,
                    strict: ls,
                }),
                Some(Bound::Upper {
                    val: hi,
                    strict: us,
                }),
            ) => {
                if lo > hi {
                    return true;
                }
                if lo < hi {
                    return false;
                }
                *ls || *us
            }
            _ => false,
        }
    }

    fn intersect(&self, other: &VarInterval) -> Option<VarInterval> {
        let lower = match (&self.lower, &other.lower) {
            (None, b) => b.clone(),
            (a, None) => a.clone(),
            (
                Some(Bound::Lower { val: a, strict: sa }),
                Some(Bound::Lower { val: b, strict: sb }),
            ) => {
                if a > b {
                    Some(Bound::Lower {
                        val: a.clone(),
                        strict: *sa,
                    })
                } else if b > a {
                    Some(Bound::Lower {
                        val: b.clone(),
                        strict: *sb,
                    })
                } else {
                    Some(Bound::Lower {
                        val: a.clone(),
                        strict: *sa || *sb,
                    })
                }
            }
            _ => return None,
        };
        let upper = match (&self.upper, &other.upper) {
            (None, b) => b.clone(),
            (a, None) => a.clone(),
            (
                Some(Bound::Upper { val: a, strict: sa }),
                Some(Bound::Upper { val: b, strict: sb }),
            ) => {
                if a < b {
                    Some(Bound::Upper {
                        val: a.clone(),
                        strict: *sa,
                    })
                } else if b < a {
                    Some(Bound::Upper {
                        val: b.clone(),
                        strict: *sb,
                    })
                } else {
                    Some(Bound::Upper {
                        val: a.clone(),
                        strict: *sa || *sb,
                    })
                }
            }
            _ => return None,
        };
        let r = VarInterval { lower, upper };
        if r.is_empty() {
            None
        } else {
            Some(r)
        }
    }
}

fn rat_atom(pool: &ExprPool, id: ExprId) -> Option<rug::Rational> {
    match pool.get(id) {
        ExprData::Integer(n) => Some(rug::Rational::from(n.0)),
        ExprData::Rational(r) => Some(r.0.clone()),
        _ => None,
    }
}

fn symbol_key(pool: &ExprPool, id: ExprId) -> Option<String> {
    pool.with(id, |d| match d {
        ExprData::Symbol { name, .. } => Some(name.clone()),
        _ => None,
    })
}

fn atom_to_interval(
    pool: &ExprPool,
    kind: PredicateKind,
    args: &[ExprId],
) -> Option<(ExprId, VarInterval)> {
    if args.len() != 2 {
        return None;
    }
    let (a, b) = (args[0], args[1]);
    let (var, c_id, swapped) = if symbol_key(pool, a).is_some() && rat_atom(pool, b).is_some() {
        (a, b, false)
    } else if rat_atom(pool, a).is_some() && symbol_key(pool, b).is_some() {
        (b, a, true)
    } else {
        return None;
    };
    let c = rat_atom(pool, c_id)?;
    let iv = match (kind, swapped) {
        (PredicateKind::Lt, false) => VarInterval {
            lower: None,
            upper: Some(Bound::Upper {
                val: c,
                strict: true,
            }),
        },
        (PredicateKind::Le, false) => VarInterval {
            lower: None,
            upper: Some(Bound::Upper {
                val: c,
                strict: false,
            }),
        },
        (PredicateKind::Gt, false) => VarInterval {
            lower: Some(Bound::Lower {
                val: c,
                strict: true,
            }),
            upper: None,
        },
        (PredicateKind::Ge, false) => VarInterval {
            lower: Some(Bound::Lower {
                val: c,
                strict: false,
            }),
            upper: None,
        },
        (PredicateKind::Eq, false) => VarInterval {
            lower: Some(Bound::Lower {
                val: c.clone(),
                strict: false,
            }),
            upper: Some(Bound::Upper {
                val: c,
                strict: false,
            }),
        },
        (PredicateKind::Lt, true) => VarInterval {
            lower: Some(Bound::Lower {
                val: c,
                strict: true,
            }),
            upper: None,
        },
        (PredicateKind::Le, true) => VarInterval {
            lower: Some(Bound::Lower {
                val: c,
                strict: false,
            }),
            upper: None,
        },
        (PredicateKind::Gt, true) => VarInterval {
            lower: None,
            upper: Some(Bound::Upper {
                val: c,
                strict: true,
            }),
        },
        (PredicateKind::Ge, true) => VarInterval {
            lower: None,
            upper: Some(Bound::Upper {
                val: c,
                strict: false,
            }),
        },
        _ => return None,
    };
    Some((var, iv))
}

fn is_rel(k: &PredicateKind) -> bool {
    matches!(
        k,
        PredicateKind::Lt
            | PredicateKind::Le
            | PredicateKind::Gt
            | PredicateKind::Ge
            | PredicateKind::Eq
            | PredicateKind::Ne
    )
}

fn dual_kind(kind: PredicateKind) -> PredicateKind {
    use PredicateKind::*;
    match kind {
        Lt => Ge,
        Le => Gt,
        Gt => Le,
        Ge => Lt,
        Eq => Ne,
        Ne => Eq,
        other => other,
    }
}

fn nnf(f: Formula) -> Formula {
    match f {
        Formula::Not(inner) => match *inner {
            Formula::True => Formula::False,
            Formula::False => Formula::True,
            Formula::Not(g) => nnf(*g),
            Formula::And(a, b) => nnf(Formula::or(Formula::not(*a), Formula::not(*b))),
            Formula::Or(a, b) => nnf(Formula::and(Formula::not(*a), Formula::not(*b))),
            Formula::Forall { var, body } => nnf(Formula::Exists {
                var,
                body: Box::new(Formula::not(*body)),
            }),
            Formula::Exists { var, body } => nnf(Formula::Forall {
                var,
                body: Box::new(Formula::not(*body)),
            }),
            Formula::Atom {
                kind: PredicateKind::True,
                ..
            } => Formula::False,
            Formula::Atom {
                kind: PredicateKind::False,
                ..
            } => Formula::True,
            Formula::Atom { kind, args } if is_rel(&kind) => Formula::Atom {
                kind: dual_kind(kind),
                args,
            },
            inner => Formula::Not(Box::new(inner)),
        },
        Formula::And(a, b) => Formula::and(nnf(*a), nnf(*b)),
        Formula::Or(a, b) => Formula::or(nnf(*a), nnf(*b)),
        Formula::Forall { var, body } => Formula::Forall {
            var,
            body: Box::new(nnf(*body)),
        },
        Formula::Exists { var, body } => Formula::Exists {
            var,
            body: Box::new(nnf(*body)),
        },
        other => other,
    }
}

fn witness_rational(iv: &VarInterval) -> Option<rug::Rational> {
    let eps = || rug::Rational::from((1, 10_000));
    match (&iv.lower, &iv.upper) {
        (None, None) => Some(rug::Rational::from(0)),
        (Some(Bound::Lower { val: lo, strict: s }), None) => {
            let e = eps();
            Some(if *s { lo.clone() + &e } else { lo.clone() })
        }
        (None, Some(Bound::Upper { val: hi, strict: s })) => {
            let e = eps();
            Some(if *s { hi.clone() - &e } else { hi.clone() })
        }
        (
            Some(Bound::Lower {
                val: lo,
                strict: sl,
            }),
            Some(Bound::Upper {
                val: hi,
                strict: su,
            }),
        ) => {
            if lo > hi {
                return None;
            }
            if lo < hi {
                return Some((lo.clone() + hi.clone()) / rug::Rational::from(2));
            }
            // lo == hi
            if *sl || *su {
                None
            } else {
                Some(lo.clone())
            }
        }
        _ => None,
    }
}

fn map_to_witness(
    m: &HashMap<ExprId, VarInterval>,
    pool: &ExprPool,
) -> Result<HashMap<String, String>, SatFail> {
    let mut out = HashMap::new();
    for (&id, iv) in m {
        let name = symbol_key(pool, id).ok_or(SatFail::Unknown)?;
        let w = witness_rational(iv).ok_or(SatFail::Unknown)?;
        out.insert(name, w.to_string());
    }
    Ok(out)
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Satisfiability {
    Sat(HashMap<String, String>),
    Unsat,
    Unknown,
}

enum SatFail {
    Unsat,
    Unknown,
}

fn merge_maps(
    mut a: HashMap<ExprId, VarInterval>,
    b: HashMap<ExprId, VarInterval>,
) -> Result<HashMap<ExprId, VarInterval>, SatFail> {
    for (k, vb) in b {
        match a.remove(&k) {
            None => {
                a.insert(k, vb);
            }
            Some(va) => {
                let m = va.intersect(&vb).ok_or(SatFail::Unsat)?;
                a.insert(k, m);
            }
        }
    }
    Ok(a)
}

fn sat_intervals(f: &Formula, pool: &ExprPool) -> Result<HashMap<ExprId, VarInterval>, SatFail> {
    match f {
        Formula::True => Ok(HashMap::new()),
        Formula::False => Err(SatFail::Unsat),
        Formula::Forall { .. } => Err(SatFail::Unknown),
        Formula::Exists { body, .. } => sat_intervals(body, pool),
        Formula::And(a, b) => {
            let ma = sat_intervals(a, pool)?;
            let mb = sat_intervals(b, pool)?;
            merge_maps(ma, mb)
        }
        Formula::Or(a, b) => match sat_intervals(a, pool) {
            Ok(m) => Ok(m),
            Err(SatFail::Unsat) => sat_intervals(b, pool),
            Err(SatFail::Unknown) => match sat_intervals(b, pool) {
                Ok(m) => Ok(m),
                Err(SatFail::Unsat) => Err(SatFail::Unknown),
                Err(SatFail::Unknown) => Err(SatFail::Unknown),
            },
        },
        Formula::Not(inner) => {
            if let Formula::Atom { kind, args } = inner.as_ref() {
                if is_rel(kind) {
                    let dual = Formula::Atom {
                        kind: dual_kind(kind.clone()),
                        args: args.clone(),
                    };
                    return sat_intervals(&dual, pool);
                }
            }
            Err(SatFail::Unknown)
        }
        Formula::Atom { kind, args } => {
            if matches!(
                kind,
                PredicateKind::And | PredicateKind::Or | PredicateKind::Not
            ) {
                return Err(SatFail::Unknown);
            }
            if matches!(kind, PredicateKind::True) {
                return Ok(HashMap::new());
            }
            if matches!(kind, PredicateKind::False) {
                return Err(SatFail::Unsat);
            }
            let (v, iv) = atom_to_interval(pool, kind.clone(), args).ok_or(SatFail::Unknown)?;
            if iv.is_empty() {
                return Err(SatFail::Unsat);
            }
            let mut m = HashMap::new();
            m.insert(v, iv);
            Ok(m)
        }
    }
}

fn simplify_formula_constants(f: Formula) -> Formula {
    match f {
        Formula::And(a, b) => {
            let la = simplify_formula_constants(*a);
            let lb = simplify_formula_constants(*b);
            match (&la, &lb) {
                (Formula::False, _) | (_, Formula::False) => Formula::False,
                (Formula::True, x) => x.clone(),
                (x, Formula::True) => x.clone(),
                _ => Formula::and(la, lb),
            }
        }
        Formula::Or(a, b) => {
            let la = simplify_formula_constants(*a);
            let lb = simplify_formula_constants(*b);
            match (&la, &lb) {
                (Formula::True, _) | (_, Formula::True) => Formula::True,
                (Formula::False, x) => x.clone(),
                (x, Formula::False) => x.clone(),
                _ => Formula::or(la, lb),
            }
        }
        Formula::Not(x) => Formula::not(simplify_formula_constants(*x)),
        Formula::Forall { var, body } => Formula::Forall {
            var,
            body: Box::new(simplify_formula_constants(*body)),
        },
        Formula::Exists { var, body } => Formula::Exists {
            var,
            body: Box::new(simplify_formula_constants(*body)),
        },
        other => other,
    }
}

// ---------------------------------------------------------------------------
// Purely propositional fragment
// ---------------------------------------------------------------------------

/// Most distinct Boolean symbols the propositional path will accept.
const MAX_PROP_VARS: usize = 64;
/// Most formula nodes the propositional path will visit while lifting.
const MAX_PROP_NODES: usize = 4096;
/// Decision budget handed to [`dpll_sat_bounded`] by [`satisfiable`].
const PROP_DECISION_BUDGET: u64 = 2_000_000;

/// The string a [`Satisfiability::Sat`] witness uses for a true Boolean.
pub(crate) const BOOL_TRUE: &str = "true";
/// The string a [`Satisfiability::Sat`] witness uses for a false Boolean.
pub(crate) const BOOL_FALSE: &str = "false";

/// A formula in the purely propositional fragment.
///
/// `Var(i)` is a **1-based** proposition index, so it doubles as a [`BoolLit`].
#[derive(Debug, Clone, PartialEq, Eq)]
enum Prop {
    Var(u32),
    Const(bool),
    And(Vec<Prop>),
    Or(Vec<Prop>),
    Not(Box<Prop>),
}

impl Prop {
    /// Evaluate under a total assignment (`model[i - 1]` is the value of `Var(i)`).
    fn eval(&self, model: &[bool]) -> bool {
        match self {
            Prop::Const(b) => *b,
            Prop::Var(i) => model[*i as usize - 1],
            Prop::Not(x) => !x.eval(model),
            Prop::And(xs) => xs.iter().all(|x| x.eval(model)),
            Prop::Or(xs) => xs.iter().any(|x| x.eval(model)),
        }
    }
}

/// Variable table built while lifting an [`ExprId`] into a [`Prop`].
struct PropVars {
    /// Symbol `ExprId` → 1-based proposition index.
    index: HashMap<ExprId, u32>,
    /// Proposition index − 1 → symbol name, for the witness map.
    names: Vec<String>,
    /// Formula nodes visited so far (a DAG node is counted once per visit).
    nodes: usize,
}

/// Lift `expr` into the purely propositional fragment.
///
/// Returns `None` when `expr` is **not** purely propositional — it contains an
/// arithmetic relation, a quantifier, a non-symbol term, two same-named symbols
/// that a witness map could not tell apart, or more material than the budget
/// allows.  `None` therefore means "some other decision procedure's problem",
/// never "unsatisfiable".
fn prop_from_expr(expr: ExprId, pool: &ExprPool, vars: &mut PropVars) -> Option<Prop> {
    vars.nodes += 1;
    if vars.nodes > MAX_PROP_NODES {
        return None;
    }
    match pool.get(expr) {
        ExprData::Symbol { name, .. } => {
            if let Some(&i) = vars.index.get(&expr) {
                return Some(Prop::Var(i));
            }
            // Distinct `ExprId`s can share a name (same name, different
            // `Domain`).  The witness map is keyed by name, so it could not
            // report both; refuse rather than conflate them.
            if vars.names.contains(&name) {
                return None;
            }
            if vars.names.len() >= MAX_PROP_VARS {
                return None;
            }
            let i = vars.names.len() as u32 + 1;
            vars.names.push(name);
            vars.index.insert(expr, i);
            Some(Prop::Var(i))
        }
        ExprData::Predicate { kind, args } => match kind {
            PredicateKind::True => Some(Prop::Const(true)),
            PredicateKind::False => Some(Prop::Const(false)),
            PredicateKind::And => args
                .into_iter()
                .map(|a| prop_from_expr(a, pool, vars))
                .collect::<Option<Vec<_>>>()
                .map(Prop::And),
            PredicateKind::Or => args
                .into_iter()
                .map(|a| prop_from_expr(a, pool, vars))
                .collect::<Option<Vec<_>>>()
                .map(Prop::Or),
            PredicateKind::Not => {
                if args.len() != 1 {
                    return None;
                }
                prop_from_expr(args[0], pool, vars).map(|p| Prop::Not(Box::new(p)))
            }
            // A relation is an arithmetic atom, not a proposition.
            _ => None,
        },
        _ => None,
    }
}

/// Fold `True`/`False` away.  Afterwards a [`Prop::Const`] can only be the root.
fn prop_fold(p: Prop) -> Prop {
    match p {
        Prop::And(xs) => {
            let mut out = Vec::with_capacity(xs.len());
            for x in xs {
                match prop_fold(x) {
                    Prop::Const(true) => {}
                    Prop::Const(false) => return Prop::Const(false),
                    other => out.push(other),
                }
            }
            match out.len() {
                0 => Prop::Const(true),
                1 => out.pop().expect("len == 1"),
                _ => Prop::And(out),
            }
        }
        Prop::Or(xs) => {
            let mut out = Vec::with_capacity(xs.len());
            for x in xs {
                match prop_fold(x) {
                    Prop::Const(false) => {}
                    Prop::Const(true) => return Prop::Const(true),
                    other => out.push(other),
                }
            }
            match out.len() {
                0 => Prop::Const(false),
                1 => out.pop().expect("len == 1"),
                _ => Prop::Or(out),
            }
        }
        Prop::Not(x) => match prop_fold(*x) {
            Prop::Const(b) => Prop::Const(!b),
            other => Prop::Not(Box::new(other)),
        },
        other => other,
    }
}

/// Tseitin encoder: a [`Prop`] becomes an equisatisfiable CNF over the original
/// propositions plus one fresh definition variable per gate.
struct Tseitin {
    clauses: Vec<BoolClause>,
    /// Highest variable index allocated so far.
    next: u32,
}

impl Tseitin {
    fn fresh(&mut self) -> Option<BoolLit> {
        self.next = self.next.checked_add(1)?;
        // Definition variables sit above the originals; keep them inside `i32`.
        if self.next > (MAX_PROP_VARS + MAX_PROP_NODES) as u32 {
            return None;
        }
        Some(self.next as BoolLit)
    }

    /// Emit the defining clauses for `p` and return the literal it denotes.
    ///
    /// Both directions of each definition are emitted (`g ↔ gate`), so the CNF
    /// is satisfiable exactly when `p` is, and any CNF model restricted to the
    /// original propositions is a model of `p`.
    fn encode(&mut self, p: &Prop) -> Option<BoolLit> {
        match p {
            // Folded away by `prop_fold` before encoding.
            Prop::Const(_) => None,
            Prop::Var(i) => Some(*i as BoolLit),
            Prop::Not(x) => Some(-self.encode(x)?),
            Prop::And(xs) => {
                let lits = xs
                    .iter()
                    .map(|x| self.encode(x))
                    .collect::<Option<Vec<_>>>()?;
                let g = self.fresh()?;
                // g → xᵢ for every i
                let mut long = Vec::with_capacity(lits.len() + 1);
                long.push(g);
                for l in lits {
                    self.clauses.push(vec![-g, l]);
                    long.push(-l);
                }
                // (⋀ xᵢ) → g
                self.clauses.push(long);
                Some(g)
            }
            Prop::Or(xs) => {
                let lits = xs
                    .iter()
                    .map(|x| self.encode(x))
                    .collect::<Option<Vec<_>>>()?;
                let g = self.fresh()?;
                // g → ⋁ xᵢ
                let mut long = Vec::with_capacity(lits.len() + 1);
                long.push(-g);
                for l in lits {
                    self.clauses.push(vec![g, -l]);
                    long.push(l);
                }
                // xᵢ → g for every i
                self.clauses.push(long);
                Some(g)
            }
        }
    }
}

/// Brute-force reference decision, used only to guard [`Prop`] verdicts in
/// debug builds on instances small enough for it to be free.
#[cfg(debug_assertions)]
fn prop_brute_force_sat(p: &Prop, n_vars: usize) -> bool {
    let mut model = vec![false; n_vars];
    for bits in 0u32..(1u32 << n_vars) {
        for (i, m) in model.iter_mut().enumerate() {
            *m = bits & (1 << i) != 0;
        }
        if p.eval(&model) {
            return true;
        }
    }
    false
}

/// Decide `expr` if — and only if — it is purely propositional.
///
/// `None` means the formula is outside this fragment and the caller should try
/// another procedure.  `Some(Satisfiability::Unknown)` means it *is* in the
/// fragment but exceeded the search budget.
fn satisfiable_propositional(expr: ExprId, pool: &ExprPool) -> Option<Satisfiability> {
    let mut vars = PropVars {
        index: HashMap::new(),
        names: Vec::new(),
        nodes: 0,
    };
    let raw = prop_from_expr(expr, pool, &mut vars)?;
    let folded = prop_fold(raw);
    let n_vars = vars.names.len();

    // A formula that folds to a constant needs no search.
    if let Prop::Const(b) = folded {
        return Some(if b {
            Satisfiability::Sat(HashMap::new())
        } else {
            Satisfiability::Unsat
        });
    }

    let mut enc = Tseitin {
        clauses: Vec::new(),
        next: n_vars as u32,
    };
    let root = enc.encode(&folded)?;
    enc.clauses.push(vec![root]);
    let n_total = enc.next;

    let verdict = dpll_sat_bounded(enc.clauses, n_total, PROP_DECISION_BUDGET);

    #[cfg(debug_assertions)]
    if n_vars <= 10 && !matches!(verdict, DpllOutcome::Exhausted) {
        assert_eq!(
            matches!(verdict, DpllOutcome::Sat(_)),
            prop_brute_force_sat(&folded, n_vars),
            "DPLL disagreed with exhaustive enumeration on {folded:?}"
        );
    }

    match verdict {
        DpllOutcome::Unsat => Some(Satisfiability::Unsat),
        DpllOutcome::Exhausted => Some(Satisfiability::Unknown),
        DpllOutcome::Sat(assign) => {
            // Never hand back an unchecked model: evaluate the *original*
            // formula under it.  A Tseitin or search defect then degrades to an
            // honest `Unknown` instead of a wrong `Sat`.
            let model = &assign[..n_vars];
            if !folded.eval(model) {
                return Some(Satisfiability::Unknown);
            }
            let witness = vars
                .names
                .iter()
                .zip(model)
                .map(|(name, &v)| {
                    (
                        name.clone(),
                        if v { BOOL_TRUE } else { BOOL_FALSE }.to_string(),
                    )
                })
                .collect();
            Some(Satisfiability::Sat(witness))
        }
    }
}

/// Quantifier-free (and single-∃) satisfiability over the supported fragment.
///
/// See the [module docs](self) for exactly which fragments are decided.  The
/// two paths are disjoint and are tried in order:
///
/// 1. purely propositional (Boolean symbols and `∧ ∨ ¬`, hence also `→` and
///    `↔` written out in terms of them) — complete, via DPLL;
/// 2. one real symbol against rational constants — incomplete, via intervals.
///
/// A [`Satisfiability::Sat`] witness maps symbol name → value string, and is
/// **homogeneous**: from the propositional path every value is `"true"` or
/// `"false"`; from the interval path every value is a rational literal.  The
/// two never mix, because a formula containing both a Boolean symbol and an
/// arithmetic relation is [`Satisfiability::Unknown`].
pub fn satisfiable(expr: ExprId, pool: &ExprPool) -> Satisfiability {
    if let Some(verdict) = satisfiable_propositional(expr, pool) {
        return verdict;
    }
    let f = match formula_from_expr(expr, pool) {
        Ok(f) => f,
        Err(_) => return Satisfiability::Unknown,
    };
    let f = nnf(simplify_formula_constants(f));
    match sat_intervals(&f, pool).and_then(|m| map_to_witness(&m, pool)) {
        Ok(w) => Satisfiability::Sat(w),
        Err(SatFail::Unsat) => Satisfiability::Unsat,
        Err(SatFail::Unknown) => Satisfiability::Unknown,
    }
}

// ---------------------------------------------------------------------------
// Boolean DPLL (purely structural; exported for benchmarks / future theory plugins)
// ---------------------------------------------------------------------------

/// Literal: signed index into a fixed proposition table.
pub type BoolLit = i32;

/// Clause disjunction; empty clause = false.
pub type BoolClause = Vec<BoolLit>;

/// Verdict of a budgeted DPLL search.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum DpllOutcome {
    /// A total assignment; every clause is satisfied by it.
    Sat(Vec<bool>),
    /// Proved: no assignment satisfies every clause.
    Unsat,
    /// The decision budget ran out.  **Not a verdict** — the instance may be
    /// satisfiable or unsatisfiable, and a caller must not read this as either.
    Exhausted,
}

/// Why a branch of the search stopped.
enum DpllStop {
    /// This branch is refuted.
    Conflict,
    /// The decision budget ran out somewhere below here.
    Budget,
}

fn dpll_is_conflict(c: &BoolClause, a: &[Option<bool>]) -> bool {
    c.iter().all(|&lit| {
        let v = lit.unsigned_abs() as usize - 1;
        let sign = lit > 0;
        match a[v] {
            Some(t) => t != sign,
            None => false,
        }
    })
}

/// Unit propagation to fixpoint.  `Err(())` means a clause went false.
///
/// On `Err` the trail is left dirty; every caller that can continue searching
/// restores a snapshot first (see [`dpll_dfs`]).
fn dpll_unit_prop(clauses: &[BoolClause], a: &mut [Option<bool>]) -> Result<(), ()> {
    loop {
        let mut progressed = false;
        for cl in clauses {
            let mut unassigned: Vec<(usize, bool)> = vec![];
            let mut satisfied = false;
            for &lit in cl {
                let v = lit.unsigned_abs() as usize - 1;
                let sign = lit > 0;
                match a[v] {
                    None => unassigned.push((v, sign)),
                    Some(t) if t == sign => satisfied = true,
                    _ => {}
                }
            }
            if satisfied {
                continue;
            }
            if unassigned.is_empty() {
                return Err(());
            }
            if unassigned.len() == 1 {
                let (v, s) = unassigned[0];
                if a[v].is_none() {
                    a[v] = Some(s);
                    progressed = true;
                }
            }
        }
        if !progressed {
            break;
        }
    }
    Ok(())
}

fn dpll_dfs(
    clauses: &[BoolClause],
    a: &mut [Option<bool>],
    budget: &mut u64,
) -> Result<(), DpllStop> {
    dpll_unit_prop(clauses, a).map_err(|()| DpllStop::Conflict)?;
    for cl in clauses {
        if dpll_is_conflict(cl, a) {
            return Err(DpllStop::Conflict);
        }
    }
    let Some((i, _)) = a.iter().enumerate().find(|(_, x)| x.is_none()) else {
        return Ok(());
    };
    if *budget == 0 {
        return Err(DpllStop::Budget);
    }
    *budget -= 1;

    // Both `dpll_unit_prop` above and the recursive calls below write into `a`.
    // Undoing only `a[i]` between the two branches — which is what this used to
    // do — leaves the second branch starting from the first branch's implied
    // literals, which can refute a branch that is in fact satisfiable and so
    // report a satisfiable instance as `Unsat`.  Restore the whole trail.
    let snapshot = a.to_vec();
    for value in [false, true] {
        a[i] = Some(value);
        match dpll_dfs(clauses, a, budget) {
            Ok(()) => return Ok(()),
            Err(DpllStop::Budget) => return Err(DpllStop::Budget),
            Err(DpllStop::Conflict) => a.copy_from_slice(&snapshot),
        }
    }
    Err(DpllStop::Conflict)
}

/// DPLL with a cap on the number of decisions, so a caller can bound the search.
///
/// `max_decisions` counts *branching* decisions only; unit propagation is free.
/// Returns [`DpllOutcome::Exhausted`] rather than guessing when the cap is hit.
pub(crate) fn dpll_sat_bounded(
    clauses: Vec<BoolClause>,
    n_vars: u32,
    max_decisions: u64,
) -> DpllOutcome {
    let mut assign = vec![None; n_vars as usize];
    let mut budget = max_decisions;
    match dpll_dfs(&clauses, &mut assign, &mut budget) {
        Ok(()) => {
            let model: Vec<bool> = assign.into_iter().map(|x| x.unwrap_or(false)).collect();
            // A model is cheap to check and expensive to get wrong; a search
            // defect must not be able to leave here as a `Sat`.
            debug_assert!(
                clauses.iter().all(|cl| cl
                    .iter()
                    .any(|&lit| { model[lit.unsigned_abs() as usize - 1] == (lit > 0) })),
                "dpll_sat_bounded returned a model that falsifies a clause"
            );
            DpllOutcome::Sat(model)
        }
        Err(DpllStop::Conflict) => DpllOutcome::Unsat,
        Err(DpllStop::Budget) => DpllOutcome::Exhausted,
    }
}

/// Very small DPLL without clause learning. Returns `Some(assign)` or `None` if UNSAT.
///
/// Sound and complete for the CNF it is handed, and unbudgeted: on a hard
/// instance it runs until it finishes.  The crate-internal `dpll_sat_bounded`
/// is the variant to use where a caller
/// needs to bound the search and to tell "no model" apart from "gave up".
///
/// # Not an SMT engine (P2-3 design decision D6)
///
/// This solves the *propositional* problem it is handed — a CNF clause list over
/// opaque propositions.  [`satisfiable`] routes the **purely propositional**
/// fragment here, where the propositions are the formula's own Boolean symbols
/// and nothing is abstracted away, so the answer transfers back exactly.
///
/// It is still deliberately **not** wired into [`smtlib`] or `alkahest.smt` as a
/// fallback engine, because the only way to reach it from an *arithmetic*
/// [`Formula`] is to abstract each relation to a fresh proposition, and that
/// abstraction is sound in one direction only: it can confirm `unsat`, but it
/// reports `x > 0 ∧ x < 0` as *sat* with a meaningless model.  Handing that back
/// as a witness — under a bridge whose entire premise is that every `sat` model
/// is checked exactly — would be precisely the silent-error shape the bridge
/// exists to prevent.  `alkahest.smt.solve` therefore refuses with `E-SMT-001`
/// when no external solver is installed rather than degrading to anything
/// in-tree.
pub fn dpll_sat(clauses: Vec<BoolClause>, n_vars: u32) -> Option<Vec<bool>> {
    match dpll_sat_bounded(clauses, n_vars, u64::MAX) {
        DpllOutcome::Sat(model) => Some(model),
        DpllOutcome::Unsat => None,
        // A `u64::MAX` decision budget cannot be spent in finite time, so this
        // is unreachable.  Returning `None` here would mean reporting "gave up"
        // as "unsatisfiable", which is the one answer this must never give.
        DpllOutcome::Exhausted => {
            unreachable!("dpll_sat: the u64::MAX decision budget was exhausted")
        }
    }
}

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

    #[test]
    fn and_contradiction_unsat() {
        let p = ExprPool::new();
        let x = p.symbol("x", Domain::Real);
        let z = p.integer(0_i32);
        let f = p.pred_and(vec![p.pred_gt(x, z), p.pred_lt(x, z)]);
        assert_eq!(satisfiable(f, &p), Satisfiability::Unsat);
    }

    #[test]
    fn or_cover_sat() {
        let p = ExprPool::new();
        let x = p.symbol("x", Domain::Real);
        let z = p.integer(0_i32);
        let f = p.pred_or(vec![p.pred_gt(x, z), p.pred_le(x, z)]);
        match satisfiable(f, &p) {
            Satisfiability::Sat(m) => assert!(m.contains_key("x")),
            other => panic!("expected Sat, got {other:?}"),
        }
    }

    #[test]
    fn forall_unknown() {
        let p = ExprPool::new();
        let x = p.symbol("x", Domain::Real);
        let f = p.forall(x, p.pred_gt(x, p.integer(0_i32)));
        assert_eq!(satisfiable(f, &p), Satisfiability::Unknown);
    }

    #[test]
    fn formula_quant_round_trip() {
        let p = ExprPool::new();
        let x = p.symbol("x", Domain::Real);
        let body = p.pred_gt(x, p.integer(0_i32));
        let q = Formula::Exists {
            var: x,
            body: Box::new(formula_from_expr(body, &p).unwrap()),
        };
        let e = q.to_expr(&p);
        let back = formula_from_expr(e, &p).unwrap();
        assert_eq!(back, q);
    }

    #[test]
    fn dpll_tiny_sat() {
        // (p1 ∨ p2) ∧ (¬p1 ∨ p2)  →  p2 true
        let r = dpll_sat(vec![vec![1, 2], vec![-1, 2]], 2);
        assert!(r.is_some());
    }

    // -----------------------------------------------------------------------
    // DPLL backtracking
    // -----------------------------------------------------------------------

    /// Regression: the search used to undo only its own decision literal on
    /// backtracking, leaving the failed branch's unit-propagated literals in
    /// place.  The second branch then started from a polluted trail and could
    /// be refuted even though it was satisfiable — reporting a *satisfiable*
    /// instance as `Unsat`, which is the worst answer this code can give.
    #[test]
    fn dpll_restores_the_trail_on_backtrack() {
        // (p1 ∨ p2) ∧ (p1 ∨ ¬p2) ∧ (¬p1 ∨ ¬p2): satisfied by p1 = ⊤, p2 = ⊥.
        let clauses = vec![vec![1, 2], vec![1, -2], vec![-1, -2]];
        let model = dpll_sat(clauses.clone(), 2).expect("instance is satisfiable");
        for cl in &clauses {
            assert!(
                cl.iter()
                    .any(|&lit| model[lit.unsigned_abs() as usize - 1] == (lit > 0)),
                "clause {cl:?} falsified by {model:?}"
            );
        }
    }

    #[test]
    fn dpll_genuine_unsat_is_still_unsat() {
        // (p1) ∧ (¬p1)
        assert!(dpll_sat(vec![vec![1], vec![-1]], 1).is_none());
        // All four assignments of two variables excluded.
        let all_excluded = vec![vec![1, 2], vec![1, -2], vec![-1, 2], vec![-1, -2]];
        assert!(dpll_sat(all_excluded, 2).is_none());
    }

    #[test]
    fn dpll_bounded_reports_exhausted_rather_than_guessing() {
        // Zero decisions allowed, and the instance needs one.
        let out = dpll_sat_bounded(vec![vec![1, 2], vec![-1, -2]], 2, 0);
        assert_eq!(out, DpllOutcome::Exhausted);
        // The same instance with a budget is decidable.
        assert!(matches!(
            dpll_sat_bounded(vec![vec![1, 2], vec![-1, -2]], 2, 100),
            DpllOutcome::Sat(_)
        ));
    }

    // -----------------------------------------------------------------------
    // Propositional satisfiability through `satisfiable`
    // -----------------------------------------------------------------------

    fn bools(p: &ExprPool, names: &[&str]) -> Vec<ExprId> {
        names.iter().map(|n| p.symbol(*n, Domain::Real)).collect()
    }

    /// `a → b`, written out: the kernel has no `Implies` predicate.
    fn implies(p: &ExprPool, a: ExprId, b: ExprId) -> ExprId {
        p.pred_or(vec![p.pred_not(a), b])
    }

    /// `a ↔ b`, written out as `(a → b) ∧ (b → a)`.
    fn iff(p: &ExprPool, a: ExprId, b: ExprId) -> ExprId {
        p.pred_and(vec![implies(p, a, b), implies(p, b, a)])
    }

    fn assert_witness_is(m: &HashMap<String, String>, expect: &[(&str, bool)]) {
        for (name, want) in expect {
            let got = m
                .get(*name)
                .unwrap_or_else(|| panic!("witness has no entry for {name}: {m:?}"));
            assert_eq!(
                got,
                if *want { BOOL_TRUE } else { BOOL_FALSE },
                "witness for {name}"
            );
        }
    }

    #[test]
    fn propositional_unsat() {
        // (A ∨ B) ∧ ¬A ∧ ¬B
        let p = ExprPool::new();
        let v = bools(&p, &["A", "B"]);
        let (a, b) = (v[0], v[1]);
        let f = p.pred_and(vec![p.pred_or(vec![a, b]), p.pred_not(a), p.pred_not(b)]);
        assert_eq!(satisfiable(f, &p), Satisfiability::Unsat);
    }

    #[test]
    fn propositional_sat_gives_a_checked_witness() {
        // (A ∨ B) ∧ ¬A  →  A = ⊥, B = ⊤ is the only model.
        let p = ExprPool::new();
        let v = bools(&p, &["A", "B"]);
        let (a, b) = (v[0], v[1]);
        let f = p.pred_and(vec![p.pred_or(vec![a, b]), p.pred_not(a)]);
        match satisfiable(f, &p) {
            Satisfiability::Sat(m) => {
                assert_eq!(m.len(), 2);
                assert_witness_is(&m, &[("A", false), ("B", true)]);
            }
            other => panic!("expected Sat, got {other:?}"),
        }
    }

    #[test]
    fn propositional_contradiction_and_tautology() {
        let p = ExprPool::new();
        let a = p.symbol("A", Domain::Real);
        // A ∧ ¬A
        let contradiction = p.pred_and(vec![a, p.pred_not(a)]);
        assert_eq!(satisfiable(contradiction, &p), Satisfiability::Unsat);
        // A ∨ ¬A — satisfiable, which is all `satisfiable` claims about it.
        let tautology = p.pred_or(vec![a, p.pred_not(a)]);
        assert!(matches!(satisfiable(tautology, &p), Satisfiability::Sat(_)));
        // ¬(A ∨ ¬A) — the negation of a tautology is unsatisfiable.
        assert_eq!(
            satisfiable(p.pred_not(tautology), &p),
            Satisfiability::Unsat
        );
    }

    #[test]
    fn propositional_bare_symbol_is_a_boolean_variable() {
        let p = ExprPool::new();
        let a = p.symbol("A", Domain::Real);
        match satisfiable(a, &p) {
            Satisfiability::Sat(m) => assert_eq!(m.len(), 1),
            other => panic!("expected Sat, got {other:?}"),
        }
        assert_eq!(
            satisfiable(p.pred_not(a), &p),
            Satisfiability::Sat(HashMap::from([("A".to_string(), BOOL_FALSE.to_string())]))
        );
    }

    #[test]
    fn propositional_implication_and_biconditional() {
        let p = ExprPool::new();
        let v = bools(&p, &["A", "B"]);
        let (a, b) = (v[0], v[1]);

        // (A → B) ∧ A ∧ ¬B denies modus ponens: unsatisfiable.
        let mp = p.pred_and(vec![implies(&p, a, b), a, p.pred_not(b)]);
        assert_eq!(satisfiable(mp, &p), Satisfiability::Unsat);

        // (A → B) ∧ A forces B.
        match satisfiable(p.pred_and(vec![implies(&p, a, b), a]), &p) {
            Satisfiability::Sat(m) => assert_witness_is(&m, &[("A", true), ("B", true)]),
            other => panic!("expected Sat, got {other:?}"),
        }

        // (A ↔ B) ∧ A ∧ ¬B is unsatisfiable; (A ↔ B) ∧ ¬A forces ¬B.
        let biconditional = iff(&p, a, b);
        assert_eq!(
            satisfiable(p.pred_and(vec![biconditional, a, p.pred_not(b)]), &p),
            Satisfiability::Unsat
        );
        match satisfiable(p.pred_and(vec![biconditional, p.pred_not(a)]), &p) {
            Satisfiability::Sat(m) => assert_witness_is(&m, &[("A", false), ("B", false)]),
            other => panic!("expected Sat, got {other:?}"),
        }
    }

    #[test]
    fn propositional_constants_fold() {
        let p = ExprPool::new();
        let a = p.symbol("A", Domain::Real);
        assert_eq!(
            satisfiable(p.pred_true(), &p),
            Satisfiability::Sat(HashMap::new())
        );
        assert_eq!(satisfiable(p.pred_false(), &p), Satisfiability::Unsat);
        assert_eq!(
            satisfiable(p.pred_and(vec![a, p.pred_false()]), &p),
            Satisfiability::Unsat
        );
        assert_eq!(
            satisfiable(p.pred_or(vec![a, p.pred_true()]), &p),
            Satisfiability::Sat(HashMap::new())
        );
    }

    #[test]
    fn propositional_pigeonhole_three_into_two_is_unsat() {
        // Three pigeons, two holes: p{i}{j} = pigeon i sits in hole j.
        let p = ExprPool::new();
        let cell: Vec<Vec<ExprId>> = (0..3)
            .map(|i| {
                (0..2)
                    .map(|j| p.symbol(format!("p{i}{j}"), Domain::Real))
                    .collect()
            })
            .collect();
        let mut clauses = Vec::new();
        // Every pigeon sits somewhere.
        for row in &cell {
            clauses.push(p.pred_or(row.clone()));
        }
        // No hole holds two pigeons.
        for (i, row_i) in cell.iter().enumerate() {
            for row_k in &cell[i + 1..] {
                for (a, b) in row_i.iter().zip(row_k) {
                    clauses.push(p.pred_or(vec![p.pred_not(*a), p.pred_not(*b)]));
                }
            }
        }
        assert_eq!(satisfiable(p.pred_and(clauses), &p), Satisfiability::Unsat);
    }

    // -----------------------------------------------------------------------
    // The boundary: what stays `Unknown`
    // -----------------------------------------------------------------------

    #[test]
    fn mixed_boolean_and_arithmetic_stays_unknown() {
        // A ∧ (x > 0): a Boolean symbol and a real relation in one formula.
        // Deciding this needs a Boolean-plus-theory combination this module
        // does not have; `Unknown` is honest and must not become `Unsat`.
        let p = ExprPool::new();
        let a = p.symbol("A", Domain::Real);
        let x = p.symbol("x", Domain::Real);
        let f = p.pred_and(vec![a, p.pred_gt(x, p.integer(0_i32))]);
        assert_eq!(satisfiable(f, &p), Satisfiability::Unknown);

        // …and the mixed *contradiction* stays `Unknown` too.
        let g = p.pred_and(vec![
            a,
            p.pred_gt(x, p.integer(0_i32)),
            p.pred_lt(x, p.integer(0_i32)),
        ]);
        assert_eq!(satisfiable(g, &p), Satisfiability::Unknown);
    }

    #[test]
    fn non_predicate_term_stays_unknown() {
        // `x + 1` is a term, not a formula.
        let p = ExprPool::new();
        let x = p.symbol("x", Domain::Real);
        let t = p.add(vec![x, p.integer(1_i32)]);
        assert_eq!(satisfiable(t, &p), Satisfiability::Unknown);
    }

    #[test]
    fn quantified_propositional_stays_unknown() {
        // ∀A. A — quantification over a Boolean is outside the fragment.
        let p = ExprPool::new();
        let a = p.symbol("A", Domain::Real);
        assert_eq!(satisfiable(p.forall(a, a), &p), Satisfiability::Unknown);
    }

    #[test]
    fn same_name_different_domain_refuses_rather_than_conflating() {
        // Two distinct symbols share the name "A"; a name-keyed witness could
        // not tell them apart, so the propositional path declines.
        let p = ExprPool::new();
        let a_real = p.symbol("A", Domain::Real);
        let a_int = p.symbol("A", Domain::Integer);
        assert_ne!(a_real, a_int);
        let f = p.pred_and(vec![a_real, p.pred_not(a_int)]);
        assert_eq!(satisfiable(f, &p), Satisfiability::Unknown);
    }

    #[test]
    fn arithmetic_path_is_untouched() {
        // The single-variable interval fragment behaves as before, and its
        // witnesses are still rationals rather than "true"/"false".
        let p = ExprPool::new();
        let x = p.symbol("x", Domain::Real);
        let z = p.integer(0_i32);
        assert_eq!(
            satisfiable(p.pred_and(vec![p.pred_gt(x, z), p.pred_lt(x, z)]), &p),
            Satisfiability::Unsat
        );
        match satisfiable(p.pred_gt(x, z), &p) {
            Satisfiability::Sat(m) => {
                let v = m.get("x").expect("witness for x");
                assert_ne!(v, BOOL_TRUE);
                assert_ne!(v, BOOL_FALSE);
                assert!(
                    v.parse::<rug::Rational>().is_ok(),
                    "witness is not a rational: {v}"
                );
            }
            other => panic!("expected Sat, got {other:?}"),
        }
    }

    // -----------------------------------------------------------------------
    // Cross-check against exhaustive enumeration
    // -----------------------------------------------------------------------

    /// xorshift64, so the corpus is reproducible without a dependency.
    fn xorshift(state: &mut u64) -> u64 {
        *state ^= *state << 13;
        *state ^= *state >> 7;
        *state ^= *state << 17;
        *state
    }

    /// Build a random propositional `ExprId` over `vars`.
    fn random_prop(p: &ExprPool, vars: &[ExprId], depth: u32, rng: &mut u64) -> ExprId {
        if depth == 0 {
            return vars[(xorshift(rng) as usize) % vars.len()];
        }
        match xorshift(rng) % 5 {
            0 => vars[(xorshift(rng) as usize) % vars.len()],
            1 => p.pred_not(random_prop(p, vars, depth - 1, rng)),
            2 => {
                let k = 2 + (xorshift(rng) % 2) as usize;
                p.pred_and(
                    (0..k)
                        .map(|_| random_prop(p, vars, depth - 1, rng))
                        .collect(),
                )
            }
            3 => {
                let k = 2 + (xorshift(rng) % 2) as usize;
                p.pred_or(
                    (0..k)
                        .map(|_| random_prop(p, vars, depth - 1, rng))
                        .collect(),
                )
            }
            // An implication, written out.
            _ => {
                let a = random_prop(p, vars, depth - 1, rng);
                let b = random_prop(p, vars, depth - 1, rng);
                p.pred_or(vec![p.pred_not(a), b])
            }
        }
    }

    /// Evaluate a propositional `ExprId` directly, independently of `Prop`.
    fn eval_expr_prop(p: &ExprPool, e: ExprId, env: &HashMap<ExprId, bool>) -> bool {
        match p.get(e) {
            ExprData::Symbol { .. } => env[&e],
            ExprData::Predicate { kind, args } => match kind {
                PredicateKind::True => true,
                PredicateKind::False => false,
                PredicateKind::Not => !eval_expr_prop(p, args[0], env),
                PredicateKind::And => args.iter().all(|&a| eval_expr_prop(p, a, env)),
                PredicateKind::Or => args.iter().any(|&a| eval_expr_prop(p, a, env)),
                other => panic!("not propositional: {other:?}"),
            },
            other => panic!("not propositional: {other:?}"),
        }
    }

    /// `satisfiable` must agree with brute-force enumeration on every random
    /// propositional formula, in **both** directions.  This is the gate that
    /// catches the DPLL backtracking bug.
    #[test]
    fn propositional_agrees_with_exhaustive_enumeration() {
        let mut rng = 0x2545_F491_4F6C_DD1D_u64;
        for case in 0..400_u32 {
            let p = ExprPool::new();
            let n = 2 + (case % 4) as usize; // 2..=5 variables
            let names: Vec<String> = (0..n).map(|i| format!("v{i}")).collect();
            let vars: Vec<ExprId> = names.iter().map(|s| p.symbol(s, Domain::Real)).collect();
            let f = random_prop(&p, &vars, 3, &mut rng);

            // Ground truth by enumeration.
            let mut truth = false;
            for bits in 0_u32..(1_u32 << n) {
                let env: HashMap<ExprId, bool> = vars
                    .iter()
                    .enumerate()
                    .map(|(i, &v)| (v, bits & (1 << i) != 0))
                    .collect();
                if eval_expr_prop(&p, f, &env) {
                    truth = true;
                    break;
                }
            }

            match satisfiable(f, &p) {
                Satisfiability::Unsat => {
                    assert!(!truth, "case {case}: reported Unsat but a model exists");
                }
                Satisfiability::Sat(m) => {
                    assert!(
                        truth,
                        "case {case}: reported Sat but the formula is unsatisfiable"
                    );
                    let env: HashMap<ExprId, bool> = vars
                        .iter()
                        .zip(&names)
                        .map(|(&v, name)| (v, m.get(name).map(|s| s == BOOL_TRUE) == Some(true)))
                        .collect();
                    assert!(
                        eval_expr_prop(&p, f, &env),
                        "case {case}: witness {m:?} does not satisfy the formula"
                    );
                }
                Satisfiability::Unknown => {
                    panic!("case {case}: purely propositional formula answered Unknown")
                }
            }
        }
    }
}