@fraczak/k 6.2.0 → 6.2.1

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.
@@ -0,0 +1,291 @@
1
+ -- Standard binary encoding of natural numbers
2
+
3
+ $ bnat = < {} _, bnat 0, bnat 1 >;
4
+
5
+ _ = {}|_;
6
+ _1 = |1;
7
+ _0 = |0;
8
+
9
+ bnat_0 = _ ;
10
+ bnat_1 = _ _1 ;
11
+ bnat_2 = _ _0 _1 ;
12
+ bnat_3 = _ _1 _1 ;
13
+ bnat_4 = _ _0 _0 _1 ;
14
+ bnat_5 = _ _1 _0 _1 ;
15
+ bnat_6 = _ _0 _1 _1 ;
16
+ bnat_7 = _ _1 _1 _1 ;
17
+ bnat_8 = _ _0 _0 _0 _1 ;
18
+ bnat_9 = _ _1 _0 _0 _1 ;
19
+ bnat_10 = _ _0 _1 _0 _1 ;
20
+
21
+ remove_leading_zeros = < /0 remove_leading_zeros, () >;
22
+
23
+ reverse = {() x, _ result} _reverse;
24
+
25
+ _reverse =
26
+ ${bnat x, bnat result}
27
+ <
28
+ {.x/_ if, .result then} .then,
29
+ {.x/0 x, .result _0 result} _reverse,
30
+ {.x/1 x, .result _1 result} _reverse
31
+ >
32
+ ;
33
+
34
+ append_0 = reverse _0 reverse; -- i.e., shift_left, double, or times2
35
+
36
+ $carry = <{} 0, {} 1>;
37
+ c0 = {} |0 ;
38
+ c1 = {} |1 ;
39
+
40
+ $ pair = {bnat x, bnat y};
41
+
42
+ plus =
43
+ -- ${bnat x, bnat y} -- $ pair
44
+ {
45
+ .x remove_leading_zeros reverse x,
46
+ .y remove_leading_zeros reverse y,
47
+ c0 c
48
+ }
49
+ _plus
50
+ reverse
51
+ ;
52
+
53
+ _plus =
54
+ -- least significant bit first addition with carry
55
+ ${bnat x, bnat y, carry c}
56
+ <
57
+ {.x/_ if1, .y/_ if2, <.c/0 bnat_0, bnat_1> then} .then,
58
+ {.x/_ if1, .c/0 if2, .y then} .then,
59
+
60
+ {.x y, .y/_ _ x, .c c} _plus, -- swap x with y, if y = 0
61
+ {<.c/0 bnat_0, bnat_1>
62
+ x, .y y, .x/_ c0 c} _plus,
63
+
64
+ {.x/0 x, .y/0 y, .c/0 c0 c} _plus _0,
65
+ {.x/0 x, .y/0 y, .c/1 c0 c} _plus _1,
66
+ {.x/0 x, .y/1 y, .c/0 c0 c} _plus _1,
67
+ {.x/0 x, .y/1 y, .c/1 c1 c} _plus _0,
68
+
69
+ {.x/1 x, .y/0 y, .c/0 c0 c} _plus _1,
70
+ {.x/1 x, .y/0 y, .c/1 c1 c} _plus _0,
71
+
72
+ {.x/1 x, .y/1 y, .c/0 c1 c} _plus _0,
73
+ {.x/1 x, .y/1 y, .c/1 c1 c} _plus _1
74
+ >
75
+ ;
76
+
77
+ minus =
78
+ -- ${bnat x, bnat y}
79
+ {
80
+ .x remove_leading_zeros reverse x,
81
+ .y remove_leading_zeros reverse y,
82
+ c0 c
83
+ }
84
+ _minus
85
+ reverse
86
+ remove_leading_zeros
87
+ ;
88
+
89
+ _minus =
90
+ -- least significat bit first substraction with carry
91
+ ${bnat x, bnat y, carry c}
92
+ <
93
+ {.x x, .y/_ y, .c/0 if} .x,
94
+ {.x/1 x, .y/_ y, .c/1 if} .x _0,
95
+ {.x/0 x, .y/_ _ y, .c/1 c1 c} _minus _1,
96
+ {.x/0 x, .y/0 y, .c/0 c0 c} _minus _0,
97
+ {.x/0 x, .y/0 y, .c/1 c1 c} _minus _1,
98
+ {.x/0 x, .y/1 y, .c/0 c1 c} _minus _1,
99
+ {.x/0 x, .y/1 y, .c/1 c1 c} _minus _0,
100
+ {.x/1 x, .y/0 y, .c/0 c0 c} _minus _1,
101
+ {.x/1 x, .y/0 y, .c/1 c0 c} _minus _0,
102
+ {.x/1 x, .y/1 y, .c/0 c0 c} _minus _0,
103
+ {.x/1 x, .y/1 y, .c/1 c1 c} _minus _1
104
+ >
105
+ ;
106
+
107
+ times =
108
+ -- ${bnat x, bnat y}
109
+ {
110
+ bnat_0 result,
111
+ .x remove_leading_zeros reverse _x,
112
+ .y remove_leading_zeros y
113
+ }
114
+ _times
115
+ reverse
116
+ ;
117
+
118
+ _times =
119
+ ${bnat result, bnat _x, bnat y}
120
+ <
121
+ { .y/_ if, .result then } .then,
122
+ { .result _0 result, ._x _x, .y/0 y } _times,
123
+ { { ._x x, .result _0 y, c0 c } _plus result, ._x _x, .y/1 y } _times
124
+ >
125
+ ;
126
+
127
+ inc = {() x, bnat_1 y} plus;
128
+ dec = {() x, bnat_1 y} minus;
129
+
130
+ mod =
131
+ -- ${bnat x, bnat y}
132
+ <
133
+ -- if x >= y then (x mod y) = ((x-y mod 2y) mod y)
134
+ { { minus x, .y append_0 y } mod x, .y y } mod,
135
+ -- else, i.e., if x < y then, (x mod y) = x
136
+ .x
137
+ >
138
+ ;
139
+
140
+ align =
141
+ -- ${bnat x, bnat y}
142
+ {
143
+ .x remove_leading_zeros reverse _x,
144
+ .y remove_leading_zeros reverse _y,
145
+ bnat_0 x,
146
+ bnat_0 y
147
+ }
148
+ _align
149
+ ;
150
+
151
+ _align =
152
+ ${bnat _x, bnat _y, bnat x, bnat y}
153
+ <
154
+ {._x/_ if1, ._y/_ if2, {.x x, .y y} then} .then,
155
+ {._x </0, /_ _> _x, ._y </0, /_ _> _y, .x _0 x, .y _0 y} _align,
156
+ {._x/1 _x, ._y </0, /_ _> _y, .x _1 x, .y _0 y} _align,
157
+ {._x </0, /_ _> _x, ._y/1 _y, .x _0 x, .y _1 y} _align,
158
+ {._x/1 _x, ._y/1 _y, .x _1 x, .y _1 y} _align
159
+ >
160
+ ;
161
+
162
+ -- $compare = <{} lt, {} eq, {} gt >;
163
+ lt = {} |lt;
164
+ eq = {} |eq;
165
+ gt = {} |gt;
166
+
167
+ compare = align _compare;
168
+ _compare =
169
+ $pair -- ${bnat x, bnat y}
170
+ <
171
+ {.x/_ if1, .y/_ if2 } eq,
172
+ {.x/0 x, .y/0 y} _compare,
173
+ {.x/1 x, .y/1 y} _compare,
174
+ .x/0 lt,
175
+ gt
176
+ >
177
+ -- $compare
178
+ ;
179
+
180
+ zero? = remove_leading_zeros /_ bnat_0;
181
+ eq? = {compare /eq if, () then} .then;
182
+ gt? = {compare /gt if, () then} .then;
183
+ lt? = {compare /lt if, () then} .then;
184
+ ge? = < gt?, eq? >;
185
+ le? = < lt?, eq? >;
186
+
187
+ // gratest common divisor
188
+ gcd = <gt?, {.y x,.x y}> _gcd;
189
+
190
+ _gcd = <
191
+ { .y zero? if, .x then } .then,
192
+ { .y x, mod y } _gcd
193
+ >;
194
+
195
+ log2_floor =
196
+ -- ${bnat x, bnat step}
197
+ -- finds the largest k, such that 'step * 2^k <= x',
198
+ -- returns k+1, i.e., 0 if x < step, and k+1 otherwise.
199
+ {.x x, .step step, bnat_0 log} _log2_floor;
200
+
201
+ _log2_floor =
202
+ <
203
+ {{.step x, .x y} gt? if, .log then} .then,
204
+ {.x x, {.step x, bnat_0 y} gt? .x append_0 step, .log inc log} _log2_floor
205
+ >
206
+ ;
207
+
208
+ append_n_times_0 =
209
+ -- ${bnat x, bnat n}
210
+ <
211
+ {.n zero? if, .x then} .then,
212
+ {.x append_0 x, .n dec n} append_n_times_0
213
+ >
214
+ ;
215
+
216
+ div =
217
+ -- $pair -- ${bnat x, bnat y}
218
+ {.x x, .y step, bnat_0 res} _div
219
+ ;
220
+
221
+ _div =
222
+ {
223
+ .x x,
224
+ .step step,
225
+ .res res,
226
+ {.x x, .step step} log2_floor log
227
+ }
228
+ <
229
+ { .log zero? if, {.res div, .x rem} then } .then,
230
+ {
231
+ -- {2 x, .log [(),1] MINUS count} rep TIMES factor,
232
+ { bnat_1 x, .log dec n} append_n_times_0 factor,
233
+ () i
234
+ }
235
+ {
236
+ {.i.x x, {.i.step x, .factor y} times y} minus x,
237
+ .i.step step,
238
+ {.i.res x, .factor y} plus res
239
+ } _div
240
+ >
241
+ ;
242
+
243
+ lcm =
244
+ -- $pair -- ${bnat x, bnat y}
245
+ {times x, gcd y}
246
+ div .div
247
+ ;
248
+
249
+ half =
250
+ reverse
251
+ <
252
+ {/0 reverse div, bnat_0 rem},
253
+ {/1 reverse div, bnat_1 rem},
254
+ {bnat_0 div, bnat_0 rem}
255
+ >
256
+ ;
257
+
258
+ pow =
259
+ -- ${ bnat base, bnat exponent }
260
+ <
261
+ .exponent zero? bnat_1,
262
+ {.base base, .exponent half half}
263
+ {.base base, .half.rem extra, {.base base, .half.div exponent} pow {()x,()y} times pow}
264
+ <
265
+ {.extra zero? if, .pow then} .then,
266
+ {.base x, .pow y} times
267
+ >
268
+ >
269
+ ;
270
+
271
+ sqrt =
272
+ <
273
+ { () x, {bnat_1 min, () max} range } _sqrt
274
+ >
275
+ ;
276
+
277
+ _sqrt =
278
+ { () input, {.range.max x, .range.min y} plus half .div middle }
279
+ <
280
+ { .input.range.min x, .middle y} eq? .x,
281
+ { () aux, {.input.x x, .middle y} div .div middle }
282
+ {
283
+ .aux.input.x x,
284
+ { .aux.middle x, .middle y } < gt?, {.x y, .y x} > {.y min, .x max} range
285
+ } _sqrt
286
+ >
287
+ ;
288
+
289
+ --------------------
290
+
291
+ _log! lcm
@@ -0,0 +1,67 @@
1
+ $ bit = < {} 0, {} 1 >;
2
+
3
+ $ byte = {
4
+ bit 0, bit 1, bit 2, bit 3,
5
+ bit 4, bit 5, bit 6, bit 7
6
+ };
7
+
8
+ $ bytes = < {} nil, {byte car, bytes cdr} cons >;
9
+
10
+ $ ascii = { bit 6, bit 5, bit 4, bit 3, bit 2, bit 1, bit 0 };
11
+
12
+ $ u0080_07FF = { bit 10, bit 9, bit 8, bit 7, bit 6, bit 5, bit 4, bit 3, bit 2, bit 1, bit 0 };
13
+
14
+ $ bmp_hi_common = <
15
+ { bit 2, bit 1, bit 0 } h08_0F,
16
+ { bit 6, bit 5, bit 4, bit 3, bit 2, bit 1, bit 0 } h10_7F,
17
+ { bit 6, bit 5, bit 4, bit 3, bit 2, bit 1, bit 0 } h80_CF,
18
+ { bit 2, bit 1, bit 0 } hD0_D7,
19
+ {} hF9,
20
+ { bit 0 } hFA_FB,
21
+ { bit 0 } hFC_FD,
22
+ { bit 0 } hFE_FF
23
+ >;
24
+
25
+ $ bmp_hi_private_use = <
26
+ { bit 3, bit 2, bit 1, bit 0 } hE0_EF,
27
+ { bit 2, bit 1, bit 0 } hF0_F7,
28
+ {} hF8
29
+ >;
30
+
31
+ $ plane_02_10 = <
32
+ { bit 0 } p02_03,
33
+ { bit 1, bit 0 } p04_07,
34
+ { bit 2, bit 1, bit 0 } p08_0F,
35
+ {} p10
36
+ >;
37
+
38
+ $ unicode = <
39
+ ascii ascii,
40
+ u0080_07FF plane0,
41
+ { bmp_hi_common hi, byte lo } bmp_common,
42
+ { bmp_hi_private_use hi, byte lo } bmp_private_use,
43
+ { byte mid, byte lo } supplementary_plane1,
44
+ { plane_02_10 plane, byte mid, byte lo } supplementary_planes2_16
45
+ >;
46
+
47
+ $ string = < {} nil, { unicode car, string cdr } cons >;
48
+
49
+ 0 = {}|0;
50
+ 1 = {}|1;
51
+
52
+ _ = {1 6, 0 5, 1 4, 1 3, 1 2, 1 1, 1 0};
53
+ map = $ unicode <
54
+ /ascii <
55
+ {.6/1 6, .5/1 5, .4/1 4, .3/1 3, .2/1 2, .1/1 1, .0/1 0 } _,
56
+ {{.6/0 6, .5/1 5} if, () then} .then,
57
+ {.6/1 if, () then} .then
58
+ >,
59
+ _
60
+ > | ascii;
61
+
62
+ filter = $string <
63
+ /cons {.car map car, .cdr filter cdr} | cons,
64
+ ()
65
+ >;
66
+
67
+ filter
@@ -0,0 +1,44 @@
1
+ $ code = < fields product, fields union, chars ref >;
2
+ $ fields = < {} nil, { chars car, fields cdr } cons >;
3
+ $ chars = <
4
+ chars" ", chars"!", chars'"', chars"#", chars"$", chars"%", chars"&", chars"'",
5
+ chars"(", chars")", chars"*", chars"+", chars",", chars"-", chars".", chars"/",
6
+ chars"0", chars"1", chars"2", chars"3", chars"4", chars"5", chars"6", chars"7",
7
+ chars"8", chars"9", chars":", chars";", chars"<", chars"=", chars">", chars"?",
8
+ chars"@", chars"A", chars"B", chars"C", chars"D", chars"E", chars"F", chars"G",
9
+ chars"H", chars"I", chars"J", chars"K", chars"L", chars"M", chars"N", chars"O",
10
+ chars"P", chars"Q", chars"R", chars"S", chars"T", chars"U", chars"V", chars"W",
11
+ chars"X", chars"Y", chars"Z", chars"[", chars"\\", chars"]", chars"^", chars"_",
12
+ chars"`", chars"a", chars"b", chars"c", chars"d", chars"e", chars"f", chars"g",
13
+ chars"h", chars"i", chars"j", chars"k", chars"l", chars"m", chars"n", chars"o",
14
+ chars"p", chars"q", chars"r", chars"s", chars"t", chars"u", chars"v", chars"w",
15
+ chars"x", chars"y", chars"z", chars"{", chars"|", chars"}", chars"~",
16
+ {} end
17
+ >;
18
+
19
+ $ char = <
20
+ {}" ", {}"!", {}'"', {}"#", {}"$", {}"%", {}"&", {}"'",
21
+ {}"(", {}")", {}"*", {}"+", {}",", {}"-", {}".", {}"/",
22
+ {}"0", {}"1", {}"2", {}"3", {}"4", {}"5", {}"6", {}"7",
23
+ {}"8", {}"9", {}":", {}";", {}"<", {}"=", {}">", {}"?",
24
+ {}"@", {}"A", {}"B", {}"C", {}"D", {}"E", {}"F", {}"G",
25
+ {}"H", {}"I", {}"J", {}"K", {}"L", {}"M", {}"N", {}"O",
26
+ {}"P", {}"Q", {}"R", {}"S", {}"T", {}"U", {}"V", {}"W",
27
+ {}"X", {}"Y", {}"Z", {}"[", {}"\\", {}"]", {}"^", {}"_",
28
+ {}"`", {}"a", {}"b", {}"c", {}"d", {}"e", {}"f", {}"g",
29
+ {}"h", {}"i", {}"j", {}"k", {}"l", {}"m", {}"n", {}"o",
30
+ {}"p", {}"q", {}"r", {}"s", {}"t", {}"u", {}"v", {}"w",
31
+ {}"x", {}"y", {}"z", {}"{", {}"|", {}"}", {}"~"
32
+ >;
33
+
34
+
35
+ nil = {}|nil;
36
+ _ = {}|end $chars;
37
+
38
+ KL = _|L|K $chars;
39
+
40
+ bool =
41
+ { KL car, { KL car, nil cdr } | cons cdr } | cons | union
42
+ $code;
43
+
44
+ bool
package/Examples/esc.k ADDED
@@ -0,0 +1,9 @@
1
+ $ utf8 = <{} '
2
+ \u2601\u2600\u2602
3
+ ',
4
+ {} '✅',
5
+ {} 'tag \u2705',
6
+ {} "strange and long tag name ✅"
7
+ >;
8
+
9
+ ()