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/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()' };