@fraczak/k 6.1.2 → 6.2.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 (76) hide show
  1. package/AGENTS.md +4 -4
  2. package/CLAUDE.md +1 -1
  3. package/DOCS/BIG_PICTURE.md +10 -10
  4. package/DOCS/DIVERGENCE_DETECTION.md +1 -1
  5. package/DOCS/OBJECT_FILE_AND_PATTERN.md +6 -0
  6. package/DOCS/REPL.md +22 -17
  7. package/DOCS/TYPE_DERIVATION.md +12 -12
  8. package/DOCS/book/01-introduction.md +2 -2
  9. package/DOCS/book/02-syntax-and-values.md +8 -8
  10. package/DOCS/book/03-types-as-finite-automata.md +2 -2
  11. package/DOCS/book/04-partial-functions-and-composition.md +2 -2
  12. package/DOCS/book/05-typing-filters-and-normalization.md +3 -3
  13. package/DOCS/book/06-values-in-memory.md +3 -3
  14. package/DOCS/book/07-the-partial-function-abi.md +1 -1
  15. package/DOCS/book/08-operational-semantics-and-execution.md +1 -1
  16. package/DOCS/book/09-compiler-architecture.md +2 -2
  17. package/DOCS/book/appendix-b-incorporating-external-predefined-types-and-functions.md +1 -0
  18. package/README.md +13 -13
  19. package/codecs/BINARY_FORMAT.md +1 -1
  20. package/codecs/POLYMORPHIC_BINARY_FORMAT.md +1 -1
  21. package/codes.mjs +14 -33
  22. package/export.mjs +13 -3
  23. package/k-vscode/README.md +10 -1
  24. package/k-vscode/extension.js +273 -0
  25. package/k-vscode/package.json +22 -0
  26. package/k-vscode/syntaxes/k.tmLanguage.json +493 -137
  27. package/k-vscode/test/background-ranges.test.js +50 -0
  28. package/k-vscode/themes/k-contrast-color-theme.json +104 -0
  29. package/object.mjs +20 -7
  30. package/package.json +3 -2
  31. package/repl.mjs +43 -2
  32. package/scripts/benchmark-idempotency.mjs +72 -0
  33. package/scripts/run-tests.mjs +54 -0
  34. package/{Code-derivation-tests → tests/code-derivation}/codec-witness-pattern.mjs +3 -3
  35. package/{Code-derivation-tests → tests/code-derivation}/convergence-strategy.mjs +1 -1
  36. package/{Code-derivation-tests → tests/code-derivation}/dot.mjs +1 -1
  37. package/{Code-derivation-tests → tests/code-derivation}/filter-syntax.mjs +1 -1
  38. package/{Code-derivation-tests → tests/code-derivation}/index.mjs +1 -1
  39. package/{Code-derivation-tests → tests/code-derivation}/pattern-envelope.mjs +5 -5
  40. package/{Code-derivation-tests → tests/code-derivation}/product.mjs +1 -1
  41. package/{tests.sh → tests/integration.sh} +3 -1
  42. package/{test-fingerprint.mjs → tests/test-fingerprint.mjs} +3 -3
  43. package/{test-hash-fuzz.mjs → tests/test-hash-fuzz.mjs} +1 -1
  44. package/{test-hash-normalization.mjs → tests/test-hash-normalization.mjs} +2 -2
  45. package/{test-ieee-arithmetic.mjs → tests/test-ieee-arithmetic.mjs} +14 -27
  46. package/{test-k-object.mjs → tests/test-k-object.mjs} +26 -7
  47. package/{test-repl.mjs → tests/test-repl.mjs} +22 -4
  48. package/{test.mjs → tests/test.mjs} +1 -1
  49. package/.codex +0 -0
  50. package/DOCS/a.html +0 -398
  51. package/DOCS/dependency-graph.html +0 -140
  52. package/DOCS/dependency-graph.md +0 -144
  53. package/NAT.k~ +0 -452
  54. package/UU/ta.k +0 -1
  55. package/UU/ta.k~ +0 -0
  56. package/UU/tb.k +0 -0
  57. package/UU/tc.k +0 -0
  58. package/codecs/BINARY_FORMAT.html +0 -471
  59. package/codecs/POLYMORPHIC_BINARY_FORMAT.html +0 -355
  60. package/core.json +0 -1
  61. package/core.klib +0 -1
  62. package/core.klib~ +0 -0
  63. package/ieee.klib +0 -1
  64. package/k-vscode/package-lock.json +0 -4176
  65. package/kkk.k +0 -197
  66. package/kkk.klib +0 -0
  67. package/kkk.klib~ +0 -0
  68. package/p.json +0 -5475
  69. package/type_registry/registry.mjs~ +0 -105
  70. package/type_registry/typeParser.jison~ +0 -132
  71. /package/{Code-derivation-tests → tests/code-derivation}/blowup.mjs +0 -0
  72. /package/{Code-derivation-tests → tests/code-derivation}/bnat.mjs +0 -0
  73. /package/{Code-derivation-tests → tests/code-derivation}/loop.mjs +0 -0
  74. /package/{Code-derivation-tests → tests/code-derivation}/nat.mjs +0 -0
  75. /package/{Code-derivation-tests → tests/code-derivation}/not-not.mjs +0 -0
  76. /package/{Code-derivation-tests → tests/code-derivation}/rlz.mjs +0 -0
@@ -16,9 +16,9 @@ import {
16
16
  isMainEntrypoint,
17
17
  lineHasExplicitContinuation,
18
18
  lineTerminatesSnippet
19
- } from "./repl.mjs";
20
- import { decodeObject, objectToFunction } from "./object.mjs";
21
- import { Product } from "./Value.mjs";
19
+ } from "../repl.mjs";
20
+ import { decodeObject, objectToFunction } from "../object.mjs";
21
+ import { Product } from "../Value.mjs";
22
22
 
23
23
  const state = createState();
24
24
 
@@ -90,6 +90,7 @@ await assert.rejects(
90
90
 
91
91
  const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "k-repl-"));
92
92
  const libPath = path.join(tmpDir, "session.klib");
93
+ const recursiveLibPath = path.join(tmpDir, "recursive.klib");
93
94
  const koPath = path.join(tmpDir, "succ.ko");
94
95
  const sourcePath = path.join(tmpDir, "session.k");
95
96
  const codecPath = path.join(tmpDir, "bool-codec.mjs");
@@ -97,6 +98,23 @@ const symlinkPath = path.resolve(".test-k-repl-link");
97
98
  output = await evaluateInput(`:klib ${libPath}`, state);
98
99
  assert.equal(output[0], `saved ${libPath}`);
99
100
 
101
+ const recursiveState = createState();
102
+ await evaluateInput(":type $ nat = < {}_, nat 0, nat 1 >;", recursiveState);
103
+ await evaluateInput(":rel 0? = $ nat < / _ {}|0, / 0 0? >;", recursiveState);
104
+ const staleZeroHash = recursiveState.relAliases["0?"];
105
+ await evaluateInput(":rel 0? = $ nat < / _ {}|_, / 0 0? >;", recursiveState);
106
+ const zeroHash = recursiveState.relAliases["0?"];
107
+ assert.notEqual(zeroHash, staleZeroHash);
108
+ await evaluateInput("{}|_|0|0 $nat", recursiveState);
109
+ assert.deepEqual(
110
+ Object.keys(recursiveState.rels).sort(),
111
+ [staleZeroHash, zeroHash].sort()
112
+ );
113
+ output = await evaluateInput(`:klib ${recursiveLibPath}`, recursiveState);
114
+ assert.equal(output[0], `saved ${recursiveLibPath}`);
115
+ const recursiveLibrary = JSON.parse(fs.readFileSync(recursiveLibPath, "utf8"));
116
+ assert.deepEqual(Object.keys(recursiveLibrary.rels), [zeroHash]);
117
+
100
118
  completions = completeInput(`:load ${tmpDir}/ses`, state)[0];
101
119
  assert(completions.includes(`:load ${libPath}`));
102
120
  completions = completeInput(`:load --no-alias ${tmpDir}/ses`, state)[0];
@@ -176,7 +194,7 @@ assert.equal(output[0], `input ${utf8State.typeAliases.string} using utf8: enter
176
194
  output = await evaluateInput("hello", utf8State);
177
195
  assert.match(output[0], /utf8: hello/);
178
196
 
179
- const replPath = fileURLToPath(new URL("./repl.mjs", import.meta.url));
197
+ const replPath = fileURLToPath(new URL("../repl.mjs", import.meta.url));
180
198
  try {
181
199
  fs.rmSync(symlinkPath, { force: true });
182
200
  } catch {}
@@ -1,4 +1,4 @@
1
- import k from "./index.mjs";
1
+ import k from "../index.mjs";
2
2
 
3
3
  [
4
4
  [{"unit": {}}, "()"],
package/.codex DELETED
File without changes
package/DOCS/a.html DELETED
@@ -1,398 +0,0 @@
1
- <h1 id="patterns-and-typed-values">Patterns And Typed Values</h1>
2
- <h2 id="abstract">Abstract</h2>
3
- <p>This note proposes a semantic foundation for <code>k</code> patterns,
4
- types, typed values, and polymorphic values. The central point is that a
5
- value in <code>k</code> is not a bare tree: it exists only in the
6
- context of a type. Accordingly, a pattern denotes a set of types, and
7
- therefore also a set of <strong>typed</strong> values, not a set of raw
8
- trees. This distinction is essential for reasoning about equality,
9
- polymorphism, and binary serialization with DAG compression.</p>
10
- <h2 id="introduction">1. Introduction</h2>
11
- <p>The <code>k</code> language manipulates algebraic values built from
12
- products and unions. At first sight one may be tempted to define a value
13
- simply as a rooted labeled tree and then define types as sets of such
14
- trees. For <code>k</code>, this is not the right semantic level.</p>
15
- <p>Two trees with the same shape but belonging to different types are
16
- different values. Type is part of value identity.</p>
17
- <p>This leads to the following semantic ladder:</p>
18
- <ul>
19
- <li>a <strong>type</strong> denotes a set of typed values,</li>
20
- <li>a <strong>pattern</strong> denotes a set of types,</li>
21
- <li>therefore a <strong>pattern</strong> also denotes a set of typed
22
- values,</li>
23
- <li>a <strong>polymorphic value</strong> is a pattern together with a
24
- typed value compatible with that pattern.</li>
25
- </ul>
26
- <p>The rest of this note makes these notions precise.</p>
27
- <h2 id="types">2. Types</h2>
28
- <p>A <strong>type graph</strong> is a finite rooted directed graph with
29
- labeled edges. Each node is one of:</p>
30
- <ul>
31
- <li><code>product</code>,</li>
32
- <li><code>union</code>.</li>
33
- </ul>
34
- <p>For a product node, outgoing edge labels are field names. For a union
35
- node, outgoing edge labels are tags. Edges are locally unique by
36
- label.</p>
37
- <p>The graph may be cyclic, which allows recursive types.</p>
38
- <p>Two type graphs that are equivalent as regular tree languages may be
39
- identified. For types, minimization by bisimulation is appropriate.</p>
40
- <h3 id="trees-accepted-by-a-type">2.1. Trees accepted by a type</h3>
41
- <p>Let <code>Tree(T)</code> denote the set of rooted value trees
42
- accepted by a type graph <code>T</code>. Acceptance is defined in the
43
- standard way:</p>
44
- <ul>
45
- <li>a product tree is accepted by a product node when its fields match
46
- the outgoing labels of that node and each child tree is accepted by the
47
- corresponding target node,</li>
48
- <li>a union tree is accepted by a union node when it chooses exactly one
49
- outgoing tag and the child tree is accepted by the corresponding target
50
- node.</li>
51
- </ul>
52
- <h2 id="typed-values">3. Typed Values</h2>
53
- <p>A <strong>typed value</strong> is a pair</p>
54
- <pre class="text"><code>(T, t)</code></pre>
55
- <p>where:</p>
56
- <ul>
57
- <li><code>T</code> is a type graph,</li>
58
- <li><code>t ∈ Tree(T)</code>.</li>
59
- </ul>
60
- <p>Thus a value is intrinsically typed. The tree <code>t</code> is only
61
- a representation of the value; it is not, by itself, a semantic value of
62
- the language.</p>
63
- <p>We write:</p>
64
- <pre class="text"><code>Val(T) = { (T, t) : t ∈ Tree(T) }.</code></pre>
65
- <h3 id="identity-of-typed-values">3.1. Identity of typed values</h3>
66
- <p>Typed values are intensional with respect to the type:</p>
67
- <pre class="text"><code>(T, t) ≠ (U, t) whenever T ≠ U.</code></pre>
68
- <p>Even when the same raw tree <code>t</code> is accepted by both
69
- <code>T</code> and <code>U</code>, the two typed values are
70
- different.</p>
71
- <p>This point is essential for serialization: the identity of a value
72
- cannot be recovered from the raw tree alone.</p>
73
- <h2 id="patterns">4. Patterns</h2>
74
- <p>A <strong>pattern graph</strong> is a finite rooted directed graph
75
- with labeled edges. Each node carries:</p>
76
- <ul>
77
- <li>a node kind:
78
- <ul>
79
- <li><code>unknown</code>,</li>
80
- <li><code>product</code>,</li>
81
- <li><code>union</code>,</li>
82
- </ul></li>
83
- <li>an openness flag:
84
- <ul>
85
- <li><code>open</code>,</li>
86
- <li><code>closed</code>.</li>
87
- </ul></li>
88
- </ul>
89
- <p>For product and union nodes, outgoing edges are labeled by field
90
- names or tags. Edges are locally unique by label.</p>
91
- <p>Unlike the case of types, <strong>node identity in a pattern graph is
92
- semantic</strong>. If two edges point to the same pattern node, this
93
- means that the corresponding positions must be inhabited by the very
94
- same type.</p>
95
- <p>Therefore patterns are not quotiented by bisimulation in general.</p>
96
- <h2 id="filters-as-syntax">5. Filters As Syntax</h2>
97
- <p>A filter expression is a textual way of describing a pattern
98
- graph.</p>
99
- <p>Variables appearing in filters are not the semantic object itself.
100
- They are a device for serializing and reusing pattern graph nodes in
101
- syntax.</p>
102
- <p>For example, the two filters</p>
103
- <pre class="k"><code>{ (...) x, (...) y, ... }
104
- { X x, X y, ... }</code></pre>
105
- <p>describe different pattern graphs:</p>
106
- <ul>
107
- <li>the first has two distinct target nodes for <code>x</code> and
108
- <code>y</code>,</li>
109
- <li>the second has one shared target node reached from both
110
- <code>x</code> and <code>y</code>.</li>
111
- </ul>
112
- <p>These two patterns are semantically different.</p>
113
- <h2 id="matching-a-type-against-a-pattern">6. Matching A Type Against A
114
- Pattern</h2>
115
- <p>Let <code>P</code> be a pattern graph and <code>T</code> a type
116
- graph. We say that <code>T</code> <strong>matches</strong>
117
- <code>P</code>, written</p>
118
- <pre class="text"><code>T ⊨ P,</code></pre>
119
- <p>if there exists a root-preserving graph morphism</p>
120
- <pre class="text"><code>h : nodes(P) → nodes(T)</code></pre>
121
- <p>such that:</p>
122
- <ol type="1">
123
- <li><p>if <code>p</code> is <code>unknown</code>, then <code>h(p)</code>
124
- may be any type node,</p></li>
125
- <li><p>if <code>p</code> is <code>product</code>, then <code>h(p)</code>
126
- is a product node,</p></li>
127
- <li><p>if <code>p</code> is <code>union</code>, then <code>h(p)</code>
128
- is a union node,</p></li>
129
- <li><p>if <code>p</code> is <code>closed</code>, then the outgoing
130
- labels of <code>h(p)</code> are exactly the outgoing labels of
131
- <code>p</code>,</p></li>
132
- <li><p>if <code>p</code> is <code>open</code>, then the outgoing labels
133
- of <code>h(p)</code> contain at least the outgoing labels of
134
- <code>p</code>,</p></li>
135
- <li><p>for every pattern edge</p>
136
- <pre class="text"><code>p --l--&gt; q</code></pre>
137
- <p>there is a corresponding type edge</p>
138
- <pre class="text"><code>h(p) --l--&gt; h(q).</code></pre></li>
139
- </ol>
140
- <p>Condition 6 is the crucial one: if two edges of <code>P</code> point
141
- to the same node <code>q</code>, then the corresponding edges of
142
- <code>T</code> must point to the same type node <code>h(q)</code>. This
143
- is how the graph structure of the pattern expresses “the same type”.</p>
144
- <p>We define:</p>
145
- <pre class="text"><code>Types(P) = { T : T ⊨ P }.</code></pre>
146
- <h3 id="proposition-0">Proposition 0</h3>
147
- <p>If <code>T ⊨ P</code>, then:</p>
148
- <ul>
149
- <li>the root kind of <code>T</code> is compatible with the root kind of
150
- <code>P</code>,</li>
151
- <li>every label explicitly present at the root of <code>P</code> is
152
- present at the root of <code>T</code>,</li>
153
- <li>if the root of <code>P</code> is closed, then <code>T</code> has no
154
- additional root labels.</li>
155
- </ul>
156
- <h4 id="proof">Proof</h4>
157
- <p>All three statements are immediate from the definition of the
158
- root-preserving morphism <code>h</code> and conditions 1-5 above,
159
- applied at the root node of <code>P</code>.</p>
160
- <h2 id="singleton-patterns-and-types">7. Singleton Patterns And
161
- Types</h2>
162
- <p>Every type graph <code>T</code> determines a closed pattern
163
- <code>Pat(T)</code> obtained by forgetting that it is a type and viewing
164
- it as a closed pattern graph.</p>
165
- <p>This gives the following proposition.</p>
166
- <h3 id="proposition-1">Proposition 1</h3>
167
- <p>For every type <code>T</code>, the pattern <code>Pat(T)</code> is
168
- singleton:</p>
169
- <pre class="text"><code>Types(Pat(T)) = { T }</code></pre>
170
- <p>up to the chosen notion of type equivalence.</p>
171
- <h4 id="proof-1">Proof</h4>
172
- <p>The identity map from <code>nodes(Pat(T))</code> to
173
- <code>nodes(T)</code> witnesses <code>T ⊨ Pat(T)</code>. Conversely,
174
- suppose <code>U ⊨ Pat(T)</code> via a root-preserving morphism
175
- <code>h</code>. Because every node of <code>Pat(T)</code> is either
176
- product or union, every node kind is preserved. Because every node is
177
- closed, the outgoing labels at each image node are exactly the outgoing
178
- labels of the corresponding node of <code>T</code>. Because every
179
- pattern edge must be respected by <code>h</code>, all target-sharing
180
- constraints are preserved as well. Therefore <code>h</code> is a graph
181
- isomorphism modulo the chosen equivalence on type graphs, and hence
182
- <code>U</code> is equivalent to <code>T</code>.</p>
183
- <h3 id="proposition-2">Proposition 2</h3>
184
- <p>Conversely, every singleton pattern determines a unique type up to
185
- type equivalence.</p>
186
- <h4 id="proof-2">Proof</h4>
187
- <p>Let <code>P</code> be singleton. By definition, <code>Types(P)</code>
188
- contains exactly one type up to equivalence. Choose any representative
189
- <code>T ∈ Types(P)</code>. Then <code>T</code> is the unique type
190
- determined by <code>P</code>, again up to the chosen equivalence
191
- relation.</p>
192
- <p>Thus types can be regarded as a special case of patterns.</p>
193
- <h2 id="polymorphic-values">8. Polymorphic Values</h2>
194
- <p>A <strong>polymorphic value</strong> is a pair</p>
195
- <pre class="text"><code>(P, v)</code></pre>
196
- <p>where:</p>
197
- <ul>
198
- <li><code>P</code> is a pattern graph,</li>
199
- <li><code>v</code> is a typed value <code>(T, t)</code>,</li>
200
- <li><code>T ⊨ P</code>.</li>
201
- </ul>
202
- <p>Equivalently:</p>
203
- <pre class="text"><code>PolyVal(P) = { (P, (T, t)) : T ⊨ P and t ∈ Tree(T) }.</code></pre>
204
- <p>So a pattern does not denote a set of raw trees. It denotes a set of
205
- typed values:</p>
206
- <pre class="text"><code>Val(P) = { (T, t) : T ⊨ P and t ∈ Tree(T) }.</code></pre>
207
- <p>This is a disjoint union over matching types, not an ordinary union
208
- of raw tree sets.</p>
209
- <h3 id="proposition-3">Proposition 3</h3>
210
- <p>For every type <code>T</code>,</p>
211
- <pre class="text"><code>Val(Pat(T)) = Val(T).</code></pre>
212
- <h4 id="proof-3">Proof</h4>
213
- <p>By Proposition 1, <code>Types(Pat(T)) = { T }</code> up to
214
- equivalence. Substituting this into the definition of
215
- <code>Val(P)</code> yields exactly the set of typed values whose type is
216
- <code>T</code>.</p>
217
- <h2 id="examples">9. Examples</h2>
218
- <h3 id="distinct-pattern-graphs">9.1. Distinct pattern graphs</h3>
219
- <p>Consider:</p>
220
- <pre class="k"><code>P1 = { (...) x, (...) y, ... }
221
- P2 = { X x, X y, ... }</code></pre>
222
- <p>Then <code>P1</code> and <code>P2</code> are different patterns.</p>
223
- <ul>
224
- <li><code>P1</code> allows the types at <code>x</code> and
225
- <code>y</code> to differ.</li>
226
- <li><code>P2</code> requires the types at <code>x</code> and
227
- <code>y</code> to be the same.</li>
228
- </ul>
229
- <p>Hence:</p>
230
- <pre class="text"><code>Types(P2) ⊊ Types(P1).</code></pre>
231
- <h4 id="proof-sketch">Proof sketch</h4>
232
- <p>Every type matching <code>P2</code> also matches <code>P1</code>,
233
- because identifying the target types of <code>x</code> and
234
- <code>y</code> is stronger than leaving them unrelated. The inclusion is
235
- strict because a product type with distinct types at <code>x</code> and
236
- <code>y</code> matches <code>P1</code> but not <code>P2</code>.</p>
237
- <h3 id="same-tree-different-typed-values">9.2. Same tree, different
238
- typed values</h3>
239
- <p>Let <code>t = {}|x</code>. Suppose <code>t</code> is accepted both
240
- by</p>
241
- <pre class="k"><code>T1 = &lt; {} x &gt;
242
- T2 = &lt; {} x, {} y &gt;</code></pre>
243
- <p>Then <code>(T1, t)</code> and <code>(T2, t)</code> are different
244
- typed values.</p>
245
- <p>The raw tree is the same, but the type is different.</p>
246
- <h3 id="proposition-4">Proposition 4</h3>
247
- <p>If <code>T ≠ U</code>, then:</p>
248
- <pre class="text"><code>Val(T) ∩ Val(U) = ∅.</code></pre>
249
- <h4 id="proof-4">Proof</h4>
250
- <p>An element of <code>Val(T)</code> has the form <code>(T, t)</code>.
251
- An element of <code>Val(U)</code> has the form <code>(U, u)</code>. If
252
- <code>(T, t) = (U, u)</code>, then necessarily <code>T = U</code>.
253
- Hence, when <code>T ≠ U</code>, the two sets are disjoint.</p>
254
- <h2 id="consequences-for-equality-and-compression">10. Consequences For
255
- Equality And Compression</h2>
256
- <p>Since values are intrinsically typed, raw tree equality is not
257
- sufficient for semantic equality.</p>
258
- <p>This has an immediate consequence for DAG compression. Suppose we
259
- have the raw tree</p>
260
- <pre class="k"><code>{{}|x a, {}|x b, {}|x c}</code></pre>
261
- <p>and consider two patterns:</p>
262
- <pre class="k"><code>P1 = { (...) a, (...) b, (...) c, ... }
263
- P2 = { X a, X b, X c, ... }</code></pre>
264
- <p>If only the raw tree is considered, the three occurrences of
265
- <code>{}|x</code> appear identical and a naive compressor may merge
266
- them. But semantically this is not always justified.</p>
267
- <ul>
268
- <li>Under <code>P1</code>, the three occurrences need not belong to the
269
- same type.</li>
270
- <li>Under <code>P2</code>, they must belong to the same type.</li>
271
- </ul>
272
- <p>Therefore canonical value sharing, when used as an optimization, must
273
- be relative to the typing information supplied by the pattern and the
274
- witness type. One must not collapse raw subtrees merely because they
275
- look the same as trees.</p>
276
- <h3 id="proposition-5">Proposition 5</h3>
277
- <p>There is, in general, no canonical DAG compression procedure that
278
- depends only on the raw value tree and is correct for all polymorphic
279
- values.</p>
280
- <h4 id="proof-5">Proof</h4>
281
- <p>Consider the raw tree</p>
282
- <pre class="text"><code>{{}|x a, {}|x b, {}|x c}.</code></pre>
283
- <p>As a raw tree, the three subtrees under <code>a</code>,
284
- <code>b</code>, and <code>c</code> are isomorphic. Hence any tree-only
285
- compressor cannot distinguish their occurrences by local tree structure
286
- alone. Now compare the two valid pattern contexts:</p>
287
- <pre class="text"><code>P1 = { (...) a, (...) b, (...) c, ... }
288
- P2 = { X a, X b, X c, ... }.</code></pre>
289
- <p>Under <code>P1</code>, the three occurrences are not forced to have
290
- the same type. Under <code>P2</code>, they are forced to have the same
291
- type because all three edges land in the same pattern node. So a
292
- compressor that always merges these subtrees is unsound for
293
- <code>P1</code>, while a compressor that never merges them fails to
294
- capture the canonical sharing justified by <code>P2</code>. Therefore no
295
- canonical sharing rule can be defined from the raw tree alone; typing
296
- information from the pattern and witness type is required.</p>
297
- <h3 id="proposition-6">Proposition 6</h3>
298
- <p>For a fixed polymorphic value <code>(P, (T, t))</code>, any canonical
299
- DAG compression of <code>t</code> must be invariant under equality of
300
- <strong>typed occurrences</strong>, not merely equality of raw
301
- subtrees.</p>
302
- <h4 id="proof-sketch-1">Proof sketch</h4>
303
- <p>By Proposition 4, typed values of different types are distinct even
304
- when represented by the same raw tree. Within a fixed witness type
305
- <code>T</code>, the pattern graph still matters because distinct
306
- positions of <code>P</code> may or may not be identified. Therefore the
307
- only semantically defensible sharing criterion is one that respects the
308
- typing/decorated occurrence information carried by <code>(P, T)</code>,
309
- not the undecorated tree <code>t</code> alone.</p>
310
- <h2 id="topological-remark">11. Topological Remark</h2>
311
- <p>There is a topology-like intuition behind patterns, but only for a
312
- fragment.</p>
313
- <p>If one looks only at open positive patterns, they behave like
314
- observable structural properties of types and suggest a topology or,
315
- more precisely, an Alexandrov-style structure on the class of types
316
- ordered by refinement.</p>
317
- <p>However, the full pattern formalism is richer than topology because
318
- it includes:</p>
319
- <ul>
320
- <li>closedness constraints,</li>
321
- <li>explicit graph sharing, which expresses sameness of type at
322
- different positions.</li>
323
- </ul>
324
- <p>These two features go beyond ordinary open-set semantics.</p>
325
- <h3 id="proposition-7">Proposition 7</h3>
326
- <p>The class of patterns is strictly richer than the open positive
327
- fragment.</p>
328
- <h4 id="proof-sketch-2">Proof sketch</h4>
329
- <p>The open positive fragment can describe only monotone structural
330
- requirements such as “at least these labels” and “these positions have
331
- these subpatterns”. It cannot express exact closedness, because
332
- closedness is not monotone under refinement. It also cannot express
333
- identification of two positions as “the same type” without preserving
334
- explicit graph sharing. Since full patterns include both closedness and
335
- shared-node identity, they strictly extend the open positive
336
- fragment.</p>
337
- <h2 id="summary">12. Summary</h2>
338
- <ul>
339
- <li>A type is a regular rooted graph denoting a set of accepted
340
- trees.</li>
341
- <li>A typed value is a pair <code>(T, t)</code> with
342
- <code>t ∈ Tree(T)</code>.</li>
343
- <li>A pattern is a rooted graph with node kind, openness, and semantic
344
- node identity.</li>
345
- <li>A filter is only syntax for serializing a pattern graph.</li>
346
- <li>A type matches a pattern via a root-preserving morphism respecting
347
- node kind, openness, labels, and shared targets.</li>
348
- <li>A pattern denotes a set of types, and therefore a set of typed
349
- values, not a set of raw trees.</li>
350
- <li>A polymorphic value is a pair <code>(P, (T, t))</code> with
351
- <code>T ⊨ P</code>.</li>
352
- <li>Types are exactly the singleton-pattern case.</li>
353
- <li>DAG compression of values is a representation optimization only and
354
- must respect typing information.</li>
355
- </ul>
356
- <h2 id="corollaries-for-binary-serialization">13. Corollaries For Binary
357
- Serialization</h2>
358
- <p>The previous propositions impose strong constraints on any correct
359
- binary format.</p>
360
- <h3 id="corollary-1">Corollary 1</h3>
361
- <p>The semantic payload of a serialized polymorphic value must
362
- determine:</p>
363
- <ul>
364
- <li>a pattern graph <code>P</code>,</li>
365
- <li>a witness typed value <code>(T, t)</code>,</li>
366
- </ul>
367
- <p>with <code>T ⊨ P</code>.</p>
368
- <p>It is not sufficient to serialize only a raw tree.</p>
369
- <h4 id="justification">Justification</h4>
370
- <p>By Proposition 4, type is part of value identity. By Proposition 5,
371
- the raw tree alone is insufficient to determine correct canonical
372
- sharing behavior.</p>
373
- <h3 id="corollary-2">Corollary 2</h3>
374
- <p>A closed monomorphic typed value is the special case where the
375
- serialized pattern is singleton.</p>
376
- <h4 id="justification-1">Justification</h4>
377
- <p>This is Proposition 1 together with Proposition 3.</p>
378
- <h3 id="corollary-3">Corollary 3</h3>
379
- <p>Any canonical DAG representation of the witness value must be
380
- computed from the decorated object <code>(P, T, t)</code>, not from
381
- <code>t</code> alone.</p>
382
- <h4 id="justification-2">Justification</h4>
383
- <p>This is exactly the content of Propositions 5 and 6.</p>
384
- <h3 id="corollary-4">Corollary 4</h3>
385
- <p>Pattern serialization must preserve explicit pattern-node
386
- identity.</p>
387
- <h4 id="justification-3">Justification</h4>
388
- <p>Pattern node sharing expresses sameness of type at different
389
- positions. Replacing a pattern graph by any representation that forgets
390
- this identity changes its denotation.</p>
391
- <h3 id="corollary-5">Corollary 5</h3>
392
- <p>For singleton patterns, one may replace the explicit serialized
393
- pattern graph by a canonical type reference as a compact optimization,
394
- provided that the decoding semantics is unchanged.</p>
395
- <h4 id="justification-4">Justification</h4>
396
- <p>By Proposition 2, a singleton pattern determines a unique type up to
397
- equivalence. Therefore the explicit graph may be recoverable from a
398
- canonical type identifier.</p>
@@ -1,140 +0,0 @@
1
- <h1 id="project-dependency-graph">Project Dependency Graph</h1>
2
- <p><em>Auto-generated by scripts/generate-dependency-graph.mjs.</em></p>
3
- <pre class="mermaid"><code>graph TD
4
- classDef default fill:#fff,stroke:#333,stroke-width:2px,color:#000;
5
-
6
- Graph_mjs[&quot;Graph.mjs (1.9 KB)&lt;br/&gt;██&quot;]
7
- TypeGraph_mjs[&quot;TypeGraph.mjs (1.1 KB)&lt;br/&gt;█&quot;]
8
- TypePatternGraph_mjs[&quot;TypePatternGraph.mjs (8.0 KB)&lt;br/&gt;████████&quot;]
9
- Value_mjs[&quot;Value.mjs (5.4 KB)&lt;br/&gt;█████&quot;]
10
- augmentation_mjs[&quot;augmentation.mjs (6.9 KB)&lt;br/&gt;███████&quot;]
11
- codecs_example_pipeline_mjs[&quot;codecs/example-pipeline.mjs (5.1 KB)&lt;br/&gt;█████&quot;]
12
- codecs_ieee_mjs[&quot;codecs/ieee.mjs (4.3 KB)&lt;br/&gt;████&quot;]
13
- codecs_int_mjs[&quot;codecs/int.mjs (3.5 KB)&lt;br/&gt;████&quot;]
14
- codecs_json_codec_mjs[&quot;codecs/json-codec.mjs (6.2 KB)&lt;br/&gt;██████&quot;]
15
- codecs_json_mjs[&quot;codecs/json.mjs (1.5 KB)&lt;br/&gt;██&quot;]
16
- codecs_k_parse_mjs[&quot;codecs/k-parse.mjs (3.0 KB)&lt;br/&gt;███&quot;]
17
- codecs_k_print_mjs[&quot;codecs/k-print.mjs (1.5 KB)&lt;br/&gt;██&quot;]
18
- codecs_runtime_codec_mjs[&quot;codecs/runtime/codec.mjs (25.7 KB)&lt;br/&gt;████████████████████&quot;]
19
- codecs_runtime_ieee_pattern_mjs[&quot;codecs/runtime/ieee-pattern.mjs (1.1 KB)&lt;br/&gt;█&quot;]
20
- codecs_runtime_pattern_json_mjs[&quot;codecs/runtime/pattern-json.mjs (3.5 KB)&lt;br/&gt;███&quot;]
21
- codecs_runtime_prefix_codec_mjs[&quot;codecs/runtime/prefix-codec.mjs (13.7 KB)&lt;br/&gt;██████████████&quot;]
22
- codecs_runtime_show_value_mjs[&quot;codecs/runtime/show-value.mjs (2.1 KB)&lt;br/&gt;██&quot;]
23
- codecs_runtime_test_codec_mjs[&quot;codecs/runtime/test-codec.mjs (3.0 KB)&lt;br/&gt;███&quot;]
24
- codecs_runtime_typeFromValue_mjs[&quot;codecs/runtime/typeFromValue.mjs (0.9 KB)&lt;br/&gt;█&quot;]
25
- codecs_runtime_unicode_string_mjs[&quot;codecs/runtime/unicode-string.mjs (7.5 KB)&lt;br/&gt;████████&quot;]
26
- codecs_show_mjs[&quot;codecs/show.mjs (1.5 KB)&lt;br/&gt;██&quot;]
27
- codecs_string_codec_mjs[&quot;codecs/string-codec.mjs (4.5 KB)&lt;br/&gt;█████&quot;]
28
- codecs_unit_mjs[&quot;codecs/unit.mjs (1.6 KB)&lt;br/&gt;██&quot;]
29
- codecs_utf16_mjs[&quot;codecs/utf16.mjs (2.2 KB)&lt;br/&gt;██&quot;]
30
- codecs_utf8_mjs[&quot;codecs/utf8.mjs (1.3 KB)&lt;br/&gt;█&quot;]
31
- codes_mjs[&quot;codes.mjs (9.9 KB)&lt;br/&gt;██████████&quot;]
32
- compiler_mjs[&quot;compiler.mjs (3.9 KB)&lt;br/&gt;████&quot;]
33
- convergence_mjs[&quot;convergence.mjs (6.5 KB)&lt;br/&gt;██████&quot;]
34
- export_mjs[&quot;export.mjs (5.2 KB)&lt;br/&gt;█████&quot;]
35
- hash_mjs[&quot;hash.mjs (1.1 KB)&lt;br/&gt;█&quot;]
36
- index_mjs[&quot;index.mjs (1.9 KB)&lt;br/&gt;██&quot;]
37
- k_mjs[&quot;k.mjs (2.7 KB)&lt;br/&gt;███&quot;]
38
- object_mjs[&quot;object.mjs (26.3 KB)&lt;br/&gt;████████████████████&quot;]
39
- pretty_mjs[&quot;pretty.mjs (5.6 KB)&lt;br/&gt;██████&quot;]
40
- repl_mjs[&quot;repl.mjs (29.3 KB)&lt;br/&gt;████████████████████&quot;]
41
- run_mjs[&quot;run.mjs (14.4 KB)&lt;br/&gt;██████████████&quot;]
42
- symbol_table_mjs[&quot;symbol-table.mjs (2.7 KB)&lt;br/&gt;███&quot;]
43
- typing_mjs[&quot;typing.mjs (4.9 KB)&lt;br/&gt;█████&quot;]
44
- unification_mjs[&quot;unification.mjs (5.4 KB)&lt;br/&gt;█████&quot;]
45
- valueIO_mjs[&quot;valueIO.mjs (1.8 KB)&lt;br/&gt;██&quot;]
46
-
47
- TypePatternGraph_mjs --&gt; Graph_mjs
48
- TypePatternGraph_mjs --&gt; unification_mjs
49
- TypePatternGraph_mjs --&gt; TypeGraph_mjs
50
- augmentation_mjs --&gt; codes_mjs
51
- codecs_example_pipeline_mjs --&gt; codecs_runtime_codec_mjs
52
- codecs_example_pipeline_mjs --&gt; codecs_runtime_prefix_codec_mjs
53
- codecs_example_pipeline_mjs --&gt; codecs_runtime_pattern_json_mjs
54
- codecs_example_pipeline_mjs --&gt; Value_mjs
55
- codecs_example_pipeline_mjs --&gt; hash_mjs
56
- codecs_ieee_mjs --&gt; Value_mjs
57
- codecs_ieee_mjs --&gt; codecs_runtime_prefix_codec_mjs
58
- codecs_ieee_mjs --&gt; codecs_runtime_ieee_pattern_mjs
59
- codecs_int_mjs --&gt; Value_mjs
60
- codecs_int_mjs --&gt; codecs_runtime_prefix_codec_mjs
61
- codecs_json_codec_mjs --&gt; Value_mjs
62
- codecs_json_codec_mjs --&gt; codecs_string_codec_mjs
63
- codecs_json_codec_mjs --&gt; codecs_runtime_ieee_pattern_mjs
64
- codecs_json_mjs --&gt; codecs_runtime_prefix_codec_mjs
65
- codecs_json_mjs --&gt; codecs_json_codec_mjs
66
- codecs_k_parse_mjs --&gt; index_mjs
67
- codecs_k_parse_mjs --&gt; valueIO_mjs
68
- codecs_k_parse_mjs --&gt; codecs_runtime_codec_mjs
69
- codecs_k_parse_mjs --&gt; codecs_runtime_pattern_json_mjs
70
- codecs_k_parse_mjs --&gt; codecs_runtime_prefix_codec_mjs
71
- codecs_k_print_mjs --&gt; codecs_runtime_prefix_codec_mjs
72
- codecs_runtime_codec_mjs --&gt; Value_mjs
73
- codecs_runtime_codec_mjs --&gt; TypePatternGraph_mjs
74
- codecs_runtime_codec_mjs --&gt; typing_mjs
75
- codecs_runtime_codec_mjs --&gt; codes_mjs
76
- codecs_runtime_codec_mjs --&gt; codecs_runtime_pattern_json_mjs
77
- codecs_runtime_ieee_pattern_mjs --&gt; codes_mjs
78
- codecs_runtime_ieee_pattern_mjs --&gt; codecs_runtime_codec_mjs
79
- codecs_runtime_ieee_pattern_mjs --&gt; codecs_runtime_pattern_json_mjs
80
- codecs_runtime_prefix_codec_mjs --&gt; Value_mjs
81
- codecs_runtime_prefix_codec_mjs --&gt; codecs_runtime_codec_mjs
82
- codecs_runtime_prefix_codec_mjs --&gt; codecs_runtime_pattern_json_mjs
83
- codecs_runtime_prefix_codec_mjs --&gt; codecs_runtime_unicode_string_mjs
84
- codecs_runtime_show_value_mjs --&gt; Value_mjs
85
- codecs_runtime_test_codec_mjs --&gt; codecs_runtime_codec_mjs
86
- codecs_runtime_test_codec_mjs --&gt; codecs_runtime_prefix_codec_mjs
87
- codecs_runtime_test_codec_mjs --&gt; codecs_runtime_pattern_json_mjs
88
- codecs_runtime_test_codec_mjs --&gt; Value_mjs
89
- codecs_runtime_test_codec_mjs --&gt; hash_mjs
90
- codecs_runtime_typeFromValue_mjs --&gt; Value_mjs
91
- codecs_runtime_unicode_string_mjs --&gt; Value_mjs
92
- codecs_show_mjs --&gt; codecs_runtime_prefix_codec_mjs
93
- codecs_show_mjs --&gt; codecs_runtime_show_value_mjs
94
- codecs_string_codec_mjs --&gt; codecs_runtime_codec_mjs
95
- codecs_string_codec_mjs --&gt; codecs_runtime_prefix_codec_mjs
96
- codecs_string_codec_mjs --&gt; codecs_runtime_pattern_json_mjs
97
- codecs_string_codec_mjs --&gt; codecs_runtime_unicode_string_mjs
98
- codecs_unit_mjs --&gt; Value_mjs
99
- codecs_unit_mjs --&gt; codecs_runtime_prefix_codec_mjs
100
- codecs_utf16_mjs --&gt; codecs_string_codec_mjs
101
- codecs_utf8_mjs --&gt; codecs_string_codec_mjs
102
- codes_mjs --&gt; hash_mjs
103
- compiler_mjs --&gt; hash_mjs
104
- compiler_mjs --&gt; TypePatternGraph_mjs
105
- compiler_mjs --&gt; Graph_mjs
106
- compiler_mjs --&gt; codes_mjs
107
- compiler_mjs --&gt; augmentation_mjs
108
- compiler_mjs --&gt; export_mjs
109
- compiler_mjs --&gt; convergence_mjs
110
- convergence_mjs --&gt; pretty_mjs
111
- convergence_mjs --&gt; typing_mjs
112
- export_mjs --&gt; hash_mjs
113
- export_mjs --&gt; pretty_mjs
114
- index_mjs --&gt; Value_mjs
115
- index_mjs --&gt; valueIO_mjs
116
- index_mjs --&gt; compiler_mjs
117
- index_mjs --&gt; run_mjs
118
- index_mjs --&gt; codes_mjs
119
- k_mjs --&gt; index_mjs
120
- k_mjs --&gt; codecs_runtime_prefix_codec_mjs
121
- k_mjs --&gt; object_mjs
122
- object_mjs --&gt; index_mjs
123
- object_mjs --&gt; run_mjs
124
- object_mjs --&gt; codes_mjs
125
- object_mjs --&gt; TypePatternGraph_mjs
126
- object_mjs --&gt; pretty_mjs
127
- pretty_mjs --&gt; TypePatternGraph_mjs
128
- repl_mjs --&gt; index_mjs
129
- repl_mjs --&gt; run_mjs
130
- repl_mjs --&gt; codes_mjs
131
- repl_mjs --&gt; Value_mjs
132
- repl_mjs --&gt; pretty_mjs
133
- repl_mjs --&gt; object_mjs
134
- repl_mjs --&gt; codecs_runtime_show_value_mjs
135
- run_mjs --&gt; Value_mjs
136
- run_mjs --&gt; codecs_runtime_codec_mjs
137
- run_mjs --&gt; codecs_runtime_pattern_json_mjs
138
- symbol_table_mjs --&gt; hash_mjs
139
- typing_mjs --&gt; TypePatternGraph_mjs
140
- valueIO_mjs --&gt; Value_mjs</code></pre>