@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.
- package/CHANGELOG.md +17 -0
- package/DOCS/ARCHITECTURE.md +83 -0
- package/DOCS/CODECS.md +93 -204
- package/DOCS/DICTIONARY.md +42 -1241
- package/DOCS/FILE_FORMATS.md +170 -625
- package/DOCS/GETTING_STARTED.md +164 -0
- package/DOCS/KIR_V1.md +22 -33
- package/DOCS/KVM_EXECUTION_MODEL.md +88 -481
- package/DOCS/PATTERNS.md +155 -138
- package/DOCS/POLYMORPHIC_IR.md +83 -101
- package/DOCS/PROFILING_AND_TRACING.md +52 -166
- package/DOCS/README.md +35 -0
- package/DOCS/REPL.md +100 -377
- package/DOCS/TEXTUAL_VALUES.md +53 -81
- package/DOCS/TYPE_DERIVATION.md +95 -234
- package/DOCS/book-combined.md +3786 -0
- package/Examples/arithmetics.k +45 -59
- package/Examples/djson.k +102 -112
- package/Examples/karatsuba-mult.k +2 -3
- package/Examples/list.k +6 -7
- package/Examples/nat.k +6 -6
- package/Examples/poly.k +45 -45
- package/LICENSE +15 -0
- package/README.md +72 -410
- package/backends/llvm/README.md +75 -97
- package/backends/wasm/README.md +88 -66
- package/canonical-pattern.mjs +7 -3
- package/codecs/README.md +23 -261
- package/codecs/WIRE_FORMAT.md +132 -0
- package/core.k +5 -15
- package/named-patterns.mjs +19 -7
- package/objects/README.md +24 -109
- package/package.json +8 -3
- package/paper/Makefile +17 -0
- package/paper/k-paper.pdf +0 -0
- package/paper/k-paper.tex +435 -0
- package/parser.jison +0 -10
- package/parser.mjs +32 -43
- package/repl.mjs +74 -29
- package/scripts/benchmark-idempotency.mjs +3 -3
- package/scripts/run-tests.mjs +1 -1
- package/scripts/test-package.mjs +64 -0
- package/tests/README.md +66 -46
- package/tests/code-derivation/core.mjs +61 -0
- package/tests/helpers/cli.mjs +45 -0
- package/tests/integration.sh +2 -3
- package/tests/test-arithmetics.mjs +47 -237
- package/tests/test-djson-methods.mjs +71 -275
- package/tests/test-hash-normalization.mjs +22 -1
- package/tests/test-karatsuba.mjs +18 -126
- package/tests/test-named-patterns.mjs +174 -621
- package/tests/test-paper-examples.mjs +90 -0
- package/tests/test-poly.mjs +2 -2
- package/tests/test-repl-html.mjs +2 -2
- package/tests/test-repl.mjs +49 -41
- package/tests/test.mjs +27 -68
- package/AGENTS.md +0 -35
- package/CLAUDE.md +0 -64
- package/DOCS/BIG_PICTURE.md +0 -439
- package/DOCS/BINARY_CODEC_STATUS_AND_TODO.md +0 -74
- package/DOCS/CANONICAL_PATTERNS.md +0 -362
- package/DOCS/CODEC_ARCHITECTURE.md +0 -257
- package/DOCS/CONVERGENCE.md +0 -123
- package/DOCS/DIVERGENCE_DETECTION.md +0 -23
- package/DOCS/JSON_APPROACHES_COMPARISON.md +0 -302
- package/DOCS/KIR_SPECIALIZATION_DESIGN.md +0 -472
- package/DOCS/Makefile +0 -99
- package/DOCS/NAMED_PATTERNS.md +0 -361
- package/DOCS/OBJECT_FILE_AND_PATTERN.md +0 -343
- package/DOCS/PATTERNS_AND_TYPED_VALUES.md +0 -665
- package/DOCS/WEB_REPL_REQUIREMENTS.md +0 -134
- package/DOCS/book/01-introduction.md +0 -48
- package/DOCS/book/02-syntax-and-values.md +0 -110
- package/DOCS/book/03-types-as-finite-automata.md +0 -155
- package/DOCS/book/04-partial-functions-and-composition.md +0 -145
- package/DOCS/book/05-typing-filters-and-normalization.md +0 -109
- package/DOCS/book/06-values-in-memory.md +0 -1085
- package/DOCS/book/07-the-partial-function-abi.md +0 -143
- package/DOCS/book/08-operational-semantics-and-execution.md +0 -196
- package/DOCS/book/09-compiler-architecture.md +0 -160
- package/DOCS/book/10-from-ast-to-intermediate-representation.md +0 -166
- package/DOCS/book/14-canonical-serialization.md +0 -176
- package/DOCS/book/appendix-b-incorporating-external-predefined-types-and-functions.md +0 -156
- package/DOCS/book/overview.txt +0 -68
- package/DOCS/book.md +0 -50
- package/Examples/bnat.k +0 -308
- package/Examples/buda.k +0 -66
- package/Examples/byte.k +0 -35
- package/Examples/code.k +0 -43
- package/Examples/vhdl_to_k.k +0 -67
- package/JOURNAL.md +0 -288
- package/TODO.md +0 -107
- package/backends/wasm/DOCS/USE-WEB-ASSEMBLY.md +0 -233
- package/backends/wasm/TECHNICAL-DEBT.md +0 -58
- package/backends/wasm/demo_ieee.mjs +0 -318
- package/browser/vfs-data.mjs +0 -21
- package/codecs/BINARY_FORMAT.md +0 -53
- package/codecs/POLYMORPHIC_BINARY_FORMAT.md +0 -303
- package/codecs/pandoc.css +0 -71
- package/tests/code-derivation/blowup.mjs +0 -25
- package/tests/code-derivation/bnat.mjs +0 -11
- package/tests/code-derivation/dot.mjs +0 -37
- package/tests/code-derivation/index.mjs +0 -20
- package/tests/code-derivation/loop.mjs +0 -17
- package/tests/code-derivation/nat.mjs +0 -10
- package/tests/code-derivation/not-not.mjs +0 -14
- package/tests/code-derivation/product.mjs +0 -22
- package/tests/code-derivation/rlz.mjs +0 -13
- package/tests/test-fingerprint.mjs +0 -38
package/DOCS/REPL.md
CHANGED
|
@@ -1,425 +1,148 @@
|
|
|
1
|
-
#
|
|
1
|
+
# REPL
|
|
2
2
|
|
|
3
|
-
`repl.mjs`
|
|
4
|
-
|
|
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
|
-
|
|
6
|
+
## Input and state
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
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
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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 |
|
|
25
|
+
| Command | Purpose |
|
|
34
26
|
| --- | --- |
|
|
35
|
-
| `:help` | Show command
|
|
36
|
-
| `:
|
|
37
|
-
| `:
|
|
38
|
-
| `:
|
|
39
|
-
| `:
|
|
40
|
-
| `:
|
|
41
|
-
| `:
|
|
42
|
-
| `:
|
|
43
|
-
| `:codec
|
|
44
|
-
| `:
|
|
45
|
-
| `:
|
|
46
|
-
| `:
|
|
47
|
-
| `:
|
|
48
|
-
| `:
|
|
49
|
-
| `:
|
|
50
|
-
| `:
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
-
|
|
167
|
-
|
|
168
|
-
list = @JagVexzgseRr4rVZRmVpsMbr2AKXDjzZnz6Qnf8a9SNe
|
|
169
|
-
pair = @KqTBNkznjWWwync2Mm94XjCez63neJBJtgYcJJEE5kEm
|
|
49
|
+
:load Examples/arithmetics.k
|
|
50
|
+
:load Examples/poly.k
|
|
170
51
|
```
|
|
171
52
|
|
|
172
|
-
|
|
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
|
-
|
|
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
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
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
|
-
|
|
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
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
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
|
-
|
|
211
|
-
|
|
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
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
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
|
-
|
|
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
|
-
|
|
252
|
-
|
|
253
|
-
|
|
86
|
+
:codec load codecs/int.mjs
|
|
87
|
+
:input int.mjs 42
|
|
88
|
+
inc
|
|
254
89
|
```
|
|
255
90
|
|
|
256
|
-
|
|
257
|
-
|
|
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
|
-
|
|
270
|
-
|
|
271
|
-
|
|
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
|
-
|
|
302
|
-
|
|
303
|
-
|
|
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
|
-
|
|
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
|
-
|
|
106
|
+
## Engines and output
|
|
313
107
|
|
|
314
|
-
|
|
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
|
-
|
|
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
|
-
|
|
114
|
+
Choose an engine or output limit at startup:
|
|
321
115
|
|
|
322
|
-
```
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
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
|
-
|
|
122
|
+
Large values are rendered iteratively and truncated at the configured limit.
|
|
123
|
+
Use `:limit none` to disable truncation.
|
|
330
124
|
|
|
331
|
-
|
|
332
|
-
|
|
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
|
-
|
|
131
|
+
:klib session.klib
|
|
132
|
+
:ko increment.ko inc
|
|
386
133
|
```
|
|
387
134
|
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
423
|
-
|
|
424
|
-
|
|
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.
|