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
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
import { YuString, YuNumber, YuNil, RuntimeObject, YuArray, YuBoolean, } from "../../primitives/index.js";
|
|
2
|
+
import { boolean, error, raise } from "../../utils.js";
|
|
3
|
+
import { StepCommand, BindCommand, } from "../kernel/commands.js";
|
|
4
|
+
import { LogicTerm, } from "../../primitives/entities/LogicResult.js";
|
|
5
|
+
function unifyArray(arr1, arr2, env) {
|
|
6
|
+
const next = (index) => {
|
|
7
|
+
if (index >= arr1.length)
|
|
8
|
+
return boolean(true);
|
|
9
|
+
return new BindCommand(arr1[index].unify(arr2[index], env), (res) => {
|
|
10
|
+
if (res instanceof YuBoolean && res.value) {
|
|
11
|
+
return next(index + 1);
|
|
12
|
+
}
|
|
13
|
+
return boolean(false);
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
return next(0);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Represents a logic variable with a unique numeric ID.
|
|
20
|
+
*/
|
|
21
|
+
export class VariableTerm extends LogicTerm {
|
|
22
|
+
id;
|
|
23
|
+
name;
|
|
24
|
+
logicTermType = "Variable";
|
|
25
|
+
constructor(id, name) {
|
|
26
|
+
super();
|
|
27
|
+
this.id = id;
|
|
28
|
+
this.name = name;
|
|
29
|
+
}
|
|
30
|
+
getType() {
|
|
31
|
+
return "Variable";
|
|
32
|
+
}
|
|
33
|
+
toJSON() {
|
|
34
|
+
return { type: "Variable", id: this.id, name: this.name };
|
|
35
|
+
}
|
|
36
|
+
resolve(env) {
|
|
37
|
+
let current = this;
|
|
38
|
+
const seen = new Set();
|
|
39
|
+
while (current.logicTermType === "Variable") {
|
|
40
|
+
const v = current;
|
|
41
|
+
if (seen.has(v.id))
|
|
42
|
+
break;
|
|
43
|
+
seen.add(v.id);
|
|
44
|
+
const bound = env.get(v.id);
|
|
45
|
+
if (!bound)
|
|
46
|
+
break;
|
|
47
|
+
current = bound;
|
|
48
|
+
}
|
|
49
|
+
return current;
|
|
50
|
+
}
|
|
51
|
+
unify(other, env) {
|
|
52
|
+
const r1 = this.resolve(env);
|
|
53
|
+
const r2 = other.resolve(env);
|
|
54
|
+
if (r1 === r2)
|
|
55
|
+
return boolean(true);
|
|
56
|
+
if (r1.logicTermType === "Variable") {
|
|
57
|
+
const v1 = r1;
|
|
58
|
+
if (r2.occurs(v1, env))
|
|
59
|
+
return boolean(false);
|
|
60
|
+
env.set(v1.id, r2);
|
|
61
|
+
return boolean(true);
|
|
62
|
+
}
|
|
63
|
+
if (r2.logicTermType === "Variable") {
|
|
64
|
+
const v2 = r2;
|
|
65
|
+
if (r1.occurs(v2, env))
|
|
66
|
+
return boolean(false);
|
|
67
|
+
env.set(v2.id, r1);
|
|
68
|
+
return boolean(true);
|
|
69
|
+
}
|
|
70
|
+
// If r1 resolved to a non-variable, delegate to its unify logic
|
|
71
|
+
return r1.unify(r2, env);
|
|
72
|
+
}
|
|
73
|
+
occurs(v, env) {
|
|
74
|
+
const resolved = this.resolve(env);
|
|
75
|
+
if (resolved.logicTermType === "Variable")
|
|
76
|
+
return resolved.id === v.id;
|
|
77
|
+
return resolved.occurs(v, env);
|
|
78
|
+
}
|
|
79
|
+
instantiate(env, seen = new Set()) {
|
|
80
|
+
let current = this;
|
|
81
|
+
const localSeen = new Set(seen);
|
|
82
|
+
while (current.logicTermType === "Variable") {
|
|
83
|
+
const v = current;
|
|
84
|
+
if (localSeen.has(v.id))
|
|
85
|
+
return current;
|
|
86
|
+
localSeen.add(v.id);
|
|
87
|
+
const bound = env.get(v.id);
|
|
88
|
+
if (!bound)
|
|
89
|
+
return current;
|
|
90
|
+
current = bound;
|
|
91
|
+
}
|
|
92
|
+
return current.instantiate(env, localSeen);
|
|
93
|
+
}
|
|
94
|
+
toPrimitive(env) {
|
|
95
|
+
const resolved = this.resolve(env);
|
|
96
|
+
if (resolved === this)
|
|
97
|
+
return new YuString(this.name);
|
|
98
|
+
return resolved.toPrimitive(env);
|
|
99
|
+
}
|
|
100
|
+
toString() {
|
|
101
|
+
return `${this.name}_${this.id}`;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Represents a constant value (Numbers, Strings, Booleans).
|
|
106
|
+
*/
|
|
107
|
+
export class ConstantTerm extends LogicTerm {
|
|
108
|
+
value;
|
|
109
|
+
logicTermType = "Constant";
|
|
110
|
+
constructor(value) {
|
|
111
|
+
super();
|
|
112
|
+
this.value = value;
|
|
113
|
+
}
|
|
114
|
+
equals(other) {
|
|
115
|
+
if (!(other instanceof ConstantTerm))
|
|
116
|
+
return boolean(false);
|
|
117
|
+
return this.value.equals(other.value);
|
|
118
|
+
}
|
|
119
|
+
compare(other) {
|
|
120
|
+
if (!(other instanceof ConstantTerm))
|
|
121
|
+
return raise(error("ConstantTerm", "Type mismatch in compare"));
|
|
122
|
+
return this.value.compare(other.value);
|
|
123
|
+
}
|
|
124
|
+
getType() {
|
|
125
|
+
return "Constant";
|
|
126
|
+
}
|
|
127
|
+
toJSON() {
|
|
128
|
+
return this.value.toJSON();
|
|
129
|
+
}
|
|
130
|
+
resolve() {
|
|
131
|
+
return this;
|
|
132
|
+
}
|
|
133
|
+
occurs() {
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
unify(other, env) {
|
|
137
|
+
const r2 = other.resolve(env);
|
|
138
|
+
if (r2.logicTermType === "Wildcard")
|
|
139
|
+
return boolean(true);
|
|
140
|
+
if (r2.logicTermType === "Variable") {
|
|
141
|
+
env.set(r2.id, this);
|
|
142
|
+
return boolean(true);
|
|
143
|
+
}
|
|
144
|
+
if (r2.logicTermType === "Constant") {
|
|
145
|
+
return this.value.equals(r2.value);
|
|
146
|
+
}
|
|
147
|
+
return boolean(false);
|
|
148
|
+
}
|
|
149
|
+
instantiate() {
|
|
150
|
+
return this;
|
|
151
|
+
}
|
|
152
|
+
toPrimitive() {
|
|
153
|
+
return this.value;
|
|
154
|
+
}
|
|
155
|
+
toString() {
|
|
156
|
+
return this.value.toString();
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Represents a compound structure (Functors, Constructors, Application).
|
|
161
|
+
*/
|
|
162
|
+
export class CompoundTerm extends LogicTerm {
|
|
163
|
+
functor;
|
|
164
|
+
args;
|
|
165
|
+
logicTermType = "Compound";
|
|
166
|
+
constructor(functor, args) {
|
|
167
|
+
super();
|
|
168
|
+
this.functor = functor;
|
|
169
|
+
this.args = args;
|
|
170
|
+
}
|
|
171
|
+
getType() {
|
|
172
|
+
return "Compound";
|
|
173
|
+
}
|
|
174
|
+
toJSON() {
|
|
175
|
+
return {
|
|
176
|
+
type: "Compound",
|
|
177
|
+
functor: this.functor,
|
|
178
|
+
args: this.args.map((a) => a.toJSON()),
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
resolve() {
|
|
182
|
+
return this;
|
|
183
|
+
}
|
|
184
|
+
occurs(v, env) {
|
|
185
|
+
return this.args.some((arg) => arg.occurs(v, env));
|
|
186
|
+
}
|
|
187
|
+
unify(other, env) {
|
|
188
|
+
const r2 = other.resolve(env);
|
|
189
|
+
if (r2.logicTermType === "Wildcard")
|
|
190
|
+
return boolean(true);
|
|
191
|
+
if (r2.logicTermType === "Variable") {
|
|
192
|
+
const v2 = r2;
|
|
193
|
+
if (this.occurs(v2, env))
|
|
194
|
+
return boolean(false);
|
|
195
|
+
env.set(v2.id, this);
|
|
196
|
+
return boolean(true);
|
|
197
|
+
}
|
|
198
|
+
if (r2.logicTermType === "Compound") {
|
|
199
|
+
const c2 = r2;
|
|
200
|
+
if (this.functor !== c2.functor)
|
|
201
|
+
return boolean(false);
|
|
202
|
+
if (this.args.length !== c2.args.length)
|
|
203
|
+
return boolean(false);
|
|
204
|
+
return unifyArray(this.args, c2.args, env);
|
|
205
|
+
}
|
|
206
|
+
return boolean(false);
|
|
207
|
+
}
|
|
208
|
+
instantiate(env, seen) {
|
|
209
|
+
return new CompoundTerm(this.functor, this.args.map((a) => a.instantiate(env, seen)));
|
|
210
|
+
}
|
|
211
|
+
toPrimitive(env) {
|
|
212
|
+
const args = this.args.map((a) => a.toPrimitive(env));
|
|
213
|
+
// represent as a RuntimeObject
|
|
214
|
+
return new RuntimeObject(this.functor, this.functor, new Map(args.map((v, i) => [`_${i}`, v])), new Map());
|
|
215
|
+
}
|
|
216
|
+
toString() {
|
|
217
|
+
return `${this.functor}(${this.args.map((a) => a.toString()).join(", ")})`;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Represents the wildcard pattern (_).
|
|
222
|
+
*/
|
|
223
|
+
export class WildcardTerm extends LogicTerm {
|
|
224
|
+
logicTermType = "Wildcard";
|
|
225
|
+
equals(other) {
|
|
226
|
+
return boolean(other instanceof WildcardTerm);
|
|
227
|
+
}
|
|
228
|
+
compare(other) {
|
|
229
|
+
return new StepCommand(new YuNumber(0));
|
|
230
|
+
}
|
|
231
|
+
getType() {
|
|
232
|
+
return "Wildcard";
|
|
233
|
+
}
|
|
234
|
+
toJSON() {
|
|
235
|
+
return "_";
|
|
236
|
+
}
|
|
237
|
+
resolve() {
|
|
238
|
+
return this;
|
|
239
|
+
}
|
|
240
|
+
occurs() {
|
|
241
|
+
return false;
|
|
242
|
+
}
|
|
243
|
+
unify(other, env) {
|
|
244
|
+
return boolean(true);
|
|
245
|
+
}
|
|
246
|
+
instantiate() {
|
|
247
|
+
return this;
|
|
248
|
+
}
|
|
249
|
+
toPrimitive() {
|
|
250
|
+
return YuNil.getInstance();
|
|
251
|
+
}
|
|
252
|
+
toString() {
|
|
253
|
+
return "_";
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Represents a List [x, y, z].
|
|
258
|
+
*/
|
|
259
|
+
export class ListTerm extends LogicTerm {
|
|
260
|
+
elements;
|
|
261
|
+
logicTermType = "List";
|
|
262
|
+
constructor(elements) {
|
|
263
|
+
super();
|
|
264
|
+
this.elements = elements;
|
|
265
|
+
}
|
|
266
|
+
getType() {
|
|
267
|
+
return "ListTerm";
|
|
268
|
+
}
|
|
269
|
+
toJSON() {
|
|
270
|
+
return this.elements.map((e) => e.toJSON());
|
|
271
|
+
}
|
|
272
|
+
resolve() {
|
|
273
|
+
return this;
|
|
274
|
+
}
|
|
275
|
+
occurs(v, env) {
|
|
276
|
+
return this.elements.some((el) => el.occurs(v, env));
|
|
277
|
+
}
|
|
278
|
+
unify(other, env) {
|
|
279
|
+
const r2 = other.resolve(env);
|
|
280
|
+
if (r2.logicTermType === "Wildcard")
|
|
281
|
+
return boolean(true);
|
|
282
|
+
if (r2.logicTermType === "Variable") {
|
|
283
|
+
const v2 = r2;
|
|
284
|
+
if (this.occurs(v2, env))
|
|
285
|
+
return boolean(false);
|
|
286
|
+
env.set(v2.id, this);
|
|
287
|
+
return boolean(true);
|
|
288
|
+
}
|
|
289
|
+
if (r2.logicTermType === "List") {
|
|
290
|
+
const l2 = r2;
|
|
291
|
+
if (this.elements.length !== l2.elements.length)
|
|
292
|
+
return boolean(false);
|
|
293
|
+
return unifyArray(this.elements, l2.elements, env);
|
|
294
|
+
}
|
|
295
|
+
if (r2.logicTermType === "Cons") {
|
|
296
|
+
// Delegate to ConsTerm.unify to leverage iterative unrolling
|
|
297
|
+
return r2.unify(this, env);
|
|
298
|
+
}
|
|
299
|
+
return boolean(false);
|
|
300
|
+
}
|
|
301
|
+
instantiate(env, seen) {
|
|
302
|
+
return new ListTerm(this.elements.map((e) => e.instantiate(env, seen)));
|
|
303
|
+
}
|
|
304
|
+
toPrimitive(env) {
|
|
305
|
+
return new YuArray(this.elements.map((e) => e.toPrimitive(env)));
|
|
306
|
+
}
|
|
307
|
+
toString() {
|
|
308
|
+
return `[${this.elements.map((e) => e.toString()).join(", ")}]`;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Represents a Cons cell (head:tail).
|
|
313
|
+
*/
|
|
314
|
+
export class ConsTerm extends LogicTerm {
|
|
315
|
+
head;
|
|
316
|
+
tail;
|
|
317
|
+
logicTermType = "Cons";
|
|
318
|
+
constructor(head, tail) {
|
|
319
|
+
super();
|
|
320
|
+
this.head = head;
|
|
321
|
+
this.tail = tail;
|
|
322
|
+
}
|
|
323
|
+
getType() {
|
|
324
|
+
return "ConsTerm";
|
|
325
|
+
}
|
|
326
|
+
toJSON() {
|
|
327
|
+
return { head: this.head.toJSON(), tail: this.tail.toJSON() };
|
|
328
|
+
}
|
|
329
|
+
resolve() {
|
|
330
|
+
return this;
|
|
331
|
+
}
|
|
332
|
+
occurs(v, env) {
|
|
333
|
+
let curr = this;
|
|
334
|
+
while (curr.logicTermType === "Cons") {
|
|
335
|
+
const c = curr;
|
|
336
|
+
if (c.head.occurs(v, env))
|
|
337
|
+
return true;
|
|
338
|
+
curr = c.tail.resolve(env);
|
|
339
|
+
}
|
|
340
|
+
return curr.occurs(v, env);
|
|
341
|
+
}
|
|
342
|
+
unify(other, env) {
|
|
343
|
+
const step = (curr1, curr2) => {
|
|
344
|
+
const r1 = curr1.resolve(env);
|
|
345
|
+
const r2 = curr2.resolve(env);
|
|
346
|
+
if (r1.logicTermType === "Cons") {
|
|
347
|
+
const c1 = r1;
|
|
348
|
+
if (r2.logicTermType === "Wildcard")
|
|
349
|
+
return boolean(true);
|
|
350
|
+
if (r2.logicTermType === "Variable") {
|
|
351
|
+
const v2 = r2;
|
|
352
|
+
if (c1.occurs(v2, env))
|
|
353
|
+
return boolean(false);
|
|
354
|
+
env.set(v2.id, c1);
|
|
355
|
+
return boolean(true);
|
|
356
|
+
}
|
|
357
|
+
if (r2.logicTermType === "Cons") {
|
|
358
|
+
const c2 = r2;
|
|
359
|
+
return new BindCommand(c1.head.unify(c2.head, env), (res) => {
|
|
360
|
+
if (res instanceof YuBoolean && res.value) {
|
|
361
|
+
return step(c1.tail, c2.tail);
|
|
362
|
+
}
|
|
363
|
+
return boolean(false);
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
if (r2.logicTermType === "List") {
|
|
367
|
+
const l2 = r2;
|
|
368
|
+
if (l2.elements.length === 0)
|
|
369
|
+
return boolean(false);
|
|
370
|
+
const [h, ...t] = l2.elements;
|
|
371
|
+
return new BindCommand(c1.head.unify(h, env), (res) => {
|
|
372
|
+
if (res instanceof YuBoolean && res.value) {
|
|
373
|
+
return step(c1.tail, new ListTerm(t));
|
|
374
|
+
}
|
|
375
|
+
return boolean(false);
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
return boolean(false);
|
|
379
|
+
}
|
|
380
|
+
return r1.unify(r2, env);
|
|
381
|
+
};
|
|
382
|
+
return step(this, other);
|
|
383
|
+
}
|
|
384
|
+
instantiate(env, seen) {
|
|
385
|
+
let curr = this;
|
|
386
|
+
const heads = [];
|
|
387
|
+
while (curr.logicTermType === "Cons") {
|
|
388
|
+
const c = curr;
|
|
389
|
+
heads.push(c.head.instantiate(env, seen));
|
|
390
|
+
curr = c.tail.resolve(env);
|
|
391
|
+
}
|
|
392
|
+
let result = curr.instantiate(env, seen);
|
|
393
|
+
for (let i = heads.length - 1; i >= 0; i--) {
|
|
394
|
+
result = new ConsTerm(heads[i], result);
|
|
395
|
+
}
|
|
396
|
+
return result;
|
|
397
|
+
}
|
|
398
|
+
toPrimitive(env) {
|
|
399
|
+
let curr = this;
|
|
400
|
+
const heads = [];
|
|
401
|
+
while (curr.logicTermType === "Cons") {
|
|
402
|
+
const c = curr;
|
|
403
|
+
heads.push(c.head.toPrimitive(env));
|
|
404
|
+
curr = c.tail.resolve(env);
|
|
405
|
+
}
|
|
406
|
+
const tailVal = curr.toPrimitive(env);
|
|
407
|
+
let res = tailVal;
|
|
408
|
+
for (let i = heads.length - 1; i >= 0; i--) {
|
|
409
|
+
const seq = res.asSequence;
|
|
410
|
+
if (seq && seq instanceof YuArray) {
|
|
411
|
+
res = new YuArray([heads[i], ...seq]);
|
|
412
|
+
}
|
|
413
|
+
else {
|
|
414
|
+
// Hybrid cons case?
|
|
415
|
+
res = new YuArray([heads[i], res]);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
return res;
|
|
419
|
+
}
|
|
420
|
+
toString() {
|
|
421
|
+
return `(${this.head.toString()}:${this.tail.toString()})`;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
@@ -1,14 +1,30 @@
|
|
|
1
|
-
import { Expression, Pattern
|
|
2
|
-
import {
|
|
3
|
-
import { ExpressionEvaluator } from "../../utils.js";
|
|
4
|
-
import { Continuation, Thunk } from "../../trampoline.js";
|
|
1
|
+
import { Expression, Pattern } from "yukigo-ast";
|
|
2
|
+
import { VariableTerm } from "./LogicTerm.js";
|
|
5
3
|
import { RuntimeContext } from "../RuntimeContext.js";
|
|
4
|
+
import { ExecutionCommand } from "../kernel/commands.js";
|
|
5
|
+
import { YuValue } from "../../primitives/YuValue.js";
|
|
6
|
+
import { LogicTerm, Substitution } from "../../primitives/index.js";
|
|
7
|
+
import { Evaluator } from "../evaluators/BaseEvaluator.js";
|
|
6
8
|
export declare class LogicTranslator {
|
|
7
9
|
private evaluator;
|
|
8
10
|
private ctx;
|
|
9
|
-
constructor(evaluator:
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
constructor(evaluator: Evaluator, ctx: RuntimeContext);
|
|
12
|
+
getNextId(name: string): number;
|
|
13
|
+
getName(id: number): string | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* Translates a Pattern (AST) into a LogicTerm (Runtime).
|
|
16
|
+
*/
|
|
17
|
+
patternToTerm(pat: Pattern, scope?: Map<string, VariableTerm>): LogicTerm;
|
|
18
|
+
/**
|
|
19
|
+
* Converts a PrimitiveValue to a LogicTerm.
|
|
20
|
+
*/
|
|
21
|
+
primitiveToTerm(val: YuValue): LogicTerm;
|
|
22
|
+
/**
|
|
23
|
+
* Evaluates an expression and returns its LogicTerm representation via the Kernel.
|
|
24
|
+
*/
|
|
25
|
+
expressionToTerm(expr: Expression, scope?: Map<string, VariableTerm>): ExecutionCommand;
|
|
26
|
+
/**
|
|
27
|
+
* Instantiates an expression as a logic term, resolving logic variables.
|
|
28
|
+
*/
|
|
29
|
+
instantiateExpressionAsTerm(expr: Expression, substs: Substitution, scope?: Map<string, VariableTerm>): ExecutionCommand;
|
|
14
30
|
}
|