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,858 @@
1
+ import {
2
+ EnvStack,
3
+ Fact,
4
+ FunctorPattern,
5
+ ListPattern,
6
+ LiteralPattern,
7
+ Pattern,
8
+ Rule,
9
+ VariablePattern,
10
+ WildcardPattern,
11
+ isRuntimePredicate,
12
+ UnguardedBody,
13
+ Findall,
14
+ ConsPattern,
15
+ Equation,
16
+ SymbolPrimitive,
17
+ Goal,
18
+ Exist,
19
+ LogicConstraint,
20
+ Sequence,
21
+ Not,
22
+ ComparisonOperation,
23
+ UnifyOperation,
24
+ ApplicationPattern,
25
+ TuplePattern,
26
+ ConstructorPattern,
27
+ UnionPattern,
28
+ AsPattern,
29
+ PatternVisitor,
30
+ TypePattern,
31
+ AssignOperation,
32
+ ArithmeticBinaryOperation,
33
+ ArithmeticUnaryOperation,
34
+ ConsExpression,
35
+ ListPrimitive,
36
+ TupleExpression,
37
+ If,
38
+ Forall,
39
+ Call,
40
+ ListBinaryOperation,
41
+ ListUnaryOperation,
42
+ LogicalBinaryOperation,
43
+ LogicalUnaryOperation,
44
+ BitwiseBinaryOperation,
45
+ BitwiseUnaryOperation,
46
+ StringOperation,
47
+ NumberPrimitive,
48
+ StringPrimitive,
49
+ BooleanPrimitive,
50
+ NilPrimitive,
51
+ CharPrimitive,
52
+ Visitor,
53
+ isUnguardedBody,
54
+ GuardedBody,
55
+ } from "yukigo-ast";
56
+ import { Thunk } from "../../trampoline.js";
57
+ import { LogicExecutable } from "./LogicEngine.js";
58
+ import { RuntimeContext } from "../RuntimeContext.js";
59
+
60
+ /**
61
+ * A Substitution maps variable names to their bound patterns.
62
+ */
63
+ export type Substitution = Map<string, Pattern>;
64
+
65
+ /**
66
+ * Internal result of a logic operation.
67
+ */
68
+ export type InternalLogicResult = { success: true; substs: Substitution };
69
+
70
+ /**
71
+ * Backtracking continuations.
72
+ */
73
+ export type SuccessCont = (
74
+ substs: Substitution,
75
+ next: () => Thunk<any>,
76
+ ) => Thunk<any>;
77
+ export type FailureCont = () => Thunk<any>;
78
+
79
+ /**
80
+ * A function that can solve a sequence of logic goals/expressions in CPS.
81
+ */
82
+ export type BodySolverCPS = (
83
+ expressions: LogicExecutable[],
84
+ env: Substitution,
85
+ onSuccess: SuccessCont,
86
+ onFailure: FailureCont,
87
+ ) => Thunk<any>;
88
+
89
+ /**
90
+ * Creates a successful logic result.
91
+ */
92
+ export function success(substs: Substitution): InternalLogicResult {
93
+ return { success: true, substs };
94
+ }
95
+
96
+ /**
97
+ * Unifies two patterns given an existing set of substitutions.
98
+ * Returns the updated substitution map or null if unification fails.
99
+ */
100
+ export function unify(
101
+ t1: Pattern,
102
+ t2: Pattern,
103
+ argEnv?: Substitution,
104
+ ): Substitution | null {
105
+ const env: Substitution = argEnv ? new Map(argEnv) : new Map();
106
+ return unifyInPlace(t1, t2, env) ? env : null;
107
+ }
108
+
109
+ /**
110
+ * Internal unification that modifies the environment in place for efficiency during recursion.
111
+ */
112
+ function unifyInPlace(t1: Pattern, t2: Pattern, env: Substitution): boolean {
113
+ const r1 = resolve(t1, env);
114
+ const r2 = resolve(t2, env);
115
+
116
+ if (r1 === r2) return true;
117
+
118
+ if (r1 instanceof WildcardPattern || r2 instanceof WildcardPattern) {
119
+ return true;
120
+ }
121
+
122
+ if (r1 instanceof VariablePattern) {
123
+ env.set(r1.name.value, r2);
124
+ return true;
125
+ }
126
+ if (r2 instanceof VariablePattern) {
127
+ env.set(r2.name.value, r1);
128
+ return true;
129
+ }
130
+
131
+ if (r1 instanceof LiteralPattern && r2 instanceof LiteralPattern) {
132
+ return r1.name.equals(r2.name);
133
+ }
134
+
135
+ if (r1 instanceof FunctorPattern && r2 instanceof FunctorPattern) {
136
+ if (r1.identifier.value !== r2.identifier.value) return false;
137
+ if (r1.args.length !== r2.args.length) return false;
138
+
139
+ for (let i = 0; i < r1.args.length; i++) {
140
+ if (!unifyInPlace(r1.args[i], r2.args[i], env)) return false;
141
+ }
142
+ return true;
143
+ }
144
+
145
+ if (r1 instanceof ListPattern && r2 instanceof ListPattern) {
146
+ if (r1.elements.length !== r2.elements.length) return false;
147
+ for (let i = 0; i < r1.elements.length; i++) {
148
+ if (!unifyInPlace(r1.elements[i], r2.elements[i], env)) return false;
149
+ }
150
+ return true;
151
+ }
152
+
153
+ if (r1 instanceof ConsPattern && r2 instanceof ConsPattern) {
154
+ let curr1: Pattern = r1;
155
+ let curr2: Pattern = r2;
156
+
157
+ while (curr1 instanceof ConsPattern && curr2 instanceof ConsPattern) {
158
+ if (!unifyInPlace(curr1.left, curr2.left, env)) return false;
159
+ curr1 = resolve(curr1.right, env);
160
+ curr2 = resolve(curr2.right, env);
161
+ }
162
+ return unifyInPlace(curr1, curr2, env);
163
+ }
164
+
165
+ if (r1 instanceof ConsPattern && r2 instanceof ListPattern) {
166
+ if (r2.elements.length === 0) return false;
167
+ const [head, ...tail] = r2.elements;
168
+ return (
169
+ unifyInPlace(r1.left, head, env) &&
170
+ unifyInPlace(r1.right, new ListPattern(tail), env)
171
+ );
172
+ }
173
+
174
+ if (r1 instanceof ListPattern && r2 instanceof ConsPattern) {
175
+ if (r1.elements.length === 0) return false;
176
+ const [head, ...tail] = r1.elements;
177
+ return (
178
+ unifyInPlace(head, r2.left, env) &&
179
+ unifyInPlace(new ListPattern(tail), r2.right, env)
180
+ );
181
+ }
182
+
183
+ return false;
184
+ }
185
+
186
+ /**
187
+ * Follows variable bindings in the substitution map until a non-variable or unbound variable is found.
188
+ */
189
+ export function resolve(node: Pattern, env: Substitution): Pattern {
190
+ let current = node;
191
+ const seen = new Set<string>();
192
+ while (current instanceof VariablePattern) {
193
+ const name = current.name.value;
194
+ if (seen.has(name)) break;
195
+ seen.add(name);
196
+ const bound = env.get(name);
197
+ if (!bound) break;
198
+ current = bound;
199
+ }
200
+ return current;
201
+ }
202
+
203
+ class Instantiator implements PatternVisitor<Pattern> {
204
+ constructor(
205
+ private substs: Substitution,
206
+ private seen: Set<string> = new Set(),
207
+ ) {}
208
+
209
+ visitVariablePattern(node: VariablePattern): Pattern {
210
+ const name = node.name.value;
211
+ if (this.seen.has(name)) return node;
212
+ const val = this.substs.get(name);
213
+ if (val) {
214
+ const nextSeen = new Set(this.seen);
215
+ nextSeen.add(name);
216
+ return new Instantiator(this.substs, nextSeen).instantiate(val);
217
+ }
218
+ return node;
219
+ }
220
+
221
+ visitLiteralPattern(node: LiteralPattern): Pattern {
222
+ return node;
223
+ }
224
+
225
+ visitApplicationPattern(node: ApplicationPattern): Pattern {
226
+ return new ApplicationPattern(
227
+ node.identifier,
228
+ node.args.map((arg) => this.instantiate(arg)),
229
+ node.loc,
230
+ );
231
+ }
232
+
233
+ visitTuplePattern(node: TuplePattern): Pattern {
234
+ return new TuplePattern(
235
+ node.elements.map((el) => this.instantiate(el)),
236
+ node.loc,
237
+ );
238
+ }
239
+
240
+ visitListPattern(node: ListPattern): Pattern {
241
+ return new ListPattern(
242
+ node.elements.map((el) => this.instantiate(el)),
243
+ node.loc,
244
+ );
245
+ }
246
+
247
+ visitFunctorPattern(node: FunctorPattern): Pattern {
248
+ return new FunctorPattern(
249
+ node.identifier,
250
+ node.args.map((arg) => this.instantiate(arg)),
251
+ node.loc,
252
+ );
253
+ }
254
+
255
+ visitAsPattern(node: AsPattern): Pattern {
256
+ return new AsPattern(
257
+ this.instantiate(node.left) as any,
258
+ this.instantiate(node.right),
259
+ node.loc,
260
+ );
261
+ }
262
+
263
+ visitWildcardPattern(node: WildcardPattern): Pattern {
264
+ return node;
265
+ }
266
+
267
+ visitUnionPattern(node: UnionPattern): Pattern {
268
+ return new UnionPattern(
269
+ node.elements.map((el) => this.instantiate(el)),
270
+ node.loc,
271
+ );
272
+ }
273
+
274
+ visitConstructorPattern(node: ConstructorPattern): Pattern {
275
+ return new ConstructorPattern(
276
+ node.identifier,
277
+ node.args.map((arg) => this.instantiate(arg)),
278
+ node.loc,
279
+ );
280
+ }
281
+
282
+ visitConsPattern(node: ConsPattern): Pattern {
283
+ return new ConsPattern(
284
+ this.instantiate(node.left),
285
+ this.instantiate(node.right),
286
+ node.loc,
287
+ );
288
+ }
289
+
290
+ visitTypePattern(node: TypePattern): Pattern {
291
+ return new TypePattern(
292
+ node.targetType,
293
+ node.innerPattern ? this.instantiate(node.innerPattern) : undefined,
294
+ node.loc,
295
+ );
296
+ }
297
+
298
+ instantiate(pattern: Pattern): Pattern {
299
+ return pattern.accept(this);
300
+ }
301
+ }
302
+
303
+ /**
304
+ * Fully instantiates a pattern by replacing all bound variables with their values.
305
+ */
306
+ export function instantiate(
307
+ pattern: Pattern,
308
+ substs: Substitution,
309
+ seen: Set<string> = new Set(),
310
+ ): Pattern {
311
+ return new Instantiator(substs, seen).instantiate(pattern);
312
+ }
313
+
314
+ let variableCounter = 0;
315
+
316
+ class LogicVariableRenamer implements PatternVisitor<Pattern> {
317
+ constructor(
318
+ private renames: Map<string, string>,
319
+ private freshId: number,
320
+ ) {}
321
+
322
+ public rename(node: any): any {
323
+ if (!node || typeof node !== "object") return node;
324
+ if (typeof node.accept === "function") {
325
+ return node.accept(this);
326
+ }
327
+ return node;
328
+ }
329
+
330
+ visitFact(node: Fact): Fact {
331
+ return new Fact(
332
+ node.identifier,
333
+ node.patterns.map((p) => this.rename(p)),
334
+ node.loc,
335
+ );
336
+ }
337
+ visitRule(node: Rule): Rule {
338
+ const renamedEquations = node.equations.map((eq) => {
339
+ const body = eq.body;
340
+ if (!isUnguardedBody(body))
341
+ throw new Error("GuardedBody renaming not implemented");
342
+ return new Equation(
343
+ eq.patterns.map((p) => this.rename(p)),
344
+ body.accept(this),
345
+ eq.returnExpr,
346
+ eq.loc,
347
+ );
348
+ });
349
+ return new Rule(node.identifier, renamedEquations, node.loc);
350
+ }
351
+ visitUnguardedBody(node: UnguardedBody): UnguardedBody {
352
+ return new UnguardedBody(
353
+ new Sequence(
354
+ node.sequence.statements.map((stmt) => this.rename(stmt)),
355
+ node.sequence.loc,
356
+ ),
357
+ node.loc,
358
+ );
359
+ }
360
+
361
+ // PatternVisitor
362
+ visitVariablePattern(node: VariablePattern): Pattern {
363
+ const name = node.name.value;
364
+ let newName = this.renames.get(name);
365
+ if (!newName) {
366
+ newName = `${name}_${this.freshId}`;
367
+ this.renames.set(name, newName);
368
+ }
369
+ return new VariablePattern(new SymbolPrimitive(newName), node.loc);
370
+ }
371
+
372
+ visitLiteralPattern(node: LiteralPattern): Pattern {
373
+ return node;
374
+ }
375
+
376
+ visitApplicationPattern(node: ApplicationPattern): Pattern {
377
+ return new ApplicationPattern(
378
+ node.identifier,
379
+ node.args.map((arg) => this.rename(arg)),
380
+ node.loc,
381
+ );
382
+ }
383
+
384
+ visitTuplePattern(node: TuplePattern): Pattern {
385
+ return new TuplePattern(
386
+ node.elements.map((el) => this.rename(el)),
387
+ node.loc,
388
+ );
389
+ }
390
+
391
+ visitListPattern(node: ListPattern): Pattern {
392
+ return new ListPattern(
393
+ node.elements.map((el) => this.rename(el)),
394
+ node.loc,
395
+ );
396
+ }
397
+
398
+ visitFunctorPattern(node: FunctorPattern): Pattern {
399
+ return new FunctorPattern(
400
+ node.identifier,
401
+ node.args.map((arg) => this.rename(arg)),
402
+ node.loc,
403
+ );
404
+ }
405
+
406
+ visitAsPattern(node: AsPattern): Pattern {
407
+ return new AsPattern(
408
+ this.rename(node.left) as any,
409
+ this.rename(node.right),
410
+ node.loc,
411
+ );
412
+ }
413
+
414
+ visitWildcardPattern(node: WildcardPattern): Pattern {
415
+ return node;
416
+ }
417
+
418
+ visitUnionPattern(node: UnionPattern): Pattern {
419
+ return new UnionPattern(
420
+ node.elements.map((el) => this.rename(el)),
421
+ node.loc,
422
+ );
423
+ }
424
+
425
+ visitConstructorPattern(node: ConstructorPattern): Pattern {
426
+ return new ConstructorPattern(
427
+ node.identifier,
428
+ node.args.map((arg) => this.rename(arg)),
429
+ node.loc,
430
+ );
431
+ }
432
+
433
+ visitConsPattern(node: ConsPattern): Pattern {
434
+ return new ConsPattern(
435
+ this.rename(node.left),
436
+ this.rename(node.right),
437
+ node.loc,
438
+ );
439
+ }
440
+
441
+ visitTypePattern(node: TypePattern): Pattern {
442
+ return new TypePattern(
443
+ node.targetType,
444
+ node.innerPattern ? this.rename(node.innerPattern) : undefined,
445
+ node.loc,
446
+ );
447
+ }
448
+
449
+ // Expression/Statement Visitor
450
+ visitSymbolPrimitive(node: SymbolPrimitive): any {
451
+ const name = node.value;
452
+ if (/^[A-Z_]/.test(name) && name !== "_") {
453
+ let newName = this.renames.get(name);
454
+ if (!newName) {
455
+ newName = `${name}_${this.freshId}`;
456
+ this.renames.set(name, newName);
457
+ }
458
+ return new SymbolPrimitive(newName, node.loc);
459
+ }
460
+ return node;
461
+ }
462
+
463
+ visitNumberPrimitive(node: NumberPrimitive): any {
464
+ return node;
465
+ }
466
+ visitStringPrimitive(node: StringPrimitive): any {
467
+ return node;
468
+ }
469
+ visitBooleanPrimitive(node: BooleanPrimitive): any {
470
+ return node;
471
+ }
472
+ visitNilPrimitive(node: NilPrimitive): any {
473
+ return node;
474
+ }
475
+ visitCharPrimitive(node: CharPrimitive): any {
476
+ return node;
477
+ }
478
+
479
+ visitGoal(node: Goal): any {
480
+ return new Goal(
481
+ node.identifier,
482
+ node.args.map((arg) => this.rename(arg)),
483
+ node.loc,
484
+ );
485
+ }
486
+
487
+ visitExist(node: Exist): any {
488
+ return new Exist(
489
+ node.identifier,
490
+ node.patterns.map((pat) => this.rename(pat)),
491
+ node.loc,
492
+ );
493
+ }
494
+
495
+ visitFindall(node: Findall): any {
496
+ return new Findall(
497
+ this.rename(node.template),
498
+ this.rename(node.goal),
499
+ this.rename(node.bag),
500
+ node.loc,
501
+ );
502
+ }
503
+
504
+ visitForall(node: Forall): any {
505
+ return new Forall(this.rename(node.condition), this.rename(node.action), node.loc);
506
+ }
507
+
508
+ visitCall(node: Call): any {
509
+ return new Call(
510
+ this.rename(node.callee),
511
+ node.args.map((arg) => this.rename(arg)),
512
+ node.loc,
513
+ );
514
+ }
515
+
516
+ visitNot(node: Not): any {
517
+ return new Not(this.rename(node.expression), node.loc);
518
+ }
519
+
520
+ visitLogicConstraint(node: LogicConstraint): any {
521
+ return new LogicConstraint(this.rename(node.expression), node.loc);
522
+ }
523
+
524
+ visitSequence(node: Sequence): any {
525
+ return new Sequence(
526
+ node.statements.map((stmt) => this.rename(stmt)),
527
+ node.loc,
528
+ );
529
+ }
530
+
531
+ visitIf(node: If): any {
532
+ return new If(
533
+ this.rename(node.condition),
534
+ this.rename(node.then),
535
+ this.rename(node.elseExpr),
536
+ node.loc,
537
+ );
538
+ }
539
+
540
+ visitComparisonOperation(node: ComparisonOperation): any {
541
+ return new ComparisonOperation(
542
+ node.operator,
543
+ this.rename(node.left),
544
+ this.rename(node.right),
545
+ node.loc,
546
+ );
547
+ }
548
+
549
+ visitUnifyOperation(node: UnifyOperation): any {
550
+ return new UnifyOperation(
551
+ node.operator,
552
+ this.rename(node.left),
553
+ this.rename(node.right),
554
+ node.loc,
555
+ );
556
+ }
557
+
558
+ visitAssignOperation(node: AssignOperation): any {
559
+ return new AssignOperation(
560
+ node.operator,
561
+ this.rename(node.left),
562
+ this.rename(node.right),
563
+ node.loc,
564
+ );
565
+ }
566
+
567
+ visitArithmeticBinaryOperation(node: ArithmeticBinaryOperation): any {
568
+ return new ArithmeticBinaryOperation(
569
+ node.operator,
570
+ this.rename(node.left),
571
+ this.rename(node.right),
572
+ node.loc,
573
+ );
574
+ }
575
+
576
+ visitArithmeticUnaryOperation(node: ArithmeticUnaryOperation): any {
577
+ return new ArithmeticUnaryOperation(
578
+ node.operator,
579
+ this.rename(node.operand),
580
+ node.loc,
581
+ );
582
+ }
583
+
584
+ visitListBinaryOperation(node: ListBinaryOperation): any {
585
+ return new ListBinaryOperation(
586
+ node.operator,
587
+ this.rename(node.left),
588
+ this.rename(node.right),
589
+ node.loc,
590
+ );
591
+ }
592
+
593
+ visitListUnaryOperation(node: ListUnaryOperation): any {
594
+ return new ListUnaryOperation(
595
+ node.operator,
596
+ this.rename(node.operand),
597
+ node.loc,
598
+ );
599
+ }
600
+
601
+ visitLogicalBinaryOperation(node: LogicalBinaryOperation): any {
602
+ return new LogicalBinaryOperation(
603
+ node.operator,
604
+ this.rename(node.left),
605
+ this.rename(node.right),
606
+ node.loc,
607
+ );
608
+ }
609
+
610
+ visitLogicalUnaryOperation(node: LogicalUnaryOperation): any {
611
+ return new LogicalUnaryOperation(
612
+ node.operator,
613
+ this.rename(node.operand),
614
+ node.loc,
615
+ );
616
+ }
617
+
618
+ visitBitwiseBinaryOperation(node: BitwiseBinaryOperation): any {
619
+ return new BitwiseBinaryOperation(
620
+ node.operator,
621
+ this.rename(node.left),
622
+ this.rename(node.right),
623
+ node.loc,
624
+ );
625
+ }
626
+
627
+ visitBitwiseUnaryOperation(node: BitwiseUnaryOperation): any {
628
+ return new BitwiseUnaryOperation(
629
+ node.operator,
630
+ this.rename(node.operand),
631
+ node.loc,
632
+ );
633
+ }
634
+
635
+ visitStringOperation(node: StringOperation): any {
636
+ return new StringOperation(
637
+ node.operator,
638
+ this.rename(node.left),
639
+ this.rename(node.right),
640
+ node.loc,
641
+ );
642
+ }
643
+
644
+ visitConsExpression(node: ConsExpression): any {
645
+ return new ConsExpression(
646
+ this.rename(node.head),
647
+ this.rename(node.tail),
648
+ node.loc,
649
+ );
650
+ }
651
+
652
+ visitListPrimitive(node: ListPrimitive): any {
653
+ return new ListPrimitive(
654
+ node.value.map((el) => this.rename(el)),
655
+ node.loc,
656
+ );
657
+ }
658
+
659
+ visitTupleExpr(node: TupleExpression): any {
660
+ return new TupleExpression(
661
+ node.elements.map((el) => this.rename(el)),
662
+ node.loc,
663
+ );
664
+ }
665
+ }
666
+
667
+ /**
668
+ * Renames all variables in a Rule or Fact to fresh names to avoid name clashes during unification.
669
+ */
670
+ export function renameVariables<T extends Rule | Fact>(clause: T): T {
671
+ const renames = new Map<string, string>();
672
+ const freshId = ++variableCounter;
673
+ const renamer = new LogicVariableRenamer(renames, freshId);
674
+ const renamedClause = clause.accept(renamer)
675
+ return renamedClause;
676
+ }
677
+
678
+ class CPSBodyVisitor implements Visitor<Thunk<any>> {
679
+ constructor(
680
+ private readonly solveBody: BodySolverCPS,
681
+ private readonly substs: Substitution,
682
+ private readonly onSuccess: SuccessCont,
683
+ private readonly onNextEq: Thunk<any>,
684
+ ) {}
685
+
686
+ public visitUnguardedBody(body: UnguardedBody): Thunk<any> {
687
+ return this.solveBody(
688
+ body.sequence.statements,
689
+ this.substs,
690
+ this.onSuccess,
691
+ this.onNextEq,
692
+ );
693
+ }
694
+
695
+ public visitGuardedBody(body: GuardedBody): Thunk<any> {
696
+ return this.solveBody(
697
+ [body.condition],
698
+ this.substs,
699
+ (guardSubsts: Substitution, _nextGuardChoice: Thunk<any>) => {
700
+ return this.solveBody(
701
+ [body.body],
702
+ guardSubsts,
703
+ this.onSuccess,
704
+ this.onNextEq,
705
+ );
706
+ },
707
+ this.onNextEq,
708
+ );
709
+ }
710
+ }
711
+
712
+ class GoalSolverVisitor implements Visitor<Thunk<any>> {
713
+ constructor(
714
+ private readonly args: Pattern[],
715
+ private readonly baseSubst: Substitution,
716
+ private readonly solveBody: BodySolverCPS,
717
+ private readonly onSuccess: SuccessCont,
718
+ private readonly onNextClause: Thunk<any>,
719
+ ) {}
720
+
721
+ public visitFact(fact: Fact): Thunk<any> {
722
+ if (fact.patterns.length !== this.args.length) return this.onNextClause;
723
+
724
+ const renamedFact = renameVariables(fact);
725
+ const substs = unifyParameters(
726
+ renamedFact.patterns,
727
+ this.args,
728
+ this.baseSubst,
729
+ );
730
+
731
+ if (substs) return this.onSuccess(substs, this.onNextClause);
732
+ return this.onNextClause;
733
+ }
734
+
735
+ public visitRule(rule: Rule): Thunk<any> {
736
+ if (rule.equations.length === 0) return this.onNextClause;
737
+
738
+ const arity = rule.equations[0].patterns.length;
739
+ if (arity !== this.args.length) return this.onNextClause;
740
+
741
+ const renamedRule = renameVariables(rule);
742
+
743
+ const tryRuleEq = (eqIndex: number): Thunk<any> => {
744
+ if (eqIndex >= renamedRule.equations.length) return this.onNextClause;
745
+
746
+ const eq = renamedRule.equations[eqIndex];
747
+ const substs = unifyParameters(eq.patterns, this.args, this.baseSubst);
748
+ const onNextEq = () => tryRuleEq(eqIndex + 1);
749
+
750
+ if (!substs) return onNextEq;
751
+
752
+ const bodyVisitor = new CPSBodyVisitor(
753
+ this.solveBody,
754
+ substs,
755
+ this.onSuccess,
756
+ onNextEq,
757
+ );
758
+
759
+ if (isUnguardedBody(eq.body)) return eq.body.accept(bodyVisitor);
760
+ return eq.body.forEach((branch) => branch.accept(bodyVisitor));
761
+ };
762
+
763
+ return () => tryRuleEq(0);
764
+ }
765
+ }
766
+
767
+ /**
768
+ * Solves a single logic goal (predicate call) using CPS.
769
+ */
770
+ export function solveGoalCPS(
771
+ ctx: RuntimeContext,
772
+ predicateName: string,
773
+ args: Pattern[],
774
+ solveBody: BodySolverCPS,
775
+ baseSubst: Substitution,
776
+ onSuccess: SuccessCont,
777
+ onFailure: FailureCont,
778
+ ): Thunk<any> {
779
+ const tryClause = (index: number): Thunk<any> => {
780
+ let equations: (Rule | Fact)[];
781
+
782
+ try {
783
+ const pred = ctx.lookup(predicateName);
784
+ if (!pred || !isRuntimePredicate(pred)) return () => onFailure();
785
+ equations = pred.equations;
786
+ } catch (error) {
787
+ return () => onFailure();
788
+ }
789
+
790
+ if (index >= equations.length) return () => onFailure();
791
+
792
+ const clause = equations[index];
793
+ const onNextClause = () => tryClause(index + 1);
794
+
795
+ const clauseVisitor = new GoalSolverVisitor(
796
+ args,
797
+ baseSubst,
798
+ solveBody,
799
+ onSuccess,
800
+ onNextClause,
801
+ );
802
+
803
+ return clause.accept(clauseVisitor);
804
+ };
805
+
806
+ return () => tryClause(0);
807
+ }
808
+
809
+ /**
810
+ * Unifies two lists of parameters. Returns the resulting Substitution or null.
811
+ */
812
+ function unifyParameters(
813
+ patterns: Pattern[],
814
+ args: Pattern[],
815
+ baseSubst: Substitution,
816
+ ): Substitution | null {
817
+ let subst: Substitution = new Map(baseSubst);
818
+ for (let i = 0; i < patterns.length; i++) {
819
+ const nextSubst = unify(patterns[i], args[i], subst);
820
+ if (!nextSubst) return null;
821
+ subst = nextSubst;
822
+ }
823
+ return subst;
824
+ }
825
+
826
+ /**
827
+ * Solves a findall/3 goal using CPS.
828
+ */
829
+ export function solveFindallCPS(
830
+ node: Findall,
831
+ currentSubsts: Substitution,
832
+ solveBody: BodySolverCPS,
833
+ onSuccess: SuccessCont,
834
+ onFailure: FailureCont,
835
+ ): Thunk<any> {
836
+ const gathered: Pattern[] = [];
837
+
838
+ const collectResults = (): Thunk<any> => {
839
+ return solveBody(
840
+ [node.goal],
841
+ currentSubsts,
842
+ (resultSubsts, next) => {
843
+ gathered.push(instantiate(node.template, resultSubsts));
844
+ return () => next();
845
+ },
846
+ () => {
847
+ const resultList = new ListPattern(gathered);
848
+ const finalSubsts = unify(node.bag, resultList, currentSubsts);
849
+ if (finalSubsts) {
850
+ return onSuccess(finalSubsts, onFailure);
851
+ }
852
+ return () => onFailure();
853
+ },
854
+ );
855
+ };
856
+
857
+ return collectResults();
858
+ }