@fraczak/k 6.0.1 → 6.0.2
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.
- package/AGENTS.md +35 -0
- package/CHANGELOG.md +48 -0
- package/CLAUDE.md +64 -0
- package/DOCS/BIG_PICTURE.md +4 -8
- package/DOCS/BINARY_CODEC_STATUS_AND_TODO.md +74 -0
- package/DOCS/CONVERGENCE.md +92 -0
- package/DOCS/DIVERGENCE_DETECTION.md +24 -0
- package/DOCS/Makefile +99 -0
- package/DOCS/OBJECT_FILE_AND_PATTERN.md +217 -0
- package/DOCS/PATTERNS.md +114 -0
- package/DOCS/PATTERNS_AND_TYPED_VALUES.md +665 -0
- package/DOCS/REPL.md +125 -125
- package/DOCS/TEXTUAL_VALUES.md +108 -0
- package/DOCS/TYPE_DERIVATION.md +230 -0
- package/DOCS/book/06-values-in-memory.md +1085 -0
- package/DOCS/book/07-the-partial-function-abi.md +143 -0
- package/DOCS/book/08-operational-semantics-and-execution.md +201 -0
- package/DOCS/book/09-compiler-architecture.md +160 -0
- package/DOCS/book/10-from-ast-to-intermediate-representation.md +167 -0
- package/DOCS/book/14-canonical-serialization.md +176 -0
- package/DOCS/book/appendix-b-incorporating-external-predefined-types-and-functions.md +155 -0
- package/DOCS/book/overview.txt +68 -0
- package/README.md +71 -27
- package/codecs/README.md +8 -0
- package/codecs/pandoc.css +71 -0
- package/codecs/runtime/test-codec.mjs +89 -0
- package/package.json +2 -2
- package/{codecs/k-pattern.mjs → patterns/from-k.mjs} +3 -3
- package/repl2.mjs +259 -50
- package/scripts/generate-dependency-graph.mjs +121 -0
- package/test-repl2.mjs +55 -8
- package/DOCS/book-combined.html +0 -3798
- package/DOCS/book-combined.md +0 -3745
- package/DOCS/book-combined.pdf +0 -0
- package/DOCS/book-combined.tex +0 -5682
- package/DOCS/book.html +0 -65
- package/DOCS/book.pdf +0 -0
- package/DOCS/book.tex +0 -150
|
@@ -0,0 +1,665 @@
|
|
|
1
|
+
# Patterns And Typed Values
|
|
2
|
+
|
|
3
|
+
## Abstract
|
|
4
|
+
|
|
5
|
+
This note proposes a semantic foundation for `k` patterns, types, typed values, and polymorphic values.
|
|
6
|
+
The central point is that a value in `k` is not a bare tree: it exists only in the context of a type.
|
|
7
|
+
Accordingly, a pattern denotes a set of types, and therefore also a set of **typed** values, not a set of raw trees.
|
|
8
|
+
This distinction is essential for reasoning about equality, polymorphism, and binary serialization with DAG compression.
|
|
9
|
+
|
|
10
|
+
## 1. Introduction
|
|
11
|
+
|
|
12
|
+
The `k` language manipulates algebraic values built from products and unions.
|
|
13
|
+
At first sight one may be tempted to define a value simply as a rooted labeled tree and then define types as sets of such trees.
|
|
14
|
+
For `k`, this is not the right semantic level.
|
|
15
|
+
|
|
16
|
+
Two trees with the same shape but belonging to different types are different values.
|
|
17
|
+
Type is part of value identity.
|
|
18
|
+
|
|
19
|
+
This leads to the following semantic ladder:
|
|
20
|
+
|
|
21
|
+
- a **type** denotes a set of typed values,
|
|
22
|
+
- a **pattern** denotes a set of types,
|
|
23
|
+
- therefore a **pattern** also denotes a set of typed values,
|
|
24
|
+
- a **polymorphic value** is a pattern together with a typed value compatible with that pattern.
|
|
25
|
+
|
|
26
|
+
The rest of this note makes these notions precise.
|
|
27
|
+
|
|
28
|
+
## 2. Types
|
|
29
|
+
|
|
30
|
+
A **type graph** is a finite rooted directed graph with labeled edges.
|
|
31
|
+
Each node is one of:
|
|
32
|
+
|
|
33
|
+
- `product`,
|
|
34
|
+
- `union`.
|
|
35
|
+
|
|
36
|
+
For a product node, outgoing edge labels are field names.
|
|
37
|
+
For a union node, outgoing edge labels are tags.
|
|
38
|
+
Edges are locally unique by label.
|
|
39
|
+
|
|
40
|
+
The graph may be cyclic, which allows recursive types.
|
|
41
|
+
|
|
42
|
+
Two type graphs that are equivalent as regular tree languages may be identified.
|
|
43
|
+
For types, minimization by bisimulation is appropriate.
|
|
44
|
+
|
|
45
|
+
### 2.1. Trees accepted by a type
|
|
46
|
+
|
|
47
|
+
Let `Tree(T)` denote the set of rooted value trees accepted by a type graph `T`.
|
|
48
|
+
Acceptance is defined in the standard way:
|
|
49
|
+
|
|
50
|
+
- a product tree is accepted by a product node when its fields match the outgoing labels of that node and each child tree is accepted by the corresponding target node,
|
|
51
|
+
- a union tree is accepted by a union node when it chooses exactly one outgoing tag and the child tree is accepted by the corresponding target node.
|
|
52
|
+
|
|
53
|
+
## 3. Typed Values
|
|
54
|
+
|
|
55
|
+
A **typed value** is a pair
|
|
56
|
+
|
|
57
|
+
```text
|
|
58
|
+
(T, t)
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
where:
|
|
62
|
+
|
|
63
|
+
- `T` is a type graph,
|
|
64
|
+
- `t ∈ Tree(T)`.
|
|
65
|
+
|
|
66
|
+
Thus a value is intrinsically typed.
|
|
67
|
+
The tree `t` is only a representation of the value; it is not, by itself, a semantic value of the language.
|
|
68
|
+
|
|
69
|
+
We write:
|
|
70
|
+
|
|
71
|
+
```text
|
|
72
|
+
Val(T) = { (T, t) : t ∈ Tree(T) }.
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### 3.1. Identity of typed values
|
|
76
|
+
|
|
77
|
+
Typed values are intensional with respect to the type:
|
|
78
|
+
|
|
79
|
+
```text
|
|
80
|
+
(T, t) ≠ (U, t) whenever T ≠ U.
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Even when the same raw tree `t` is accepted by both `T` and `U`, the two typed values are different.
|
|
84
|
+
|
|
85
|
+
This point is essential for serialization: the identity of a value cannot be recovered from the raw tree alone.
|
|
86
|
+
|
|
87
|
+
## 4. Patterns
|
|
88
|
+
|
|
89
|
+
A **pattern graph** is a finite rooted directed graph with labeled edges.
|
|
90
|
+
Each node carries:
|
|
91
|
+
|
|
92
|
+
- a node kind:
|
|
93
|
+
- `unknown`,
|
|
94
|
+
- `product`,
|
|
95
|
+
- `union`,
|
|
96
|
+
- an openness flag:
|
|
97
|
+
- `open`,
|
|
98
|
+
- `closed`.
|
|
99
|
+
|
|
100
|
+
For product and union nodes, outgoing edges are labeled by field names or tags.
|
|
101
|
+
Edges are locally unique by label.
|
|
102
|
+
Unknown-kind nodes use only the open form `(...)`; closed patterns must choose product or union kind explicitly.
|
|
103
|
+
|
|
104
|
+
Unlike the case of types, **node identity in a pattern graph is semantic**.
|
|
105
|
+
If two edges point to the same pattern node, this means that the corresponding positions must be inhabited by the very same type.
|
|
106
|
+
|
|
107
|
+
Therefore patterns are not quotiented by bisimulation in general.
|
|
108
|
+
|
|
109
|
+
## 5. Filters As Syntax
|
|
110
|
+
|
|
111
|
+
A filter expression is a textual way of describing a pattern graph.
|
|
112
|
+
|
|
113
|
+
Variables appearing in filters are not the semantic object itself.
|
|
114
|
+
They are a device for serializing and reusing pattern graph nodes in syntax.
|
|
115
|
+
|
|
116
|
+
For example, the two filters
|
|
117
|
+
|
|
118
|
+
```k
|
|
119
|
+
{ (...) x, (...) y, ... }
|
|
120
|
+
{ X x, X y, ... }
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
describe different pattern graphs:
|
|
124
|
+
|
|
125
|
+
- the first has two distinct target nodes for `x` and `y`,
|
|
126
|
+
- the second has one shared target node reached from both `x` and `y`.
|
|
127
|
+
|
|
128
|
+
These two patterns are semantically different.
|
|
129
|
+
|
|
130
|
+
## 6. Matching A Type Against A Pattern
|
|
131
|
+
|
|
132
|
+
Let `P` be a pattern graph and `T` a type graph.
|
|
133
|
+
We say that `T` **matches** `P`, written
|
|
134
|
+
|
|
135
|
+
```text
|
|
136
|
+
T ⊨ P,
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
if there exists a root-preserving graph morphism
|
|
140
|
+
|
|
141
|
+
```text
|
|
142
|
+
h : nodes(P) → nodes(T)
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
such that:
|
|
146
|
+
|
|
147
|
+
1. if `p` is `unknown`, then `h(p)` may be any type node,
|
|
148
|
+
2. if `p` is `product`, then `h(p)` is a product node,
|
|
149
|
+
3. if `p` is `union`, then `h(p)` is a union node,
|
|
150
|
+
4. if `p` is `closed`, then the outgoing labels of `h(p)` are exactly the outgoing labels of `p`,
|
|
151
|
+
5. if `p` is `open`, then the outgoing labels of `h(p)` contain at least the outgoing labels of `p`,
|
|
152
|
+
6. for every pattern edge
|
|
153
|
+
|
|
154
|
+
```text
|
|
155
|
+
p --l--> q
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
there is a corresponding type edge
|
|
159
|
+
|
|
160
|
+
```text
|
|
161
|
+
h(p) --l--> h(q).
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Condition 6 is the crucial one: if two edges of `P` point to the same node `q`, then the corresponding edges of `T` must point to the same type node `h(q)`.
|
|
165
|
+
This is how the graph structure of the pattern expresses “the same type”.
|
|
166
|
+
|
|
167
|
+
We define:
|
|
168
|
+
|
|
169
|
+
```text
|
|
170
|
+
Types(P) = { T : T ⊨ P }.
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### Proposition 0
|
|
174
|
+
|
|
175
|
+
If `T ⊨ P`, then:
|
|
176
|
+
|
|
177
|
+
- the root kind of `T` is compatible with the root kind of `P`,
|
|
178
|
+
- every label explicitly present at the root of `P` is present at the root of `T`,
|
|
179
|
+
- if the root of `P` is closed, then `T` has no additional root labels.
|
|
180
|
+
|
|
181
|
+
#### Proof
|
|
182
|
+
|
|
183
|
+
All three statements are immediate from the definition of the root-preserving morphism `h` and conditions 1-5 above, applied at the root node of `P`.
|
|
184
|
+
|
|
185
|
+
## 7. Singleton Patterns And Types
|
|
186
|
+
|
|
187
|
+
Every type graph `T` determines a closed pattern `Pat(T)` obtained by forgetting that it is a type and viewing it as a closed pattern graph.
|
|
188
|
+
|
|
189
|
+
This gives the following proposition.
|
|
190
|
+
|
|
191
|
+
### Proposition 1
|
|
192
|
+
|
|
193
|
+
For every type `T`, the pattern `Pat(T)` is singleton:
|
|
194
|
+
|
|
195
|
+
```text
|
|
196
|
+
Types(Pat(T)) = { T }
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
up to the chosen notion of type equivalence.
|
|
200
|
+
|
|
201
|
+
#### Proof
|
|
202
|
+
|
|
203
|
+
The identity map from `nodes(Pat(T))` to `nodes(T)` witnesses `T ⊨ Pat(T)`.
|
|
204
|
+
Conversely, suppose `U ⊨ Pat(T)` via a root-preserving morphism `h`.
|
|
205
|
+
Because every node of `Pat(T)` is either product or union, every node kind is preserved.
|
|
206
|
+
Because every node is closed, the outgoing labels at each image node are exactly the outgoing labels of the corresponding node of `T`.
|
|
207
|
+
Because every pattern edge must be respected by `h`, all target-sharing constraints are preserved as well.
|
|
208
|
+
Therefore `h` is a graph isomorphism modulo the chosen equivalence on type graphs, and hence `U` is equivalent to `T`.
|
|
209
|
+
|
|
210
|
+
### Proposition 2
|
|
211
|
+
|
|
212
|
+
Conversely, every singleton pattern determines a unique type up to type equivalence.
|
|
213
|
+
|
|
214
|
+
#### Proof
|
|
215
|
+
|
|
216
|
+
Let `P` be singleton.
|
|
217
|
+
By definition, `Types(P)` contains exactly one type up to equivalence.
|
|
218
|
+
Choose any representative `T ∈ Types(P)`.
|
|
219
|
+
Then `T` is the unique type determined by `P`, again up to the chosen equivalence relation.
|
|
220
|
+
|
|
221
|
+
Thus types can be regarded as a special case of patterns.
|
|
222
|
+
|
|
223
|
+
## 8. Polymorphic Values
|
|
224
|
+
|
|
225
|
+
A **polymorphic value** is a pair
|
|
226
|
+
|
|
227
|
+
```text
|
|
228
|
+
(P, v)
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
where:
|
|
232
|
+
|
|
233
|
+
- `P` is a pattern graph,
|
|
234
|
+
- `v` is a typed value `(T, t)`,
|
|
235
|
+
- `T ⊨ P`.
|
|
236
|
+
|
|
237
|
+
Equivalently:
|
|
238
|
+
|
|
239
|
+
```text
|
|
240
|
+
PolyVal(P) = { (P, (T, t)) : T ⊨ P and t ∈ Tree(T) }.
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
In the current JavaScript implementation, this semantic pair is represented
|
|
244
|
+
directly in memory: `Product` and `Variant` carry the witness tree, and every
|
|
245
|
+
`Value` may also carry the root pattern as `value.pattern`. The codec stream is
|
|
246
|
+
the serialized form of the same object: an encoded `$pattern` value followed by
|
|
247
|
+
the value payload interpreted under that pattern.
|
|
248
|
+
|
|
249
|
+
So a pattern does not denote a set of raw trees.
|
|
250
|
+
It denotes a set of typed values:
|
|
251
|
+
|
|
252
|
+
```text
|
|
253
|
+
Val(P) = { (T, t) : T ⊨ P and t ∈ Tree(T) }.
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
This is a disjoint union over matching types, not an ordinary union of raw tree sets.
|
|
257
|
+
|
|
258
|
+
### Proposition 3
|
|
259
|
+
|
|
260
|
+
For every type `T`,
|
|
261
|
+
|
|
262
|
+
```text
|
|
263
|
+
Val(Pat(T)) = Val(T).
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
#### Proof
|
|
267
|
+
|
|
268
|
+
By Proposition 1, `Types(Pat(T)) = { T }` up to equivalence.
|
|
269
|
+
Substituting this into the definition of `Val(P)` yields exactly the set of typed values whose type is `T`.
|
|
270
|
+
|
|
271
|
+
## 9. Examples
|
|
272
|
+
|
|
273
|
+
### 9.1. Distinct pattern graphs
|
|
274
|
+
|
|
275
|
+
Consider:
|
|
276
|
+
|
|
277
|
+
```k
|
|
278
|
+
P1 = { (...) x, (...) y, ... }
|
|
279
|
+
P2 = { X x, X y, ... }
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
Then `P1` and `P2` are different patterns.
|
|
283
|
+
|
|
284
|
+
- `P1` allows the types at `x` and `y` to differ.
|
|
285
|
+
- `P2` requires the types at `x` and `y` to be the same.
|
|
286
|
+
|
|
287
|
+
Hence:
|
|
288
|
+
|
|
289
|
+
```text
|
|
290
|
+
Types(P2) ⊊ Types(P1).
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
#### Proof sketch
|
|
294
|
+
|
|
295
|
+
Every type matching `P2` also matches `P1`, because identifying the target types of `x` and `y` is stronger than leaving them unrelated.
|
|
296
|
+
The inclusion is strict because a product type with distinct types at `x` and `y` matches `P1` but not `P2`.
|
|
297
|
+
|
|
298
|
+
### 9.2. Same tree, different typed values
|
|
299
|
+
|
|
300
|
+
Let `t = {}|x`.
|
|
301
|
+
Suppose `t` is accepted both by
|
|
302
|
+
|
|
303
|
+
```k
|
|
304
|
+
T1 = < {} x >
|
|
305
|
+
T2 = < {} x, {} y >
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
Then `(T1, t)` and `(T2, t)` are different typed values.
|
|
309
|
+
|
|
310
|
+
The raw tree is the same, but the type is different.
|
|
311
|
+
|
|
312
|
+
### Proposition 4
|
|
313
|
+
|
|
314
|
+
If `T ≠ U`, then:
|
|
315
|
+
|
|
316
|
+
```text
|
|
317
|
+
Val(T) ∩ Val(U) = ∅.
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
#### Proof
|
|
321
|
+
|
|
322
|
+
An element of `Val(T)` has the form `(T, t)`.
|
|
323
|
+
An element of `Val(U)` has the form `(U, u)`.
|
|
324
|
+
If `(T, t) = (U, u)`, then necessarily `T = U`.
|
|
325
|
+
Hence, when `T ≠ U`, the two sets are disjoint.
|
|
326
|
+
|
|
327
|
+
## 10. Consequences For Equality And Compression
|
|
328
|
+
|
|
329
|
+
Since values are intrinsically typed, raw tree equality is not sufficient for semantic equality.
|
|
330
|
+
|
|
331
|
+
This has an immediate consequence for DAG compression.
|
|
332
|
+
Suppose we have the raw tree
|
|
333
|
+
|
|
334
|
+
```k
|
|
335
|
+
{{}|x a, {}|x b, {}|x c}
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
and consider two patterns:
|
|
339
|
+
|
|
340
|
+
```k
|
|
341
|
+
P1 = { (...) a, (...) b, (...) c, ... }
|
|
342
|
+
P2 = { X a, X b, X c, ... }
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
If only the raw tree is considered, the three occurrences of `{}|x` appear identical and a naive compressor may merge them.
|
|
346
|
+
But semantically this is not always justified.
|
|
347
|
+
|
|
348
|
+
- Under `P1`, the three occurrences need not belong to the same type.
|
|
349
|
+
- Under `P2`, they must belong to the same type.
|
|
350
|
+
|
|
351
|
+
Therefore canonical value sharing, when used as an optimization, must be relative to the typing information supplied by the pattern and the witness type.
|
|
352
|
+
One must not collapse raw subtrees merely because they look the same as trees.
|
|
353
|
+
|
|
354
|
+
### Proposition 5
|
|
355
|
+
|
|
356
|
+
There is, in general, no canonical DAG compression procedure that depends only on the raw value tree and is correct for all polymorphic values.
|
|
357
|
+
|
|
358
|
+
#### Proof
|
|
359
|
+
|
|
360
|
+
Consider the raw tree
|
|
361
|
+
|
|
362
|
+
```text
|
|
363
|
+
{{}|x a, {}|x b, {}|x c}.
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
As a raw tree, the three subtrees under `a`, `b`, and `c` are isomorphic.
|
|
367
|
+
Hence any tree-only compressor cannot distinguish their occurrences by local tree structure alone.
|
|
368
|
+
Now compare the two valid pattern contexts:
|
|
369
|
+
|
|
370
|
+
```text
|
|
371
|
+
P1 = { (...) a, (...) b, (...) c, ... }
|
|
372
|
+
P2 = { X a, X b, X c, ... }.
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
Under `P1`, the three occurrences are not forced to have the same type.
|
|
376
|
+
Under `P2`, they are forced to have the same type because all three edges land in the same pattern node.
|
|
377
|
+
So a compressor that always merges these subtrees is unsound for `P1`, while a compressor that never merges them fails to capture the canonical sharing justified by `P2`.
|
|
378
|
+
Therefore no canonical sharing rule can be defined from the raw tree alone; typing information from the pattern and witness type is required.
|
|
379
|
+
|
|
380
|
+
### Proposition 6
|
|
381
|
+
|
|
382
|
+
For a fixed polymorphic value `(P, (T, t))`, any canonical DAG compression of `t` must be invariant under equality of **typed occurrences**, not merely equality of raw subtrees.
|
|
383
|
+
|
|
384
|
+
#### Proof sketch
|
|
385
|
+
|
|
386
|
+
By Proposition 4, typed values of different types are distinct even when represented by the same raw tree.
|
|
387
|
+
Within a fixed witness type `T`, the pattern graph still matters because distinct positions of `P` may or may not be identified.
|
|
388
|
+
Therefore the only semantically defensible sharing criterion is one that respects the typing/decorated occurrence information carried by `(P, T)`, not the undecorated tree `t` alone.
|
|
389
|
+
|
|
390
|
+
## 11. Topological Remark
|
|
391
|
+
|
|
392
|
+
There is a topology-like intuition behind patterns, but only for a fragment.
|
|
393
|
+
|
|
394
|
+
If one looks only at open positive patterns, they behave like observable structural properties of types and suggest a topology or, more precisely, an Alexandrov-style structure on the class of types ordered by refinement.
|
|
395
|
+
|
|
396
|
+
However, the full pattern formalism is richer than topology because it includes:
|
|
397
|
+
|
|
398
|
+
- closedness constraints,
|
|
399
|
+
- explicit graph sharing, which expresses sameness of type at different positions.
|
|
400
|
+
|
|
401
|
+
These two features go beyond ordinary open-set semantics.
|
|
402
|
+
|
|
403
|
+
### Proposition 7
|
|
404
|
+
|
|
405
|
+
The class of patterns is strictly richer than the open positive fragment.
|
|
406
|
+
|
|
407
|
+
#### Proof sketch
|
|
408
|
+
|
|
409
|
+
The open positive fragment can describe only monotone structural requirements such as "at least these labels" and "these positions have these subpatterns".
|
|
410
|
+
It cannot express exact closedness, because closedness is not monotone under refinement.
|
|
411
|
+
It also cannot express identification of two positions as "the same type" without preserving explicit graph sharing.
|
|
412
|
+
Since full patterns include both closedness and shared-node identity, they strictly extend the open positive fragment.
|
|
413
|
+
|
|
414
|
+
## 12. Summary
|
|
415
|
+
|
|
416
|
+
- A type is a regular rooted graph denoting a set of accepted trees.
|
|
417
|
+
- A typed value is a pair `(T, t)` with `t ∈ Tree(T)`.
|
|
418
|
+
- A pattern is a rooted graph with node kind, openness, and semantic node identity.
|
|
419
|
+
- A filter is only syntax for serializing a pattern graph.
|
|
420
|
+
- A type matches a pattern via a root-preserving morphism respecting node kind, openness, labels, and shared targets.
|
|
421
|
+
- A pattern denotes a set of types, and therefore a set of typed values, not a set of raw trees.
|
|
422
|
+
- A polymorphic value is a pair `(P, (T, t))` with `T ⊨ P`.
|
|
423
|
+
- Types are exactly the singleton-pattern case.
|
|
424
|
+
- DAG compression of values is a representation optimization only and must respect typing information.
|
|
425
|
+
|
|
426
|
+
## 13. Corollaries For Binary Serialization
|
|
427
|
+
|
|
428
|
+
The previous propositions impose strong constraints on any correct binary format.
|
|
429
|
+
|
|
430
|
+
### Corollary 1
|
|
431
|
+
|
|
432
|
+
The semantic payload of a serialized polymorphic value must determine:
|
|
433
|
+
|
|
434
|
+
- a pattern graph `P`,
|
|
435
|
+
- a witness typed value `(T, t)`,
|
|
436
|
+
|
|
437
|
+
with `T ⊨ P`.
|
|
438
|
+
|
|
439
|
+
It is not sufficient to serialize only a raw tree.
|
|
440
|
+
|
|
441
|
+
#### Justification
|
|
442
|
+
|
|
443
|
+
By Proposition 4, type is part of value identity.
|
|
444
|
+
By Proposition 5, the raw tree alone is insufficient to determine correct canonical sharing behavior.
|
|
445
|
+
|
|
446
|
+
### Corollary 2
|
|
447
|
+
|
|
448
|
+
A closed monomorphic typed value is the special case where the serialized pattern is singleton.
|
|
449
|
+
|
|
450
|
+
#### Justification
|
|
451
|
+
|
|
452
|
+
This is Proposition 1 together with Proposition 3.
|
|
453
|
+
|
|
454
|
+
### Corollary 3
|
|
455
|
+
|
|
456
|
+
Any canonical DAG representation of the witness value must be computed from the decorated object `(P, T, t)`, not from `t` alone.
|
|
457
|
+
|
|
458
|
+
#### Justification
|
|
459
|
+
|
|
460
|
+
This is exactly the content of Propositions 5 and 6.
|
|
461
|
+
|
|
462
|
+
### Corollary 4
|
|
463
|
+
|
|
464
|
+
Pattern serialization must preserve explicit pattern-node identity.
|
|
465
|
+
|
|
466
|
+
#### Justification
|
|
467
|
+
|
|
468
|
+
Pattern node sharing expresses sameness of type at different positions.
|
|
469
|
+
Replacing a pattern graph by any representation that forgets this identity changes its denotation.
|
|
470
|
+
|
|
471
|
+
### Corollary 5
|
|
472
|
+
|
|
473
|
+
For singleton patterns, one may replace the explicit serialized pattern graph by a canonical type reference as a compact optimization, provided that the decoding semantics is unchanged.
|
|
474
|
+
|
|
475
|
+
#### Justification
|
|
476
|
+
|
|
477
|
+
By Proposition 2, a singleton pattern determines a unique type up to equivalence.
|
|
478
|
+
Therefore the explicit graph may be recoverable from a canonical type identifier.
|
|
479
|
+
|
|
480
|
+
## 14. Practical Goal And Derived Toolbox
|
|
481
|
+
|
|
482
|
+
The ultimate engineering goal is not merely to classify values, but to transform them efficiently with `k` programs.
|
|
483
|
+
From a practical point of view, polymorphism is essential: it lets one write one transformation over a whole family of types instead of duplicating monomorphic programs.
|
|
484
|
+
|
|
485
|
+
This suggests the following operational point of view:
|
|
486
|
+
|
|
487
|
+
- `k` programs should be allowed to consume **polymorphic values**,
|
|
488
|
+
- `k` programs themselves may have **polymorphic input and output patterns**,
|
|
489
|
+
- runtime and storage mechanisms should preserve enough typing information to make such transformations sound and efficient.
|
|
490
|
+
|
|
491
|
+
The current evaluator follows this point of view for materialized values:
|
|
492
|
+
projection operations select the corresponding subpattern, product construction
|
|
493
|
+
builds a closed product pattern from its fields, and variant construction builds
|
|
494
|
+
a closed union pattern for the introduced tag. Boundary input/output patterns
|
|
495
|
+
from compilation are still used to constrain and merge with the pattern observed
|
|
496
|
+
on the runtime value.
|
|
497
|
+
|
|
498
|
+
From the semantic development above, the core toolbox should include at least the following components.
|
|
499
|
+
|
|
500
|
+
### 14.1. Pattern unification
|
|
501
|
+
|
|
502
|
+
Given two pattern graphs `P` and `Q`, compute their least common refinement when it exists.
|
|
503
|
+
|
|
504
|
+
This operation is needed for:
|
|
505
|
+
|
|
506
|
+
- type inference,
|
|
507
|
+
- composing polymorphic programs,
|
|
508
|
+
- checking compatibility of program boundaries,
|
|
509
|
+
- specializing generic transformations.
|
|
510
|
+
|
|
511
|
+
Because patterns carry semantic graph identity, unification must preserve shared nodes and must not collapse distinct positions merely by local structural similarity.
|
|
512
|
+
|
|
513
|
+
### 14.2. Encoding and decoding of polymorphic values
|
|
514
|
+
|
|
515
|
+
There must be a concrete external representation of polymorphic values so they can be:
|
|
516
|
+
|
|
517
|
+
- printed,
|
|
518
|
+
- stored,
|
|
519
|
+
- sent between processes,
|
|
520
|
+
- consumed as input to `k` programs.
|
|
521
|
+
|
|
522
|
+
Semantically, such a representation must determine:
|
|
523
|
+
|
|
524
|
+
```text
|
|
525
|
+
(P, (T, t))
|
|
526
|
+
```
|
|
527
|
+
|
|
528
|
+
with `T ⊨ P`.
|
|
529
|
+
|
|
530
|
+
Implementation-wise, one may choose compact encodings of:
|
|
531
|
+
|
|
532
|
+
- the pattern graph,
|
|
533
|
+
- the witness type,
|
|
534
|
+
- the witness value tree,
|
|
535
|
+
|
|
536
|
+
with optional DAG compression for the value component.
|
|
537
|
+
|
|
538
|
+
### 14.3. Indexing `k` programs by input and output patterns
|
|
539
|
+
|
|
540
|
+
If a `k` program has boundary description
|
|
541
|
+
|
|
542
|
+
```text
|
|
543
|
+
Pin -> Pout,
|
|
544
|
+
```
|
|
545
|
+
|
|
546
|
+
then it should be searchable and composable by those patterns.
|
|
547
|
+
|
|
548
|
+
This supports:
|
|
549
|
+
|
|
550
|
+
- lookup of transformations applicable to a given polymorphic value,
|
|
551
|
+
- composition planning,
|
|
552
|
+
- registry-style discovery of programs by structural interface,
|
|
553
|
+
- optimization by selecting more specialized implementations when available.
|
|
554
|
+
|
|
555
|
+
The indexing problem is therefore pattern-theoretic rather than merely nominal.
|
|
556
|
+
|
|
557
|
+
### 14.4. Deriving a pattern from a raw value tree
|
|
558
|
+
|
|
559
|
+
Given a raw tree `t`, one wants to infer useful typing information from it.
|
|
560
|
+
|
|
561
|
+
This operation can have several variants, for example:
|
|
562
|
+
|
|
563
|
+
- derive the most specific singleton pattern compatible with `t`,
|
|
564
|
+
- derive a more general open pattern that exposes only the observed structure,
|
|
565
|
+
- derive repeated-shape constraints when justified by an external typing context.
|
|
566
|
+
|
|
567
|
+
This is not the same as recovering a unique semantic value from the tree alone, because a raw tree does not determine its type.
|
|
568
|
+
Still, such derivations are useful as heuristics, input assistance, and initialization of pattern inference.
|
|
569
|
+
|
|
570
|
+
The current codec stream uses one such derivation when no input pattern or type
|
|
571
|
+
is supplied. Empty nodes derive the closed product `{}`. Multi-child nodes
|
|
572
|
+
derive closed products. A one-child node derives an open union by default, unless
|
|
573
|
+
an explicit product pattern is supplied to disambiguate it as a singleton
|
|
574
|
+
product.
|
|
575
|
+
|
|
576
|
+
For example:
|
|
577
|
+
|
|
578
|
+
```k
|
|
579
|
+
{a:{b:x,c:{}}}
|
|
580
|
+
```
|
|
581
|
+
|
|
582
|
+
derives the pattern:
|
|
583
|
+
|
|
584
|
+
```k
|
|
585
|
+
< { <{} x, ...> b, {} c } a, ... >
|
|
586
|
+
```
|
|
587
|
+
|
|
588
|
+
The derived wire pattern may then be canonicalized by collapsing finite closed
|
|
589
|
+
subtrees from the leaves upward. The base case is the closed empty product `{}`;
|
|
590
|
+
two closed nodes collapse only when their kind, labels, and already collapsed
|
|
591
|
+
children are identical. Open pattern nodes keep their identity. This closed-node
|
|
592
|
+
collapse belongs to pattern graph construction and should not be confused with
|
|
593
|
+
DAG compression of the witness value tree.
|
|
594
|
+
|
|
595
|
+
### 14.5. Additional likely tools
|
|
596
|
+
|
|
597
|
+
The same framework naturally suggests further tools:
|
|
598
|
+
|
|
599
|
+
- checking whether a typed value matches a pattern,
|
|
600
|
+
- specializing a polymorphic value to a singleton pattern when possible,
|
|
601
|
+
- deriving the output pattern of a composed program,
|
|
602
|
+
- deciding whether one pattern is more specific than another,
|
|
603
|
+
- canonical textual printing of pattern graphs,
|
|
604
|
+
- canonical binary printing of pattern-and-value packages.
|
|
605
|
+
|
|
606
|
+
## 15. Research Program
|
|
607
|
+
|
|
608
|
+
The concepts introduced in this note suggest a concrete research and implementation sequence.
|
|
609
|
+
|
|
610
|
+
### Stage 1. Static semantics
|
|
611
|
+
|
|
612
|
+
Define precisely:
|
|
613
|
+
|
|
614
|
+
- type graphs,
|
|
615
|
+
- pattern graphs,
|
|
616
|
+
- matching `T ⊨ P`,
|
|
617
|
+
- typed values,
|
|
618
|
+
- polymorphic values,
|
|
619
|
+
- singleton patterns,
|
|
620
|
+
- refinement and unification of patterns.
|
|
621
|
+
|
|
622
|
+
### Stage 2. Canonical representations
|
|
623
|
+
|
|
624
|
+
Define canonical forms for:
|
|
625
|
+
|
|
626
|
+
- type graphs,
|
|
627
|
+
- pattern graphs,
|
|
628
|
+
- program boundary patterns.
|
|
629
|
+
|
|
630
|
+
This stage is foundational for indexing and content-addressed storage.
|
|
631
|
+
|
|
632
|
+
### Stage 3. Serialization
|
|
633
|
+
|
|
634
|
+
Define external formats for:
|
|
635
|
+
|
|
636
|
+
- patterns,
|
|
637
|
+
- typed values,
|
|
638
|
+
- polymorphic values.
|
|
639
|
+
|
|
640
|
+
The key semantic distinction must be preserved:
|
|
641
|
+
|
|
642
|
+
- the pattern is fundamentally graph-shaped,
|
|
643
|
+
- the witness value is fundamentally tree-shaped,
|
|
644
|
+
- DAG encoding of the witness value is an optimization only.
|
|
645
|
+
|
|
646
|
+
### Stage 4. Program indexing and execution
|
|
647
|
+
|
|
648
|
+
Use input and output patterns to:
|
|
649
|
+
|
|
650
|
+
- index `k` programs,
|
|
651
|
+
- choose compatible programs for a given input,
|
|
652
|
+
- guide specialization and composition,
|
|
653
|
+
- support efficient execution over serialized polymorphic values.
|
|
654
|
+
|
|
655
|
+
## 16. Final Perspective
|
|
656
|
+
|
|
657
|
+
The semantic distinction between types, patterns, typed values, and polymorphic values is not a purely academic refinement.
|
|
658
|
+
It directly determines what can be compressed, what can be indexed, and what information must survive serialization.
|
|
659
|
+
|
|
660
|
+
In particular:
|
|
661
|
+
|
|
662
|
+
- polymorphism is a first-class practical feature, not an afterthought,
|
|
663
|
+
- patterns are the correct interface language for polymorphic programming,
|
|
664
|
+
- typed values, not raw trees, are the right semantic runtime objects,
|
|
665
|
+
- and efficient execution must be built on top of those distinctions rather than by erasing them.
|