@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.
- package/CHANGELOG.md +59 -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 +156 -123
- 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 -371
- 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/llvm/src/cli.mjs +0 -31
- package/backends/wasm/README.md +88 -66
- package/backends/wasm/src/cli.mjs +0 -31
- package/canonical-pattern.mjs +69 -35
- package/codecs/README.md +23 -261
- package/codecs/WIRE_FORMAT.md +132 -0
- package/core.k +5 -15
- package/index.mjs +5 -1
- package/named-patterns.mjs +55 -7
- package/object.mjs +28 -6
- 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 +79 -101
- 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 -606
- 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 +89 -38
- 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 -349
- 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 -340
- 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 -136
- 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,419 +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
|
-
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
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
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
|
-
|
|
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
|
-
|
|
246
|
-
|
|
247
|
-
|
|
86
|
+
:codec load codecs/int.mjs
|
|
87
|
+
:input int.mjs 42
|
|
88
|
+
inc
|
|
248
89
|
```
|
|
249
90
|
|
|
250
|
-
|
|
251
|
-
|
|
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
|
-
|
|
266
|
-
|
|
267
|
-
|
|
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
|
-
|
|
296
|
-
|
|
297
|
-
|
|
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
|
-
|
|
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
|
-
|
|
106
|
+
## Engines and output
|
|
313
107
|
|
|
314
|
-
|
|
108
|
+
The terminal REPL provides:
|
|
315
109
|
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
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
|
-
|
|
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
|
-
```
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
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
|
-
|
|
122
|
+
Large values are rendered iteratively and truncated at the configured limit.
|
|
123
|
+
Use `:limit none` to disable truncation.
|
|
336
124
|
|
|
337
|
-
|
|
338
|
-
|
|
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
|
-
|
|
131
|
+
:klib session.klib
|
|
132
|
+
:ko increment.ko inc
|
|
380
133
|
```
|
|
381
134
|
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
417
|
-
|
|
418
|
-
|
|
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.
|