eyemind 0.3.2 → 0.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -14,9 +14,9 @@ JavaScript library, and a browser application, with no dependencies.
14
14
  The guide shows how to run programs, write rules, check proofs, use the
15
15
  library and develop eyemind; the playground runs programs in your browser;
16
16
  the specification defines the language, its canonical text, the proof format
17
- and the C1–C9 checks. The examples are 94 runnable programs, each with a card
17
+ and the C1–C10 checks. The examples are 94 runnable programs, each with a card
18
18
  deck and saved conclusions, proofs and check reports. The conformance suite
19
- has 1290 cases that test any implementation of the specification through its
19
+ has 1329 cases that test any implementation of the specification through its
20
20
  command line; run it [in your browser](https://eyereasoner.github.io/eyemind/playground/conformance).
21
21
 
22
22
  ```js
package/SPEC.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  ```text
4
4
  Title: The eyemind Rule Language and Proof Format
5
- Version: eyemind 0.3.2
5
+ Version: eyemind 0.3.4
6
6
  ```
7
7
 
8
8
  This document specifies the syntax and meaning of eyemind programs and data
@@ -64,6 +64,7 @@ proof.
64
64
  - [10.2 Clause displays](#102-clause-displays)
65
65
  - [10.3 Steps](#103-steps)
66
66
  - [10.4 Search certificates](#104-search-certificates)
67
+ - [10.5 Call-fidelity certificates](#105-call-fidelity-certificates)
67
68
  - [11. Proof Checking](#11-proof-checking)
68
69
  - [11.1 Reading the document](#111-reading-the-document)
69
70
  - [11.2 C1 Resolution](#112-c1-resolution)
@@ -78,6 +79,7 @@ proof.
78
79
  - [11.11 Counts and validity](#1111-counts-and-validity)
79
80
  - [11.12 Scope of a valid proof](#1112-scope-of-a-valid-proof)
80
81
  - [11.13 Operational query fidelity](#1113-operational-query-fidelity)
82
+ - [11.14 C10 Call fidelity](#1114-c10-call-fidelity)
81
83
  - [12. Check Reports](#12-check-reports)
82
84
  - [12.1 JSON form](#121-json-form)
83
85
  - [13. Command-Line Interface](#13-command-line-interface)
@@ -368,7 +370,7 @@ A clause MUST be rejected when:
368
370
  - a head is not an atom or compound, or is a list, the atom `'[]'` or a
369
371
  `'.'/2` compound, which as a goal would be a conjunction (Section 4.1) and
370
372
  could never call the clause;
371
- - a head uses `step/4`, `clause/2`, `searched/2` or `derived/2`, reserved
373
+ - a head uses `step/4`, `clause/2`, `searched/2`, `derived/2`, `called/2`, reserved
372
374
  for proof documents, or explicitly states the reserved predicate
373
375
  `activated/2`;
374
376
  - a head is a primitive or control, except the head `'true'` of a query or
@@ -955,7 +957,8 @@ When a proof is requested, each solution records how each of its goals was
955
957
  justified: by a fact or rule of the program, by a derived fact (whose own
956
958
  record is kept), by a primitive, by a control, by an absence (a negation) or
957
959
  by a collection (`findall`). For each conclusion the first derivation found is
958
- recorded. Each negation and collection of a proof gets the search certificate
960
+ recorded. Each original question gets call-fidelity evidence (Sections 10.5 and 11.14),
961
+ when supported. Each negation and collection of a proof gets the search certificate
959
962
  that settles it (Sections 10.4 and 11.10) whenever Section 11.10 allows one. A
960
963
  table holds every answer of its call, so it can search beyond where the run
961
964
  stopped, as under `once`; a boundary whose tables would meet an error, or
@@ -1091,6 +1094,8 @@ the conclusions of a run and in a proof document, the variables are named
1091
1094
  first shows them, across the whole document, the conclusions written first;
1092
1095
  the clause displays of a proof keep the program's names. A check report
1093
1096
  names the variables of each fact anew (Section 12).
1097
+ Call tables have a separate naming scope and preserve anonymous-variable
1098
+ status as specified in Section 10.5.
1094
1099
 
1095
1100
  ## 10. Proof Documents
1096
1101
 
@@ -1109,13 +1114,16 @@ A proof document is a document (Section 9.1) holding, in this order:
1109
1114
  `step(Goal, By, Bindings, Uses)` per justified goal;
1110
1115
  6. when it has a search certificate (Section 10.4), an empty line, comment
1111
1116
  lines introducing it, then one `derived(Pattern, Facts)` per listed
1112
- relation, then one `searched(Call, Answers)` per table.
1117
+ relation, then one `searched(Call, Answers)` per table;
1118
+ 7. then any `called(Call, Answers)` tables (Section 10.5), in first-consulted
1119
+ order and with their own variable naming scope.
1113
1120
 
1114
1121
  Comments are informative and MAY be worded differently; they are ignored when
1115
1122
  the document is read.
1116
1123
 
1117
1124
  A checker MUST treat every statement that is neither `clause/2`, `step/4`,
1118
- `searched/2` nor `derived/2` as a claim, whatever its position.
1125
+ `searched/2`, `derived/2` nor `called/2` as a claim, whatever its position.
1126
+ Call tables follow the other search certificate statements (Section 10.5).
1119
1127
 
1120
1128
  ### 10.2 Clause displays
1121
1129
 
@@ -1206,6 +1214,37 @@ derived(nonpacifist(A), [nonpacifist('nixon')])
1206
1214
  searched(nonpacifist('ada'), [])
1207
1215
  ```
1208
1216
 
1217
+ ### 10.5 Call-fidelity certificates
1218
+
1219
+ `called(Call, Answers)` is a **call table**. Its shape and answer ordering
1220
+ are those of `searched/2` (Section 10.4), but it certifies calls made from
1221
+ the original questions. C10 is always checked, including when the document
1222
+ has no call tables. External questions remain inputs to the checker: a
1223
+ table cannot supply or specialize the question to which a claim must answer.
1224
+ Missing call evidence remains an explicit obligation, never an unconditional
1225
+ `checked` verdict (Section 11.14).
1226
+
1227
+ Two calls identify the same call table only when they are identical up to
1228
+ renaming **that preserves anonymous-variable status** (Section 4.2), including
1229
+ inside nested compounds. There MUST NOT be two such tables. An anonymous
1230
+ variable cannot be renamed to a named one, or vice versa, when comparing
1231
+ call-table answers either. These restrictions matter because a named unbound
1232
+ variable makes a negation an error. A malformed or duplicate call table
1233
+ fails C3 as a malformed or duplicate search table does.
1234
+
1235
+ Call-table variables have their own naming scope. Writers name named
1236
+ variables `A`, `B`, ..., counting variables in first-occurrence order, and
1237
+ anonymous ones `_0`, `_1`, ... using their position in the same order.
1238
+ For example, a named call returning an anonymous parameter may be:
1239
+
1240
+ ```js
1241
+ called(p(A), [p(_1)])
1242
+ ```
1243
+
1244
+ Each answer is freshened separately when used, as in Section 10.4. C10 call
1245
+ tables and C9 search tables are distinct, even for textually identical calls.
1246
+ They share any applicable `derived/2` listings, whose facts require steps.
1247
+
1209
1248
  ## 11. Proof Checking
1210
1249
 
1211
1250
  A checker is given a program, a proof document and, when the proof answers
@@ -1213,7 +1252,7 @@ goals asked from outside the program, those goals. It MUST NOT call a
1213
1252
  reasoner to supply a missing step; it MAY recompute primitives, and it MAY
1214
1253
  expand a call one level against the program with every inner call answered
1215
1254
  by the search certificate (Section 11.10). It
1216
- establishes nine conditions and records each failure with its condition, a
1255
+ checks C1–C10 and records each failure with its condition, a
1217
1256
  detail and, where there is one, the term concerned.
1218
1257
 
1219
1258
  ### 11.1 Reading the document
@@ -1295,8 +1334,10 @@ Host math is the one exception to identical re-decision. A `'builtin'` step
1295
1334
  `log10`, a trigonometric or hyperbolic function, or `pow`, also passes C5
1296
1335
  when `E` evaluates to a float at most 4 units in the last place from `V`,
1297
1336
  that is, with at most 3 floats between them. Such results may differ between
1298
- conforming implementations (Section 14), so a proof made by one still checks
1299
- with another. Every other recomputed result MUST be identical; a difference
1337
+ conforming implementations (Section 14). C5 therefore accepts this bounded
1338
+ variation. C10 still compares actual answers of the original question;
1339
+ when those answers depend on host math, a proof MAY fail C10 on another
1340
+ implementation even when its primitive steps pass C5. Every other recomputed result MUST be identical; a difference
1300
1341
  that grows beyond this bound, for example through cancellation, MAY make a
1301
1342
  proof fail on an implementation other than the one that made it.
1302
1343
 
@@ -1469,7 +1510,9 @@ not closed, tables depend on themselves, a search leads to an error, or a
1469
1510
  negation's goal has a solution. Each wrong table or listing is reported once,
1470
1511
  for the first boundary resting on it. A boundary whose certificate is
1471
1512
  missing, incomplete or not applicable under item 5, or a collection whose
1472
- list does not agree, is not settled. A boundary that is not settled is
1513
+ list does not agree, is not settled. A listing's facts are examined in
1514
+ order, and the first that is not ground, such as a pattern a source
1515
+ inspection derived, leaves the boundary not settled rather than failing it. A boundary that is not settled is
1473
1516
  trusted, including one that fails C9; C6 confronts each boundary C9 neither
1474
1517
  settled nor refuted with the evidence (Section 11.7).
1475
1518
 
@@ -1486,21 +1529,23 @@ settled nor refuted with the evidence (Section 11.7).
1486
1529
  | C7 relevance | claims plus steps |
1487
1530
  | C8 witness_integrity | `clause` steps with witnesses in their forward head or enclosing generators that passed C8 |
1488
1531
  | C9 search_closure | boundaries their search certificate settled or refuted |
1532
+ | C10 call_fidelity | claims whose call fidelity is established or refuted |
1489
1533
 
1490
1534
  A check also counts the **steps** it read, the **claims**, the `clause` steps
1491
1535
  **verified** by C1, the `source` inspection steps also **verified** by C1,
1492
1536
  the `'builtin'` steps **recomputed** by C5, the
1493
1537
  `'control'` steps **composed** by C5, and the `'absent'` and `'collected'`
1494
- steps **trusted**, those C9 did not settle.
1538
+ steps **trusted**, those C9 did not settle, plus each claim whose call
1539
+ fidelity remains unproved by C10.
1495
1540
 
1496
- A proof is **valid** when no condition failed. A valid proof with trusted
1497
- boundaries is valid *conditional on* them.
1541
+ A proof is **valid** when no condition failed. A valid proof with obligations is valid *conditional on* them.
1498
1542
 
1499
1543
  ### 11.12 Scope of a valid proof
1500
1544
 
1501
1545
  A valid proof establishes that each of its claims follows from the program
1502
- by the recorded steps, given its trusted boundaries; a settled boundary is
1503
- established by its search certificate. It does not establish
1546
+ by the recorded steps, given its obligations; a settled boundary is
1547
+ established by its search certificate, and an unconditional verdict also
1548
+ establishes operational query fidelity (Section 11.13). It does not establish
1504
1549
  that the claims are all the conclusions of the program, that the program
1505
1550
  derives no `'false'`, or that the program is the one intended: a proof MAY
1506
1551
  leave conclusions out, and a run's own proof holds exactly its conclusions.
@@ -1512,7 +1557,9 @@ for an original question under Section 8, retaining its original call-time
1512
1557
  bindings, control choices and applicable forward state. Each claimed answer
1513
1558
  must be reachable in that question's ordered search. This is distinct from
1514
1559
  completeness: a proof may omit other reachable answers without falsifying
1515
- the answers it does give.
1560
+ the answers it does give. Fidelity concerns the question's search; it does
1561
+ not certify that execution of the whole program reaches that question,
1562
+ avoids a prior contradiction, or terminates preceding questions.
1516
1563
 
1517
1564
  C1–C9 check the recorded instances and their evidence. They do not establish
1518
1565
  operational query fidelity for binding-sensitive or order-sensitive goals.
@@ -1537,9 +1584,101 @@ freshening and scope, and any first-solution choice or required search
1537
1584
  closure. A claimed call state or substitution is not authority: it must
1538
1585
  follow from the original question, source clauses and checked evidence.
1539
1586
  It MUST preserve generator captures independently of later caller bindings
1540
- and MUST NOT invoke the reasoner to fill gaps. No additional condition or
1541
- evidence format for that property is defined by this specification; a
1542
- C1–C9 verdict alone MUST NOT be presented as establishing it.
1587
+ and MUST NOT invoke the reasoner to fill gaps. C10 provides the certificate
1588
+ check of Section 11.14. A C1–C9 verdict alone MUST NOT be presented as
1589
+ establishing it.
1590
+
1591
+ ### 11.14 C10 Call fidelity
1592
+
1593
+ Every proof check includes C10. A false original-call certificate or an
1594
+ answer excluded by a complete certificate fails C10. Missing, incomplete or
1595
+ unsupported evidence leaves an explicit
1596
+ `obligation('call_fidelity', 'original_call', Claim)`. Such a claim MUST NOT
1597
+ receive an unconditional `checked` verdict. When trusted obligations are
1598
+ forbidden, each such obligation also fails C10.
1599
+
1600
+ The **original questions** are the external goals, if any. Otherwise, when
1601
+ the program has a forward head `'true'`, they are those rules' bodies as
1602
+ conjunctions; otherwise they are its ordinary forward heads, excluding
1603
+ `'false'`. The questions are not instantiated with a proof's final bindings.
1604
+ Only questions of which a claim is an instance need a certificate. Empty
1605
+ proofs certify no answers and do not establish that a question has none.
1606
+ When the claims contain `'false'`, C10 is inapplicable: it covers no answers
1607
+ and adds no call-fidelity obligations for that halted proof.
1608
+
1609
+ For each such question a checker searches its answers by Section 11.10's
1610
+ one-level expansion and closure checks, with the following differences:
1611
+
1612
+ 1. Inner relation calls use `called/2` rather than `searched/2`. Call and
1613
+ answer comparison preserves anonymous-variable status (Section 10.5).
1614
+ Facts alone and primitives need no table, as in C9. Source clauses,
1615
+ freshened apart, determine the bindings passed between ordered premises;
1616
+ the final values displayed by a logical step are not inputs to that search.
1617
+ 2. Negation MUST enforce Section 4.2's binding restriction at the actual
1618
+ call. C9's treatment of an unbound named variable as anonymous MUST NOT
1619
+ be used. A source search that raises an error fails its certificate.
1620
+ 3. `once` and `findall` keep their Section 4.2 meanings, so the first answer
1621
+ or collected answers come from the actual inner call. Section 11.10's
1622
+ restriction on order-sensitive uses of derived listings still applies.
1623
+ The order of unrelated answers of an ordinary question need not be
1624
+ established to check membership of a claim.
1625
+ 4. A comparison `compare/3` with either compared term nonground is not
1626
+ supported by this certificate format: alpha renaming of a table need
1627
+ not preserve the order of variable names. Such a search leaves call fidelity unproved.
1628
+ 5. A consulted forward listing MUST have a dependency closure in the ground
1629
+ positive fragment described below. A final listing alone MUST NOT certify
1630
+ binding-sensitive or order-sensitive forward firings outside that fragment.
1631
+
1632
+ For this check, a forward listing's **ground positive dependency closure**
1633
+ contains its relation and each ordinary body relation of every source fact,
1634
+ backward rule or forward rule defining any relation in that closure. All
1635
+ source facts in the closure MUST be ground. Rule bodies MUST contain only
1636
+ ordinary calls, `'true'`, conjunction and disjunction; templates, captures
1637
+ and source inspection are excluded. A body call to an ordinary relation
1638
+ grounds its variables. Conjunction accumulates the grounded variables of its
1639
+ parts; disjunction grounds only those grounded by both alternatives. Every
1640
+ backward head variable MUST be grounded by its body. Every forward head
1641
+ variable MUST either be grounded by its body or be a head-only witness
1642
+ checked by C8. Supported facts plus a closed listing then establish the
1643
+ least ground positive closure. More general forward execution requires
1644
+ publication evidence not supplied by this format, and leaves call fidelity
1645
+ unproved.
1646
+
1647
+ Every relevant call certificate MUST be valid. Each claim MUST be identical,
1648
+ up to renaming, to an answer of at least one corresponding original question;
1649
+ merely being an instance of a more general answer does not suffice. For
1650
+ example, `q('b')` fails C10 for the question in Section 11.13 even though
1651
+ its ground logical derivation passes C1–C9. A proof of `p('a')` likewise
1652
+ cannot replace an original call's actual universal answer `p(A)`.
1653
+
1654
+ A claim fails C10 once when a corresponding question has a false
1655
+ certificate or complete certificates for every matching question exclude its
1656
+ answer. An unresolved alternative question leaves an obligation unless
1657
+ another question certifies the answer. With no applicable complete
1658
+ certificate, its call fidelity likewise remains an obligation. C10 does not discharge earlier C6/C9 obligations.
1659
+
1660
+ The current format requires complete tables, including for a relation
1661
+ called under `once`. A later error or recursion may prevent that table
1662
+ from closing even when `once` itself terminates. C10 then cannot be
1663
+ established; a search-prefix certificate is not defined here. A reasoner
1664
+ MUST attempt call-fidelity certification for every generated proof and check
1665
+ the resulting proof before returning it. Unresolved evidence remains an
1666
+ obligation. A reasoner MAY bound this auxiliary production; exceeding its
1667
+ bound leaves call fidelity unproved, and it MUST NOT change the run's
1668
+ answers. The bounds are not part of conformance, so two conforming reasoners
1669
+ may differ in which claims of a generated proof carry call-fidelity
1670
+ obligations. (eyemind bounds it per question by the smaller of
1671
+ `max_inferences` and 50,000 calls plus answers, and by 8,388,608 UTF-16 code
1672
+ units of answer text.)
1673
+
1674
+ Call tables and their supporting listings consulted for C10 serve the claims
1675
+ for C7, and their listed facts are reachable through that evidence. C10's
1676
+ auxiliary search does not change C1–C9 covered counts. Its own covered count
1677
+ counts claims whose call fidelity is established or refuted, excluding
1678
+ unresolved claims. Failures are reported in
1679
+ claim order after checking boundaries and before C7 and C2 (Section 12).
1680
+ The answer checks are per original question; they do not establish that all
1681
+ answers were claimed or that preceding distinct questions terminated.
1543
1682
 
1544
1683
  ## 12. Check Reports
1545
1684
 
@@ -1547,12 +1686,14 @@ A check report is a document (Section 9.1) beginning with the proof's claims,
1547
1686
  in proof order, in canonical text, one per line, followed by a blank line and
1548
1687
  the following report facts, one per line:
1549
1688
 
1550
- 1. `condition(Id, Name, Outcome, Covered)` for C1 to C9, where `Outcome` is
1689
+ 1. `condition(Id, Name, Outcome, Covered)` for C1 to C10, where `Outcome` is
1551
1690
  `'ok'` or `failed(N)`, `N` the number of that condition's failures;
1552
1691
  2. `failure(Condition, Subject, Detail)` per failure, where `Subject` is the
1553
1692
  term concerned or `'proof_document'`, and `Detail` an atom;
1554
1693
  3. `obligation(Kind, 'theory_scoped', Goal)` per trusted boundary, in step
1555
- order, where `Kind` is `'absent'` or `'collected'`;
1694
+ order, where `Kind` is `'absent'` or `'collected'`, followed by
1695
+ `obligation('call_fidelity', 'original_call', Claim)` per unresolved claim,
1696
+ in claim order;
1556
1697
  4. `steps(N)`, `verified(N)`, `recomputed(N)`, `composed(N)`, `trusted(N)`,
1557
1698
  `claims(N)`, the counts of Section 11.11;
1558
1699
  5. `verdict(V)`, where `V` is `failed(N)` when `N` failures were recorded,
@@ -1564,7 +1705,7 @@ report is valid with no obligations. A valid report with obligations MUST
1564
1705
  label them as conditional on those obligations. A failed report MUST label
1565
1706
  them as unverified claims; their inclusion does not validate them. A valid
1566
1707
  report with no claims MUST say it establishes no conclusions. Comments also
1567
- explain the C1-C9 order, failures and obligations when present, the statistics,
1708
+ explain the C1-C10 order, failures and obligations when present, the statistics,
1568
1709
  and that the verdict does not establish completeness. Comments are informative
1569
1710
  and MAY be worded differently. They are ignored when the document is read.
1570
1711
 
@@ -1573,7 +1714,7 @@ document (Section 11.1), in document order; C4 for each claim, in claim
1573
1714
  order; then for each step, in document order, C4 for its uses and the C1, C3,
1574
1715
  C5 or C8 failure of its justification; for each boundary, in step order, the
1575
1716
  C9 failures of its search certificate in the order found, or else its C6
1576
- failure; C7 for each claim, then
1717
+ failure; C10 for each claim; C7 for each claim, then
1577
1718
  for each step, then for each table and listing, in document order; and C2.
1578
1719
 
1579
1720
  `Detail` is informative text identifying what failed; its wording is not
@@ -1590,7 +1731,8 @@ The JSON form includes `answers`, an array of the initial claims' canonical
1590
1731
  texts, including unverified claims when `valid` is false. It also has `valid` (boolean), `steps`,
1591
1732
  `claims`, `verified`, `redecided` (the count `recomputed`), `composed`,
1592
1733
  `uses` (the number of uses of all steps, integers), `trusted`
1593
- (a list of `{kind, conclusion}`), `failures` (a list of
1734
+ (a list of `{kind, conclusion, reason?}`, with `reason: 'original_call'`
1735
+ for call-fidelity obligations), `failures` (a list of
1594
1736
  `{condition, detail, conclusion?}`) and `conditions` (a list of
1595
1737
  `{id, name, covered, failed}`), where `conclusion` is a canonical text.
1596
1738
  Its variables keep the names the document gives them, rather than being
@@ -1612,7 +1754,7 @@ eyemind [OPTION ...] [FILE ...]
1612
1754
  | `--check-proof FILE` | Check the proof in `FILE` (`-` for standard input) against the program and print its answers and report. |
1613
1755
  | `--json` | With either checking mode, print the report, including `answers`, as JSON. |
1614
1756
  | `--goal GOAL` | Ask `GOAL`, read as in Section 9.1, where an array is a conjunction; MAY be repeated. With `--check-proof`, the goals the proof answers. |
1615
- | `--strict-proof` | With either checking mode, forbid trusted boundaries. |
1757
+ | `--strict-proof` | With either checking mode, forbid trusted obligations. |
1616
1758
  | `--unused` | Print the unused clauses (Section 13.2). |
1617
1759
  | `--stats` | Print reasoning statistics as JSON to standard error: `inferences`, the goals started, which `--max-inferences` bounds; `rounds`, the forward rounds run over all strata; and `derived`, the facts forward rules added, not counting a conclusion the program states as a fact, a query's answers or `'false'`. |
1618
1760
  | `--max-depth N`, `--max-iterations N`, `--max-inferences N` | The bounds of Section 8.3; `N` MUST be a positive safe integer, at most 9007199254740991. |
@@ -1624,7 +1766,7 @@ exclusive. `--json` and `--strict-proof` need a checking mode. With
1624
1766
  `--prove-and-check`, reasoning bounds and `--stats` apply to proof generation;
1625
1767
  an invalid check returns 2, otherwise the run's halt code is retained
1626
1768
  (65 for a contradiction, 0 otherwise). A program
1627
- text that has a line starting with `step(` is a proof document; given one as
1769
+ text that has a line starting with `step(` or `called(` is a proof document; given one as
1628
1770
  a program, the command line MUST say how to check it instead.
1629
1771
 
1630
1772
  | Exit code | Meaning |
@@ -1822,6 +1964,11 @@ clause(3, implies([type(S, A), subclass_of(A, B)], type(S, B)))
1822
1964
  step(type('socrates', 'mortal'), clause(3), {S: 'socrates', A: 'human', B: 'mortal'}, [type('socrates', 'human'), subclass_of('human', 'mortal')])
1823
1965
  step(type('socrates', 'human'), clause(1), {}, [])
1824
1966
  step(subclass_of('human', 'mortal'), clause(2), {}, [])
1967
+
1968
+ // Search certificate: derived facts and complete answers used to check
1969
+ // negations, collections and the original questions.
1970
+ derived(type(A, B), [type('socrates', 'mortal')])
1971
+ called(type(A, B), [type('socrates', 'mortal'), type('socrates', 'human')])
1825
1972
  ```
1826
1973
 
1827
1974
  Its check report (Section 12):
@@ -1831,8 +1978,8 @@ Its check report (Section 12):
1831
1978
  type('socrates', 'mortal')
1832
1979
  type('socrates', 'human')
1833
1980
 
1834
- // C1-C9: resolution, acyclicity, justification, coverage, re-decision,
1835
- // boundary consistency, relevance, witness integrity and search closure.
1981
+ // C1-C10: resolution, acyclicity, justification, coverage, re-decision,
1982
+ // boundary consistency, relevance, witness integrity, search closure and call fidelity.
1836
1983
  condition('C1', 'resolution', 'ok', 3)
1837
1984
  condition('C2', 'well_founded', 'ok', 3)
1838
1985
  condition('C3', 'justification', 'ok', 3)
@@ -1842,6 +1989,7 @@ condition('C6', 'boundary_consistency', 'ok', 0)
1842
1989
  condition('C7', 'relevance', 'ok', 5)
1843
1990
  condition('C8', 'witness_integrity', 'ok', 0)
1844
1991
  condition('C9', 'search_closure', 'ok', 0)
1992
+ condition('C10', 'call_fidelity', 'ok', 2)
1845
1993
 
1846
1994
  // Proof statistics: recorded steps and how they were checked.
1847
1995
  steps(3)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eyemind",
3
- "version": "0.3.2",
3
+ "version": "0.3.4",
4
4
  "description": "A dependency-free JavaScript rule language with forward and backward reasoning and checkable proofs.",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
package/src/cli.js CHANGED
@@ -9,11 +9,11 @@ export const HELP = `Usage: eyemind [OPTION ...] [FILE ...]
9
9
  Programs are JavaScript; forward rules run to a fixpoint.
10
10
  -p, --proof Print the claims with clause(...) and step(...) records
11
11
  -c, --prove-and-check Generate a proof and check it against the input program
12
- --check-proof FILE Print checked answers and a C1-C9 report (- for stdin)
12
+ --check-proof FILE Print checked answers and a proof check report (- for stdin)
13
13
  --json Print the check report as JSON instead
14
14
  --goal GOAL Ask a backward goal after forward reasoning; with
15
15
  --check-proof, the goal the proof answers
16
- --strict-proof Reject proofs relying on absence or collection
16
+ --strict-proof Reject proofs with unresolved obligations
17
17
  --unused List the clauses that make no difference to the conclusions
18
18
  --stats Print reasoning statistics to stderr
19
19
  --max-depth N Bound backward recursion (default 1000000)
package/src/closure.js CHANGED
@@ -20,7 +20,7 @@ import { freshClause, text, variant } from './common.js';
20
20
  import { INSPECTION_KEYS } from './inspection.js';
21
21
  import {
22
22
  Env, EyemindError, Struct, Var, copyResolved, deref, flattenConjunction, freshTerm, identity, isCallable, isGround, isTerm, key,
23
- listFromItems, unify, variables,
23
+ isGroundButAnonymous, listFromItems, unify, variables,
24
24
  } from './terms.js';
25
25
 
26
26
  // A search the certificate cannot vouch for: its boundary stays an obligation.
@@ -60,13 +60,21 @@ export function forwardKeys(program) {
60
60
  // supported(fact) whether the proof has a step for a listed fact. Tables and
61
61
  // listings are checked once, however many boundaries rest on them.
62
62
  export class Settler {
63
- constructor(program, source) {
63
+ constructor(program, source, operational = false) {
64
64
  this.program = program;
65
65
  this.source = source;
66
66
  this.derivedKeys = forwardKeys(program);
67
67
  this.tables = new Map();
68
68
  this.listings = new Map();
69
69
  this.serial = 0;
70
+ this.operational = operational;
71
+ this.variant = (term) => variant(term, operational);
72
+ }
73
+
74
+ // All answers of an original call, rather than a final substituted step.
75
+ // This is the C10 root; inner calls still come only from the certificate.
76
+ answers(call) {
77
+ return this.settle(new Struct('answers', [call]));
70
78
  }
71
79
 
72
80
  // The outcome for one negation or collection: status 'settled',
@@ -75,6 +83,7 @@ export class Settler {
75
83
  // rests on.
76
84
  settle(boundary) {
77
85
  const collecting = isTerm(boundary, 'findall', 3);
86
+ const answering = isTerm(boundary, 'answers', 1);
78
87
  const goal = collecting ? boundary.args[1] : boundary.args[0];
79
88
  const root = new Reliance(collecting);
80
89
  const outcome = { status: 'settled', failures: [], boundaryOnly: false, tables: [], listings: [] };
@@ -82,10 +91,10 @@ export class Settler {
82
91
  try {
83
92
  const env = new Env();
84
93
  const copied = freshTerm(collecting ? new Struct('-', [boundary.args[0], goal]) : goal, `root${++this.serial}`);
85
- const [template, searched] = collecting ? copied.args : [null, copied];
94
+ const [template, searched] = collecting ? copied.args : [answering ? copied : null, copied];
86
95
  for (const _ of this.solve(searched, env, root)) { // eslint-disable-line no-unused-vars
87
- found.push(collecting ? freshTerm(copyResolved(template, env), `item${++this.serial}`) : null);
88
- if (!collecting) break;
96
+ found.push(collecting || answering ? freshTerm(copyResolved(template, env), `item${++this.serial}`) : null);
97
+ if (!collecting && !answering) break;
89
98
  }
90
99
  } catch (error) {
91
100
  if (error instanceof Unsettled) return { ...outcome, status: 'unsettled' };
@@ -126,6 +135,7 @@ export class Settler {
126
135
  }
127
136
  if (outcome.failures.length || outcome.failed) return { ...outcome, status: 'failed' };
128
137
  if (unsettled || [...ordered].some((item) => !this.exact(item))) return { ...outcome, status: 'unsettled' };
138
+ if (answering) return { ...outcome, answers: found };
129
139
  if (!collecting && found.length) {
130
140
  outcome.failures.push([`absence contradicted by its search: ${text(boundary)}`, boundary]);
131
141
  return { ...outcome, status: 'failed', boundaryOnly: true };
@@ -157,6 +167,9 @@ export class Settler {
157
167
  // A negation's unbound variables stand for any value, as anonymous
158
168
  // ones do; a reasoner would not have run one with a named variable.
159
169
  const once = goalKey === 'once/1';
170
+ if (this.operational && !once && !isGroundButAnonymous(goal.args[0], env)) {
171
+ throw new EyemindError('call fidelity: negation has an unbound named variable');
172
+ }
160
173
  if (once) reliance.inner++;
161
174
  const inner = this.solve(goal.args[0], env, reliance);
162
175
  const first = inner.next();
@@ -175,6 +188,10 @@ export class Settler {
175
188
  env.undo(mark);
176
189
  if (unify(goal.args[2], listFromItems(items), env)) yield;
177
190
  } else if (PRIMITIVE_KEYS.has(goalKey)) {
191
+ // Relative names of unbound variables are not preserved by alpha
192
+ // renaming of a table. A name-dependent comparison needs more evidence.
193
+ if (this.operational && goalKey === 'compare/3' &&
194
+ (!isGround(goal.args[1], env) || !isGround(goal.args[2], env))) throw new Unsettled();
178
195
  for (const _ of primitive(goal, env)) { // eslint-disable-line no-unused-vars
179
196
  yield;
180
197
  env.undo(mark);
@@ -187,7 +204,7 @@ export class Settler {
187
204
  yield* this.expand(goal, env, reliance);
188
205
  } else {
189
206
  const call = copyResolved(goal, env);
190
- const callKey = variant(call);
207
+ const callKey = this.variant(call);
191
208
  let entry = this.tables.get(callKey);
192
209
  if (entry === undefined) {
193
210
  const answers = this.source.answers(call);
@@ -219,7 +236,9 @@ export class Settler {
219
236
  env.undo(mark);
220
237
  }
221
238
  }
222
- for (const clause of this.program.groups.get(goalKey) || []) {
239
+ const clauses = this.program.hasTemplates ? this.program.groups.get(goalKey) || []
240
+ : this.program.candidates(call, env, goalKey);
241
+ for (const clause of clauses) {
223
242
  // A derived rule instance answers only through the run that made it.
224
243
  if (clause.parentId !== null) throw new Unsettled();
225
244
  const [head, body] = freshClause(clause, `clause${++this.serial}`);
@@ -260,7 +279,7 @@ export class Settler {
260
279
  for (const _ of this.expand(call, env, entry.reliance)) found.push(copyResolved(call, env)); // eslint-disable-line no-unused-vars
261
280
  const listed = entry.answers;
262
281
  let i = 0;
263
- while (i < found.length && i < listed.length && variant(found[i]) === variant(listed[i])) i++;
282
+ while (i < found.length && i < listed.length && this.variant(found[i]) === this.variant(listed[i])) i++;
264
283
  if (i < found.length && i < listed.length) {
265
284
  entry.failure = `the search of ${text(entry.call)} answers ${text(found[i])} where its table lists ${text(listed[i])}`;
266
285
  } else if (i < found.length) {
package/src/common.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // Helpers shared by the engine and the proof checker.
2
- import { Struct, Var, deref, freshTerm, unify } from './terms.js';
2
+ import { Struct, Var, deref, freshTerm, isAnonymous, unify } from './terms.js';
3
3
  import { write } from './writer.js';
4
4
 
5
5
  // The canonical spelling of a term, which also serves as its identity.
@@ -184,11 +184,11 @@ function build(term, values, fresh) {
184
184
  }
185
185
 
186
186
  // A key that preserves sharing while forgetting variable names.
187
- export function variant(term) {
187
+ export function variant(term, anonymous = false) {
188
188
  const names = new Map();
189
189
  function visit(t) {
190
190
  if (t instanceof Var) {
191
- if (!names.has(t.name)) names.set(t.name, new Var(`V${names.size}`));
191
+ if (!names.has(t.name)) names.set(t.name, new Var(`${anonymous && isAnonymous(t) ? '_' : 'V'}${names.size}`));
192
192
  return names.get(t.name);
193
193
  }
194
194
  if (t instanceof Struct) return new Struct(t.name, t.args.map(visit));
package/src/engine.js CHANGED
@@ -3,6 +3,7 @@ import { PRIMITIVE_KEYS, primitive } from './builtins.js';
3
3
  import { Reliance, Settler, Unsettled } from './closure.js';
4
4
  import { freshClause, matchHead, renaming, text, variant } from './common.js';
5
5
  import { goalTerm, subgoals } from './goals.js';
6
+ import { callQuestions, positiveListings } from './fidelity.js';
6
7
  import { INSPECTION_KEYS } from './inspection.js';
7
8
  import { checkRendered, clauseDisplay, renderProof } from './proof.js';
8
9
  import { CONTROL_KEYS, Program } from './program.js';
@@ -925,7 +926,7 @@ export function skolemTerms(clause, head, unresolved, env, context = null) {
925
926
  // then checks each table again, and each boundary against what the run found.
926
927
  // A boundary the certificate cannot settle stays an obligation; one the run
927
928
  // and its certificate disagree on is an error, since a proof must not hide it.
928
- function certify(solver, boundaries) {
929
+ function certify(solver, boundaries, operational = false) {
929
930
  const program = solver.program;
930
931
  const computed = new Map();
931
932
  const met = new Map();
@@ -933,14 +934,17 @@ function certify(solver, boundaries) {
933
934
  const listed = new Map();
934
935
  let budget = 0;
935
936
  let depth = 0;
937
+ let volume = 0;
938
+ const limit = operational ? Math.min(solver.maxInferences, 50000) : solver.maxInferences;
939
+ const callKeyOf = (call) => variant(call, operational);
936
940
  const settler = new Settler(program, {
937
941
  answers(call) {
938
- const callKey = variant(call);
942
+ const callKey = callKeyOf(call);
939
943
  if (computed.has(callKey)) return computed.get(callKey);
940
944
  if (!met.has(callKey)) met.set(callKey, met.size);
941
945
  // A search that met its own call again would not have finished, and
942
946
  // one that keeps meeting new calls is cut off.
943
- if (open.has(callKey) || ++budget > solver.maxInferences) throw new Unsettled();
947
+ if (open.has(callKey) || ++budget > limit) throw new Unsettled();
944
948
  // Deep searches are taken up again from an explicit stack.
945
949
  if (depth >= CERTIFY_DEPTH) throw new Missing(call);
946
950
  depth++;
@@ -950,9 +954,11 @@ function certify(solver, boundaries) {
950
954
  const env = new Env();
951
955
  const answers = [];
952
956
  for (const _ of settler.expand(term, env, new Reliance())) { // eslint-disable-line no-unused-vars
953
- if (++budget > solver.maxInferences) throw new Unsettled();
957
+ if (++budget > limit) throw new Unsettled();
954
958
  // Each answer is written with variables of its own.
955
- answers.push(freshTerm(copyResolved(term, env), `answer${++settler.serial}`));
959
+ const answer = freshTerm(copyResolved(term, env), `answer${++settler.serial}`);
960
+ if (operational && (volume += text(answer).length) > 8 * 1024 * 1024) throw new Unsettled();
961
+ answers.push(answer);
956
962
  }
957
963
  computed.set(callKey, answers);
958
964
  return answers;
@@ -967,25 +973,26 @@ function certify(solver, boundaries) {
967
973
  }
968
974
  },
969
975
  facts(goalKey) {
976
+ if (operational && !positiveListings(program, [goalKey])) throw new Unsettled();
970
977
  const nodes = solver.facts.get(goalKey) || NO_NODES;
971
978
  if (nodes.some((node) => node.generic)) throw new Unsettled();
972
979
  listed.set(goalKey, nodes);
973
980
  return nodes.map((node) => node.goal);
974
981
  },
975
982
  supported: () => true,
976
- });
983
+ }, operational);
977
984
  // Answer a call too deep to reach directly, and the deeper calls it needs.
978
985
  function ensure(start) {
979
986
  const pending = [start];
980
- const waiting = new Set([variant(start)]);
987
+ const waiting = new Set([callKeyOf(start)]);
981
988
  while (pending.length) {
982
989
  const call = pending[pending.length - 1];
983
990
  try {
984
991
  settler.source.answers(call);
985
- waiting.delete(variant(pending.pop()));
992
+ waiting.delete(callKeyOf(pending.pop()));
986
993
  } catch (error) {
987
994
  if (!(error instanceof Missing)) throw error;
988
- const deeper = variant(error.call);
995
+ const deeper = callKeyOf(error.call);
989
996
  if (waiting.has(deeper)) throw new Unsettled();
990
997
  waiting.add(deeper);
991
998
  pending.push(error.call);
@@ -997,9 +1004,10 @@ function certify(solver, boundaries) {
997
1004
  for (const boundary of boundaries) {
998
1005
  let outcome = null;
999
1006
  budget = 0;
1007
+ volume = 0;
1000
1008
  while (outcome === null) {
1001
1009
  try {
1002
- outcome = settler.settle(boundary);
1010
+ outcome = operational ? settler.answers(boundary) : settler.settle(boundary);
1003
1011
  } catch (error) {
1004
1012
  if (!(error instanceof Missing)) throw error;
1005
1013
  try {
@@ -1010,7 +1018,7 @@ function certify(solver, boundaries) {
1010
1018
  }
1011
1019
  }
1012
1020
  }
1013
- if (outcome.status === 'settled') {
1021
+ if (outcome.status === 'settled' && (!operational || positiveListings(program, outcome.listings))) {
1014
1022
  for (const item of outcome.tables) tables.add(item);
1015
1023
  for (const item of outcome.listings) listings.add(item);
1016
1024
  } else if (outcome.status === 'failed') {
@@ -1142,7 +1150,11 @@ function reason(program, goalInputs, options) {
1142
1150
  return out;
1143
1151
  });
1144
1152
  const rendered = options.proof
1145
- ? renderProof(program, claims, roots, true, heading, (boundaries) => certify(solver, boundaries)) : null;
1153
+ ? renderProof(program, claims, roots, true, heading, (boundaries) => {
1154
+ const ordinary = certify(solver, boundaries);
1155
+ const calls = certify(solver, callQuestions(program, goalInputs, claims), true);
1156
+ return { ...ordinary, calls: calls.tables, listings: [...new Map([...ordinary.listings, ...calls.listings])] };
1157
+ }) : null;
1146
1158
  const document = rendered !== null ? rendered[0] : null;
1147
1159
  // A rule that reports a claim is used by it, though a proof records only
1148
1160
  // the claim's support.
@@ -0,0 +1,120 @@
1
+ // C10: answers of the original questions, checked from source call tables.
2
+ // Unlike logical steps, these searches start before a final substitution.
3
+ import { PRIMITIVE_KEYS } from './builtins.js';
4
+ import { Settler } from './closure.js';
5
+ import { text, variant } from './common.js';
6
+ import { goalTerm } from './goals.js';
7
+ import { INSPECTION_KEYS } from './inspection.js';
8
+ import {
9
+ Env, conjunction, copyResolved, freshTerm, isCallable, isGround, isTerm, key, unify, variables,
10
+ } from './terms.js';
11
+
12
+ function instance(goal, pattern) {
13
+ const renamed = freshTerm(pattern, 'callQuestion');
14
+ const env = new Env();
15
+ return unify(renamed, goal, env) && text(copyResolved(renamed, env)) === text(goal);
16
+ }
17
+
18
+ // The original questions of Section 8.6, without specializing them to claims.
19
+ export function callQuestions(program, goals, claims) {
20
+ if (claims.includes('false')) return [];
21
+ let questions = (goals || []).map(goalTerm);
22
+ if (!questions.length) {
23
+ questions = program.forward.filter((clause) => clause.heads.includes('true'))
24
+ .map((clause) => conjunction(clause.body));
25
+ if (!questions.length) questions = program.forward.flatMap((clause) => clause.plainHeads)
26
+ .filter((head) => head !== 'true' && head !== 'false');
27
+ }
28
+ const seen = new Set();
29
+ return questions.filter((question) => {
30
+ const id = variant(question, true);
31
+ if (seen.has(id) || !claims.some((claim) => instance(claim, question))) return false;
32
+ seen.add(id);
33
+ return true;
34
+ });
35
+ }
36
+
37
+ // C9's support plus closure establishes the least ground positive model.
38
+ // For forward rules that observe bindings or order, a final listing alone
39
+ // does not establish which firings occurred. Refuse that stronger promise.
40
+ export function positiveListings(program, listings) {
41
+ const pending = [...listings];
42
+ const seen = new Set();
43
+ function bound(goal) {
44
+ if (goal === 'true') return new Set();
45
+ if (isTerm(goal, 'and', 2) || isTerm(goal, 'or', 2)) {
46
+ const left = bound(goal.args[0]);
47
+ const right = bound(goal.args[1]);
48
+ if (left === null || right === null) return null;
49
+ return goal.name === 'and' ? new Set([...left, ...right])
50
+ : new Set([...left].filter((name) => right.has(name)));
51
+ }
52
+ if (!isCallable(goal)) return null;
53
+ const goalKey = key(goal);
54
+ if (PRIMITIVE_KEYS.has(goalKey) || INSPECTION_KEYS.has(goalKey) ||
55
+ ['not/1', 'findall/3', 'once/1', 'call/1'].includes(goalKey)) return null;
56
+ pending.push(goalKey);
57
+ return new Set(variables(goal).keys());
58
+ }
59
+ while (pending.length) {
60
+ const goalKey = pending.pop();
61
+ if (seen.has(goalKey)) continue;
62
+ seen.add(goalKey);
63
+ const clauses = [...(program.groups.get(goalKey) || []), ...program.forward.filter((clause) =>
64
+ clause.plainHeads.some((head) => isCallable(head) && key(head) === goalKey))];
65
+ for (const clause of clauses) {
66
+ if (clause.parentId !== null || clause.templates.length || clause.captures !== null) return false;
67
+ if (!clause.forward && !clause.body.length) {
68
+ if (!isGround(clause.head)) return false;
69
+ continue;
70
+ }
71
+ const grounded = new Set();
72
+ for (const goal of clause.body) {
73
+ const names = bound(goal);
74
+ if (names === null) return false;
75
+ for (const name of names) grounded.add(name);
76
+ }
77
+ const witnesses = new Set(clause.forward ? clause.witnessNames : []);
78
+ for (const name of variables(clause.head).keys()) {
79
+ if (!grounded.has(name) && !witnesses.has(name)) return false;
80
+ }
81
+ }
82
+ }
83
+ return true;
84
+ }
85
+
86
+ export function checkCalls(program, claims, goals, certificate, steps, spellings, fail, allowTrusted, name) {
87
+ const settler = new Settler(program, {
88
+ answers: (call) => certificate.calls.get(variant(call, true)) ?? null,
89
+ facts: (goalKey) => certificate.listings.get(goalKey) ?? null,
90
+ supported: (fact) => steps.has(spellings.spell(fact)),
91
+ }, true);
92
+ if (claims.includes('false')) return { settler, outcomes: [], unresolved: [], covered: 0 };
93
+ const outcomes = callQuestions(program, goals, claims).map((question) => {
94
+ const outcome = settler.answers(question);
95
+ if (outcome.status === 'settled' && !positiveListings(program, outcome.listings)) {
96
+ return { question, ...outcome, status: 'unsupported' };
97
+ }
98
+ return { question, ...outcome };
99
+ });
100
+ const unresolved = [];
101
+ let covered = 0;
102
+ for (const claim of claims) {
103
+ const matching = outcomes.filter(({ question }) => instance(claim, question));
104
+ let detail = null;
105
+ if (matching.some(({ status }) => status === 'failed')) detail = 'the original call has an invalid search certificate';
106
+ else if (!matching.some(({ status, answers }) => status === 'settled' &&
107
+ answers.some((answer) => variant(answer) === variant(claim)))) {
108
+ if (matching.length && matching.every(({ status }) => status === 'settled')) {
109
+ detail = 'the original call does not return this answer';
110
+ } else {
111
+ unresolved.push([{ kind: 'call_fidelity', reason: 'original_call', conclusion: name(claim) }, claim]);
112
+ if (!allowTrusted) fail.add('C10', `call fidelity remains unproved: ${name(claim)}`, claim);
113
+ continue;
114
+ }
115
+ }
116
+ covered++;
117
+ if (detail !== null) fail.add('C10', `${detail}: ${text(claim)}`, claim);
118
+ }
119
+ return { settler, outcomes, unresolved, covered };
120
+ }
package/src/index.js CHANGED
@@ -3,7 +3,7 @@
3
3
  // Facts and rules are JavaScript. Forward rules materialize conclusions until
4
4
  // a fixpoint, backward rules are decided when a goal asks for them, and every
5
5
  // answer can come with a proof that a separate checker verifies against the
6
- // program, condition by condition (C1-C9).
6
+ // program, condition by condition (C1-C10).
7
7
  export { VERSION } from './version.js';
8
8
  export { Result, run, unusedClauses } from './engine.js';
9
9
  export { readGoal } from './goals.js';
package/src/lang.js CHANGED
@@ -642,7 +642,7 @@ function compile(source, url, names = null) {
642
642
  return new Function('__eyemind_scope__', `with (__eyemind_scope__) {\n${source}\n}\n//# sourceURL=${url}`);
643
643
  }
644
644
 
645
- const PROOF_LINE = /^step\(/m;
645
+ const PROOF_LINE = /^(?:step|called)\(/m;
646
646
  const LEADING_CALL = /^([A-Za-z_$][\w$]*)[ \t]*\(/gm;
647
647
  const DECLARED = /\b(?:function\s*\*?\s*|class\s+|(?:const|let|var)\s+)([A-Za-z_$][\w$]*)|^[ \t]*([A-Za-z_$][\w$]*)[ \t]*=(?!=)/gm;
648
648
  // Statements of earlier versions, and what states the same clause now.
package/src/program.js CHANGED
@@ -10,7 +10,7 @@ import {
10
10
  import { write } from './writer.js';
11
11
 
12
12
  export const CONTROL_KEYS = new Set(['and/2', 'or/2', 'not/1', 'call/1', 'once/1', 'findall/3']);
13
- export const RESERVED_KEYS = new Set(['step/4', 'clause/2', 'searched/2', 'derived/2', 'activated/2', 'implies/2', 'implied_by/2', 'forward/2', 'backward/2']);
13
+ export const RESERVED_KEYS = new Set(['step/4', 'clause/2', 'searched/2', 'derived/2', 'called/2', 'activated/2', 'implies/2', 'implied_by/2', 'forward/2', 'backward/2']);
14
14
 
15
15
  // The statements that state a forward rule, and how an error names them.
16
16
  const FORWARD_KINDS = { forward: 'a forward rule', query: 'query()', contradiction: 'contradiction()' };
package/src/proof.js CHANGED
@@ -1,4 +1,4 @@
1
- // Proof documents: rendering, and checking C1-C9 against the source program.
1
+ // Proof documents: rendering and checking C1-C10 against the source program.
2
2
  //
3
3
  // A proof document is JavaScript source, one expression per line: first the
4
4
  // claims, then clause(N, Display) for every source clause the proof cites,
@@ -13,6 +13,7 @@ import { PRIMITIVE_KEYS, primitive } from './builtins.js';
13
13
  import { Settler } from './closure.js';
14
14
  import { freshClause, text, variant } from './common.js';
15
15
  import { goalTerm } from './goals.js';
16
+ import { checkCalls } from './fidelity.js';
16
17
  import { INSPECTION_KEYS, isInspection } from './inspection.js';
17
18
  import { CONTROL_KEYS, Program } from './program.js';
18
19
  import { readTerms } from './reader.js';
@@ -207,7 +208,7 @@ export function renderProof(program, claims, roots, checkable = false, heading =
207
208
  walk(roots);
208
209
  const boundaries = [...steps.values()].filter((node) => node.by === 'absent' || node.by === 'collected')
209
210
  .map((node) => node.goal);
210
- const certificate = certify !== null && boundaries.length ? certify(boundaries) : { tables: [], listings: [] };
211
+ const certificate = certify !== null ? certify(boundaries) : { tables: [], listings: [] };
211
212
  // A listed derived fact is shown by its own steps, after the claims' steps.
212
213
  walk(certificate.listings.flatMap(([, nodes]) => nodes));
213
214
  // Variables are named A, B, ... in the order the document first shows
@@ -230,10 +231,10 @@ export function renderProof(program, claims, roots, checkable = false, heading =
230
231
  lines.push(`step(${spellings.lettered(node.goal, lettering)}, ${text(node.by)}, ` +
231
232
  `${bindingsText(node.bindings, lettering)}, ${uses})`);
232
233
  }
233
- const read = { tables: new Map(), listings: new Map(), order: [] };
234
- if (certificate.tables.length || certificate.listings.length) {
235
- lines.push('', '// Search certificate: every fact derived for each relation the searches behind',
236
- '// negations and collections consult, and every answer of each call they made, in order.');
234
+ const read = { tables: new Map(), calls: new Map(), listings: new Map(), order: [] };
235
+ if (certificate.tables.length || certificate.listings.length || certificate.calls?.length) {
236
+ lines.push('', '// Search certificate: derived facts and complete answers used to check',
237
+ '// negations, collections and the original questions.');
237
238
  }
238
239
  for (const [goalKey, nodes] of certificate.listings) {
239
240
  const facts = nodes.map((node) => node.goal);
@@ -247,6 +248,14 @@ export function renderProof(program, claims, roots, checkable = false, heading =
247
248
  read.tables.set(variant(call), answers);
248
249
  read.order.push(['table', variant(call), call]);
249
250
  }
251
+ // C10 must retain the distinction between named and anonymous variables
252
+ // because a named unbound variable makes a negation an error.
253
+ const callNames = new Lettering(true);
254
+ for (const [call, answers] of certificate.calls || []) {
255
+ lines.push(spellings.lettered(new Struct('called', [call, listFromItems(answers)]), callNames));
256
+ read.calls.set(variant(call, true), answers);
257
+ read.order.push(['call', variant(call, true), call]);
258
+ }
250
259
  const document = `${lines.join('\n')}\n`;
251
260
  if (!checkable) return document;
252
261
  // The steps as reading the document would give them: bindings as '='/2
@@ -281,7 +290,7 @@ class Failures {
281
290
 
282
291
  // Check a proof document against the program that produced it. goals names
283
292
  // the goals the proof answers when they were asked from outside the program.
284
- // allowTrusted false rejects any trusted boundary.
293
+ // allowTrusted false rejects every unresolved obligation.
285
294
  export function checkProof(program, document, { goals = null, allowTrusted = true } = {}) {
286
295
  if (!(program instanceof Program)) throw new EyemindError('checkProof() needs a Program; load one first');
287
296
  const fail = new Failures();
@@ -330,6 +339,8 @@ function check(program, claims, steps, certificate, goals, allowTrusted, fail, s
330
339
  const provenance = new WitnessProvenance(program, steps, certificate, settlement.outcomes, spellings);
331
340
  const tally = checkSteps(program, steps, allowTrusted, fail, spellings, name, settlement.outcomes, provenance);
332
341
  const [confronted, closed] = checkBoundaries(program, steps, tally.boundaries, fail, settlement.outcomes, spellings);
342
+ settlement.calls = checkCalls(program, claims, goals, certificate, steps, spellings, fail, allowTrusted, name);
343
+ tally.trusted.push(...settlement.calls.unresolved);
333
344
  checkRelevance(program, claims, steps, goals || [], fail, spellings, settlement, certificate);
334
345
  checkWellFounded(steps, fail, spellings);
335
346
  let uses = 0;
@@ -345,6 +356,7 @@ function check(program, claims, steps, certificate, goals, allowTrusted, fail, s
345
356
  { id: 'C7', name: 'relevance', covered: claims.length + steps.size, failed: failed('C7') },
346
357
  { id: 'C8', name: 'witness_integrity', covered: tally.witnessed, failed: failed('C8') },
347
358
  { id: 'C9', name: 'search_closure', covered: closed, failed: failed('C9') },
359
+ { id: 'C10', name: 'call_fidelity', covered: settlement.calls.covered, failed: failed('C10') },
348
360
  ];
349
361
  return {
350
362
  valid: !fail.items.length,
@@ -365,16 +377,18 @@ function check(program, claims, steps, certificate, goals, allowTrusted, fail, s
365
377
  }
366
378
 
367
379
  // The verdict allowTrusted false would give, from a report without it.
368
- // Without failures, a strict check differs only in failing C5 once for each
369
- // trusted boundary, in step order, so it needs no second reading of the
370
- // document. A report with failures returns null: check again instead.
380
+ // Without failures, strict checking adds C5 failures for trusted boundaries
381
+ // and C10 failures for unresolved original calls, in obligation order. It
382
+ // needs no second reading. A report with failures returns null: check again.
371
383
  export function strict(report) {
372
384
  if (report.failures.length) return null;
373
385
  const failures = report.trusted.map((record) => ({
374
- condition: 'C5', detail: `trusted boundary forbidden: ${record.kind}`, conclusion: record.conclusion,
386
+ condition: record.kind === 'call_fidelity' ? 'C10' : 'C5',
387
+ detail: record.kind === 'call_fidelity' ? `call fidelity remains unproved: ${record.conclusion}` : `trusted boundary forbidden: ${record.kind}`,
388
+ conclusion: record.conclusion,
375
389
  }));
376
390
  const conditions = report.conditions.map((condition) => (
377
- condition.id === 'C5' ? { ...condition, failed: failures.length } : { ...condition }));
391
+ ['C5', 'C10'].includes(condition.id) ? { ...condition, failed: failures.filter((failure) => failure.condition === condition.id).length } : { ...condition }));
378
392
  return { ...report, valid: !failures.length, failures, conditions, failureTerms: [...report.trustedTerms] };
379
393
  }
380
394
 
@@ -384,11 +398,11 @@ export function strict(report) {
384
398
  function readDocument(program, document, fail, spellings) {
385
399
  const claims = [];
386
400
  const steps = new Map();
387
- const certificate = { tables: new Map(), listings: new Map(), order: [] };
401
+ const certificate = { tables: new Map(), calls: new Map(), listings: new Map(), order: [] };
388
402
  const empty = new Env();
389
403
  try {
390
404
  for (const [term] of readTerms(document)) {
391
- if (isTerm(term, 'searched', 2) || isTerm(term, 'derived', 2)) {
405
+ if (isTerm(term, 'searched', 2) || isTerm(term, 'derived', 2) || isTerm(term, 'called', 2)) {
392
406
  readCertificate(term, certificate, fail, empty);
393
407
  continue;
394
408
  }
@@ -425,29 +439,30 @@ function readDocument(program, document, fail, spellings) {
425
439
  return [claims, steps, certificate];
426
440
  }
427
441
 
428
- // A search certificate statement: searched(Call, Answers), a table of every
429
- // answer of a call, or derived(Pattern, Facts), every fact forward rules
430
- // derived for the relation of Pattern.
442
+ // A search certificate statement: searched/2 for C9 or called/2 for C10,
443
+ // each listing every answer of its call; or derived/2, listing every fact
444
+ // forward rules derived for the relation of its pattern.
431
445
  function readCertificate(term, certificate, fail, empty) {
432
446
  const [subject, list] = term.args;
433
447
  const items = properListItems(list, empty);
434
- const table = term.name === 'searched';
448
+ const calling = term.name === 'called';
449
+ const table = term.name === 'searched' || calling;
435
450
  // A listing names its relation by the name applied to distinct variables.
436
451
  const pattern = table || typeof subject === 'string' || (subject instanceof Struct &&
437
452
  subject.args.every((arg, i) => arg instanceof Var && subject.args.findIndex((other) => other instanceof Var && other.name === arg.name) === i));
438
453
  if (!isCallable(subject) || items === null || !pattern) {
439
- fail.add('C3', table ? 'a search table must be searched(Goal, Answers) with an array of answers'
454
+ fail.add('C3', table ? `a ${calling ? 'call' : 'search'} table must be ${calling ? 'called' : 'searched'}(Goal, Answers) with an array of answers`
440
455
  : 'a listing must be derived(Pattern, Facts) with an array of facts', subject);
441
456
  return;
442
457
  }
443
- const entryKey = table ? variant(subject) : key(subject);
444
- const entries = table ? certificate.tables : certificate.listings;
458
+ const entryKey = table ? variant(subject, calling) : key(subject);
459
+ const entries = calling ? certificate.calls : table ? certificate.tables : certificate.listings;
445
460
  if (entries.has(entryKey)) {
446
- fail.add('C3', `duplicate ${table ? 'search table' : 'listing'} for ${table ? text(subject) : entryKey}`, subject);
461
+ fail.add('C3', `duplicate ${calling ? 'call table' : table ? 'search table' : 'listing'} for ${table ? text(subject) : entryKey}`, subject);
447
462
  return;
448
463
  }
449
464
  entries.set(entryKey, items);
450
- certificate.order.push([table ? 'table' : 'listing', entryKey, subject]);
465
+ certificate.order.push([calling ? 'call' : table ? 'table' : 'listing', entryKey, subject]);
451
466
  }
452
467
 
453
468
  function clauseNamed(program, id) {
@@ -970,6 +985,9 @@ function checkRelevance(program, claims, steps, goals, fail, spellings, settleme
970
985
  const reached = new Set();
971
986
  const reach = [];
972
987
  for (const claim of claims) for (const part of flattenConjunction(claim)) reach.push(spellings.spell(part));
988
+ for (const entry of settlement.calls.settler.listings.values()) {
989
+ for (const fact of entry.facts) reach.push(spellings.spell(fact));
990
+ }
973
991
  while (reach.length) {
974
992
  const stepId = reach.pop();
975
993
  if (reached.has(stepId) || !steps.has(stepId)) continue;
@@ -989,8 +1007,10 @@ function checkRelevance(program, claims, steps, goals, fail, spellings, settleme
989
1007
  if (!reached.has(stepId)) fail.add('C7', `step serves no claim: ${stepId}`, step.goal);
990
1008
  }
991
1009
  for (const [kind, entryKey, subject] of certificate.order) {
992
- const used = kind === 'table' ? settlement.settler.tables : settlement.settler.listings;
993
- if (!used.has(entryKey)) fail.add('C7', `search certificate serves no boundary: ${text(subject)}`, subject);
1010
+ const used = kind === 'call' ? settlement.calls?.settler.tables
1011
+ : kind === 'table' ? settlement.settler.tables : settlement.settler.listings;
1012
+ const callListing = kind === 'listing' && settlement.calls?.settler.listings.has(entryKey);
1013
+ if (!used?.has(entryKey) && !callListing) fail.add('C7', `search certificate serves no boundary or original call: ${text(subject)}`, subject);
994
1014
  }
995
1015
  }
996
1016
 
@@ -1079,15 +1099,15 @@ export function checkReport(report) {
1079
1099
  : report.trusted.length ? '// Answers checked subject to the obligations below.'
1080
1100
  : '// Checked answers: these follow from the input program.';
1081
1101
  const lines = [heading, ...report.answers, '',
1082
- '// C1-C9: resolution, acyclicity, justification, coverage, re-decision,',
1083
- '// boundary consistency, relevance, witness integrity and search closure.'];
1102
+ '// C1-C10: resolution, acyclicity, justification, coverage, re-decision,',
1103
+ '// boundary consistency, relevance, witness integrity, search closure and call fidelity.'];
1084
1104
  let previous = null;
1085
1105
  for (const fact of facts) {
1086
1106
  if (fact.name === 'failure' && previous !== 'failure') {
1087
1107
  lines.push('', '// Failures: the claims above have not passed every check.');
1088
1108
  }
1089
1109
  if (fact.name === 'obligation' && previous !== 'obligation') {
1090
- lines.push('', '// Obligations: absence and collection completeness are taken on trust.');
1110
+ lines.push('', '// Obligations: absence, collection or original-call evidence remains unproved.');
1091
1111
  }
1092
1112
  if (fact.name === 'steps') lines.push('', '// Proof statistics: recorded steps and how they were checked.');
1093
1113
  if (fact.name === 'verdict') lines.push('', '// This verdict applies to the claims above; it does not establish completeness.');
package/src/version.js CHANGED
@@ -1,2 +1,2 @@
1
1
  // The release this checkout is; ./release keeps it in step with package.json.
2
- export const VERSION = '0.3.2';
2
+ export const VERSION = '0.3.4';
package/src/writer.js CHANGED
@@ -7,7 +7,7 @@
7
7
  // ...T for an open tail. A conjunction or disjunction built left to right,
8
8
  // and(and(A, B), C), is written and(A, B, C). A compound whose name is not a
9
9
  // plain identifier is written struct('name', Arg, ...).
10
- import { Struct, Var, deref } from './terms.js';
10
+ import { Struct, Var, deref, isAnonymous } from './terms.js';
11
11
 
12
12
  // Words a document cannot use as a plain name: JavaScript's reserved words,
13
13
  // and the names of its values that are not terms (Section 9.1).
@@ -138,14 +138,15 @@ export function letterName(n) {
138
138
  // Names for write(): each variable met is named A, B, ..., Z, A1, ... in the
139
139
  // order the writing meets it, across every write sharing the lettering.
140
140
  export class Lettering {
141
- constructor() {
141
+ constructor(anonymous = false) {
142
142
  this.names = new Map();
143
+ this.anonymous = anonymous;
143
144
  }
144
145
 
145
146
  get(name) {
146
147
  let letter = this.names.get(name);
147
148
  if (letter === undefined) {
148
- letter = letterName(this.names.size);
149
+ letter = this.anonymous && isAnonymous(new Var(name)) ? `_${this.names.size}` : letterName(this.names.size);
149
150
  this.names.set(name, letter);
150
151
  }
151
152
  return letter;