yukigo 0.2.3 → 0.3.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.
- package/.mocharc.json +3 -3
- package/CHANGELOG.md +105 -40
- package/README.md +193 -193
- package/dist/analyzer/GraphBuilder.d.ts +1 -0
- package/dist/analyzer/GraphBuilder.js +2 -1
- package/dist/analyzer/index.d.ts +0 -7
- package/dist/analyzer/index.js +3 -3
- package/dist/analyzer/inspections/functional/functional.d.ts +1 -1
- package/dist/analyzer/inspections/functional/functional.js +1 -1
- package/dist/analyzer/inspections/functional/smells.d.ts +3 -3
- package/dist/analyzer/inspections/functional/smells.js +27 -35
- package/dist/analyzer/inspections/generic/generic.d.ts +22 -14
- package/dist/analyzer/inspections/generic/generic.js +82 -60
- package/dist/analyzer/inspections/generic/smells.d.ts +2 -0
- package/dist/analyzer/inspections/generic/smells.js +55 -64
- package/dist/analyzer/inspections/imperative/imperative.d.ts +3 -3
- package/dist/analyzer/inspections/imperative/imperative.js +3 -3
- package/dist/analyzer/inspections/imperative/smells.d.ts +1 -0
- package/dist/analyzer/inspections/imperative/smells.js +8 -5
- package/dist/analyzer/inspections/logic/logic.d.ts +8 -8
- package/dist/analyzer/inspections/logic/logic.js +5 -5
- package/dist/analyzer/inspections/logic/smells.js +2 -2
- package/dist/analyzer/inspections/object/object.d.ts +5 -5
- package/dist/analyzer/inspections/object/object.js +22 -21
- package/dist/analyzer/inspections/object/smells.js +11 -23
- package/dist/analyzer/utils.d.ts +6 -2
- package/dist/analyzer/utils.js +6 -2
- package/dist/interpreter/components/EnvBuilder.d.ts +20 -1
- package/dist/interpreter/components/EnvBuilder.js +211 -66
- package/dist/interpreter/components/EqualityComparer.d.ts +15 -0
- package/dist/interpreter/components/EqualityComparer.js +69 -0
- package/dist/interpreter/components/Operations.d.ts +17 -14
- package/dist/interpreter/components/Operations.js +61 -62
- package/dist/interpreter/components/PatternMatcher.d.ts +19 -58
- package/dist/interpreter/components/PatternMatcher.js +185 -286
- package/dist/interpreter/components/RuntimeContext.d.ts +24 -15
- package/dist/interpreter/components/RuntimeContext.js +43 -4
- package/dist/interpreter/components/TestRunner.d.ts +15 -13
- package/dist/interpreter/components/TestRunner.js +86 -53
- package/dist/interpreter/components/evaluators/BaseEvaluator.d.ts +19 -0
- package/dist/interpreter/components/evaluators/BaseEvaluator.js +20 -0
- package/dist/interpreter/components/evaluators/ExpressionEvaluator.d.ts +34 -0
- package/dist/interpreter/components/evaluators/ExpressionEvaluator.js +298 -0
- package/dist/interpreter/components/evaluators/OperationEvaluator.d.ts +25 -0
- package/dist/interpreter/components/evaluators/OperationEvaluator.js +226 -0
- package/dist/interpreter/components/evaluators/PrimitiveEvaluator.d.ts +19 -0
- package/dist/interpreter/components/evaluators/PrimitiveEvaluator.js +50 -0
- package/dist/interpreter/components/evaluators/StatementEvaluator.d.ts +28 -0
- package/dist/interpreter/components/evaluators/StatementEvaluator.js +240 -0
- package/dist/interpreter/components/evaluators/TestingEvaluator.d.ts +15 -0
- package/dist/interpreter/components/evaluators/TestingEvaluator.js +14 -0
- package/dist/interpreter/components/evaluators/index.d.ts +110 -0
- package/dist/interpreter/components/evaluators/index.js +21 -0
- package/dist/interpreter/components/evaluators/utils.d.ts +7 -0
- package/dist/interpreter/components/evaluators/utils.js +50 -0
- package/dist/interpreter/components/hooks/YukigoHook.d.ts +27 -0
- package/dist/interpreter/components/hooks/YukigoHook.js +1 -0
- package/dist/interpreter/components/kernel/commands.d.ts +122 -0
- package/dist/interpreter/components/kernel/commands.js +203 -0
- package/dist/interpreter/components/kernel/index.d.ts +51 -0
- package/dist/interpreter/components/kernel/index.js +176 -0
- package/dist/interpreter/components/logic/LogicEngine.d.ts +26 -23
- package/dist/interpreter/components/logic/LogicEngine.js +234 -221
- package/dist/interpreter/components/logic/LogicResolver.d.ts +22 -51
- package/dist/interpreter/components/logic/LogicResolver.js +73 -440
- package/dist/interpreter/components/logic/LogicTerm.d.ts +104 -0
- package/dist/interpreter/components/logic/LogicTerm.js +423 -0
- package/dist/interpreter/components/logic/LogicTranslator.d.ts +25 -9
- package/dist/interpreter/components/logic/LogicTranslator.js +211 -74
- package/dist/interpreter/components/runtimes/FunctionRuntime.d.ts +6 -4
- package/dist/interpreter/components/runtimes/FunctionRuntime.js +82 -84
- package/dist/interpreter/components/runtimes/LazyRuntime.d.ts +13 -14
- package/dist/interpreter/components/runtimes/LazyRuntime.js +112 -239
- package/dist/interpreter/components/runtimes/ObjectRuntime.d.ts +17 -14
- package/dist/interpreter/components/runtimes/ObjectRuntime.js +105 -74
- package/dist/interpreter/errors.d.ts +10 -0
- package/dist/interpreter/errors.js +17 -0
- package/dist/interpreter/index.d.ts +9 -5
- package/dist/interpreter/index.js +11 -13
- package/dist/interpreter/primitives/YuValue.d.ts +21 -0
- package/dist/interpreter/primitives/YuValue.js +40 -0
- package/dist/interpreter/primitives/capabilities.d.ts +90 -0
- package/dist/interpreter/primitives/capabilities.js +1 -0
- package/dist/interpreter/primitives/entities/LazyList.d.ts +48 -0
- package/dist/interpreter/primitives/entities/LazyList.js +164 -0
- package/dist/interpreter/primitives/entities/LogicResult.d.ts +51 -0
- package/dist/interpreter/primitives/entities/LogicResult.js +101 -0
- package/dist/interpreter/primitives/entities/RuntimeClass.d.ts +24 -0
- package/dist/interpreter/primitives/entities/RuntimeClass.js +57 -0
- package/dist/interpreter/primitives/entities/RuntimeFunction.d.ts +40 -0
- package/dist/interpreter/primitives/entities/RuntimeFunction.js +73 -0
- package/dist/interpreter/primitives/entities/RuntimeObject.d.ts +27 -0
- package/dist/interpreter/primitives/entities/RuntimeObject.js +82 -0
- package/dist/interpreter/primitives/entities/RuntimePredicate.d.ts +19 -0
- package/dist/interpreter/primitives/entities/RuntimePredicate.js +59 -0
- package/dist/interpreter/primitives/index.d.ts +14 -0
- package/dist/interpreter/primitives/index.js +14 -0
- package/dist/interpreter/primitives/scalars/YuBoolean.d.ts +24 -0
- package/dist/interpreter/primitives/scalars/YuBoolean.js +69 -0
- package/dist/interpreter/primitives/scalars/YuChar.d.ts +28 -0
- package/dist/interpreter/primitives/scalars/YuChar.js +78 -0
- package/dist/interpreter/primitives/scalars/YuNil.d.ts +25 -0
- package/dist/interpreter/primitives/scalars/YuNil.js +73 -0
- package/dist/interpreter/primitives/scalars/YuNumber.d.ts +49 -0
- package/dist/interpreter/primitives/scalars/YuNumber.js +165 -0
- package/dist/interpreter/primitives/sequences/YuArray.d.ts +33 -0
- package/dist/interpreter/primitives/sequences/YuArray.js +150 -0
- package/dist/interpreter/primitives/sequences/YuString.d.ts +40 -0
- package/dist/interpreter/primitives/sequences/YuString.js +130 -0
- package/dist/interpreter/utils.d.ts +23 -10
- package/dist/interpreter/utils.js +25 -30
- package/dist/tester/index.d.ts +3 -3
- package/dist/tester/index.js +39 -28
- package/dist/utils/helpers.d.ts +13 -5
- package/dist/utils/helpers.js +38 -85
- package/package.json +3 -16
- package/src/analyzer/GraphBuilder.ts +143 -142
- package/src/analyzer/index.ts +185 -194
- package/src/analyzer/inspections/functional/functional.ts +121 -121
- package/src/analyzer/inspections/functional/smells.ts +88 -102
- package/src/analyzer/inspections/generic/generic.ts +610 -577
- package/src/analyzer/inspections/generic/smells.ts +355 -365
- package/src/analyzer/inspections/imperative/imperative.ts +104 -101
- package/src/analyzer/inspections/imperative/smells.ts +58 -54
- package/src/analyzer/inspections/logic/logic.ts +84 -90
- package/src/analyzer/inspections/logic/smells.ts +54 -54
- package/src/analyzer/inspections/object/object.ts +295 -264
- package/src/analyzer/inspections/object/smells.ts +130 -144
- package/src/analyzer/utils.ts +115 -109
- package/src/index.ts +3 -3
- package/src/interpreter/components/EnvBuilder.ts +405 -202
- package/src/interpreter/components/EqualityComparer.ts +107 -0
- package/src/interpreter/components/Operations.ts +126 -99
- package/src/interpreter/components/PatternMatcher.ts +375 -475
- package/src/interpreter/components/RuntimeContext.ts +181 -119
- package/src/interpreter/components/TestRunner.ts +223 -151
- package/src/interpreter/components/evaluators/BaseEvaluator.ts +36 -0
- package/src/interpreter/components/evaluators/ExpressionEvaluator.ts +475 -0
- package/src/interpreter/components/evaluators/OperationEvaluator.ts +477 -0
- package/src/interpreter/components/evaluators/PrimitiveEvaluator.ts +80 -0
- package/src/interpreter/components/evaluators/StatementEvaluator.ts +372 -0
- package/src/interpreter/components/evaluators/TestingEvaluator.ts +26 -0
- package/src/interpreter/components/evaluators/index.ts +34 -0
- package/src/interpreter/components/evaluators/utils.ts +64 -0
- package/src/interpreter/components/hooks/YukigoHook.ts +45 -0
- package/src/interpreter/components/kernel/commands.ts +255 -0
- package/src/interpreter/components/kernel/index.ts +222 -0
- package/src/interpreter/components/logic/LogicEngine.ts +509 -519
- package/src/interpreter/components/logic/LogicResolver.ts +209 -858
- package/src/interpreter/components/logic/LogicTerm.ts +477 -0
- package/src/interpreter/components/logic/LogicTranslator.ts +362 -149
- package/src/interpreter/components/runtimes/FunctionRuntime.ts +249 -227
- package/src/interpreter/components/runtimes/LazyRuntime.ts +226 -334
- package/src/interpreter/components/runtimes/ObjectRuntime.ts +262 -224
- package/src/interpreter/errors.ts +68 -47
- package/src/interpreter/index.ts +50 -52
- package/src/interpreter/primitives/YuValue.ts +64 -0
- package/src/interpreter/primitives/capabilities.ts +111 -0
- package/src/interpreter/primitives/entities/LazyList.ts +225 -0
- package/src/interpreter/primitives/entities/LogicResult.ts +133 -0
- package/src/interpreter/primitives/entities/RuntimeClass.ts +70 -0
- package/src/interpreter/primitives/entities/RuntimeFunction.ts +108 -0
- package/src/interpreter/primitives/entities/RuntimeObject.ts +110 -0
- package/src/interpreter/primitives/entities/RuntimePredicate.ts +77 -0
- package/src/interpreter/primitives/index.ts +17 -0
- package/src/interpreter/primitives/scalars/YuBoolean.ts +79 -0
- package/src/interpreter/primitives/scalars/YuChar.ts +94 -0
- package/src/interpreter/primitives/scalars/YuNil.ts +84 -0
- package/src/interpreter/primitives/scalars/YuNumber.ts +187 -0
- package/src/interpreter/primitives/sequences/YuArray.ts +178 -0
- package/src/interpreter/primitives/sequences/YuString.ts +165 -0
- package/src/interpreter/utils.ts +105 -84
- package/src/tester/index.ts +157 -128
- package/src/utils/helpers.ts +113 -169
- package/tests/analyzer/functional.spec.ts +221 -207
- package/tests/analyzer/generic.spec.ts +178 -178
- package/tests/analyzer/helpers.spec.ts +83 -89
- package/tests/analyzer/logic.spec.ts +237 -237
- package/tests/analyzer/oop.spec.ts +320 -323
- package/tests/analyzer/transitive.spec.ts +166 -166
- package/tests/interpreter/EnvBuilder.spec.ts +181 -183
- package/tests/interpreter/FunctionRuntime.spec.ts +224 -223
- package/tests/interpreter/LazyRuntime.spec.ts +227 -225
- package/tests/interpreter/LogicEngine.spec.ts +371 -327
- package/tests/interpreter/LogicSubstitution.spec.ts +90 -80
- package/tests/interpreter/ObjectRuntime.spec.ts +431 -606
- package/tests/interpreter/Operations.spec.ts +312 -220
- package/tests/interpreter/PatternSystem.spec.ts +254 -213
- package/tests/interpreter/Tests.spec.ts +122 -122
- package/tests/interpreter/interpreter.spec.ts +1143 -991
- package/tests/kernel/YukigoKernel.spec.ts +112 -0
- package/tests/tester/Tester.spec.ts +156 -152
- package/tsconfig.build.json +16 -15
- package/tsconfig.json +27 -25
- package/dist/interpreter/components/Visitor.d.ts +0 -70
- package/dist/interpreter/components/Visitor.js +0 -634
- package/dist/interpreter/entities.d.ts +0 -105
- package/dist/interpreter/entities.js +0 -96
- package/dist/interpreter/trampoline.d.ts +0 -17
- package/dist/interpreter/trampoline.js +0 -38
- package/dist/src/analyzer/GraphBuilder.d.ts +0 -30
- package/dist/src/analyzer/GraphBuilder.js +0 -100
- package/dist/src/analyzer/index.d.ts +0 -59
- package/dist/src/analyzer/index.js +0 -152
- package/dist/src/analyzer/inspections/functional/functional.d.ts +0 -44
- package/dist/src/analyzer/inspections/functional/functional.js +0 -149
- package/dist/src/analyzer/inspections/functional/smells.d.ts +0 -16
- package/dist/src/analyzer/inspections/functional/smells.js +0 -98
- package/dist/src/analyzer/inspections/generic/generic.d.ts +0 -178
- package/dist/src/analyzer/inspections/generic/generic.js +0 -604
- package/dist/src/analyzer/inspections/generic/smells.d.ts +0 -61
- package/dist/src/analyzer/inspections/generic/smells.js +0 -349
- package/dist/src/analyzer/inspections/imperative/imperative.d.ts +0 -35
- package/dist/src/analyzer/inspections/imperative/imperative.js +0 -109
- package/dist/src/analyzer/inspections/imperative/smells.d.ts +0 -16
- package/dist/src/analyzer/inspections/imperative/smells.js +0 -58
- package/dist/src/analyzer/inspections/logic/logic.d.ts +0 -32
- package/dist/src/analyzer/inspections/logic/logic.js +0 -96
- package/dist/src/analyzer/inspections/logic/smells.d.ts +0 -15
- package/dist/src/analyzer/inspections/logic/smells.js +0 -60
- package/dist/src/analyzer/inspections/object/object.d.ts +0 -90
- package/dist/src/analyzer/inspections/object/object.js +0 -321
- package/dist/src/analyzer/inspections/object/smells.d.ts +0 -30
- package/dist/src/analyzer/inspections/object/smells.js +0 -135
- package/dist/src/analyzer/utils.d.ts +0 -30
- package/dist/src/analyzer/utils.js +0 -78
- package/dist/src/index.d.ts +0 -4
- package/dist/src/index.js +0 -4
- package/dist/src/interpreter/components/EnvBuilder.d.ts +0 -21
- package/dist/src/interpreter/components/EnvBuilder.js +0 -155
- package/dist/src/interpreter/components/Operations.d.ts +0 -14
- package/dist/src/interpreter/components/Operations.js +0 -84
- package/dist/src/interpreter/components/PatternMatcher.d.ts +0 -73
- package/dist/src/interpreter/components/PatternMatcher.js +0 -358
- package/dist/src/interpreter/components/RuntimeContext.d.ts +0 -35
- package/dist/src/interpreter/components/RuntimeContext.js +0 -93
- package/dist/src/interpreter/components/TestRunner.d.ts +0 -19
- package/dist/src/interpreter/components/TestRunner.js +0 -110
- package/dist/src/interpreter/components/Visitor.d.ts +0 -71
- package/dist/src/interpreter/components/Visitor.js +0 -638
- package/dist/src/interpreter/components/logic/LogicEngine.d.ts +0 -29
- package/dist/src/interpreter/components/logic/LogicEngine.js +0 -259
- package/dist/src/interpreter/components/logic/LogicResolver.d.ts +0 -53
- package/dist/src/interpreter/components/logic/LogicResolver.js +0 -484
- package/dist/src/interpreter/components/logic/LogicTranslator.d.ts +0 -14
- package/dist/src/interpreter/components/logic/LogicTranslator.js +0 -99
- package/dist/src/interpreter/components/runtimes/FunctionRuntime.d.ts +0 -12
- package/dist/src/interpreter/components/runtimes/FunctionRuntime.js +0 -149
- package/dist/src/interpreter/components/runtimes/LazyRuntime.d.ts +0 -19
- package/dist/src/interpreter/components/runtimes/LazyRuntime.js +0 -269
- package/dist/src/interpreter/components/runtimes/ObjectRuntime.d.ts +0 -35
- package/dist/src/interpreter/components/runtimes/ObjectRuntime.js +0 -126
- package/dist/src/interpreter/errors.d.ts +0 -19
- package/dist/src/interpreter/errors.js +0 -36
- package/dist/src/interpreter/index.d.ts +0 -24
- package/dist/src/interpreter/index.js +0 -41
- package/dist/src/interpreter/trampoline.d.ts +0 -17
- package/dist/src/interpreter/trampoline.js +0 -38
- package/dist/src/interpreter/utils.d.ts +0 -11
- package/dist/src/interpreter/utils.js +0 -65
- package/dist/src/tester/index.d.ts +0 -25
- package/dist/src/tester/index.js +0 -113
- package/dist/src/utils/helpers.d.ts +0 -13
- package/dist/src/utils/helpers.js +0 -52
- package/src/interpreter/components/Visitor.ts +0 -1065
- package/src/interpreter/trampoline.ts +0 -71
|
@@ -1,259 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,53 +0,0 @@
|
|
|
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>;
|