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
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
//! Auto-generated module
//!
//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)

use std::collections::{HashMap, VecDeque};
use std::fmt;
use std::fmt::Write as FmtWrite;

use super::functions::*;
use crate::{BinderInfo, Expr, Level, Name};

/// Pretty printer for kernel expressions.
pub struct ExprPrinter {
    /// Output buffer
    buffer: String,
    /// Print configuration
    pub(super) config: PrintConfig,
}
impl ExprPrinter {
    /// Create a new expression printer with default config.
    pub fn new() -> Self {
        Self {
            buffer: String::new(),
            config: PrintConfig::default(),
        }
    }
    /// Create a printer with a specific configuration.
    pub fn with_config(config: PrintConfig) -> Self {
        Self {
            buffer: String::new(),
            config,
        }
    }
    /// Set whether to use unicode symbols.
    pub fn with_unicode(mut self, unicode: bool) -> Self {
        self.config.unicode = unicode;
        self
    }
    /// Get the output.
    pub fn output(self) -> String {
        self.buffer
    }
    /// Print an expression.
    pub fn print(&mut self, expr: &Expr) -> fmt::Result {
        self.print_expr(expr, 0)
    }
    fn print_expr(&mut self, expr: &Expr, prec: u32) -> fmt::Result {
        match expr {
            Expr::Sort(level) => self.print_sort(level),
            Expr::BVar(idx) => {
                if self.config.show_indices {
                    write!(self.buffer, "#{}", idx)
                } else {
                    write!(self.buffer, "_")
                }
            }
            Expr::FVar(fvar) => write!(self.buffer, "@{}", fvar.0),
            Expr::Const(name, levels) => {
                self.print_name(name)?;
                if self.config.show_universes && !levels.is_empty() {
                    write!(self.buffer, ".{{")?;
                    for (i, level) in levels.iter().enumerate() {
                        if i > 0 {
                            write!(self.buffer, ", ")?;
                        }
                        self.print_level(level)?;
                    }
                    write!(self.buffer, "}}")?;
                }
                Ok(())
            }
            Expr::App(fun, arg) => {
                let (head, args) = collect_app_args(expr);
                if args.len() > 1 {
                    if prec > 10 {
                        write!(self.buffer, "(")?;
                    }
                    self.print_expr(head, 10)?;
                    for a in &args {
                        write!(self.buffer, " ")?;
                        self.print_expr(a, 11)?;
                    }
                    if prec > 10 {
                        write!(self.buffer, ")")?;
                    }
                    Ok(())
                } else {
                    if prec > 10 {
                        write!(self.buffer, "(")?;
                    }
                    self.print_expr(fun, 10)?;
                    write!(self.buffer, " ")?;
                    self.print_expr(arg, 11)?;
                    if prec > 10 {
                        write!(self.buffer, ")")?;
                    }
                    Ok(())
                }
            }
            Expr::Lam(bi, name, ty, body) => {
                if prec > 0 {
                    write!(self.buffer, "(")?;
                }
                if self.config.unicode {
                    write!(self.buffer, "λ ")?;
                } else {
                    write!(self.buffer, "fun ")?;
                }
                self.print_binder(*bi, name, ty)?;
                let mut current_body = body.as_ref();
                while let Expr::Lam(bi2, name2, ty2, body2) = current_body {
                    write!(self.buffer, " ")?;
                    self.print_binder(*bi2, name2, ty2)?;
                    current_body = body2;
                }
                if self.config.unicode {
                    write!(self.buffer, ", ")?;
                } else {
                    write!(self.buffer, " => ")?;
                }
                self.print_expr(current_body, 0)?;
                if prec > 0 {
                    write!(self.buffer, ")")?;
                }
                Ok(())
            }
            Expr::Pi(bi, name, ty, body) => {
                if prec > 0 {
                    write!(self.buffer, "(")?;
                }
                if name.is_anonymous() || *name == Name::str("_") {
                    self.print_expr(ty, 25)?;
                    if self.config.unicode {
                        write!(self.buffer, " → ")?;
                    } else {
                        write!(self.buffer, " -> ")?;
                    }
                    self.print_expr(body, 24)?;
                } else {
                    if self.config.unicode {
                        write!(self.buffer, "∀ ")?;
                    } else {
                        write!(self.buffer, "forall ")?;
                    }
                    self.print_binder(*bi, name, ty)?;
                    let mut current_body = body.as_ref();
                    while let Expr::Pi(bi2, name2, ty2, body2) = current_body {
                        if !name2.is_anonymous() && *name2 != Name::str("_") {
                            write!(self.buffer, " ")?;
                            self.print_binder(*bi2, name2, ty2)?;
                            current_body = body2;
                        } else {
                            break;
                        }
                    }
                    write!(self.buffer, ", ")?;
                    self.print_expr(current_body, 0)?;
                }
                if prec > 0 {
                    write!(self.buffer, ")")?;
                }
                Ok(())
            }
            Expr::Let(name, ty, val, body) => {
                write!(self.buffer, "let ")?;
                self.print_name(name)?;
                write!(self.buffer, " : ")?;
                self.print_expr(ty, 0)?;
                write!(self.buffer, " := ")?;
                self.print_expr(val, 0)?;
                write!(self.buffer, " in ")?;
                self.print_expr(body, 0)
            }
            Expr::Lit(lit) => write!(self.buffer, "{}", lit),
            Expr::Proj(name, idx, expr) => {
                self.print_expr(expr, 11)?;
                write!(self.buffer, ".{}.{}", name, idx)
            }
        }
    }
    /// Print a binder with its info.
    fn print_binder(&mut self, bi: BinderInfo, name: &Name, ty: &Expr) -> fmt::Result {
        if self.config.show_binder_info {
            match bi {
                BinderInfo::Default => {
                    write!(self.buffer, "(")?;
                    self.print_name(name)?;
                    write!(self.buffer, " : ")?;
                    self.print_expr(ty, 0)?;
                    write!(self.buffer, ")")
                }
                BinderInfo::Implicit => {
                    write!(self.buffer, "{{")?;
                    self.print_name(name)?;
                    write!(self.buffer, " : ")?;
                    self.print_expr(ty, 0)?;
                    write!(self.buffer, "}}")
                }
                BinderInfo::StrictImplicit => {
                    if self.config.unicode {
                        write!(self.buffer, "\u{2983}")?;
                    } else {
                        write!(self.buffer, "{{{{")?;
                    }
                    self.print_name(name)?;
                    write!(self.buffer, " : ")?;
                    self.print_expr(ty, 0)?;
                    if self.config.unicode {
                        write!(self.buffer, "\u{2984}")
                    } else {
                        write!(self.buffer, "}}}}")
                    }
                }
                BinderInfo::InstImplicit => {
                    write!(self.buffer, "[")?;
                    self.print_name(name)?;
                    write!(self.buffer, " : ")?;
                    self.print_expr(ty, 0)?;
                    write!(self.buffer, "]")
                }
            }
        } else {
            self.print_name(name)?;
            write!(self.buffer, " : ")?;
            self.print_expr(ty, 0)
        }
    }
    /// Print a sort expression with smart level display.
    fn print_sort(&mut self, level: &Level) -> fmt::Result {
        match level {
            Level::Zero => write!(self.buffer, "Prop"),
            _ => {
                if let Some(n) = level_to_nat(level) {
                    if n == 1 {
                        write!(self.buffer, "Type")
                    } else {
                        write!(self.buffer, "Type {}", n - 1)
                    }
                } else {
                    write!(self.buffer, "Sort ")?;
                    self.print_level(level)
                }
            }
        }
    }
    pub(super) fn print_level(&mut self, level: &Level) -> fmt::Result {
        match level {
            Level::Zero => write!(self.buffer, "0"),
            Level::Succ(_) => {
                if let Some(n) = level_to_nat(level) {
                    write!(self.buffer, "{}", n)
                } else {
                    let (base, offset) = level_to_offset(level);
                    if offset > 0 {
                        self.print_level(base)?;
                        write!(self.buffer, "+{}", offset)
                    } else {
                        write!(self.buffer, "(")?;
                        self.print_level(level)?;
                        write!(self.buffer, ")")
                    }
                }
            }
            Level::Max(l1, l2) => {
                write!(self.buffer, "max(")?;
                self.print_level(l1)?;
                write!(self.buffer, ", ")?;
                self.print_level(l2)?;
                write!(self.buffer, ")")
            }
            Level::IMax(l1, l2) => {
                write!(self.buffer, "imax(")?;
                self.print_level(l1)?;
                write!(self.buffer, ", ")?;
                self.print_level(l2)?;
                write!(self.buffer, ")")
            }
            Level::Param(name) => self.print_name(name),
            Level::MVar(id) => write!(self.buffer, "?u_{}", id.0),
        }
    }
    fn print_name(&mut self, name: &Name) -> fmt::Result {
        write!(self.buffer, "{}", name)
    }
}
/// Prints an expression as an S-expression.
#[allow(dead_code)]
pub struct SExprPrinter {
    indent: usize,
}
#[allow(dead_code)]
impl SExprPrinter {
    /// Creates a new S-expression printer.
    pub fn new() -> Self {
        Self { indent: 0 }
    }
    /// Renders a name applied to arguments as `(name arg1 arg2 ...)`.
    pub fn app(&self, name: &str, args: &[&str]) -> String {
        if args.is_empty() {
            return name.to_string();
        }
        format!("({} {})", name, args.join(" "))
    }
    /// Renders a nested S-expression with indentation.
    pub fn nested_app(&self, name: &str, args: &[String]) -> String {
        if args.is_empty() {
            return name.to_string();
        }
        let pad = " ".repeat(self.indent + 2);
        let inner = args
            .iter()
            .map(|a| format!("{}{}", pad, a))
            .collect::<Vec<_>>()
            .join("\n");
        format!("({}\n{})", name, inner)
    }
    /// Renders a list.
    pub fn list(&self, items: &[String]) -> String {
        format!("[{}]", items.join(", "))
    }
}
/// A simple LRU cache backed by a linked list + hash map.
#[allow(dead_code)]
pub struct SimpleLruCache<K: std::hash::Hash + Eq + Clone, V: Clone> {
    capacity: usize,
    map: std::collections::HashMap<K, usize>,
    keys: Vec<K>,
    vals: Vec<V>,
    order: Vec<usize>,
}
#[allow(dead_code)]
impl<K: std::hash::Hash + Eq + Clone, V: Clone> SimpleLruCache<K, V> {
    /// Creates a new LRU cache with the given capacity.
    pub fn new(capacity: usize) -> Self {
        assert!(capacity > 0);
        Self {
            capacity,
            map: std::collections::HashMap::new(),
            keys: Vec::new(),
            vals: Vec::new(),
            order: Vec::new(),
        }
    }
    /// Inserts or updates a key-value pair.
    pub fn put(&mut self, key: K, val: V) {
        if let Some(&idx) = self.map.get(&key) {
            self.vals[idx] = val;
            self.order.retain(|&x| x != idx);
            self.order.insert(0, idx);
            return;
        }
        if self.keys.len() >= self.capacity {
            let evict_idx = *self
                .order
                .last()
                .expect("order list must be non-empty before eviction");
            self.map.remove(&self.keys[evict_idx]);
            self.order.pop();
            self.keys[evict_idx] = key.clone();
            self.vals[evict_idx] = val;
            self.map.insert(key, evict_idx);
            self.order.insert(0, evict_idx);
        } else {
            let idx = self.keys.len();
            self.keys.push(key.clone());
            self.vals.push(val);
            self.map.insert(key, idx);
            self.order.insert(0, idx);
        }
    }
    /// Returns a reference to the value for `key`, promoting it.
    pub fn get(&mut self, key: &K) -> Option<&V> {
        let idx = *self.map.get(key)?;
        self.order.retain(|&x| x != idx);
        self.order.insert(0, idx);
        Some(&self.vals[idx])
    }
    /// Returns the number of entries.
    pub fn len(&self) -> usize {
        self.keys.len()
    }
    /// Returns `true` if empty.
    pub fn is_empty(&self) -> bool {
        self.keys.is_empty()
    }
}
/// An abstract pretty-print document.
#[allow(dead_code)]
#[derive(Clone, Debug)]
pub enum PrettyDoc {
    /// A literal text string.
    Text(String),
    /// A newline followed by indentation.
    Newline,
    /// Increase indentation for nested content.
    Indent(Box<PrettyDoc>),
    /// Concatenation of two documents.
    Concat(Box<PrettyDoc>, Box<PrettyDoc>),
    /// A group: try to fit on one line, break if too long.
    Group(Box<PrettyDoc>),
    /// An empty document.
    Empty,
}
#[allow(dead_code)]
impl PrettyDoc {
    /// Creates a `Text` document.
    pub fn text(s: impl Into<String>) -> Self {
        PrettyDoc::Text(s.into())
    }
    /// Creates a `Concat` document.
    pub fn concat(a: PrettyDoc, b: PrettyDoc) -> Self {
        PrettyDoc::Concat(Box::new(a), Box::new(b))
    }
    /// Creates an `Indent` document.
    pub fn indent(inner: PrettyDoc) -> Self {
        PrettyDoc::Indent(Box::new(inner))
    }
    /// Creates a `Group` document.
    pub fn group(inner: PrettyDoc) -> Self {
        PrettyDoc::Group(Box::new(inner))
    }
    /// Renders the document to a `String` using the given line width and indent style.
    pub fn render(&self, _width: usize, style: IndentStyle) -> String {
        let mut out = String::new();
        let mut depth = 0usize;
        self.render_inner(&mut out, &mut depth, &style);
        out
    }
    fn render_inner(&self, out: &mut String, depth: &mut usize, style: &IndentStyle) {
        match self {
            PrettyDoc::Text(s) => out.push_str(s),
            PrettyDoc::Newline => {
                out.push('\n');
                out.push_str(&style.for_depth(*depth));
            }
            PrettyDoc::Indent(inner) => {
                *depth += 1;
                inner.render_inner(out, depth, style);
                *depth -= 1;
            }
            PrettyDoc::Concat(a, b) => {
                a.render_inner(out, depth, style);
                b.render_inner(out, depth, style);
            }
            PrettyDoc::Group(inner) => inner.render_inner(out, depth, style),
            PrettyDoc::Empty => {}
        }
    }
}
/// A pretty printer that works with `PrettyToken` streams.
#[allow(dead_code)]
pub struct TokenPrinter {
    config: PrettyConfig,
    scheme: ColorScheme,
}
#[allow(dead_code)]
impl TokenPrinter {
    /// Creates a new token printer.
    pub fn new(config: PrettyConfig) -> Self {
        Self {
            config,
            scheme: ColorScheme::MONO,
        }
    }
    /// Sets the color scheme.
    pub fn with_colors(mut self, scheme: ColorScheme) -> Self {
        self.scheme = scheme;
        self
    }
    /// Renders a slice of tokens to a `String`.
    pub fn render(&self, tokens: &[PrettyToken]) -> String {
        let mut out = String::new();
        for tok in tokens {
            if self.config.colored {
                out.push_str(&tok.colored_text(&self.scheme));
            } else {
                out.push_str(tok.raw_text());
            }
        }
        out
    }
    /// Renders with line wrapping at `config.width`.
    pub fn render_wrapped(&self, tokens: &[PrettyToken]) -> String {
        let mut out = String::new();
        let mut col = 0usize;
        for tok in tokens {
            let text = tok.raw_text();
            if matches!(tok, PrettyToken::LineBreak) {
                out.push('\n');
                col = 0;
                continue;
            }
            if col + text.len() > self.config.width && col > 0 {
                out.push('\n');
                col = 0;
            }
            out.push_str(text);
            col += text.len();
        }
        out
    }
}
/// Utilities for computing display widths.
#[allow(dead_code)]
pub struct FmtWidth;
#[allow(dead_code)]
impl FmtWidth {
    /// Returns the display width of a `usize` in decimal.
    pub fn decimal_width(n: usize) -> usize {
        if n == 0 {
            return 1;
        }
        let mut w = 0;
        let mut v = n;
        while v > 0 {
            v /= 10;
            w += 1;
        }
        w
    }
    /// Pads `s` on the right with spaces to `width` total chars.
    pub fn pad_right(s: &str, width: usize) -> String {
        if s.len() >= width {
            return s.to_string();
        }
        format!("{}{}", s, " ".repeat(width - s.len()))
    }
    /// Pads `s` on the left with spaces to `width` total chars.
    pub fn pad_left(s: &str, width: usize) -> String {
        if s.len() >= width {
            return s.to_string();
        }
        format!("{}{}", " ".repeat(width - s.len()), s)
    }
    /// Centers `s` within `width` chars.
    pub fn center(s: &str, width: usize) -> String {
        if s.len() >= width {
            return s.to_string();
        }
        let pad = width - s.len();
        let left = pad / 2;
        let right = pad - left;
        format!("{}{}{}", " ".repeat(left), s, " ".repeat(right))
    }
}
/// An indented document for pretty-printing with line wrapping.
#[allow(dead_code)]
#[derive(Clone, Debug)]
pub enum Doc {
    /// Empty document.
    Empty,
    /// A text fragment.
    Text(String),
    /// Append two documents.
    Concat(Box<Doc>, Box<Doc>),
    /// A line break; collapses to a space when flattened.
    Line,
    /// Indented document.
    Nest(usize, Box<Doc>),
    /// Choose between two layouts.
    Union(Box<Doc>, Box<Doc>),
}
#[allow(dead_code)]
impl Doc {
    /// Create a text document.
    pub fn text(s: impl Into<String>) -> Self {
        Doc::Text(s.into())
    }
    /// Append two documents.
    pub fn concat(self, other: Doc) -> Self {
        Doc::Concat(Box::new(self), Box::new(other))
    }
    /// Add a line break.
    pub fn line() -> Self {
        Doc::Line
    }
    /// Indent a document by `n` spaces.
    pub fn nest(n: usize, doc: Doc) -> Self {
        Doc::Nest(n, Box::new(doc))
    }
    /// Render to a string with the given page width.
    pub fn render(&self, width: usize) -> String {
        let mut out = String::new();
        self.render_impl(0, &mut out, width);
        out
    }
    fn render_impl(&self, indent: usize, out: &mut String, _width: usize) {
        match self {
            Doc::Empty => {}
            Doc::Text(s) => out.push_str(s),
            Doc::Concat(a, b) => {
                a.render_impl(indent, out, _width);
                b.render_impl(indent, out, _width);
            }
            Doc::Line => {
                out.push('\n');
                for _ in 0..indent {
                    out.push(' ');
                }
            }
            Doc::Nest(n, doc) => {
                doc.render_impl(indent + n, out, _width);
            }
            Doc::Union(a, _b) => {
                a.render_impl(indent, out, _width);
            }
        }
    }
}
/// A simple ASCII table formatter.
#[allow(dead_code)]
pub struct PrettyTable {
    headers: Vec<String>,
    rows: Vec<Vec<String>>,
}
#[allow(dead_code)]
impl PrettyTable {
    /// Creates a new table with the given headers.
    pub fn new(headers: Vec<String>) -> Self {
        Self {
            headers,
            rows: Vec::new(),
        }
    }
    /// Adds a row.  Pads or truncates to match the number of columns.
    pub fn add_row(&mut self, row: Vec<String>) {
        let n = self.headers.len();
        let mut r = row;
        r.resize(n, String::new());
        self.rows.push(r);
    }
    /// Renders the table as an ASCII grid.
    pub fn render(&self) -> String {
        let n = self.headers.len();
        let mut widths: Vec<usize> = self.headers.iter().map(|h| h.len()).collect();
        for row in &self.rows {
            for (i, cell) in row.iter().enumerate() {
                if i < n {
                    widths[i] = widths[i].max(cell.len());
                }
            }
        }
        let sep: String = widths
            .iter()
            .map(|&w| "-".repeat(w + 2))
            .collect::<Vec<_>>()
            .join("+");
        let sep = format!("+{}+", sep);
        let mut out = String::new();
        out.push_str(&sep);
        out.push('\n');
        let header_line: String = self
            .headers
            .iter()
            .enumerate()
            .map(|(i, h)| FmtWidth::pad_right(h, widths[i]))
            .collect::<Vec<_>>()
            .join(" | ");
        out.push_str(&format!("| {} |", header_line));
        out.push('\n');
        out.push_str(&sep);
        out.push('\n');
        for row in &self.rows {
            let line: String = (0..n)
                .map(|i| {
                    FmtWidth::pad_right(row.get(i).map(|s| s.as_str()).unwrap_or(""), widths[i])
                })
                .collect::<Vec<_>>()
                .join(" | ");
            out.push_str(&format!("| {} |", line));
            out.push('\n');
        }
        out.push_str(&sep);
        out
    }
}
/// A FIFO work queue.
#[allow(dead_code)]
pub struct WorkQueue<T> {
    items: std::collections::VecDeque<T>,
}
#[allow(dead_code)]
impl<T> WorkQueue<T> {
    /// Creates a new empty queue.
    pub fn new() -> Self {
        Self {
            items: std::collections::VecDeque::new(),
        }
    }
    /// Enqueues a work item.
    pub fn enqueue(&mut self, item: T) {
        self.items.push_back(item);
    }
    /// Dequeues the next work item.
    pub fn dequeue(&mut self) -> Option<T> {
        self.items.pop_front()
    }
    /// Returns `true` if empty.
    pub fn is_empty(&self) -> bool {
        self.items.is_empty()
    }
    /// Returns the number of pending items.
    pub fn len(&self) -> usize {
        self.items.len()
    }
}
/// A key-value store for diagnostic metadata.
#[allow(dead_code)]
pub struct DiagMeta {
    pub(super) entries: Vec<(String, String)>,
}
#[allow(dead_code)]
impl DiagMeta {
    /// Creates an empty metadata store.
    pub fn new() -> Self {
        Self {
            entries: Vec::new(),
        }
    }
    /// Adds a key-value pair.
    pub fn add(&mut self, key: impl Into<String>, val: impl Into<String>) {
        self.entries.push((key.into(), val.into()));
    }
    /// Returns the value for `key`, or `None`.
    pub fn get(&self, key: &str) -> Option<&str> {
        self.entries
            .iter()
            .find(|(k, _)| k == key)
            .map(|(_, v)| v.as_str())
    }
    /// Returns the number of entries.
    pub fn len(&self) -> usize {
        self.entries.len()
    }
    /// Returns `true` if empty.
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }
}
/// A type-safe wrapper around a `u32` identifier.
#[allow(dead_code)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TypedId<T> {
    pub(super) id: u32,
    _phantom: std::marker::PhantomData<T>,
}
#[allow(dead_code)]
impl<T> TypedId<T> {
    /// Creates a new typed ID.
    pub const fn new(id: u32) -> Self {
        Self {
            id,
            _phantom: std::marker::PhantomData,
        }
    }
    /// Returns the raw `u32` ID.
    pub fn raw(&self) -> u32 {
        self.id
    }
}
/// Mutable state threaded through a recursive pretty printer.
#[allow(dead_code)]
pub struct PrettyPrinterState {
    output: String,
    indent_depth: usize,
    indent_str: String,
    pub(crate) col: usize,
    max_width: usize,
}
#[allow(dead_code)]
impl PrettyPrinterState {
    /// Creates a new printer state.
    pub fn new(max_width: usize, indent: IndentStyle) -> Self {
        Self {
            output: String::new(),
            indent_depth: 0,
            indent_str: indent.one_level(),
            col: 0,
            max_width,
        }
    }
    /// Writes a string token.
    pub fn write(&mut self, s: &str) {
        self.output.push_str(s);
        self.col += s.len();
    }
    /// Writes a newline and current indentation.
    pub fn newline(&mut self) {
        self.output.push('\n');
        let indent = self.indent_str.repeat(self.indent_depth);
        self.output.push_str(&indent);
        self.col = indent.len();
    }
    /// Increases indentation depth.
    pub fn push_indent(&mut self) {
        self.indent_depth += 1;
    }
    /// Decreases indentation depth.
    pub fn pop_indent(&mut self) {
        if self.indent_depth > 0 {
            self.indent_depth -= 1;
        }
    }
    /// Returns `true` if the current line is past `max_width`.
    pub fn over_width(&self) -> bool {
        self.col > self.max_width
    }
    /// Returns the accumulated output.
    pub fn finish(self) -> String {
        self.output
    }
}
/// A set of non-overlapping integer intervals.
#[allow(dead_code)]
pub struct IntervalSet {
    intervals: Vec<(i64, i64)>,
}
#[allow(dead_code)]
impl IntervalSet {
    /// Creates an empty interval set.
    pub fn new() -> Self {
        Self {
            intervals: Vec::new(),
        }
    }
    /// Adds the interval `[lo, hi]` to the set.
    pub fn add(&mut self, lo: i64, hi: i64) {
        if lo > hi {
            return;
        }
        let mut new_lo = lo;
        let mut new_hi = hi;
        let mut i = 0;
        while i < self.intervals.len() {
            let (il, ih) = self.intervals[i];
            if ih < new_lo - 1 {
                i += 1;
                continue;
            }
            if il > new_hi + 1 {
                break;
            }
            new_lo = new_lo.min(il);
            new_hi = new_hi.max(ih);
            self.intervals.remove(i);
        }
        self.intervals.insert(i, (new_lo, new_hi));
    }
    /// Returns `true` if `x` is in the set.
    pub fn contains(&self, x: i64) -> bool {
        self.intervals.iter().any(|&(lo, hi)| x >= lo && x <= hi)
    }
    /// Returns the number of intervals.
    pub fn num_intervals(&self) -> usize {
        self.intervals.len()
    }
    /// Returns the total count of integers covered.
    pub fn cardinality(&self) -> i64 {
        self.intervals.iter().map(|&(lo, hi)| hi - lo + 1).sum()
    }
}
/// A counter that dispenses monotonically increasing `TypedId` values.
#[allow(dead_code)]
pub struct IdDispenser<T> {
    next: u32,
    _phantom: std::marker::PhantomData<T>,
}
#[allow(dead_code)]
impl<T> IdDispenser<T> {
    /// Creates a new dispenser starting from zero.
    pub fn new() -> Self {
        Self {
            next: 0,
            _phantom: std::marker::PhantomData,
        }
    }
    /// Dispenses the next ID.
    #[allow(clippy::should_implement_trait)]
    pub fn next(&mut self) -> TypedId<T> {
        let id = TypedId::new(self.next);
        self.next += 1;
        id
    }
    /// Returns the number of IDs dispensed.
    pub fn count(&self) -> u32 {
        self.next
    }
}
/// A key-value annotation table for arbitrary metadata.
#[allow(dead_code)]
pub struct AnnotationTable {
    map: std::collections::HashMap<String, Vec<String>>,
}
#[allow(dead_code)]
impl AnnotationTable {
    /// Creates an empty annotation table.
    pub fn new() -> Self {
        Self {
            map: std::collections::HashMap::new(),
        }
    }
    /// Adds an annotation value for the given key.
    pub fn annotate(&mut self, key: impl Into<String>, val: impl Into<String>) {
        self.map.entry(key.into()).or_default().push(val.into());
    }
    /// Returns all annotations for `key`.
    pub fn get_all(&self, key: &str) -> &[String] {
        self.map.get(key).map(|v| v.as_slice()).unwrap_or(&[])
    }
    /// Returns the number of distinct annotation keys.
    pub fn num_keys(&self) -> usize {
        self.map.len()
    }
    /// Returns `true` if the table has any annotations for `key`.
    pub fn has(&self, key: &str) -> bool {
        self.map.contains_key(key)
    }
}
/// Tracks the frequency of items.
#[allow(dead_code)]
pub struct FrequencyTable<T: std::hash::Hash + Eq + Clone> {
    counts: std::collections::HashMap<T, u64>,
}
#[allow(dead_code)]
impl<T: std::hash::Hash + Eq + Clone> FrequencyTable<T> {
    /// Creates a new empty frequency table.
    pub fn new() -> Self {
        Self {
            counts: std::collections::HashMap::new(),
        }
    }
    /// Records one occurrence of `item`.
    pub fn record(&mut self, item: T) {
        *self.counts.entry(item).or_insert(0) += 1;
    }
    /// Returns the frequency of `item`.
    pub fn freq(&self, item: &T) -> u64 {
        self.counts.get(item).copied().unwrap_or(0)
    }
    /// Returns the item with the highest frequency.
    pub fn most_frequent(&self) -> Option<(&T, u64)> {
        self.counts
            .iter()
            .max_by_key(|(_, &v)| v)
            .map(|(k, &v)| (k, v))
    }
    /// Returns the total number of recordings.
    pub fn total(&self) -> u64 {
        self.counts.values().sum()
    }
    /// Returns the number of distinct items.
    pub fn distinct(&self) -> usize {
        self.counts.len()
    }
}
/// A builder for `PrettyDoc` values with a fluent API.
#[allow(dead_code)]
pub struct DocBuilder {
    doc: PrettyDoc,
}
#[allow(dead_code)]
impl DocBuilder {
    /// Creates a builder from an existing doc.
    pub fn from(doc: PrettyDoc) -> Self {
        Self { doc }
    }
    /// Creates a text builder.
    pub fn text(s: impl Into<String>) -> Self {
        Self {
            doc: PrettyDoc::text(s),
        }
    }
    /// Creates an empty builder.
    pub fn empty() -> Self {
        Self {
            doc: PrettyDoc::Empty,
        }
    }
    /// Appends another document.
    pub fn then(self, other: PrettyDoc) -> Self {
        Self {
            doc: PrettyDoc::concat(self.doc, other),
        }
    }
    /// Appends a text string.
    pub fn then_text(self, s: impl Into<String>) -> Self {
        self.then(PrettyDoc::text(s))
    }
    /// Appends a newline.
    pub fn then_newline(self) -> Self {
        self.then(PrettyDoc::Newline)
    }
    /// Wraps in an indent.
    pub fn indented(self) -> Self {
        Self {
            doc: PrettyDoc::indent(self.doc),
        }
    }
    /// Returns the built document.
    pub fn build(self) -> PrettyDoc {
        self.doc
    }
}
/// A simple LIFO work queue.
#[allow(dead_code)]
pub struct WorkStack<T> {
    items: Vec<T>,
}
#[allow(dead_code)]
impl<T> WorkStack<T> {
    /// Creates a new empty stack.
    pub fn new() -> Self {
        Self { items: Vec::new() }
    }
    /// Pushes a work item.
    pub fn push(&mut self, item: T) {
        self.items.push(item);
    }
    /// Pops the next work item.
    pub fn pop(&mut self) -> Option<T> {
        self.items.pop()
    }
    /// Returns `true` if empty.
    pub fn is_empty(&self) -> bool {
        self.items.is_empty()
    }
    /// Returns the number of pending work items.
    pub fn len(&self) -> usize {
        self.items.len()
    }
}
/// A memoized computation slot that stores a cached value.
#[allow(dead_code)]
pub struct MemoSlot<T: Clone> {
    cached: Option<T>,
}
#[allow(dead_code)]
impl<T: Clone> MemoSlot<T> {
    /// Creates an uncomputed memo slot.
    pub fn new() -> Self {
        Self { cached: None }
    }
    /// Returns the cached value, computing it with `f` if absent.
    pub fn get_or_compute(&mut self, f: impl FnOnce() -> T) -> &T {
        if self.cached.is_none() {
            self.cached = Some(f());
        }
        self.cached
            .as_ref()
            .expect("cached value must be initialized before access")
    }
    /// Invalidates the cached value.
    pub fn invalidate(&mut self) {
        self.cached = None;
    }
    /// Returns `true` if the value has been computed.
    pub fn is_cached(&self) -> bool {
        self.cached.is_some()
    }
}
/// A counted-access cache that tracks hit and miss statistics.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct StatCache<K: std::hash::Hash + Eq + Clone, V: Clone> {
    /// The inner LRU cache.
    pub inner: SimpleLruCache<K, V>,
    /// Number of cache hits.
    pub hits: u64,
    /// Number of cache misses.
    pub misses: u64,
}
#[allow(dead_code)]
impl<K: std::hash::Hash + Eq + Clone, V: Clone> StatCache<K, V> {
    /// Creates a new stat cache with the given capacity.
    pub fn new(capacity: usize) -> Self {
        Self {
            inner: SimpleLruCache::new(capacity),
            hits: 0,
            misses: 0,
        }
    }
    /// Performs a lookup, tracking hit/miss.
    pub fn get(&mut self, key: &K) -> Option<&V> {
        let result = self.inner.get(key);
        if result.is_some() {
            self.hits += 1;
        } else {
            self.misses += 1;
        }
        None
    }
    /// Inserts a key-value pair.
    pub fn put(&mut self, key: K, val: V) {
        self.inner.put(key, val);
    }
    /// Returns the hit rate.
    pub fn hit_rate(&self) -> f64 {
        let total = self.hits + self.misses;
        if total == 0 {
            return 0.0;
        }
        self.hits as f64 / total as f64
    }
}
/// Helpers for string escaping in pretty output.
#[allow(dead_code)]
pub struct EscapeHelper;
#[allow(dead_code)]
impl EscapeHelper {
    /// Escapes `\n`, `\t`, `\\`, and `"` in `s`.
    pub fn escape_str(s: &str) -> String {
        let mut out = String::with_capacity(s.len() + 2);
        out.push('"');
        for c in s.chars() {
            match c {
                '"' => out.push_str("\\\""),
                '\\' => out.push_str("\\\\"),
                '\n' => out.push_str("\\n"),
                '\t' => out.push_str("\\t"),
                _ => out.push(c),
            }
        }
        out.push('"');
        out
    }
    /// Unescapes a string that may contain `\\n`, `\\t`, `\\\\`, `\\"`.
    pub fn unescape_str(s: &str) -> String {
        let s = if s.starts_with('"') && s.ends_with('"') && s.len() >= 2 {
            &s[1..s.len() - 1]
        } else {
            s
        };
        let mut out = String::new();
        let mut chars = s.chars().peekable();
        while let Some(c) = chars.next() {
            if c == '\\' {
                match chars.next() {
                    Some('n') => out.push('\n'),
                    Some('t') => out.push('\t'),
                    Some('\\') => out.push('\\'),
                    Some('"') => out.push('"'),
                    Some(x) => {
                        out.push('\\');
                        out.push(x);
                    }
                    None => out.push('\\'),
                }
            } else {
                out.push(c);
            }
        }
        out
    }
}
/// A simple event counter with named events.
#[allow(dead_code)]
pub struct EventCounter {
    counts: std::collections::HashMap<String, u64>,
}
#[allow(dead_code)]
impl EventCounter {
    /// Creates a new empty counter.
    pub fn new() -> Self {
        Self {
            counts: std::collections::HashMap::new(),
        }
    }
    /// Increments the counter for `event`.
    pub fn inc(&mut self, event: &str) {
        *self.counts.entry(event.to_string()).or_insert(0) += 1;
    }
    /// Adds `n` to the counter for `event`.
    pub fn add(&mut self, event: &str, n: u64) {
        *self.counts.entry(event.to_string()).or_insert(0) += n;
    }
    /// Returns the count for `event`.
    pub fn get(&self, event: &str) -> u64 {
        self.counts.get(event).copied().unwrap_or(0)
    }
    /// Returns the total count across all events.
    pub fn total(&self) -> u64 {
        self.counts.values().sum()
    }
    /// Resets all counters.
    pub fn reset(&mut self) {
        self.counts.clear();
    }
    /// Returns all event names.
    pub fn event_names(&self) -> Vec<&str> {
        self.counts.keys().map(|s| s.as_str()).collect()
    }
}
/// A bidirectional map between two types.
#[allow(dead_code)]
pub struct BiMap<A: std::hash::Hash + Eq + Clone, B: std::hash::Hash + Eq + Clone> {
    forward: std::collections::HashMap<A, B>,
    backward: std::collections::HashMap<B, A>,
}
#[allow(dead_code)]
impl<A: std::hash::Hash + Eq + Clone, B: std::hash::Hash + Eq + Clone> BiMap<A, B> {
    /// Creates an empty bidirectional map.
    pub fn new() -> Self {
        Self {
            forward: std::collections::HashMap::new(),
            backward: std::collections::HashMap::new(),
        }
    }
    /// Inserts a pair `(a, b)`.
    pub fn insert(&mut self, a: A, b: B) {
        self.forward.insert(a.clone(), b.clone());
        self.backward.insert(b, a);
    }
    /// Looks up `b` for a given `a`.
    pub fn get_b(&self, a: &A) -> Option<&B> {
        self.forward.get(a)
    }
    /// Looks up `a` for a given `b`.
    pub fn get_a(&self, b: &B) -> Option<&A> {
        self.backward.get(b)
    }
    /// Returns the number of pairs.
    pub fn len(&self) -> usize {
        self.forward.len()
    }
    /// Returns `true` if empty.
    pub fn is_empty(&self) -> bool {
        self.forward.is_empty()
    }
}
/// A slot that can hold a value, with lazy initialization.
#[allow(dead_code)]
pub struct Slot<T> {
    inner: Option<T>,
}
#[allow(dead_code)]
impl<T> Slot<T> {
    /// Creates an empty slot.
    pub fn empty() -> Self {
        Self { inner: None }
    }
    /// Fills the slot with `val`.  Panics if already filled.
    pub fn fill(&mut self, val: T) {
        assert!(self.inner.is_none(), "Slot: already filled");
        self.inner = Some(val);
    }
    /// Returns the slot's value, or `None`.
    pub fn get(&self) -> Option<&T> {
        self.inner.as_ref()
    }
    /// Returns `true` if the slot is filled.
    pub fn is_filled(&self) -> bool {
        self.inner.is_some()
    }
    /// Takes the value out of the slot.
    pub fn take(&mut self) -> Option<T> {
        self.inner.take()
    }
    /// Fills the slot if empty, returning a reference to the value.
    pub fn get_or_fill_with(&mut self, f: impl FnOnce() -> T) -> &T {
        if self.inner.is_none() {
            self.inner = Some(f());
        }
        self.inner
            .as_ref()
            .expect("inner value must be initialized before access")
    }
}
/// Interns strings to save memory (each unique string stored once).
#[allow(dead_code)]
pub struct StringInterner {
    strings: Vec<String>,
    map: std::collections::HashMap<String, u32>,
}
#[allow(dead_code)]
impl StringInterner {
    /// Creates a new string interner.
    pub fn new() -> Self {
        Self {
            strings: Vec::new(),
            map: std::collections::HashMap::new(),
        }
    }
    /// Interns `s` and returns its ID.
    pub fn intern(&mut self, s: &str) -> u32 {
        if let Some(&id) = self.map.get(s) {
            return id;
        }
        let id = self.strings.len() as u32;
        self.strings.push(s.to_string());
        self.map.insert(s.to_string(), id);
        id
    }
    /// Returns the string for `id`.
    pub fn get(&self, id: u32) -> Option<&str> {
        self.strings.get(id as usize).map(|s| s.as_str())
    }
    /// Returns the total number of interned strings.
    pub fn len(&self) -> usize {
        self.strings.len()
    }
    /// Returns `true` if empty.
    pub fn is_empty(&self) -> bool {
        self.strings.is_empty()
    }
}
/// Implements Wadler/Lindig-style pretty printing using a box model.
#[allow(dead_code)]
pub struct BoxedPrinter {
    width: usize,
}
#[allow(dead_code)]
impl BoxedPrinter {
    /// Creates a printer with the given line width.
    pub fn new(width: usize) -> Self {
        Self { width }
    }
    /// Renders a `PrettyDoc` to a `String`.
    pub fn render(&self, doc: &PrettyDoc) -> String {
        doc.render(self.width, IndentStyle::Spaces(2))
    }
}
/// Configuration for pretty printing.
#[derive(Debug, Clone)]
pub struct PrintConfig {
    /// Use unicode symbols (λ, ∀, →)
    pub unicode: bool,
    /// Show implicit arguments
    pub show_implicit: bool,
    /// Show universe levels
    pub show_universes: bool,
    /// Maximum line width for wrapping
    pub max_width: usize,
    /// Show binder info (default, implicit, etc.)
    pub show_binder_info: bool,
    /// Show de Bruijn indices
    pub show_indices: bool,
}
impl PrintConfig {
    /// ASCII-only configuration.
    pub fn ascii() -> Self {
        Self {
            unicode: false,
            ..Default::default()
        }
    }
    /// Verbose configuration showing everything.
    pub fn verbose() -> Self {
        Self {
            show_implicit: true,
            show_universes: true,
            show_binder_info: true,
            show_indices: true,
            ..Default::default()
        }
    }
}
/// Configuration for the pretty printer.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct PrettyConfig {
    /// Maximum line width.
    pub width: usize,
    /// Indentation style.
    pub indent: IndentStyle,
    /// Whether to use color.
    pub colored: bool,
}
#[allow(dead_code)]
impl PrettyConfig {
    /// Default config: 80 columns, 2-space indent, no color.
    pub fn default_config() -> Self {
        Self {
            width: 80,
            indent: IndentStyle::Spaces(2),
            colored: false,
        }
    }
    /// Creates a compact single-line config (no newlines honoured).
    pub fn compact() -> Self {
        Self {
            width: usize::MAX,
            indent: IndentStyle::Spaces(0),
            colored: false,
        }
    }
}
/// A simple sparse bit set.
#[allow(dead_code)]
pub struct SparseBitSet {
    words: Vec<u64>,
}
#[allow(dead_code)]
impl SparseBitSet {
    /// Creates a new bit set that can hold at least `capacity` bits.
    pub fn new(capacity: usize) -> Self {
        let words = (capacity + 63) / 64;
        Self {
            words: vec![0u64; words],
        }
    }
    /// Sets bit `i`.
    pub fn set(&mut self, i: usize) {
        let word = i / 64;
        let bit = i % 64;
        if word < self.words.len() {
            self.words[word] |= 1u64 << bit;
        }
    }
    /// Clears bit `i`.
    pub fn clear(&mut self, i: usize) {
        let word = i / 64;
        let bit = i % 64;
        if word < self.words.len() {
            self.words[word] &= !(1u64 << bit);
        }
    }
    /// Returns `true` if bit `i` is set.
    pub fn get(&self, i: usize) -> bool {
        let word = i / 64;
        let bit = i % 64;
        self.words.get(word).is_some_and(|w| w & (1u64 << bit) != 0)
    }
    /// Returns the number of set bits.
    pub fn count_ones(&self) -> u32 {
        self.words.iter().map(|w| w.count_ones()).sum()
    }
    /// Returns the union with another bit set.
    pub fn union(&self, other: &SparseBitSet) -> SparseBitSet {
        let len = self.words.len().max(other.words.len());
        let mut result = SparseBitSet {
            words: vec![0u64; len],
        };
        for i in 0..self.words.len() {
            result.words[i] |= self.words[i];
        }
        for i in 0..other.words.len() {
            result.words[i] |= other.words[i];
        }
        result
    }
}
/// A simple stack-based scope tracker.
#[allow(dead_code)]
pub struct ScopeStack {
    names: Vec<String>,
}
#[allow(dead_code)]
impl ScopeStack {
    /// Creates a new empty scope stack.
    pub fn new() -> Self {
        Self { names: Vec::new() }
    }
    /// Pushes a scope name.
    pub fn push(&mut self, name: impl Into<String>) {
        self.names.push(name.into());
    }
    /// Pops the current scope.
    pub fn pop(&mut self) -> Option<String> {
        self.names.pop()
    }
    /// Returns the current (innermost) scope name, or `None`.
    pub fn current(&self) -> Option<&str> {
        self.names.last().map(|s| s.as_str())
    }
    /// Returns the depth of the scope stack.
    pub fn depth(&self) -> usize {
        self.names.len()
    }
    /// Returns `true` if the stack is empty.
    pub fn is_empty(&self) -> bool {
        self.names.is_empty()
    }
    /// Returns the full path as a dot-separated string.
    pub fn path(&self) -> String {
        self.names.join(".")
    }
}
/// A monotonic timestamp in microseconds.
#[allow(dead_code)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Timestamp(u64);
#[allow(dead_code)]
impl Timestamp {
    /// Creates a timestamp from microseconds.
    pub const fn from_us(us: u64) -> Self {
        Self(us)
    }
    /// Returns the timestamp in microseconds.
    pub fn as_us(self) -> u64 {
        self.0
    }
    /// Returns the duration between two timestamps.
    pub fn elapsed_since(self, earlier: Timestamp) -> u64 {
        self.0.saturating_sub(earlier.0)
    }
}
/// A clock that measures elapsed time in a loop.
#[allow(dead_code)]
pub struct LoopClock {
    start: std::time::Instant,
    iters: u64,
}
#[allow(dead_code)]
impl LoopClock {
    /// Starts the clock.
    pub fn start() -> Self {
        Self {
            start: std::time::Instant::now(),
            iters: 0,
        }
    }
    /// Records one iteration.
    pub fn tick(&mut self) {
        self.iters += 1;
    }
    /// Returns the elapsed time in microseconds.
    pub fn elapsed_us(&self) -> f64 {
        self.start.elapsed().as_secs_f64() * 1e6
    }
    /// Returns the average microseconds per iteration.
    pub fn avg_us_per_iter(&self) -> f64 {
        if self.iters == 0 {
            return 0.0;
        }
        self.elapsed_us() / self.iters as f64
    }
    /// Returns the number of iterations.
    pub fn iters(&self) -> u64 {
        self.iters
    }
}
/// A stream of pretty-printable tokens.
#[allow(dead_code)]
#[derive(Clone, Debug)]
pub enum PrettyToken {
    /// A keyword.
    Keyword(String),
    /// An identifier.
    Ident(String),
    /// A literal (number, string, etc.).
    Literal(String),
    /// An operator or punctuation.
    Operator(String),
    /// Whitespace.
    Space,
    /// A line break.
    LineBreak,
}
#[allow(dead_code)]
impl PrettyToken {
    /// Returns the raw text of the token (no color).
    pub fn raw_text(&self) -> &str {
        match self {
            PrettyToken::Keyword(s) => s.as_str(),
            PrettyToken::Ident(s) => s.as_str(),
            PrettyToken::Literal(s) => s.as_str(),
            PrettyToken::Operator(s) => s.as_str(),
            PrettyToken::Space => " ",
            PrettyToken::LineBreak => "\n",
        }
    }
    /// Returns colored text using the given color scheme.
    pub fn colored_text(&self, cs: &ColorScheme) -> String {
        match self {
            PrettyToken::Keyword(s) => format!("{}{}{}", cs.keyword, s, cs.reset),
            PrettyToken::Ident(s) => format!("{}{}{}", cs.ident, s, cs.reset),
            PrettyToken::Literal(s) => format!("{}{}{}", cs.literal, s, cs.reset),
            PrettyToken::Operator(s) => format!("{}{}{}", cs.operator, s, cs.reset),
            PrettyToken::Space => " ".to_string(),
            PrettyToken::LineBreak => "\n".to_string(),
        }
    }
}
/// Describes the indentation style used by the pretty printer.
#[allow(dead_code)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum IndentStyle {
    /// Indent using `n` space characters.
    Spaces(usize),
    /// Indent using a single tab character.
    Tabs,
}
#[allow(dead_code)]
impl IndentStyle {
    /// Returns the indentation string for one level.
    pub fn one_level(self) -> String {
        match self {
            IndentStyle::Spaces(n) => " ".repeat(n),
            IndentStyle::Tabs => "\t".to_string(),
        }
    }
    /// Returns the indentation string for `depth` levels.
    pub fn for_depth(self, depth: usize) -> String {
        self.one_level().repeat(depth)
    }
}
/// A set of ANSI color codes for syntax-highlighted pretty printing.
#[allow(dead_code)]
pub struct ColorScheme {
    /// Color for keywords.
    pub keyword: &'static str,
    /// Color for identifiers.
    pub ident: &'static str,
    /// Color for literals.
    pub literal: &'static str,
    /// Color for operators.
    pub operator: &'static str,
    /// Reset color.
    pub reset: &'static str,
}
#[allow(dead_code)]
impl ColorScheme {
    /// The default ANSI color scheme.
    pub const DEFAULT: ColorScheme = ColorScheme {
        keyword: "\x1b[34m",
        ident: "\x1b[0m",
        literal: "\x1b[32m",
        operator: "\x1b[33m",
        reset: "\x1b[0m",
    };
    /// A monochrome (no color) scheme.
    pub const MONO: ColorScheme = ColorScheme {
        keyword: "",
        ident: "",
        literal: "",
        operator: "",
        reset: "",
    };
    /// Returns `true` if this is a colored scheme.
    pub fn is_colored(&self) -> bool {
        !self.keyword.is_empty()
    }
}