typesea 0.1.0 → 0.3.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 (301) hide show
  1. package/CHANGELOG.md +85 -6
  2. package/README.md +143 -28
  3. package/dist/adapters/index.d.ts +50 -8
  4. package/dist/adapters/index.d.ts.map +1 -1
  5. package/dist/adapters/index.js +169 -48
  6. package/dist/aot/index.d.ts +19 -3
  7. package/dist/aot/index.d.ts.map +1 -1
  8. package/dist/aot/index.js +115 -17
  9. package/dist/async/index.d.ts +28 -56
  10. package/dist/async/index.d.ts.map +1 -1
  11. package/dist/async/index.js +94 -37
  12. package/dist/builders/composite.d.ts +43 -9
  13. package/dist/builders/composite.d.ts.map +1 -1
  14. package/dist/builders/composite.js +100 -17
  15. package/dist/builders/index.d.ts +8 -5
  16. package/dist/builders/index.d.ts.map +1 -1
  17. package/dist/builders/index.js +7 -4
  18. package/dist/builders/modifier.d.ts +36 -5
  19. package/dist/builders/modifier.d.ts.map +1 -1
  20. package/dist/builders/modifier.js +52 -5
  21. package/dist/builders/object/guard.d.ts +72 -24
  22. package/dist/builders/object/guard.d.ts.map +1 -1
  23. package/dist/builders/object/guard.js +139 -29
  24. package/dist/builders/object/index.d.ts +4 -2
  25. package/dist/builders/object/index.d.ts.map +1 -1
  26. package/dist/builders/object/index.js +3 -1
  27. package/dist/builders/object/schema.d.ts +88 -11
  28. package/dist/builders/object/schema.d.ts.map +1 -1
  29. package/dist/builders/object/schema.js +290 -23
  30. package/dist/builders/object/types.d.ts +20 -31
  31. package/dist/builders/object/types.d.ts.map +1 -1
  32. package/dist/builders/object/types.js +2 -0
  33. package/dist/builders/runtime.d.ts +40 -0
  34. package/dist/builders/runtime.d.ts.map +1 -0
  35. package/dist/builders/runtime.js +150 -0
  36. package/dist/builders/scalar.d.ts +49 -9
  37. package/dist/builders/scalar.d.ts.map +1 -1
  38. package/dist/builders/scalar.js +87 -9
  39. package/dist/builders/table.d.ts +35 -5
  40. package/dist/builders/table.d.ts.map +1 -1
  41. package/dist/builders/table.js +35 -5
  42. package/dist/builders/types.d.ts +20 -4
  43. package/dist/builders/types.d.ts.map +1 -1
  44. package/dist/builders/types.js +2 -0
  45. package/dist/compile/check-composite.d.ts +25 -2
  46. package/dist/compile/check-composite.d.ts.map +1 -1
  47. package/dist/compile/check-composite.js +699 -27
  48. package/dist/compile/check-scalar.d.ts +88 -0
  49. package/dist/compile/check-scalar.d.ts.map +1 -1
  50. package/dist/compile/check-scalar.js +570 -3
  51. package/dist/compile/check.d.ts +12 -0
  52. package/dist/compile/check.d.ts.map +1 -1
  53. package/dist/compile/check.js +62 -3
  54. package/dist/compile/context.d.ts +47 -9
  55. package/dist/compile/context.d.ts.map +1 -1
  56. package/dist/compile/context.js +53 -8
  57. package/dist/compile/first.d.ts +26 -0
  58. package/dist/compile/first.d.ts.map +1 -0
  59. package/dist/compile/first.js +850 -0
  60. package/dist/compile/graph-predicate.d.ts +4 -2
  61. package/dist/compile/graph-predicate.d.ts.map +1 -1
  62. package/dist/compile/graph-predicate.js +2272 -165
  63. package/dist/compile/guard.d.ts +16 -24
  64. package/dist/compile/guard.d.ts.map +1 -1
  65. package/dist/compile/guard.js +202 -72
  66. package/dist/compile/index.d.ts +3 -1
  67. package/dist/compile/index.d.ts.map +1 -1
  68. package/dist/compile/index.js +2 -0
  69. package/dist/compile/issue.d.ts +110 -0
  70. package/dist/compile/issue.d.ts.map +1 -1
  71. package/dist/compile/issue.js +184 -1
  72. package/dist/compile/names.d.ts +12 -2
  73. package/dist/compile/names.d.ts.map +1 -1
  74. package/dist/compile/names.js +19 -3
  75. package/dist/compile/predicate.d.ts +24 -0
  76. package/dist/compile/predicate.d.ts.map +1 -1
  77. package/dist/compile/predicate.js +287 -10
  78. package/dist/compile/runtime.d.ts +100 -13
  79. package/dist/compile/runtime.d.ts.map +1 -1
  80. package/dist/compile/runtime.js +56 -6
  81. package/dist/compile/source.d.ts +10 -2
  82. package/dist/compile/source.d.ts.map +1 -1
  83. package/dist/compile/source.js +385 -26
  84. package/dist/compile/types.d.ts +22 -0
  85. package/dist/compile/types.d.ts.map +1 -1
  86. package/dist/compile/types.js +2 -0
  87. package/dist/decoder/index.d.ts +92 -46
  88. package/dist/decoder/index.d.ts.map +1 -1
  89. package/dist/decoder/index.js +266 -39
  90. package/dist/evaluate/check-composite.d.ts +111 -2
  91. package/dist/evaluate/check-composite.d.ts.map +1 -1
  92. package/dist/evaluate/check-composite.js +343 -8
  93. package/dist/evaluate/check-scalar.d.ts +25 -0
  94. package/dist/evaluate/check-scalar.d.ts.map +1 -1
  95. package/dist/evaluate/check-scalar.js +124 -3
  96. package/dist/evaluate/check.d.ts +7 -0
  97. package/dist/evaluate/check.d.ts.map +1 -1
  98. package/dist/evaluate/check.js +62 -4
  99. package/dist/evaluate/index.d.ts +2 -0
  100. package/dist/evaluate/index.d.ts.map +1 -1
  101. package/dist/evaluate/index.js +2 -0
  102. package/dist/evaluate/issue.d.ts +11 -1
  103. package/dist/evaluate/issue.d.ts.map +1 -1
  104. package/dist/evaluate/issue.js +15 -1
  105. package/dist/evaluate/predicate.d.ts +16 -5
  106. package/dist/evaluate/predicate.d.ts.map +1 -1
  107. package/dist/evaluate/predicate.js +20 -5
  108. package/dist/evaluate/shared.d.ts +78 -13
  109. package/dist/evaluate/shared.d.ts.map +1 -1
  110. package/dist/evaluate/shared.js +101 -8
  111. package/dist/evaluate/state.d.ts +35 -13
  112. package/dist/evaluate/state.d.ts.map +1 -1
  113. package/dist/evaluate/state.js +35 -2
  114. package/dist/guard/array.d.ts +48 -0
  115. package/dist/guard/array.d.ts.map +1 -0
  116. package/dist/guard/array.js +84 -0
  117. package/dist/guard/base.d.ts +111 -31
  118. package/dist/guard/base.d.ts.map +1 -1
  119. package/dist/guard/base.js +165 -32
  120. package/dist/guard/date.d.ts +34 -0
  121. package/dist/guard/date.d.ts.map +1 -0
  122. package/dist/guard/date.js +60 -0
  123. package/dist/guard/error.d.ts +10 -5
  124. package/dist/guard/error.d.ts.map +1 -1
  125. package/dist/guard/error.js +10 -5
  126. package/dist/guard/index.d.ts +4 -0
  127. package/dist/guard/index.d.ts.map +1 -1
  128. package/dist/guard/index.js +4 -0
  129. package/dist/guard/number.d.ts +86 -11
  130. package/dist/guard/number.d.ts.map +1 -1
  131. package/dist/guard/number.js +159 -11
  132. package/dist/guard/props.d.ts +27 -3
  133. package/dist/guard/props.d.ts.map +1 -1
  134. package/dist/guard/props.js +27 -3
  135. package/dist/guard/read.d.ts +115 -10
  136. package/dist/guard/read.d.ts.map +1 -1
  137. package/dist/guard/read.js +185 -10
  138. package/dist/guard/registry.d.ts +12 -2
  139. package/dist/guard/registry.d.ts.map +1 -1
  140. package/dist/guard/registry.js +15 -3
  141. package/dist/guard/string.d.ts +115 -13
  142. package/dist/guard/string.d.ts.map +1 -1
  143. package/dist/guard/string.js +250 -13
  144. package/dist/guard/types.d.ts +110 -40
  145. package/dist/guard/types.d.ts.map +1 -1
  146. package/dist/guard/types.js +2 -0
  147. package/dist/index.d.ts +5 -5
  148. package/dist/index.d.ts.map +1 -1
  149. package/dist/index.js +4 -4
  150. package/dist/internal/index.d.ts +42 -6
  151. package/dist/internal/index.d.ts.map +1 -1
  152. package/dist/internal/index.js +51 -8
  153. package/dist/ir/builder.d.ts +17 -127
  154. package/dist/ir/builder.d.ts.map +1 -1
  155. package/dist/ir/builder.js +80 -137
  156. package/dist/ir/freeze.d.ts +4 -0
  157. package/dist/ir/freeze.d.ts.map +1 -1
  158. package/dist/ir/freeze.js +66 -0
  159. package/dist/ir/index.d.ts +3 -1
  160. package/dist/ir/index.d.ts.map +1 -1
  161. package/dist/ir/index.js +2 -0
  162. package/dist/ir/regexp.d.ts +2 -0
  163. package/dist/ir/regexp.d.ts.map +1 -1
  164. package/dist/ir/regexp.js +2 -0
  165. package/dist/ir/types.d.ts +94 -56
  166. package/dist/ir/types.d.ts.map +1 -1
  167. package/dist/ir/types.js +2 -0
  168. package/dist/ir/validate.d.ts +8 -1
  169. package/dist/ir/validate.d.ts.map +1 -1
  170. package/dist/ir/validate.js +511 -61
  171. package/dist/issue/index.d.ts +42 -10
  172. package/dist/issue/index.d.ts.map +1 -1
  173. package/dist/issue/index.js +65 -11
  174. package/dist/json-schema/emit-combinator.d.ts +44 -4
  175. package/dist/json-schema/emit-combinator.d.ts.map +1 -1
  176. package/dist/json-schema/emit-combinator.js +44 -4
  177. package/dist/json-schema/emit-composite.d.ts +16 -2
  178. package/dist/json-schema/emit-composite.d.ts.map +1 -1
  179. package/dist/json-schema/emit-composite.js +81 -13
  180. package/dist/json-schema/emit-scalar.d.ts +26 -3
  181. package/dist/json-schema/emit-scalar.d.ts.map +1 -1
  182. package/dist/json-schema/emit-scalar.js +124 -10
  183. package/dist/json-schema/emit-types.d.ts +11 -1
  184. package/dist/json-schema/emit-types.d.ts.map +1 -1
  185. package/dist/json-schema/emit-types.js +2 -0
  186. package/dist/json-schema/emit.d.ts +12 -1
  187. package/dist/json-schema/emit.d.ts.map +1 -1
  188. package/dist/json-schema/emit.js +23 -3
  189. package/dist/json-schema/freeze.d.ts +13 -2
  190. package/dist/json-schema/freeze.d.ts.map +1 -1
  191. package/dist/json-schema/freeze.js +41 -8
  192. package/dist/json-schema/index.d.ts +16 -2
  193. package/dist/json-schema/index.d.ts.map +1 -1
  194. package/dist/json-schema/index.js +23 -3
  195. package/dist/json-schema/issue.d.ts +4 -1
  196. package/dist/json-schema/issue.d.ts.map +1 -1
  197. package/dist/json-schema/issue.js +4 -1
  198. package/dist/json-schema/read.d.ts +24 -3
  199. package/dist/json-schema/read.d.ts.map +1 -1
  200. package/dist/json-schema/read.js +59 -12
  201. package/dist/json-schema/types.d.ts +45 -16
  202. package/dist/json-schema/types.d.ts.map +1 -1
  203. package/dist/json-schema/types.js +2 -0
  204. package/dist/kind/index.d.ts +40 -28
  205. package/dist/kind/index.d.ts.map +1 -1
  206. package/dist/kind/index.js +41 -13
  207. package/dist/lower/index.d.ts +6 -1
  208. package/dist/lower/index.d.ts.map +1 -1
  209. package/dist/lower/index.js +462 -46
  210. package/dist/message/index.d.ts +64 -10
  211. package/dist/message/index.d.ts.map +1 -1
  212. package/dist/message/index.js +155 -17
  213. package/dist/optimize/algebraic.d.ts +54 -0
  214. package/dist/optimize/algebraic.d.ts.map +1 -0
  215. package/dist/optimize/algebraic.js +314 -0
  216. package/dist/optimize/compact.d.ts +8 -1
  217. package/dist/optimize/compact.d.ts.map +1 -1
  218. package/dist/optimize/compact.js +13 -2
  219. package/dist/optimize/domain.d.ts +16 -0
  220. package/dist/optimize/domain.d.ts.map +1 -0
  221. package/dist/optimize/domain.js +619 -0
  222. package/dist/optimize/fold-boolean.d.ts +17 -2
  223. package/dist/optimize/fold-boolean.d.ts.map +1 -1
  224. package/dist/optimize/fold-boolean.js +59 -14
  225. package/dist/optimize/fold-common.d.ts +43 -8
  226. package/dist/optimize/fold-common.d.ts.map +1 -1
  227. package/dist/optimize/fold-common.js +37 -6
  228. package/dist/optimize/fold-constraints.d.ts +33 -0
  229. package/dist/optimize/fold-constraints.d.ts.map +1 -0
  230. package/dist/optimize/fold-constraints.js +484 -0
  231. package/dist/optimize/fold-scalar.d.ts +98 -13
  232. package/dist/optimize/fold-scalar.d.ts.map +1 -1
  233. package/dist/optimize/fold-scalar.js +98 -13
  234. package/dist/optimize/fold.d.ts +8 -1
  235. package/dist/optimize/fold.d.ts.map +1 -1
  236. package/dist/optimize/fold.js +22 -2
  237. package/dist/optimize/index.d.ts +9 -1
  238. package/dist/optimize/index.d.ts.map +1 -1
  239. package/dist/optimize/index.js +18 -3
  240. package/dist/optimize/map-node.d.ts +3 -1
  241. package/dist/optimize/map-node.d.ts.map +1 -1
  242. package/dist/optimize/map-node.js +48 -3
  243. package/dist/optimize/peephole.d.ts +16 -0
  244. package/dist/optimize/peephole.d.ts.map +1 -0
  245. package/dist/optimize/peephole.js +254 -0
  246. package/dist/optimize/remap.d.ts +2 -0
  247. package/dist/optimize/remap.d.ts.map +1 -1
  248. package/dist/optimize/remap.js +2 -0
  249. package/dist/optimize/rewrite.d.ts +13 -8
  250. package/dist/optimize/rewrite.d.ts.map +1 -1
  251. package/dist/optimize/rewrite.js +13 -8
  252. package/dist/plan/cache.d.ts +9 -3
  253. package/dist/plan/cache.d.ts.map +1 -1
  254. package/dist/plan/cache.js +34 -6
  255. package/dist/plan/index.d.ts +2 -0
  256. package/dist/plan/index.d.ts.map +1 -1
  257. package/dist/plan/index.js +2 -0
  258. package/dist/plan/predicate.d.ts +2 -0
  259. package/dist/plan/predicate.d.ts.map +1 -1
  260. package/dist/plan/predicate.js +298 -29
  261. package/dist/plan/schema-predicate.d.ts +6 -0
  262. package/dist/plan/schema-predicate.d.ts.map +1 -1
  263. package/dist/plan/schema-predicate.js +382 -19
  264. package/dist/plan/types.d.ts +2 -0
  265. package/dist/plan/types.d.ts.map +1 -1
  266. package/dist/plan/types.js +2 -0
  267. package/dist/result/index.d.ts +19 -5
  268. package/dist/result/index.d.ts.map +1 -1
  269. package/dist/result/index.js +10 -2
  270. package/dist/schema/common.d.ts +69 -6
  271. package/dist/schema/common.d.ts.map +1 -1
  272. package/dist/schema/common.js +104 -10
  273. package/dist/schema/freeze.d.ts +4 -0
  274. package/dist/schema/freeze.d.ts.map +1 -1
  275. package/dist/schema/freeze.js +40 -0
  276. package/dist/schema/index.d.ts +5 -2
  277. package/dist/schema/index.d.ts.map +1 -1
  278. package/dist/schema/index.js +4 -1
  279. package/dist/schema/lazy.d.ts +4 -0
  280. package/dist/schema/lazy.d.ts.map +1 -1
  281. package/dist/schema/lazy.js +4 -0
  282. package/dist/schema/literal.d.ts +7 -1
  283. package/dist/schema/literal.d.ts.map +1 -1
  284. package/dist/schema/literal.js +7 -1
  285. package/dist/schema/types.d.ts +109 -100
  286. package/dist/schema/types.d.ts.map +1 -1
  287. package/dist/schema/types.js +13 -2
  288. package/dist/schema/undefined.d.ts +17 -0
  289. package/dist/schema/undefined.d.ts.map +1 -0
  290. package/dist/schema/undefined.js +77 -0
  291. package/dist/schema/validate.d.ts +8 -1
  292. package/dist/schema/validate.d.ts.map +1 -1
  293. package/dist/schema/validate.js +255 -57
  294. package/docs/api.md +128 -8
  295. package/docs/assets/benchmark-headline.svg +163 -0
  296. package/docs/engine-notes.md +62 -15
  297. package/docs/index.html +1340 -702
  298. package/docs/ko/api.md +375 -0
  299. package/docs/ko/engine-notes.md +156 -0
  300. package/docs/ko/readme.md +378 -0
  301. package/package.json +66 -65
@@ -0,0 +1,314 @@
1
+ /**
2
+ * @file algebraic.ts
3
+ * @brief Boolean algebra simplification for validation predicates.
4
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
5
+ * before code generation consumes the graph.
6
+ */
7
+ import { NodeTag } from "../kind/index.js";
8
+ /**
9
+ * @brief Flatten nested boolean folds of the same kind.
10
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
11
+ * before code generation consumes the graph.
12
+ * @param values Input operand ids.
13
+ * @param nodes Graph node table used to inspect nested folds.
14
+ * @param tag Boolean fold kind to flatten.
15
+ * @returns Operand list with same-kind nested folds expanded in place.
16
+ */
17
+ export function flattenBooleanValues(values, nodes, tag) {
18
+ const flattened = [];
19
+ for (let index = 0; index < values.length; index += 1) {
20
+ const value = values[index];
21
+ if (value === undefined) {
22
+ continue;
23
+ }
24
+ const node = nodes[value];
25
+ if (node?.tag === tag) {
26
+ /*
27
+ * And(And(a,b),c) and Or(Or(a,b),c) are associative. Flattening
28
+ * reduces later duplicate and complement checks to one linear pass.
29
+ */
30
+ appendFlattenedBooleanValues(flattened, node.values, nodes, tag);
31
+ }
32
+ else {
33
+ flattened.push(value);
34
+ }
35
+ }
36
+ return flattened;
37
+ }
38
+ /**
39
+ * @brief Simplify operands of an And fold.
40
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
41
+ * before code generation consumes the graph.
42
+ * @param values Input operand ids.
43
+ * @param nodes Graph node table used to inspect nested complement forms.
44
+ * @returns Simplification result with contradiction status and kept operands.
45
+ */
46
+ export function simplifyAndValues(values, nodes) {
47
+ const kept = [];
48
+ const seen = new Set();
49
+ const positive = new Set();
50
+ const negative = new Set();
51
+ for (let index = 0; index < values.length; index += 1) {
52
+ const value = values[index];
53
+ if (value === undefined || seen.has(value)) {
54
+ continue;
55
+ }
56
+ if (isAbsorbedAndValue(value, nodes, seen)) {
57
+ /*
58
+ * a && (a || b) is equivalent to a. The Or branch cannot make the
59
+ * validation stricter once the shared value is already required.
60
+ */
61
+ continue;
62
+ }
63
+ if (hasComplementConflict(value, nodes, positive, negative)) {
64
+ /*
65
+ * a && !a is impossible for side-effect-free predicate nodes. Only
66
+ * complement-safe nodes are allowed into the positive/negative sets.
67
+ */
68
+ return {
69
+ contradiction: true,
70
+ tautology: false,
71
+ values: []
72
+ };
73
+ }
74
+ rememberComplementValue(value, nodes, positive, negative);
75
+ seen.add(value);
76
+ kept.push(value);
77
+ }
78
+ return {
79
+ contradiction: false,
80
+ tautology: false,
81
+ values: kept
82
+ };
83
+ }
84
+ /**
85
+ * @brief Simplify operands of an Or fold.
86
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
87
+ * before code generation consumes the graph.
88
+ * @param values Input operand ids.
89
+ * @param nodes Graph node table used to inspect nested complement forms.
90
+ * @returns Simplification result with tautology status and kept operands.
91
+ */
92
+ export function simplifyOrValues(values, nodes) {
93
+ const kept = [];
94
+ const seen = new Set();
95
+ const positive = new Set();
96
+ const negative = new Set();
97
+ for (let index = 0; index < values.length; index += 1) {
98
+ const value = values[index];
99
+ if (value === undefined || seen.has(value)) {
100
+ continue;
101
+ }
102
+ if (isAbsorbedOrValue(value, nodes, seen)) {
103
+ /*
104
+ * a || (a && b) is equivalent to a. The And branch cannot widen the
105
+ * accepted set once the shared value already passes.
106
+ */
107
+ continue;
108
+ }
109
+ if (hasComplementConflict(value, nodes, positive, negative)) {
110
+ /*
111
+ * a || !a is always true for complement-safe predicates, so the Or
112
+ * fold can collapse to a tautology.
113
+ */
114
+ return {
115
+ contradiction: false,
116
+ tautology: true,
117
+ values: []
118
+ };
119
+ }
120
+ rememberComplementValue(value, nodes, positive, negative);
121
+ seen.add(value);
122
+ kept.push(value);
123
+ }
124
+ return {
125
+ contradiction: false,
126
+ tautology: false,
127
+ values: kept
128
+ };
129
+ }
130
+ /**
131
+ * @brief Recursively append flattened operands.
132
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
133
+ * before code generation consumes the graph.
134
+ * @param output Destination operand list.
135
+ * @param values Source operand ids.
136
+ * @param nodes Graph node table used to inspect nested folds.
137
+ * @param tag Boolean fold kind to flatten.
138
+ */
139
+ function appendFlattenedBooleanValues(output, values, nodes, tag) {
140
+ for (let index = 0; index < values.length; index += 1) {
141
+ const value = values[index];
142
+ if (value === undefined) {
143
+ continue;
144
+ }
145
+ const node = nodes[value];
146
+ if (node?.tag === tag) {
147
+ appendFlattenedBooleanValues(output, node.values, nodes, tag);
148
+ }
149
+ else {
150
+ output.push(value);
151
+ }
152
+ }
153
+ }
154
+ /**
155
+ * @brief Test And absorption for `a && (a || b)`.
156
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
157
+ * before code generation consumes the graph.
158
+ * @param value Candidate operand id.
159
+ * @param nodes Graph node table.
160
+ * @param seen Operands already kept by the parent And.
161
+ * @returns True when the candidate is redundant.
162
+ */
163
+ function isAbsorbedAndValue(value, nodes, seen) {
164
+ const node = nodes[value];
165
+ if (node?.tag !== NodeTag.Or) {
166
+ return false;
167
+ }
168
+ return containsSeenValue(node.values, seen);
169
+ }
170
+ /**
171
+ * @brief Test Or absorption for `a || (a && b)`.
172
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
173
+ * before code generation consumes the graph.
174
+ * @param value Candidate operand id.
175
+ * @param nodes Graph node table.
176
+ * @param seen Operands already kept by the parent Or.
177
+ * @returns True when the candidate is redundant.
178
+ */
179
+ function isAbsorbedOrValue(value, nodes, seen) {
180
+ const node = nodes[value];
181
+ if (node?.tag !== NodeTag.And) {
182
+ return false;
183
+ }
184
+ return containsSeenValue(node.values, seen);
185
+ }
186
+ /**
187
+ * @brief Test whether a nested fold contains an operand already kept.
188
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
189
+ * before code generation consumes the graph.
190
+ * @param values Nested fold operand ids.
191
+ * @param seen Parent fold operands already kept.
192
+ * @returns True when the nested fold repeats a kept operand.
193
+ */
194
+ function containsSeenValue(values, seen) {
195
+ for (let index = 0; index < values.length; index += 1) {
196
+ const value = values[index];
197
+ if (value !== undefined && seen.has(value)) {
198
+ return true;
199
+ }
200
+ }
201
+ return false;
202
+ }
203
+ /**
204
+ * @brief Detect a positive/negative complement conflict.
205
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
206
+ * before code generation consumes the graph.
207
+ * @param value Candidate operand id.
208
+ * @param nodes Graph node table.
209
+ * @param positive Complement-safe positive operands already seen.
210
+ * @param negative Complement-safe negated operands already seen.
211
+ * @returns True when the candidate conflicts with prior operands.
212
+ */
213
+ function hasComplementConflict(value, nodes, positive, negative) {
214
+ const negated = readNegatedValue(value, nodes);
215
+ if (negated !== undefined) {
216
+ return isComplementSafe(negated, nodes) && positive.has(negated);
217
+ }
218
+ return isComplementSafe(value, nodes) && negative.has(value);
219
+ }
220
+ /**
221
+ * @brief Record a complement-safe operand in positive or negative form.
222
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
223
+ * before code generation consumes the graph.
224
+ * @param value Candidate operand id.
225
+ * @param nodes Graph node table.
226
+ * @param positive Mutable set for positive operands.
227
+ * @param negative Mutable set for negated operands.
228
+ */
229
+ function rememberComplementValue(value, nodes, positive, negative) {
230
+ const negated = readNegatedValue(value, nodes);
231
+ if (negated !== undefined) {
232
+ if (isComplementSafe(negated, nodes)) {
233
+ negative.add(negated);
234
+ }
235
+ return;
236
+ }
237
+ if (isComplementSafe(value, nodes)) {
238
+ positive.add(value);
239
+ }
240
+ }
241
+ /**
242
+ * @brief Read the operand of a Not node.
243
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
244
+ * before code generation consumes the graph.
245
+ * @param value Candidate node id.
246
+ * @param nodes Graph node table.
247
+ * @returns Inner node id for Not, otherwise undefined.
248
+ */
249
+ function readNegatedValue(value, nodes) {
250
+ const node = nodes[value];
251
+ return node?.tag === NodeTag.Not ? node.value : undefined;
252
+ }
253
+ /**
254
+ * @brief Decide whether a node is safe for complement algebra.
255
+ * @details Only pure structural predicates are allowed. Nodes that inspect user
256
+ * predicates or nested graph execution stay out of `a`/`!a` folding.
257
+ * @param value Candidate node id.
258
+ * @param nodes Graph node table.
259
+ * @returns True when complement laws can be applied to the node.
260
+ */
261
+ function isComplementSafe(value, nodes) {
262
+ const node = nodes[value];
263
+ if (node === undefined) {
264
+ return false;
265
+ }
266
+ switch (node.tag) {
267
+ case NodeTag.Const:
268
+ return typeof node.value === "boolean";
269
+ case NodeTag.IsString:
270
+ case NodeTag.IsNumber:
271
+ case NodeTag.IsBoolean:
272
+ case NodeTag.IsBigInt:
273
+ case NodeTag.IsSymbol:
274
+ case NodeTag.IsObject:
275
+ case NodeTag.IsArray:
276
+ case NodeTag.IsUndefined:
277
+ case NodeTag.IsNull:
278
+ case NodeTag.IsInteger:
279
+ case NodeTag.Equals:
280
+ case NodeTag.Gte:
281
+ case NodeTag.Lte:
282
+ case NodeTag.StringMin:
283
+ case NodeTag.StringMax:
284
+ case NodeTag.Regex:
285
+ case NodeTag.HasOwn:
286
+ case NodeTag.HasOwnData:
287
+ case NodeTag.StrictKeys:
288
+ return true;
289
+ case NodeTag.Not:
290
+ return isComplementSafe(node.value, nodes);
291
+ case NodeTag.And:
292
+ case NodeTag.Or:
293
+ return areComplementSafeValues(node.values, nodes);
294
+ default:
295
+ return false;
296
+ }
297
+ }
298
+ /**
299
+ * @brief Decide whether every value in a boolean fold is complement-safe.
300
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
301
+ * before code generation consumes the graph.
302
+ * @param values Operand ids from a nested fold.
303
+ * @param nodes Graph node table.
304
+ * @returns True when the whole nested fold can participate in complement laws.
305
+ */
306
+ function areComplementSafeValues(values, nodes) {
307
+ for (let index = 0; index < values.length; index += 1) {
308
+ const value = values[index];
309
+ if (value === undefined || !isComplementSafe(value, nodes)) {
310
+ return false;
311
+ }
312
+ }
313
+ return true;
314
+ }
@@ -1,10 +1,17 @@
1
1
  /**
2
2
  * @file compact.ts
3
3
  * @brief Reachability compaction for optimized graphs.
4
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
5
+ * before code generation consumes the graph.
4
6
  */
5
7
  import type { Graph } from "../ir/index.js";
6
8
  /**
7
- * @brief compact reachable.
9
+ * @brief Remove unreachable IR nodes and remap the remaining node ids densely.
10
+ * @details Earlier optimizer passes may leave replaced nodes in the arena so
11
+ * alias bookkeeping stays simple. This pass is the boundary that rewrites the
12
+ * graph back into the dense id space expected by codegen and public graph reads.
13
+ * @param graph Graph produced by earlier optimizer passes.
14
+ * @returns Graph containing only nodes reachable from entry and result.
8
15
  */
9
16
  export declare function compactReachable(graph: Graph): Graph;
10
17
  //# sourceMappingURL=compact.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"compact.d.ts","sourceRoot":"","sources":["../../src/optimize/compact.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAqB,MAAM,gBAAgB,CAAC;AAG/D;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,CAyBpD"}
1
+ {"version":3,"file":"compact.d.ts","sourceRoot":"","sources":["../../src/optimize/compact.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAqB,MAAM,gBAAgB,CAAC;AAG/D;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,CAyBpD"}
@@ -1,10 +1,17 @@
1
1
  /**
2
2
  * @file compact.ts
3
3
  * @brief Reachability compaction for optimized graphs.
4
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
5
+ * before code generation consumes the graph.
4
6
  */
5
7
  import { remapId, remapNode } from "./remap.js";
6
8
  /**
7
- * @brief compact reachable.
9
+ * @brief Remove unreachable IR nodes and remap the remaining node ids densely.
10
+ * @details Earlier optimizer passes may leave replaced nodes in the arena so
11
+ * alias bookkeeping stays simple. This pass is the boundary that rewrites the
12
+ * graph back into the dense id space expected by codegen and public graph reads.
13
+ * @param graph Graph produced by earlier optimizer passes.
14
+ * @returns Graph containing only nodes reachable from entry and result.
8
15
  */
9
16
  export function compactReachable(graph) {
10
17
  const reachable = markReachable(graph);
@@ -33,7 +40,11 @@ export function compactReachable(graph) {
33
40
  };
34
41
  }
35
42
  /**
36
- * @brief mark reachable.
43
+ * @brief Mark graph nodes reachable from the entry and result roots.
44
+ * @details The scan walks dependency edges with an explicit stack to avoid
45
+ * recursion limits on large generated schemas.
46
+ * @param graph Graph to scan.
47
+ * @returns Boolean reachability bitmap aligned with the original node table.
37
48
  */
38
49
  function markReachable(graph) {
39
50
  const reachable = new Array(graph.nodes.length).fill(false);
@@ -0,0 +1,16 @@
1
+ /**
2
+ * @file domain.ts
3
+ * @brief Validation-domain specialization for dispatch child graphs.
4
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
5
+ * before code generation consumes the graph.
6
+ */
7
+ import type { Graph } from "../ir/index.js";
8
+ /**
9
+ * @brief Specialize nested validation graphs with domain knowledge.
10
+ * @details Dispatch nodes already know the primitive domain of each child arm.
11
+ * This pass pushes that fact into child graphs so redundant type checks collapse.
12
+ * @param graph Root graph to specialize.
13
+ * @returns The original graph when no node changed, otherwise a rewritten graph.
14
+ */
15
+ export declare function specializeDomains(graph: Graph): Graph;
16
+ //# sourceMappingURL=domain.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"domain.d.ts","sourceRoot":"","sources":["../../src/optimize/domain.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAER,KAAK,EAIR,MAAM,gBAAgB,CAAC;AAiDxB;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,CAsBrD"}