decl-lang 0.3.0

Decl — a declarative language for describing, generating, and validating structured data: the `decl` CLI (check / evaluate / validate / fmt) and the `decl-lsp` language server, implemented natively in Rust
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
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
//! Expression-level static analysis — a port of the reference
//! implementation's infer.ts: type inference, assignability (§3.18,
//! strict S ⊑ T), the absence discipline (§4.10) with its two narrowing
//! rules, and the `match` static checks (§4.7). Inference is
//! conservative: a form whose type cannot be determined yields `unknown`
//! (rt None) and suppresses downstream judgments rather than guessing.
use crate::ast::*;
use crate::semantics::*;
use crate::subsume::subsumes;
use num_bigint::BigInt;
use std::cell::RefCell;
use std::collections::{HashMap, HashSet};
use std::rc::Rc;

#[derive(Clone)]
pub struct Ty {
    pub rt: Option<RT>,
    pub abs: bool,
}
pub fn unk() -> Ty {
    Ty {
        rt: None,
        abs: false,
    }
}
pub fn tyv(rt: Option<RT>) -> Ty {
    Ty { rt, abs: false }
}
pub fn prim(name: &str) -> RT {
    ty(RTk::Prim(name.to_string()))
}
fn bool_ty() -> Ty {
    tyv(Some(prim("bool")))
}

pub type Report = Rc<dyn Fn(&str, String)>;

#[derive(Clone)]
pub struct Ctx {
    pub env: Rc<Env>,
    pub report: Report,
    pub vars: HashMap<String, Ty>,
    pub present: HashSet<String>,
    pub nonnull: HashSet<String>,
    pub const_memo: Rc<RefCell<HashMap<String, Ty>>>,
    /// the expression under inference (shared by child contexts): what a report is anchored to
    pub pos: Rc<RefCell<Option<Rc<Expr>>>>,
    /// Phase 6 foundations: every inferred node's type, and every name's resolution (the language server's tables)
    pub record: Option<Rc<dyn Fn(&Rc<Expr>, &Ty)>>,
    pub resolve_hook: Option<Rc<dyn Fn(&Rc<Expr>, Option<Target>)>>,
}

/// what a name denotes: the declaration behind it, imports followed to their module
#[derive(Clone)]
pub struct Target {
    pub kind: &'static str, // var | const | func | output | input | namespace | type | diagnostic | export
    pub env: Option<Rc<Env>>,
    pub name: String,
}
pub fn resolve_name(cx: &Ctx, name: &str) -> Option<Target> {
    if cx.vars.contains_key(name) {
        return Some(Target {
            kind: "var",
            env: None,
            name: name.to_string(),
        });
    }
    resolve_in(&cx.env, name)
}
pub fn resolve_in(env: &Rc<Env>, name: &str) -> Option<Target> {
    let t = |kind: &'static str| {
        Some(Target {
            kind,
            env: Some(env.clone()),
            name: name.to_string(),
        })
    };
    if env.consts.borrow().contains_key(name) {
        return t("const");
    }
    if env.funcs.borrow().contains_key(name) {
        return t("func");
    }
    if env.outputs.borrow().iter().any(|(o, _, _)| o == name) {
        return t("output");
    }
    if env.inputs.borrow().contains_key(name) {
        return t("input");
    }
    let im = env.imports.borrow().get(name).cloned();
    if let Some(im) = im {
        return resolve_in(&im.env, &im.name).or(Some(Target {
            kind: "export",
            env: Some(im.env.clone()),
            name: im.name.clone(),
        }));
    }
    if env.namespaces.borrow().contains_key(name) {
        return t("namespace");
    }
    if env.type_asts.borrow().contains_key(name) {
        return t("type");
    }
    if env.diags.borrow().contains_key(name) {
        return t("diagnostic");
    }
    None
}
impl Ctx {
    pub fn report(&self, code: &str, msg: String) {
        (self.report)(code, msg)
    }
    pub fn child(&self) -> Ctx {
        self.clone()
    }
    pub fn with_env(&self, env: Rc<Env>) -> Ctx {
        let mut c = self.clone();
        c.env = env;
        c
    }
}
pub fn make_ctx(env: Rc<Env>, report: Report) -> Ctx {
    Ctx {
        env,
        report,
        vars: HashMap::new(),
        present: HashSet::new(),
        nonnull: HashSet::new(),
        const_memo: Rc::new(RefCell::new(HashMap::new())),
        pos: Rc::new(RefCell::new(None)),
        record: None,
        resolve_hook: None,
    }
}

// ---------------- JS-faithful helpers ----------------
pub fn js_typeof(v: &Value) -> &'static str {
    match v {
        Value::Bool(_) => "boolean",
        Value::Int(_) => "bigint",
        Value::Float(_) => "number",
        Value::Str(_) => "string",
        _ => "object",
    }
}
pub fn js_str(v: &Value) -> String {
    match v {
        Value::Null => "null".into(),
        Value::Bool(b) => {
            if *b {
                "true".into()
            } else {
                "false".into()
            }
        }
        Value::Int(i) => i.to_string(),
        Value::Float(f) => js_num_str(*f),
        Value::Str(s) => s.clone(),
        other => format!("{other:?}"),
    }
}
pub fn tag(rt: &RT) -> &'static str {
    match &rt.k {
        RTk::Prim(_) => "prim",
        RTk::Lit(_) => "lit",
        RTk::Range { .. } => "range",
        RTk::Pattern { .. } => "pattern",
        RTk::Arr { .. } => "arr",
        RTk::Map { .. } => "map",
        RTk::Union(_) => "union",
        RTk::IsectN(_) => "isectN",
        RTk::Rec(_) => "rec",
        RTk::Pred { .. } => "pred",
        RTk::Ref(_) => "ref",
        RTk::Quantity(_) => "quantity",
        RTk::Func { .. } => "func",
        RTk::Any => "any",
    }
}
/// a function's unknown result is carried as `any`
fn ret_of(rt: &RT) -> Option<RT> {
    if matches!(rt.k, RTk::Any) {
        None
    } else {
        Some(rt.clone())
    }
}
pub fn member_ty(m: &Member) -> Option<RT> {
    match &m.conj {
        Some(c) => Some(ty(RTk::IsectN(c.clone()))),
        None => m.ty.clone(),
    }
}
fn find_member<'a>(members: &'a [Member], name: &str) -> Option<&'a Member> {
    members.iter().find(|m| m.name == name)
}

// ---------------- type utilities ----------------
fn is_null_lit(t: &RT) -> bool {
    matches!(&t.k, RTk::Lit(Value::Null)) || matches!(&t.k, RTk::Prim(n) if n == "null")
}
pub fn has_null(rt: Option<&RT>) -> bool {
    match rt {
        None => false,
        Some(t) => {
            is_null_lit(t)
                || matches!(&t.k, RTk::Union(arms) if arms.iter().any(|a| has_null(Some(a))))
        }
    }
}
fn strip_null(rt: &RT) -> RT {
    if let RTk::Union(arms) = &rt.k {
        let kept: Vec<RT> = arms.iter().filter(|a| !is_null_lit(a)).cloned().collect();
        return if kept.len() == 1 {
            kept[0].clone()
        } else {
            ty(RTk::Union(kept))
        };
    }
    rt.clone()
}
fn same_rt(a: &RT, b: &RT) -> bool {
    if Rc::ptr_eq(a, b) {
        return true;
    }
    match (&a.k, &b.k) {
        (RTk::Prim(x), RTk::Prim(y)) => x == y,
        (RTk::Lit(x), RTk::Lit(y)) => js_typeof(x) == js_typeof(y) && value_eq(x, y),
        _ => false,
    }
}
pub fn mk_union(arms: Vec<Option<RT>>) -> Option<RT> {
    if arms.iter().any(|a| a.is_none()) {
        return None;
    }
    let mut flat: Vec<RT> = vec![];
    for a in arms.into_iter().flatten() {
        match &a.k {
            RTk::Union(xs) => flat.extend(xs.iter().cloned()),
            _ => flat.push(a),
        }
    }
    let mut uniq: Vec<RT> = vec![];
    for a in flat {
        if !uniq.iter().any(|b| same_rt(&a, b)) {
            uniq.push(a);
        }
    }
    Some(if uniq.len() == 1 {
        uniq[0].clone()
    } else {
        ty(RTk::Union(uniq))
    })
}
pub fn num_kind(rt: Option<&RT>) -> Option<String> {
    let rt = rt?;
    match &rt.k {
        RTk::Prim(n) => {
            if ["int", "float", "string", "bool"].contains(&n.as_str()) {
                Some(n.clone())
            } else {
                None
            }
        }
        RTk::Lit(v) => match v {
            Value::Bool(_) => Some("bool".into()),
            Value::Int(_) => Some("int".into()),
            Value::Float(_) => Some("float".into()),
            Value::Str(_) => Some("string".into()),
            _ => None,
        },
        RTk::Range { base, .. } => Some(base.clone()),
        RTk::Pattern { .. } => Some("string".into()),
        RTk::Pred { base, .. } => num_kind(Some(base)),
        RTk::Quantity(_) => Some("quantity".into()),
        RTk::Union(arms) => {
            let ks: Vec<Option<String>> = arms.iter().map(|a| num_kind(Some(a))).collect();
            match ks.first() {
                Some(Some(k0)) if ks.iter().all(|k| k.as_ref() == Some(k0)) => Some(k0.clone()),
                _ => None,
            }
        }
        _ => None,
    }
}
fn is_boolish(rt: Option<&RT>) -> bool {
    rt.is_none() || num_kind(rt).as_deref() == Some("bool")
}
/// structural view: unwrap ref/pred and select the arm of an intersection
/// that has the wanted shape (merged `&` members carry conj arms)
fn arm_of(rt: Option<&RT>, t: &str) -> Option<RT> {
    let rt = rt?;
    match &rt.k {
        RTk::Ref(target) if t != "ref" => return arm_of(Some(target), t),
        RTk::Pred { base, .. } => return arm_of(Some(base), t),
        _ => {}
    }
    if tag(rt) == t {
        return Some(rt.clone());
    }
    if let RTk::IsectN(arms) = &rt.k {
        for x in arms {
            if let Some(v) = arm_of(Some(x), t) {
                return Some(v);
            }
        }
    }
    if let RTk::Union(arms) = &rt.k {
        let sub: Vec<RT> = arms.iter().filter_map(|x| arm_of(Some(x), t)).collect();
        if sub.len() == arms.len() && !sub.is_empty() {
            if t == "arr" {
                let elems: Vec<RT> = sub
                    .iter()
                    .filter_map(|a| {
                        if let RTk::Arr { elem, .. } = &a.k {
                            Some(elem.clone())
                        } else {
                            None
                        }
                    })
                    .collect();
                return Some(ty(RTk::Arr {
                    elem: ty(RTk::Union(elems)),
                    lo: None,
                    hi: None,
                }));
            }
            return Some(sub[0].clone());
        }
    }
    None
}

// ---------------- navigation paths & narrowing ----------------
pub fn path_key(e: &Expr) -> Option<String> {
    match e {
        Expr::Name(n) => Some(n.clone()),
        Expr::Ctx(n) => Some(n.clone()),
        Expr::Paren(x) => path_key(x),
        Expr::Member { x, name, safe } => {
            if *safe {
                return None;
            }
            path_key(x).map(|b| format!("{b}.{name}"))
        }
        Expr::Index { x, i } => {
            let b = path_key(x)?;
            match &**i {
                Expr::Lit(v) => Some(format!("{b}[{}]", js_str(v))),
                Expr::Name(n) => Some(format!("{b}[{n}]")),
                _ => None,
            }
        }
        _ => None,
    }
}
#[derive(Default)]
pub struct Guards {
    pub present: Vec<String>,
    pub nonnull: Vec<String>,
}
fn merge(mut a: Guards, b: Guards) -> Guards {
    a.present.extend(b.present);
    a.nonnull.extend(b.nonnull);
    a
}
pub fn guards_of(e: &Expr, polarity: bool) -> Guards {
    match e {
        Expr::Paren(x) => guards_of(x, polarity),
        Expr::Un { op, x } => {
            if op == "!" {
                guards_of(x, !polarity)
            } else {
                Guards::default()
            }
        }
        Expr::Bin { op, l, r } => {
            if op == "&&" && polarity {
                return merge(guards_of(l, true), guards_of(r, true));
            }
            if op == "||" && !polarity {
                return merge(guards_of(l, false), guards_of(r, false));
            }
            if op == "in" && polarity {
                let Some(b) = path_key(r) else {
                    return Guards::default();
                };
                return match &**l {
                    Expr::Lit(Value::Str(s)) => Guards {
                        present: vec![format!("{b}.{s}"), format!("{b}[{s}]")],
                        nonnull: vec![],
                    },
                    Expr::Name(n) => Guards {
                        present: vec![format!("{b}[{n}]")],
                        nonnull: vec![],
                    },
                    _ => Guards::default(),
                };
            }
            let null_side = if matches!(&**l, Expr::Lit(Value::Null)) {
                Some(r)
            } else if matches!(&**r, Expr::Lit(Value::Null)) {
                Some(l)
            } else {
                None
            };
            if let Some(side) = null_side {
                if let Some(p) = path_key(side) {
                    if (op == "!=" && polarity) || (op == "==" && !polarity) {
                        return Guards {
                            present: vec![],
                            nonnull: vec![p],
                        };
                    }
                }
            }
            Guards::default()
        }
        _ => Guards::default(),
    }
}
/// is a name already taken here? (locals or the module namespace — the
/// no-shadowing rule E3019 spans both)
fn name_bound(cx: &Ctx, n: &str) -> bool {
    cx.vars.contains_key(n)
        || cx.env.consts.borrow().contains_key(n)
        || cx.env.funcs.borrow().contains_key(n)
        || cx.env.type_asts.borrow().contains_key(n)
        || cx.env.inputs.borrow().contains_key(n)
        || cx.env.outputs.borrow().iter().any(|(o, _, _)| o == n)
}
pub fn apply_guards(cx: &Ctx, g: Guards) -> Ctx {
    let mut c2 = cx.child();
    c2.present.extend(g.present);
    c2.nonnull.extend(g.nonnull);
    c2
}

// ---------------- stdlib signatures (arity + result) ----------------
/// the return type of a std function, by shape
#[derive(Clone, Copy)]
pub enum StdRet {
    Unknown,
    Int,
    Bool,
    Str,
    Float,
    ArrStr,
    PredFn,
}
/// the std functions (§13.1: names not listed do not exist): name, arity, return
pub const STD: &[(&str, usize, StdRet)] = &[
    ("array.count", 1, StdRet::Int),
    ("array.all", 2, StdRet::Bool),
    ("array.any", 2, StdRet::Bool),
    ("array.filter", 2, StdRet::Unknown),
    ("array.all_distinct", 1, StdRet::Bool),
    ("array.sum", 1, StdRet::Unknown),
    ("array.fold", 3, StdRet::Unknown),
    ("map.keys", 1, StdRet::ArrStr),
    ("map.values", 1, StdRet::Unknown),
    ("string.length", 1, StdRet::Int),
    ("string.of", 1, StdRet::Str),
    ("string.join", 2, StdRet::Str),
    ("string.starts_with", 2, StdRet::Bool),
    ("string.ends_with", 2, StdRet::Bool),
    ("string.contains", 2, StdRet::Bool),
    ("string.split", 2, StdRet::ArrStr),
    ("map.entries", 1, StdRet::Unknown),
    ("ref.path", 1, StdRet::Str),
    ("math.abs", 1, StdRet::Unknown),
    ("math.min", 2, StdRet::Unknown),
    ("math.max", 2, StdRet::Unknown),
    ("math.clog2", 1, StdRet::Int),
    ("math.floor", 1, StdRet::Int),
    ("math.ceil", 1, StdRet::Int),
    ("math.round", 1, StdRet::Int),
    ("int.of", 1, StdRet::Int),
    ("int.at_least", 1, StdRet::PredFn),
    ("int.at_most", 1, StdRet::PredFn),
    ("float.of", 1, StdRet::Float),
    ("object.merge", 2, StdRet::Unknown),
];
/// the std function names, in table order (completion)
pub fn std_names() -> impl Iterator<Item = &'static str> {
    STD.iter().map(|e| e.0)
}
fn std_sig(name: &str) -> Option<(usize, Option<RT>)> {
    let e = STD.iter().find(|e| e.0 == name)?;
    let ret = match e.2 {
        StdRet::Unknown => None,
        StdRet::Int => Some(prim("int")),
        StdRet::Bool => Some(prim("bool")),
        StdRet::Str => Some(prim("string")),
        StdRet::Float => Some(prim("float")),
        StdRet::ArrStr => Some(ty(RTk::Arr {
            elem: prim("string"),
            lo: None,
            hi: None,
        })),
        StdRet::PredFn => Some(ty(RTk::Func {
            params: vec![prim("int")],
            ret: prim("bool"),
        })),
    };
    Some((e.1, ret))
}

// ---------------- type text ----------------
// the static type as inference sees it, spelled in the language's own
// type syntax where it has one (`:type` in the REPL, hover in the editor)
pub fn type_text(rt: Option<&RT>) -> String {
    let Some(rt) = rt else {
        return "unknown".into();
    };
    let lit = |v: &Value| match v {
        Value::Str(s) => crate::semantics::json_str(s),
        other => js_str(other),
    };
    let is_null_arm = |a: &RT| {
        matches!(&a.k, RTk::Prim(n) if n == "null") || matches!(&a.k, RTk::Lit(Value::Null))
    };
    match &rt.k {
        RTk::Any => "any".into(),
        RTk::Prim(n) => n.clone(),
        RTk::Lit(v) => lit(v),
        RTk::Range { lo, hi, excl, .. } => {
            format!("{}..{}{}", lit(lo), if *excl { "<" } else { "" }, lit(hi))
        }
        RTk::Pattern { src, .. } => format!("/{src}/"),
        RTk::Quantity(dim) => format!("quantity<{dim}>"),
        RTk::Ref(t) => format!("ref<{}>", type_text(Some(t))),
        RTk::Map { key, val } => format!("map<{}, {}>", type_text(Some(key)), type_text(Some(val))),
        RTk::Arr { elem, lo, hi } => {
            let b = if lo.is_some() || hi.is_some() {
                format!(
                    "[{}..{}]",
                    lo.map(|x| x.to_string()).unwrap_or_default(),
                    hi.map(|x| x.to_string()).unwrap_or_default()
                )
            } else {
                "[]".into()
            };
            let e = type_text(Some(elem));
            // a compound element type is parenthesized (`(1 | 2)[]`, `(1..8)[]`), as the grammar's paren_type spells it
            let wrap = matches!(
                &elem.k,
                RTk::Union(_) | RTk::Func { .. } | RTk::Pred { .. } | RTk::Range { .. }
            );
            format!("{}{b}", if wrap { format!("({e})") } else { e })
        }
        RTk::Union(arms) => {
            let nn: Vec<&RT> = arms.iter().filter(|a| !is_null_arm(a)).collect();
            if nn.len() + 1 == arms.len() && nn.len() == 1 {
                return format!("{}?", type_text(Some(nn[0])));
            }
            arms.iter()
                .map(|a| type_text(Some(a)))
                .collect::<Vec<_>>()
                .join(" | ")
        }
        RTk::Pred { base, .. } => format!("{} where …", type_text(Some(base))),
        RTk::Func { params, ret } => format!(
            "({}) => {}",
            params
                .iter()
                .map(|p| type_text(Some(p)))
                .collect::<Vec<_>>()
                .join(", "),
            type_text(Some(ret))
        ),
        RTk::Rec(r) => {
            if let Some(n) = rt.name.borrow().as_ref() {
                if !n.starts_with('{') {
                    return n.clone();
                }
            }
            let ms: Vec<String> = r
                .members
                .borrow()
                .iter()
                .map(|m| {
                    format!(
                        "{}{}{}{}",
                        m.name,
                        if m.kind == MKind::Opt { "?" } else { "" },
                        m.ty.as_ref()
                            .map(|t| format!(": {}", type_text(Some(t))))
                            .unwrap_or_default(),
                        if matches!(m.kind, MKind::Der | MKind::Dflt) {
                            " = …"
                        } else {
                            ""
                        }
                    )
                })
                .collect();
            let open = if r.open.get() {
                if ms.is_empty() {
                    "..."
                } else {
                    ", ..."
                }
            } else {
                ""
            };
            format!("{{ {}{open} }}", ms.join(", "))
        }
        RTk::IsectN(_) => "?".into(),
    }
}
pub fn std_path(e: &Expr) -> Option<String> {
    match e {
        Expr::Member {
            x,
            name,
            safe: false,
        } => {
            let b = std_path(x)?;
            Some(if b.is_empty() {
                name.clone()
            } else {
                format!("{b}.{name}")
            })
        }
        Expr::Name(n) if n == "std" => Some(String::new()),
        _ => None,
    }
}

// ---------------- the judgment ----------------
pub fn try_resolve(env: &Rc<Env>, ast: Option<&TypeAst>) -> Option<RT> {
    env.resolve(ast?, None).ok()
}
pub fn named(name: &str) -> TypeAst {
    TypeAst::Named {
        name: name.to_string(),
        args: vec![],
        preds: None,
        ext: None,
        loc: None,
    }
}

pub fn require_val(cx: &Ctx, e: &Expr, ty: Ty, what: &str) -> Ty {
    if ty.abs {
        let k = path_key(e);
        if !k.map(|k| cx.present.contains(&k)).unwrap_or(false) {
            cx.report(
                "E4050",
                format!("maybe-absent expression consumed {what} (use ?. / ?? or an `in` guard)"),
            );
        }
    }
    ty
}

pub fn infer(cx: &Ctx, e: &Rc<Expr>) -> Ty {
    let prev = cx.pos.replace(Some(e.clone()));
    let t = infer0(cx, e);
    if let Some(r) = &cx.record {
        r(e, &t);
    }
    if let Some(h) = &cx.resolve_hook {
        if let Expr::Name(n) = &**e {
            h(e, resolve_name(cx, n));
        }
    }
    *cx.pos.borrow_mut() = prev;
    t
}
fn infer0(cx: &Ctx, e: &Rc<Expr>) -> Ty {
    match &**e {
        Expr::Lit(v) => tyv(Some(ty(RTk::Lit(v.clone())))),
        Expr::Pattern(src) => match pattern_error(src) {
            Some(bad) => {
                cx.report("E4119", format!("malformed pattern /{src}/: {bad}"));
                unk()
            }
            None => match compile_pattern(src) {
                Ok(re) => tyv(Some(ty(RTk::Pattern {
                    src: src.clone(),
                    re,
                }))),
                Err(_) => unk(),
            },
        },
        Expr::UnitLit { unit, .. } => match cx.env.unit_info(unit) {
            Ok((key, _)) => tyv(Some(ty(RTk::Quantity(key)))),
            Err(msg) => {
                cx.report("E4073", msg);
                unk()
            }
        },
        Expr::Template(parts) => {
            for p in parts {
                if let TPart::Expr(x) = p {
                    require_val(cx, x, infer(cx, x), "in a template");
                }
            }
            tyv(Some(prim("string")))
        }
        Expr::Name(name) => {
            if let Some(t) = cx.vars.get(name) {
                return t.clone();
            }
            let env = &cx.env;
            if env.consts.borrow().contains_key(name) {
                return const_ty(cx, name);
            }
            if env.funcs.borrow().contains_key(name) {
                return tyv(Some(func_rt(cx, name)));
            }
            if name == "std" {
                return unk();
            }
            let out = env
                .outputs
                .borrow()
                .iter()
                .find(|(o, _, _)| o == name)
                .map(|(_, t, _)| t.clone());
            if let Some(t) = out {
                return tyv(try_resolve(env, Some(&t)));
            }
            let inp = env.inputs.borrow().get(name).map(|(t, _)| t.clone());
            if let Some(t) = inp {
                return tyv(try_resolve(env, Some(&t)));
            }
            let im = env.imports.borrow().get(name).cloned();
            if let Some(im) = im {
                return imported_ty(cx, &im);
            }
            if env.namespaces.borrow().contains_key(name) {
                cx.report("E3008", format!("namespace name {name} used as a value"));
                return unk();
            }
            if env.type_asts.borrow().contains_key(name) {
                cx.report(
                    "E3008",
                    format!("type/namespace name {name} used as a value"),
                );
                return unk();
            }
            cx.report("E3003", format!("unknown name {name}"));
            unk()
        }
        Expr::Ctx(n) => cx.vars.get(n).cloned().unwrap_or_else(unk),
        Expr::Referrers { ty: tn, .. } => {
            let rt = try_resolve(&cx.env, Some(&named(tn)));
            match &rt {
                None => cx.report("E4091", format!("$referrers: unknown record type {tn}")),
                Some(r) if !is_rec(r) => {
                    cx.report("E4091", format!("$referrers: {tn} is not a record type"))
                }
                _ => {}
            }
            tyv(rt.filter(is_rec).map(|r| {
                ty(RTk::Arr {
                    elem: ty(RTk::Ref(r)),
                    lo: None,
                    hi: None,
                })
            }))
        }
        Expr::Obj(entries) => {
            for (_, v) in entries {
                require_val(cx, v, infer(cx, v), "as a construction member");
            }
            unk() // literals are typed by their checked position (§3.18)
        }
        Expr::Arr(items) => {
            let ts: Vec<Option<RT>> = items
                .iter()
                .map(|(spread, x)| {
                    let t = require_val(cx, x, infer(cx, x), "as an array element");
                    if *spread {
                        t.rt.and_then(|r| {
                            if let RTk::Arr { elem, .. } = &r.k {
                                Some(elem.clone())
                            } else {
                                None
                            }
                        })
                    } else {
                        t.rt
                    }
                })
                .collect();
            tyv(mk_union(ts).map(|elem| {
                ty(RTk::Arr {
                    elem,
                    lo: None,
                    hi: None,
                })
            }))
        }
        Expr::Comp { head, clauses } => {
            let c2 = bind_clauses(cx, clauses);
            let h = require_val(&c2, head, infer(&c2, head), "as a comprehension element");
            tyv(h.rt.map(|elem| {
                ty(RTk::Arr {
                    elem,
                    lo: None,
                    hi: None,
                })
            }))
        }
        Expr::MapComp { key, val, clauses } => {
            let c2 = bind_clauses(cx, clauses);
            let k = require_val(&c2, key, infer(&c2, key), "as a map key");
            if k.rt.is_some() && num_kind(k.rt.as_ref()).as_deref() != Some("string") {
                cx.report("E4001", "map-comprehension key is not a string".into());
            }
            let v = require_val(&c2, val, infer(&c2, val), "as a map value");
            tyv(v.rt.map(|val| {
                ty(RTk::Map {
                    key: prim("string"),
                    val,
                })
            }))
        }
        Expr::Bin { .. } => infer_bin(cx, e),
        Expr::Un { op, x } => {
            let t = require_val(cx, x, infer(cx, x), &format!("as `{op}` operand"));
            if op == "!" {
                if t.rt.is_some() && !is_boolish(t.rt.as_ref()) {
                    cx.report("E4071", "`!` on a non-bool operand".into());
                }
                return bool_ty();
            }
            if op == "~" {
                if t.rt.is_some() && num_kind(t.rt.as_ref()).as_deref() != Some("int") {
                    cx.report("E4071", "`~` on a non-int operand".into());
                }
                return tyv(Some(prim("int")));
            }
            let k = num_kind(t.rt.as_ref());
            if t.rt.is_some()
                && !matches!(k.as_deref(), Some("int") | Some("float") | Some("quantity"))
            {
                cx.report("E4071", "unary `-` on a non-numeric operand".into());
            }
            tyv(match k.as_deref() {
                Some("int") => Some(prim("int")),
                Some("float") => Some(prim("float")),
                _ => None,
            })
        }
        Expr::Paren(x) => infer(cx, x),
        Expr::If { c, t, f } => {
            let ct = require_val(cx, c, infer(cx, c), "as a condition");
            if ct.rt.is_some() && !is_boolish(ct.rt.as_ref()) {
                cx.report("E4001", "`if` condition is not bool".into());
            }
            let tt = infer(&apply_guards(cx, guards_of(c, true)), t);
            let ft = infer(&apply_guards(cx, guards_of(c, false)), f);
            Ty {
                rt: mk_union(vec![tt.rt, ft.rt]),
                abs: tt.abs || ft.abs,
            }
        }
        Expr::Lambda { params, body } => {
            let mut c2 = cx.child();
            for p in params {
                if name_bound(&c2, p) {
                    cx.report(
                        "E3019",
                        format!("lambda parameter {p} shadows an enclosing name"),
                    );
                }
                c2.vars.insert(p.clone(), unk());
            }
            infer(&c2, body);
            unk()
        }
        Expr::Call { .. } => infer_call(cx, e),
        Expr::Member { .. } => infer_member(cx, e),
        Expr::Index { x, .. } => {
            let b = require_val(cx, x, infer(cx, x), "for indexing");
            index_core(cx, b, e)
        }
        Expr::With { base, patch } => {
            let b = require_val(cx, base, infer(cx, base), "as `with` base");
            let brt = match &b.rt {
                Some(r) => match &r.k {
                    RTk::Ref(t) => Some(t.clone()),
                    _ => Some(r.clone()),
                },
                None => None,
            };
            if let Some(r) = &brt {
                if !is_rec(r) {
                    cx.report("E4080", "`with` on a non-record base".into());
                    return unk();
                }
            }
            if let (Expr::Obj(entries), Some(r)) = (&**patch, &brt) {
                let RTk::Rec(rec) = &r.k else { unreachable!() };
                let members = rec.members.borrow();
                for (k, _) in entries {
                    match find_member(&members, k) {
                        None if !rec.open.get() => {
                            cx.report("E4080", format!("`with` updates unknown member {k}"))
                        }
                        Some(m) if m.kind == MKind::Der => {
                            cx.report("E4080", format!("`with` updates derived member {k}"))
                        }
                        _ => {}
                    }
                }
            }
            if let Expr::Obj(entries) = &**patch {
                for (_, v) in entries {
                    require_val(cx, v, infer(cx, v), "as a `with` update");
                }
            } else {
                infer(cx, patch);
            }
            tyv(brt)
        }
        Expr::Match { .. } => infer_match(cx, e, None),
    }
}

fn bind_clauses(cx: &Ctx, clauses: &[ForClause]) -> Ctx {
    let mut c2 = cx.child();
    for cl in clauses {
        let vt = iter_var_ty(&c2, &cl.iter);
        if name_bound(&c2, &cl.v) {
            cx.report(
                "E3019",
                format!("comprehension variable {} shadows an enclosing name", cl.v),
            );
        }
        c2.vars.insert(cl.v.clone(), vt);
        for f in &cl.filters {
            require_val(&c2, f, infer(&c2, f), "as a filter");
            c2 = apply_guards(&c2, guards_of(f, true));
        }
    }
    c2
}

fn iter_var_ty(cx: &Ctx, it: &Rc<Expr>) -> Ty {
    let t = require_val(cx, it, infer(cx, it), "as an iterable");
    if let Expr::Bin { op, l, r } = &**it {
        if op == ".." || op == "..<" {
            let lo = if let Expr::Lit(v) = &**l {
                Some(v.clone())
            } else {
                None
            };
            let hi = if let Expr::Lit(v) = &**r {
                Some(v.clone())
            } else {
                None
            };
            if matches!(lo, Some(Value::Float(_))) || matches!(hi, Some(Value::Float(_))) {
                cx.report("E4115", "comprehension over a float range".into());
                return unk();
            }
            if let (Some(lo), Some(hi)) = (lo, hi) {
                return tyv(Some(ty(RTk::Range {
                    lo,
                    hi,
                    excl: op == "..<",
                    base: "int".into(),
                })));
            }
            return tyv(Some(prim("int")));
        }
    }
    let Some(rt) = &t.rt else { return unk() };
    if let Some(a) = arm_of(Some(rt), "arr") {
        if let RTk::Arr { elem, .. } = &a.k {
            return tyv(Some(elem.clone()));
        }
    }
    let what = if arm_of(Some(rt), "map").is_some() {
        "map (use std.map.keys/values)"
    } else {
        "value"
    };
    cx.report("E4115", format!("comprehension over a non-iterable {what}"));
    unk()
}

fn q_dim(rt: Option<&RT>) -> Option<String> {
    match rt.map(|r| &r.k) {
        Some(RTk::Quantity(d)) => Some(d.clone()),
        Some(RTk::Pred { base, .. }) => match &base.k {
            RTk::Quantity(d) => Some(d.clone()),
            _ => None,
        },
        _ => None,
    }
}

fn infer_bin(cx: &Ctx, e: &Rc<Expr>) -> Ty {
    let Expr::Bin { op, l, r } = &**e else {
        return unk();
    };
    let op = op.as_str();
    if op == "|>" {
        // first-argument insertion (§4.9)
        let call = match &**r {
            Expr::Call { fun, args } => {
                let mut a = vec![l.clone()];
                a.extend(args.iter().cloned());
                Expr::Call {
                    fun: fun.clone(),
                    args: a,
                }
            }
            _ => Expr::Call {
                fun: r.clone(),
                args: vec![l.clone()],
            },
        };
        return infer_call(cx, &Rc::new(call));
    }
    if op == "??" {
        let lt = infer(cx, l); // absence/null on the left is the point
        let rt = require_val(cx, r, infer(cx, r), "as `??` fallback");
        return tyv(match (lt.rt, rt.rt) {
            (Some(a), Some(b)) => mk_union(vec![Some(strip_null(&a)), Some(b)]),
            _ => None,
        });
    }
    if op == "&&" || op == "||" {
        let lt = require_val(cx, l, infer(cx, l), &format!("as `{op}` operand"));
        if lt.rt.is_some() && !is_boolish(lt.rt.as_ref()) {
            cx.report("E4071", format!("`{op}` on a non-bool operand"));
        }
        let c2 = apply_guards(cx, guards_of(l, op == "&&"));
        let rt = require_val(&c2, r, infer(&c2, r), &format!("as `{op}` operand"));
        if rt.rt.is_some() && !is_boolish(rt.rt.as_ref()) {
            cx.report("E4071", format!("`{op}` on a non-bool operand"));
        }
        return bool_ty();
    }
    if op == "in" {
        require_val(cx, l, infer(cx, l), "as `in` key");
        let rt = require_val(cx, r, infer(cx, r), "as `in` container");
        let rrt = rt.rt.map(|x| match &x.k {
            RTk::Ref(t) => t.clone(),
            _ => x,
        });
        if let (Some(rr), Expr::Lit(Value::Str(key))) = (&rrt, &**l) {
            if let RTk::Rec(rec) = &rr.k {
                let members = rec.members.borrow();
                match find_member(&members, key) {
                    Some(m) if m.kind != MKind::Opt => cx.report(
                        "E4054",
                        format!("`in` on member {key}, which is not optional"),
                    ),
                    None if !rec.open.get() => cx.report(
                        "E4054",
                        format!("`in` on undeclared member {key} of a closed record"),
                    ),
                    _ => {}
                }
            }
        }
        return bool_ty();
    }
    if op == ".." || op == "..<" {
        require_val(cx, l, infer(cx, l), "as a range endpoint");
        require_val(cx, r, infer(cx, r), "as a range endpoint");
        return unk(); // a range value: iterable / membership container only
    }
    let lt = require_val(cx, l, infer(cx, l), &format!("as `{op}` operand"));
    let rt = require_val(cx, r, infer(cx, r), &format!("as `{op}` operand"));
    if op == "matches" {
        if lt.rt.is_some() && num_kind(lt.rt.as_ref()).as_deref() != Some("string") {
            cx.report("E4071", "`matches` needs a string left operand".into());
        }
        return bool_ty();
    }
    if op == "==" || op == "!=" {
        return bool_ty();
    }
    let lk = num_kind(lt.rt.as_ref());
    let rk = num_kind(rt.rt.as_ref());
    let cmp = ["<", "<=", ">", ">="].contains(&op);
    let (lks, rks) = (lk.as_deref(), rk.as_deref());
    if lks == Some("quantity") || rks == Some("quantity") {
        // §3.16: +/-/compare need equal dimensions; * and / compose them;
        // a bare int/float scales; a cancelled vector is a plain number
        if op == "+" || op == "-" || cmp {
            if lt.rt.is_some() && rt.rt.is_some() {
                if lks != Some("quantity") || rks != Some("quantity") {
                    let other = if lks == Some("quantity") { rks } else { lks };
                    cx.report(
                        "E4071",
                        format!("`{op}` mixes quantity and {}", other.unwrap_or("null")),
                    );
                } else {
                    let (a, b) = (q_dim(lt.rt.as_ref()), q_dim(rt.rt.as_ref()));
                    if let (Some(a), Some(b)) = (&a, &b) {
                        if a != b {
                            let one = |s: &str| {
                                if s.is_empty() {
                                    "1".to_string()
                                } else {
                                    s.to_string()
                                }
                            };
                            cx.report(
                                "E4072",
                                format!(
                                    "`{op}` on quantities of different dimensions ({} vs {})",
                                    one(a),
                                    one(b)
                                ),
                            );
                        }
                    }
                }
            }
            return if cmp {
                bool_ty()
            } else {
                tyv(if lks == Some("quantity") {
                    lt.rt.clone()
                } else {
                    rt.rt.clone()
                })
            };
        }
        if op == "*" || op == "/" {
            let (Some(_), Some(_)) = (&lt.rt, &rt.rt) else {
                return unk();
            };
            let (lv, rv) = (q_dim(lt.rt.as_ref()), q_dim(rt.rt.as_ref()));
            let numeric = |k: Option<&str>| matches!(k, Some("int") | Some("float"));
            if (lv.is_none() && !numeric(lks)) || (rv.is_none() && !numeric(rks)) {
                cx.report("E4071", format!("`{op}` on a non-numeric operand"));
                return unk();
            }
            let key = key_of_vec(&vec_combine(
                &lv.as_deref().map(vec_of_key).unwrap_or_default(),
                &rv.as_deref().map(vec_of_key).unwrap_or_default(),
                if op == "*" { 1 } else { -1 },
            ));
            return tyv(Some(if key.is_empty() {
                prim("float")
            } else {
                ty(RTk::Quantity(key))
            }));
        }
        cx.report("E4071", format!("`{op}` on quantity operands"));
        return unk();
    }
    if let (Some(_), Some(_), Some(a), Some(b)) = (&lt.rt, &rt.rt, lks, rks) {
        if a != b {
            cx.report("E4071", format!("`{op}` mixes {a} and {b} operands"));
        }
    }
    if cmp {
        return bool_ty();
    }
    if ["&", "^", "<<", ">>"].contains(&op) {
        if (lt.rt.is_some() && lks != Some("int")) || (rt.rt.is_some() && rks != Some("int")) {
            cx.report("E4071", format!("`{op}` on non-int operands"));
        }
        return tyv(Some(prim("int")));
    }
    if op == "|" {
        // bitwise on ints (type-level | never reaches expressions)
        if (lt.rt.is_some() && lks != Some("int")) || (rt.rt.is_some() && rks != Some("int")) {
            cx.report("E4071", "`|` on non-int operands".into());
        }
        return tyv(Some(prim("int")));
    }
    // + - * / %
    if op == "+" && lks == Some("string") && rks == Some("string") {
        return tyv(Some(prim("string")));
    }
    if let (Some(lrt), Some(rrt), Some(a), Some(_)) = (&lt.rt, &rt.rt, lks, rks) {
        if !["int", "float", "quantity"].contains(&a) {
            cx.report("E4071", format!("`{op}` on {a} operands"));
        }
        if a == "int" && ["+", "-", "*"].contains(&op) {
            // interval arithmetic keeps range-typed operands range-typed, so
            // `9000 + i` with i: 0..<3 stays assignable where 1..65535 is expected
            if let (Some(x), Some(y)) = (as_ival(lrt), as_ival(rrt)) {
                let cands: Vec<BigInt> = match op {
                    "+" => vec![&x.0 + &y.0, &x.1 + &y.1],
                    "-" => vec![&x.0 - &y.1, &x.1 - &y.0],
                    _ => vec![&x.0 * &y.0, &x.0 * &y.1, &x.1 * &y.0, &x.1 * &y.1],
                };
                let lo = cands.iter().min().unwrap().clone();
                let hi = cands.iter().max().unwrap().clone();
                return tyv(Some(ty(RTk::Range {
                    lo: Value::Int(lo),
                    hi: Value::Int(hi),
                    excl: false,
                    base: "int".into(),
                })));
            }
        }
        return tyv(if a == "int" || a == "float" {
            Some(prim(a))
        } else {
            None
        });
    }
    unk()
}

fn as_ival(rt: &RT) -> Option<(BigInt, BigInt)> {
    match &rt.k {
        RTk::Lit(Value::Int(i)) => Some((i.clone(), i.clone())),
        RTk::Range {
            lo: Value::Int(lo),
            hi: Value::Int(hi),
            excl,
            base,
        } if base == "int" => Some((lo.clone(), if *excl { hi - 1 } else { hi.clone() })),
        RTk::Union(arms) => {
            let ivs: Vec<Option<(BigInt, BigInt)>> = arms.iter().map(as_ival).collect();
            if !ivs.is_empty() && ivs.iter().all(|v| v.is_some()) {
                let ivs: Vec<(BigInt, BigInt)> = ivs.into_iter().flatten().collect();
                let lo = ivs.iter().map(|v| v.0.clone()).min().unwrap();
                let hi = ivs.iter().map(|v| v.1.clone()).max().unwrap();
                return Some((lo, hi));
            }
            None
        }
        RTk::Pred { base, .. } => as_ival(base),
        _ => None,
    }
}

fn index_core(cx: &Ctx, b: Ty, e: &Rc<Expr>) -> Ty {
    let Expr::Index { i, .. } = &**e else {
        return unk();
    };
    let it = require_val(cx, i, infer(cx, i), "as an index");
    let Some(brt) = &b.rt else { return unk() };
    if let Some(a) = arm_of(Some(brt), "arr") {
        if it.rt.is_some() && num_kind(it.rt.as_ref()).as_deref() != Some("int") {
            cx.report("E4071", "array index is not an int".into());
        }
        if let RTk::Arr { elem, .. } = &a.k {
            return tyv(Some(elem.clone()));
        }
    }
    if let Some(m) = arm_of(Some(brt), "map") {
        let k = path_key(e);
        if let RTk::Map { val, .. } = &m.k {
            return Ty {
                rt: Some(val.clone()),
                abs: !k.map(|k| cx.present.contains(&k)).unwrap_or(false),
            };
        }
    }
    if arm_of(Some(brt), "rec").is_some() {
        return unk(); // dynamic member access
    }
    cx.report("E4071", "indexing a non-collection".into());
    unk()
}

/// a name imported from another module, typed in that module's scope
fn imported_ty(cx: &Ctx, ex: &Export) -> Ty {
    let t = &ex.env;
    let name = &ex.name;
    let c = t.consts.borrow().get(name).cloned();
    if let Some(c) = c {
        // an imported constant is typed as a local one: its annotation, else
        // its expression inferred silently in the exporting module
        return match try_resolve(t, c.ty.as_ref()) {
            Some(a) => tyv(Some(a)),
            None => infer(&make_ctx(t.clone(), Rc::new(|_, _| {})), &c.expr),
        };
    }
    let f = t.funcs.borrow().get(name).cloned();
    if let Some(f) = f {
        return tyv(Some(func_rt_of(t, &f)));
    }
    let out = t
        .outputs
        .borrow()
        .iter()
        .find(|(o, _, _)| o == name)
        .map(|(_, ty, _)| ty.clone());
    if let Some(o) = out {
        return tyv(try_resolve(t, Some(&o)));
    }
    let inp = t.inputs.borrow().get(name).map(|(ty, _)| ty.clone());
    if let Some(i) = inp {
        return tyv(try_resolve(t, Some(&i)));
    }
    if t.type_asts.borrow().contains_key(name) {
        cx.report("E3008", format!("type name {name} used as a value"));
        return unk();
    }
    unk()
}

fn infer_member(cx: &Ctx, e: &Rc<Expr>) -> Ty {
    let Expr::Member { x, name, safe } = &**e else {
        return unk();
    };
    if std_path(e).is_some() {
        return unk(); // std.* namespace path (typed at the call)
    }
    if let Expr::Name(xn) = &**x {
        if !cx.vars.contains_key(xn) {
            let ns = cx.env.namespaces.borrow().get(xn).map(|(_, ex)| ex.clone());
            if let Some(exports) = ns {
                let ex = exports.borrow().get(name).cloned();
                let Some(ex) = ex else {
                    cx.report("E3005", format!("namespace {xn} has no export {name}"));
                    return unk();
                };
                return imported_ty(cx, &ex);
            }
        }
    }
    let b = infer(cx, x);
    let key = path_key(x);
    if !*safe {
        let present = key
            .as_ref()
            .map(|k| cx.present.contains(k))
            .unwrap_or(false);
        let nonnull = key
            .as_ref()
            .map(|k| cx.nonnull.contains(k))
            .unwrap_or(false);
        if b.abs && !present {
            cx.report(
                "E4050",
                "member access on a maybe-absent expression (use ?. or an `in` guard)".into(),
            );
        }
        if has_null(b.rt.as_ref()) && !nonnull {
            cx.report(
                "E4051",
                format!("member .{name} on a possibly-null expression without ?."),
            );
        }
    }
    member_core(cx, b, e)
}

fn member_core(cx: &Ctx, b: Ty, e: &Rc<Expr>) -> Ty {
    let Expr::Member { name, safe, .. } = &**e else {
        return unk();
    };
    let mut brt = b.rt.as_ref().map(strip_null);
    if let Some(RTk::Ref(t)) = brt.as_ref().map(|r| &r.k) {
        brt = Some(t.clone());
    }
    if let Some(RTk::Pred { base, .. }) = brt.as_ref().map(|r| &r.k) {
        brt = Some(base.clone());
    }
    if let Some(r) = &brt {
        if matches!(r.k, RTk::IsectN(_)) {
            brt = arm_of(Some(r), "rec")
                .or_else(|| arm_of(Some(r), "map"))
                .or_else(|| Some(r.clone()));
        }
    }
    let mk_abs = |t: Ty| {
        if *safe {
            Ty {
                rt: t.rt,
                abs: true,
            }
        } else {
            t
        }
    };
    let Some(brt) = brt else { return mk_abs(unk()) };
    match &brt.k {
        RTk::Rec(rec) => {
            let members = rec.members.borrow();
            let Some(m) = find_member(&members, name) else {
                if !rec.open.get() {
                    cx.report(
                        "E4003",
                        format!(
                            "member {name} is not declared on {}",
                            brt.name
                                .borrow()
                                .clone()
                                .unwrap_or_else(|| "this record".into())
                        ),
                    );
                }
                return mk_abs(unk());
            };
            let rt = member_ty(m);
            let present = path_key(e)
                .map(|k| cx.present.contains(&k))
                .unwrap_or(false);
            Ty {
                rt,
                abs: *safe || (m.kind == MKind::Opt && !present),
            }
        }
        RTk::Map { val, .. } => {
            let present = path_key(e)
                .map(|k| cx.present.contains(&k))
                .unwrap_or(false);
            Ty {
                rt: Some(val.clone()),
                abs: *safe || !present,
            }
        }
        RTk::Union(arms) => {
            let parts: Vec<Option<RT>> = arms
                .iter()
                .map(|a| match &a.k {
                    RTk::Rec(rec) => {
                        find_member(&rec.members.borrow(), name).and_then(|m| m.ty.clone())
                    }
                    _ => None,
                })
                .collect();
            mk_abs(tyv(mk_union(parts)))
        }
        RTk::Quantity(_) if name == "value" || name == "unit" => {
            mk_abs(tyv(Some(prim(if name == "value" {
                "float"
            } else {
                "string"
            }))))
        }
        _ => mk_abs(unk()),
    }
}

fn func_rt_of(env: &Rc<Env>, f: &FuncEntry) -> RT {
    ty(RTk::Func {
        params: f
            .params
            .iter()
            .map(|p| try_resolve(env, p.ty.as_ref()).unwrap_or_else(|| ty(RTk::Any)))
            .collect(),
        ret: try_resolve(env, f.ret.as_ref()).unwrap_or_else(|| ty(RTk::Any)),
    })
}
fn func_rt(cx: &Ctx, name: &str) -> RT {
    let f = cx.env.funcs.borrow().get(name).cloned().unwrap();
    func_rt_of(&cx.env, &f)
}
fn const_ty(cx: &Ctx, name: &str) -> Ty {
    if let Some(t) = cx.const_memo.borrow().get(name) {
        return t.clone();
    }
    cx.const_memo.borrow_mut().insert(name.to_string(), unk()); // cycle guard
    let c = cx.env.consts.borrow().get(name).cloned().unwrap();
    let anno = try_resolve(&cx.env, c.ty.as_ref());
    let t = match anno {
        Some(a) => tyv(Some(a)),
        None => infer(&make_ctx(cx.env.clone(), Rc::new(|_, _| {})), &c.expr), // silent module-scope inference
    };
    cx.const_memo
        .borrow_mut()
        .insert(name.to_string(), t.clone());
    t
}

fn infer_call(cx: &Ctx, e: &Rc<Expr>) -> Ty {
    let Expr::Call { fun, args } = &**e else {
        return unk();
    };
    if let Some(sp) = std_path(fun) {
        let sig = std_sig(&sp);
        if sig.is_none() {
            cx.report(
                "E3003",
                format!("std.{sp} does not exist (§13.1: names not listed do not exist)"),
            );
        }
        if let Some((arity, _)) = &sig {
            if args.len() != *arity {
                cx.report(
                    "E4062",
                    format!("std.{sp} expects {arity} argument(s), got {}", args.len()),
                );
            }
        }
        for a in args {
            if matches!(&**a, Expr::Lambda { .. }) {
                infer(cx, a);
                continue;
            }
            require_val(cx, a, infer(cx, a), "as an argument");
        }
        return tyv(sig.and_then(|(_, r)| r));
    }
    let f = infer(cx, fun);
    let frt = f.rt.filter(|r| matches!(r.k, RTk::Func { .. }));
    let (params, ret): (Vec<RT>, Option<RT>) = match frt.as_ref().map(|r| &r.k) {
        Some(RTk::Func { params, ret }) => (params.clone(), ret_of(ret)),
        _ => (vec![], None),
    };
    if frt.is_some() && args.len() != params.len() {
        cx.report(
            "E4062",
            format!(
                "call expects {} argument(s), got {}",
                params.len(),
                args.len()
            ),
        );
    }
    for (i, a) in args.iter().enumerate() {
        let expected: Option<RT> =
            if frt.is_some() && i < params.len() && !matches!(params[i].k, RTk::Any) {
                Some(params[i].clone())
            } else {
                None
            };
        if let (Expr::Lambda { .. }, Some(ex)) = (&**a, &expected) {
            if matches!(ex.k, RTk::Func { .. }) {
                check_lambda(cx, a, ex);
                continue;
            }
        }
        if matches!(&**a, Expr::Lambda { .. }) {
            infer(cx, a);
            continue;
        }
        let at = require_val(cx, a, infer(cx, a), "as an argument");
        if let (Some(art), Some(ex)) = (&at.rt, &expected) {
            if !subsumes(&cx.env, art, ex) && !deferrable(art, ex) {
                cx.report(
                    "E4001",
                    format!("argument {} is not assignable to its parameter", i + 1),
                );
            }
        }
    }
    tyv(if frt.is_some() { ret } else { None })
}

fn check_lambda(cx: &Ctx, e: &Rc<Expr>, expected: &RT) {
    let (Expr::Lambda { params, body }, RTk::Func { params: eps, ret }) = (&**e, &expected.k)
    else {
        return;
    };
    if params.len() != eps.len() {
        cx.report(
            "E4062",
            "lambda arity differs from expected function type".into(),
        );
        return;
    }
    let mut c2 = cx.child();
    for (i, p) in params.iter().enumerate() {
        if name_bound(&c2, p) {
            cx.report(
                "E3019",
                format!("lambda parameter {p} shadows an enclosing name"),
            );
        }
        c2.vars.insert(
            p.clone(),
            tyv(if matches!(eps[i].k, RTk::Any) {
                None
            } else {
                Some(eps[i].clone())
            }),
        );
    }
    let b = require_val(&c2, body, infer(&c2, body), "as a lambda result");
    if let (Some(brt), Some(r)) = (&b.rt, ret_of(ret)) {
        if !subsumes(&cx.env, brt, &r) && !deferrable(brt, &r) {
            cx.report(
                "E4001",
                "lambda body is not assignable to the expected result type".into(),
            );
        }
    }
}

// ---------------- match (§4.7) ----------------
fn infer_match(cx: &Ctx, e: &Rc<Expr>, expected: Option<&RT>) -> Ty {
    let Expr::Match { subject, arms } = &**e else {
        return unk();
    };
    let s = require_val(cx, subject, infer(cx, subject), "as a match subject");
    let mut variants: Option<Vec<RT>> = None;
    if let Some(srt0) = &s.rt {
        let srt = strip_null(srt0);
        if let RTk::Union(vs) = &srt.k {
            let mut v = vs.clone();
            if has_null(Some(srt0)) {
                v.push(ty(RTk::Lit(Value::Null)));
            }
            variants = Some(v);
        } else {
            cx.report(
                "E4103",
                "`match` subject is not a discriminable union".into(),
            );
        }
    }
    let mut covered: HashSet<usize> = HashSet::new();
    let mut catch_alls = 0;
    let mut results: Vec<Option<RT>> = vec![];
    for arm in arms {
        let mut c2 = cx.child();
        if name_bound(&c2, &arm.v) {
            cx.report(
                "E3019",
                format!("match binding {} shadows an enclosing name", arm.v),
            );
        }
        let mut arm_ty: Option<RT> = None;
        if let Some(t) = &arm.ty {
            arm_ty = try_resolve(&cx.env, Some(t));
            if arm_ty.is_none() {
                cx.report("E3003", "unknown type in match arm".into());
            }
            if let (Some(vs), Some(at)) = (&variants, &arm_ty) {
                for (i, v) in vs.iter().enumerate() {
                    if subsumes(&cx.env, v, at) {
                        if covered.contains(&i) {
                            cx.report("E4100", "match arms overlap on a variant".into());
                        }
                        covered.insert(i);
                    }
                }
            }
        } else {
            catch_alls += 1;
            if let Some(vs) = &variants {
                let rest: Vec<Option<RT>> = vs
                    .iter()
                    .enumerate()
                    .filter(|(i, _)| !covered.contains(i))
                    .map(|(_, v)| Some(v.clone()))
                    .collect();
                if rest.is_empty() {
                    cx.report(
                        "E4102",
                        "match catch-all is dead (typed arms are exhaustive)".into(),
                    );
                }
                arm_ty = mk_union(rest);
            }
        }
        c2.vars.insert(arm.v.clone(), tyv(arm_ty));
        let bt = match expected {
            Some(ex) => check_expr(&c2, &arm.body, Some(ex)),
            None => infer(&c2, &arm.body),
        };
        let b = require_val(&c2, &arm.body, bt, "as a match result");
        results.push(b.rt);
    }
    if catch_alls > 1 {
        cx.report("E4100", "more than one match catch-all arm".into());
    }
    if let Some(vs) = &variants {
        if catch_alls == 0 && covered.len() < vs.len() {
            cx.report(
                "E4101",
                "`match` is not exhaustive over the subject union".into(),
            );
        }
    }
    tyv(mk_union(results))
}

// ---------------- bidirectional checking (§3.18) ----------------
/// a navigation expression in a ref<T> position denotes a place (§7.4):
/// the absence discipline does not apply along the spine — whether the
/// place holds a value is reference integrity (§7.5), checked at binding
fn place_ty(cx: &Ctx, e: &Rc<Expr>) -> Ty {
    match &**e {
        Expr::Paren(x) => place_ty(cx, x),
        Expr::Member { x, name, .. } => {
            let base = place_ty(cx, x);
            // a hidden member's value is not part of any document: no reference
            // can target it (§7.5, D34)
            let rec = base.rt.as_ref().map(|t| match &t.k {
                RTk::Ref(target) => target.clone(),
                _ => t.clone(),
            });
            if let Some(rec) = rec {
                if let RTk::Rec(r) = &rec.k {
                    if r.members
                        .borrow()
                        .iter()
                        .any(|m| m.name == *name && m.hidden)
                    {
                        cx.report("E4093", format!("`ref` position navigates hidden member {name} — not part of the value (§7.5)"));
                    }
                }
            }
            member_core(cx, base, e)
        }
        Expr::Index { x, .. } => index_core(cx, place_ty(cx, x), e),
        Expr::If { c, t, f } => {
            // a conditional between places is a place: each branch is read in
            // the ref position, the condition as an ordinary value (§7.4)
            let ct = require_val(cx, c, infer(cx, c), "as a condition");
            if ct.rt.is_some() && !is_boolish(ct.rt.as_ref()) {
                cx.report("E4001", "`if` condition is not bool".into());
            }
            let tt = place_ty(&apply_guards(cx, guards_of(c, true)), t);
            let ft = place_ty(&apply_guards(cx, guards_of(c, false)), f);
            Ty {
                rt: mk_union(vec![tt.rt, ft.rt]),
                abs: tt.abs || ft.abs,
            }
        }
        Expr::Name(n) if !cx.vars.contains_key(n) && cx.env.consts.borrow().contains_key(n) => {
            // the spine root must be a root-derived place, never a module const (§7.5, D32)
            cx.report(
                "E4093",
                format!(
                    "`ref` position navigates module const {n} — not a root-derived place (§7.5)"
                ),
            );
            infer(cx, e)
        }
        _ => infer(cx, e),
    }
}

pub fn check_expr(cx: &Ctx, e: &Rc<Expr>, expected: Option<&RT>) -> Ty {
    let Some(expected) = expected else {
        return infer(cx, e);
    };
    if let RTk::Ref(_) = &expected.k {
        place_ty(cx, e);
        return tyv(Some(expected.clone())); // place, not value (§7.4)
    }
    if let RTk::Pred { base, .. } = &expected.k {
        return check_expr(cx, e, Some(base));
    }
    if let RTk::IsectN(arms) = &expected.k {
        if matches!(
            &**e,
            Expr::Obj(_) | Expr::Arr(_) | Expr::Comp { .. } | Expr::MapComp { .. }
        ) {
            for arm in arms {
                check_expr(cx, e, Some(arm)); // a literal must satisfy every arm
            }
            return tyv(Some(expected.clone()));
        }
    }
    match (&**e, &expected.k) {
        (Expr::Comp { head, clauses }, RTk::Arr { elem, .. }) => {
            let c2 = bind_clauses(cx, clauses);
            check_expr(&c2, head, Some(elem));
            return tyv(Some(expected.clone()));
        }
        (Expr::MapComp { key, val, clauses }, RTk::Map { val: ev, .. }) => {
            let c2 = bind_clauses(cx, clauses);
            let k = require_val(&c2, key, infer(&c2, key), "as a map key");
            if k.rt.is_some() && num_kind(k.rt.as_ref()).as_deref() != Some("string") {
                cx.report("E4001", "map-comprehension key is not a string".into());
            }
            check_expr(&c2, val, Some(ev));
            return tyv(Some(expected.clone()));
        }
        (Expr::Paren(x), _) => return check_expr(cx, x, Some(expected)),
        (Expr::If { c, t, f }, _) => {
            let ct = require_val(cx, c, infer(cx, c), "as a condition");
            if ct.rt.is_some() && !is_boolish(ct.rt.as_ref()) {
                cx.report("E4001", "`if` condition is not bool".into());
            }
            check_expr(&apply_guards(cx, guards_of(c, true)), t, Some(expected));
            check_expr(&apply_guards(cx, guards_of(c, false)), f, Some(expected));
            return tyv(Some(expected.clone()));
        }
        (Expr::Match { .. }, _) => return infer_match(cx, e, Some(expected)),
        (Expr::Obj(entries), RTk::Rec(rec)) => {
            // entries see the record's members (siblings + inherited scope chain)
            let members = rec.members.borrow().clone();
            let mut cx_r = cx.child();
            for m in &members {
                cx_r.vars.insert(
                    m.name.clone(),
                    Ty {
                        rt: member_ty(m),
                        abs: m.kind == MKind::Opt,
                    },
                );
            }
            for (k, v) in entries {
                let Some(m) = find_member(&members, k) else {
                    if !rec.open.get() {
                        cx.report(
                            "E4003",
                            format!(
                                "member {k} is not declared on {}",
                                expected
                                    .name
                                    .borrow()
                                    .clone()
                                    .unwrap_or_else(|| "the record".into())
                            ),
                        );
                    }
                    require_val(&cx_r, v, infer(&cx_r, v), "as a construction member");
                    continue;
                };
                let mt = member_ty(m);
                let t = check_expr(&cx_r, v, mt.as_ref());
                require_val(&cx_r, v, t, "as a construction member");
            }
            for m in &members {
                if m.kind == MKind::Req && !entries.iter().any(|(k, _)| *k == m.name) {
                    cx.report(
                        "E4002",
                        format!("required member {} missing in the construction", m.name),
                    );
                }
            }
            return tyv(Some(expected.clone()));
        }
        (Expr::Obj(entries), RTk::Map { val, .. }) => {
            for (_, v) in entries {
                let t = check_expr(cx, v, Some(val));
                require_val(cx, v, t, "as a map value");
            }
            return tyv(Some(expected.clone()));
        }
        (Expr::Obj(_), RTk::Union(_)) => {
            infer(cx, e);
            return tyv(Some(expected.clone())); // discriminated at binding
        }
        (Expr::Obj(_), _) => {
            infer(cx, e);
            cx.report(
                "E4001",
                format!("object literal where {} is expected", tag(expected)),
            );
            return tyv(Some(expected.clone()));
        }
        (Expr::Arr(items), RTk::Arr { elem, .. }) => {
            for (spread, x) in items {
                if *spread {
                    require_val(cx, x, infer(cx, x), "as a spread");
                    continue;
                }
                let t = check_expr(cx, x, Some(elem));
                require_val(cx, x, t, "as an array element");
            }
            return tyv(Some(expected.clone()));
        }
        (Expr::Lambda { .. }, RTk::Func { .. }) => {
            check_lambda(cx, e, expected);
            return tyv(Some(expected.clone()));
        }
        _ => {}
    }
    let t = require_val(cx, e, infer(cx, e), "as a value");
    if let Some(rt) = &t.rt {
        if !subsumes(&cx.env, rt, expected) && !deferrable(rt, expected) {
            cx.report(
                "E4001",
                "expression type does not satisfy the expected type".into(),
            );
        }
    }
    t
}

/// a same-kind refinement target (pattern, range, literal set) whose
/// membership the static type cannot prove is validated at binding, not
/// rejected here — the corpus (guide, benchmarks) relies on this split;
/// kind-level mismatches still fail statically
fn deferrable(s: &RT, t: &RT) -> bool {
    let Some(k) = num_kind(Some(s)) else {
        return false;
    };
    match &t.k {
        RTk::Pattern { .. } => k == "string",
        RTk::Range { base, .. } => &k == base,
        RTk::Lit(_) => Some(k) == num_kind(Some(t)),
        RTk::Union(arms) => arms.iter().any(|a| deferrable(s, a)),
        RTk::Pred { base, .. } => deferrable(s, base),
        _ => false,
    }
}