oxilean-kernel 0.1.2

OxiLean kernel - The trusted computing base for type checking
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
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
//! Auto-generated module
//!
//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)

use super::functions::AbstractDomain;

/// A reduced product of interval and parity domains.
#[allow(dead_code)]
#[derive(Debug, Clone, Copy)]
#[allow(missing_docs)]
pub struct IntervalParityProduct {
    pub interval: Interval,
    pub parity: ParityDomain,
}
#[allow(dead_code)]
impl IntervalParityProduct {
    /// Create from components.
    pub fn new(interval: Interval, parity: ParityDomain) -> Self {
        IntervalParityProduct { interval, parity }
    }
    /// Create from a concrete value.
    pub fn from_value(v: i64) -> Self {
        IntervalParityProduct {
            interval: Interval::new(v, v),
            parity: ParityDomain::from_value(v),
        }
    }
    /// Return the top element.
    pub fn top() -> Self {
        IntervalParityProduct {
            interval: Interval::top(),
            parity: ParityDomain::Top,
        }
    }
    /// Return the bottom element.
    pub fn bottom() -> Self {
        IntervalParityProduct {
            interval: Interval::bottom(),
            parity: ParityDomain::Bottom,
        }
    }
    /// Join two products.
    pub fn join(&self, other: &IntervalParityProduct) -> IntervalParityProduct {
        IntervalParityProduct {
            interval: self.interval.join(&other.interval),
            parity: self.parity.join(&other.parity),
        }
    }
    /// Add two products.
    pub fn add(&self, other: &IntervalParityProduct) -> IntervalParityProduct {
        IntervalParityProduct {
            interval: self.interval.add(&other.interval),
            parity: self.parity.add(&other.parity),
        }
    }
    /// Return whether this is bottom.
    pub fn is_bottom(&self) -> bool {
        self.interval.is_bottom() || self.parity.is_bottom()
    }
}
/// An alarm generated when abstract analysis detects a potential issue.
#[allow(dead_code)]
#[derive(Debug, Clone)]
#[allow(missing_docs)]
pub struct AbstractAlarm {
    pub label: String,
    pub message: String,
    pub severity: AlarmSeverity,
}
#[allow(dead_code)]
impl AbstractAlarm {
    /// Create an alarm.
    pub fn new(label: &str, message: &str, severity: AlarmSeverity) -> Self {
        AbstractAlarm {
            label: label.to_string(),
            message: message.to_string(),
            severity,
        }
    }
    /// Return whether this is an error-level alarm.
    pub fn is_error(&self) -> bool {
        self.severity == AlarmSeverity::Error
    }
}
/// An analysis pass configuration.
#[allow(dead_code)]
#[derive(Debug, Clone)]
#[allow(missing_docs)]
pub struct AnalysisConfig {
    pub max_iterations: u32,
    pub use_widening: bool,
    pub use_narrowing: bool,
    pub collect_alarms: bool,
    pub verbose: bool,
}
#[allow(dead_code)]
impl AnalysisConfig {
    /// Default configuration.
    pub fn default_config() -> Self {
        AnalysisConfig {
            max_iterations: 100,
            use_widening: true,
            use_narrowing: true,
            collect_alarms: true,
            verbose: false,
        }
    }
    /// Quick (fast) configuration for CI.
    pub fn fast() -> Self {
        AnalysisConfig {
            max_iterations: 10,
            use_widening: true,
            use_narrowing: false,
            collect_alarms: false,
            verbose: false,
        }
    }
}
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[allow(missing_docs)]
pub enum AlarmSeverity {
    Info,
    Warning,
    Error,
}
/// Abstract comparison result (may hold, definitely holds, definitely fails).
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(missing_docs)]
pub enum AbstractCmp {
    DefinitelyTrue,
    DefinitelyFalse,
    Unknown,
}
#[allow(dead_code)]
impl AbstractCmp {
    /// Abstract less-than comparison of two intervals.
    pub fn lt(a: &Interval, b: &Interval) -> Self {
        if a.is_bottom() || b.is_bottom() {
            return AbstractCmp::Unknown;
        }
        if a.hi < b.lo {
            AbstractCmp::DefinitelyTrue
        } else if a.lo >= b.hi {
            AbstractCmp::DefinitelyFalse
        } else {
            AbstractCmp::Unknown
        }
    }
    /// Abstract equality comparison.
    pub fn eq(a: &Interval, b: &Interval) -> Self {
        if a.is_bottom() || b.is_bottom() {
            return AbstractCmp::Unknown;
        }
        let meet = a.meet(b);
        if meet.is_bottom() {
            AbstractCmp::DefinitelyFalse
        } else if a.lo == a.hi && b.lo == b.hi && a.lo == b.lo {
            AbstractCmp::DefinitelyTrue
        } else {
            AbstractCmp::Unknown
        }
    }
    /// Return whether the comparison is definitely true.
    pub fn is_definitely_true(&self) -> bool {
        *self == AbstractCmp::DefinitelyTrue
    }
    /// Return whether the comparison is definitely false.
    pub fn is_definitely_false(&self) -> bool {
        *self == AbstractCmp::DefinitelyFalse
    }
}
/// A collection of abstract analysis results for all variables.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct AnalysisResults {
    entries: Vec<(String, IntervalParityProduct)>,
}
#[allow(dead_code)]
impl AnalysisResults {
    /// Create empty results.
    pub fn new() -> Self {
        AnalysisResults {
            entries: Vec::new(),
        }
    }
    /// Record results for a variable.
    pub fn set(&mut self, var: &str, result: IntervalParityProduct) {
        if let Some(e) = self.entries.iter_mut().find(|(v, _)| v == var) {
            e.1 = result;
        } else {
            self.entries.push((var.to_string(), result));
        }
    }
    /// Look up results for a variable.
    pub fn get(&self, var: &str) -> Option<IntervalParityProduct> {
        self.entries.iter().find(|(v, _)| v == var).map(|(_, r)| *r)
    }
    /// Return all variables and their results.
    pub fn all(&self) -> &[(String, IntervalParityProduct)] {
        &self.entries
    }
    /// Return the number of analyzed variables.
    pub fn len(&self) -> usize {
        self.entries.len()
    }
    /// Return whether any results are present.
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }
    /// Return variables where the interval is proven to be non-negative.
    pub fn proven_non_negative(&self) -> Vec<&str> {
        self.entries
            .iter()
            .filter(|(_, r)| r.interval.lo >= 0 && !r.interval.is_bottom())
            .map(|(v, _)| v.as_str())
            .collect()
    }
    /// Return variables proven to be even.
    pub fn proven_even(&self) -> Vec<&str> {
        self.entries
            .iter()
            .filter(|(_, r)| r.parity == ParityDomain::Even)
            .map(|(v, _)| v.as_str())
            .collect()
    }
}
/// Abstract domain for expression depth analysis.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DepthDomain {
    /// Maximum allowed depth.
    pub max_depth: usize,
    /// Current depth reached.
    pub current_depth: usize,
}
impl DepthDomain {
    /// Create a new depth domain with the given maximum.
    pub fn new(max: usize) -> Self {
        DepthDomain {
            max_depth: max,
            current_depth: 0,
        }
    }
    /// True if the current depth is within bounds.
    pub fn is_bounded(&self) -> bool {
        self.current_depth <= self.max_depth
    }
    /// Increase depth by one, saturating at max_depth + 1.
    pub fn increase(&self) -> DepthDomain {
        DepthDomain {
            max_depth: self.max_depth,
            current_depth: self.current_depth.saturating_add(1),
        }
    }
    /// Join (take the maximum current depth) with another DepthDomain.
    pub fn join(&self, other: &DepthDomain) -> DepthDomain {
        DepthDomain {
            max_depth: self.max_depth.max(other.max_depth),
            current_depth: self.current_depth.max(other.current_depth),
        }
    }
}
/// An abstract value in the interval domain [lo, hi].
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(missing_docs)]
pub struct Interval {
    pub lo: i64,
    pub hi: i64,
}
#[allow(dead_code)]
impl Interval {
    /// Create an interval [lo, hi].
    pub fn new(lo: i64, hi: i64) -> Self {
        Interval {
            lo: lo.min(hi),
            hi: lo.max(hi),
        }
    }
    /// Return the top interval (-∞, +∞) represented as (i64::MIN, i64::MAX).
    pub fn top() -> Self {
        Interval {
            lo: i64::MIN,
            hi: i64::MAX,
        }
    }
    /// Return the bottom interval (empty).
    pub fn bottom() -> Self {
        Interval {
            lo: i64::MAX,
            hi: i64::MIN,
        }
    }
    /// Return whether this is the bottom (empty) interval.
    pub fn is_bottom(&self) -> bool {
        self.lo > self.hi
    }
    /// Return whether this is the top interval.
    pub fn is_top(&self) -> bool {
        self.lo == i64::MIN && self.hi == i64::MAX
    }
    /// Return the join (widening step) of two intervals.
    pub fn join(&self, other: &Interval) -> Interval {
        if self.is_bottom() {
            return *other;
        }
        if other.is_bottom() {
            return *self;
        }
        Interval::new(self.lo.min(other.lo), self.hi.max(other.hi))
    }
    /// Return the meet (intersection) of two intervals.
    pub fn meet(&self, other: &Interval) -> Interval {
        let lo = self.lo.max(other.lo);
        let hi = self.hi.min(other.hi);
        if lo > hi {
            Interval::bottom()
        } else {
            Interval { lo, hi }
        }
    }
    /// Return whether this interval contains a given value.
    pub fn contains(&self, v: i64) -> bool {
        !self.is_bottom() && self.lo <= v && v <= self.hi
    }
    /// Return the width (hi - lo + 1), or 0 if bottom.
    pub fn width(&self) -> u64 {
        if self.is_bottom() {
            0
        } else {
            (self.hi - self.lo + 1) as u64
        }
    }
    /// Add two intervals.
    pub fn add(&self, other: &Interval) -> Interval {
        if self.is_bottom() || other.is_bottom() {
            return Interval::bottom();
        }
        Interval::new(
            self.lo.saturating_add(other.lo),
            self.hi.saturating_add(other.hi),
        )
    }
    /// Negate an interval.
    pub fn negate(&self) -> Interval {
        if self.is_bottom() {
            return Interval::bottom();
        }
        Interval::new(-self.hi, -self.lo)
    }
    /// Subtract two intervals.
    pub fn sub(&self, other: &Interval) -> Interval {
        self.add(&other.negate())
    }
}
/// Abstract interpreter for kernel expressions.
pub struct AbstractInterpreter {
    max_depth: usize,
}
impl AbstractInterpreter {
    /// Create a new abstract interpreter with the given maximum depth.
    pub fn new(max_depth: usize) -> Self {
        AbstractInterpreter { max_depth }
    }
    /// Analyze the nesting depth of a parenthesised expression string.
    pub fn analyze_depth(&self, expr_str: &str) -> DepthDomain {
        let mut domain = DepthDomain::new(self.max_depth);
        let mut max_seen: usize = 0;
        let mut current: usize = 0;
        for ch in expr_str.chars() {
            match ch {
                '(' | '[' | '{' => {
                    current = current.saturating_add(1);
                    if current > max_seen {
                        max_seen = current;
                    }
                }
                ')' | ']' | '}' => {
                    current = current.saturating_sub(1);
                }
                _ => {}
            }
        }
        domain.current_depth = max_seen;
        domain
    }
    /// Analyze the numeric sign of a simple expression string.
    ///
    /// Recognises leading `-` for negative and plain digits for positive.
    pub fn analyze_sign(&self, expr_str: &str) -> SignDomain {
        let trimmed = expr_str.trim();
        if trimmed.is_empty() {
            return SignDomain::Bottom;
        }
        if let Ok(n) = trimmed.parse::<i64>() {
            return match n.cmp(&0) {
                std::cmp::Ordering::Less => SignDomain::Neg,
                std::cmp::Ordering::Equal => SignDomain::Zero,
                std::cmp::Ordering::Greater => SignDomain::Pos,
            };
        }
        if trimmed.starts_with('-') {
            SignDomain::Neg
        } else if trimmed.chars().next().is_some_and(|c| c.is_ascii_digit()) {
            SignDomain::Pos
        } else {
            SignDomain::Top
        }
    }
    /// Analyze the approximate size of an expression string.
    pub fn analyze_size(&self, expr_str: &str) -> SizeDomain {
        let count = expr_str
            .split(|c: char| c.is_whitespace() || matches!(c, '(' | ')' | '[' | ']'))
            .filter(|s| !s.is_empty())
            .count();
        SizeDomain::from_count(count)
    }
    /// Compute a fixed point of `f` starting from `init`.
    ///
    /// Iterates until the value stabilises or a maximum number of steps
    /// (1 000) is reached, at which point the last value is returned.
    pub fn fixed_point<S: Clone + PartialEq, F: Fn(&S) -> S>(&self, init: S, f: F) -> S {
        const MAX_ITERS: usize = 1_000;
        let mut current = init;
        for _ in 0..MAX_ITERS {
            let next = f(&current);
            if next == current {
                return current;
            }
            current = next;
        }
        current
    }
}
/// Abstract reachability analysis: which program points are reachable?
#[allow(dead_code)]
pub struct ReachabilityAnalysis {
    reachable: Vec<String>,
    unreachable: Vec<String>,
}
#[allow(dead_code)]
impl ReachabilityAnalysis {
    /// Create an empty analysis.
    pub fn new() -> Self {
        ReachabilityAnalysis {
            reachable: Vec::new(),
            unreachable: Vec::new(),
        }
    }
    /// Mark a point as reachable.
    pub fn mark_reachable(&mut self, label: &str) {
        if !self.reachable.contains(&label.to_string()) {
            self.reachable.push(label.to_string());
        }
        self.unreachable.retain(|s| s != label);
    }
    /// Mark a point as unreachable.
    pub fn mark_unreachable(&mut self, label: &str) {
        if !self.unreachable.contains(&label.to_string()) {
            self.unreachable.push(label.to_string());
        }
        self.reachable.retain(|s| s != label);
    }
    /// Return whether a point is definitely reachable.
    pub fn is_reachable(&self, label: &str) -> bool {
        self.reachable.contains(&label.to_string())
    }
    /// Return whether a point is definitely unreachable.
    pub fn is_unreachable(&self, label: &str) -> bool {
        self.unreachable.contains(&label.to_string())
    }
    /// Return the count of reachable points.
    pub fn reachable_count(&self) -> usize {
        self.reachable.len()
    }
    /// Return the count of unreachable points.
    pub fn unreachable_count(&self) -> usize {
        self.unreachable.len()
    }
}
/// Combined abstract state for expression analysis.
#[derive(Debug, Clone)]
pub struct AbstractState {
    /// Sign information.
    pub sign: SignDomain,
    /// Depth information.
    pub depth: DepthDomain,
    /// Size information.
    pub size: SizeDomain,
}
impl AbstractState {
    /// Create an initial abstract state.
    pub fn new() -> Self {
        AbstractState {
            sign: SignDomain::Bottom,
            depth: DepthDomain::new(1024),
            size: SizeDomain::Zero,
        }
    }
    /// Join two abstract states component-wise.
    pub fn join(&self, other: &AbstractState) -> AbstractState {
        AbstractState {
            sign: self.sign.join(&other.sign),
            depth: self.depth.join(&other.depth),
            size: SizeDomain::max(self.size, other.size),
        }
    }
}
/// A database of function summaries.
#[allow(dead_code)]
pub struct SummaryDatabase {
    entries: Vec<FunctionSummary>,
}
#[allow(dead_code)]
impl SummaryDatabase {
    /// Create an empty database.
    pub fn new() -> Self {
        SummaryDatabase {
            entries: Vec::new(),
        }
    }
    /// Add a summary.
    pub fn add(&mut self, summary: FunctionSummary) {
        self.entries.push(summary);
    }
    /// Look up a summary by function name.
    pub fn find(&self, name: &str) -> Option<&FunctionSummary> {
        self.entries.iter().find(|s| s.function_name == name)
    }
    /// Return all functions with proven termination.
    pub fn proven_terminating(&self) -> Vec<&str> {
        self.entries
            .iter()
            .filter(|s| s.terminates())
            .map(|s| s.function_name.as_str())
            .collect()
    }
    /// Return the number of summaries.
    pub fn len(&self) -> usize {
        self.entries.len()
    }
    /// Return whether the database is empty.
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }
}
/// An abstract reachability domain for basic blocks.
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(missing_docs)]
pub enum BlockReachability {
    Unreachable,
    Reachable,
    Unknown,
}
#[allow(dead_code)]
impl BlockReachability {
    /// Join two reachability values.
    pub fn join(&self, other: &BlockReachability) -> BlockReachability {
        use BlockReachability::*;
        match (self, other) {
            (Unreachable, x) | (x, Unreachable) => *x,
            (Reachable, _) | (_, Reachable) => Reachable,
            _ => Unknown,
        }
    }
    /// Return whether this block may be reachable.
    pub fn may_be_reachable(&self) -> bool {
        !matches!(self, BlockReachability::Unreachable)
    }
}
/// Collects alarms during abstract analysis.
#[allow(dead_code)]
pub struct AlarmCollector {
    alarms: Vec<AbstractAlarm>,
}
#[allow(dead_code)]
impl AlarmCollector {
    /// Create an empty collector.
    pub fn new() -> Self {
        AlarmCollector { alarms: Vec::new() }
    }
    /// Add an alarm.
    pub fn add(&mut self, alarm: AbstractAlarm) {
        self.alarms.push(alarm);
    }
    /// Return all alarms.
    pub fn alarms(&self) -> &[AbstractAlarm] {
        &self.alarms
    }
    /// Return all error-level alarms.
    pub fn errors(&self) -> Vec<&AbstractAlarm> {
        self.alarms.iter().filter(|a| a.is_error()).collect()
    }
    /// Return whether there are any errors.
    pub fn has_errors(&self) -> bool {
        self.alarms.iter().any(|a| a.is_error())
    }
    /// Return the total alarm count.
    pub fn len(&self) -> usize {
        self.alarms.len()
    }
    /// Return whether no alarms were collected.
    pub fn is_empty(&self) -> bool {
        self.alarms.is_empty()
    }
    /// Clear all alarms.
    pub fn clear(&mut self) {
        self.alarms.clear();
    }
    /// Return the count of alarms at each severity.
    pub fn count_by_severity(&self) -> (usize, usize, usize) {
        let info = self
            .alarms
            .iter()
            .filter(|a| a.severity == AlarmSeverity::Info)
            .count();
        let warn = self
            .alarms
            .iter()
            .filter(|a| a.severity == AlarmSeverity::Warning)
            .count();
        let err = self
            .alarms
            .iter()
            .filter(|a| a.severity == AlarmSeverity::Error)
            .count();
        (info, warn, err)
    }
}
/// A powerset domain over a finite set of tags.
#[allow(dead_code)]
pub struct PowersetDomain {
    elements: u64,
    universe_size: u8,
}
#[allow(dead_code)]
impl PowersetDomain {
    /// Create an empty set (bottom).
    pub fn bottom(universe_size: u8) -> Self {
        PowersetDomain {
            elements: 0,
            universe_size,
        }
    }
    /// Create the full set (top).
    pub fn top(universe_size: u8) -> Self {
        let mask = if universe_size >= 64 {
            u64::MAX
        } else {
            (1u64 << universe_size) - 1
        };
        PowersetDomain {
            elements: mask,
            universe_size,
        }
    }
    /// Add an element by index.
    pub fn add(&mut self, idx: u8) {
        if idx < self.universe_size {
            self.elements |= 1 << idx;
        }
    }
    /// Remove an element by index.
    pub fn remove(&mut self, idx: u8) {
        self.elements &= !(1 << idx);
    }
    /// Return whether an element is present.
    pub fn contains(&self, idx: u8) -> bool {
        (self.elements >> idx) & 1 != 0
    }
    /// Return the join (union).
    pub fn join(&self, other: &PowersetDomain) -> PowersetDomain {
        PowersetDomain {
            elements: self.elements | other.elements,
            universe_size: self.universe_size,
        }
    }
    /// Return the meet (intersection).
    pub fn meet(&self, other: &PowersetDomain) -> PowersetDomain {
        PowersetDomain {
            elements: self.elements & other.elements,
            universe_size: self.universe_size,
        }
    }
    /// Return the number of elements.
    pub fn count(&self) -> u32 {
        self.elements.count_ones()
    }
    /// Return whether the set is empty (bottom).
    pub fn is_bottom(&self) -> bool {
        self.elements == 0
    }
    /// Return whether the set is full (top).
    pub fn is_top(&self) -> bool {
        let mask = if self.universe_size >= 64 {
            u64::MAX
        } else {
            (1u64 << self.universe_size) - 1
        };
        self.elements == mask
    }
}
/// A trace of abstract values at program points.
#[allow(dead_code)]
pub struct AbstractTrace {
    points: Vec<(String, Interval)>,
}
#[allow(dead_code)]
impl AbstractTrace {
    /// Create an empty trace.
    pub fn new() -> Self {
        AbstractTrace { points: Vec::new() }
    }
    /// Record an abstract value at a program point.
    pub fn record(&mut self, label: &str, iv: Interval) {
        self.points.push((label.to_string(), iv));
    }
    /// Return the interval at the given label (first match).
    pub fn at(&self, label: &str) -> Option<Interval> {
        self.points
            .iter()
            .find(|(l, _)| l == label)
            .map(|(_, iv)| *iv)
    }
    /// Return all trace points.
    pub fn all(&self) -> &[(String, Interval)] {
        &self.points
    }
    /// Return the number of trace points.
    pub fn len(&self) -> usize {
        self.points.len()
    }
    /// Return whether the trace is empty.
    pub fn is_empty(&self) -> bool {
        self.points.is_empty()
    }
    /// Format the trace as a readable string.
    pub fn format(&self) -> String {
        self.points
            .iter()
            .map(|(l, iv)| format!("{}: [{}, {}]", l, iv.lo, iv.hi))
            .collect::<Vec<_>>()
            .join("; ")
    }
}
/// Abstract cost model: estimated number of reduction steps.
#[allow(dead_code)]
#[derive(Debug, Clone, Copy)]
#[allow(missing_docs)]
pub struct CostBound {
    pub lower: u64,
    pub upper: Option<u64>,
}
#[allow(dead_code)]
impl CostBound {
    /// Create a tight bound.
    pub fn exact(n: u64) -> Self {
        CostBound {
            lower: n,
            upper: Some(n),
        }
    }
    /// Create an open-ended bound.
    pub fn at_least(n: u64) -> Self {
        CostBound {
            lower: n,
            upper: None,
        }
    }
    /// Create a range bound.
    pub fn range(lo: u64, hi: u64) -> Self {
        CostBound {
            lower: lo,
            upper: Some(hi),
        }
    }
    /// Return whether the cost is bounded.
    pub fn is_bounded(&self) -> bool {
        self.upper.is_some()
    }
    /// Return the cost width (hi - lo), or None if unbounded.
    pub fn width(&self) -> Option<u64> {
        self.upper.map(|h| h - self.lower)
    }
    /// Add two cost bounds.
    pub fn add(&self, other: &CostBound) -> CostBound {
        CostBound {
            lower: self.lower.saturating_add(other.lower),
            upper: match (self.upper, other.upper) {
                (Some(a), Some(b)) => Some(a.saturating_add(b)),
                _ => None,
            },
        }
    }
}
/// The congruence abstract domain: values congruent to r mod m.
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(missing_docs)]
pub struct CongruenceDomain {
    pub modulus: u64,
    pub residue: u64,
}
#[allow(dead_code)]
impl CongruenceDomain {
    /// Create a singleton value (mod=1, res=0 is "any").
    pub fn singleton(v: u64) -> Self {
        CongruenceDomain {
            modulus: 0,
            residue: v,
        }
    }
    /// Create "all values congruent to r mod m".
    pub fn congruent(modulus: u64, residue: u64) -> Self {
        let r = if modulus == 0 { 0 } else { residue % modulus };
        CongruenceDomain {
            modulus,
            residue: r,
        }
    }
    /// Create the top element (any value).
    pub fn top() -> Self {
        CongruenceDomain {
            modulus: 1,
            residue: 0,
        }
    }
    /// Create the bottom element (no values).
    pub fn bottom() -> Self {
        CongruenceDomain {
            modulus: 0,
            residue: u64::MAX,
        }
    }
    /// Return whether this is top (everything).
    pub fn is_top(&self) -> bool {
        self.modulus == 1
    }
    /// Return whether this is bottom (nothing).
    pub fn is_bottom(&self) -> bool {
        self.modulus == 0 && self.residue == u64::MAX
    }
    /// Return whether a value satisfies this congruence.
    pub fn satisfies(&self, v: u64) -> bool {
        if self.is_bottom() {
            return false;
        }
        if self.is_top() {
            return true;
        }
        if self.modulus == 0 {
            return v == self.residue;
        }
        v % self.modulus == self.residue
    }
    /// Join (GCD-based).
    pub fn join(&self, other: &CongruenceDomain) -> CongruenceDomain {
        if self.is_bottom() {
            return *other;
        }
        if other.is_bottom() {
            return *self;
        }
        if self.is_top() || other.is_top() {
            return CongruenceDomain::top();
        }
        if self.modulus == other.modulus && self.residue == other.residue {
            return *self;
        }
        CongruenceDomain::top()
    }
}
/// The three-valued logic domain (maybe, true, false).
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(missing_docs)]
pub enum TrileanDomain {
    Bottom,
    False,
    True,
    Top,
}
#[allow(dead_code)]
impl TrileanDomain {
    /// Create from a concrete bool.
    pub fn from_bool(b: bool) -> Self {
        if b {
            TrileanDomain::True
        } else {
            TrileanDomain::False
        }
    }
    /// Logical AND of two trileans.
    pub fn and(&self, other: &TrileanDomain) -> TrileanDomain {
        use TrileanDomain::*;
        match (self, other) {
            (Bottom, _) | (_, Bottom) => Bottom,
            (False, _) | (_, False) => False,
            (True, True) => True,
            _ => Top,
        }
    }
    /// Logical OR of two trileans.
    pub fn or(&self, other: &TrileanDomain) -> TrileanDomain {
        use TrileanDomain::*;
        match (self, other) {
            (Bottom, _) | (_, Bottom) => Bottom,
            (True, _) | (_, True) => True,
            (False, False) => False,
            _ => Top,
        }
    }
    /// Logical NOT.
    pub fn not(&self) -> TrileanDomain {
        use TrileanDomain::*;
        match self {
            Bottom => Bottom,
            False => True,
            True => False,
            Top => Top,
        }
    }
    /// Join two trileans.
    pub fn join(&self, other: &TrileanDomain) -> TrileanDomain {
        use TrileanDomain::*;
        match (self, other) {
            (Bottom, x) | (x, Bottom) => *x,
            (True, True) => True,
            (False, False) => False,
            _ => Top,
        }
    }
    /// Return whether this is definitely true.
    pub fn is_definitely_true(&self) -> bool {
        *self == TrileanDomain::True
    }
    /// Return whether this might be true.
    pub fn may_be_true(&self) -> bool {
        matches!(self, TrileanDomain::True | TrileanDomain::Top)
    }
}
#[allow(dead_code)]
#[derive(Debug, Clone)]
#[allow(missing_docs)]
pub enum TransferEffect {
    /// Assign a variable to an interval.
    Assign { var: String, interval: Interval },
    /// Constrain a variable to a non-bottom interval.
    Constrain { var: String, constraint: Interval },
    /// Invalidate (set to top) a variable.
    Invalidate { var: String },
}
/// Abstract sign domain for numeric expressions.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SignDomain {
    /// No information (least element).
    Bottom,
    /// Strictly negative.
    Neg,
    /// Exactly zero.
    Zero,
    /// Strictly positive.
    Pos,
    /// Non-zero (either negative or positive).
    Nonzero,
    /// Non-negative (zero or positive).
    NonNeg,
    /// Non-positive (zero or negative).
    NonPos,
    /// All values (greatest element).
    Top,
}
#[allow(clippy::should_implement_trait)]
impl SignDomain {
    /// Negate a sign value.
    pub fn negate(&self) -> SignDomain {
        use SignDomain::*;
        match self {
            Bottom => Bottom,
            Neg => Pos,
            Zero => Zero,
            Pos => Neg,
            Nonzero => Nonzero,
            NonNeg => NonPos,
            NonPos => NonNeg,
            Top => Top,
        }
    }
    /// Abstract addition of two sign values.
    pub fn add(s1: SignDomain, s2: SignDomain) -> SignDomain {
        use SignDomain::*;
        match (s1, s2) {
            (Bottom, _) | (_, Bottom) => Bottom,
            (Zero, x) | (x, Zero) => x,
            (Pos, Pos) => Pos,
            (Neg, Neg) => Neg,
            (Pos, NonNeg) | (NonNeg, Pos) => Pos,
            (Neg, NonPos) | (NonPos, Neg) => Neg,
            (NonNeg, NonNeg) => NonNeg,
            (NonPos, NonPos) => NonPos,
            _ => Top,
        }
    }
    /// Abstract multiplication of two sign values.
    pub fn mul(s1: SignDomain, s2: SignDomain) -> SignDomain {
        use SignDomain::*;
        match (s1, s2) {
            (Bottom, _) | (_, Bottom) => Bottom,
            (Zero, _) | (_, Zero) => Zero,
            (Top, _) | (_, Top) => Top,
            (Pos, Pos) | (Neg, Neg) => Pos,
            (Pos, Neg) | (Neg, Pos) => Neg,
            (NonNeg, NonNeg) => NonNeg,
            (NonPos, NonPos) => NonNeg,
            (NonNeg, NonPos) | (NonPos, NonNeg) => NonPos,
            (Pos, NonNeg) | (NonNeg, Pos) => NonNeg,
            (Neg, NonPos) | (NonPos, Neg) => NonNeg,
            (Pos, NonPos) | (NonPos, Pos) => NonPos,
            (Neg, NonNeg) | (NonNeg, Neg) => NonPos,
            _ => Top,
        }
    }
}
/// A stub abstract interpreter that runs for a fixed number of steps.
#[allow(dead_code)]
pub struct SimpleAbstractInterpreter {
    config: AnalysisConfig,
    results: AnalysisResults,
    alarms: AlarmCollector,
}
#[allow(dead_code)]
impl SimpleAbstractInterpreter {
    /// Create a new interpreter with the given config.
    pub fn new(config: AnalysisConfig) -> Self {
        SimpleAbstractInterpreter {
            config,
            results: AnalysisResults::new(),
            alarms: AlarmCollector::new(),
        }
    }
    /// Set an initial abstract value for a variable.
    pub fn init_var(&mut self, var: &str, value: IntervalParityProduct) {
        self.results.set(var, value);
    }
    /// Return a reference to current results.
    pub fn results(&self) -> &AnalysisResults {
        &self.results
    }
    /// Return a reference to collected alarms.
    pub fn alarms(&self) -> &AlarmCollector {
        &self.alarms
    }
    /// Return a summary (stub: always converges in 1 iteration).
    pub fn run_stub(&mut self) -> InterpretationSummary {
        InterpretationSummary::new(1, true, self.alarms.len())
    }
}
/// A fixpoint iteration engine for abstract interpretation.
#[allow(dead_code)]
pub struct FixpointEngine {
    max_iterations: u32,
    iterations_done: u32,
    widening_threshold: u32,
}
#[allow(dead_code)]
impl FixpointEngine {
    /// Create an engine with a given maximum iteration count.
    pub fn new(max_iterations: u32) -> Self {
        FixpointEngine {
            max_iterations,
            iterations_done: 0,
            widening_threshold: max_iterations / 2,
        }
    }
    /// Return whether fixpoint was reached (next == current).
    pub fn is_fixpoint(current: &IntervalEnv, next: &IntervalEnv) -> bool {
        current.is_at_least_as_wide(next) && next.is_at_least_as_wide(current)
    }
    /// Return whether we should apply widening (past the widening threshold).
    pub fn should_widen(&self) -> bool {
        self.iterations_done >= self.widening_threshold
    }
    /// Record one iteration step.
    pub fn step(&mut self) {
        self.iterations_done += 1;
    }
    /// Return whether we've exceeded the iteration limit.
    pub fn is_exhausted(&self) -> bool {
        self.iterations_done >= self.max_iterations
    }
    /// Return the iteration count so far.
    pub fn iterations(&self) -> u32 {
        self.iterations_done
    }
    /// Reset to initial state.
    pub fn reset(&mut self) {
        self.iterations_done = 0;
    }
}
/// The parity abstract domain: even, odd, or top/bottom.
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(missing_docs)]
pub enum ParityDomain {
    Bottom,
    Even,
    Odd,
    Top,
}
#[allow(dead_code)]
impl ParityDomain {
    /// Create from a concrete integer.
    pub fn from_value(v: i64) -> Self {
        if v % 2 == 0 {
            ParityDomain::Even
        } else {
            ParityDomain::Odd
        }
    }
    /// Join two parity values.
    pub fn join(&self, other: &ParityDomain) -> ParityDomain {
        use ParityDomain::*;
        match (self, other) {
            (Bottom, x) | (x, Bottom) => *x,
            (Even, Even) => Even,
            (Odd, Odd) => Odd,
            _ => Top,
        }
    }
    /// Meet two parity values.
    pub fn meet(&self, other: &ParityDomain) -> ParityDomain {
        use ParityDomain::*;
        match (self, other) {
            (Top, x) | (x, Top) => *x,
            (Even, Even) => Even,
            (Odd, Odd) => Odd,
            _ => Bottom,
        }
    }
    /// Add two parity values.
    pub fn add(&self, other: &ParityDomain) -> ParityDomain {
        use ParityDomain::*;
        match (self, other) {
            (Bottom, _) | (_, Bottom) => Bottom,
            (Top, _) | (_, Top) => Top,
            (Even, x) | (x, Even) => *x,
            (Odd, Odd) => Even,
        }
    }
    /// Multiply two parity values.
    pub fn mul(&self, other: &ParityDomain) -> ParityDomain {
        use ParityDomain::*;
        match (self, other) {
            (Bottom, _) | (_, Bottom) => Bottom,
            (Even, _) | (_, Even) => Even,
            (Odd, Odd) => Odd,
            _ => Top,
        }
    }
    /// Return whether this is bottom.
    pub fn is_bottom(&self) -> bool {
        *self == ParityDomain::Bottom
    }
    /// Return whether this is top.
    pub fn is_top(&self) -> bool {
        *self == ParityDomain::Top
    }
}
/// A node in the call graph (function definition).
#[allow(dead_code)]
#[derive(Debug, Clone)]
#[allow(missing_docs)]
pub struct CallGraphNode {
    pub name: String,
    pub is_recursive: bool,
    pub callees: Vec<String>,
}
#[allow(dead_code)]
impl CallGraphNode {
    /// Create a new call graph node.
    pub fn new(name: impl Into<String>) -> Self {
        CallGraphNode {
            name: name.into(),
            is_recursive: false,
            callees: Vec::new(),
        }
    }
    /// Add a callee.
    pub fn add_callee(&mut self, name: &str) {
        if name == self.name.as_str() {
            self.is_recursive = true;
        }
        if !self.callees.iter().any(|s| s == name) {
            self.callees.push(name.to_string());
        }
    }
    /// Return whether this node calls `name`.
    pub fn calls(&self, name: &str) -> bool {
        self.callees.iter().any(|s| s == name)
    }
}
/// A product abstract domain combining two domains.
#[allow(dead_code)]
#[allow(missing_docs)]
#[derive(Debug, Clone, Copy)]
pub struct ProductDomain<A: Copy, B: Copy> {
    pub first: A,
    pub second: B,
}
#[allow(dead_code)]
impl<A: Copy, B: Copy> ProductDomain<A, B> {
    /// Create a product domain.
    pub fn new(first: A, second: B) -> Self {
        ProductDomain { first, second }
    }
}
/// A simple chaotic iteration fixpoint solver.
#[allow(dead_code)]
pub struct ChaoticIterator {
    max_steps: u32,
    current_step: u32,
    converged: bool,
}
#[allow(dead_code)]
impl ChaoticIterator {
    /// Create an iterator with a step limit.
    pub fn new(max_steps: u32) -> Self {
        ChaoticIterator {
            max_steps,
            current_step: 0,
            converged: false,
        }
    }
    /// Mark as converged.
    pub fn mark_converged(&mut self) {
        self.converged = true;
    }
    /// Advance one step. Returns false if limit exceeded.
    pub fn advance(&mut self) -> bool {
        if self.current_step >= self.max_steps {
            return false;
        }
        self.current_step += 1;
        true
    }
    /// Return whether the iterator has converged.
    pub fn is_converged(&self) -> bool {
        self.converged
    }
    /// Return whether the step limit was exceeded without convergence.
    pub fn is_limit_exceeded(&self) -> bool {
        !self.converged && self.current_step >= self.max_steps
    }
    /// Return the number of steps taken.
    pub fn steps(&self) -> u32 {
        self.current_step
    }
    /// Reset to initial state.
    pub fn reset(&mut self) {
        self.current_step = 0;
        self.converged = false;
    }
}
/// A call graph for program analysis.
#[allow(dead_code)]
pub struct CallGraph {
    nodes: Vec<CallGraphNode>,
}
#[allow(dead_code)]
impl CallGraph {
    /// Create an empty call graph.
    pub fn new() -> Self {
        CallGraph { nodes: Vec::new() }
    }
    /// Add a node.
    pub fn add_node(&mut self, node: CallGraphNode) {
        self.nodes.push(node);
    }
    /// Look up a node by name.
    pub fn find(&self, name: &str) -> Option<&CallGraphNode> {
        self.nodes.iter().find(|n| n.name == name)
    }
    /// Return all recursive functions.
    pub fn recursive_fns(&self) -> Vec<&str> {
        self.nodes
            .iter()
            .filter(|n| n.is_recursive)
            .map(|n| n.name.as_str())
            .collect()
    }
    /// Return callers of a given function.
    pub fn callers_of(&self, name: &str) -> Vec<&str> {
        self.nodes
            .iter()
            .filter(|n| n.calls(name))
            .map(|n| n.name.as_str())
            .collect()
    }
    /// Return the number of nodes.
    pub fn len(&self) -> usize {
        self.nodes.len()
    }
    /// Return whether the graph is empty.
    pub fn is_empty(&self) -> bool {
        self.nodes.is_empty()
    }
    /// Return all function names.
    pub fn function_names(&self) -> Vec<&str> {
        self.nodes.iter().map(|n| n.name.as_str()).collect()
    }
}
/// Abstract termination evidence: witness for why a function terminates.
#[allow(dead_code)]
#[derive(Debug, Clone)]
#[allow(missing_docs)]
pub enum TerminationEvidence {
    /// Structural recursion on argument `idx`.
    Structural { arg_index: u32 },
    /// Lexicographic tuple ordering.
    Lexicographic { measures: Vec<String> },
    /// Unknown; may not terminate.
    Unknown,
}
#[allow(dead_code)]
impl TerminationEvidence {
    /// Return whether termination is proven.
    pub fn is_proven(&self) -> bool {
        !matches!(self, TerminationEvidence::Unknown)
    }
    /// Format a description.
    pub fn describe(&self) -> String {
        match self {
            TerminationEvidence::Structural { arg_index } => {
                format!("structural on arg #{}", arg_index)
            }
            TerminationEvidence::Lexicographic { measures } => {
                format!("lexicographic on [{}]", measures.join(", "))
            }
            TerminationEvidence::Unknown => "unknown".to_string(),
        }
    }
}
/// An abstract environment mapping variable names to interval values.
#[allow(dead_code)]
pub struct IntervalEnv {
    pub(super) bindings: Vec<(String, Interval)>,
}
#[allow(dead_code)]
impl IntervalEnv {
    /// Create an empty environment.
    pub fn new() -> Self {
        IntervalEnv {
            bindings: Vec::new(),
        }
    }
    /// Bind a variable to an interval.
    pub fn set(&mut self, name: &str, iv: Interval) {
        if let Some(b) = self.bindings.iter_mut().find(|(n, _)| n == name) {
            b.1 = iv;
        } else {
            self.bindings.push((name.to_string(), iv));
        }
    }
    /// Look up a variable's interval (returns Top if not found).
    pub fn get(&self, name: &str) -> Interval {
        self.bindings
            .iter()
            .find(|(n, _)| n == name)
            .map(|(_, iv)| *iv)
            .unwrap_or(Interval::top())
    }
    /// Join two environments point-wise.
    pub fn join(&self, other: &IntervalEnv) -> IntervalEnv {
        let mut result = IntervalEnv::new();
        for (name, iv) in &self.bindings {
            let iv2 = other.get(name);
            result.set(name, iv.join(&iv2));
        }
        for (name, iv) in &other.bindings {
            if self.bindings.iter().all(|(n, _)| n != name) {
                result.set(name, *iv);
            }
        }
        result
    }
    /// Return whether this environment is "wider" than or equal to `other`.
    pub fn is_at_least_as_wide(&self, other: &IntervalEnv) -> bool {
        other.bindings.iter().all(|(name, iv2)| {
            let iv1 = self.get(name);
            iv1.lo <= iv2.lo && iv1.hi >= iv2.hi
        })
    }
    /// Return the number of variables bound.
    pub fn len(&self) -> usize {
        self.bindings.len()
    }
    /// Return whether the environment is empty.
    pub fn is_empty(&self) -> bool {
        self.bindings.is_empty()
    }
    /// Return all variable names.
    pub fn names(&self) -> Vec<&str> {
        self.bindings.iter().map(|(n, _)| n.as_str()).collect()
    }
}
/// Abstract domain for expression size analysis.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SizeDomain {
    /// No information.
    Bottom,
    /// Exactly zero nodes.
    Zero,
    /// A small, known number of nodes.
    Small(usize),
    /// Large but finite (unknown exact count).
    Large,
    /// Unknown or unbounded size.
    Top,
}
#[allow(clippy::should_implement_trait)]
impl SizeDomain {
    /// Construct from a concrete node count.
    pub fn from_count(n: usize) -> Self {
        const SMALL_THRESHOLD: usize = 100;
        if n == 0 {
            SizeDomain::Zero
        } else if n <= SMALL_THRESHOLD {
            SizeDomain::Small(n)
        } else {
            SizeDomain::Large
        }
    }
    /// Abstract addition of two sizes.
    pub fn add(a: SizeDomain, b: SizeDomain) -> SizeDomain {
        use SizeDomain::*;
        match (a, b) {
            (Bottom, _) | (_, Bottom) => Bottom,
            (Top, _) | (_, Top) => Top,
            (Zero, x) | (x, Zero) => x,
            (Small(m), Small(n)) => SizeDomain::from_count(m + n),
            (Large, _) | (_, Large) => Large,
        }
    }
    /// Abstract maximum of two sizes.
    pub fn max(a: SizeDomain, b: SizeDomain) -> SizeDomain {
        use SizeDomain::*;
        match (a, b) {
            (Bottom, x) | (x, Bottom) => x,
            (Top, _) | (_, Top) => Top,
            (Zero, x) | (x, Zero) => x,
            (Small(m), Small(n)) => Small(m.max(n)),
            (Large, _) | (_, Large) => Large,
        }
    }
}
/// A summary of an abstract interpretation run.
#[allow(dead_code)]
#[derive(Debug, Clone)]
#[allow(missing_docs)]
pub struct InterpretationSummary {
    pub iterations: u32,
    pub converged: bool,
    pub alarm_count: usize,
    pub proven_safe: bool,
}
#[allow(dead_code)]
impl InterpretationSummary {
    /// Create a new summary.
    pub fn new(iterations: u32, converged: bool, alarm_count: usize) -> Self {
        InterpretationSummary {
            iterations,
            converged,
            alarm_count,
            proven_safe: converged && alarm_count == 0,
        }
    }
    /// Format a brief description.
    pub fn describe(&self) -> String {
        format!(
            "iters={} converged={} alarms={} safe={}",
            self.iterations, self.converged, self.alarm_count, self.proven_safe
        )
    }
}
/// A transfer function maps an abstract state at a point to the next point.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct TransferFunction {
    pub name: String,
    pub effects: Vec<TransferEffect>,
}
#[allow(dead_code)]
impl TransferFunction {
    /// Create an empty transfer function.
    pub fn new(name: impl Into<String>) -> Self {
        TransferFunction {
            name: name.into(),
            effects: Vec::new(),
        }
    }
    /// Add an effect.
    pub fn add_effect(&mut self, effect: TransferEffect) {
        self.effects.push(effect);
    }
    /// Apply the transfer function to an environment.
    pub fn apply(&self, env: &IntervalEnv) -> IntervalEnv {
        let result_bindings = env.bindings.clone();
        let mut result = IntervalEnv {
            bindings: result_bindings,
        };
        for effect in &self.effects {
            match effect {
                TransferEffect::Assign { var, interval } => {
                    result.set(var, *interval);
                }
                TransferEffect::Constrain { var, constraint } => {
                    let current = result.get(var);
                    let narrowed = current.meet(constraint);
                    result.set(var, narrowed);
                }
                TransferEffect::Invalidate { var } => {
                    result.set(var, Interval::top());
                }
            }
        }
        result
    }
    /// Return the number of effects.
    pub fn effect_count(&self) -> usize {
        self.effects.len()
    }
}
/// A function summary: pre/post conditions as interval environments.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct FunctionSummary {
    pub function_name: String,
    pub precondition: IntervalEnv,
    pub postcondition: IntervalEnv,
    pub termination: TerminationEvidence,
    pub cost: CostBound,
}
#[allow(dead_code)]
impl FunctionSummary {
    /// Create a new summary.
    pub fn new(name: impl Into<String>) -> Self {
        FunctionSummary {
            function_name: name.into(),
            precondition: IntervalEnv::new(),
            postcondition: IntervalEnv::new(),
            termination: TerminationEvidence::Unknown,
            cost: CostBound::at_least(0),
        }
    }
    /// Return whether the function is proven to terminate.
    pub fn terminates(&self) -> bool {
        self.termination.is_proven()
    }
    /// Return a description of the summary.
    pub fn describe(&self) -> String {
        format!(
            "fn {}: terminates={} cost=[{}, {:?}]",
            self.function_name,
            self.terminates(),
            self.cost.lower,
            self.cost.upper
        )
    }
}
/// The nullness abstract domain: null, non-null, or top.
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(missing_docs)]
pub enum NullnessDomain {
    Bottom,
    Null,
    NonNull,
    Top,
}
#[allow(dead_code)]
impl NullnessDomain {
    /// Create from a bool (true = non-null, false = null).
    pub fn from_bool(non_null: bool) -> Self {
        if non_null {
            NullnessDomain::NonNull
        } else {
            NullnessDomain::Null
        }
    }
    /// Join two values.
    pub fn join(&self, other: &NullnessDomain) -> NullnessDomain {
        use NullnessDomain::*;
        match (self, other) {
            (Bottom, x) | (x, Bottom) => *x,
            (Null, Null) => Null,
            (NonNull, NonNull) => NonNull,
            _ => Top,
        }
    }
    /// Return whether this might be null.
    pub fn may_be_null(&self) -> bool {
        matches!(self, NullnessDomain::Null | NullnessDomain::Top)
    }
    /// Return whether this is definitely non-null.
    pub fn is_definitely_non_null(&self) -> bool {
        *self == NullnessDomain::NonNull
    }
}