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
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
//! Kernel — query execution, proof emission, receipt assembly.
//!
//! ARD section 8 (execution model), 11 (proof requirements), 13 (WASM ABI).
//!
//! The kernel never sees strings. It accepts only `Catalog`, `FactBlock8`s,
//! `Rule8`s, and `QueryAtom8`s, and emits answers + proof + receipt.
use crate::admission::{admit_atom, admit_rule, RejectionCode};
use crate::catalog::Catalog;
use crate::hash::{
combine_roots, Hash, DOMAIN_PROLOG8_INPUT, DOMAIN_PROLOG8_OUTPUT, DOMAIN_PROLOG8_PROOF_ROOT,
DOMAIN_PROLOG8_RULES,
};
use crate::types::{
Atom8, DecisionKind, EpochId, FactBlock8, FactRow8, ProofKind, ProofMode, ProofNode,
ProofNodeId, QueryAtom8, Receipt, Rule8, SubstitutionId, TermId, ARITY_CAP,
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
// ---------------------------------------------------------------------------
// Variable-encoding helpers (module-level so `solve_body` can use them)
// ---------------------------------------------------------------------------
/// Variables are encoded as TermIds with the high bit set: VAR_SENTINEL_BASE + N.
/// This partitions the u32 space cleanly — normal term IDs never have the high bit set.
const VAR_SENTINEL_BASE: u32 = 0x8000_0000;
/// Maximum recursive rule-chaining depth in `derive_atom`. Prevents infinite
/// loops on recursive rules while supporting PARARULE-Plus depth-5 chains.
pub const MAX_DERIVE_DEPTH: u32 = 32;
/// One derived solution: concrete head args + the leaf facts that support it.
type Solution = ([TermId; ARITY_CAP as usize], Vec<FactRow8>);
/// Per-query evaluation state. Created fresh by `Kernel::query` (so it is
/// trivially invalidated at `load_facts`/`load_rule` boundaries — it never
/// outlives a single query) and threaded through the solver.
///
/// Memoization soundness: results are depth-sensitive (the cap truncates),
/// so entries record the deepest call depth at which the goal was derived
/// WITHOUT truncation, and are reused only when the current call depth is <=
/// the recorded depth — an untruncated derivation of a goal has the same
/// solution set at any smaller call depth. Entries computed while the depth
/// cap fired are never cached. The cache is per-query only (created in
/// `Kernel::query`), so it is trivially invalidated at load_facts/load_rule
/// boundaries.
#[derive(Default)]
struct EvalState {
/// Memo table: goal atom -> (max untruncated call depth, solutions).
memo: HashMap<Atom8, (u32, Vec<Solution>)>,
/// Number of memo hits (diagnostic; proves the cache fires).
memo_hits: u64,
/// Number of memo misses (derivation computations actually performed).
memo_misses: u64,
/// Set when a negated body atom was evaluated with unbound variables.
negation_floundered: bool,
/// Set when the derivation recursion hit `MAX_DERIVE_DEPTH`.
depth_exceeded: bool,
}
type Subst = [Option<TermId>; ARITY_CAP as usize];
fn var_term(i: usize) -> TermId {
TermId(VAR_SENTINEL_BASE + i as u32)
}
fn var_index(t: TermId) -> Option<usize> {
if t.0 >= VAR_SENTINEL_BASE && t.0 < VAR_SENTINEL_BASE + ARITY_CAP as u32 {
Some((t.0 - VAR_SENTINEL_BASE) as usize)
} else {
None
}
}
fn resolve_var(t: TermId, subst: &Subst) -> TermId {
if let Some(vi) = var_index(t) {
subst[vi].unwrap_or(t)
} else {
t
}
}
/// Robinson unification for flat terms (no recursive structure, occurs-check trivially satisfied).
fn unify_terms(a: TermId, b: TermId, subst: &mut Subst) -> bool {
let ra = resolve_var(a, subst);
let rb = resolve_var(b, subst);
if ra == rb {
return true;
}
if let Some(vi) = var_index(ra) {
subst[vi] = Some(rb);
return true;
}
if let Some(vi) = var_index(rb) {
subst[vi] = Some(ra);
return true;
}
false
}
/// One answer row: bindings for the output positions.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Decision {
/// The decision value.
pub kind: DecisionKind,
/// Output bindings: one entry per `Some` output position in `output_mask`.
pub bindings: Vec<TermId>,
/// Proof DAG nodes (positive or negative).
pub proof: Vec<ProofNode>,
/// Receipt assembled from all roots.
pub receipt: Receipt,
/// True when derivation was truncated by `MAX_DERIVE_DEPTH`. The answer
/// set may be incomplete; callers decide refusal semantics. Never a
/// silent truncation: this flag makes the cap observable.
pub depth_exceeded: bool,
}
/// Result of running a query.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum QueryResult {
/// Query produced one or more answers.
Answered(Vec<Decision>),
/// Query produced no answers (false / deny).
Denied(Box<Decision>),
/// Inputs failed admission.
Invalid(RejectionCode),
}
/// The Prolog8 kernel. Owns a catalog, fact blocks, and rule set.
pub struct Kernel {
/// Admitted catalog.
pub catalog: Catalog,
/// Loaded fact blocks (per ARD section 4.7).
pub fact_blocks: Vec<FactBlock8>,
/// Loaded rules (per ARD section 4.4).
pub rules: Vec<Rule8>,
}
impl Kernel {
/// Construct an empty kernel.
pub fn new(catalog: Catalog) -> Self {
Self {
catalog,
fact_blocks: Vec::new(),
rules: Vec::new(),
}
}
/// Load a fact block. Block must be admitted before any query touches it.
pub fn load_facts(&mut self, block: FactBlock8) -> Result<(), RejectionCode> {
// Sanity: block predicate must exist in catalog with matching arity.
let meta = self
.catalog
.predicate(block.pred_id)
.ok_or(RejectionCode::PredicateNotInCatalog)?;
if meta.arity != block.arity {
return Err(RejectionCode::ArityMismatch);
}
self.fact_blocks.push(block);
Ok(())
}
/// Load a rule. Runs admission.
pub fn load_rule(&mut self, rule: Rule8) -> Result<(), RejectionCode> {
admit_rule(&rule, &self.catalog)?;
self.rules.push(rule);
Ok(())
}
/// Run a query end-to-end: validate, scan facts, fire applicable rules,
/// emit proof and receipt.
/// Run a query end-to-end with a fresh per-query `EvalState`
/// (memoization + fault tracking). See `query_with_state`.
pub fn query(&self, q: &QueryAtom8) -> QueryResult {
let mut state = EvalState::default();
let result = self.query_with_state(q, &mut state);
if state.negation_floundered {
// Floundered negation is a typed refusal, never a silent false.
return QueryResult::Invalid(RejectionCode::NegationFloundering);
}
result
}
/// Query with memoization statistics: `(result, memo_misses, memo_hits)`.
/// Diagnostic surface for tests and benchmarks.
pub fn query_with_stats(&self, q: &QueryAtom8) -> (QueryResult, u64, u64) {
let mut state = EvalState::default();
let result = self.query_with_state(q, &mut state);
if state.negation_floundered {
return (
QueryResult::Invalid(RejectionCode::NegationFloundering),
state.memo_misses,
state.memo_hits,
);
}
(result, state.memo_misses, state.memo_hits)
}
fn query_with_state(&self, q: &QueryAtom8, state: &mut EvalState) -> QueryResult {
if let Err(code) = admit_atom(&q.atom, &self.catalog) {
return QueryResult::Invalid(code);
}
// Phase 1: direct fact matches.
let fact_answers = self.scan_facts(&q.atom, q.epoch);
if !fact_answers.is_empty() {
let answers = fact_answers
.into_iter()
.enumerate()
.map(|(i, (row, fact_hash))| {
let mut d = self.assemble_fact_answer(q, &row, fact_hash, i);
d.depth_exceeded = state.depth_exceeded;
d
})
.collect();
return QueryResult::Answered(answers);
}
// Phase 2: rule application (one-step, depth-1 backwards chaining).
let rule_answers = self.scan_rules(q, state);
if !rule_answers.is_empty() {
return QueryResult::Answered(rule_answers);
}
// Phase 3: deny with negative proof. Depth truncation is surfaced on
// the decision — the caller decides refusal semantics.
let mut d = self.assemble_negative(q);
d.depth_exceeded = state.depth_exceeded;
QueryResult::Denied(Box::new(d))
}
/// Scan all admitted fact blocks for rows that unify with `query`.
/// Returns `(row, fact_hash)` pairs.
fn scan_facts(&self, query: &Atom8, epoch: EpochId) -> Vec<(FactRow8, [u8; 32])> {
self.fact_blocks
.iter()
.filter(|b| b.pred_id == query.pred_id && !b.skip_for(query, epoch))
.flat_map(|b| b.rows.iter())
.filter(|row| Self::row_matches(row, query))
.map(|row| (*row, row.fact_hash))
.collect()
}
/// Test whether a fact row matches a query atom under bound positions.
fn row_matches(row: &FactRow8, query: &Atom8) -> bool {
if row.pred_id != query.pred_id || row.arity != query.arity {
return false;
}
(0..row.arity as usize).all(|i| !query.is_bound(i as u8) || row.args[i] == query.args[i])
}
/// Horn clause evaluation with backtracking: tries every matching fact per body
/// atom, backtracks on dead ends, and collects all successful derivations.
///
/// Every rule whose head unifies with the query is tried independently. The
/// visited-set is NOT used here because `solve_body` never recurses back into
/// rule evaluation (it calls `scan_facts` only), so there is no rule-level
/// recursion to prevent. Blocking duplicate (pred_id, args) entries would
/// incorrectly suppress multiple rules that derive the same head.
fn scan_rules(&self, q: &QueryAtom8, state: &mut EvalState) -> Vec<Decision> {
let mut answers = Vec::new();
for rule in &self.rules {
if rule.head.pred_id != q.atom.pred_id || rule.head.arity != q.atom.arity {
continue;
}
let mut subst: Subst = [None; ARITY_CAP as usize];
// Unify rule head args with query args.
let mut head_ok = true;
for i in 0..rule.head.arity as usize {
let h_arg = rule.head.args[i];
let h_effective = if h_arg.is_sentinel() {
var_term(i)
} else {
h_arg
};
if q.atom.is_bound(i as u8) {
if !unify_terms(h_effective, q.atom.args[i], &mut subst) {
head_ok = false;
break;
}
}
}
if !head_ok {
continue;
}
// Depth-first backtracking search over body atoms.
self.solve_body(rule, subst, 0, &mut Vec::new(), &mut answers, q, state);
}
// Deduplicate by bindings: two proofs for the same conclusion are one answer.
// This matches standard Prolog semantics for ground queries (first proof wins)
// while preserving distinct binding solutions for unbound queries.
let mut seen: Vec<Vec<TermId>> = Vec::with_capacity(answers.len());
answers.retain(|d| {
if seen.contains(&d.bindings) {
false
} else {
seen.push(d.bindings.clone());
true
}
});
answers
}
/// Recursive backtracking solver for a rule's body atoms.
///
/// Each body atom is evaluated via `derive_atom_with_support`, which resolves
/// it against base facts AND recursively against other rules (full SLD resolution).
/// On each candidate, the substitution is extended and we recurse to the next
/// body atom, backtracking on failure. All leaf base facts are collected into
/// `supporting` for proof generation.
fn solve_body(
&self,
rule: &Rule8,
subst: Subst,
bi: usize,
supporting: &mut Vec<FactRow8>,
answers: &mut Vec<Decision>,
q: &QueryAtom8,
state: &mut EvalState,
) {
if bi == rule.body_len as usize {
let mut bindings_arr = [TermId::sentinel(); ARITY_CAP as usize];
for i in 0..rule.head.arity as usize {
let h = rule.head.args[i];
let effective = if h.is_sentinel() { var_term(i) } else { h };
bindings_arr[i] = resolve_var(effective, &subst);
}
answers.push(self.assemble_rule_answer(q, rule, &bindings_arr, supporting, state));
return;
}
let body_atom = rule.body[bi];
// Build a concrete query atom: resolve every already-bound variable.
let mut concrete_args = [TermId::sentinel(); ARITY_CAP as usize];
let mut concrete_binding = 0u8;
for ai in 0..body_atom.arity as usize {
let raw = body_atom.args[ai];
let effective = if raw.is_sentinel() { var_term(ai) } else { raw };
let resolved = resolve_var(effective, &subst);
if var_index(resolved).is_none() {
concrete_args[ai] = resolved;
concrete_binding |= 1u8 << ai;
}
}
let mut concrete = Atom8::new(body_atom.pred_id, body_atom.arity, &concrete_args);
concrete.binding_mask = concrete_binding;
let negated = (rule.negation_mask >> bi) & 1 == 1;
if negated {
// NAF: body atom succeeds iff it has NO derivation.
// Groundness gate: every variable of the negated atom must already
// be bound. Evaluating a non-ground negation would silently treat
// "no proof found for unknown bindings" as false — unsound. Refuse
// with a typed error instead.
if concrete_binding != (1u8 << body_atom.arity).wrapping_sub(1) {
state.negation_floundered = true;
return;
}
let solutions = self.derive_atom_with_support(&concrete, q.epoch, 0, state);
if solutions.is_empty() {
// NAF succeeds — no new bindings, continue to next body atom.
self.solve_body(rule, subst, bi + 1, supporting, answers, q, state);
}
// else: NAF fails — backtrack (no recursive call).
} else {
// Positive atom: evaluate and extend substitution for each solution.
for (sol_args, sol_facts) in
self.derive_atom_with_support(&concrete, q.epoch, 0, state)
{
let mut new_subst = subst;
let mut ok = true;
for ai in 0..body_atom.arity as usize {
let raw = body_atom.args[ai];
let effective = if raw.is_sentinel() { var_term(ai) } else { raw };
if !unify_terms(effective, sol_args[ai], &mut new_subst) {
ok = false;
break;
}
}
if ok {
let pre_len = supporting.len();
supporting.extend_from_slice(&sol_facts);
self.solve_body(rule, new_subst, bi + 1, supporting, answers, q, state);
supporting.truncate(pre_len);
}
}
}
}
/// Derive all solutions for `query`, returning `(concrete_args, leaf_facts)` pairs.
///
/// Tries base facts first, then recursively fires rules whose head unifies with
/// the query. `depth` is incremented on each rule-recursive step and capped at
/// `MAX_DERIVE_DEPTH` to prevent infinite loops on recursive rules.
fn derive_atom_with_support(
&self,
query: &Atom8,
epoch: EpochId,
depth: u32,
state: &mut EvalState,
) -> Vec<Solution> {
// Memo lookup: reuse only if the cached derivation ran with at least
// the current budget (depth <= recorded depth) and was not truncated.
if let Some((d0, sols)) = state.memo.get(query) {
if depth <= *d0 {
state.memo_hits += 1;
return sols.clone();
}
}
if depth > MAX_DERIVE_DEPTH {
// Typed, observable truncation: record the cap hit so the caller's
// Decision carries `depth_exceeded = true`. Never silently empty.
state.depth_exceeded = true;
return Vec::new();
}
state.memo_misses += 1;
let mut out: Vec<([TermId; ARITY_CAP as usize], Vec<FactRow8>)> = Vec::new();
// Base: matching facts.
for (row, _) in self.scan_facts(query, epoch) {
out.push((row.args, vec![row]));
}
// Inductive: matching rules.
for rule in &self.rules {
if rule.head.pred_id != query.pred_id || rule.head.arity != query.arity {
continue;
}
let mut head_subst: Subst = [None; ARITY_CAP as usize];
let mut head_ok = true;
for i in 0..rule.head.arity as usize {
let h = rule.head.args[i];
let he = if h.is_sentinel() { var_term(i) } else { h };
if query.is_bound(i as u8) {
if !unify_terms(he, query.args[i], &mut head_subst) {
head_ok = false;
break;
}
}
}
if !head_ok {
continue;
}
self.derive_body_with_support(
rule,
head_subst,
0,
epoch,
depth,
&mut Vec::new(),
&mut out,
state,
);
}
// Dedup by head args: multiple derivation paths to the same ground
// instance yield one solution (first supporting-fact set wins). This
// is the tabled-prolog collapse — answer bindings are unchanged
// (scan_rules dedups by bindings anyway) and it bounds the solution
// count by distinct ground instances instead of derivation paths.
let mut seen_args: Vec<[TermId; ARITY_CAP as usize]> = Vec::new();
out.retain(|(args, _)| {
if seen_args.contains(args) {
false
} else {
seen_args.push(*args);
true
}
});
// Cache only complete (untruncated) derivations, recording the deepest
// call depth at which the goal was derived; the entry is sound to
// reuse at any smaller call depth.
if !state.depth_exceeded {
state
.memo
.entry(query.clone())
.and_modify(|(d0, sols)| {
*d0 = (*d0).max(depth);
*sols = out.clone();
})
.or_insert((depth, out.clone()));
}
out
}
/// Recursive body evaluator for `derive_atom_with_support`.
///
/// When all body atoms are satisfied (`bi == body_len`), extracts the rule
/// head's concrete arg values and pushes them with the accumulated leaf facts.
fn derive_body_with_support(
&self,
rule: &Rule8,
subst: Subst,
bi: usize,
epoch: EpochId,
depth: u32,
supporting: &mut Vec<FactRow8>,
out: &mut Vec<Solution>,
state: &mut EvalState,
) {
if bi == rule.body_len as usize {
let mut args = [TermId::sentinel(); ARITY_CAP as usize];
for i in 0..rule.head.arity as usize {
let h = rule.head.args[i];
let he = if h.is_sentinel() { var_term(i) } else { h };
args[i] = resolve_var(he, &subst);
}
out.push((args, supporting.clone()));
return;
}
let body_atom = rule.body[bi];
let mut c_args = [TermId::sentinel(); ARITY_CAP as usize];
let mut c_mask = 0u8;
for ai in 0..body_atom.arity as usize {
let raw = body_atom.args[ai];
let eff = if raw.is_sentinel() { var_term(ai) } else { raw };
let res = resolve_var(eff, &subst);
if var_index(res).is_none() {
c_args[ai] = res;
c_mask |= 1u8 << ai;
}
}
let mut c_atom = Atom8::new(body_atom.pred_id, body_atom.arity, &c_args);
c_atom.binding_mask = c_mask;
let negated = (rule.negation_mask >> bi) & 1 == 1;
if negated {
// Same groundness gate as `solve_body`: refuse non-ground NAF.
if c_mask != (1u8 << body_atom.arity).wrapping_sub(1) {
state.negation_floundered = true;
return;
}
let solutions = self.derive_atom_with_support(&c_atom, epoch, depth + 1, state);
if solutions.is_empty() {
self.derive_body_with_support(
rule, subst, bi + 1, epoch, depth, supporting, out, state,
);
}
} else {
for (sol_args, sol_facts) in
self.derive_atom_with_support(&c_atom, epoch, depth + 1, state)
{
let mut new_subst = subst;
let mut ok = true;
for ai in 0..body_atom.arity as usize {
let raw = body_atom.args[ai];
let eff = if raw.is_sentinel() { var_term(ai) } else { raw };
if !unify_terms(eff, sol_args[ai], &mut new_subst) {
ok = false;
break;
}
}
if ok {
let pre_len = supporting.len();
supporting.extend_from_slice(&sol_facts);
self.derive_body_with_support(
rule,
new_subst,
bi + 1,
epoch,
depth,
supporting,
out,
state,
);
supporting.truncate(pre_len);
}
}
}
}
fn assemble_fact_answer(
&self,
q: &QueryAtom8,
row: &FactRow8,
fact_hash: [u8; 32],
idx: usize,
) -> Decision {
let mut bindings = Vec::new();
for i in 0..row.arity {
if (q.output_mask >> i) & 1 == 1 {
bindings.push(row.args[i as usize]);
}
}
let proof = if matches!(q.proof_mode, ProofMode::PositiveOnly | ProofMode::Both) {
vec![ProofNode {
node_id: ProofNodeId(idx as u32),
kind: ProofKind::Fact,
pred_id: row.pred_id,
rule_id: None,
fact_hash: Some(fact_hash),
children: [ProofNodeId(0); 8],
child_count: 0,
substitution_id: SubstitutionId(idx as u32),
node_hash: [0u8; 32],
}]
} else {
Vec::new()
};
let proof = self.finalize_proof(proof);
let receipt = self.assemble_receipt(q, &bindings, false, &proof, DecisionKind::Allow);
Decision {
kind: DecisionKind::Allow,
bindings,
proof,
receipt,
depth_exceeded: false,
}
}
fn assemble_rule_answer(
&self,
q: &QueryAtom8,
rule: &Rule8,
bindings_arr: &[TermId; 8],
supporting_facts: &[FactRow8],
state: &EvalState,
) -> Decision {
let mut bindings = Vec::new();
for i in 0..rule.head.arity {
if (q.output_mask >> i) & 1 == 1 {
bindings.push(bindings_arr[i as usize]);
}
}
let mut proof = Vec::new();
for (i, fact) in supporting_facts.iter().enumerate() {
proof.push(ProofNode {
node_id: ProofNodeId(i as u32),
kind: ProofKind::Fact,
pred_id: fact.pred_id,
rule_id: None,
fact_hash: Some(fact.fact_hash),
children: [ProofNodeId(0); 8],
child_count: 0,
substitution_id: SubstitutionId(i as u32),
node_hash: [0u8; 32],
});
}
// Reduce the supporting facts into a valid fan-in<=8 proof DAG.
// Layers of chunked grouping nodes (kind: Rule, rule_id: None) combine
// >8 supports; every fact node stays in the Vec, so proof_root still
// commits to the full derivation. (Previously this indexed out of
// bounds when a derivation had more than 8 supporting facts.)
let mut layer: Vec<ProofNodeId> = (0..supporting_facts.len() as u32)
.map(ProofNodeId)
.collect();
while layer.len() > 8 {
let mut next = Vec::new();
for chunk in layer.chunks(8) {
let node_id = ProofNodeId(proof.len() as u32);
let mut children = [ProofNodeId(0); 8];
for (j, c) in chunk.iter().enumerate() {
children[j] = *c;
}
let node = ProofNode {
node_id,
kind: ProofKind::Rule,
pred_id: rule.head.pred_id,
rule_id: None,
fact_hash: None,
children,
child_count: chunk.len() as u8,
substitution_id: SubstitutionId(0),
node_hash: [0u8; 32],
};
proof.push(node);
next.push(node_id);
}
layer = next;
}
let mut child_ids = [ProofNodeId(0); 8];
for (i, id) in layer.iter().enumerate() {
child_ids[i] = *id;
}
proof.push(ProofNode {
node_id: ProofNodeId(proof.len() as u32),
kind: ProofKind::Rule,
rule_id: Some(rule.rule_id),
pred_id: rule.head.pred_id,
fact_hash: None,
children: child_ids,
child_count: layer.len() as u8,
substitution_id: SubstitutionId(0),
node_hash: [0u8; 32],
});
let proof = self.finalize_proof(proof);
let receipt = self.assemble_receipt(
q,
&bindings,
state.depth_exceeded,
&proof,
DecisionKind::Allow,
);
Decision {
kind: DecisionKind::Allow,
bindings,
proof,
receipt,
depth_exceeded: state.depth_exceeded,
}
}
fn assemble_negative(&self, q: &QueryAtom8) -> Decision {
let proof = if matches!(q.proof_mode, ProofMode::NegativeOnly | ProofMode::Both) {
vec![ProofNode {
node_id: ProofNodeId(0),
kind: ProofKind::MissingFact,
pred_id: q.atom.pred_id,
rule_id: None,
fact_hash: None,
children: [ProofNodeId(0); 8],
child_count: 0,
substitution_id: SubstitutionId(0),
node_hash: [0u8; 32],
}]
} else {
Vec::new()
};
let proof = self.finalize_proof(proof);
let receipt = self.assemble_receipt(q, &[], false, &proof, DecisionKind::Deny);
Decision {
kind: DecisionKind::Deny,
bindings: Vec::new(),
proof,
receipt,
depth_exceeded: false,
}
}
fn finalize_proof(&self, mut proof: Vec<ProofNode>) -> Vec<ProofNode> {
for node in &mut proof {
node.node_hash = node.canonical_hash();
}
proof
}
fn assemble_receipt(
&self,
q: &QueryAtom8,
bindings: &[TermId],
depth_exceeded: bool,
proof: &[ProofNode],
decision: DecisionKind,
) -> Receipt {
let catalog_root = self.catalog.catalog_root();
let rule_root = self.rule_root();
let fact_root = self.fact_root();
let input_root = Self::input_root(q);
let proof_root = Self::proof_root(proof);
let output_root = Self::output_root(q, bindings, depth_exceeded);
let mut receipt = Receipt {
engine_version: crate::ENGINE_VERSION.to_string(),
catalog_root,
rule_root,
fact_root,
input_root,
proof_root,
output_root,
decision,
epoch: q.epoch,
receipt_hash: [0u8; 32],
};
receipt.receipt_hash = receipt.compute_hash();
receipt
}
/// Hash a single atom into `hasher`: pred_id, arity, binding mask, then
/// every live argument term. Fixed-width fields — no extra length prefix
/// needed because the arg count is determined by `arity`.
fn hash_atom_into(hasher: &mut blake3::Hasher, a: &Atom8) {
hasher.update(&a.pred_id.0.to_le_bytes());
hasher.update(&[a.arity, a.binding_mask]);
for i in 0..a.arity as usize {
hasher.update(&a.args[i].0.to_le_bytes());
}
}
fn rule_root(&self) -> Hash {
let mut hasher = blake3::Hasher::new_keyed(&DOMAIN_PROLOG8_RULES);
hasher.update(&(self.rules.len() as u32).to_le_bytes());
for r in &self.rules {
hasher.update(&r.rule_id.0.to_le_bytes());
hasher.update(&[r.head.arity, r.body_len, r.var_count]);
hasher.update(&[r.body_mask, r.negation_mask, r.builtin_mask, r.feature_mask]);
// Head atom in full (pred + mask + args), then the count-prefixed
// body atoms in full. Binding the bodies closes the gap where rule
// BODIES were unbound in receipts (only head id/arity/masks were
// hashed).
Self::hash_atom_into(&mut hasher, &r.head);
hasher.update(&[r.body_len]);
for bi in 0..r.body_len as usize {
Self::hash_atom_into(&mut hasher, &r.body[bi]);
}
}
hasher.finalize().into()
}
fn fact_root(&self) -> Hash {
let mut roots: Vec<&Hash> = Vec::new();
for b in &self.fact_blocks {
roots.push(&b.metadata.fact_root);
}
if roots.is_empty() {
return [0u8; 32];
}
combine_roots(&roots)
}
fn input_root(q: &QueryAtom8) -> Hash {
let mut hasher = blake3::Hasher::new_keyed(&DOMAIN_PROLOG8_INPUT);
hasher.update(&q.atom.pred_id.0.to_le_bytes());
hasher.update(&[q.atom.arity, q.atom.binding_mask, q.output_mask]);
for i in 0..q.atom.arity as usize {
hasher.update(&q.atom.args[i].0.to_le_bytes());
}
hasher.update(&q.epoch.0.to_le_bytes());
hasher.finalize().into()
}
fn proof_root(proof: &[ProofNode]) -> Hash {
let mut hasher = blake3::Hasher::new_keyed(&DOMAIN_PROLOG8_PROOF_ROOT);
hasher.update(&(proof.len() as u32).to_le_bytes());
for n in proof {
hasher.update(&n.node_hash);
}
hasher.finalize().into()
}
/// Output root binds the actual `Decision.bindings` (count-prefixed terms)
/// plus the depth-truncation marker. Previously it hashed only the output
/// mask and proof fact-hashes, leaving the bindings themselves unbound.
fn output_root(q: &QueryAtom8, bindings: &[TermId], depth_exceeded: bool) -> Hash {
let mut hasher = blake3::Hasher::new_keyed(&DOMAIN_PROLOG8_OUTPUT);
hasher.update(&q.output_mask.to_le_bytes());
hasher.update(&(bindings.len() as u32).to_le_bytes());
for b in bindings {
hasher.update(&b.0.to_le_bytes());
}
hasher.update(&[depth_exceeded as u8]);
hasher.finalize().into()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::catalog::{PredicateMeta, PredicateProofPolicy};
use crate::types::{
Atom8, CatalogId, EpochId, FactBlock8, FactRow8, FeatureBit, PlanId, PredicateId,
QueryAtom8, RuleId, SourceId,
};
fn build_kernel() -> Kernel {
let mut cat = Catalog::new(CatalogId(1));
cat.add_predicate(PredicateMeta {
pred_id: PredicateId(1),
label: "parent".into(),
arity: 2,
access_orders: vec![],
proof_policy: PredicateProofPolicy::OnRequest,
materialized: false,
});
cat.add_predicate(PredicateMeta {
pred_id: PredicateId(2),
label: "ancestor".into(),
arity: 2,
access_orders: vec![],
proof_policy: PredicateProofPolicy::OnRequest,
materialized: false,
});
let alice = cat.intern_term("alice");
let bob = cat.intern_term("bob");
let carol = cat.intern_term("carol");
let mut k = Kernel::new(cat);
let rows = vec![
FactRow8::new(PredicateId(1), 2, &[alice, bob], SourceId(0)),
FactRow8::new(PredicateId(1), 2, &[bob, carol], SourceId(0)),
];
k.load_facts(FactBlock8::new(PredicateId(1), 2, rows))
.unwrap();
k
}
#[test]
fn query_known_fact_returns_allow_with_proof() {
let k = build_kernel();
let alice = k.catalog.term_id("alice").unwrap();
let bob = k.catalog.term_id("bob").unwrap();
let mut q_atom = Atom8::new(PredicateId(1), 2, &[alice, bob]);
q_atom.binding_mask = 0b11;
let q = QueryAtom8 {
atom: q_atom,
output_mask: 0,
proof_mode: ProofMode::Both,
epoch: EpochId(0),
};
match k.query(&q) {
QueryResult::Answered(answers) => {
assert_eq!(answers.len(), 1);
assert_eq!(answers[0].kind, DecisionKind::Allow);
assert!(!answers[0].proof.is_empty());
assert_ne!(answers[0].receipt.receipt_hash, [0u8; 32]);
}
other => unreachable!("expected Answered, got {other:?}"),
}
}
#[test]
fn query_unknown_fact_returns_deny_with_negative_proof() {
let k = build_kernel();
let alice = k.catalog.term_id("alice").unwrap();
let carol = k.catalog.term_id("carol").unwrap();
let mut q_atom = Atom8::new(PredicateId(1), 2, &[alice, carol]);
q_atom.binding_mask = 0b11;
let q = QueryAtom8 {
atom: q_atom,
output_mask: 0,
proof_mode: ProofMode::Both,
epoch: EpochId(0),
};
match k.query(&q) {
QueryResult::Denied(d) => {
assert_eq!(d.kind, DecisionKind::Deny);
assert_eq!(d.proof.len(), 1);
assert_eq!(d.proof[0].kind, ProofKind::MissingFact);
assert_eq!(d.bindings.len(), 0);
}
other => unreachable!("expected Denied, got {other:?}"),
}
}
#[test]
fn query_with_oversize_arity_returns_invalid() {
let k = build_kernel();
// Construct an atom whose arity disagrees with catalog.
let mut bad = Atom8::new(PredicateId(1), 3, &[TermId(1), TermId(2), TermId(3)]);
bad.binding_mask = 0b111;
let q = QueryAtom8 {
atom: bad,
output_mask: 0,
proof_mode: ProofMode::PositiveOnly,
epoch: EpochId(0),
};
match k.query(&q) {
QueryResult::Invalid(code) => assert_eq!(code, RejectionCode::ArityMismatch),
other => unreachable!("expected Invalid, got {other:?}"),
}
}
#[test]
fn rule_one_step_chain_yields_answer() {
let mut k = build_kernel();
let alice = k.catalog.term_id("alice").unwrap();
let bob = k.catalog.term_id("bob").unwrap();
// Define a rule: ancestor(alice, bob) :- parent(alice, bob).
let head = Atom8::new(PredicateId(2), 2, &[alice, bob]);
let body = Atom8::new(PredicateId(1), 2, &[alice, bob]);
let mut body_arr = [Atom8::new(PredicateId(1), 0, &[]); 8];
body_arr[0] = body;
let rule = Rule8 {
rule_id: RuleId(1),
head,
body: body_arr,
body_len: 1,
body_mask: 0b1,
negation_mask: 0,
builtin_mask: 0,
var_count: 0,
var_live_mask: 0,
feature_mask: FeatureBit::Facts.mask() | FeatureBit::HornRules.mask(),
proof_mask: 0,
plan_id: PlanId::default(),
};
k.load_rule(rule).unwrap();
// Query: ancestor(alice, bob)?
let mut q_atom = Atom8::new(PredicateId(2), 2, &[alice, bob]);
q_atom.binding_mask = 0b11;
let q = QueryAtom8 {
atom: q_atom,
output_mask: 0,
proof_mode: ProofMode::Both,
epoch: EpochId(0),
};
match k.query(&q) {
QueryResult::Answered(answers) => {
assert_eq!(answers.len(), 1);
assert_eq!(answers[0].kind, DecisionKind::Allow);
// Proof must contain at least the rule node + one fact node.
assert!(answers[0].proof.len() >= 2);
let rule_nodes: Vec<&ProofNode> = answers[0]
.proof
.iter()
.filter(|n| n.kind == ProofKind::Rule)
.collect();
assert_eq!(rule_nodes.len(), 1);
}
other => unreachable!("expected Answered, got {other:?}"),
}
}
#[test]
fn receipt_is_deterministic_across_runs() {
let k = build_kernel();
let alice = k.catalog.term_id("alice").unwrap();
let bob = k.catalog.term_id("bob").unwrap();
let mut q_atom = Atom8::new(PredicateId(1), 2, &[alice, bob]);
q_atom.binding_mask = 0b11;
let q = QueryAtom8 {
atom: q_atom,
output_mask: 0,
proof_mode: ProofMode::PositiveOnly,
epoch: EpochId(0),
};
let r1 = match k.query(&q) {
QueryResult::Answered(a) => a[0].receipt.clone(),
_ => unreachable!(),
};
let r2 = match k.query(&q) {
QueryResult::Answered(a) => a[0].receipt.clone(),
_ => unreachable!(),
};
assert_eq!(r1.receipt_hash, r2.receipt_hash);
assert_eq!(r1.proof_root, r2.proof_root);
}
/// Grandparent via shared-variable SLD unification.
///
/// This test is UNFAKEABLE without shared-variable unification: ?1 appears
/// in both body atoms and must unify to the same intermediate value.
///
/// Rule: grandparent(?0, ?2) :- parent(?0, ?1), parent(?1, ?2)
/// Facts: parent(alice, bob), parent(bob, carol)
/// Query: grandparent(alice, ?) → should derive carol
#[test]
fn test_grandparent_unification() {
let mut cat = Catalog::new(CatalogId(1));
cat.add_predicate(PredicateMeta {
pred_id: PredicateId(10),
label: "parent".into(),
arity: 2,
access_orders: vec![],
proof_policy: PredicateProofPolicy::OnRequest,
materialized: false,
});
cat.add_predicate(PredicateMeta {
pred_id: PredicateId(11),
label: "grandparent".into(),
arity: 2,
access_orders: vec![],
proof_policy: PredicateProofPolicy::OnRequest,
materialized: false,
});
let alice = cat.intern_term("alice");
let bob = cat.intern_term("bob");
let carol = cat.intern_term("carol");
let mut k = Kernel::new(cat);
// Load facts: parent(alice, bob) and parent(bob, carol)
let rows = vec![
FactRow8::new(PredicateId(10), 2, &[alice, bob], SourceId(0)),
FactRow8::new(PredicateId(10), 2, &[bob, carol], SourceId(0)),
];
k.load_facts(FactBlock8::new(PredicateId(10), 2, rows))
.unwrap();
// Rule: grandparent(?0, ?2) :- parent(?0, ?1), parent(?1, ?2)
// Use sentinel (TermId(0)) to mark variable positions:
// head: grandparent(sentinel_0=?0, sentinel_2=?2)
// body[0]: parent(sentinel_0=?0, sentinel_1=?1)
// body[1]: parent(sentinel_1=?1, sentinel_2=?2)
//
// Since our convention maps arg position i to var ?i via sentinel,
// we need head arity=3 to have positions 0,1,2. But grandparent arity=2.
// We represent ?0=pos0 and ?2=pos1 in head. For body, each body atom
// independently maps pos→var. The "shared" var ?1 must be encoded
// consistently: in body[0] pos1=?1, in body[1] pos0=?1.
//
// Our implementation treats sentinel at body atom position ai as var(ai).
// So for the chain:
// body[0]: parent(sentinel@0, sentinel@1) → parent(?0, ?1)
// body[1]: parent(sentinel@0, sentinel@1) → parent(?0, ?1) — WRONG
//
// We need a different encoding. Let's use a dedicated body atom arity
// approach where we put concrete values in bound positions.
// Actually the test must be done differently: we need to load rule
// with alice bound in head pos 0, and use unbound pos 1 in head.
// Then body[0] = parent(alice, ?) and body[1] = parent(?, ?).
// This still requires binding propagation.
//
// Simpler: do it ground-per-instance. Load the rule as
// grandparent(alice, carol) :- parent(alice, bob), parent(bob, carol)
// but with head having alice at pos 0 (bound) and sentinel at pos 1.
// Query: grandparent(alice, ?) → should return carol.
//
// This still exercises shared-variable binding: bob must unify across
// the two body atoms.
//
// Encoding: head pos0=alice (ground), pos1=sentinel(?1)
// body[0]: parent(alice, sentinel@1=?1) [bound pos0=alice]
// body[1]: parent(sentinel@0=?0, carol) [bound pos1=carol]
// — still doesn't chain ?1 between body atoms.
//
// Real encoding requires that body[0] pos1 and body[1] pos0 share a var.
// We achieve this by making body[0] fully unbound (output all) and
// letting the kernel bind ?1=bob from fact, then body[1] uses that binding.
//
// In our sentinel scheme: sentinel at position ai in a body atom = var(ai).
// So body[0] = parent(alice_ground, sentinel@1) → sentinel@1 = ?1
// body[1] = parent(sentinel@0, sentinel@1) → sentinel@0=?0, sentinel@1=?1
// After body[0] matches parent(alice, bob): subst[1] = bob
// body[1] now resolves to parent(?0, bob). That matches parent(bob, carol) → binds ?0=bob — WRONG for our query.
//
// The correct encoding for grandparent(?0, ?2) :- parent(?0, ?1), parent(?1, ?2):
// body[0]: parent(sentinel@0, sentinel@1) — pos0=?0, pos1=?1
// body[1]: parent(sentinel@1, sentinel@2) — BUT sentinel at pos0 != sentinel at pos1
//
// Our scheme only maps pos i → var i. We can't naturally express "body[1] pos0 = var ?1".
//
// SOLUTION: Encode differently. Use a ground query approach:
// Query grandparent(alice, unbound)
// head: grandparent(alice, sentinel@1) — alice ground, pos1 unbound
// body[0]: parent(alice, sentinel@1) — alice ground, pos1=?1
// body[1]: parent(sentinel@0, sentinel@1)
//
// After body[0] matches parent(alice,bob): subst[1]=bob
// body[1]: parent(sentinel@0, bob-resolved) → parent(?0, bob) → matches parent(bob, carol)? NO.
// we need parent(bob, carol), but pos1=bob means we'd look for parent(?0, bob).
// That doesn't match parent(bob, carol) because carol≠bob.
//
// The fundamental issue: our pos→var scheme can only represent vars where
// the var index equals the argument position. For grandparent, ?1 appears
// at pos1 in body[0] but at pos0 in body[1]. These are different positions.
//
// TRUE Robinson unification requires named variables independent of position.
// The spec says "?N positional variables" where ?N = position in the HEAD.
// Let's re-read: "Variable ID = position index (0-7)". This means variable ?1
// is the variable at position 1 of the HEAD, not of the body atom.
//
// So the rule grandparent:?0,?2 :- parent:?0,?1, parent:?1,?2 means:
// head var0=head_pos0, head var2=head_pos2
// body[0] var0=head_pos0, var1=new_var — but how encoded in atoms?
//
// The spec says premises in rule strings are like "parent:?0,?1" where
// ?0 and ?1 are NAMED variables shared across the rule. This is NOT
// positional-in-body but positional-in-variable-namespace.
//
// For the kernel-level test we need a way to encode this. Let's use
// the var_count/var_live_mask fields plus a separate variable encoding.
//
// PRAGMATIC APPROACH: Test the grandparent chain using a 3-step ground
// query that exercises the body-atom variable propagation we DO implement:
// Query: grandparent(alice, ?)
// head: grandparent(alice, sentinel@1) — alice at pos0, ?1 at pos1
// body[0]: parent(alice, sentinel@1) — alice bound, ?1 unbound
// body[1]: parent(sentinel@1, sentinel@1) — WRONG, this makes both args the same var
//
// We cannot express grandparent properly with pos→var. Instead, let's
// test what the spec actually requires: that ?N in premise strings maps
// to named variables across body atoms. Since prolog.rs parses string rules,
// let's test at the prolog.rs level and at kernel level do a simpler but
// valid chain test.
//
// For the kernel test, demonstrate shared-variable binding by testing
// a rule where the SAME variable appears in body[0] pos1 and body[1] pos0.
// We need to extend the kernel to support explicit variable IDs, not
// just pos→var. Let's implement this properly.
//
// Actually, looking at the spec more carefully: the rule head
// "grandparent:?0,?1" means head args are (?0, ?1). In body:
// "parent:?0,?2" means first body atom has args (?0, ?2).
// ?0 is SHARED between head and body[0].
// ?2 is a new variable appearing in body[0] pos1.
// "parent:?2,?1" means second body atom has args (?2, ?1).
// ?2 shared between body[0] pos1 and body[1] pos0. THIS IS THE KEY.
//
// So the variable encoding in atoms needs to carry the VARIABLE ID,
// not the position. Let's use a different sentinel scheme:
// var ?N is encoded as TermId(VAR_SENTINEL_BASE + N) where N is the
// variable index (0-7), regardless of which arg position it occupies.
//
// In Atom8 args: if args[i] = TermId(VAR_SENTINEL_BASE + N), then
// arg position i holds variable ?N.
//
// For grandparent(?0, ?1) :- parent(?0, ?2), parent(?2, ?1):
// head.args = [var(0), var(1), sentinel, ...]
// body[0].args = [var(0), var(2), sentinel, ...]
// body[1].args = [var(2), var(1), sentinel, ...]
//
// Query: grandparent(alice, ?) means head.args[0]=alice (ground), args[1]=unbound
// Initial subst: unify head.args[0]=var(0) with alice → subst[0]=alice
// unify head.args[1]=var(1) with unbound query → subst[1]=None
// body[0]: parent(var(0), var(2)) → parent(alice, ?) → matches parent(alice,bob) → subst[2]=bob
// body[1]: parent(var(2), var(1)) → parent(bob, ?) → matches parent(bob,carol) → subst[1]=carol
// Result: head args resolved → (alice, carol) ✓
//
// This is what we should implement. The VAR_SENTINEL_BASE scheme with
// var_term(N) encoding enables this.
//
// The kernel already defines var_term/var_index with VAR_SENTINEL_BASE.
// We just need to build the Rule8 with proper var-encoded args.
// Build grandparent rule using VAR_SENTINEL scheme:
// var(0) = TermId(0x8000_0000), var(1) = TermId(0x8000_0001), var(2) = TermId(0x8000_0002)
const VAR_BASE: u32 = 0x8000_0000;
let v0 = TermId(VAR_BASE);
let v1 = TermId(VAR_BASE + 1);
let v2 = TermId(VAR_BASE + 2);
// head: grandparent(v0, v1)
let head = Atom8::new(PredicateId(11), 2, &[v0, v1]);
// body[0]: parent(v0, v2)
let body0 = Atom8::new(PredicateId(10), 2, &[v0, v2]);
// body[1]: parent(v2, v1)
let body1 = Atom8::new(PredicateId(10), 2, &[v2, v1]);
let mut body_arr = [Atom8::new(PredicateId(0), 0, &[]); 8];
body_arr[0] = body0;
body_arr[1] = body1;
let rule = Rule8 {
rule_id: RuleId(42),
head,
body: body_arr,
body_len: 2,
body_mask: 0b11,
negation_mask: 0,
builtin_mask: 0,
var_count: 3, // vars ?0, ?1, ?2
var_live_mask: 0b11, // ?0 and ?1 appear in head
feature_mask: FeatureBit::Facts.mask() | FeatureBit::HornRules.mask(),
proof_mask: 0,
plan_id: PlanId::default(),
};
k.load_rule(rule).unwrap();
// Query: grandparent(alice, ?) — alice bound, pos1 unbound
let mut q_atom = Atom8::new(PredicateId(11), 2, &[alice, TermId::sentinel()]);
q_atom.binding_mask = 0b01; // only pos0 (alice) is bound
let q = QueryAtom8 {
atom: q_atom,
output_mask: 0b10, // request pos1 as output
proof_mode: ProofMode::Both,
epoch: EpochId(0),
};
match k.query(&q) {
QueryResult::Answered(answers) => {
assert!(
!answers.is_empty(),
"grandparent(alice, ?) must derive an answer"
);
let first = &answers[0];
assert_eq!(first.kind, DecisionKind::Allow);
// The bound output binding at pos1 should be carol.
assert_eq!(
first.bindings.first().copied(),
Some(carol),
"grandparent(alice, ?) should derive carol via shared-variable unification"
);
}
other => unreachable!("expected Answered, got {other:?}"),
}
}
/// Variable binding extraction: rule with ?0 in head, query with unbound position.
#[test]
fn test_variable_binding_extraction() {
let mut cat = Catalog::new(CatalogId(1));
cat.add_predicate(PredicateMeta {
pred_id: PredicateId(20),
label: "source".into(),
arity: 1,
access_orders: vec![],
proof_policy: PredicateProofPolicy::OnRequest,
materialized: false,
});
cat.add_predicate(PredicateMeta {
pred_id: PredicateId(21),
label: "derived".into(),
arity: 1,
access_orders: vec![],
proof_policy: PredicateProofPolicy::OnRequest,
materialized: false,
});
let foo = cat.intern_term("foo");
let mut k = Kernel::new(cat);
let rows = vec![FactRow8::new(PredicateId(20), 1, &[foo], SourceId(0))];
k.load_facts(FactBlock8::new(PredicateId(20), 1, rows))
.unwrap();
// Rule: derived(?0) :- source(?0)
const VAR_BASE: u32 = 0x8000_0000;
let v0 = TermId(VAR_BASE);
let head = Atom8::new(PredicateId(21), 1, &[v0]);
let body0 = Atom8::new(PredicateId(20), 1, &[v0]);
let mut body_arr = [Atom8::new(PredicateId(0), 0, &[]); 8];
body_arr[0] = body0;
let rule = Rule8 {
rule_id: RuleId(100),
head,
body: body_arr,
body_len: 1,
body_mask: 0b1,
negation_mask: 0,
builtin_mask: 0,
var_count: 1,
var_live_mask: 0b1,
feature_mask: FeatureBit::Facts.mask() | FeatureBit::HornRules.mask(),
proof_mask: 0,
plan_id: PlanId::default(),
};
k.load_rule(rule).unwrap();
// Query: derived(?) — unbound, request output
let q_atom = Atom8::new(PredicateId(21), 1, &[TermId::sentinel()]);
let q = QueryAtom8 {
atom: q_atom,
output_mask: 0b1,
proof_mode: ProofMode::Both,
epoch: EpochId(0),
};
match k.query(&q) {
QueryResult::Answered(answers) => {
assert!(!answers.is_empty());
// Should get back foo
assert_eq!(answers[0].bindings.first().copied(), Some(foo));
}
other => unreachable!("expected Answered, got {other:?}"),
}
}
/// Same input must produce bit-exact serde_json output (receipt-hash determinism).
#[test]
fn test_double_run_deterministic() {
let k = build_kernel();
let alice = k.catalog.term_id("alice").unwrap();
let bob = k.catalog.term_id("bob").unwrap();
let mut q_atom = Atom8::new(PredicateId(1), 2, &[alice, bob]);
q_atom.binding_mask = 0b11;
let q = QueryAtom8 {
atom: q_atom,
output_mask: 0,
proof_mode: ProofMode::PositiveOnly,
epoch: EpochId(0),
};
let r1 = match k.query(&q) {
QueryResult::Answered(a) => serde_json::to_string(&a[0]).unwrap(),
_ => unreachable!(),
};
let r2 = match k.query(&q) {
QueryResult::Answered(a) => serde_json::to_string(&a[0]).unwrap(),
_ => unreachable!(),
};
assert_eq!(
r1, r2,
"same input must produce bit-exact serde_json output"
);
}
/// Rule where head has a variable not appearing in any body atom terminates
/// without panic within visited-set cap.
#[test]
fn test_head_var_not_in_body_terminates() {
let mut k = build_kernel();
let alice = k.catalog.term_id("alice").unwrap();
let bob = k.catalog.term_id("bob").unwrap();
// Rule: ancestor(alice, ?0) :- parent(alice, bob)
// ?0 in head not constrained by body → head var unbound after body passes.
const VAR_BASE: u32 = 0x8000_0000;
let v0 = TermId(VAR_BASE);
let head = Atom8::new(PredicateId(2), 2, &[alice, v0]);
let body0 = Atom8::new(PredicateId(1), 2, &[alice, bob]);
let mut body_arr = [Atom8::new(PredicateId(1), 0, &[]); 8];
body_arr[0] = body0;
let rule = Rule8 {
rule_id: RuleId(200),
head,
body: body_arr,
body_len: 1,
body_mask: 0b1,
negation_mask: 0,
builtin_mask: 0,
var_count: 1,
var_live_mask: 0b10,
feature_mask: FeatureBit::Facts.mask() | FeatureBit::HornRules.mask(),
proof_mask: 0,
plan_id: PlanId::default(),
};
k.load_rule(rule).unwrap();
let mut q_atom = Atom8::new(PredicateId(2), 2, &[alice, TermId::sentinel()]);
q_atom.binding_mask = 0b01;
let q = QueryAtom8 {
atom: q_atom,
output_mask: 0b10,
proof_mode: ProofMode::Both,
epoch: EpochId(0),
};
// Must terminate without panic.
let _ = k.query(&q);
}
/// Every Deny decision MUST carry a non-zero BLAKE3 receipt hash.
///
/// Rationale: A zero receipt hash is indistinguishable from an
/// uninitialized or fabricated receipt (FM-5 / receipt-forgery attack).
/// The kernel commits to BLAKE3(input_root ‖ proof_root ‖ "deny"),
/// which is non-zero for any non-trivially-colliding input — this is
/// guaranteed by BLAKE3's preimage resistance (a collision against the
/// all-zero output would break the hash function).
///
/// This test is the most critical single correctness gate for
/// `Kernel::query`: if the deny path skips receipt assembly, every
/// policy enforcement point that trusts the receipt is bypassed.
#[test]
fn deny_decision_emits_nonzero_receipt_hash() {
let k = build_kernel();
let alice = k.catalog.term_id("alice").unwrap();
let carol = k.catalog.term_id("carol").unwrap();
// alice→carol is NOT in the fact block, so query must return Denied.
let mut q_atom = Atom8::new(PredicateId(1), 2, &[alice, carol]);
q_atom.binding_mask = 0b11;
let q = QueryAtom8 {
atom: q_atom,
output_mask: 0,
proof_mode: ProofMode::NegativeOnly,
epoch: EpochId(0),
};
match k.query(&q) {
QueryResult::Denied(d) => {
assert_eq!(d.kind, DecisionKind::Deny);
assert_ne!(
d.receipt.receipt_hash, [0u8; 32],
"Deny receipt_hash must be non-zero (BLAKE3 preimage resistance)"
);
}
other => unreachable!("expected Denied, got {other:?}"),
}
}
/// Backtracking test: solver must explore all intermediate nodes.
///
/// Facts: link(a,b), link(a,c), link(b,END), link(c,END)
/// Rule: via(?0, ?1) :- link(a, ?0), link(?0, ?1)
/// Query: via(?, END) — unbound on pos-0 (intermediate node)
///
/// Backtracking must find BOTH b and c as valid intermediates.
/// Old greedy code found only b (first match); correct SLD finds both.
/// Using the intermediate as a head variable gives DISTINCT bindings per path,
/// proving exhaustive search without relying on proof-count semantics.
#[test]
fn test_backtracking_finds_all_answers() {
let mut cat = Catalog::new(CatalogId(99));
cat.add_predicate(PredicateMeta {
pred_id: PredicateId(1),
label: "link".into(),
arity: 2,
access_orders: vec![],
proof_policy: PredicateProofPolicy::OnRequest,
materialized: false,
});
cat.add_predicate(PredicateMeta {
pred_id: PredicateId(2),
label: "via".into(),
arity: 2,
access_orders: vec![],
proof_policy: PredicateProofPolicy::OnRequest,
materialized: false,
});
let a = cat.intern_term("a");
let b = cat.intern_term("b");
let c = cat.intern_term("c");
let end = cat.intern_term("END");
let mut k = Kernel::new(cat);
let rows = vec![
FactRow8::new(PredicateId(1), 2, &[a, b], SourceId(0)),
FactRow8::new(PredicateId(1), 2, &[a, c], SourceId(0)),
FactRow8::new(PredicateId(1), 2, &[b, end], SourceId(0)),
FactRow8::new(PredicateId(1), 2, &[c, end], SourceId(0)),
];
k.load_facts(FactBlock8::new(PredicateId(1), 2, rows))
.unwrap();
// Rule: via(?0, ?1) :- link(a, ?0), link(?0, ?1)
// ?0 = intermediate node (appears in head — distinct per path)
// ?1 = destination (END for both paths)
const VAR_BASE: u32 = 0x8000_0000;
let v0 = TermId(VAR_BASE);
let v1 = TermId(VAR_BASE + 1);
let head = Atom8::new(PredicateId(2), 2, &[v0, v1]);
let body0 = Atom8::new(PredicateId(1), 2, &[a, v0]); // link(a, ?0)
let body1 = Atom8::new(PredicateId(1), 2, &[v0, v1]); // link(?0, ?1)
let mut body_arr = [Atom8::new(PredicateId(0), 0, &[]); 8];
body_arr[0] = body0;
body_arr[1] = body1;
let rule = Rule8 {
rule_id: RuleId(99),
head,
body: body_arr,
body_len: 2,
body_mask: 0b11,
negation_mask: 0,
builtin_mask: 0,
var_count: 2,
var_live_mask: 0b11,
feature_mask: FeatureBit::Facts.mask() | FeatureBit::HornRules.mask(),
proof_mask: 0,
plan_id: PlanId::default(),
};
k.load_rule(rule).unwrap();
// Query: via(?, END) — find all intermediates reachable from a to END
let mut q_atom = Atom8::new(PredicateId(2), 2, &[TermId::sentinel(), end]);
q_atom.binding_mask = 0b10; // pos-1 (END) bound, pos-0 (intermediate) unbound
let q = QueryAtom8 {
atom: q_atom,
output_mask: 0b01, // output pos-0 (the intermediate node)
proof_mode: ProofMode::PositiveOnly,
epoch: EpochId(0),
};
match k.query(&q) {
QueryResult::Answered(answers) => {
assert_eq!(
answers.len(),
2,
"backtracking must find both intermediates (b and c); got {}",
answers.len()
);
let intermediates: Vec<TermId> = answers
.iter()
.filter_map(|a| a.bindings.first().copied())
.collect();
assert!(
intermediates.contains(&b),
"b must be found as intermediate"
);
assert!(
intermediates.contains(&c),
"c must be found as intermediate"
);
}
other => panic!("expected Answered, got {other:?}"),
}
}
/// Depth cap: hitting MAX_DERIVE_DEPTH must surface a typed marker on the
/// Decision (depth_exceeded = true), not silently return empty/Denied.
#[test]
fn depth_exceeded_is_visible_in_decision() {
let mut cat = Catalog::new(CatalogId(1));
for (pid, label) in [
(PredicateId(1), "base"),
(PredicateId(2), "p"),
] {
cat.add_predicate(PredicateMeta {
pred_id: pid,
label: label.into(),
arity: 1,
access_orders: vec![],
proof_policy: PredicateProofPolicy::OnRequest,
materialized: false,
});
}
let x = cat.intern_term("x");
let mut k = Kernel::new(cat);
// Fact: base(x). No fact for p. Recursive rule with no terminating
// base case: p(?0) :- p(?0). Deriving p(x) recurses forever → cap.
k.load_facts(FactBlock8::new(
PredicateId(1),
1,
vec![FactRow8::new(PredicateId(1), 1, &[x], SourceId(0))],
))
.unwrap();
let v0 = var_term(0);
let head = Atom8::new(PredicateId(2), 1, &[v0]);
let body0 = Atom8::new(PredicateId(2), 1, &[v0]);
let mut body_arr = [Atom8::new(PredicateId(0), 0, &[]); 8];
body_arr[0] = body0;
let rule = Rule8 {
rule_id: RuleId(1),
head,
body: body_arr,
body_len: 1,
body_mask: 0b1,
negation_mask: 0,
builtin_mask: 0,
var_count: 1,
var_live_mask: 0b1,
feature_mask: FeatureBit::Facts.mask() | FeatureBit::BoundedRecursion.mask(),
proof_mask: 0,
plan_id: PlanId::default(),
};
k.load_rule(rule).unwrap();
let q_atom = Atom8::new(PredicateId(2), 1, &[x]);
let q = QueryAtom8 {
atom: q_atom,
output_mask: 0,
proof_mode: ProofMode::Both,
epoch: EpochId(0),
};
let (result, _misses, _hits) = k.query_with_stats(&q);
match result {
QueryResult::Denied(d) => assert!(
d.depth_exceeded,
"depth cap hit must be visible on the Decision"
),
QueryResult::Answered(a) => panic!("unexpected answers {a:?}"),
other => panic!("unexpected {other:?}"),
}
}
// ------------------------------------------------------------------
// Validation-gap upgrades (u5 lane)
// ------------------------------------------------------------------
/// NAF groundness: a negated body atom with an unbound variable must be a
/// typed refusal (`NegationFloundering`), never a silent false.
#[test]
fn negation_with_unbound_var_is_typed_refusal() {
let mut cat = Catalog::new(CatalogId(1));
for (pid, label) in [(PredicateId(1), "q"), (PredicateId(2), "r"), (PredicateId(3), "p")] {
cat.add_predicate(PredicateMeta {
pred_id: pid,
label: label.into(),
arity: 1,
access_orders: vec![],
proof_policy: PredicateProofPolicy::OnRequest,
materialized: false,
});
}
let x = cat.intern_term("x");
let mut k = Kernel::new(cat);
k.load_facts(FactBlock8::new(
PredicateId(1),
1,
vec![FactRow8::new(PredicateId(1), 1, &[x], SourceId(0))],
))
.unwrap();
// Rule: p(?0) :- q(?0), not r(?1) — ?1 never bound → must flounder.
let v0 = var_term(0);
let v1 = var_term(1);
let head = Atom8::new(PredicateId(3), 1, &[v0]);
let body0 = Atom8::new(PredicateId(1), 1, &[v0]);
let body1 = Atom8::new(PredicateId(2), 1, &[v1]);
let mut body_arr = [Atom8::new(PredicateId(0), 0, &[]); 8];
body_arr[0] = body0;
body_arr[1] = body1;
let rule = Rule8 {
rule_id: RuleId(1),
head,
body: body_arr,
body_len: 2,
body_mask: 0b11,
negation_mask: 0b10,
builtin_mask: 0,
var_count: 2,
var_live_mask: 0b1,
feature_mask: FeatureBit::Facts.mask() | FeatureBit::StratifiedNegation.mask(),
proof_mask: 0,
plan_id: PlanId::default(),
};
k.load_rule(rule).unwrap();
let q_atom = Atom8::new(PredicateId(3), 1, &[TermId::sentinel()]);
let q = QueryAtom8 {
atom: q_atom,
output_mask: 0b1,
proof_mode: ProofMode::PositiveOnly,
epoch: EpochId(0),
};
match k.query(&q) {
QueryResult::Invalid(RejectionCode::NegationFloundering) => {}
other => panic!("expected NegationFloundering refusal, got {other:?}"),
}
}
/// Ground negation still evaluates: `not r(x)` with r(x) absent succeeds,
/// `not r(x)` with r(x) present fails — no floundering refusal.
#[test]
fn ground_negation_still_evaluates() {
let mut cat = Catalog::new(CatalogId(1));
for (pid, label) in [(PredicateId(1), "q"), (PredicateId(2), "r"), (PredicateId(3), "p")] {
cat.add_predicate(PredicateMeta {
pred_id: pid,
label: label.into(),
arity: 1,
access_orders: vec![],
proof_policy: PredicateProofPolicy::OnRequest,
materialized: false,
});
}
let x = cat.intern_term("x");
let y = cat.intern_term("y");
let mut k = Kernel::new(cat);
k.load_facts(FactBlock8::new(
PredicateId(1),
1,
vec![FactRow8::new(PredicateId(1), 1, &[x], SourceId(0))],
))
.unwrap();
// r(y) exists, r(x) does not.
k.load_facts(FactBlock8::new(
PredicateId(2),
1,
vec![FactRow8::new(PredicateId(2), 1, &[y], SourceId(0))],
))
.unwrap();
// Rule: p(?0) :- q(?0), not r(?0) — ground at NAF time.
let v0 = var_term(0);
let head = Atom8::new(PredicateId(3), 1, &[v0]);
let body0 = Atom8::new(PredicateId(1), 1, &[v0]);
let body1 = Atom8::new(PredicateId(2), 1, &[v0]);
let mut body_arr = [Atom8::new(PredicateId(0), 0, &[]); 8];
body_arr[0] = body0;
body_arr[1] = body1;
let rule = Rule8 {
rule_id: RuleId(1),
head,
body: body_arr,
body_len: 2,
body_mask: 0b11,
negation_mask: 0b10,
builtin_mask: 0,
var_count: 1,
var_live_mask: 0b1,
feature_mask: FeatureBit::Facts.mask() | FeatureBit::StratifiedNegation.mask(),
proof_mask: 0,
plan_id: PlanId::default(),
};
k.load_rule(rule).unwrap();
let q_atom = Atom8::new(PredicateId(3), 1, &[TermId::sentinel()]);
let q = QueryAtom8 {
atom: q_atom,
output_mask: 0b1,
proof_mode: ProofMode::PositiveOnly,
epoch: EpochId(0),
};
match k.query(&q) {
QueryResult::Answered(a) => assert_eq!(a.len(), 1),
other => panic!("expected Answered for ground NAF, got {other:?}"),
}
}
/// rule_root must bind rule BODIES: two kernels with rules that share
/// rule_id, head, and masks but differ in a body atom must produce
/// different rule roots (and receipt hashes).
#[test]
fn rule_root_binds_rule_bodies() {
let (k1, q) = body_discrimination_kernel(PredicateId(1));
let (k2, _) = body_discrimination_kernel(PredicateId(2));
let r1 = match k1.query(&q) {
QueryResult::Answered(a) => a[0].receipt.clone(),
QueryResult::Denied(d) => d.receipt,
QueryResult::Invalid(_) => unreachable!(),
};
let r2 = match k2.query(&q) {
QueryResult::Answered(a) => a[0].receipt.clone(),
QueryResult::Denied(d) => d.receipt,
QueryResult::Invalid(_) => unreachable!(),
};
assert_ne!(
r1.rule_root, r2.rule_root,
"rule_root must change when a rule body changes"
);
assert_ne!(r1.receipt_hash, r2.receipt_hash);
}
/// Kernel factory for `rule_root_binds_rule_bodies`: builds identical
/// fact bases and a rule whose first body atom differs by pred_id.
fn body_discrimination_kernel(body0_pred: PredicateId) -> (Kernel, QueryAtom8) {
let mut cat = Catalog::new(CatalogId(1));
for (pid, label) in [
(PredicateId(1), "f1"),
(PredicateId(2), "f2"),
(PredicateId(3), "p"),
] {
cat.add_predicate(PredicateMeta {
pred_id: pid,
label: label.into(),
arity: 1,
access_orders: vec![],
proof_policy: PredicateProofPolicy::OnRequest,
materialized: false,
});
}
let x = cat.intern_term("x");
let mut k = Kernel::new(cat);
k.load_facts(FactBlock8::new(
PredicateId(1),
1,
vec![FactRow8::new(PredicateId(1), 1, &[x], SourceId(0))],
))
.unwrap();
k.load_facts(FactBlock8::new(
PredicateId(2),
1,
vec![FactRow8::new(PredicateId(2), 1, &[x], SourceId(0))],
))
.unwrap();
// Rule: p(?0) :- <body0_pred>(?0) — same head/ids/masks, body differs.
let v0 = var_term(0);
let head = Atom8::new(PredicateId(3), 1, &[v0]);
let body0 = Atom8::new(body0_pred, 1, &[v0]);
let mut body_arr = [Atom8::new(PredicateId(0), 0, &[]); 8];
body_arr[0] = body0;
let rule = Rule8 {
rule_id: RuleId(7),
head,
body: body_arr,
body_len: 1,
body_mask: 0b1,
negation_mask: 0,
builtin_mask: 0,
var_count: 1,
var_live_mask: 0b1,
feature_mask: FeatureBit::Facts.mask() | FeatureBit::HornRules.mask(),
proof_mask: 0,
plan_id: PlanId::default(),
};
k.load_rule(rule).unwrap();
let q_atom = Atom8::new(PredicateId(3), 1, &[x]);
let mut q_atom = q_atom;
q_atom.binding_mask = 0b1;
let q = QueryAtom8 {
atom: q_atom,
output_mask: 0,
proof_mode: ProofMode::PositiveOnly,
epoch: EpochId(0),
};
(k, q)
}
/// output_root must bind Decision.bindings: the same query against two
/// fact bases that yield different output bindings must yield different
/// output roots (the old root hashed only the mask + fact hashes).
#[test]
fn output_root_binds_decision_bindings() {
let mk = |rows: Vec<TermId>| -> (Kernel, QueryAtom8) {
let mut cat = Catalog::new(CatalogId(1));
cat.add_predicate(PredicateMeta {
pred_id: PredicateId(1),
label: "src".into(),
arity: 1,
access_orders: vec![],
proof_policy: PredicateProofPolicy::OnRequest,
materialized: false,
});
let a = cat.intern_term("a");
let b = cat.intern_term("b");
let terms = [a, b];
let (k, q) = {
let mut k = Kernel::new(cat);
k.load_facts(FactBlock8::new(
PredicateId(1),
1,
rows.into_iter()
.map(|t| FactRow8::new(PredicateId(1), 1, &[t], SourceId(0)))
.collect(),
))
.unwrap();
let q_atom = Atom8::new(PredicateId(1), 1, &[TermId::sentinel()]);
let q = QueryAtom8 {
atom: q_atom,
output_mask: 0b1,
proof_mode: ProofMode::PositiveOnly,
epoch: EpochId(0),
};
(k, q)
};
let _ = terms;
(k, q)
};
let (k1, q1) = mk(vec![TermId(101)]);
let (k2, q2) = mk(vec![TermId(202)]);
// Sanity: the two kernels use different catalogs so term ids don't collide.
let r1 = match k1.query(&q1) {
QueryResult::Answered(a) => a[0].receipt.clone(),
_ => unreachable!(),
};
let r2 = match k2.query(&q2) {
QueryResult::Answered(a) => a[0].receipt.clone(),
_ => unreachable!(),
};
assert_ne!(r1.output_root, r2.output_root);
assert_ne!(r1.receipt_hash, r2.receipt_hash);
}
#[test]
fn memoization_collapses_exponential_path_enumeration() {
// Layered DAG: width 2, 10 hops. Naive path count = 2^10 = 1024; distinct
// nodes = 22. The memo table must collapse repeated (goal, depth) subgoals.
const LEVELS: usize = 10;
let mut cat = Catalog::new(CatalogId(1));
for (pid, label, ar) in [
(PredicateId(1), "edge".to_string(), 2u8),
(PredicateId(2), "path".to_string(), 2u8),
] {
cat.add_predicate(PredicateMeta {
pred_id: pid,
label,
arity: ar,
access_orders: vec![],
proof_policy: PredicateProofPolicy::OnRequest,
materialized: false,
});
}
let node = |k: usize, w: usize| TermId(1000 + (k * 2 + w) as u32);
let mut rows = Vec::new();
for k in 0..LEVELS {
for w in 0..2usize {
for w2 in 0..2usize {
rows.push(FactRow8::new(
PredicateId(1),
2,
&[node(k, w), node(k + 1, w2)],
SourceId(0),
));
}
}
}
let mut k = Kernel::new(cat);
k.load_facts(FactBlock8::new(PredicateId(1), 2, rows))
.unwrap();
// Rule A: path(?0,?1) :- edge(?0,?1).
// Rule B: path(?0,?1) :- path(?0,?2), edge(?2,?1).
let v0 = var_term(0);
let v1 = var_term(1);
let v2 = var_term(2);
let mut b0 = [Atom8::new(PredicateId(0), 0, &[]); 8];
b0[0] = Atom8::new(PredicateId(1), 2, &[v0, v1]);
let mut b1 = [Atom8::new(PredicateId(0), 0, &[]); 8];
// edge(?0,?2) first, then the recursive path(?2,?1) — subgoals become
// the forward nodes, which are shared across derivation routes.
b1[0] = Atom8::new(PredicateId(1), 2, &[v0, v2]);
b1[1] = Atom8::new(PredicateId(2), 2, &[v2, v1]);
let mk_rule = |rid: u32, body: [Atom8; 8], body_len: u8| Rule8 {
rule_id: RuleId(rid),
head: Atom8::new(PredicateId(2), 2, &[v0, v1]),
body,
body_len,
body_mask: (1u16 << body_len) as u8 - 1,
negation_mask: 0,
builtin_mask: 0,
var_count: 3,
var_live_mask: 0b11,
feature_mask: FeatureBit::Facts.mask() | FeatureBit::HornRules.mask(),
proof_mask: 0,
plan_id: PlanId::default(),
};
k.load_rule(mk_rule(1, b0, 1)).unwrap();
k.load_rule(mk_rule(2, b1, 2)).unwrap();
// Query: path(L0_0, ?) — all reachable endpoints.
let q_atom = Atom8::new(PredicateId(2), 2, &[node(0, 0), TermId::sentinel()]);
let q = QueryAtom8 {
atom: q_atom,
output_mask: 0b10,
proof_mode: ProofMode::PositiveOnly,
epoch: EpochId(0),
};
let (result, misses, hits) = k.query_with_stats(&q);
let answers = match result {
QueryResult::Answered(a) => a,
other => panic!("expected Answered, got {other:?}"),
};
// Every layer's nodes are reachable.
let mut endpoints: Vec<u32> = answers
.iter()
.filter_map(|a| a.bindings.first().map(|t| t.0))
.collect();
endpoints.sort_unstable();
endpoints.dedup();
assert_eq!(endpoints.len(), 2 * LEVELS, "all layer nodes reachable");
for k in 1..=LEVELS {
for w in 0..2 {
assert!(endpoints.contains(&(node(k, w).0)));
}
}
// No duplicate derivations.
let n = answers.len();
for i in 0..n {
for j in (i + 1)..n {
assert_ne!(answers[i].bindings, answers[j].bindings);
assert_ne!(answers[i].receipt.receipt_hash, answers[j].receipt.receipt_hash);
}
}
// Naive SLD enumerates 2^LEVELS paths; memoization must collapse the
// repeated (goal, depth) subgoals to a small number of computations.
let naive_paths = 1usize << LEVELS;
assert!(
hits > 0,
"memo table must actually fire"
);
assert!(
(misses as usize) < naive_paths / 8,
"memo must collapse exponential re-derivation: misses={misses}, naive={naive_paths}"
);
}
}