@rsconcept/domain 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (224) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +55 -0
  3. package/dist/cctext/index.d.ts +1 -0
  4. package/dist/cctext/index.js +42 -0
  5. package/dist/cctext/index.js.map +1 -0
  6. package/dist/cctext/language-api.d.ts +43 -0
  7. package/dist/cctext/language-api.js +252 -0
  8. package/dist/cctext/language-api.js.map +1 -0
  9. package/dist/cctext/language.d.ts +58 -0
  10. package/dist/cctext/language.js +44 -0
  11. package/dist/cctext/language.js.map +1 -0
  12. package/dist/graph/graph.d.ts +62 -0
  13. package/dist/graph/graph.js +385 -0
  14. package/dist/graph/graph.js.map +1 -0
  15. package/dist/graph/index.d.ts +1 -0
  16. package/dist/graph/index.js +384 -0
  17. package/dist/graph/index.js.map +1 -0
  18. package/dist/index.d.ts +28 -0
  19. package/dist/index.js +5851 -0
  20. package/dist/index.js.map +1 -0
  21. package/dist/library/folder-tree.d.ts +32 -0
  22. package/dist/library/folder-tree.js +136 -0
  23. package/dist/library/folder-tree.js.map +1 -0
  24. package/dist/library/index.d.ts +17 -0
  25. package/dist/library/index.js +2800 -0
  26. package/dist/library/index.js.map +1 -0
  27. package/dist/library/library-api.d.ts +6 -0
  28. package/dist/library/library-api.js +13 -0
  29. package/dist/library/library-api.js.map +1 -0
  30. package/dist/library/library.d.ts +56 -0
  31. package/dist/library/library.js +23 -0
  32. package/dist/library/library.js.map +1 -0
  33. package/dist/library/oss-api.d.ts +47 -0
  34. package/dist/library/oss-api.js +1105 -0
  35. package/dist/library/oss-api.js.map +1 -0
  36. package/dist/library/oss-layout-api.d.ts +36 -0
  37. package/dist/library/oss-layout-api.js +330 -0
  38. package/dist/library/oss-layout-api.js.map +1 -0
  39. package/dist/library/oss-layout.d.ts +25 -0
  40. package/dist/library/oss-layout.js +1 -0
  41. package/dist/library/oss-layout.js.map +1 -0
  42. package/dist/library/oss.d.ts +136 -0
  43. package/dist/library/oss.js +30 -0
  44. package/dist/library/oss.js.map +1 -0
  45. package/dist/library/rsengine.d.ts +116 -0
  46. package/dist/library/rsengine.js +2604 -0
  47. package/dist/library/rsengine.js.map +1 -0
  48. package/dist/library/rsform-api.d.ts +74 -0
  49. package/dist/library/rsform-api.js +879 -0
  50. package/dist/library/rsform-api.js.map +1 -0
  51. package/dist/library/rsform.d.ts +206 -0
  52. package/dist/library/rsform.js +32 -0
  53. package/dist/library/rsform.js.map +1 -0
  54. package/dist/library/rsmodel-api.d.ts +43 -0
  55. package/dist/library/rsmodel-api.js +836 -0
  56. package/dist/library/rsmodel-api.js.map +1 -0
  57. package/dist/library/rsmodel.d.ts +52 -0
  58. package/dist/library/rsmodel.js +25 -0
  59. package/dist/library/rsmodel.js.map +1 -0
  60. package/dist/library/structure-planner.d.ts +33 -0
  61. package/dist/library/structure-planner.js +481 -0
  62. package/dist/library/structure-planner.js.map +1 -0
  63. package/dist/parsing/ast.d.ts +49 -0
  64. package/dist/parsing/ast.js +93 -0
  65. package/dist/parsing/ast.js.map +1 -0
  66. package/dist/parsing/index.d.ts +3 -0
  67. package/dist/parsing/index.js +141 -0
  68. package/dist/parsing/index.js.map +1 -0
  69. package/dist/parsing/lezer-tree.d.ts +13 -0
  70. package/dist/parsing/lezer-tree.js +50 -0
  71. package/dist/parsing/lezer-tree.js.map +1 -0
  72. package/dist/rslang/api.d.ts +53 -0
  73. package/dist/rslang/api.js +846 -0
  74. package/dist/rslang/api.js.map +1 -0
  75. package/dist/rslang/ast-annotations.d.ts +18 -0
  76. package/dist/rslang/ast-annotations.js +56 -0
  77. package/dist/rslang/ast-annotations.js.map +1 -0
  78. package/dist/rslang/error.d.ts +85 -0
  79. package/dist/rslang/error.js +159 -0
  80. package/dist/rslang/error.js.map +1 -0
  81. package/dist/rslang/eval/calculator.d.ts +43 -0
  82. package/dist/rslang/eval/calculator.js +1639 -0
  83. package/dist/rslang/eval/calculator.js.map +1 -0
  84. package/dist/rslang/eval/evaluation-cache.d.ts +36 -0
  85. package/dist/rslang/eval/evaluation-cache.js +310 -0
  86. package/dist/rslang/eval/evaluation-cache.js.map +1 -0
  87. package/dist/rslang/eval/evaluator.d.ts +70 -0
  88. package/dist/rslang/eval/evaluator.js +1514 -0
  89. package/dist/rslang/eval/evaluator.js.map +1 -0
  90. package/dist/rslang/eval/value-api.d.ts +48 -0
  91. package/dist/rslang/eval/value-api.js +490 -0
  92. package/dist/rslang/eval/value-api.js.map +1 -0
  93. package/dist/rslang/eval/value.d.ts +36 -0
  94. package/dist/rslang/eval/value.js +118 -0
  95. package/dist/rslang/eval/value.js.map +1 -0
  96. package/dist/rslang/index.d.ts +17 -0
  97. package/dist/rslang/index.js +4314 -0
  98. package/dist/rslang/index.js.map +1 -0
  99. package/dist/rslang/labels.d.ts +16 -0
  100. package/dist/rslang/labels.js +315 -0
  101. package/dist/rslang/labels.js.map +1 -0
  102. package/dist/rslang/parser/expression-generator.d.ts +10 -0
  103. package/dist/rslang/parser/expression-generator.js +451 -0
  104. package/dist/rslang/parser/expression-generator.js.map +1 -0
  105. package/dist/rslang/parser/normalize.d.ts +11 -0
  106. package/dist/rslang/parser/normalize.js +507 -0
  107. package/dist/rslang/parser/normalize.js.map +1 -0
  108. package/dist/rslang/parser/parser.d.ts +5 -0
  109. package/dist/rslang/parser/parser.js +24 -0
  110. package/dist/rslang/parser/parser.js.map +1 -0
  111. package/dist/rslang/parser/parser.terms.d.ts +42 -0
  112. package/dist/rslang/parser/parser.terms.js +84 -0
  113. package/dist/rslang/parser/parser.terms.js.map +1 -0
  114. package/dist/rslang/parser/syntax-errors.d.ts +11 -0
  115. package/dist/rslang/parser/syntax-errors.js +403 -0
  116. package/dist/rslang/parser/syntax-errors.js.map +1 -0
  117. package/dist/rslang/parser/token.d.ts +79 -0
  118. package/dist/rslang/parser/token.js +95 -0
  119. package/dist/rslang/parser/token.js.map +1 -0
  120. package/dist/rslang/semantic/analyzer.d.ts +39 -0
  121. package/dist/rslang/semantic/analyzer.js +2604 -0
  122. package/dist/rslang/semantic/analyzer.js.map +1 -0
  123. package/dist/rslang/semantic/arguments-extractor.d.ts +42 -0
  124. package/dist/rslang/semantic/arguments-extractor.js +366 -0
  125. package/dist/rslang/semantic/arguments-extractor.js.map +1 -0
  126. package/dist/rslang/semantic/type-auditor.d.ts +73 -0
  127. package/dist/rslang/semantic/type-auditor.js +1570 -0
  128. package/dist/rslang/semantic/type-auditor.js.map +1 -0
  129. package/dist/rslang/semantic/typification-api.d.ts +27 -0
  130. package/dist/rslang/semantic/typification-api.js +320 -0
  131. package/dist/rslang/semantic/typification-api.js.map +1 -0
  132. package/dist/rslang/semantic/typification-parser.d.ts +12 -0
  133. package/dist/rslang/semantic/typification-parser.js +226 -0
  134. package/dist/rslang/semantic/typification-parser.js.map +1 -0
  135. package/dist/rslang/semantic/typification.d.ts +119 -0
  136. package/dist/rslang/semantic/typification.js +74 -0
  137. package/dist/rslang/semantic/typification.js.map +1 -0
  138. package/dist/rslang/semantic/value-auditor.d.ts +43 -0
  139. package/dist/rslang/semantic/value-auditor.js +523 -0
  140. package/dist/rslang/semantic/value-auditor.js.map +1 -0
  141. package/dist/rslang/semantic/value-class.d.ts +10 -0
  142. package/dist/rslang/semantic/value-class.js +9 -0
  143. package/dist/rslang/semantic/value-class.js.map +1 -0
  144. package/dist/rslang/typification-graph.d.ts +33 -0
  145. package/dist/rslang/typification-graph.js +311 -0
  146. package/dist/rslang/typification-graph.js.map +1 -0
  147. package/dist/shared/branded.d.ts +7 -0
  148. package/dist/shared/branded.js +1 -0
  149. package/dist/shared/branded.js.map +1 -0
  150. package/dist/shared/hash.d.ts +6 -0
  151. package/dist/shared/hash.js +18 -0
  152. package/dist/shared/hash.js.map +1 -0
  153. package/dist/shared/index.d.ts +2 -0
  154. package/dist/shared/index.js +18 -0
  155. package/dist/shared/index.js.map +1 -0
  156. package/package.json +184 -0
  157. package/src/cctext/index.ts +9 -0
  158. package/src/cctext/language-api.test.ts +149 -0
  159. package/src/cctext/language-api.ts +285 -0
  160. package/src/cctext/language.ts +80 -0
  161. package/src/graph/graph.test.ts +392 -0
  162. package/src/graph/graph.ts +433 -0
  163. package/src/graph/index.ts +1 -0
  164. package/src/index.ts +96 -0
  165. package/src/library/folder-tree.test.ts +47 -0
  166. package/src/library/folder-tree.ts +156 -0
  167. package/src/library/index.ts +46 -0
  168. package/src/library/library-api.test.ts +32 -0
  169. package/src/library/library-api.ts +11 -0
  170. package/src/library/library.ts +61 -0
  171. package/src/library/oss-api.ts +449 -0
  172. package/src/library/oss-layout-api.ts +377 -0
  173. package/src/library/oss-layout.ts +27 -0
  174. package/src/library/oss.ts +150 -0
  175. package/src/library/rsengine.ts +593 -0
  176. package/src/library/rsform-api.ts +533 -0
  177. package/src/library/rsform.ts +228 -0
  178. package/src/library/rsmodel-api.ts +340 -0
  179. package/src/library/rsmodel.ts +50 -0
  180. package/src/library/structure-planner.ts +143 -0
  181. package/src/parsing/ast.ts +136 -0
  182. package/src/parsing/index.ts +15 -0
  183. package/src/parsing/lezer-tree.ts +69 -0
  184. package/src/rslang/api.test.ts +116 -0
  185. package/src/rslang/api.ts +183 -0
  186. package/src/rslang/ast-annotations.ts +70 -0
  187. package/src/rslang/error.ts +129 -0
  188. package/src/rslang/eval/calculator.test.ts +124 -0
  189. package/src/rslang/eval/calculator.ts +121 -0
  190. package/src/rslang/eval/evaluation-cache.ts +257 -0
  191. package/src/rslang/eval/evaluator.test.ts +352 -0
  192. package/src/rslang/eval/evaluator.ts +935 -0
  193. package/src/rslang/eval/value-api.test.ts +105 -0
  194. package/src/rslang/eval/value-api.ts +444 -0
  195. package/src/rslang/eval/value.ts +102 -0
  196. package/src/rslang/index.ts +23 -0
  197. package/src/rslang/labels.ts +191 -0
  198. package/src/rslang/parser/expression-generator.test.ts +100 -0
  199. package/src/rslang/parser/expression-generator.ts +466 -0
  200. package/src/rslang/parser/normalize.test.ts +99 -0
  201. package/src/rslang/parser/normalize.ts +462 -0
  202. package/src/rslang/parser/parser.terms.ts +42 -0
  203. package/src/rslang/parser/parser.test.ts +153 -0
  204. package/src/rslang/parser/parser.ts +20 -0
  205. package/src/rslang/parser/rslang.grammar +251 -0
  206. package/src/rslang/parser/syntax-errors.ts +209 -0
  207. package/src/rslang/parser/token.ts +106 -0
  208. package/src/rslang/semantic/analyzer.test.ts +59 -0
  209. package/src/rslang/semantic/analyzer.ts +179 -0
  210. package/src/rslang/semantic/arguments-extractor.ts +327 -0
  211. package/src/rslang/semantic/type-auditor.test.ts +326 -0
  212. package/src/rslang/semantic/type-auditor.ts +1049 -0
  213. package/src/rslang/semantic/typification-api.test.ts +46 -0
  214. package/src/rslang/semantic/typification-api.ts +321 -0
  215. package/src/rslang/semantic/typification-parser.test.ts +50 -0
  216. package/src/rslang/semantic/typification-parser.ts +220 -0
  217. package/src/rslang/semantic/typification.ts +180 -0
  218. package/src/rslang/semantic/value-auditor.test.ts +206 -0
  219. package/src/rslang/semantic/value-auditor.ts +332 -0
  220. package/src/rslang/semantic/value-class.ts +11 -0
  221. package/src/rslang/typification-graph.ts +155 -0
  222. package/src/shared/branded.ts +6 -0
  223. package/src/shared/hash.ts +17 -0
  224. package/src/shared/index.ts +2 -0
@@ -0,0 +1,352 @@
1
+ import { beforeEach, describe, expect, it } from 'vitest';
2
+
3
+ import { type AstNode, buildTree, visitAstDFS } from '../../parsing';
4
+ import { readErrorAnnotation } from '../ast-annotations';
5
+ import { RSErrorCode, type RSErrorDescription } from '../error';
6
+ import { normalizeAST } from '../parser/normalize';
7
+ import { parser as rslangParser } from '../parser/parser';
8
+ import { TokenID } from '../parser/token';
9
+
10
+ import { EvaluationMetadata } from './evaluation-cache';
11
+ import { type ASTContext, Evaluator } from './evaluator';
12
+ import { BOOL_INFINITY, TUPLE_ID, type ValueContext } from './value';
13
+ import { printValue } from './value-api';
14
+
15
+ // Helper to build AST
16
+ function buildAST(expression: string) {
17
+ const tree = rslangParser.parse(expression);
18
+ const ast = buildTree(tree.cursor());
19
+ normalizeAST(ast, expression);
20
+ return ast;
21
+ }
22
+
23
+ function setupValueContext(context: ValueContext): void {
24
+ context.set('X1', [1, 2, 3]);
25
+ context.set('X2', [1, 2, 3]);
26
+ context.set('S1', [1, 2]);
27
+ context.set('S2', [[1, 2, 3], []]);
28
+ context.set('S3', [[TUPLE_ID, 1, 1]]);
29
+ context.set('S4', 1);
30
+ }
31
+
32
+ function setupTreeContext(treeContext: ASTContext): void {
33
+ treeContext.set('F1', buildAST('[a∈ℬ(R1), b∈ℬ(R1×R2)] a∩Pr1(b)'));
34
+ }
35
+
36
+ function findFirstNode(ast: AstNode, typeID: number): AstNode | null {
37
+ let found: AstNode | null = null;
38
+ visitAstDFS(ast, node => {
39
+ if (found === null && node.typeID === typeID) {
40
+ found = node;
41
+ }
42
+ });
43
+ return found;
44
+ }
45
+
46
+ const correctValuesData = [
47
+ // Literals and global identifiers
48
+ ['1', '1'],
49
+ ['X1', '{1, 2, 3}'],
50
+ ['∅', '{}'],
51
+
52
+ // Functions
53
+
54
+ // Integral
55
+ ['1+2', '3'],
56
+ ['2*2', '4'],
57
+ ['4-11', '-7'],
58
+ ['card(X1)', '3'],
59
+ ['card(∅)', '0'],
60
+ // Logical predicates
61
+ ['2+2=4', '1'],
62
+ ['1=1', '1'],
63
+ ['1≠1', '0'],
64
+ ['1<1', '0'],
65
+ ['1≤1', '1'],
66
+ ['1>1', '0'],
67
+ ['1≥1', '1'],
68
+ ['1<2', '1'],
69
+ ['2>1', '1'],
70
+ ['¬1=2', '1'],
71
+ // Logical operations
72
+ ['¬1=1', '0'],
73
+ ['1=1 & 1=1', '1'],
74
+ ['1=2 & 1=1', '0'],
75
+ ['1=1 & 1=2', '0'],
76
+ ['1=2 & 1=2', '0'],
77
+ ['1=1 ∨ 1=1', '1'],
78
+ ['1=2 ∨ 1=1', '1'],
79
+ ['1=1 ∨ 1=2', '1'],
80
+ ['1=2 ∨ 1=2', '0'],
81
+ ['1=1 ⇒ 1=1', '1'],
82
+ ['1=2 ⇒ 1=1', '1'],
83
+ ['1=1 ⇒ 1=2', '0'],
84
+ ['1=2 ⇒ 1=2', '1'],
85
+ ['1=1 ⇔ 1=1', '1'],
86
+ ['1=2 ⇔ 1=1', '0'],
87
+ ['1=1 ⇔ 1=2', '0'],
88
+ ['1=2 ⇔ 1=2', '1'],
89
+ // Quantifiers
90
+ ['∀a∈X1 a=a', '1'],
91
+ ['∀a,b∈X1 a=b', '0'],
92
+ ['∃a,b∈X1 a=b', '1'],
93
+ ['∃(a,b)∈S3 (a∈X1 & b∈X2)', '1'],
94
+ ['∀a∈X1 a≠a', '0'],
95
+ ['∀a∈(X1\\X1) a=a', '1'],
96
+ ['∀a∈(X1\\X1) a≠a', '1'],
97
+ ['∀a∈X1 a∈S1', '0'],
98
+ ['∃a∈X1 a=a', '1'],
99
+ ['∃a∈X1 a≠a', '0'],
100
+ ['∃a∈(X1\\X1) a=a', '0'],
101
+ ['∃a∈(X1\\X1) a≠a', '0'],
102
+ ['∃a∈X1 a∈S1', '1'],
103
+ ['debool({X1})=X1', '1'],
104
+ ['∀a∈X1 debool({a})=a', '1'],
105
+ ['∀a∈X1×X1 debool({a})=a', '1'],
106
+ // Set predicates
107
+ ['X1=X1', '1'],
108
+ ['X1≠X1', '0'],
109
+ ['X1=∅', '0'],
110
+ ['S4∈X1', '1'],
111
+ ['X1≠X1', '0'],
112
+ ['X1≠S1', '1'],
113
+ ['S1≠X1', '1'],
114
+ ['X1∈S2', '1'],
115
+ ['S1∈S2', '0'],
116
+ ['X1∉S2', '0'],
117
+ ['S1∉S2', '1'],
118
+ ['X1⊂X1', '0'],
119
+ ['S1⊂X1', '1'],
120
+ ['X1⊂S1', '0'],
121
+ ['X1⊄X1', '1'],
122
+ ['S1⊄X1', '0'],
123
+ ['X1⊄S1', '1'],
124
+ ['X1⊆X1', '1'],
125
+ ['S1⊆X1', '1'],
126
+ ['X1⊆S1', '0'],
127
+ // Constructors
128
+ ['(1,2)', '(1, 2)'],
129
+ ['{1,2}', '{1, 2}'],
130
+ ['X1×∅', '{}'],
131
+ ['X1×(X2\\X2)', '{}'],
132
+ ['(X1\\X1)×X2', '{}'],
133
+ ['X1×X2', '{(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)}'],
134
+ ['ℬ(∅)', '{{}}'],
135
+ ['ℬ(X1)', '{{}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}}'],
136
+ ['ℬ(X1\\X1)', '{{}}'],
137
+ ['(1,2)', '(1, 2)'],
138
+ ['(X1,2)', '({1, 2, 3}, 2)'],
139
+ ['{X1}', '{{1, 2, 3}}'],
140
+ ['{X1, X1}', '{{1, 2, 3}}'],
141
+ ['{X1, S1}', '{{1, 2}, {1, 2, 3}}'],
142
+ ['{X1, S1, X1}', '{{1, 2}, {1, 2, 3}}'],
143
+ ['(X1, X1)', '({1, 2, 3}, {1, 2, 3})'],
144
+ ['(X1, X2, X1)', '({1, 2, 3}, {1, 2, 3}, {1, 2, 3})'],
145
+ ['D{a∈X1|1=1}', '{1, 2, 3}'],
146
+ ['D{a∈X1|a≠a}', '{}'],
147
+ ['D{(a,b)∈S3|a∈X1}', '{(1, 1)}'],
148
+ ['I{(a, b) | a:∈X1; b:=a; b≠a}', '{}'],
149
+ ['I{a | a:∈X1}', '{1, 2, 3}'],
150
+ ['I{a | (a, b):∈X1×X1; b=b}', '{1, 2, 3}'],
151
+ ['red(I{σ | α:∈S2 ; β:∈α ; σ:={β}})', '{1, 2, 3}'],
152
+ ['R{a:=X1 | a\\a}', '{}'],
153
+ ['R{a:=∅ | a∪X1}', '{1, 2, 3}'],
154
+ ['R{a:=X1\\X1 | a∪X1}', '{1, 2, 3}'],
155
+ ['R{(a,b):=(∅,0) | b < 3 | (a∪X1, b+1)}', '({1, 2, 3}, 3)'],
156
+ ['R{ξ:=2 | ξ≥2 | ξ-2}', '0'],
157
+ ['R{ξ:=3 | ξ≥2 | ξ-2}', '1'],
158
+ // Set operations
159
+ ['X1∪X1', '{1, 2, 3}'],
160
+ ['X1∪∅', '{1, 2, 3}'],
161
+ ['X1∪S1', '{1, 2, 3}'],
162
+ ['S1∪X1', '{1, 2, 3}'],
163
+ ['X1∩X1', '{1, 2, 3}'],
164
+ ['X1∩∅', '{}'],
165
+ ['X1∩S1', '{1, 2}'],
166
+ ['S1∩X1', '{1, 2}'],
167
+ ['X1\\X1', '{}'],
168
+ ['X1\\S1', '{3}'],
169
+ ['S1\\X1', '{}'],
170
+ ['X1∆X1', '{}'],
171
+ ['X1∆S1', '{3}'],
172
+ ['S1∆X1', '{3}'],
173
+ ['bool(X1)', '{{1, 2, 3}}'],
174
+ ['bool(X1\\X1)', '{{}}'],
175
+ ['debool(bool(X1))', '{1, 2, 3}'],
176
+ ['red(S2)', '{1, 2, 3}'],
177
+ ['red(S2\\S2)', '{}'],
178
+ ['pr1((1, 2))', '1'],
179
+ ['pr2((1, 2))', '2'],
180
+ ['pr2,1((1, 2))', '(2, 1)'],
181
+ ['Pr1(S3)', '{1}'],
182
+ ['Pr1({(1,2),(1,3)})', '{1}'],
183
+ ['Pr2(S3)', '{1}'],
184
+ ['Pr1(S3\\S3)', '{}'],
185
+ ['Pr1,2(S3\\S3)', '{}'],
186
+ ['Fi1[X1](S3)', '{(1, 1)}'],
187
+ ['Fi2,1[X2,X1](S3)', '{(1, 1)}'],
188
+ ['Fi1[(X1\\X1)](S3)', '{}'],
189
+ ['Fi1[X1](S3\\S3)', '{}']
190
+ ];
191
+
192
+ const errorData = [
193
+ ['[a∈ℬ(R1)] a=a', { code: RSErrorCode.calculationNotSupported, from: 0, to: 13 }],
194
+ ['debool(X1)', { code: RSErrorCode.calcInvalidDebool, from: 7, to: 9 }],
195
+ ['∀a∈Z a=a', { code: RSErrorCode.iterateInfinity, from: 3, to: 4 }],
196
+ ['ℬℬ({1,2,3,4,5})', { code: RSErrorCode.booleanBaseLimit, from: 1, to: 15, params: [String(BOOL_INFINITY)] }],
197
+ ['X1\\D1', { code: RSErrorCode.calcGlobalMissing, from: 3, to: 5, params: ['D1'] }],
198
+ ['D1\\X1', { code: RSErrorCode.calcGlobalMissing, from: 0, to: 2, params: ['D1'] }],
199
+ ['¬D1\\X1=X1', { code: RSErrorCode.calcGlobalMissing, from: 1, to: 3, params: ['D1'] }],
200
+ ['card(D1)=1', { code: RSErrorCode.calcGlobalMissing, from: 5, to: 7, params: ['D1'] }],
201
+ ['1=card(D1)', { code: RSErrorCode.calcGlobalMissing, from: 7, to: 9, params: ['D1'] }],
202
+ ['card(D1)+1', { code: RSErrorCode.calcGlobalMissing, from: 5, to: 7, params: ['D1'] }],
203
+ ['1+card(D1)', { code: RSErrorCode.calcGlobalMissing, from: 7, to: 9, params: ['D1'] }],
204
+ ['card(D1)>1', { code: RSErrorCode.calcGlobalMissing, from: 5, to: 7, params: ['D1'] }],
205
+ ['1>card(D1)', { code: RSErrorCode.calcGlobalMissing, from: 7, to: 9, params: ['D1'] }],
206
+ ['card(D1)=1 & 1=1', { code: RSErrorCode.calcGlobalMissing, from: 5, to: 7, params: ['D1'] }],
207
+ ['1=1 & card(D1)=1', { code: RSErrorCode.calcGlobalMissing, from: 11, to: 13, params: ['D1'] }],
208
+ ['∀a∈D1 a=a', { code: RSErrorCode.calcGlobalMissing, from: 3, to: 5, params: ['D1'] }],
209
+ ['∀a∈X1 a∈D1', { code: RSErrorCode.calcGlobalMissing, from: 8, to: 10, params: ['D1'] }],
210
+ ['X1×D1', { code: RSErrorCode.calcGlobalMissing, from: 3, to: 5, params: ['D1'] }],
211
+ ['D1×X1', { code: RSErrorCode.calcGlobalMissing, from: 0, to: 2, params: ['D1'] }],
212
+ ['ℬ(D1)', { code: RSErrorCode.calcGlobalMissing, from: 2, to: 4, params: ['D1'] }],
213
+ ['pr1(D1)', { code: RSErrorCode.calcGlobalMissing, from: 4, to: 6, params: ['D1'] }],
214
+ ['Pr1(D1)', { code: RSErrorCode.calcGlobalMissing, from: 4, to: 6, params: ['D1'] }],
215
+ ['bool(D1)', { code: RSErrorCode.calcGlobalMissing, from: 5, to: 7, params: ['D1'] }],
216
+ ['debool(D1)', { code: RSErrorCode.calcGlobalMissing, from: 7, to: 9, params: ['D1'] }],
217
+ ['red(D1)', { code: RSErrorCode.calcGlobalMissing, from: 4, to: 6, params: ['D1'] }],
218
+ ['{D1}', { code: RSErrorCode.calcGlobalMissing, from: 1, to: 3, params: ['D1'] }],
219
+ ['(D1, X1)', { code: RSErrorCode.calcGlobalMissing, from: 1, to: 3, params: ['D1'] }],
220
+ ['D{t∈D1 | t=t}', { code: RSErrorCode.calcGlobalMissing, from: 4, to: 6, params: ['D1'] }],
221
+ ['D{t∈X1 | t∈D1}', { code: RSErrorCode.calcGlobalMissing, from: 11, to: 13, params: ['D1'] }],
222
+ ['D{a∈D1|a=X1}', { code: RSErrorCode.calcGlobalMissing, from: 4, to: 6, params: ['D1'] }],
223
+ ['D{a∈X1|a=D1}', { code: RSErrorCode.calcGlobalMissing, from: 9, to: 11, params: ['D1'] }],
224
+ ['R{a:=D1 | a∪X1}', { code: RSErrorCode.calcGlobalMissing, from: 5, to: 7, params: ['D1'] }],
225
+ ['R{a:=X1 | a∪D1}', { code: RSErrorCode.calcGlobalMissing, from: 12, to: 14, params: ['D1'] }],
226
+ ['R{a:=X1 | 1=1 | a∪D1}', { code: RSErrorCode.calcGlobalMissing, from: 18, to: 20, params: ['D1'] }],
227
+ ['R{a:=X1 | D1=D1 | a∪X1}', { code: RSErrorCode.calcGlobalMissing, from: 10, to: 12, params: ['D1'] }],
228
+ ['I{(a,b) | a:∈D1; b:=a; b≠a}', { code: RSErrorCode.calcGlobalMissing, from: 13, to: 15, params: ['D1'] }],
229
+ ['I{(a,b) | a:∈X1; b:=a; b≠D1}', { code: RSErrorCode.calcGlobalMissing, from: 25, to: 27, params: ['D1'] }],
230
+ ['I{(a,b) | a:∈X1; b:=D1; b≠a}', { code: RSErrorCode.calcGlobalMissing, from: 20, to: 22, params: ['D1'] }]
231
+ ];
232
+
233
+ describe('Calculator', () => {
234
+ const context: ValueContext = new Map();
235
+ const treeContext: ASTContext = new Map();
236
+ let calculator: Evaluator;
237
+ let errors: RSErrorDescription[];
238
+
239
+ beforeEach(() => {
240
+ calculator = new Evaluator(context, treeContext);
241
+ setupTreeContext(treeContext);
242
+ setupValueContext(context);
243
+ errors = [];
244
+ });
245
+
246
+ function expectValue(input: string, expectedValue: string) {
247
+ const ast = buildAST(input);
248
+ expect(ast.hasError).toBe(false);
249
+ const result = calculator.run(ast, error => errors.push(error));
250
+ expect(printValue(result)).toBe(expectedValue);
251
+ }
252
+
253
+ function expectError(input: string, expectedError: RSErrorDescription) {
254
+ const ast = buildAST(input);
255
+ expect(ast.hasError).toBe(false);
256
+ expect(errors.length).toBe(0);
257
+
258
+ calculator.run(ast, error => errors.push(error));
259
+ expect(errors.length).toBe(1);
260
+ expect(errors[0]).toMatchObject(expectedError);
261
+ }
262
+
263
+ // .filter(([input]) => input === 'red(I{σ | α:∈S2 ; β:∈α ; σ:={β}})')
264
+ correctValuesData.forEach(([input, expectedValue]) => {
265
+ it(`Correct value for "${input}"`, () => expectValue(input, expectedValue));
266
+ });
267
+
268
+ errorData.forEach(([input, expectedError]) => {
269
+ it(`Error for "${input as string}"`, () => expectError(input as string, expectedError as RSErrorDescription));
270
+ });
271
+
272
+ it('Function', () => {
273
+ const funcAst = buildAST('[a∈ℬ(R1), b∈Z] a\\a');
274
+ treeContext.set('F1', funcAst);
275
+ expectValue('F1[F1[X1, 0], 1]', '{}');
276
+ });
277
+
278
+ describe('error annotation', () => {
279
+ it('attributes evaluation errors inside a function body to the call site in the main expression', () => {
280
+ const funcAst = buildAST('[a∈X1] ∀b∈Z b=b');
281
+ treeContext.set('F9', funcAst);
282
+ const mainAst = buildAST('F9[X1]');
283
+ const callSite = findFirstNode(mainAst, TokenID.NT_FUNC_CALL);
284
+ expect(callSite).not.toBeNull();
285
+
286
+ const funcExpr = '[a∈X1] ∀b∈Z b=b';
287
+ const zOffset = funcExpr.indexOf('Z');
288
+ let innerErrorNode: AstNode | null = null;
289
+ visitAstDFS(funcAst, node => {
290
+ if (
291
+ innerErrorNode === null &&
292
+ node.typeID === TokenID.LIT_WHOLE_NUMBERS &&
293
+ node.from === zOffset &&
294
+ node.to === zOffset + 1
295
+ ) {
296
+ innerErrorNode = node;
297
+ }
298
+ });
299
+ expect(innerErrorNode).not.toBeNull();
300
+
301
+ calculator.run(mainAst, error => errors.push(error), true);
302
+
303
+ expect(errors).toHaveLength(1);
304
+ expect(errors[0]).toMatchObject({
305
+ code: RSErrorCode.iterateInfinity,
306
+ from: callSite!.from,
307
+ to: callSite!.to
308
+ });
309
+ expect(readErrorAnnotation(callSite!)).toMatchObject({ code: RSErrorCode.iterateInfinity });
310
+ expect(readErrorAnnotation(innerErrorNode!)).toBeNull();
311
+ });
312
+ });
313
+
314
+ describe('evaluation cache', () => {
315
+ beforeEach(() => {
316
+ treeContext.set('F9', buildAST('[a∈ℬ(X1)] a'));
317
+ });
318
+
319
+ function runWithHits(input: string, disableCache: boolean = false): { value: string; hits: number } {
320
+ const ast = buildAST(input);
321
+ expect(ast.hasError).toBe(false);
322
+ errors = [];
323
+ const result = calculator.run(ast, error => errors.push(error), false, disableCache);
324
+ expect(errors.length).toBe(0);
325
+ return { value: printValue(result), hits: calculator.cacheHits };
326
+ }
327
+
328
+ it('reuses cached subexpression while loop variables are unchanged', () => {
329
+ const { hits } = runWithHits('I{a | a:∈S1; u:=F9[a]; v:=F9[a]}');
330
+ expect(hits).toBe(2);
331
+ });
332
+
333
+ it('recomputes when a dependency changes across iterations', () => {
334
+ const { value, hits } = runWithHits('I{a | a:∈X1; t:=F9[a]}');
335
+ expect(value).toBe('{1, 2, 3}');
336
+ expect(hits).toBe(0);
337
+ });
338
+
339
+ it('shares cache between structurally identical call sites', () => {
340
+ const metadata = new EvaluationMetadata();
341
+ const callA = buildAST('F9[X1]');
342
+ const callB = buildAST('F9[X1]');
343
+ expect(metadata.get(callA).structuralKey).toBe(metadata.get(callB).structuralKey);
344
+ });
345
+
346
+ it('disables cache when disableCache is true', () => {
347
+ const { value, hits } = runWithHits('I{a | a:∈X1; t:=F9[a]}', true);
348
+ expect(value).toBe('{1, 2, 3}');
349
+ expect(hits).toBe(0);
350
+ });
351
+ });
352
+ });