@fraczak/k 6.0.0 → 6.0.2
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/.codex +0 -0
- package/AGENTS.md +35 -0
- package/CHANGELOG.md +48 -0
- package/CLAUDE.md +64 -0
- package/DOCS/BIG_PICTURE.md +4 -8
- package/DOCS/BINARY_CODEC_STATUS_AND_TODO.md +74 -0
- package/DOCS/CONVERGENCE.md +92 -0
- package/DOCS/DIVERGENCE_DETECTION.md +24 -0
- package/DOCS/Makefile +99 -0
- package/DOCS/OBJECT_FILE_AND_PATTERN.md +217 -0
- package/DOCS/PATTERNS.md +114 -0
- package/DOCS/PATTERNS_AND_TYPED_VALUES.md +665 -0
- package/DOCS/REPL.md +125 -125
- package/DOCS/TEXTUAL_VALUES.md +108 -0
- package/DOCS/TYPE_DERIVATION.md +230 -0
- package/DOCS/a.html +398 -0
- package/DOCS/book/06-values-in-memory.md +1085 -0
- package/DOCS/book/07-the-partial-function-abi.md +143 -0
- package/DOCS/book/08-operational-semantics-and-execution.md +201 -0
- package/DOCS/book/09-compiler-architecture.md +160 -0
- package/DOCS/book/10-from-ast-to-intermediate-representation.md +167 -0
- package/DOCS/book/14-canonical-serialization.md +176 -0
- package/DOCS/book/appendix-b-incorporating-external-predefined-types-and-functions.md +155 -0
- package/DOCS/book/overview.txt +68 -0
- package/NAT.k~ +452 -0
- package/README.md +71 -27
- package/codecs/BINARY_FORMAT.html +471 -0
- package/codecs/POLYMORPHIC_BINARY_FORMAT.html +355 -0
- package/codecs/README.md +8 -0
- package/codecs/pandoc.css +71 -0
- package/codecs/runtime/test-codec.mjs +89 -0
- package/ieee.klib +0 -0
- package/k-vscode/package-lock.json +4176 -0
- package/package.json +10 -13
- package/{codecs/k-pattern.mjs → patterns/from-k.mjs} +3 -3
- package/repl2.mjs +270 -51
- package/scripts/generate-dependency-graph.mjs +121 -0
- package/test-repl2.mjs +67 -9
- package/type_registry/registry.mjs~ +105 -0
- package/type_registry/typeParser.jison~ +132 -0
package/NAT.k~
ADDED
|
@@ -0,0 +1,452 @@
|
|
|
1
|
+
-- Examples for defining arithmetic functions natively
|
|
2
|
+
$ yes_no = < {} yes, {} no >;
|
|
3
|
+
swap_yes_no = < /yes |no, /no |yes > $yes_no;
|
|
4
|
+
yes = {}|yes $yes_no;
|
|
5
|
+
no = yes swap_yes_no;
|
|
6
|
+
|
|
7
|
+
list? = ?< {} nil, {X car, Y cdr} cons > = Y;
|
|
8
|
+
nil = {}|nil list?;
|
|
9
|
+
singleton = {() car, nil cdr} |cons list?;
|
|
10
|
+
cons = {.car car, .cdr cdr} |cons list?;
|
|
11
|
+
nil? = list? / nil nil;
|
|
12
|
+
car = list? /cons .car;
|
|
13
|
+
cdr = list? /cons .cdr;
|
|
14
|
+
|
|
15
|
+
$ bits = < {} _, bits 0, bits 1 >;
|
|
16
|
+
|
|
17
|
+
0b = {} | _;
|
|
18
|
+
1_ = | 1;
|
|
19
|
+
0_ = | 0;
|
|
20
|
+
|
|
21
|
+
0 = 0b 0_;
|
|
22
|
+
succ = {inv x, 0b y} _inc;
|
|
23
|
+
1 = 0 succ;
|
|
24
|
+
2 = 1 succ;
|
|
25
|
+
3 = 2 succ;
|
|
26
|
+
4 = 3 succ;
|
|
27
|
+
5 = 4 succ;
|
|
28
|
+
6 = 5 succ;
|
|
29
|
+
7 = 6 succ;
|
|
30
|
+
8 = 7 succ;
|
|
31
|
+
9 = 8 succ;
|
|
32
|
+
10 = 9 succ;
|
|
33
|
+
11 = 10 succ;
|
|
34
|
+
12 = 11 succ;
|
|
35
|
+
|
|
36
|
+
empty = $bits < /_ yes, no >;
|
|
37
|
+
empty? = {empty /yes if, () then} .then;
|
|
38
|
+
|
|
39
|
+
concat = ${bits x, bits y} {.x inv x, .y y} concat_;
|
|
40
|
+
concat_ = ${bits x, bits y}
|
|
41
|
+
<
|
|
42
|
+
{.x /0 x, .y 0_ y} concat_,
|
|
43
|
+
{.x /1 x, .y 1_ y} concat_,
|
|
44
|
+
.y
|
|
45
|
+
>;
|
|
46
|
+
inv = {()x, 0b r} _inv;
|
|
47
|
+
_inv = ${bits x, bits r} < {.x /0 x, .r 0_ r} _inv, {.x /1 x, .r 1_ r} _inv, .r >;
|
|
48
|
+
|
|
49
|
+
inOrder = ${bits x, bits y} {.x inv x, .y inv y, yes inOrder} inOrder_;
|
|
50
|
+
inOrder_ = ${bits x, bits y, yes_no inOrder}
|
|
51
|
+
<
|
|
52
|
+
{.x /0 x, .y /0 y, .inOrder inOrder} inOrder_,
|
|
53
|
+
{.x /1 x, .y /1 y, .inOrder inOrder} inOrder_,
|
|
54
|
+
{.x /0 x, .y /1 y, .inOrder inOrder} inOrder_,
|
|
55
|
+
{.x /1 y, .y /0 x, .inOrder swap_yes_no inOrder} inOrder_,
|
|
56
|
+
{.x </0,/1> if, .inOrder swap_yes_no then} .then,
|
|
57
|
+
.inOrder
|
|
58
|
+
>;
|
|
59
|
+
|
|
60
|
+
GT = {inOrder /no if, () then} . then;
|
|
61
|
+
|
|
62
|
+
EQ = GT {.x y, .y x} GT;
|
|
63
|
+
|
|
64
|
+
toBitList = {inv x, nil r} _toBitList;
|
|
65
|
+
_toBitList = <
|
|
66
|
+
{.x empty? if, .r then }.then,
|
|
67
|
+
{.x /0 x, {0 car, .r cdr} cons r} _toBitList,
|
|
68
|
+
{.x /1 x, {1 car, .r cdr} cons r} _toBitList
|
|
69
|
+
>;
|
|
70
|
+
fromBitList =
|
|
71
|
+
<
|
|
72
|
+
cdr <
|
|
73
|
+
{car /0 if, cdr fromBitList 0_ then} .then,
|
|
74
|
+
{car /1 if, cdr fromBitList 1_ then} .then
|
|
75
|
+
>,
|
|
76
|
+
0b
|
|
77
|
+
> $bits;
|
|
78
|
+
|
|
79
|
+
--------------------------------------
|
|
80
|
+
$ int = < bits '+', bits '-' >;
|
|
81
|
+
|
|
82
|
+
remove_leading_zeros = <
|
|
83
|
+
/0 remove_leading_zeros,
|
|
84
|
+
()
|
|
85
|
+
>;
|
|
86
|
+
nat = remove_leading_zeros <empty? 0, ()>;
|
|
87
|
+
int = nat | '+' $int;
|
|
88
|
+
abs = $int </'+',/'-'>;
|
|
89
|
+
|
|
90
|
+
pos? = $int {dec /'+' if, () then} .then;
|
|
91
|
+
neg? = $int {/'-' if, () then} .then;
|
|
92
|
+
zero_int? = $int {/'+' /0 empty? if, () then} .then;
|
|
93
|
+
minus_one? = $int {/'-' /1 empty? if, () then} .then;
|
|
94
|
+
|
|
95
|
+
inc = $int <
|
|
96
|
+
/'+' {inv x, 0b y} _inc |'+',
|
|
97
|
+
minus_one? 0 int,
|
|
98
|
+
/'-' {inv x, 0b y} _dec |'-'
|
|
99
|
+
>;
|
|
100
|
+
|
|
101
|
+
_inc = ${bits x, bits y} <
|
|
102
|
+
{.x /0 x, .y 1_ y} concat_,
|
|
103
|
+
{.x /1 x, .y 0_ y} _inc,
|
|
104
|
+
.y 1_
|
|
105
|
+
>;
|
|
106
|
+
|
|
107
|
+
dec = $int <
|
|
108
|
+
zero_int? 1 |'-',
|
|
109
|
+
/'+' {inv x, 0b y} _dec |'+',
|
|
110
|
+
/'-' {inv x, 0b y} _inc |'-'
|
|
111
|
+
> $int;
|
|
112
|
+
|
|
113
|
+
_dec = ?{X x, X y, ...} <
|
|
114
|
+
{.x /1 x, .y 0_ y} concat_,
|
|
115
|
+
{.x /0 x, .y 1_ y} _dec
|
|
116
|
+
> ?X;
|
|
117
|
+
|
|
118
|
+
$carry = <{} 0, {} 1>;
|
|
119
|
+
c0 = {}|0 $carry;
|
|
120
|
+
c1 = {}|1 $carry;
|
|
121
|
+
|
|
122
|
+
plus = ${int x, int y}
|
|
123
|
+
<
|
|
124
|
+
{.x/'+' inv x, .y/'+' inv y, c0 c } _plus inv nat | '+',
|
|
125
|
+
{.x/'-' inv x, .y/'-' inv y, c0 c } _plus inv nat | '-',
|
|
126
|
+
{.y/'-' x, .x/'+' y} GT {.x inv x, .y inv y, c0 c} _minus inv nat | '-',
|
|
127
|
+
{.x/'+' inv x, .y/'-' inv y, c0 c} _minus inv nat |'+',
|
|
128
|
+
{.x/'-' x, .y/'+' y} GT {.x inv x, .y inv y, c0 c} _minus inv nat | '-',
|
|
129
|
+
{.y/'+' inv x, .x/'-' inv y, c0 c} _minus inv nat | '+'
|
|
130
|
+
>
|
|
131
|
+
$int;
|
|
132
|
+
|
|
133
|
+
swap_sign = <
|
|
134
|
+
zero_int?,
|
|
135
|
+
/'+' | '-',
|
|
136
|
+
/'-' | '+'
|
|
137
|
+
>;
|
|
138
|
+
|
|
139
|
+
minus = ${int x, int y}
|
|
140
|
+
{.x x, .y swap_sign y} plus;
|
|
141
|
+
|
|
142
|
+
_plus =
|
|
143
|
+
-- least significant bit first addition with carry
|
|
144
|
+
${bits x, bits y, carry c}
|
|
145
|
+
<
|
|
146
|
+
{.x empty? if, .y empty? and, <.c/0 0, 1> then} .then,
|
|
147
|
+
{.x empty? if, .c/0 and, .y then} .then,
|
|
148
|
+
|
|
149
|
+
{.x y, .y empty? x, .c c} _plus,
|
|
150
|
+
{<.c/0 0, 1> x,
|
|
151
|
+
.y y, .x empty? c0 c} _plus,
|
|
152
|
+
|
|
153
|
+
{.x /0 x, .y /0 y, .c/0 c0 c} _plus 0_,
|
|
154
|
+
{.x /0 x, .y /0 y, .c/1 c0 c} _plus 1_,
|
|
155
|
+
|
|
156
|
+
{.x /0 x, .y /1 y, .c/0 c0 c} _plus 1_,
|
|
157
|
+
{.x /0 x, .y /1 y, .c/1 c1 c} _plus 0_,
|
|
158
|
+
{.x /1 x, .y /0 y, .c/0 c0 c} _plus 1_,
|
|
159
|
+
{.x /1 x, .y /0 y, .c/1 c1 c} _plus 0_,
|
|
160
|
+
|
|
161
|
+
{.x /1 x, .y /1 y, .c/0 c1 c} _plus 0_,
|
|
162
|
+
{.x /1 x, .y /1 y, .c/1 c1 c} _plus 1_
|
|
163
|
+
>
|
|
164
|
+
;
|
|
165
|
+
|
|
166
|
+
_minus =
|
|
167
|
+
-- least significat bit first substraction with carry
|
|
168
|
+
${bits x, bits y, carry c}
|
|
169
|
+
<
|
|
170
|
+
{.x then, .y empty? if, .c/0 and} .then,
|
|
171
|
+
{.x/1 then, .y empty? if, .c/1 and} .then 0_,
|
|
172
|
+
{.x/0 x, .y empty? y, .c/1 c1 c} _minus 1_,
|
|
173
|
+
{.x/0 x, .y/0 y, .c/0 c0 c} _minus 0_,
|
|
174
|
+
{.x/0 x, .y/0 y, .c/1 c1 c} _minus 1_,
|
|
175
|
+
{.x/0 x, .y/1 y, .c/0 c1 c} _minus 1_,
|
|
176
|
+
{.x/0 x, .y/1 y, .c/1 c1 c} _minus 0_,
|
|
177
|
+
{.x/1 x, .y/0 y, .c/0 c0 c} _minus 1_,
|
|
178
|
+
{.x/1 x, .y/0 y, .c/1 c0 c} _minus 0_,
|
|
179
|
+
{.x/1 x, .y/1 y, .c/0 c0 c} _minus 0_,
|
|
180
|
+
{.x/1 x, .y/1 y, .c/1 c1 c} _minus 1_
|
|
181
|
+
>
|
|
182
|
+
;
|
|
183
|
+
|
|
184
|
+
$ sign = < {} '+' , {} '-' >;
|
|
185
|
+
sign_plus = {}|'+' $sign;
|
|
186
|
+
sign_minus = {}|'-' $sign;
|
|
187
|
+
get_sign = $int </'+' sign_plus, /'-' sign_minus>;
|
|
188
|
+
|
|
189
|
+
times = ${int x, int y}
|
|
190
|
+
{
|
|
191
|
+
<
|
|
192
|
+
{.x/'+' if, .y /'+' and} sign_plus,
|
|
193
|
+
{.x/'-' if, .y /'-' and} sign_plus,
|
|
194
|
+
sign_minus
|
|
195
|
+
> sign,
|
|
196
|
+
{
|
|
197
|
+
0 result,
|
|
198
|
+
.x abs inv _x,
|
|
199
|
+
.y abs y
|
|
200
|
+
} _times result
|
|
201
|
+
}
|
|
202
|
+
<
|
|
203
|
+
{.sign/'+' if, .result inv int then} .then,
|
|
204
|
+
{.sign/'-' if, .result inv int swap_sign then} .then
|
|
205
|
+
>
|
|
206
|
+
;
|
|
207
|
+
|
|
208
|
+
_times =
|
|
209
|
+
${bits result, bits _x, bits y}
|
|
210
|
+
<
|
|
211
|
+
{ .y empty? if, .result then } .then,
|
|
212
|
+
{
|
|
213
|
+
.result 0_ result,
|
|
214
|
+
._x _x,
|
|
215
|
+
.y /0 y
|
|
216
|
+
} _times,
|
|
217
|
+
{
|
|
218
|
+
{._x x, .result 0_ y, c0 c } _plus result,
|
|
219
|
+
._x _x,
|
|
220
|
+
.y /1 y
|
|
221
|
+
} _times
|
|
222
|
+
>
|
|
223
|
+
;
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
mod =
|
|
227
|
+
${bits x, bits y}
|
|
228
|
+
_log!
|
|
229
|
+
<
|
|
230
|
+
-- if y > x then (x mod y) = x
|
|
231
|
+
{.y x, .x y} GT .y,
|
|
232
|
+
-- else, i.e.
|
|
233
|
+
-- if x >= y then (x mod y) = ((x-y mod 2y) mod y)
|
|
234
|
+
{
|
|
235
|
+
{
|
|
236
|
+
{.x int x, .y int y} minus /"+" x,
|
|
237
|
+
.y inv 0_ inv y
|
|
238
|
+
} mod x,
|
|
239
|
+
.y y
|
|
240
|
+
} mod
|
|
241
|
+
>
|
|
242
|
+
;
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
// gratest common divisor
|
|
246
|
+
gcd = ${bits x, bits y}
|
|
247
|
+
<
|
|
248
|
+
{.y x, .x y} GT,
|
|
249
|
+
()
|
|
250
|
+
> _gcd
|
|
251
|
+
;
|
|
252
|
+
|
|
253
|
+
_gcd = <
|
|
254
|
+
{ .y /0 if, .x then } .then,
|
|
255
|
+
{ .y x, mod y } _gcd
|
|
256
|
+
>;
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
// div
|
|
261
|
+
|
|
262
|
+
log2_floor =
|
|
263
|
+
${bits x, bits step}
|
|
264
|
+
-- finds the largest k, such that 'step * 2^k <= x',
|
|
265
|
+
-- returns k1_, i.e., 0 if x < step, and k1_ otherwise.
|
|
266
|
+
{.x x, .step step, 0 log} log2_floor_;
|
|
267
|
+
|
|
268
|
+
log2_floor_ = <
|
|
269
|
+
{{.step x,.x y} GT if, .log then} .then,
|
|
270
|
+
{.x x, {.step x,0 y} GT {.x x,2 y} {.x int x, .y int y} times abs step, .log int inc abs log} log2_floor_
|
|
271
|
+
>;
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
x_times_2_to_k =
|
|
275
|
+
${bits x, bits k}
|
|
276
|
+
{.x inv _x, .k int k} _x_times_2_to_the_power_of_k inv
|
|
277
|
+
;
|
|
278
|
+
|
|
279
|
+
_x_times_2_to_the_power_of_k = <
|
|
280
|
+
{.k zero_int? if, ._x then} .then,
|
|
281
|
+
{._x 0_ _x, .k dec k} _x_times_2_to_the_power_of_k
|
|
282
|
+
>
|
|
283
|
+
;
|
|
284
|
+
|
|
285
|
+
div =
|
|
286
|
+
${int x, int y}
|
|
287
|
+
// remember sign
|
|
288
|
+
{
|
|
289
|
+
<
|
|
290
|
+
{ .x/'+' if, .y get_sign then } .then,
|
|
291
|
+
{ .x/'-' if, .y swap_sign get_sign then } .then
|
|
292
|
+
> sign,
|
|
293
|
+
{.x abs x, .y abs step, 0 int res} _div res_wo_sign
|
|
294
|
+
}
|
|
295
|
+
<
|
|
296
|
+
{.sign/'+' if, {.res_wo_sign.div div, .res_wo_sign.rem rem} then} .then,
|
|
297
|
+
{ .res_wo_sign.div swap_sign div, .res_wo_sign.rem rem }
|
|
298
|
+
>
|
|
299
|
+
;
|
|
300
|
+
|
|
301
|
+
_div =
|
|
302
|
+
{
|
|
303
|
+
.x x,
|
|
304
|
+
.step step,
|
|
305
|
+
.res res,
|
|
306
|
+
{.x x, .step step} log2_floor log
|
|
307
|
+
}
|
|
308
|
+
<
|
|
309
|
+
{ .log int zero_int? if, {.res div, .x rem} then } .then,
|
|
310
|
+
{
|
|
311
|
+
{1 x, .log int dec abs k} x_times_2_to_k factor,
|
|
312
|
+
() i
|
|
313
|
+
}
|
|
314
|
+
{
|
|
315
|
+
{.i.x int x, {.i.step int x, .factor int y} times y} minus abs x,
|
|
316
|
+
.i.step step,
|
|
317
|
+
{.i.res x, .factor int y} plus res
|
|
318
|
+
} _div
|
|
319
|
+
>;
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
lcm =
|
|
323
|
+
${bits x, bits y}
|
|
324
|
+
{{.x int x, .y int y} times x, gcd int y} div .div abs
|
|
325
|
+
;
|
|
326
|
+
|
|
327
|
+
factorial = <
|
|
328
|
+
zero_int? 1 int,
|
|
329
|
+
/"+" int {dec factorial x, () y} times
|
|
330
|
+
>;
|
|
331
|
+
|
|
332
|
+
fib_ =
|
|
333
|
+
<
|
|
334
|
+
{ .n int dec /"+" n,
|
|
335
|
+
."fib(n),fib(n.1)" {plus x, .x y} "fib(n),fib(n.1)"
|
|
336
|
+
} fib_,
|
|
337
|
+
."fib(n),fib(n.1)"
|
|
338
|
+
>
|
|
339
|
+
;
|
|
340
|
+
|
|
341
|
+
fib = {$bits n, {1 int x, 0 int y} "fib(n),fib(n.1)"} fib_ .x;
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
--------------------
|
|
345
|
+
$ rat = { int n, int d };
|
|
346
|
+
|
|
347
|
+
rat = ${bits n, bits d} {.n int n, .d int d } normalize_rat;
|
|
348
|
+
|
|
349
|
+
normalize_rat =
|
|
350
|
+
$rat
|
|
351
|
+
<
|
|
352
|
+
{ .d/"-" if,
|
|
353
|
+
{
|
|
354
|
+
.n swap_sign n,
|
|
355
|
+
.d swap_sign d
|
|
356
|
+
} then
|
|
357
|
+
} .then,
|
|
358
|
+
()
|
|
359
|
+
>
|
|
360
|
+
{() x, {.n abs x,.d abs y} gcd int y}
|
|
361
|
+
{
|
|
362
|
+
{.x.n x,.y y} div .div n,
|
|
363
|
+
{.x.d x,.y y} div .div d
|
|
364
|
+
}
|
|
365
|
+
$rat
|
|
366
|
+
;
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
plus_rat =
|
|
370
|
+
${rat x, rat y}
|
|
371
|
+
{
|
|
372
|
+
.x x,
|
|
373
|
+
.y y,
|
|
374
|
+
{.x.d abs x,.y.d abs y} lcm int lcm
|
|
375
|
+
}
|
|
376
|
+
{
|
|
377
|
+
{
|
|
378
|
+
{.x.n x, {.lcm x, .x.d y} div .div y} times x,
|
|
379
|
+
{.y.n x, {.lcm x, .y.d y} div .div y} times y
|
|
380
|
+
} plus n,
|
|
381
|
+
.lcm d
|
|
382
|
+
}
|
|
383
|
+
normalize_rat
|
|
384
|
+
;
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
times_rat =
|
|
388
|
+
${rat x, rat y}
|
|
389
|
+
{
|
|
390
|
+
{.x.n x,.y.n y} times n,
|
|
391
|
+
{.x.d x,.y.d y} times d
|
|
392
|
+
}
|
|
393
|
+
normalize_rat;
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
minus_rat =
|
|
397
|
+
${rat x, rat y}
|
|
398
|
+
{
|
|
399
|
+
.x x,
|
|
400
|
+
.y {.n swap_sign n, .d d} y
|
|
401
|
+
}
|
|
402
|
+
plus_rat;
|
|
403
|
+
|
|
404
|
+
inverse_rat =
|
|
405
|
+
$rat
|
|
406
|
+
{ .d n, .n d }
|
|
407
|
+
normalize_rat
|
|
408
|
+
;
|
|
409
|
+
|
|
410
|
+
div_rat =
|
|
411
|
+
${rat x, rat y}
|
|
412
|
+
{ .x x, .y inverse_rat y} times_rat
|
|
413
|
+
;
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
--------------------
|
|
417
|
+
|
|
418
|
+
int_rat =
|
|
419
|
+
$rat
|
|
420
|
+
{ .n n, .d d, {.n x,.d y} div div }
|
|
421
|
+
{ .div.div int, {.div.rem int n, .d d} rat}
|
|
422
|
+
${int int, rat rat}
|
|
423
|
+
;
|
|
424
|
+
|
|
425
|
+
/*
|
|
426
|
+
ratToString =
|
|
427
|
+
-- $rat
|
|
428
|
+
[
|
|
429
|
+
.n toJSON,
|
|
430
|
+
"/",
|
|
431
|
+
.d toJSON,
|
|
432
|
+
" = ",
|
|
433
|
+
normalize_rat
|
|
434
|
+
<
|
|
435
|
+
[.d,.n] GT [.1 toJSON ,"/", .0 toJSON] CONCAT,
|
|
436
|
+
{.n 0,[.d,1] EQ 1} [.0 toJSON] CONCAT,
|
|
437
|
+
int_rat [.int toJSON,"+",.rat.n toJSON,"/",.rat.d toJSON] CONCAT
|
|
438
|
+
>
|
|
439
|
+
] CONCAT
|
|
440
|
+
-- $@string
|
|
441
|
+
;
|
|
442
|
+
|
|
443
|
+
_rat! = $rat {
|
|
444
|
+
() carryOn,
|
|
445
|
+
ratToString _log! drop
|
|
446
|
+
} .carryOn;
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
*/
|
|
451
|
+
|
|
452
|
+
times
|
package/README.md
CHANGED
|
@@ -28,12 +28,12 @@ k-repl # start the interactive REPL (or: node repl2.mjs)
|
|
|
28
28
|
| Binary | Source | Purpose |
|
|
29
29
|
|--------|--------|---------|
|
|
30
30
|
| `k` | `k.mjs` | Execute a `.k` script; reads binary pattern+value stream from stdin |
|
|
31
|
-
| `k-repl` / `k-repl2` | `repl2.mjs` | Interactive
|
|
32
|
-
| `k-parse` | `codecs/k-parse.mjs` | Encode
|
|
33
|
-
| `k-print` | `codecs/k-print.mjs` | Decode binary pattern+value stream
|
|
34
|
-
| `k-pattern` | `
|
|
35
|
-
| `k-compile-object` | `objects/compile.mjs` | Compile a `.k` source to
|
|
36
|
-
| `k-decompile-object` | `objects/decompile.mjs` | Decompile a `.
|
|
31
|
+
| `k-repl` / `k-repl2` | `repl2.mjs` | Interactive interpreter |
|
|
32
|
+
| `k-parse` | `codecs/k-parse.mjs` | Encode textual k values to the binary pattern+value wire format |
|
|
33
|
+
| `k-print` | `codecs/k-print.mjs` | Decode binary pattern+value stream to JSON-like textual k values |
|
|
34
|
+
| `k-pattern` | `patterns/from-k.mjs` | Extract the canonical root pattern from a k script |
|
|
35
|
+
| `k-compile-object` | `objects/compile.mjs` | Compile a `.k` source to an executable `.ko` object |
|
|
36
|
+
| `k-decompile-object` | `objects/decompile.mjs` | Decompile a `.ko` or `.klib` file back to k source |
|
|
37
37
|
|
|
38
38
|
---
|
|
39
39
|
|
|
@@ -48,6 +48,9 @@ Codes are algebraic data types built from two constructors:
|
|
|
48
48
|
|
|
49
49
|
Native k-like notation is canonical; JSON-like is supported as syntactic sugar.
|
|
50
50
|
|
|
51
|
+
For the JSON-like textual notation used by `k-parse` and `k-print`, see
|
|
52
|
+
[DOCS/TEXTUAL_VALUES.md](DOCS/TEXTUAL_VALUES.md).
|
|
53
|
+
|
|
51
54
|
### Defining codes (`$`)
|
|
52
55
|
|
|
53
56
|
```k
|
|
@@ -135,38 +138,60 @@ A partial function may be undefined on some inputs. Core primitives:
|
|
|
135
138
|
|
|
136
139
|
---
|
|
137
140
|
|
|
138
|
-
##
|
|
141
|
+
## Interpreter
|
|
139
142
|
|
|
140
143
|
Start with `k-repl` (or `node repl2.mjs`). The prompt is `> `.
|
|
141
144
|
|
|
142
|
-
|
|
145
|
+
`repl2.mjs` keeps a live `.klib`-style state in memory. You can:
|
|
143
146
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
147
|
+
- define types and relations incrementally
|
|
148
|
+
- run expressions against the current value
|
|
149
|
+
- load `.k` or `.klib` files into the session
|
|
150
|
+
- export the session as `.klib`
|
|
151
|
+
- export an executable `.ko` from an expression in the current context
|
|
149
152
|
|
|
150
|
-
###
|
|
153
|
+
### Main commands
|
|
151
154
|
|
|
152
155
|
| Command | Effect |
|
|
153
156
|
|---------|--------|
|
|
154
|
-
|
|
|
155
|
-
|
|
|
156
|
-
|
|
|
157
|
+
| `:type name = <...>` | Define a type alias |
|
|
158
|
+
| `:rel name = expr` | Define a relation alias |
|
|
159
|
+
| `:run expr` | Evaluate an expression on the current value |
|
|
160
|
+
| `:t name` | Show relation input/output filters |
|
|
161
|
+
| `:d name` | Show relation definition |
|
|
162
|
+
| `:C name` | Show canonical code definition |
|
|
163
|
+
| `:codes` / `:rels` | List type or relation aliases |
|
|
164
|
+
| `:load file` | Load `.k` or `.klib` |
|
|
165
|
+
| `:klib file` | Export current state as `.klib` |
|
|
166
|
+
| `:ko file expr` | Export executable `.ko` |
|
|
167
|
+
| `:val` | Print current value |
|
|
168
|
+
| `:reset` | Clear state |
|
|
169
|
+
| `:help` | Show command summary |
|
|
170
|
+
|
|
171
|
+
### Raw snippets
|
|
172
|
+
|
|
173
|
+
Raw k input is also accepted. It compiles on top of the current interpreter
|
|
174
|
+
state.
|
|
157
175
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
>
|
|
162
|
-
|
|
163
|
-
> --C pair
|
|
164
|
-
$ @wq9VvJgYt8sASQfevD23... = { @w8iSHe... x, @w8iSHe... y };
|
|
165
|
-
> toto = .toto;
|
|
166
|
-
> --R toto
|
|
167
|
-
toto : ?{ X0 toto, ... } --> ?X0
|
|
176
|
+
```text
|
|
177
|
+
> $ bool = <{} true, {} false>;
|
|
178
|
+
> not = $bool </true | false, {} | true>;
|
|
179
|
+
> {} |true not
|
|
180
|
+
{false: {}} ?<{} true, {} false>
|
|
168
181
|
```
|
|
169
182
|
|
|
183
|
+
Definitions-only snippets extend the session silently. Snippets with a terminal
|
|
184
|
+
expression are evaluated immediately.
|
|
185
|
+
|
|
186
|
+
Tab completion covers:
|
|
187
|
+
|
|
188
|
+
- command names
|
|
189
|
+
- file paths for `:load`, `:klib`, and `:ko`
|
|
190
|
+
- aliases
|
|
191
|
+
- canonical names beginning with `@`
|
|
192
|
+
|
|
193
|
+
See [DOCS/REPL.md](DOCS/REPL.md) for the exact buffering and evaluation rules.
|
|
194
|
+
|
|
170
195
|
---
|
|
171
196
|
|
|
172
197
|
## Code Derivation and Patterns
|
|
@@ -269,6 +294,25 @@ echo '{"cons":{"car":{"1":{}},"cdr":{"nil":{}}}}' \
|
|
|
269
294
|
|
|
270
295
|
See [`codecs/README.md`](codecs/README.md) for codec internals and format details.
|
|
271
296
|
|
|
297
|
+
For the textual boundary notation used by `k-parse` and `k-print`, see
|
|
298
|
+
[DOCS/TEXTUAL_VALUES.md](DOCS/TEXTUAL_VALUES.md). For canonical exported
|
|
299
|
+
patterns and `k-pattern`, see [DOCS/PATTERNS.md](DOCS/PATTERNS.md).
|
|
300
|
+
|
|
301
|
+
## Object and Library Files
|
|
302
|
+
|
|
303
|
+
The object toolchain is documented in [objects/README.md](objects/README.md).
|
|
304
|
+
|
|
305
|
+
- `.klib` stores compiled library state: codes, relations, aliases, metadata
|
|
306
|
+
- `.ko` stores an executable object with a main expression
|
|
307
|
+
|
|
308
|
+
Typical CLI usage:
|
|
309
|
+
|
|
310
|
+
```bash
|
|
311
|
+
./objects/compile-lib.mjs Examples/ieee.k ieee.klib
|
|
312
|
+
./objects/compile.mjs --lib ieee.klib Examples/nat.k nat.ko
|
|
313
|
+
./objects/decompile.mjs nat.ko
|
|
314
|
+
```
|
|
315
|
+
|
|
272
316
|
---
|
|
273
317
|
|
|
274
318
|
## Examples
|