yukigo 0.2.0 → 0.2.2

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 (73) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/dist/analyzer/index.d.ts +7 -0
  3. package/dist/src/analyzer/GraphBuilder.d.ts +30 -0
  4. package/dist/src/analyzer/GraphBuilder.js +100 -0
  5. package/dist/src/analyzer/index.d.ts +59 -0
  6. package/dist/src/analyzer/index.js +152 -0
  7. package/dist/src/analyzer/inspections/functional/functional.d.ts +44 -0
  8. package/dist/src/analyzer/inspections/functional/functional.js +149 -0
  9. package/dist/src/analyzer/inspections/functional/smells.d.ts +16 -0
  10. package/dist/src/analyzer/inspections/functional/smells.js +98 -0
  11. package/dist/src/analyzer/inspections/generic/generic.d.ts +178 -0
  12. package/dist/src/analyzer/inspections/generic/generic.js +604 -0
  13. package/dist/src/analyzer/inspections/generic/smells.d.ts +61 -0
  14. package/dist/src/analyzer/inspections/generic/smells.js +349 -0
  15. package/dist/src/analyzer/inspections/imperative/imperative.d.ts +35 -0
  16. package/dist/src/analyzer/inspections/imperative/imperative.js +109 -0
  17. package/dist/src/analyzer/inspections/imperative/smells.d.ts +16 -0
  18. package/dist/src/analyzer/inspections/imperative/smells.js +58 -0
  19. package/dist/src/analyzer/inspections/logic/logic.d.ts +32 -0
  20. package/dist/src/analyzer/inspections/logic/logic.js +96 -0
  21. package/dist/src/analyzer/inspections/logic/smells.d.ts +15 -0
  22. package/dist/src/analyzer/inspections/logic/smells.js +60 -0
  23. package/dist/src/analyzer/inspections/object/object.d.ts +90 -0
  24. package/dist/src/analyzer/inspections/object/object.js +321 -0
  25. package/dist/src/analyzer/inspections/object/smells.d.ts +30 -0
  26. package/dist/src/analyzer/inspections/object/smells.js +135 -0
  27. package/dist/src/analyzer/utils.d.ts +30 -0
  28. package/dist/src/analyzer/utils.js +78 -0
  29. package/dist/src/index.d.ts +4 -0
  30. package/dist/src/index.js +4 -0
  31. package/dist/src/interpreter/components/EnvBuilder.d.ts +21 -0
  32. package/dist/src/interpreter/components/EnvBuilder.js +155 -0
  33. package/dist/src/interpreter/components/Operations.d.ts +14 -0
  34. package/dist/src/interpreter/components/Operations.js +84 -0
  35. package/dist/src/interpreter/components/PatternMatcher.d.ts +73 -0
  36. package/dist/src/interpreter/components/PatternMatcher.js +358 -0
  37. package/dist/src/interpreter/components/RuntimeContext.d.ts +35 -0
  38. package/dist/src/interpreter/components/RuntimeContext.js +93 -0
  39. package/dist/src/interpreter/components/TestRunner.d.ts +19 -0
  40. package/dist/src/interpreter/components/TestRunner.js +110 -0
  41. package/dist/src/interpreter/components/Visitor.d.ts +71 -0
  42. package/dist/src/interpreter/components/Visitor.js +638 -0
  43. package/dist/src/interpreter/components/logic/LogicEngine.d.ts +29 -0
  44. package/dist/src/interpreter/components/logic/LogicEngine.js +259 -0
  45. package/dist/src/interpreter/components/logic/LogicResolver.d.ts +53 -0
  46. package/dist/src/interpreter/components/logic/LogicResolver.js +484 -0
  47. package/dist/src/interpreter/components/logic/LogicTranslator.d.ts +14 -0
  48. package/dist/src/interpreter/components/logic/LogicTranslator.js +99 -0
  49. package/dist/src/interpreter/components/runtimes/FunctionRuntime.d.ts +12 -0
  50. package/dist/src/interpreter/components/runtimes/FunctionRuntime.js +149 -0
  51. package/dist/src/interpreter/components/runtimes/LazyRuntime.d.ts +19 -0
  52. package/dist/src/interpreter/components/runtimes/LazyRuntime.js +269 -0
  53. package/dist/src/interpreter/components/runtimes/ObjectRuntime.d.ts +35 -0
  54. package/dist/src/interpreter/components/runtimes/ObjectRuntime.js +126 -0
  55. package/dist/src/interpreter/errors.d.ts +19 -0
  56. package/dist/src/interpreter/errors.js +36 -0
  57. package/dist/src/interpreter/index.d.ts +24 -0
  58. package/dist/src/interpreter/index.js +41 -0
  59. package/dist/src/interpreter/trampoline.d.ts +17 -0
  60. package/dist/src/interpreter/trampoline.js +38 -0
  61. package/dist/src/interpreter/utils.d.ts +11 -0
  62. package/dist/src/interpreter/utils.js +65 -0
  63. package/dist/src/tester/index.d.ts +25 -0
  64. package/dist/src/tester/index.js +113 -0
  65. package/dist/src/utils/helpers.d.ts +13 -0
  66. package/dist/src/utils/helpers.js +52 -0
  67. package/dist/utils/helpers.d.ts +5 -1
  68. package/dist/utils/helpers.js +79 -6
  69. package/package.json +4 -2
  70. package/src/analyzer/index.ts +9 -0
  71. package/src/utils/helpers.ts +111 -10
  72. package/tests/analyzer/helpers.spec.ts +14 -8
  73. package/tests/tester/Tester.spec.ts +0 -1
@@ -0,0 +1,484 @@
1
+ import { Fact, FunctorPattern, ListPattern, LiteralPattern, Rule, VariablePattern, WildcardPattern, isRuntimePredicate, UnguardedBody, Findall, ConsPattern, Equation, SymbolPrimitive, Goal, Exist, LogicConstraint, Sequence, Not, ComparisonOperation, UnifyOperation, ApplicationPattern, TuplePattern, ConstructorPattern, UnionPattern, AsPattern, TypePattern, AssignOperation, ArithmeticBinaryOperation, ArithmeticUnaryOperation, ConsExpression, ListPrimitive, TupleExpression, If, Forall, Call, ListBinaryOperation, ListUnaryOperation, LogicalBinaryOperation, LogicalUnaryOperation, BitwiseBinaryOperation, BitwiseUnaryOperation, StringOperation, isUnguardedBody, } from "yukigo-ast";
2
+ import { UnexpectedNode } from "../../../utils/helpers.js";
3
+ /**
4
+ * Creates a successful logic result.
5
+ */
6
+ export function success(substs) {
7
+ return { success: true, substs };
8
+ }
9
+ /**
10
+ * Unifies two patterns given an existing set of substitutions.
11
+ * Returns the updated substitution map or null if unification fails.
12
+ */
13
+ export function unify(t1, t2, argEnv) {
14
+ const env = argEnv ? new Map(argEnv) : new Map();
15
+ return unifyInPlace(t1, t2, env) ? env : null;
16
+ }
17
+ /**
18
+ * Internal unification that modifies the environment in place for efficiency during recursion.
19
+ */
20
+ function unifyInPlace(t1, t2, env) {
21
+ const r1 = resolve(t1, env);
22
+ const r2 = resolve(t2, env);
23
+ if (r1 === r2)
24
+ return true;
25
+ if (r1 instanceof WildcardPattern || r2 instanceof WildcardPattern) {
26
+ return true;
27
+ }
28
+ if (r1 instanceof VariablePattern) {
29
+ env.set(r1.name.value, r2);
30
+ return true;
31
+ }
32
+ if (r2 instanceof VariablePattern) {
33
+ env.set(r2.name.value, r1);
34
+ return true;
35
+ }
36
+ if (r1 instanceof LiteralPattern && r2 instanceof LiteralPattern) {
37
+ return r1.name.equals(r2.name);
38
+ }
39
+ if (r1 instanceof FunctorPattern && r2 instanceof FunctorPattern) {
40
+ if (r1.identifier.value !== r2.identifier.value)
41
+ return false;
42
+ if (r1.args.length !== r2.args.length)
43
+ return false;
44
+ for (let i = 0; i < r1.args.length; i++) {
45
+ if (!unifyInPlace(r1.args[i], r2.args[i], env))
46
+ return false;
47
+ }
48
+ return true;
49
+ }
50
+ if (r1 instanceof ListPattern && r2 instanceof ListPattern) {
51
+ if (r1.elements.length !== r2.elements.length)
52
+ return false;
53
+ for (let i = 0; i < r1.elements.length; i++) {
54
+ if (!unifyInPlace(r1.elements[i], r2.elements[i], env))
55
+ return false;
56
+ }
57
+ return true;
58
+ }
59
+ if (r1 instanceof ConsPattern && r2 instanceof ConsPattern) {
60
+ let curr1 = r1;
61
+ let curr2 = r2;
62
+ while (curr1 instanceof ConsPattern && curr2 instanceof ConsPattern) {
63
+ if (!unifyInPlace(curr1.left, curr2.left, env))
64
+ return false;
65
+ curr1 = resolve(curr1.right, env);
66
+ curr2 = resolve(curr2.right, env);
67
+ }
68
+ return unifyInPlace(curr1, curr2, env);
69
+ }
70
+ if (r1 instanceof ConsPattern && r2 instanceof ListPattern) {
71
+ if (r2.elements.length === 0)
72
+ return false;
73
+ const [head, ...tail] = r2.elements;
74
+ return (unifyInPlace(r1.left, head, env) &&
75
+ unifyInPlace(r1.right, new ListPattern(tail), env));
76
+ }
77
+ if (r1 instanceof ListPattern && r2 instanceof ConsPattern) {
78
+ if (r1.elements.length === 0)
79
+ return false;
80
+ const [head, ...tail] = r1.elements;
81
+ return (unifyInPlace(head, r2.left, env) &&
82
+ unifyInPlace(new ListPattern(tail), r2.right, env));
83
+ }
84
+ return false;
85
+ }
86
+ /**
87
+ * Follows variable bindings in the substitution map until a non-variable or unbound variable is found.
88
+ */
89
+ export function resolve(node, env) {
90
+ let current = node;
91
+ const seen = new Set();
92
+ while (current instanceof VariablePattern) {
93
+ const name = current.name.value;
94
+ if (seen.has(name))
95
+ break;
96
+ seen.add(name);
97
+ const bound = env.get(name);
98
+ if (!bound)
99
+ break;
100
+ current = bound;
101
+ }
102
+ return current;
103
+ }
104
+ class Instantiator {
105
+ substs;
106
+ seen;
107
+ constructor(substs, seen = new Set()) {
108
+ this.substs = substs;
109
+ this.seen = seen;
110
+ }
111
+ visitVariablePattern(node) {
112
+ const name = node.name.value;
113
+ if (this.seen.has(name))
114
+ return node;
115
+ const val = this.substs.get(name);
116
+ if (val) {
117
+ const nextSeen = new Set(this.seen);
118
+ nextSeen.add(name);
119
+ return new Instantiator(this.substs, nextSeen).instantiate(val);
120
+ }
121
+ return node;
122
+ }
123
+ visitLiteralPattern(node) {
124
+ return node;
125
+ }
126
+ visitApplicationPattern(node) {
127
+ return new ApplicationPattern(node.identifier, node.args.map((arg) => this.instantiate(arg)), node.loc);
128
+ }
129
+ visitTuplePattern(node) {
130
+ return new TuplePattern(node.elements.map((el) => this.instantiate(el)), node.loc);
131
+ }
132
+ visitListPattern(node) {
133
+ return new ListPattern(node.elements.map((el) => this.instantiate(el)), node.loc);
134
+ }
135
+ visitFunctorPattern(node) {
136
+ return new FunctorPattern(node.identifier, node.args.map((arg) => this.instantiate(arg)), node.loc);
137
+ }
138
+ visitAsPattern(node) {
139
+ return new AsPattern(this.instantiate(node.left), this.instantiate(node.right), node.loc);
140
+ }
141
+ visitWildcardPattern(node) {
142
+ return node;
143
+ }
144
+ visitUnionPattern(node) {
145
+ return new UnionPattern(node.elements.map((el) => this.instantiate(el)), node.loc);
146
+ }
147
+ visitConstructorPattern(node) {
148
+ return new ConstructorPattern(node.identifier, node.args.map((arg) => this.instantiate(arg)), node.loc);
149
+ }
150
+ visitConsPattern(node) {
151
+ return new ConsPattern(this.instantiate(node.left), this.instantiate(node.right), node.loc);
152
+ }
153
+ visitTypePattern(node) {
154
+ return new TypePattern(node.targetType, node.innerPattern ? this.instantiate(node.innerPattern) : undefined, node.loc);
155
+ }
156
+ fallback(node) {
157
+ throw new UnexpectedNode(node.constructor.name, "Instantiator");
158
+ }
159
+ instantiate(pattern) {
160
+ return pattern.accept(this);
161
+ }
162
+ }
163
+ /**
164
+ * Fully instantiates a pattern by replacing all bound variables with their values.
165
+ */
166
+ export function instantiate(pattern, substs, seen = new Set()) {
167
+ return new Instantiator(substs, seen).instantiate(pattern);
168
+ }
169
+ let variableCounter = 0;
170
+ class LogicVariableRenamer {
171
+ renames;
172
+ freshId;
173
+ constructor(renames, freshId) {
174
+ this.renames = renames;
175
+ this.freshId = freshId;
176
+ }
177
+ rename(node) {
178
+ if (!node || typeof node !== "object")
179
+ return node;
180
+ if (typeof node.accept === "function") {
181
+ return node.accept(this);
182
+ }
183
+ return node;
184
+ }
185
+ visitFact(node) {
186
+ return new Fact(node.identifier, node.patterns.map((p) => this.rename(p)), node.loc);
187
+ }
188
+ visitRule(node) {
189
+ const renamedEquations = node.equations.map((eq) => {
190
+ const body = eq.body;
191
+ if (!isUnguardedBody(body))
192
+ throw new Error("GuardedBody renaming not implemented");
193
+ return new Equation(eq.patterns.map((p) => this.rename(p)), body.accept(this), eq.returnExpr, eq.loc);
194
+ });
195
+ return new Rule(node.identifier, renamedEquations, node.loc);
196
+ }
197
+ visitUnguardedBody(node) {
198
+ return new UnguardedBody(new Sequence(node.sequence.statements.map((stmt) => this.rename(stmt)), node.sequence.loc), node.loc);
199
+ }
200
+ // PatternVisitor
201
+ visitVariablePattern(node) {
202
+ const name = node.name.value;
203
+ let newName = this.renames.get(name);
204
+ if (!newName) {
205
+ newName = `${name}_${this.freshId}`;
206
+ this.renames.set(name, newName);
207
+ }
208
+ return new VariablePattern(new SymbolPrimitive(newName), node.loc);
209
+ }
210
+ visitLiteralPattern(node) {
211
+ return node;
212
+ }
213
+ visitApplicationPattern(node) {
214
+ return new ApplicationPattern(node.identifier, node.args.map((arg) => this.rename(arg)), node.loc);
215
+ }
216
+ visitTuplePattern(node) {
217
+ return new TuplePattern(node.elements.map((el) => this.rename(el)), node.loc);
218
+ }
219
+ visitListPattern(node) {
220
+ return new ListPattern(node.elements.map((el) => this.rename(el)), node.loc);
221
+ }
222
+ visitFunctorPattern(node) {
223
+ return new FunctorPattern(node.identifier, node.args.map((arg) => this.rename(arg)), node.loc);
224
+ }
225
+ visitAsPattern(node) {
226
+ return new AsPattern(this.rename(node.left), this.rename(node.right), node.loc);
227
+ }
228
+ visitWildcardPattern(node) {
229
+ return node;
230
+ }
231
+ visitUnionPattern(node) {
232
+ return new UnionPattern(node.elements.map((el) => this.rename(el)), node.loc);
233
+ }
234
+ visitConstructorPattern(node) {
235
+ return new ConstructorPattern(node.identifier, node.args.map((arg) => this.rename(arg)), node.loc);
236
+ }
237
+ visitConsPattern(node) {
238
+ return new ConsPattern(this.rename(node.left), this.rename(node.right), node.loc);
239
+ }
240
+ visitTypePattern(node) {
241
+ return new TypePattern(node.targetType, node.innerPattern ? this.rename(node.innerPattern) : undefined, node.loc);
242
+ }
243
+ // Expression/Statement Visitor
244
+ visitSymbolPrimitive(node) {
245
+ const name = node.value;
246
+ if (/^[A-Z_]/.test(name) && name !== "_") {
247
+ let newName = this.renames.get(name);
248
+ if (!newName) {
249
+ newName = `${name}_${this.freshId}`;
250
+ this.renames.set(name, newName);
251
+ }
252
+ return new SymbolPrimitive(newName, node.loc);
253
+ }
254
+ return node;
255
+ }
256
+ visitNumberPrimitive(node) {
257
+ return node;
258
+ }
259
+ visitStringPrimitive(node) {
260
+ return node;
261
+ }
262
+ visitBooleanPrimitive(node) {
263
+ return node;
264
+ }
265
+ visitNilPrimitive(node) {
266
+ return node;
267
+ }
268
+ visitCharPrimitive(node) {
269
+ return node;
270
+ }
271
+ visitGoal(node) {
272
+ return new Goal(node.identifier, node.args.map((arg) => this.rename(arg)), node.loc);
273
+ }
274
+ visitExist(node) {
275
+ return new Exist(node.identifier, node.patterns.map((pat) => this.rename(pat)), node.loc);
276
+ }
277
+ visitFindall(node) {
278
+ return new Findall(this.rename(node.template), this.rename(node.goal), this.rename(node.bag), node.loc);
279
+ }
280
+ visitForall(node) {
281
+ return new Forall(this.rename(node.condition), this.rename(node.action), node.loc);
282
+ }
283
+ visitCall(node) {
284
+ return new Call(this.rename(node.callee), node.args.map((arg) => this.rename(arg)), node.loc);
285
+ }
286
+ visitNot(node) {
287
+ return new Not(this.rename(node.expression), node.loc);
288
+ }
289
+ visitLogicConstraint(node) {
290
+ return new LogicConstraint(this.rename(node.expression), node.loc);
291
+ }
292
+ visitSequence(node) {
293
+ return new Sequence(node.statements.map((stmt) => this.rename(stmt)), node.loc);
294
+ }
295
+ visitIf(node) {
296
+ return new If(this.rename(node.condition), this.rename(node.then), this.rename(node.elseExpr), node.loc);
297
+ }
298
+ visitComparisonOperation(node) {
299
+ return new ComparisonOperation(node.operator, this.rename(node.left), this.rename(node.right), node.loc);
300
+ }
301
+ visitUnifyOperation(node) {
302
+ return new UnifyOperation(node.operator, this.rename(node.left), this.rename(node.right), node.loc);
303
+ }
304
+ visitAssignOperation(node) {
305
+ return new AssignOperation(node.operator, this.rename(node.left), this.rename(node.right), node.loc);
306
+ }
307
+ visitArithmeticBinaryOperation(node) {
308
+ return new ArithmeticBinaryOperation(node.operator, this.rename(node.left), this.rename(node.right), node.loc);
309
+ }
310
+ visitArithmeticUnaryOperation(node) {
311
+ return new ArithmeticUnaryOperation(node.operator, this.rename(node.operand), node.loc);
312
+ }
313
+ visitListBinaryOperation(node) {
314
+ return new ListBinaryOperation(node.operator, this.rename(node.left), this.rename(node.right), node.loc);
315
+ }
316
+ visitListUnaryOperation(node) {
317
+ return new ListUnaryOperation(node.operator, this.rename(node.operand), node.loc);
318
+ }
319
+ visitLogicalBinaryOperation(node) {
320
+ return new LogicalBinaryOperation(node.operator, this.rename(node.left), this.rename(node.right), node.loc);
321
+ }
322
+ visitLogicalUnaryOperation(node) {
323
+ return new LogicalUnaryOperation(node.operator, this.rename(node.operand), node.loc);
324
+ }
325
+ visitBitwiseBinaryOperation(node) {
326
+ return new BitwiseBinaryOperation(node.operator, this.rename(node.left), this.rename(node.right), node.loc);
327
+ }
328
+ visitBitwiseUnaryOperation(node) {
329
+ return new BitwiseUnaryOperation(node.operator, this.rename(node.operand), node.loc);
330
+ }
331
+ visitStringOperation(node) {
332
+ return new StringOperation(node.operator, this.rename(node.left), this.rename(node.right), node.loc);
333
+ }
334
+ visitConsExpression(node) {
335
+ return new ConsExpression(this.rename(node.head), this.rename(node.tail), node.loc);
336
+ }
337
+ visitListPrimitive(node) {
338
+ return new ListPrimitive(node.value.map((el) => this.rename(el)), node.loc);
339
+ }
340
+ visitTupleExpr(node) {
341
+ return new TupleExpression(node.elements.map((el) => this.rename(el)), node.loc);
342
+ }
343
+ fallback(node) {
344
+ throw new UnexpectedNode(node.constructor.name, "LogicVariableRenamer");
345
+ }
346
+ }
347
+ /**
348
+ * Renames all variables in a Rule or Fact to fresh names to avoid name clashes during unification.
349
+ */
350
+ export function renameVariables(clause) {
351
+ const renames = new Map();
352
+ const freshId = ++variableCounter;
353
+ const renamer = new LogicVariableRenamer(renames, freshId);
354
+ const renamedClause = clause.accept(renamer);
355
+ return renamedClause;
356
+ }
357
+ class CPSBodyVisitor {
358
+ solveBody;
359
+ substs;
360
+ onSuccess;
361
+ onNextEq;
362
+ constructor(solveBody, substs, onSuccess, onNextEq) {
363
+ this.solveBody = solveBody;
364
+ this.substs = substs;
365
+ this.onSuccess = onSuccess;
366
+ this.onNextEq = onNextEq;
367
+ }
368
+ visitUnguardedBody(body) {
369
+ return this.solveBody(body.sequence.statements, this.substs, this.onSuccess, this.onNextEq);
370
+ }
371
+ visitGuardedBody(body) {
372
+ return this.solveBody([body.condition], this.substs, (guardSubsts, _nextGuardChoice) => {
373
+ return this.solveBody([body.body], guardSubsts, this.onSuccess, this.onNextEq);
374
+ }, this.onNextEq);
375
+ }
376
+ fallback(node) {
377
+ throw new UnexpectedNode(node.constructor.name, "CPSBodyVisitor");
378
+ }
379
+ }
380
+ class GoalSolverVisitor {
381
+ args;
382
+ baseSubst;
383
+ solveBody;
384
+ onSuccess;
385
+ onNextClause;
386
+ constructor(args, baseSubst, solveBody, onSuccess, onNextClause) {
387
+ this.args = args;
388
+ this.baseSubst = baseSubst;
389
+ this.solveBody = solveBody;
390
+ this.onSuccess = onSuccess;
391
+ this.onNextClause = onNextClause;
392
+ }
393
+ visitFact(fact) {
394
+ if (fact.patterns.length !== this.args.length)
395
+ return this.onNextClause;
396
+ const renamedFact = renameVariables(fact);
397
+ const substs = unifyParameters(renamedFact.patterns, this.args, this.baseSubst);
398
+ if (substs)
399
+ return this.onSuccess(substs, this.onNextClause);
400
+ return this.onNextClause;
401
+ }
402
+ visitRule(rule) {
403
+ if (rule.equations.length === 0)
404
+ return this.onNextClause;
405
+ const arity = rule.equations[0].patterns.length;
406
+ if (arity !== this.args.length)
407
+ return this.onNextClause;
408
+ const renamedRule = renameVariables(rule);
409
+ const tryRuleEq = (eqIndex) => {
410
+ if (eqIndex >= renamedRule.equations.length)
411
+ return this.onNextClause;
412
+ const eq = renamedRule.equations[eqIndex];
413
+ const substs = unifyParameters(eq.patterns, this.args, this.baseSubst);
414
+ const onNextEq = () => tryRuleEq(eqIndex + 1);
415
+ if (!substs)
416
+ return onNextEq;
417
+ const bodyVisitor = new CPSBodyVisitor(this.solveBody, substs, this.onSuccess, onNextEq);
418
+ if (isUnguardedBody(eq.body))
419
+ return eq.body.accept(bodyVisitor);
420
+ return eq.body.forEach((branch) => branch.accept(bodyVisitor));
421
+ };
422
+ return () => tryRuleEq(0);
423
+ }
424
+ fallback(node) {
425
+ throw new UnexpectedNode(node.constructor.name, "GoalSolverVisitor");
426
+ }
427
+ }
428
+ /**
429
+ * Solves a single logic goal (predicate call) using CPS.
430
+ */
431
+ export function solveGoalCPS(ctx, predicateName, args, solveBody, baseSubst, onSuccess, onFailure) {
432
+ const tryClause = (index) => {
433
+ let equations;
434
+ try {
435
+ const pred = ctx.lookup(predicateName);
436
+ if (!pred || !isRuntimePredicate(pred))
437
+ return () => onFailure();
438
+ equations = pred.equations;
439
+ }
440
+ catch (error) {
441
+ return () => onFailure();
442
+ }
443
+ if (index >= equations.length)
444
+ return () => onFailure();
445
+ const clause = equations[index];
446
+ const onNextClause = () => tryClause(index + 1);
447
+ const clauseVisitor = new GoalSolverVisitor(args, baseSubst, solveBody, onSuccess, onNextClause);
448
+ return clause.accept(clauseVisitor);
449
+ };
450
+ return () => tryClause(0);
451
+ }
452
+ /**
453
+ * Unifies two lists of parameters. Returns the resulting Substitution or null.
454
+ */
455
+ function unifyParameters(patterns, args, baseSubst) {
456
+ let subst = new Map(baseSubst);
457
+ for (let i = 0; i < patterns.length; i++) {
458
+ const nextSubst = unify(patterns[i], args[i], subst);
459
+ if (!nextSubst)
460
+ return null;
461
+ subst = nextSubst;
462
+ }
463
+ return subst;
464
+ }
465
+ /**
466
+ * Solves a findall/3 goal using CPS.
467
+ */
468
+ export function solveFindallCPS(node, currentSubsts, solveBody, onSuccess, onFailure) {
469
+ const gathered = [];
470
+ const collectResults = () => {
471
+ return solveBody([node.goal], currentSubsts, (resultSubsts, next) => {
472
+ gathered.push(instantiate(node.template, resultSubsts));
473
+ return () => next();
474
+ }, () => {
475
+ const resultList = new ListPattern(gathered);
476
+ const finalSubsts = unify(node.bag, resultList, currentSubsts);
477
+ if (finalSubsts) {
478
+ return onSuccess(finalSubsts, onFailure);
479
+ }
480
+ return () => onFailure();
481
+ });
482
+ };
483
+ return collectResults();
484
+ }
@@ -0,0 +1,14 @@
1
+ import { Expression, Pattern, PrimitiveValue } from "yukigo-ast";
2
+ import { Substitution } from "./LogicResolver.js";
3
+ import { ExpressionEvaluator } from "../../utils.js";
4
+ import { Continuation, Thunk } from "../../trampoline.js";
5
+ import { RuntimeContext } from "../RuntimeContext.js";
6
+ export declare class LogicTranslator {
7
+ private evaluator;
8
+ private ctx;
9
+ constructor(evaluator: ExpressionEvaluator, ctx: RuntimeContext);
10
+ patternToPrimitive(pat: Pattern): PrimitiveValue | undefined;
11
+ expressionToPattern<R = Pattern>(expr: Expression, k: Continuation<Pattern, R>): Thunk<R>;
12
+ primitiveToPattern(val: PrimitiveValue): Pattern;
13
+ instantiateExpressionAsPattern<R = Pattern>(expr: Expression, substs: Substitution, k: Continuation<Pattern, R>): Thunk<R>;
14
+ }
@@ -0,0 +1,99 @@
1
+ import { LiteralPattern, SymbolPrimitive, Variable, VariablePattern, isPattern, NumberPrimitive, StringPrimitive, BooleanPrimitive, ListPrimitive, ListPattern, ConsExpression, ConsPattern, FunctorPattern, } from "yukigo-ast";
2
+ import { instantiate } from "./LogicResolver.js";
3
+ import { InterpreterError } from "../../errors.js";
4
+ export class LogicTranslator {
5
+ evaluator;
6
+ ctx;
7
+ constructor(evaluator, ctx) {
8
+ this.evaluator = evaluator;
9
+ this.ctx = ctx;
10
+ }
11
+ patternToPrimitive(pat) {
12
+ if (pat instanceof LiteralPattern) {
13
+ const primitive = pat.name;
14
+ return primitive.value;
15
+ }
16
+ if (pat instanceof ListPattern) {
17
+ return pat.elements.map((el) => this.patternToPrimitive(el));
18
+ }
19
+ if (pat instanceof ConsPattern) {
20
+ const head = this.patternToPrimitive(pat.left);
21
+ const tail = this.patternToPrimitive(pat.right);
22
+ if (Array.isArray(tail)) {
23
+ return [head, ...tail];
24
+ }
25
+ return [head, tail];
26
+ }
27
+ // Return the pattern itself for non-primitive logic terms (VariablePattern, FunctorPattern, etc.)
28
+ return pat;
29
+ }
30
+ expressionToPattern(expr, k) {
31
+ if (isPattern(expr))
32
+ return k(expr);
33
+ if (expr instanceof ListPrimitive) {
34
+ const results = [];
35
+ const next = (index) => {
36
+ if (index >= expr.value.length)
37
+ return k(new ListPattern(results));
38
+ return this.expressionToPattern(expr.value[index], (p) => {
39
+ results.push(p);
40
+ return () => next(index + 1);
41
+ });
42
+ };
43
+ return next(0);
44
+ }
45
+ if (expr instanceof ConsExpression) {
46
+ return this.expressionToPattern(expr.head, (headPat) => {
47
+ return () => this.expressionToPattern(expr.tail, (tailPat) => {
48
+ return k(new ConsPattern(headPat, tailPat));
49
+ });
50
+ });
51
+ }
52
+ if (expr instanceof Variable || expr instanceof SymbolPrimitive) {
53
+ const name = expr instanceof Variable ? expr.identifier.value : expr.value;
54
+ if (this.ctx.isDefined(name)) {
55
+ return this.evaluator.evaluate(expr, (val) => {
56
+ return k(this.primitiveToPattern(val));
57
+ });
58
+ }
59
+ return k(new VariablePattern(expr instanceof Variable ? expr.identifier : expr));
60
+ }
61
+ return this.evaluator.evaluate(expr, (val) => {
62
+ return k(this.primitiveToPattern(val));
63
+ });
64
+ }
65
+ primitiveToPattern(val) {
66
+ if (isPattern(val)) {
67
+ return val;
68
+ }
69
+ if (typeof val === "number") {
70
+ return new LiteralPattern(new NumberPrimitive(val));
71
+ }
72
+ if (typeof val === "string") {
73
+ return new LiteralPattern(new StringPrimitive(val));
74
+ }
75
+ if (typeof val === "boolean") {
76
+ return new LiteralPattern(new BooleanPrimitive(val));
77
+ }
78
+ if (Array.isArray(val)) {
79
+ return new ListPattern(val.map((v) => this.primitiveToPattern(v)));
80
+ }
81
+ if (val &&
82
+ typeof val === "object" &&
83
+ "type" in val &&
84
+ val.type === "Object") {
85
+ // Convert RuntimeObject to FunctorPattern for logic matching
86
+ const args = [];
87
+ for (const [_, fieldVal] of val.fields) {
88
+ args.push(this.primitiveToPattern(fieldVal));
89
+ }
90
+ return new FunctorPattern(new SymbolPrimitive(val.className || val.identifier), args);
91
+ }
92
+ throw new InterpreterError("primitiveToPattern", `Cannot convert value ${val} to Logic Pattern`);
93
+ }
94
+ instantiateExpressionAsPattern(expr, substs, k) {
95
+ return this.expressionToPattern(expr, (patternBase) => {
96
+ return k(instantiate(patternBase, substs));
97
+ });
98
+ }
99
+ }
@@ -0,0 +1,12 @@
1
+ import { PrimitiveValue, RuntimeFunction } from "yukigo-ast";
2
+ import { Continuation, CPSThunk, Thunk } from "../../trampoline.js";
3
+ import { RuntimeContext } from "../RuntimeContext.js";
4
+ export declare class FunctionRuntime {
5
+ private context;
6
+ constructor(context: RuntimeContext);
7
+ apply(func: RuntimeFunction, args: PrimitiveValue[], k: Continuation<PrimitiveValue>): Thunk<PrimitiveValue>;
8
+ applyArguments(func: RuntimeFunction, args: (PrimitiveValue | (() => PrimitiveValue))[]): CPSThunk<PrimitiveValue>;
9
+ private preloadDefinitions;
10
+ private patternsMatch;
11
+ private evaluateSequence;
12
+ }