yukigo 0.1.0 → 0.2.0

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 (144) hide show
  1. package/.mocharc.json +3 -3
  2. package/CHANGELOG.md +26 -0
  3. package/README.md +193 -199
  4. package/dist/analyzer/GraphBuilder.d.ts +29 -0
  5. package/dist/analyzer/GraphBuilder.js +99 -0
  6. package/dist/analyzer/index.d.ts +11 -23
  7. package/dist/analyzer/index.js +100 -58
  8. package/dist/analyzer/inspections/functional/functional.d.ts +44 -0
  9. package/dist/analyzer/inspections/functional/functional.js +149 -0
  10. package/dist/analyzer/inspections/functional/smells.d.ts +16 -0
  11. package/dist/analyzer/inspections/functional/smells.js +98 -0
  12. package/dist/analyzer/inspections/{generic.d.ts → generic/generic.d.ts} +70 -43
  13. package/dist/analyzer/inspections/generic/generic.js +604 -0
  14. package/dist/analyzer/inspections/generic/smells.d.ts +61 -0
  15. package/dist/analyzer/inspections/generic/smells.js +349 -0
  16. package/dist/analyzer/inspections/imperative/imperative.d.ts +35 -0
  17. package/dist/analyzer/inspections/imperative/imperative.js +109 -0
  18. package/dist/analyzer/inspections/imperative/smells.d.ts +16 -0
  19. package/dist/analyzer/inspections/imperative/smells.js +58 -0
  20. package/dist/analyzer/inspections/logic/logic.d.ts +32 -0
  21. package/dist/analyzer/inspections/logic/logic.js +96 -0
  22. package/dist/analyzer/inspections/logic/smells.d.ts +15 -0
  23. package/dist/analyzer/inspections/logic/smells.js +60 -0
  24. package/dist/analyzer/inspections/object/object.d.ts +88 -0
  25. package/dist/analyzer/inspections/object/object.js +319 -0
  26. package/dist/analyzer/inspections/object/smells.d.ts +30 -0
  27. package/dist/analyzer/inspections/object/smells.js +135 -0
  28. package/dist/analyzer/utils.d.ts +26 -4
  29. package/dist/analyzer/utils.js +71 -13
  30. package/dist/index.d.ts +1 -0
  31. package/dist/index.js +1 -0
  32. package/dist/interpreter/components/EnvBuilder.d.ts +9 -5
  33. package/dist/interpreter/components/EnvBuilder.js +100 -30
  34. package/dist/interpreter/components/Operations.d.ts +4 -4
  35. package/dist/interpreter/components/Operations.js +17 -2
  36. package/dist/interpreter/components/PatternMatcher.d.ts +47 -17
  37. package/dist/interpreter/components/PatternMatcher.js +264 -119
  38. package/dist/interpreter/components/RuntimeContext.d.ts +35 -0
  39. package/dist/interpreter/components/RuntimeContext.js +93 -0
  40. package/dist/interpreter/components/TestRunner.d.ts +18 -0
  41. package/dist/interpreter/components/TestRunner.js +103 -0
  42. package/dist/interpreter/components/Visitor.d.ts +63 -57
  43. package/dist/interpreter/components/Visitor.js +508 -173
  44. package/dist/interpreter/components/logic/LogicEngine.d.ts +29 -0
  45. package/dist/interpreter/components/logic/LogicEngine.js +259 -0
  46. package/dist/interpreter/components/logic/LogicResolver.d.ts +53 -0
  47. package/dist/interpreter/components/logic/LogicResolver.js +471 -0
  48. package/dist/interpreter/components/logic/LogicTranslator.d.ts +14 -0
  49. package/dist/interpreter/components/logic/LogicTranslator.js +99 -0
  50. package/dist/interpreter/components/runtimes/FunctionRuntime.d.ts +12 -0
  51. package/dist/interpreter/components/runtimes/FunctionRuntime.js +147 -0
  52. package/dist/interpreter/components/runtimes/LazyRuntime.d.ts +19 -0
  53. package/dist/interpreter/components/runtimes/LazyRuntime.js +269 -0
  54. package/dist/interpreter/components/runtimes/ObjectRuntime.d.ts +35 -0
  55. package/dist/interpreter/components/runtimes/ObjectRuntime.js +126 -0
  56. package/dist/interpreter/entities.d.ts +105 -0
  57. package/dist/interpreter/entities.js +96 -0
  58. package/dist/interpreter/errors.d.ts +1 -1
  59. package/dist/interpreter/index.d.ts +4 -12
  60. package/dist/interpreter/index.js +10 -13
  61. package/dist/interpreter/trampoline.d.ts +17 -0
  62. package/dist/interpreter/trampoline.js +38 -0
  63. package/dist/interpreter/utils.d.ts +4 -7
  64. package/dist/interpreter/utils.js +25 -17
  65. package/dist/tester/index.d.ts +25 -0
  66. package/dist/tester/index.js +108 -0
  67. package/dist/utils/helpers.d.ts +0 -4
  68. package/dist/utils/helpers.js +20 -24
  69. package/package.json +2 -2
  70. package/src/analyzer/GraphBuilder.ts +142 -0
  71. package/src/analyzer/index.ts +185 -132
  72. package/src/analyzer/inspections/functional/functional.ts +121 -0
  73. package/src/analyzer/inspections/functional/smells.ts +102 -0
  74. package/src/analyzer/inspections/{generic.ts → generic/generic.ts} +581 -499
  75. package/src/analyzer/inspections/generic/smells.ts +365 -0
  76. package/src/analyzer/inspections/imperative/imperative.ts +101 -0
  77. package/src/analyzer/inspections/imperative/smells.ts +54 -0
  78. package/src/analyzer/inspections/logic/logic.ts +90 -0
  79. package/src/analyzer/inspections/logic/smells.ts +54 -0
  80. package/src/analyzer/inspections/{object.ts → object/object.ts} +264 -282
  81. package/src/analyzer/inspections/object/smells.ts +144 -0
  82. package/src/analyzer/utils.ts +109 -26
  83. package/src/index.ts +3 -2
  84. package/src/interpreter/components/EnvBuilder.ts +202 -97
  85. package/src/interpreter/components/Operations.ts +99 -81
  86. package/src/interpreter/components/PatternMatcher.ts +475 -254
  87. package/src/interpreter/components/RuntimeContext.ts +119 -0
  88. package/src/interpreter/components/TestRunner.ts +151 -0
  89. package/src/interpreter/components/Visitor.ts +1065 -493
  90. package/src/interpreter/components/logic/LogicEngine.ts +519 -0
  91. package/src/interpreter/components/logic/LogicResolver.ts +858 -0
  92. package/src/interpreter/components/logic/LogicTranslator.ts +149 -0
  93. package/src/interpreter/components/runtimes/FunctionRuntime.ts +227 -0
  94. package/src/interpreter/components/runtimes/LazyRuntime.ts +334 -0
  95. package/src/interpreter/components/runtimes/ObjectRuntime.ts +224 -0
  96. package/src/interpreter/errors.ts +47 -47
  97. package/src/interpreter/index.ts +52 -59
  98. package/src/interpreter/trampoline.ts +71 -0
  99. package/src/interpreter/utils.ts +84 -79
  100. package/src/tester/index.ts +128 -0
  101. package/src/utils/helpers.ts +67 -73
  102. package/tests/analyzer/functional.spec.ts +207 -221
  103. package/tests/analyzer/generic.spec.ts +178 -100
  104. package/tests/analyzer/helpers.spec.ts +83 -83
  105. package/tests/analyzer/logic.spec.ts +237 -292
  106. package/tests/analyzer/oop.spec.ts +323 -338
  107. package/tests/analyzer/transitive.spec.ts +166 -0
  108. package/tests/interpreter/EnvBuilder.spec.ts +183 -178
  109. package/tests/interpreter/FunctionRuntime.spec.ts +223 -234
  110. package/tests/interpreter/LazyRuntime.spec.ts +225 -190
  111. package/tests/interpreter/LogicEngine.spec.ts +327 -194
  112. package/tests/interpreter/LogicSubstitution.spec.ts +80 -0
  113. package/tests/interpreter/ObjectRuntime.spec.ts +606 -0
  114. package/tests/interpreter/Operations.spec.ts +220 -220
  115. package/tests/interpreter/PatternSystem.spec.ts +213 -189
  116. package/tests/interpreter/Tests.spec.ts +122 -0
  117. package/tests/interpreter/interpreter.spec.ts +991 -937
  118. package/tests/tester/Tester.spec.ts +153 -0
  119. package/tsconfig.build.json +15 -7
  120. package/tsconfig.json +25 -17
  121. package/dist/analyzer/inspections/functional.d.ts +0 -46
  122. package/dist/analyzer/inspections/functional.js +0 -123
  123. package/dist/analyzer/inspections/generic.js +0 -427
  124. package/dist/analyzer/inspections/imperative.d.ts +0 -37
  125. package/dist/analyzer/inspections/imperative.js +0 -105
  126. package/dist/analyzer/inspections/logic.d.ts +0 -49
  127. package/dist/analyzer/inspections/logic.js +0 -140
  128. package/dist/analyzer/inspections/object.d.ts +0 -83
  129. package/dist/analyzer/inspections/object.js +0 -235
  130. package/dist/interpreter/components/FunctionRuntime.d.ts +0 -8
  131. package/dist/interpreter/components/FunctionRuntime.js +0 -52
  132. package/dist/interpreter/components/LazyRuntime.d.ts +0 -7
  133. package/dist/interpreter/components/LazyRuntime.js +0 -75
  134. package/dist/interpreter/components/LogicEngine.d.ts +0 -21
  135. package/dist/interpreter/components/LogicEngine.js +0 -152
  136. package/dist/interpreter/components/LogicResolver.d.ts +0 -11
  137. package/dist/interpreter/components/LogicResolver.js +0 -87
  138. package/src/analyzer/inspections/functional.ts +0 -159
  139. package/src/analyzer/inspections/imperative.ts +0 -129
  140. package/src/analyzer/inspections/logic.ts +0 -166
  141. package/src/interpreter/components/FunctionRuntime.ts +0 -79
  142. package/src/interpreter/components/LazyRuntime.ts +0 -97
  143. package/src/interpreter/components/LogicEngine.ts +0 -227
  144. package/src/interpreter/components/LogicResolver.ts +0 -130
@@ -0,0 +1,519 @@
1
+ import {
2
+ Exist,
3
+ Expression,
4
+ Findall,
5
+ Forall,
6
+ Goal,
7
+ Pattern,
8
+ PrimitiveValue,
9
+ Query,
10
+ EnvStack,
11
+ LogicResult,
12
+ Statement,
13
+ Not,
14
+ LogicConstraint,
15
+ Sequence,
16
+ SymbolPrimitive,
17
+ VariablePattern,
18
+ } from "yukigo-ast";
19
+ import {
20
+ solveFindallCPS,
21
+ solveGoalCPS,
22
+ Substitution,
23
+ unify,
24
+ instantiate,
25
+ SuccessCont,
26
+ FailureCont,
27
+ } from "./LogicResolver.js";
28
+ import { createStream, ExpressionEvaluator } from "../../utils.js";
29
+ import { InterpreterVisitor } from "../Visitor.js";
30
+ import { LogicTranslator } from "./LogicTranslator.js";
31
+ import { trampoline, Continuation, Thunk } from "../../trampoline.js";
32
+ import { RuntimeContext, LogicSearchMode } from "../RuntimeContext.js";
33
+ import { inspect } from "util";
34
+ import { PatternResolver } from "../PatternMatcher.js";
35
+
36
+ export type LogicExecutable = Expression | Statement | Goal | Exist | Findall;
37
+
38
+ export class LogicEngine {
39
+ private translator: LogicTranslator;
40
+
41
+ constructor(
42
+ evaluator: ExpressionEvaluator,
43
+ private context: RuntimeContext,
44
+ ) {
45
+ this.translator = new LogicTranslator(evaluator, this.context);
46
+ }
47
+
48
+ public unifyExpr(
49
+ left: Expression,
50
+ right: Expression,
51
+ k: Continuation<PrimitiveValue>,
52
+ ): Thunk<PrimitiveValue> {
53
+ return this.translator.instantiateExpressionAsPattern(
54
+ left,
55
+ new Map(),
56
+ (p1) => {
57
+ return () =>
58
+ this.translator.instantiateExpressionAsPattern(
59
+ right,
60
+ new Map(),
61
+ (p2) => {
62
+ return k(unify(p1, p2, new Map()) !== null);
63
+ },
64
+ );
65
+ },
66
+ );
67
+ }
68
+
69
+ public solveQuery(
70
+ node: Query,
71
+ k: Continuation<PrimitiveValue>,
72
+ modeOverride?: LogicSearchMode,
73
+ ): Thunk<PrimitiveValue> {
74
+ const mode = modeOverride || this.context.config.outputMode || "first";
75
+ if (mode === "all") {
76
+ return this.collectAllResults(node.expressions, new Map(), (results) =>
77
+ k(results.map((s) => this.formatLogicResult(s))),
78
+ );
79
+ } else if (mode === "stream") {
80
+ return k(this.createLazyStream(node.expressions, new Map()));
81
+ }
82
+ // "first" mode
83
+ return this.solveConjunction(
84
+ node.expressions,
85
+ new Map(),
86
+ (s) => k(this.formatLogicResult(s)),
87
+ () => k(false),
88
+ );
89
+ }
90
+
91
+ public solveGoal(
92
+ node: Goal,
93
+ k: Continuation<PrimitiveValue>,
94
+ modeOverride?: LogicSearchMode,
95
+ ): Thunk<PrimitiveValue> {
96
+ return this.prepareLogicTargetCPS(node, new Map(), ({ id, patterns }) => {
97
+ // Goals evaluated as expressions should usually default to "first"
98
+ // to avoid combinatorial explosion when they are part of a sequence.
99
+ const mode = modeOverride || this.context.config.outputMode || "first";
100
+ if (mode === "all") {
101
+ return this.collectAllResultsForGoal(
102
+ id,
103
+ patterns,
104
+ new Map(),
105
+ (results) => k(results.map((s) => this.formatLogicResult(s))),
106
+ );
107
+ } else if (mode === "stream") {
108
+ return k(this.createLazyStreamForGoal(id, patterns, new Map()));
109
+ }
110
+ return solveGoalCPS(
111
+ this.context,
112
+ id,
113
+ patterns,
114
+ (body, s, onSucc, onFail) =>
115
+ this.solveConjunction(body, s, onSucc, onFail),
116
+ new Map(),
117
+ (s) => k(this.formatLogicResult(s)),
118
+ () => k(false),
119
+ );
120
+ });
121
+ }
122
+
123
+ public solveNot(
124
+ node: Not,
125
+ k: Continuation<PrimitiveValue>,
126
+ ): Thunk<PrimitiveValue> {
127
+ return () =>
128
+ this.solveConjunction(
129
+ [node.expression],
130
+ new Map(),
131
+ () => k(false),
132
+ () => k(true),
133
+ );
134
+ }
135
+
136
+ public solveFindall(
137
+ node: Findall,
138
+ k: Continuation<PrimitiveValue>,
139
+ ): Thunk<PrimitiveValue> {
140
+ return solveFindallCPS(
141
+ node,
142
+ new Map(),
143
+ (body, substs, onSuccess, onFailure) =>
144
+ this.solveConjunction(body, substs, onSuccess, onFailure),
145
+ (finalSubsts) => {
146
+ const pat = finalSubsts.get((node.bag as any).name.value);
147
+ const val = this.translator.patternToPrimitive(pat!);
148
+ return k(val!);
149
+ },
150
+ () => k([]),
151
+ );
152
+ }
153
+
154
+ public solveForall(
155
+ node: Forall,
156
+ k: Continuation<PrimitiveValue>,
157
+ ): Thunk<any> {
158
+ return this.solveConjunction(
159
+ [node.condition],
160
+ new Map(),
161
+ (condSubsts, nextCond) => {
162
+ return this.solveConjunction(
163
+ [node.action],
164
+ condSubsts,
165
+ () => nextCond,
166
+ () => k(false),
167
+ );
168
+ },
169
+ () => k(true),
170
+ );
171
+ }
172
+
173
+ public solveExist(
174
+ node: Exist,
175
+ k: Continuation<PrimitiveValue>,
176
+ modeOverride?: LogicSearchMode,
177
+ ): Thunk<PrimitiveValue> {
178
+ return this.prepareLogicTargetCPS(node, new Map(), ({ id, patterns }) => {
179
+ // Exists evaluated as expressions should usually default to "first"
180
+ const mode = modeOverride || this.context.config.outputMode || "first";
181
+ if (mode === "all") {
182
+ return this.collectAllResultsForGoal(
183
+ id,
184
+ patterns,
185
+ new Map(),
186
+ (results) => k(results.map((s) => this.formatLogicResult(s))),
187
+ );
188
+ } else if (mode === "stream") {
189
+ return k(this.createLazyStreamForGoal(id, patterns, new Map()));
190
+ }
191
+ return solveGoalCPS(
192
+ this.context,
193
+ id,
194
+ patterns,
195
+ (body, s, onSucc, onFail) =>
196
+ this.solveConjunction(body, s, onSucc, onFail),
197
+ new Map(),
198
+ (s) => k(this.formatLogicResult(s)),
199
+ () => k(false),
200
+ );
201
+ });
202
+ }
203
+
204
+ private solveConjunction(
205
+ nodes: LogicExecutable[],
206
+ substs: Substitution,
207
+ onSuccess: SuccessCont,
208
+ onFailure: FailureCont,
209
+ ): Thunk<any> {
210
+ if (this.context.config.debug)
211
+ console.log(`[LogicEngine] Solving conjunction.`);
212
+ if (nodes.length === 0) {
213
+ if (this.context.config.debug)
214
+ console.log(`[LogicEngine] Nothing else to solve. Success.`);
215
+ return onSuccess(substs, onFailure);
216
+ }
217
+
218
+ const [head, ...tail] = nodes;
219
+
220
+ return () =>
221
+ this.solveSingle(
222
+ head,
223
+ substs,
224
+ (newSubsts, next) => {
225
+ return this.solveConjunction(tail, newSubsts, onSuccess, next);
226
+ },
227
+ onFailure,
228
+ );
229
+ }
230
+
231
+ private solveSingle(
232
+ node: LogicExecutable,
233
+ substs: Substitution,
234
+ onSuccess: SuccessCont,
235
+ onFailure: FailureCont,
236
+ ): Thunk<any> {
237
+ if (this.isLogicGoal(node))
238
+ return () => this.solveLogicGoal(node, substs, onSuccess, onFailure);
239
+ return () => this.solveCondition(node, substs, onSuccess, onFailure);
240
+ }
241
+
242
+ private solveLogicGoal(
243
+ goal: Goal | Exist | Findall | LogicConstraint | Sequence,
244
+ substs: Substitution,
245
+ onSuccess: SuccessCont,
246
+ onFailure: FailureCont,
247
+ ): Thunk<any> {
248
+ if (goal instanceof LogicConstraint) {
249
+ return () =>
250
+ this.solveConjunction([goal.expression], substs, onSuccess, onFailure);
251
+ }
252
+
253
+ if (goal instanceof Sequence) {
254
+ return () =>
255
+ this.solveConjunction(goal.statements, substs, onSuccess, onFailure);
256
+ }
257
+
258
+ if (goal instanceof Findall) {
259
+ return solveFindallCPS(
260
+ goal,
261
+ substs,
262
+ (body, s, onSucc, onFail) =>
263
+ this.solveConjunction(body, s, onSucc, onFail),
264
+ onSuccess,
265
+ onFailure,
266
+ );
267
+ }
268
+ if (this.context.config.debug) {
269
+ console.log(
270
+ `[LogicEngine] Solving logic goal: ${goal.identifier.value}.`,
271
+ );
272
+ }
273
+
274
+ return this.prepareLogicTargetCPS(goal, substs, ({ id, patterns }) => {
275
+ return solveGoalCPS(
276
+ this.context,
277
+ id,
278
+ patterns,
279
+ (body, s, onSucc, onFail) =>
280
+ this.solveConjunction(body, s, onSucc, onFail),
281
+ substs,
282
+ onSuccess,
283
+ onFailure,
284
+ );
285
+ });
286
+ }
287
+
288
+ private solveCondition(
289
+ expr: Expression | Statement,
290
+ substs: Substitution,
291
+ onSuccess: SuccessCont,
292
+ onFailure: FailureCont,
293
+ ): Thunk<any> {
294
+ this.createLocalEnv(substs);
295
+ const localEvaluator = new InterpreterVisitor(this.context);
296
+
297
+ return localEvaluator.evaluate(expr, (result) => {
298
+ if (result !== undefined && result !== false) {
299
+ let currentSubsts = new Map(substs);
300
+ for (const [name, val] of this.context.env.head) {
301
+ const pat = this.translator.primitiveToPattern(val);
302
+ const unified = unify(
303
+ new VariablePattern(new SymbolPrimitive(name)),
304
+ pat,
305
+ currentSubsts,
306
+ );
307
+ if (unified) {
308
+ currentSubsts = unified;
309
+ } else {
310
+ return onFailure();
311
+ }
312
+ }
313
+ return onSuccess(currentSubsts, onFailure);
314
+ }
315
+ return onFailure();
316
+ });
317
+ }
318
+
319
+ private isLogicGoal(
320
+ node: LogicExecutable,
321
+ ): node is Goal | Exist | Findall | LogicConstraint | Sequence {
322
+ return (
323
+ node instanceof Goal ||
324
+ node instanceof Exist ||
325
+ node instanceof Findall ||
326
+ node instanceof LogicConstraint ||
327
+ node instanceof Sequence
328
+ );
329
+ }
330
+
331
+ private createLocalEnv(substs: Substitution) {
332
+ this.context.pushEnv(new Map());
333
+ for (const [name, pattern] of substs) {
334
+ const resolvedPattern = instantiate(pattern, substs);
335
+ const value = this.translator.patternToPrimitive(resolvedPattern);
336
+
337
+ this.context.define(name, value);
338
+
339
+ // map base name if it was standardized apart ("X_1" -> "X")
340
+ const baseNameMatch = name.match(/^(.*)_\d+$/);
341
+ if (baseNameMatch) {
342
+ const baseName = baseNameMatch[1];
343
+ if (!this.context.env.head.has(baseName)) {
344
+ this.context.define(baseName, value);
345
+ }
346
+ }
347
+ }
348
+ }
349
+
350
+ private prepareLogicTargetCPS(
351
+ node: Goal | Exist,
352
+ substs: Substitution,
353
+ k: (res: { id: string; patterns: Pattern[] }) => Thunk<any>,
354
+ ): Thunk<any> {
355
+ const id = node.identifier.value;
356
+ if (node instanceof Goal) {
357
+ const patterns: Pattern[] = [];
358
+ const next = (index: number): Thunk<any> => {
359
+ if (index >= node.args.length) return k({ id, patterns });
360
+ return this.translator.instantiateExpressionAsPattern(
361
+ node.args[index],
362
+ substs,
363
+ (p) => {
364
+ patterns.push(p);
365
+ return () => next(index + 1);
366
+ },
367
+ );
368
+ };
369
+ return () => next(0);
370
+ } else {
371
+ const patterns = node.patterns.map((pat) => instantiate(pat, substs));
372
+ return k({ id, patterns });
373
+ }
374
+ }
375
+
376
+ private collectAllResults(
377
+ nodes: LogicExecutable[],
378
+ substs: Substitution,
379
+ k: (results: Substitution[]) => Thunk<any>,
380
+ ): Thunk<any> {
381
+ const results: Substitution[] = [];
382
+ return this.solveConjunction(
383
+ nodes,
384
+ substs,
385
+ (s, next) => {
386
+ if (this.context.config.debug)
387
+ console.log(
388
+ `[LogicEngine] Pushing { ${Array.from(s).map(([k, pat]) => `${k} -> ${pat.accept(new PatternResolver())}`)} } to results`,
389
+ );
390
+ results.push(s);
391
+ if (this.context.config.debug)
392
+ console.log(
393
+ `[LogicEngine] Collected results: ${results.map((s) => `{ ${Array.from(s).map(([k, pat]) => `${k} -> ${pat.accept(new PatternResolver())}`)} }`)}`,
394
+ );
395
+ return next;
396
+ },
397
+ () => k(results),
398
+ );
399
+ }
400
+
401
+ private collectAllResultsForGoal(
402
+ id: string,
403
+ patterns: Pattern[],
404
+ substs: Substitution,
405
+ k: (results: Substitution[]) => Thunk<any>,
406
+ ): Thunk<any> {
407
+ const results: Substitution[] = [];
408
+ return solveGoalCPS(
409
+ this.context,
410
+ id,
411
+ patterns,
412
+ (body, s, onSucc, onFail) =>
413
+ this.solveConjunction(body, s, onSucc, onFail),
414
+ substs,
415
+ (s, next) => {
416
+ results.push(s);
417
+ return next;
418
+ },
419
+ () => k(results),
420
+ );
421
+ }
422
+
423
+ private createLazyStream(
424
+ nodes: LogicExecutable[],
425
+ substs: Substitution,
426
+ ): any {
427
+ let currentSubst: Substitution | null = null;
428
+ let currentNext: (() => Thunk<any>) | null = null;
429
+
430
+ let thunk = this.solveConjunction(
431
+ nodes,
432
+ substs,
433
+ (s, next) => {
434
+ currentSubst = s;
435
+ currentNext = next;
436
+ return null;
437
+ },
438
+ () => {
439
+ currentSubst = null;
440
+ return null;
441
+ },
442
+ );
443
+
444
+ return createStream(() => {
445
+ return {
446
+ next: () => {
447
+ trampoline(thunk);
448
+ if (currentSubst) {
449
+ const res = this.formatLogicResult(currentSubst);
450
+ thunk = currentNext!();
451
+ return { value: res, done: false };
452
+ }
453
+ return { value: undefined, done: true };
454
+ },
455
+ } as any;
456
+ });
457
+ }
458
+
459
+ private createLazyStreamForGoal(
460
+ id: string,
461
+ patterns: Pattern[],
462
+ substs: Substitution,
463
+ ): any {
464
+ let currentSubst: Substitution | null = null;
465
+ let currentNext: (() => Thunk<any>) | null = null;
466
+
467
+ let thunk = solveGoalCPS(
468
+ this.context,
469
+ id,
470
+ patterns,
471
+ (body, s, onSucc, onFail) =>
472
+ this.solveConjunction(body, s, onSucc, onFail),
473
+ substs,
474
+ (s, next) => {
475
+ currentSubst = s;
476
+ currentNext = next;
477
+ return null;
478
+ },
479
+ () => {
480
+ currentSubst = null;
481
+ return null;
482
+ },
483
+ );
484
+
485
+ return createStream(() => {
486
+ return {
487
+ next: () => {
488
+ trampoline(thunk);
489
+ if (currentSubst) {
490
+ const res = this.formatLogicResult(currentSubst);
491
+ thunk = currentNext!();
492
+ return { value: res, done: false };
493
+ }
494
+ return { value: undefined, done: true };
495
+ },
496
+ } as any;
497
+ });
498
+ }
499
+
500
+ private formatLogicResult(substs: Substitution): LogicResult {
501
+ const solutions = new Map<string, PrimitiveValue>();
502
+ substs.forEach((pattern, key) => {
503
+ const val = this.translator.patternToPrimitive(pattern);
504
+ if (val !== undefined) {
505
+ solutions.set(key, val);
506
+
507
+ // Also map base name if it was standardized apart (e.g., "X_1" -> "X")
508
+ const baseNameMatch = key.match(/^(.*)_\d+$/);
509
+ if (baseNameMatch) {
510
+ const baseName = baseNameMatch[1];
511
+ if (!solutions.has(baseName)) {
512
+ solutions.set(baseName, val);
513
+ }
514
+ }
515
+ }
516
+ });
517
+ return { success: true, solutions };
518
+ }
519
+ }