@fraczak/k 8.2.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 +36 -0
- package/DOCS/CANONICAL_PATTERNS.md +252 -0
- package/DOCS/REPL.md +30 -3
- 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 +19 -4
- package/repl.mjs +319 -104
- 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 +150 -10
|
@@ -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,9 +620,15 @@ 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(
|
|
@@ -648,8 +654,7 @@ output = await evaluateInput(
|
|
|
648
654
|
);
|
|
649
655
|
assert.equal(
|
|
650
656
|
output[0],
|
|
651
|
-
"
|
|
652
|
-
"@MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg"
|
|
657
|
+
"@MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg[] = <{}=X0 0, X0 1>;"
|
|
653
658
|
);
|
|
654
659
|
|
|
655
660
|
output = await evaluateInput(
|
|
@@ -710,9 +715,9 @@ assert.equal(
|
|
|
710
715
|
output = await evaluateInput(":load Examples/list.k", patState);
|
|
711
716
|
assert.equal(output[0], "loaded Examples/list.k");
|
|
712
717
|
output = await evaluateInput(":patterns", patState);
|
|
713
|
-
assert(output[0].includes("bit[]"));
|
|
714
|
-
assert(output[0].includes("list[X]"));
|
|
715
|
-
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"));
|
|
716
721
|
|
|
717
722
|
output = await evaluateInput(":pattern list", patState);
|
|
718
723
|
assert.equal(
|
|
@@ -732,9 +737,9 @@ const freshState = createState({ engine: "wasm" });
|
|
|
732
737
|
output = await evaluateInput(`:load ${testKlibPath}`, freshState);
|
|
733
738
|
assert.equal(output[0], `loaded ${testKlibPath}`);
|
|
734
739
|
output = await evaluateInput(":patterns", freshState);
|
|
735
|
-
assert(output[0].includes("bit[]"));
|
|
736
|
-
assert(output[0].includes("pair[A,
|
|
737
|
-
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"));
|
|
738
743
|
|
|
739
744
|
output = await evaluateInput(":pattern bit", freshState);
|
|
740
745
|
assert.equal(
|
|
@@ -767,6 +772,141 @@ assert.equal(
|
|
|
767
772
|
output = await evaluateInput("{} | nil ? list[bit[]]", freshState);
|
|
768
773
|
assert.match(output[0], /\{\}\|nil/);
|
|
769
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
|
+
|
|
770
910
|
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
771
911
|
fs.rmSync(symlinkPath, { force: true });
|
|
772
912
|
console.log("OK");
|