@fraczak/k 8.4.0 → 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 (113) hide show
  1. package/CHANGELOG.md +59 -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 +156 -123
  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 -371
  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/llvm/src/cli.mjs +0 -31
  27. package/backends/wasm/README.md +88 -66
  28. package/backends/wasm/src/cli.mjs +0 -31
  29. package/canonical-pattern.mjs +69 -35
  30. package/codecs/README.md +23 -261
  31. package/codecs/WIRE_FORMAT.md +132 -0
  32. package/core.k +5 -15
  33. package/index.mjs +5 -1
  34. package/named-patterns.mjs +55 -7
  35. package/object.mjs +28 -6
  36. package/objects/README.md +24 -109
  37. package/package.json +8 -3
  38. package/paper/Makefile +17 -0
  39. package/paper/k-paper.pdf +0 -0
  40. package/paper/k-paper.tex +435 -0
  41. package/parser.jison +0 -10
  42. package/parser.mjs +32 -43
  43. package/repl.mjs +79 -101
  44. package/scripts/benchmark-idempotency.mjs +3 -3
  45. package/scripts/run-tests.mjs +1 -1
  46. package/scripts/test-package.mjs +64 -0
  47. package/tests/README.md +66 -46
  48. package/tests/code-derivation/core.mjs +61 -0
  49. package/tests/helpers/cli.mjs +45 -0
  50. package/tests/integration.sh +2 -3
  51. package/tests/test-arithmetics.mjs +47 -237
  52. package/tests/test-djson-methods.mjs +71 -275
  53. package/tests/test-hash-normalization.mjs +22 -1
  54. package/tests/test-karatsuba.mjs +18 -126
  55. package/tests/test-named-patterns.mjs +174 -606
  56. package/tests/test-paper-examples.mjs +90 -0
  57. package/tests/test-poly.mjs +2 -2
  58. package/tests/test-repl-html.mjs +2 -2
  59. package/tests/test-repl.mjs +89 -38
  60. package/tests/test.mjs +27 -68
  61. package/AGENTS.md +0 -35
  62. package/CLAUDE.md +0 -64
  63. package/DOCS/BIG_PICTURE.md +0 -439
  64. package/DOCS/BINARY_CODEC_STATUS_AND_TODO.md +0 -74
  65. package/DOCS/CANONICAL_PATTERNS.md +0 -349
  66. package/DOCS/CODEC_ARCHITECTURE.md +0 -257
  67. package/DOCS/CONVERGENCE.md +0 -123
  68. package/DOCS/DIVERGENCE_DETECTION.md +0 -23
  69. package/DOCS/JSON_APPROACHES_COMPARISON.md +0 -302
  70. package/DOCS/KIR_SPECIALIZATION_DESIGN.md +0 -472
  71. package/DOCS/Makefile +0 -99
  72. package/DOCS/NAMED_PATTERNS.md +0 -340
  73. package/DOCS/OBJECT_FILE_AND_PATTERN.md +0 -343
  74. package/DOCS/PATTERNS_AND_TYPED_VALUES.md +0 -665
  75. package/DOCS/WEB_REPL_REQUIREMENTS.md +0 -136
  76. package/DOCS/book/01-introduction.md +0 -48
  77. package/DOCS/book/02-syntax-and-values.md +0 -110
  78. package/DOCS/book/03-types-as-finite-automata.md +0 -155
  79. package/DOCS/book/04-partial-functions-and-composition.md +0 -145
  80. package/DOCS/book/05-typing-filters-and-normalization.md +0 -109
  81. package/DOCS/book/06-values-in-memory.md +0 -1085
  82. package/DOCS/book/07-the-partial-function-abi.md +0 -143
  83. package/DOCS/book/08-operational-semantics-and-execution.md +0 -196
  84. package/DOCS/book/09-compiler-architecture.md +0 -160
  85. package/DOCS/book/10-from-ast-to-intermediate-representation.md +0 -166
  86. package/DOCS/book/14-canonical-serialization.md +0 -176
  87. package/DOCS/book/appendix-b-incorporating-external-predefined-types-and-functions.md +0 -156
  88. package/DOCS/book/overview.txt +0 -68
  89. package/DOCS/book.md +0 -50
  90. package/Examples/bnat.k +0 -308
  91. package/Examples/buda.k +0 -66
  92. package/Examples/byte.k +0 -35
  93. package/Examples/code.k +0 -43
  94. package/Examples/vhdl_to_k.k +0 -67
  95. package/JOURNAL.md +0 -288
  96. package/TODO.md +0 -107
  97. package/backends/wasm/DOCS/USE-WEB-ASSEMBLY.md +0 -233
  98. package/backends/wasm/TECHNICAL-DEBT.md +0 -58
  99. package/backends/wasm/demo_ieee.mjs +0 -318
  100. package/browser/vfs-data.mjs +0 -21
  101. package/codecs/BINARY_FORMAT.md +0 -53
  102. package/codecs/POLYMORPHIC_BINARY_FORMAT.md +0 -303
  103. package/codecs/pandoc.css +0 -71
  104. package/tests/code-derivation/blowup.mjs +0 -25
  105. package/tests/code-derivation/bnat.mjs +0 -11
  106. package/tests/code-derivation/dot.mjs +0 -37
  107. package/tests/code-derivation/index.mjs +0 -20
  108. package/tests/code-derivation/loop.mjs +0 -17
  109. package/tests/code-derivation/nat.mjs +0 -10
  110. package/tests/code-derivation/not-not.mjs +0 -14
  111. package/tests/code-derivation/product.mjs +0 -22
  112. package/tests/code-derivation/rlz.mjs +0 -13
  113. package/tests/test-fingerprint.mjs +0 -38
package/DOCS/REPL.md CHANGED
@@ -1,419 +1,148 @@
1
- # k Interpreter
1
+ # REPL
2
2
 
3
- `repl.mjs` is the interactive k interpreter. After linking or installing
4
- the package, start it with `k-repl`.
3
+ Run `node repl.mjs` from the repository root, or `k-repl` after `npm link`.
4
+ The [walkthrough](GETTING_STARTED.md) introduces language evaluation.
5
5
 
6
- The interpreter keeps a live `.klib`-style state in memory:
6
+ ## Input and state
7
7
 
8
- - registered codes
9
- - compiled relations
10
- - human aliases for relations
11
- - metadata origins used to recover aliases
12
- - the current value flowing through the session
8
+ The current input begins as unit, `{}`. An expression transforms it, and a
9
+ successful result becomes the next input. An undefined result is printed
10
+ without replacing the current input.
13
11
 
14
- Raw k snippets compile on top of that state. You can also export the
15
- active library closure as a `.klib` file or compile an executable `.ko`
16
- object from an expression in the session context.
12
+ Relation definitions (`name = expression;`) and named patterns
13
+ (`name[X] = pattern;`) extend session state. Definitions-only input reports
14
+ compilation timing; it does not evaluate a new value.
17
15
 
18
- ## Prompt Model
16
+ Input is buffered until a complete program is available. A trailing `\` forces
17
+ continuation. A line ending in `;` followed only by spaces closes the snippet.
19
18
 
20
- There are two kinds of input:
21
-
22
- - commands, starting with `:`
23
- - raw k source
24
-
25
- Commands execute immediately, but only when there is no open raw snippet
26
- in the buffer.
27
-
28
- Raw k source is accumulated until the interpreter can decide the snippet
29
- is complete.
19
+ Evaluated values print in native k notation followed by the carried envelope.
20
+ Loaded codecs add external renderings when they recognize the value. Timing
21
+ shows compilation and execution costs for the selected engine.
30
22
 
31
23
  ## Commands
32
24
 
33
- | Command | Meaning |
25
+ | Command | Purpose |
34
26
  | --- | --- |
35
- | `:help` | Show command summary |
36
- | `:engine [wasm\|pkir\|js]` | Display or switch evaluation engine (`wasm`, `pkir`, or `js`) |
37
- | `:wasm` / `:pkir` / `:js` | Shortcut to switch engine |
38
- | `:rel name` | Show relation definition |
39
- | `:rels` | List relation aliases |
40
- | `:pattern name` | Show canonical definition of pattern (alias: `:pat`) |
41
- | `:patterns` | List pattern aliases as `alias = @hash` (alias: `:pats`) |
42
- | `:limit [num\|none]` | Set maximum characters for value output (default: 4096) |
43
- | `:codec load file` | Load a codec from an ES module file (e.g. `:codec load codecs/int.mjs`) |
44
- | `:codec unload name` | Unload a registered codec |
45
- | `:codec list` | List loaded REPL codecs (alias: `:codecs`) |
46
- | `:input <codec.mjs> [text]` | Read next input line or parse inline text using specified codec |
47
- | `:load [--no-alias] file` | Load `.k` source or `.klib` into the current state |
48
- | `:klib file` | Export the active relation closure as a `.klib` library |
49
- | `:ko file expr` | Export a `.ko` executable with `expr` as main |
50
- | `:reset` | Reset interpreter state |
51
- | `:quit` / `:exit` | Exit |
52
-
53
- ## Raw Snippets
54
-
55
- Raw input is compiled as k source on top of the current state.
56
- Both relation definitions (`name = expr;`) and named pattern templates
57
- (`name[...] = filter;`) can be introduced incrementally.
58
-
59
- Examples:
60
-
61
- ```k
62
- > {} |ok
63
- ```
64
-
65
- ```k
66
- > bool[] = < {} true, {} false >;
67
- > bool? = ? bool[];
68
- ```
69
-
70
- ```k
71
- > list[X] = < {} nil, { X car, Y cdr } cons > = Y;
72
- > ? list[bool[]]
73
- ```
74
-
75
- ```k
76
- > not = ? bool[] < /true | false, /false | true >;
77
- {} |true not
78
- ```
79
-
80
- The interpreter uses these rules, in order:
81
-
82
- 1. If the line ends with `\` followed only by spaces, keep buffering.
83
- 2. If the line ends with `;` followed only by spaces, that line
84
- definitively closes the snippet.
85
- 3. Otherwise, try to parse the buffered snippet:
86
- - if it is a complete k program, compile it now
87
- - if it is a valid prefix, wait for more input
88
- - otherwise report the parse error immediately
89
-
90
- After a snippet is accepted:
91
-
92
- - if it has a terminal expression, compile it and evaluate it on the
93
- current value
94
- - if it is definitions only, compile it into the current state and print
95
- nothing on success
96
-
97
- This makes raw snippets useful both for interactive evaluation and for
98
- growing the live library context.
99
-
100
- ## State
101
-
102
- The interpreter keeps:
103
-
104
- - `rels`: canonical compiled relations
105
- - `relAliases`: human relation names to canonical hashes
106
- - `patterns`: defined named pattern templates
107
- - `value`: current value, initially `{}`
108
-
109
- Definitions are content-addressed. Rebinding an alias changes the
110
- name-to-hash mapping, but older canonical definitions remain available
111
- by hash. They stay available in the live session, but `:klib` omits
112
- historical relations that are no longer reachable from an active
113
- relation alias.
114
-
115
- ## Alias Resolution
116
-
117
- Before compiling user input, the interpreter injects an alias preamble
118
- so human names can be reused naturally:
119
-
120
- ```k
121
- nat = @...;
122
- succ = @...;
123
- <user snippet>
124
- ```
125
-
126
- Relation aliases use `name = @hash;`.
127
-
128
- Diagnostic locations are remapped back to the visible user snippet, so
129
- error line numbers do not count the hidden preamble.
130
-
131
- ## Completion
132
-
133
- Tab completion covers:
134
-
135
- - command names after `:`
136
- - file paths for `:load`, `:klib`, and `:ko`
137
- - relation aliases
138
- - canonical names beginning with `@`
139
-
140
- For codec commands, completion covers `:codec load`, `:codec unload`,
141
- `:codec list`, file paths after `:codec load`, and codec names after
142
- `:input`.
143
-
144
- ## Loading
145
-
146
- ### `:load [--no-alias] file.k`
147
-
148
- Compiles the source in the current library context, merges the resulting
149
- codes and relations into the session, and recovers aliases from
150
- user-defined names in the file unless `--no-alias` is used.
151
-
152
- ### `:load [--no-alias] file.klib`
153
-
154
- Reads the plain-JSON library file and merges its codes, relations,
155
- aliases, and metadata into the session. Aliases are recovered from
156
- `meta[hash].origins[]` unless `--no-alias` is used.
157
-
158
- ## Pattern Inspection
159
-
160
- ### `:patterns` (or `:pats`)
161
-
162
- Lists all defined named pattern aliases and their canonical `@hash`
163
- addresses (formatted as `alias = @hash`, matching `:rels`):
27
+ | `:help` | Show command help |
28
+ | `:rel name` / `:rels` | Inspect a relation / list relation aliases |
29
+ | `:pat name` / `:pats` | Inspect a pattern / list pattern aliases |
30
+ | `:pattern` / `:patterns` | Long forms of the preceding pattern commands |
31
+ | `:load [--no-alias] file` | Load source or a compiled library |
32
+ | `:codec load file` | Register a codec module |
33
+ | `:codec unload name` | Remove a codec |
34
+ | `:codec list` / `:codecs` | List registered codecs |
35
+ | `:input codec.mjs [text]` | Parse inline text, or prompt for the next input line |
36
+ | `:engine [wasm\|pkir\|js]` | Show or select the evaluation engine |
37
+ | `:wasm` / `:pkir` / `:js` | Engine shortcuts |
38
+ | `:limit [number\|none]` | Show or change the output limit (default: 4096 characters) |
39
+ | `:klib file` | Export the current library |
40
+ | `:ko file expression` | Export an executable object |
41
+ | `:reset` | Clear session state |
42
+ | `:quit` / `:exit` | Exit the terminal REPL |
43
+
44
+ Regular evaluations include timing; there is no separate timing command.
45
+
46
+ ## Loading and aliases
164
47
 
165
48
  ```text
166
- > :pats
167
- bit = @MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg
168
- list = @JagVexzgseRr4rVZRmVpsMbr2AKXDjzZnz6Qnf8a9SNe
169
- pair = @KqTBNkznjWWwync2Mm94XjCez63neJBJtgYcJJEE5kEm
49
+ :load Examples/arithmetics.k
50
+ :load Examples/poly.k
170
51
  ```
171
52
 
172
- ### `:pattern <name>` (or `:pat <name>`, `:pat @hash`)
53
+ Libraries are loaded explicitly. `poly.k` depends on definitions in
54
+ `arithmetics.k`, so load them in that order. Loading a codec does not
55
+ automatically load its corresponding k library.
173
56
 
174
- Displays the **canonical definition** of a named pattern template:
57
+ `:load` compiles a `.k` file in the current library context or loads a plain
58
+ JSON `.klib`. It recovers relation and pattern aliases unless `--no-alias`
59
+ is specified. Source libraries' terminal expressions are not run by `:load`.
175
60
 
176
- ```text
177
- > pair[A, B] = { A first, B second };
178
- > :pat pair
179
- pair[X0, X1] = {X0 first, X1 second}; -- @...
180
-
181
- > c[X,Y] = {X x, Y y};d[Y,X] = {X x, Y y};
182
- > :pat c
183
- c[X0, X1] = {X0 x, X1 y}; -- @NPSHm9...
184
- > :pat d
185
- d[X1, X0] = {X0 x, X1 y}; -- @NPSHm9...
186
-
187
- > list[T] = < {} nil, { T car, Y cdr } cons > = Y;
188
- > :pat list
189
- list[X1] = <{X1 car, X0 cdr} cons, {} nil>=X0; -- @...
190
-
191
- > same_pair[T] = { T first, T second };
192
- > :pat same_pair
193
- same_pair[X0] = {X0 first, X0 second}; -- @...
194
- ```
61
+ Names are local aliases for canonical definitions. Rebinding a relation alias
62
+ leaves older definitions addressable by hash in the live session.
63
+ `:klib` includes relations reachable from current aliases rather than all
64
+ historical relations retained by the session.
195
65
 
196
- Listing defined pattern aliases with `:pats` shows how each pattern's
197
- parameters map into the canonical template's slots, using `(...)` for
198
- any unexposed pattern holes:
66
+ ## Pattern inspection
199
67
 
200
68
  ```text
201
- > a[]= {X x, Y y}; b[X] = {X x, Y y};
202
- > c[X,Y] = {X x, Y y}; d[Y,X] = {X x, Y y};
203
- > :pats
204
- a[] = @NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[(...),(...)]
205
- b[X] = @NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[X,(...)]
206
- c[X,Y] = @NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[X,Y]
207
- d[Y,X] = @NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[X,Y]
69
+ pair[A, B] = { A first, B second };
70
+ same_pair[T] = { T first, T second };
71
+ :pats
72
+ :pat same_pair
208
73
  ```
209
74
 
210
- Rather than echoing the syntax used to construct the template, the
211
- canonical definition decompiles the underlying minimal finite tree
212
- automaton. Local parameter and knot names chosen by the author are
213
- erased; instead, the canonical definition introduces its own variables
214
- (`X0`, `X1`, `X2`, ...) in deterministic traversal (discovery) order:
75
+ `:pats` shows each template header, skeleton hash, and routing arguments.
76
+ The aliases above share a skeleton; their arguments are `[A,B]` and `[T,T]`.
215
77
 
216
- - **External Ports**: Open type holes (`(...)`) are recorded as
217
- canonical parameters in the template header (`[X0, X1]`, `[X1]`, etc.)
218
- in discovery order.
219
- - **Recursive Knots**: Cycles back to existing nodes are bound using
220
- knot variables (`=X0`).
221
- - **Content Hash**: Every canonical definition is annotated with its
222
- deterministic content-addressed hash (`-- @hash`).
78
+ `:pat alias` shows the alias definition with its canonical hash/routing
79
+ annotation. `:pat @hash` inspects a registered canonical template.
80
+ Inspecting an alias need not erase its author-chosen parameter names.
81
+ See [patterns and canonical identity](PATTERNS.md).
223
82
 
224
- Lookup by canonical hash is also supported: `:pattern @hash`.
225
-
226
- ## Codecs and Numerical Values
227
-
228
- In `k`, there are no primitive numbers or built-in numerical literals.
229
- Values such as integers are represented as algebraic bit trees (e.g.
230
- `bits? = ?< {} _, B 0, B 1 > = B;`).
231
-
232
- A codec translates between external text and enveloped *k* values:
233
- - `parse(text)`: parses an external string into an enveloped *k* value.
234
- - `print(value)`: serializes an enveloped *k* value to external string,
235
- throwing an `Error` if the value is not representable.
236
-
237
- Codecs can define a family of target patterns (e.g. `int` parses/prints
238
- single integers as well as lists like `[0,1,2]`).
239
-
240
- For printing in the REPL, every value is printed with the standard *k*
241
- envelope representation, followed by lines for all loaded codecs that can
242
- format it:
83
+ ## Codecs
243
84
 
244
85
  ```text
245
- > :input int.mjs 42
246
- {}|_|0|1|0|1|0|1|+ ?<{} _, ...>
247
- int.mjs: 42
86
+ :codec load codecs/int.mjs
87
+ :input int.mjs 42
88
+ inc
248
89
  ```
249
90
 
250
- For a complete guide to writing a new codec module, see
251
- [`CODECS.md`](./CODECS.md).
252
-
253
- ### `:codec load file`
254
-
255
- Loads a codec module from an ES module file (e.g.
256
- `:codec load codecs/int.mjs`). Codecs are identified by their file name,
257
- and export `parse(text)` and `print(value)` functions.
258
-
259
- ### `:codec unload name`
260
-
261
- Unloads a previously loaded codec by name (e.g. `:codec unload int.mjs`).
262
-
263
- ### `:codec list` (or `:codecs`)
91
+ Load `Examples/arithmetics.k` before using `inc`. The codec supplies the
92
+ integer value; the library supplies the relation.
264
93
 
265
- Lists all currently loaded codecs and their source files.
266
-
267
- ### `:input <codec.mjs> [text]`
268
-
269
- Parses external input using the specified codec file.
270
-
271
- - **Interactive mode**: `:input <codec.mjs>` switches the REPL prompt
272
- to `<codec.mjs>> `, and the next input line is parsed using that codec:
273
- ```text
274
- > :input json.mjs
275
- json.mjs> {"hello": "world"}
276
- {...} ?<{...}>
277
- json.mjs: {"hello":"world"}
278
- ```
279
-
280
- - **One-line mode**: `:input <codec.mjs> <text>` immediately parses
281
- `<text>`:
282
- ```text
283
- > :input int.mjs 42
284
- {}|_|0|1|0|1|0|1|+ ?<{} _, ...>
285
- int.mjs: 42
286
-
287
- > :input int.mjs [0,1,2]
288
- {...} ?<{...}>
289
- int.mjs: [0,1,2]
290
- ```
291
-
292
- Entering `:input` with no arguments displays usage and a list of loaded
94
+ Codecs export `parse` and `print`. Input uses the codec explicitly selected
95
+ with `:input`. When the text is omitted, the next line is parsed as external
96
+ input rather than k source. `:input` without arguments shows usage and loaded
293
97
  codecs.
294
98
 
295
- ## Evaluation Engines: WASM and JS
296
-
297
- The REPL supports two evaluation engines:
298
-
299
- - **`wasm`** (default): Compiles k expressions into WebAssembly
300
- bytecode via polymorphic kVM lowering. Provides near-native execution
301
- speed, tail-call optimization (TCO), and stack safety on deep recursion.
302
- - **`js`**: The tree-walking JavaScript interpreter. Provides near-zero
303
- compilation latency (interprets parsed definitions directly) and
304
- convenient runtime inspection.
305
-
306
- ### Engine Commands
99
+ After evaluation, each loaded codec attempts to print the result. Successful
100
+ renderings appear as `codec.mjs: text`; failed renderings are suppressed.
101
+ Use module `--help` for codec documentation, for example
102
+ `node codecs/int.mjs --help`. The REPL's `:help` displays general command help.
307
103
 
308
- - `:engine` — displays active engine (`wasm` or `js`)
309
- - `:engine wasm` (or `:wasm`) — switches to the WebAssembly engine
310
- - `:engine js` (or `:js`) — switches to the JavaScript engine
104
+ See [writing codecs](CODECS.md) for the module contract.
311
105
 
312
- ### CLI Options
106
+ ## Engines and output
313
107
 
314
- When starting `k-repl`, choose the engine or output limit with:
108
+ The terminal REPL provides:
315
109
 
316
- ```bash
317
- k-repl --engine=js # or k-repl --js
318
- k-repl --engine=pkir # or k-repl --pkir
319
- k-repl --engine=wasm # or k-repl --wasm (default)
320
- k-repl --limit=8192 # set max characters for rendered output
321
- ```
322
-
323
- ## Timing and Profiling
110
+ - `wasm` (default): compile and execute through WebAssembly;
111
+ - `pkir`: execute through the experimental polymorphic IR;
112
+ - `js`: use the envelope-aware JavaScript reference evaluator.
324
113
 
325
- Timing reporting is enabled by default for all evaluations, reporting
326
- elapsed time broken down into compilation and runtime execution,
327
- annotated with the active engine:
114
+ Choose an engine or output limit at startup:
328
115
 
329
- ```text
330
- > {10 int x, 5 int y} plus
331
- {}|_|1|1|1|1|+ ?<{} _, ...>
332
- /* comp: 12.4ms, exec: 0.8ms (wasm) */
116
+ ```sh
117
+ node repl.mjs --engine=js
118
+ node repl.mjs --engine=pkir
119
+ node repl.mjs --engine=wasm --limit=8192
333
120
  ```
334
121
 
335
- ## Web REPL (Studio)
122
+ Large values are rendered iteratively and truncated at the configured limit.
123
+ Use `:limit none` to disable truncation.
336
124
 
337
- The repository provides a standalone, single-file browser REPL:
338
- `repl.html`.
339
-
340
- - **Zero-install in-browser execution**: Powered by WebAssembly
341
- (`wasm-in-process`) and virtual filesystem (VFS).
342
- - **Interactive UI**: Tab autocompletion, persistent text selection,
343
- execution timing badges (`comp: Xms | exec: Yms`), interactive Codecs
344
- management modal, and interactive `:input` popup modal.
345
- - **Build**: Generate `repl.html` locally using:
346
- ```bash
347
- npm run build:repl-html
348
- ```
349
- - **Live Demo**: Automatically deployed to GitHub Pages via
350
- `.github/workflows/pages.yml`.
125
+ Tab completion includes commands, file paths, relation aliases, codec names,
126
+ and canonical references.
351
127
 
352
128
  ## Export
353
129
 
354
- The REPL can export session state to ahead-of-time (AOT) compiled
355
- artifacts. For full details on all formats, see
356
- [`DOCS/FILE_FORMATS.md`](./FILE_FORMATS.md).
357
-
358
- ### `:klib file`
359
-
360
- Writes a plain-JSON `.klib` library rooted at the current relation
361
- aliases. Relations referenced by those aliases are included
362
- transitively. Historical relations retained in the live session are
363
- omitted when no active alias depends on them. The registered code
364
- snapshot is included. The library has `main: null`; it has no binary
365
- header and no object payload version.
366
-
367
- ### `:ko file expr`
368
-
369
- Compiles `expr` as the main expression (`main: "__main__"`) in the
370
- current interpreter context and writes the resulting executable `.ko`
371
- object container (prefixed by magic header `KOBJ\n`). Running this `.ko`
372
- skips parsing and type derivation for instant execution.
373
-
374
- ## Output
375
-
376
- Evaluated values print in k syntax together with the inferred envelope:
377
-
378
130
  ```text
379
- {}|0|+1 ?<<{} 0, ...> +1, ...>
131
+ :klib session.klib
132
+ :ko increment.ko inc
380
133
  ```
381
134
 
382
- Large values are rendered iteratively and truncated at `maxChars`
383
- (default: `4096` characters) so deeply nested or large values print
384
- instantly without exhausting stack or terminal buffers. Use
385
- `:limit <num>` or `:limit none` to adjust or disable the limit.
386
-
387
- `undefined` prints as:
388
-
389
- ```text
390
- ... undefined
391
- ```
135
+ The second command requires `inc` in the active session. Libraries store
136
+ definitions and aliases with `main: null`. Executable objects have a main
137
+ relation and a `KOBJ\n` container header.
138
+ See [file formats](FILE_FORMATS.md) for inspection, validation, and CLI execution.
392
139
 
393
- `valueToK`, `propertyListToFilter`, and `valueWithEnvelopeToK` from
394
- `codecs/runtime/show-value.mjs` are used for rendering.
395
-
396
- ## Example Session
397
-
398
- ```text
399
- > nat? = ?< {} 0, N +1 > = N;
400
- > inc = | +1;
401
- > {} | 0
402
- {}|0 ?<{} 0, ...>
403
- > :rel inc
404
- inc = | +1; -- @...
405
- > pair[A, B] = { A first, B second };
406
- > :pat pair
407
- pair[X0, X1] = {X0 first, X1 second}; -- @...
408
- > :klib nat.klib
409
- saved nat.klib
410
- > :ko inc.ko inc
411
- saved inc.ko (inc)
412
- ```
140
+ ## Browser REPL
413
141
 
414
- ## Further Reading
142
+ `npm run build:repl-html` creates a self-contained `repl.html` with an
143
+ in-process WebAssembly backend, bundled libraries/codecs, and a virtual
144
+ filesystem. It is also [hosted online](https://fraczak.github.io/k/).
415
145
 
416
- - [DOCS/FILE_FORMATS.md](./FILE_FORMATS.md) —
417
- guide to .k, .ko, .klib, and .kvm formats
418
- - [DOCS/DICTIONARY.md](./DICTIONARY.md) — concept names and terminology
419
- - [DOCS/CODECS.md](./CODECS.md) — guide to writing codecs
146
+ The browser adds file loading, codec management, interactive input dialogs, and
147
+ value-tree inspection. Uploaded and exported files use the virtual filesystem
148
+ and browser downloads rather than the terminal's filesystem access.