@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,326 @@
1
+ import { beforeEach, describe, expect, it } from 'vitest';
2
+
3
+ import { buildTree } from '../../parsing';
4
+ import { RSErrorCode, type RSErrorDescription } from '../error';
5
+ import { labelType } from '../labels';
6
+ import { normalizeAST } from '../parser/normalize';
7
+ import { parser as rslangParser } from '../parser/parser';
8
+
9
+ import { TypeAuditor } from './type-auditor';
10
+ import { basic, bool, constant, LogicT, tuple, type TypeContext, TypeID } from './typification';
11
+
12
+ // Helper to build AST
13
+ function buildAST(expression: string) {
14
+ const tree = rslangParser.parse(expression);
15
+ const ast = buildTree(tree.cursor());
16
+ normalizeAST(ast, expression);
17
+ return ast;
18
+ }
19
+
20
+ // Helper: Set up type context
21
+ function setupTypeContext(): TypeContext {
22
+ const context: TypeContext = new Map();
23
+ context.set('X1', bool({ typeID: TypeID.basic, baseID: 'X1' }));
24
+ context.set('S1', bool(tuple([basic('X1'), basic('X1')])));
25
+ context.set('S2', bool(bool(basic('X1'))));
26
+ context.set('F1', {
27
+ typeID: TypeID.function,
28
+ result: bool(basic('X1')),
29
+ args: [
30
+ { alias: 'a', type: bool(basic('X1')) },
31
+ { alias: 'b', type: bool(basic('X1')) }
32
+ ]
33
+ });
34
+ context.set('P1', {
35
+ typeID: TypeID.predicate,
36
+ result: LogicT,
37
+ args: [
38
+ { alias: 'a', type: bool(basic('X1')) },
39
+ { alias: 'b', type: bool(basic('X1')) }
40
+ ]
41
+ });
42
+ context.set('C1', bool(constant('C1')));
43
+ context.set('C2', bool(constant('C2')));
44
+ context.set('S3', constant('C1'));
45
+ context.set('S4', constant('C2'));
46
+ return context;
47
+ }
48
+
49
+ const correctTypesData = [
50
+ // Literals and global identifiers
51
+ ['1', 'Z'],
52
+ ['Z', 'ℬ(Z)'],
53
+ ['∅', 'ℬ(R0)'],
54
+ ['S1', 'ℬ(X1×X1)'],
55
+ // Functions
56
+ ['[a∈ℬ(R1)] a=a', '[ℬ(R1)] → Logic'],
57
+ ['[a∈X1] X1\\{a}', '[X1] → ℬ(X1)'],
58
+ ['[a∈D{b∈X1 | b=b}] a=a', '[X1] → Logic'],
59
+ ['P1[X1, X1]', 'Logic'],
60
+ ['F1[X1, X1]', 'ℬ(X1)'],
61
+ ['[β∈ℬ(R1×R2)] Pr1(β)', '[ℬ(R1×R2)] → ℬ(R1)'],
62
+ ['[α∈ℬ(R1), β∈ℬ(R1×R2)] α⊆Pr1(β) & card(α)=card(β)', '[ℬ(R1), ℬ(R1×R2)] → Logic'],
63
+ ['[α∈Z, μ∈Z] R{ξ:=α | ξ<0 ∨ ξ≥μ | debool(I{ξ-μ | ξ≥μ} ∪ I{ξ+μ | ξ<0})}', '[Z, Z] → Z'],
64
+ ['[σ∈ℬ(Z×Z), π∈Z] I{(α, μ+π) | (α,μ):∈σ}', '[ℬ(Z×Z), Z] → ℬ(Z×Z)'],
65
+ // Integral
66
+ ['card(X1)', 'Z'],
67
+ ['card(C1)', 'Z'],
68
+ ['1+1', 'Z'],
69
+ ['1-2', 'Z'],
70
+ ['1*2', 'Z'],
71
+ ['S3+1', 'C1'],
72
+ ['1+S3', 'C1'],
73
+ ['S3+S3', 'C1'],
74
+ ['S3-S4', 'Z'],
75
+ // Logical
76
+ ['1 > 1', 'Logic'],
77
+ ['1 ≥ 1', 'Logic'],
78
+ ['1 < 1', 'Logic'],
79
+ ['1 ≤ 1', 'Logic'],
80
+ ['1 = 1', 'Logic'],
81
+ ['1 ≠ 1', 'Logic'],
82
+ ['1 > S3', 'Logic'],
83
+ ['S4 = S4', 'Logic'],
84
+ ['1=2 & 3=4', 'Logic'],
85
+ ['X1=X1', 'Logic'],
86
+ ['X1=∅', 'Logic'],
87
+ ['∅=X1', 'Logic'],
88
+ ['{S3}=C1', 'Logic'],
89
+ ['∀a∈X1 1=1', 'Logic'],
90
+ ['∃a∈X1 1=1', 'Logic'],
91
+ ['∃a,b∈X1 a=b', 'Logic'],
92
+ ['∃(a,b)∈X1×X1 a=b', 'Logic'],
93
+ ['∀a∈X1 R{b := a | b}=a', 'Logic'],
94
+ ['∀a∈S1 R{(a1,a2) := a | (a1,a2)}=a', 'Logic'],
95
+ ['¬∃a∈ℬ(S1)\\{∅} P1[Pr1(a),Pr2(a)]', 'Logic'],
96
+ // Set predicates
97
+ ['X1=X1', 'Logic'],
98
+ ['X1=∅', 'Logic'],
99
+ ['Z=∅', 'Logic'],
100
+ ['1 ∈ Z', 'Logic'],
101
+ ['S3 ∈ C1', 'Logic'],
102
+ ['S3 ∈ Z', 'Logic'],
103
+ ['X1 ∈ S2', 'Logic'],
104
+ ['X1 ⊂ X1', 'Logic'],
105
+ ['X1 ∉ S2', 'Logic'],
106
+ ['X1 ⊆ X1', 'Logic'],
107
+ ['X1 ⊄ X1', 'Logic'],
108
+ // Constructors
109
+ ['ℬ(X1)', 'ℬℬ(X1)'],
110
+ ['X1×X1', 'ℬ(X1×X1)'],
111
+ ['{X1, X1}', 'ℬℬ(X1)'],
112
+ ['{1}', 'ℬ(Z)'],
113
+ ['{1, card(X1)}', 'ℬ(Z)'],
114
+ ['{S3, 1}', 'ℬ(C1)'],
115
+ ['{1, S3}', 'ℬ(C1)'],
116
+ ['(X1, X1)', 'ℬ(X1)×ℬ(X1)'],
117
+ ['(1, 2)', 'Z×Z'],
118
+ ['{(1, S3), (S3, 1)}', 'ℬ(C1×C1)'],
119
+ ['D{t ∈ ℬ(X1) | t=t}', 'ℬℬ(X1)'],
120
+ ['D{t ∈ Z | t>2}', 'ℬ(Z)'],
121
+ ['D{t ∈ S1 | pr2(t)=pr1(t)}', 'ℬ(X1×X1)'],
122
+ ['R{a := S1 | card(a)<10 | a ∪ a}', 'ℬ(X1×X1)'],
123
+ ['R{a := S1 | a ∪ a}', 'ℬ(X1×X1)'],
124
+ ['R{a := 1 | a<10 | a+1}', 'Z'],
125
+ ['R{a := 1 | a<S3 | a+S3}', 'C1'],
126
+ ['I{(a, b) | a :∈ X1; b := a}', 'ℬ(X1×X1)'],
127
+ ['I{(a, b) | a :∈ X1; b := a; 1=1}', 'ℬ(X1×X1)'],
128
+ // Set operations
129
+ ['X1 ∪ X1', 'ℬ(X1)'],
130
+ ['X1 \\ X1', 'ℬ(X1)'],
131
+ ['X1 ∩ X1', 'ℬ(X1)'],
132
+ ['X1 ∆ X1', 'ℬ(X1)'],
133
+ ['C1 ∪ C1', 'ℬ(C1)'],
134
+ ['{1} ∪ C1', 'ℬ(C1)'],
135
+ ['C1 ∪ {1}', 'ℬ(C1)'],
136
+ ['{(S3, 1)} ∪ {(1, S3)}', 'ℬ(C1×C1)'],
137
+ ['Pr1(S1)', 'ℬ(X1)'],
138
+ ['pr2((S1,1))', 'Z'],
139
+ ['Fi1[X1](S1)', 'ℬ(X1×X1)'],
140
+ ['Fi1[{1,2,3}](Z×X1)', 'ℬ(Z×X1)'],
141
+ ['Fi1[{1,2,3}](C1×X1)', 'ℬ(C1×X1)'],
142
+ ['Fi2,1[S1](S1)', 'ℬ(X1×X1)'],
143
+ ['Pr2,1(S1)', 'ℬ(X1×X1)'],
144
+ ['Pr2,1(C1×X1)', 'ℬ(X1×C1)'],
145
+ ['bool(X1)', 'ℬℬ(X1)'],
146
+ ['debool({X1})', 'ℬ(X1)'],
147
+ ['red(S2)', 'ℬ(X1)'],
148
+ // Recursion with empty set
149
+ ['R{a:=∅ | a∪X1}', 'ℬ(X1)']
150
+ ];
151
+
152
+ const errorData = [
153
+ // Identifiers
154
+ ['X42', { code: RSErrorCode.globalNotTyped, from: 0, to: 3, params: ['X42'] }],
155
+ ['F42[X1]', { code: RSErrorCode.globalNotTyped, from: 0, to: 3, params: ['F42'] }],
156
+ // Radicals
157
+ ['R1', { code: RSErrorCode.radicalUsage, from: 0, to: 2, params: ['R1'] }],
158
+ ['[a∈ℬ(R1)] R1\\a', { code: RSErrorCode.radicalUsage, from: 10, to: 12, params: ['R1'] }],
159
+ // Functions
160
+ ['F1[X1]', { code: RSErrorCode.invalidArgsArity, from: 3, to: 5, params: ['2', '1'] }],
161
+ ['F1[X1, X1, X1]', { code: RSErrorCode.invalidArgsArity, from: 3, to: 5, params: ['2', '3'] }],
162
+ ['F1[X1, S1]', { code: RSErrorCode.invalidArgumentType, from: 7, to: 9, params: ['b∈ℬ(X1)', 'ℬ(X1×X1)'] }],
163
+ ['F1[S1, X1]', { code: RSErrorCode.invalidArgumentType, from: 3, to: 5, params: ['a∈ℬ(X1)', 'ℬ(X1×X1)'] }],
164
+ ['P1[X1]', { code: RSErrorCode.invalidArgsArity, from: 3, to: 5, params: ['2', '1'] }],
165
+ ['P1[X1, X1, X1]', { code: RSErrorCode.invalidArgsArity, from: 3, to: 5, params: ['2', '3'] }],
166
+ ['P1[X1, S1]', { code: RSErrorCode.invalidArgumentType, from: 7, to: 9, params: ['b∈ℬ(X1)', 'ℬ(X1×X1)'] }],
167
+ ['P1[S1, X1]', { code: RSErrorCode.invalidArgumentType, from: 3, to: 5, params: ['a∈ℬ(X1)', 'ℬ(X1×X1)'] }],
168
+ // Integral
169
+ ['card(debool(X1))', { code: RSErrorCode.invalidCard, from: 5, to: 15, params: ['X1'] }],
170
+ ['card(S3)', { code: RSErrorCode.invalidCard, from: 5, to: 7, params: ['C1'] }],
171
+ ['card(1)', { code: RSErrorCode.invalidCard, from: 5, to: 6, params: ['Z'] }],
172
+ ['card((1,2))', { code: RSErrorCode.invalidCard, from: 5, to: 10, params: ['Z×Z'] }],
173
+ ['debool(X1)+1', { code: RSErrorCode.arithmeticNotSupported, from: 0, to: 10, params: ['X1'] }],
174
+ ['1+debool(X1)', { code: RSErrorCode.arithmeticNotSupported, from: 2, to: 12, params: ['X1'] }],
175
+ // Logical
176
+ ['debool(X1)<1', { code: RSErrorCode.orderingNotSupported, from: 0, to: 10, params: ['X1'] }],
177
+ ['1<debool(X1)', { code: RSErrorCode.orderingNotSupported, from: 2, to: 12, params: ['X1'] }],
178
+ ['debool(X1)=1', { code: RSErrorCode.typesNotCompatible, from: 0, to: 12, params: ['X1', 'Z'] }],
179
+ ['1=debool(X1)', { code: RSErrorCode.typesNotCompatible, from: 0, to: 12, params: ['Z', 'X1'] }],
180
+ ['S1=S2', { code: RSErrorCode.typesNotCompatible, from: 0, to: 5, params: ['ℬ(X1×X1)', 'ℬℬ(X1)'] }],
181
+ ['∀a,a∈X1 a=a', { code: RSErrorCode.localShadowing, from: 3, to: 4, params: ['a'] }],
182
+ ['∀(a,a)∈S1 a=a', { code: RSErrorCode.localShadowing, from: 4, to: 5, params: ['a'] }],
183
+ ['∀(a,b)∈X1 a=b', { code: RSErrorCode.invalidCortegeDeclare, from: 2, to: 3 }],
184
+ ['∀a∈X1 D{a∈S1| 1=1}=S1', { code: RSErrorCode.localShadowing, from: 8, to: 9, params: ['a'] }],
185
+ ['∀a∈X1 a=a & a=a', { code: RSErrorCode.localOutOfScope, from: 12, to: 13, params: ['a'] }],
186
+ ['∀a∈X1 a=a & debool(X1)=a', { code: RSErrorCode.localOutOfScope, from: 23, to: 24, params: ['a'] }],
187
+ // Set predicates
188
+ ['X1∈X1', { code: RSErrorCode.invalidElementPredicate, from: 0, to: 5, params: ['ℬ(X1)', '∈', 'ℬ(X1)'] }],
189
+ ['(1,2)∈X1', { code: RSErrorCode.invalidElementPredicate, from: 0, to: 8, params: ['Z×Z', '∈', 'ℬ(X1)'] }],
190
+ ['(1,2)∈Z', { code: RSErrorCode.invalidElementPredicate, from: 0, to: 7, params: ['Z×Z', '∈', 'ℬ(Z)'] }],
191
+ ['{1}∈Z', { code: RSErrorCode.invalidElementPredicate, from: 0, to: 5, params: ['ℬ(Z)', '∈', 'ℬ(Z)'] }],
192
+ ['{1}∉Z', { code: RSErrorCode.invalidElementPredicate, from: 0, to: 5, params: ['ℬ(Z)', '∉', 'ℬ(Z)'] }],
193
+ ['X1⊆S2', { code: RSErrorCode.typesNotEqual, from: 0, to: 5, params: ['ℬ(X1)', 'ℬℬ(X1)'] }],
194
+ ['X1⊄S2', { code: RSErrorCode.typesNotEqual, from: 0, to: 5, params: ['ℬ(X1)', 'ℬℬ(X1)'] }],
195
+ ['X1⊂S2', { code: RSErrorCode.typesNotEqual, from: 0, to: 5, params: ['ℬ(X1)', 'ℬℬ(X1)'] }],
196
+ // Constructors
197
+ ['ℬ(S3)', { code: RSErrorCode.invalidBoolean, from: 2, to: 4, params: ['C1'] }],
198
+ ['ℬ((1,2))', { code: RSErrorCode.invalidBoolean, from: 2, to: 7, params: ['Z×Z'] }],
199
+ ['S3×Z', { code: RSErrorCode.invalidDecart, from: 0, to: 2, params: ['C1'] }],
200
+ ['{X1, S1}', { code: RSErrorCode.invalidEnumeration, from: 5, to: 7, params: ['ℬ(X1)', 'ℬ(X1×X1)'] }],
201
+ ['{S1, X1}', { code: RSErrorCode.invalidEnumeration, from: 5, to: 7, params: ['ℬ(X1×X1)', 'ℬ(X1)'] }],
202
+ ['{1, X1}', { code: RSErrorCode.invalidEnumeration, from: 4, to: 6, params: ['Z', 'ℬ(X1)'] }],
203
+ ['{X1, 1}', { code: RSErrorCode.invalidEnumeration, from: 5, to: 6, params: ['ℬ(X1)', 'Z'] }],
204
+ ['{(1,2), (X1,X1)}', { code: RSErrorCode.invalidEnumeration, from: 8, to: 15, params: ['Z×Z', 'ℬ(X1)×ℬ(X1)'] }],
205
+ ['R{a := S1 | {a}}', { code: RSErrorCode.typesNotEqual, from: 12, to: 15, params: ['ℬℬ(X1×X1)', 'ℬ(X1×X1)'] }],
206
+ [
207
+ 'I{(a, b) | a:∈X1; b:={a}; a≠b}',
208
+ { code: RSErrorCode.typesNotCompatible, from: 26, to: 29, params: ['X1', 'ℬ(X1)'] }
209
+ ],
210
+ // Set operations
211
+ ['X1 ∪ ∅', { code: RSErrorCode.invalidEmptySetUsage, from: 5, to: 6 }],
212
+ ['X1 ∪ S1', { code: RSErrorCode.typesNotEqual, from: 0, to: 7, params: ['ℬ(X1)', 'ℬ(X1×X1)'] }],
213
+ ['S1 ∪ X1', { code: RSErrorCode.typesNotEqual, from: 0, to: 7, params: ['ℬ(X1×X1)', 'ℬ(X1)'] }],
214
+ ['Pr1(X1)', { code: RSErrorCode.invalidProjectionSet, from: 4, to: 6, params: ['Pr1', 'ℬ(X1)'] }],
215
+ ['Pr3(S1)', { code: RSErrorCode.invalidProjectionSet, from: 4, to: 6, params: ['Pr3', 'ℬ(X1×X1)'] }],
216
+ ['Pr1,3(S1)', { code: RSErrorCode.invalidProjectionSet, from: 6, to: 8, params: ['Pr1,3', 'ℬ(X1×X1)'] }],
217
+ ['Pr1((1,2))', { code: RSErrorCode.invalidProjectionSet, from: 4, to: 9, params: ['Z×Z'] }],
218
+ ['pr1(X1)', { code: RSErrorCode.invalidProjectionTuple, from: 4, to: 6, params: ['pr1', 'ℬ(X1)'] }],
219
+ ['pr1(debool(X1))', { code: RSErrorCode.invalidProjectionTuple, from: 4, to: 14, params: ['pr1', 'X1'] }],
220
+ ['pr3(debool(S1))', { code: RSErrorCode.invalidProjectionTuple, from: 4, to: 14, params: ['pr3', 'X1×X1'] }],
221
+ ['Fi1[X1](ℬ(X1))', { code: RSErrorCode.invalidFilterArgumentType, from: 8, to: 13, params: ['Fi1', 'ℬℬ(X1)'] }],
222
+ ['Fi1[1](S1)', { code: RSErrorCode.typesNotEqual, from: 4, to: 5, params: ['Z', 'ℬ(X1)'] }],
223
+ ['Fi1[X1](ℬ(X1)×X1)', { code: RSErrorCode.typesNotEqual, from: 4, to: 6, params: ['ℬ(X1)', 'ℬℬ(X1)'] }],
224
+ ['Fi3[X1](S1)', { code: RSErrorCode.invalidFilterArgumentType, from: 8, to: 10, params: ['Fi3', 'ℬ(X1×X1)'] }],
225
+ ['Fi1[X1,X1](S1)', { code: RSErrorCode.invalidFilterArity, from: 0, to: 14 }],
226
+ ['Fi1,2[X1](S1)', { code: RSErrorCode.typesNotEqual, from: 6, to: 8, params: ['ℬ(X1)', 'ℬ(X1×X1)'] }],
227
+ ['red(X1)', { code: RSErrorCode.invalidReduce, from: 4, to: 6, params: ['ℬ(X1)'] }],
228
+ ['debool(S3)', { code: RSErrorCode.invalidDebool, from: 7, to: 9, params: ['C1'] }],
229
+ // Locals
230
+ ['D{t ∈ X1 | 1=1}', { code: RSErrorCode.localNotUsed, from: 11, to: 14, params: ['t'] }],
231
+ ['D{t ∈ X1 | t=t} ∪ D{t∈X1 | t=t}', { code: RSErrorCode.localDoubleDeclare, from: 20, to: 21, params: ['t'] }],
232
+ ['D{t ∈ X1 | ∀t∈X1 t=t}', { code: RSErrorCode.localShadowing, from: 12, to: 13, params: ['t'] }],
233
+ // Error popup
234
+ ['1<card(X42)', { code: RSErrorCode.globalNotTyped, from: 7, to: 10, params: ['X42'] }],
235
+ ['X42=X1', { code: RSErrorCode.globalNotTyped, from: 0, to: 3, params: ['X42'] }],
236
+ // Logic vs setexpr discrimination
237
+ ['D{α∈X1|F1[X1,X1]}', { code: RSErrorCode.expectedLogic, from: 7, to: 16, params: ['ℬ(X1)'] }],
238
+ ['F1[1=1,X1]', { code: RSErrorCode.expectedSetexpr, from: 3, to: 6, params: ['Logic'] }],
239
+ ['∀a∈(1=1) a=a', { code: RSErrorCode.expectedSetexpr, from: 3, to: 8, params: ['Logic'] }],
240
+ ['∀a∈X1 F1[{a},X1]', { code: RSErrorCode.expectedLogic, from: 6, to: 16, params: ['ℬ(X1)'] }],
241
+ ['(1=1)∪X1', { code: RSErrorCode.expectedSetexpr, from: 0, to: 5, params: ['Logic'] }],
242
+ ['X1 & 1=1', { code: RSErrorCode.expectedLogic, from: 0, to: 2, params: ['ℬ(X1)'] }],
243
+ ['[(a,b)∈S1] (a,b)', { code: RSErrorCode.invalidArgumentCortegeDeclare, from: 1, to: 6 }]
244
+ ];
245
+
246
+ describe('TypeAuditor', () => {
247
+ let typeContext: TypeContext;
248
+ let auditor: TypeAuditor;
249
+ let errors: RSErrorDescription[];
250
+
251
+ beforeEach(() => {
252
+ typeContext = setupTypeContext();
253
+ auditor = new TypeAuditor(typeContext);
254
+ errors = [];
255
+ });
256
+
257
+ function expectType(input: string, expectedType: string) {
258
+ const ast = buildAST(input);
259
+ expect(ast.hasError).toBe(false);
260
+ const result = auditor.run(ast, false, error => errors.push(error));
261
+ expect(labelType(result)).toBe(expectedType);
262
+ }
263
+
264
+ function expectError(input: string, expectedError: RSErrorDescription) {
265
+ const ast = buildAST(input);
266
+ expect(ast.hasError).toBe(false);
267
+ auditor.run(ast, false, error => errors.push(error));
268
+ expect(errors.length).toBe(1);
269
+ expect(errors[0]).toMatchObject(expectedError);
270
+ }
271
+
272
+ // .filter(([input]) => input === '[σ∈ℬ(Z×Z), π∈Z] I{(α, μ+π) | (α,μ):∈σ}')
273
+ correctTypesData.forEach(([input, expectedType]) => {
274
+ it(`Correct type for "${input}"`, () => expectType(input, expectedType));
275
+ });
276
+
277
+ errorData.forEach(([input, expectedError]) => {
278
+ it(`Error for "${input as string}"`, () => expectError(input as string, expectedError as RSErrorDescription));
279
+ });
280
+
281
+ it('Templated function', () => {
282
+ typeContext.set('F2', {
283
+ typeID: TypeID.function,
284
+ result: bool(basic('R1')),
285
+ args: [
286
+ { alias: 'a', type: bool(basic('R1')) },
287
+ { alias: 'b', type: basic('R1') }
288
+ ]
289
+ });
290
+ expectType('F2[ℬ(X1), X1]', 'ℬℬ(X1)');
291
+ expectType('F2[Z, 1]', 'ℬ(Z)');
292
+ expectType('F2[Z, S3]', 'ℬ(C1)');
293
+ expectType('F2[{∅}, ∅]', 'ℬℬ(R0)');
294
+ expectError('F2[X1, ℬ(X1)]', {
295
+ code: RSErrorCode.invalidArgumentType,
296
+ from: 7,
297
+ to: 12,
298
+ params: ['b∈R1F2', 'ℬℬ(X1)']
299
+ });
300
+ });
301
+
302
+ it('Templated nesting', () => {
303
+ typeContext.set('F2', {
304
+ typeID: TypeID.function,
305
+ result: bool(basic('R1')),
306
+ args: [
307
+ { alias: 'a', type: basic('R1') },
308
+ {
309
+ alias: 'b',
310
+ type: {
311
+ typeID: TypeID.tuple,
312
+ factors: [basic('R1'), basic('R2')]
313
+ }
314
+ }
315
+ ]
316
+ });
317
+ expectType('[a∈R2, b∈R2×R1] F2[a, b]', '[R2, R2×R1] → ℬ(R2)');
318
+ expectType('[a∈R3, b∈R3×R4] F2[a, b]', '[R3, R3×R4] → ℬ(R3)');
319
+ });
320
+
321
+ it('Reports token end positions for semantic errors', () => {
322
+ const ast = buildAST('X42');
323
+ auditor.run(ast, false, error => errors.push(error));
324
+ expect(errors[0]).toMatchObject({ code: RSErrorCode.globalNotTyped, from: 0, to: 3 });
325
+ });
326
+ });