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,259 @@
1
+ import { Exist, Findall, Goal, LogicConstraint, Sequence, SymbolPrimitive, VariablePattern, } from "yukigo-ast";
2
+ import { solveFindallCPS, solveGoalCPS, unify, instantiate, } from "./LogicResolver.js";
3
+ import { createStream } from "../../utils.js";
4
+ import { InterpreterVisitor } from "../Visitor.js";
5
+ import { LogicTranslator } from "./LogicTranslator.js";
6
+ import { trampoline } from "../../trampoline.js";
7
+ import { PatternResolver } from "../PatternMatcher.js";
8
+ export class LogicEngine {
9
+ context;
10
+ translator;
11
+ constructor(evaluator, context) {
12
+ this.context = context;
13
+ this.translator = new LogicTranslator(evaluator, this.context);
14
+ }
15
+ unifyExpr(left, right, k) {
16
+ return this.translator.instantiateExpressionAsPattern(left, new Map(), (p1) => {
17
+ return () => this.translator.instantiateExpressionAsPattern(right, new Map(), (p2) => {
18
+ return k(unify(p1, p2, new Map()) !== null);
19
+ });
20
+ });
21
+ }
22
+ solveQuery(node, k, modeOverride) {
23
+ const mode = modeOverride || this.context.config.outputMode || "first";
24
+ if (mode === "all") {
25
+ return this.collectAllResults(node.expressions, new Map(), (results) => k(results.map((s) => this.formatLogicResult(s))));
26
+ }
27
+ else if (mode === "stream") {
28
+ return k(this.createLazyStream(node.expressions, new Map()));
29
+ }
30
+ // "first" mode
31
+ return this.solveConjunction(node.expressions, new Map(), (s) => k(this.formatLogicResult(s)), () => k(false));
32
+ }
33
+ solveGoal(node, k, modeOverride) {
34
+ return this.prepareLogicTargetCPS(node, new Map(), ({ id, patterns }) => {
35
+ // Goals evaluated as expressions should usually default to "first"
36
+ // to avoid combinatorial explosion when they are part of a sequence.
37
+ const mode = modeOverride || this.context.config.outputMode || "first";
38
+ if (mode === "all") {
39
+ return this.collectAllResultsForGoal(id, patterns, new Map(), (results) => k(results.map((s) => this.formatLogicResult(s))));
40
+ }
41
+ else if (mode === "stream") {
42
+ return k(this.createLazyStreamForGoal(id, patterns, new Map()));
43
+ }
44
+ return solveGoalCPS(this.context, id, patterns, (body, s, onSucc, onFail) => this.solveConjunction(body, s, onSucc, onFail), new Map(), (s) => k(this.formatLogicResult(s)), () => k(false));
45
+ });
46
+ }
47
+ solveNot(node, k) {
48
+ return () => this.solveConjunction([node.expression], new Map(), () => k(false), () => k(true));
49
+ }
50
+ solveFindall(node, k) {
51
+ return solveFindallCPS(node, new Map(), (body, substs, onSuccess, onFailure) => this.solveConjunction(body, substs, onSuccess, onFailure), (finalSubsts) => {
52
+ const pat = finalSubsts.get(node.bag.name.value);
53
+ const val = this.translator.patternToPrimitive(pat);
54
+ return k(val);
55
+ }, () => k([]));
56
+ }
57
+ solveForall(node, k) {
58
+ return this.solveConjunction([node.condition], new Map(), (condSubsts, nextCond) => {
59
+ return this.solveConjunction([node.action], condSubsts, () => nextCond, () => k(false));
60
+ }, () => k(true));
61
+ }
62
+ solveExist(node, k, modeOverride) {
63
+ return this.prepareLogicTargetCPS(node, new Map(), ({ id, patterns }) => {
64
+ // Exists evaluated as expressions should usually default to "first"
65
+ const mode = modeOverride || this.context.config.outputMode || "first";
66
+ if (mode === "all") {
67
+ return this.collectAllResultsForGoal(id, patterns, new Map(), (results) => k(results.map((s) => this.formatLogicResult(s))));
68
+ }
69
+ else if (mode === "stream") {
70
+ return k(this.createLazyStreamForGoal(id, patterns, new Map()));
71
+ }
72
+ return solveGoalCPS(this.context, id, patterns, (body, s, onSucc, onFail) => this.solveConjunction(body, s, onSucc, onFail), new Map(), (s) => k(this.formatLogicResult(s)), () => k(false));
73
+ });
74
+ }
75
+ solveConjunction(nodes, substs, onSuccess, onFailure) {
76
+ if (this.context.config.debug)
77
+ console.log(`[LogicEngine] Solving conjunction.`);
78
+ if (nodes.length === 0) {
79
+ if (this.context.config.debug)
80
+ console.log(`[LogicEngine] Nothing else to solve. Success.`);
81
+ return onSuccess(substs, onFailure);
82
+ }
83
+ const [head, ...tail] = nodes;
84
+ return () => this.solveSingle(head, substs, (newSubsts, next) => {
85
+ return this.solveConjunction(tail, newSubsts, onSuccess, next);
86
+ }, onFailure);
87
+ }
88
+ solveSingle(node, substs, onSuccess, onFailure) {
89
+ if (this.isLogicGoal(node))
90
+ return () => this.solveLogicGoal(node, substs, onSuccess, onFailure);
91
+ return () => this.solveCondition(node, substs, onSuccess, onFailure);
92
+ }
93
+ solveLogicGoal(goal, substs, onSuccess, onFailure) {
94
+ if (goal instanceof LogicConstraint) {
95
+ return () => this.solveConjunction([goal.expression], substs, onSuccess, onFailure);
96
+ }
97
+ if (goal instanceof Sequence) {
98
+ return () => this.solveConjunction(goal.statements, substs, onSuccess, onFailure);
99
+ }
100
+ if (goal instanceof Findall) {
101
+ return solveFindallCPS(goal, substs, (body, s, onSucc, onFail) => this.solveConjunction(body, s, onSucc, onFail), onSuccess, onFailure);
102
+ }
103
+ if (this.context.config.debug) {
104
+ console.log(`[LogicEngine] Solving logic goal: ${goal.identifier.value}.`);
105
+ }
106
+ return this.prepareLogicTargetCPS(goal, substs, ({ id, patterns }) => {
107
+ return solveGoalCPS(this.context, id, patterns, (body, s, onSucc, onFail) => this.solveConjunction(body, s, onSucc, onFail), substs, onSuccess, onFailure);
108
+ });
109
+ }
110
+ solveCondition(expr, substs, onSuccess, onFailure) {
111
+ this.createLocalEnv(substs);
112
+ const localEvaluator = new InterpreterVisitor(this.context);
113
+ return localEvaluator.evaluate(expr, (result) => {
114
+ if (result !== undefined && result !== false) {
115
+ let currentSubsts = new Map(substs);
116
+ for (const [name, val] of this.context.env.head) {
117
+ const pat = this.translator.primitiveToPattern(val);
118
+ const unified = unify(new VariablePattern(new SymbolPrimitive(name)), pat, currentSubsts);
119
+ if (unified) {
120
+ currentSubsts = unified;
121
+ }
122
+ else {
123
+ return onFailure();
124
+ }
125
+ }
126
+ return onSuccess(currentSubsts, onFailure);
127
+ }
128
+ return onFailure();
129
+ });
130
+ }
131
+ isLogicGoal(node) {
132
+ return (node instanceof Goal ||
133
+ node instanceof Exist ||
134
+ node instanceof Findall ||
135
+ node instanceof LogicConstraint ||
136
+ node instanceof Sequence);
137
+ }
138
+ createLocalEnv(substs) {
139
+ this.context.pushEnv(new Map());
140
+ for (const [name, pattern] of substs) {
141
+ const resolvedPattern = instantiate(pattern, substs);
142
+ const value = this.translator.patternToPrimitive(resolvedPattern);
143
+ this.context.define(name, value);
144
+ // map base name if it was standardized apart ("X_1" -> "X")
145
+ const baseNameMatch = name.match(/^(.*)_\d+$/);
146
+ if (baseNameMatch) {
147
+ const baseName = baseNameMatch[1];
148
+ if (!this.context.env.head.has(baseName)) {
149
+ this.context.define(baseName, value);
150
+ }
151
+ }
152
+ }
153
+ }
154
+ prepareLogicTargetCPS(node, substs, k) {
155
+ const id = node.identifier.value;
156
+ if (node instanceof Goal) {
157
+ const patterns = [];
158
+ const next = (index) => {
159
+ if (index >= node.args.length)
160
+ return k({ id, patterns });
161
+ return this.translator.instantiateExpressionAsPattern(node.args[index], substs, (p) => {
162
+ patterns.push(p);
163
+ return () => next(index + 1);
164
+ });
165
+ };
166
+ return () => next(0);
167
+ }
168
+ else {
169
+ const patterns = node.patterns.map((pat) => instantiate(pat, substs));
170
+ return k({ id, patterns });
171
+ }
172
+ }
173
+ collectAllResults(nodes, substs, k) {
174
+ const results = [];
175
+ return this.solveConjunction(nodes, substs, (s, next) => {
176
+ if (this.context.config.debug)
177
+ console.log(`[LogicEngine] Pushing { ${Array.from(s).map(([k, pat]) => `${k} -> ${pat.accept(new PatternResolver())}`)} } to results`);
178
+ results.push(s);
179
+ if (this.context.config.debug)
180
+ console.log(`[LogicEngine] Collected results: ${results.map((s) => `{ ${Array.from(s).map(([k, pat]) => `${k} -> ${pat.accept(new PatternResolver())}`)} }`)}`);
181
+ return next;
182
+ }, () => k(results));
183
+ }
184
+ collectAllResultsForGoal(id, patterns, substs, k) {
185
+ const results = [];
186
+ return solveGoalCPS(this.context, id, patterns, (body, s, onSucc, onFail) => this.solveConjunction(body, s, onSucc, onFail), substs, (s, next) => {
187
+ results.push(s);
188
+ return next;
189
+ }, () => k(results));
190
+ }
191
+ createLazyStream(nodes, substs) {
192
+ let currentSubst = null;
193
+ let currentNext = null;
194
+ let thunk = this.solveConjunction(nodes, substs, (s, next) => {
195
+ currentSubst = s;
196
+ currentNext = next;
197
+ return null;
198
+ }, () => {
199
+ currentSubst = null;
200
+ return null;
201
+ });
202
+ return createStream(() => {
203
+ return {
204
+ next: () => {
205
+ trampoline(thunk);
206
+ if (currentSubst) {
207
+ const res = this.formatLogicResult(currentSubst);
208
+ thunk = currentNext();
209
+ return { value: res, done: false };
210
+ }
211
+ return { value: undefined, done: true };
212
+ },
213
+ };
214
+ });
215
+ }
216
+ createLazyStreamForGoal(id, patterns, substs) {
217
+ let currentSubst = null;
218
+ let currentNext = null;
219
+ let thunk = solveGoalCPS(this.context, id, patterns, (body, s, onSucc, onFail) => this.solveConjunction(body, s, onSucc, onFail), substs, (s, next) => {
220
+ currentSubst = s;
221
+ currentNext = next;
222
+ return null;
223
+ }, () => {
224
+ currentSubst = null;
225
+ return null;
226
+ });
227
+ return createStream(() => {
228
+ return {
229
+ next: () => {
230
+ trampoline(thunk);
231
+ if (currentSubst) {
232
+ const res = this.formatLogicResult(currentSubst);
233
+ thunk = currentNext();
234
+ return { value: res, done: false };
235
+ }
236
+ return { value: undefined, done: true };
237
+ },
238
+ };
239
+ });
240
+ }
241
+ formatLogicResult(substs) {
242
+ const solutions = new Map();
243
+ substs.forEach((pattern, key) => {
244
+ const val = this.translator.patternToPrimitive(pattern);
245
+ if (val !== undefined) {
246
+ solutions.set(key, val);
247
+ // Also map base name if it was standardized apart (e.g., "X_1" -> "X")
248
+ const baseNameMatch = key.match(/^(.*)_\d+$/);
249
+ if (baseNameMatch) {
250
+ const baseName = baseNameMatch[1];
251
+ if (!solutions.has(baseName)) {
252
+ solutions.set(baseName, val);
253
+ }
254
+ }
255
+ }
256
+ });
257
+ return { success: true, solutions };
258
+ }
259
+ }
@@ -0,0 +1,53 @@
1
+ import { Fact, Pattern, Rule, Findall } from "yukigo-ast";
2
+ import { Thunk } from "../../trampoline.js";
3
+ import { LogicExecutable } from "./LogicEngine.js";
4
+ import { RuntimeContext } from "../RuntimeContext.js";
5
+ /**
6
+ * A Substitution maps variable names to their bound patterns.
7
+ */
8
+ export type Substitution = Map<string, Pattern>;
9
+ /**
10
+ * Internal result of a logic operation.
11
+ */
12
+ export type InternalLogicResult = {
13
+ success: true;
14
+ substs: Substitution;
15
+ };
16
+ /**
17
+ * Backtracking continuations.
18
+ */
19
+ export type SuccessCont = (substs: Substitution, next: () => Thunk<any>) => Thunk<any>;
20
+ export type FailureCont = () => Thunk<any>;
21
+ /**
22
+ * A function that can solve a sequence of logic goals/expressions in CPS.
23
+ */
24
+ export type BodySolverCPS = (expressions: LogicExecutable[], env: Substitution, onSuccess: SuccessCont, onFailure: FailureCont) => Thunk<any>;
25
+ /**
26
+ * Creates a successful logic result.
27
+ */
28
+ export declare function success(substs: Substitution): InternalLogicResult;
29
+ /**
30
+ * Unifies two patterns given an existing set of substitutions.
31
+ * Returns the updated substitution map or null if unification fails.
32
+ */
33
+ export declare function unify(t1: Pattern, t2: Pattern, argEnv?: Substitution): Substitution | null;
34
+ /**
35
+ * Follows variable bindings in the substitution map until a non-variable or unbound variable is found.
36
+ */
37
+ export declare function resolve(node: Pattern, env: Substitution): Pattern;
38
+ /**
39
+ * Fully instantiates a pattern by replacing all bound variables with their values.
40
+ */
41
+ export declare function instantiate(pattern: Pattern, substs: Substitution, seen?: Set<string>): Pattern;
42
+ /**
43
+ * Renames all variables in a Rule or Fact to fresh names to avoid name clashes during unification.
44
+ */
45
+ export declare function renameVariables<T extends Rule | Fact>(clause: T): T;
46
+ /**
47
+ * Solves a single logic goal (predicate call) using CPS.
48
+ */
49
+ export declare function solveGoalCPS(ctx: RuntimeContext, predicateName: string, args: Pattern[], solveBody: BodySolverCPS, baseSubst: Substitution, onSuccess: SuccessCont, onFailure: FailureCont): Thunk<any>;
50
+ /**
51
+ * Solves a findall/3 goal using CPS.
52
+ */
53
+ export declare function solveFindallCPS(node: Findall, currentSubsts: Substitution, solveBody: BodySolverCPS, onSuccess: SuccessCont, onFailure: FailureCont): Thunk<any>;