@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
package/DOCS/REPL.md CHANGED
@@ -1,425 +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
- Note: In `a[] = {X x, Y y}`, `X` and `Y` are bare identifiers and thus
211
- treated as existential type variables. If an author writes `missing[]`,
212
- the REPL rejects the definition with an error
213
- (`Unknown pattern template 'missing[]'`). Undefined pattern applications
214
- are never treated as open variables or leaves.
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
- Rather than echoing the syntax used to construct the template, the
217
- canonical definition decompiles the underlying minimal finite tree
218
- automaton. Local parameter and knot names chosen by the author are
219
- erased; instead, the canonical definition introduces its own variables
220
- (`X0`, `X1`, `X2`, ...) in deterministic traversal (discovery) order:
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).
221
82
 
222
- - **External Ports**: Open type holes (`(...)`) are recorded as
223
- canonical parameters in the template header (`[X0, X1]`, `[X1]`, etc.)
224
- in discovery order.
225
- - **Recursive Knots**: Cycles back to existing nodes are bound using
226
- knot variables (`=X0`).
227
- - **Content Hash**: Every canonical definition is annotated with its
228
- deterministic content-addressed hash (`-- @hash`).
229
-
230
- Lookup by canonical hash is also supported: `:pattern @hash`.
231
-
232
- ## Codecs and Numerical Values
233
-
234
- In `k`, there are no primitive numbers or built-in numerical literals.
235
- Values such as integers are represented as algebraic bit trees (e.g.
236
- `bits? = ?< {} _, B 0, B 1 > = B;`).
237
-
238
- A codec translates between external text and enveloped *k* values:
239
- - `parse(text)`: parses an external string into an enveloped *k* value.
240
- - `print(value)`: serializes an enveloped *k* value to external string,
241
- throwing an `Error` if the value is not representable.
242
-
243
- Codecs can define a family of target patterns (e.g. `int` parses/prints
244
- single integers as well as lists like `[0,1,2]`).
245
-
246
- For printing in the REPL, every value is printed with the standard *k*
247
- envelope representation, followed by lines for all loaded codecs that can
248
- format it:
83
+ ## Codecs
249
84
 
250
85
  ```text
251
- > :input int.mjs 42
252
- {}|_|0|1|0|1|0|1|+ ?<{} _, ...>
253
- int.mjs: 42
86
+ :codec load codecs/int.mjs
87
+ :input int.mjs 42
88
+ inc
254
89
  ```
255
90
 
256
- For a complete guide to writing a new codec module, see
257
- [`CODECS.md`](./CODECS.md).
258
-
259
- ### `:codec load file`
260
-
261
- Loads a codec module from an ES module file (e.g.
262
- `:codec load codecs/int.mjs`). Codecs are identified by their file name,
263
- and export `parse(text)` and `print(value)` functions.
264
-
265
- ### `:codec unload name`
266
-
267
- Unloads a previously loaded codec by name (e.g. `:codec unload int.mjs`).
91
+ Load `Examples/arithmetics.k` before using `inc`. The codec supplies the
92
+ integer value; the library supplies the relation.
268
93
 
269
- ### `:codec list` (or `:codecs`)
270
-
271
- Lists all currently loaded codecs and their source files.
272
-
273
- ### `:input <codec.mjs> [text]`
274
-
275
- Parses external input using the specified codec file.
276
-
277
- - **Interactive mode**: `:input <codec.mjs>` switches the REPL prompt
278
- to `<codec.mjs>> `, and the next input line is parsed using that codec:
279
- ```text
280
- > :input json.mjs
281
- json.mjs> {"hello": "world"}
282
- {...} ?<{...}>
283
- json.mjs: {"hello":"world"}
284
- ```
285
-
286
- - **One-line mode**: `:input <codec.mjs> <text>` immediately parses
287
- `<text>`:
288
- ```text
289
- > :input int.mjs 42
290
- {}|_|0|1|0|1|0|1|+ ?<{} _, ...>
291
- int.mjs: 42
292
-
293
- > :input int.mjs [0,1,2]
294
- {...} ?<{...}>
295
- int.mjs: [0,1,2]
296
- ```
297
-
298
- 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
299
97
  codecs.
300
98
 
301
- ## Evaluation Engines: WASM and JS
302
-
303
- The REPL supports two evaluation engines:
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.
304
103
 
305
- - **`wasm`** (default): Compiles k expressions into WebAssembly
306
- bytecode via polymorphic kVM lowering. Provides near-native execution
307
- speed, tail-call optimization (TCO), and stack safety on deep recursion.
308
- - **`js`**: The tree-walking JavaScript interpreter. Provides near-zero
309
- compilation latency (interprets parsed definitions directly) and
310
- convenient runtime inspection.
104
+ See [writing codecs](CODECS.md) for the module contract.
311
105
 
312
- ### Engine Commands
106
+ ## Engines and output
313
107
 
314
- - `:engine` — displays active engine (`wasm` or `js`)
315
- - `:engine wasm` (or `:wasm`) — switches to the WebAssembly engine
316
- - `:engine js` (or `:js`) — switches to the JavaScript engine
108
+ The terminal REPL provides:
317
109
 
318
- ### CLI Options
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.
319
113
 
320
- When starting `k-repl`, choose the engine or output limit with:
114
+ Choose an engine or output limit at startup:
321
115
 
322
- ```bash
323
- k-repl --engine=js # or k-repl --js
324
- k-repl --engine=pkir # or k-repl --pkir
325
- k-repl --engine=wasm # or k-repl --wasm (default)
326
- k-repl --limit=8192 # set max characters for rendered output
116
+ ```sh
117
+ node repl.mjs --engine=js
118
+ node repl.mjs --engine=pkir
119
+ node repl.mjs --engine=wasm --limit=8192
327
120
  ```
328
121
 
329
- ## Timing and Profiling
122
+ Large values are rendered iteratively and truncated at the configured limit.
123
+ Use `:limit none` to disable truncation.
330
124
 
331
- Timing reporting is enabled by default for all evaluations, reporting
332
- elapsed time broken down into compilation and runtime execution,
333
- annotated with the active engine:
334
-
335
- ```text
336
- > {10 int x, 5 int y} plus
337
- {}|_|1|1|1|1|+ ?<{} _, ...>
338
- /* comp: 12.4ms, exec: 0.8ms (wasm) */
339
- ```
340
-
341
- ## Web REPL (Studio)
342
-
343
- The repository provides a standalone, single-file browser REPL:
344
- `repl.html`.
345
-
346
- - **Zero-install in-browser execution**: Powered by WebAssembly
347
- (`wasm-in-process`) and virtual filesystem (VFS).
348
- - **Interactive UI**: Tab autocompletion, persistent text selection,
349
- execution timing badges (`comp: Xms | exec: Yms`), interactive Codecs
350
- management modal, and interactive `:input` popup modal.
351
- - **Build**: Generate `repl.html` locally using:
352
- ```bash
353
- npm run build:repl-html
354
- ```
355
- - **Live Demo**: Automatically deployed to GitHub Pages via
356
- `.github/workflows/pages.yml`.
125
+ Tab completion includes commands, file paths, relation aliases, codec names,
126
+ and canonical references.
357
127
 
358
128
  ## Export
359
129
 
360
- The REPL can export session state to ahead-of-time (AOT) compiled
361
- artifacts. For full details on all formats, see
362
- [`DOCS/FILE_FORMATS.md`](./FILE_FORMATS.md).
363
-
364
- ### `:klib file`
365
-
366
- Writes a plain-JSON `.klib` library rooted at the current relation
367
- aliases. Relations referenced by those aliases are included
368
- transitively. Historical relations retained in the live session are
369
- omitted when no active alias depends on them. The registered code
370
- snapshot is included. The library has `main: null`; it has no binary
371
- header and no object payload version.
372
-
373
- ### `:ko file expr`
374
-
375
- Compiles `expr` as the main expression (`main: "__main__"`) in the
376
- current interpreter context and writes the resulting executable `.ko`
377
- object container (prefixed by magic header `KOBJ\n`). Running this `.ko`
378
- skips parsing and type derivation for instant execution.
379
-
380
- ## Output
381
-
382
- Evaluated values print in k syntax together with the inferred envelope:
383
-
384
130
  ```text
385
- {}|0|+1 ?<<{} 0, ...> +1, ...>
131
+ :klib session.klib
132
+ :ko increment.ko inc
386
133
  ```
387
134
 
388
- Large values are rendered iteratively and truncated at `maxChars`
389
- (default: `4096` characters) so deeply nested or large values print
390
- instantly without exhausting stack or terminal buffers. Use
391
- `:limit <num>` or `:limit none` to adjust or disable the limit.
392
-
393
- `undefined` prints as:
394
-
395
- ```text
396
- ... undefined
397
- ```
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.
398
139
 
399
- `valueToK`, `propertyListToFilter`, and `valueWithEnvelopeToK` from
400
- `codecs/runtime/show-value.mjs` are used for rendering.
401
-
402
- ## Example Session
403
-
404
- ```text
405
- > nat? = ?< {} 0, N +1 > = N;
406
- > inc = | +1;
407
- > {} | 0
408
- {}|0 ?<{} 0, ...>
409
- > :rel inc
410
- inc = | +1; -- @...
411
- > pair[A, B] = { A first, B second };
412
- > :pat pair
413
- pair[X0, X1] = {X0 first, X1 second}; -- @...
414
- > :klib nat.klib
415
- saved nat.klib
416
- > :ko inc.ko inc
417
- saved inc.ko (inc)
418
- ```
140
+ ## Browser REPL
419
141
 
420
- ## 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/).
421
145
 
422
- - [DOCS/FILE_FORMATS.md](./FILE_FORMATS.md) —
423
- guide to .k, .ko, .klib, and .kvm formats
424
- - [DOCS/DICTIONARY.md](./DICTIONARY.md) — concept names and terminology
425
- - [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.