@fraczak/k 8.4.1 → 8.5.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 (109) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/DOCS/ARCHITECTURE.md +83 -0
  3. package/DOCS/CODECS.md +93 -204
  4. package/DOCS/DICTIONARY.md +42 -1241
  5. package/DOCS/FILE_FORMATS.md +170 -625
  6. package/DOCS/GETTING_STARTED.md +164 -0
  7. package/DOCS/KIR_V1.md +22 -33
  8. package/DOCS/KVM_EXECUTION_MODEL.md +88 -481
  9. package/DOCS/PATTERNS.md +155 -138
  10. package/DOCS/POLYMORPHIC_IR.md +83 -101
  11. package/DOCS/PROFILING_AND_TRACING.md +52 -166
  12. package/DOCS/README.md +35 -0
  13. package/DOCS/REPL.md +100 -377
  14. package/DOCS/TEXTUAL_VALUES.md +53 -81
  15. package/DOCS/TYPE_DERIVATION.md +95 -234
  16. package/DOCS/book-combined.md +3786 -0
  17. package/Examples/arithmetics.k +45 -59
  18. package/Examples/djson.k +102 -112
  19. package/Examples/karatsuba-mult.k +2 -3
  20. package/Examples/list.k +6 -7
  21. package/Examples/nat.k +6 -6
  22. package/Examples/poly.k +45 -45
  23. package/LICENSE +15 -0
  24. package/README.md +72 -410
  25. package/backends/llvm/README.md +75 -97
  26. package/backends/wasm/README.md +88 -66
  27. package/canonical-pattern.mjs +7 -3
  28. package/codecs/README.md +23 -261
  29. package/codecs/WIRE_FORMAT.md +132 -0
  30. package/core.k +5 -15
  31. package/named-patterns.mjs +19 -7
  32. package/objects/README.md +24 -109
  33. package/package.json +8 -3
  34. package/paper/Makefile +17 -0
  35. package/paper/k-paper.pdf +0 -0
  36. package/paper/k-paper.tex +435 -0
  37. package/parser.jison +0 -10
  38. package/parser.mjs +32 -43
  39. package/repl.mjs +74 -29
  40. package/scripts/benchmark-idempotency.mjs +3 -3
  41. package/scripts/run-tests.mjs +1 -1
  42. package/scripts/test-package.mjs +64 -0
  43. package/tests/README.md +66 -46
  44. package/tests/code-derivation/core.mjs +61 -0
  45. package/tests/helpers/cli.mjs +45 -0
  46. package/tests/integration.sh +2 -3
  47. package/tests/test-arithmetics.mjs +47 -237
  48. package/tests/test-djson-methods.mjs +71 -275
  49. package/tests/test-hash-normalization.mjs +22 -1
  50. package/tests/test-karatsuba.mjs +18 -126
  51. package/tests/test-named-patterns.mjs +174 -621
  52. package/tests/test-paper-examples.mjs +90 -0
  53. package/tests/test-poly.mjs +2 -2
  54. package/tests/test-repl-html.mjs +2 -2
  55. package/tests/test-repl.mjs +49 -41
  56. package/tests/test.mjs +27 -68
  57. package/AGENTS.md +0 -35
  58. package/CLAUDE.md +0 -64
  59. package/DOCS/BIG_PICTURE.md +0 -439
  60. package/DOCS/BINARY_CODEC_STATUS_AND_TODO.md +0 -74
  61. package/DOCS/CANONICAL_PATTERNS.md +0 -362
  62. package/DOCS/CODEC_ARCHITECTURE.md +0 -257
  63. package/DOCS/CONVERGENCE.md +0 -123
  64. package/DOCS/DIVERGENCE_DETECTION.md +0 -23
  65. package/DOCS/JSON_APPROACHES_COMPARISON.md +0 -302
  66. package/DOCS/KIR_SPECIALIZATION_DESIGN.md +0 -472
  67. package/DOCS/Makefile +0 -99
  68. package/DOCS/NAMED_PATTERNS.md +0 -361
  69. package/DOCS/OBJECT_FILE_AND_PATTERN.md +0 -343
  70. package/DOCS/PATTERNS_AND_TYPED_VALUES.md +0 -665
  71. package/DOCS/WEB_REPL_REQUIREMENTS.md +0 -134
  72. package/DOCS/book/01-introduction.md +0 -48
  73. package/DOCS/book/02-syntax-and-values.md +0 -110
  74. package/DOCS/book/03-types-as-finite-automata.md +0 -155
  75. package/DOCS/book/04-partial-functions-and-composition.md +0 -145
  76. package/DOCS/book/05-typing-filters-and-normalization.md +0 -109
  77. package/DOCS/book/06-values-in-memory.md +0 -1085
  78. package/DOCS/book/07-the-partial-function-abi.md +0 -143
  79. package/DOCS/book/08-operational-semantics-and-execution.md +0 -196
  80. package/DOCS/book/09-compiler-architecture.md +0 -160
  81. package/DOCS/book/10-from-ast-to-intermediate-representation.md +0 -166
  82. package/DOCS/book/14-canonical-serialization.md +0 -176
  83. package/DOCS/book/appendix-b-incorporating-external-predefined-types-and-functions.md +0 -156
  84. package/DOCS/book/overview.txt +0 -68
  85. package/DOCS/book.md +0 -50
  86. package/Examples/bnat.k +0 -308
  87. package/Examples/buda.k +0 -66
  88. package/Examples/byte.k +0 -35
  89. package/Examples/code.k +0 -43
  90. package/Examples/vhdl_to_k.k +0 -67
  91. package/JOURNAL.md +0 -288
  92. package/TODO.md +0 -107
  93. package/backends/wasm/DOCS/USE-WEB-ASSEMBLY.md +0 -233
  94. package/backends/wasm/TECHNICAL-DEBT.md +0 -58
  95. package/backends/wasm/demo_ieee.mjs +0 -318
  96. package/browser/vfs-data.mjs +0 -21
  97. package/codecs/BINARY_FORMAT.md +0 -53
  98. package/codecs/POLYMORPHIC_BINARY_FORMAT.md +0 -303
  99. package/codecs/pandoc.css +0 -71
  100. package/tests/code-derivation/blowup.mjs +0 -25
  101. package/tests/code-derivation/bnat.mjs +0 -11
  102. package/tests/code-derivation/dot.mjs +0 -37
  103. package/tests/code-derivation/index.mjs +0 -20
  104. package/tests/code-derivation/loop.mjs +0 -17
  105. package/tests/code-derivation/nat.mjs +0 -10
  106. package/tests/code-derivation/not-not.mjs +0 -14
  107. package/tests/code-derivation/product.mjs +0 -22
  108. package/tests/code-derivation/rlz.mjs +0 -13
  109. package/tests/test-fingerprint.mjs +0 -38
@@ -0,0 +1,164 @@
1
+ # Getting started
2
+
3
+ Use the [browser REPL](https://fraczak.github.io/k/) or start a local session
4
+ from the repository root:
5
+
6
+ ```sh
7
+ npm install
8
+ node repl.mjs
9
+ ```
10
+
11
+ Use Node.js 22+ for development. The examples below are REPL inputs; `>` is
12
+ the prompt, not part of the program.
13
+
14
+ ## Unit, variants, and products
15
+
16
+ The current input starts as the empty product `{}`, called *unit*.
17
+ Each expression transforms the current input and makes its result the next input.
18
+
19
+ ```k
20
+ {} |hello
21
+ ```
22
+
23
+ `{}` produces unit and `|hello` wraps it in a variant tagged `hello`.
24
+ The REPL prints the value followed by its pattern envelope and timing.
25
+ The value part is `{}|hello`.
26
+
27
+ A product constructs several fields from the same input:
28
+
29
+ ```k
30
+ { {} |hello x, {} |world y }
31
+ ```
32
+
33
+ Project a field with `.x`. Extract a variant payload with `/hello`:
34
+
35
+ ```k
36
+ .x /hello
37
+ ```
38
+
39
+ The result is unit again.
40
+
41
+ ## Relations and ordered choice
42
+
43
+ Define a reusable relation with `name = expression;`:
44
+
45
+ ```k
46
+ swap = { .x y, .y x };
47
+ { {} |hello x, {} |world y } swap
48
+ ```
49
+
50
+ Expressions composed left to right pass each result to the next expression.
51
+ Parentheses group a composition; `()` is identity.
52
+
53
+ Relations may be undefined. Choice tries branches in source order and uses
54
+ the first defined result:
55
+
56
+ ```k
57
+ choose = < /left, /right >;
58
+ {} |hello |right choose
59
+ ```
60
+
61
+ The value part is `{}|hello`. The `/left` branch is undefined for a
62
+ `right` variant, so `/right` supplies the result. A diverging branch does
63
+ not count as an undefined result.
64
+
65
+ The empty choice `<>` is always undefined. An undefined evaluation leaves the
66
+ REPL's current input unchanged. Structural type errors are reported separately.
67
+
68
+ ## Schemas and filters
69
+
70
+ Named patterns describe reusable structures:
71
+
72
+ ```k
73
+ bool[] = < {} false, {} true >;
74
+ not = ?bool[] < /false |true, /true |false > ?bool[];
75
+ {} |true not
76
+ ```
77
+
78
+ The result is the `false` variant. `?bool[]` constrains the schema while
79
+ leaving the payload unchanged. The compiler derives input/output constraints;
80
+ runtime boundaries validate arriving schemas, and optimized paths can erase
81
+ guards already proved by specialization.
82
+
83
+ Square brackets distinguish pattern applications such as `bool[]` from
84
+ relation names such as `not`. The [language reference](PATTERNS.md) explains
85
+ variables, recursive knots, and parameterized patterns.
86
+
87
+ ## Recursion: Peano numbers
88
+
89
+ Load the small arithmetic example:
90
+
91
+ ```text
92
+ :load Examples/nat.k
93
+ ```
94
+
95
+ Its number schema is:
96
+
97
+ ```k
98
+ nat[] = < {} 0, N +1 > = N;
99
+ ```
100
+
101
+ A natural number is either zero or another natural wrapped in `+1`.
102
+ The binding `= N` closes the recursive cycle.
103
+
104
+ The library defines `0`, `succ`, `pred`, `add`, and `mult`. Try:
105
+
106
+ ```k
107
+ 0 succ succ
108
+ { 0 succ succ x, 0 succ y } add
109
+ ```
110
+
111
+ The second expression computes 2 + 1. Its value part is
112
+ `{}|0|+1|+1|+1`. These are ordinary relation names, not builtin numeric
113
+ literals.
114
+
115
+ Recursive filters give the contract-derivation engine finite structural
116
+ constraints. Derivation convergence does not prove that every program terminates.
117
+
118
+ ## Libraries and external numbers
119
+
120
+ Load the integer library and a codec explicitly:
121
+
122
+ ```text
123
+ :load Examples/arithmetics.k
124
+ :codec load codecs/int.mjs
125
+ :input int.mjs 42
126
+ ```
127
+
128
+ Now evaluate `inc` to obtain 43. The codec prints the decimal representation
129
+ alongside the structural value.
130
+
131
+ For list operations, load the arithmetic library first, then:
132
+
133
+ ```text
134
+ :load Examples/poly.k
135
+ ```
136
+
137
+ For a smaller self-contained list example, use `:load Examples/list.k`.
138
+ See the [REPL guide](REPL.md) for loading, pattern inspection, and exports.
139
+
140
+ ## Shell pipelines
141
+
142
+ The command-line runtime receives binary schema+payload data, so use codecs
143
+ at text boundaries. From the repository root:
144
+
145
+ ```sh
146
+ printf '42\n' | node codecs/int.mjs --parse |
147
+ node k.mjs --lib Examples/arithmetics.k --export inc 'inc' |
148
+ node codecs/int.mjs --print
149
+ ```
150
+
151
+ This prints `43`. `--lib` loads definitions; `--export inc` brings the
152
+ friendly name into scope. Library relations are also available by their
153
+ content hashes.
154
+
155
+ To compile a small executable object and run it:
156
+
157
+ ```sh
158
+ node objects/compile.mjs '{} |ok' /tmp/k-ok.ko
159
+ node codecs/unit.mjs --parse | node k.mjs /tmp/k-ok.ko |
160
+ node codecs/k-print.mjs
161
+ ```
162
+
163
+ This prints `"ok"`. Continue with [file formats and tools](FILE_FORMATS.md)
164
+ or return to the [documentation index](README.md).
package/DOCS/KIR_V1.md CHANGED
@@ -1,11 +1,9 @@
1
- # KIR v1
1
+ # KIR-P export (version 1)
2
2
 
3
- KIR v1 is the first backend-facing artifact contract for compiled k
4
- objects. It is an inspection and export contract, not a replacement
5
- for the current `.ko` and `.klib` containers.
3
+ KIR-P is the portable backend export contract for compiled k objects.
4
+ It does not replace the stored `.ko` and `.klib` containers.
6
5
 
7
- The first layer is **KIR-P**, the portable polymorphic relation format.
8
- KIR-P keeps the same pattern-carrying semantics as current object
6
+ The format keeps the same pattern-carrying semantics as current object
9
7
  execution, but normalizes names and graph IDs so backends can consume
10
8
  object contents without depending on parser-shaped fields, current
11
9
  object storage, or `TypePatternGraph` internals.
@@ -24,7 +22,7 @@ KIR v1 defines:
24
22
 
25
23
  KIR v1 does not define:
26
24
 
27
- - KIR-M layout and ABI decisions;
25
+ - execution layouts and target ABI decisions;
28
26
  - LLVM, Wasm, C, or native runtime layout;
29
27
  - a new stored object format;
30
28
  - a required physical encoding.
@@ -81,9 +79,9 @@ Rules:
81
79
  `patternGraph`.
82
80
  - Valid type-derivation statuses are `converged`, `not-converged`, and
83
81
  `unknown`.
84
- - LLVM and optimized Wasm backends should reject relations whose status
85
- is not `converged`, unless they intentionally implement an
86
- envelope-aware reference mode.
82
+ - Export validity alone does not establish backend eligibility. Code generators
83
+ must establish the contracts needed by their execution path, using stored
84
+ derivation status and input-envelope specialization as appropriate.
87
85
  - `sourceNodeMap` maps original object pattern graph representative IDs
88
86
  to KIR node IDs. It exists for inspection and debugging, not as a
89
87
  semantic contract.
@@ -149,8 +147,7 @@ Rules:
149
147
  - `union.items` preserve source branch priority.
150
148
  - `product.fields` preserve the relation body field list. Backends may
151
149
  sort or lay out fields later only after preserving observable semantics.
152
- - `filter.filter` currently preserves the stored filter tree. A later KIR
153
- revision may normalize filter internals.
150
+ - `filter.filter` preserves the stored filter tree.
154
151
 
155
152
  ## Current Tooling
156
153
 
@@ -179,31 +176,24 @@ validate KIR-P export for deterministic fixtures:
179
176
  npm run conformance
180
177
  ```
181
178
 
182
- ## Backend Path
179
+ ## Backend path
183
180
 
184
- The intended backend pipeline remains:
181
+ The current kVM lowerer consumes KIR-P relation bodies directly:
185
182
 
186
183
  ```text
187
- k source
188
- -> .ko / .klib object
189
- -> KIR-P
190
- -> envelope-specialized KIR-P for an input envelope
191
- -> KIR-M / kVM (.kvm)
192
- -> LLVM / Wasm / C / other backend
184
+ k source -> .ko / .klib -> KIR-P -> kVM template
185
+ + input envelope
186
+ -> specialized kVM -> interpreter / Wasm / LLVM
193
187
  ```
194
188
 
195
- KIR-P is the shared semantic object contract. Envelope Specialization
196
- produces another KIR-P object whose entry relation is specialized by an
197
- input envelope. KIR-M remains a separate backend contract and should not
198
- be encoded by overloading KIR-P fields. The current kVM lowerer consumes
199
- KIR-P relation bodies directly.
189
+ KIR-P envelope specialization is also available as a separate derivation pass,
190
+ producing another KIR-P object. It is distinct from specialization of a lowered
191
+ kVM template. See [compiler architecture](ARCHITECTURE.md).
200
192
 
201
193
  ## Envelope Specialization
202
194
 
203
- The current API `retypeObjectRelation(object, relationName, inputPattern)`
204
- exports envelope-specialized KIR-P. The `retype...` spelling is
205
- legacy/current implementation naming; new public names should use
206
- envelope specialization. The pass re-runs existing type derivation as
195
+ The API `retypeObjectRelation(object, relationName, inputPattern)` exports
196
+ envelope-specialized KIR-P. The pass re-runs contract derivation as
207
197
  if the entry program were:
208
198
 
209
199
  ```k
@@ -219,14 +209,13 @@ The result is a normal KIR-P executable object:
219
209
  relation's `patternGraph`;
220
210
  - ordinary KIR relation bodies in `rels`.
221
211
 
222
- This is the safe baseline: it preserves envelope-aware execution
223
- semantics while making the specialized entry pattern explicit through
224
- ordinary KIR-P relation typing. Relation/input-pattern cache keys and
212
+ The specialized entry pattern is explicit through ordinary KIR-P relation typing.
213
+ Relation/input-pattern cache keys and
225
214
  call-site worklists are local compiler data; they are not part of the
226
215
  serialized KIR artifact.
227
216
 
228
217
  - [DOCS/FILE_FORMATS.md](./FILE_FORMATS.md) —
229
- guide to .k, .ko, .klib, and .kvm formats
218
+ source, object, library, kVM, and PKIR workflows
230
219
  - [DOCS/KVM_EXECUTION_MODEL.md](./KVM_EXECUTION_MODEL.md) —
231
220
  kVM execution model and bytecode design
232
221
  - [DOCS/DICTIONARY.md](./DICTIONARY.md) — concept names and terminology