@fraczak/k 8.0.0 → 8.2.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.
@@ -581,74 +581,33 @@ assert.equal(output[0], "output limit disabled");
581
581
  output = await evaluateInput(":limit", limitState);
582
582
  assert.equal(output[0], "Current output limit: none");
583
583
 
584
- // Verify Examples/djson.k (json?, dot, has_key?, keys, values, remove, put)
585
- const jsonKState = createState({ engine: "wasm" });
586
- output = await evaluateInput(":load Examples/djson.k", jsonKState);
584
+ // Verify Examples/djson.k (json?, get, nth, set, del)
585
+ const djsonKState = createState({ engine: "wasm" });
586
+ output = await evaluateInput(":load Examples/djson.k", djsonKState);
587
587
  assert.equal(output[0], "loaded Examples/djson.k");
588
- await evaluateInput(":codec load codecs/djson.mjs", jsonKState);
588
+ await evaluateInput(":codec load codecs/djson.mjs", djsonKState);
589
589
  await evaluateInput(
590
- ':input djson.mjs {"name":"Alice","age":30,"active":true}',
591
- jsonKState
590
+ ':input djson.mjs ["age", {"name":"Alice","age":30}]',
591
+ djsonKState
592
592
  );
593
593
  output = await evaluateInput(
594
- "{/object cdr car .key key, /object object} dot",
595
- jsonKState
594
+ "{{0 index, () json} nth /string key, " +
595
+ "{1 index, () json} nth json} get",
596
+ djsonKState
596
597
  );
597
598
  assert.match(output[0], /djson\.mjs: 30/);
598
599
 
599
600
  await evaluateInput(
600
- ':input djson.mjs {"name":"Alice","age":30}',
601
- jsonKState
601
+ ':input djson.mjs ["age", {"name":"Alice","age":30}]',
602
+ djsonKState
602
603
  );
603
604
  output = await evaluateInput(
604
- "{/object car .key key, /object object} remove object",
605
- jsonKState
605
+ "{{0 index, () json} nth /string key, " +
606
+ "{1 index, () json} nth json} del",
607
+ djsonKState
606
608
  );
607
609
  assert.match(output[0], /djson\.mjs: \{"name":"Alice"\}/);
608
610
 
609
- // Verify Examples/djson-btrie.k (json?, dot, nth, put, remove)
610
- const btrieState = createState({ engine: "wasm" });
611
- output = await evaluateInput(":load Examples/djson-btrie.k", btrieState);
612
- assert.equal(output[0], "loaded Examples/djson-btrie.k");
613
- await evaluateInput(":codec load codecs/djson-btrie.mjs", btrieState);
614
- await evaluateInput(
615
- ':input djson-btrie.mjs ["age", {"name":"Alice","age":30}]',
616
- btrieState
617
- );
618
- output = await evaluateInput(
619
- "{{0 index, /list list} nth /string key, " +
620
- "{1 index, /list list} nth /object object} dot",
621
- btrieState
622
- );
623
- assert.match(output[0], /djson-btrie\.mjs: 30/);
624
-
625
- await evaluateInput(
626
- ':input djson-btrie.mjs ["age", {"name":"Alice","age":30}]',
627
- btrieState
628
- );
629
- output = await evaluateInput(
630
- "{{0 index, /list list} nth /string key, " +
631
- "{1 index, /list list} nth /object object} remove object",
632
- btrieState
633
- );
634
- assert.match(output[0], /djson-btrie\.mjs: \{"name":"Alice"\}/);
635
-
636
- // Verify Examples/djson-ptrie.k (json?, dot, nth, list_size, obj_size)
637
- const ptrieState = createState({ engine: "wasm" });
638
- output = await evaluateInput(":load Examples/djson-ptrie.k", ptrieState);
639
- assert.equal(output[0], "loaded Examples/djson-ptrie.k");
640
- await evaluateInput(":codec load codecs/djson-ptrie.mjs", ptrieState);
641
- await evaluateInput(
642
- ':input djson-ptrie.mjs ["age", {"name":"Alice","age":30}]',
643
- ptrieState
644
- );
645
- output = await evaluateInput(
646
- "{{0 index, /list list} nth /string key, " +
647
- "{1 index, /list list} nth /object object} dot",
648
- ptrieState
649
- );
650
- assert.match(output[0], /djson-ptrie\.mjs: 30/);
651
-
652
611
  // Verify named pattern persistence, listing, display, and tab completion
653
612
  const patState = createState({ engine: "wasm" });
654
613
 
@@ -668,19 +627,19 @@ assert.equal(output[0], "bit[]");
668
627
  output = await evaluateInput(":pattern bit", patState);
669
628
  assert.equal(
670
629
  output[0],
671
- "bit[] = <{} 0, {} 1>; -- " +
630
+ "bit[] = <{}=X0 0, X0 1>; -- " +
672
631
  "@MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg"
673
632
  );
674
633
  output = await evaluateInput(":pattern bit[]", patState);
675
634
  assert.equal(
676
635
  output[0],
677
- "bit[] = <{} 0, {} 1>; -- " +
636
+ "bit[] = <{}=X0 0, X0 1>; -- " +
678
637
  "@MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg"
679
638
  );
680
639
  output = await evaluateInput(":pat bit", patState);
681
640
  assert.equal(
682
641
  output[0],
683
- "bit[] = <{} 0, {} 1>; -- " +
642
+ "bit[] = <{}=X0 0, X0 1>; -- " +
684
643
  "@MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg"
685
644
  );
686
645
  output = await evaluateInput(
@@ -689,10 +648,25 @@ output = await evaluateInput(
689
648
  );
690
649
  assert.equal(
691
650
  output[0],
692
- "bit[] = <{} 0, {} 1>; -- " +
651
+ "bit[] = <{}=X0 0, X0 1>; -- " +
693
652
  "@MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg"
694
653
  );
695
654
 
655
+ output = await evaluateInput(
656
+ "byte[] = { " +
657
+ "bit[] b0, bit[] b1, bit[] b2, bit[] b3, " +
658
+ "bit[] b4, bit[] b5, bit[] b6, bit[] b7 " +
659
+ "};",
660
+ patState
661
+ );
662
+ output = await evaluateInput(":pattern byte", patState);
663
+ assert.equal(
664
+ output[0],
665
+ "byte[] = {<{}=X1 0, X1 1>=X0 b0, X0 b1, X0 b2, X0 b3, " +
666
+ "X0 b4, X0 b5, X0 b6, X0 b7}; -- " +
667
+ "@4ukq2gpiEBFa7WXxcfRm8pfdr2kgYLTTnwsmLHeMd9i5"
668
+ );
669
+
696
670
  await assert.rejects(
697
671
  () => evaluateInput(":pattern unknown", patState),
698
672
  /Unknown pattern 'unknown'/
@@ -729,7 +703,7 @@ output = await evaluateInput(
729
703
  output = await evaluateInput(":pattern pair", patState);
730
704
  assert.equal(
731
705
  output[0],
732
- "pair[A, B] = {A first, B second}; -- " +
706
+ "pair[X0, X1] = {X0 first, X1 second}; -- " +
733
707
  "@KqTBNkznjWWwync2Mm94XjCez63neJBJtgYcJJEE5kEm"
734
708
  );
735
709
 
@@ -743,7 +717,7 @@ assert(output[0].includes("pair[A, B]"));
743
717
  output = await evaluateInput(":pattern list", patState);
744
718
  assert.equal(
745
719
  output[0],
746
- "list[X] = <{} nil, {X car, Y cdr} cons>=Y; -- " +
720
+ "list[X1] = <{X1 car, X0 cdr} cons, {} nil>=X0; -- " +
747
721
  "@JagVexzgseRr4rVZRmVpsMbr2AKXDjzZnz6Qnf8a9SNe"
748
722
  );
749
723
 
@@ -765,9 +739,31 @@ assert(output[0].includes("list[X]"));
765
739
  output = await evaluateInput(":pattern bit", freshState);
766
740
  assert.equal(
767
741
  output[0],
768
- "bit[] = <{} 0, {} 1>; -- " +
742
+ "bit[] = <{}=X0 0, X0 1>; -- " +
769
743
  "@MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg"
770
744
  );
745
+ output = await evaluateInput(":pattern pair", freshState);
746
+ assert.equal(
747
+ output[0],
748
+ "pair[X0, X1] = {X0 first, X1 second}; -- " +
749
+ "@KqTBNkznjWWwync2Mm94XjCez63neJBJtgYcJJEE5kEm"
750
+ );
751
+ output = await evaluateInput(":pattern list", freshState);
752
+ assert.equal(
753
+ output[0],
754
+ "list[X1] = <{X1 car, X0 cdr} cons, {} nil>=X0; -- " +
755
+ "@JagVexzgseRr4rVZRmVpsMbr2AKXDjzZnz6Qnf8a9SNe"
756
+ );
757
+ output = await evaluateInput(
758
+ "same_pair[T] = { T first, T second };",
759
+ freshState
760
+ );
761
+ output = await evaluateInput(":pattern same_pair", freshState);
762
+ assert.equal(
763
+ output[0],
764
+ "same_pair[X0] = {X0 first, X0 second}; -- " +
765
+ "@TyjJDJuYAbp56mytRMzu2HMTfQq7rKLcGsaQtFG28yhW"
766
+ );
771
767
  output = await evaluateInput("{} | nil ? list[bit[]]", freshState);
772
768
  assert.match(output[0], /\{\}\|nil/);
773
769
 
@@ -1,414 +0,0 @@
1
- -- Examples/djson-btrie.k: Binary Bit-Trie JSON schema and helpers
2
- -- Works with codecs/djson-btrie.mjs and codecs/utf8.mjs.
3
-
4
- ---------------------------------------------------------------
5
- -- §1. Named Pattern Definitions & Component Schemas
6
- ---------------------------------------------------------------
7
-
8
- bit[] = < {} 0, {} 1 >;
9
-
10
- sign[] = < {} "+", {} "-" >;
11
-
12
- exponent[] = {
13
- bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4,
14
- bit[] 5, bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10
15
- };
16
-
17
- fraction[] = {
18
- bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4, bit[] 5,
19
- bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10, bit[] 11,
20
- bit[] 12, bit[] 13, bit[] 14, bit[] 15, bit[] 16, bit[] 17,
21
- bit[] 18, bit[] 19, bit[] 20, bit[] 21, bit[] 22, bit[] 23,
22
- bit[] 24, bit[] 25, bit[] 26, bit[] 27, bit[] 28, bit[] 29,
23
- bit[] 30, bit[] 31, bit[] 32, bit[] 33, bit[] 34, bit[] 35,
24
- bit[] 36, bit[] 37, bit[] 38, bit[] 39, bit[] 40, bit[] 41,
25
- bit[] 42, bit[] 43, bit[] 44, bit[] 45, bit[] 46, bit[] 47,
26
- bit[] 48, bit[] 49, bit[] 50, bit[] 51
27
- };
28
-
29
- number[] = {
30
- sign[] sign,
31
- exponent[] exponent,
32
- fraction[] fraction
33
- };
34
-
35
- byte[] = {
36
- bit[] 0, bit[] 1, bit[] 2, bit[] 3,
37
- bit[] 4, bit[] 5, bit[] 6, bit[] 7
38
- };
39
-
40
- ascii[] = {
41
- bit[] 0, bit[] 1, bit[] 2, bit[] 3,
42
- bit[] 4, bit[] 5, bit[] 6
43
- };
44
-
45
- bmp_hi_common[] = <
46
- { bit[] 0, bit[] 1, bit[] 2 } h08_0F,
47
- ascii[] h10_7F,
48
- ascii[] h80_CF,
49
- { bit[] 0, bit[] 1, bit[] 2 } hD0_D7,
50
- {} hF9,
51
- { bit[] 0 } hFA_FB,
52
- { bit[] 0 } hFC_FD,
53
- { bit[] 0 } hFE_FF
54
- >;
55
-
56
- bmp_hi_private_use[] = <
57
- { bit[] 0, bit[] 1, bit[] 2, bit[] 3 } hE0_EF,
58
- { bit[] 0, bit[] 1, bit[] 2 } hF0_F7,
59
- {} hF8
60
- >;
61
-
62
- plane0[] = {
63
- bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4,
64
- bit[] 5, bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10
65
- };
66
-
67
- plane_02_10[] = <
68
- { bit[] 0 } p02_03,
69
- { bit[] 0, bit[] 1 } p04_07,
70
- { bit[] 0, bit[] 1, bit[] 2 } p08_0F,
71
- {} p10
72
- >;
73
-
74
- unicode[] = <
75
- ascii[] ascii,
76
- { bmp_hi_common[] hi, byte[] lo } bmp_common,
77
- { bmp_hi_private_use[] hi, byte[] lo } bmp_private_use,
78
- plane0[] plane0,
79
- { byte[] mid, byte[] lo } supplementary_plane1,
80
- { plane_02_10[] plane, byte[] mid, byte[] lo } supplementary_planes2_16
81
- >;
82
-
83
- string[] = < {} nil, { unicode[] car, S cdr } cons > = S;
84
-
85
- bool[] = < {} true, {} false >;
86
-
87
- opt[V] = < {} nil, V val >;
88
-
89
- btrie[V] = <
90
- {} nil,
91
- {
92
- opt[V] val,
93
- T 0,
94
- T 1
95
- } trie
96
- > = T;
97
-
98
- djson_btrie[] = <
99
- number[] number,
100
- string[] string,
101
- bool[] bool,
102
- {} null,
103
- btrie[J] list,
104
- btrie[J] object
105
- > = J;
106
-
107
- json? = ? djson_btrie[];
108
-
109
- ---------------------------------------------------------------
110
- -- §2. Constructors and Projections
111
- ---------------------------------------------------------------
112
-
113
- bits? = ?< {} _, B 0, B 1 > = B;
114
- str_list? = ?< {} nil, { C car, S cdr } cons > = S;
115
- trie? = ? btrie[V];
116
-
117
- 0b = {} | _;
118
- 0 = 0b | 0;
119
- 1 = 0b | 1;
120
-
121
- nil = {} | nil;
122
- null = {} | null json?;
123
- true = {} | true | bool json?;
124
- false = {} | false | bool json?;
125
- number = | number json?;
126
- string = | string json?;
127
- bool = | bool json?;
128
- list = | list json?;
129
- object = | object json?;
130
-
131
- ---------------------------------------------------------------
132
- -- §3. Direct Binary Bit-Trie Traversal (Zero Equality Checks)
133
- ---------------------------------------------------------------
134
-
135
- -- Step one bit (0 or 1) in a binary trie
136
- step_0 = /trie .0;
137
- step_1 = /trie .1;
138
-
139
- step_bit = <
140
- { .b /0 if, .t step_0 then } .then,
141
- { .b /1 if, .t step_1 then } .then
142
- >;
143
-
144
- -- Step N bits from MSB (.N-1) down to LSB (.0)
145
- step_bits1 = { .x .0 b, .t t } step_bit;
146
- step_bits2 = { .x x, { .x .1 b, .t t } step_bit t } step_bits1;
147
- step_bits3 = { .x x, { .x .2 b, .t t } step_bit t } step_bits2;
148
- step_bits4 = { .x x, { .x .3 b, .t t } step_bit t } step_bits3;
149
- step_bits5 = { .x x, { .x .4 b, .t t } step_bit t } step_bits4;
150
- step_bits6 = { .x x, { .x .5 b, .t t } step_bit t } step_bits5;
151
- step_bits7 = { .x x, { .x .6 b, .t t } step_bit t } step_bits6;
152
- step_bits8 = { .x x, { .x .7 b, .t t } step_bit t } step_bits7;
153
- step_bits9 = { .x x, { .x .8 b, .t t } step_bit t } step_bits8;
154
- step_bits10 = { .x x, { .x .9 b, .t t } step_bit t } step_bits9;
155
- step_bits11 = { .x x, { .x .10 b, .t t } step_bit t } step_bits10;
156
-
157
- step_bmp_common_hi = <
158
- { .hi /h08_0F x, .t step_0 step_0 step_0 t } step_bits3,
159
- { .hi /h10_7F x, .t step_0 step_0 step_1 t } step_bits7,
160
- { .hi /h80_CF x, .t step_0 step_1 step_0 t } step_bits7,
161
- { .hi /hD0_D7 x, .t step_0 step_1 step_1 t } step_bits3,
162
- { .hi /hF9 if, .t step_1 step_0 step_0 then } .then,
163
- { .hi /hFA_FB x, .t step_1 step_0 step_1 t } step_bits1,
164
- { .hi /hFC_FD x, .t step_1 step_1 step_0 t } step_bits1,
165
- { .hi /hFE_FF x, .t step_1 step_1 step_1 t } step_bits1
166
- >;
167
-
168
- step_bmp_priv_hi = <
169
- { .hi /hE0_EF x, .t step_0 t } step_bits4,
170
- { .hi /hF0_F7 x, .t step_1 step_0 t } step_bits3,
171
- { .hi /hF8 if, .t step_1 step_1 then } .then
172
- >;
173
-
174
- step_plane2_16 = <
175
- { .p /p02_03 x, .t step_0 step_0 t } step_bits1,
176
- { .p /p04_07 x, .t step_0 step_1 t } step_bits2,
177
- { .p /p08_0F x, .t step_1 step_0 t } step_bits3,
178
- { .p /p10 if, .t step_1 step_1 then } .then
179
- >;
180
-
181
- -- Advance trie by one Unicode character: { char, t } -> t
182
- step_char = <
183
- { .char /ascii x, .t step_0 t } step_bits7,
184
- { .char /plane0 x, .t step_1 step_0 t } step_bits11,
185
- {
186
- .char /bmp_common .lo x,
187
- { .char /bmp_common .hi hi, .t step_1 step_1 step_0 t }
188
- step_bmp_common_hi t
189
- } step_bits8,
190
- {
191
- .char /bmp_private_use .lo x,
192
- { .char /bmp_private_use .hi hi, .t step_1 step_1 step_1 step_0 t }
193
- step_bmp_priv_hi t
194
- } step_bits8,
195
- {
196
- .char /supplementary_plane1 .lo x,
197
- {
198
- .char /supplementary_plane1 .mid x,
199
- .t step_1 step_1 step_1 step_1 step_0 t
200
- } step_bits8 t
201
- } step_bits8,
202
- {
203
- .char /supplementary_planes2_16 .lo x,
204
- {
205
- .char /supplementary_planes2_16 .mid x,
206
- {
207
- .char /supplementary_planes2_16 .plane p,
208
- .t step_1 step_1 step_1 step_1 step_1 t
209
- } step_plane2_16 t
210
- } step_bits8 t
211
- } step_bits8
212
- >;
213
-
214
- ---------------------------------------------------------------
215
- -- §4. Object & List Lookup Relations (dot, nth, has_key?)
216
- ---------------------------------------------------------------
217
-
218
- -- Lookup string key in object trie: { key, object } -> value
219
- dot = ?{ K key, T object } { .key str_list? key, .object trie? t } <
220
- { .key /nil if, .t /trie .val /val then } .then,
221
- {
222
- .key /cons .cdr key,
223
- { .key /cons .car char, .t t } step_char object
224
- } dot
225
- >;
226
-
227
- -- Filter: succeeds as identity if object contains key
228
- has_key? = ?{ K key, O object } { dot if, () then } .then;
229
-
230
- -- Lookup bits? index in list trie: { index, list } -> value
231
- nth = ?{ K index, T list } { .index bits? index, .list trie? t } <
232
- { .index /_ if, .t /trie .val /val then } .then,
233
- { .index /0 index, .t /trie .0 list } nth,
234
- { .index /1 index, .t /trie .1 list } nth
235
- >;
236
-
237
- ---------------------------------------------------------------
238
- -- §5. Trie Update & Removal (put, remove, put_nth, remove_nth)
239
- ---------------------------------------------------------------
240
-
241
- push_bit = ?{ < {} 0, {} 1 > b, < {} _, BITS 0, BITS 1 >=BITS acc } <
242
- { .b /0 if, .acc |0 then } .then,
243
- { .b /1 if, .acc |1 then } .then
244
- > bits?;
245
-
246
- push_bits1 = { .x .0 b, .acc acc } push_bit;
247
- push_bits2 = { .x .1 b, { .x x, .acc acc } push_bits1 acc } push_bit;
248
- push_bits3 = { .x .2 b, { .x x, .acc acc } push_bits2 acc } push_bit;
249
- push_bits4 = { .x .3 b, { .x x, .acc acc } push_bits3 acc } push_bit;
250
- push_bits5 = { .x .4 b, { .x x, .acc acc } push_bits4 acc } push_bit;
251
- push_bits6 = { .x .5 b, { .x x, .acc acc } push_bits5 acc } push_bit;
252
- push_bits7 = { .x .6 b, { .x x, .acc acc } push_bits6 acc } push_bit;
253
- push_bits8 = { .x .7 b, { .x x, .acc acc } push_bits7 acc } push_bit;
254
- push_bits9 = { .x .8 b, { .x x, .acc acc } push_bits8 acc } push_bit;
255
- push_bits10 = { .x .9 b, { .x x, .acc acc } push_bits9 acc } push_bit;
256
- push_bits11 = { .x .10 b, { .x x, .acc acc } push_bits10 acc } push_bit;
257
-
258
- push_bmp_common_hi = <
259
- { .hi /h08_0F x, .acc acc } push_bits3 |0 |0 |0,
260
- { .hi /h10_7F x, .acc acc } push_bits7 |1 |0 |0,
261
- { .hi /h80_CF x, .acc acc } push_bits7 |0 |1 |0,
262
- { .hi /hD0_D7 x, .acc acc } push_bits3 |1 |1 |0,
263
- { .hi /hF9 if, .acc |0 |0 |1 then } .then,
264
- { .hi /hFA_FB x, .acc acc } push_bits1 |1 |0 |1,
265
- { .hi /hFC_FD x, .acc acc } push_bits1 |0 |1 |1,
266
- { .hi /hFE_FF x, .acc acc } push_bits1 |1 |1 |1
267
- > bits?;
268
-
269
- push_bmp_priv_hi = <
270
- { .hi /hE0_EF x, .acc acc } push_bits4 |0,
271
- { .hi /hF0_F7 x, .acc acc } push_bits3 |0 |1,
272
- { .hi /hF8 if, .acc |1 |1 then } .then
273
- > bits?;
274
-
275
- push_plane2_16 = <
276
- { .p /p02_03 x, .acc acc } push_bits1 |0 |0,
277
- { .p /p04_07 x, .acc acc } push_bits2 |1 |0,
278
- { .p /p08_0F x, .acc acc } push_bits3 |0 |1,
279
- { .p /p10 if, .acc |1 |1 then } .then
280
- > bits?;
281
-
282
- prepend_char_bits = <
283
- { .char /ascii x, .acc acc } push_bits7 |0,
284
- { .char /plane0 x, .acc acc } push_bits11 |0 |1,
285
- {
286
- .char /bmp_common .hi hi,
287
- { .char /bmp_common .lo x, .acc acc } push_bits8 acc
288
- } push_bmp_common_hi |0 |1 |1,
289
- {
290
- .char /bmp_private_use .hi hi,
291
- { .char /bmp_private_use .lo x, .acc acc } push_bits8 acc
292
- } push_bmp_priv_hi |0 |1 |1 |1,
293
- {
294
- .char /supplementary_plane1 .mid x,
295
- { .char /supplementary_plane1 .lo x, .acc acc } push_bits8 acc
296
- } push_bits8 |0 |1 |1 |1 |1,
297
- {
298
- .char /supplementary_planes2_16 .plane p,
299
- {
300
- .char /supplementary_planes2_16 .mid x,
301
- { .char /supplementary_planes2_16 .lo x, .acc acc } push_bits8 acc
302
- } push_bits8 acc
303
- } push_plane2_16 |1 |1 |1 |1 |1
304
- > bits?;
305
-
306
- str_to_bits = str_list? <
307
- /nil 0b,
308
- /cons { .car char, .cdr str_to_bits acc } prepend_char_bits
309
- > bits?;
310
-
311
- ensure_trie = trie? < /nil { nil val, nil 0, nil 1 }, /trie () >;
312
-
313
- -- Insert or replace { index, val, list } -> list
314
- put_nth = ?{ K index, V val, T list } {
315
- .index bits? index,
316
- .val val,
317
- .list trie? ensure_trie t
318
- } <
319
- {
320
- .index /_ if,
321
- { .val |val val, .t .0 0, .t .1 1 } |trie then
322
- } .then,
323
- {
324
- .index /0 if,
325
- {
326
- .t .val val,
327
- { .index /0 index, .val val, .t .0 list } put_nth 0,
328
- .t .1 1
329
- } |trie then
330
- } .then,
331
- {
332
- .index /1 if,
333
- {
334
- .t .val val,
335
- .t .0 0,
336
- { .index /1 index, .val val, .t .1 list } put_nth 1
337
- } |trie then
338
- } .then
339
- > trie?;
340
-
341
- -- Remove entry at bits? index from trie: { index, list } -> list
342
- remove_nth = ?{ K index, T list } { .index bits? index, .list trie? t } <
343
- { .t /nil if, nil then } .then,
344
- {
345
- .index /_ if,
346
- { nil val, .t /trie .0 0, .t /trie .1 1 } |trie then
347
- } .then,
348
- {
349
- .index /0 if,
350
- {
351
- .t /trie .val val,
352
- { .index /0 index, .t /trie .0 list } remove_nth 0,
353
- .t /trie .1 1
354
- } |trie then
355
- } .then,
356
- {
357
- .index /1 if,
358
- {
359
- .t /trie .val val,
360
- .t /trie .0 0,
361
- { .index /1 index, .t /trie .1 list } remove_nth 1
362
- } |trie then
363
- } .then
364
- > trie?;
365
-
366
- -- Insert or replace { key, val, object } -> object
367
- put = ?{ K key, V val, O object } {
368
- .key str_to_bits index,
369
- .val val,
370
- .object list
371
- } put_nth;
372
-
373
- -- Remove key from object trie: { key, object } -> object
374
- remove = ?{ K key, O object } {
375
- .key str_to_bits index,
376
- .object list
377
- } remove_nth;
378
-
379
- -- Count elements in list or object trie: trie -> bits?
380
- _inv = ?{ < {} _, B 0, B 1 >=B x, B r } <
381
- { .x /_ if, .r then } .then,
382
- { .x /0 x, .r |0 r } _inv,
383
- { .x /1 x, .r |1 r } _inv
384
- > bits?;
385
- _inc = ?{ < {} _, B 0, B 1 >=B x, B y } <
386
- { .x /_ if, .y |1 then } .then,
387
- { .x /0 x, .y |1 r } _inv,
388
- { .x /1 x, .y |0 y } _inc
389
- > bits?;
390
- inc_nat = { { () x, 0b r } _inv x, 0b y } _inc;
391
-
392
- _trie_size = ?{
393
- < {} nil, { < {} nil, V val > val, T 0, T 1 } trie >=T t,
394
- < {} _, B 0, B 1 >=B acc
395
- } <
396
- { .t /nil if, .acc then } .then,
397
- {
398
- .t /trie .val /nil if,
399
- {
400
- .t /trie .1 t,
401
- { .t /trie .0 t, .acc acc } _trie_size acc
402
- } _trie_size then
403
- } .then,
404
- {
405
- .t /trie .val /val if,
406
- {
407
- .t /trie .1 t,
408
- { .t /trie .0 t, .acc inc_nat acc } _trie_size acc
409
- } _trie_size then
410
- } .then
411
- > bits?;
412
-
413
- list_size = { trie? t, 0 acc } _trie_size;
414
- obj_size = { trie? t, 0 acc } _trie_size;