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