@fraczak/k 5.1.2 → 6.0.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.
Files changed (99) hide show
  1. package/Code-derivation-tests/blowup.mjs +3 -3
  2. package/Code-derivation-tests/bnat.mjs +2 -2
  3. package/Code-derivation-tests/codec-witness-pattern.mjs +50 -0
  4. package/Code-derivation-tests/convergence-strategy.mjs +24 -0
  5. package/Code-derivation-tests/dot.mjs +12 -9
  6. package/Code-derivation-tests/filter-syntax.mjs +21 -0
  7. package/Code-derivation-tests/loop.mjs +8 -2
  8. package/Code-derivation-tests/nat.mjs +1 -1
  9. package/Code-derivation-tests/not-not.mjs +3 -3
  10. package/Code-derivation-tests/pattern-envelope.mjs +68 -0
  11. package/Code-derivation-tests/rlz.mjs +2 -2
  12. package/{BIG_PICTURE.md → DOCS/BIG_PICTURE.md} +103 -50
  13. package/DOCS/REPL.md +191 -0
  14. package/DOCS/book/01-introduction.md +51 -0
  15. package/DOCS/book/02-syntax-and-values.md +111 -0
  16. package/DOCS/book/03-types-as-finite-automata.md +156 -0
  17. package/DOCS/book/04-partial-functions-and-composition.md +146 -0
  18. package/DOCS/book/05-typing-filters-and-normalization.md +111 -0
  19. package/DOCS/book.md +54 -0
  20. package/README.md +204 -508
  21. package/TypeGraph.mjs +43 -0
  22. package/TypePatternGraph.mjs +251 -0
  23. package/Value.mjs +192 -0
  24. package/augmentation.mjs +198 -0
  25. package/codecs/BINARY_FORMAT.md +53 -0
  26. package/codecs/POLYMORPHIC_BINARY_FORMAT.md +303 -0
  27. package/codecs/README.md +215 -0
  28. package/codecs/example-pipeline.mjs +149 -0
  29. package/codecs/ieee.mjs +140 -0
  30. package/codecs/int.mjs +104 -0
  31. package/codecs/json-codec.mjs +215 -0
  32. package/codecs/json.mjs +40 -0
  33. package/codecs/k-parse.mjs +90 -0
  34. package/codecs/k-pattern.mjs +75 -0
  35. package/codecs/k-print.mjs +46 -0
  36. package/codecs/runtime/codec.mjs +768 -0
  37. package/codecs/runtime/ieee-pattern.mjs +30 -0
  38. package/codecs/runtime/pattern-json.mjs +96 -0
  39. package/codecs/runtime/prefix-codec.mjs +425 -0
  40. package/codecs/runtime/show-value.mjs +68 -0
  41. package/codecs/runtime/typeFromValue.mjs +34 -0
  42. package/codecs/runtime/unicode-string.mjs +220 -0
  43. package/codecs/show.mjs +41 -0
  44. package/codecs/string-codec.mjs +80 -0
  45. package/codecs/unit.mjs +51 -0
  46. package/codecs/utf16.mjs +66 -0
  47. package/codecs/utf8.mjs +37 -0
  48. package/codes.mjs +146 -44
  49. package/compiler.mjs +111 -0
  50. package/convergence.mjs +197 -0
  51. package/core.k +197 -0
  52. package/export.mjs +22 -32
  53. package/hash.mjs +29 -31
  54. package/index.mjs +41 -15
  55. package/k-vscode/.vscode/launch.json +14 -0
  56. package/k-vscode/README.md +65 -0
  57. package/k-vscode/language-configuration.json +42 -0
  58. package/k-vscode/package.json +57 -0
  59. package/k-vscode/snippets/k.json +92 -0
  60. package/k-vscode/syntaxes/k.tmLanguage.json +243 -0
  61. package/k.mjs +56 -54
  62. package/object.mjs +517 -0
  63. package/objects/README.md +38 -0
  64. package/objects/compile-lib.mjs +42 -0
  65. package/objects/compile.mjs +42 -0
  66. package/objects/decompile.mjs +32 -0
  67. package/package.json +18 -8
  68. package/parser.jison +27 -46
  69. package/parser.mjs +68 -100
  70. package/pretty.mjs +21 -40
  71. package/repl2.mjs +652 -0
  72. package/run.mjs +264 -231
  73. package/symbol-table.mjs +3 -17
  74. package/test-fingerprint.mjs +7 -7
  75. package/test-hash-fuzz.mjs +107 -0
  76. package/test-hash-normalization.mjs +134 -0
  77. package/test-k-object.mjs +74 -0
  78. package/test-repl2.mjs +95 -0
  79. package/test.mjs +45 -70
  80. package/tests.sh +10 -1
  81. package/typing.mjs +129 -676
  82. package/unification.mjs +157 -0
  83. package/valueIO.mjs +54 -0
  84. package/valueParser.jison +143 -0
  85. package/valueParser.mjs +713 -0
  86. package/Code-derivation-tests/built-in.mjs +0 -19
  87. package/Code-derivation-tests/vector.mjs +0 -19
  88. package/Filters/bug.k +0 -3
  89. package/Filters/composing.k +0 -15
  90. package/Filters/filter.k +0 -9
  91. package/TODO.md +0 -53
  92. package/TYPING.md +0 -365
  93. package/k-repl.cast +0 -1913
  94. package/k.cast +0 -618
  95. package/my-objects.json +0 -6
  96. package/patterns.mjs +0 -543
  97. package/repl.mjs +0 -213
  98. package/test-filters.mjs +0 -45
  99. package/typing-test.mjs +0 -40
package/repl.mjs DELETED
@@ -1,213 +0,0 @@
1
- #!/usr/bin/env node
2
- import readline from "node:readline";
3
- import fs from "node:fs";
4
-
5
- import k from "./index.mjs";
6
- import {run, closeVector } from "./run.mjs";
7
- import { prettyCode, prettyRel, patterns2filters } from "./pretty.mjs";
8
- import { find } from "./codes.mjs";
9
- import { exportRelation } from "./export.mjs";
10
-
11
-
12
- let DEBUG_FLAG = false;
13
-
14
- console.log("Very! experimental repl shell for 'k-language'...");
15
-
16
- const help = function () {
17
- console.log(` --debug toggle debug flag: [${DEBUG_FLAG?'ON':'OFF'}]`);
18
- if (DEBUG_FLAG)
19
- console.log(" --load loads codes and relations as value");
20
- console.log(" --c print aliased codes");
21
- console.log(" --C code print 'code' definition");
22
- console.log(" --r print relations");
23
- console.log(" --R rel print 'rel' definition with type patterns");
24
- console.log(" --p (--pp) pretty-print last value");
25
- console.log(" --s (--g) reg store (get) the current value in (from) register 'reg'");
26
- console.log(" --regs print register names");
27
- console.log(" --l file.k load 'file.k'");
28
- };
29
-
30
- help();
31
-
32
- const re__l = /^[ \n\t]*(?:--l[ ]+)(.+)[ ]*?$/;
33
- const re__s = /^[ \n\t]*(?:--s[ ]+)(.+)[ ]*?$/;
34
- const re__g = /^[ \n\t]*(?:--g[ ]+)(.+)[ ]*?$/;
35
- const re__R = /^[ \n\t]*(?:--R[ ]+)(.+)[ ]*?$/;
36
- const re__C = /^[ \n\t]*(?:--C[ ]+)(.+)[ ]*?$/;
37
-
38
- const rl = readline.createInterface({
39
- input: process.stdin,
40
- output: process.stdout,
41
- prompt: '> '
42
- });
43
-
44
- rl.prompt();
45
-
46
- rl.on('line', (line) => {
47
- evaluate(line);
48
- rl.prompt();
49
- }).on('close', () => {
50
- console.log('REPL closed');
51
- process.exit(0);
52
- });
53
-
54
- let old_val = k.compile("{}")({});
55
- let val = old_val;
56
- const printVal = function (v = val) {
57
- if (v === undefined)
58
- console.log("...",v);
59
- else
60
- console.log(`--> ${JSON.stringify(v)}`);
61
- rl.setPrompt('> ');
62
- }
63
- const registers = {};
64
- const buffer = [];
65
- function evaluate(line) {
66
- if (line.trim().endsWith('\\')) {
67
- buffer.push(line.slice(0, -1));
68
- rl.setPrompt(' ');
69
- return;
70
- }
71
- line = [...buffer, line].join('\n');
72
- buffer.length = 0;
73
- try {
74
- if (val === undefined)
75
- val = old_val;
76
- else
77
- old_val = val;
78
- if (line.match(/^[ \n\t]*(?:--h)?$/)) {
79
- // --h
80
- help();
81
- } else if (line.match(/^[ \n\t]*\^$/)) {
82
- val = closeVector(val);
83
- printVal();
84
-
85
- } else if (line.match(re__l)) {
86
- // --l
87
- const file = line.match(re__l)[1];
88
- console.log(` ... loading file: ${file} ...`);
89
- const kScript = fs.readFileSync(file).toString();
90
- console.log(` Done!`);
91
- // console.log(kScript);
92
- val = k.compile("+++\n" + kScript + "\n()")(val);
93
- printVal();
94
- // --debug
95
- } else if (line.match(/^[ \n\t]*--debug$/)) {
96
- DEBUG_FLAG = ! DEBUG_FLAG;
97
- // --load
98
- } else if (line.match(/^[ \n\t]*--load$/)) {
99
- val = run.defs;
100
- console.log(val);
101
- // --r
102
- } else if (line.match(/^[ \n\t]*(?:--r)?$/)) {
103
- console.log("BUILT-IN:", run.builtin);
104
- console.log("USER-DEFINED:",
105
- (function (defs, result) {
106
- if (defs == null) {
107
- return result;
108
- }
109
-
110
- for (const relName in defs.rels) {
111
- if (relName != "__main__")
112
- result[relName] = prettyRel(defs.rels[relName].def);
113
- }
114
- return result;
115
- })(run.defs, {}));
116
- // --c
117
- } else if (line.match(/^[ \n\t]*(?:--c)?$/)) {
118
- console.log(
119
- (function (defs, result) {
120
- for (const codeName of Object.keys(defs.representatives || {})) {
121
- const codeRep = defs.representatives[codeName] || codeName;
122
- if (codeName == codeRep) {
123
- result[codeName] = prettyCode(
124
- defs.representatives,
125
- find(codeRep)
126
- );
127
- } else if (! codeName.startsWith(":")) {
128
- result[codeName] = codeRep;
129
- }
130
- }
131
- return result;
132
- })(run.defs || {}, {})
133
- );
134
- // --C
135
- } else if (line.match(re__C)) {
136
- let codeName = line.match(re__C)[1];
137
- if (codeName.startsWith("?"))
138
- codeName = codeName.slice(1);
139
- if (codeName.startsWith("$"))
140
- codeName = codeName.slice(1);
141
- const canonicalName = run.defs.representatives[codeName] || codeName;
142
- const codeExp = find( canonicalName );
143
- if (codeExp.def == "builtin") {
144
- return console.log(` $ ${canonicalName} = :builtin;`);
145
- }
146
- console.log(` $ ${canonicalName} = ${prettyCode(run.defs.representatives, codeExp)}; -- ${codeExp.def}`);
147
- // --pp
148
- } else if (line.match(/^[ \n\t]*(?:--pp)?$/)) {
149
- console.log(val);
150
- // --p
151
- } else if (line.match(/^[ \n\t]*(?:--p)?$/)) {
152
- console.log(JSON.stringify(val, null, 2));
153
- // --regs
154
- } else if (line.match(/^[ \n\t]*(?:--regs)?$/)) {
155
- console.log(` -- registers: ${Object.keys(registers).join(", ")}`);
156
- // --s
157
- } else if (line.match(re__s)) {
158
- let reg = line.match(re__s)[1];
159
- registers[reg] = val;
160
- console.log(` -- current value stored in register: '${reg}'`);
161
- // --g
162
- } else if (line.match(re__g)) {
163
- console.log(" -- getting value from register...");
164
- let reg = line.match(re__g)[1];
165
- val = registers[reg];
166
- console.log(val);
167
- // --R rel
168
- } else if (line.match(re__R)) {
169
- const relName = line.match(re__R)[1];
170
- const rel = run.defs?.rels[relName];
171
- if (!rel) {
172
- if (relName in run.builtin) {
173
- console.log(` -- builtin relation: ${relName}\n ${relName} = ${run.builtin[relName]}`);
174
- return;
175
- }
176
- console.log(` -- relation '${relName}' not found`);
177
- return;
178
- }
179
- const canonicalRelName = run.defs.relAlias[relName];
180
- const filters = patterns2filters(rel.typePatternGraph, ...rel.def.patterns);
181
- // console.log(filters);
182
- // console.log(" variables:", JSON.stringify(variables));
183
- // console.log(JSON.stringify({filters, variables}, null, 2));
184
- // for (const filter of filters) {
185
- const filtersStr = filters.map( x => prettyRel( {op: "filter", filter: x}));
186
- // console.log(` -- canonical relation name: ${canonicalRelName} --`);
187
- // console.log(` ${relName} : ${filtersStr[0]} --> ${filtersStr[1]}`);
188
- // console.log(` ${relName} = ${prettyRel(rel.def)};`);
189
- // console.log(` ${relName} = ${prettyRel(rel.simplified)};`);
190
- console.log({
191
- name: canonicalRelName,
192
- type: `${filtersStr[0]} --> ${filtersStr[1]}`,
193
- def: prettyRel(rel.def)
194
- });
195
- if (DEBUG_FLAG) {
196
- val = exportRelation(run.defs.rels, run.defs.relAlias, relName);
197
- console.log(val);
198
- }
199
- // }
200
- // ------ k code
201
- } else if (!line.match(/^[ \n\t]*(?:#.*)?$/)) {
202
- try {
203
- val = k.run(`+++\n${line}\n()`, val);
204
- printVal();
205
- } catch (error) {
206
- console.error(error.message);
207
- }
208
- }
209
- } catch (e) {
210
- console.error(e.message);
211
- console.error(e);
212
- }
213
- }
package/test-filters.mjs DELETED
@@ -1,45 +0,0 @@
1
- import { unify_with_code } from './filters.mjs';
2
- import k from './index.mjs';
3
- // { compile, run, parse, annotate };
4
-
5
- const annotated = k.annotate(`
6
- $nat = < {} _, nat s >;
7
- $pair = { nat x, nat y };
8
- $list = < {} empty, { nat x, list xs } list>;
9
- z = {{} _} $nat;
10
- s = { $nat s} $nat;
11
- f1 = ? { n x, ...};
12
- f2 = ? { <...> y, ...};
13
- f3 = ? < x empty, { $MWqNxWxHMy xs, ...} list >;
14
- {}
15
- `);
16
-
17
-
18
-
19
- // console.log(JSON.stringify(annotated, null, 2));
20
-
21
-
22
-
23
- const test1 = unify_with_code(
24
- annotated.codes,
25
- annotated.representatives["pair"],
26
- annotated.rels['f1'].def.filter,
27
- {}
28
- );
29
- console.log(JSON.stringify(test1, null, 2));
30
-
31
- const test2 = unify_with_code(
32
- annotated.codes,
33
- annotated.representatives["pair"],
34
- annotated.rels['f2'].def.filter,
35
- {}
36
- );
37
- console.log(JSON.stringify(test2, null, 2));
38
-
39
- const test3 = unify_with_code(
40
- annotated.codes,
41
- annotated.representatives["list"],
42
- annotated.rels['f3'].def.filter,
43
- {}
44
- );
45
- console.log(JSON.stringify(test3, null, 2));
package/typing-test.mjs DELETED
@@ -1,40 +0,0 @@
1
- import t from './typing.mjs';
2
- // const t = await import('./typing.mjs');
3
- const g = new t.typePatternGraph();
4
-
5
- const pu = {pattern: '(...)', fields: []};
6
-
7
- // x = .x; { { 5 x } x i, { true x } x b }
8
- // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
9
-
10
- for (var i = 0; i < 16; i++) { g.addNewNode(pu); }
11
-
12
- g.addNewNode({pattern: '(...)'}, {x: [1]}); // 16
13
- g.unify("y:input", 0,16); // 17
14
-
15
- //
16
- g.unify('main:input',2,3,4,9,10); // 18
17
-
18
- g.addNewNode({pattern: '{}'}, {i: [8], b: [14]}); // 19
19
- g.unify("main:output", 15,19); // 20
20
-
21
- g.unify('comp:chain',6,7); // 21
22
- g.unify('comp:chain',12,13); // 22
23
-
24
-
25
- // {5 x}
26
- g.addNewNode({pattern: '<...>'}, {x: [5]}); // 23
27
- g.unify('variant:output',6,23); // 24
28
-
29
- // {true x}
30
- g.addNewNode({pattern: '<...>'}, {x: [11]}); // 25
31
- g.unify('variant:output',12,25); // 26
32
-
33
-
34
- // 5
35
- g.addNewNode({pattern: 'type', type: 'int'}); // 27
36
- g.unify('5:outout',5,27); // 28
37
-
38
- // true
39
- g.addNewNode({pattern: 'type', type: 'bool'}); // 29
40
- g.unify('true:output',11,29); // 30