eyemind 0.3.1 → 0.3.3
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 +2 -2
- package/SPEC.md +248 -26
- package/package.json +1 -1
- package/src/cli.js +2 -2
- package/src/closure.js +41 -21
- package/src/common.js +3 -3
- package/src/engine.js +24 -12
- package/src/fidelity.js +120 -0
- package/src/index.js +1 -1
- package/src/lang.js +1 -1
- package/src/program.js +1 -1
- package/src/proof.js +227 -54
- package/src/version.js +1 -1
- package/src/writer.js +4 -3
package/src/proof.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Proof documents: rendering
|
|
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
|
|
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:
|
|
236
|
-
'// negations
|
|
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
|
|
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();
|
|
@@ -327,8 +336,11 @@ function check(program, claims, steps, certificate, goals, allowTrusted, fail, s
|
|
|
327
336
|
fail.add('C4', `unjustified claim ${text(claim)}`, claim);
|
|
328
337
|
}
|
|
329
338
|
}
|
|
330
|
-
const
|
|
339
|
+
const provenance = new WitnessProvenance(program, steps, certificate, settlement.outcomes, spellings);
|
|
340
|
+
const tally = checkSteps(program, steps, allowTrusted, fail, spellings, name, settlement.outcomes, provenance);
|
|
331
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);
|
|
332
344
|
checkRelevance(program, claims, steps, goals || [], fail, spellings, settlement, certificate);
|
|
333
345
|
checkWellFounded(steps, fail, spellings);
|
|
334
346
|
let uses = 0;
|
|
@@ -344,6 +356,7 @@ function check(program, claims, steps, certificate, goals, allowTrusted, fail, s
|
|
|
344
356
|
{ id: 'C7', name: 'relevance', covered: claims.length + steps.size, failed: failed('C7') },
|
|
345
357
|
{ id: 'C8', name: 'witness_integrity', covered: tally.witnessed, failed: failed('C8') },
|
|
346
358
|
{ id: 'C9', name: 'search_closure', covered: closed, failed: failed('C9') },
|
|
359
|
+
{ id: 'C10', name: 'call_fidelity', covered: settlement.calls.covered, failed: failed('C10') },
|
|
347
360
|
];
|
|
348
361
|
return {
|
|
349
362
|
valid: !fail.items.length,
|
|
@@ -364,16 +377,18 @@ function check(program, claims, steps, certificate, goals, allowTrusted, fail, s
|
|
|
364
377
|
}
|
|
365
378
|
|
|
366
379
|
// The verdict allowTrusted false would give, from a report without it.
|
|
367
|
-
// Without failures,
|
|
368
|
-
//
|
|
369
|
-
//
|
|
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.
|
|
370
383
|
export function strict(report) {
|
|
371
384
|
if (report.failures.length) return null;
|
|
372
385
|
const failures = report.trusted.map((record) => ({
|
|
373
|
-
condition: '
|
|
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,
|
|
374
389
|
}));
|
|
375
390
|
const conditions = report.conditions.map((condition) => (
|
|
376
|
-
condition.id
|
|
391
|
+
['C5', 'C10'].includes(condition.id) ? { ...condition, failed: failures.filter((failure) => failure.condition === condition.id).length } : { ...condition }));
|
|
377
392
|
return { ...report, valid: !failures.length, failures, conditions, failureTerms: [...report.trustedTerms] };
|
|
378
393
|
}
|
|
379
394
|
|
|
@@ -383,11 +398,11 @@ export function strict(report) {
|
|
|
383
398
|
function readDocument(program, document, fail, spellings) {
|
|
384
399
|
const claims = [];
|
|
385
400
|
const steps = new Map();
|
|
386
|
-
const certificate = { tables: new Map(), listings: new Map(), order: [] };
|
|
401
|
+
const certificate = { tables: new Map(), calls: new Map(), listings: new Map(), order: [] };
|
|
387
402
|
const empty = new Env();
|
|
388
403
|
try {
|
|
389
404
|
for (const [term] of readTerms(document)) {
|
|
390
|
-
if (isTerm(term, 'searched', 2) || isTerm(term, 'derived', 2)) {
|
|
405
|
+
if (isTerm(term, 'searched', 2) || isTerm(term, 'derived', 2) || isTerm(term, 'called', 2)) {
|
|
391
406
|
readCertificate(term, certificate, fail, empty);
|
|
392
407
|
continue;
|
|
393
408
|
}
|
|
@@ -424,29 +439,30 @@ function readDocument(program, document, fail, spellings) {
|
|
|
424
439
|
return [claims, steps, certificate];
|
|
425
440
|
}
|
|
426
441
|
|
|
427
|
-
// A search certificate statement: searched
|
|
428
|
-
// answer of
|
|
429
|
-
// derived for the relation of
|
|
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.
|
|
430
445
|
function readCertificate(term, certificate, fail, empty) {
|
|
431
446
|
const [subject, list] = term.args;
|
|
432
447
|
const items = properListItems(list, empty);
|
|
433
|
-
const
|
|
448
|
+
const calling = term.name === 'called';
|
|
449
|
+
const table = term.name === 'searched' || calling;
|
|
434
450
|
// A listing names its relation by the name applied to distinct variables.
|
|
435
451
|
const pattern = table || typeof subject === 'string' || (subject instanceof Struct &&
|
|
436
452
|
subject.args.every((arg, i) => arg instanceof Var && subject.args.findIndex((other) => other instanceof Var && other.name === arg.name) === i));
|
|
437
453
|
if (!isCallable(subject) || items === null || !pattern) {
|
|
438
|
-
fail.add('C3', table ?
|
|
454
|
+
fail.add('C3', table ? `a ${calling ? 'call' : 'search'} table must be ${calling ? 'called' : 'searched'}(Goal, Answers) with an array of answers`
|
|
439
455
|
: 'a listing must be derived(Pattern, Facts) with an array of facts', subject);
|
|
440
456
|
return;
|
|
441
457
|
}
|
|
442
|
-
const entryKey = table ? variant(subject) : key(subject);
|
|
443
|
-
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;
|
|
444
460
|
if (entries.has(entryKey)) {
|
|
445
|
-
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);
|
|
446
462
|
return;
|
|
447
463
|
}
|
|
448
464
|
entries.set(entryKey, items);
|
|
449
|
-
certificate.order.push([table ? 'table' : 'listing', entryKey, subject]);
|
|
465
|
+
certificate.order.push([calling ? 'call' : table ? 'table' : 'listing', entryKey, subject]);
|
|
450
466
|
}
|
|
451
467
|
|
|
452
468
|
function clauseNamed(program, id) {
|
|
@@ -460,7 +476,7 @@ const CONTROL_BY = 'control';
|
|
|
460
476
|
// C4, C1, C3 and C5 for each step: every use is justified, and the step is an
|
|
461
477
|
// instance of the clause it cites, a recomputed primitive, a control composed
|
|
462
478
|
// of its uses, or a trusted boundary the later checks confront.
|
|
463
|
-
function checkSteps(program, steps, allowTrusted, fail, spellings, name, outcomes) {
|
|
479
|
+
function checkSteps(program, steps, allowTrusted, fail, spellings, name, outcomes, provenance) {
|
|
464
480
|
const tally = { verified: 0, redecided: 0, composed: 0, witnessed: 0, boundaries: [], trusted: [] };
|
|
465
481
|
|
|
466
482
|
function covered(goal) {
|
|
@@ -478,7 +494,7 @@ function checkSteps(program, steps, allowTrusted, fail, spellings, name, outcome
|
|
|
478
494
|
if (!covered(use)) fail.add('C4', `unjustified use ${text(use)}`, goal);
|
|
479
495
|
}
|
|
480
496
|
if (isTerm(by, 'clause', 1)) {
|
|
481
|
-
if (checkResolution(program, step, fail, tally)) tally.verified++;
|
|
497
|
+
if (checkResolution(program, step, fail, tally, provenance)) tally.verified++;
|
|
482
498
|
} else if (isTerm(by, 'source', 1)) {
|
|
483
499
|
const clause = clauseNamed(program, by.args[0]);
|
|
484
500
|
const inspected = clause === null ? null : program.inspectedById.get(clause.id);
|
|
@@ -491,19 +507,7 @@ function checkSteps(program, steps, allowTrusted, fail, spellings, name, outcome
|
|
|
491
507
|
fail.add('C3', 'invalid builtin justification', goal);
|
|
492
508
|
continue;
|
|
493
509
|
}
|
|
494
|
-
|
|
495
|
-
try {
|
|
496
|
-
for (const env of primitive(goal, new Env())) {
|
|
497
|
-
if (resolvesTo(goal, env, goal)) {
|
|
498
|
-
agrees = true;
|
|
499
|
-
break;
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
} catch (error) {
|
|
503
|
-
// A primitive that cannot be recomputed does not pass C5.
|
|
504
|
-
if (!(error instanceof EyemindError)) throw error;
|
|
505
|
-
}
|
|
506
|
-
if (!agrees) agrees = nearHostMath(goal);
|
|
510
|
+
const agrees = redecides(goal);
|
|
507
511
|
if (!agrees) fail.add('C5', `primitive disagrees: ${text(goal)}`, goal);
|
|
508
512
|
else tally.redecided++;
|
|
509
513
|
} else if (by === CONTROL_BY) {
|
|
@@ -520,7 +524,10 @@ function checkSteps(program, steps, allowTrusted, fail, spellings, name, outcome
|
|
|
520
524
|
tally.composed++;
|
|
521
525
|
}
|
|
522
526
|
} else if ((by === 'absent' && isTerm(goal, 'not', 1)) || (by === 'collected' && isTerm(goal, 'findall', 3))) {
|
|
523
|
-
if (bindings.length || uses.length)
|
|
527
|
+
if (bindings.length || uses.length) {
|
|
528
|
+
fail.add('C3', 'trusted boundaries cannot have bindings or uses', goal);
|
|
529
|
+
continue;
|
|
530
|
+
}
|
|
524
531
|
tally.boundaries.push(goal);
|
|
525
532
|
// A boundary its search certificate settles is checked, not trusted.
|
|
526
533
|
if (outcomes.get(spellings.spell(goal))?.status === 'settled') continue;
|
|
@@ -533,6 +540,17 @@ function checkSteps(program, steps, allowTrusted, fail, spellings, name, outcome
|
|
|
533
540
|
return tally;
|
|
534
541
|
}
|
|
535
542
|
|
|
543
|
+
function redecides(goal) {
|
|
544
|
+
try {
|
|
545
|
+
for (const env of primitive(goal, new Env())) {
|
|
546
|
+
if (resolvesTo(goal, env, goal)) return true;
|
|
547
|
+
}
|
|
548
|
+
} catch (error) {
|
|
549
|
+
if (!(error instanceof EyemindError)) throw error;
|
|
550
|
+
}
|
|
551
|
+
return nearHostMath(goal);
|
|
552
|
+
}
|
|
553
|
+
|
|
536
554
|
const isConstant = (term) => typeof term === 'string' || typeof term === 'bigint' || typeof term === 'number';
|
|
537
555
|
|
|
538
556
|
// Shortcuts the checker may take; tests turn them off to compare with the
|
|
@@ -589,7 +607,7 @@ function flatResolution(clause, goal, bindings, uses) {
|
|
|
589
607
|
// variables of that clause, and under them one of the clause's heads is the
|
|
590
608
|
// step's goal and its body is exactly the step's uses. C8 is checked under
|
|
591
609
|
// the same substitution.
|
|
592
|
-
function checkResolution(program, step, fail, tally) {
|
|
610
|
+
function checkResolution(program, step, fail, tally, provenance = null) {
|
|
593
611
|
const { goal, by, bindings, uses } = step;
|
|
594
612
|
const clauseId = by.args[0];
|
|
595
613
|
const clause = clauseNamed(program, clauseId);
|
|
@@ -631,11 +649,11 @@ function checkResolution(program, step, fail, tally) {
|
|
|
631
649
|
if (matches && resolvesTo(candidate, env, goal) && body.every((item, i) => resolvesTo(item, env, uses[i]))) {
|
|
632
650
|
resolution = true;
|
|
633
651
|
// Any head the step can be an instance of decides; a verified witness wins.
|
|
634
|
-
let found = clause.forward ? checkWitnesses(clause, head, names, env) : null;
|
|
635
|
-
for (const parentId of clause.scopeIds) {
|
|
652
|
+
let found = clause.forward && provenance !== null ? checkWitnesses(clause, head, body, names, env, provenance) : null;
|
|
653
|
+
for (const parentId of provenance === null ? [] : clause.scopeIds) {
|
|
636
654
|
const parent = program.byId.get(parentId);
|
|
637
655
|
const scopeHead = freshTerm(parent.head, `check${clauseId}`, names);
|
|
638
|
-
const checked = checkWitnesses(parent, scopeHead, names, env);
|
|
656
|
+
const checked = checkWitnesses(parent, scopeHead, body.slice(0, parent.body.length), names, env, provenance);
|
|
639
657
|
if (checked === false) found = false;
|
|
640
658
|
else if (checked === true && found === null) found = true;
|
|
641
659
|
}
|
|
@@ -647,7 +665,7 @@ function checkResolution(program, step, fail, tally) {
|
|
|
647
665
|
fail.add('C1', `not an instance of source clause ${clauseId}: ${text(goal)}`, goal);
|
|
648
666
|
return false;
|
|
649
667
|
}
|
|
650
|
-
if (witnesses === false) fail.add('C8', `witnesses do not match the source scopes of clause ${clauseId}: ${text(goal)}`, goal);
|
|
668
|
+
if (witnesses === false) fail.add('C8', `witnesses or their premise provenance do not match the source scopes of clause ${clauseId}: ${text(goal)}`, goal);
|
|
651
669
|
else if (witnesses === true) tally.witnessed++;
|
|
652
670
|
return true;
|
|
653
671
|
}
|
|
@@ -655,11 +673,11 @@ function checkResolution(program, step, fail, tally) {
|
|
|
655
673
|
// C8: under a step's substitution, each witness of a forward rule is the
|
|
656
674
|
// Skolem term of this firing, skolem(Rule, Variable, Arguments), where
|
|
657
675
|
// Arguments are the ground values of the conclusions' other variables, in
|
|
658
|
-
// order of first occurrence, leaving out
|
|
659
|
-
//
|
|
676
|
+
// order of first occurrence, leaving out fresh parameters only when their
|
|
677
|
+
// premise evidence establishes that provenance. Returns null when the
|
|
660
678
|
// rule has no witnesses, else whether they all hold; head is the rule's
|
|
661
679
|
// renamed conclusions and names maps its variables to their renamed ones.
|
|
662
|
-
function checkWitnesses(clause, head, names, env) {
|
|
680
|
+
function checkWitnesses(clause, head, body, names, env, provenance) {
|
|
663
681
|
const witnessed = clause.witnessNames;
|
|
664
682
|
if (!witnessed.length) return null;
|
|
665
683
|
// An existential firing needs bound inspection values. A generic rule
|
|
@@ -688,11 +706,161 @@ function checkWitnesses(clause, head, names, env) {
|
|
|
688
706
|
const expected = clause.captures === null ? [] : [listFromItems(
|
|
689
707
|
clause.captures.map((name) => copyResolved(names.get(name), env)),
|
|
690
708
|
)];
|
|
709
|
+
const introduced = new Set(witnessed.map((name) => names.get(name).name));
|
|
710
|
+
let contextual = false;
|
|
691
711
|
for (const variable of variables(head).values()) {
|
|
692
712
|
const value = copyResolved(variable, env);
|
|
693
713
|
if (isGround(value) && !ofThisFiring(value)) expected.push(value);
|
|
714
|
+
else if (!introduced.has(variable.name)) contextual = true;
|
|
715
|
+
}
|
|
716
|
+
return text(listFromItems(expected)) === text(firing) &&
|
|
717
|
+
(!contextual || provenance.check(clause, head, body, names, env, firing));
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
// A value may be omitted from a firing's context only when its apparent
|
|
721
|
+
// fresh symbols really are parameters of the premise derivation. Abstract
|
|
722
|
+
// those symbols together, then check the recorded evidence against the
|
|
723
|
+
// source again. In particular, abstracting a literal input Skolem constant
|
|
724
|
+
// must fail; its spelling does not make it a fresh value of this firing.
|
|
725
|
+
class WitnessProvenance {
|
|
726
|
+
constructor(program, steps, certificate, outcomes, spellings) {
|
|
727
|
+
this.program = program;
|
|
728
|
+
this.steps = steps;
|
|
729
|
+
this.certificate = certificate;
|
|
730
|
+
this.outcomes = outcomes;
|
|
731
|
+
this.spellings = spellings;
|
|
732
|
+
this.serial = 0;
|
|
733
|
+
this.usedNames = null;
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
fresh() {
|
|
737
|
+
if (this.usedNames === null) {
|
|
738
|
+
this.usedNames = new Map();
|
|
739
|
+
for (const step of this.steps.values()) {
|
|
740
|
+
for (const term of [step.goal, ...step.bindings, ...step.uses]) variables(term, this.usedNames);
|
|
741
|
+
}
|
|
742
|
+
for (const clause of this.program.clauses) {
|
|
743
|
+
for (const term of [clause.head, ...clause.body, ...clause.extraTerms]) variables(term, this.usedNames);
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
let name;
|
|
747
|
+
do { name = `\0witness${++this.serial}`; } while (this.usedNames.has(name));
|
|
748
|
+
return new Var(name);
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
check(clause, head, body, names, env, firing) {
|
|
752
|
+
const argumentsId = text(firing);
|
|
753
|
+
const symbols = new Map();
|
|
754
|
+
const copied = new Map();
|
|
755
|
+
const abstract = (term) => {
|
|
756
|
+
if (!(term instanceof Struct)) return term;
|
|
757
|
+
if (copied.has(term)) return copied.get(term);
|
|
758
|
+
let result;
|
|
759
|
+
if (isTerm(term, 'skolem', 3) && term.args[0] === BigInt(clause.id) && text(term.args[2]) === argumentsId) {
|
|
760
|
+
const id = text(term);
|
|
761
|
+
if (!symbols.has(id)) symbols.set(id, this.fresh());
|
|
762
|
+
result = symbols.get(id);
|
|
763
|
+
} else {
|
|
764
|
+
const args = term.args.map(abstract);
|
|
765
|
+
result = args.every((arg, i) => arg === term.args[i]) ? term : new Struct(term.name, args);
|
|
766
|
+
}
|
|
767
|
+
copied.set(term, result);
|
|
768
|
+
return result;
|
|
769
|
+
};
|
|
770
|
+
// Captures belong to an already-created instance, before this firing.
|
|
771
|
+
for (const name of clause.captures || []) {
|
|
772
|
+
const value = copyResolved(names.get(name), env);
|
|
773
|
+
if (abstract(value) !== value) return false;
|
|
774
|
+
}
|
|
775
|
+
const projected = abstract(copyResolved(new Struct('scope', [head, ...body]), env));
|
|
776
|
+
const source = new Struct('scope', [clause.head, ...clause.body]);
|
|
777
|
+
if (!isInstance(projected, source, 'provenance')) return false;
|
|
778
|
+
const pending = body.map((goal) => copyResolved(goal, env));
|
|
779
|
+
const seen = new Set();
|
|
780
|
+
const fail = new Failures();
|
|
781
|
+
while (pending.length) {
|
|
782
|
+
const original = pending.pop();
|
|
783
|
+
if (isTerm(original, 'and', 2)) {
|
|
784
|
+
pending.push(...flattenConjunction(original));
|
|
785
|
+
continue;
|
|
786
|
+
}
|
|
787
|
+
const goal = abstract(original);
|
|
788
|
+
if (goal === original) continue;
|
|
789
|
+
const id = this.spellings.spell(original);
|
|
790
|
+
if (seen.has(id)) continue;
|
|
791
|
+
seen.add(id);
|
|
792
|
+
const step = this.steps.get(id);
|
|
793
|
+
if (step === undefined) {
|
|
794
|
+
if (!isCallable(goal) || !(this.program.groups.get(key(goal)) || []).some((given) =>
|
|
795
|
+
!given.body.length && isInstance(goal, given.head, 'provenanceFact'))) return false;
|
|
796
|
+
continue;
|
|
797
|
+
}
|
|
798
|
+
if (isTerm(step.by, 'clause', 1)) {
|
|
799
|
+
const cited = clauseNamed(this.program, step.by.args[0]);
|
|
800
|
+
if (cited === null) return false;
|
|
801
|
+
if (cited.forward) {
|
|
802
|
+
// A premise's derived fact already exists. Its ground values are
|
|
803
|
+
// data, and only its universal variables may be instantiated.
|
|
804
|
+
if (!isInstance(goal, step.goal, 'provenanceDerived')) return false;
|
|
805
|
+
continue;
|
|
806
|
+
}
|
|
807
|
+
if (!this.fixedScopes(cited, step, abstract)) return false;
|
|
808
|
+
const generalized = { ...step, goal, bindings: step.bindings.map(abstract), uses: step.uses.map(abstract) };
|
|
809
|
+
// Original steps receive their own C8 checks. Here only source
|
|
810
|
+
// resolution is repeated, under the one shared abstraction.
|
|
811
|
+
if (!checkResolution(this.program, generalized, fail, { witnessed: 0 })) return false;
|
|
812
|
+
pending.push(...step.uses);
|
|
813
|
+
} else if (isTerm(step.by, 'source', 1)) {
|
|
814
|
+
const cited = clauseNamed(this.program, step.by.args[0]);
|
|
815
|
+
const inspected = cited === null ? null : this.program.inspectedById.get(cited.id);
|
|
816
|
+
if (!inspected || step.bindings.length || step.uses.length || !isInspection(goal) ||
|
|
817
|
+
!isInstance(goal, inspected.term, 'provenanceInspection')) return false;
|
|
818
|
+
} else if (step.by === BUILTIN_BY) {
|
|
819
|
+
if (step.bindings.length || step.uses.length || !isCallable(goal) ||
|
|
820
|
+
!PRIMITIVE_KEYS.has(key(goal)) || !redecides(goal)) return false;
|
|
821
|
+
} else if (step.by === CONTROL_BY) {
|
|
822
|
+
const candidates = isTerm(goal, 'call', 1) || isTerm(goal, 'once', 1) ? [goal.args[0]]
|
|
823
|
+
: isTerm(goal, 'or', 2) ? goal.args : [];
|
|
824
|
+
const uses = step.uses.map(abstract);
|
|
825
|
+
if (step.bindings.length || !candidates.some((candidate) => {
|
|
826
|
+
const parts = flattenConjunction(candidate);
|
|
827
|
+
return parts.length === uses.length && parts.every((part, i) => text(part) === text(uses[i]));
|
|
828
|
+
})) return false;
|
|
829
|
+
pending.push(...step.uses);
|
|
830
|
+
} else if (isBoundary(step)) {
|
|
831
|
+
// Generalizing a trusted assumption would silently strengthen it.
|
|
832
|
+
// Only the already-consulted certificate may discharge a stronger
|
|
833
|
+
// boundary; missing evidence fails provenance instead.
|
|
834
|
+
const outcome = this.outcomes.get(id);
|
|
835
|
+
if (outcome?.status !== 'settled') return false;
|
|
836
|
+
const tables = new Set(outcome.tables);
|
|
837
|
+
const listings = new Set(outcome.listings);
|
|
838
|
+
const settler = new Settler(this.program, {
|
|
839
|
+
answers: (call) => tables.has(variant(call)) ? this.certificate.tables.get(variant(call)) : null,
|
|
840
|
+
facts: (goalKey) => listings.has(goalKey) ? this.certificate.listings.get(goalKey) : null,
|
|
841
|
+
supported: (fact) => this.steps.has(this.spellings.spell(fact)),
|
|
842
|
+
});
|
|
843
|
+
if (settler.settle(goal).status !== 'settled') return false;
|
|
844
|
+
} else return false;
|
|
845
|
+
}
|
|
846
|
+
return true;
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
fixedScopes(clause, step, abstract) {
|
|
850
|
+
if (!clause.scopeIds.length) return true;
|
|
851
|
+
const [head, body, names] = freshClause(clause, 'provenanceScope');
|
|
852
|
+
const env = new Env();
|
|
853
|
+
for (const pair of step.bindings) {
|
|
854
|
+
if (!names.has(pair.args[0]) || !unify(names.get(pair.args[0]), pair.args[1], env)) return false;
|
|
855
|
+
}
|
|
856
|
+
if (!unify(head, step.goal, env) || body.length !== step.uses.length ||
|
|
857
|
+
!body.every((goal, i) => unify(goal, step.uses[i], env))) return false;
|
|
858
|
+
for (const term of clause.extraTerms) {
|
|
859
|
+
const value = copyResolved(freshTerm(term, 'provenanceScope', names), env);
|
|
860
|
+
if (abstract(value) !== value) return false;
|
|
861
|
+
}
|
|
862
|
+
return true;
|
|
694
863
|
}
|
|
695
|
-
return text(listFromItems(expected)) === text(firing);
|
|
696
864
|
}
|
|
697
865
|
|
|
698
866
|
// C9 and C6. A boundary its search certificate settles holds, and one whose
|
|
@@ -817,6 +985,9 @@ function checkRelevance(program, claims, steps, goals, fail, spellings, settleme
|
|
|
817
985
|
const reached = new Set();
|
|
818
986
|
const reach = [];
|
|
819
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
|
+
}
|
|
820
991
|
while (reach.length) {
|
|
821
992
|
const stepId = reach.pop();
|
|
822
993
|
if (reached.has(stepId) || !steps.has(stepId)) continue;
|
|
@@ -836,8 +1007,10 @@ function checkRelevance(program, claims, steps, goals, fail, spellings, settleme
|
|
|
836
1007
|
if (!reached.has(stepId)) fail.add('C7', `step serves no claim: ${stepId}`, step.goal);
|
|
837
1008
|
}
|
|
838
1009
|
for (const [kind, entryKey, subject] of certificate.order) {
|
|
839
|
-
const used = kind === '
|
|
840
|
-
|
|
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);
|
|
841
1014
|
}
|
|
842
1015
|
}
|
|
843
1016
|
|
|
@@ -926,15 +1099,15 @@ export function checkReport(report) {
|
|
|
926
1099
|
: report.trusted.length ? '// Answers checked subject to the obligations below.'
|
|
927
1100
|
: '// Checked answers: these follow from the input program.';
|
|
928
1101
|
const lines = [heading, ...report.answers, '',
|
|
929
|
-
'// C1-
|
|
930
|
-
'// boundary consistency, relevance, witness integrity and
|
|
1102
|
+
'// C1-C10: resolution, acyclicity, justification, coverage, re-decision,',
|
|
1103
|
+
'// boundary consistency, relevance, witness integrity, search closure and call fidelity.'];
|
|
931
1104
|
let previous = null;
|
|
932
1105
|
for (const fact of facts) {
|
|
933
1106
|
if (fact.name === 'failure' && previous !== 'failure') {
|
|
934
1107
|
lines.push('', '// Failures: the claims above have not passed every check.');
|
|
935
1108
|
}
|
|
936
1109
|
if (fact.name === 'obligation' && previous !== 'obligation') {
|
|
937
|
-
lines.push('', '// Obligations: absence
|
|
1110
|
+
lines.push('', '// Obligations: absence, collection or original-call evidence remains unproved.');
|
|
938
1111
|
}
|
|
939
1112
|
if (fact.name === 'steps') lines.push('', '// Proof statistics: recorded steps and how they were checked.');
|
|
940
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
|
+
export const VERSION = '0.3.3';
|
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;
|