@fraczak/k 8.7.1 → 8.8.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/CODECS.md +42 -40
- package/DOCS/FILE_FORMATS.md +28 -25
- package/DOCS/GETTING_STARTED.md +10 -15
- package/DOCS/PATTERNS.md +3 -3
- package/DOCS/REPL.md +3 -6
- package/Examples/ieee-arithmetics.k +14 -29
- package/Examples/poly.k +18 -29
- package/README.md +14 -18
- package/backend-api.mjs +1 -3
- package/backends/llvm/src/cli.mjs +1 -2
- package/backends/llvm/tests/integration.sh +2 -6
- package/backends/wasm/tests/integration.sh +5 -10
- package/browser/shims/fs.mjs +2 -6
- package/canonical-pattern.mjs +9 -29
- package/compiler.mjs +22 -12
- package/harness/suites/poly.mjs +9 -11
- package/index.mjs +119 -50
- package/lib/djson.k +981 -295
- package/lib/generic.k +701 -0
- package/lib/ieee.k +662 -638
- package/lib/integer.k +542 -0
- package/object.mjs +19 -12
- package/objects/README.md +6 -6
- package/package.json +1 -1
- package/scripts/benchmark-idempotency.mjs +16 -17
- package/scripts/benchmark.mjs +39 -29
- package/scripts/build-repl-html.mjs +45 -35
- package/scripts/perf-ieee.mjs +1 -5
- package/scripts/perf-int.mjs +12 -27
- package/scripts/perf-multiplication.mjs +8 -9
- package/scripts/perf-poly.mjs +7 -10
- package/scripts/render-benchmark.mjs +1 -1
- package/scripts/run-tests.mjs +4 -2
- package/scripts/test-package.mjs +4 -10
- package/tests/README.md +4 -5
- package/tests/helpers/cli.mjs +30 -7
- package/tests/integration.sh +5 -10
- package/tests/test-arithmetics.mjs +23 -16
- package/tests/test-djson-methods.mjs +17 -25
- package/tests/test-djson.mjs +273 -0
- package/tests/test-generic.mjs +239 -0
- package/tests/test-ieee-arithmetic.mjs +1 -5
- package/tests/test-ieee.mjs +153 -0
- package/tests/test-integer.mjs +264 -0
- package/tests/test-k-object.mjs +12 -18
- package/tests/test-kvm-polymorphic.mjs +6 -7
- package/tests/test-lib.mjs +62 -52
- package/tests/test-library-isolation.mjs +349 -26
- package/tests/test-parse-json.mjs +15 -24
- package/tests/test-poly.mjs +21 -16
- package/tests/test-repl-html.mjs +39 -44
- package/tests/test-repl.mjs +46 -59
- package/DOCS/book-combined.md +0 -3786
- package/DOCS/dependency-graph.md +0 -144
- package/Examples/arithmetics.k +0 -108
- package/Examples/esc.k +0 -9
- package/lib/arithmetics.k +0 -286
- package/lib/bit.k +0 -68
- package/lib/bits.k +0 -227
- package/lib/btrie.k +0 -187
- package/lib/list.k +0 -49
- package/lib/string.k +0 -588
- package/tests/test-ieee-text.mjs +0 -74
- package/tests/test-karatsuba.mjs +0 -25
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,23 @@ All notable changes to this project will be documented here.
|
|
|
5
5
|
The format follows [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
|
6
6
|
This project uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [8.8.0] — 2026-11-13 — Standard Library Consolidation, Tail-Recursive Conventions, and Plain Dependency Comments
|
|
9
|
+
|
|
10
|
+
- **Standard Library Consolidation (`lib/`)**:
|
|
11
|
+
- Reorganize standard library modules into four focused files (`lib/generic.k`, `lib/integer.k`, `lib/ieee.k`, `lib/djson.k`), replacing `lib/bit.k`, `lib/bits.k`, `lib/arithmetics.k`, `lib/list.k`, `lib/btrie.k`, and `lib/string.k`.
|
|
12
|
+
- Make `lib/integer.k` completely self-contained with zero dependencies (defines `bit[]` and `bits[]` locally alongside arbitrary-precision signed integer arithmetic, Karatsuba `times`, `divmod`, `div`, `mod`, `gcd`, and `lcm`).
|
|
13
|
+
- Rename `parseJson` to `parse_json` in `lib/djson.k` and align helper names and signatures across `lib/`.
|
|
14
|
+
- Enforce uniform style and correctness conventions across all `lib/` modules: every `?`-suffixed relation is an identity filter (`?X -> ?X`), all recursive loops are strictly tail-recursive, internal helpers use `_`-prefixed names, discarded fields use descriptive `_<field>` labels, and redundant `()` compositions are removed.
|
|
15
|
+
- Add dedicated per-library specification test suites (`tests/test-generic.mjs`, `tests/test-integer.mjs`, `tests/test-ieee.mjs`, `tests/test-djson.mjs`) and update browser REPL (`scripts/build-repl-html.mjs`), example files, and `k-vscode` tests.
|
|
16
|
+
- **Plain Comment Handling & Library Isolation**:
|
|
17
|
+
- Remove `parseHeaderDirectives` and `parseHeaderUses` from `canonical-pattern.mjs`, `object.mjs`, `backend-api.mjs`, and `backends/llvm/src/cli.mjs` so `# --lib ...` comments are never parsed specially and remain strictly ordinary K comments across tests, scripts, and integration pipelines.
|
|
18
|
+
- Strengthen scope binding and transitive library pattern forwarding in `index.mjs`, `compiler.mjs`, `canonical-pattern.mjs`, and `object.mjs`, including `--use` redefinition warnings and expanded coverage in `tests/test-library-isolation.mjs`.
|
|
19
|
+
- **Example Cleanup & Browser REPL Documentation**:
|
|
20
|
+
- Remove obsolete `Examples/arithmetics.k` (superseded by `lib/integer.k`) and unused `Examples/esc.k`.
|
|
21
|
+
- Include `Examples/nat.k` in the browser REPL VFS alongside the remaining examples and update the `repl.html` dropdown and Help reference.
|
|
22
|
+
- **Documentation Cleanup**:
|
|
23
|
+
- Update `README.md`, `AGENTS.md`, `DOCS/CODECS.md`, `DOCS/FILE_FORMATS.md`, `DOCS/GETTING_STARTED.md`, `DOCS/PATTERNS.md`, `DOCS/REPL.md`, `objects/README.md`, and `tests/README.md` for the consolidated `lib/` structure, plain dependency comment convention, valid CLI pipelines, and fixed internal anchor links.
|
|
24
|
+
|
|
8
25
|
## [8.7.1] — 2026-11-08 — Content-Addressable Library Isolation and Scope Binding Fixes
|
|
9
26
|
|
|
10
27
|
- **Strict Content-Addressable Library Isolation**:
|
package/DOCS/CODECS.md
CHANGED
|
@@ -116,45 +116,43 @@ witness derivation. General wire semantics are specified in
|
|
|
116
116
|
`del`, `push`, `keys`, and `values`. The specializing JSON codec and dynamic
|
|
117
117
|
JSON codec serve different schemas; they are not interchangeable encodings.
|
|
118
118
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
the
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
`
|
|
132
|
-
|
|
133
|
-
`
|
|
134
|
-
|
|
135
|
-
`
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
payload; NaN payloads are not preserved through text.
|
|
139
|
-
The printer prioritizes exact round-trips over shortest formatting: `0.1`
|
|
140
|
-
prints as `0.1000000000000000055511151231257827021181583404541015625`.
|
|
141
|
-
|
|
142
|
-
The library also defines `parseJson`: `string[]` → `djson[]`, entirely in k.
|
|
143
|
-
It consumes one complete JSON document, including nested containers, Unicode
|
|
144
|
-
escapes, and decimal numbers rounded to binary64. Invalid JSON, unpaired
|
|
119
|
+
`lib/integer.k` is completely self-contained, `lib/generic.k` and `lib/ieee.k`
|
|
120
|
+
depend on `core.k`, and `lib/djson.k` composes `core.k`, `lib/generic.k`,
|
|
121
|
+
and `lib/ieee.k`. Its `number[]` is the IEEE `float64[]`; the sign, exponent,
|
|
122
|
+
and fraction schemas are shared too. In the REPL, load `core.k`,
|
|
123
|
+
`lib/generic.k`, `lib/ieee.k`, then `lib/djson.k`. When compiling on the CLI,
|
|
124
|
+
chain each module in order with `--lib` so the single compiled `.klib` carries
|
|
125
|
+
forward all accumulated definitions.
|
|
126
|
+
|
|
127
|
+
`lib/ieee.k` contains the binary64 representation, constructors, identity
|
|
128
|
+
predicates (`zero_float64?`, `finite_float64?`, `inf_float64?`, `nan_float64?`),
|
|
129
|
+
negation (`negate_float64`), and rational/integer conversions
|
|
130
|
+
(`ratio_to_float64`, `bits_to_float64`). Heavy floating-point arithmetic is in
|
|
131
|
+
`Examples/ieee-arithmetics.k`; load that file after `lib/ieee.k` when arithmetic
|
|
132
|
+
is needed. IEEE text parsing and formatting are handled at boundaries by
|
|
133
|
+
`codecs/ieee.mjs`, while JSON decimal number parsing lives inside `lib/djson.k`.
|
|
134
|
+
|
|
135
|
+
`lib/djson.k` defines `parse_json`: `string[]` → `djson[]`, entirely in k.
|
|
136
|
+
It consumes one complete RFC 8259 JSON document, including nested containers,
|
|
137
|
+
Unicode escapes, and decimal numbers rounded to binary64. Invalid JSON, unpaired
|
|
145
138
|
surrogate escapes, and numbers that overflow to infinity are undefined.
|
|
146
139
|
For example, decode UTF-8 to a k string, then parse that string in k:
|
|
147
140
|
|
|
148
141
|
```sh
|
|
149
|
-
node objects/compile.mjs
|
|
150
|
-
node objects/compile.mjs --lib /tmp/
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
node objects/compile.mjs --lib /tmp/
|
|
154
|
-
|
|
155
|
-
|
|
142
|
+
node objects/compile.mjs core.k /tmp/core.klib
|
|
143
|
+
node objects/compile.mjs --lib /tmp/core.klib \
|
|
144
|
+
--use 'bit[] bits[] list[X] byte[] ascii[] bmp_hi_common[] bmp_hi_private_use[] plane0[] plane_02_10[] unicode[] string[]' \
|
|
145
|
+
lib/generic.k /tmp/generic.klib
|
|
146
|
+
node objects/compile.mjs --lib /tmp/generic.klib \
|
|
147
|
+
--use 'bit[] bits[]' \
|
|
148
|
+
lib/ieee.k /tmp/ieee.klib
|
|
149
|
+
node objects/compile.mjs --lib /tmp/ieee.klib \
|
|
150
|
+
--use 'bit[] bits[] list[] byte[] ascii[] bmp_hi_common[] bmp_hi_private_use[] plane0[] plane_02_10[] unicode[] string[]' \
|
|
151
|
+
--use 'bool[] false? true? order[] lt? eq? gt? opt[] reverse trim_bits inc_bits btrie[] btrie_empty btrie_get btrie_set btrie_del btrie_entries_raw btrie_max_key btrie_bfs_values string_to_bits bits_to_string' \
|
|
152
|
+
--use 'sign[] float64[] pos_zero neg_zero finite_float64? ratio_to_float64' \
|
|
153
|
+
lib/djson.k /tmp/djson.klib
|
|
156
154
|
printf '%s' '{"answer":42}' | node codecs/utf8.mjs --parse |
|
|
157
|
-
node k.mjs --lib /tmp/djson.klib --use
|
|
155
|
+
node k.mjs --lib /tmp/djson.klib --use parse_json 'parse_json' |
|
|
158
156
|
node codecs/djson.mjs --print
|
|
159
157
|
```
|
|
160
158
|
|
|
@@ -162,18 +160,22 @@ This prints `{"answer":42}`. Exact conversion near binary64 limits is expensive
|
|
|
162
160
|
in the envelope-aware JavaScript interpreter; the Wasm backend runs the same
|
|
163
161
|
relation with much less overhead.
|
|
164
162
|
|
|
165
|
-
The
|
|
163
|
+
The IEEE library can also convert bit sequences directly to `float64[]` and
|
|
164
|
+
format them with `codecs/ieee.mjs`:
|
|
166
165
|
|
|
167
166
|
```sh
|
|
168
|
-
|
|
169
|
-
node k.mjs --lib /tmp/ieee.klib --use
|
|
170
|
-
node codecs/
|
|
167
|
+
node codecs/unit.mjs --parse |
|
|
168
|
+
node k.mjs --lib /tmp/ieee.klib --use bits_to_float64 '{} |_ |0 |1 |0 |1 |0 |1 bits_to_float64' |
|
|
169
|
+
node codecs/ieee.mjs --print
|
|
171
170
|
```
|
|
172
171
|
|
|
173
|
-
For arithmetic, compile the optional layer against the same IEEE library
|
|
172
|
+
For arithmetic, compile the optional layer against the same IEEE library
|
|
173
|
+
using explicit `--use` bindings:
|
|
174
174
|
|
|
175
175
|
```sh
|
|
176
|
-
node objects/compile.mjs --lib /tmp/ieee.klib
|
|
176
|
+
node objects/compile.mjs --lib /tmp/ieee.klib \
|
|
177
|
+
--use 'bit[] sign[] exp11[] frac52[] float64[] all_ones_exponent? exp0 finite_float64? frac0 inf_float64? nan_float64? neg_inf neg_zero negate_float64 pos_inf pos_zero qnan zero_exponent? zero_float64?' \
|
|
178
|
+
Examples/ieee-arithmetics.k /tmp/ieee-arithmetics.klib
|
|
177
179
|
```
|
|
178
180
|
|
|
179
181
|
`k-parse` and `k-print` use [textual k values](TEXTUAL_VALUES.md), rather than
|
package/DOCS/FILE_FORMATS.md
CHANGED
|
@@ -67,38 +67,40 @@ This prints `"hello"`. `--lib` loads a dependency; it does not select library
|
|
|
67
67
|
output. These are equivalent ways to generate a library from source:
|
|
68
68
|
|
|
69
69
|
```sh
|
|
70
|
-
node objects/compile.mjs lib/
|
|
71
|
-
node objects/compile.mjs --
|
|
72
|
-
node objects/compile.mjs --lib /tmp/bits.klib lib/arithmetics.k /tmp/k-math.klib
|
|
70
|
+
node objects/compile.mjs lib/integer.k /tmp/integer.klib
|
|
71
|
+
node objects/compile.mjs --format klib lib/integer.k /tmp/integer.klib
|
|
73
72
|
```
|
|
74
73
|
|
|
75
74
|
Only one `--lib` is accepted on the command line by current CLI tools. Chained
|
|
76
75
|
libraries carry forward definitions from their predecessors, so compiling a
|
|
77
76
|
series of libraries extends the environment cumulatively.
|
|
78
77
|
|
|
79
|
-
### Source
|
|
78
|
+
### Source dependency comments
|
|
80
79
|
|
|
81
|
-
To
|
|
80
|
+
To document expected dependencies and recommended `--use` symbols without
|
|
81
|
+
scattering hardcoded content hashes (`@hash`) throughout source code, `.k` files
|
|
82
|
+
can include ordinary `# --lib ...` comments near the top of the file:
|
|
82
83
|
|
|
83
84
|
```k
|
|
84
85
|
# --lib <dependency-path> --use <spec1> <spec2> ...
|
|
85
86
|
```
|
|
86
87
|
|
|
87
|
-
|
|
88
|
+
Because `#` is a standard one-line comment character in k, these lines are valid
|
|
89
|
+
k syntax and are ignored completely by the compiler and CLI tools (they are
|
|
90
|
+
**not** parsed as compiler directives). By convention, `# --lib <path> --use ...`
|
|
91
|
+
lists:
|
|
88
92
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
- `pattern[Params]`: references a named pattern with its parameter signature (e.g. `btrie[V]` or `byte[]`).
|
|
96
|
-
- `pattern[Params]:localname[Params]`: aliases a named pattern under a local name (e.g. `float64[]:number[]`).
|
|
93
|
+
- `<dependency-path>`: the expected dependency (for example, `core.k` or `lib/generic.k`).
|
|
94
|
+
- `--use` specs:
|
|
95
|
+
- `name`: references a library relation or pattern under the same name.
|
|
96
|
+
- `libname:localname`: aliases a library relation or pattern `libname` under `localname`.
|
|
97
|
+
- `pattern[Params]`: references a named pattern with its parameter signature (e.g. `btrie[V]` or `byte[]`).
|
|
98
|
+
- `pattern[Params]:localname[Params]`: aliases a named pattern under `localname` (e.g. `float64[]:number[]`).
|
|
97
99
|
|
|
98
|
-
Multiple `# --lib` lines
|
|
100
|
+
Multiple `# --lib` comment lines may appear when a file uses several libraries:
|
|
99
101
|
|
|
100
102
|
```k
|
|
101
|
-
# --lib
|
|
103
|
+
# --lib core.k --use bit[] bits[]
|
|
102
104
|
```
|
|
103
105
|
|
|
104
106
|
#### How `--lib` and `--use` Work
|
|
@@ -107,27 +109,28 @@ When source code is compiled with library dependencies (`k-compile`, `k.mjs`, `c
|
|
|
107
109
|
|
|
108
110
|
1. **Content-Addressed Availability (Hash-Only)**: When `--lib file.klib` is supplied, all relations and patterns defined in the library are available in memory indexed strictly by their content hashes (`@<hash>`). Friendly names (the aliases recorded in library metadata) are **never** in scope by default.
|
|
109
111
|
2. **Explicit Symbol Scope (`--use`)**: To bring a library relation or pattern into scope under a friendly name, `--use <spec>` must be specified (or `useSpecs` passed in the compiler API). The compiler resolves the spec against the library's metadata aliases and binds the friendly or aliased name directly in the in-memory symbol tables.
|
|
110
|
-
3. **No Automatic Ingestion or Precedence Ambiguity**: Because library friendly names are not in scope without `--use`, there is no implicit shadowing or "precedence". Local definitions define local names; library definitions are accessed by `@<hash>` or by the names explicitly brought into scope via `--use`.
|
|
112
|
+
3. **No Automatic Ingestion or Precedence Ambiguity**: Because library friendly names are not in scope without `--use`, there is no implicit shadowing or "precedence". Local definitions define local names; library definitions are accessed by `@<hash>` or by the names explicitly brought into scope via `--use`. If a name declared via `--use` is redefined (by another `--use` binding or by a local definition), the compiler emits a warning and the later definition wins.
|
|
111
113
|
4. **No Text Synthesis or Line Offset**: The compiler never synthesizes source code or prepends preamble text to user files. Source text remains completely untouched, preserving exact 1-to-1 line numbers in AST nodes, diagnostics, warnings, and error messages.
|
|
112
114
|
|
|
113
115
|
#### Command-Line Usage
|
|
114
116
|
|
|
115
|
-
- Compile a
|
|
117
|
+
- Compile a library or dependent file using explicit `--lib` / `--use` flags:
|
|
116
118
|
```sh
|
|
117
|
-
node objects/compile.mjs
|
|
118
|
-
|
|
119
|
-
|
|
119
|
+
node objects/compile.mjs lib/integer.k /tmp/integer.klib
|
|
120
|
+
node objects/compile.mjs --lib /tmp/integer.klib \
|
|
121
|
+
--use "int[] nat int pos? neg? zero? gt? inc dec" \
|
|
122
|
+
Examples/poly.k /tmp/poly.klib
|
|
120
123
|
```
|
|
121
124
|
- Command-line `--use spec` flags bring relations into scope for evaluation:
|
|
122
125
|
```sh
|
|
123
126
|
printf '42\n' | node codecs/int.mjs --parse |
|
|
124
|
-
node k.mjs --lib /tmp/
|
|
127
|
+
node k.mjs --lib /tmp/integer.klib --use inc 'inc' |
|
|
125
128
|
node codecs/int.mjs --print
|
|
126
129
|
```
|
|
127
130
|
|
|
128
|
-
This prints `43`.
|
|
129
|
-
|
|
130
|
-
names are not builtin literals.
|
|
131
|
+
This prints `43`. `lib/integer.k` defines `0` and `1` as signed integers
|
|
132
|
+
and `nat` to lift unsigned bit sequences such as `{} |_ |1 |0 |1 nat` (5);
|
|
133
|
+
numeral names are not builtin literals.
|
|
131
134
|
|
|
132
135
|
## Inspect and decompile
|
|
133
136
|
|
package/DOCS/GETTING_STARTED.md
CHANGED
|
@@ -117,12 +117,10 @@ constraints. Derivation convergence does not prove that every program terminates
|
|
|
117
117
|
|
|
118
118
|
## Libraries and external numbers
|
|
119
119
|
|
|
120
|
-
Load the integer library
|
|
120
|
+
Load the self-contained integer library and a codec explicitly:
|
|
121
121
|
|
|
122
122
|
```text
|
|
123
|
-
:load lib/
|
|
124
|
-
:load lib/bits.k
|
|
125
|
-
:load lib/arithmetics.k
|
|
123
|
+
:load lib/integer.k
|
|
126
124
|
:codec load codecs/int.mjs
|
|
127
125
|
:input int.mjs 42
|
|
128
126
|
```
|
|
@@ -130,10 +128,10 @@ Load the integer library chain and a codec explicitly:
|
|
|
130
128
|
Now evaluate `inc` to obtain 43. The codec prints the decimal representation
|
|
131
129
|
alongside the structural value.
|
|
132
130
|
|
|
133
|
-
For list operations, load `
|
|
131
|
+
For list operations, load `Examples/poly.k` on top of `lib/integer.k`:
|
|
134
132
|
|
|
135
133
|
```text
|
|
136
|
-
:load lib/
|
|
134
|
+
:load lib/integer.k
|
|
137
135
|
:load Examples/poly.k
|
|
138
136
|
```
|
|
139
137
|
|
|
@@ -145,19 +143,16 @@ The command-line runtime receives binary schema+payload data, so use codecs
|
|
|
145
143
|
at text boundaries. From the repository root:
|
|
146
144
|
|
|
147
145
|
```sh
|
|
148
|
-
node objects/compile.mjs lib/
|
|
149
|
-
node objects/compile.mjs --lib /tmp/bit.klib lib/bits.k /tmp/bits.klib
|
|
150
|
-
node objects/compile.mjs --lib /tmp/bits.klib lib/arithmetics.k /tmp/arithmetics.klib
|
|
146
|
+
node objects/compile.mjs lib/integer.k /tmp/integer.klib
|
|
151
147
|
printf '42\n' | node codecs/int.mjs --parse |
|
|
152
|
-
node k.mjs --lib /tmp/
|
|
148
|
+
node k.mjs --lib /tmp/integer.klib --use inc 'inc' |
|
|
153
149
|
node codecs/int.mjs --print
|
|
154
150
|
```
|
|
155
151
|
|
|
156
|
-
This prints `43`. When compiling with `--lib
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
into scope
|
|
160
|
-
accessible by their canonical content hashes.
|
|
152
|
+
This prints `43`. When compiling or evaluating with `--lib`, library
|
|
153
|
+
definitions are carried in memory indexed by their canonical content hashes
|
|
154
|
+
(`@<hash>`), and `--use` explicitly brings requested friendly names (or
|
|
155
|
+
`libname:localname` aliases) into scope.
|
|
161
156
|
|
|
162
157
|
To compile a small executable object and run it:
|
|
163
158
|
|
package/DOCS/PATTERNS.md
CHANGED
|
@@ -36,15 +36,15 @@ Relations can refer to other definitions in the same source, including recursive
|
|
|
36
36
|
references. Labels can be identifiers or quoted strings with escapes. Line
|
|
37
37
|
comments use `--`, `//`, `#`, or `%`; block comments use `/* ... */`.
|
|
38
38
|
|
|
39
|
-
### Source
|
|
39
|
+
### Source dependency comments
|
|
40
40
|
|
|
41
|
-
Files document
|
|
41
|
+
Files can document expected library dependencies and imported symbols using ordinary `#` comments near the top of the file:
|
|
42
42
|
|
|
43
43
|
```k
|
|
44
44
|
# --lib <dependency-path> --use <spec1> <spec2> ...
|
|
45
45
|
```
|
|
46
46
|
|
|
47
|
-
Because `#` is a standard line comment character in k, these lines are
|
|
47
|
+
Because `#` is a standard line comment character in k, these lines are ignored completely by the compiler and tools. When compiling with `--lib`, definitions from the library are available in memory strictly by their content hashes (`@<hash>`), and `--use` brings requested friendly names or aliases into the local compilation scope. See [file formats and tools](FILE_FORMATS.md#source-dependency-comments) for complete details.
|
|
48
48
|
|
|
49
49
|
## Pattern syntax
|
|
50
50
|
|
package/DOCS/REPL.md
CHANGED
|
@@ -48,15 +48,12 @@ Regular evaluations include timing; there is no separate timing command.
|
|
|
48
48
|
## Loading and aliases
|
|
49
49
|
|
|
50
50
|
```text
|
|
51
|
-
:load lib/
|
|
52
|
-
:load lib/bits.k
|
|
53
|
-
:load lib/arithmetics.k
|
|
54
|
-
:load lib/list.k
|
|
51
|
+
:load lib/integer.k
|
|
55
52
|
:load Examples/poly.k
|
|
56
53
|
```
|
|
57
54
|
|
|
58
55
|
Libraries are loaded explicitly. `poly.k` depends on definitions in
|
|
59
|
-
`lib/
|
|
56
|
+
`lib/integer.k`, so load them in dependency order. Loading
|
|
60
57
|
a codec does not automatically load its corresponding k library.
|
|
61
58
|
|
|
62
59
|
`:load` compiles a `.k` file in the current library context or loads a plain
|
|
@@ -95,7 +92,7 @@ See [patterns and canonical identity](PATTERNS.md).
|
|
|
95
92
|
inc
|
|
96
93
|
```
|
|
97
94
|
|
|
98
|
-
Load `lib/
|
|
95
|
+
Load `lib/integer.k` before using `inc`.
|
|
99
96
|
The codec supplies the integer value; the library supplies the relation.
|
|
100
97
|
|
|
101
98
|
Codecs export `parse` and `print`. Input uses the codec explicitly selected
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
+
# --lib lib/ieee.k --use bit[] sign[] exp11[] frac52[] float64[] all_ones_exponent? exp0 finite_float64? frac0 inf_float64? nan_float64? neg_inf neg_zero negate_float64 pos_inf pos_zero qnan zero_exponent? zero_float64?
|
|
1
2
|
-- IEEE binary64 arithmetic.
|
|
2
3
|
|
|
3
|
-
bit[] = < {} 0, {} 1 >;
|
|
4
|
-
sign[] = < {} "+", {} "-" >;
|
|
5
|
-
|
|
6
4
|
0 = {} |0 ?bit[];
|
|
7
5
|
1 = {} |1 ?bit[];
|
|
8
6
|
0? = ?X /0 |0 ?X;
|
|
@@ -13,32 +11,19 @@ sign[] = < {} "+", {} "-" >;
|
|
|
13
11
|
+? = ?sign[] /"+" +;
|
|
14
12
|
-? = ?sign[] /"-" -;
|
|
15
13
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
exp0 = @5CevJXf7TxM9xpnE6d7wUPxCNUCb8hjcyDxK27Qdb99d;
|
|
30
|
-
finite_float64? = @rXgbm83aU9KXZKsnUTKaMa6xE4L4SYr53QKWj55HgTXX;
|
|
31
|
-
frac0 = @cEAdYip46Ce9nsZS8HvRg6iHqeLaAzWBcB5PvWyZHcPz;
|
|
32
|
-
inf_float64? = @EHkVn6SrkQf9xx5j7nxBpA4wrMbrmnkNZGAGVHZESTbE;
|
|
33
|
-
nan_float64? = @C4gMueJKY8pkL4sXhA9Mm4myUdMfMnKLDNsJijr4Gzmw;
|
|
34
|
-
neg_inf = @dDUVHjeQsLRfZ9P2faLv7uHd94w8XUxhug9xJr3CxhGA;
|
|
35
|
-
neg_zero = @KkPWjqrqkhgNXTTeG8J7ZZhBUjVmN9nRnp42j6nRtb8V;
|
|
36
|
-
negate_float64 = @3V9Ve52CnGsvJ9xxHgBwL47L4ac3Xh98bPuLJLHgeU78;
|
|
37
|
-
pos_inf = @8paZ6U6NYfRBsnQHEJQFxtSWsET5WyAZBDUWaZkcPtmz;
|
|
38
|
-
pos_zero = @QTRfm3S4BKsqqt7WDPAPhfNA2dajxcj9iyAtvnqpMcdf;
|
|
39
|
-
qnan = @tS5xBCNSeuX3ahkFgk8wpX8nB6XrH9teHvYhhKxTFu7h;
|
|
40
|
-
zero_exponent? = @NqwhAUhQMnLpZgCevmmMtDFcSLtfxLgkbq85ckwgrKHy;
|
|
41
|
-
zero_float64? = @4p9tjLASFGTZCbW7sha7JzZDBkCNuVwyLhLKiQfBJSNV;
|
|
14
|
+
not_bit = ?bit[] < /0 1, /1 0 > ?bit[];
|
|
15
|
+
and_bit = ?{ bit[] x, bit[] y } <
|
|
16
|
+
{ .x 0? _x, .y ?bit[] _y } 0,
|
|
17
|
+
{ .x 1? _x, .y ?bit[] y } .y
|
|
18
|
+
> ?bit[];
|
|
19
|
+
or_bit = ?{ bit[] x, bit[] y } <
|
|
20
|
+
{ .x 1? _x, .y ?bit[] _y } 1,
|
|
21
|
+
{ .x 0? _x, .y ?bit[] y } .y
|
|
22
|
+
> ?bit[];
|
|
23
|
+
xor_bit = ?{ bit[] x, bit[] y } <
|
|
24
|
+
{ .x 0? _x, .y ?bit[] y } .y,
|
|
25
|
+
{ .x 1? _x, .y ?bit[] y } .y not_bit
|
|
26
|
+
> ?bit[];
|
|
42
27
|
|
|
43
28
|
// Arithmetic-specific patterns and ripple-carry helpers.
|
|
44
29
|
|
package/Examples/poly.k
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
+
# --lib lib/integer.k --use int[] nat int pos? neg? zero? gt? inc dec
|
|
1
2
|
-- Examples/poly.k — Polymorphic list operations indexed by int[]
|
|
2
3
|
|
|
3
4
|
---------------------------------------------------------------
|
|
4
|
-
-- §1. Base Patterns
|
|
5
|
+
-- §1. Base Patterns & Primitives
|
|
5
6
|
---------------------------------------------------------------
|
|
6
7
|
|
|
7
8
|
bits[] = < {} _, B 0, B 1 > = B;
|
|
8
|
-
int[] = < bits[] -, bits[] + >;
|
|
9
9
|
list[X] = < {} nil, { X car, L cdr } cons > = L;
|
|
10
10
|
|
|
11
11
|
_ = {} |_ ?bits[];
|
|
@@ -24,25 +24,21 @@ cdr = ?list[X] /cons .cdr;
|
|
|
24
24
|
nil? = un_nil nil;
|
|
25
25
|
singleton = { () car, nil cdr } cons;
|
|
26
26
|
|
|
27
|
-
# --lib lib/arithmetics.k --use nat int neg? zero_int? GT inc dec
|
|
28
|
-
nat = @ktrZRefK3zvnUqYYisWBH3MgzcQJD9hs2P5LeSJ7tFrA;
|
|
29
|
-
int = @BgUayEar88hcmcAYM5giDsucyGbtumbaXpyussK8k7vm;
|
|
30
|
-
neg? = @Hwv3CjPRgtJDb7ZGn7cReF9iZ25LFpjv2MXRBJyUGBij;
|
|
31
|
-
zero_int? = @eYsSuRAeFTejC7W9mLLtxuSeebLuszMGxadnZsg4iuUf;
|
|
32
|
-
GT = @qWipdJHEEG3KccU4RVFLrWciLCvP2Cbz7myNxjNtvUCP;
|
|
33
|
-
inc = @FpWRmLNa6guwACtpwYUAuX3usE27WqAKPzQxrVRtyKND;
|
|
34
|
-
dec = @3YRXdfNimGSWfSpCcTzBauTU7HRrjLUFMp4dbRZGhJXv;
|
|
35
|
-
|
|
36
|
-
# --lib lib/list.k --use rev_append reverse
|
|
37
|
-
rev_append = @7xb2uLU5cS7XgmFbKMrvg4UwpcTQ3whSMzBMeaSnGwkm;
|
|
38
|
-
reverse = @peiZq2evd5u4Qk42AT3W4YmWZZ3JAeXNKTnmDtpb9EZh;
|
|
39
|
-
|
|
40
27
|
---------------------------------------------------------------
|
|
41
28
|
-- §2. Polymorphic List Operations
|
|
42
29
|
---------------------------------------------------------------
|
|
43
30
|
|
|
44
|
-
--
|
|
45
|
-
|
|
31
|
+
-- Reverse-append x onto y: {x, y} -> list
|
|
32
|
+
rev_append = ?{ list[X] x, list[X] y } <
|
|
33
|
+
{ .x un_nil if, .y then } .then,
|
|
34
|
+
{ .x cdr x, { .x car car, .y cdr } cons y } rev_append
|
|
35
|
+
> ?list[X];
|
|
36
|
+
|
|
37
|
+
-- Reverse a list: xs -> list
|
|
38
|
+
reverse = { () x, nil y } rev_append;
|
|
39
|
+
|
|
40
|
+
-- Decrement a positive integer
|
|
41
|
+
pred_int = pos? dec;
|
|
46
42
|
|
|
47
43
|
-- Concatenate two lists: {xs, ys} -> list
|
|
48
44
|
concat = { .xs reverse x, .ys y } rev_append;
|
|
@@ -52,17 +48,17 @@ length_loop = <
|
|
|
52
48
|
{ .xs un_nil if, .acc then } .then,
|
|
53
49
|
{ .xs cdr xs, .acc inc acc } length_loop
|
|
54
50
|
>;
|
|
55
|
-
length = { () xs, 0
|
|
51
|
+
length = { () xs, 0 nat acc } length_loop;
|
|
56
52
|
|
|
57
53
|
-- Get N-th element of a list (0-indexed): {n, xs} -> element
|
|
58
54
|
get_nth = <
|
|
59
|
-
{ .n
|
|
55
|
+
{ .n zero? if, .xs car then } .then,
|
|
60
56
|
{ .n pred_int n, .xs cdr xs } get_nth
|
|
61
57
|
>;
|
|
62
58
|
|
|
63
59
|
-- Split list at index N: {n, xs} -> {first, second}
|
|
64
60
|
split_by_loop = <
|
|
65
|
-
{ .n
|
|
61
|
+
{ .n zero? if, { .acc reverse first, .xs second } then } .then,
|
|
66
62
|
{ .xs un_nil if, { .acc reverse first, nil second } then } .then,
|
|
67
63
|
{ .n pred_int n, .xs cdr xs, { .xs car car, .acc cdr } cons acc } split_by_loop
|
|
68
64
|
>;
|
|
@@ -73,14 +69,14 @@ take = split_by .first;
|
|
|
73
69
|
|
|
74
70
|
-- Drop the first N elements of a list: {n, xs} -> list
|
|
75
71
|
drop = <
|
|
76
|
-
{ .n
|
|
72
|
+
{ .n zero? if, .xs then } .then,
|
|
77
73
|
{ .xs un_nil if, nil then } .then,
|
|
78
74
|
{ .n pred_int n, .xs cdr xs } drop
|
|
79
75
|
>;
|
|
80
76
|
|
|
81
77
|
-- Repeat element X, N times: {n, x} -> list
|
|
82
78
|
repeat_loop = <
|
|
83
|
-
{ .n
|
|
79
|
+
{ .n zero? if, .acc then } .then,
|
|
84
80
|
{ .n pred_int n, .x x, { .x car, .acc cdr } cons acc } repeat_loop
|
|
85
81
|
>;
|
|
86
82
|
repeat = { .n n, .x x, nil acc } repeat_loop;
|
|
@@ -106,13 +102,6 @@ gen_list_loop = <
|
|
|
106
102
|
>;
|
|
107
103
|
gen_list = { () n, nil list } gen_list_loop;
|
|
108
104
|
|
|
109
|
-
-- Identity filter on {x, y} when x > y
|
|
110
|
-
gt? = <
|
|
111
|
-
{ .x un_neg if1, .y un_pos if2, () then } .then,
|
|
112
|
-
{ { .x un_neg y, .y un_neg x } GT if, () then } .then,
|
|
113
|
-
{ { .x un_pos x, .y un_pos y } GT if, () then } .then
|
|
114
|
-
>;
|
|
115
|
-
|
|
116
105
|
-- Split list into two halves by alternating elements: xs -> {first, second}
|
|
117
106
|
split_loop = <
|
|
118
107
|
{ .xs un_nil if, { .first first, .second second } then } .then,
|
package/README.md
CHANGED
|
@@ -63,25 +63,21 @@ relations, libraries, and codecs.
|
|
|
63
63
|
|
|
64
64
|
| File | Purpose |
|
|
65
65
|
| --- | --- |
|
|
66
|
-
| [
|
|
67
|
-
| [lib/
|
|
68
|
-
| [lib/
|
|
69
|
-
| [lib/
|
|
70
|
-
| [lib/
|
|
71
|
-
| [lib/string.k](lib/string.k) | ASCII bytes, Unicode codepoints, and strings (`ascii[]`, `unicode[]`, `string[]`) |
|
|
72
|
-
| [lib/ieee.k](lib/ieee.k) | Binary64 types and `parseFloat64` / `printFloat64` (`float64[]`) |
|
|
73
|
-
| [lib/djson.k](lib/djson.k) | JSON text parsing (`parseJson`) and dynamic JSON operations (`djson[]`) |
|
|
74
|
-
| [core.k](core.k) | Standalone bit, Unicode string, and self-hosted pattern schemas |
|
|
66
|
+
| [core.k](core.k) | Standalone bit, Unicode string, and self-hosted pattern schemas (`bit[]`, `bits[]`, `list[X]`, `string[]`, `pattern[]`) |
|
|
67
|
+
| [lib/generic.k](lib/generic.k) | Algebraic types, binary tries, and string/bit codecs (`bool[]`, `order[]`, `opt[V]`, `btrie[V]`, `string_to_bits`, `bits_to_string`) |
|
|
68
|
+
| [lib/integer.k](lib/integer.k) | Signed arbitrary-precision integer arithmetic (`int[]`, `nat`, `int`, `plus`, `minus`, `times` via Karatsuba, `divmod`, `div`, `mod`, `gcd`, `lcm`) |
|
|
69
|
+
| [lib/ieee.k](lib/ieee.k) | Binary64 types, predicates, negation, and rational conversions (`float64[]`, `negate_float64`, `bits_to_float64`, `ratio_to_float64`) |
|
|
70
|
+
| [lib/djson.k](lib/djson.k) | RFC 8259 JSON parser (`parse_json`) and dynamic JSON operations (`djson[]`, `get`, `nth`, `set`, `del`, `push`, `keys`, `values`) |
|
|
75
71
|
| [Examples/nat.k](Examples/nat.k) | Small Peano arithmetic example |
|
|
76
|
-
| [Examples/
|
|
77
|
-
| [Examples/
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
72
|
+
| [Examples/poly.k](Examples/poly.k) | Polymorphic list operations indexed by `int[]`; load `lib/integer.k` first |
|
|
73
|
+
| [Examples/ieee-arithmetics.k](Examples/ieee-arithmetics.k) | Binary64 arithmetic returning `{result, flags}`; load `core.k` and `lib/ieee.k` first |
|
|
74
|
+
|
|
75
|
+
Load libraries explicitly with `:load` in the REPL. Leading
|
|
76
|
+
`# --lib <dependency> --use <specs>` lines in source files are ordinary
|
|
77
|
+
human-readable comments documenting expected dependencies. On the CLI,
|
|
78
|
+
`--lib <dependency.klib>` makes library definitions available in memory by
|
|
79
|
+
content hash (`@<hash>`), while `--use` explicitly brings requested friendly
|
|
80
|
+
names or aliases into compilation scope. See
|
|
85
81
|
[file formats and tools](DOCS/FILE_FORMATS.md).
|
|
86
82
|
|
|
87
83
|
## JavaScript API
|
package/backend-api.mjs
CHANGED
|
@@ -21,9 +21,7 @@ export {
|
|
|
21
21
|
compileLibrary,
|
|
22
22
|
decodeObject,
|
|
23
23
|
loadLibrary,
|
|
24
|
-
buildUsePreamble
|
|
25
|
-
parseHeaderDirectives,
|
|
26
|
-
parseHeaderUses
|
|
24
|
+
buildUsePreamble
|
|
27
25
|
} from "./object.mjs";
|
|
28
26
|
export { createState, evaluateInput } from "./repl.mjs";
|
|
29
27
|
export { parse as parseFloat64, print as printFloat64 } from "./codecs/ieee.mjs";
|
|
@@ -121,12 +121,8 @@ printf '2' \
|
|
|
121
121
|
|
|
122
122
|
node --input-type=module -e "import assert from 'node:assert/strict'; import { spawn } from 'node:child_process'; import { encodeToWire, decodeWire } from '../../codecs/runtime/prefix-codec.mjs'; import { parse, INT_PATTERN } from '../../codecs/int.mjs'; const input = encodeToWire(parse('2'), INT_PATTERN); const header = Buffer.alloc(4); header.writeUInt32BE(input.length); const child = spawn(process.argv[1], ['--server']); const chunks = []; const stderr = []; child.stdout.on('data', (chunk) => chunks.push(chunk)); child.stderr.on('data', (chunk) => stderr.push(chunk)); child.stdin.end(Buffer.concat([header, input])); const status = await new Promise((resolve, reject) => { child.on('error', reject); child.on('close', resolve); }); assert.equal(status, 0, Buffer.concat(stderr).toString('utf8')); const output = Buffer.concat(chunks); const length = output.readUInt32BE(0); assert.equal(output.length, 4 + length); assert.deepEqual(decodeWire(output.subarray(4)).value.toJSON(), parse('2').toJSON());" "$TMP_DIR/id-int-exe"
|
|
123
123
|
|
|
124
|
-
node ../../objects/compile.mjs ../../lib/
|
|
125
|
-
node
|
|
126
|
-
node ../../objects/compile.mjs --lib "$TMP_DIR/bits.klib" \
|
|
127
|
-
--use "trim inv_loop inv cmp inc_bits dec_bits plus_bits minus_bits mul_bits" \
|
|
128
|
-
../../lib/arithmetics.k "$TMP_DIR/arithmetics.klib"
|
|
129
|
-
node ./bin/k-llvm-build.mjs --lib "$TMP_DIR/arithmetics.klib" --use times 's = ?< <bits 0, bits 1, {} _>=bits "+", bits "-" > {()x, ()y} times; s' -o "$TMP_DIR/square-exe"
|
|
124
|
+
node ../../objects/compile.mjs ../../lib/integer.k "$TMP_DIR/integer.klib"
|
|
125
|
+
node ./bin/k-llvm-build.mjs --lib "$TMP_DIR/integer.klib" --use times 's = ?< <bits 0, bits 1, {} _>=bits "+", bits "-" > {()x, ()y} times; s' -o "$TMP_DIR/square-exe"
|
|
130
126
|
printf '987654321' \
|
|
131
127
|
| node ../../codecs/int.mjs --parse \
|
|
132
128
|
| "$TMP_DIR/square-exe" \
|
|
@@ -176,10 +176,9 @@ compile_step() {
|
|
|
176
176
|
local prev="$1"
|
|
177
177
|
local src="$2"
|
|
178
178
|
local out="$3"
|
|
179
|
+
local use_spec="$4"
|
|
179
180
|
local uses=()
|
|
180
|
-
|
|
181
|
-
[ -n "$line" ] && uses+=(--use "$line")
|
|
182
|
-
done < <(node -e 'import("'$K_ROOT'/canonical-pattern.mjs").then(m => { for (const u of m.parseHeaderUses(fs.readFileSync(process.argv[1],"utf8"))) console.log(u.use); })' "$src" 2>/dev/null || true)
|
|
181
|
+
[ -n "$use_spec" ] && uses=(--use "$use_spec")
|
|
183
182
|
if [ -n "$prev" ]; then
|
|
184
183
|
node "$K_ROOT/objects/compile.mjs" --lib "$prev" "${uses[@]}" "$src" "$out"
|
|
185
184
|
else
|
|
@@ -187,13 +186,9 @@ compile_step() {
|
|
|
187
186
|
fi
|
|
188
187
|
}
|
|
189
188
|
|
|
190
|
-
compile_step "" "$K_ROOT/
|
|
191
|
-
compile_step "$TMP_DIR/
|
|
192
|
-
compile_step "$TMP_DIR/
|
|
193
|
-
compile_step "$TMP_DIR/list.klib" "$K_ROOT/lib/btrie.k" "$TMP_DIR/btrie.klib"
|
|
194
|
-
compile_step "$TMP_DIR/btrie.klib" "$K_ROOT/lib/string.k" "$TMP_DIR/string.klib"
|
|
195
|
-
compile_step "$TMP_DIR/string.klib" "$K_ROOT/lib/ieee.k" "$TMP_DIR/ieee-types.klib"
|
|
196
|
-
compile_step "$TMP_DIR/ieee-types.klib" "$K_ROOT/Examples/ieee-arithmetics.k" "$TMP_DIR/ieee.klib"
|
|
189
|
+
compile_step "" "$K_ROOT/core.k" "$TMP_DIR/core.klib" ""
|
|
190
|
+
compile_step "$TMP_DIR/core.klib" "$K_ROOT/lib/ieee.k" "$TMP_DIR/ieee-types.klib" "bit[] bits[]"
|
|
191
|
+
compile_step "$TMP_DIR/ieee-types.klib" "$K_ROOT/Examples/ieee-arithmetics.k" "$TMP_DIR/ieee.klib" "bit[] sign[] exp11[] frac52[] float64[] all_ones_exponent? exp0 finite_float64? frac0 inf_float64? nan_float64? neg_inf neg_zero negate_float64 pos_inf pos_zero qnan zero_exponent? zero_float64?"
|
|
197
192
|
if node ./bin/k-wasm-compile.mjs --lib "$TMP_DIR/ieee.klib" --lib "$TMP_DIR/ieee.klib" --use mul:times "{()x,()y} times .result" "$TMP_DIR/repeated-lib.wasm" >/dev/null 2>&1; then
|
|
198
193
|
echo "--lib unexpectedly accepted more than once" >&2
|
|
199
194
|
exit 1
|