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
@@ -1,15 +1,27 @@
1
1
  /**
2
2
  * @file fold-boolean.ts
3
3
  * @brief Constant folding for boolean fold nodes.
4
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
5
+ * before code generation consumes the graph.
4
6
  */
5
7
  import { ensureConst, keep, readConst, replace } from "./fold-common.js";
8
+ import { flattenBooleanValues, simplifyAndValues, simplifyOrValues } from "./algebraic.js";
9
+ import { canonicalizeAndValues, canonicalizeOrValues } from "./fold-constraints.js";
6
10
  /**
7
- * @brief fold and.
11
+ * @brief Simplify an `And` fold node using boolean identities and constraints.
12
+ * @details False annihilates the whole conjunction, true disappears, nested
13
+ * conjunctions are flattened, and scalar constraints are canonicalized after
14
+ * algebraic simplification.
15
+ * @param node Boolean fold node to simplify.
16
+ * @param nodes Mutable graph node table for constant interning.
17
+ * @param aliases Mutable alias table updated when a constant is inserted.
18
+ * @returns Fold result for the conjunction.
8
19
  */
9
20
  export function foldAnd(node, nodes, aliases) {
10
21
  const values = [];
11
- for (let index = 0; index < node.values.length; index += 1) {
12
- const value = node.values[index];
22
+ const flattened = flattenBooleanValues(node.values, nodes, node.tag);
23
+ for (let index = 0; index < flattened.length; index += 1) {
24
+ const value = flattened[index];
13
25
  if (value === undefined) {
14
26
  continue;
15
27
  }
@@ -25,8 +37,23 @@ export function foldAnd(node, nodes, aliases) {
25
37
  if (values.length === 0) {
26
38
  return replace(node, ensureConst(nodes, aliases, true));
27
39
  }
28
- if (values.length === 1) {
29
- const only = values[0];
40
+ const algebraic = simplifyAndValues(values, nodes);
41
+ if (algebraic.contradiction) {
42
+ return replace(node, ensureConst(nodes, aliases, false));
43
+ }
44
+ if (algebraic.values.length === 0) {
45
+ return replace(node, ensureConst(nodes, aliases, true));
46
+ }
47
+ const canonical = canonicalizeAndValues(algebraic.values, nodes);
48
+ if (canonical.contradiction) {
49
+ return replace(node, ensureConst(nodes, aliases, false));
50
+ }
51
+ const canonicalValues = canonical.values;
52
+ if (canonicalValues.length === 0) {
53
+ return replace(node, ensureConst(nodes, aliases, true));
54
+ }
55
+ if (canonicalValues.length === 1) {
56
+ const only = canonicalValues[0];
30
57
  if (only !== undefined) {
31
58
  return replace(node, only);
32
59
  }
@@ -34,17 +61,24 @@ export function foldAnd(node, nodes, aliases) {
34
61
  return keep({
35
62
  id: node.id,
36
63
  tag: node.tag,
37
- deps: values,
38
- values
64
+ deps: canonicalValues,
65
+ values: canonicalValues
39
66
  });
40
67
  }
41
68
  /**
42
- * @brief fold or.
69
+ * @brief Simplify an `Or` fold node using boolean identities and tautologies.
70
+ * @details True satisfies the whole disjunction, false disappears, nested
71
+ * disjunctions are flattened, and duplicate surviving arms are removed.
72
+ * @param node Boolean fold node to simplify.
73
+ * @param nodes Mutable graph node table for constant interning.
74
+ * @param aliases Mutable alias table updated when a constant is inserted.
75
+ * @returns Fold result for the disjunction.
43
76
  */
44
77
  export function foldOr(node, nodes, aliases) {
45
78
  const values = [];
46
- for (let index = 0; index < node.values.length; index += 1) {
47
- const value = node.values[index];
79
+ const flattened = flattenBooleanValues(node.values, nodes, node.tag);
80
+ for (let index = 0; index < flattened.length; index += 1) {
81
+ const value = flattened[index];
48
82
  if (value === undefined) {
49
83
  continue;
50
84
  }
@@ -60,8 +94,19 @@ export function foldOr(node, nodes, aliases) {
60
94
  if (values.length === 0) {
61
95
  return replace(node, ensureConst(nodes, aliases, false));
62
96
  }
63
- if (values.length === 1) {
64
- const only = values[0];
97
+ const algebraic = simplifyOrValues(values, nodes);
98
+ if (algebraic.tautology) {
99
+ return replace(node, ensureConst(nodes, aliases, true));
100
+ }
101
+ if (algebraic.values.length === 0) {
102
+ return replace(node, ensureConst(nodes, aliases, false));
103
+ }
104
+ const canonicalValues = canonicalizeOrValues(algebraic.values);
105
+ if (canonicalValues.length === 0) {
106
+ return replace(node, ensureConst(nodes, aliases, false));
107
+ }
108
+ if (canonicalValues.length === 1) {
109
+ const only = canonicalValues[0];
65
110
  if (only !== undefined) {
66
111
  return replace(node, only);
67
112
  }
@@ -69,7 +114,7 @@ export function foldOr(node, nodes, aliases) {
69
114
  return keep({
70
115
  id: node.id,
71
116
  tag: node.tag,
72
- deps: values,
73
- values
117
+ deps: canonicalValues,
118
+ values: canonicalValues
74
119
  });
75
120
  }
@@ -1,45 +1,80 @@
1
1
  /**
2
2
  * @file fold-common.ts
3
3
  * @brief Shared constant-folding helpers.
4
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
5
+ * before code generation consumes the graph.
4
6
  */
5
7
  import type { GraphNode, NodeId } from "../ir/index.js";
6
8
  import type { LiteralValue } from "../schema/index.js";
7
9
  /**
8
- * @brief const read.
10
+ * @brief Result of probing a node slot for a constant value.
11
+ * @details `found` is separate from `value` because `undefined` is itself a
12
+ * valid literal constant in the graph.
9
13
  */
10
14
  export interface ConstRead {
11
15
  readonly found: boolean;
12
16
  readonly value: LiteralValue | undefined;
13
17
  }
14
18
  /**
15
- * @brief fold result.
19
+ * @brief Node rewrite result returned by scalar folding helpers.
20
+ * @details The node carries the possibly edited payload while `replacement`
21
+ * records aliasing information for later dependency rewrites.
16
22
  */
17
23
  export interface FoldResult {
18
24
  readonly node: GraphNode;
19
25
  readonly replacement: NodeId | undefined;
20
26
  }
21
27
  /**
22
- * @brief ensure const.
28
+ * @brief Reuse or append a constant node for a literal value.
29
+ * @details Constants are interned with `Object.is` so NaN, -0, and undefined
30
+ * keep JavaScript literal identity semantics. The alias table is updated
31
+ * immediately because later folders may depend on the new node id.
32
+ * @param nodes Mutable graph node table for the folding pass.
33
+ * @param aliases Mutable node alias table kept in step with `nodes`.
34
+ * @param value Literal value that must exist in the graph.
35
+ * @returns Node id of the existing or newly appended constant.
23
36
  */
24
37
  export declare function ensureConst(nodes: GraphNode[], aliases: NodeId[], value: LiteralValue): NodeId;
25
38
  /**
26
- * @brief read const.
39
+ * @brief Probe a node id for an IR constant without conflating misses.
40
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
41
+ * before code generation consumes the graph.
42
+ * @param nodes Graph node table being optimized.
43
+ * @param id Node id to inspect.
44
+ * @returns Const probe result with an explicit found bit.
27
45
  */
28
46
  export declare function readConst(nodes: readonly GraphNode[], id: NodeId): ConstRead;
29
47
  /**
30
- * @brief keep.
48
+ * @brief Return a fold result that keeps the node in place.
49
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
50
+ * before code generation consumes the graph.
51
+ * @param node Node after local payload edits.
52
+ * @returns Fold result without an alias replacement.
31
53
  */
32
54
  export declare function keep(node: GraphNode): FoldResult;
33
55
  /**
34
- * @brief replace.
56
+ * @brief Return a fold result that aliases this node to another node id.
57
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
58
+ * before code generation consumes the graph.
59
+ * @param node Original node retained for pass bookkeeping.
60
+ * @param replacement Node id that should replace references to `node`.
61
+ * @returns Fold result carrying the alias replacement.
35
62
  */
36
63
  export declare function replace(node: GraphNode, replacement: NodeId): FoldResult;
37
64
  /**
38
- * @brief is finite number.
65
+ * @brief Check whether a value is a finite JavaScript number.
66
+ * @details Numeric bound folding excludes NaN and infinities because order
67
+ * comparisons against those values do not form useful closed validation ranges.
68
+ * @param value Candidate literal value.
69
+ * @returns True when the value is a finite number.
39
70
  */
40
71
  export declare function isFiniteNumber(value: unknown): boolean;
41
72
  /**
42
- * @brief is plain record.
73
+ * @brief Check whether a value is an object-like record for fold metadata.
74
+ * @details Arrays are excluded because optimizer metadata objects use named
75
+ * fields, and accepting arrays would hide malformed pass state.
76
+ * @param value Candidate metadata value.
77
+ * @returns True when the value is a non-array object.
43
78
  */
44
79
  export declare function isPlainRecord(value: unknown): boolean;
45
80
  //# sourceMappingURL=fold-common.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"fold-common.d.ts","sourceRoot":"","sources":["../../src/optimize/fold-common.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AACxD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEvD;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC;IACxB,QAAQ,CAAC,KAAK,EAAE,YAAY,GAAG,SAAS,CAAC;CAC1C;AAED;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IACzB,QAAQ,CAAC,WAAW,EAAE,MAAM,GAAG,SAAS,CAAC;CAC1C;AAED;;GAEG;AACH,wBAAgB,WAAW,CACzB,KAAK,EAAE,SAAS,EAAE,EAClB,OAAO,EAAE,MAAM,EAAE,EACjB,KAAK,EAAE,YAAY,GAClB,MAAM,CAgBR;AAED;;GAEG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,SAAS,SAAS,EAAE,EAAE,EAAE,EAAE,MAAM,GAAG,SAAS,CAY5E;AAED;;GAEG;AACH,wBAAgB,IAAI,CAAC,IAAI,EAAE,SAAS,GAAG,UAAU,CAKhD;AAED;;GAEG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,GAAG,UAAU,CAKxE;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAEtD;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAErD"}
1
+ {"version":3,"file":"fold-common.d.ts","sourceRoot":"","sources":["../../src/optimize/fold-common.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AACxD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEvD;;;;GAIG;AACH,MAAM,WAAW,SAAS;IACtB,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC;IACxB,QAAQ,CAAC,KAAK,EAAE,YAAY,GAAG,SAAS,CAAC;CAC5C;AAED;;;;GAIG;AACH,MAAM,WAAW,UAAU;IACvB,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IACzB,QAAQ,CAAC,WAAW,EAAE,MAAM,GAAG,SAAS,CAAC;CAC5C;AAED;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CACvB,KAAK,EAAE,SAAS,EAAE,EAClB,OAAO,EAAE,MAAM,EAAE,EACjB,KAAK,EAAE,YAAY,GACpB,MAAM,CAgBR;AAED;;;;;;;GAOG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,SAAS,SAAS,EAAE,EAAE,EAAE,EAAE,MAAM,GAAG,SAAS,CAY5E;AAED;;;;;;GAMG;AACH,wBAAgB,IAAI,CAAC,IAAI,EAAE,SAAS,GAAG,UAAU,CAKhD;AAED;;;;;;;GAOG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,GAAG,UAAU,CAKxE;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAEtD;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAErD"}
@@ -1,10 +1,19 @@
1
1
  /**
2
2
  * @file fold-common.ts
3
3
  * @brief Shared constant-folding helpers.
4
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
5
+ * before code generation consumes the graph.
4
6
  */
5
7
  import { NodeTag } from "../kind/index.js";
6
8
  /**
7
- * @brief ensure const.
9
+ * @brief Reuse or append a constant node for a literal value.
10
+ * @details Constants are interned with `Object.is` so NaN, -0, and undefined
11
+ * keep JavaScript literal identity semantics. The alias table is updated
12
+ * immediately because later folders may depend on the new node id.
13
+ * @param nodes Mutable graph node table for the folding pass.
14
+ * @param aliases Mutable node alias table kept in step with `nodes`.
15
+ * @param value Literal value that must exist in the graph.
16
+ * @returns Node id of the existing or newly appended constant.
8
17
  */
9
18
  export function ensureConst(nodes, aliases, value) {
10
19
  for (let index = 0; index < nodes.length; index += 1) {
@@ -24,7 +33,12 @@ export function ensureConst(nodes, aliases, value) {
24
33
  return id;
25
34
  }
26
35
  /**
27
- * @brief read const.
36
+ * @brief Probe a node id for an IR constant without conflating misses.
37
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
38
+ * before code generation consumes the graph.
39
+ * @param nodes Graph node table being optimized.
40
+ * @param id Node id to inspect.
41
+ * @returns Const probe result with an explicit found bit.
28
42
  */
29
43
  export function readConst(nodes, id) {
30
44
  const node = nodes[id];
@@ -40,7 +54,11 @@ export function readConst(nodes, id) {
40
54
  };
41
55
  }
42
56
  /**
43
- * @brief keep.
57
+ * @brief Return a fold result that keeps the node in place.
58
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
59
+ * before code generation consumes the graph.
60
+ * @param node Node after local payload edits.
61
+ * @returns Fold result without an alias replacement.
44
62
  */
45
63
  export function keep(node) {
46
64
  return {
@@ -49,7 +67,12 @@ export function keep(node) {
49
67
  };
50
68
  }
51
69
  /**
52
- * @brief replace.
70
+ * @brief Return a fold result that aliases this node to another node id.
71
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
72
+ * before code generation consumes the graph.
73
+ * @param node Original node retained for pass bookkeeping.
74
+ * @param replacement Node id that should replace references to `node`.
75
+ * @returns Fold result carrying the alias replacement.
53
76
  */
54
77
  export function replace(node, replacement) {
55
78
  return {
@@ -58,13 +81,21 @@ export function replace(node, replacement) {
58
81
  };
59
82
  }
60
83
  /**
61
- * @brief is finite number.
84
+ * @brief Check whether a value is a finite JavaScript number.
85
+ * @details Numeric bound folding excludes NaN and infinities because order
86
+ * comparisons against those values do not form useful closed validation ranges.
87
+ * @param value Candidate literal value.
88
+ * @returns True when the value is a finite number.
62
89
  */
63
90
  export function isFiniteNumber(value) {
64
91
  return typeof value === "number" && Number.isFinite(value);
65
92
  }
66
93
  /**
67
- * @brief is plain record.
94
+ * @brief Check whether a value is an object-like record for fold metadata.
95
+ * @details Arrays are excluded because optimizer metadata objects use named
96
+ * fields, and accepting arrays would hide malformed pass state.
97
+ * @param value Candidate metadata value.
98
+ * @returns True when the value is a non-array object.
68
99
  */
69
100
  export function isPlainRecord(value) {
70
101
  return typeof value === "object" && value !== null && !Array.isArray(value);
@@ -0,0 +1,33 @@
1
+ /**
2
+ * @file fold-constraints.ts
3
+ * @brief Boolean constraint canonicalization for pure predicate runs.
4
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
5
+ * before code generation consumes the graph.
6
+ */
7
+ import type { GraphNode, NodeId } from "../ir/index.js";
8
+ /**
9
+ * @brief Canonicalized input list for a boolean fold node.
10
+ * @details Contradictions are represented separately from an empty value list so
11
+ * callers can distinguish a provably false conjunction from a fold that simply
12
+ * had no surviving children.
13
+ */
14
+ export interface ConstraintFold {
15
+ readonly contradiction: boolean;
16
+ readonly values: readonly NodeId[];
17
+ }
18
+ /**
19
+ * @brief canonicalize and values.
20
+ * @details Removes duplicate predicates and dominated scalar bounds while keeping
21
+ * the first observable execution order intact. Only the pure prefix may collapse
22
+ * the whole conjunction to `false`.
23
+ * @returns Canonical node id vector or a contradiction marker.
24
+ */
25
+ export declare function canonicalizeAndValues(values: readonly NodeId[], nodes: readonly GraphNode[]): ConstraintFold;
26
+ /**
27
+ * @brief canonicalize or values.
28
+ * @details Removes repeated node ids. Repeated ids are already memoized by graph
29
+ * execution, so this only shrinks emitted boolean expressions.
30
+ * @returns Canonical node id vector.
31
+ */
32
+ export declare function canonicalizeOrValues(values: readonly NodeId[]): readonly NodeId[];
33
+ //# sourceMappingURL=fold-constraints.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fold-constraints.d.ts","sourceRoot":"","sources":["../../src/optimize/fold-constraints.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EACR,SAAS,EACT,MAAM,EACT,MAAM,gBAAgB,CAAC;AAGxB;;;;;GAKG;AACH,MAAM,WAAW,cAAc;IAC3B,QAAQ,CAAC,aAAa,EAAE,OAAO,CAAC;IAChC,QAAQ,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC;CACtC;AAoED;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CACjC,MAAM,EAAE,SAAS,MAAM,EAAE,EACzB,KAAK,EAAE,SAAS,SAAS,EAAE,GAC5B,cAAc,CA6BhB;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,GAAG,SAAS,MAAM,EAAE,CAYjF"}