@fraczak/k 8.6.1 → 8.7.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 +22 -0
- package/DOCS/CODECS.md +20 -14
- package/DOCS/FILE_FORMATS.md +79 -12
- package/DOCS/GETTING_STARTED.md +17 -8
- package/DOCS/PATTERNS.md +17 -0
- package/DOCS/REPL.md +13 -7
- package/Examples/arithmetics.k +81 -468
- package/Examples/ieee-arithmetics.k +37 -33
- package/Examples/poly.k +107 -104
- package/README.md +19 -11
- package/backend-api.mjs +3 -1
- package/backends/llvm/README.md +1 -1
- package/backends/llvm/bin/k-llvm-build.mjs +5 -5
- package/backends/llvm/bin/k-llvm-compile.mjs +4 -4
- package/backends/llvm/bin/k-llvm.mjs +6 -6
- package/backends/llvm/src/cli.mjs +17 -12
- package/backends/llvm/tests/integration.sh +4 -1
- package/backends/wasm/README.md +3 -3
- package/backends/wasm/bin/k-wasm-compile.mjs +4 -4
- package/backends/wasm/bin/k-wasm.mjs +4 -4
- package/backends/wasm/src/cli.mjs +12 -10
- package/backends/wasm/src/wasm.mjs +6 -2
- package/backends/wasm/tests/integration.sh +8 -3
- package/browser/shims/fs.mjs +19 -3
- package/canonical-pattern.mjs +217 -57
- package/core.k +2 -1
- package/harness/suites/poly.mjs +14 -30
- package/k.mjs +17 -13
- package/kvm.mjs +13 -9
- package/lib/arithmetics.k +287 -0
- package/lib/bit.k +68 -0
- package/lib/bits.k +221 -0
- package/lib/btrie.k +187 -0
- package/lib/djson.k +326 -0
- package/lib/ieee.k +709 -0
- package/lib/list.k +49 -0
- package/lib/string.k +588 -0
- package/named-patterns.mjs +2 -2
- package/object.mjs +51 -9
- package/objects/README.md +10 -3
- package/objects/compile.mjs +10 -8
- package/package.json +1 -1
- package/repl.mjs +28 -6
- package/scripts/benchmark-idempotency.mjs +22 -15
- package/scripts/benchmark.mjs +29 -10
- package/scripts/build-repl-html.mjs +41 -13
- package/scripts/perf-ieee.mjs +9 -1
- package/scripts/perf-int.mjs +10 -17
- package/scripts/perf-multiplication.mjs +9 -15
- package/scripts/perf-poly.mjs +16 -32
- package/scripts/run-tests.mjs +2 -2
- package/scripts/test-package.mjs +8 -2
- package/tests/helpers/cli.mjs +11 -3
- package/tests/integration.sh +9 -4
- package/tests/test-arithmetics.mjs +4 -2
- package/tests/test-djson-methods.mjs +16 -12
- package/tests/test-ieee-arithmetic.mjs +11 -3
- package/tests/test-ieee-text.mjs +15 -4
- package/tests/test-k-object.mjs +11 -3
- package/tests/test-karatsuba.mjs +1 -1
- package/tests/test-kvm-polymorphic.mjs +54 -34
- package/tests/test-lib.mjs +115 -0
- package/tests/test-named-patterns.mjs +12 -3
- package/tests/test-parse-json.mjs +33 -13
- package/tests/test-poly.mjs +10 -4
- package/tests/test-repl-html.mjs +18 -7
- package/tests/test-repl.mjs +50 -35
- package/Examples/djson.k +0 -894
- package/Examples/ieee.k +0 -969
- package/Examples/karatsuba-mult.k +0 -227
- package/Examples/list.k +0 -10
package/k.mjs
CHANGED
|
@@ -13,7 +13,8 @@ import {
|
|
|
13
13
|
decodeObject,
|
|
14
14
|
objectToFunction,
|
|
15
15
|
loadLibrary,
|
|
16
|
-
|
|
16
|
+
buildUsePreamble,
|
|
17
|
+
parseHeaderUses
|
|
17
18
|
} from "./object.mjs";
|
|
18
19
|
|
|
19
20
|
let kScript, inputStream;
|
|
@@ -34,14 +35,14 @@ function helpText() {
|
|
|
34
35
|
"",
|
|
35
36
|
"Options:",
|
|
36
37
|
" --lib file Load one .klib or .k library before compiling.",
|
|
37
|
-
" --
|
|
38
|
+
" --use spec Use library alias in scope (repeatable).",
|
|
38
39
|
" spec is 'name' or 'libname:localname'.",
|
|
39
40
|
" -h, --help Show this help.",
|
|
40
41
|
"",
|
|
41
42
|
"Examples:",
|
|
42
43
|
` echo '"abc"' | k-parse | ${prog} '{.1 0}' | k-print`,
|
|
43
44
|
` echo '{"x":1,"y":2}' | k-json --parse | \\`,
|
|
44
|
-
` ${prog} --lib Examples/poly.k --
|
|
45
|
+
` ${prog} --lib Examples/poly.k --use concat \\`,
|
|
45
46
|
` 's={()xs,()ys}concat; s' | k-json --print`
|
|
46
47
|
].join("\n");
|
|
47
48
|
}
|
|
@@ -61,12 +62,15 @@ function isFile(filePath) {
|
|
|
61
62
|
}
|
|
62
63
|
}
|
|
63
64
|
|
|
64
|
-
function compileFile(filePath, libraries,
|
|
65
|
+
function compileFile(filePath, libraries, useSpecs = []) {
|
|
65
66
|
const buffer = fs.readFileSync(filePath);
|
|
66
67
|
try {
|
|
67
68
|
return objectToFunction(decodeObject(buffer));
|
|
68
69
|
} catch {
|
|
69
|
-
|
|
70
|
+
const raw = buffer.toString("utf8");
|
|
71
|
+
const allSpecs = [...parseHeaderUses(raw), ...useSpecs];
|
|
72
|
+
const preamble = buildUsePreamble(allSpecs, libraries);
|
|
73
|
+
return k.compile(preamble + raw, { libraries });
|
|
70
74
|
}
|
|
71
75
|
}
|
|
72
76
|
|
|
@@ -78,8 +82,8 @@ function compileFile(filePath, libraries, preamble) {
|
|
|
78
82
|
}
|
|
79
83
|
|
|
80
84
|
const libraries = [];
|
|
81
|
-
const
|
|
82
|
-
// Parse --lib and --
|
|
85
|
+
const useSpecs = [];
|
|
86
|
+
// Parse --lib and --use flags
|
|
83
87
|
while (args.length > 0 && args[0].startsWith("--")) {
|
|
84
88
|
if (args[0] === "--lib") {
|
|
85
89
|
args.shift();
|
|
@@ -97,18 +101,16 @@ function compileFile(filePath, libraries, preamble) {
|
|
|
97
101
|
const libBuffer = fs.readFileSync(libPath);
|
|
98
102
|
libraries.push(loadLibrary(decodeObject(libBuffer)));
|
|
99
103
|
}
|
|
100
|
-
} else if (args[0] === "--
|
|
104
|
+
} else if (args[0] === "--use") {
|
|
101
105
|
args.shift();
|
|
102
106
|
const spec = args.shift();
|
|
103
|
-
if (!spec) throw new Error("--
|
|
104
|
-
|
|
107
|
+
if (!spec) throw new Error("--use requires a spec argument");
|
|
108
|
+
useSpecs.push(spec);
|
|
105
109
|
} else {
|
|
106
110
|
break;
|
|
107
111
|
}
|
|
108
112
|
}
|
|
109
113
|
|
|
110
|
-
const preamble = buildExportPreamble(exports, libraries);
|
|
111
|
-
|
|
112
114
|
let kScriptStr = (function (arg) {
|
|
113
115
|
if (arg == null) {
|
|
114
116
|
throw new Error("Missing script argument");
|
|
@@ -119,7 +121,9 @@ function compileFile(filePath, libraries, preamble) {
|
|
|
119
121
|
"pass the source/object file path directly"
|
|
120
122
|
);
|
|
121
123
|
}
|
|
122
|
-
if (isFile(arg)) return compileFile(arg, libraries,
|
|
124
|
+
if (isFile(arg)) return compileFile(arg, libraries, useSpecs);
|
|
125
|
+
const allSpecs = [...parseHeaderUses(arg), ...useSpecs];
|
|
126
|
+
const preamble = buildUsePreamble(allSpecs, libraries);
|
|
123
127
|
return preamble + arg;
|
|
124
128
|
})(args.shift());
|
|
125
129
|
let kScript = typeof kScriptStr === "function"
|
package/kvm.mjs
CHANGED
|
@@ -14,7 +14,8 @@ import {
|
|
|
14
14
|
compileObjectBuffer,
|
|
15
15
|
decodeObject,
|
|
16
16
|
loadLibrary,
|
|
17
|
-
|
|
17
|
+
buildUsePreamble,
|
|
18
|
+
parseHeaderUses
|
|
18
19
|
} from "./object.mjs";
|
|
19
20
|
import codes from "./codes.mjs";
|
|
20
21
|
import { isMainEntrypoint } from "./codecs/runtime/cli-entry.mjs";
|
|
@@ -959,7 +960,7 @@ function helpText() {
|
|
|
959
960
|
"",
|
|
960
961
|
"Options:",
|
|
961
962
|
" --lib file Load one .klib or .k library before compiling.",
|
|
962
|
-
" --
|
|
963
|
+
" --use spec Use library alias in scope (repeatable).",
|
|
963
964
|
" spec is 'name' or 'libname:localname'.",
|
|
964
965
|
" --envelope-free Run the interpreter in envelope-free mode.",
|
|
965
966
|
" -h, --help Show this help.",
|
|
@@ -967,7 +968,7 @@ function helpText() {
|
|
|
967
968
|
"Examples:",
|
|
968
969
|
` echo '"abc"' | k-parse | ${prog} '{.1 0}' | k-print`,
|
|
969
970
|
` echo '{"x":1,"y":2}' | k-json --parse | \\`,
|
|
970
|
-
` ${prog} --lib Examples/poly.k --
|
|
971
|
+
` ${prog} --lib Examples/poly.k --use concat \\`,
|
|
971
972
|
` 's={()xs,()ys}concat; s' | k-json --print`
|
|
972
973
|
].join("\n");
|
|
973
974
|
}
|
|
@@ -985,7 +986,7 @@ async function main() {
|
|
|
985
986
|
|
|
986
987
|
let envelopeFree = false;
|
|
987
988
|
const libraries = [];
|
|
988
|
-
const
|
|
989
|
+
const useSpecs = [];
|
|
989
990
|
|
|
990
991
|
// Parse options
|
|
991
992
|
while (args.length > 0) {
|
|
@@ -1008,11 +1009,11 @@ async function main() {
|
|
|
1008
1009
|
const libBuffer = fs.readFileSync(libPath);
|
|
1009
1010
|
libraries.push(loadLibrary(decodeObject(libBuffer)));
|
|
1010
1011
|
}
|
|
1011
|
-
} else if (args[0] === "--
|
|
1012
|
+
} else if (args[0] === "--use") {
|
|
1012
1013
|
args.shift();
|
|
1013
1014
|
const spec = args.shift();
|
|
1014
|
-
if (!spec) throw new Error("--
|
|
1015
|
-
|
|
1015
|
+
if (!spec) throw new Error("--use requires a spec argument");
|
|
1016
|
+
useSpecs.push(spec);
|
|
1016
1017
|
} else if (args[0].startsWith("--")) {
|
|
1017
1018
|
throw new Error(`Unknown option: ${args[0]}`);
|
|
1018
1019
|
} else {
|
|
@@ -1039,13 +1040,16 @@ async function main() {
|
|
|
1039
1040
|
if (error.message === "Cannot run a .klib library without a main relation; load it with --lib.") {
|
|
1040
1041
|
throw error;
|
|
1041
1042
|
}
|
|
1043
|
+
const raw = buffer.toString("utf8");
|
|
1044
|
+
const allSpecs = [...parseHeaderUses(raw), ...useSpecs];
|
|
1042
1045
|
return {
|
|
1043
1046
|
kind: "source",
|
|
1044
|
-
source:
|
|
1047
|
+
source: buildUsePreamble(allSpecs, libraries) + raw
|
|
1045
1048
|
};
|
|
1046
1049
|
}
|
|
1047
1050
|
}
|
|
1048
|
-
|
|
1051
|
+
const allSpecs = [...parseHeaderUses(arg), ...useSpecs];
|
|
1052
|
+
return { kind: "source", source: buildUsePreamble(allSpecs, libraries) + arg };
|
|
1049
1053
|
})(args.shift());
|
|
1050
1054
|
|
|
1051
1055
|
const inputStream = (function (arg) {
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
# --lib lib/bits.k --use trim inv_loop inv cmp
|
|
2
|
+
# --lib lib/bits.k --use inc:inc_bits dec:dec_bits plus:plus_bits minus:minus_bits mul:mul_bits
|
|
3
|
+
|
|
4
|
+
-- lib/arithmetics.k — Signed arbitrary-precision integers (int[])
|
|
5
|
+
|
|
6
|
+
---------------------------------------------------------------
|
|
7
|
+
-- §1. Base Patterns & Primitives
|
|
8
|
+
---------------------------------------------------------------
|
|
9
|
+
|
|
10
|
+
bool[] = < {} true, {} false >;
|
|
11
|
+
order[] = < {} lt, {} eq, {} gt >;
|
|
12
|
+
bits[] = < {} _, B 0, B 1 > = B;
|
|
13
|
+
|
|
14
|
+
true = {} |true ?bool[];
|
|
15
|
+
false = {} |false ?bool[];
|
|
16
|
+
un_true = ?bool[] /true;
|
|
17
|
+
un_false = ?bool[] /false;
|
|
18
|
+
un_lt = ?order[] /lt;
|
|
19
|
+
un_eq = ?order[] /eq;
|
|
20
|
+
un_gt = ?order[] /gt;
|
|
21
|
+
|
|
22
|
+
_ = {} |_ ?bits[];
|
|
23
|
+
0b = |0 ?bits[];
|
|
24
|
+
1b = |1 ?bits[];
|
|
25
|
+
un_ = ?bits[] /_;
|
|
26
|
+
un0 = ?bits[] /0;
|
|
27
|
+
un1 = ?bits[] /1;
|
|
28
|
+
_? = un_ _;
|
|
29
|
+
0 = _ 0b;
|
|
30
|
+
1 = _ 1b;
|
|
31
|
+
|
|
32
|
+
---------------------------------------------------------------
|
|
33
|
+
-- §2. Natural Constants & Helpers (bits[])
|
|
34
|
+
---------------------------------------------------------------
|
|
35
|
+
|
|
36
|
+
succ = inc_bits;
|
|
37
|
+
2 = 1 succ;
|
|
38
|
+
3 = 2 succ;
|
|
39
|
+
4 = 3 succ;
|
|
40
|
+
5 = 4 succ;
|
|
41
|
+
6 = 5 succ;
|
|
42
|
+
7 = 6 succ;
|
|
43
|
+
8 = 7 succ;
|
|
44
|
+
9 = 8 succ;
|
|
45
|
+
10 = 9 succ;
|
|
46
|
+
11 = 10 succ;
|
|
47
|
+
12 = 11 succ;
|
|
48
|
+
|
|
49
|
+
empty? = _?;
|
|
50
|
+
remove_leading_zeros = trim;
|
|
51
|
+
nat = trim < _? 0, () >;
|
|
52
|
+
concat = { .x inv x, .y r } inv_loop;
|
|
53
|
+
|
|
54
|
+
cmp_nat = { .x trim x, .y trim y } cmp;
|
|
55
|
+
GT = { cmp_nat un_gt if, () then } .then;
|
|
56
|
+
EQ = { cmp_nat un_eq if, () then } .then;
|
|
57
|
+
LE = { cmp_nat <un_lt, un_eq> if, () then } .then;
|
|
58
|
+
|
|
59
|
+
---------------------------------------------------------------
|
|
60
|
+
-- §3. Signed Integer Pattern & Primitives (int[])
|
|
61
|
+
---------------------------------------------------------------
|
|
62
|
+
|
|
63
|
+
int[] = < bits[] "+", bits[] "-" >;
|
|
64
|
+
int? = ?int[];
|
|
65
|
+
int_pair[] = { int[] x, int[] y };
|
|
66
|
+
|
|
67
|
+
-- Only the 4 atomic primitives mention ?int[]
|
|
68
|
+
pos_int = |"+" ?int[];
|
|
69
|
+
neg_int = |"-" ?int[];
|
|
70
|
+
un_pos = ?int[] /"+";
|
|
71
|
+
un_neg = ?int[] /"-";
|
|
72
|
+
|
|
73
|
+
int = nat pos_int;
|
|
74
|
+
abs = < un_pos, un_neg >;
|
|
75
|
+
|
|
76
|
+
pos? = { un_pos trim un1 if, () then } .then;
|
|
77
|
+
neg? = { un_neg if, () then } .then;
|
|
78
|
+
zero_int? = { un_pos trim un_ if, () then } .then;
|
|
79
|
+
minus_one? = { un_neg trim un1 un_ if, () then } .then;
|
|
80
|
+
|
|
81
|
+
swap_sign = <
|
|
82
|
+
zero_int?,
|
|
83
|
+
un_pos neg_int,
|
|
84
|
+
un_neg pos_int
|
|
85
|
+
>;
|
|
86
|
+
|
|
87
|
+
---------------------------------------------------------------
|
|
88
|
+
-- §4. Integer Increment, Decrement, Addition, Subtraction, Multiplication
|
|
89
|
+
---------------------------------------------------------------
|
|
90
|
+
|
|
91
|
+
inc = <
|
|
92
|
+
un_pos inc_bits nat pos_int,
|
|
93
|
+
minus_one? 0 int,
|
|
94
|
+
un_neg dec_bits nat neg_int
|
|
95
|
+
>;
|
|
96
|
+
|
|
97
|
+
dec = <
|
|
98
|
+
zero_int? 1 neg_int,
|
|
99
|
+
un_pos dec_bits nat pos_int,
|
|
100
|
+
un_neg inc_bits nat neg_int
|
|
101
|
+
>;
|
|
102
|
+
|
|
103
|
+
plus = <
|
|
104
|
+
{ .x un_pos x, .y un_pos y } plus_bits nat pos_int,
|
|
105
|
+
{ .x un_neg x, .y un_neg y } plus_bits nat neg_int,
|
|
106
|
+
{ .y un_neg x, .x un_pos y } GT minus_bits nat neg_int,
|
|
107
|
+
{ .x un_pos x, .y un_neg y } minus_bits nat pos_int,
|
|
108
|
+
{ .x un_neg x, .y un_pos y } GT minus_bits nat neg_int,
|
|
109
|
+
{ .y un_pos x, .x un_neg y } minus_bits nat pos_int
|
|
110
|
+
>;
|
|
111
|
+
|
|
112
|
+
minus = { .x x, .y swap_sign y } plus;
|
|
113
|
+
|
|
114
|
+
times = <
|
|
115
|
+
{ .x un_pos x, .y un_pos y } mul_bits int,
|
|
116
|
+
{ .x un_neg x, .y un_neg y } mul_bits int,
|
|
117
|
+
{ .x un_pos x, .y un_neg y } mul_bits int swap_sign,
|
|
118
|
+
{ .x un_neg x, .y un_pos y } mul_bits int swap_sign
|
|
119
|
+
>;
|
|
120
|
+
|
|
121
|
+
---------------------------------------------------------------
|
|
122
|
+
-- §5. Karatsuba Multiplication (O(n^1.585))
|
|
123
|
+
---------------------------------------------------------------
|
|
124
|
+
|
|
125
|
+
peel2 = < <un0, un1> <un0, un1>, <un0, un1>, _? >;
|
|
126
|
+
|
|
127
|
+
half_loop = <
|
|
128
|
+
{ .x un_ a, .y un_ b, .m then } .then,
|
|
129
|
+
{ .x peel2 x, .y peel2 y, .m 0b m } half_loop
|
|
130
|
+
>;
|
|
131
|
+
half_bits = { .x trim x, .y trim y, _ m } half_loop;
|
|
132
|
+
|
|
133
|
+
drop8 = un0 un0 un0 un0 un0 un0 un0 un0;
|
|
134
|
+
drop64 = drop8 drop8 drop8 drop8 drop8 drop8 drop8 drop8;
|
|
135
|
+
drop256 = drop64 drop64 drop64 drop64;
|
|
136
|
+
at_least_256? = < drop256 true, false >;
|
|
137
|
+
|
|
138
|
+
split_rev_loop = <
|
|
139
|
+
{ .m un_ if, { .low nat low, .x_rev inv nat high } then } .then,
|
|
140
|
+
{ .x_rev un_ if, { .low nat low, 0 high } then } .then,
|
|
141
|
+
<
|
|
142
|
+
{ .x_rev un0 x_rev, .m un0 m, .low 0b low },
|
|
143
|
+
{ .x_rev un1 x_rev, .m un0 m, .low 1b low }
|
|
144
|
+
> split_rev_loop
|
|
145
|
+
>;
|
|
146
|
+
split_at = { .x inv x_rev, .m m, _ low } split_rev_loop;
|
|
147
|
+
|
|
148
|
+
shift_bits = <
|
|
149
|
+
{ .val trim un_ if, 0 then } .then,
|
|
150
|
+
{ .val inv x, .m r } inv_loop
|
|
151
|
+
>;
|
|
152
|
+
|
|
153
|
+
shift2_bits = <
|
|
154
|
+
{ .val trim un_ if, 0 then } .then,
|
|
155
|
+
{ .val inv x, { .m x, .m r } inv_loop r } inv_loop
|
|
156
|
+
>;
|
|
157
|
+
|
|
158
|
+
karatsuba_bits = { { .x x, .y y } half_bits m, .x x, .y y } <
|
|
159
|
+
{ .m at_least_256? un_false if, { .x x, .y y } mul_bits nat then } .then,
|
|
160
|
+
{
|
|
161
|
+
.m at_least_256? un_true if,
|
|
162
|
+
{
|
|
163
|
+
{ .x x, .m m } split_at sx,
|
|
164
|
+
{ .y x, .m m } split_at sy,
|
|
165
|
+
.m m
|
|
166
|
+
}
|
|
167
|
+
{
|
|
168
|
+
{ .sx.high x, .sy.high y } karatsuba_bits z2,
|
|
169
|
+
{ .sx.low x, .sy.low y } karatsuba_bits z0,
|
|
170
|
+
{
|
|
171
|
+
{ .sx.high x, .sx.low y } plus_bits x,
|
|
172
|
+
{ .sy.high x, .sy.low y } plus_bits y
|
|
173
|
+
} karatsuba_bits z_sum,
|
|
174
|
+
.m m
|
|
175
|
+
}
|
|
176
|
+
{
|
|
177
|
+
.z2 z2,
|
|
178
|
+
.z0 z0,
|
|
179
|
+
{ { .z_sum x, .z2 y } minus_bits x, .z0 y } minus_bits z1,
|
|
180
|
+
.m m
|
|
181
|
+
}
|
|
182
|
+
{
|
|
183
|
+
{ .z2 val, .m m } shift2_bits z2_s,
|
|
184
|
+
{ .z1 val, .m m } shift_bits z1_s,
|
|
185
|
+
.z0 z0
|
|
186
|
+
}
|
|
187
|
+
{ { .z2_s x, .z1_s y } plus_bits x, .z0 y } plus_bits nat then
|
|
188
|
+
} .then
|
|
189
|
+
>;
|
|
190
|
+
|
|
191
|
+
karatsuba = <
|
|
192
|
+
{ .x un_pos x, .y un_pos y } karatsuba_bits int,
|
|
193
|
+
{ .x un_neg x, .y un_neg y } karatsuba_bits int,
|
|
194
|
+
{ .x un_pos x, .y un_neg y } karatsuba_bits int swap_sign,
|
|
195
|
+
{ .x un_neg x, .y un_pos y } karatsuba_bits int swap_sign
|
|
196
|
+
>;
|
|
197
|
+
|
|
198
|
+
---------------------------------------------------------------
|
|
199
|
+
-- §6. Integer Division (Adaptive Hybrid), Modulo, GCD, LCM
|
|
200
|
+
---------------------------------------------------------------
|
|
201
|
+
|
|
202
|
+
shift_in_0 = trim < _? 0, { inv 0b x, _ r } inv_loop >;
|
|
203
|
+
shift_in_1 = trim < _? 1, { inv 1b x, _ r } inv_loop >;
|
|
204
|
+
sub_bits = minus_bits nat;
|
|
205
|
+
|
|
206
|
+
div_restoring = <
|
|
207
|
+
{ .x un_ if, { .q inv int div, .r rem } then } .then,
|
|
208
|
+
<
|
|
209
|
+
{ .x un0 x, .r shift_in_0 r, .y y, .q q },
|
|
210
|
+
{ .x un1 x, .r shift_in_1 r, .y y, .q q }
|
|
211
|
+
>
|
|
212
|
+
<
|
|
213
|
+
{ { .y x, .r y } LE if, { .x x, .y y, .q 1b q, { .r x, .y y } sub_bits r } then } .then,
|
|
214
|
+
{ { .y x, .r y } GT if, { .x x, .y y, .q 0b q, .r r } then } .then
|
|
215
|
+
>
|
|
216
|
+
div_restoring
|
|
217
|
+
>;
|
|
218
|
+
|
|
219
|
+
pad_down = <
|
|
220
|
+
{ .x un_ if, { ._y _y, .steps steps } then } .then,
|
|
221
|
+
{ .x <un0, un1> x, ._y 0b _y, .steps 0b steps } pad_down
|
|
222
|
+
>;
|
|
223
|
+
|
|
224
|
+
align_down = <
|
|
225
|
+
{ .x <un0, un1> x, .y <un0, un1> y, ._y _y } align_down,
|
|
226
|
+
{ .y un_ if, { .x x, ._y _y, _ steps } pad_down then } .then,
|
|
227
|
+
{ .x un_ a, .y un_ b, { ._y _y, _ steps } then } .then
|
|
228
|
+
>;
|
|
229
|
+
|
|
230
|
+
div_down =
|
|
231
|
+
{ .x x, ._y _y, .steps steps, .q q, ._y inv nat y_scaled }
|
|
232
|
+
<
|
|
233
|
+
{
|
|
234
|
+
{ .y_scaled x, .x y } LE if,
|
|
235
|
+
{ { .x x, .y_scaled y } sub_bits x, .q 1b q, ._y _y, .steps steps } then
|
|
236
|
+
} .then,
|
|
237
|
+
{
|
|
238
|
+
{ .y_scaled x, .x y } GT if,
|
|
239
|
+
{ .x x, .q 0b q, ._y _y, .steps steps } then
|
|
240
|
+
} .then
|
|
241
|
+
>
|
|
242
|
+
<
|
|
243
|
+
{ .steps un_ if, { .q inv int div, .x rem } then } .then,
|
|
244
|
+
{ .x x, ._y un0 _y, .steps un0 steps, .q q } div_down
|
|
245
|
+
>;
|
|
246
|
+
|
|
247
|
+
check_2y = <
|
|
248
|
+
{ .x un_ a, .y <_?, un0, un1> b } false,
|
|
249
|
+
{ .y un_ a, .x <un0, un1> b } true,
|
|
250
|
+
{ .x <un0, un1> un_ a, .y <un0, un1> b } false,
|
|
251
|
+
{ .x <un0, un1> <un0, un1> x, .y <un0, un1> y } check_2y
|
|
252
|
+
>;
|
|
253
|
+
|
|
254
|
+
div_mag = <
|
|
255
|
+
{ .x un_ a, .y <_?, un0, un1> b, { 0 int div, 0 rem } then } .then,
|
|
256
|
+
{ { .y x, .x y } GT if, { 0 int div, .x rem } then } .then,
|
|
257
|
+
{
|
|
258
|
+
{ .x x, .y y } check_2y un_false if,
|
|
259
|
+
{ { .x x, .y y, .y inv _y } align_down aligned, .x x }
|
|
260
|
+
{ .x x, .aligned._y _y, .aligned.steps steps, _ q } div_down then
|
|
261
|
+
} .then,
|
|
262
|
+
{
|
|
263
|
+
{ .x x, .y y } check_2y un_true if,
|
|
264
|
+
{ .x x, .y y, 0 r, _ q } div_restoring then
|
|
265
|
+
} .then
|
|
266
|
+
>;
|
|
267
|
+
|
|
268
|
+
div = <
|
|
269
|
+
{ .x un_pos trim x, .y un_pos trim y } div_mag,
|
|
270
|
+
{ .x un_neg trim x, .y un_neg trim y } div_mag,
|
|
271
|
+
{ .x un_pos trim x, .y un_neg trim y } div_mag { .div swap_sign div, .rem rem },
|
|
272
|
+
{ .x un_neg trim x, .y un_pos trim y } div_mag { .div swap_sign div, .rem rem }
|
|
273
|
+
>;
|
|
274
|
+
|
|
275
|
+
mod = { .x int x, .y int y } div .rem;
|
|
276
|
+
|
|
277
|
+
gcd_loop = <
|
|
278
|
+
{ .y trim un_ if, .x nat then } .then,
|
|
279
|
+
{ .y x, { .x x, .y y } mod y } gcd_loop
|
|
280
|
+
>;
|
|
281
|
+
|
|
282
|
+
gcd = <
|
|
283
|
+
{ .y x, .x y } GT,
|
|
284
|
+
()
|
|
285
|
+
> gcd_loop;
|
|
286
|
+
|
|
287
|
+
lcm = { { .x int x, gcd int y } div .div x, .y int y } times abs;
|
package/lib/bit.k
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
-- lib/bit.k — Finite enumeration patterns: bit[], bool[], order[]
|
|
2
|
+
|
|
3
|
+
---------------------------------------------------------------
|
|
4
|
+
-- §1. Bit
|
|
5
|
+
---------------------------------------------------------------
|
|
6
|
+
|
|
7
|
+
bit[] = < {} 0, {} 1 >;
|
|
8
|
+
|
|
9
|
+
-- Primitive constructors and destructors
|
|
10
|
+
0 = {} |0 ?bit[];
|
|
11
|
+
1 = {} |1 ?bit[];
|
|
12
|
+
is0 = ?bit[] /0;
|
|
13
|
+
is1 = ?bit[] /1;
|
|
14
|
+
|
|
15
|
+
-- Polymorphic tag guards
|
|
16
|
+
0? = ?X /0 |0 ?X;
|
|
17
|
+
1? = ?X /1 |1 ?X;
|
|
18
|
+
|
|
19
|
+
-- Bit operations and equality guard
|
|
20
|
+
not_bit = < is0 1, is1 0 >;
|
|
21
|
+
and_bit = < .x is0 0, .y is0 0, { .x is1 a, .y is1 b } 1 >;
|
|
22
|
+
or_bit = < .x is1 1, .y is1 1, { .x is0 a, .y is0 b } 0 >;
|
|
23
|
+
xor_bit = <
|
|
24
|
+
{ .x is0 a, .y is0 b } 0,
|
|
25
|
+
{ .x is1 a, .y is1 b } 0,
|
|
26
|
+
{ .x is0 a, .y is1 b } 1,
|
|
27
|
+
{ .x is1 a, .y is0 b } 1
|
|
28
|
+
>;
|
|
29
|
+
eq_bit? = <
|
|
30
|
+
{ .x is0 0 x, .y is0 0 y },
|
|
31
|
+
{ .x is1 1 x, .y is1 1 y }
|
|
32
|
+
>;
|
|
33
|
+
|
|
34
|
+
---------------------------------------------------------------
|
|
35
|
+
-- §2. Bool
|
|
36
|
+
---------------------------------------------------------------
|
|
37
|
+
|
|
38
|
+
bool[] = < {} true, {} false >;
|
|
39
|
+
|
|
40
|
+
-- Primitive constructors and destructors
|
|
41
|
+
true = {} |true ?bool[];
|
|
42
|
+
false = {} |false ?bool[];
|
|
43
|
+
un_true = ?bool[] /true;
|
|
44
|
+
un_false = ?bool[] /false;
|
|
45
|
+
|
|
46
|
+
-- Bool guards and negation
|
|
47
|
+
true? = un_true true;
|
|
48
|
+
false? = un_false false;
|
|
49
|
+
not = < un_true false, un_false true >;
|
|
50
|
+
|
|
51
|
+
---------------------------------------------------------------
|
|
52
|
+
-- §3. Order
|
|
53
|
+
---------------------------------------------------------------
|
|
54
|
+
|
|
55
|
+
order[] = < {} lt, {} eq, {} gt >;
|
|
56
|
+
|
|
57
|
+
-- Primitive constructors and destructors
|
|
58
|
+
lt = {} |lt ?order[];
|
|
59
|
+
eq = {} |eq ?order[];
|
|
60
|
+
gt = {} |gt ?order[];
|
|
61
|
+
un_lt = ?order[] /lt;
|
|
62
|
+
un_eq = ?order[] /eq;
|
|
63
|
+
un_gt = ?order[] /gt;
|
|
64
|
+
|
|
65
|
+
-- Order guards
|
|
66
|
+
lt_order? = un_lt lt;
|
|
67
|
+
eq_order? = un_eq eq;
|
|
68
|
+
gt_order? = un_gt gt;
|