@fraczak/k 8.1.0 → 8.3.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 +63 -0
- package/DOCS/CANONICAL_PATTERNS.md +252 -0
- package/DOCS/NAMED_PATTERNS.md +23 -0
- package/DOCS/PATTERNS.md +5 -0
- package/DOCS/REPL.md +73 -2
- package/Examples/arithmetics.k +31 -14
- package/Examples/bnat.k +13 -6
- package/Examples/buda.k +9 -4
- package/Examples/byte.k +23 -9
- package/Examples/ieee.k +102 -34
- package/Examples/karatsuba-mult.k +23 -9
- package/Examples/nat.k +3 -2
- package/Examples/poly.k +30 -16
- package/Examples/vhdl_to_k.k +7 -2
- package/README.md +33 -5
- package/backend-api.mjs +6 -1
- package/backends/llvm/src/cli.mjs +3 -33
- package/backends/wasm/src/cli.mjs +3 -33
- package/backends/wasm/src/wasm.mjs +1 -29
- package/browser/vfs-data.mjs +8 -8
- package/canonical-pattern.mjs +350 -0
- package/k.mjs +12 -29
- package/kvm.mjs +2 -28
- package/named-patterns.mjs +35 -2
- package/object.mjs +137 -20
- package/objects/compile.mjs +2 -29
- package/objects/validate.mjs +13 -1
- package/package.json +1 -1
- package/parser.jison +72 -10
- package/parser.mjs +114 -49
- package/pretty.mjs +125 -2
- package/repl.mjs +334 -82
- package/scripts/build-repl-html.mjs +1 -1
- package/tests/test-k-object.mjs +5 -1
- package/tests/test-named-patterns.mjs +363 -0
- package/tests/test-repl.mjs +193 -16
|
@@ -186,4 +186,367 @@ console.log("--> Test 7: Error handling (unknown, arity, cycles, dupes)");
|
|
|
186
186
|
console.log(" Passed: Error conditions correctly handled.");
|
|
187
187
|
}
|
|
188
188
|
|
|
189
|
+
// Test 8: Canonical pattern references and aliasing
|
|
190
|
+
console.log("--> Test 8: Canonical pattern references and aliasing");
|
|
191
|
+
{
|
|
192
|
+
const { compileLibrary } = await import("../object.mjs");
|
|
193
|
+
const baseLib = compileLibrary(`
|
|
194
|
+
bit[] = < {}0, {}1 >;
|
|
195
|
+
list[X] = < {}nil, { X car, Y cdr } cons > = Y;
|
|
196
|
+
`, { exports: ["bit", "list"] });
|
|
197
|
+
|
|
198
|
+
const script1 = `
|
|
199
|
+
-- Canonical hash application with and without arguments
|
|
200
|
+
is_bit = ? @MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg[];
|
|
201
|
+
is_bit2 = ? @MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg;
|
|
202
|
+
is_bit_list = ? @JagVexzgseRr4rVZRmVpsMbr2AKXDjzZnz6Qnf8a9SNe[
|
|
203
|
+
@MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg[]
|
|
204
|
+
];
|
|
205
|
+
|
|
206
|
+
b0 = {}|0 is_bit;
|
|
207
|
+
b1 = {}|1 is_bit2;
|
|
208
|
+
l = { b1 car, {}|nil cdr }|cons is_bit_list;
|
|
209
|
+
{ b0 b0, b1 b1, l l }
|
|
210
|
+
`;
|
|
211
|
+
const fn1 = k.compile(script1, { libraries: [baseLib] });
|
|
212
|
+
const out1 = fn1(Value.product({}));
|
|
213
|
+
assert.equal(out1.product.b0.tag, "0");
|
|
214
|
+
assert.equal(out1.product.b1.tag, "1");
|
|
215
|
+
assert.equal(out1.product.l.tag, "cons");
|
|
216
|
+
|
|
217
|
+
// Aliasing canonical patterns
|
|
218
|
+
const script2 = `
|
|
219
|
+
my_bit[] = @MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg[];
|
|
220
|
+
my_bit2[] = @MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg;
|
|
221
|
+
my_list[T] = @JagVexzgseRr4rVZRmVpsMbr2AKXDjzZnz6Qnf8a9SNe[T];
|
|
222
|
+
|
|
223
|
+
is_my_list = ? my_list[my_bit[]];
|
|
224
|
+
val = { {}|0 car, {}|nil cdr }|cons is_my_list;
|
|
225
|
+
val
|
|
226
|
+
`;
|
|
227
|
+
const fn2 = k.compile(script2, { libraries: [baseLib] });
|
|
228
|
+
const out2 = fn2(Value.product({}));
|
|
229
|
+
assert.equal(out2.tag, "cons");
|
|
230
|
+
console.log(" Passed: Canonical pattern references and aliasing.");
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Test 9: Library export, validation, and multi-library consumption
|
|
234
|
+
console.log("--> Test 9: Library export and canonical pattern metadata");
|
|
235
|
+
{
|
|
236
|
+
const { compileLibrary, extractAliasesFromObject } =
|
|
237
|
+
await import("../object.mjs");
|
|
238
|
+
const { validateObjectPayload } =
|
|
239
|
+
await import("../objects/validate.mjs");
|
|
240
|
+
const { buildExportPreamble } =
|
|
241
|
+
await import("../canonical-pattern.mjs");
|
|
242
|
+
|
|
243
|
+
const libSource = `
|
|
244
|
+
bit[] = < {}0, {}1 >;
|
|
245
|
+
list[X] = < {}nil, { X car, Y cdr } cons > = Y;
|
|
246
|
+
pair[A, B] = { A first, B second };
|
|
247
|
+
`;
|
|
248
|
+
const lib = compileLibrary(libSource, {
|
|
249
|
+
exports: ["bit", "list", "pair"]
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
validateObjectPayload(lib);
|
|
253
|
+
|
|
254
|
+
assert.equal(
|
|
255
|
+
lib.patternAlias.bit,
|
|
256
|
+
"@MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg"
|
|
257
|
+
);
|
|
258
|
+
assert.equal(
|
|
259
|
+
lib.patternAlias.list,
|
|
260
|
+
"@JagVexzgseRr4rVZRmVpsMbr2AKXDjzZnz6Qnf8a9SNe"
|
|
261
|
+
);
|
|
262
|
+
assert.equal(
|
|
263
|
+
lib.patternAlias.pair,
|
|
264
|
+
"@KqTBNkznjWWwync2Mm94XjCez63neJBJtgYcJJEE5kEm"
|
|
265
|
+
);
|
|
266
|
+
|
|
267
|
+
const bitHash = "@MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg";
|
|
268
|
+
const listHash = "@JagVexzgseRr4rVZRmVpsMbr2AKXDjzZnz6Qnf8a9SNe";
|
|
269
|
+
assert.equal(lib.meta[bitHash].type, "pattern");
|
|
270
|
+
assert.equal(lib.meta[listHash].type, "pattern");
|
|
271
|
+
|
|
272
|
+
const aliases = extractAliasesFromObject(lib);
|
|
273
|
+
assert.match(aliases, /bit\[\] = @Mjusy/);
|
|
274
|
+
assert.match(aliases, /list\[X\] = @JagVex/);
|
|
275
|
+
assert.match(aliases, /pair\[A, B\] = @KqTB/);
|
|
276
|
+
|
|
277
|
+
const preamble = buildExportPreamble(
|
|
278
|
+
["bit", "list:my_list"],
|
|
279
|
+
[lib]
|
|
280
|
+
);
|
|
281
|
+
assert.match(preamble, /bit\[\] = @Mjusy/);
|
|
282
|
+
assert.match(preamble, /my_list\[X\] = @JagVex/);
|
|
283
|
+
|
|
284
|
+
const consumerSource = `
|
|
285
|
+
is_bit = ? bit[];
|
|
286
|
+
is_pair = ? @KqTBNkznjWWwync2Mm94XjCez63neJBJtgYcJJEE5kEm[
|
|
287
|
+
bit[], bit[]
|
|
288
|
+
];
|
|
289
|
+
is_pair_list = ? list[@KqTBNkznjWWwync2Mm94XjCez63neJBJtgYcJJEE5kEm[
|
|
290
|
+
bit[], bit[]
|
|
291
|
+
]];
|
|
292
|
+
|
|
293
|
+
b = {}|0 is_bit;
|
|
294
|
+
p = { b first, {}|1 second } is_pair;
|
|
295
|
+
l = { p car, {}|nil cdr }|cons is_pair_list;
|
|
296
|
+
l
|
|
297
|
+
`;
|
|
298
|
+
const fnConsumer = k.compile(consumerSource, {
|
|
299
|
+
libraries: [lib]
|
|
300
|
+
});
|
|
301
|
+
const outConsumer = fnConsumer(Value.product({}));
|
|
302
|
+
assert.equal(outConsumer.tag, "cons");
|
|
303
|
+
console.log(" Passed: Library export and pattern metadata.");
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// Test 10: Canonical pattern sharing & parameter aliasing
|
|
307
|
+
console.log(
|
|
308
|
+
"--> Test 10: Canonical pattern sharing (a[], b[X], c[X,Y], d[Y,X])"
|
|
309
|
+
);
|
|
310
|
+
{
|
|
311
|
+
const { resolvePatternCanonicalInfo } =
|
|
312
|
+
await import("../canonical-pattern.mjs");
|
|
313
|
+
const { parse } = await import("../parser.mjs");
|
|
314
|
+
|
|
315
|
+
const ast = parse(`
|
|
316
|
+
a[] = {X x, Y y};
|
|
317
|
+
b[X] = {X x, Y y};
|
|
318
|
+
c[X,Y] = {X x, Y y};
|
|
319
|
+
d[Y,X] = {X x, Y y};
|
|
320
|
+
()
|
|
321
|
+
`);
|
|
322
|
+
const aInfo = resolvePatternCanonicalInfo(
|
|
323
|
+
"a",
|
|
324
|
+
ast.defs.patterns.a,
|
|
325
|
+
{ patterns: ast.defs.patterns }
|
|
326
|
+
);
|
|
327
|
+
const bInfo = resolvePatternCanonicalInfo(
|
|
328
|
+
"b",
|
|
329
|
+
ast.defs.patterns.b,
|
|
330
|
+
{ patterns: ast.defs.patterns }
|
|
331
|
+
);
|
|
332
|
+
const cInfo = resolvePatternCanonicalInfo(
|
|
333
|
+
"c",
|
|
334
|
+
ast.defs.patterns.c,
|
|
335
|
+
{ patterns: ast.defs.patterns }
|
|
336
|
+
);
|
|
337
|
+
const dInfo = resolvePatternCanonicalInfo(
|
|
338
|
+
"d",
|
|
339
|
+
ast.defs.patterns.d,
|
|
340
|
+
{ patterns: ast.defs.patterns }
|
|
341
|
+
);
|
|
342
|
+
|
|
343
|
+
const sharedHash = "@NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x";
|
|
344
|
+
assert.equal(aInfo.hash, sharedHash);
|
|
345
|
+
assert.equal(bInfo.hash, sharedHash);
|
|
346
|
+
assert.equal(cInfo.hash, sharedHash);
|
|
347
|
+
assert.equal(dInfo.hash, sharedHash);
|
|
348
|
+
|
|
349
|
+
assert.equal(aInfo.def, "{X0 x, X1 y}");
|
|
350
|
+
assert.equal(bInfo.def, "{X0 x, X1 y}");
|
|
351
|
+
assert.equal(cInfo.def, "{X0 x, X1 y}");
|
|
352
|
+
assert.equal(dInfo.def, "{X0 x, X1 y}");
|
|
353
|
+
|
|
354
|
+
assert.deepEqual(aInfo.aliasArgs, ["(...)", "(...)"]);
|
|
355
|
+
assert.deepEqual(bInfo.aliasArgs, ["X", "(...)"]);
|
|
356
|
+
assert.deepEqual(cInfo.aliasArgs, ["X", "Y"]);
|
|
357
|
+
assert.deepEqual(dInfo.aliasArgs, ["X", "Y"]);
|
|
358
|
+
|
|
359
|
+
assert.deepEqual(aInfo.params, []);
|
|
360
|
+
assert.deepEqual(bInfo.params, ["X0"]);
|
|
361
|
+
assert.deepEqual(cInfo.params, ["X0", "X1"]);
|
|
362
|
+
assert.deepEqual(dInfo.params, ["X1", "X0"]);
|
|
363
|
+
console.log(
|
|
364
|
+
" Passed: Canonical pattern sharing & parameter aliasing."
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// Test 11: Knot parameter conflict rejection and canonical patterns
|
|
369
|
+
console.log(
|
|
370
|
+
"--> Test 11: Knot conflict rejection & direct canonical patterns"
|
|
371
|
+
);
|
|
372
|
+
{
|
|
373
|
+
assert.throws(
|
|
374
|
+
() =>
|
|
375
|
+
k.compile("list[X,Y] = <{}nil,{X car, Y cdr} cons> = Y; ()"),
|
|
376
|
+
/conflicts with knot 'Y'/
|
|
377
|
+
);
|
|
378
|
+
|
|
379
|
+
const directDefSource = `
|
|
380
|
+
@A6vrY7TP9s34tvNgVZsk7N5m4Cr4ZJERYPBevgmqnK7P[X, Y] = {
|
|
381
|
+
X x, Y y, Y z
|
|
382
|
+
};
|
|
383
|
+
is_p = ? @A6vrY7TP9s34tvNgVZsk7N5m4Cr4ZJERYPBevgmqnK7P[
|
|
384
|
+
{}, {}
|
|
385
|
+
];
|
|
386
|
+
{ {} x, {} y, {} z } is_p
|
|
387
|
+
`;
|
|
388
|
+
const fnDirect = k.compile(directDefSource);
|
|
389
|
+
const outDirect = fnDirect(Value.product({}));
|
|
390
|
+
assert.equal(outDirect != null, true);
|
|
391
|
+
console.log(
|
|
392
|
+
" Passed: Knot conflict rejection and canonical patterns."
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
// Test 12: Unrestricted pattern (...) unification with variable ports
|
|
397
|
+
console.log(
|
|
398
|
+
"--> Test 12: Wildcard (...) unification with canonical variables"
|
|
399
|
+
);
|
|
400
|
+
{
|
|
401
|
+
const { resolvePatternCanonicalInfo } =
|
|
402
|
+
await import("../canonical-pattern.mjs");
|
|
403
|
+
const { parse } = await import("../parser.mjs");
|
|
404
|
+
|
|
405
|
+
const ast = parse(`
|
|
406
|
+
c[X,Y] = {X x, Y y};
|
|
407
|
+
p[X] = {X x, (...) y};
|
|
408
|
+
q[X,Y,Z] = {X x, Y y, Z z};
|
|
409
|
+
t[] = {(...) x, (...) y, (...) z};
|
|
410
|
+
()
|
|
411
|
+
`);
|
|
412
|
+
const cInfo = resolvePatternCanonicalInfo(
|
|
413
|
+
"c",
|
|
414
|
+
ast.defs.patterns.c,
|
|
415
|
+
{ patterns: ast.defs.patterns }
|
|
416
|
+
);
|
|
417
|
+
const pInfo = resolvePatternCanonicalInfo(
|
|
418
|
+
"p",
|
|
419
|
+
ast.defs.patterns.p,
|
|
420
|
+
{ patterns: ast.defs.patterns }
|
|
421
|
+
);
|
|
422
|
+
const qInfo = resolvePatternCanonicalInfo(
|
|
423
|
+
"q",
|
|
424
|
+
ast.defs.patterns.q,
|
|
425
|
+
{ patterns: ast.defs.patterns }
|
|
426
|
+
);
|
|
427
|
+
const tInfo = resolvePatternCanonicalInfo(
|
|
428
|
+
"t",
|
|
429
|
+
ast.defs.patterns.t,
|
|
430
|
+
{ patterns: ast.defs.patterns }
|
|
431
|
+
);
|
|
432
|
+
|
|
433
|
+
assert.equal(cInfo.hash, pInfo.hash);
|
|
434
|
+
assert.equal(qInfo.hash, tInfo.hash);
|
|
435
|
+
|
|
436
|
+
assert.equal(pInfo.def, "{X0 x, X1 y}");
|
|
437
|
+
assert.equal(tInfo.def, "{X0 x, X1 y, X2 z}");
|
|
438
|
+
|
|
439
|
+
assert.deepEqual(pInfo.aliasArgs, ["X", "(...)"]);
|
|
440
|
+
assert.deepEqual(tInfo.aliasArgs, ["(...)", "(...)", "(...)"]);
|
|
441
|
+
console.log(
|
|
442
|
+
" Passed: Wildcard (...) unification with canonical variables."
|
|
443
|
+
);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
// Test 13: Nested template composition & parameter routing
|
|
447
|
+
console.log(
|
|
448
|
+
"--> Test 13: Nested composition & parameter routing"
|
|
449
|
+
);
|
|
450
|
+
{
|
|
451
|
+
const { resolvePatternCanonicalInfo } =
|
|
452
|
+
await import("../canonical-pattern.mjs");
|
|
453
|
+
const { parse } = await import("../parser.mjs");
|
|
454
|
+
|
|
455
|
+
const ast = parse(`
|
|
456
|
+
pair[A, B] = { A a, B b };
|
|
457
|
+
flip[Y, X] = pair[X, Y];
|
|
458
|
+
outer[Z] = pair[pair[Z, (...)], (...)];
|
|
459
|
+
deep[] = pair[pair[(...), (...)], (...)];
|
|
460
|
+
()
|
|
461
|
+
`);
|
|
462
|
+
const flipInfo = resolvePatternCanonicalInfo(
|
|
463
|
+
"flip",
|
|
464
|
+
ast.defs.patterns.flip,
|
|
465
|
+
{ patterns: ast.defs.patterns }
|
|
466
|
+
);
|
|
467
|
+
const pairInfo = resolvePatternCanonicalInfo(
|
|
468
|
+
"pair",
|
|
469
|
+
ast.defs.patterns.pair,
|
|
470
|
+
{ patterns: ast.defs.patterns }
|
|
471
|
+
);
|
|
472
|
+
const outerInfo = resolvePatternCanonicalInfo(
|
|
473
|
+
"outer",
|
|
474
|
+
ast.defs.patterns.outer,
|
|
475
|
+
{ patterns: ast.defs.patterns }
|
|
476
|
+
);
|
|
477
|
+
const deepInfo = resolvePatternCanonicalInfo(
|
|
478
|
+
"deep",
|
|
479
|
+
ast.defs.patterns.deep,
|
|
480
|
+
{ patterns: ast.defs.patterns }
|
|
481
|
+
);
|
|
482
|
+
|
|
483
|
+
assert.equal(flipInfo.hash, pairInfo.hash);
|
|
484
|
+
assert.deepEqual(flipInfo.params, ["X1", "X0"]);
|
|
485
|
+
assert.deepEqual(flipInfo.aliasArgs, ["X", "Y"]);
|
|
486
|
+
|
|
487
|
+
assert.equal(outerInfo.hash, deepInfo.hash);
|
|
488
|
+
assert.equal(outerInfo.def, "{{X0 a, X1 b} a, X2 b}");
|
|
489
|
+
assert.deepEqual(outerInfo.params, ["X0"]);
|
|
490
|
+
assert.deepEqual(outerInfo.aliasArgs, ["Z", "(...)", "(...)"]);
|
|
491
|
+
assert.deepEqual(deepInfo.params, []);
|
|
492
|
+
assert.deepEqual(deepInfo.aliasArgs, ["(...)", "(...)", "(...)"]);
|
|
493
|
+
console.log(
|
|
494
|
+
" Passed: Nested composition and parameter routing."
|
|
495
|
+
);
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
// Test 14: Repeated variable unification with wildcards
|
|
499
|
+
console.log(
|
|
500
|
+
"--> Test 14: Repeated existential variable wildcard constraints"
|
|
501
|
+
);
|
|
502
|
+
{
|
|
503
|
+
const { resolvePatternCanonicalInfo } =
|
|
504
|
+
await import("../canonical-pattern.mjs");
|
|
505
|
+
const { parse } = await import("../parser.mjs");
|
|
506
|
+
|
|
507
|
+
const ast = parse(`
|
|
508
|
+
pair_equal[Z] = { Z a, Y b, Y c };
|
|
509
|
+
full_equal[A, B] = { A a, B b, B c };
|
|
510
|
+
()
|
|
511
|
+
`);
|
|
512
|
+
const peInfo = resolvePatternCanonicalInfo(
|
|
513
|
+
"pair_equal",
|
|
514
|
+
ast.defs.patterns.pair_equal,
|
|
515
|
+
{ patterns: ast.defs.patterns }
|
|
516
|
+
);
|
|
517
|
+
const feInfo = resolvePatternCanonicalInfo(
|
|
518
|
+
"full_equal",
|
|
519
|
+
ast.defs.patterns.full_equal,
|
|
520
|
+
{ patterns: ast.defs.patterns }
|
|
521
|
+
);
|
|
522
|
+
|
|
523
|
+
assert.equal(peInfo.hash, feInfo.hash);
|
|
524
|
+
assert.equal(peInfo.def, "{X0 a, X1 b, X1 c}");
|
|
525
|
+
assert.deepEqual(peInfo.params, ["X0"]);
|
|
526
|
+
assert.deepEqual(peInfo.aliasArgs, ["Z", "(...)"]);
|
|
527
|
+
assert.deepEqual(feInfo.params, ["X0", "X1"]);
|
|
528
|
+
assert.deepEqual(feInfo.aliasArgs, ["A", "B"]);
|
|
529
|
+
|
|
530
|
+
// Verify that pair_equal enforces equality between b and c
|
|
531
|
+
const fnOk = k.compile(`
|
|
532
|
+
pair_equal[Z] = { Z a, Y b, Y c };
|
|
533
|
+
check = ? pair_equal[{}];
|
|
534
|
+
{ {} a, {} b, {} c } check
|
|
535
|
+
`);
|
|
536
|
+
assert.doesNotThrow(() => fnOk());
|
|
537
|
+
|
|
538
|
+
assert.throws(
|
|
539
|
+
() =>
|
|
540
|
+
k.compile(`
|
|
541
|
+
pair_equal[Z] = { Z a, Y b, Y c };
|
|
542
|
+
check = ? pair_equal[{}];
|
|
543
|
+
{ {} a, {} b, {}|0 c } check
|
|
544
|
+
`),
|
|
545
|
+
/Cannot unify/
|
|
546
|
+
);
|
|
547
|
+
console.log(
|
|
548
|
+
" Passed: Repeated existential variable wildcard constraints."
|
|
549
|
+
);
|
|
550
|
+
}
|
|
551
|
+
|
|
189
552
|
console.log("==> All Named Patterns Tests Passed Successfully!");
|
package/tests/test-repl.mjs
CHANGED
|
@@ -620,26 +620,32 @@ output = await evaluateInput("bit[] = < {} 0, {} 1 >;", patState);
|
|
|
620
620
|
assert.match(output[0], /\/\* comp:/);
|
|
621
621
|
|
|
622
622
|
output = await evaluateInput(":patterns", patState);
|
|
623
|
-
assert.equal(
|
|
623
|
+
assert.equal(
|
|
624
|
+
output[0],
|
|
625
|
+
"bit[] = @MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg[]"
|
|
626
|
+
);
|
|
624
627
|
output = await evaluateInput(":pats", patState);
|
|
625
|
-
assert.equal(
|
|
628
|
+
assert.equal(
|
|
629
|
+
output[0],
|
|
630
|
+
"bit[] = @MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg[]"
|
|
631
|
+
);
|
|
626
632
|
|
|
627
633
|
output = await evaluateInput(":pattern bit", patState);
|
|
628
634
|
assert.equal(
|
|
629
635
|
output[0],
|
|
630
|
-
"bit[] = <{} 0,
|
|
636
|
+
"bit[] = <{}=X0 0, X0 1>; -- " +
|
|
631
637
|
"@MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg"
|
|
632
638
|
);
|
|
633
639
|
output = await evaluateInput(":pattern bit[]", patState);
|
|
634
640
|
assert.equal(
|
|
635
641
|
output[0],
|
|
636
|
-
"bit[] = <{} 0,
|
|
642
|
+
"bit[] = <{}=X0 0, X0 1>; -- " +
|
|
637
643
|
"@MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg"
|
|
638
644
|
);
|
|
639
645
|
output = await evaluateInput(":pat bit", patState);
|
|
640
646
|
assert.equal(
|
|
641
647
|
output[0],
|
|
642
|
-
"bit[] = <{} 0,
|
|
648
|
+
"bit[] = <{}=X0 0, X0 1>; -- " +
|
|
643
649
|
"@MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg"
|
|
644
650
|
);
|
|
645
651
|
output = await evaluateInput(
|
|
@@ -648,8 +654,22 @@ output = await evaluateInput(
|
|
|
648
654
|
);
|
|
649
655
|
assert.equal(
|
|
650
656
|
output[0],
|
|
651
|
-
"
|
|
652
|
-
|
|
657
|
+
"@MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg[] = <{}=X0 0, X0 1>;"
|
|
658
|
+
);
|
|
659
|
+
|
|
660
|
+
output = await evaluateInput(
|
|
661
|
+
"byte[] = { " +
|
|
662
|
+
"bit[] b0, bit[] b1, bit[] b2, bit[] b3, " +
|
|
663
|
+
"bit[] b4, bit[] b5, bit[] b6, bit[] b7 " +
|
|
664
|
+
"};",
|
|
665
|
+
patState
|
|
666
|
+
);
|
|
667
|
+
output = await evaluateInput(":pattern byte", patState);
|
|
668
|
+
assert.equal(
|
|
669
|
+
output[0],
|
|
670
|
+
"byte[] = {<{}=X1 0, X1 1>=X0 b0, X0 b1, X0 b2, X0 b3, " +
|
|
671
|
+
"X0 b4, X0 b5, X0 b6, X0 b7}; -- " +
|
|
672
|
+
"@4ukq2gpiEBFa7WXxcfRm8pfdr2kgYLTTnwsmLHeMd9i5"
|
|
653
673
|
);
|
|
654
674
|
|
|
655
675
|
await assert.rejects(
|
|
@@ -688,21 +708,21 @@ output = await evaluateInput(
|
|
|
688
708
|
output = await evaluateInput(":pattern pair", patState);
|
|
689
709
|
assert.equal(
|
|
690
710
|
output[0],
|
|
691
|
-
"pair[
|
|
711
|
+
"pair[X0, X1] = {X0 first, X1 second}; -- " +
|
|
692
712
|
"@KqTBNkznjWWwync2Mm94XjCez63neJBJtgYcJJEE5kEm"
|
|
693
713
|
);
|
|
694
714
|
|
|
695
715
|
output = await evaluateInput(":load Examples/list.k", patState);
|
|
696
716
|
assert.equal(output[0], "loaded Examples/list.k");
|
|
697
717
|
output = await evaluateInput(":patterns", patState);
|
|
698
|
-
assert(output[0].includes("bit[]"));
|
|
699
|
-
assert(output[0].includes("list[X]"));
|
|
700
|
-
assert(output[0].includes("pair[A,
|
|
718
|
+
assert(output[0].includes("bit[] = @Mjusy"));
|
|
719
|
+
assert(output[0].includes("list[X] = @JagVex"));
|
|
720
|
+
assert(output[0].includes("pair[A,B] = @KqTB"));
|
|
701
721
|
|
|
702
722
|
output = await evaluateInput(":pattern list", patState);
|
|
703
723
|
assert.equal(
|
|
704
724
|
output[0],
|
|
705
|
-
"list[
|
|
725
|
+
"list[X1] = <{X1 car, X0 cdr} cons, {} nil>=X0; -- " +
|
|
706
726
|
"@JagVexzgseRr4rVZRmVpsMbr2AKXDjzZnz6Qnf8a9SNe"
|
|
707
727
|
);
|
|
708
728
|
|
|
@@ -717,19 +737,176 @@ const freshState = createState({ engine: "wasm" });
|
|
|
717
737
|
output = await evaluateInput(`:load ${testKlibPath}`, freshState);
|
|
718
738
|
assert.equal(output[0], `loaded ${testKlibPath}`);
|
|
719
739
|
output = await evaluateInput(":patterns", freshState);
|
|
720
|
-
assert(output[0].includes("bit[]"));
|
|
721
|
-
assert(output[0].includes("pair[A,
|
|
722
|
-
assert(output[0].includes("list[X]"));
|
|
740
|
+
assert(output[0].includes("bit[] = @Mjusy"));
|
|
741
|
+
assert(output[0].includes("pair[A,B] = @KqTB"));
|
|
742
|
+
assert(output[0].includes("list[X] = @JagVex"));
|
|
723
743
|
|
|
724
744
|
output = await evaluateInput(":pattern bit", freshState);
|
|
725
745
|
assert.equal(
|
|
726
746
|
output[0],
|
|
727
|
-
"bit[] = <{} 0,
|
|
747
|
+
"bit[] = <{}=X0 0, X0 1>; -- " +
|
|
728
748
|
"@MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg"
|
|
729
749
|
);
|
|
750
|
+
output = await evaluateInput(":pattern pair", freshState);
|
|
751
|
+
assert.equal(
|
|
752
|
+
output[0],
|
|
753
|
+
"pair[X0, X1] = {X0 first, X1 second}; -- " +
|
|
754
|
+
"@KqTBNkznjWWwync2Mm94XjCez63neJBJtgYcJJEE5kEm"
|
|
755
|
+
);
|
|
756
|
+
output = await evaluateInput(":pattern list", freshState);
|
|
757
|
+
assert.equal(
|
|
758
|
+
output[0],
|
|
759
|
+
"list[X1] = <{X1 car, X0 cdr} cons, {} nil>=X0; -- " +
|
|
760
|
+
"@JagVexzgseRr4rVZRmVpsMbr2AKXDjzZnz6Qnf8a9SNe"
|
|
761
|
+
);
|
|
762
|
+
output = await evaluateInput(
|
|
763
|
+
"same_pair[T] = { T first, T second };",
|
|
764
|
+
freshState
|
|
765
|
+
);
|
|
766
|
+
output = await evaluateInput(":pattern same_pair", freshState);
|
|
767
|
+
assert.equal(
|
|
768
|
+
output[0],
|
|
769
|
+
"same_pair[X0] = {X0 first, X0 second}; -- " +
|
|
770
|
+
"@TyjJDJuYAbp56mytRMzu2HMTfQq7rKLcGsaQtFG28yhW"
|
|
771
|
+
);
|
|
730
772
|
output = await evaluateInput("{} | nil ? list[bit[]]", freshState);
|
|
731
773
|
assert.match(output[0], /\{\}\|nil/);
|
|
732
774
|
|
|
775
|
+
output = await evaluateInput("a[] = x;", freshState);
|
|
776
|
+
output = await evaluateInput(":pattern a", freshState);
|
|
777
|
+
assert.equal(
|
|
778
|
+
output[0],
|
|
779
|
+
"a[] = X0; -- " +
|
|
780
|
+
"@QfyLpmn56wuppuGvrrUJz8LKtgaXfbMtad7RnzBcFk2S"
|
|
781
|
+
);
|
|
782
|
+
output = await evaluateInput(
|
|
783
|
+
"closed_same_pair[] = { T first, T second };",
|
|
784
|
+
freshState
|
|
785
|
+
);
|
|
786
|
+
output = await evaluateInput(":pattern closed_same_pair", freshState);
|
|
787
|
+
assert.equal(
|
|
788
|
+
output[0],
|
|
789
|
+
"closed_same_pair[] = {X0 first, X0 second}; -- " +
|
|
790
|
+
"@TyjJDJuYAbp56mytRMzu2HMTfQq7rKLcGsaQtFG28yhW"
|
|
791
|
+
);
|
|
792
|
+
|
|
793
|
+
output = await evaluateInput(
|
|
794
|
+
"a[]= {X x, Y y};b[X] = {X x, Y y};" +
|
|
795
|
+
"c[X,Y] = {X x, Y y};d[Y,X] = {X x, Y y};",
|
|
796
|
+
freshState
|
|
797
|
+
);
|
|
798
|
+
output = await evaluateInput(":pat a", freshState);
|
|
799
|
+
assert.equal(
|
|
800
|
+
output[0],
|
|
801
|
+
"a[] = {X0 x, X1 y}; -- " +
|
|
802
|
+
"@NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x"
|
|
803
|
+
);
|
|
804
|
+
output = await evaluateInput(":pat b", freshState);
|
|
805
|
+
assert.equal(
|
|
806
|
+
output[0],
|
|
807
|
+
"b[X0] = {X0 x, X1 y}; -- " +
|
|
808
|
+
"@NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x"
|
|
809
|
+
);
|
|
810
|
+
output = await evaluateInput(":pat c", freshState);
|
|
811
|
+
assert.equal(
|
|
812
|
+
output[0],
|
|
813
|
+
"c[X0, X1] = {X0 x, X1 y}; -- " +
|
|
814
|
+
"@NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x"
|
|
815
|
+
);
|
|
816
|
+
output = await evaluateInput(":pat d", freshState);
|
|
817
|
+
assert.equal(
|
|
818
|
+
output[0],
|
|
819
|
+
"d[X1, X0] = {X0 x, X1 y}; -- " +
|
|
820
|
+
"@NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x"
|
|
821
|
+
);
|
|
822
|
+
output = await evaluateInput(":pats", freshState);
|
|
823
|
+
assert(
|
|
824
|
+
output[0].includes(
|
|
825
|
+
"a[] = @NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[(...),(...)]"
|
|
826
|
+
)
|
|
827
|
+
);
|
|
828
|
+
assert(
|
|
829
|
+
output[0].includes(
|
|
830
|
+
"b[X] = @NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[X,(...)]"
|
|
831
|
+
)
|
|
832
|
+
);
|
|
833
|
+
assert(
|
|
834
|
+
output[0].includes(
|
|
835
|
+
"c[X,Y] = @NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[X,Y]"
|
|
836
|
+
)
|
|
837
|
+
);
|
|
838
|
+
assert(
|
|
839
|
+
output[0].includes(
|
|
840
|
+
"d[Y,X] = @NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[X,Y]"
|
|
841
|
+
)
|
|
842
|
+
);
|
|
843
|
+
|
|
844
|
+
await assert.rejects(
|
|
845
|
+
() =>
|
|
846
|
+
evaluateInput(
|
|
847
|
+
"list[X,Y] = <{}nil,{X car, Y cdr} cons> = Y;",
|
|
848
|
+
freshState
|
|
849
|
+
),
|
|
850
|
+
/conflicts with knot 'Y'/
|
|
851
|
+
);
|
|
852
|
+
|
|
853
|
+
await evaluateInput("p[X]= {X x, Y y, Y z};", freshState);
|
|
854
|
+
output = await evaluateInput(":pats", freshState);
|
|
855
|
+
assert(
|
|
856
|
+
output[0].includes(
|
|
857
|
+
"p[X] = @A6vrY7TP9s34tvNgVZsk7N5m4Cr4ZJERYPBevgmqnK7P[X,(...)]"
|
|
858
|
+
)
|
|
859
|
+
);
|
|
860
|
+
|
|
861
|
+
output = await evaluateInput(":pat p", freshState);
|
|
862
|
+
assert.equal(
|
|
863
|
+
output[0],
|
|
864
|
+
"p[X0] = {X0 x, X1 y, X1 z}; -- " +
|
|
865
|
+
"@A6vrY7TP9s34tvNgVZsk7N5m4Cr4ZJERYPBevgmqnK7P"
|
|
866
|
+
);
|
|
867
|
+
|
|
868
|
+
output = await evaluateInput(
|
|
869
|
+
":pat @A6vrY7TP9s34tvNgVZsk7N5m4Cr4ZJERYPBevgmqnK7P",
|
|
870
|
+
freshState
|
|
871
|
+
);
|
|
872
|
+
assert.equal(
|
|
873
|
+
output[0],
|
|
874
|
+
"@A6vrY7TP9s34tvNgVZsk7N5m4Cr4ZJERYPBevgmqnK7P[X0, X1] = " +
|
|
875
|
+
"{X0 x, X1 y, X1 z};"
|
|
876
|
+
);
|
|
877
|
+
|
|
878
|
+
output = await evaluateInput(
|
|
879
|
+
"{{} x, {} y, {} z} ? " +
|
|
880
|
+
"@A6vrY7TP9s34tvNgVZsk7N5m4Cr4ZJERYPBevgmqnK7P[{}, {}]",
|
|
881
|
+
freshState
|
|
882
|
+
);
|
|
883
|
+
assert.match(output[0], /\{\{\} x, \{\} y, \{\} z\}/);
|
|
884
|
+
|
|
885
|
+
output = await evaluateInput(
|
|
886
|
+
"{{} x, {} y, {} z} ? p[{}]",
|
|
887
|
+
freshState
|
|
888
|
+
);
|
|
889
|
+
assert.match(output[0], /\{\{\} x, \{\} y, \{\} z\}/);
|
|
890
|
+
|
|
891
|
+
const testKlibPath2 = path.join(tmpDir, "patterns-test2.klib");
|
|
892
|
+
await evaluateInput(`:klib ${testKlibPath2}`, freshState);
|
|
893
|
+
const freshState2 = createState({ engine: "wasm" });
|
|
894
|
+
await evaluateInput(`:load ${testKlibPath2}`, freshState2);
|
|
895
|
+
const pHash = freshState2.patternAliases.p;
|
|
896
|
+
assert.equal(pHash, "@A6vrY7TP9s34tvNgVZsk7N5m4Cr4ZJERYPBevgmqnK7P");
|
|
897
|
+
const pOrigins = freshState2.meta[pHash]?.origins?.filter(
|
|
898
|
+
(o) => o.name === "p"
|
|
899
|
+
);
|
|
900
|
+
assert.equal(pOrigins?.length, 1);
|
|
901
|
+
assert.deepEqual(pOrigins[0].params, ["X"]);
|
|
902
|
+
assert.deepEqual(pOrigins[0].aliasArgs, ["X", "(...)"]);
|
|
903
|
+
output = await evaluateInput(":pats", freshState2);
|
|
904
|
+
assert(
|
|
905
|
+
output[0].includes(
|
|
906
|
+
"p[X] = @A6vrY7TP9s34tvNgVZsk7N5m4Cr4ZJERYPBevgmqnK7P[X,(...)]"
|
|
907
|
+
)
|
|
908
|
+
);
|
|
909
|
+
|
|
733
910
|
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
734
911
|
fs.rmSync(symlinkPath, { force: true });
|
|
735
912
|
console.log("OK");
|