@fraczak/k 6.1.1 → 6.1.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/Code-derivation-tests/pattern-envelope.mjs +26 -0
- package/DOCS/REPL.md +33 -0
- package/DOCS/a.html +398 -0
- package/DOCS/dependency-graph.html +140 -0
- package/DOCS/dependency-graph.md +144 -0
- package/NAT.k~ +452 -0
- package/UU/ta.k +1 -0
- package/UU/ta.k~ +0 -0
- package/UU/tb.k +0 -0
- package/UU/tc.k +0 -0
- package/codecs/BINARY_FORMAT.html +471 -0
- package/codecs/POLYMORPHIC_BINARY_FORMAT.html +355 -0
- package/codecs/ieee.mjs +19 -4
- package/codecs/int.mjs +11 -4
- package/codecs/json.mjs +7 -4
- package/codecs/k-parse.mjs +7 -4
- package/codecs/k-print.mjs +7 -4
- package/codecs/runtime/cli-entry.mjs +13 -0
- package/codecs/show.mjs +7 -4
- package/codecs/unit.mjs +23 -8
- package/codecs/utf16.mjs +27 -5
- package/codecs/utf8.mjs +27 -5
- package/core.json +1 -0
- package/core.klib +1 -0
- package/core.klib~ +0 -0
- package/ieee.klib +1 -0
- package/k-vscode/package-lock.json +4176 -0
- package/kkk.k +197 -0
- package/kkk.klib +0 -0
- package/kkk.klib~ +0 -0
- package/p.json +5475 -0
- package/package.json +2 -2
- package/repl-codecs.mjs +173 -0
- package/repl.mjs +215 -5
- package/run.mjs +16 -2
- package/test-ieee-arithmetic.mjs +93 -0
- package/test-repl.mjs +60 -1
- package/tests.sh +10 -1
- package/type_registry/registry.mjs~ +105 -0
- package/type_registry/typeParser.jison~ +132 -0
- package/.idea/codeStyles/codeStyleConfig.xml +0 -5
- package/.idea/copilot.data.migration.agent.xml +0 -6
- package/.idea/copilot.data.migration.ask.xml +0 -6
- package/.idea/copilot.data.migration.ask2agent.xml +0 -6
- package/.idea/copilot.data.migration.edit.xml +0 -6
- package/.idea/k.iml +0 -9
- package/.idea/misc.xml +0 -6
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
- package/k_compiler/runtime.c +0 -205
- package/k_compiler/runtime.h +0 -34
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
# Project Dependency Graph
|
|
2
|
+
|
|
3
|
+
_Auto-generated by scripts/generate-dependency-graph.mjs._
|
|
4
|
+
|
|
5
|
+
```mermaid
|
|
6
|
+
graph TD
|
|
7
|
+
classDef default fill:#fff,stroke:#333,stroke-width:2px,color:#000;
|
|
8
|
+
|
|
9
|
+
Graph_mjs["Graph.mjs (1.9 KB)<br/>██"]
|
|
10
|
+
TypeGraph_mjs["TypeGraph.mjs (1.1 KB)<br/>█"]
|
|
11
|
+
TypePatternGraph_mjs["TypePatternGraph.mjs (8.0 KB)<br/>████████"]
|
|
12
|
+
Value_mjs["Value.mjs (5.4 KB)<br/>█████"]
|
|
13
|
+
augmentation_mjs["augmentation.mjs (6.9 KB)<br/>███████"]
|
|
14
|
+
codecs_example_pipeline_mjs["codecs/example-pipeline.mjs (5.1 KB)<br/>█████"]
|
|
15
|
+
codecs_ieee_mjs["codecs/ieee.mjs (4.3 KB)<br/>████"]
|
|
16
|
+
codecs_int_mjs["codecs/int.mjs (3.5 KB)<br/>████"]
|
|
17
|
+
codecs_json_codec_mjs["codecs/json-codec.mjs (6.2 KB)<br/>██████"]
|
|
18
|
+
codecs_json_mjs["codecs/json.mjs (1.5 KB)<br/>██"]
|
|
19
|
+
codecs_k_parse_mjs["codecs/k-parse.mjs (3.0 KB)<br/>███"]
|
|
20
|
+
codecs_k_print_mjs["codecs/k-print.mjs (1.5 KB)<br/>██"]
|
|
21
|
+
codecs_runtime_codec_mjs["codecs/runtime/codec.mjs (25.7 KB)<br/>████████████████████"]
|
|
22
|
+
codecs_runtime_ieee_pattern_mjs["codecs/runtime/ieee-pattern.mjs (1.1 KB)<br/>█"]
|
|
23
|
+
codecs_runtime_pattern_json_mjs["codecs/runtime/pattern-json.mjs (3.5 KB)<br/>███"]
|
|
24
|
+
codecs_runtime_prefix_codec_mjs["codecs/runtime/prefix-codec.mjs (13.7 KB)<br/>██████████████"]
|
|
25
|
+
codecs_runtime_show_value_mjs["codecs/runtime/show-value.mjs (2.1 KB)<br/>██"]
|
|
26
|
+
codecs_runtime_test_codec_mjs["codecs/runtime/test-codec.mjs (3.0 KB)<br/>███"]
|
|
27
|
+
codecs_runtime_typeFromValue_mjs["codecs/runtime/typeFromValue.mjs (0.9 KB)<br/>█"]
|
|
28
|
+
codecs_runtime_unicode_string_mjs["codecs/runtime/unicode-string.mjs (7.5 KB)<br/>████████"]
|
|
29
|
+
codecs_show_mjs["codecs/show.mjs (1.5 KB)<br/>██"]
|
|
30
|
+
codecs_string_codec_mjs["codecs/string-codec.mjs (4.5 KB)<br/>█████"]
|
|
31
|
+
codecs_unit_mjs["codecs/unit.mjs (1.6 KB)<br/>██"]
|
|
32
|
+
codecs_utf16_mjs["codecs/utf16.mjs (2.2 KB)<br/>██"]
|
|
33
|
+
codecs_utf8_mjs["codecs/utf8.mjs (1.3 KB)<br/>█"]
|
|
34
|
+
codes_mjs["codes.mjs (9.9 KB)<br/>██████████"]
|
|
35
|
+
compiler_mjs["compiler.mjs (3.9 KB)<br/>████"]
|
|
36
|
+
convergence_mjs["convergence.mjs (6.5 KB)<br/>██████"]
|
|
37
|
+
export_mjs["export.mjs (5.2 KB)<br/>█████"]
|
|
38
|
+
hash_mjs["hash.mjs (1.1 KB)<br/>█"]
|
|
39
|
+
index_mjs["index.mjs (1.9 KB)<br/>██"]
|
|
40
|
+
k_mjs["k.mjs (2.7 KB)<br/>███"]
|
|
41
|
+
object_mjs["object.mjs (26.3 KB)<br/>████████████████████"]
|
|
42
|
+
pretty_mjs["pretty.mjs (5.6 KB)<br/>██████"]
|
|
43
|
+
repl_mjs["repl.mjs (29.3 KB)<br/>████████████████████"]
|
|
44
|
+
run_mjs["run.mjs (14.4 KB)<br/>██████████████"]
|
|
45
|
+
symbol_table_mjs["symbol-table.mjs (2.7 KB)<br/>███"]
|
|
46
|
+
typing_mjs["typing.mjs (4.9 KB)<br/>█████"]
|
|
47
|
+
unification_mjs["unification.mjs (5.4 KB)<br/>█████"]
|
|
48
|
+
valueIO_mjs["valueIO.mjs (1.8 KB)<br/>██"]
|
|
49
|
+
|
|
50
|
+
TypePatternGraph_mjs --> Graph_mjs
|
|
51
|
+
TypePatternGraph_mjs --> unification_mjs
|
|
52
|
+
TypePatternGraph_mjs --> TypeGraph_mjs
|
|
53
|
+
augmentation_mjs --> codes_mjs
|
|
54
|
+
codecs_example_pipeline_mjs --> codecs_runtime_codec_mjs
|
|
55
|
+
codecs_example_pipeline_mjs --> codecs_runtime_prefix_codec_mjs
|
|
56
|
+
codecs_example_pipeline_mjs --> codecs_runtime_pattern_json_mjs
|
|
57
|
+
codecs_example_pipeline_mjs --> Value_mjs
|
|
58
|
+
codecs_example_pipeline_mjs --> hash_mjs
|
|
59
|
+
codecs_ieee_mjs --> Value_mjs
|
|
60
|
+
codecs_ieee_mjs --> codecs_runtime_prefix_codec_mjs
|
|
61
|
+
codecs_ieee_mjs --> codecs_runtime_ieee_pattern_mjs
|
|
62
|
+
codecs_int_mjs --> Value_mjs
|
|
63
|
+
codecs_int_mjs --> codecs_runtime_prefix_codec_mjs
|
|
64
|
+
codecs_json_codec_mjs --> Value_mjs
|
|
65
|
+
codecs_json_codec_mjs --> codecs_string_codec_mjs
|
|
66
|
+
codecs_json_codec_mjs --> codecs_runtime_ieee_pattern_mjs
|
|
67
|
+
codecs_json_mjs --> codecs_runtime_prefix_codec_mjs
|
|
68
|
+
codecs_json_mjs --> codecs_json_codec_mjs
|
|
69
|
+
codecs_k_parse_mjs --> index_mjs
|
|
70
|
+
codecs_k_parse_mjs --> valueIO_mjs
|
|
71
|
+
codecs_k_parse_mjs --> codecs_runtime_codec_mjs
|
|
72
|
+
codecs_k_parse_mjs --> codecs_runtime_pattern_json_mjs
|
|
73
|
+
codecs_k_parse_mjs --> codecs_runtime_prefix_codec_mjs
|
|
74
|
+
codecs_k_print_mjs --> codecs_runtime_prefix_codec_mjs
|
|
75
|
+
codecs_runtime_codec_mjs --> Value_mjs
|
|
76
|
+
codecs_runtime_codec_mjs --> TypePatternGraph_mjs
|
|
77
|
+
codecs_runtime_codec_mjs --> typing_mjs
|
|
78
|
+
codecs_runtime_codec_mjs --> codes_mjs
|
|
79
|
+
codecs_runtime_codec_mjs --> codecs_runtime_pattern_json_mjs
|
|
80
|
+
codecs_runtime_ieee_pattern_mjs --> codes_mjs
|
|
81
|
+
codecs_runtime_ieee_pattern_mjs --> codecs_runtime_codec_mjs
|
|
82
|
+
codecs_runtime_ieee_pattern_mjs --> codecs_runtime_pattern_json_mjs
|
|
83
|
+
codecs_runtime_prefix_codec_mjs --> Value_mjs
|
|
84
|
+
codecs_runtime_prefix_codec_mjs --> codecs_runtime_codec_mjs
|
|
85
|
+
codecs_runtime_prefix_codec_mjs --> codecs_runtime_pattern_json_mjs
|
|
86
|
+
codecs_runtime_prefix_codec_mjs --> codecs_runtime_unicode_string_mjs
|
|
87
|
+
codecs_runtime_show_value_mjs --> Value_mjs
|
|
88
|
+
codecs_runtime_test_codec_mjs --> codecs_runtime_codec_mjs
|
|
89
|
+
codecs_runtime_test_codec_mjs --> codecs_runtime_prefix_codec_mjs
|
|
90
|
+
codecs_runtime_test_codec_mjs --> codecs_runtime_pattern_json_mjs
|
|
91
|
+
codecs_runtime_test_codec_mjs --> Value_mjs
|
|
92
|
+
codecs_runtime_test_codec_mjs --> hash_mjs
|
|
93
|
+
codecs_runtime_typeFromValue_mjs --> Value_mjs
|
|
94
|
+
codecs_runtime_unicode_string_mjs --> Value_mjs
|
|
95
|
+
codecs_show_mjs --> codecs_runtime_prefix_codec_mjs
|
|
96
|
+
codecs_show_mjs --> codecs_runtime_show_value_mjs
|
|
97
|
+
codecs_string_codec_mjs --> codecs_runtime_codec_mjs
|
|
98
|
+
codecs_string_codec_mjs --> codecs_runtime_prefix_codec_mjs
|
|
99
|
+
codecs_string_codec_mjs --> codecs_runtime_pattern_json_mjs
|
|
100
|
+
codecs_string_codec_mjs --> codecs_runtime_unicode_string_mjs
|
|
101
|
+
codecs_unit_mjs --> Value_mjs
|
|
102
|
+
codecs_unit_mjs --> codecs_runtime_prefix_codec_mjs
|
|
103
|
+
codecs_utf16_mjs --> codecs_string_codec_mjs
|
|
104
|
+
codecs_utf8_mjs --> codecs_string_codec_mjs
|
|
105
|
+
codes_mjs --> hash_mjs
|
|
106
|
+
compiler_mjs --> hash_mjs
|
|
107
|
+
compiler_mjs --> TypePatternGraph_mjs
|
|
108
|
+
compiler_mjs --> Graph_mjs
|
|
109
|
+
compiler_mjs --> codes_mjs
|
|
110
|
+
compiler_mjs --> augmentation_mjs
|
|
111
|
+
compiler_mjs --> export_mjs
|
|
112
|
+
compiler_mjs --> convergence_mjs
|
|
113
|
+
convergence_mjs --> pretty_mjs
|
|
114
|
+
convergence_mjs --> typing_mjs
|
|
115
|
+
export_mjs --> hash_mjs
|
|
116
|
+
export_mjs --> pretty_mjs
|
|
117
|
+
index_mjs --> Value_mjs
|
|
118
|
+
index_mjs --> valueIO_mjs
|
|
119
|
+
index_mjs --> compiler_mjs
|
|
120
|
+
index_mjs --> run_mjs
|
|
121
|
+
index_mjs --> codes_mjs
|
|
122
|
+
k_mjs --> index_mjs
|
|
123
|
+
k_mjs --> codecs_runtime_prefix_codec_mjs
|
|
124
|
+
k_mjs --> object_mjs
|
|
125
|
+
object_mjs --> index_mjs
|
|
126
|
+
object_mjs --> run_mjs
|
|
127
|
+
object_mjs --> codes_mjs
|
|
128
|
+
object_mjs --> TypePatternGraph_mjs
|
|
129
|
+
object_mjs --> pretty_mjs
|
|
130
|
+
pretty_mjs --> TypePatternGraph_mjs
|
|
131
|
+
repl_mjs --> index_mjs
|
|
132
|
+
repl_mjs --> run_mjs
|
|
133
|
+
repl_mjs --> codes_mjs
|
|
134
|
+
repl_mjs --> Value_mjs
|
|
135
|
+
repl_mjs --> pretty_mjs
|
|
136
|
+
repl_mjs --> object_mjs
|
|
137
|
+
repl_mjs --> codecs_runtime_show_value_mjs
|
|
138
|
+
run_mjs --> Value_mjs
|
|
139
|
+
run_mjs --> codecs_runtime_codec_mjs
|
|
140
|
+
run_mjs --> codecs_runtime_pattern_json_mjs
|
|
141
|
+
symbol_table_mjs --> hash_mjs
|
|
142
|
+
typing_mjs --> TypePatternGraph_mjs
|
|
143
|
+
valueIO_mjs --> Value_mjs
|
|
144
|
+
```
|
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/UU/ta.k
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
$bit
|
package/UU/ta.k~
ADDED
|
File without changes
|
package/UU/tb.k
ADDED
|
File without changes
|
package/UU/tc.k
ADDED
|
File without changes
|