@polintpro/proposit-core 0.2.5 → 0.2.6

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.
Files changed (59) hide show
  1. package/README.md +79 -27
  2. package/dist/cli/commands/analysis.js +1 -1
  3. package/dist/cli/commands/analysis.js.map +1 -1
  4. package/dist/cli/commands/expressions.d.ts.map +1 -1
  5. package/dist/cli/commands/expressions.js +9 -3
  6. package/dist/cli/commands/expressions.js.map +1 -1
  7. package/dist/cli/commands/premises.d.ts.map +1 -1
  8. package/dist/cli/commands/premises.js +28 -11
  9. package/dist/cli/commands/premises.js.map +1 -1
  10. package/dist/cli/engine.d.ts +1 -1
  11. package/dist/cli/engine.d.ts.map +1 -1
  12. package/dist/cli/engine.js +14 -6
  13. package/dist/cli/engine.js.map +1 -1
  14. package/dist/cli/import.d.ts.map +1 -1
  15. package/dist/cli/import.js +10 -6
  16. package/dist/cli/import.js.map +1 -1
  17. package/dist/cli/schemata.d.ts +3 -0
  18. package/dist/cli/schemata.d.ts.map +1 -1
  19. package/dist/cli/schemata.js +2 -1
  20. package/dist/cli/schemata.js.map +1 -1
  21. package/dist/index.d.ts +3 -3
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +2 -2
  24. package/dist/index.js.map +1 -1
  25. package/dist/lib/consts.d.ts.map +1 -1
  26. package/dist/lib/consts.js +7 -1
  27. package/dist/lib/consts.js.map +1 -1
  28. package/dist/lib/core/ArgumentEngine.d.ts +34 -13
  29. package/dist/lib/core/ArgumentEngine.d.ts.map +1 -1
  30. package/dist/lib/core/ArgumentEngine.js +149 -23
  31. package/dist/lib/core/ArgumentEngine.js.map +1 -1
  32. package/dist/lib/core/ExpressionManager.d.ts +18 -8
  33. package/dist/lib/core/ExpressionManager.d.ts.map +1 -1
  34. package/dist/lib/core/ExpressionManager.js +42 -16
  35. package/dist/lib/core/ExpressionManager.js.map +1 -1
  36. package/dist/lib/core/PremiseEngine.d.ts +195 -0
  37. package/dist/lib/core/PremiseEngine.d.ts.map +1 -0
  38. package/dist/lib/core/PremiseEngine.js +851 -0
  39. package/dist/lib/core/PremiseEngine.js.map +1 -0
  40. package/dist/lib/core/VariableManager.d.ts +12 -2
  41. package/dist/lib/core/VariableManager.d.ts.map +1 -1
  42. package/dist/lib/core/VariableManager.js +19 -5
  43. package/dist/lib/core/VariableManager.js.map +1 -1
  44. package/dist/lib/core/diff.js +2 -2
  45. package/dist/lib/core/diff.js.map +1 -1
  46. package/dist/lib/core/relationships.d.ts +2 -2
  47. package/dist/lib/core/relationships.d.ts.map +1 -1
  48. package/dist/lib/core/relationships.js.map +1 -1
  49. package/dist/lib/index.d.ts +7 -2
  50. package/dist/lib/index.d.ts.map +1 -1
  51. package/dist/lib/index.js +2 -2
  52. package/dist/lib/index.js.map +1 -1
  53. package/dist/lib/schemata/propositional.d.ts +11 -0
  54. package/dist/lib/schemata/propositional.d.ts.map +1 -1
  55. package/dist/lib/schemata/propositional.js +3 -0
  56. package/dist/lib/schemata/propositional.js.map +1 -1
  57. package/dist/lib/types/evaluation.d.ts +1 -9
  58. package/dist/lib/types/evaluation.d.ts.map +1 -1
  59. package/package.json +1 -1
@@ -0,0 +1,851 @@
1
+ import { DefaultMap } from "../utils.js";
2
+ import { sortedCopyById, sortedUnique } from "../utils/collections.js";
3
+ import { buildDirectionalVacuity, kleeneAnd, kleeneIff, kleeneImplies, kleeneNot, kleeneOr, makeErrorIssue, makeValidationResult, } from "./evaluation/shared.js";
4
+ import { DEFAULT_CHECKSUM_CONFIG } from "../consts.js";
5
+ import { ChangeCollector } from "./ChangeCollector.js";
6
+ import { canonicalSerialize, computeHash, entityChecksum } from "./checksum.js";
7
+ import { ExpressionManager } from "./ExpressionManager.js";
8
+ import { VariableManager } from "./VariableManager.js";
9
+ export class PremiseEngine {
10
+ premise;
11
+ rootExpressionId;
12
+ variables;
13
+ expressions;
14
+ expressionsByVariableId;
15
+ argument;
16
+ checksumConfig;
17
+ checksumDirty = true;
18
+ cachedChecksum;
19
+ constructor(premise, deps, config) {
20
+ this.premise = { ...premise };
21
+ this.argument = deps.argument;
22
+ this.checksumConfig = config?.checksumConfig;
23
+ this.rootExpressionId = undefined;
24
+ this.variables = deps.variables;
25
+ this.expressions = new ExpressionManager(config);
26
+ this.expressionsByVariableId = new DefaultMap(() => new Set());
27
+ }
28
+ /**
29
+ * Deletes all expressions that reference the given variable ID,
30
+ * including their subtrees. Operator collapse runs after each removal.
31
+ * Returns all removed expressions in the changeset.
32
+ */
33
+ deleteExpressionsUsingVariable(variableId) {
34
+ const expressionIds = this.expressionsByVariableId.get(variableId);
35
+ if (expressionIds.size === 0) {
36
+ return { result: [], changes: {} };
37
+ }
38
+ const collector = new ChangeCollector();
39
+ // Copy the set since removeExpression mutates expressionsByVariableId
40
+ const removed = [];
41
+ for (const exprId of [...expressionIds]) {
42
+ // The expression may already have been removed as part of a
43
+ // prior subtree deletion or operator collapse in this loop.
44
+ if (!this.expressions.getExpression(exprId))
45
+ continue;
46
+ const { result, changes } = this.removeExpression(exprId, true);
47
+ if (result)
48
+ removed.push(result);
49
+ if (changes.expressions) {
50
+ for (const e of changes.expressions.removed) {
51
+ collector.removedExpression(e);
52
+ }
53
+ }
54
+ }
55
+ // Expressions in the collector already have checksums attached
56
+ // (from ExpressionManager which stores expressions with checksums).
57
+ return {
58
+ result: removed,
59
+ changes: collector.toChangeset(),
60
+ };
61
+ }
62
+ /**
63
+ * Adds an expression to this premise's tree.
64
+ *
65
+ * If the expression has `parentId: null` it becomes the root; only one
66
+ * root is permitted per premise. If `parentId` is non-null the parent
67
+ * must already exist within this premise.
68
+ *
69
+ * All other structural rules (`implies`/`iff` root-only, child limits,
70
+ * position uniqueness) are enforced by the underlying `ExpressionManager`.
71
+ *
72
+ * @throws If the premise already has a root expression and this one is also a root.
73
+ * @throws If the expression's parent does not exist in this premise.
74
+ * @throws If the expression is a variable reference and the variable has not been registered.
75
+ * @throws If the expression does not belong to this argument.
76
+ */
77
+ addExpression(expression) {
78
+ this.assertBelongsToArgument(expression.argumentId, expression.argumentVersion);
79
+ if (expression.type === "variable" &&
80
+ !this.variables.hasVariable(expression.variableId)) {
81
+ throw new Error(`Variable expression "${expression.id}" references non-existent variable "${expression.variableId}".`);
82
+ }
83
+ if (expression.parentId === null) {
84
+ if (this.rootExpressionId !== undefined) {
85
+ throw new Error(`Premise "${this.premise.id}" already has a root expression.`);
86
+ }
87
+ }
88
+ else {
89
+ if (!this.expressions.getExpression(expression.parentId)) {
90
+ throw new Error(`Parent expression "${expression.parentId}" does not exist in this premise.`);
91
+ }
92
+ }
93
+ const collector = new ChangeCollector();
94
+ this.expressions.setCollector(collector);
95
+ try {
96
+ // Delegate structural validation (operator type checks, position
97
+ // uniqueness, child limits) to ExpressionManager.
98
+ this.expressions.addExpression(expression);
99
+ if (expression.parentId === null) {
100
+ this.rootExpressionId = expression.id;
101
+ }
102
+ if (expression.type === "variable") {
103
+ this.expressionsByVariableId
104
+ .get(expression.variableId)
105
+ .add(expression.id);
106
+ }
107
+ this.markDirty();
108
+ return {
109
+ result: this.expressions.getExpression(expression.id),
110
+ changes: collector.toChangeset(),
111
+ };
112
+ }
113
+ finally {
114
+ this.expressions.setCollector(null);
115
+ }
116
+ }
117
+ /**
118
+ * Adds an expression as the last child of the given parent, with
119
+ * position computed automatically.
120
+ *
121
+ * If `parentId` is `null`, the expression becomes the root.
122
+ *
123
+ * @throws If the premise already has a root and parentId is null.
124
+ * @throws If the expression does not belong to this argument.
125
+ * @throws If the expression is a variable reference and the variable has not been registered.
126
+ */
127
+ appendExpression(parentId, expression) {
128
+ this.assertBelongsToArgument(expression.argumentId, expression.argumentVersion);
129
+ if (expression.type === "variable" &&
130
+ !this.variables.hasVariable(expression.variableId)) {
131
+ throw new Error(`Variable expression "${expression.id}" references non-existent variable "${expression.variableId}".`);
132
+ }
133
+ if (parentId === null) {
134
+ if (this.rootExpressionId !== undefined) {
135
+ throw new Error(`Premise "${this.premise.id}" already has a root expression.`);
136
+ }
137
+ }
138
+ else {
139
+ if (!this.expressions.getExpression(parentId)) {
140
+ throw new Error(`Parent expression "${parentId}" does not exist in this premise.`);
141
+ }
142
+ }
143
+ const collector = new ChangeCollector();
144
+ this.expressions.setCollector(collector);
145
+ try {
146
+ this.expressions.appendExpression(parentId, expression);
147
+ if (parentId === null) {
148
+ this.syncRootExpressionId();
149
+ }
150
+ if (expression.type === "variable") {
151
+ this.expressionsByVariableId
152
+ .get(expression.variableId)
153
+ .add(expression.id);
154
+ }
155
+ this.markDirty();
156
+ return {
157
+ result: this.expressions.getExpression(expression.id),
158
+ changes: collector.toChangeset(),
159
+ };
160
+ }
161
+ finally {
162
+ this.expressions.setCollector(null);
163
+ }
164
+ }
165
+ /**
166
+ * Adds an expression immediately before or after an existing sibling,
167
+ * with position computed automatically.
168
+ *
169
+ * @throws If the sibling does not exist in this premise.
170
+ * @throws If the expression does not belong to this argument.
171
+ * @throws If the expression is a variable reference and the variable has not been registered.
172
+ */
173
+ addExpressionRelative(siblingId, relativePosition, expression) {
174
+ this.assertBelongsToArgument(expression.argumentId, expression.argumentVersion);
175
+ if (expression.type === "variable" &&
176
+ !this.variables.hasVariable(expression.variableId)) {
177
+ throw new Error(`Variable expression "${expression.id}" references non-existent variable "${expression.variableId}".`);
178
+ }
179
+ if (!this.expressions.getExpression(siblingId)) {
180
+ throw new Error(`Expression "${siblingId}" not found in this premise.`);
181
+ }
182
+ const collector = new ChangeCollector();
183
+ this.expressions.setCollector(collector);
184
+ try {
185
+ this.expressions.addExpressionRelative(siblingId, relativePosition, expression);
186
+ if (expression.type === "variable") {
187
+ this.expressionsByVariableId
188
+ .get(expression.variableId)
189
+ .add(expression.id);
190
+ }
191
+ this.markDirty();
192
+ return {
193
+ result: this.expressions.getExpression(expression.id),
194
+ changes: collector.toChangeset(),
195
+ };
196
+ }
197
+ finally {
198
+ this.expressions.setCollector(null);
199
+ }
200
+ }
201
+ /**
202
+ * Updates mutable fields of an existing expression in this premise.
203
+ *
204
+ * Only `position`, `variableId`, and `operator` may be updated. Structural
205
+ * fields (`id`, `parentId`, `type`, `argumentId`, `argumentVersion`,
206
+ * `checksum`) are forbidden — enforced by the underlying
207
+ * `ExpressionManager`.
208
+ *
209
+ * If `variableId` changes, the internal `expressionsByVariableId` index is
210
+ * updated so that cascade deletion (`deleteExpressionsUsingVariable`) stays
211
+ * correct.
212
+ *
213
+ * @throws If the expression does not exist in this premise.
214
+ * @throws If `variableId` references a non-existent variable.
215
+ */
216
+ updateExpression(expressionId, updates) {
217
+ const existing = this.expressions.getExpression(expressionId);
218
+ if (!existing) {
219
+ throw new Error(`Expression "${expressionId}" not found in premise "${this.premise.id}".`);
220
+ }
221
+ if (updates.variableId !== undefined) {
222
+ if (!this.variables.hasVariable(updates.variableId)) {
223
+ throw new Error(`Variable expression "${expressionId}" references non-existent variable "${updates.variableId}".`);
224
+ }
225
+ }
226
+ const collector = new ChangeCollector();
227
+ this.expressions.setCollector(collector);
228
+ try {
229
+ const oldVariableId = existing.type === "variable" ? existing.variableId : undefined;
230
+ const updated = this.expressions.updateExpression(expressionId, updates);
231
+ if (updates.variableId !== undefined &&
232
+ oldVariableId !== undefined &&
233
+ oldVariableId !== updates.variableId) {
234
+ this.expressionsByVariableId
235
+ .get(oldVariableId)
236
+ ?.delete(expressionId);
237
+ this.expressionsByVariableId
238
+ .get(updates.variableId)
239
+ .add(expressionId);
240
+ }
241
+ const changeset = collector.toChangeset();
242
+ if (changeset.expressions !== undefined) {
243
+ this.markDirty();
244
+ }
245
+ return {
246
+ result: updated,
247
+ changes: changeset,
248
+ };
249
+ }
250
+ finally {
251
+ this.expressions.setCollector(null);
252
+ }
253
+ }
254
+ /**
255
+ * Removes an expression and its entire descendant subtree, then collapses
256
+ * any ancestor operators with fewer than two children (same semantics as
257
+ * before). Returns the removed root expression, or `undefined` if not
258
+ * found.
259
+ *
260
+ * `rootExpressionId` is recomputed after every removal because operator
261
+ * collapse can silently promote a new expression into the root slot.
262
+ */
263
+ removeExpression(expressionId, deleteSubtree) {
264
+ // Snapshot the expression before removal (for result).
265
+ const snapshot = this.expressions.getExpression(expressionId);
266
+ const collector = new ChangeCollector();
267
+ this.expressions.setCollector(collector);
268
+ try {
269
+ if (!snapshot) {
270
+ return {
271
+ result: undefined,
272
+ changes: collector.toChangeset(),
273
+ };
274
+ }
275
+ if (deleteSubtree) {
276
+ // Snapshot the subtree before deletion so we can clean up
277
+ // expressionsByVariableId for cascade-deleted descendants — they are
278
+ // not individually surfaced by ExpressionManager.removeExpression.
279
+ const subtree = this.collectSubtree(expressionId);
280
+ this.expressions.removeExpression(expressionId, true);
281
+ for (const expr of subtree) {
282
+ if (expr.type === "variable") {
283
+ this.expressionsByVariableId
284
+ .get(expr.variableId)
285
+ ?.delete(expr.id);
286
+ }
287
+ }
288
+ }
289
+ else {
290
+ // Only clean up expressionsByVariableId for the removed
291
+ // expression itself — children survive promotion.
292
+ if (snapshot.type === "variable") {
293
+ this.expressionsByVariableId
294
+ .get(snapshot.variableId)
295
+ ?.delete(snapshot.id);
296
+ }
297
+ this.expressions.removeExpression(expressionId, false);
298
+ }
299
+ this.syncRootExpressionId();
300
+ this.markDirty();
301
+ return {
302
+ result: snapshot,
303
+ changes: collector.toChangeset(),
304
+ };
305
+ }
306
+ finally {
307
+ this.expressions.setCollector(null);
308
+ }
309
+ }
310
+ /**
311
+ * Splices a new expression between existing nodes in the tree. The new
312
+ * expression inherits the tree slot of the anchor node
313
+ * (`leftNodeId ?? rightNodeId`).
314
+ *
315
+ * `rootExpressionId` is recomputed after every insertion because the
316
+ * anchor may have been the root.
317
+ *
318
+ * See `ArgumentEngine.insertExpression` for the full contract; the same
319
+ * rules apply here.
320
+ *
321
+ * @throws If the expression does not belong to this argument.
322
+ * @throws If the expression is a variable reference and the variable has not been registered.
323
+ */
324
+ insertExpression(expression, leftNodeId, rightNodeId) {
325
+ this.assertBelongsToArgument(expression.argumentId, expression.argumentVersion);
326
+ if (expression.type === "variable" &&
327
+ !this.variables.hasVariable(expression.variableId)) {
328
+ throw new Error(`Variable expression "${expression.id}" references non-existent variable "${expression.variableId}".`);
329
+ }
330
+ const collector = new ChangeCollector();
331
+ this.expressions.setCollector(collector);
332
+ try {
333
+ this.expressions.insertExpression(expression, leftNodeId, rightNodeId);
334
+ if (expression.type === "variable") {
335
+ this.expressionsByVariableId
336
+ .get(expression.variableId)
337
+ .add(expression.id);
338
+ }
339
+ this.syncRootExpressionId();
340
+ this.markDirty();
341
+ return {
342
+ result: this.expressions.getExpression(expression.id),
343
+ changes: collector.toChangeset(),
344
+ };
345
+ }
346
+ finally {
347
+ this.expressions.setCollector(null);
348
+ }
349
+ }
350
+ /**
351
+ * Returns an expression by ID, or `undefined` if not found in this
352
+ * premise.
353
+ */
354
+ getExpression(id) {
355
+ return this.expressions.getExpression(id);
356
+ }
357
+ getId() {
358
+ return this.premise.id;
359
+ }
360
+ getExtras() {
361
+ const { id: _id, argumentId: _argumentId, argumentVersion: _argumentVersion, rootExpressionId: _rootExpressionId, variables: _variables, expressions: _expressions, checksum: _checksum, ...extras } = this.premise;
362
+ return { ...extras };
363
+ }
364
+ setExtras(extras) {
365
+ // Strip old extras and replace with new ones
366
+ const { id, argumentId, argumentVersion, rootExpressionId, variables, expressions, checksum, } = this.premise;
367
+ this.premise = {
368
+ ...extras,
369
+ id,
370
+ argumentId,
371
+ argumentVersion,
372
+ ...(rootExpressionId !== undefined ? { rootExpressionId } : {}),
373
+ ...(variables !== undefined ? { variables } : {}),
374
+ ...(expressions !== undefined ? { expressions } : {}),
375
+ ...(checksum !== undefined ? { checksum } : {}),
376
+ };
377
+ this.markDirty();
378
+ return { result: this.getExtras(), changes: {} };
379
+ }
380
+ getRootExpressionId() {
381
+ return this.rootExpressionId;
382
+ }
383
+ getRootExpression() {
384
+ if (this.rootExpressionId === undefined) {
385
+ return undefined;
386
+ }
387
+ return this.expressions.getExpression(this.rootExpressionId);
388
+ }
389
+ /**
390
+ * Returns all argument-level variables (from the shared VariableManager)
391
+ * sorted by ID. Since the VariableManager is shared across all premises,
392
+ * this returns every registered variable — not just those referenced by
393
+ * expressions in this premise.
394
+ */
395
+ getVariables() {
396
+ return sortedCopyById(this.variables.toArray());
397
+ }
398
+ getExpressions() {
399
+ return sortedCopyById(this.expressions.toArray());
400
+ }
401
+ getChildExpressions(parentId) {
402
+ return this.expressions.getChildExpressions(parentId);
403
+ }
404
+ /**
405
+ * Returns `true` if the root expression is an `implies` or `iff` operator,
406
+ * meaning this premise expresses a logical inference relationship.
407
+ */
408
+ isInference() {
409
+ const root = this.getRootExpression();
410
+ return (root?.type === "operator" &&
411
+ (root.operator === "implies" || root.operator === "iff"));
412
+ }
413
+ /**
414
+ * Returns `true` if this premise does not have an inference operator at its
415
+ * root (i.e. it is a constraint premise). Equivalent to `!isInference()`.
416
+ */
417
+ isConstraint() {
418
+ return !this.isInference();
419
+ }
420
+ validateEvaluability() {
421
+ const issues = [];
422
+ const roots = this.expressions.getChildExpressions(null);
423
+ if (this.expressions.toArray().length === 0) {
424
+ issues.push(makeErrorIssue({
425
+ code: "PREMISE_EMPTY",
426
+ message: `Premise "${this.premise.id}" has no expressions to evaluate.`,
427
+ premiseId: this.premise.id,
428
+ }));
429
+ return makeValidationResult(issues);
430
+ }
431
+ if (roots.length === 0) {
432
+ issues.push(makeErrorIssue({
433
+ code: "PREMISE_ROOT_MISSING",
434
+ message: `Premise "${this.premise.id}" has expressions but no root expression.`,
435
+ premiseId: this.premise.id,
436
+ }));
437
+ }
438
+ if (this.rootExpressionId === undefined) {
439
+ issues.push(makeErrorIssue({
440
+ code: "PREMISE_ROOT_MISSING",
441
+ message: `Premise "${this.premise.id}" does not have rootExpressionId set.`,
442
+ premiseId: this.premise.id,
443
+ }));
444
+ }
445
+ else if (!this.expressions.getExpression(this.rootExpressionId)) {
446
+ issues.push(makeErrorIssue({
447
+ code: "PREMISE_ROOT_MISMATCH",
448
+ message: `Premise "${this.premise.id}" rootExpressionId "${this.rootExpressionId}" does not exist.`,
449
+ premiseId: this.premise.id,
450
+ expressionId: this.rootExpressionId,
451
+ }));
452
+ }
453
+ else if (roots[0] && roots[0].id !== this.rootExpressionId) {
454
+ issues.push(makeErrorIssue({
455
+ code: "PREMISE_ROOT_MISMATCH",
456
+ message: `Premise "${this.premise.id}" rootExpressionId "${this.rootExpressionId}" does not match actual root "${roots[0].id}".`,
457
+ premiseId: this.premise.id,
458
+ expressionId: this.rootExpressionId,
459
+ }));
460
+ }
461
+ for (const expr of this.expressions.toArray()) {
462
+ if (expr.type === "variable" &&
463
+ !this.variables.hasVariable(expr.variableId)) {
464
+ issues.push(makeErrorIssue({
465
+ code: "EXPR_VARIABLE_UNDECLARED",
466
+ message: `Expression "${expr.id}" references undeclared variable "${expr.variableId}".`,
467
+ premiseId: this.premise.id,
468
+ expressionId: expr.id,
469
+ variableId: expr.variableId,
470
+ }));
471
+ }
472
+ if (expr.type !== "operator" && expr.type !== "formula") {
473
+ continue;
474
+ }
475
+ const children = this.expressions.getChildExpressions(expr.id);
476
+ if (expr.type === "formula") {
477
+ if (children.length !== 1) {
478
+ issues.push(makeErrorIssue({
479
+ code: "EXPR_CHILD_COUNT_INVALID",
480
+ message: `Formula expression "${expr.id}" must have exactly 1 child; found ${children.length}.`,
481
+ premiseId: this.premise.id,
482
+ expressionId: expr.id,
483
+ }));
484
+ }
485
+ continue;
486
+ }
487
+ if (expr.operator === "not" && children.length !== 1) {
488
+ issues.push(makeErrorIssue({
489
+ code: "EXPR_CHILD_COUNT_INVALID",
490
+ message: `Operator "${expr.id}" (not) must have exactly 1 child; found ${children.length}.`,
491
+ premiseId: this.premise.id,
492
+ expressionId: expr.id,
493
+ }));
494
+ }
495
+ if ((expr.operator === "implies" || expr.operator === "iff") &&
496
+ children.length !== 2) {
497
+ issues.push(makeErrorIssue({
498
+ code: "EXPR_CHILD_COUNT_INVALID",
499
+ message: `Operator "${expr.id}" (${expr.operator}) must have exactly 2 children; found ${children.length}.`,
500
+ premiseId: this.premise.id,
501
+ expressionId: expr.id,
502
+ }));
503
+ }
504
+ if ((expr.operator === "and" || expr.operator === "or") &&
505
+ children.length < 2) {
506
+ issues.push(makeErrorIssue({
507
+ code: "EXPR_CHILD_COUNT_INVALID",
508
+ message: `Operator "${expr.id}" (${expr.operator}) must have at least 2 children; found ${children.length}.`,
509
+ premiseId: this.premise.id,
510
+ expressionId: expr.id,
511
+ }));
512
+ }
513
+ if (expr.operator === "implies" || expr.operator === "iff") {
514
+ const childPositions = new Set(children.map((child) => child.position));
515
+ if (!childPositions.has(0) || !childPositions.has(1)) {
516
+ issues.push(makeErrorIssue({
517
+ code: "EXPR_BINARY_POSITIONS_INVALID",
518
+ message: `Operator "${expr.id}" (${expr.operator}) must have children at positions 0 and 1.`,
519
+ premiseId: this.premise.id,
520
+ expressionId: expr.id,
521
+ }));
522
+ }
523
+ }
524
+ }
525
+ return makeValidationResult(issues);
526
+ }
527
+ /**
528
+ * Evaluates the premise under a three-valued expression assignment.
529
+ *
530
+ * Variable values are looked up in `assignment.variables` using Kleene
531
+ * three-valued logic (`null` = unknown). Missing variables default to `null`.
532
+ * Expressions listed in `assignment.rejectedExpressionIds` evaluate to
533
+ * `false` and their children are not evaluated.
534
+ *
535
+ * For inference premises (`implies`/`iff`), an `inferenceDiagnostic` is
536
+ * computed with three-valued fields unless the root is rejected.
537
+ */
538
+ evaluate(assignment, options) {
539
+ const validation = this.validateEvaluability();
540
+ if (!validation.ok) {
541
+ throw new Error(`Premise "${this.premise.id}" is not evaluable: ${validation.issues
542
+ .map((issue) => issue.code)
543
+ .join(", ")}`);
544
+ }
545
+ const rootExpressionId = this.rootExpressionId;
546
+ const referencedVariableIds = sortedUnique(this.expressions
547
+ .toArray()
548
+ .filter((expr) => expr.type === "variable")
549
+ .map((expr) => expr.variableId));
550
+ if (options?.strictUnknownKeys || options?.requireExactCoverage) {
551
+ const knownVariableIds = new Set(referencedVariableIds);
552
+ const unknownKeys = Object.keys(assignment.variables).filter((variableId) => !knownVariableIds.has(variableId));
553
+ if (unknownKeys.length > 0) {
554
+ throw new Error(`Assignment contains unknown variable IDs for premise "${this.premise.id}": ${unknownKeys.join(", ")}`);
555
+ }
556
+ }
557
+ const expressionValues = {};
558
+ const evaluateExpression = (expressionId) => {
559
+ const expression = this.expressions.getExpression(expressionId);
560
+ if (!expression) {
561
+ throw new Error(`Expression "${expressionId}" was not found.`);
562
+ }
563
+ if (assignment.rejectedExpressionIds.includes(expression.id)) {
564
+ expressionValues[expression.id] = false;
565
+ return false;
566
+ }
567
+ if (expression.type === "variable") {
568
+ const value = assignment.variables[expression.variableId] ?? null;
569
+ expressionValues[expression.id] = value;
570
+ return value;
571
+ }
572
+ const children = this.expressions.getChildExpressions(expression.id);
573
+ let value;
574
+ if (expression.type === "formula") {
575
+ value = evaluateExpression(children[0].id);
576
+ expressionValues[expression.id] = value;
577
+ return value;
578
+ }
579
+ switch (expression.operator) {
580
+ case "not":
581
+ value = kleeneNot(evaluateExpression(children[0].id));
582
+ break;
583
+ case "and":
584
+ value = children.reduce((acc, child) => kleeneAnd(acc, evaluateExpression(child.id)), true);
585
+ break;
586
+ case "or":
587
+ value = children.reduce((acc, child) => kleeneOr(acc, evaluateExpression(child.id)), false);
588
+ break;
589
+ case "implies": {
590
+ const left = children.find((child) => child.position === 0);
591
+ const right = children.find((child) => child.position === 1);
592
+ value = kleeneImplies(evaluateExpression(left.id), evaluateExpression(right.id));
593
+ break;
594
+ }
595
+ case "iff": {
596
+ const left = children.find((child) => child.position === 0);
597
+ const right = children.find((child) => child.position === 1);
598
+ value = kleeneIff(evaluateExpression(left.id), evaluateExpression(right.id));
599
+ break;
600
+ }
601
+ }
602
+ expressionValues[expression.id] = value;
603
+ return value;
604
+ };
605
+ const rootValue = evaluateExpression(rootExpressionId);
606
+ const variableValues = {};
607
+ for (const variableId of referencedVariableIds) {
608
+ variableValues[variableId] =
609
+ assignment.variables[variableId] ?? null;
610
+ }
611
+ let inferenceDiagnostic;
612
+ if (this.isInference() &&
613
+ !assignment.rejectedExpressionIds.includes(rootExpressionId)) {
614
+ const root = this.expressions.getExpression(rootExpressionId);
615
+ if (root?.type === "operator") {
616
+ const children = this.expressions.getChildExpressions(root.id);
617
+ const left = children.find((child) => child.position === 0);
618
+ const right = children.find((child) => child.position === 1);
619
+ if (left && right) {
620
+ const leftValue = expressionValues[left.id];
621
+ const rightValue = expressionValues[right.id];
622
+ if (root.operator === "implies") {
623
+ inferenceDiagnostic = {
624
+ kind: "implies",
625
+ premiseId: this.premise.id,
626
+ rootExpressionId,
627
+ leftValue,
628
+ rightValue,
629
+ rootValue,
630
+ antecedentTrue: leftValue,
631
+ consequentTrue: rightValue,
632
+ isVacuouslyTrue: kleeneNot(leftValue),
633
+ fired: leftValue,
634
+ firedAndHeld: kleeneAnd(leftValue, rightValue),
635
+ };
636
+ }
637
+ else if (root.operator === "iff") {
638
+ const leftToRight = buildDirectionalVacuity(leftValue, rightValue);
639
+ const rightToLeft = buildDirectionalVacuity(rightValue, leftValue);
640
+ inferenceDiagnostic = {
641
+ kind: "iff",
642
+ premiseId: this.premise.id,
643
+ rootExpressionId,
644
+ leftValue,
645
+ rightValue,
646
+ rootValue,
647
+ leftToRight,
648
+ rightToLeft,
649
+ bothSidesTrue: kleeneAnd(leftValue, rightValue),
650
+ bothSidesFalse: kleeneAnd(kleeneNot(leftValue), kleeneNot(rightValue)),
651
+ };
652
+ }
653
+ }
654
+ }
655
+ }
656
+ return {
657
+ premiseId: this.premise.id,
658
+ premiseType: this.isInference() ? "inference" : "constraint",
659
+ rootExpressionId,
660
+ rootValue,
661
+ expressionValues,
662
+ variableValues,
663
+ inferenceDiagnostic,
664
+ };
665
+ }
666
+ /**
667
+ * Returns a human-readable string of this premise's expression tree using
668
+ * standard logical notation (∧ ∨ ¬ → ↔). Missing operands are rendered
669
+ * as `(?)`. Returns an empty string when the premise has no expressions.
670
+ */
671
+ toDisplayString() {
672
+ if (this.rootExpressionId === undefined) {
673
+ return "";
674
+ }
675
+ return this.renderExpression(this.rootExpressionId);
676
+ }
677
+ /**
678
+ * Returns the set of variable IDs referenced by expressions in this premise.
679
+ * Only variables that appear in `type: "variable"` expression nodes are
680
+ * included — not all variables in the shared VariableManager.
681
+ */
682
+ getReferencedVariableIds() {
683
+ const ids = new Set();
684
+ for (const expr of this.expressions.toArray()) {
685
+ if (expr.type === "variable") {
686
+ ids.add(expr.variableId);
687
+ }
688
+ }
689
+ return ids;
690
+ }
691
+ /**
692
+ * Returns a serializable TPremise representation of this premise.
693
+ * Builds the premise data from the snapshot, including expressions,
694
+ * referenced variable IDs, and checksum.
695
+ */
696
+ toPremiseData() {
697
+ const snap = this.snapshot();
698
+ return {
699
+ ...snap.premise,
700
+ expressions: snap.expressions.expressions,
701
+ variables: [...this.getReferencedVariableIds()].sort(),
702
+ checksum: this.checksum(),
703
+ };
704
+ }
705
+ /**
706
+ * Returns a premise-level checksum combining all entity checksums.
707
+ * Computed lazily -- only recalculated when state has changed.
708
+ */
709
+ checksum() {
710
+ if (this.checksumDirty || this.cachedChecksum === undefined) {
711
+ this.cachedChecksum = this.computeChecksum();
712
+ this.checksumDirty = false;
713
+ }
714
+ return this.cachedChecksum;
715
+ }
716
+ // -------------------------------------------------------------------------
717
+ // Private helpers
718
+ // -------------------------------------------------------------------------
719
+ computeChecksum() {
720
+ const checksumMap = {};
721
+ // Premise's own entity checksum
722
+ const premiseFields = this.checksumConfig?.premiseFields ??
723
+ DEFAULT_CHECKSUM_CONFIG.premiseFields;
724
+ checksumMap[this.premise.id] = entityChecksum({
725
+ id: this.premise.id,
726
+ rootExpressionId: this.rootExpressionId,
727
+ }, premiseFields);
728
+ // All owned expression checksums
729
+ for (const expr of this.expressions.toArray()) {
730
+ checksumMap[expr.id] = expr.checksum;
731
+ }
732
+ return computeHash(canonicalSerialize(checksumMap));
733
+ }
734
+ /** Invalidate the cached checksum so the next call recomputes it. */
735
+ markDirty() {
736
+ this.checksumDirty = true;
737
+ }
738
+ /**
739
+ * Re-reads the single root from ExpressionManager after any operation
740
+ * that may have caused operator collapse to silently change the root.
741
+ */
742
+ syncRootExpressionId() {
743
+ const roots = this.expressions.getChildExpressions(null);
744
+ this.rootExpressionId = roots[0]?.id;
745
+ }
746
+ collectSubtree(rootId) {
747
+ const result = [];
748
+ const stack = [rootId];
749
+ while (stack.length > 0) {
750
+ const id = stack.pop();
751
+ const expr = this.expressions.getExpression(id);
752
+ if (!expr)
753
+ continue;
754
+ result.push(expr);
755
+ for (const child of this.expressions.getChildExpressions(id)) {
756
+ stack.push(child.id);
757
+ }
758
+ }
759
+ return result;
760
+ }
761
+ assertBelongsToArgument(argumentId, argumentVersion) {
762
+ if (argumentId !== this.argument.id) {
763
+ throw new Error(`Entity argumentId "${argumentId}" does not match engine argument ID "${this.argument.id}".`);
764
+ }
765
+ if (argumentVersion !== this.argument.version) {
766
+ throw new Error(`Entity argumentVersion "${argumentVersion}" does not match engine argument version "${this.argument.version}".`);
767
+ }
768
+ }
769
+ renderExpression(expressionId) {
770
+ const expression = this.expressions.getExpression(expressionId);
771
+ if (!expression) {
772
+ throw new Error(`Expression "${expressionId}" was not found.`);
773
+ }
774
+ if (expression.type === "variable") {
775
+ const variable = this.variables.getVariable(expression.variableId);
776
+ if (!variable) {
777
+ throw new Error(`Variable "${expression.variableId}" for expression "${expressionId}" was not found.`);
778
+ }
779
+ return variable.symbol;
780
+ }
781
+ if (expression.type === "formula") {
782
+ const children = this.expressions.getChildExpressions(expression.id);
783
+ if (children.length === 0) {
784
+ return "(?)";
785
+ }
786
+ return `(${this.renderExpression(children[0].id)})`;
787
+ }
788
+ const children = this.expressions.getChildExpressions(expression.id);
789
+ if (expression.operator === "not") {
790
+ if (children.length === 0) {
791
+ return `${this.operatorSymbol(expression.operator)} (?)`;
792
+ }
793
+ return `${this.operatorSymbol(expression.operator)}(${this.renderExpression(children[0].id)})`;
794
+ }
795
+ if (children.length === 0) {
796
+ return "(?)";
797
+ }
798
+ const renderedChildren = children.map((child) => this.renderExpression(child.id));
799
+ return `(${renderedChildren.join(` ${this.operatorSymbol(expression.operator)} `)})`;
800
+ }
801
+ operatorSymbol(operator) {
802
+ switch (operator) {
803
+ case "and":
804
+ return "∧";
805
+ case "or":
806
+ return "∨";
807
+ case "implies":
808
+ return "→";
809
+ case "iff":
810
+ return "↔";
811
+ case "not":
812
+ return "¬";
813
+ }
814
+ }
815
+ /** Returns a serializable snapshot of the premise's owned state. */
816
+ snapshot() {
817
+ const exprSnapshot = this.expressions.snapshot();
818
+ return {
819
+ premise: {
820
+ ...this.premise,
821
+ rootExpressionId: this.rootExpressionId,
822
+ },
823
+ expressions: exprSnapshot,
824
+ config: {
825
+ checksumConfig: this.checksumConfig,
826
+ positionConfig: exprSnapshot.config?.positionConfig,
827
+ },
828
+ };
829
+ }
830
+ /** Creates a new PremiseEngine from a previously captured snapshot. */
831
+ static fromSnapshot(snapshot, argument, variables) {
832
+ const pe = new PremiseEngine(snapshot.premise, { argument, variables }, snapshot.config);
833
+ // Restore expressions from the snapshot
834
+ pe.expressions = ExpressionManager.fromSnapshot(snapshot.expressions);
835
+ // Restore rootExpressionId from premise data
836
+ pe.rootExpressionId = snapshot.premise
837
+ .rootExpressionId;
838
+ // Rebuild the expressionsByVariableId index
839
+ pe.rebuildVariableIndex();
840
+ return pe;
841
+ }
842
+ rebuildVariableIndex() {
843
+ this.expressionsByVariableId = new DefaultMap(() => new Set());
844
+ for (const expr of this.expressions.toArray()) {
845
+ if (expr.type === "variable") {
846
+ this.expressionsByVariableId.get(expr.variableId).add(expr.id);
847
+ }
848
+ }
849
+ }
850
+ }
851
+ //# sourceMappingURL=PremiseEngine.js.map